root/tags/renetcol-0.0.11/trunk/src/renetcol.c

Revision 47, 22.3 KB (checked in by andreu, 16 years ago)

merge from tag REL-0.0.11beta

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