root/trunk/src/routers_mgmt.c @ 76

Revision 76, 5.4 KB (checked in by andreu, 14 years ago)

error in configure.in, more checkpoints in template.c, few correction in others files

  • 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 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 "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)
42{
43  RouterPtr tmp = (RouterPtr) malloc(sizeof(struct Router));
44  if (tmp==NULL) {
45    syslog(LOG_ERR, "ERROR in malloc in add_routers function\n");
46    exit(1);
47  } else {
48    tmp->IpAddress = address;
49    tmp->tplList = NULL;
50    tmp->tplOptList = NULL;
51    tmp->sampled = 0;
52    tmp->next = routersList;
53    if (routersList!=NULL) { routersList->prev = tmp;}
54    tmp->prev = NULL;
55    return tmp;
56  }
57}
58
59RouterPtr updateRouter(RouterPtr routersList, unsigned long address,
60                       unsigned long spled)
61{
62  RouterPtr tmp = NULL;
63  if ((tmp = notExistRouter(routersList,
64                            address))==NULL) {
65    syslog(LOG_INFO,"Router Address not registered : %lu.%lu.%lu.%lu)",
66           (address>>24),
67           (address<<8>>24),
68           (address<<16>>24),
69           (address<<24>>24));
70    return routersList;
71  } else {
72    tmp->sampled = spled;
73    return tmp;
74  }
75  return NULL;
76}
77
78TplFlowSetPtr newRouterTplList()
79{
80  TplFlowSetPtr tmp = (TplFlowSetPtr) malloc(sizeof(struct TemplateFlowSet));
81  tmp->sourceId = 0;
82  tmp->templateFlowSetId = 99;
83  tmp->fieldCount = 0;
84  tmp->fieldSet = NULL;
85  tmp->lastField = NULL;
86  tmp->next = NULL;
87  tmp->prev = NULL;
88  return tmp;
89}
90
91TplFlowSetPtr deleteTplFlSet(TplFlowSetPtr ptpl)
92{
93  FieldPtr tmp = NULL;
94  if (ptpl) {
95    while ( ptpl->fieldSet != NULL ){
96      if (!(ptpl->lastField->prev)) {
97        freeField(ptpl->lastField);
98        ptpl->lastField = NULL;
99        ptpl->fieldSet = NULL;
100      } else {
101        tmp = ptpl->lastField->prev;
102        tmp->next = NULL;
103        freeField(ptpl->lastField);
104        ptpl->lastField = tmp;
105      }
106    }
107    free(ptpl);
108    ptpl = NULL;
109  }
110  return NULL;
111}
112
113TplOptionPtr newRouterTplOptList()
114{
115  TplOptionPtr tmp = (TplOptionPtr) malloc(sizeof(struct TemplateOption));
116  tmp->sourceId = 0;
117  tmp->templateOptionId = 66;
118  tmp->length = 0;
119  tmp->optionScopeLg = 0;
120  tmp->optionLg = 0;
121  tmp->fieldSet = NULL;
122  tmp->lastField = NULL;
123  tmp->next = NULL;
124  tmp->prev = NULL;
125  return tmp;
126}
127
128TplOptionPtr deleteTplOption(TplOptionPtr ptplo)
129{
130  FieldPtr tmp = NULL;
131  if (ptplo) {
132    while ( ptplo->fieldSet != NULL ){
133      if (!(ptplo->lastField->prev)) {
134        freeField(ptplo->lastField);
135        ptplo->lastField = NULL;
136        ptplo->fieldSet = NULL;
137      } else {
138        tmp = ptplo->lastField->prev;
139        tmp->next = NULL;
140        freeField(ptplo->lastField);
141        ptplo->lastField = tmp;
142      }
143    }
144    free(ptplo);
145    ptplo = NULL;
146  }
147  return NULL; /* FIXME I don't known */
148}
149
150#ifdef IPV4AGGIDSNMP
151int getSNMPIndexList(char *filename, RouterPtr routersListPtr)
152{
153  FILE *indexFile = NULL;
154  char line[200];
155  int cptLine = 0;
156  char tindex[5];
157  char ttoken[2];
158  char tid[2];
159  char tad[16];
160  int index = 0;
161  unsigned short n0, n1, n2, n3;
162  unsigned char buffer4[4];
163  unsigned long ipAddress;
164  RouterPtr routerTmp = NULL;
165
166  if (!(indexFile = fopen(filename, "r"))) {
167    fprintf (stderr, "error during %s opening\n", filename);
168    exit(1);
169  }
170  while ( fgets(line, 200, indexFile) != 0) {   
171    cptLine++;
172    if ( strspn(line, "R") == 1 ) {
173      if (sscanf(line, "%s %s\n",
174                 ttoken,
175                 tad) == 0) {
176        fprintf(stderr, "Error in file %s, line %d\n",
177                filename, cptLine);
178        exit(1);
179      }
180      if (sscanf(tad,"%hu.%hu.%hu.%hu\n",&n0,&n1,&n2,&n3) == 0){
181        fprintf(stderr, "Address error in file %s, line %d\n",
182                filename, cptLine);
183        exit(1);
184      }
185      buffer4[3] = (unsigned char)n0;
186      buffer4[2] = (unsigned char)n1;
187      buffer4[1] = (unsigned char)n2;
188      buffer4[0] = (unsigned char)n3;   
189      ipAddress = *((unsigned long*)(&buffer4));
190      if ((routerTmp = notExistRouter(routersListPtr,
191                                      ipAddress))==NULL){
192        fprintf(stderr, "Error in file %s, line %d : IP unknown : %lu\n",
193                filename, cptLine, ipAddress);
194        exit(1);
195      }
196    }else{
197      if ( strspn(line, "I") == 1 ) {
198        if (sscanf(line, "%s %s %s\n",
199                   ttoken,
200                   tindex,
201                   tid) == 0) {
202          fprintf(stderr, "Error in file %s, line %d\n",
203                  filename, cptLine);
204          exit(1);
205        }
206        index = atoi(tindex);
207        if ( strcmp(tid, "B") == 0 ) {
208          routerTmp->snmpIndexList[index] = 0;
209        } else if ( strcmp(tid, "C") == 0 ) {
210          routerTmp->snmpIndexList[index] = 1;
211        } else {
212          fprintf(stderr, "Error in file %s, line %d : bad code B or C \n",
213                  filename, cptLine);
214          exit(1);
215        }
216      } else {
217        fprintf(stderr, "Error in file %s, line %d : bad index line \n",
218                filename, cptLine);
219        exit(1);
220      }
221    }
222  }
223  return 1;
224}
225#endif
Note: See TracBrowser for help on using the browser.