root/branches/renetcolSMP/src/renetcolSMP4.c @ 165

Revision 165, 30.3 KB (checked in by andreu, 9 years ago)

filters readed by all renetcolSMP process

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