2007-03-25 15:24:47 +02:00
|
|
|
#include <stdio.h>
|
2007-03-24 19:49:30 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2007-03-25 15:24:47 +02:00
|
|
|
#include <mysql/mysql.h>
|
|
|
|
|
|
|
|
#include "configfile.h"
|
2007-03-24 19:49:30 +01:00
|
|
|
#include "conntrack.h"
|
|
|
|
#include "database.h"
|
|
|
|
#include "hashtable.h"
|
|
|
|
#include "logging.h"
|
|
|
|
|
2007-03-25 15:24:47 +02:00
|
|
|
static MYSQL *dbh;
|
|
|
|
|
2007-03-24 19:49:30 +01:00
|
|
|
static void purge_hash_cb(struct hash_entry *entry, void *privdata)
|
|
|
|
{
|
2007-03-25 16:24:27 +02:00
|
|
|
int long *now = (long *)privdata;
|
2007-03-25 15:24:47 +02:00
|
|
|
char query[256];
|
2007-03-24 19:49:30 +01:00
|
|
|
|
2007-03-25 15:24:47 +02:00
|
|
|
int len = snprintf(query, sizeof(query),
|
2007-03-25 16:24:27 +02:00
|
|
|
"INSERT INTO stats SET timestamp='%lu', srcip='%u', proto='%u', dport='%u', srcbytes='%llu', dstbytes='%llu', count='%u'",
|
|
|
|
*now, ntohl(entry->src_ip), entry->protonum, ntohs(entry->dst_port),
|
2007-03-25 15:24:47 +02:00
|
|
|
entry->src_bytes, entry->dst_bytes, entry->count);
|
2007-03-24 19:49:30 +01:00
|
|
|
|
2007-03-25 15:24:47 +02:00
|
|
|
if (mysql_real_query(dbh, query, len +1) != 0)
|
|
|
|
log_print(LOG_WARN, "purge_hash_cb: mysql_real_query(): %s", mysql_error(dbh));
|
2007-03-24 19:49:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int database_analyse(void)
|
|
|
|
{
|
2007-03-25 15:24:47 +02:00
|
|
|
struct hash_table *hash = conntrack_get_hash();
|
|
|
|
|
|
|
|
if (mysql_ping(dbh) != 0) {
|
|
|
|
log_print(LOG_WARN, "database_analyse: mysql_ping(): %s", mysql_error(dbh));
|
|
|
|
purge_hash(hash, NULL, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-25 16:24:27 +02:00
|
|
|
long now = time(NULL);
|
|
|
|
purge_hash(hash, purge_hash_cb, (void *)&now);
|
2007-03-24 19:49:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int database_init(void)
|
|
|
|
{
|
2007-03-25 15:24:47 +02:00
|
|
|
dbh = mysql_init(NULL);
|
|
|
|
if (dbh == NULL) {
|
|
|
|
log_print(LOG_ERROR, "database_init: mysql_init(): %s", mysql_error(dbh));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *hostname = config_get_string("mysql", "hostname", NULL);
|
|
|
|
char *username = config_get_string("mysql", "username", NULL);
|
|
|
|
char *password = config_get_string("mysql", "password", NULL);
|
|
|
|
MYSQL *ret = mysql_real_connect(dbh, hostname, username, password, NULL, 0, NULL, 0);
|
|
|
|
if (ret != dbh) {
|
|
|
|
log_print(LOG_ERROR, "database_init: mysql_real_connect(): %s", mysql_error(dbh));
|
|
|
|
mysql_close(dbh);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *database = config_get_string("mysql", "database", NULL);
|
|
|
|
if (mysql_select_db(dbh, database) != 0) {
|
|
|
|
log_print(LOG_ERROR, "database_init: mysql_select_db(): %s", mysql_error(dbh));
|
|
|
|
mysql_close(dbh);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-24 19:49:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int database_close(void)
|
|
|
|
{
|
2007-03-25 15:24:47 +02:00
|
|
|
mysql_close(dbh);
|
2007-03-24 19:49:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|