root/trunk/src/template.c @ 34

Revision 34, 16.6 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: template.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 "template.h"
27
28/*
29 * getFlowsetId
30 *
31 * return the flowset id
32 */
33short 
34getFlowsetId(short cfn, unsigned short *offV9, unsigned char *buf)
35{
36  unsigned short os;
37  unsigned char buffer2[2];
38 
39  if ( cfn == 0 ) {
40    os = 20+8+20;
41  } else {
42    os = *offV9;
43  }
44  buffer2[1]= *(buf+os+0);
45  buffer2[0]= *(buf+os+1);
46  return *((unsigned short*)&buffer2);
47}
48
49/*
50 * checkTemplateFlowSet
51 *
52 * check the fields of a template flowset and build the Template Flowset List
53 * it can exist many templates definitions in a template flowset
54 */
55short 
56checkTemplateFlowSet(RouterPtr cr, unsigned short *offV9,
57                     unsigned char *buf,
58                     NetFlowV9HeaderPtr v9Ptr,
59                     TplFlowSetPtr cTFSPtr,
60                     short *cFNPtr,
61                     int myQueue)
62{
63  int i = 0;
64  int cpt = 0;
65  int cpt_fields = 0;
66  int index = 0;
67  unsigned char buffer2[2];
68  unsigned short tmp = 0;
69  unsigned short flowsetId = 0;
70  unsigned short length = 0;
71  TplFlowSetPtr ptpltmp = NULL;
72  /* for msg sending: */
73  msgType myMsg;
74  FieldPtr tmpField;
75  char *msgTextIndex;
76  unsigned short tplMsgType = 1;
77  unsigned short badFieldNumber = 0;
78  unsigned short badFieldLength = 0;
79  unsigned short badFieldCount = 0;
80
81  buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
82  buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
83  flowsetId = *((unsigned short*)&buffer2);
84  buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
85  buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
86  length = *((unsigned short*)&buffer2);
87  do {
88#ifdef DEBUG   
89    fprintf (stderr, "{");
90#endif
91    badFieldNumber = 0;
92    badFieldLength = 0;
93    badFieldCount = 0;
94    buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
95    buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
96    if ((cTFSPtr=
97         existTplId(cr, v9Ptr->sourceId,
98                    *((unsigned short*)&buffer2)))==NULL) {
99      cTFSPtr = newRouterTplList();
100      cTFSPtr->next = cr->tplList;
101      if (cr->tplList!=NULL) {cr->tplList->prev = cTFSPtr;}
102      cr->tplList = cTFSPtr;
103      cTFSPtr->templateFlowSetId = *((unsigned short*)&buffer2);
104      cTFSPtr->sourceId = v9Ptr->sourceId;
105      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
106      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
107      if ( (cTFSPtr->fieldCount = *((unsigned short*)&buffer2) ) == 0 ) {
108        badFieldCount = 1;
109        ptpltmp = cTFSPtr;
110        if (ptpltmp->prev == NULL) {
111          cr->tplList = ptpltmp->next;
112          if (cr->tplList) {
113            cr->tplList->prev = NULL;
114          }
115        } else {
116          ptpltmp->prev->next = ptpltmp->next;
117          if (ptpltmp->next) {
118            ptpltmp->next->prev = ptpltmp->prev;
119          }
120        }
121        deleteTplFlSet(ptpltmp);
122        cTFSPtr = NULL;
123        return (-1);
124      }
125      cpt_fields = 0;
126      for ( i=0; i<cTFSPtr->fieldCount; i++) {
127        FieldPtr fldPtr = (FieldPtr) malloc(sizeof(struct Field));
128        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
129        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
130        if ( (fldPtr->fieldType = *((unsigned short*)&buffer2) ) > 89) {
131          badFieldNumber = 1;
132        }
133        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
134        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
135        if ( (fldPtr->fieldLength = *((unsigned short*)&buffer2) ) > 16) {
136          badFieldLength = 1;
137        }
138        fldPtr->next = cTFSPtr->fieldSet;
139        fldPtr->prev = NULL;
140        if ( cTFSPtr->fieldSet ) {cTFSPtr->fieldSet->prev = fldPtr;}
141        cTFSPtr->fieldSet = fldPtr;
142        if (i==0) {
143          cTFSPtr->lastField = fldPtr;
144        }
145        cpt_fields++;
146        if ( (cTFSPtr->templateFlowSetId < 256 )
147             || (cpt_fields > 40)
148             || (badFieldNumber == 1)
149             || badFieldLength == 1
150             || (badFieldCount == 1)) {
151/*        fprintf(stderr,"\n IP: %lu.%lu.%lu.%lu, %d,%d\n", */
152/*                cr->IpAddress>>24, */
153/*                cr->IpAddress<<8>>24, */
154/*                cr->IpAddress<<16>>24, */
155/*                cr->IpAddress<<24>>24, */
156/*                badFieldNumber, */
157/*                badFieldLength */
158/*                ); */
159/*        fprintf (stderr, "\n cTFSPtr->templateFlowSetId: %hu\n",  */
160/*                 cTFSPtr->templateFlowSetId); */
161          while ( ((*offV9))> (index)){
162            buffer2[1]= *(buf+index); index++;
163            buffer2[0]= *(buf+index); index++;
164            tmp = *((unsigned short*)&buffer2);
165/*          fprintf (stderr, " %hu ", tmp); */
166          }
167          syslog (LOG_INFO,
168                  "in new template: field nb gt 89, field lg gt 16 or too many fields, bad template definition possibility");
169/*        fprintf(stderr,  */
170/*                "\n in new template function: too many fields, bug collector or wrong template def \n");  */
171          ptpltmp = cTFSPtr;
172          if (ptpltmp->prev == NULL) {
173            cr->tplList = ptpltmp->next;
174            if (cr->tplList) {
175              cr->tplList->prev = NULL;
176            }
177          } else {
178            ptpltmp->prev->next = ptpltmp->next;
179            if (ptpltmp->next) {
180              ptpltmp->next->prev = ptpltmp->prev;
181            }
182          }
183          deleteTplFlSet(ptpltmp);
184          cTFSPtr = NULL;
185          return (-1);
186        }
187      }
188    } else { /*update*/
189      ptpltmp = cTFSPtr;
190      if (ptpltmp->prev == NULL) {
191        cr->tplList = ptpltmp->next;
192        if (cr->tplList) {
193          cr->tplList->prev = NULL;
194        }
195      } else {
196        ptpltmp->prev->next = ptpltmp->next;
197        if (ptpltmp->next) {
198          ptpltmp->next->prev = ptpltmp->prev;
199        }
200      }
201      deleteTplFlSet(ptpltmp);
202      ptpltmp = NULL;
203      cTFSPtr = NULL;
204      cTFSPtr = newRouterTplList();
205      cTFSPtr->next = cr->tplList;
206      if (cr->tplList!=NULL) {cr->tplList->prev = cTFSPtr;}
207      cr->tplList = cTFSPtr;
208      cTFSPtr->templateFlowSetId = *((unsigned short*)&buffer2);
209      cTFSPtr->sourceId = v9Ptr->sourceId;
210      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
211      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
212      if ( (cTFSPtr->fieldCount = *((unsigned short*)&buffer2) ) == 0 ) {
213        badFieldCount = 1;
214        ptpltmp = cTFSPtr;
215        if (ptpltmp->prev == NULL) {
216          cr->tplList = ptpltmp->next;
217          if (cr->tplList) {
218            cr->tplList->prev = NULL;
219          }
220        } else {
221          ptpltmp->prev->next = ptpltmp->next;
222          if (ptpltmp->next) {
223            ptpltmp->next->prev = ptpltmp->prev;
224          }
225        }
226        deleteTplFlSet(ptpltmp);
227        cTFSPtr = NULL;
228        return (-1);
229      }
230      cpt_fields = 0;
231      for ( i=0; i<cTFSPtr->fieldCount; i++) {
232        FieldPtr fldPtr = (FieldPtr) malloc(sizeof(struct Field));
233        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
234        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
235        if ( (fldPtr->fieldType = *((unsigned short*)&buffer2) ) > 89) {
236          badFieldNumber = 1;
237        }
238        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
239        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
240        if ( (fldPtr->fieldLength = *((unsigned short*)&buffer2) ) > 16) {
241          badFieldLength = 1;
242        }
243        fldPtr->next = cTFSPtr->fieldSet;
244        fldPtr->prev = NULL;
245        if ( cTFSPtr->fieldSet ) {
246          cTFSPtr->fieldSet->prev = fldPtr;
247        }
248        cTFSPtr->fieldSet = fldPtr;
249        if (i==0) {
250          cTFSPtr->lastField = fldPtr;
251        }
252        cpt_fields++;
253        if ( (cTFSPtr->templateFlowSetId < 256 )
254             || (cpt_fields > 40)
255             || (badFieldNumber == 1)
256             || badFieldLength == 1
257             || (badFieldCount == 1)) {
258          while ( ((*offV9))> (index)){
259            buffer2[1]= *(buf+index); index++;
260            buffer2[0]= *(buf+index); index++;
261            tmp = *((unsigned short*)&buffer2);
262/*          fprintf (stderr, " %hu ", tmp); */
263          }
264          syslog (LOG_INFO,
265                  "in update template function: too many fields, bug collector or wrong template def in template update");
266/*        fprintf(stderr, */
267/*                "\nin update template function: too many fields, bug collector or wrong template def in template update\n"); */
268          ptpltmp = cTFSPtr;
269          if (ptpltmp->prev == NULL) {
270            cr->tplList = ptpltmp->next;
271            if (cr->tplList) {
272              cr->tplList->prev = NULL;
273            }
274          } else {
275            ptpltmp->prev->next = ptpltmp->next;
276            if (ptpltmp->next) {
277              ptpltmp->next->prev = ptpltmp->prev;
278            }
279          }
280          deleteTplFlSet(ptpltmp);
281          cTFSPtr = NULL;
282          return (-1);   
283        }
284      }
285    } /* end if template creation or update */
286    /* send to queue the template definition */
287/*     fprintf(stderr,"%hu %d\n", tplMsgType, sizeof(tplMsgType)); */
288/*     fprintf(stderr,"%lu %d\n", cr->IpAddress, sizeof(cr->IpAddress)); */
289/*     fprintf(stderr, "%lu.%lu.%lu.%lu ", */
290/*          ( cr->IpAddress)>>24, */
291/*          ( cr->IpAddress)<<8>>24, */
292/*          ( cr->IpAddress)<<16>>24, */
293/*          ( cr->IpAddress)<<24>>24); */
294/*     fprintf(stderr,"%lu %d\n", cTFSPtr->sourceId, sizeof(cTFSPtr->sourceId)); */
295    /* FIXME HERE don't send an wrong def if error detected */
296    msgTextIndex = mempcpy(mempcpy(mempcpy(mempcpy(mempcpy(myMsg.text,
297                                                           &tplMsgType,
298                                                           sizeof(unsigned short)
299                                                           ),
300                                                   &cr->IpAddress,
301                                                   sizeof(unsigned long)
302                                                   ),
303                                           &cTFSPtr->sourceId,
304                                           sizeof(unsigned long)
305                                           ),
306                                   &cTFSPtr->templateFlowSetId,
307                                   sizeof(cTFSPtr->templateFlowSetId)
308                                   ),
309                           &cTFSPtr->fieldCount,
310                           sizeof(cTFSPtr->fieldCount)
311                           );
312    /* add the fields list */
313    tmpField = cTFSPtr->lastField;
314    for (; tmpField; tmpField=tmpField->prev) {
315      msgTextIndex = mempcpy(mempcpy(msgTextIndex,
316                                     &tmpField->fieldType,
317                                     sizeof(tmpField->fieldType)),
318                             &tmpField->fieldLength,
319                             sizeof(tmpField->fieldLength));
320    }
321    myMsg.type = 1;
322    msgSend( myQueue, myMsg);
323    /* end send */
324    (*cFNPtr)++;   
325  } while ( (cpt) < (length-2) ); /*length-2*/
326#ifdef DEBUG
327  fprintf (stderr, "}");
328#endif
329  return length;
330}
331
332/*
333 * checkTemplateOption
334 *
335 */
336short 
337checkTemplateOption(RouterPtr cr, unsigned short *offV9,
338                    unsigned char *buf, NetFlowV9HeaderPtr v9Ptr,
339                    TplOptionPtr cTOPtr,
340                    short *cFNPtr)
341{
342  int i;
343  int cpt = 0;
344  unsigned short getFlowsetId = 0;
345  unsigned short length = 0;
346  unsigned char buffer2[2];
347  TplOptionPtr ptplotmp;
348  unsigned short badFieldNumber = 0;
349  unsigned short badFieldLength = 0;
350  int cpt_fields = 0;
351
352  buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
353  buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
354  getFlowsetId = *((unsigned short*)&buffer2);
355  buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
356  buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
357  length = *((unsigned short*)&buffer2);
358  do {
359#ifdef DEBUG   
360    fprintf (stderr, "<");
361#endif
362    badFieldNumber = 0;
363    badFieldLength = 0;
364    buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
365    buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
366    if ((cTOPtr=
367         existTplOptId(cr, v9Ptr->sourceId,
368                       *((unsigned short*)&buffer2)))==NULL) {
369      cTOPtr = newRouterTplOptList();
370      cTOPtr->next = cr->tplOptList;
371      if (cr->tplOptList!=NULL) {cr->tplOptList->prev = cTOPtr;}
372      cr->tplOptList = cTOPtr;
373      cTOPtr->templateOptionId = *((unsigned short*)&buffer2);
374      cTOPtr->sourceId = v9Ptr->sourceId;
375      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
376      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
377      cTOPtr->optionScopeLg = *((unsigned short*)&buffer2);
378      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
379      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
380      cTOPtr->optionLg = *((unsigned short*)&buffer2);
381      cpt_fields = 0;
382      for ( i=0; i<(((cTOPtr->optionScopeLg)+(cTOPtr->optionLg))/4); i++) {
383        FieldPtr tmp = (FieldPtr) malloc(sizeof(struct Field));
384        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
385        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
386        if ( (tmp->fieldType = *((unsigned short*)&buffer2) ) > 89) {
387          badFieldNumber = 1;
388        }
389        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
390        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
391        if ( (tmp->fieldLength = *((unsigned short*)&buffer2) ) > 16) {
392          badFieldLength = 1;
393        }
394        tmp->next = cTOPtr->fieldSet;
395        tmp->prev = NULL;
396        if ( cTOPtr->fieldSet ) {cTOPtr->fieldSet->prev = tmp;}
397        cTOPtr->fieldSet = tmp;
398        if (i==0) {
399          cTOPtr->lastField = tmp;
400        }
401        cpt_fields++;
402        if ( (cTOPtr->templateOptionId < 256) || (cpt_fields > 40) || (badFieldNumber == 1) || badFieldLength == 1) {
403          syslog (LOG_INFO,
404                  "in option template function: too many fields, bug collector or wrong template def in template update");
405#ifdef DEBUG
406          fprintf(stderr,
407                  "\nin option template function: too many fields, bug collector or wrong template def in template update\n");
408#endif
409          ptplotmp = cTOPtr;
410          if (ptplotmp->prev == NULL) {
411            cr->tplOptList = ptplotmp->next;
412            if (cr->tplOptList) {
413              cr->tplOptList->prev = NULL;
414            }
415          } else {
416            ptplotmp->prev->next = ptplotmp->next;
417            if (ptplotmp->next) {
418              ptplotmp->next->prev = ptplotmp->prev;
419            }
420          }
421          deleteTplOption(ptplotmp);
422          cTOPtr = NULL;
423          return (-1);
424        }
425      }
426      while ((*offV9)%4 != 0) {
427        (*offV9)++; cpt++;
428      } /*padding case*/
429    } else { /*update */
430      ptplotmp = cTOPtr;
431      if (ptplotmp->prev == NULL) {
432        cr->tplOptList = ptplotmp->next;
433        if (cr->tplOptList) {
434          cr->tplOptList->prev = NULL;
435        }
436      } else {
437        ptplotmp->prev->next = ptplotmp->next;
438        if (ptplotmp->next) {
439          ptplotmp->next->prev = ptplotmp->prev;
440        }
441      }
442      deleteTplOption(ptplotmp);
443      cTOPtr = NULL;
444      cTOPtr = newRouterTplOptList();
445      cTOPtr->next = cr->tplOptList;
446      if (cr->tplOptList!=NULL) {cr->tplOptList->prev = cTOPtr;}
447      cr->tplOptList = cTOPtr;
448      cTOPtr->templateOptionId = *((unsigned short*)&buffer2);
449      cTOPtr->sourceId = v9Ptr->sourceId;
450      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
451      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
452      cTOPtr->optionScopeLg = *((unsigned short*)&buffer2);
453      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
454      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
455      cTOPtr->optionLg = *((unsigned short*)&buffer2);
456      cpt_fields = 0;
457      for ( i=0; i<(((cTOPtr->optionScopeLg)+(cTOPtr->optionLg))/4); i++) {
458        FieldPtr tmp = (FieldPtr) malloc(sizeof(struct Field));
459        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
460        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
461        if ( (tmp->fieldType = *((unsigned short*)&buffer2) ) > 89) {
462          badFieldNumber = 1;
463        }
464        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
465        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
466        if ( (tmp->fieldLength = *((unsigned short*)&buffer2) ) > 16) {
467          badFieldLength = 1;
468        }
469        tmp->next = cTOPtr->fieldSet;
470        tmp->prev = NULL;
471        if ( cTOPtr->fieldSet ) {cTOPtr->fieldSet->prev = tmp;}
472        cTOPtr->fieldSet = tmp;
473        if (i==0) {
474          cTOPtr->lastField = tmp;
475        }
476        cpt_fields++;
477        if ( (cTOPtr->templateOptionId < 256) || (cpt_fields > 40) || (badFieldNumber == 1) || badFieldLength == 1) {
478          syslog (LOG_INFO,
479                  "in update option tpl function: too many fields, bug collector or wrong template def in template update");
480#ifdef DEBUG
481          fprintf(stderr,
482                  "\nin update option tpl function: too many fields, bug collector or wrong template def in template update\n");
483#endif
484          ptplotmp = cTOPtr;
485          if (ptplotmp->prev == NULL) {
486            cr->tplOptList = ptplotmp->next;
487            if (cr->tplOptList) {
488              cr->tplOptList->prev = NULL;
489            }
490          } else {
491            ptplotmp->prev->next = ptplotmp->next;
492            if (ptplotmp->next) {
493              ptplotmp->next->prev = ptplotmp->prev;
494            }
495          }
496          deleteTplOption(ptplotmp);
497          cTOPtr = NULL;
498          return (-1);
499        }
500      }
501      while ((*offV9)%4 != 0) {
502        (*offV9)++; cpt++;
503      } /*padding case*/
504    }
505    (*cFNPtr)++;
506  } while ((cpt) < (length-2));
507/*   show_all_tplFlSet(); */
508#ifdef DEBUG
509  fprintf (stderr, ">");
510#endif
511  return length;
512}
513
514
515/*
516 * exist_id
517 *
518 * used in check a template flowset
519 * the id is the source_id(idh) plus getFlowsetId
520 * return:
521 *    - a pointer on the template flowset or NULL
522 */
523TplFlowSetPtr
524existId(RouterPtr cr, unsigned long idh, short idfs)
525{
526  TplFlowSetPtr tmp=cr->tplList;
527  for (; tmp; tmp=tmp->next) {
528    if ((tmp->sourceId==idh)) {
529      return tmp;
530    }
531  }
532  return NULL;
533}
534
535/*
536 * existTplId
537 *
538 * return:
539 *    - a pointer on the correct template flowset definition
540 */
541TplFlowSetPtr
542existTplId(RouterPtr cr, unsigned long sid, short idfs)
543{
544  TplFlowSetPtr tmp=cr->tplList;
545  for (; tmp; tmp=tmp->next) {
546    if ((tmp->sourceId==sid)&&(tmp->templateFlowSetId == idfs)) {
547      return tmp;
548    }
549  }
550  return NULL;
551}
552
553/*
554 * existTploptId
555 *
556 * return:
557 *    - a pointer on the correct template option definition
558 */
559TplOptionPtr
560existTplOptId(RouterPtr cr, unsigned long sid, short idfs)
561{
562  TplOptionPtr tmp=cr->tplOptList;
563  for (; tmp; tmp=tmp->next) {
564    if ((tmp->sourceId==sid)&&(tmp->templateOptionId == idfs)) {
565      return tmp;
566    }
567  }
568  return NULL;
569}
Note: See TracBrowser for help on using the browser.