root/trunk/src/renetcol.c @ 24

Revision 24, 23.0 KB (checked in by andreu, 16 years ago)

aggregation feature first E2E step : collect to rrdfiles

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