root/trunk/src/template.c @ 16

Revision 16, 12.0 KB (checked in by andreu, 17 years ago)

Friday save

  • Property svn:eol-style set to native
Line 
1/*
2 * File: template.c
3 *
4 * Authors: ANDREU François-Xavier
5 *
6 * Copyright (C) 2005 GIP RENATER
7 */
8
9/*  This file is part of renetcol.
10 *
11 *  renetcol is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  renetcol is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with renetcol; if not, write to the Free Software
23 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26#include "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 */
55unsigned short 
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 getFlowsetId = 0;
70  unsigned short length = 0;
71  TplFlowSetPtr ptpltmp;
72  /* for msg sending: */
73  msgType myMsg;
74  FieldPtr tmpField;
75  char *msgTextIndex;
76  unsigned short tplMsgType = 1;
77
78  buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
79  buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
80  getFlowsetId = *((unsigned short*)&buffer2);
81  buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
82  buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
83  length = *((unsigned short*)&buffer2);
84  do {
85    buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
86    buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
87    if ((cTFSPtr=
88         existTplId(cr, v9Ptr->sourceId,
89                    *((unsigned short*)&buffer2)))==NULL) {
90      cTFSPtr = newRouterTplList();
91      cTFSPtr->next = cr->tplList;
92      if (cr->tplList!=NULL) {cr->tplList->prev = cTFSPtr;}
93      cr->tplList = cTFSPtr;
94      cTFSPtr->templateFlowSetId = *((unsigned short*)&buffer2);
95      cTFSPtr->sourceId = v9Ptr->sourceId;
96      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
97      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
98      cTFSPtr->fieldCount = *((unsigned short*)&buffer2);
99      cpt_fields = 0;
100      for ( i=0; i<cTFSPtr->fieldCount; i++) {
101        FieldPtr fldPtr = (FieldPtr) malloc(sizeof(struct Field));
102        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
103        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
104        fldPtr->fieldType = *((unsigned short*)&buffer2);
105        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
106        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
107        fldPtr->fieldLength = *((unsigned short*)&buffer2);
108        fldPtr->next = cTFSPtr->fieldSet;
109        fldPtr->prev = NULL;
110        if ( cTFSPtr->fieldSet ) {cTFSPtr->fieldSet->prev = fldPtr;}
111        cTFSPtr->fieldSet = fldPtr;
112        if (i==0) {
113          cTFSPtr->lastField = fldPtr;
114        }
115        cpt_fields++;
116        if ( cpt_fields > 40 ) { /* FIXME : declarer une constante pour cela */
117          fprintf(stderr,"IP: %lu \n",cr->IpAddress);
118          fprintf (stderr, "cTFSPtr->templateFlowSetId: %hu\n",
119                   cTFSPtr->templateFlowSetId);
120          while ( ((*offV9))> (index)){
121            buffer2[1]= *(buf+index); index++;
122            buffer2[0]= *(buf+index); index++;
123            tmp = *((unsigned short*)&buffer2);
124            fprintf (stderr, " %hu ", tmp);
125          }
126          syslog (LOG_INFO,
127                  "fields nb too large, bug collector or wrong template def");
128          exit(1);
129        }
130      }
131    } else { /*update*/
132      ptpltmp = cTFSPtr;
133      if (ptpltmp->prev == NULL) {
134        cr->tplList = ptpltmp->next;
135        if (cr->tplList) {
136          cr->tplList->prev = NULL;
137        }
138      } else {
139        ptpltmp->prev->next = ptpltmp->next;
140        if (ptpltmp->next) {
141          ptpltmp->next->prev = ptpltmp->prev;
142        }
143      }
144      deleteTplFlSet(ptpltmp);
145      cTFSPtr = NULL;
146      cTFSPtr = newRouterTplList();
147      cTFSPtr->next = cr->tplList;
148      if (cr->tplList!=NULL) {cr->tplList->prev = cTFSPtr;}
149      cr->tplList = cTFSPtr;
150      cTFSPtr->templateFlowSetId = *((unsigned short*)&buffer2);
151      cTFSPtr->sourceId = v9Ptr->sourceId;
152      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
153      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
154      cTFSPtr->fieldCount = *((unsigned short*)&buffer2);
155      cpt_fields = 0;
156      for ( i=0; i<cTFSPtr->fieldCount; i++) {
157        FieldPtr fldPtr = (FieldPtr) malloc(sizeof(struct Field));
158        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
159        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
160        fldPtr->fieldType = *((unsigned short*)&buffer2);
161        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
162        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
163        fldPtr->fieldLength = *((unsigned short*)&buffer2);
164        fldPtr->next = cTFSPtr->fieldSet;
165        fldPtr->prev = NULL;
166        if ( cTFSPtr->fieldSet ) {
167          cTFSPtr->fieldSet->prev = fldPtr;
168        }
169        cTFSPtr->fieldSet = fldPtr;
170        if (i==0) {
171          cTFSPtr->lastField = fldPtr;
172        }
173        cpt_fields++;
174        if ( cpt_fields > 40 ) { /* FIXME : declarer une constante pour cela */
175          while ( ((*offV9))> (index)){
176            buffer2[1]= *(buf+index); index++;
177            buffer2[0]= *(buf+index); index++;
178            tmp = *((unsigned short*)&buffer2);
179            fprintf (stderr, " %hu ", tmp);
180          }
181          syslog (LOG_INFO,
182                  "fields nb too large, bug collector or wrong template def");
183          exit(1);
184        }
185      }
186    } /* end if template creation or update */
187    /* send to queue the template definition */
188/*     fprintf(stderr,"%hu %d\n", tplMsgType, sizeof(tplMsgType)); */
189/*     fprintf(stderr,"%lu %d\n", cr->IpAddress, sizeof(cr->IpAddress)); */
190/*     fprintf(stderr, "%lu.%lu.%lu.%lu ", */
191/*          ( cr->IpAddress)>>24, */
192/*          ( cr->IpAddress)<<8>>24, */
193/*          ( cr->IpAddress)<<16>>24, */
194/*          ( cr->IpAddress)<<24>>24); */
195/*     fprintf(stderr,"%lu %d\n", cTFSPtr->sourceId, sizeof(cTFSPtr->sourceId)); */
196    msgTextIndex = mempcpy(mempcpy(mempcpy(mempcpy(mempcpy(myMsg.text,
197                                                           &tplMsgType,
198                                                           sizeof(unsigned short)
199                                                           ),
200                                                   &cr->IpAddress,
201                                                   sizeof(unsigned long)
202                                                   ),
203                                           &cTFSPtr->sourceId,
204                                           sizeof(unsigned long)
205                                           ),
206                                   &cTFSPtr->templateFlowSetId,
207                                   sizeof(cTFSPtr->templateFlowSetId)
208                                   ),
209                           &cTFSPtr->fieldCount,
210                           sizeof(cTFSPtr->fieldCount)
211                           );
212    /* add the fields list */
213    tmpField = cTFSPtr->lastField;
214    for (; tmpField; tmpField=tmpField->prev) {
215      msgTextIndex = mempcpy(mempcpy(msgTextIndex,
216                                     &tmpField->fieldType,
217                                     sizeof(tmpField->fieldType)),
218                             &tmpField->fieldLength,
219                             sizeof(tmpField->fieldLength));
220    }
221    myMsg.type = 1;
222    msgSend( myQueue, myMsg);
223    /* end send */
224    (*cFNPtr)++;   
225  } while ( (cpt) < (length-2) ); /*length-2*/
226  return length;
227}
228
229/*
230 * checkTemplateOption
231 *
232 */
233unsigned short 
234checkTemplateOption(RouterPtr cr, unsigned short *offV9,
235                    unsigned char *buf, NetFlowV9HeaderPtr v9Ptr,
236                    TplOptionPtr cTOPtr,
237                    short *cFNPtr)
238{
239  int i;
240  static int cpt = 0;
241  static unsigned short getFlowsetId = 0;
242  static unsigned short length = 0;
243  unsigned char buffer2[2];
244  TplOptionPtr ptplotmp;
245
246  buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
247  buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
248  getFlowsetId = *((unsigned short*)&buffer2);
249  buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
250  buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
251  length = *((unsigned short*)&buffer2);
252  do {
253    buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
254    buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
255    if ((cTOPtr=
256         existTplOptId(cr, v9Ptr->sourceId,
257                       *((unsigned short*)&buffer2)))==NULL) {
258      cTOPtr = newRouterTplOptList();
259      cTOPtr->next = cr->tplOptList;
260      if (cr->tplOptList!=NULL) {cr->tplOptList->prev = cTOPtr;}
261      cr->tplOptList = cTOPtr;
262      cTOPtr->templateOptionId = *((unsigned short*)&buffer2);
263      cTOPtr->sourceId = v9Ptr->sourceId;
264      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
265      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
266      cTOPtr->optionScopeLg = *((unsigned short*)&buffer2);
267      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
268      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
269      cTOPtr->optionLg = *((unsigned short*)&buffer2);
270      for ( i=0; i<(((cTOPtr->optionScopeLg)+(cTOPtr->optionLg))/4); i++) {
271        FieldPtr tmp = (FieldPtr) malloc(sizeof(struct Field));
272        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
273        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
274        tmp->fieldType = *((unsigned short*)&buffer2);
275        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
276        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
277        tmp->fieldLength = *((unsigned short*)&buffer2);
278        tmp->next = cTOPtr->fieldSet;
279        tmp->prev = NULL;
280        if ( cTOPtr->fieldSet ) {cTOPtr->fieldSet->prev = tmp;}
281        cTOPtr->fieldSet = tmp;
282        if (i==0) {
283          cTOPtr->lastField = tmp;
284        }
285      }
286      while ((*offV9)%4 != 0) {
287        (*offV9)++; cpt++;
288      } /*padding case*/
289    } else { /*update */
290      ptplotmp = cTOPtr;
291      if (ptplotmp->prev == NULL) {
292        cr->tplOptList = ptplotmp->next;
293        if (cr->tplOptList) {
294          cr->tplOptList->prev = NULL;
295        }
296      } else {
297        ptplotmp->prev->next = ptplotmp->next;
298        if (ptplotmp->next) {
299          ptplotmp->next->prev = ptplotmp->prev;
300        }
301      }
302      deleteTplOption(ptplotmp);
303      cTOPtr = NULL;
304      cTOPtr = newRouterTplOptList();
305      cTOPtr->next = cr->tplOptList;
306      if (cr->tplOptList!=NULL) {cr->tplOptList->prev = cTOPtr;}
307      cr->tplOptList = cTOPtr;
308      cTOPtr->templateOptionId = *((unsigned short*)&buffer2);
309      cTOPtr->sourceId = v9Ptr->sourceId;
310      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
311      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
312      cTOPtr->optionScopeLg = *((unsigned short*)&buffer2);
313      buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
314      buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
315      cTOPtr->optionLg = *((unsigned short*)&buffer2);
316      for ( i=0; i<(((cTOPtr->optionScopeLg)+(cTOPtr->optionLg))/4); i++) {
317        FieldPtr tmp = (FieldPtr) malloc(sizeof(struct Field));
318        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
319        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
320        tmp->fieldType = *((unsigned short*)&buffer2);
321        buffer2[1]= *(buf+(*offV9)); (*offV9)++; cpt++;
322        buffer2[0]= *(buf+(*offV9)); (*offV9)++; cpt++;
323        tmp->fieldLength = *((unsigned short*)&buffer2);
324        tmp->next = cTOPtr->fieldSet;
325        tmp->prev = NULL;
326        if ( cTOPtr->fieldSet ) {cTOPtr->fieldSet->prev = tmp;}
327        cTOPtr->fieldSet = tmp;
328        if (i==0) {
329          cTOPtr->lastField = tmp;
330        }
331      }
332      while ((*offV9)%4 != 0) {
333        (*offV9)++; cpt++;
334      } /*padding case*/
335    }
336    (*cFNPtr)++;
337  } while ((cpt) < (length-2));
338/*   show_all_tplFlSet(); */
339  return length;
340}
341
342
343/*
344 * exist_id
345 *
346 * used in check a template flowset
347 * the id is the source_id(idh) plus getFlowsetId
348 * return:
349 *    - a pointer on the template flowset or NULL
350 */
351TplFlowSetPtr
352existId(RouterPtr cr, unsigned long idh, short idfs)
353{
354  TplFlowSetPtr tmp=cr->tplList;
355  for (; tmp; tmp=tmp->next) {
356    if ((tmp->sourceId==idh)) {
357      return tmp;
358    }
359  }
360  return NULL;
361}
362
363/*
364 * existTplId
365 *
366 * return:
367 *    - a pointer on the correct template flowset definition
368 */
369TplFlowSetPtr
370existTplId(RouterPtr cr, unsigned long sid, short idfs)
371{
372  TplFlowSetPtr tmp=cr->tplList;
373  for (; tmp; tmp=tmp->next) {
374    if ((tmp->sourceId==sid)&&(tmp->templateFlowSetId == idfs)) {
375      return tmp;
376    }
377  }
378  return NULL;
379}
380
381/*
382 * existTploptId
383 *
384 * return:
385 *    - a pointer on the correct template option definition
386 */
387TplOptionPtr
388existTplOptId(RouterPtr cr, unsigned long sid, short idfs)
389{
390  TplOptionPtr tmp=cr->tplOptList;
391  for (; tmp; tmp=tmp->next) {
392    if ((tmp->sourceId==sid)&&(tmp->templateOptionId == idfs)) {
393      return tmp;
394    }
395  }
396  return NULL;
397}
Note: See TracBrowser for help on using the browser.