root/trunk/src/renetcol.c @ 95

Revision 95, 21.9 KB (checked in by andreu, 14 years ago)

IPv6 link aggregation mode (first step : identify flows)

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