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