2007-07-14 15:38:16 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 07/2007 by Olaf Rempel *
|
|
|
|
* razzor@kopf-tisch.de *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; version 2 of the License *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
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-31 21:16:52 +02:00
|
|
|
static void purge_hash_cb(const struct hash_entry *entry, void *privdata)
|
2007-03-24 19:49:30 +01:00
|
|
|
{
|
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-04-08 17:02:24 +02:00
|
|
|
"INSERT INTO stats SET timestamp='%lu', srcip='%u', proto='%u', dport='%u', flags='%u', srcbytes='%llu', dstbytes='%llu', count='%u'",
|
2007-03-25 16:24:27 +02:00
|
|
|
*now, ntohl(entry->src_ip), entry->protonum, ntohs(entry->dst_port),
|
2007-04-08 17:02:24 +02:00
|
|
|
entry->flags, 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;
|
|
|
|
}
|
|
|
|
|
2007-03-31 21:16:52 +02:00
|
|
|
const char *hostname = config_get_string("mysql", "hostname", NULL);
|
|
|
|
const char *username = config_get_string("mysql", "username", NULL);
|
|
|
|
const char *password = config_get_string("mysql", "password", NULL);
|
2007-03-25 15:24:47 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-03-31 21:16:52 +02:00
|
|
|
const char *database = config_get_string("mysql", "database", NULL);
|
2007-03-25 15:24:47 +02:00
|
|
|
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;
|
|
|
|
}
|