root/trunk/src/routers_mgmt.c @ 158

Revision 154, 6.6 KB (checked in by andreu, 12 years ago)

Typo correction and copyright update

  • Property svn:eol-style set to native
Line 
1/*
2 * File: routers_mgmt.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2005-2011 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 "routers_mgmt.h"
27
28RouterPtr notExistRouter(RouterPtr rL, unsigned long address)
29{
30  RouterPtr tmp = rL;
31  int i = 0;
32  for ( ; tmp; tmp=tmp->next) {
33    i++;
34    if ( tmp->IpAddress == address ){
35      return tmp;
36    }
37  }
38  return NULL;
39}
40
41RouterPtr addRouter(RouterPtr routersList, unsigned long address, unsigned short id)
42{
43  RouterPtr tmp = (RouterPtr) malloc(sizeof(struct Router));
44  if (tmp==NULL) {
45    syslog(LOG_ERR, "ERROR in malloc in addRouter function\n");
46    exit(1);
47  } else {
48    tmp->IpAddress = address;
49    tmp->ID = id;
50    tmp->tplList = NULL;
51    tmp->tplOptList = NULL;
52    tmp->engineList = NULL;
53#if defined(MULTISAMPLING)
54    tmp->sampled = 1;
55#else
56    tmp->sampled = 0;
57#endif
58    tmp->next = routersList;
59    if (routersList!=NULL) { routersList->prev = tmp;}
60    tmp->prev = NULL;
61    return tmp;
62  }
63}
64
65RouterPtr updateRouter(RouterPtr routersList, unsigned long address,
66                       unsigned long spled)
67{
68  RouterPtr tmp = NULL;
69  if ((tmp = notExistRouter(routersList,
70                            address))==NULL) {
71    syslog(LOG_INFO,"Router Address not registered : %lu.%lu.%lu.%lu",
72           (address>>24),
73           (address<<8>>24),
74           (address<<16>>24),
75           (address<<24>>24));
76    return routersList;
77  } else {
78    tmp->sampled = spled;
79    return tmp;
80  }
81  return NULL;
82}
83
84TplFlowSetPtr newRouterTplList()
85{
86  TplFlowSetPtr tmp = (TplFlowSetPtr) malloc(sizeof(struct TemplateFlowSet));
87  tmp->sourceId = 0;
88  tmp->templateFlowSetId = 99;
89  tmp->fieldCount = 0;
90  tmp->fieldSet = NULL;
91  tmp->lastField = NULL;
92  tmp->next = NULL;
93  tmp->prev = NULL;
94  return tmp;
95}
96
97TplFlowSetPtr deleteTplFlSet(TplFlowSetPtr ptpl)
98{
99  FieldPtr tmp = NULL;
100  if (ptpl) {
101    while ( ptpl->fieldSet != NULL ){
102      if (!(ptpl->lastField->prev)) {
103        freeField(ptpl->lastField);
104        ptpl->lastField = NULL;
105        ptpl->fieldSet = NULL;
106      } else {
107        tmp = ptpl->lastField->prev;
108        tmp->next = NULL;
109        freeField(ptpl->lastField);
110        ptpl->lastField = tmp;
111      }
112    }
113    free(ptpl);
114    ptpl = NULL;
115  }
116  return NULL;
117}
118
119NDEEnginePtr newRouterEngineList()
120{
121  NDEEnginePtr tmp = (NDEEnginePtr) malloc(sizeof(struct NDEEngine));
122  tmp->package_sequence = 0;
123  tmp->engineId = 0;
124  tmp->prev = NULL;
125  tmp->next = NULL;
126  return tmp;
127}
128
129/*
130 * existEngId
131 *
132 * return:
133 *    - a pointer on the EngineId or NULL
134 */
135NDEEnginePtr existEngId(RouterPtr cr, unsigned long sid)
136{
137  NDEEnginePtr tmp=cr->engineList;
138  for (; tmp; tmp=tmp->next) {
139    if (tmp->engineId==sid) {
140      return tmp;
141    }
142  }
143  return NULL;
144}
145
146TplOptionPtr newRouterTplOptList()
147{
148  TplOptionPtr tmp = (TplOptionPtr) malloc(sizeof(struct TemplateOption));
149  tmp->sourceId = 0;
150  tmp->templateOptionId = 66;
151  tmp->length = 0;
152  tmp->optionScopeLg = 0;
153  tmp->optionLg = 0;
154  tmp->fieldSet = NULL;
155  tmp->lastField = NULL;
156  tmp->next = NULL;
157  tmp->prev = NULL;
158  return tmp;
159}
160
161TplOptionPtr deleteTplOption(TplOptionPtr ptplo)
162{
163  FieldPtr tmp = NULL;
164  if (ptplo) {
165    while ( ptplo->fieldSet != NULL ){
166      if (!(ptplo->lastField->prev)) {
167        freeField(ptplo->lastField);
168        ptplo->lastField = NULL;
169        ptplo->fieldSet = NULL;
170      } else {
171        tmp = ptplo->lastField->prev;
172        tmp->next = NULL;
173        freeField(ptplo->lastField);
174        ptplo->lastField = tmp;
175      }
176    }
177    free(ptplo);
178    ptplo = NULL;
179  }
180  return NULL; /* FIXME I don't known */
181}
182
183#ifdef IPV4AGGIDSNMP
184int getSNMPIndexList(char *filename, RouterPtr routersListPtr)
185{
186  FILE *indexFile = NULL;
187  char line[200];
188  int cptLine = 0;
189  char tindex[5];
190  char ttoken[2];
191  char tid[2];
192  char tad[16];
193  int index = 0;
194  int i = 0;
195  unsigned short n0, n1, n2, n3;
196  unsigned char buffer4[4];
197  unsigned long ipAddress;
198  RouterPtr routerTmp = NULL;
199  unsigned short snmpIndex = 0;
200  unsigned short snmpCpt = 0;
201
202  if (!(indexFile = fopen(filename, "r"))) {
203    fprintf (stderr, "error during %s opening\n", filename);
204    exit(1);
205  }
206  while ( fgets(line, 200, indexFile) != 0) {   
207    cptLine++;
208    if ( strspn(line, "R") == 1 ) {
209      if (sscanf(line, "%s %s\n",
210                 ttoken,
211                 tad) == 0) {
212        fprintf(stderr, "Error in file %s, line %d\n",
213                filename, cptLine);
214        exit(1);
215      }
216      if (sscanf(tad,"%hu.%hu.%hu.%hu\n",&n0,&n1,&n2,&n3) == 0){
217        fprintf(stderr, "Address error in file %s, line %d\n",
218                filename, cptLine);
219        exit(1);
220      }
221      buffer4[3] = (unsigned char)n0;
222      buffer4[2] = (unsigned char)n1;
223      buffer4[1] = (unsigned char)n2;
224      buffer4[0] = (unsigned char)n3;   
225      ipAddress = *((unsigned long*)(&buffer4));
226      if ((routerTmp = notExistRouter(routersListPtr,
227                                      ipAddress))==NULL){
228        fprintf(stderr, "Error in file %s, line %d : IP unknown : %lu\n",
229                filename, cptLine, ipAddress);
230        exit(1);
231      }
232      for (i=0; i<MAX_INDEX_BY_ROUTER; i++) {
233        routerTmp->snmpIndexType[i] = 2;
234        routerTmp->snmpIndexID[i] = 0;
235      }     
236      snmpCpt = 0;
237    }else{
238      if (snmpCpt==0){
239        routerTmp->snmpIndexType[0] = 1;
240        routerTmp->snmpIndexID[0] = 0;
241        snmpCpt++;
242      }
243      if ( strspn(line, "I") == 1 ) {
244        if (sscanf(line, "%s %s %s\n",
245                   ttoken,
246                   tindex,
247                   tid) == 0) {
248          fprintf(stderr, "Error in file %s, line %d\n",
249                  filename, cptLine);
250          exit(1);
251        }
252        index = atoi(tindex);
253        if ( strcmp(tid, "B") == 0 ) {
254          routerTmp->snmpIndexList[index] = 0;
255          routerTmp->snmpIndexID[snmpCpt] = index;
256          routerTmp->snmpIndexType[snmpCpt] = 0;
257/*        fprintf(stderr, "snmpCpt %hu --> 0 \n", snmpCpt); */
258        } else if ( strcmp(tid, "C") == 0 ) {
259          routerTmp->snmpIndexList[index] = 1;
260          routerTmp->snmpIndexID[snmpCpt] = index;
261          routerTmp->snmpIndexType[snmpCpt] = 1;
262/*        fprintf(stderr, "snmpCpt %hu --> 1 , indexSNMP: %hu\n", snmpCpt, index); */
263        } else {
264          fprintf(stderr, "Error in file %s, line %d : bad code B or C \n",
265                  filename, cptLine);
266          exit(1);
267        }
268        snmpCpt++;
269      } else {
270        fprintf(stderr, "Error in file %s, line %d : bad index line \n",
271                filename, cptLine);
272        exit(1);
273      }
274    }
275  }
276  return 1;
277}
278#endif
Note: See TracBrowser for help on using the browser.