root/trunk/src/renetcol.c @ 97

Revision 97, 23.3 KB (checked in by andreu, 14 years ago)

Rename old releases and update current

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