root/trunk/src/dataFlowSet.c @ 60

Revision 60, 32.9 KB (checked in by andreu, 15 years ago)

new aggregation mode based on SNMP index and AS aggregation

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