root/trunk/src/routers_mgmt.c @ 84

Revision 84, 5.5 KB (checked in by andreu, 14 years ago)

multi-sampling implementation - phase 1

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