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 | #include "renetcol.h" |
---|
27 | |
---|
28 | struct MyPtrs myPtrs; |
---|
29 | |
---|
30 | struct SHMForAgg *shmForAgg; |
---|
31 | |
---|
32 | unsigned short currentIPOutputFile = 0; |
---|
33 | unsigned short currentMPLSOutputFile = 0; |
---|
34 | char outputName[256]; |
---|
35 | char cof_str[20]; |
---|
36 | FILE *fIP; |
---|
37 | FILE *fMPLS; |
---|
38 | |
---|
39 | struct sigaction myAction; |
---|
40 | sigjmp_buf contextSigalrm; |
---|
41 | volatile sig_atomic_t sigusr1Up = 0; |
---|
42 | |
---|
43 | |
---|
44 | unsigned short currentIntervalle = 0; |
---|
45 | |
---|
46 | unsigned short offsetV9 = 0; |
---|
47 | |
---|
48 | unsigned char buffer1; |
---|
49 | unsigned char buffer2[2]; |
---|
50 | unsigned char buffer4[4]; |
---|
51 | unsigned char buffer6[6]; |
---|
52 | |
---|
53 | short currentFlowsetNumber; |
---|
54 | short currentFlowsetId; |
---|
55 | TplFlowSetPtr curTplFlSetPtr; |
---|
56 | TplOptionPtr curTplOptionPtr; |
---|
57 | RouterPtr routersListPtr; |
---|
58 | |
---|
59 | unsigned long sortedRouterList[ROUTER_INDEX_MAX]; |
---|
60 | unsigned long indexedRouterList[ROUTER_INDEX_MAX]; |
---|
61 | int routerNb = 0; |
---|
62 | |
---|
63 | key_t myKey = 0; |
---|
64 | int myQueue = 0; |
---|
65 | |
---|
66 | |
---|
67 | RuleDefPtr rulesAddress[FIELD_TYPE_NUMBER+1][MAX_RULES_PER_FIELD]; |
---|
68 | |
---|
69 | int reInitConf = 0; |
---|
70 | |
---|
71 | static struct tm *tmPtr; |
---|
72 | |
---|
73 | extern int errno; |
---|
74 | static int inputSock; |
---|
75 | static int sockNamelg; |
---|
76 | static int inputSock2; |
---|
77 | static int sockNameFromlg; |
---|
78 | static int rcv; |
---|
79 | static int sockBufSize = SOCKET_BUFFER_SIZE; |
---|
80 | |
---|
81 | static unsigned short receptPort = RECEPTION_PORT; |
---|
82 | |
---|
83 | static char *receptAddress = RECEPTION_ADDRESS; |
---|
84 | static struct sockaddr_in name; |
---|
85 | static struct sockaddr_in name2; |
---|
86 | static struct sockaddr_in fromName; |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | int |
---|
93 | main (int argc, char *argv[]) |
---|
94 | { |
---|
95 | RulesPtr tmp = NULL; |
---|
96 | int shmid; |
---|
97 | key_t key; |
---|
98 | int i,j; |
---|
99 | int k; |
---|
100 | |
---|
101 | #ifdef IPV4AGGIDR |
---|
102 | fprintf(stderr, "renetcol compilation and execution with IPv4 Aggregation feature enable based on routers ID in %s file.\n", ROUTERS_LIST); |
---|
103 | #endif |
---|
104 | |
---|
105 | #ifdef IPV4AGGIDSNMP |
---|
106 | fprintf(stderr, "renetcol compilation and execution with IPv4 Aggregation feature enable based on SNMP ID in %s file.\n", INTERFACES_TYPE_LIST); |
---|
107 | #endif |
---|
108 | |
---|
109 | openlog(argv[0], LOG_PID, LOG_USER); |
---|
110 | |
---|
111 | initCache(); |
---|
112 | |
---|
113 | if ( (routerNb = getRegisteredRouters(ROUTERS_LIST, |
---|
114 | &sortedRouterList[0], |
---|
115 | &indexedRouterList[0])) == -1) { |
---|
116 | fprintf(stderr, |
---|
117 | "ERROR in getRegisteredRouters from file %s\n", |
---|
118 | ROUTERS_LIST); |
---|
119 | exit(-1); |
---|
120 | } |
---|
121 | |
---|
122 | for (i=0; i<routerNb; i++){ |
---|
123 | routersListPtr = addRouter(routersListPtr, sortedRouterList[i]); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | key = 8765; |
---|
128 | if ((shmid = shmget(key, SHMSIZE, IPC_CREAT | 0666)) < 0) { |
---|
129 | perror("shmget"); |
---|
130 | exit(1); |
---|
131 | } |
---|
132 | if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1) { |
---|
133 | perror("shmat"); |
---|
134 | exit(1); |
---|
135 | } |
---|
136 | |
---|
137 | if ( (shmForAgg->v4PrefixNb = |
---|
138 | getPrefixV4(SUBNETS_LIST, |
---|
139 | &(shmForAgg->prefixV4Tab[0][0]), |
---|
140 | &(shmForAgg->prefixV4SubnetTab[0][0]), |
---|
141 | &(shmForAgg->v4SubnetNb) |
---|
142 | )) < 1){ |
---|
143 | fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST); |
---|
144 | } else { |
---|
145 | fprintf(stderr, "We have found %hu supernets from %s\n", |
---|
146 | shmForAgg->v4PrefixNb, |
---|
147 | SUBNETS_LIST); |
---|
148 | fprintf(stderr, "and %hu subnets\n", |
---|
149 | shmForAgg->v4SubnetNb); |
---|
150 | } |
---|
151 | if ( (shmForAgg->v4PrefixNb = |
---|
152 | getPrefixV4(SUBNETS_LIST, |
---|
153 | &(shmForAgg->prefixV4Tab[1][0]), |
---|
154 | &(shmForAgg->prefixV4SubnetTab[1][0]), |
---|
155 | &(shmForAgg->v4SubnetNb) |
---|
156 | )) < 1){ |
---|
157 | fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST); |
---|
158 | exit(1); |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | shmForAgg->currentTable = 0; |
---|
168 | shmForAgg->secondTable = 1; |
---|
169 | shmForAgg->readed = 0; |
---|
170 | |
---|
171 | |
---|
172 | myPtrs.rulesListPtr = NULL; |
---|
173 | myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE); |
---|
174 | tmp = myPtrs.rulesListPtr; |
---|
175 | for ( ; tmp->next; tmp=tmp->next) { |
---|
176 | if (tmp->type != 2) { |
---|
177 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
178 | } |
---|
179 | } |
---|
180 | tmp = NULL; |
---|
181 | #ifdef DEBUG |
---|
182 | printRule(rulesListPtr); |
---|
183 | #endif |
---|
184 | |
---|
185 | #ifdef ASACC |
---|
186 | |
---|
187 | if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[0][0]))) < 1){ |
---|
188 | fprintf(stderr, "renetcol: Error in %s\n", AS_LIST); |
---|
189 | exit(1); |
---|
190 | } else { |
---|
191 | fprintf(stderr, "%hu AS are readed from %s\n", |
---|
192 | shmForAgg->ASNb, |
---|
193 | AS_LIST); |
---|
194 | } |
---|
195 | if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[1][0]))) < 1){ |
---|
196 | fprintf(stderr, "renetcol: Error in %s\n", AS_LIST); |
---|
197 | exit(1); |
---|
198 | } |
---|
199 | #endif |
---|
200 | |
---|
201 | |
---|
202 | fprintf(stderr, "renetcol: I become a deamon, next messages via syslogd. By.\n"); |
---|
203 | if (fork () != 0) |
---|
204 | exit (0); |
---|
205 | if (setsid() == -1){ |
---|
206 | exit(4); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | myAction.sa_handler = sigusr1Mgmt; |
---|
211 | myAction.sa_flags = SA_RESTART; |
---|
212 | sigemptyset (&(myAction.sa_mask)); |
---|
213 | sigaddset (&(myAction.sa_mask), SIGALRM); |
---|
214 | sigaction (SIGUSR1, &myAction, NULL); |
---|
215 | |
---|
216 | |
---|
217 | setCache(myPtrs.rulesListPtr); |
---|
218 | myPtrs.rulesAddressPtr = (RuleDefPtr *)rulesAddress; |
---|
219 | |
---|
220 | |
---|
221 | myKey = createKey(argv[0]); |
---|
222 | myQueue = createQueue(myKey); |
---|
223 | sendMyPid(myQueue); |
---|
224 | |
---|
225 | |
---|
226 | k = getSNMPIndexList(INTERFACES_TYPE_LIST, routersListPtr); |
---|
227 | |
---|
228 | |
---|
229 | myPtrs.currentRouterPtr = routersListPtr; |
---|
230 | myPtrs.currentHeaderV9Ptr = (NetFlowV9HeaderPtr) |
---|
231 | malloc(sizeof(struct NetFlowV9Header)); |
---|
232 | myPtrs.offsetV9Ptr = &offsetV9; |
---|
233 | myPtrs.currentFlowsetIdPtr = ¤tFlowsetId; |
---|
234 | myPtrs.pcktPtr = (DatagramPtr) malloc(sizeof(struct Datagram)); |
---|
235 | myPtrs.currentFlowsetNumberPtr = ¤tFlowsetNumber; |
---|
236 | #ifdef IPV4AGGIDR |
---|
237 | myPtrs.routersID = &indexedRouterList; |
---|
238 | #endif |
---|
239 | myPtrs.currentV4Tab = shmForAgg->prefixV4Tab[shmForAgg->currentTable]; |
---|
240 | myPtrs.secondV4Tab = shmForAgg->prefixV4Tab[shmForAgg->secondTable]; |
---|
241 | #ifdef ASACC |
---|
242 | myPtrs.asNb = shmForAgg->ASNb; |
---|
243 | myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable]; |
---|
244 | myPtrs.secondASTab = shmForAgg->ASTab[shmForAgg->secondTable]; |
---|
245 | #endif |
---|
246 | #ifdef MATRIX |
---|
247 | myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]); |
---|
248 | #endif |
---|
249 | |
---|
250 | |
---|
251 | #ifdef MATRIX |
---|
252 | for (i=0; i<ROUTER_INDEX_MAX; i++){ |
---|
253 | for (j=0; j<ROUTER_INDEX_MAX; j++) { |
---|
254 | shmForAgg->matrixPOP[0][i][j].bytesNb = 0; |
---|
255 | shmForAgg->matrixPOP[0][i][j].pktsNb = 0; |
---|
256 | shmForAgg->matrixPOP[0][i][j].flowNb = 0; |
---|
257 | shmForAgg->matrixPOP[1][i][j].bytesNb = 0; |
---|
258 | shmForAgg->matrixPOP[1][i][j].pktsNb = 0; |
---|
259 | shmForAgg->matrixPOP[1][i][j].flowNb = 0; |
---|
260 | } |
---|
261 | } |
---|
262 | #endif |
---|
263 | |
---|
264 | |
---|
265 | if (myPtrs.pcktPtr==NULL) { |
---|
266 | fprintf(stderr, "ERROR in struct Datagram allocation\n"); |
---|
267 | exit(1); |
---|
268 | } else { |
---|
269 | myPtrs.pcktPtr->ipH = (IpHeaderPtr) malloc(sizeof(struct IpHeader)); |
---|
270 | myPtrs.pcktPtr->udp_header = (UdpHeaderPtr) malloc(sizeof(struct UdpHeader)); |
---|
271 | } |
---|
272 | if (! (myPtrs.ptr_buffer = malloc(sockBufSize))) |
---|
273 | { |
---|
274 | printf("ERROR during socket buffer allocation\n"); |
---|
275 | exit(2); |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | initStream(); |
---|
280 | |
---|
281 | socketLoop(); |
---|
282 | |
---|
283 | closelog(); |
---|
284 | |
---|
285 | fprintf(stderr, "END\n"); |
---|
286 | |
---|
287 | return (0); |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | void |
---|
297 | sendMyPid(int queueID) |
---|
298 | { |
---|
299 | msgType myMsg; |
---|
300 | char *msgTextIndex; |
---|
301 | unsigned short tplMsgType = 12; |
---|
302 | pid_t myPID; |
---|
303 | |
---|
304 | myPID = getpid(); |
---|
305 | msgTextIndex = mempcpy(mempcpy(myMsg.text, |
---|
306 | &tplMsgType, |
---|
307 | sizeof (unsigned short) |
---|
308 | ), |
---|
309 | &myPID, |
---|
310 | sizeof(pid_t) |
---|
311 | ); |
---|
312 | myMsg.type = 1; |
---|
313 | msgSend(queueID, myMsg); |
---|
314 | } |
---|
315 | |
---|
316 | |
---|
317 | |
---|
318 | |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | void sendReadRulesSignal(int queueID) |
---|
323 | { |
---|
324 | msgType myMsg; |
---|
325 | char *msgTextIndex; |
---|
326 | unsigned short tplMsgType = 13; |
---|
327 | |
---|
328 | msgTextIndex = mempcpy(myMsg.text, |
---|
329 | &tplMsgType, |
---|
330 | sizeof (unsigned short) |
---|
331 | ); |
---|
332 | myMsg.type = 1; |
---|
333 | msgSend(queueID, myMsg); |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | |
---|
338 | |
---|
339 | void |
---|
340 | sigusr1Mgmt(int num) |
---|
341 | { |
---|
342 | sigusr1Up = 1; |
---|
343 | } |
---|
344 | |
---|
345 | |
---|
346 | |
---|
347 | |
---|
348 | void |
---|
349 | initRule() |
---|
350 | { |
---|
351 | RulesPtr tmp = NULL; |
---|
352 | initCache(); |
---|
353 | tmp = myPtrs.rulesListPtr; |
---|
354 | for ( ; tmp; tmp=tmp->next) { |
---|
355 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
356 | } |
---|
357 | myPtrs.rulesListPtr = NULL; |
---|
358 | myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE); |
---|
359 | tmp = myPtrs.rulesListPtr; |
---|
360 | for ( ; tmp; tmp=tmp->next) { |
---|
361 | if (tmp->type != 2) { |
---|
362 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
363 | } |
---|
364 | } |
---|
365 | tmp = NULL; |
---|
366 | initCache(); |
---|
367 | setCache(myPtrs.rulesListPtr); |
---|
368 | sendReadRulesSignal(myQueue); |
---|
369 | #ifdef DEBUG |
---|
370 | printRule(myPtrs.rulesListPtr); |
---|
371 | #endif |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | void |
---|
378 | initCache() |
---|
379 | { |
---|
380 | int i,j; |
---|
381 | for (i=0; i<FIELD_TYPE_NUMBER+1; i++){ |
---|
382 | for (j=0; j<MAX_RULES_PER_FIELD; j++){ |
---|
383 | rulesAddress[i][j] = NULL; |
---|
384 | } |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | |
---|
389 | |
---|
390 | |
---|
391 | void |
---|
392 | setCache(RulesPtr rPtr) |
---|
393 | { |
---|
394 | int i=0; |
---|
395 | RulesPtr tmp = rPtr; |
---|
396 | RuleDefPtr def = NULL; |
---|
397 | |
---|
398 | #ifdef DEBUG |
---|
399 | fprintf(stderr,"Cache for Rules\n"); |
---|
400 | #endif |
---|
401 | while (tmp) { |
---|
402 | def = tmp->def; |
---|
403 | while (def) { |
---|
404 | i = 0; |
---|
405 | while (rulesAddress[def->fieldType][i] != NULL){ |
---|
406 | i++; |
---|
407 | } |
---|
408 | rulesAddress[def->fieldType][i] = def; |
---|
409 | def = def->next; |
---|
410 | #ifdef DEBUG |
---|
411 | fprintf(stderr,"%d: %d ", def->fieldType, i); |
---|
412 | #endif |
---|
413 | } |
---|
414 | #ifdef DEBUG |
---|
415 | fprintf(stderr,"\n"); |
---|
416 | #endif |
---|
417 | tmp = tmp->next; |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | |
---|
422 | |
---|
423 | |
---|
424 | void |
---|
425 | initStream() |
---|
426 | { |
---|
427 | static unsigned short n0, n1, n2, n3; |
---|
428 | |
---|
429 | initSocket(); |
---|
430 | if (sscanf(receptAddress,"%hu.%hu.%hu.%hu",&n0,&n1,&n2,&n3)==0) { |
---|
431 | perror("sscanf"); |
---|
432 | } |
---|
433 | buffer4[0] = (unsigned char)n3; |
---|
434 | buffer4[1] = (unsigned char)n2; |
---|
435 | buffer4[2] = (unsigned char)n1; |
---|
436 | buffer4[3] = (unsigned char)n0; |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | |
---|
441 | |
---|
442 | int |
---|
443 | socketLoop() |
---|
444 | { |
---|
445 | short shift; |
---|
446 | short version = 0; |
---|
447 | int regRouter = 0; |
---|
448 | time_t now = time((time_t *)NULL); |
---|
449 | int loopNb = 0; |
---|
450 | int gardeFou = 0; |
---|
451 | time_t lastIPOutput, lastMPLSOutput; |
---|
452 | |
---|
453 | now = time((time_t *)NULL); |
---|
454 | tmPtr = localtime(&now); |
---|
455 | currentIntervalle = tmPtr->tm_min; |
---|
456 | lastMPLSOutput = now; |
---|
457 | lastIPOutput = now; |
---|
458 | do { |
---|
459 | #ifdef DEBUG |
---|
460 | fprintf (stderr, "["); |
---|
461 | #endif |
---|
462 | if ( ((tmPtr->tm_min)%STEP == 0) && (currentIntervalle != tmPtr->tm_min)){ |
---|
463 | currentIntervalle = tmPtr->tm_min; |
---|
464 | if (shmForAgg->currentTable == 0) { |
---|
465 | shmForAgg->currentTable = 1; |
---|
466 | shmForAgg->secondTable = 0; |
---|
467 | }else{ |
---|
468 | shmForAgg->currentTable = 0; |
---|
469 | shmForAgg->secondTable = 1; |
---|
470 | } |
---|
471 | shmForAgg->readed = 1; |
---|
472 | #ifdef ASACC |
---|
473 | myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable]; |
---|
474 | #endif |
---|
475 | #ifdef MATRIX |
---|
476 | myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]); |
---|
477 | #endif |
---|
478 | } |
---|
479 | if (sigusr1Up == 1){ |
---|
480 | sigusr1Up = 0; |
---|
481 | initRule(); |
---|
482 | } |
---|
483 | socketReading(); |
---|
484 | getIpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer); |
---|
485 | regRouter = checkIpHeader(myPtrs.pcktPtr, &sortedRouterList[0], routerNb); |
---|
486 | getUdpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer); |
---|
487 | if ( checkUdpHeader(myPtrs.pcktPtr, regRouter, receptPort) == 1 ){ |
---|
488 | continue; |
---|
489 | } |
---|
490 | |
---|
491 | switch( version = |
---|
492 | getNetFlowHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer, |
---|
493 | myPtrs.currentHeaderV9Ptr, myPtrs.offsetV9Ptr)){ |
---|
494 | case 9: |
---|
495 | if ((myPtrs.currentRouterPtr=notExistRouter(routersListPtr, |
---|
496 | myPtrs.pcktPtr->ipH->srcAdd))==NULL) { |
---|
497 | myPtrs.currentRouterPtr = routersListPtr; |
---|
498 | } |
---|
499 | currentFlowsetNumber = 0; |
---|
500 | shift = 0; |
---|
501 | gardeFou=0; |
---|
502 | while ((currentFlowsetNumber < myPtrs.currentHeaderV9Ptr->count)) { |
---|
503 | gardeFou++; |
---|
504 | curTplFlSetPtr = NULL; |
---|
505 | currentFlowsetId=getFlowsetId(currentFlowsetNumber, |
---|
506 | myPtrs.offsetV9Ptr, myPtrs.ptr_buffer); |
---|
507 | if ( currentFlowsetId == 0 ) { |
---|
508 | if ( (shift = checkTemplateFlowSet(myPtrs.currentRouterPtr, |
---|
509 | myPtrs.offsetV9Ptr, |
---|
510 | myPtrs.ptr_buffer, |
---|
511 | myPtrs.currentHeaderV9Ptr, |
---|
512 | curTplFlSetPtr, |
---|
513 | myPtrs.currentFlowsetNumberPtr, |
---|
514 | myQueue)) < 0 ) { |
---|
515 | #ifdef DEBUG |
---|
516 | fprintf (stderr, "w>\n"); |
---|
517 | #endif |
---|
518 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
519 | }; |
---|
520 | writeAllTplFlSet(); |
---|
521 | } else if (currentFlowsetId == 1) { |
---|
522 | if ( (shift = checkTemplateOption(myPtrs.currentRouterPtr, |
---|
523 | myPtrs.offsetV9Ptr, |
---|
524 | myPtrs.ptr_buffer, |
---|
525 | myPtrs.currentHeaderV9Ptr, |
---|
526 | curTplOptionPtr, |
---|
527 | myPtrs.currentFlowsetNumberPtr) ) < 0 ) { |
---|
528 | #ifdef DEBUG |
---|
529 | fprintf (stderr, "wo>\n"); |
---|
530 | #endif |
---|
531 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
532 | } |
---|
533 | } else if (currentFlowsetId > 255) { |
---|
534 | if ( (shift = checkDataFlowSet(shift, |
---|
535 | &myPtrs, |
---|
536 | myQueue, |
---|
537 | shmForAgg->prefixV4Tab[shmForAgg->currentTable], |
---|
538 | (size_t) shmForAgg->v4PrefixNb, |
---|
539 | shmForAgg->prefixV4SubnetTab[shmForAgg->currentTable], |
---|
540 | (size_t) shmForAgg->v4SubnetNb) ) < 0 ) { |
---|
541 | #ifdef DEBUG |
---|
542 | fprintf (stderr, "wd>\n"); |
---|
543 | #endif |
---|
544 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
545 | } |
---|
546 | } else { |
---|
547 | #ifdef DEBUG |
---|
548 | fprintf (stderr, "1<<255\n"); |
---|
549 | #endif |
---|
550 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
551 | } |
---|
552 | if ( gardeFou > 200) { exit(-1); } |
---|
553 | } |
---|
554 | break; |
---|
555 | default: |
---|
556 | syslog(LOG_INFO, |
---|
557 | "NetFlow exp. version unknown: %hu, from router: %lu.%lu.%lu.%lu", |
---|
558 | (unsigned short) version, |
---|
559 | (myPtrs.pcktPtr->ipH->srcAdd>>24), |
---|
560 | (myPtrs.pcktPtr->ipH->srcAdd<<8>>24), |
---|
561 | (myPtrs.pcktPtr->ipH->srcAdd<<16>>24), |
---|
562 | (myPtrs.pcktPtr->ipH->srcAdd<<24>>24)); |
---|
563 | break; |
---|
564 | } |
---|
565 | if (loopNb > 1000 ){ |
---|
566 | |
---|
567 | writeAllTplFlSet(); |
---|
568 | #ifdef DEBUG |
---|
569 | fprintf(stderr, "W"); |
---|
570 | #endif |
---|
571 | loopNb = 0; |
---|
572 | } |
---|
573 | loopNb++; |
---|
574 | |
---|
575 | now = time((time_t *)NULL); |
---|
576 | tmPtr = localtime(&now); |
---|
577 | #ifdef DEBUG |
---|
578 | fprintf (stderr, "]\n"); |
---|
579 | #endif |
---|
580 | } while (1); |
---|
581 | } |
---|
582 | |
---|
583 | |
---|
584 | |
---|
585 | |
---|
586 | short |
---|
587 | initSocket() |
---|
588 | { |
---|
589 | inputSock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); |
---|
590 | if (inputSock < 0) |
---|
591 | { |
---|
592 | syslog(LOG_ERR,"socket : %s", strerror(errno)); |
---|
593 | exit(1); |
---|
594 | } |
---|
595 | memset((void*)&name, 0, sizeof(name)); |
---|
596 | name.sin_family = AF_INET; |
---|
597 | name.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
598 | if (name.sin_addr.s_addr == INADDR_NONE){ |
---|
599 | syslog(LOG_ERR, " INADDR_NONE "); |
---|
600 | exit(1); |
---|
601 | } |
---|
602 | name.sin_port = htons(receptPort); |
---|
603 | if (bind(inputSock,(struct sockaddr *)(&name), sizeof(name)) < 0) |
---|
604 | { |
---|
605 | syslog(LOG_ERR, "bind : %s", strerror(errno)); |
---|
606 | exit(1); |
---|
607 | } |
---|
608 | sockNamelg = sizeof(name); |
---|
609 | if (getsockname(inputSock, (struct sockaddr *) (&name), |
---|
610 | (socklen_t *)&sockNamelg) < 0) |
---|
611 | { |
---|
612 | syslog(LOG_ERR, "getsockname: %s", strerror(errno)); |
---|
613 | exit(1); |
---|
614 | } |
---|
615 | |
---|
616 | |
---|
617 | inputSock2 = socket(AF_INET, SOCK_DGRAM, 0); |
---|
618 | if (inputSock2 < 0) |
---|
619 | { |
---|
620 | syslog(LOG_ERR,"socket : %s", strerror(errno)); |
---|
621 | exit(1); |
---|
622 | } |
---|
623 | memset((void*)&name, 0, sizeof(name)); |
---|
624 | name2.sin_family = AF_INET; |
---|
625 | name2.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
626 | if (name2.sin_addr.s_addr == INADDR_NONE){ |
---|
627 | syslog(LOG_ERR, " INADDR_NONE "); |
---|
628 | exit(1); |
---|
629 | } |
---|
630 | name2.sin_port = htons(receptPort); |
---|
631 | if (bind(inputSock2,(struct sockaddr *)(&name2), sizeof(name2)) < 0) |
---|
632 | { |
---|
633 | syslog(LOG_ERR, "bind : %s", strerror(errno)); |
---|
634 | exit(1); |
---|
635 | } |
---|
636 | sockNamelg = sizeof(name2); |
---|
637 | if (getsockname(inputSock2, (struct sockaddr *) (&name2), |
---|
638 | (socklen_t *)&sockNamelg) < 0) |
---|
639 | { |
---|
640 | syslog(LOG_ERR, "getsockname: %s", strerror(errno)); |
---|
641 | exit(1); |
---|
642 | } |
---|
643 | return(0); |
---|
644 | } |
---|
645 | |
---|
646 | |
---|
647 | |
---|
648 | |
---|
649 | |
---|
650 | short |
---|
651 | socketReading() |
---|
652 | { |
---|
653 | signed short sockLg; |
---|
654 | |
---|
655 | sockNameFromlg = sizeof(fromName); |
---|
656 | rcv = recvfrom(inputSock, myPtrs.ptr_buffer, sockBufSize, 0, |
---|
657 | (struct sockaddr *)(&fromName), |
---|
658 | (socklen_t *)&sockNameFromlg); |
---|
659 | sockLg = rcv; |
---|
660 | if (sockLg < 0) { |
---|
661 | syslog(LOG_ERR,"recvfrom : %s", strerror(errno)); |
---|
662 | exit(1); |
---|
663 | } |
---|
664 | if (sockLg == 0) { |
---|
665 | syslog(LOG_ERR,"recvfrom : %s", strerror(errno)); |
---|
666 | exit(1); |
---|
667 | } |
---|
668 | return(0); |
---|
669 | } |
---|
670 | |
---|
671 | |
---|
672 | |
---|
673 | |
---|
674 | short |
---|
675 | checkFlow(short flowNumber) |
---|
676 | { |
---|
677 | return(0); |
---|
678 | } |
---|
679 | |
---|
680 | |
---|
681 | |
---|
682 | |
---|
683 | |
---|
684 | |
---|
685 | void |
---|
686 | showAllTplFlSet() |
---|
687 | { |
---|
688 | RouterPtr tmp = routersListPtr; |
---|
689 | TplFlowSetPtr tmpFS; |
---|
690 | TplOptionPtr tmpOP; |
---|
691 | fprintf(stderr,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n"); |
---|
692 | for (; tmp; tmp=tmp->next) { |
---|
693 | fprintf(stderr,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n", |
---|
694 | (tmp->IpAddress>>24), |
---|
695 | (tmp->IpAddress<<8>>24), |
---|
696 | (tmp->IpAddress<<16>>24), |
---|
697 | (tmp->IpAddress<<24>>24)); |
---|
698 | tmpFS = tmp->tplList; |
---|
699 | for (; tmpFS; tmpFS=tmpFS->next) { |
---|
700 | fprintf(stderr,"TId %hu (sourceId: %lu):\n", |
---|
701 | tmpFS->templateFlowSetId, |
---|
702 | tmpFS->sourceId); |
---|
703 | printFieldSet(stderr, tmpFS->fieldSet); |
---|
704 | fprintf(stderr,"\n"); |
---|
705 | } |
---|
706 | if ((tmpOP = tmp->tplOptList) != NULL){ |
---|
707 | for (; tmpOP; tmpOP=tmpOP->next) { |
---|
708 | fprintf(stderr,"OpTId %hu (sourceId: %lu) >\n", |
---|
709 | tmpOP->templateOptionId, |
---|
710 | tmpOP->sourceId); |
---|
711 | printFieldSet(stderr, tmpOP->fieldSet); |
---|
712 | fprintf(stderr,"\n"); |
---|
713 | } |
---|
714 | fprintf(stderr,"\n"); |
---|
715 | } |
---|
716 | } |
---|
717 | } |
---|
718 | |
---|
719 | |
---|
720 | |
---|
721 | |
---|
722 | |
---|
723 | |
---|
724 | void |
---|
725 | writeAllTplFlSet() |
---|
726 | { |
---|
727 | |
---|
728 | RouterPtr tmp = routersListPtr; |
---|
729 | TplFlowSetPtr tmpFS; |
---|
730 | TplOptionPtr tmpOP; |
---|
731 | FILE *TPLFILE; |
---|
732 | if (!(TPLFILE = fopen("/tmp/TemplateDef.txt", "w"))) { |
---|
733 | syslog (LOG_ERR, "error during %s opening", "/tmp/TemplateDef.txt"); |
---|
734 | } |
---|
735 | fprintf(TPLFILE,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n"); |
---|
736 | for (; tmp; tmp=tmp->next) { |
---|
737 | fprintf(TPLFILE,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n", |
---|
738 | (tmp->IpAddress>>24), |
---|
739 | (tmp->IpAddress<<8>>24), |
---|
740 | (tmp->IpAddress<<16>>24), |
---|
741 | (tmp->IpAddress<<24>>24)); |
---|
742 | tmpFS = tmp->tplList; |
---|
743 | for (; tmpFS; tmpFS=tmpFS->next) { |
---|
744 | fprintf(TPLFILE,"TId %hu (sourceId: %lu):\n", |
---|
745 | tmpFS->templateFlowSetId, |
---|
746 | tmpFS->sourceId); |
---|
747 | printFieldSet(TPLFILE, tmpFS->fieldSet); |
---|
748 | fprintf(TPLFILE,"\n"); |
---|
749 | } |
---|
750 | if ((tmpOP = tmp->tplOptList) != NULL){ |
---|
751 | for (; tmpOP; tmpOP=tmpOP->next) { |
---|
752 | fprintf(TPLFILE,"OpTId %hu (sourceId: %lu) last %d brackets are Scope >\n lg: %hu, opScopeLg: %hu, opLg: %hu\n", |
---|
753 | tmpOP->templateOptionId, |
---|
754 | tmpOP->sourceId, |
---|
755 | tmpOP->optionScopeLg/4, |
---|
756 | tmpOP->length, |
---|
757 | tmpOP->optionScopeLg, |
---|
758 | tmpOP->optionLg); |
---|
759 | printFieldSet(TPLFILE, tmpOP->fieldSet); |
---|
760 | fprintf(TPLFILE,"\n"); |
---|
761 | } |
---|
762 | fprintf(TPLFILE,"\n"); |
---|
763 | } |
---|
764 | } |
---|
765 | fclose(TPLFILE); |
---|
766 | } |
---|