root/trunk/src/renetcol.c @ 88

Revision 88, 19.9 KB (checked in by andreu, 14 years ago)

ticket #18

  • Property svn:eol-style set to native
Line 
1/*
2 * File: renetcol.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2005 2006 2007 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
101#if defined(IPV4AGGIDR)
102  fprintf(stderr, "renetcol compilation and execution with IPv4 Aggregation feature enable based on routers ID in %s file.\n", ROUTERS_LIST);
103#endif
104
105#if defined(IPV4AGGIDSNMP)
106  fprintf(stderr, "renetcol compilation and execution with IPv4 Aggregation feature enable based on SNMP ID in %s file.\n", INTERFACES_TYPE_LIST);
107#endif
108 
109  openlog(argv[0], LOG_PID, LOG_USER);
110
111  initCache();
112  /* GET the list of routers who are available to send NDE to the collector */
113  if ( (routerNb = getRegisteredRouters(ROUTERS_LIST,
114                                        &sortedRouterList[0],
115                                        &indexedRouterList[0])) == -1) {
116    fprintf(stderr,
117            "ERROR in getRegisteredRouters from file %s\n",
118            ROUTERS_LIST);
119    exit(-1);
120  }
121  /* Create the list of routers */
122  for (i=0; i<routerNb; i++){
123    routersListPtr = addRouter(routersListPtr, sortedRouterList[i]);
124  }
125 
126  /* Shared memory */
127  key = 8765;
128  if ((shmid = shmget(key, SHMSIZE, IPC_CREAT | 0666)) < 0) {
129    perror("shmget");
130    exit(1);
131  }
132  if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1) {
133    perror("shmat");
134    exit(1);
135  }
136
137  if ( (shmForAgg->v4PrefixNb =
138        getPrefixV4(SUBNETS_LIST,
139                    &(shmForAgg->prefixV4Tab[0][0]),
140                    &(shmForAgg->prefixV4SubnetTab[0][0]),
141                    &(shmForAgg->v4SubnetNb)
142                    )) < 1){
143    fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST);
144  } else {
145    fprintf(stderr, "We have found %hu supernets from %s\n",
146            shmForAgg->v4PrefixNb,
147            SUBNETS_LIST);
148    fprintf(stderr, "and %hu subnets\n",
149            shmForAgg->v4SubnetNb);   
150  }
151  if ( (shmForAgg->v4PrefixNb =
152        getPrefixV4(SUBNETS_LIST,
153                    &(shmForAgg->prefixV4Tab[1][0]),
154                    &(shmForAgg->prefixV4SubnetTab[1][0]),
155                    &(shmForAgg->v4SubnetNb)
156                    )) < 1){
157    fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST);
158    exit(1);
159  }
160/*   for (i=0; i<shmForAgg->v4PrefixNb; i++){ */
161/*    fprintf(stderr, "SUPERNET : %lu, %hu\n",shmForAgg->prefixV4Tab[0][i].beginning, shmForAgg->prefixV4Tab[0][i].hasSubnet); */
162/*   } */
163/*   for (i=0; i<shmForAgg->v4SubnetNb; i++){ */
164/*    fprintf(stderr, "SUBNET : %lu, %hu\n",shmForAgg->prefixV4SubnetTab[0][i].beginning, shmForAgg->prefixV4SubnetTab[0][i].hasSubnet); */
165/*   } */
166
167  shmForAgg->currentTable = 0;
168  shmForAgg->secondTable = 1;
169  shmForAgg->readed = 0;
170  /* get the rules */
171  myPtrs.rulesListPtr = NULL;
172  myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE);
173  tmp = myPtrs.rulesListPtr;
174 if (tmp!=NULL){
175   for ( ; tmp->next; tmp=tmp->next) {
176     if (tmp->type != 2) {
177       myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
178     }
179   }
180 }
181  tmp = NULL;
182#ifdef DEBUG
183  printRule(myPtrs.rulesListPtr);
184#endif
185
186#ifdef ASACC
187  /* get the AS list */
188  if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[0][0]))) < 1){
189    fprintf(stderr, "renetcol: Error in %s\n", AS_LIST);
190    exit(1);
191  } else {
192    fprintf(stderr, "%hu AS are readed from %s\n",
193            shmForAgg->ASNb,
194            AS_LIST);
195  }
196  if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[1][0]))) < 1){
197    fprintf(stderr, "renetcol: Error in %s\n", AS_LIST);
198    exit(1);
199  }
200#endif
201
202  /* the fork */
203  fprintf(stderr, "renetcol: I become a deamon, next messages via syslogd. Bye.\n");
204  if (fork () != 0)
205    exit (0);
206  if (setsid() == -1){
207    exit(4);
208  }
209
210  /* handler, SIGUSR1 from renetcolSender */
211  myAction.sa_handler = sigusr1Mgmt;
212  myAction.sa_flags = SA_RESTART;
213  sigemptyset (&(myAction.sa_mask));
214  sigaddset (&(myAction.sa_mask), SIGALRM);
215  sigaction (SIGUSR1, &myAction, NULL);
216
217  /* Cache for fast rules access */
218  setCache(myPtrs.rulesListPtr);
219  myPtrs.rulesAddressPtr = (RuleDefPtr *)rulesAddress;
220
221  /* IPC messages queue init */
222  myKey = createKey(argv[0]);
223  myQueue = createQueue(myKey);
224  sendMyPid(myQueue);
225
226  /* get SNMP index information */
227#ifdef IPV4AGGIDSNMP
228  k = getSNMPIndexList(INTERFACES_TYPE_LIST, routersListPtr);
229#endif
230  /* INIT THE CURRENTS VARIABLES*/
231  myPtrs.currentRouterPtr = routersListPtr;
232  myPtrs.currentHeaderV9Ptr = (NetFlowV9HeaderPtr)
233    malloc(sizeof(struct NetFlowV9Header));
234  myPtrs.offsetV9Ptr = &offsetV9;
235  myPtrs.currentFlowsetIdPtr = &currentFlowsetId;
236  myPtrs.pcktPtr = (DatagramPtr) malloc(sizeof(struct Datagram));
237  myPtrs.currentFlowsetNumberPtr = &currentFlowsetNumber;
238#ifdef IPV4AGGIDR
239  myPtrs.routersID = &indexedRouterList;
240#endif
241  myPtrs.currentV4Tab = shmForAgg->prefixV4Tab[shmForAgg->currentTable];
242  myPtrs.secondV4Tab = shmForAgg->prefixV4Tab[shmForAgg->secondTable];
243#ifdef ASACC
244  myPtrs.asNb = shmForAgg->ASNb;
245  myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable];
246  myPtrs.secondASTab = shmForAgg->ASTab[shmForAgg->secondTable];
247#endif
248#ifdef MATRIX
249  myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]);
250#endif
251
252  /* INIT MATRIX TABLES */
253#ifdef MATRIX
254  for (i=0; i<ROUTER_INDEX_MAX; i++){
255    for (j=0; j<ROUTER_INDEX_MAX; j++) {
256      shmForAgg->matrixPOP[0][i][j].bytesNb = 0;
257      shmForAgg->matrixPOP[0][i][j].pktsNb = 0;
258      shmForAgg->matrixPOP[0][i][j].flowNb = 0;
259      shmForAgg->matrixPOP[1][i][j].bytesNb = 0;
260      shmForAgg->matrixPOP[1][i][j].pktsNb = 0;
261      shmForAgg->matrixPOP[1][i][j].flowNb = 0;
262    }
263  }
264#endif
265
266  /* Checkup */
267  if (myPtrs.pcktPtr==NULL) {
268    fprintf(stderr, "ERROR in struct Datagram allocation\n");
269    exit(1);
270  } else {
271    myPtrs.pcktPtr->ipH = (IpHeaderPtr) malloc(sizeof(struct IpHeader));
272    myPtrs.pcktPtr->udp_header = (UdpHeaderPtr) malloc(sizeof(struct UdpHeader));
273  }
274  if (! (myPtrs.ptr_buffer = malloc(sockBufSize))) 
275    {
276      printf("ERROR during socket buffer allocation\n");
277      exit(2);
278    }
279
280  /* INIT INPUT STREAM*/
281  initStream();
282
283  socketLoop(); /* all work on datagram is made here */
284
285  closelog();
286
287  fprintf(stderr, "END\n");
288
289  return (0);
290}
291
292/*
293 * send his PID to another program via IPC message queue
294 *
295 * @param queueID the IPC queue ident
296 *
297 */
298void 
299sendMyPid(int queueID)
300{
301  msgType myMsg;
302  char *msgTextIndex;
303  unsigned short tplMsgType = 12;
304  pid_t myPID;
305 
306  myPID = getpid();
307  msgTextIndex = mempcpy(mempcpy(myMsg.text,
308                                 &tplMsgType,
309                                 sizeof (unsigned short)
310                                 ),
311                         &myPID,
312                         sizeof(pid_t)
313                         );
314  myMsg.type = 1;
315  msgSend(queueID, myMsg);
316}
317
318/*
319 * Send the "read rules list" message to another program
320 *
321 * @param queueID the IPC queue ident
322 *
323 */
324void sendReadRulesSignal(int queueID)
325{
326  msgType myMsg;
327  char *msgTextIndex;
328  unsigned short tplMsgType = 13;
329 
330  msgTextIndex = mempcpy(myMsg.text,
331                         &tplMsgType,
332                         sizeof (unsigned short)
333                         );
334  myMsg.type = 1;
335  msgSend(queueID, myMsg);
336}
337
338/*
339 *
340 */
341void 
342sigusr1Mgmt(int num)
343{
344  sigusr1Up = 1;
345}
346
347/*
348 * init the rule structure
349 */
350void 
351initRule()
352{
353  RulesPtr tmp = NULL;
354  initCache();
355  tmp = myPtrs.rulesListPtr;
356  for ( ; tmp; tmp=tmp->next) {
357    myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
358  }
359  myPtrs.rulesListPtr = NULL;
360  myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE);
361  tmp = myPtrs.rulesListPtr;
362  for ( ; tmp; tmp=tmp->next) {
363    if (tmp->type != 2) {
364      myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr);
365    }
366  }
367  tmp = NULL;
368  initCache();
369  setCache(myPtrs.rulesListPtr);
370  sendReadRulesSignal(myQueue);
371#ifdef DEBUG
372  printRule(myPtrs.rulesListPtr);
373#endif
374}
375
376/*
377 * init cache table
378 */
379void
380initCache()
381{
382  int i,j;
383  for (i=0; i<FIELD_TYPE_NUMBER+1; i++){
384    for (j=0; j<MAX_RULES_PER_FIELD; j++){
385      rulesAddress[i][j] = NULL;
386    }
387  }
388}
389
390/*
391 * setCache()
392 */
393void
394setCache(RulesPtr rPtr)
395{
396  int i=0;
397  RulesPtr tmp = rPtr;
398  RuleDefPtr def = NULL;
399
400#ifdef DEBUG
401  fprintf(stderr,"Cache for Rules\n");
402#endif 
403  while (tmp) {
404    def = tmp->def;
405    while (def) {
406      i = 0;
407      while (rulesAddress[def->fieldType][i] != NULL){
408        i++;
409      }
410      rulesAddress[def->fieldType][i] = def;
411      def = def->next;
412#ifdef DEBUG
413      fprintf(stderr,"%d: %d ", def->fieldType, i);
414#endif     
415    }
416#ifdef DEBUG
417    fprintf(stderr,"\n");
418#endif     
419    tmp = tmp->next;
420  }
421}
422
423/*
424 * initStream()
425 */
426void 
427initStream()
428{
429  static unsigned short n0, n1, n2, n3;
430
431  initSocket();
432  if (sscanf(receptAddress,"%hu.%hu.%hu.%hu",&n0,&n1,&n2,&n3)==0) {
433    perror("sscanf");
434  }             
435  buffer4[0] = (unsigned char)n3;
436  buffer4[1] = (unsigned char)n2;
437  buffer4[2] = (unsigned char)n1;
438  buffer4[3] = (unsigned char)n0;
439}
440
441/*
442 * socketLoop()
443 */
444int 
445socketLoop()
446{
447  short shift;
448  short version = 0;
449  int regRouter = 0;
450  time_t now = time((time_t *)NULL);
451  int loopNb = 0;
452  int gardeFou = 0;
453  time_t lastIPOutput, lastMPLSOutput;
454
455  now = time((time_t *)NULL);
456  tmPtr = localtime(&now);
457  currentIntervalle = tmPtr->tm_min;
458  lastMPLSOutput = now;
459  lastIPOutput = now;
460  do {
461#ifdef DEBUG   
462    fprintf (stderr, "[");
463#endif
464    if ( ((tmPtr->tm_min)%STEP == 0) && (currentIntervalle != tmPtr->tm_min)){
465      currentIntervalle = tmPtr->tm_min;
466      if (shmForAgg->currentTable == 0) {
467        shmForAgg->currentTable = 1;
468        shmForAgg->secondTable = 0;
469      }else{
470        shmForAgg->currentTable = 0;
471        shmForAgg->secondTable = 1;
472      }
473      shmForAgg->readed = 1;
474#ifdef ASACC
475      myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable];
476#endif
477#ifdef MATRIX
478      myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]);
479#endif
480    }
481    if (sigusr1Up == 1){
482      sigusr1Up = 0;
483      initRule();
484    }
485    socketReading();
486    getIpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer);
487    regRouter = checkIpHeader(myPtrs.pcktPtr, &sortedRouterList[0], routerNb);
488    getUdpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer);
489    if ( checkUdpHeader(myPtrs.pcktPtr, regRouter, receptPort) == 1 ){
490      continue;
491    }
492   
493    switch( version =
494            getNetFlowHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer,
495                             myPtrs.currentHeaderV9Ptr, myPtrs.offsetV9Ptr)){
496    case 9:
497      if ((myPtrs.currentRouterPtr=notExistRouter(routersListPtr,
498                                                  myPtrs.pcktPtr->ipH->srcAdd))==NULL) {
499        myPtrs.currentRouterPtr = routersListPtr;
500      }
501      currentFlowsetNumber = 0;
502      shift = 0;
503      gardeFou=0;
504      while ((currentFlowsetNumber < myPtrs.currentHeaderV9Ptr->count)) {
505        gardeFou++;
506        curTplFlSetPtr = NULL;
507        currentFlowsetId=getFlowsetId(currentFlowsetNumber,
508                                      myPtrs.offsetV9Ptr, myPtrs.ptr_buffer);
509        if ( currentFlowsetId == 0 ) {
510          if ( (shift = checkTemplateFlowSet(myPtrs.currentRouterPtr,
511                                             myPtrs.offsetV9Ptr,
512                                             myPtrs.ptr_buffer,
513                                             myPtrs.currentHeaderV9Ptr,
514                                             curTplFlSetPtr,
515                                             myPtrs.currentFlowsetNumberPtr,
516                                             myQueue)) < 0 ) {
517#ifdef DEBUG
518            fprintf (stderr, "w>\n");
519#endif
520            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
521          };
522          writeAllTplFlSet();
523        } else if (currentFlowsetId == 1) {
524          if ( (shift = checkTemplateOption(myPtrs.currentRouterPtr,
525                                            myPtrs.offsetV9Ptr,
526                                            myPtrs.ptr_buffer,
527                                            myPtrs.currentHeaderV9Ptr,
528                                            curTplOptionPtr,
529                                            myPtrs.currentFlowsetNumberPtr) ) < 0 ) {
530#ifdef DEBUG
531            fprintf (stderr, "wo>\n");
532#endif 
533            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
534          }
535        } else if (currentFlowsetId > 255) {
536          if ( (shift = checkDataFlowSet(shift,
537                                         &myPtrs,
538                                         myQueue,
539                                         shmForAgg->prefixV4Tab[shmForAgg->currentTable],
540                                         (size_t) shmForAgg->v4PrefixNb,
541                                         shmForAgg->prefixV4SubnetTab[shmForAgg->currentTable],
542                                         (size_t) shmForAgg->v4SubnetNb) ) < 0 ) {
543#ifdef DEBUG   
544            fprintf (stderr, "wd>\n");
545#endif
546            currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
547          }
548        } else {
549#ifdef DEBUG   
550          fprintf (stderr, "1<<255\n");
551#endif
552          currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1;
553        }
554        if ( gardeFou > 200) { exit(-1); }
555      } /* end while flowset exist */
556      break;
557    default:
558      syslog(LOG_INFO,
559             "NetFlow exp. version unknown: %hu, from router: %lu.%lu.%lu.%lu",
560             (unsigned short) version,
561             (myPtrs.pcktPtr->ipH->srcAdd>>24),
562             (myPtrs.pcktPtr->ipH->srcAdd<<8>>24),
563             (myPtrs.pcktPtr->ipH->srcAdd<<16>>24),
564             (myPtrs.pcktPtr->ipH->srcAdd<<24>>24));
565      break;
566    }
567    if (loopNb  > 1000 ){
568      /* FIXME perhaps call this function via the compilation options */
569      writeAllTplFlSet();
570#ifdef DEBUG   
571      fprintf(stderr, "W");
572#endif
573      loopNb = 0;
574    }
575    loopNb++;
576
577    now = time((time_t *)NULL);
578    tmPtr = localtime(&now);
579#ifdef DEBUG   
580    fprintf (stderr, "]\n");
581#endif
582  } while (1);
583}
584
585/*
586 * initSocket()
587 */
588short 
589initSocket()
590{
591  inputSock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
592  if (inputSock < 0)
593    {
594      syslog(LOG_ERR,"socket : %s", strerror(errno));
595      exit(1);
596    }
597  memset((void*)&name, 0, sizeof(name));
598  name.sin_family = AF_INET;
599  name.sin_addr.s_addr = htonl(INADDR_ANY);
600  if (name.sin_addr.s_addr == INADDR_NONE){
601    syslog(LOG_ERR, " INADDR_NONE ");
602    exit(1);
603  }
604  name.sin_port = htons(receptPort);
605  if (bind(inputSock,(struct sockaddr *)(&name), sizeof(name)) < 0)
606    {
607      syslog(LOG_ERR, "bind : %s", strerror(errno));
608      exit(1);
609    }                                                           
610  sockNamelg = sizeof(name);
611  if (getsockname(inputSock, (struct sockaddr *) (&name),
612                  (socklen_t *)&sockNamelg) < 0)
613    {
614      syslog(LOG_ERR, "getsockname: %s", strerror(errno));
615      exit(1);
616    }
617  /* Here socket DGRAM creation, only to not have a unreachable service */
618  /* message in return */
619  inputSock2 = socket(AF_INET, SOCK_DGRAM, 0);
620  if (inputSock2 < 0)
621    {
622      syslog(LOG_ERR,"socket : %s", strerror(errno));
623      exit(1);
624    }
625  memset((void*)&name, 0, sizeof(name));
626  name2.sin_family = AF_INET;
627  name2.sin_addr.s_addr = htonl(INADDR_ANY);
628  if (name2.sin_addr.s_addr == INADDR_NONE){
629    syslog(LOG_ERR, " INADDR_NONE ");
630    exit(1);
631  }
632  name2.sin_port = htons(receptPort);
633  if (bind(inputSock2,(struct sockaddr *)(&name2), sizeof(name2)) < 0)
634    {
635      syslog(LOG_ERR, "bind : %s", strerror(errno));
636      exit(1);
637    }                                                           
638  sockNamelg = sizeof(name2);
639  if (getsockname(inputSock2, (struct sockaddr *) (&name2),
640                  (socklen_t *)&sockNamelg) < 0)
641    {
642      syslog(LOG_ERR, "getsockname: %s", strerror(errno));
643      exit(1);
644    }
645  return(0);
646}
647
648
649/*
650 * init socket
651 */
652short 
653socketReading()
654{
655  signed short sockLg;
656
657  sockNameFromlg = sizeof(fromName);
658  rcv = recvfrom(inputSock, myPtrs.ptr_buffer, sockBufSize, 0,
659                 (struct sockaddr *)(&fromName),
660                 (socklen_t *)&sockNameFromlg);   
661  sockLg = rcv;
662  if (sockLg < 0) {
663    syslog(LOG_ERR,"recvfrom : %s", strerror(errno));
664    exit(1);
665  }
666  if (sockLg == 0) {
667    syslog(LOG_ERR,"recvfrom : %s", strerror(errno));
668    exit(1);
669  }
670  return(0);
671}
672 
673/*
674 * check up flow datagramme 
675 */
676short 
677checkFlow(short flowNumber)
678{
679  return(0); /* FIXME : why this function ??? */
680}
681
682/*
683 * showAllTplFlSet
684 *
685 * to use only in debug mode
686 */
687void 
688showAllTplFlSet()
689{
690  RouterPtr tmp = routersListPtr;
691  TplFlowSetPtr tmpFS;
692  TplOptionPtr tmpOP;
693  fprintf(stderr,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n");
694  for (; tmp; tmp=tmp->next) {
695    fprintf(stderr,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n",
696            (tmp->IpAddress>>24),
697            (tmp->IpAddress<<8>>24),
698            (tmp->IpAddress<<16>>24),
699            (tmp->IpAddress<<24>>24));
700    tmpFS =  tmp->tplList;
701    for (; tmpFS; tmpFS=tmpFS->next) {
702      fprintf(stderr,"TId %hu (sourceId: %lu):\n",
703              tmpFS->templateFlowSetId,
704              tmpFS->sourceId);
705      printFieldSet(stderr, tmpFS->fieldSet);
706      fprintf(stderr,"\n");
707    }
708    if ((tmpOP = tmp->tplOptList) != NULL){
709      for (; tmpOP; tmpOP=tmpOP->next) {
710        fprintf(stderr,"OpTId %hu (sourceId: %lu) >\n",
711                tmpOP->templateOptionId,
712                tmpOP->sourceId);
713        printFieldSet(stderr, tmpOP->fieldSet);
714        fprintf(stderr,"\n");
715      }
716      fprintf(stderr,"\n");
717    }
718  }
719}
720
721
722/*
723 * writeAllTplFlSet
724 *
725 */
726void 
727writeAllTplFlSet()
728{
729
730  RouterPtr tmp = routersListPtr;
731  TplFlowSetPtr tmpFS;
732  TplOptionPtr tmpOP;
733  FILE *TPLFILE;
734  if (!(TPLFILE = fopen("/tmp/TemplateDef.txt", "w"))) {
735    syslog (LOG_ERR, "error during %s opening", "/tmp/TemplateDef.txt");
736  }
737  fprintf(TPLFILE,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n");
738  for (; tmp; tmp=tmp->next) {
739    fprintf(TPLFILE,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n",
740            (tmp->IpAddress>>24),
741            (tmp->IpAddress<<8>>24),
742            (tmp->IpAddress<<16>>24),
743            (tmp->IpAddress<<24>>24));
744    tmpFS =  tmp->tplList;
745    for (; tmpFS; tmpFS=tmpFS->next) {
746      fprintf(TPLFILE,"TId %hu (sourceId: %lu):\n",
747              tmpFS->templateFlowSetId,
748              tmpFS->sourceId);
749      printFieldSet(TPLFILE, tmpFS->fieldSet);
750      fprintf(TPLFILE,"\n");
751    }
752    if ((tmpOP = tmp->tplOptList) != NULL){
753      for (; tmpOP; tmpOP=tmpOP->next) {
754        fprintf(TPLFILE,"OpTId %hu (sourceId: %lu) last %d brackets are Scope >\n lg: %hu, opScopeLg: %hu, opLg: %hu\n",
755                tmpOP->templateOptionId,
756                tmpOP->sourceId,
757                tmpOP->optionScopeLg/4,
758                tmpOP->length,
759                tmpOP->optionScopeLg,
760                tmpOP->optionLg);
761        printFieldSet(TPLFILE, tmpOP->fieldSet);
762        fprintf(TPLFILE,"\n");
763      }
764      fprintf(TPLFILE,"\n");
765    }
766  }
767  fclose(TPLFILE);
768}
Note: See TracBrowser for help on using the browser.