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