root/trunk/src/dataFlowSet.c @ 27

Revision 27, 28.9 KB (checked in by andreu, 16 years ago)

explicite output to find wrong definition bug - memory leak in renetcolAgg find

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