root/trunk/src/dataFlowSet.c @ 34

Revision 34, 32.7 KB (checked in by andreu, 16 years ago)

bug on bad reading of routers configuration file fixed
bug on wrong sampled value fixed
in tool, we add a supervisor script shell

  • Property svn:eol-style set to native
Line 
1/*
2 * File: dataFlowSet.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 "dataFlowSet.h"
27
28/*
29 * Check Data FlowSet
30 *
31 */
32short 
33checkDataFlowSet(short shift,
34                 struct MyPtrs *myPtrs,
35                 int myQueue,
36                 struct PrefixV4 *V4PTab,
37                 size_t nbPV4
38                 )
39{
40  TplFlowSetPtr tmp = NULL;
41  TplOptionPtr tmpOpt = NULL;
42  FieldPtr pftmp = NULL;
43  FieldPtr secondPftmp = NULL;
44  unsigned short data_length = 0;
45  unsigned short flow_size = 0;
46  unsigned short oldOffset = *myPtrs->offsetV9Ptr;
47  unsigned short secondOffset = 0;
48  unsigned short secondOldOffset = 0;
49  int moreIsNecessary = 1;
50  int field_size = 0;
51  int cpt = 0;
52  int secondCpt = 0;
53  int overflow = 0;
54  int noEnd = 1;
55  int i = 0;
56  int j = 0;
57  int pos = 0;
58  unsigned char buffer1; 
59  unsigned char buffer2[2];
60  unsigned char buffer4[4];
61  RulesPtr tmpRuleList = myPtrs->rulesListPtr;
62  msgType myMsg;
63  char *msgTextIndex;
64  unsigned short tplMsgType = 11;
65  struct PrefixV4 prefixKey, *res; /* for bsearch */
66  struct AggCache agCache;
67  int bool = 0; /* in IPV4 Agg mode enabled, we need to now if it's an IPv4 */
68                /* flow, we test on the field and then put bool at 1 */
69  int isMplsFlow = 0;
70  int paddingCounter = 0;
71  int crazyCounter = 0;
72
73#ifdef CRIHAN
74  struct IPFLowCache ipFirstCache;
75  struct MPLSFlowCache mplsFirstCache;
76  unsigned long firstTime = 0;
77  unsigned long lastTime = 0;
78
79  ipFirstCache.ipProt = 0;
80  ipFirstCache.bytes = 0;
81  ipFirstCache.pkts = 0;
82  ipFirstCache.inSnmp = 0;
83  ipFirstCache.outSnmp = 0;
84  ipFirstCache.v4AdS = 0;
85  ipFirstCache.v4AdD = 0;
86  ipFirstCache.tProt = 0;
87  ipFirstCache.sPort = 0;
88  ipFirstCache.dPort = 0;
89  ipFirstCache.maskD = 0;
90  ipFirstCache.maskS = 0;
91  ipFirstCache.routerAd = 0;
92  ipFirstCache.liveTime = 0;
93  mplsFirstCache.ipProt = 0;
94  mplsFirstCache.v4AdS = 0;
95  mplsFirstCache.v4AdD = 0;
96  mplsFirstCache.routerAd = 0;
97  mplsFirstCache.mplsLabel1 = 0;
98#endif /* CRIHAN */
99
100  buffer2[1] = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));(*myPtrs->offsetV9Ptr)++;
101  buffer2[0] = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));(*myPtrs->offsetV9Ptr)++;
102  (*myPtrs->currentFlowsetIdPtr) = *((unsigned short*)&buffer2);
103  buffer2[1] = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));(*myPtrs->offsetV9Ptr)++;
104  buffer2[0] = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));(*myPtrs->offsetV9Ptr)++;
105  data_length = *((unsigned short*)&buffer2);
106
107  if ( (tmp=existTplId(myPtrs->currentRouterPtr, myPtrs->currentHeaderV9Ptr->sourceId,
108                      (*myPtrs->currentFlowsetIdPtr)))!=NULL ) {
109#ifdef DEBUG
110    fprintf(stderr,"{d id: %hu, lg %hu", (*myPtrs->currentFlowsetIdPtr), data_length);
111    if ( (*myPtrs->currentFlowsetIdPtr) > TRESHOLD_TEMPLATE_ID ) {
112/*      fprintf(stderr,"Bad TID from router: %lu.%lu.%lu.%lu\n", */
113/*              (*myPtrs->currentFlowsetIdPtr), */
114/*              (myPtrs->pcktPtr->ipH->srcAdd>>24), */
115/*              (myPtrs->pcktPtr->ipH->srcAdd<<8>>24), */
116/*              (myPtrs->pcktPtr->ipH->srcAdd<<16>>24), */
117/*              (myPtrs->pcktPtr->ipH->srcAdd<<24>>24)); */
118/*      fprintf(stderr," Bytes : \n"); */
119/*      while ( ( myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr) ) > (myPtrs->ptr_buffer+1416) ){ */
120/*        buffer1= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr));(*myPtrs->offsetV9Ptr)++; */
121/*        tmp = *((unsigned char*)&buffer1); */
122/*        fprintf (stderr, " %s ", tmp); */
123/*      } */
124/*      exit(-1); */
125      return (data_length+shift);
126    }
127#endif
128    pftmp = tmp->lastField;
129    for (; pftmp; pftmp = pftmp->prev) {
130      flow_size += pftmp->fieldLength;
131    }
132    if ( data_length%flow_size >= 9 ) {
133      (*myPtrs->currentFlowsetNumberPtr) = myPtrs->currentHeaderV9Ptr->count;
134      syslog(LOG_INFO, "data flowset length not match with length from template definition, wrong template definition suspected; all next informations of this data flowset are not considered! flowset ID: %hu, from router: %lu.%lu.%lu.%lu",
135             (*myPtrs->currentFlowsetIdPtr),
136             (myPtrs->pcktPtr->ipH->srcAdd>>24),
137             (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
138             (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
139             (myPtrs->pcktPtr->ipH->srcAdd<<24>>24));
140      return (data_length+shift);
141    }
142    if ( data_length >= 1452 ) {
143      syslog(LOG_INFO, "data flowset length too raised; all next informations of this data flowset are not considered! flowset ID: %hu, from router: %lu.%lu.%lu.%lu",
144             (*myPtrs->currentFlowsetIdPtr),
145             (myPtrs->pcktPtr->ipH->srcAdd>>24),
146             (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
147             (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
148             (myPtrs->pcktPtr->ipH->srcAdd<<24>>24));
149      return (-1);
150    }
151#ifdef IPV4AGG
152    agCache.routerAd = myPtrs->pcktPtr->ipH->srcAdd;
153#endif
154
155#ifdef CRIHAN
156    ipFirstCache.routerAd = myPtrs->pcktPtr->ipH->srcAdd;
157    mplsFirstCache.routerAd = myPtrs->pcktPtr->ipH->srcAdd;
158#endif /* CRIHAN */
159
160    pftmp = tmp->lastField;
161    secondPftmp = tmp->lastField;   
162    secondOffset = *myPtrs->offsetV9Ptr;
163    secondOldOffset = secondOffset;
164    while ( (((*myPtrs->offsetV9Ptr)-48-shift) <= data_length) && (overflow!=1) ) {
165      /*
166       * progression in a data flow Set
167       * notes:
168       *   48=header ip + header netf
169       *   shift = shift if there is a template declaration
170       */
171      cpt++;
172      j = 0;
173      pos = (pftmp->fieldType)*10+j;
174      field_size = (int) pftmp->fieldLength;
175     
176      /* special case: check yes on all flows from one router (phantom field nb 0) */
177      while ( ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+j))) != NULL ) { 
178        if  ( (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+j)))->value->stor.lvalue)
179              == myPtrs->pcktPtr->ipH->srcAdd )
180          {
181            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+j)))->check = 1;
182          }     
183        j++;
184      }   
185      j = 0;
186     
187      /*
188       * Comparaison between the field value and the rules
189       * ... if one rule exist
190       * FIXME : warning, if no rules, no accounting ?
191       */
192      if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos))) != NULL) {
193        oldOffset = *myPtrs->offsetV9Ptr;
194        while (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos))) != NULL){
195          /*
196           * while on one cache table line
197           */
198          *myPtrs->offsetV9Ptr = oldOffset;
199          switch ((int)
200                  (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->operator)){
201          case 2:
202            /* operator: "=" */ 
203            switch (field_size) {
204            case 1:
205              buffer1 = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
206              /* rule check */
207              if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.cvalue
208                  == *((unsigned char*)&buffer1)) {
209                ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
210              }
211              /* end rule check */
212#ifdef IPV4AGG
213              if ((pftmp->fieldType==9)||(pftmp->fieldType==29)){
214                agCache.maskS = *((unsigned char*)&buffer1);
215              }
216              if ((pftmp->fieldType==13)||(pftmp->fieldType==30)){
217                agCache.maskD = *((unsigned char*)&buffer1);
218              }
219              if (pftmp->fieldType==60){
220                agCache.ipProt = *((unsigned char*)&buffer1);
221              }
222              if (pftmp->fieldType==4){
223                agCache.tProt = *((unsigned char*)&buffer1);
224              }
225              if (pftmp->fieldType==61){
226                agCache.sens = *((unsigned char*)&buffer1);
227              }
228              if (pftmp->fieldType==5){
229                agCache.dscp = *((unsigned char*)&buffer1);
230              }
231#endif       
232#ifdef CRIHAN
233              if ((pftmp->fieldType==9)||(pftmp->fieldType==29)){
234                ipFirstCache.maskS = *((unsigned char*)&buffer1);
235              }
236              if ((pftmp->fieldType==13)||(pftmp->fieldType==30)){
237                ipFirstCache.maskD = *((unsigned char*)&buffer1);
238              }
239              if (pftmp->fieldType==60){
240                ipFirstCache.ipProt = *((unsigned char*)&buffer1);
241                mplsFirstCache.ipProt = *((unsigned char*)&buffer1);
242              }
243              if (pftmp->fieldType==4){
244                ipFirstCache.tProt = *((unsigned char*)&buffer1);
245              }
246#endif
247              break;
248            case 2:
249              buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
250              buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
251              if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.svalue
252                  == *((unsigned short*)&buffer2))
253                ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
254#ifdef IPV4AGG
255              if (pftmp->fieldType==10){
256                agCache.idSnmp = *((unsigned short*)&buffer2);
257              }
258#endif
259#ifdef CRIHAN
260              if (pftmp->fieldType==10){
261                ipFirstCache.inSnmp = *((unsigned short*)&buffer2);
262              }
263              if (pftmp->fieldType==14){
264                ipFirstCache.outSnmp = *((unsigned short*)&buffer2);
265              }
266              if (pftmp->fieldType==7){
267                ipFirstCache.sPort = *((unsigned short*)&buffer2);
268              }
269              if (pftmp->fieldType==11){
270                ipFirstCache.dPort = *((unsigned short*)&buffer2);
271              }
272#endif
273              break;
274            case 3:
275              buffer4[3]= 0;
276              buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
277              buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
278              buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
279              if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.lvalue
280                  == *((unsigned long*)&buffer4))
281                ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
282              /* aggregation */
283              /* end aggregation */
284              if (pftmp->fieldType==70){
285                isMplsFlow = 1;
286              }
287#ifdef CRIHAN
288              if (pftmp->fieldType==70){
289                mplsFirstCache.mplsLabel1 = (*((unsigned long*)&buffer4))>>4;
290              }
291#endif
292              break;
293            case 4:
294              buffer4[3]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
295              buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
296              buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
297              buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
298              /* FIXME : here , add a check on the field type */
299              if ((pftmp->fieldType==8)||(pftmp->fieldType==12)){
300                if ((((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.lvalue)
301                    == (*((unsigned long*)&buffer4))>>(32-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(32-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask) )
302                  {
303                    ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
304                  }
305              }
306#ifdef IPV4AGG
307              if ((pftmp->fieldType==8)){
308                bool = 1; /* very important, test if we have an IPv4 flow for Agg */
309                agCache.v4AdS = *((unsigned long*)&buffer4);
310              }
311              if ((pftmp->fieldType==12)){
312                agCache.v4AdD = *((unsigned long*)&buffer4);
313              }
314              if (pftmp->fieldType==1){
315                agCache.bytes = *((unsigned long*)&buffer4);
316              }
317              if (pftmp->fieldType==2){
318                agCache.pkts = *((unsigned long*)&buffer4);
319              }
320#endif
321#ifdef CRIHAN
322              if (pftmp->fieldType==8){
323                ipFirstCache.v4AdS = *((unsigned long*)&buffer4);
324                mplsFirstCache.v4AdS = *((unsigned long*)&buffer4);
325              }
326              if (pftmp->fieldType==12){
327                ipFirstCache.v4AdD = *((unsigned long*)&buffer4);
328                mplsFirstCache.v4AdD = *((unsigned long*)&buffer4);
329              }
330              if (pftmp->fieldType==1){
331                ipFirstCache.bytes = *((unsigned long*)&buffer4);
332              }
333              if (pftmp->fieldType==2){
334                ipFirstCache.pkts = *((unsigned long*)&buffer4);
335              }
336              if (pftmp->fieldType==22){
337                firstTime = *((unsigned long*)&buffer4);
338              }
339              if (pftmp->fieldType==21){
340                lastTime = *((unsigned long*)&buffer4);
341              }
342#endif
343              break;
344            case 16:
345              for (i=0; i<4; i++) {
346                buffer4[3]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
347                buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
348                buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
349                buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
350                if (1==moreIsNecessary){
351                  switch(i){
352                  case 0:
353                    if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask <= 32){
354                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
355                          ==
356                          (*((unsigned long*)&buffer4))>>(32-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(32-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)
357                          )
358                        {
359                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
360                          moreIsNecessary = 0;
361                        } else {
362                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
363                          moreIsNecessary = 0;
364                        }
365                    } else {
366                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
367                          ==
368                          (*((unsigned long*)&buffer4))
369                          )
370                        {
371                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 1;
372                        } else {
373                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
374                          moreIsNecessary = 0;
375                        }
376                    }
377                    break;
378                  case 1:
379                    if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask <= 64){
380                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
381                          ==
382                          (*((unsigned long*)&buffer4))>>(64-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(64-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)
383                          )
384                        {
385                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
386                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
387                          moreIsNecessary = 0;
388                        } else {
389                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
390                          moreIsNecessary = 0;
391                        }
392                    } else {
393                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
394                          ==
395                          (*((unsigned long*)&buffer4))
396                          )
397                        {
398                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
399                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
400                        } else {
401                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
402                          moreIsNecessary = 0;
403                        }
404                    }
405                    break;
406                  case 2:
407                    if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask <= 96){
408                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
409                          ==
410                          (*((unsigned long*)&buffer4))>>(96-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(96-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)
411                          )
412                        {
413                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
414                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
415                          moreIsNecessary = 0;
416                        } else {
417                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
418                          moreIsNecessary = 0;
419                        }
420                    } else {
421                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
422                          ==
423                          (*((unsigned long*)&buffer4))
424                          )
425                        {
426                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
427                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
428                        } else {
429                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
430                          moreIsNecessary = 0;
431                        }
432                    }
433                    break;
434                  case 3:
435                    if (((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask <= 128){
436                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
437                          ==
438                          (*((unsigned long*)&buffer4))>>(128-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)<<(128-((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->mask)
439                          )
440                        {
441                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
442                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
443                        } else {
444                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
445                          moreIsNecessary = 0;
446                        }
447                    } else {
448                      if (ntohl(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->value->stor.tabAdd6[i])
449                          ==
450                          (*((unsigned long*)&buffer4))
451                          )
452                        {
453                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check =
454                            ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check && 1;
455                        } else {
456                          ((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->check = 0;
457                          moreIsNecessary = 0;
458                        }
459                    }
460                    break;
461                  default:
462                    break;
463                  } /* end of switch(i) */
464                } /* end of if moreIsNecessary */
465              }
466              moreIsNecessary = 1;
467              /* aggregation for IPv6 flows */
468             
469              /* end aggregation */
470              break;
471            default:
472              syslog(LOG_INFO, "Field size not known: %d\n", field_size);
473              for (i=0; i<field_size; i++){
474                (*myPtrs->offsetV9Ptr)++;
475              }
476              break;
477            }
478            break;
479          default:
480            syslog(LOG_INFO, "Operator not known: %d\n",
481                   (int)(((RuleDefPtr)(*(myPtrs->rulesAddressPtr+pos)))->operator));
482            break;
483          }
484          j++;
485          pos = (pftmp->fieldType)*10+j;
486        } /* end while myPtrs->rulesAddressPtr */
487      } else {
488        /*
489         * no rule within this field type, but we must read the value
490         */
491        switch (field_size) {
492        case 1:
493          buffer1 = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
494#ifdef IPV4AGG
495          if ((pftmp->fieldType==9)||(pftmp->fieldType==29)){
496            agCache.maskS = *((unsigned char*)&buffer1);
497          }
498          if ((pftmp->fieldType==13)||(pftmp->fieldType==30)){
499            agCache.maskD = *((unsigned char*)&buffer1);
500          }
501          if (pftmp->fieldType==60){
502            agCache.ipProt = *((unsigned char*)&buffer1);
503          }
504          if (pftmp->fieldType==4){
505            agCache.tProt = *((unsigned char*)&buffer1);
506          }
507          if (pftmp->fieldType==61){
508            agCache.sens = *((unsigned char*)&buffer1);
509          }
510          if (pftmp->fieldType==5){
511            agCache.dscp = *((unsigned char*)&buffer1);
512          }       
513#endif
514#ifdef CRIHAN
515          if ((pftmp->fieldType==9)||(pftmp->fieldType==29)){
516            ipFirstCache.maskS = *((unsigned char*)&buffer1);
517          }
518          if ((pftmp->fieldType==13)||(pftmp->fieldType==30)){
519            ipFirstCache.maskD = *((unsigned char*)&buffer1);
520          }
521          if (pftmp->fieldType==60){
522            ipFirstCache.ipProt = *((unsigned char*)&buffer1);
523            mplsFirstCache.ipProt = *((unsigned char*)&buffer1);
524          }
525          if (pftmp->fieldType==4){
526            ipFirstCache.tProt = *((unsigned char*)&buffer1);
527          }
528#endif
529          break;
530        case 2:
531          buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
532          buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
533#ifdef IPV4AGG
534          if (pftmp->fieldType==10){
535            agCache.idSnmp = *((unsigned short*)&buffer2);
536          }       
537#endif
538#ifdef CRIHAN
539          if (pftmp->fieldType==10){
540            ipFirstCache.inSnmp = *((unsigned short*)&buffer2);
541          }
542          if (pftmp->fieldType==14){
543            ipFirstCache.outSnmp = *((unsigned short*)&buffer2);
544          }
545          if (pftmp->fieldType==7){
546            ipFirstCache.sPort = *((unsigned short*)&buffer2);
547          }
548          if (pftmp->fieldType==11){
549            ipFirstCache.dPort = *((unsigned short*)&buffer2);
550          }
551#endif
552          break;
553        case 3:
554          buffer4[3]= 0;
555          buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
556          buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
557          buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
558          /* aggregation */
559          /* end aggregation */
560          if (pftmp->fieldType==70){
561            isMplsFlow = 1;
562          }
563#ifdef CRIHAN
564          if (pftmp->fieldType==70){
565            mplsFirstCache.mplsLabel1 = (*((unsigned long*)&buffer4))>>4;
566          }
567#endif
568          break;
569        case 4:
570          buffer4[3]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
571          buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
572          buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
573          buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
574#ifdef IPV4AGG
575          if ((pftmp->fieldType==8)){
576            bool = 1;
577            agCache.v4AdS = *((unsigned long*)&buffer4);
578          } else if ((pftmp->fieldType==12)){
579            agCache.v4AdD = *((unsigned long*)&buffer4);
580          } else if (pftmp->fieldType==1){
581            agCache.bytes = *((unsigned long*)&buffer4);
582          } else if (pftmp->fieldType==2){
583            agCache.pkts = *((unsigned long*)&buffer4);
584          }
585#endif
586#ifdef CRIHAN
587          if (pftmp->fieldType==8){
588            ipFirstCache.v4AdS = *((unsigned long*)&buffer4);
589            mplsFirstCache.v4AdS = *((unsigned long*)&buffer4);
590          }
591          if (pftmp->fieldType==12){
592            ipFirstCache.v4AdD = *((unsigned long*)&buffer4);
593            mplsFirstCache.v4AdD = *((unsigned long*)&buffer4);
594          }
595          if (pftmp->fieldType==1){
596            ipFirstCache.bytes = *((unsigned long*)&buffer4);
597          }
598          if (pftmp->fieldType==2){
599            ipFirstCache.pkts = *((unsigned long*)&buffer4);
600          }
601          if (pftmp->fieldType==22){
602            firstTime = *((unsigned long*)&buffer4);
603          }
604          if (pftmp->fieldType==21){
605            lastTime = *((unsigned long*)&buffer4);
606          }
607#endif
608          break;
609        case 16:
610          for (i=0; i<4; i++) {
611            buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
612            buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
613            buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
614            buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
615          }
616          /* aggregation IPv6 */         
617          /* end aggregation */
618          break;
619        default:
620          syslog(LOG_INFO,"UNKNOWN FIELDS LENGTH: %d ", field_size);
621          for (i=0; i<field_size; i++){
622            (*myPtrs->offsetV9Ptr)++;
623          }
624        }
625      } /* end if one cache table line existence */
626      if (cpt==tmp->fieldCount) {
627        /*
628         * end of one flow (not the flowset)
629         */
630#ifdef CRIHAN
631        if ( mplsFirstCache.mplsLabel1 == 0 ) {
632          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->ipProt = ipFirstCache.ipProt;
633          ipFirstCache.ipProt = 0;
634          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->bytes = ipFirstCache.bytes;
635          ipFirstCache.bytes = 0;
636          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->pkts = ipFirstCache.pkts;
637          ipFirstCache.pkts = 0;
638          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->inSnmp = ipFirstCache.inSnmp;
639          ipFirstCache.inSnmp = 0;
640          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->outSnmp = ipFirstCache.outSnmp;
641          ipFirstCache.outSnmp = 0;
642          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->v4AdS = ipFirstCache.v4AdS;
643          ipFirstCache.outSnmp = 0;
644          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->v4AdD = ipFirstCache.v4AdD;
645          ipFirstCache.v4AdD = 0;
646          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->tProt = ipFirstCache.tProt;
647          ipFirstCache.tProt = 0;
648          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->sPort = ipFirstCache.sPort;
649          ipFirstCache.sPort = 0;
650          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->dPort = ipFirstCache.dPort;
651          ipFirstCache.dPort = 0;
652          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->maskD = ipFirstCache.maskD;
653          ipFirstCache.maskD = 0;
654          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->maskS = ipFirstCache.maskS;
655          ipFirstCache.maskS = 0;
656          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->routerAd = ipFirstCache.routerAd;
657          (myPtrs->tabIPPtr+(*myPtrs->ipNbPtr))->liveTime = lastTime - firstTime;
658          firstTime = 0; lastTime = 0;
659          (*myPtrs->ipNbPtr)++;
660        } else {
661          (myPtrs->tabMPLSPtr+(*myPtrs->mplsNbPtr))->ipProt = mplsFirstCache.ipProt;
662          mplsFirstCache.ipProt = 0;
663          (myPtrs->tabMPLSPtr+(*myPtrs->mplsNbPtr))->v4AdS =  mplsFirstCache.v4AdS;
664          mplsFirstCache.v4AdS = 0;
665          (myPtrs->tabMPLSPtr+(*myPtrs->mplsNbPtr))->v4AdD =  mplsFirstCache.v4AdD;
666          mplsFirstCache.v4AdD = 0;
667          (myPtrs->tabMPLSPtr+(*myPtrs->mplsNbPtr))->routerAd =  mplsFirstCache.routerAd;
668          (myPtrs->tabMPLSPtr+(*myPtrs->mplsNbPtr))->mplsLabel1 =  mplsFirstCache.mplsLabel1;
669          mplsFirstCache.mplsLabel1 = 0;
670          (*myPtrs->mplsNbPtr)++;
671        }
672#endif 
673        /* put aggregation cache information to IPv4 Prefixes table */
674        /* Aggregation mode must be enable in ./configure options   */
675        /* first we must know if the address belong to our prefix   */
676#ifdef IPV4AGG
677        if ( (bool == 1) && (isMplsFlow == 0) ){
678          prefixKey.beginning = agCache.v4AdS>>(32-agCache.maskS)<<(32-agCache.maskS);
679          res = bsearch(&prefixKey, V4PTab, nbPV4,
680                        sizeof(struct PrefixV4), prefGlobalCmp);
681          if (res!=NULL){
682            if ( myPtrs->routersID[res->routerNb] == agCache.routerAd  ) {
683              /* OUT ("traffic from the prefix/subnet")  */
684              res->bytesNbOUT += agCache.bytes;
685              res->pktsNbOUT += agCache.pkts;
686              (res->flowNbOUT)++;
687            } else {
688              /* doublon or bad reference in prefix list */
689            }
690          } else {
691            prefixKey.beginning = agCache.v4AdD>>(32-agCache.maskD)<<(32-agCache.maskD);
692            res = bsearch(&prefixKey, V4PTab, nbPV4,
693                          sizeof(struct PrefixV4), prefGlobalCmp);
694            if (res!=NULL){
695              if ( myPtrs->routersID[res->routerNb] == agCache.routerAd  ) {
696                /* IN ("traffic to the prefix")  */
697                res->bytesNbIN += agCache.bytes;
698                res->pktsNbIN += agCache.pkts;
699                (res->flowNbIN)++;
700              } else {
701                /* doublon or bad reference in prefix list */
702               
703/*              fprintf(stderr, "IN NC: %lu.%lu.%lu.%lu/%hu -> %lu.%lu.%lu.%lu/%hu R:%lu.%lu.%lu.%lu \n", */
704/*                      (agCache.v4AdS>>24), */
705/*                      (agCache.v4AdS<<8>>24),  */
706/*                      (agCache.v4AdS<<16>>24),  */
707/*                      (agCache.v4AdS<<24>>24),  */
708/*                      (agCache.maskS),  */
709/*                      (agCache.v4AdD>>24),  */
710/*                      (agCache.v4AdD<<8>>24),  */
711/*                      (agCache.v4AdD<<16>>24),  */
712/*                      (agCache.v4AdD<<24>>24),  */
713/*                      (agCache.maskD),  */
714/*                      (myPtrs->pcktPtr->ipH->srcAdd>>24),  */
715/*                      (myPtrs->pcktPtr->ipH->srcAdd<<8>>24), */
716/*                      (myPtrs->pcktPtr->ipH->srcAdd<<16>>24), */
717/*                      (myPtrs->pcktPtr->ipH->srcAdd<<24>>24) */
718/*                      ); */
719              }
720            } else {
721              /* HERE : we are in the cases of the address/mask don't      */
722              /*        belong to a prefix listed in the IPv4 prefix file  */
723              /* possibilities :                                           */
724              /*     -  prefix not referenced but allowed to be routed     */
725              /*     -  prefix not referenced but not allowed to be routed */
726              /*     -  spoofing                                           */
727#ifdef PRINTUNKNOWNSUBNET 
728              fprintf(stderr, "%lu.%lu.%lu.%lu/%hu -> %lu.%lu.%lu.%lu/%hu (R:%lu.%lu.%lu.%lu) \n",
729                      (agCache.v4AdS>>24),
730                      (agCache.v4AdS<<8>>24),
731                      (agCache.v4AdS<<16>>24),
732                      (agCache.v4AdS<<24>>24),
733                      (agCache.maskS),
734                      (agCache.v4AdD>>24),
735                      (agCache.v4AdD<<8>>24),
736                      (agCache.v4AdD<<16>>24),
737                      (agCache.v4AdD<<24>>24),
738                      (agCache.maskD),
739                      (myPtrs->pcktPtr->ipH->srcAdd>>24),
740                      (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
741                      (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
742                      (myPtrs->pcktPtr->ipH->srcAdd<<24>>24)
743                      );
744#endif
745            }
746          }
747        }
748        bool = 0;
749        isMplsFlow = 0;
750#endif
751        /*
752         * Redirection if needed
753         * switch the rules definition (check & fieldToRecord),
754         * we send the flow or a part of the flow to a remote host or a file.
755         * In a first time (until release 0.0.7, the information was sent via
756         * the process renetcolSender with an IPC message queue. But, the perf
757         * of IPC Msg was very bad with a system compilation by default.
758         * So now, we send information directly from here.
759         */
760        tmpRuleList = myPtrs->rulesListPtr;
761        while (tmpRuleList){
762/*        RulesPtr tmpRuleList_for_to = myPtrs->rulesListPtr; */
763          RuleDefPtr tmpRuleDefList = tmpRuleList->def;
764          unsigned short check = 1;
765/*        int s=0; */
766          secondOffset = secondOldOffset;
767          while (tmpRuleDefList){
768            check = check && tmpRuleDefList->check;
769            tmpRuleDefList->check = 0;
770            tmpRuleDefList = tmpRuleDefList->next;
771          }
772          if ( (tmpRuleList->def != NULL) && (check == 1)) {
773            /* msg building */
774            secondPftmp = tmp->lastField;
775            msgTextIndex = mempcpy(mempcpy(mempcpy(myMsg.text,
776                                                   &tplMsgType,
777                                                   sizeof(unsigned short)
778                                                   ),
779                                           &tmpRuleList->id,
780                                           sizeof(tmpRuleList->id)
781                                           ),
782                                   &myPtrs->currentRouterPtr->IpAddress,
783                                   sizeof(unsigned long)
784                                   );
785            msgTextIndex = mempcpy(mempcpy(mempcpy(msgTextIndex,
786                                                   &tmp->sourceId,
787                                                   sizeof(unsigned long)
788                                                   ),
789                                           &tmp->templateFlowSetId,
790                                           sizeof(tmp->templateFlowSetId)
791                                           ),
792                                   myPtrs->ptr_buffer+secondOffset,
793                                   flow_size
794                                   );
795            myMsg.type = 1;
796            /* OLD transfert type */
797            msgSend( myQueue, myMsg);
798            /* NEW transfert type */
799/*          for ( ; tmpRuleList_for_to; tmpRuleList_for_to=tmpRuleList_for_to->next){ */
800/*            fprintf(stderr, "?\n"); */
801/*            if (tmpRuleList_for_to->id == tmpRuleList->id){ */
802/*              fprintf(stderr, "y\n"); */
803/*              s = sendMessage(tmpRuleList_for_to->host->sockId, &(myMsg.text), sizeof(myMsg.text), */
804/*                              tmpRuleList_for_to->host->hostAddressPtr); */
805/*              fprintf(stderr, "o\n"); */
806/*            } */
807/*          } */
808            /* FIXME : perhaps check "s" */
809            noEnd = 1;
810            secondCpt=0;
811          } /* end if check */
812          tmpRuleList = tmpRuleList->next;
813          secondPftmp = tmp->lastField;
814        } /* end while tmpRuleList */
815        /*
816         * end redirection
817         */
818        secondOffset = *myPtrs->offsetV9Ptr;
819        secondOldOffset = secondOffset;
820        pftmp = tmp->lastField;
821        cpt=0;
822        (*myPtrs->currentFlowsetNumberPtr)++; /* pointer on the flows number */
823        if (((*myPtrs->offsetV9Ptr)-48-shift+flow_size) > data_length){
824          overflow = 1; /* buffer index not correct */
825        }
826      } else {
827        /* not the flow end, progress in field list */
828        pftmp = pftmp->prev;
829      }
830    } /* end of the while on one flow record */
831   
832    while ( ((*myPtrs->offsetV9Ptr)-48-shift) < data_length ) {
833      (*myPtrs->offsetV9Ptr)++; /* if padding */
834      paddingCounter++;
835      if ( paddingCounter > 8 ) {
836#ifdef DEBUG
837        fprintf(stderr," padding too high: %d ", paddingCounter);
838#endif
839        syslog(LOG_INFO,"padding too high: %d ", paddingCounter);
840        return (-1);
841      }
842    }
843    while ( (*myPtrs->offsetV9Ptr)-48-shift > data_length ) {
844      (*myPtrs->offsetV9Ptr)--; /* crazy loop (when bug appears in template def) */
845      crazyCounter++;
846    }
847#ifdef DEBUG
848    if (crazyCounter != 0){ fprintf(stderr," crazyCounter: %d ", crazyCounter); }
849#endif
850    if (crazyCounter!=0) { syslog(LOG_INFO,"crazyCounter: %d ", crazyCounter);}
851   
852#ifdef DEBUG
853    fprintf(stderr,"}");
854#endif
855
856    return (data_length+shift);
857    /* end new place */
858   
859  }else{
860    /*
861     *
862     *
863     *       Optional template
864     *
865     *
866     */
867    if ((tmpOpt=existTplOptId(myPtrs->currentRouterPtr, myPtrs->currentHeaderV9Ptr->sourceId,
868                              (*myPtrs->currentFlowsetIdPtr)))!=NULL) {       
869#ifdef DEBUG
870      fprintf(stderr,"<d id: %hu, lg %hu", (*myPtrs->currentFlowsetIdPtr), data_length);
871#endif     
872      for ( i=0; i<(tmpOpt->optionScopeLg/4); i++){
873        /* FIXME : today we skip the scope fields, it's bad :( */ 
874        if ((pftmp=tmpOpt->lastField)!=NULL) {
875          for (j=0; j<pftmp->fieldLength; j++) {
876            (*myPtrs->offsetV9Ptr)++;
877          }
878          pftmp = pftmp->prev;
879        }
880      }
881      while (pftmp != NULL) {
882        if (pftmp->fieldLength==1){
883          buffer1 = *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
884        }
885        if (pftmp->fieldLength==2){
886          buffer2[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
887          buffer2[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
888        }
889        if (pftmp->fieldLength==4){
890          buffer4[3]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
891          buffer4[2]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
892          buffer4[1]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
893          buffer4[0]= *(myPtrs->ptr_buffer+(*myPtrs->offsetV9Ptr)); (*myPtrs->offsetV9Ptr)++;
894          if (pftmp->fieldType==34){
895            if ((*((unsigned long*)&buffer4)==1)
896                || (*((unsigned long*)&buffer4)==10)
897                || (*((unsigned long*)&buffer4)==1000)) {
898              if (myPtrs->currentRouterPtr->sampled != *((unsigned long*)&buffer4) ) {
899#ifdef DEBUGAGG
900                fprintf(stderr,"\n SV (p) %lu != %lu (r)", *((unsigned long*)&buffer4),
901                        myPtrs->currentRouterPtr->sampled);
902#endif                     
903                myPtrs->currentRouterPtr->sampled = *((unsigned long*)&buffer4);
904                for (i=0; i<nbPV4; i++) {
905                  /* we update the PrefixV4 structure with the sampled information */
906                  if ( myPtrs->currentRouterPtr->IpAddress == myPtrs->routersID[V4PTab[i].routerNb]) {
907                    V4PTab[i].sampling = *((unsigned long*)&buffer4);
908                    myPtrs->secondV4Tab[i].sampling = *((unsigned long*)&buffer4);
909                  }
910                }
911              }
912            }
913          }
914        }       
915        pftmp = pftmp->prev;
916      }
917      if ( pftmp == NULL ) {
918        while ((*myPtrs->offsetV9Ptr)%4 != 0) {
919          (*myPtrs->offsetV9Ptr)++;
920#ifdef DEBUG
921          fprintf(stderr," p ");
922#endif
923        }
924      } else {
925        fprintf(stderr,"PB ");
926      }
927#ifdef DEBUG
928      fprintf(stderr,">");
929#endif
930      return (data_length+shift);
931    } else {
932      /*
933       * template unknown, we skip this all the data
934       */
935      (*myPtrs->offsetV9Ptr)+=(data_length-4);
936      (*myPtrs->currentFlowsetNumberPtr) = myPtrs->currentHeaderV9Ptr->count;
937#ifdef DEBUG
938      fprintf(stderr, " U, r: %lu.%lu.%lu.%lu, sourceId: %lu, TID: %hu",
939              (myPtrs->pcktPtr->ipH->srcAdd>>24),
940              (myPtrs->pcktPtr->ipH->srcAdd<<8>>24),
941              (myPtrs->pcktPtr->ipH->srcAdd<<16>>24),
942              (myPtrs->pcktPtr->ipH->srcAdd<<24>>24),
943              myPtrs->currentHeaderV9Ptr->sourceId,
944              (*myPtrs->currentFlowsetIdPtr)
945              );
946#endif
947      return (data_length+shift);
948    }
949  }
950}
Note: See TracBrowser for help on using the browser.