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