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
Line 
1/*
2 * File: renetcol.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2005-2011 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;
58NDEEnginePtr enginePtrTmp;
59
60unsigned long sortedRouterList[ROUTER_INDEX_MAX];
61unsigned long indexedRouterList[ROUTER_INDEX_MAX];
62int routerNb = 0;                                  /* routers number */
63
64key_t myKey = 0;
65int myQueue = 0;
66
67/* Next structure used as cache in rules reading */
68RuleDefPtr rulesAddress[FIELD_TYPE_NUMBER+1][MAX_RULES_PER_FIELD];
69
70int reInitConf = 0;
71
72static struct tm *tmPtr;
73
74extern int errno;
75static int inputSock;
76static int sockNamelg;
77static int inputSock2;
78static int sockNameFromlg;
79static int rcv;
80static int sockBufSize = SOCKET_BUFFER_SIZE;           
81/* static unsigned short flowNumber; */
82static unsigned short receptPort = RECEPTION_PORT;
83/* static unsigned char *ptr_buffer; */
84static char *receptAddress = RECEPTION_ADDRESS;
85static struct sockaddr_in name;
86static struct sockaddr_in name2;
87static struct sockaddr_in fromName;
88char *log = "renetcol";
89#ifdef READFROMFILE
90  char *myPcapFileName, errBuffer[PCAP_ERRBUF_SIZE];
91  pcap_t *myPcapHandle;
92#endif
93
94/*
95 * Main
96 * ----
97 */
98int 
99main (int argc, char *argv[])
100{
101  RulesPtr tmp = NULL;
102  int shmid;
103  key_t key;
104  int i,j;
105  int k;
106  unsigned short routerIndex = 0;
107
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
122#if defined(IPV4AGGIDR)
123  fprintf(stderr, "renetcol compilation and execution with IPv4 Prefixes Aggregation feature enable based on routers ID in %s file.\n", ROUTERS_LIST);
124#endif
125
126#if defined(IPV4AGGIDSNMP)
127  fprintf(stderr, "renetcol compilation and execution with IPv4 Prefixes Aggregation feature enable based on SNMP ID in %s file.\n", INTERFACES_TYPE_LIST);
128#endif
129#if defined(IPV6AGGIDSNMP)
130  fprintf(stderr, "renetcol compilation and execution with IPv6 Prefixes Aggregation feature enable based on SNMP ID in %s file.\n", INTERFACES_TYPE_LIST);
131#endif
132#if defined(IPV6LINKAGG)
133  fprintf(stderr, "renetcol compilation and execution with IPv6 Links Aggregation.\n");
134#endif
135
136  /*   openlog(argv[0], LOG_PID, LOG_USER);*/
137  openlog(log, NULL, LOG_LOCAL7);
138
139  initCache();
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 */
150  for (routerIndex=0; routerIndex<routerNb; routerIndex++){
151    routersListPtr = addRouter(routersListPtr, sortedRouterList[routerIndex],routerIndex);
152  }
153 
154  /* Shared memory */
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
165  if ( (shmForAgg->v4PrefixNb =
166        getPrefixV4(SUBNETS_LIST,
167                    &(shmForAgg->prefixV4Tab[0][0]),
168                    &(shmForAgg->prefixV4SubnetTab[0][0]),
169                    &(shmForAgg->v4SubnetNb)
170                    )) < 1){
171    fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST);
172  } else {
173    fprintf(stderr, "We have found %hu supernets from %s\n",
174            shmForAgg->v4PrefixNb,
175            SUBNETS_LIST);
176    fprintf(stderr, "and %hu subnets\n",
177            shmForAgg->v4SubnetNb);   
178  }
179  if ( (shmForAgg->v4PrefixNb =
180        getPrefixV4(SUBNETS_LIST,
181                    &(shmForAgg->prefixV4Tab[1][0]),
182                    &(shmForAgg->prefixV4SubnetTab[1][0]),
183                    &(shmForAgg->v4SubnetNb)
184                    )) < 1){
185    fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST);
186    exit(1);
187  }
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
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/*   } */
214
215  shmForAgg->currentTable = 0;
216  shmForAgg->secondTable = 1;
217  shmForAgg->readed = 0;
218  /* get the rules */
219  myPtrs.rulesListPtr = NULL;
220  myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE);
221  tmp = myPtrs.rulesListPtr;
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 }
229  tmp = NULL;
230#ifdef DEBUG
231  printRule(myPtrs.rulesListPtr);
232#endif
233
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
250  /* the fork */
251  fprintf(stderr, "renetcol: I become a deamon, next messages via syslogd. Bye.\n");
252  if (fork () != 0)
253    exit (0);
254  if (setsid() == -1){
255    exit(4);
256  }
257  /* handler, SIGUSR1 from renetcolSender */
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
264  /* Cache for fast rules access */
265  setCache(myPtrs.rulesListPtr);
266  myPtrs.rulesAddressPtr = (RuleDefPtr *)rulesAddress;
267  /* IPC messages queue init */
268  myKey = createKey(argv[0]);
269  myQueue = createQueue(myKey);
270  sendMyPid(myQueue);
271  /* get SNMP index information */
272#ifdef IPV4AGGIDSNMP
273  k = getSNMPIndexList(INTERFACES_TYPE_LIST, routersListPtr);
274#endif
275  /* INIT THE CURRENTS VARIABLES*/
276  myPtrs.currentRouterPtr = routersListPtr;
277  myPtrs.currentHeaderV9Ptr = (NetFlowV9HeaderPtr)
278    malloc(sizeof(struct NetFlowV9Header));
279  myPtrs.offsetV9Ptr = &offsetV9;
280  myPtrs.currentFlowsetIdPtr = &currentFlowsetId;
281  myPtrs.pcktPtr = (DatagramPtr) malloc(sizeof(struct Datagram));
282  myPtrs.currentFlowsetNumberPtr = &currentFlowsetNumber;
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);}
291#ifdef IPV4AGGIDR
292  myPtrs.routersID = &indexedRouterList;
293#endif
294  myPtrs.currentV4Tab = shmForAgg->prefixV4Tab[shmForAgg->currentTable];
295  myPtrs.secondV4Tab = shmForAgg->prefixV4Tab[shmForAgg->secondTable];
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
304#ifdef ASACC
305  myPtrs.asNb = shmForAgg->ASNb;
306  myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable];
307  myPtrs.secondASTab = shmForAgg->ASTab[shmForAgg->secondTable];
308#endif
309#ifdef MATRIX
310  myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]);
311#endif
312
313  /* INIT MATRIX TABLES */
314#ifdef MATRIX
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  }
325#endif
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++) {
330      shmForAgg->indexV6Tab[0][i][j].routerIPAddress = 0;
331      shmForAgg->indexV6Tab[0][i][j].indexSNMP = 0;
332      shmForAgg->indexV6Tab[0][i][j].hasStats = 0;
333      shmForAgg->indexV6Tab[0][i][j].sampling = 0;
334      shmForAgg->indexV6Tab[0][i][j].bytesNbIN = 0;
335      shmForAgg->indexV6Tab[0][i][j].pktsNbIN = 0;
336      shmForAgg->indexV6Tab[0][i][j].flowNbIN = 0;
337      shmForAgg->indexV6Tab[0][i][j].bytesNbOUT = 0;
338      shmForAgg->indexV6Tab[0][i][j].pktsNbOUT = 0;
339      shmForAgg->indexV6Tab[0][i][j].flowNbOUT = 0;
340      shmForAgg->indexV6Tab[1][i][j].routerIPAddress = 0;
341      shmForAgg->indexV6Tab[1][i][j].indexSNMP = 0;
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;
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
353
354  /* Checkup */
355  if (myPtrs.pcktPtr==NULL) {
356    fprintf(stderr, "ERROR in struct Datagram allocation\n");
357    exit(1);
358  } else {
359    myPtrs.pcktPtr->ipH = (IpHeaderPtr) malloc(sizeof(struct IpHeader));
360    myPtrs.pcktPtr->udp_header = (UdpHeaderPtr) malloc(sizeof(struct UdpHeader));
361  }
362  if (! (myPtrs.ptr_buffer = malloc(sockBufSize))) 
363    {
364      printf("ERROR during socket buffer allocation\n");
365      exit(2);
366    }
367
368  /* INIT INPUT STREAM*/
369  initStream();
370
371  socketLoop(); /* all work on datagram is made here */
372
373  closelog();
374
375  fprintf(stderr, "END\n");
376
377  return (0);
378}
379
380/*
381 * send his PID to another program via IPC message queue
382 *
383 * @param queueID the IPC queue ident
384 *
385 */
386void 
387sendMyPid(int queueID)
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/*
407 * Send the "read rules list" message to another program
408 *
409 * @param queueID the IPC queue ident
410 *
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/*
427 *
428 */
429void 
430sigusr1Mgmt(int num)
431{
432  sigusr1Up = 1;
433}
434
435/*
436 * init the rule structure
437 */
438void 
439initRule()
440{
441  RulesPtr tmp = NULL;
442  initCache();
443  tmp = myPtrs.rulesListPtr;
444  for ( ; tmp; tmp=tmp->next) {
445    myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
446  }
447  myPtrs.rulesListPtr = NULL;
448  myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE);
449  tmp = myPtrs.rulesListPtr;
450  for ( ; tmp; tmp=tmp->next) {
451    if (tmp->type != 2) {
452      myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
453    }
454  }
455  tmp = NULL;
456  initCache();
457  setCache(myPtrs.rulesListPtr);
458  sendReadRulesSignal(myQueue);
459#ifdef DEBUG
460  printRule(myPtrs.rulesListPtr);
461#endif
462}
463
464/*
465 * init cache table
466 */
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/*
479 * setCache()
480 */
481void
482setCache(RulesPtr rPtr)
483{
484  int i=0;
485  RulesPtr tmp = rPtr;
486  RuleDefPtr def = NULL;
487
488#ifdef DEBUG
489  fprintf(stderr,"Cache for Rules: \n");
490#endif 
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;
499#ifdef DEBUG
500      fprintf(stderr,"%d: %d \n", def->fieldType, i);
501#endif     
502      def = def->next;
503    }
504#ifdef DEBUG
505    fprintf(stderr,"\n");
506#endif     
507    tmp = tmp->next;
508  }
509#ifdef DEBUG
510  fprintf(stderr,"Cache for Rules: OK");
511#endif 
512}
513
514/*
515 * initStream()
516 *
517 * Stream can be a file (pcap format) or a socket
518 */
519void 
520initStream()
521{
522  static unsigned short n0, n1, n2, n3;
523
524#ifdef READFROMFILE
525  initFile();
526#else
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;
535#endif
536}
537
538/*
539 * socketLoop()
540 */
541int 
542socketLoop()
543{
544  short shift;
545  short version = 0;
546  int regRouter = 0;
547  time_t now = time((time_t *)NULL);
548  int loopNb = 0;
549  int gardeFou = 0;
550  time_t lastIPOutput, lastMPLSOutput;
551  RouterPtr tmp = routersListPtr; 
552  int dd = 0;
553  FILE *TPLFILE;
554
555  now = time((time_t *)NULL);
556  tmPtr = localtime(&now);
557  currentIntervalle = tmPtr->tm_min;
558  lastMPLSOutput = now;
559  lastIPOutput = now;
560  do {
561#ifdef DEBUG   
562    fprintf (stderr, "[");
563#endif
564    if ( ((tmPtr->tm_min)%STEP == 0) && (currentIntervalle != tmPtr->tm_min)){
565      currentIntervalle = tmPtr->tm_min;
566      if (shmForAgg->currentTable == 0) {
567        shmForAgg->currentTable = 1;
568        shmForAgg->secondTable = 0;
569      }else{
570        shmForAgg->currentTable = 0;
571        shmForAgg->secondTable = 1;
572      }
573      shmForAgg->readed = 1;
574      myPtrs.currentMIB = shmForAgg->myMIB[shmForAgg->currentTable];
575      myPtrs.secondMIB = shmForAgg->myMIB[shmForAgg->secondTable];
576#ifdef ASACC
577      myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable];
578#endif
579#ifdef MATRIX
580      myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]);
581#endif
582#ifdef IPV6LINKAGG
583      myPtrs.currentV6IndexTab = &(shmForAgg->indexV6Tab[shmForAgg->currentTable][0][0]);
584      myPtrs.secondV6IndexTab = &(shmForAgg->indexV6Tab[shmForAgg->secondTable][0][0]);
585      /* FIXME : to delete : */
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)
603                                        +((tmp->ID)*MAX_INDEX_BY_ROUTER)
604                                        +(tmp->snmpIndexID[dd])))->bytesNbIN,
605                    ((struct IndexV6 *)((myPtrs.secondV6IndexTab)
606                                        +((tmp->ID)*MAX_INDEX_BY_ROUTER)
607                                        +(tmp->snmpIndexID[dd])))->bytesNbOUT
608                    );
609          }
610        }
611      }
612      fclose(TPLFILE); /* <-- */
613#endif
614    }
615    if (sigusr1Up == 1){
616      sigusr1Up = 0;
617      initRule();
618    }
619#ifdef READFROMFILE
620    fileReading();
621#else
622    socketReading();
623#endif
624    getIpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer);
625    regRouter = checkIpHeader(myPtrs.pcktPtr, &sortedRouterList[0], routerNb);
626    myPtrs.currentMIB->ipPacketNb += 1;
627    getUdpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer);
628    if ( checkUdpHeader(myPtrs.pcktPtr, regRouter, receptPort) == 1 ){
629      continue;
630    }
631    myPtrs.currentMIB->udpPacketNb += 1;
632    switch( version =
633            getNetFlowHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer,
634                             myPtrs.currentHeaderV9Ptr, myPtrs.offsetV9Ptr)){
635    case 9:
636#ifdef DEBUG
637      fprintf (stderr, ", %d %hu %lu ,",
638               myPtrs.currentHeaderV9Ptr->version,
639               myPtrs.currentHeaderV9Ptr->count,
640               myPtrs.currentHeaderV9Ptr->package_sequence);
641#endif
642      myPtrs.currentMIB->v9PacketNb += 1;
643      myPtrs.currentMIB->flowNb += myPtrs.currentHeaderV9Ptr->count;
644      if ((myPtrs.currentRouterPtr=notExistRouter(routersListPtr,
645                                                  myPtrs.pcktPtr->ipH->srcAdd))==NULL) {
646        myPtrs.currentRouterPtr = routersListPtr;
647      }
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      }
664      currentFlowsetNumber = 0;
665      shift = 0;
666      gardeFou=0;
667      while ((currentFlowsetNumber < myPtrs.currentHeaderV9Ptr->count)
668             && (shift+48 < myPtrs.pcktPtr->udp_header->length)) {
669        gardeFou++;
670        curTplFlSetPtr = NULL;
671        currentFlowsetId=getFlowsetId(currentFlowsetNumber,
672                                      myPtrs.offsetV9Ptr, myPtrs.ptr_buffer);
673        myPtrs.currentMIB->flowSetNb += 1;
674        if ( currentFlowsetId == 0 ) {
675          if ( (shift = checkTemplateFlowSet(shift, myPtrs.currentRouterPtr,
676                                             myPtrs.currentMIB,
677                                             myPtrs.offsetV9Ptr,
678                                             myPtrs.ptr_buffer,
679                                             myPtrs.currentHeaderV9Ptr,
680                                             curTplFlSetPtr,
681                                             myPtrs.currentFlowsetNumberPtr,
682                                             myQueue)) < 0 ) {
683#ifdef DEBUG
684            fprintf (stderr, "w> ");
685#endif
686            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
687          }
688          /* writeAllTplFlSet(); */
689        } else if (currentFlowsetId == 1) {
690          if ( (shift = checkTemplateOption(shift, myPtrs.currentRouterPtr,
691                                            myPtrs.currentMIB,
692                                            myPtrs.offsetV9Ptr,
693                                            myPtrs.ptr_buffer,
694                                            myPtrs.currentHeaderV9Ptr,
695                                            curTplOptionPtr,
696                                            myPtrs.currentFlowsetNumberPtr) ) < 0 ) {
697#ifdef DEBUG
698            fprintf (stderr, "wo> ");
699#endif 
700            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
701          }
702        } else if (currentFlowsetId > 255) {
703          if ( (shift = checkDataFlowSet(shift,
704                                         &myPtrs,
705                                         myQueue,
706                                         shmForAgg->prefixV4Tab[shmForAgg->currentTable],
707                                         (size_t) shmForAgg->v4PrefixNb,
708                                         shmForAgg->prefixV4SubnetTab[shmForAgg->currentTable],
709                                         (size_t) shmForAgg->v4SubnetNb) ) < 0 ) {
710#ifdef DEBUG   
711            fprintf (stderr, "wd> ");
712#endif
713            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
714          }
715        } else {
716#ifdef DEBUG   
717          fprintf (stderr, " 1<<255 ");
718#endif
719          currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
720        }
721        if ( gardeFou > 200) { exit(-1); }
722      } /* end while flowset exist */
723#ifdef DEBUG
724      if (shift > 1456) {
725        fprintf(stderr," SHIT SHIFT > 1456 : %d \n", shift);
726      }
727#endif
728      break;
729    default:
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));
737      break;
738    }
739    if (loopNb  > 140000 ){
740      /* FIXME perhaps call this function via the compilation options */
741      writeAllTplFlSet();
742#ifdef DEBUG   
743      fprintf(stderr, "W");
744#endif
745      loopNb = 0;
746    }
747    loopNb++;
748
749    now = time((time_t *)NULL);
750    tmPtr = localtime(&now);
751#ifdef DEBUG   
752    fprintf (stderr, "]\n");
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             );
763#endif
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    }
799  /* Here socket DGRAM creation, only to not have a unreachable service */
800  /* message in return */
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    }
827  return(0);
828}
829
830/*
831 * initFile
832 */
833short initFile()
834{
835 
836}
837
838/*
839 * socketReading
840 */
841short 
842socketReading()
843{
844  signed short sockLg;
845
846  sockNameFromlg = sizeof(fromName);
847  rcv = recvfrom(inputSock, myPtrs.ptr_buffer, sockBufSize, 0,
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}
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) {
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             );
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}
891 
892/*
893 * check up flow datagramme 
894 */
895short 
896checkFlow(short flowNumber)
897{
898  return(0); /* FIXME : why this function ??? */
899}
900
901/*
902 * showAllTplFlSet
903 *
904 * to use only in debug mode
905 */
906void 
907showAllTplFlSet()
908{
909  RouterPtr tmp = routersListPtr;
910  TplFlowSetPtr tmpFS;
911  TplOptionPtr tmpOP;
912  fprintf(stderr,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n");
913  for (; tmp; tmp=tmp->next) {
914    fprintf(stderr,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n",
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) {
921      fprintf(stderr,"TId %hu (sourceId: %lu):\n",
922              tmpFS->templateFlowSetId,
923              tmpFS->sourceId);
924      printFieldSet(stderr, tmpFS->fieldSet);
925      fprintf(stderr,"\n");
926    }
927    if ((tmpOP = tmp->tplOptList) != NULL){
928      for (; tmpOP; tmpOP=tmpOP->next) {
929        fprintf(stderr,"OpTId %hu (sourceId: %lu) >\n",
930                tmpOP->templateOptionId,
931                tmpOP->sourceId);
932        printFieldSet(stderr, tmpOP->fieldSet);
933        fprintf(stderr,"\n");
934      }
935      fprintf(stderr,"\n");
936    }
937  }
938}
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) {
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),
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) {
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),
976                tmpOP->templateOptionId,
977                tmpOP->sourceId,
978                tmpOP->optionScopeLg/4, /* last x brackets are scope */
979                tmpOP->length,
980                tmpOP->optionScopeLg,
981                tmpOP->optionLg);
982        printFieldSet(TPLFILE, tmpOP->fieldSet);
983        fprintf(TPLFILE,"\n");
984      }
985      /*      fprintf(TPLFILE,"\n");*/
986    }
987  }
988  fclose(TPLFILE);
989}
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;
1007  theMIB->ipv4Bytes = 0;
1008  theMIB->ipv4Pckts = 0;
1009  theMIB->ipv4Flow = 0;   
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;
1046  theMIB->ipv6Bytes = 0;
1047  theMIB->ipv6Pckts = 0;
1048  theMIB->ipv6Flow = 0;
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;
1075  theMIB->ipv6FlowSizeBytesMT100M = 0; 
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.