root/trunk/src/renetcol.c @ 154

Revision 154, 30.3 KB (checked in by andreu, 12 years ago)

Typo correction and copyright update

  • Property svn:eol-style set to native
RevLine 
[110]1/*
[2]2 * File: renetcol.c
3 *
[28]4 * Authors: ANDREU Francois-Xavier
[2]5 *
[154]6 * Copyright (C) 2005-2011 GIP RENATER
[2]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
[36]28struct MyPtrs myPtrs; /* defined in routers_mgmt.h */
[23]29
[24]30struct SHMForAgg *shmForAgg;
31
[23]32unsigned short currentIPOutputFile = 0;    /* index on the current IP file */
33unsigned short currentMPLSOutputFile = 0;  /* index on the current MPLS file */
[19]34char outputName[256];
[23]35char cof_str[20];                          /* current ouput file name */
36FILE *fIP;                                 /* pointer on the IP output file */
37FILE *fMPLS;                              /* pointer on the MPLS output file */
[18]38
[22]39struct sigaction myAction; /* handler for renetcolSender communication */
40sigjmp_buf contextSigalrm; /* '' */
[8]41volatile sig_atomic_t sigusr1Up = 0;
[22]42/* struct sigaction myAction;  for timer not used in this version */
43
[23]44unsigned short currentIntervalle = 0;        /* based on 5 min intervalles */ 
[22]45
[23]46unsigned short offsetV9 = 0;                 /* index in a neflow v9 packet */
[22]47
[2]48unsigned char buffer1;
49unsigned char buffer2[2];
50unsigned char buffer4[4];
51unsigned char buffer6[6];
[22]52
[2]53short currentFlowsetNumber;
54short currentFlowsetId;
[22]55TplFlowSetPtr curTplFlSetPtr; /* current template flowset pointer */
[2]56TplOptionPtr curTplOptionPtr;
[113]57RouterPtr routersListPtr;
58NDEEnginePtr enginePtrTmp;
[23]59
[2]60unsigned long sortedRouterList[ROUTER_INDEX_MAX];
61unsigned long indexedRouterList[ROUTER_INDEX_MAX];
[23]62int routerNb = 0;                                  /* routers number */
63
[36]64key_t myKey = 0;
65int myQueue = 0;
[24]66
[22]67/* Next structure used as cache in rules reading */
68RuleDefPtr rulesAddress[FIELD_TYPE_NUMBER+1][MAX_RULES_PER_FIELD];
69
[8]70int reInitConf = 0;
[22]71
72static struct tm *tmPtr;
73
[2]74extern int errno;
75static int inputSock;
76static int sockNamelg;
[17]77static int inputSock2;
[2]78static int sockNameFromlg;
79static int rcv;
80static int sockBufSize = SOCKET_BUFFER_SIZE;           
[8]81/* static unsigned short flowNumber; */
[2]82static unsigned short receptPort = RECEPTION_PORT;
[23]83/* static unsigned char *ptr_buffer; */
[2]84static char *receptAddress = RECEPTION_ADDRESS;
85static struct sockaddr_in name;
[17]86static struct sockaddr_in name2;
[2]87static struct sockaddr_in fromName;
[125]88char *log = "renetcol";
[111]89#ifdef READFROMFILE
90  char *myPcapFileName, errBuffer[PCAP_ERRBUF_SIZE];
91  pcap_t *myPcapHandle;
92#endif
[2]93
94/*
95 * Main
96 * ----
97 */
98int 
99main (int argc, char *argv[])
100{
101  RulesPtr tmp = NULL;
[24]102  int shmid;
103  key_t key;
[36]104  int i,j;
[58]105  int k;
[95]106  unsigned short routerIndex = 0;
[8]107
[111]108#if defined(READFROMFILE)
109  if ( argc != 2) {
110    fprintf (stderr,
111             "%s: Usage: %s <pcap file>\n exp: %s /tmp/my_pcap_file.pcap\n",
112             argv[0], argv[0], argv[0]);
113    exit(1);
114  }
115  myPcapFileName = argv[1];
116  if ((myPcapHandle = pcap_open_offline(myPcapFileName, errBuffer)) == NULL) {
117    fprintf(stderr, "Couldn't open PCAP file %s: %s\n", myPcapFileName, errBuffer);
118    exit(1);
119  }
120#endif
121
[76]122#if defined(IPV4AGGIDR)
[96]123  fprintf(stderr, "renetcol compilation and execution with IPv4 Prefixes Aggregation feature enable based on routers ID in %s file.\n", ROUTERS_LIST);
[22]124#endif
[59]125
[76]126#if defined(IPV4AGGIDSNMP)
[96]127  fprintf(stderr, "renetcol compilation and execution with IPv4 Prefixes Aggregation feature enable based on SNMP ID in %s file.\n", INTERFACES_TYPE_LIST);
[59]128#endif
[94]129#if defined(IPV6AGGIDSNMP)
[96]130  fprintf(stderr, "renetcol compilation and execution with IPv6 Prefixes Aggregation feature enable based on SNMP ID in %s file.\n", INTERFACES_TYPE_LIST);
[94]131#endif
[95]132#if defined(IPV6LINKAGG)
133  fprintf(stderr, "renetcol compilation and execution with IPv6 Links Aggregation.\n");
134#endif
[111]135
[125]136  /*   openlog(argv[0], LOG_PID, LOG_USER);*/
137  openlog(log, NULL, LOG_LOCAL7);
[2]138
139  initCache();
[58]140  /* GET the list of routers who are available to send NDE to the collector */
141  if ( (routerNb = getRegisteredRouters(ROUTERS_LIST,
142                                        &sortedRouterList[0],
143                                        &indexedRouterList[0])) == -1) {
144    fprintf(stderr,
145            "ERROR in getRegisteredRouters from file %s\n",
146            ROUTERS_LIST);
147    exit(-1);
148  }
149  /* Create the list of routers */
[95]150  for (routerIndex=0; routerIndex<routerNb; routerIndex++){
151    routersListPtr = addRouter(routersListPtr, sortedRouterList[routerIndex],routerIndex);
[58]152  }
153 
154  /* Shared memory */
[24]155  key = 8765;
156  if ((shmid = shmget(key, SHMSIZE, IPC_CREAT | 0666)) < 0) {
157    perror("shmget");
158    exit(1);
159  }
160  if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1) {
161    perror("shmat");
162    exit(1);
163  }
164
[61]165  if ( (shmForAgg->v4PrefixNb =
166        getPrefixV4(SUBNETS_LIST,
167                    &(shmForAgg->prefixV4Tab[0][0]),
168                    &(shmForAgg->prefixV4SubnetTab[0][0]),
169                    &(shmForAgg->v4SubnetNb)
170                    )) < 1){
[59]171    fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST);
172  } else {
[61]173    fprintf(stderr, "We have found %hu supernets from %s\n",
[59]174            shmForAgg->v4PrefixNb,
175            SUBNETS_LIST);
[61]176    fprintf(stderr, "and %hu subnets\n",
177            shmForAgg->v4SubnetNb);   
[2]178  }
[61]179  if ( (shmForAgg->v4PrefixNb =
180        getPrefixV4(SUBNETS_LIST,
181                    &(shmForAgg->prefixV4Tab[1][0]),
182                    &(shmForAgg->prefixV4SubnetTab[1][0]),
183                    &(shmForAgg->v4SubnetNb)
184                    )) < 1){
[59]185    fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST);
[24]186    exit(1);
187  }
[94]188#if defined(IPV6AGGIDSNMP)
189  if ( (shmForAgg->v6PrefixNb =
190        getPrefixV6(SUBNETS_V6_LIST,
191                    &(shmForAgg->prefixV6Tab[0][0])
192                    )) < 1){
193    fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_V6_LIST);
194  } else {
195    fprintf(stderr, "We have found %hu IPv6 prefixes from %s\n",
196            shmForAgg->v6PrefixNb,
197            SUBNETS_V6_LIST);
198  }
199  if ( (shmForAgg->v6PrefixNb =
200        getPrefixV6(SUBNETS_V6_LIST,
201                    &(shmForAgg->prefixV6Tab[1][0])
202                    )) < 1){
203    fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_V6_LIST);
204    exit(1);
205  }
206#endif
207
[61]208/*   for (i=0; i<shmForAgg->v4PrefixNb; i++){ */
209/*    fprintf(stderr, "SUPERNET : %lu, %hu\n",shmForAgg->prefixV4Tab[0][i].beginning, shmForAgg->prefixV4Tab[0][i].hasSubnet); */
210/*   } */
211/*   for (i=0; i<shmForAgg->v4SubnetNb; i++){ */
212/*    fprintf(stderr, "SUBNET : %lu, %hu\n",shmForAgg->prefixV4SubnetTab[0][i].beginning, shmForAgg->prefixV4SubnetTab[0][i].hasSubnet); */
213/*   } */
[29]214
215  shmForAgg->currentTable = 0;
216  shmForAgg->secondTable = 1;
[47]217  shmForAgg->readed = 0;
[58]218  /* get the rules */
[23]219  myPtrs.rulesListPtr = NULL;
[58]220  myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE);
[23]221  tmp = myPtrs.rulesListPtr;
[88]222 if (tmp!=NULL){
223   for ( ; tmp->next; tmp=tmp->next) {
224     if (tmp->type != 2) {
225       myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
226     }
227   }
228 }
[36]229  tmp = NULL;
[47]230#ifdef DEBUG
[88]231  printRule(myPtrs.rulesListPtr);
[47]232#endif
[8]233
[60]234#ifdef ASACC
235  /* get the AS list */
236  if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[0][0]))) < 1){
237    fprintf(stderr, "renetcol: Error in %s\n", AS_LIST);
238    exit(1);
239  } else {
240    fprintf(stderr, "%hu AS are readed from %s\n",
241            shmForAgg->ASNb,
242            AS_LIST);
243  }
244  if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[1][0]))) < 1){
245    fprintf(stderr, "renetcol: Error in %s\n", AS_LIST);
246    exit(1);
247  }
248#endif
249
[58]250  /* the fork */
[75]251  fprintf(stderr, "renetcol: I become a deamon, next messages via syslogd. Bye.\n");
[8]252  if (fork () != 0)
253    exit (0);
254  if (setsid() == -1){
255    exit(4);
256  }
[15]257  /* handler, SIGUSR1 from renetcolSender */
[8]258  myAction.sa_handler = sigusr1Mgmt;
259  myAction.sa_flags = SA_RESTART;
260  sigemptyset (&(myAction.sa_mask));
261  sigaddset (&(myAction.sa_mask), SIGALRM);
262  sigaction (SIGUSR1, &myAction, NULL);
263
[58]264  /* Cache for fast rules access */
[23]265  setCache(myPtrs.rulesListPtr);
266  myPtrs.rulesAddressPtr = (RuleDefPtr *)rulesAddress;
[58]267  /* IPC messages queue init */
[2]268  myKey = createKey(argv[0]);
269  myQueue = createQueue(myKey);
[8]270  sendMyPid(myQueue);
[58]271  /* get SNMP index information */
[76]272#ifdef IPV4AGGIDSNMP
[58]273  k = getSNMPIndexList(INTERFACES_TYPE_LIST, routersListPtr);
[76]274#endif
[2]275  /* INIT THE CURRENTS VARIABLES*/
[23]276  myPtrs.currentRouterPtr = routersListPtr;
277  myPtrs.currentHeaderV9Ptr = (NetFlowV9HeaderPtr)
[2]278    malloc(sizeof(struct NetFlowV9Header));
[23]279  myPtrs.offsetV9Ptr = &offsetV9;
280  myPtrs.currentFlowsetIdPtr = &currentFlowsetId;
281  myPtrs.pcktPtr = (DatagramPtr) malloc(sizeof(struct Datagram));
282  myPtrs.currentFlowsetNumberPtr = &currentFlowsetNumber;
[113]283  myPtrs.currentMIB = shmForAgg->myMIB[shmForAgg->currentTable];
284  myPtrs.secondMIB = shmForAgg->myMIB[shmForAgg->secondTable];
285  if (initMIB(myPtrs.currentMIB)!=1) {
286    fprintf(stderr, "ERROR in MIB initialisation\n");
287    exit(1);}
288  if (initMIB(myPtrs.secondMIB)!=1) {
289    fprintf(stderr, "ERROR in second MIB initialisation\n");
290    exit(1);}
[59]291#ifdef IPV4AGGIDR
[27]292  myPtrs.routersID = &indexedRouterList;
[59]293#endif
[29]294  myPtrs.currentV4Tab = shmForAgg->prefixV4Tab[shmForAgg->currentTable];
[36]295  myPtrs.secondV4Tab = shmForAgg->prefixV4Tab[shmForAgg->secondTable];
[95]296#ifdef IPV6AGGIDSNMP
297  myPtrs.currentV6Tab = shmForAgg->prefixV6Tab[shmForAgg->currentTable];
298  myPtrs.secondV6Tab = shmForAgg->prefixV6Tab[shmForAgg->secondTable];
299#endif
300#ifdef IPV6LINKAGG
301  myPtrs.currentV6IndexTab = shmForAgg->indexV6Tab[shmForAgg->currentTable];
302  myPtrs.secondV6IndexTab = shmForAgg->indexV6Tab[shmForAgg->secondTable];
303#endif
[60]304#ifdef ASACC
305  myPtrs.asNb = shmForAgg->ASNb;
306  myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable];
307  myPtrs.secondASTab = shmForAgg->ASTab[shmForAgg->secondTable];
308#endif
[59]309#ifdef MATRIX
[36]310  myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]);
[59]311#endif
[36]312
[58]313  /* INIT MATRIX TABLES */
[59]314#ifdef MATRIX
[36]315  for (i=0; i<ROUTER_INDEX_MAX; i++){
316    for (j=0; j<ROUTER_INDEX_MAX; j++) {
317      shmForAgg->matrixPOP[0][i][j].bytesNb = 0;
318      shmForAgg->matrixPOP[0][i][j].pktsNb = 0;
319      shmForAgg->matrixPOP[0][i][j].flowNb = 0;
320      shmForAgg->matrixPOP[1][i][j].bytesNb = 0;
321      shmForAgg->matrixPOP[1][i][j].pktsNb = 0;
322      shmForAgg->matrixPOP[1][i][j].flowNb = 0;
323    }
324  }
[59]325#endif
[95]326  /* INIT IPV6 index TABLES */
327#ifdef IPV6LINKAGG
328  for (i=0; i<ROUTER_INDEX_MAX; i++){
329    for (j=0; j<MAX_INDEX_BY_ROUTER; j++) {
[111]330      shmForAgg->indexV6Tab[0][i][j].routerIPAddress = 0;
331      shmForAgg->indexV6Tab[0][i][j].indexSNMP = 0;
[96]332      shmForAgg->indexV6Tab[0][i][j].hasStats = 0;
333      shmForAgg->indexV6Tab[0][i][j].sampling = 0;
[95]334      shmForAgg->indexV6Tab[0][i][j].bytesNbIN = 0;
335      shmForAgg->indexV6Tab[0][i][j].pktsNbIN = 0;
336      shmForAgg->indexV6Tab[0][i][j].flowNbIN = 0;
[96]337      shmForAgg->indexV6Tab[0][i][j].bytesNbOUT = 0;
338      shmForAgg->indexV6Tab[0][i][j].pktsNbOUT = 0;
339      shmForAgg->indexV6Tab[0][i][j].flowNbOUT = 0;
[111]340      shmForAgg->indexV6Tab[1][i][j].routerIPAddress = 0;
341      shmForAgg->indexV6Tab[1][i][j].indexSNMP = 0;
[96]342      shmForAgg->indexV6Tab[1][i][j].hasStats = 0;
343      shmForAgg->indexV6Tab[1][i][j].sampling = 0;
344      shmForAgg->indexV6Tab[1][i][j].bytesNbIN = 0;
345      shmForAgg->indexV6Tab[1][i][j].pktsNbIN = 0;
346      shmForAgg->indexV6Tab[1][i][j].flowNbIN = 0;
[95]347      shmForAgg->indexV6Tab[1][i][j].bytesNbOUT = 0;
348      shmForAgg->indexV6Tab[1][i][j].pktsNbOUT = 0;
349      shmForAgg->indexV6Tab[1][i][j].flowNbOUT = 0;
350    }
351  }
352#endif
[36]353
[58]354  /* Checkup */
[23]355  if (myPtrs.pcktPtr==NULL) {
[2]356    fprintf(stderr, "ERROR in struct Datagram allocation\n");
357    exit(1);
358  } else {
[23]359    myPtrs.pcktPtr->ipH = (IpHeaderPtr) malloc(sizeof(struct IpHeader));
360    myPtrs.pcktPtr->udp_header = (UdpHeaderPtr) malloc(sizeof(struct UdpHeader));
[2]361  }
[23]362  if (! (myPtrs.ptr_buffer = malloc(sockBufSize))) 
[2]363    {
364      printf("ERROR during socket buffer allocation\n");
365      exit(2);
366    }
367
368  /* INIT INPUT STREAM*/
369  initStream();
370
[23]371  socketLoop(); /* all work on datagram is made here */
[2]372
373  closelog();
[23]374
[2]375  fprintf(stderr, "END\n");
[23]376
[2]377  return (0);
378}
379
380/*
[23]381 * send his PID to another program via IPC message queue
[2]382 *
[23]383 * @param queueID the IPC queue ident
384 *
[2]385 */
[23]386void 
387sendMyPid(int queueID)
[8]388{
389  msgType myMsg;
390  char *msgTextIndex;
391  unsigned short tplMsgType = 12;
392  pid_t myPID;
393 
394  myPID = getpid();
395  msgTextIndex = mempcpy(mempcpy(myMsg.text,
396                                 &tplMsgType,
397                                 sizeof (unsigned short)
398                                 ),
399                         &myPID,
400                         sizeof(pid_t)
401                         );
402  myMsg.type = 1;
403  msgSend(queueID, myMsg);
404}
405
406/*
[23]407 * Send the "read rules list" message to another program
[8]408 *
[23]409 * @param queueID the IPC queue ident
410 *
[8]411 */
412void sendReadRulesSignal(int queueID)
413{
414  msgType myMsg;
415  char *msgTextIndex;
416  unsigned short tplMsgType = 13;
417 
418  msgTextIndex = mempcpy(myMsg.text,
419                         &tplMsgType,
420                         sizeof (unsigned short)
421                         );
422  myMsg.type = 1;
423  msgSend(queueID, myMsg);
424}
425
426/*
[23]427 *
[8]428 */
[23]429void 
430sigusr1Mgmt(int num)
[8]431{
432  sigusr1Up = 1;
433}
434
435/*
[23]436 * init the rule structure
[8]437 */
438void 
439initRule()
440{
441  RulesPtr tmp = NULL;
442  initCache();
[23]443  tmp = myPtrs.rulesListPtr;
[8]444  for ( ; tmp; tmp=tmp->next) {
[23]445    myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
[8]446  }
[23]447  myPtrs.rulesListPtr = NULL;
[58]448  myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE);
[23]449  tmp = myPtrs.rulesListPtr;
[8]450  for ( ; tmp; tmp=tmp->next) {
451    if (tmp->type != 2) {
[23]452      myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
[8]453    }
454  }
455  tmp = NULL;
456  initCache();
[23]457  setCache(myPtrs.rulesListPtr);
[8]458  sendReadRulesSignal(myQueue);
[47]459#ifdef DEBUG
460  printRule(myPtrs.rulesListPtr);
461#endif
[8]462}
463
464/*
[23]465 * init cache table
[8]466 */
[2]467void
468initCache()
469{
470  int i,j;
471  for (i=0; i<FIELD_TYPE_NUMBER+1; i++){
472    for (j=0; j<MAX_RULES_PER_FIELD; j++){
473      rulesAddress[i][j] = NULL;
474    }
475  }
476}
477
478/*
[18]479 * setCache()
[2]480 */
481void
482setCache(RulesPtr rPtr)
483{
484  int i=0;
485  RulesPtr tmp = rPtr;
486  RuleDefPtr def = NULL;
487
[47]488#ifdef DEBUG
[111]489  fprintf(stderr,"Cache for Rules: \n");
[47]490#endif 
[2]491  while (tmp) {
492    def = tmp->def;
493    while (def) {
494      i = 0;
495      while (rulesAddress[def->fieldType][i] != NULL){
496        i++;
497      }
498      rulesAddress[def->fieldType][i] = def;
[47]499#ifdef DEBUG
[111]500      fprintf(stderr,"%d: %d \n", def->fieldType, i);
[47]501#endif     
[111]502      def = def->next;
[2]503    }
[47]504#ifdef DEBUG
505    fprintf(stderr,"\n");
506#endif     
[2]507    tmp = tmp->next;
508  }
[111]509#ifdef DEBUG
510  fprintf(stderr,"Cache for Rules: OK");
511#endif 
[2]512}
513
514/*
515 * initStream()
[111]516 *
517 * Stream can be a file (pcap format) or a socket
[2]518 */
519void 
520initStream()
521{
522  static unsigned short n0, n1, n2, n3;
[22]523
[111]524#ifdef READFROMFILE
525  initFile();
526#else
[2]527  initSocket();
528  if (sscanf(receptAddress,"%hu.%hu.%hu.%hu",&n0,&n1,&n2,&n3)==0) {
529    perror("sscanf");
530  }             
531  buffer4[0] = (unsigned char)n3;
532  buffer4[1] = (unsigned char)n2;
533  buffer4[2] = (unsigned char)n1;
534  buffer4[3] = (unsigned char)n0;
[111]535#endif
[2]536}
537
538/*
539 * socketLoop()
540 */
541int 
542socketLoop()
543{
[27]544  short shift;
[2]545  short version = 0;
[8]546  int regRouter = 0;
[19]547  time_t now = time((time_t *)NULL);
[23]548  int loopNb = 0;
[27]549  int gardeFou = 0;
[19]550  time_t lastIPOutput, lastMPLSOutput;
[96]551  RouterPtr tmp = routersListPtr; 
552  int dd = 0;
553  FILE *TPLFILE;
[19]554
555  now = time((time_t *)NULL);
[22]556  tmPtr = localtime(&now);
557  currentIntervalle = tmPtr->tm_min;
[19]558  lastMPLSOutput = now;
559  lastIPOutput = now;
[2]560  do {
[28]561#ifdef DEBUG   
562    fprintf (stderr, "[");
563#endif
[24]564    if ( ((tmPtr->tm_min)%STEP == 0) && (currentIntervalle != tmPtr->tm_min)){
[22]565      currentIntervalle = tmPtr->tm_min;
[24]566      if (shmForAgg->currentTable == 0) {
567        shmForAgg->currentTable = 1;
[29]568        shmForAgg->secondTable = 0;
[24]569      }else{
570        shmForAgg->currentTable = 0;
[29]571        shmForAgg->secondTable = 1;
[24]572      }
573      shmForAgg->readed = 1;
[113]574      myPtrs.currentMIB = shmForAgg->myMIB[shmForAgg->currentTable];
575      myPtrs.secondMIB = shmForAgg->myMIB[shmForAgg->secondTable];
[60]576#ifdef ASACC
577      myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable];
578#endif
[59]579#ifdef MATRIX
[36]580      myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]);
[59]581#endif
[96]582#ifdef IPV6LINKAGG
583      myPtrs.currentV6IndexTab = &(shmForAgg->indexV6Tab[shmForAgg->currentTable][0][0]);
584      myPtrs.secondV6IndexTab = &(shmForAgg->indexV6Tab[shmForAgg->secondTable][0][0]);
[113]585      /* FIXME : to delete : */
[96]586      if (!(TPLFILE = fopen("/tmp/IPV6LINKAGG.txt", "w"))) {
587        syslog (LOG_ERR, "error during %s opening", "/tmp/IPV6LINKAGG.txt");
588      }
589      tmp = routersListPtr;
590      for (; tmp; tmp=tmp->next) {
591        for (dd=0;dd<MAX_INDEX_BY_ROUTER;dd++) {
592          if ( tmp->snmpIndexType[dd] != 2) {
593            fprintf(TPLFILE,
594                    "router: %lu.%lu.%lu.%lu dd: %hu , indexSNMP : %hu , type : %hu ,total bytes IN : %llu, OUT : %llu\n",
595                    (tmp->IpAddress>>24),
596                    (tmp->IpAddress<<8>>24),
597                    (tmp->IpAddress<<16>>24),
598                    (tmp->IpAddress<<24>>24),
599                    dd,
600                    tmp->snmpIndexID[dd],
601                    tmp->snmpIndexType[dd],
602                    ((struct IndexV6 *)((myPtrs.secondV6IndexTab)
[97]603                                        +((tmp->ID)*MAX_INDEX_BY_ROUTER)
[96]604                                        +(tmp->snmpIndexID[dd])))->bytesNbIN,
605                    ((struct IndexV6 *)((myPtrs.secondV6IndexTab)
[97]606                                        +((tmp->ID)*MAX_INDEX_BY_ROUTER)
[96]607                                        +(tmp->snmpIndexID[dd])))->bytesNbOUT
608                    );
609          }
610        }
611      }
[113]612      fclose(TPLFILE); /* <-- */
[96]613#endif
[22]614    }
[8]615    if (sigusr1Up == 1){
616      sigusr1Up = 0;
617      initRule();
618    }
[111]619#ifdef READFROMFILE
620    fileReading();
621#else
[2]622    socketReading();
[111]623#endif
[23]624    getIpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer);
625    regRouter = checkIpHeader(myPtrs.pcktPtr, &sortedRouterList[0], routerNb);
[113]626    myPtrs.currentMIB->ipPacketNb += 1;
[23]627    getUdpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer);
628    if ( checkUdpHeader(myPtrs.pcktPtr, regRouter, receptPort) == 1 ){
629      continue;
630    }
[113]631    myPtrs.currentMIB->udpPacketNb += 1;
[2]632    switch( version =
[23]633            getNetFlowHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer,
634                             myPtrs.currentHeaderV9Ptr, myPtrs.offsetV9Ptr)){
[2]635    case 9:
[111]636#ifdef DEBUG
637      fprintf (stderr, ", %d %hu %lu ,",
638               myPtrs.currentHeaderV9Ptr->version,
639               myPtrs.currentHeaderV9Ptr->count,
640               myPtrs.currentHeaderV9Ptr->package_sequence);
641#endif
[113]642      myPtrs.currentMIB->v9PacketNb += 1;
643      myPtrs.currentMIB->flowNb += myPtrs.currentHeaderV9Ptr->count;
[23]644      if ((myPtrs.currentRouterPtr=notExistRouter(routersListPtr,
645                                                  myPtrs.pcktPtr->ipH->srcAdd))==NULL) {
646        myPtrs.currentRouterPtr = routersListPtr;
[2]647      }
[113]648      if ((enginePtrTmp = existEngId(myPtrs.currentRouterPtr,myPtrs.currentHeaderV9Ptr->sourceId))==NULL) {
649        /* add new engineID */
650        enginePtrTmp = newRouterEngineList();
651        enginePtrTmp->next = myPtrs.currentRouterPtr->engineList;
652        if (myPtrs.currentRouterPtr->engineList!=NULL) {
653          myPtrs.currentRouterPtr->engineList->prev=enginePtrTmp;}
654        myPtrs.currentRouterPtr->engineList = enginePtrTmp;
655        enginePtrTmp->engineId = myPtrs.currentHeaderV9Ptr->sourceId;
656        enginePtrTmp->package_sequence = myPtrs.currentHeaderV9Ptr->package_sequence;
657      } else {
658        enginePtrTmp->engineId = myPtrs.currentHeaderV9Ptr->sourceId;
659        if (enginePtrTmp->package_sequence != (myPtrs.currentHeaderV9Ptr->package_sequence-1)) {
660          myPtrs.currentMIB->v9UnSeqNb += 1;
661        }
662        enginePtrTmp->package_sequence = myPtrs.currentHeaderV9Ptr->package_sequence;
663      }
[2]664      currentFlowsetNumber = 0;
665      shift = 0;
[27]666      gardeFou=0;
[113]667      while ((currentFlowsetNumber < myPtrs.currentHeaderV9Ptr->count)
668             && (shift+48 < myPtrs.pcktPtr->udp_header->length)) {
[27]669        gardeFou++;
[2]670        curTplFlSetPtr = NULL;
671        currentFlowsetId=getFlowsetId(currentFlowsetNumber,
[23]672                                      myPtrs.offsetV9Ptr, myPtrs.ptr_buffer);
[113]673        myPtrs.currentMIB->flowSetNb += 1;
[2]674        if ( currentFlowsetId == 0 ) {
[113]675          if ( (shift = checkTemplateFlowSet(shift, myPtrs.currentRouterPtr,
676                                             myPtrs.currentMIB,
[57]677                                             myPtrs.offsetV9Ptr,
678                                             myPtrs.ptr_buffer,
679                                             myPtrs.currentHeaderV9Ptr,
[27]680                                             curTplFlSetPtr,
681                                             myPtrs.currentFlowsetNumberPtr,
682                                             myQueue)) < 0 ) {
[30]683#ifdef DEBUG
[111]684            fprintf (stderr, "w> ");
[28]685#endif
[27]686            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
[113]687          }
688          /* writeAllTplFlSet(); */
[2]689        } else if (currentFlowsetId == 1) {
[113]690          if ( (shift = checkTemplateOption(shift, myPtrs.currentRouterPtr,
691                                            myPtrs.currentMIB,
[57]692                                            myPtrs.offsetV9Ptr,
693                                            myPtrs.ptr_buffer,
694                                            myPtrs.currentHeaderV9Ptr,
695                                            curTplOptionPtr,
[27]696                                            myPtrs.currentFlowsetNumberPtr) ) < 0 ) {
[30]697#ifdef DEBUG
[111]698            fprintf (stderr, "wo> ");
[30]699#endif 
[27]700            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
701          }
[30]702        } else if (currentFlowsetId > 255) {
[27]703          if ( (shift = checkDataFlowSet(shift,
704                                         &myPtrs,
705                                         myQueue,
706                                         shmForAgg->prefixV4Tab[shmForAgg->currentTable],
[61]707                                         (size_t) shmForAgg->v4PrefixNb,
708                                         shmForAgg->prefixV4SubnetTab[shmForAgg->currentTable],
709                                         (size_t) shmForAgg->v4SubnetNb) ) < 0 ) {
[28]710#ifdef DEBUG   
[111]711            fprintf (stderr, "wd> ");
[28]712#endif
[27]713            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
714          }
[30]715        } else {
716#ifdef DEBUG   
[111]717          fprintf (stderr, " 1<<255 ");
[30]718#endif
719          currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
[2]720        }
[27]721        if ( gardeFou > 200) { exit(-1); }
[23]722      } /* end while flowset exist */
[111]723#ifdef DEBUG
724      if (shift > 1456) {
725        fprintf(stderr," SHIT SHIFT > 1456 : %d \n", shift);
726      }
727#endif
[2]728      break;
729    default:
[23]730      syslog(LOG_INFO,
731             "NetFlow exp. version unknown: %hu, from router: %lu.%lu.%lu.%lu",
732             (unsigned short) version,
733             (myPtrs.pcktPtr->ipH->srcAdd>>24),
734             (myPtrs.pcktPtr->ipH->srcAdd<<8>>24),
735             (myPtrs.pcktPtr->ipH->srcAdd<<16>>24),
736             (myPtrs.pcktPtr->ipH->srcAdd<<24>>24));
[2]737      break;
738    }
[120]739    if (loopNb  > 140000 ){
[23]740      /* FIXME perhaps call this function via the compilation options */
741      writeAllTplFlSet();
[28]742#ifdef DEBUG   
743      fprintf(stderr, "W");
744#endif
[23]745      loopNb = 0;
746    }
747    loopNb++;
[18]748
[19]749    now = time((time_t *)NULL);
[22]750    tmPtr = localtime(&now);
[28]751#ifdef DEBUG   
752    fprintf (stderr, "]\n");
[113]753    fprintf (stderr, "IP: %llu , UDP: %llu, NDEv9: %llu, drops: %llu, FS: %llu, Flows: %llu, realFl: %llu Diff:%llu\n",
754             myPtrs.currentMIB->ipPacketNb,
755             myPtrs.currentMIB->udpPacketNb,
756             myPtrs.currentMIB->v9PacketNb,
757             myPtrs.currentMIB->v9UnSeqNb,
758             myPtrs.currentMIB->flowSetNb,
759             myPtrs.currentMIB->flowNb,
760             myPtrs.currentMIB->realFlowNb,
761             myPtrs.currentMIB->flowNb - myPtrs.currentMIB->realFlowNb
762             );
[28]763#endif
[2]764  } while (1);
765}
766
767/*
768 * initSocket()
769 */
770short 
771initSocket()
772{
773  inputSock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
774  if (inputSock < 0)
775    {
776      syslog(LOG_ERR,"socket : %s", strerror(errno));
777      exit(1);
778    }
779  memset((void*)&name, 0, sizeof(name));
780  name.sin_family = AF_INET;
781  name.sin_addr.s_addr = htonl(INADDR_ANY);
782  if (name.sin_addr.s_addr == INADDR_NONE){
783    syslog(LOG_ERR, " INADDR_NONE ");
784    exit(1);
785  }
786  name.sin_port = htons(receptPort);
787  if (bind(inputSock,(struct sockaddr *)(&name), sizeof(name)) < 0)
788    {
789      syslog(LOG_ERR, "bind : %s", strerror(errno));
790      exit(1);
791    }                                                           
792  sockNamelg = sizeof(name);
793  if (getsockname(inputSock, (struct sockaddr *) (&name),
794                  (socklen_t *)&sockNamelg) < 0)
795    {
796      syslog(LOG_ERR, "getsockname: %s", strerror(errno));
797      exit(1);
798    }
[17]799  /* Here socket DGRAM creation, only to not have a unreachable service */
[23]800  /* message in return */
[17]801  inputSock2 = socket(AF_INET, SOCK_DGRAM, 0);
802  if (inputSock2 < 0)
803    {
804      syslog(LOG_ERR,"socket : %s", strerror(errno));
805      exit(1);
806    }
807  memset((void*)&name, 0, sizeof(name));
808  name2.sin_family = AF_INET;
809  name2.sin_addr.s_addr = htonl(INADDR_ANY);
810  if (name2.sin_addr.s_addr == INADDR_NONE){
811    syslog(LOG_ERR, " INADDR_NONE ");
812    exit(1);
813  }
814  name2.sin_port = htons(receptPort);
815  if (bind(inputSock2,(struct sockaddr *)(&name2), sizeof(name2)) < 0)
816    {
817      syslog(LOG_ERR, "bind : %s", strerror(errno));
818      exit(1);
819    }                                                           
820  sockNamelg = sizeof(name2);
821  if (getsockname(inputSock2, (struct sockaddr *) (&name2),
822                  (socklen_t *)&sockNamelg) < 0)
823    {
824      syslog(LOG_ERR, "getsockname: %s", strerror(errno));
825      exit(1);
826    }
[2]827  return(0);
828}
829
[111]830/*
831 * initFile
832 */
833short initFile()
834{
835 
836}
[2]837
838/*
[111]839 * socketReading
[2]840 */
841short 
842socketReading()
843{
844  signed short sockLg;
845
846  sockNameFromlg = sizeof(fromName);
[23]847  rcv = recvfrom(inputSock, myPtrs.ptr_buffer, sockBufSize, 0,
[2]848                 (struct sockaddr *)(&fromName),
849                 (socklen_t *)&sockNameFromlg);   
850  sockLg = rcv;
851  if (sockLg < 0) {
852    syslog(LOG_ERR,"recvfrom : %s", strerror(errno));
853    exit(1);
854  }
855  if (sockLg == 0) {
856    syslog(LOG_ERR,"recvfrom : %s", strerror(errno));
857    exit(1);
858  }
859  return(0);
860}
[111]861
862/*
863 * fileReading
864 */
865short fileReading()
866{
867#ifdef READFROMFILE
868  struct pcap_pkthdr myPcapHeader; 
869  const u_char *ethernetPacket;
870
871  if ((ethernetPacket = pcap_next(myPcapHandle, &myPcapHeader)) == NULL) {
[121]872    fprintf (stderr, "IP: %llu , UDP: %llu, NDEv9: %llu, drops: %llu, FS: %llu, Flows: %llu, realFl: %llu Diff:%llu\n",
873             myPtrs.currentMIB->ipPacketNb,
874             myPtrs.currentMIB->udpPacketNb,
875             myPtrs.currentMIB->v9PacketNb,
876             myPtrs.currentMIB->v9UnSeqNb,
877             myPtrs.currentMIB->flowSetNb,
878             myPtrs.currentMIB->flowNb,
879             myPtrs.currentMIB->realFlowNb,
880             myPtrs.currentMIB->flowNb - myPtrs.currentMIB->realFlowNb
881             );
[111]882    pcap_close(myPcapHandle);
883    exit(0);
884  }
885  myPtrs.ptr_buffer = (DatagramPtr) (ethernetPacket + 14);
886#ifdef DEBUG
887  fprintf(stderr,"P");
888#endif
889#endif
890}
[2]891 
892/*
[23]893 * check up flow datagramme 
[2]894 */
895short 
896checkFlow(short flowNumber)
897{
[23]898  return(0); /* FIXME : why this function ??? */
[2]899}
900
901/*
902 * showAllTplFlSet
903 *
[23]904 * to use only in debug mode
[2]905 */
906void 
907showAllTplFlSet()
908{
909  RouterPtr tmp = routersListPtr;
910  TplFlowSetPtr tmpFS;
911  TplOptionPtr tmpOP;
[22]912  fprintf(stderr,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n");
[2]913  for (; tmp; tmp=tmp->next) {
[22]914    fprintf(stderr,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n",
[2]915            (tmp->IpAddress>>24),
916            (tmp->IpAddress<<8>>24),
917            (tmp->IpAddress<<16>>24),
918            (tmp->IpAddress<<24>>24));
919    tmpFS =  tmp->tplList;
920    for (; tmpFS; tmpFS=tmpFS->next) {
[22]921      fprintf(stderr,"TId %hu (sourceId: %lu):\n",
922              tmpFS->templateFlowSetId,
923              tmpFS->sourceId);
[23]924      printFieldSet(stderr, tmpFS->fieldSet);
[2]925      fprintf(stderr,"\n");
926    }
927    if ((tmpOP = tmp->tplOptList) != NULL){
928      for (; tmpOP; tmpOP=tmpOP->next) {
[22]929        fprintf(stderr,"OpTId %hu (sourceId: %lu) >\n",
930                tmpOP->templateOptionId,
931                tmpOP->sourceId);
[23]932        printFieldSet(stderr, tmpOP->fieldSet);
[2]933        fprintf(stderr,"\n");
934      }
935      fprintf(stderr,"\n");
936    }
937  }
938}
[23]939
940
941/*
942 * writeAllTplFlSet
943 *
944 */
945void 
946writeAllTplFlSet()
947{
948
949  RouterPtr tmp = routersListPtr;
950  TplFlowSetPtr tmpFS;
951  TplOptionPtr tmpOP;
952  FILE *TPLFILE;
953  if (!(TPLFILE = fopen("/tmp/TemplateDef.txt", "w"))) {
954    syslog (LOG_ERR, "error during %s opening", "/tmp/TemplateDef.txt");
955  }
956  for (; tmp; tmp=tmp->next) {
957    tmpFS =  tmp->tplList;
958    for (; tmpFS; tmpFS=tmpFS->next) {
[120]959      fprintf(TPLFILE,"%lu.%lu.%lu.%lu TId %hu %lu ",
960              (tmp->IpAddress>>24),
961              (tmp->IpAddress<<8>>24),
962              (tmp->IpAddress<<16>>24),
963              (tmp->IpAddress<<24>>24),
[23]964              tmpFS->templateFlowSetId,
965              tmpFS->sourceId);
966      printFieldSet(TPLFILE, tmpFS->fieldSet);
967      fprintf(TPLFILE,"\n");
968    }
969    if ((tmpOP = tmp->tplOptList) != NULL){
970      for (; tmpOP; tmpOP=tmpOP->next) {
[120]971        fprintf(TPLFILE,"%lu.%lu.%lu.%lu OpTId %hu %lu %d %hu %hu %hu ",
972                (tmp->IpAddress>>24),
973                (tmp->IpAddress<<8>>24),
974                (tmp->IpAddress<<16>>24),
975                (tmp->IpAddress<<24>>24),
[23]976                tmpOP->templateOptionId,
[27]977                tmpOP->sourceId,
[120]978                tmpOP->optionScopeLg/4, /* last x brackets are scope */
[27]979                tmpOP->length,
980                tmpOP->optionScopeLg,
981                tmpOP->optionLg);
[23]982        printFieldSet(TPLFILE, tmpOP->fieldSet);
983        fprintf(TPLFILE,"\n");
984      }
[120]985      /*      fprintf(TPLFILE,"\n");*/
[23]986    }
987  }
988  fclose(TPLFILE);
989}
[113]990
991/*
992 * initMIB
993 */
994int initMIB(struct RenetcolMIB *theMIB)
995{
996  theMIB->ipPacketNb = 0;
997  theMIB->udpPacketNb = 0;
998  theMIB->v9PacketNb = 0;
999  theMIB->v9UnSeqNb = 0;
1000  theMIB->flowSetNb = 0;
1001  theMIB->dataFlowSetNb = 0;
1002  theMIB->defFlowSetNb = 0;
1003  theMIB->optDataFlowSetNb = 0;
1004  theMIB->optDefFlowSetNb = 0;
1005  theMIB->flowNb = 0;         
1006  theMIB->realFlowNb = 0;
[114]1007  theMIB->ipv4Bytes = 0;
1008  theMIB->ipv4Pckts = 0;
1009  theMIB->ipv4Flow = 0;   
[113]1010  theMIB->ipv4IcmpFlowNb = 0;
1011  theMIB->ipv4IcmpBytesNb = 0;
1012  theMIB->ipv4IcmpPktsNb = 0;
1013  theMIB->ipv4UDPFlowNb = 0;
1014  theMIB->ipv4UDPBytesNb = 0;
1015  theMIB->ipv4UDPPktsNb = 0;
1016  theMIB->ipv4TCPFlowNb = 0;
1017  theMIB->ipv4TCPBytesNb = 0;
1018  theMIB->ipv4TCPPktsNb = 0;
1019  theMIB->ipv4OthersFlowNb = 0;
1020  theMIB->ipv4OthersBytesNb = 0;
1021  theMIB->ipv4OthersPktsNb = 0;
1022  theMIB->ipv4FlowSizePcktsE1 = 0;   
1023  theMIB->ipv4FlowSizePcktsLT10 = 0;
1024  theMIB->ipv4FlowSizePcktsLT100 = 0;
1025  theMIB->ipv4FlowSizePcktsLT1k = 0;
1026  theMIB->ipv4FlowSizePcktsLT10k = 0;
1027  theMIB->ipv4FlowSizePcktsMT10k = 0;
1028  theMIB->ipv4FlowSizeBytesLT50 = 0;
1029  theMIB->ipv4FlowSizeBytesLT100 = 0;
1030  theMIB->ipv4FlowSizeBytesLT1k = 0;
1031  theMIB->ipv4FlowSizeBytesLT10k = 0;
1032  theMIB->ipv4FlowSizeBytesLT100k = 0;
1033  theMIB->ipv4FlowSizeBytesLT1M = 0; 
1034  theMIB->ipv4FlowSizeBytesLT10M = 0;
1035  theMIB->ipv4FlowSizeBytesLT100M = 0;
1036  theMIB->ipv4FlowSizeBytesMT100M = 0;
1037  theMIB->ipv4WebBytesNb = 0;
1038  theMIB->ipv4WebPcktsNb = 0;
1039  theMIB->ipv4WebFlowNb = 0;
1040  theMIB->ipv4DNSBytesNb = 0;
1041  theMIB->ipv4DNSPcktsNb = 0;
1042  theMIB->ipv4DNSFlowNb = 0;
1043  theMIB->ipv4OthersApBytesNb = 0;
1044  theMIB->ipv4OthersApPcktsNb = 0;
1045  theMIB->ipv4OthersApFlowNb = 0;
[114]1046  theMIB->ipv6Bytes = 0;
1047  theMIB->ipv6Pckts = 0;
1048  theMIB->ipv6Flow = 0;
[113]1049  theMIB->ipv6IcmpFlowNb = 0;
1050  theMIB->ipv6IcmpBytesNb = 0;
1051  theMIB->ipv6IcmpPktsNb = 0;
1052  theMIB->ipv6UDPFlowNb = 0;
1053  theMIB->ipv6UDPBytesNb = 0;
1054  theMIB->ipv6UDPPktsNb = 0;
1055  theMIB->ipv6TCPFlowNb = 0;
1056  theMIB->ipv6TCPBytesNb = 0;
1057  theMIB->ipv6TCPPktsNb = 0;
1058  theMIB->ipv6OthersFlowNb = 0;
1059  theMIB->ipv6OthersBytesNb = 0;
1060  theMIB->ipv6OthersPktsNb = 0;
1061  theMIB->ipv6FlowSizePcktsE1 = 0;     
1062  theMIB->ipv6FlowSizePcktsLT10 = 0;   
1063  theMIB->ipv6FlowSizePcktsLT100 = 0; 
1064  theMIB->ipv6FlowSizePcktsLT1k = 0;   
1065  theMIB->ipv6FlowSizePcktsLT10k = 0; 
1066  theMIB->ipv6FlowSizePcktsMT10k = 0; 
1067  theMIB->ipv6FlowSizeBytesLT50 = 0;   
1068  theMIB->ipv6FlowSizeBytesLT100 = 0; 
1069  theMIB->ipv6FlowSizeBytesLT1k = 0;   
1070  theMIB->ipv6FlowSizeBytesLT10k = 0; 
1071  theMIB->ipv6FlowSizeBytesLT100k = 0;
1072  theMIB->ipv6FlowSizeBytesLT1M = 0;   
1073  theMIB->ipv6FlowSizeBytesLT10M = 0; 
1074  theMIB->ipv6FlowSizeBytesLT100M = 0;
[114]1075  theMIB->ipv6FlowSizeBytesMT100M = 0; 
[113]1076  theMIB->ipv6WebBytesNb = 0;
1077  theMIB->ipv6WebPcktsNb = 0;
1078  theMIB->ipv6WebFlowNb = 0;
1079  theMIB->ipv6DNSBytesNb = 0;
1080  theMIB->ipv6DNSPcktsNb = 0;
1081  theMIB->ipv6DNSFlowNb = 0;
1082  theMIB->ipv6OthersApBytesNb = 0;
1083  theMIB->ipv6OthersApPcktsNb = 0;
1084  theMIB->ipv6OthersApFlowNb = 0;
1085  return 1;
1086}
Note: See TracBrowser for help on using the browser.