forked from vjanelle/nprobe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.c
352 lines (283 loc) · 9.59 KB
/
database.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* nProbe - a Netflow v5/v9/IPFIX probe for IPv4/v6
*
* Copyright (C) 2004-11 Luca Deri <deri@ntop.org>
*
* http://www.ntop.org/
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "nprobe.h"
#ifdef HAVE_MYSQL
/* #define DEBUG */
static MYSQL mysql;
static char * table_prefix = NULL;
u_int8_t db_initialized = 0, skip_db_creation = 0;
/* If you need to add a key to the table
then add the the V9 name of the field
to the array below
*/
static char *db_keys[] = {
"FIRST_SWITCHED",
"LAST_SWITCHED",
"IPV4_SRC_ADDR",
"IPV4_DST_ADDR",
"L4_SRC_PORT",
"L4_DST_PORT",
NULL
};
/* ***************************************************** */
int exec_sql_query(char *sql, u_char dump_error_if_any) {
#ifdef DEBUG
traceEvent(TRACE_NORMAL, "%s", sql);
#endif
if(!db_initialized) {
static char shown_msg = 0;
if(!shown_msg) {
traceEvent(TRACE_INFO, "MySQL error: DB not yet initialized");
traceEvent(TRACE_INFO, "Please use the %s command line option", MYSQL_OPT);
shown_msg = 1;
}
return(-2);
}
if(mysql_query(&mysql, sql)) {
if(dump_error_if_any)
traceEvent(TRACE_ERROR, "MySQL error: %s", mysql_error(&mysql));
return(-1);
} else {
/* traceEvent(TRACE_INFO, "Successfully executed '%s'", sql); */
return(0);
}
}
/* ***************************************************** */
char* get_last_db_error() {
return((char*)mysql_error(&mysql));
}
/* ***************************************************** */
char * get_db_table_prefix() { return table_prefix; }
/* ***************************************************** */
int init_database(char *db_host, char* user, char *pw,
char *db_name, char *tp) {
char sql[2048];
db_initialized = 0;
if(mysql_init(&mysql) == NULL) {
traceEvent(TRACE_ERROR, "Failed to initialize MySQL connection");
return(-1);
} else
traceEvent(TRACE_INFO, "MySQL initialized");
if(!mysql_real_connect(&mysql, db_host, user, pw, NULL, 0, NULL, 0)){
traceEvent(TRACE_ERROR, "Failed to connect to MySQL: %s [%s:%s:%s:%s]\n",
mysql_error(&mysql), db_host, user, pw, db_name);
return(-2);
} else
traceEvent(TRACE_INFO, "Successfully connected to MySQL [host:dbname:user:passwd]=[%s:%s:%s:%s]",
db_host, db_name, user, pw);
db_initialized = 1;
table_prefix = strdup(tp);
/* *************************************** */
snprintf(sql, sizeof(sql), "CREATE DATABASE IF NOT EXISTS %s", db_name);
if(exec_sql_query(sql, 0) != 0) {
traceEvent(TRACE_ERROR, "MySQL error: %s\n", get_last_db_error());
return(-3);
}
if(mysql_select_db(&mysql, db_name)) {
traceEvent(TRACE_ERROR, "MySQL error: %s\n", get_last_db_error());
return(-4);
}
/* *************************************** */
/* NetFlow */
snprintf(sql, sizeof(sql), "CREATE TABLE IF NOT EXISTS `%sflows` ("
"`idx` int(11) NOT NULL auto_increment,"
"UNIQUE KEY `idx` (`idx`)"
") ENGINE=MyISAM"
/* " DEFAULT CHARSET=latin1" */
, table_prefix
);
if(exec_sql_query(sql, 0) != 0) {
traceEvent(TRACE_ERROR, "MySQL error: %s\n", get_last_db_error());
return(-5);
}
return(0);
}
/* *************************************** */
static void createTemplateTable(V9V10TemplateElementId **template) {
char sql[2048];
int i, j;
for(i=0; i<TEMPLATE_LIST_LEN; i++) {
if(template[i] != NULL) {
#ifdef DEBUG
traceEvent(TRACE_INFO, "Found [%20s][%d bytes]",
template[i]->templateElementName,
template[i]->templateElementLen);
#endif
if((template[i]->elementFormat != ascii_format)
&& (template[i]->templateElementLen <= 4)) {
char *sql_type;
if(template[i]->templateElementLen <= 1)
sql_type = "tinyint(4) unsigned";
else if(template[i]->templateElementLen <= 2)
sql_type = "smallint(6) unsigned";
else if(template[i]->templateElementLen <= 4)
sql_type = "int(20) unsigned";
snprintf(sql, sizeof(sql), "ALTER TABLE `%sflows` ADD `%s` %s NOT NULL default '0'",
table_prefix ? table_prefix : "",
template[i]->templateElementName, sql_type);
} else {
snprintf(sql, sizeof(sql), "ALTER TABLE `%sflows` ADD `%s` varchar(%d) NOT NULL default ''",
table_prefix ? table_prefix : "",
template[i]->templateElementName,
2*template[i]->templateElementLen);
}
if(exec_sql_query(sql, 0) != 0) {
#ifdef DEBUG
traceEvent(TRACE_ERROR, "MySQL error: %s\n", get_last_db_error());
#endif
} else {
for(j=0; db_keys[j] != NULL; j++)
if(!strcmp(template[i]->templateElementName, db_keys[j])) {
snprintf(sql, sizeof(sql), "ALTER TABLE `%sflows` ADD INDEX (`%s`)",
table_prefix ? table_prefix : "",
template[i]->templateElementName);
if(exec_sql_query(sql, 0) != 0) {
#ifdef DEBUG
traceEvent(TRACE_ERROR, "MySQL error: %s\n", get_last_db_error());
#endif
}
break;
}
}
} else
break;
}
}
/* ************************************************ */
int init_db_table(void) {
if(!db_initialized) return(0);
if(skip_db_creation) {
traceEvent(TRACE_NORMAL, "Skipping database schema creation...");
return(0);
} else
traceEvent(TRACE_NORMAL, "Creating database schema...");
traceEvent(TRACE_INFO, "Scanning templates");
createTemplateTable(readOnlyGlobals.v9TemplateElementListV4);
if(readOnlyGlobals.v9TemplateElementListV6[0] != NULL) /* IPv6 template defined */
createTemplateTable(readOnlyGlobals.v9TemplateElementListV6);
return(0);
}
/* ************************************************ */
void dump_flow2db(V9V10TemplateElementId **template, char *buffer, u_int32_t buffer_len) {
if(db_initialized) {
char sql_a[2048] = { 0 }, sql_b[2048] = { 0 }, sql[4096] = { 0 }, buf[128];
int i, pos = 0;
/* traceEvent(TRACE_INFO, "dump_flow2db()"); */
snprintf(sql_a, sizeof(sql_a), "INSERT DELAYED INTO `%sflows` (",
table_prefix ? table_prefix : "");
strcpy(sql_b, "VALUES (");
for(i=0; (i<TEMPLATE_LIST_LEN); i++) {
if(template[i] != NULL) {
#ifdef DEBUG
traceEvent(TRACE_INFO, "Found [%20s][%d bytes]",
template[i]->templateElementName,
template[i]->templateElementLen);
#endif
if(i > 0) {
strcat(sql_a, ", ");
strcat(sql_b, ", ");
}
buf[0] = '\0';
strcat(sql_a, template[i]->templateElementName);
if((template[i]->elementFormat != ascii_format)
&& (template[i]->templateElementLen <= 4)) {
u_int8_t a = 0, b = 0, c = 0, d = 0;
u_int32_t val;
if(template[i]->templateElementLen == 1) {
d = buffer[pos];
pos += 1;
} else if(template[i]->templateElementLen == 2) {
c = buffer[pos], d = buffer[pos+1];
pos += 2;
} else if(template[i]->templateElementLen == 3) {
b = buffer[pos], c = buffer[pos+1], d = buffer[pos+2];
pos += 3;
} else if(template[i]->templateElementLen == 4) {
a = buffer[pos], b = buffer[pos+1], c = buffer[pos+2], d = buffer[pos+3];
pos += 4;
}
a &= 0xFF, b &= 0xFF, c &= 0xFF, d &= 0xFF;
val = (a << 24) + (b << 16) + (c << 8) + d;
if((template[i]->templateElementId == 21 /* LAST_SWITCHED */)
|| (template[i]->templateElementId == 22 /* FIRST_SWITCHED */)) {
/*
We need to patch this value as we want to save the epoch on fastbit and not
the sysuptime expressed in msec
*/
if(readOnlyGlobals.numCollectors == 0) /* Don't do this with collectors */
val = (val / 1000) + readOnlyGlobals.initialSniffTime.tv_sec;
}
snprintf(buf, sizeof(buf), "'%u'", val);
/*
snprintf(sql, sizeof(sql), "ALTER TABLE `%sflows` ADD `%s` varchar(%d) NOT NULL default ''",
table_prefix ? table_prefix : "",
template[i]->templateElementName,
template[i]->templateElementLen);
*/
// traceEvent(TRACE_INFO, "%X", val);
} else {
int k = 0, j = 0;
buf[0] = '\'';
switch(template[i]->elementFormat) {
case ipv6_address_format:
/* ret = (char*)*/ inet_ntop(AF_INET6, &buffer[pos], &buf[1], sizeof(buf)-1);
j = strlen(buf);
break;
case ascii_format:
for(j = 1; k<template[i]->templateElementLen; pos++, k++) {
if(buffer[pos] == '\'')
snprintf(&buf[j], sizeof(buf)-j, "\\%c", buffer[pos]);
snprintf(&buf[j], sizeof(buf)-j, "%c", buffer[pos]);
j++;
}
j = strlen(buf);
break;
case numeric_format:
case hex_format:
for(j = 1; k<template[i]->templateElementLen; pos++, k++) {
snprintf(&buf[j], sizeof(buf)-j, "%02X", buffer[pos] & 0xFF);
j += 2;
}
break;
}
buf[j] = '\'';
buf[j+1] = '\0';
if(template[i]->templateElementLen == 0) {
traceEvent(TRACE_WARNING, "nProbe does not yet handle variable length flow elements"); // FIX
} else
pos += template[i]->templateElementLen;
}
strcat(sql_b, buf);
}
if(pos > buffer_len) {
traceEvent(TRACE_WARNING, "Internal error [pos=%d][buffer_len=%d]",
pos, buffer_len);
break;
}
}
strcat(sql_a, ")");
strcat(sql_b, ")");
snprintf(sql, sizeof(sql), "%s %s", sql_a, sql_b);
exec_sql_query(sql, 1);
}
}
#endif