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

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

filters readed by all renetcolSMP process

Line 
1/*
2 * File: renetcolSMP2.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 "renetcolSMP2.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[SMP2_ROUTER_INDEX_MAX];
62unsigned long indexedRouterList[SMP2_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 = SMP2_RECEPTION_PORT;
84/* static unsigned char *ptr_buffer; */
85static char *receptAddress = SMP2_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, "renetcolSMP2 compilation and execution with IPv4 Prefixes Aggregation feature enable based on routers ID in %s file.\n", SMP2_ROUTERS_LIST);
125#endif
126
127#if defined(IPV4AGGIDSNMP)
128  fprintf(stderr, "renetcolSMP2 compilation and execution with IPv4 Prefixes Aggregation feature enable based on SNMP ID in %s file.\n", SMP2_INTERFACES_TYPE_LIST);
129#endif
130#if defined(IPV6AGGIDSNMP)
131  fprintf(stderr, "renetcolSMP2 compilation and execution with IPv6 Prefixes Aggregation feature enable based on SNMP ID in %s file.\n", SMP2_INTERFACES_TYPE_LIST);
132#endif
133#if defined(IPV6LINKAGG)
134  fprintf(stderr, "renetcolSMP2 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(SMP2_ROUTERS_LIST,
143                                        &sortedRouterList[0],
144                                        &indexedRouterList[0])) == -1) {
145    fprintf(stderr,
146            "ERROR in getRegisteredRouters from file %s\n",
147            SMP2_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 = 8766;
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, "renetcolSMP2: 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, "renetcolSMP2: 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, "renetcolSMP2: 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, "renetcolSMP2: 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, "renetcolSMP2: 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, "renetcolSMP2: Error in %s\n", AS_LIST);
247    exit(1);
248  }
249#endif
250
251  /* the fork */
252  fprintf(stderr, "renetcolSMP2: 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(SMP2_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    exit(1);}
289  if (initMIB(myPtrs.secondMIB)!=1) {
290    fprintf(stderr, "ERROR in second MIB initialisation\n");
291    exit(1);}
292#ifdef IPV4AGGIDR
293  myPtrs.routersID = &indexedRouterList;
294#endif
295  myPtrs.currentV4Tab = shmForAgg->prefixV4Tab[shmForAgg->currentTable];
296  myPtrs.secondV4Tab = shmForAgg->prefixV4Tab[shmForAgg->secondTable];
297#ifdef IPV6AGGIDSNMP
298  myPtrs.currentV6Tab = shmForAgg->prefixV6Tab[shmForAgg->currentTable];
299  myPtrs.secondV6Tab = shmForAgg->prefixV6Tab[shmForAgg->secondTable];
300#endif
301#ifdef IPV6LINKAGG
302  myPtrs.currentV6IndexTab = shmForAgg->indexV6Tab[shmForAgg->currentTable];
303  myPtrs.secondV6IndexTab = shmForAgg->indexV6Tab[shmForAgg->secondTable];
304#endif
305#ifdef ASACC
306  myPtrs.asNb = shmForAgg->ASNb;
307  myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable];
308  myPtrs.secondASTab = shmForAgg->ASTab[shmForAgg->secondTable];
309#endif
310#ifdef MATRIX
311  myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]);
312#endif
313
314  /* INIT MATRIX TABLES */
315#ifdef MATRIX
316  for (i=0; i<SMP2_ROUTER_INDEX_MAX; i++){
317    for (j=0; j<SMP2_ROUTER_INDEX_MAX; j++) {
318      shmForAgg->matrixPOP[0][i][j].bytesNb = 0;
319      shmForAgg->matrixPOP[0][i][j].pktsNb = 0;
320      shmForAgg->matrixPOP[0][i][j].flowNb = 0;
321      shmForAgg->matrixPOP[1][i][j].bytesNb = 0;
322      shmForAgg->matrixPOP[1][i][j].pktsNb = 0;
323      shmForAgg->matrixPOP[1][i][j].flowNb = 0;
324    }
325  }
326#endif
327  /* INIT IPV6 index TABLES */
328#ifdef IPV6LINKAGG
329  for (i=0; i<SMP2_ROUTER_INDEX_MAX; i++){
330    for (j=0; j<MAX_INDEX_BY_ROUTER; j++) {
331      shmForAgg->indexV6Tab[0][i][j].routerIPAddress = 0;
332      shmForAgg->indexV6Tab[0][i][j].indexSNMP = 0;
333      shmForAgg->indexV6Tab[0][i][j].hasStats = 0;
334      shmForAgg->indexV6Tab[0][i][j].sampling = 0;
335      shmForAgg->indexV6Tab[0][i][j].bytesNbIN = 0;
336      shmForAgg->indexV6Tab[0][i][j].pktsNbIN = 0;
337      shmForAgg->indexV6Tab[0][i][j].flowNbIN = 0;
338      shmForAgg->indexV6Tab[0][i][j].bytesNbOUT = 0;
339      shmForAgg->indexV6Tab[0][i][j].pktsNbOUT = 0;
340      shmForAgg->indexV6Tab[0][i][j].flowNbOUT = 0;
341      shmForAgg->indexV6Tab[1][i][j].routerIPAddress = 0;
342      shmForAgg->indexV6Tab[1][i][j].indexSNMP = 0;
343      shmForAgg->indexV6Tab[1][i][j].hasStats = 0;
344      shmForAgg->indexV6Tab[1][i][j].sampling = 0;
345      shmForAgg->indexV6Tab[1][i][j].bytesNbIN = 0;
346      shmForAgg->indexV6Tab[1][i][j].pktsNbIN = 0;
347      shmForAgg->indexV6Tab[1][i][j].flowNbIN = 0;
348      shmForAgg->indexV6Tab[1][i][j].bytesNbOUT = 0;
349      shmForAgg->indexV6Tab[1][i][j].pktsNbOUT = 0;
350      shmForAgg->indexV6Tab[1][i][j].flowNbOUT = 0;
351    }
352  }
353#endif
354
355  /* Checkup */
356  if (myPtrs.pcktPtr==NULL) {
357    fprintf(stderr, "ERROR in struct Datagram allocation\n");
358    exit(1);
359  } else {
360    myPtrs.pcktPtr->ipH = (IpHeaderPtr) malloc(sizeof(struct IpHeader));
361    myPtrs.pcktPtr->udp_header = (UdpHeaderPtr) malloc(sizeof(struct UdpHeader));
362  }
363  if (! (myPtrs.ptr_buffer = malloc(sockBufSize)))
364    {
365      printf("ERROR during socket buffer allocation\n");
366      exit(2);
367    }
368
369  /* INIT INPUT STREAM*/
370  initStream();
371
372  socketLoop(); /* all work on datagram is made here */
373
374  closelog();
375
376  fprintf(stderr, "END\n");
377
378  return (0);
379}
380
381/*
382 * send his PID to another program via IPC message queue
383 *
384 * @param queueID the IPC queue ident
385 *
386 */
387void
388sendMyPid(int queueID)
389{
390  msgType myMsg;
391  char *msgTextIndex;
392  unsigned short tplMsgType = 14;
393  pid_t myPID;
394
395  myPID = getpid();
396  msgTextIndex = mempcpy(mempcpy(myMsg.text,
397                                 &tplMsgType,
398                                 sizeof (unsigned short)
399                                 ),
400                         &myPID,
401                         sizeof(pid_t)
402                         );
403  myMsg.type = 1;
404  msgSend(queueID, myMsg);
405}
406
407/*
408 * Send the "read rules list" message to another program
409 *
410 * @param queueID the IPC queue ident
411 *
412 */
413void sendReadRulesSignal(int queueID)
414{
415  msgType myMsg;
416  char *msgTextIndex;
417  unsigned short tplMsgType = 13;
418
419  msgTextIndex = mempcpy(myMsg.text,
420                         &tplMsgType,
421                         sizeof (unsigned short)
422                         );
423  myMsg.type = 1;
424  msgSend(queueID, myMsg);
425}
426
427/*
428 *
429 */
430void
431sigusr1Mgmt(int num)
432{
433  sigusr1Up = 1;
434}
435
436/*
437 * init the rule structure
438 */
439void
440initRule()
441{
442  RulesPtr tmp = NULL;
443  initCache();
444  tmp = myPtrs.rulesListPtr;
445  for ( ; tmp; tmp=tmp->next) {
446    myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
447  }
448  myPtrs.rulesListPtr = NULL;
449  myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE);
450  tmp = myPtrs.rulesListPtr;
451  for ( ; tmp; tmp=tmp->next) {
452    if (tmp->type != 2) {
453      myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
454    }
455  }
456  tmp = NULL;
457  initCache();
458  setCache(myPtrs.rulesListPtr);
459  sendReadRulesSignal(myQueue);
460#ifdef DEBUG
461  printRule(myPtrs.rulesListPtr);
462#endif
463}
464
465/*
466 * init cache table
467 */
468void
469initCache()
470{
471  int i,j;
472  for (i=0; i<FIELD_TYPE_NUMBER+1; i++){
473    for (j=0; j<MAX_RULES_PER_FIELD; j++){
474      rulesAddress[i][j] = NULL;
475    }
476  }
477}
478
479/*
480 * setCache()
481 */
482void
483setCache(RulesPtr rPtr)
484{
485  int i=0;
486  RulesPtr tmp = rPtr;
487  RuleDefPtr def = NULL;
488
489#ifdef DEBUG
490  fprintf(stderr,"Cache for Rules: \n");
491#endif
492  while (tmp) {
493    def = tmp->def;
494    while (def) {
495      i = 0;
496      while (rulesAddress[def->fieldType][i] != NULL){
497        i++;
498      }
499      rulesAddress[def->fieldType][i] = def;
500#ifdef DEBUG
501      fprintf(stderr,"%d: %d \n", def->fieldType, i);
502#endif
503      def = def->next;
504    }
505#ifdef DEBUG
506    fprintf(stderr,"\n");
507#endif
508    tmp = tmp->next;
509  }
510#ifdef DEBUG
511  fprintf(stderr,"Cache for Rules: OK");
512#endif
513}
514
515/*
516 * initStream()
517 *
518 * Stream can be a file (pcap format) or a socket
519 */
520void
521initStream()
522{
523  static unsigned short n0, n1, n2, n3;
524
525#ifdef READFROMFILE
526  initFile();
527#else
528  initSocket();
529  if (sscanf(receptAddress,"%hu.%hu.%hu.%hu",&n0,&n1,&n2,&n3)==0) {
530    perror("sscanf");
531  }
532  buffer4[0] = (unsigned char)n3;
533  buffer4[1] = (unsigned char)n2;
534  buffer4[2] = (unsigned char)n1;
535  buffer4[3] = (unsigned char)n0;
536#endif
537}
538
539/*
540 * socketLoop()
541 */
542int
543socketLoop()
544{
545  short shift;
546  short version = 0;
547  int regRouter = 0;
548  time_t now = time((time_t *)NULL);
549  int loopNb = 0;
550  int gardeFou = 0;
551  time_t lastIPOutput, lastMPLSOutput;
552  RouterPtr tmp = routersListPtr;
553  int dd = 0;
554  FILE *TPLFILE;
555
556  now = time((time_t *)NULL);
557  tmPtr = localtime(&now);
558  currentIntervalle = tmPtr->tm_min;
559  lastMPLSOutput = now;
560  lastIPOutput = now;
561  do {
562#ifdef DEBUG
563    fprintf (stderr, "[");
564#endif
565    if ( ((tmPtr->tm_min)%STEP == 0) && (currentIntervalle != tmPtr->tm_min)){
566      currentIntervalle = tmPtr->tm_min;
567      if (shmForAgg->currentTable == 0) {
568        shmForAgg->currentTable = 1;
569        shmForAgg->secondTable = 0;
570      }else{
571        shmForAgg->currentTable = 0;
572        shmForAgg->secondTable = 1;
573      }
574      shmForAgg->readed = 1;
575      myPtrs.currentMIB = shmForAgg->myMIB[shmForAgg->currentTable];
576      myPtrs.secondMIB = shmForAgg->myMIB[shmForAgg->secondTable];
577#ifdef ASACC
578      myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable];
579#endif
580#ifdef MATRIX
581      myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]);
582#endif
583#ifdef IPV6LINKAGG
584      myPtrs.currentV6IndexTab = &(shmForAgg->indexV6Tab[shmForAgg->currentTable][0][0]);
585      myPtrs.secondV6IndexTab = &(shmForAgg->indexV6Tab[shmForAgg->secondTable][0][0]);
586      /* FIXME : to delete : */
587      if (!(TPLFILE = fopen("/tmp/IPV6LINKAGGSMP2.txt", "w"))) {
588        syslog (LOG_ERR, "error during %s opening", "/tmp/IPV6LINKAGGSMP2.txt");
589      }
590      tmp = routersListPtr;
591      for (; tmp; tmp=tmp->next) {
592        for (dd=0;dd<MAX_INDEX_BY_ROUTER;dd++) {
593          if ( tmp->snmpIndexType[dd] != 2) {
594            fprintf(TPLFILE,
595                    "router: %lu.%lu.%lu.%lu dd: %hu , indexSNMP : %hu , type : %hu ,total bytes IN : %llu, OUT : %llu\n",
596                    (tmp->IpAddress>>24),
597                    (tmp->IpAddress<<8>>24),
598                    (tmp->IpAddress<<16>>24),
599                    (tmp->IpAddress<<24>>24),
600                    dd,
601                    tmp->snmpIndexID[dd],
602                    tmp->snmpIndexType[dd],
603                    ((struct IndexV6 *)((myPtrs.secondV6IndexTab)
604                                        +((tmp->ID)*MAX_INDEX_BY_ROUTER)
605                                        +(tmp->snmpIndexID[dd])))->bytesNbIN,
606                    ((struct IndexV6 *)((myPtrs.secondV6IndexTab)
607                                        +((tmp->ID)*MAX_INDEX_BY_ROUTER)
608                                        +(tmp->snmpIndexID[dd])))->bytesNbOUT
609                    );
610          }
611        }
612      }
613      fclose(TPLFILE); /* <-- */
614#endif
615    }
616    if (sigusr1Up == 1){
617      sigusr1Up = 0;
618      initRule();
619    }
620#ifdef READFROMFILE
621    fileReading();
622#else
623    socketReading();
624#endif
625    getIpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer);
626    regRouter = checkIpHeader(myPtrs.pcktPtr, &sortedRouterList[0], routerNb);
627    myPtrs.currentMIB->ipPacketNb += 1;
628    getUdpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer);
629    if ( checkUdpHeader(myPtrs.pcktPtr, regRouter, receptPort) == 1 ){
630      continue;
631    }
632    myPtrs.currentMIB->udpPacketNb += 1;
633    switch( version =
634            getNetFlowHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer,
635                             myPtrs.currentHeaderV9Ptr, myPtrs.offsetV9Ptr)){
636    case 9:
637#ifdef DEBUG
638      fprintf (stderr, ", %d %hu %lu ,",
639               myPtrs.currentHeaderV9Ptr->version,
640               myPtrs.currentHeaderV9Ptr->count,
641               myPtrs.currentHeaderV9Ptr->package_sequence);
642#endif
643      myPtrs.currentMIB->v9PacketNb += 1;
644      myPtrs.currentMIB->flowNb += myPtrs.currentHeaderV9Ptr->count;
645      if ((myPtrs.currentRouterPtr=notExistRouter(routersListPtr,
646                                                  myPtrs.pcktPtr->ipH->srcAdd))==NULL) {
647        myPtrs.currentRouterPtr = routersListPtr;
648      }
649      if ((enginePtrTmp = existEngId(myPtrs.currentRouterPtr,myPtrs.currentHeaderV9Ptr->sourceId))==NULL) {
650        /* add new engineID */
651        enginePtrTmp = newRouterEngineList();
652        enginePtrTmp->next = myPtrs.currentRouterPtr->engineList;
653        if (myPtrs.currentRouterPtr->engineList!=NULL) {
654          myPtrs.currentRouterPtr->engineList->prev=enginePtrTmp;}
655        myPtrs.currentRouterPtr->engineList = enginePtrTmp;
656        enginePtrTmp->engineId = myPtrs.currentHeaderV9Ptr->sourceId;
657        enginePtrTmp->package_sequence = myPtrs.currentHeaderV9Ptr->package_sequence;
658      } else {
659        enginePtrTmp->engineId = myPtrs.currentHeaderV9Ptr->sourceId;
660        if (enginePtrTmp->package_sequence != (myPtrs.currentHeaderV9Ptr->package_sequence-1)) {
661          myPtrs.currentMIB->v9UnSeqNb += 1;
662        }
663        enginePtrTmp->package_sequence = myPtrs.currentHeaderV9Ptr->package_sequence;
664      }
665      currentFlowsetNumber = 0;
666      shift = 0;
667      gardeFou=0;
668      while ((currentFlowsetNumber < myPtrs.currentHeaderV9Ptr->count)
669             && (shift+48 < myPtrs.pcktPtr->udp_header->length)) {
670        gardeFou++;
671        curTplFlSetPtr = NULL;
672        currentFlowsetId=getFlowsetId(currentFlowsetNumber,
673                                      myPtrs.offsetV9Ptr, myPtrs.ptr_buffer);
674        myPtrs.currentMIB->flowSetNb += 1;
675        if ( currentFlowsetId == 0 ) {
676          if ( (shift = checkTemplateFlowSet(shift, myPtrs.currentRouterPtr,
677                                             myPtrs.currentMIB,
678                                             myPtrs.offsetV9Ptr,
679                                             myPtrs.ptr_buffer,
680                                             myPtrs.currentHeaderV9Ptr,
681                                             curTplFlSetPtr,
682                                             myPtrs.currentFlowsetNumberPtr,
683                                             myQueue)) < 0 ) {
684#ifdef DEBUG
685            fprintf (stderr, "w> ");
686#endif
687            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
688          }
689          /* writeAllTplFlSet(); */
690        } else if (currentFlowsetId == 1) {
691          if ( (shift = checkTemplateOption(shift, myPtrs.currentRouterPtr,
692                                            myPtrs.currentMIB,
693                                            myPtrs.offsetV9Ptr,
694                                            myPtrs.ptr_buffer,
695                                            myPtrs.currentHeaderV9Ptr,
696                                            curTplOptionPtr,
697                                            myPtrs.currentFlowsetNumberPtr) ) < 0 ) {
698#ifdef DEBUG
699            fprintf (stderr, "wo> ");
700#endif
701            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
702          }
703        } else if (currentFlowsetId > 255) {
704          if ( (shift = checkDataFlowSet(shift,
705                                         &myPtrs,
706                                         myQueue,
707                                         shmForAgg->prefixV4Tab[shmForAgg->currentTable],
708                                         (size_t) shmForAgg->v4PrefixNb,
709                                         shmForAgg->prefixV4SubnetTab[shmForAgg->currentTable],
710                                         (size_t) shmForAgg->v4SubnetNb) ) < 0 ) {
711#ifdef DEBUG
712            fprintf (stderr, "wd> ");
713#endif
714            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
715          }
716        } else {
717#ifdef DEBUG
718          fprintf (stderr, " 1<<255 ");
719#endif
720          currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
721        }
722        if ( gardeFou > 200) { exit(-1); }
723      } /* end while flowset exist */
724#ifdef DEBUG
725      if (shift > 1456) {
726        fprintf(stderr," SHIT SHIFT > 1456 : %d \n", shift);
727      }
728#endif
729      break;
730    default:
731      syslog(LOG_INFO,
732             "NetFlow exp. version unknown: %hu, from router: %lu.%lu.%lu.%lu",
733             (unsigned short) version,
734             (myPtrs.pcktPtr->ipH->srcAdd>>24),
735             (myPtrs.pcktPtr->ipH->srcAdd<<8>>24),
736             (myPtrs.pcktPtr->ipH->srcAdd<<16>>24),
737             (myPtrs.pcktPtr->ipH->srcAdd<<24>>24));
738      break;
739    }
740    if (loopNb  > 140000 ){
741      /* FIXME perhaps call this function via the compilation options */
742      writeAllTplFlSet();
743#ifdef DEBUG
744      fprintf(stderr, "W");
745#endif
746      loopNb = 0;
747    }
748    loopNb++;
749
750    now = time((time_t *)NULL);
751    tmPtr = localtime(&now);
752#ifdef DEBUG
753    fprintf (stderr, "]\n");
754    fprintf (stderr, "IP: %llu , UDP: %llu, NDEv9: %llu, drops: %llu, FS: %llu, Flows: %llu, realFl: %llu Diff:%llu\n",
755             myPtrs.currentMIB->ipPacketNb,
756             myPtrs.currentMIB->udpPacketNb,
757             myPtrs.currentMIB->v9PacketNb,
758             myPtrs.currentMIB->v9UnSeqNb,
759             myPtrs.currentMIB->flowSetNb,
760             myPtrs.currentMIB->flowNb,
761             myPtrs.currentMIB->realFlowNb,
762             myPtrs.currentMIB->flowNb - myPtrs.currentMIB->realFlowNb
763             );
764#endif
765  } while (1);
766}
767
768/*
769 * initSocket()
770 */
771short
772initSocket()
773{
774  inputSock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
775  if (inputSock < 0)
776    {
777      syslog(LOG_ERR,"socket : %s", strerror(errno));
778      exit(1);
779    }
780  memset((void*)&name, 0, sizeof(name));
781  name.sin_family = AF_INET;
782  /*name.sin_addr.s_addr = htonl(INADDR_ANY);*/
783  name.sin_addr.s_addr = inet_addr(SMP2_RECEPTION_ADDRESS);
784  if (name.sin_addr.s_addr == INADDR_NONE){
785    syslog(LOG_ERR, " INADDR_NONE ");
786    exit(1);
787  }
788  name.sin_port = htons(receptPort);
789  if (bind(inputSock,(struct sockaddr *)(&name), sizeof(name)) < 0)
790    {
791      syslog(LOG_ERR, "bind : %s", strerror(errno));
792      exit(1);
793    }
794  sockNamelg = sizeof(name);
795  if (getsockname(inputSock, (struct sockaddr *) (&name),
796                  (socklen_t *)&sockNamelg) < 0)
797    {
798      syslog(LOG_ERR, "getsockname: %s", strerror(errno));
799      exit(1);
800    }
801  /* Here socket DGRAM creation, only to not have a unreachable service */
802  /* message in return */
803  inputSock2 = socket(AF_INET, SOCK_DGRAM, 0);
804  if (inputSock2 < 0)
805    {
806      syslog(LOG_ERR,"socket : %s", strerror(errno));
807      exit(1);
808    }
809  memset((void*)&name, 0, sizeof(name));
810  name2.sin_family = AF_INET;
811  /*name2.sin_addr.s_addr = htonl(INADDR_ANY);*/
812  name2.sin_addr.s_addr = inet_addr(SMP2_RECEPTION_ADDRESS);
813  if (name2.sin_addr.s_addr == INADDR_NONE){
814    syslog(LOG_ERR, " INADDR_NONE ");
815    exit(1);
816  }
817  name2.sin_port = htons(receptPort);
818  if (bind(inputSock2,(struct sockaddr *)(&name2), sizeof(name2)) < 0)
819    {
820      syslog(LOG_ERR, "bind : %s", strerror(errno));
821      exit(1);
822    }
823  sockNamelg = sizeof(name2);
824  if (getsockname(inputSock2, (struct sockaddr *) (&name2),
825                  (socklen_t *)&sockNamelg) < 0)
826    {
827      syslog(LOG_ERR, "getsockname: %s", strerror(errno));
828      exit(1);
829    }
830  return(0);
831}
832
833/*
834 * initFile
835 */
836short initFile()
837{
838
839}
840
841/*
842 * socketReading
843 */
844short
845socketReading()
846{
847  signed short sockLg;
848
849  sockNameFromlg = sizeof(fromName);
850  rcv = recvfrom(inputSock, myPtrs.ptr_buffer, sockBufSize, 0,
851                 (struct sockaddr *)(&fromName),
852                 (socklen_t *)&sockNameFromlg);
853  sockLg = rcv;
854  if (sockLg < 0) {
855    syslog(LOG_ERR,"recvfrom : %s", strerror(errno));
856    exit(1);
857  }
858  if (sockLg == 0) {
859    syslog(LOG_ERR,"recvfrom : %s", strerror(errno));
860    exit(1);
861  }
862  return(0);
863}
864
865/*
866 * fileReading
867 */
868short fileReading()
869{
870#ifdef READFROMFILE
871  struct pcap_pkthdr myPcapHeader;
872  const u_char *ethernetPacket;
873
874  if ((ethernetPacket = pcap_next(myPcapHandle, &myPcapHeader)) == NULL) {
875    fprintf (stderr, "IP: %llu , UDP: %llu, NDEv9: %llu, drops: %llu, FS: %llu, Flows: %llu, realFl: %llu Diff:%llu\n",
876             myPtrs.currentMIB->ipPacketNb,
877             myPtrs.currentMIB->udpPacketNb,
878             myPtrs.currentMIB->v9PacketNb,
879             myPtrs.currentMIB->v9UnSeqNb,
880             myPtrs.currentMIB->flowSetNb,
881             myPtrs.currentMIB->flowNb,
882             myPtrs.currentMIB->realFlowNb,
883             myPtrs.currentMIB->flowNb - myPtrs.currentMIB->realFlowNb
884             );
885    pcap_close(myPcapHandle);
886    exit(0);
887  }
888  myPtrs.ptr_buffer = (DatagramPtr) (ethernetPacket + 14);
889#ifdef DEBUG
890  fprintf(stderr,"P");
891#endif
892#endif
893}
894
895/*
896 * check up flow datagramme
897 */
898short
899checkFlow(short flowNumber)
900{
901  return(0); /* FIXME : why this function ??? */
902}
903
904/*
905 * showAllTplFlSet
906 *
907 * to use only in debug mode
908 */
909void
910showAllTplFlSet()
911{
912  RouterPtr tmp = routersListPtr;
913  TplFlowSetPtr tmpFS;
914  TplOptionPtr tmpOP;
915  fprintf(stderr,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n");
916  for (; tmp; tmp=tmp->next) {
917    fprintf(stderr,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n",
918            (tmp->IpAddress>>24),
919            (tmp->IpAddress<<8>>24),
920            (tmp->IpAddress<<16>>24),
921            (tmp->IpAddress<<24>>24));
922    tmpFS =  tmp->tplList;
923    for (; tmpFS; tmpFS=tmpFS->next) {
924      fprintf(stderr,"TId %hu (sourceId: %lu):\n",
925              tmpFS->templateFlowSetId,
926              tmpFS->sourceId);
927      printFieldSet(stderr, tmpFS->fieldSet);
928      fprintf(stderr,"\n");
929    }
930    if ((tmpOP = tmp->tplOptList) != NULL){
931      for (; tmpOP; tmpOP=tmpOP->next) {
932        fprintf(stderr,"OpTId %hu (sourceId: %lu) >\n",
933                tmpOP->templateOptionId,
934                tmpOP->sourceId);
935        printFieldSet(stderr, tmpOP->fieldSet);
936        fprintf(stderr,"\n");
937      }
938      fprintf(stderr,"\n");
939    }
940  }
941}
942
943
944/*
945 * writeAllTplFlSet
946 *
947 */
948void
949writeAllTplFlSet()
950{
951
952  RouterPtr tmp = routersListPtr;
953  TplFlowSetPtr tmpFS;
954  TplOptionPtr tmpOP;
955  FILE *TPLFILE;
956  if (!(TPLFILE = fopen("/tmp/TemplateDefSMP2.txt", "w"))) {
957    syslog (LOG_ERR, "error during %s opening", "/tmp/TemplateDefSMP2.txt");
958  }
959  for (; tmp; tmp=tmp->next) {
960    tmpFS =  tmp->tplList;
961    for (; tmpFS; tmpFS=tmpFS->next) {
962      fprintf(TPLFILE,"%lu.%lu.%lu.%lu TId %hu %lu ",
963              (tmp->IpAddress>>24),
964              (tmp->IpAddress<<8>>24),
965              (tmp->IpAddress<<16>>24),
966              (tmp->IpAddress<<24>>24),
967              tmpFS->templateFlowSetId,
968              tmpFS->sourceId);
969      printFieldSet(TPLFILE, tmpFS->fieldSet);
970      fprintf(TPLFILE,"\n");
971    }
972    if ((tmpOP = tmp->tplOptList) != NULL){
973      for (; tmpOP; tmpOP=tmpOP->next) {
974        fprintf(TPLFILE,"%lu.%lu.%lu.%lu OpTId %hu %lu %d %hu %hu %hu ",
975                (tmp->IpAddress>>24),
976                (tmp->IpAddress<<8>>24),
977                (tmp->IpAddress<<16>>24),
978                (tmp->IpAddress<<24>>24),
979                tmpOP->templateOptionId,
980                tmpOP->sourceId,
981                tmpOP->optionScopeLg/4, /* last x brackets are scope */
982                tmpOP->length,
983                tmpOP->optionScopeLg,
984                tmpOP->optionLg);
985        printFieldSet(TPLFILE, tmpOP->fieldSet);
986        fprintf(TPLFILE,"\n");
987      }
988      /*      fprintf(TPLFILE,"\n");*/
989    }
990  }
991  fclose(TPLFILE);
992}
993
994/*
995 * initMIB
996 */
997int initMIB(struct RenetcolMIB *theMIB)
998{
999  theMIB->ipPacketNb = 0;
1000  theMIB->udpPacketNb = 0;
1001  theMIB->v9PacketNb = 0;
1002  theMIB->v9UnSeqNb = 0;
1003  theMIB->flowSetNb = 0;
1004  theMIB->dataFlowSetNb = 0;
1005  theMIB->defFlowSetNb = 0;
1006  theMIB->optDataFlowSetNb = 0;
1007  theMIB->optDefFlowSetNb = 0;
1008  theMIB->flowNb = 0;
1009  theMIB->realFlowNb = 0;
1010  theMIB->ipv4Bytes = 0;
1011  theMIB->ipv4Pckts = 0;
1012  theMIB->ipv4Flow = 0;
1013  theMIB->ipv4IcmpFlowNb = 0;
1014  theMIB->ipv4IcmpBytesNb = 0;
1015  theMIB->ipv4IcmpPktsNb = 0;
1016  theMIB->ipv4UDPFlowNb = 0;
1017  theMIB->ipv4UDPBytesNb = 0;
1018  theMIB->ipv4UDPPktsNb = 0;
1019  theMIB->ipv4TCPFlowNb = 0;
1020  theMIB->ipv4TCPBytesNb = 0;
1021  theMIB->ipv4TCPPktsNb = 0;
1022  theMIB->ipv4OthersFlowNb = 0;
1023  theMIB->ipv4OthersBytesNb = 0;
1024  theMIB->ipv4OthersPktsNb = 0;
1025  theMIB->ipv4FlowSizePcktsE1 = 0;
1026  theMIB->ipv4FlowSizePcktsLT10 = 0;
1027  theMIB->ipv4FlowSizePcktsLT100 = 0;
1028  theMIB->ipv4FlowSizePcktsLT1k = 0;
1029  theMIB->ipv4FlowSizePcktsLT10k = 0;
1030  theMIB->ipv4FlowSizePcktsMT10k = 0;
1031  theMIB->ipv4FlowSizeBytesLT50 = 0;
1032  theMIB->ipv4FlowSizeBytesLT100 = 0;
1033  theMIB->ipv4FlowSizeBytesLT1k = 0;
1034  theMIB->ipv4FlowSizeBytesLT10k = 0;
1035  theMIB->ipv4FlowSizeBytesLT100k = 0;
1036  theMIB->ipv4FlowSizeBytesLT1M = 0;
1037  theMIB->ipv4FlowSizeBytesLT10M = 0;
1038  theMIB->ipv4FlowSizeBytesLT100M = 0;
1039  theMIB->ipv4FlowSizeBytesMT100M = 0;
1040  theMIB->ipv4WebBytesNb = 0;
1041  theMIB->ipv4WebPcktsNb = 0;
1042  theMIB->ipv4WebFlowNb = 0;
1043  theMIB->ipv4DNSBytesNb = 0;
1044  theMIB->ipv4DNSPcktsNb = 0;
1045  theMIB->ipv4DNSFlowNb = 0;
1046  theMIB->ipv4OthersApBytesNb = 0;
1047  theMIB->ipv4OthersApPcktsNb = 0;
1048  theMIB->ipv4OthersApFlowNb = 0;
1049  theMIB->ipv6Bytes = 0;
1050  theMIB->ipv6Pckts = 0;
1051  theMIB->ipv6Flow = 0;
1052  theMIB->ipv6IcmpFlowNb = 0;
1053  theMIB->ipv6IcmpBytesNb = 0;
1054  theMIB->ipv6IcmpPktsNb = 0;
1055  theMIB->ipv6UDPFlowNb = 0;
1056  theMIB->ipv6UDPBytesNb = 0;
1057  theMIB->ipv6UDPPktsNb = 0;
1058  theMIB->ipv6TCPFlowNb = 0;
1059  theMIB->ipv6TCPBytesNb = 0;
1060  theMIB->ipv6TCPPktsNb = 0;
1061  theMIB->ipv6OthersFlowNb = 0;
1062  theMIB->ipv6OthersBytesNb = 0;
1063  theMIB->ipv6OthersPktsNb = 0;
1064  theMIB->ipv6FlowSizePcktsE1 = 0;
1065  theMIB->ipv6FlowSizePcktsLT10 = 0;
1066  theMIB->ipv6FlowSizePcktsLT100 = 0;
1067  theMIB->ipv6FlowSizePcktsLT1k = 0;
1068  theMIB->ipv6FlowSizePcktsLT10k = 0;
1069  theMIB->ipv6FlowSizePcktsMT10k = 0;
1070  theMIB->ipv6FlowSizeBytesLT50 = 0;
1071  theMIB->ipv6FlowSizeBytesLT100 = 0;
1072  theMIB->ipv6FlowSizeBytesLT1k = 0;
1073  theMIB->ipv6FlowSizeBytesLT10k = 0;
1074  theMIB->ipv6FlowSizeBytesLT100k = 0;
1075  theMIB->ipv6FlowSizeBytesLT1M = 0;
1076  theMIB->ipv6FlowSizeBytesLT10M = 0;
1077  theMIB->ipv6FlowSizeBytesLT100M = 0;
1078  theMIB->ipv6FlowSizeBytesMT100M = 0;
1079  theMIB->ipv6WebBytesNb = 0;
1080  theMIB->ipv6WebPcktsNb = 0;
1081  theMIB->ipv6WebFlowNb = 0;
1082  theMIB->ipv6DNSBytesNb = 0;
1083  theMIB->ipv6DNSPcktsNb = 0;
1084  theMIB->ipv6DNSFlowNb = 0;
1085  theMIB->ipv6OthersApBytesNb = 0;
1086  theMIB->ipv6OthersApPcktsNb = 0;
1087  theMIB->ipv6OthersApFlowNb = 0;
1088  return 1;
1089}
Note: See TracBrowser for help on using the browser.