root/trunk/src/dataFlowSet.c @ 6

Revision 2, 8.9 KB (checked in by andreu, 17 years ago)

First RENETCOL CVS Integration

  • Property svn:eol-style set to native
Line 
1/*
2 * File: dataFlowSet.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 "dataFlowSet.h"
27
28/*
29 * checkDataFlowSet
30 *
31 */
32unsigned short 
33checkDataFlowSet(unsigned short shift, RouterPtr cr, NetFlowV9HeaderPtr v9Ptr,
34                 unsigned short *offV9, unsigned char *buf, short *cFNPtr,
35                 short *cFId, DatagramPtr pcktPtr, RuleDefPtr *rulesCache,
36                 RulesPtr rlPtr, int myQueue)
37{
38  TplFlowSetPtr tmp;
39  FieldPtr pftmp;
40  FieldPtr secondPftmp;
41  unsigned short data_length = 0;
42  unsigned short flow_size = 0;
43  unsigned short oldOffset = *offV9;
44  unsigned short secondOffset = 0;
45  unsigned short secondOldOffset = 0;
46  int field_size = 0;
47  int cpt = 0;
48  int secondCpt = 0;
49  int overflow = 0;
50  int noEnd = 1;
51  int i=0;
52  int j=0;
53  int pos = 0;
54  unsigned char buffer1; 
55  unsigned char buffer2[2];
56  unsigned char buffer4[4];
57  RulesPtr tmpRuleList = rlPtr;
58  msgType myMsg;
59  char *msgTextIndex;
60  unsigned short tplMsgType = 11;
61
62  buffer2[1] = *(buf+(*offV9));(*offV9)++;
63  buffer2[0] = *(buf+(*offV9));(*offV9)++;
64  (*cFId) = *((unsigned short*)&buffer2);
65  buffer2[1] = *(buf+(*offV9));(*offV9)++;
66  buffer2[0] = *(buf+(*offV9));(*offV9)++;
67  data_length = *((unsigned short*)&buffer2);
68  if ((tmp=existTplId(cr, v9Ptr->sourceId,
69                      (*cFId)))!=NULL) {
70    pftmp = tmp->lastField;
71    for (; pftmp; pftmp = pftmp->prev) {
72      flow_size += pftmp->fieldLength;
73    }
74    if ( data_length%flow_size >= 9 ) {
75      (*cFNPtr) = v9Ptr->count;
76      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",
77             (*cFId),
78             (pcktPtr->ipH->srcAdd>>24),
79             (pcktPtr->ipH->srcAdd<<8>>24),
80             (pcktPtr->ipH->srcAdd<<16>>24),
81             (pcktPtr->ipH->srcAdd<<24>>24));
82      return (data_length+shift);
83    }
84    pftmp = tmp->lastField;
85    secondPftmp = tmp->lastField;   
86    secondOffset = *offV9;
87    secondOldOffset = secondOffset;
88    while ( (((*offV9)-48-shift) <= data_length) && (overflow!=1) ) {
89      /*
90       * progression in a flow Set
91       * notes:
92       *   48=header ip + header netf
93       *   shift = shift if there is a template declaration
94       */
95      cpt++;
96      j=0;
97      pos = (pftmp->fieldType)*10+j;
98      field_size = (int) pftmp->fieldLength;
99      /*
100       * Comparaison between the field value and the rules
101       * ... if one rule exist
102       */
103      if (((RuleDefPtr)(*(rulesCache+pos))) != NULL) {
104        oldOffset = *offV9;
105        while (((RuleDefPtr)(*(rulesCache+pos))) != NULL){
106          /*
107           * while on one cache table line
108           */
109          *offV9 = oldOffset;
110          switch ((int)
111                  (((RuleDefPtr)(*(rulesCache+pos)))->operator)){
112          case 2:
113            switch (field_size) {
114            case 1:
115              buffer1 = *(buf+(*offV9)); (*offV9)++;
116              if (((RuleDefPtr)(*(rulesCache+pos)))->value->stor.cvalue
117                  == *((unsigned char*)&buffer1)) {
118                ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
119              }
120              break;
121            case 2:
122              buffer2[1]= *(buf+(*offV9)); (*offV9)++;
123              buffer2[0]= *(buf+(*offV9)); (*offV9)++;
124              if (((RuleDefPtr)(*(rulesCache+pos)))->value->stor.svalue
125                  == *((unsigned short*)&buffer2))
126                ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
127              break;
128            case 4:
129              buffer4[3]= *(buf+(*offV9)); (*offV9)++;
130              buffer4[2]= *(buf+(*offV9)); (*offV9)++;
131              buffer4[1]= *(buf+(*offV9)); (*offV9)++;
132              buffer4[0]= *(buf+(*offV9)); (*offV9)++;
133              if ((((RuleDefPtr)(*(rulesCache+pos)))->value->stor.lvalue)
134                  == (*((unsigned long*)&buffer4)))
135                {
136                  ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
137                }
138              break;
139            case 16:
140              for (i=0; i<4; i++) {
141                buffer4[3]= *(buf+(*offV9)); (*offV9)++;
142                buffer4[2]= *(buf+(*offV9)); (*offV9)++;
143                buffer4[1]= *(buf+(*offV9)); (*offV9)++;
144                buffer4[0]= *(buf+(*offV9)); (*offV9)++;
145                if (ntohl(((RuleDefPtr)(*(rulesCache+pos)))->value->stor.tabAdd6[i])
146                    == (*((unsigned long*)&buffer4)))
147                  {
148                    if (0==i){
149                      ((RuleDefPtr)(*(rulesCache+pos)))->check = 1;
150                    } else {
151                      ((RuleDefPtr)(*(rulesCache+pos)))->check =
152                        ((RuleDefPtr)(*(rulesCache+pos)))->check && 1;
153                    }
154                  } else {
155                    ((RuleDefPtr)(*(rulesCache+pos)))->check = 0;
156                  }                           
157              }
158              break;
159            default:
160              syslog(LOG_INFO, "Field size not known: %d\n", field_size);
161              for (i=0; i<field_size; i++){
162                (*offV9)++;
163              }
164              break;
165            }
166            break;
167          default:
168            syslog(LOG_INFO, "Operator not known: %d\n",
169                   (int)(((RuleDefPtr)(*(rulesCache+pos)))->operator));
170            break;
171          }
172          j++;
173          pos = (pftmp->fieldType)*10+j;
174        } /* end while rulesCache */
175      } else {
176        /*
177         * no rule within this field type, but we must reading the value
178         */
179        switch (field_size) {
180        case 1:
181          buffer1 = *(buf+(*offV9)); (*offV9)++;
182          break;
183        case 2:
184          buffer2[1]= *(buf+(*offV9)); (*offV9)++;
185          buffer2[0]= *(buf+(*offV9)); (*offV9)++;
186          break;
187        case 4:
188          buffer4[3]= *(buf+(*offV9)); (*offV9)++;
189          buffer4[2]= *(buf+(*offV9)); (*offV9)++;
190          buffer4[1]= *(buf+(*offV9)); (*offV9)++;
191          buffer4[0]= *(buf+(*offV9)); (*offV9)++;
192          break;
193        case 16:
194          for (i=0; i<4; i++) {
195            buffer2[1]= *(buf+(*offV9)); (*offV9)++;
196            buffer2[0]= *(buf+(*offV9)); (*offV9)++;
197            buffer2[1]= *(buf+(*offV9)); (*offV9)++;
198            buffer2[0]= *(buf+(*offV9)); (*offV9)++;
199          }
200          break;
201        default:
202          syslog(LOG_INFO,"UNKNOWN FIELDS LENGTH: %d ", field_size);
203          for (i=0; i<field_size; i++){
204            (*offV9)++;
205          }
206        }
207      } /* end if one cache table line existence */
208      if (cpt==tmp->fieldCount) {
209        /*
210         * end of one flow (not the flowset)
211         */
212        /*
213         * Redirection if needed
214         * switch the rules definition (check & fieldToRecord),
215         * we send the flow or a part of the flow to the msg queue
216         */
217        tmpRuleList = rlPtr;
218        while (tmpRuleList){
219          unsigned short check = 1;
220          RuleDefPtr tmpRuleDefList = tmpRuleList->def;
221          secondOffset = secondOldOffset;
222          while (tmpRuleDefList){
223            check = check && tmpRuleDefList->check;
224            tmpRuleDefList->check = 0;
225            tmpRuleDefList = tmpRuleDefList->next;
226          }
227          if ( (tmpRuleList->def != NULL) && (check == 1)) {
228            /* msg building */
229            secondPftmp = tmp->lastField;
230/*          memcpy(myMsg.text, (char*)(buf+secondOffset),flow_size); */
231            msgTextIndex = mempcpy(mempcpy(mempcpy(myMsg.text,
232                                                   &tplMsgType,
233                                                   sizeof(unsigned short)
234                                                   ),
235                                           &tmpRuleList->id,
236                                           sizeof(tmpRuleList->id)
237                                           ),
238                                   &cr->IpAddress,
239                                   sizeof(unsigned long)
240                                   );
241            msgTextIndex = mempcpy(mempcpy(mempcpy(msgTextIndex,
242                                                   &tmp->sourceId,
243                                                   sizeof(unsigned long)
244                                                   ),
245                                           &tmp->templateFlowSetId,
246                                           sizeof(tmp->templateFlowSetId)
247                                           ),
248                                   buf+secondOffset,
249                                   flow_size
250                                   );
251            myMsg.type = 1;
252            msgSend( myQueue, myMsg);
253            noEnd = 1;
254            secondCpt=0;
255          } /* end if check */
256          tmpRuleList = tmpRuleList->next;
257          secondPftmp = tmp->lastField;
258        } /* end while tmpRuleList */
259        /*
260         * end redirection
261         */
262        secondOffset = *offV9;
263        secondOldOffset = secondOffset;
264        pftmp = tmp->lastField;
265        cpt=0;
266        (*cFNPtr)++; /* pointer on the flows number */
267        if (((*offV9)-48-shift+flow_size) > data_length){
268          overflow = 1; /* buffer index not correct */
269        }
270      } else {
271        /* not the flow end, progress in field list */
272        pftmp = pftmp->prev;
273      }
274    } /* end of the while on one flow record */   
275  }else{
276    /*
277     * template unknown, we skip this all the data
278     */
279    (*offV9)+=(data_length-4);
280    (*cFNPtr) = v9Ptr->count;
281  }
282  while ( ((*offV9)-48-shift) < data_length ) {
283    (*offV9)++; /* if padding */
284  }
285  while ( (*offV9)-48-shift > data_length ) {
286    (*offV9)--; /* crazy loop (when bug appears in template def) */
287  }
288  return (data_length+shift);
289}
290
291/*     for (i=0; i<(90); i++){ */
292/*       int k=0; */
293/*       pos = i*10+k; */
294/*       if (((RuleDefPtr)(*(rulesCache+pos))) != NULL){ */
295/*      fprintf(stderr," type: %d ",i); */
296/*      while ( ((RuleDefPtr)(*(rulesCache+pos))) != NULL){ */
297/*        fprintf(stderr, "\n op:%hu, pos: %d",  */
298/*                ((RuleDefPtr)(*(rulesCache+pos)))->operator, */
299/*                pos); */
300/*        k++; */
301/*        pos = i*10+k; */
302/*      } */
303/*      fprintf(stderr,"\n"); */
304/*       } */
305/*     }  */
Note: See TracBrowser for help on using the browser.