root/trunk/src/renetcol.c @ 94

Revision 94, 20.6 KB (checked in by andreu, 14 years ago)

IPv6 aggregation : prefix reading

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