forked from vjanelle/nprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.c
More file actions
430 lines (349 loc) · 12 KB
/
database.c
File metadata and controls
430 lines (349 loc) · 12 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* nProbe - a Netflow v5/v9/IPFIX probe for IPv4/v6
*
* Copyright (C) 2004-14 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
/* 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
};
#ifndef htonll
#define ntohll(x) ( ( (uint64_t)(ntohl( (uint32_t)((x << 32) >> 32) )) << 32) | ntohl( ((uint32_t)(x >> 32)) ) )
#define htonll(x) ntohll(x)
#endif
/* ***************************************************** */
int exec_sql_query(char *sql, u_char dump_error_if_any) {
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_NORMAL, "%s", sql);
if(!readOnlyGlobals.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(&readOnlyGlobals.db.mysql, sql)) {
if(dump_error_if_any)
traceEvent(TRACE_ERROR, "MySQL error: [%s][%s]", mysql_error(&readOnlyGlobals.db.mysql), sql);
return(-1);
} else {
/* traceEvent(TRACE_INFO, "Successfully executed '%s'", sql); */
return(0);
}
}
/* ***************************************************** */
char* get_last_db_error() {
return((char*)mysql_error(&readOnlyGlobals.db.mysql));
}
/* ***************************************************** */
char* get_db_table_prefix() { return readOnlyGlobals.db.table_prefix; }
/* ***************************************************** */
int init_database(char *db_host, u_int db_port,
char* user, char *pw,
char *db_name, char *tp) {
char sql[2048];
MYSQL *rc;
readOnlyGlobals.db_initialized = 0;
if(mysql_init(&readOnlyGlobals.db.mysql) == NULL) {
traceEvent(TRACE_ERROR, "Failed to initialize MySQL connection");
return(-1);
} else
traceEvent(TRACE_INFO, "MySQL initialized");
if(db_host[0] == '/')
rc = mysql_real_connect(&readOnlyGlobals.db.mysql, NULL /* host */, user, pw,
NULL /* db */, 0, db_host /* socket */, 0);
else
rc = mysql_real_connect(&readOnlyGlobals.db.mysql, db_host, user, pw,
NULL /* db */, db_port, NULL /*socket */, 0);
if(rc == NULL) {
traceEvent(TRACE_ERROR, "Failed to connect to MySQL: %s [%s:%s:%s:%s]\n",
mysql_error(&readOnlyGlobals.db.mysql), db_host, user, pw, db_name);
return(-2);
} else {
char pwd[32];
int len = min(strlen(pw), sizeof(pwd)-1);
memset(pwd, 'x', len);
pwd[len] = '\0';
traceEvent(TRACE_INFO, "Successfully connected to MySQL [host:dbname:user:passwd]=[%s@%d:%s:%s:%s]",
db_host, db_port, db_name, user, pwd);
}
readOnlyGlobals.db_initialized = 1;
readOnlyGlobals.db.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(&readOnlyGlobals.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=%s"
/* " DEFAULT CHARSET=latin1" */
, readOnlyGlobals.db.table_prefix, readOnlyGlobals.dbEngineType);
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) {
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "Found [%20s][%d bytes]",
template[i]->netflowElementName,
template[i]->templateElementLen);
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'",
readOnlyGlobals.db.table_prefix ? readOnlyGlobals.db.table_prefix : "",
template[i]->netflowElementName, sql_type);
} else {
snprintf(sql, sizeof(sql), "ALTER TABLE `%sflows` ADD `%s` varchar(%d) NOT NULL default ''",
readOnlyGlobals.db.table_prefix ? readOnlyGlobals.db.table_prefix : "",
template[i]->netflowElementName,
2*template[i]->templateElementLen);
}
if(exec_sql_query(sql, 0) != 0) {
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_ERROR, "MySQL error: %s\n", get_last_db_error());
} else {
for(j=0; db_keys[j] != NULL; j++)
if(!strcmp(template[i]->netflowElementName, db_keys[j])) {
snprintf(sql, sizeof(sql), "ALTER TABLE `%sflows` ADD INDEX (`%s`)",
readOnlyGlobals.db.table_prefix ? readOnlyGlobals.db.table_prefix : "",
template[i]->netflowElementName);
if(exec_sql_query(sql, 0) != 0) {
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_ERROR, "MySQL error: %s\n", get_last_db_error());
}
break;
}
}
} else
break;
}
}
/* ************************************************ */
int init_db_table(void) {
int i;
if(!readOnlyGlobals.db_initialized) return(0);
if(readOnlyGlobals.skip_db_creation) {
traceEvent(TRACE_NORMAL, "Skipping database schema creation...");
return(0);
} else
traceEvent(TRACE_NORMAL, "Creating database schema...");
traceEvent(TRACE_INFO, "Scanning templates");
for(i=0; i<readOnlyGlobals.numActiveTemplates; i++)
createTemplateTable(readOnlyGlobals.templateBuffers[i].v9TemplateElementList);
return(0);
}
/* ************************************************ */
void dump_flow2db(V9V10TemplateElementId **template, char *buffer, u_int32_t buffer_len) {
if(readOnlyGlobals.db_initialized) {
char sql_a[4096] = { 0 }, sql_b[4096] = { 0 }, sql[4096] = { 0 }, buf[256];
int i, pos = 0;
/* traceEvent(TRACE_INFO, "dump_flow2db()"); */
snprintf(sql_a, sizeof(sql_a), "INSERT INTO `%sflows` (",
readOnlyGlobals.db.table_prefix ? readOnlyGlobals.db.table_prefix : "");
strcpy(sql_b, "VALUES (");
for(i=0; (i<TEMPLATE_LIST_LEN) && (template[i] != NULL); i++) {
u_int16_t field_len;
if(i > 0) {
strcat(sql_a, ", ");
strcat(sql_b, ", ");
}
buf[0] = '\0';
memset(buf, 0, sizeof(buf));
strcat(sql_a, template[i]->netflowElementName);
if((readOnlyGlobals.netFlowVersion == 10)
&& (template[i]->variableFieldLength == VARIABLE_FIELD_LEN)) {
field_len = buffer[pos];
pos++;
if(field_len == 255) {
/* Long length */
memcpy(&field_len, &buffer[pos], 2);
pos += 2;
field_len = ntohs(field_len);
}
} else
field_len = template[i]->templateElementLen;
if((template[i]->elementFormat != ascii_format)
&& (field_len <= 4)) {
u_int8_t a = 0, b = 0, c = 0, d = 0;
u_int32_t val;
if(field_len == 1) {
d = buffer[pos];
} else if(field_len == 2) {
c = buffer[pos], d = buffer[pos+1];
} else if(field_len == 3) {
b = buffer[pos], c = buffer[pos+1], d = buffer[pos+2];
} else if(field_len == 4) {
a = buffer[pos], b = buffer[pos+1], c = buffer[pos+2], d = buffer[pos+3];
}
pos += field_len;
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
*/
val = (val / 1000) + readOnlyGlobals.initialSniffTime.tv_sec;
}
snprintf(buf, sizeof(buf), "'%u'", val);
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_NORMAL, "[%s][%u][variable length=%s]",
template[i]->netflowElementName, val,
template[i]->variableFieldLength == VARIABLE_FIELD_LEN ? "Yes" : "No");
/*
snprintf(sql, sizeof(sql), "ALTER TABLE `%sflows` ADD `%s` varchar(%d) NOT NULL default ''",
readOnlyGlobals.db.table_prefix ? readOnlyGlobals.db.table_prefix : "",
template[i]->netflowElementName,
field_len);
*/
// traceEvent(TRACE_INFO, "%X", val);
} else {
int j = 0;
int dump_len = min(field_len, sizeof(buf));
/*
if(dump_len == 0)
traceEvent(TRACE_WARNING, "Zero length detected");
*/
buf[0] = '\'';
if(dump_len > 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);
pos += field_len;
break;
case ascii_format:
{
int k;
for(k = 0, j = 1; k<dump_len; pos++, k++) {
if(buffer[pos] == '\'') {
snprintf(&buf[j], sizeof(buf)-j, "\\%c", buffer[pos]);
j++; /* We add both \\ and ' */
} else
snprintf(&buf[j], sizeof(buf)-j, "%c", buffer[pos]);
j++;
}
j = strlen(buf);
}
break;
case numeric_format:
{
u_int8_t u8;
u_int16_t u16;
u_int32_t u32;
u_int64_t u64;
switch(dump_len) {
case 1:
u8 = *(u_int8_t*)&buffer[pos];
u64 = (u_int64_t)u8;
break;
case 2:
u16 = *(u_int16_t*)&buffer[pos];
u64 = (u_int64_t)ntohs(u16);
break;
case 4:
u32 = *(u_int32_t*)&buffer[pos];
u64 = (u_int64_t)ntohl(u32);
break;
case 8:
u64 = ntohll(*(u_int64_t*)&buffer[pos]);
break;
default:
traceEvent(TRACE_WARNING, "Internal error [dump_len=%u]", dump_len);
}
j = 1;
snprintf(&buf[j], sizeof(buf)-j, "%lu", u64);
j = strlen(buf);
}
break;
case hex_format:
{
int k;
for(j = 1, k = 0; k<dump_len; pos++, k++) {
snprintf(&buf[j], sizeof(buf)-j, "%02X", buffer[pos] & 0xFF);
j += 2;
}
}
break;
}
} else
j = 1;
buf[j] = '\'';
buf[j+1] = '\0';
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_NORMAL, "[%s][%s][len=%d][variable length=%s]",
template[i]->netflowElementName, buf, field_len,
template[i]->variableFieldLength == VARIABLE_FIELD_LEN ? "Yes" : "No");
}
strcat(sql_b, buf);
if(pos > buffer_len) {
traceEvent(TRACE_WARNING, "Internal error [pos=%d][buffer_len=%d]",
pos, buffer_len);
break;
}
if(readOnlyGlobals.enable_debug && (template[i] != NULL))
traceEvent(TRACE_INFO, "Handled %20s [id %d][%d bytes][total %d/%d bytes]",
template[i]->netflowElementName,
(template[i]->templateElementEnterpriseId == NTOP_ENTERPRISE_ID) ? template[i]->templateElementId-NTOP_BASE_ID : template[i]->templateElementId,
field_len, pos, buffer_len);
}
strcat(sql_a, ")");
strcat(sql_b, ")");
snprintf(sql, sizeof(sql), "%s %s", sql_a, sql_b);
exec_sql_query(sql, 1);
}
}
#endif