root/trunk/src/routers_mgmt.c @ 61

Revision 59, 5.4 KB (checked in by andreu, 15 years ago)

new aggregation method, based on SNMP index. New parameters in renetcolParam.h and new compilation options in configure.in.

  • 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
150int getSNMPIndexList(char *filename, RouterPtr routersListPtr)
151{
152  FILE *indexFile = NULL;
153  char line[200];
154  int cptLine = 0;
155  char tindex[5];
156  char ttoken[2];
157  char tid[2];
158  char tad[16];
159  int index = 0;
160  unsigned short n0, n1, n2, n3;
161  unsigned char buffer4[4];
162  unsigned long ipAddress;
163  RouterPtr routerTmp = NULL;
164
165  if (!(indexFile = fopen(filename, "r"))) {
166    fprintf (stderr, "error during %s opening\n", filename);
167    exit(1);
168  }
169  while ( fgets(line, 200, indexFile) != 0) {   
170    cptLine++;
171    if ( strspn(line, "R") == 1 ) {
172      if (sscanf(line, "%s %s\n",
173                 ttoken,
174                 tad) == 0) {
175        fprintf(stderr, "Error in file %s, line %d\n",
176                filename, cptLine);
177        exit(1);
178      }
179      if (sscanf(tad,"%hu.%hu.%hu.%hu\n",&n0,&n1,&n2,&n3) == 0){
180        fprintf(stderr, "Address error in file %s, line %d\n",
181                filename, cptLine);
182        exit(1);
183      }
184      buffer4[3] = (unsigned char)n0;
185      buffer4[2] = (unsigned char)n1;
186      buffer4[1] = (unsigned char)n2;
187      buffer4[0] = (unsigned char)n3;   
188      ipAddress = *((unsigned long*)(&buffer4));
189      if ((routerTmp = notExistRouter(routersListPtr,
190                                      ipAddress))==NULL){
191        fprintf(stderr, "Error in file %s, line %d : IP unknown : %lu\n",
192                filename, cptLine, ipAddress);
193        exit(1);
194      }
195    }else{
196      if ( strspn(line, "I") == 1 ) {
197        if (sscanf(line, "%s %s %s\n",
198                   ttoken,
199                   tindex,
200                   tid) == 0) {
201          fprintf(stderr, "Error in file %s, line %d\n",
202                  filename, cptLine);
203          exit(1);
204        }
205        index = atoi(tindex);
206        if ( strcmp(tid, "B") == 0 ) {
207          routerTmp->snmpIndexList[index] = 0;
208        } else if ( strcmp(tid, "C") == 0 ) {
209          routerTmp->snmpIndexList[index] = 1;
210        } else {
211          fprintf(stderr, "Error in file %s, line %d : bad code B or C \n",
212                  filename, cptLine);
213          exit(1);
214        }
215      } else {
216        fprintf(stderr, "Error in file %s, line %d : bad index line \n",
217                filename, cptLine);
218        exit(1);
219      }
220    }
221  }
222  return 1;
223}
Note: See TracBrowser for help on using the browser.