root/trunk/src/renetcol.c @ 111

Revision 111, 24.9 KB (checked in by andreu, 13 years ago)

ticket #41 Pcap file integration

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