root/trunk/src/routers_mgmt.c @ 58

Revision 58, 5.5 KB (checked in by andreu, 15 years ago)

new aggregation method, new configuration files, corrections on the php script for graph generation.

  • 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 = 1; /* ==1 required in case of no sampling configured */
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    fprintf(stderr, "\nSAMPLED VALUE: %lu\n", spled);
73    tmp->sampled = spled;
74    return tmp;
75  }
76  return NULL;
77}
78
79TplFlowSetPtr newRouterTplList()
80{
81  TplFlowSetPtr tmp = (TplFlowSetPtr) malloc(sizeof(struct TemplateFlowSet));
82  tmp->sourceId = 0;
83  tmp->templateFlowSetId = 99;
84  tmp->fieldCount = 0;
85  tmp->fieldSet = NULL;
86  tmp->lastField = NULL;
87  tmp->next = NULL;
88  tmp->prev = NULL;
89  return tmp;
90}
91
92TplFlowSetPtr deleteTplFlSet(TplFlowSetPtr ptpl)
93{
94  FieldPtr tmp = NULL;
95  if (ptpl) {
96    while ( ptpl->fieldSet != NULL ){
97      if (!(ptpl->lastField->prev)) {
98        freeField(ptpl->lastField);
99        ptpl->lastField = NULL;
100        ptpl->fieldSet = NULL;
101      } else {
102        tmp = ptpl->lastField->prev;
103        tmp->next = NULL;
104        freeField(ptpl->lastField);
105        ptpl->lastField = tmp;
106      }
107    }
108    free(ptpl);
109    ptpl = NULL;
110  }
111  return NULL;
112}
113
114TplOptionPtr newRouterTplOptList()
115{
116  TplOptionPtr tmp = (TplOptionPtr) malloc(sizeof(struct TemplateOption));
117  tmp->sourceId = 0;
118  tmp->templateOptionId = 66;
119  tmp->length = 0;
120  tmp->optionScopeLg = 0;
121  tmp->optionLg = 0;
122  tmp->fieldSet = NULL;
123  tmp->lastField = NULL;
124  tmp->next = NULL;
125  tmp->prev = NULL;
126  return tmp;
127}
128
129TplOptionPtr deleteTplOption(TplOptionPtr ptplo)
130{
131  FieldPtr tmp = NULL;
132  if (ptplo) {
133    while ( ptplo->fieldSet != NULL ){
134      if (!(ptplo->lastField->prev)) {
135        freeField(ptplo->lastField);
136        ptplo->lastField = NULL;
137        ptplo->fieldSet = NULL;
138      } else {
139        tmp = ptplo->lastField->prev;
140        tmp->next = NULL;
141        freeField(ptplo->lastField);
142        ptplo->lastField = tmp;
143      }
144    }
145    free(ptplo);
146    ptplo = NULL;
147  }
148  return NULL; /* FIXME I don't known */
149}
150
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}
Note: See TracBrowser for help on using the browser.