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 | #include "renetcolSMP1.h" |
---|
28 | |
---|
29 | struct MyPtrs myPtrs; |
---|
30 | |
---|
31 | struct SHMForAgg *shmForAgg; |
---|
32 | |
---|
33 | unsigned short currentIPOutputFile = 0; |
---|
34 | unsigned short currentMPLSOutputFile = 0; |
---|
35 | char outputName[256]; |
---|
36 | char cof_str[20]; |
---|
37 | FILE *fIP; |
---|
38 | FILE *fMPLS; |
---|
39 | |
---|
40 | #if defined(RECORDFLOW) |
---|
41 | FILE *fRECORDF1; |
---|
42 | FILE *fRECORDF2; |
---|
43 | char *bufRF1 = NULL; |
---|
44 | char *bufRF2 = NULL; |
---|
45 | char *nameRF[128]; |
---|
46 | char *pathRF[256]; |
---|
47 | #endif |
---|
48 | |
---|
49 | struct sigaction myAction; |
---|
50 | sigjmp_buf contextSigalrm; |
---|
51 | volatile sig_atomic_t sigusr1Up = 0; |
---|
52 | |
---|
53 | |
---|
54 | unsigned short currentIntervalle = 0; |
---|
55 | |
---|
56 | unsigned short offsetV9 = 0; |
---|
57 | |
---|
58 | unsigned char buffer1; |
---|
59 | unsigned char buffer2[2]; |
---|
60 | unsigned char buffer4[4]; |
---|
61 | unsigned char buffer6[6]; |
---|
62 | |
---|
63 | short currentFlowsetNumber; |
---|
64 | short currentFlowsetId; |
---|
65 | TplFlowSetPtr curTplFlSetPtr; |
---|
66 | TplOptionPtr curTplOptionPtr; |
---|
67 | RouterPtr routersListPtr; |
---|
68 | NDEEnginePtr enginePtrTmp; |
---|
69 | |
---|
70 | unsigned long sortedRouterList[SMP1_ROUTER_INDEX_MAX]; |
---|
71 | unsigned long indexedRouterList[SMP1_ROUTER_INDEX_MAX]; |
---|
72 | int routerNb = 0; |
---|
73 | |
---|
74 | key_t myKey = 0; |
---|
75 | int myQueue = 0; |
---|
76 | |
---|
77 | |
---|
78 | RuleDefPtr rulesAddress[FIELD_TYPE_NUMBER+1][MAX_RULES_PER_FIELD]; |
---|
79 | |
---|
80 | int reInitConf = 0; |
---|
81 | |
---|
82 | static struct tm *tmPtr; |
---|
83 | |
---|
84 | extern int errno; |
---|
85 | static int inputSock; |
---|
86 | static int sockNamelg; |
---|
87 | static int inputSock2; |
---|
88 | static int sockNameFromlg; |
---|
89 | static int rcv; |
---|
90 | static int sockBufSize = SOCKET_BUFFER_SIZE; |
---|
91 | |
---|
92 | static unsigned short receptPort = SMP1_RECEPTION_PORT; |
---|
93 | |
---|
94 | static char *receptAddress = SMP1_RECEPTION_ADDRESS; |
---|
95 | static struct sockaddr_in name; |
---|
96 | static struct sockaddr_in name2; |
---|
97 | static struct sockaddr_in fromName; |
---|
98 | char *log = "renetcol"; |
---|
99 | #ifdef READFROMFILE |
---|
100 | char *myPcapFileName, errBuffer[PCAP_ERRBUF_SIZE]; |
---|
101 | pcap_t *myPcapHandle; |
---|
102 | #endif |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | int |
---|
109 | main (int argc, char *argv[]) |
---|
110 | { |
---|
111 | RulesPtr tmp = NULL; |
---|
112 | int shmid; |
---|
113 | key_t key; |
---|
114 | int i,j; |
---|
115 | int k; |
---|
116 | unsigned short routerIndex = 0; |
---|
117 | |
---|
118 | #if defined(READFROMFILE) |
---|
119 | if ( argc != 2) { |
---|
120 | fprintf (stderr, |
---|
121 | "%s: Usage: %s <pcap file>\n exp: %s /tmp/my_pcap_file.pcap\n", |
---|
122 | argv[0], argv[0], argv[0]); |
---|
123 | exit(1); |
---|
124 | } |
---|
125 | myPcapFileName = argv[1]; |
---|
126 | if ((myPcapHandle = pcap_open_offline(myPcapFileName, errBuffer)) == NULL) { |
---|
127 | fprintf(stderr, "Couldn't open PCAP file %s: %s\n", myPcapFileName, errBuffer); |
---|
128 | exit(1); |
---|
129 | } |
---|
130 | #endif |
---|
131 | |
---|
132 | #if defined(RECORDFLOW) |
---|
133 | strncpy(pathRF, RECORD_FILE_PATH,sizeof(RECORD_FILE_PATH)); |
---|
134 | strncpy(nameRF, SMP1_RECORD_FILE_1,sizeof(SMP1_RECORD_FILE_1)); |
---|
135 | errno = 0; |
---|
136 | prepend_timestamp ( nameRF, sizeof nameRF ); |
---|
137 | if ( errno != ERANGE ) |
---|
138 | printf ( "Timestamped: %s\n", nameRF ); |
---|
139 | else { |
---|
140 | |
---|
141 | } |
---|
142 | strncat(pathRF,nameRF, sizeof(nameRF)); |
---|
143 | if ((fRECORDF1 = fopen(pathRF, "w")) == NULL) { |
---|
144 | syslog(LOG_ERR,"Error in %s opening : %s", pathRF, strerror(errno)); |
---|
145 | } else { |
---|
146 | fprintf(stderr,"fRECORDF1 %d",fRECORDF1); |
---|
147 | bufRF1 = (char *)malloc (262144); |
---|
148 | setvbuf (fRECORDF1, bufRF1, _IOFBF, 262144); |
---|
149 | } |
---|
150 | #endif |
---|
151 | |
---|
152 | #if defined(IPV4AGGIDR) |
---|
153 | fprintf(stderr, "renetcolSMP1 compilation and execution with IPv4 Prefixes Aggregation feature enable based on routers ID in %s file.\n", SMP1_ROUTERS_LIST); |
---|
154 | #endif |
---|
155 | |
---|
156 | #if defined(IPV4AGGIDSNMP) |
---|
157 | fprintf(stderr, "renetcolSMP1 compilation and execution with IPv4 Prefixes Aggregation feature enable based on SNMP ID in %s file.\n", SMP1_INTERFACES_TYPE_LIST); |
---|
158 | #endif |
---|
159 | #if defined(IPV6AGGIDSNMP) |
---|
160 | fprintf(stderr, "renetcolSMP1 compilation and execution with IPv6 Prefixes Aggregation feature enable based on SNMP ID in %s file.\n", SMP1_INTERFACES_TYPE_LIST); |
---|
161 | #endif |
---|
162 | #if defined(IPV6LINKAGG) |
---|
163 | fprintf(stderr, "renetcolSMP1 compilation and execution with IPv6 Links Aggregation.\n"); |
---|
164 | #endif |
---|
165 | |
---|
166 | |
---|
167 | openlog(log, NULL, LOG_LOCAL7); |
---|
168 | |
---|
169 | initCache(); |
---|
170 | |
---|
171 | if ( (routerNb = getRegisteredRouters(SMP1_ROUTERS_LIST, |
---|
172 | &sortedRouterList[0], |
---|
173 | &indexedRouterList[0])) == -1) { |
---|
174 | fprintf(stderr, |
---|
175 | "ERROR in getRegisteredRouters from file %s\n", |
---|
176 | SMP1_ROUTERS_LIST); |
---|
177 | exit(-1); |
---|
178 | } |
---|
179 | |
---|
180 | for (routerIndex=0; routerIndex<routerNb; routerIndex++){ |
---|
181 | routersListPtr = addRouter(routersListPtr, sortedRouterList[routerIndex],routerIndex); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | key = 8765; |
---|
186 | if ((shmid = shmget(key, SHMSIZE, IPC_CREAT | 0666)) < 0) { |
---|
187 | perror("shmget"); |
---|
188 | exit(1); |
---|
189 | } |
---|
190 | if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1) { |
---|
191 | perror("shmat"); |
---|
192 | exit(1); |
---|
193 | } |
---|
194 | |
---|
195 | if ( (shmForAgg->v4PrefixNb = |
---|
196 | getPrefixV4(SUBNETS_LIST, |
---|
197 | &(shmForAgg->prefixV4Tab[0][0]), |
---|
198 | &(shmForAgg->prefixV4SubnetTab[0][0]), |
---|
199 | &(shmForAgg->v4SubnetNb) |
---|
200 | )) < 1){ |
---|
201 | fprintf(stderr, "renetcolSMP1: Error in %s\n", SUBNETS_LIST); |
---|
202 | } else { |
---|
203 | fprintf(stderr, "We have found %hu supernets from %s\n", |
---|
204 | shmForAgg->v4PrefixNb, |
---|
205 | SUBNETS_LIST); |
---|
206 | fprintf(stderr, "and %hu subnets\n", |
---|
207 | shmForAgg->v4SubnetNb); |
---|
208 | } |
---|
209 | if ( (shmForAgg->v4PrefixNb = |
---|
210 | getPrefixV4(SUBNETS_LIST, |
---|
211 | &(shmForAgg->prefixV4Tab[1][0]), |
---|
212 | &(shmForAgg->prefixV4SubnetTab[1][0]), |
---|
213 | &(shmForAgg->v4SubnetNb) |
---|
214 | )) < 1){ |
---|
215 | fprintf(stderr, "renetcolSMP1: Error in %s\n", SUBNETS_LIST); |
---|
216 | exit(1); |
---|
217 | } |
---|
218 | #if defined(IPV6AGGIDSNMP) |
---|
219 | if ( (shmForAgg->v6PrefixNb = |
---|
220 | getPrefixV6(SUBNETS_V6_LIST, |
---|
221 | &(shmForAgg->prefixV6Tab[0][0]) |
---|
222 | )) < 1){ |
---|
223 | fprintf(stderr, "renetcolSMP1: Error in %s\n", SUBNETS_V6_LIST); |
---|
224 | } else { |
---|
225 | fprintf(stderr, "We have found %hu IPv6 prefixes from %s\n", |
---|
226 | shmForAgg->v6PrefixNb, |
---|
227 | SUBNETS_V6_LIST); |
---|
228 | } |
---|
229 | if ( (shmForAgg->v6PrefixNb = |
---|
230 | getPrefixV6(SUBNETS_V6_LIST, |
---|
231 | &(shmForAgg->prefixV6Tab[1][0]) |
---|
232 | )) < 1){ |
---|
233 | fprintf(stderr, "renetcolSMP1: Error in %s\n", SUBNETS_V6_LIST); |
---|
234 | exit(1); |
---|
235 | } |
---|
236 | #endif |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | shmForAgg->currentTable = 0; |
---|
246 | shmForAgg->secondTable = 1; |
---|
247 | shmForAgg->readed = 0; |
---|
248 | |
---|
249 | myPtrs.rulesListPtr = NULL; |
---|
250 | myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE); |
---|
251 | tmp = myPtrs.rulesListPtr; |
---|
252 | if (tmp!=NULL){ |
---|
253 | for ( ; tmp->next; tmp=tmp->next) { |
---|
254 | if (tmp->type != 2) { |
---|
255 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | tmp = NULL; |
---|
260 | #ifdef DEBUG |
---|
261 | printRule(myPtrs.rulesListPtr); |
---|
262 | #endif |
---|
263 | |
---|
264 | #ifdef ASACC |
---|
265 | |
---|
266 | if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[0][0]))) < 1){ |
---|
267 | fprintf(stderr, "renetcolSMP1: Error in %s\n", AS_LIST); |
---|
268 | exit(1); |
---|
269 | } else { |
---|
270 | fprintf(stderr, "%hu AS are readed from %s\n", |
---|
271 | shmForAgg->ASNb, |
---|
272 | AS_LIST); |
---|
273 | } |
---|
274 | if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[1][0]))) < 1){ |
---|
275 | fprintf(stderr, "renetcolSMP1: Error in %s\n", AS_LIST); |
---|
276 | exit(1); |
---|
277 | } |
---|
278 | #endif |
---|
279 | |
---|
280 | |
---|
281 | fprintf(stderr, "renetcolSMP1: I become a deamon, next messages via syslogd. Bye.\n"); |
---|
282 | if (fork () != 0) |
---|
283 | exit (0); |
---|
284 | if (setsid() == -1){ |
---|
285 | exit(4); |
---|
286 | } |
---|
287 | |
---|
288 | myAction.sa_handler = sigusr1Mgmt; |
---|
289 | myAction.sa_flags = SA_RESTART; |
---|
290 | sigemptyset (&(myAction.sa_mask)); |
---|
291 | sigaddset (&(myAction.sa_mask), SIGALRM); |
---|
292 | sigaction (SIGUSR1, &myAction, NULL); |
---|
293 | |
---|
294 | |
---|
295 | setCache(myPtrs.rulesListPtr); |
---|
296 | myPtrs.rulesAddressPtr = (RuleDefPtr *)rulesAddress; |
---|
297 | |
---|
298 | myKey = createKey(argv[0]); |
---|
299 | myQueue = createQueue(myKey); |
---|
300 | sendMyPid(myQueue); |
---|
301 | |
---|
302 | #ifdef IPV4AGGIDSNMP |
---|
303 | k = getSNMPIndexList(SMP1_INTERFACES_TYPE_LIST, routersListPtr); |
---|
304 | #endif |
---|
305 | |
---|
306 | myPtrs.currentRouterPtr = routersListPtr; |
---|
307 | myPtrs.currentHeaderV9Ptr = (NetFlowV9HeaderPtr) |
---|
308 | malloc(sizeof(struct NetFlowV9Header)); |
---|
309 | myPtrs.offsetV9Ptr = &offsetV9; |
---|
310 | myPtrs.currentFlowsetIdPtr = ¤tFlowsetId; |
---|
311 | myPtrs.pcktPtr = (DatagramPtr) malloc(sizeof(struct Datagram)); |
---|
312 | myPtrs.currentFlowsetNumberPtr = ¤tFlowsetNumber; |
---|
313 | myPtrs.currentMIB = shmForAgg->myMIB[shmForAgg->currentTable]; |
---|
314 | myPtrs.secondMIB = shmForAgg->myMIB[shmForAgg->secondTable]; |
---|
315 | if (initMIB(myPtrs.currentMIB)!=1) { |
---|
316 | fprintf(stderr, "ERROR in MIB initialisation\n"); |
---|
317 | exit(1);} |
---|
318 | if (initMIB(myPtrs.secondMIB)!=1) { |
---|
319 | fprintf(stderr, "ERROR in second MIB initialisation\n"); |
---|
320 | exit(1);} |
---|
321 | #ifdef IPV4AGGIDR |
---|
322 | myPtrs.routersID = &indexedRouterList; |
---|
323 | #endif |
---|
324 | myPtrs.currentV4Tab = shmForAgg->prefixV4Tab[shmForAgg->currentTable]; |
---|
325 | myPtrs.secondV4Tab = shmForAgg->prefixV4Tab[shmForAgg->secondTable]; |
---|
326 | #ifdef IPV6AGGIDSNMP |
---|
327 | myPtrs.currentV6Tab = shmForAgg->prefixV6Tab[shmForAgg->currentTable]; |
---|
328 | myPtrs.secondV6Tab = shmForAgg->prefixV6Tab[shmForAgg->secondTable]; |
---|
329 | #endif |
---|
330 | #ifdef IPV6LINKAGG |
---|
331 | myPtrs.currentV6IndexTab = shmForAgg->indexV6Tab[shmForAgg->currentTable]; |
---|
332 | myPtrs.secondV6IndexTab = shmForAgg->indexV6Tab[shmForAgg->secondTable]; |
---|
333 | #endif |
---|
334 | #ifdef ASACC |
---|
335 | myPtrs.asNb = shmForAgg->ASNb; |
---|
336 | myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable]; |
---|
337 | myPtrs.secondASTab = shmForAgg->ASTab[shmForAgg->secondTable]; |
---|
338 | #endif |
---|
339 | #ifdef MATRIX |
---|
340 | myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]); |
---|
341 | #endif |
---|
342 | |
---|
343 | |
---|
344 | #ifdef MATRIX |
---|
345 | for (i=0; i<SMP1_ROUTER_INDEX_MAX; i++){ |
---|
346 | for (j=0; j<SMP1_ROUTER_INDEX_MAX; j++) { |
---|
347 | shmForAgg->matrixPOP[0][i][j].bytesNb = 0; |
---|
348 | shmForAgg->matrixPOP[0][i][j].pktsNb = 0; |
---|
349 | shmForAgg->matrixPOP[0][i][j].flowNb = 0; |
---|
350 | shmForAgg->matrixPOP[1][i][j].bytesNb = 0; |
---|
351 | shmForAgg->matrixPOP[1][i][j].pktsNb = 0; |
---|
352 | shmForAgg->matrixPOP[1][i][j].flowNb = 0; |
---|
353 | } |
---|
354 | } |
---|
355 | #endif |
---|
356 | |
---|
357 | #ifdef IPV6LINKAGG |
---|
358 | for (i=0; i<SMP1_ROUTER_INDEX_MAX; i++){ |
---|
359 | for (j=0; j<MAX_INDEX_BY_ROUTER; j++) { |
---|
360 | shmForAgg->indexV6Tab[0][i][j].routerIPAddress = 0; |
---|
361 | shmForAgg->indexV6Tab[0][i][j].indexSNMP = 0; |
---|
362 | shmForAgg->indexV6Tab[0][i][j].hasStats = 0; |
---|
363 | shmForAgg->indexV6Tab[0][i][j].sampling = 0; |
---|
364 | shmForAgg->indexV6Tab[0][i][j].bytesNbIN = 0; |
---|
365 | shmForAgg->indexV6Tab[0][i][j].pktsNbIN = 0; |
---|
366 | shmForAgg->indexV6Tab[0][i][j].flowNbIN = 0; |
---|
367 | shmForAgg->indexV6Tab[0][i][j].bytesNbOUT = 0; |
---|
368 | shmForAgg->indexV6Tab[0][i][j].pktsNbOUT = 0; |
---|
369 | shmForAgg->indexV6Tab[0][i][j].flowNbOUT = 0; |
---|
370 | shmForAgg->indexV6Tab[1][i][j].routerIPAddress = 0; |
---|
371 | shmForAgg->indexV6Tab[1][i][j].indexSNMP = 0; |
---|
372 | shmForAgg->indexV6Tab[1][i][j].hasStats = 0; |
---|
373 | shmForAgg->indexV6Tab[1][i][j].sampling = 0; |
---|
374 | shmForAgg->indexV6Tab[1][i][j].bytesNbIN = 0; |
---|
375 | shmForAgg->indexV6Tab[1][i][j].pktsNbIN = 0; |
---|
376 | shmForAgg->indexV6Tab[1][i][j].flowNbIN = 0; |
---|
377 | shmForAgg->indexV6Tab[1][i][j].bytesNbOUT = 0; |
---|
378 | shmForAgg->indexV6Tab[1][i][j].pktsNbOUT = 0; |
---|
379 | shmForAgg->indexV6Tab[1][i][j].flowNbOUT = 0; |
---|
380 | } |
---|
381 | } |
---|
382 | #endif |
---|
383 | |
---|
384 | |
---|
385 | if (myPtrs.pcktPtr==NULL) { |
---|
386 | fprintf(stderr, "ERROR in struct Datagram allocation\n"); |
---|
387 | exit(1); |
---|
388 | } else { |
---|
389 | myPtrs.pcktPtr->ipH = (IpHeaderPtr) malloc(sizeof(struct IpHeader)); |
---|
390 | myPtrs.pcktPtr->udp_header = (UdpHeaderPtr) malloc(sizeof(struct UdpHeader)); |
---|
391 | } |
---|
392 | if (! (myPtrs.ptr_buffer = malloc(sockBufSize))) |
---|
393 | { |
---|
394 | printf("ERROR during socket buffer allocation\n"); |
---|
395 | exit(2); |
---|
396 | } |
---|
397 | |
---|
398 | |
---|
399 | initStream(); |
---|
400 | |
---|
401 | socketLoop(); |
---|
402 | |
---|
403 | closelog(); |
---|
404 | |
---|
405 | fprintf(stderr, "END\n"); |
---|
406 | |
---|
407 | return (0); |
---|
408 | } |
---|
409 | |
---|
410 | char *prepend_timestamp ( char s[], size_t capacity ) |
---|
411 | { |
---|
412 | const char fmt[] = "%y-%m-%d_%H-%M"; |
---|
413 | size_t n = sizeof ( fmt ); |
---|
414 | size_t len = strlen ( s ); |
---|
415 | if ( len + n < capacity ) { |
---|
416 | time_t init = time ( NULL ); |
---|
417 | memmove ( s + n, s, len + 1 ); |
---|
418 | strftime ( s, n, fmt, localtime ( &init ) ); |
---|
419 | s[n - 1] = '_'; |
---|
420 | } |
---|
421 | else |
---|
422 | errno = ERANGE; |
---|
423 | |
---|
424 | return s; |
---|
425 | } |
---|
426 | |
---|
427 | |
---|
428 | |
---|
429 | |
---|
430 | |
---|
431 | |
---|
432 | |
---|
433 | void |
---|
434 | sendMyPid(int queueID) |
---|
435 | { |
---|
436 | msgType myMsg; |
---|
437 | char *msgTextIndex; |
---|
438 | unsigned short tplMsgType = 12; |
---|
439 | pid_t myPID; |
---|
440 | |
---|
441 | myPID = getpid(); |
---|
442 | msgTextIndex = mempcpy(mempcpy(myMsg.text, |
---|
443 | &tplMsgType, |
---|
444 | sizeof (unsigned short) |
---|
445 | ), |
---|
446 | &myPID, |
---|
447 | sizeof(pid_t) |
---|
448 | ); |
---|
449 | myMsg.type = 1; |
---|
450 | msgSend(queueID, myMsg); |
---|
451 | } |
---|
452 | |
---|
453 | |
---|
454 | |
---|
455 | |
---|
456 | |
---|
457 | |
---|
458 | |
---|
459 | void sendReadRulesSignal(int queueID) |
---|
460 | { |
---|
461 | msgType myMsg; |
---|
462 | char *msgTextIndex; |
---|
463 | unsigned short tplMsgType = 13; |
---|
464 | |
---|
465 | msgTextIndex = mempcpy(myMsg.text, |
---|
466 | &tplMsgType, |
---|
467 | sizeof (unsigned short) |
---|
468 | ); |
---|
469 | myMsg.type = 1; |
---|
470 | msgSend(queueID, myMsg); |
---|
471 | } |
---|
472 | |
---|
473 | |
---|
474 | |
---|
475 | |
---|
476 | void |
---|
477 | sigusr1Mgmt(int num) |
---|
478 | { |
---|
479 | sigusr1Up = 1; |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | |
---|
484 | |
---|
485 | void |
---|
486 | initRule() |
---|
487 | { |
---|
488 | RulesPtr tmp = NULL; |
---|
489 | initCache(); |
---|
490 | tmp = myPtrs.rulesListPtr; |
---|
491 | for ( ; tmp; tmp=tmp->next) { |
---|
492 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
493 | } |
---|
494 | myPtrs.rulesListPtr = NULL; |
---|
495 | myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE); |
---|
496 | tmp = myPtrs.rulesListPtr; |
---|
497 | for ( ; tmp; tmp=tmp->next) { |
---|
498 | if (tmp->type != 2) { |
---|
499 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
500 | } |
---|
501 | } |
---|
502 | tmp = NULL; |
---|
503 | initCache(); |
---|
504 | setCache(myPtrs.rulesListPtr); |
---|
505 | sendReadRulesSignal(myQueue); |
---|
506 | #ifdef DEBUG |
---|
507 | printRule(myPtrs.rulesListPtr); |
---|
508 | #endif |
---|
509 | } |
---|
510 | |
---|
511 | |
---|
512 | |
---|
513 | |
---|
514 | void |
---|
515 | initCache() |
---|
516 | { |
---|
517 | int i,j; |
---|
518 | for (i=0; i<FIELD_TYPE_NUMBER+1; i++){ |
---|
519 | for (j=0; j<MAX_RULES_PER_FIELD; j++){ |
---|
520 | rulesAddress[i][j] = NULL; |
---|
521 | } |
---|
522 | } |
---|
523 | } |
---|
524 | |
---|
525 | |
---|
526 | |
---|
527 | |
---|
528 | void |
---|
529 | setCache(RulesPtr rPtr) |
---|
530 | { |
---|
531 | int i=0; |
---|
532 | RulesPtr tmp = rPtr; |
---|
533 | RuleDefPtr def = NULL; |
---|
534 | |
---|
535 | #ifdef DEBUG |
---|
536 | fprintf(stderr,"Cache for Rules: \n"); |
---|
537 | #endif |
---|
538 | while (tmp) { |
---|
539 | def = tmp->def; |
---|
540 | while (def) { |
---|
541 | i = 0; |
---|
542 | while (rulesAddress[def->fieldType][i] != NULL){ |
---|
543 | i++; |
---|
544 | } |
---|
545 | rulesAddress[def->fieldType][i] = def; |
---|
546 | #ifdef DEBUG |
---|
547 | fprintf(stderr,"%d: %d \n", def->fieldType, i); |
---|
548 | #endif |
---|
549 | def = def->next; |
---|
550 | } |
---|
551 | #ifdef DEBUG |
---|
552 | fprintf(stderr,"\n"); |
---|
553 | #endif |
---|
554 | tmp = tmp->next; |
---|
555 | } |
---|
556 | #ifdef DEBUG |
---|
557 | fprintf(stderr,"Cache for Rules: OK"); |
---|
558 | #endif |
---|
559 | } |
---|
560 | |
---|
561 | |
---|
562 | |
---|
563 | |
---|
564 | |
---|
565 | |
---|
566 | void |
---|
567 | initStream() |
---|
568 | { |
---|
569 | static unsigned short n0, n1, n2, n3; |
---|
570 | |
---|
571 | #ifdef READFROMFILE |
---|
572 | initFile(); |
---|
573 | #else |
---|
574 | initSocket(); |
---|
575 | if (sscanf(receptAddress,"%hu.%hu.%hu.%hu",&n0,&n1,&n2,&n3)==0) { |
---|
576 | perror("sscanf"); |
---|
577 | } |
---|
578 | buffer4[0] = (unsigned char)n3; |
---|
579 | buffer4[1] = (unsigned char)n2; |
---|
580 | buffer4[2] = (unsigned char)n1; |
---|
581 | buffer4[3] = (unsigned char)n0; |
---|
582 | #endif |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | |
---|
587 | |
---|
588 | int |
---|
589 | socketLoop() |
---|
590 | { |
---|
591 | short shift; |
---|
592 | short version = 0; |
---|
593 | int regRouter = 0; |
---|
594 | time_t now = time((time_t *)NULL); |
---|
595 | int loopNb = 0; |
---|
596 | int gardeFou = 0; |
---|
597 | time_t lastIPOutput, lastMPLSOutput; |
---|
598 | RouterPtr tmp = routersListPtr; |
---|
599 | int dd = 0; |
---|
600 | FILE *TPLFILE; |
---|
601 | #if defined(RECORDFLOW) |
---|
602 | FILE *currentRECORDFLOWFILE; |
---|
603 | #endif |
---|
604 | now = time((time_t *)NULL); |
---|
605 | tmPtr = localtime(&now); |
---|
606 | currentIntervalle = tmPtr->tm_min; |
---|
607 | lastMPLSOutput = now; |
---|
608 | lastIPOutput = now; |
---|
609 | #if defined(RECORDFLOW) |
---|
610 | currentRECORDFLOWFILE = fRECORDF1; |
---|
611 | #endif |
---|
612 | do { |
---|
613 | #ifdef DEBUG |
---|
614 | fprintf (stderr, "["); |
---|
615 | #endif |
---|
616 | if ( ((tmPtr->tm_min)%STEP == 0) && (currentIntervalle != tmPtr->tm_min)){ |
---|
617 | currentIntervalle = tmPtr->tm_min; |
---|
618 | if (shmForAgg->currentTable == 0) { |
---|
619 | shmForAgg->currentTable = 1; |
---|
620 | shmForAgg->secondTable = 0; |
---|
621 | #if defined(RECORDFLOW) |
---|
622 | |
---|
623 | fflush(fRECORDF1); |
---|
624 | if (fclose(fRECORDF1) == NULL){ |
---|
625 | fprintf(stderr,"error fclose : %s",strerror(errno)); |
---|
626 | } |
---|
627 | strncpy(pathRF, RECORD_FILE_PATH,sizeof(RECORD_FILE_PATH)); |
---|
628 | strncpy(nameRF, SMP1_RECORD_FILE_2,sizeof(SMP1_RECORD_FILE_2)); |
---|
629 | errno = 0; |
---|
630 | prepend_timestamp ( nameRF, sizeof nameRF ); |
---|
631 | if ( errno != ERANGE ) |
---|
632 | printf ( "Timestamped: %s\n", nameRF ); |
---|
633 | else { |
---|
634 | |
---|
635 | } |
---|
636 | strncat(pathRF, nameRF, sizeof(nameRF)); |
---|
637 | |
---|
638 | if ((fRECORDF2 = fopen(pathRF, "w")) == NULL) { |
---|
639 | syslog(LOG_ERR,"Error in %s opening : %s", pathRF, strerror(errno)); |
---|
640 | } else { |
---|
641 | bufRF2 = (char *)malloc (262144); |
---|
642 | setvbuf (fRECORDF2, bufRF2, _IOFBF, 262144); |
---|
643 | } |
---|
644 | currentRECORDFLOWFILE = fRECORDF2; |
---|
645 | #endif |
---|
646 | }else{ |
---|
647 | shmForAgg->currentTable = 0; |
---|
648 | shmForAgg->secondTable = 1; |
---|
649 | #if defined(RECORDFLOW) |
---|
650 | fprintf (stderr, "AVANT free(bufRF 2 ) *****************************************************"); |
---|
651 | |
---|
652 | fflush(fRECORDF2); |
---|
653 | fclose(fRECORDF2); |
---|
654 | strncpy(pathRF, RECORD_FILE_PATH,sizeof(RECORD_FILE_PATH)); |
---|
655 | strncpy(nameRF, SMP1_RECORD_FILE_1,sizeof(SMP1_RECORD_FILE_1)); |
---|
656 | errno = 0; |
---|
657 | prepend_timestamp ( nameRF, sizeof nameRF ); |
---|
658 | if ( errno != ERANGE ) |
---|
659 | printf ( "Timestamped: %s\n", nameRF ); |
---|
660 | else { |
---|
661 | |
---|
662 | } |
---|
663 | strncat(pathRF, nameRF, sizeof(nameRF)); |
---|
664 | if ((fRECORDF1 = fopen(pathRF, "w")) == NULL) { |
---|
665 | syslog(LOG_ERR,"Error in %s opening : %s", pathRF, strerror(errno)); |
---|
666 | } else { |
---|
667 | bufRF1 = (char *)malloc (262144); |
---|
668 | setvbuf (fRECORDF1, bufRF1, _IOFBF, 262144); |
---|
669 | } |
---|
670 | currentRECORDFLOWFILE = fRECORDF1; |
---|
671 | #endif |
---|
672 | } |
---|
673 | shmForAgg->readed = 1; |
---|
674 | myPtrs.currentMIB = shmForAgg->myMIB[shmForAgg->currentTable]; |
---|
675 | myPtrs.secondMIB = shmForAgg->myMIB[shmForAgg->secondTable]; |
---|
676 | #ifdef ASACC |
---|
677 | myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable]; |
---|
678 | #endif |
---|
679 | #ifdef MATRIX |
---|
680 | myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]); |
---|
681 | #endif |
---|
682 | #ifdef IPV6LINKAGG |
---|
683 | myPtrs.currentV6IndexTab = &(shmForAgg->indexV6Tab[shmForAgg->currentTable][0][0]); |
---|
684 | myPtrs.secondV6IndexTab = &(shmForAgg->indexV6Tab[shmForAgg->secondTable][0][0]); |
---|
685 | |
---|
686 | if (!(TPLFILE = fopen("/tmp/IPV6LINKAGGSMP1.txt", "w"))) { |
---|
687 | syslog (LOG_ERR, "error during %s opening", "/tmp/IPV6LINKAGGSMP1.txt"); |
---|
688 | } |
---|
689 | tmp = routersListPtr; |
---|
690 | for (; tmp; tmp=tmp->next) { |
---|
691 | for (dd=0;dd<MAX_INDEX_BY_ROUTER;dd++) { |
---|
692 | if ( tmp->snmpIndexType[dd] != 2) { |
---|
693 | fprintf(TPLFILE, |
---|
694 | "router: %lu.%lu.%lu.%lu dd: %hu , indexSNMP : %hu , type : %hu ,total bytes IN : %llu, OUT : %llu\n", |
---|
695 | (tmp->IpAddress>>24), |
---|
696 | (tmp->IpAddress<<8>>24), |
---|
697 | (tmp->IpAddress<<16>>24), |
---|
698 | (tmp->IpAddress<<24>>24), |
---|
699 | dd, |
---|
700 | tmp->snmpIndexID[dd], |
---|
701 | tmp->snmpIndexType[dd], |
---|
702 | ((struct IndexV6 *)((myPtrs.secondV6IndexTab) |
---|
703 | +((tmp->ID)*MAX_INDEX_BY_ROUTER) |
---|
704 | +(tmp->snmpIndexID[dd])))->bytesNbIN, |
---|
705 | ((struct IndexV6 *)((myPtrs.secondV6IndexTab) |
---|
706 | +((tmp->ID)*MAX_INDEX_BY_ROUTER) |
---|
707 | +(tmp->snmpIndexID[dd])))->bytesNbOUT |
---|
708 | ); |
---|
709 | } |
---|
710 | } |
---|
711 | } |
---|
712 | fclose(TPLFILE); |
---|
713 | #endif |
---|
714 | } |
---|
715 | if (sigusr1Up == 1){ |
---|
716 | sigusr1Up = 0; |
---|
717 | initRule(); |
---|
718 | } |
---|
719 | #ifdef READFROMFILE |
---|
720 | fileReading(); |
---|
721 | #else |
---|
722 | socketReading(); |
---|
723 | #endif |
---|
724 | getIpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer); |
---|
725 | regRouter = checkIpHeader(myPtrs.pcktPtr, &sortedRouterList[0], routerNb); |
---|
726 | myPtrs.currentMIB->ipPacketNb += 1; |
---|
727 | getUdpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer); |
---|
728 | if ( checkUdpHeader(myPtrs.pcktPtr, regRouter, receptPort) == 1 ){ |
---|
729 | continue; |
---|
730 | } |
---|
731 | myPtrs.currentMIB->udpPacketNb += 1; |
---|
732 | switch( version = |
---|
733 | getNetFlowHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer, |
---|
734 | myPtrs.currentHeaderV9Ptr, myPtrs.offsetV9Ptr)){ |
---|
735 | case 9: |
---|
736 | #ifdef DEBUG |
---|
737 | fprintf (stderr, ", %d %hu %lu ,", |
---|
738 | myPtrs.currentHeaderV9Ptr->version, |
---|
739 | myPtrs.currentHeaderV9Ptr->count, |
---|
740 | myPtrs.currentHeaderV9Ptr->package_sequence); |
---|
741 | #endif |
---|
742 | myPtrs.currentMIB->v9PacketNb += 1; |
---|
743 | myPtrs.currentMIB->flowNb += myPtrs.currentHeaderV9Ptr->count; |
---|
744 | if ((myPtrs.currentRouterPtr=notExistRouter(routersListPtr, |
---|
745 | myPtrs.pcktPtr->ipH->srcAdd))==NULL) { |
---|
746 | myPtrs.currentRouterPtr = routersListPtr; |
---|
747 | } |
---|
748 | if ((enginePtrTmp = existEngId(myPtrs.currentRouterPtr,myPtrs.currentHeaderV9Ptr->sourceId))==NULL) { |
---|
749 | |
---|
750 | enginePtrTmp = newRouterEngineList(); |
---|
751 | enginePtrTmp->next = myPtrs.currentRouterPtr->engineList; |
---|
752 | if (myPtrs.currentRouterPtr->engineList!=NULL) { |
---|
753 | myPtrs.currentRouterPtr->engineList->prev=enginePtrTmp;} |
---|
754 | myPtrs.currentRouterPtr->engineList = enginePtrTmp; |
---|
755 | enginePtrTmp->engineId = myPtrs.currentHeaderV9Ptr->sourceId; |
---|
756 | enginePtrTmp->package_sequence = myPtrs.currentHeaderV9Ptr->package_sequence; |
---|
757 | } else { |
---|
758 | enginePtrTmp->engineId = myPtrs.currentHeaderV9Ptr->sourceId; |
---|
759 | if (enginePtrTmp->package_sequence != (myPtrs.currentHeaderV9Ptr->package_sequence-1)) { |
---|
760 | myPtrs.currentMIB->v9UnSeqNb += 1; |
---|
761 | } |
---|
762 | enginePtrTmp->package_sequence = myPtrs.currentHeaderV9Ptr->package_sequence; |
---|
763 | } |
---|
764 | currentFlowsetNumber = 0; |
---|
765 | shift = 0; |
---|
766 | gardeFou=0; |
---|
767 | while ((currentFlowsetNumber < myPtrs.currentHeaderV9Ptr->count) |
---|
768 | && (shift+48 < myPtrs.pcktPtr->udp_header->length)) { |
---|
769 | gardeFou++; |
---|
770 | curTplFlSetPtr = NULL; |
---|
771 | currentFlowsetId=getFlowsetId(currentFlowsetNumber, |
---|
772 | myPtrs.offsetV9Ptr, myPtrs.ptr_buffer); |
---|
773 | myPtrs.currentMIB->flowSetNb += 1; |
---|
774 | if ( currentFlowsetId == 0 ) { |
---|
775 | if ( (shift = checkTemplateFlowSet(shift, myPtrs.currentRouterPtr, |
---|
776 | myPtrs.currentMIB, |
---|
777 | myPtrs.offsetV9Ptr, |
---|
778 | myPtrs.ptr_buffer, |
---|
779 | myPtrs.currentHeaderV9Ptr, |
---|
780 | curTplFlSetPtr, |
---|
781 | myPtrs.currentFlowsetNumberPtr, |
---|
782 | myQueue)) < 0 ) { |
---|
783 | #ifdef DEBUG |
---|
784 | fprintf (stderr, "w> "); |
---|
785 | #endif |
---|
786 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
787 | } |
---|
788 | |
---|
789 | } else if (currentFlowsetId == 1) { |
---|
790 | if ( (shift = checkTemplateOption(shift, myPtrs.currentRouterPtr, |
---|
791 | myPtrs.currentMIB, |
---|
792 | myPtrs.offsetV9Ptr, |
---|
793 | myPtrs.ptr_buffer, |
---|
794 | myPtrs.currentHeaderV9Ptr, |
---|
795 | curTplOptionPtr, |
---|
796 | myPtrs.currentFlowsetNumberPtr) ) < 0 ) { |
---|
797 | #ifdef DEBUG |
---|
798 | fprintf (stderr, "wo> "); |
---|
799 | #endif |
---|
800 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
801 | } |
---|
802 | } else if (currentFlowsetId > 255) { |
---|
803 | #if defined(RECORDFLOW) |
---|
804 | if ( (shift = checkDataFlowSet(shift, |
---|
805 | &myPtrs, |
---|
806 | myQueue, |
---|
807 | shmForAgg->prefixV4Tab[shmForAgg->currentTable], |
---|
808 | (size_t) shmForAgg->v4PrefixNb, |
---|
809 | shmForAgg->prefixV4SubnetTab[shmForAgg->currentTable], |
---|
810 | (size_t) shmForAgg->v4SubnetNb, |
---|
811 | currentRECORDFLOWFILE) ) < 0 ) { |
---|
812 | #else |
---|
813 | if ( (shift = checkDataFlowSet(shift, |
---|
814 | &myPtrs, |
---|
815 | myQueue, |
---|
816 | shmForAgg->prefixV4Tab[shmForAgg->currentTable], |
---|
817 | (size_t) shmForAgg->v4PrefixNb, |
---|
818 | shmForAgg->prefixV4SubnetTab[shmForAgg->currentTable], |
---|
819 | (size_t) shmForAgg->v4SubnetNb) ) < 0 ) { |
---|
820 | #endif |
---|
821 | #ifdef DEBUG |
---|
822 | fprintf (stderr, "wd> "); |
---|
823 | #endif |
---|
824 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
825 | } |
---|
826 | } else { |
---|
827 | #ifdef DEBUG |
---|
828 | fprintf (stderr, " 1<<255 "); |
---|
829 | #endif |
---|
830 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
831 | } |
---|
832 | if ( gardeFou > 200) { exit(-1); } |
---|
833 | } |
---|
834 | #ifdef DEBUG |
---|
835 | if (shift > 1456) { |
---|
836 | fprintf(stderr," SHIT SHIFT > 1456 : %d \n", shift); |
---|
837 | } |
---|
838 | #endif |
---|
839 | break; |
---|
840 | default: |
---|
841 | syslog(LOG_INFO, |
---|
842 | "NetFlow exp. version unknown: %hu, from router: %lu.%lu.%lu.%lu", |
---|
843 | (unsigned short) version, |
---|
844 | (myPtrs.pcktPtr->ipH->srcAdd>>24), |
---|
845 | (myPtrs.pcktPtr->ipH->srcAdd<<8>>24), |
---|
846 | (myPtrs.pcktPtr->ipH->srcAdd<<16>>24), |
---|
847 | (myPtrs.pcktPtr->ipH->srcAdd<<24>>24)); |
---|
848 | break; |
---|
849 | } |
---|
850 | if (loopNb > 140000 ){ |
---|
851 | |
---|
852 | writeAllTplFlSet(); |
---|
853 | #ifdef DEBUG |
---|
854 | fprintf(stderr, "W"); |
---|
855 | #endif |
---|
856 | loopNb = 0; |
---|
857 | } |
---|
858 | loopNb++; |
---|
859 | |
---|
860 | now = time((time_t *)NULL); |
---|
861 | tmPtr = localtime(&now); |
---|
862 | #ifdef DEBUG |
---|
863 | fprintf (stderr, "]\n"); |
---|
864 | fprintf (stderr, "IP: %llu , UDP: %llu, NDEv9: %llu, drops: %llu, FS: %llu, Flows: %llu, realFl: %llu Diff:%llu\n", |
---|
865 | myPtrs.currentMIB->ipPacketNb, |
---|
866 | myPtrs.currentMIB->udpPacketNb, |
---|
867 | myPtrs.currentMIB->v9PacketNb, |
---|
868 | myPtrs.currentMIB->v9UnSeqNb, |
---|
869 | myPtrs.currentMIB->flowSetNb, |
---|
870 | myPtrs.currentMIB->flowNb, |
---|
871 | myPtrs.currentMIB->realFlowNb, |
---|
872 | myPtrs.currentMIB->flowNb - myPtrs.currentMIB->realFlowNb |
---|
873 | ); |
---|
874 | #endif |
---|
875 | } while (1); |
---|
876 | } |
---|
877 | |
---|
878 | |
---|
879 | |
---|
880 | |
---|
881 | short |
---|
882 | initSocket() |
---|
883 | { |
---|
884 | inputSock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); |
---|
885 | if (inputSock < 0) |
---|
886 | { |
---|
887 | syslog(LOG_ERR,"socket : %s", strerror(errno)); |
---|
888 | exit(1); |
---|
889 | } |
---|
890 | memset((void*)&name, 0, sizeof(name)); |
---|
891 | name.sin_family = AF_INET; |
---|
892 | |
---|
893 | name.sin_addr.s_addr = inet_addr(SMP1_RECEPTION_ADDRESS); |
---|
894 | if (name.sin_addr.s_addr == INADDR_NONE){ |
---|
895 | syslog(LOG_ERR, " INADDR_NONE "); |
---|
896 | exit(1); |
---|
897 | } |
---|
898 | name.sin_port = htons(receptPort); |
---|
899 | if (bind(inputSock,(struct sockaddr *)(&name), sizeof(name)) < 0) |
---|
900 | { |
---|
901 | syslog(LOG_ERR, "bind : %s", strerror(errno)); |
---|
902 | exit(1); |
---|
903 | } |
---|
904 | sockNamelg = sizeof(name); |
---|
905 | if (getsockname(inputSock, (struct sockaddr *) (&name), |
---|
906 | (socklen_t *)&sockNamelg) < 0) |
---|
907 | { |
---|
908 | syslog(LOG_ERR, "getsockname: %s", strerror(errno)); |
---|
909 | exit(1); |
---|
910 | } |
---|
911 | |
---|
912 | |
---|
913 | inputSock2 = socket(AF_INET, SOCK_DGRAM, 0); |
---|
914 | if (inputSock2 < 0) |
---|
915 | { |
---|
916 | syslog(LOG_ERR,"socket : %s", strerror(errno)); |
---|
917 | exit(1); |
---|
918 | } |
---|
919 | memset((void*)&name, 0, sizeof(name)); |
---|
920 | name2.sin_family = AF_INET; |
---|
921 | |
---|
922 | name2.sin_addr.s_addr = inet_addr(SMP1_RECEPTION_ADDRESS); |
---|
923 | if (name2.sin_addr.s_addr == INADDR_NONE){ |
---|
924 | syslog(LOG_ERR, " INADDR_NONE "); |
---|
925 | exit(1); |
---|
926 | } |
---|
927 | name2.sin_port = htons(receptPort); |
---|
928 | if (bind(inputSock2,(struct sockaddr *)(&name2), sizeof(name2)) < 0) |
---|
929 | { |
---|
930 | syslog(LOG_ERR, "bind : %s", strerror(errno)); |
---|
931 | exit(1); |
---|
932 | } |
---|
933 | sockNamelg = sizeof(name2); |
---|
934 | if (getsockname(inputSock2, (struct sockaddr *) (&name2), |
---|
935 | (socklen_t *)&sockNamelg) < 0) |
---|
936 | { |
---|
937 | syslog(LOG_ERR, "getsockname: %s", strerror(errno)); |
---|
938 | exit(1); |
---|
939 | } |
---|
940 | return(0); |
---|
941 | } |
---|
942 | |
---|
943 | |
---|
944 | |
---|
945 | |
---|
946 | short initFile() |
---|
947 | { |
---|
948 | |
---|
949 | } |
---|
950 | |
---|
951 | |
---|
952 | |
---|
953 | |
---|
954 | short |
---|
955 | socketReading() |
---|
956 | { |
---|
957 | signed short sockLg; |
---|
958 | |
---|
959 | sockNameFromlg = sizeof(fromName); |
---|
960 | rcv = recvfrom(inputSock, myPtrs.ptr_buffer, sockBufSize, 0, |
---|
961 | (struct sockaddr *)(&fromName), |
---|
962 | (socklen_t *)&sockNameFromlg); |
---|
963 | sockLg = rcv; |
---|
964 | if (sockLg < 0) { |
---|
965 | syslog(LOG_ERR,"recvfrom : %s", strerror(errno)); |
---|
966 | exit(1); |
---|
967 | } |
---|
968 | if (sockLg == 0) { |
---|
969 | syslog(LOG_ERR,"recvfrom : %s", strerror(errno)); |
---|
970 | exit(1); |
---|
971 | } |
---|
972 | return(0); |
---|
973 | } |
---|
974 | |
---|
975 | |
---|
976 | |
---|
977 | |
---|
978 | short fileReading() |
---|
979 | { |
---|
980 | #ifdef READFROMFILE |
---|
981 | struct pcap_pkthdr myPcapHeader; |
---|
982 | const u_char *ethernetPacket; |
---|
983 | |
---|
984 | if ((ethernetPacket = pcap_next(myPcapHandle, &myPcapHeader)) == NULL) { |
---|
985 | fprintf (stderr, "IP: %llu , UDP: %llu, NDEv9: %llu, drops: %llu, FS: %llu, Flows: %llu, realFl: %llu Diff:%llu\n", |
---|
986 | myPtrs.currentMIB->ipPacketNb, |
---|
987 | myPtrs.currentMIB->udpPacketNb, |
---|
988 | myPtrs.currentMIB->v9PacketNb, |
---|
989 | myPtrs.currentMIB->v9UnSeqNb, |
---|
990 | myPtrs.currentMIB->flowSetNb, |
---|
991 | myPtrs.currentMIB->flowNb, |
---|
992 | myPtrs.currentMIB->realFlowNb, |
---|
993 | myPtrs.currentMIB->flowNb - myPtrs.currentMIB->realFlowNb |
---|
994 | ); |
---|
995 | pcap_close(myPcapHandle); |
---|
996 | exit(0); |
---|
997 | } |
---|
998 | myPtrs.ptr_buffer = (DatagramPtr) (ethernetPacket + 14); |
---|
999 | #ifdef DEBUG |
---|
1000 | fprintf(stderr,"P"); |
---|
1001 | #endif |
---|
1002 | #endif |
---|
1003 | } |
---|
1004 | |
---|
1005 | |
---|
1006 | |
---|
1007 | |
---|
1008 | short |
---|
1009 | checkFlow(short flowNumber) |
---|
1010 | { |
---|
1011 | return(0); |
---|
1012 | } |
---|
1013 | |
---|
1014 | |
---|
1015 | |
---|
1016 | |
---|
1017 | |
---|
1018 | |
---|
1019 | void |
---|
1020 | showAllTplFlSet() |
---|
1021 | { |
---|
1022 | RouterPtr tmp = routersListPtr; |
---|
1023 | TplFlowSetPtr tmpFS; |
---|
1024 | TplOptionPtr tmpOP; |
---|
1025 | fprintf(stderr,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n"); |
---|
1026 | for (; tmp; tmp=tmp->next) { |
---|
1027 | fprintf(stderr,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n", |
---|
1028 | (tmp->IpAddress>>24), |
---|
1029 | (tmp->IpAddress<<8>>24), |
---|
1030 | (tmp->IpAddress<<16>>24), |
---|
1031 | (tmp->IpAddress<<24>>24)); |
---|
1032 | tmpFS = tmp->tplList; |
---|
1033 | for (; tmpFS; tmpFS=tmpFS->next) { |
---|
1034 | fprintf(stderr,"TId %hu (sourceId: %lu):\n", |
---|
1035 | tmpFS->templateFlowSetId, |
---|
1036 | tmpFS->sourceId); |
---|
1037 | printFieldSet(stderr, tmpFS->fieldSet); |
---|
1038 | fprintf(stderr,"\n"); |
---|
1039 | } |
---|
1040 | if ((tmpOP = tmp->tplOptList) != NULL){ |
---|
1041 | for (; tmpOP; tmpOP=tmpOP->next) { |
---|
1042 | fprintf(stderr,"OpTId %hu (sourceId: %lu) >\n", |
---|
1043 | tmpOP->templateOptionId, |
---|
1044 | tmpOP->sourceId); |
---|
1045 | printFieldSet(stderr, tmpOP->fieldSet); |
---|
1046 | fprintf(stderr,"\n"); |
---|
1047 | } |
---|
1048 | fprintf(stderr,"\n"); |
---|
1049 | } |
---|
1050 | } |
---|
1051 | } |
---|
1052 | |
---|
1053 | |
---|
1054 | |
---|
1055 | |
---|
1056 | |
---|
1057 | |
---|
1058 | void |
---|
1059 | writeAllTplFlSet() |
---|
1060 | { |
---|
1061 | |
---|
1062 | RouterPtr tmp = routersListPtr; |
---|
1063 | TplFlowSetPtr tmpFS; |
---|
1064 | TplOptionPtr tmpOP; |
---|
1065 | FILE *TPLFILE; |
---|
1066 | if (!(TPLFILE = fopen("/tmp/TemplateDefSMP1.txt", "w"))) { |
---|
1067 | syslog (LOG_ERR, "error during %s opening", "/tmp/TemplateDefSMP1.txt"); |
---|
1068 | } |
---|
1069 | for (; tmp; tmp=tmp->next) { |
---|
1070 | tmpFS = tmp->tplList; |
---|
1071 | for (; tmpFS; tmpFS=tmpFS->next) { |
---|
1072 | fprintf(TPLFILE,"%lu.%lu.%lu.%lu TId %hu %lu ", |
---|
1073 | (tmp->IpAddress>>24), |
---|
1074 | (tmp->IpAddress<<8>>24), |
---|
1075 | (tmp->IpAddress<<16>>24), |
---|
1076 | (tmp->IpAddress<<24>>24), |
---|
1077 | tmpFS->templateFlowSetId, |
---|
1078 | tmpFS->sourceId); |
---|
1079 | printFieldSet(TPLFILE, tmpFS->fieldSet); |
---|
1080 | fprintf(TPLFILE,"\n"); |
---|
1081 | } |
---|
1082 | if ((tmpOP = tmp->tplOptList) != NULL){ |
---|
1083 | for (; tmpOP; tmpOP=tmpOP->next) { |
---|
1084 | fprintf(TPLFILE,"%lu.%lu.%lu.%lu OpTId %hu %lu %d %hu %hu %hu ", |
---|
1085 | (tmp->IpAddress>>24), |
---|
1086 | (tmp->IpAddress<<8>>24), |
---|
1087 | (tmp->IpAddress<<16>>24), |
---|
1088 | (tmp->IpAddress<<24>>24), |
---|
1089 | tmpOP->templateOptionId, |
---|
1090 | tmpOP->sourceId, |
---|
1091 | tmpOP->optionScopeLg/4, |
---|
1092 | tmpOP->length, |
---|
1093 | tmpOP->optionScopeLg, |
---|
1094 | tmpOP->optionLg); |
---|
1095 | printFieldSet(TPLFILE, tmpOP->fieldSet); |
---|
1096 | fprintf(TPLFILE,"\n"); |
---|
1097 | } |
---|
1098 | |
---|
1099 | } |
---|
1100 | } |
---|
1101 | fclose(TPLFILE); |
---|
1102 | } |
---|
1103 | |
---|
1104 | |
---|
1105 | |
---|
1106 | |
---|
1107 | int initMIB(struct RenetcolMIB *theMIB) |
---|
1108 | { |
---|
1109 | theMIB->ipPacketNb = 0; |
---|
1110 | theMIB->udpPacketNb = 0; |
---|
1111 | theMIB->v9PacketNb = 0; |
---|
1112 | theMIB->v9UnSeqNb = 0; |
---|
1113 | theMIB->flowSetNb = 0; |
---|
1114 | theMIB->dataFlowSetNb = 0; |
---|
1115 | theMIB->defFlowSetNb = 0; |
---|
1116 | theMIB->optDataFlowSetNb = 0; |
---|
1117 | theMIB->optDefFlowSetNb = 0; |
---|
1118 | theMIB->flowNb = 0; |
---|
1119 | theMIB->realFlowNb = 0; |
---|
1120 | theMIB->ipv4Bytes = 0; |
---|
1121 | theMIB->ipv4Pckts = 0; |
---|
1122 | theMIB->ipv4Flow = 0; |
---|
1123 | theMIB->ipv4IcmpFlowNb = 0; |
---|
1124 | theMIB->ipv4IcmpBytesNb = 0; |
---|
1125 | theMIB->ipv4IcmpPktsNb = 0; |
---|
1126 | theMIB->ipv4UDPFlowNb = 0; |
---|
1127 | theMIB->ipv4UDPBytesNb = 0; |
---|
1128 | theMIB->ipv4UDPPktsNb = 0; |
---|
1129 | theMIB->ipv4TCPFlowNb = 0; |
---|
1130 | theMIB->ipv4TCPBytesNb = 0; |
---|
1131 | theMIB->ipv4TCPPktsNb = 0; |
---|
1132 | theMIB->ipv4OthersFlowNb = 0; |
---|
1133 | theMIB->ipv4OthersBytesNb = 0; |
---|
1134 | theMIB->ipv4OthersPktsNb = 0; |
---|
1135 | theMIB->ipv4FlowSizePcktsE1 = 0; |
---|
1136 | theMIB->ipv4FlowSizePcktsLT10 = 0; |
---|
1137 | theMIB->ipv4FlowSizePcktsLT100 = 0; |
---|
1138 | theMIB->ipv4FlowSizePcktsLT1k = 0; |
---|
1139 | theMIB->ipv4FlowSizePcktsLT10k = 0; |
---|
1140 | theMIB->ipv4FlowSizePcktsMT10k = 0; |
---|
1141 | theMIB->ipv4FlowSizeBytesLT50 = 0; |
---|
1142 | theMIB->ipv4FlowSizeBytesLT100 = 0; |
---|
1143 | theMIB->ipv4FlowSizeBytesLT1k = 0; |
---|
1144 | theMIB->ipv4FlowSizeBytesLT10k = 0; |
---|
1145 | theMIB->ipv4FlowSizeBytesLT100k = 0; |
---|
1146 | theMIB->ipv4FlowSizeBytesLT1M = 0; |
---|
1147 | theMIB->ipv4FlowSizeBytesLT10M = 0; |
---|
1148 | theMIB->ipv4FlowSizeBytesLT100M = 0; |
---|
1149 | theMIB->ipv4FlowSizeBytesMT100M = 0; |
---|
1150 | theMIB->ipv4WebBytesNb = 0; |
---|
1151 | theMIB->ipv4WebPcktsNb = 0; |
---|
1152 | theMIB->ipv4WebFlowNb = 0; |
---|
1153 | theMIB->ipv4DNSBytesNb = 0; |
---|
1154 | theMIB->ipv4DNSPcktsNb = 0; |
---|
1155 | theMIB->ipv4DNSFlowNb = 0; |
---|
1156 | theMIB->ipv4OthersApBytesNb = 0; |
---|
1157 | theMIB->ipv4OthersApPcktsNb = 0; |
---|
1158 | theMIB->ipv4OthersApFlowNb = 0; |
---|
1159 | theMIB->ipv6Bytes = 0; |
---|
1160 | theMIB->ipv6Pckts = 0; |
---|
1161 | theMIB->ipv6Flow = 0; |
---|
1162 | theMIB->ipv6IcmpFlowNb = 0; |
---|
1163 | theMIB->ipv6IcmpBytesNb = 0; |
---|
1164 | theMIB->ipv6IcmpPktsNb = 0; |
---|
1165 | theMIB->ipv6UDPFlowNb = 0; |
---|
1166 | theMIB->ipv6UDPBytesNb = 0; |
---|
1167 | theMIB->ipv6UDPPktsNb = 0; |
---|
1168 | theMIB->ipv6TCPFlowNb = 0; |
---|
1169 | theMIB->ipv6TCPBytesNb = 0; |
---|
1170 | theMIB->ipv6TCPPktsNb = 0; |
---|
1171 | theMIB->ipv6OthersFlowNb = 0; |
---|
1172 | theMIB->ipv6OthersBytesNb = 0; |
---|
1173 | theMIB->ipv6OthersPktsNb = 0; |
---|
1174 | theMIB->ipv6FlowSizePcktsE1 = 0; |
---|
1175 | theMIB->ipv6FlowSizePcktsLT10 = 0; |
---|
1176 | theMIB->ipv6FlowSizePcktsLT100 = 0; |
---|
1177 | theMIB->ipv6FlowSizePcktsLT1k = 0; |
---|
1178 | theMIB->ipv6FlowSizePcktsLT10k = 0; |
---|
1179 | theMIB->ipv6FlowSizePcktsMT10k = 0; |
---|
1180 | theMIB->ipv6FlowSizeBytesLT50 = 0; |
---|
1181 | theMIB->ipv6FlowSizeBytesLT100 = 0; |
---|
1182 | theMIB->ipv6FlowSizeBytesLT1k = 0; |
---|
1183 | theMIB->ipv6FlowSizeBytesLT10k = 0; |
---|
1184 | theMIB->ipv6FlowSizeBytesLT100k = 0; |
---|
1185 | theMIB->ipv6FlowSizeBytesLT1M = 0; |
---|
1186 | theMIB->ipv6FlowSizeBytesLT10M = 0; |
---|
1187 | theMIB->ipv6FlowSizeBytesLT100M = 0; |
---|
1188 | theMIB->ipv6FlowSizeBytesMT100M = 0; |
---|
1189 | theMIB->ipv6WebBytesNb = 0; |
---|
1190 | theMIB->ipv6WebPcktsNb = 0; |
---|
1191 | theMIB->ipv6WebFlowNb = 0; |
---|
1192 | theMIB->ipv6DNSBytesNb = 0; |
---|
1193 | theMIB->ipv6DNSPcktsNb = 0; |
---|
1194 | theMIB->ipv6DNSFlowNb = 0; |
---|
1195 | theMIB->ipv6OthersApBytesNb = 0; |
---|
1196 | theMIB->ipv6OthersApPcktsNb = 0; |
---|
1197 | theMIB->ipv6OthersApFlowNb = 0; |
---|
1198 | return 1; |
---|
1199 | } |
---|