root/trunk/src/get_conf.c @ 80

Revision 80, 12.8 KB (checked in by andreu, 14 years ago)

bug correction in prefix reading

  • Property svn:eol-style set to native
Line 
1/*
2 * File: get_conf.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 <stdio.h>
27#include <string.h>
28#include <stdlib.h>
29
30#include "get_conf.h"
31
32/*
33 * initConf()
34 */
35void initConf(char *routersfile)
36
37  /* getRegisteredRouters(routersfile); */
38}
39
40/*
41 * longCmp()
42 */
43int
44longCmp(const void *elem1, const void *elem2){
45  return (*((unsigned long*)elem1) - *((unsigned long*)elem2));
46}
47
48/*
49 * getRegisteredRouters
50 */
51int
52getRegisteredRouters(char *filename, unsigned long *sortedRLPtr,
53                          unsigned long *indexedRLPtr)
54{
55  FILE *routerFile;
56  char line[200];
57  int cptLine;
58  int nbR = 0;
59  char tindex[5];
60  char tname[40];
61  char tad[16];
62  char tad2[16];
63  int ind=0;
64  unsigned short n0, n1, n2, n3;
65  unsigned char buffer4[4];
66
67  if (!(routerFile = fopen(filename, "r"))) {
68    fprintf (stderr, "error during file \"%s\" opening\n", filename);
69    exit(1);
70  }
71  cptLine=0;
72  while ( fgets(line, 200, routerFile) != 0) {   
73    if ( strspn(line, "#") == 0 ) {
74      if (sscanf(line, "%s %s %s %s\n",
75                 tindex,       
76                 tname,
77                 tad,
78                 tad2) == 0) {
79        fprintf(stderr, "Error in file %s, line %d\n",
80                filename, cptLine);
81        exit(1);
82      }
83      cptLine++;
84      ind = atoi(tindex);
85      if (sscanf(tad,"%hu.%hu.%hu.%hu\n",&n0,&n1,&n2,&n3) == 0){
86        fprintf(stderr, "Address error in file %s, line %d\n",
87                filename, cptLine);
88        exit(1);
89      }
90      buffer4[3] = (unsigned char)n0;
91      buffer4[2] = (unsigned char)n1;
92      buffer4[1] = (unsigned char)n2;
93      buffer4[0] = (unsigned char)n3;   
94      sortedRLPtr[ind] = *((unsigned long*)(&buffer4));
95      if (sortedRLPtr[ind] != 0){
96        nbR++;
97      }
98      indexedRLPtr[ind] = *((unsigned long*)(&buffer4));
99    }
100  }
101  qsort(sortedRLPtr, cptLine, sizeof(unsigned long), longCmp);
102  if( fclose(routerFile) != 0) {
103    return (-1);
104  } else {
105    return nbR;
106  }
107}
108
109
110/*
111 *
112 */
113int compStr(a,b)
114{
115  static unsigned short a0,a1,a2,a3,a4,a5;
116  static unsigned short b0,b1,b2,b3,b4,b5;
117  sscanf((char *)a,"%hu.%hu.%hu.%hu/%hu-%hu\n",&a0,&a1,&a2,&a3,&a4,&a5);
118  sscanf((char *)b,"%hu.%hu.%hu.%hu/%hu-%hu\n",&b0,&b1,&b2,&b3,&b4,&b5);
119  if (a0 > b0) return(1);
120  if (a0 < b0) return(-1);
121  if (a1 > b1) return(1);
122  if (a1 < b1) return(-1);
123  if (a2 > b2) return(1);
124  if (a2 < b2) return(-1);
125  if (a3 > b3) return(1);
126  if (a3 < b3) return(-1);
127  return(0);
128}
129
130/*
131 * getPrefixV4()
132 *
133 * read IPv4 prefix file and sort the list
134 */
135unsigned short getPrefixV4(char *filename,
136                           struct PrefixV4 *pV4TabPtr,
137                           struct PrefixV4 *pV4SubTabPtr,
138                           unsigned short *subnetNb)
139{
140  FILE *prefixFile;
141  char line[200];
142  unsigned short counter = 0;
143  unsigned short n0, n1, n2, n3, n4;
144#if defined(IPV4AGGIDR)
145  unsigned short n5;
146#endif
147  unsigned char buffer4[4];
148  int i = 0;
149  char prefixStrTab[MAX_IPV4_PREFIX+MAX_IPV4_SUBNET][50];
150  struct PrefixV4 lastSuperNet;
151  unsigned short lastSuperNetIndex = 0;
152  unsigned short superNetIndex = 0;
153  unsigned short subNetIndex = 0;
154
155  if (!(prefixFile = fopen(filename, "r"))) {
156    fprintf (stderr, "error during file \"%s\" opening\n", filename);
157    exit(1);
158  }
159 
160  *subnetNb = 0;
161
162  lastSuperNet.beginning = 0;
163  lastSuperNet.end = 0;
164  lastSuperNet.mask = 0;
165  lastSuperNet.sampling = 0;
166#ifdef IPV4AGGIDR     
167  lastSuperNet.routerNb = 0;
168#endif
169  lastSuperNet.flowNbIN = 0;
170  lastSuperNet.bytesNbIN = 0;
171  lastSuperNet.pktsNbIN = 0;
172  lastSuperNet.flowNbOUT = 0;
173  lastSuperNet.bytesNbOUT = 0;
174  lastSuperNet.pktsNbOUT = 0;
175  lastSuperNet.firstCoSIN = 0;
176  lastSuperNet.secondCoSIN = 0;
177  lastSuperNet.thirdCoSIN = 0;
178  lastSuperNet.fourthCoSIN = 0;
179  lastSuperNet.firstCoSOUT = 0;
180  lastSuperNet.secondCoSOUT = 0;
181  lastSuperNet.thirdCoSOUT = 0;
182  lastSuperNet.fourthCoSOUT = 0;
183
184  for(i=0;i<MAX_IPV4_PREFIX;i++){
185    pV4TabPtr[i].beginning = 0;
186    pV4TabPtr[i].end = 0;
187    pV4TabPtr[i].mask = 0;
188    pV4TabPtr[i].sampling = 0;
189#ifdef IPV4AGGIDR     
190    pV4TabPtr[i].routerNb = 0;
191#endif
192    pV4TabPtr[i].flowNbIN = 0;
193    pV4TabPtr[i].bytesNbIN = 0;
194    pV4TabPtr[i].pktsNbIN = 0;
195    pV4TabPtr[i].flowNbOUT = 0;
196    pV4TabPtr[i].bytesNbOUT = 0;
197    pV4TabPtr[i].pktsNbOUT = 0;
198    pV4TabPtr[i].firstCoSIN = 0;
199    pV4TabPtr[i].secondCoSIN = 0;
200    pV4TabPtr[i].thirdCoSIN = 0;
201    pV4TabPtr[i].fourthCoSIN = 0;
202    pV4TabPtr[i].firstCoSOUT = 0;
203    pV4TabPtr[i].secondCoSOUT = 0;
204    pV4TabPtr[i].thirdCoSOUT = 0;
205    pV4TabPtr[i].fourthCoSOUT = 0;
206  }
207  for(i=0;i<MAX_IPV4_SUBNET;i++){
208    pV4SubTabPtr[i].beginning = 0;
209    pV4SubTabPtr[i].end = 0;
210    pV4SubTabPtr[i].mask = 0;
211    pV4SubTabPtr[i].sampling = 0;
212#ifdef IPV4AGGIDR     
213    pV4SubTabPtr[i].routerNb = 0;
214#endif
215    pV4SubTabPtr[i].flowNbIN = 0;
216    pV4SubTabPtr[i].bytesNbIN = 0;
217    pV4SubTabPtr[i].pktsNbIN = 0;
218    pV4SubTabPtr[i].flowNbOUT = 0;
219    pV4SubTabPtr[i].bytesNbOUT = 0;
220    pV4SubTabPtr[i].pktsNbOUT = 0;
221    pV4SubTabPtr[i].firstCoSIN = 0;
222    pV4SubTabPtr[i].secondCoSIN = 0;
223    pV4SubTabPtr[i].thirdCoSIN = 0;
224    pV4SubTabPtr[i].fourthCoSIN = 0;
225    pV4SubTabPtr[i].firstCoSOUT = 0;
226    pV4SubTabPtr[i].secondCoSOUT = 0;
227    pV4SubTabPtr[i].thirdCoSOUT = 0;
228    pV4SubTabPtr[i].fourthCoSOUT = 0;
229  }
230  while (fgets(line, 50, prefixFile) != 0)
231    {
232      strcpy(prefixStrTab[counter], line);
233      counter++;
234      if (counter == MAX_IPV4_PREFIX+MAX_IPV4_SUBNET) {
235        fprintf(stderr, "bufferoverflow in getPrefixV4 function (get_conf.c)\
236change the value of MAX_IPV4_PREFIX  declaration and recompile \n");
237        exit(1);
238      }
239    }
240  qsort(prefixStrTab, counter, 50, compStr);
241#ifdef DEBUG
242  for(i=0;i<counter;i++) {
243    fprintf(stderr,"i : %hu - line : %s\n", i,prefixStrTab[i]);
244  }
245#endif
246  for(i=0;i<counter;i++)
247    {
248#if defined(IPV4AGGIDR)
249      sscanf(prefixStrTab[i],"%hu.%hu.%hu.%hu/%hu-%hu\n",
250             &n0,&n1,&n2,&n3,&n4,&n5);
251#else
252      sscanf(prefixStrTab[i],"%hu.%hu.%hu.%hu %hu\n",
253             &n0,&n1,&n2,&n3,&n4);
254#endif
255      buffer4[0] = (unsigned char)n3;
256      buffer4[1] = (unsigned char)n2;
257      buffer4[2] = (unsigned char)n1;
258      buffer4[3] = (unsigned char)n0;
259      /* we sort supernet and subnet */
260      if (lastSuperNet.beginning == 0) {
261        /* case of first supernet */
262        lastSuperNet.beginning = *((unsigned long*)&buffer4[0]);
263        lastSuperNet.end = *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
264#ifdef DEBUG
265        fprintf(stderr,"First SUPERNET : %lu.%lu.%lu.%lu",
266                (lastSuperNet.beginning>>24),
267                (lastSuperNet.beginning<<8>>24),
268                (lastSuperNet.beginning<<16>>24),
269                (lastSuperNet.beginning<<24>>24));
270#endif
271        lastSuperNet.mask = n4;
272        lastSuperNetIndex = i;
273        superNetIndex++;
274        pV4TabPtr[i].beginning =
275          *((unsigned long*)&buffer4[0]);
276        pV4TabPtr[i].end =
277          *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
278        pV4TabPtr[i].mask = n4;
279        pV4TabPtr[i].sampling = 0;
280#ifdef IPV4AGGIDR     
281        pV4TabPtr[i].routerNb = n5;
282#endif
283        pV4TabPtr[i].flowNbIN = 0;
284        pV4TabPtr[i].bytesNbIN = 0;
285        pV4TabPtr[i].pktsNbIN = 0;
286        pV4TabPtr[i].flowNbOUT = 0;
287        pV4TabPtr[i].bytesNbOUT = 0;
288        pV4TabPtr[i].pktsNbOUT = 0;
289        pV4TabPtr[i].firstCoSIN = 0;
290        pV4TabPtr[i].secondCoSIN = 0;
291        pV4TabPtr[i].thirdCoSIN = 0;
292        pV4TabPtr[i].fourthCoSIN = 0;
293        pV4TabPtr[i].firstCoSOUT = 0;
294        pV4TabPtr[i].secondCoSOUT = 0;
295        pV4TabPtr[i].thirdCoSOUT = 0;
296        pV4TabPtr[i].fourthCoSOUT = 0; 
297      } else if ( (lastSuperNet.beginning <= *((unsigned long*)&buffer4[0]))
298                  && (lastSuperNet.end>=*((unsigned long*)&buffer4[0]) +~(~0<<(32-n4))) ) {
299        /* we are in the case of subnet */
300        *subnetNb += 1;
301#ifdef DEBUG
302        fprintf(stderr,"SUBNET %lu.%lu.%lu.%lu FROM SUPERNET %lu.%lu.%lu.%lu %lu.%lu.%lu.%lu , subnetnb : %hu\n",
303                (*((unsigned long*)&buffer4[0])>>24),
304                (*((unsigned long*)&buffer4[0])<<8>>24),
305                (*((unsigned long*)&buffer4[0])<<16>>24),
306                (*((unsigned long*)&buffer4[0])<<24>>24),
307                (pV4TabPtr[lastSuperNetIndex].beginning)>>24,
308                (pV4TabPtr[lastSuperNetIndex].beginning)<<8>>24,
309                (pV4TabPtr[lastSuperNetIndex].beginning)<<16>>24,
310                (pV4TabPtr[lastSuperNetIndex].beginning)<<24>>24,
311                (pV4TabPtr[lastSuperNetIndex].end)>>24,
312                (pV4TabPtr[lastSuperNetIndex].end)<<8>>24,
313                (pV4TabPtr[lastSuperNetIndex].end)<<16>>24,
314                (pV4TabPtr[lastSuperNetIndex].end)<<24>>24,
315                *subnetNb);
316#endif
317        pV4TabPtr[lastSuperNetIndex].hasSubnet = 1;
318        pV4SubTabPtr[subNetIndex].beginning =
319          *((unsigned long*)&buffer4[0]);
320        pV4SubTabPtr[subNetIndex].end =
321          *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
322        pV4SubTabPtr[subNetIndex].mask = n4;
323        pV4SubTabPtr[subNetIndex].sampling = 0;
324#ifdef IPV4AGGIDR     
325        pV4SubTabPtr[subNetIndex].routerNb = n5;
326#endif
327        pV4SubTabPtr[subNetIndex].flowNbIN = 0;
328        pV4SubTabPtr[subNetIndex].bytesNbIN = 0;
329        pV4SubTabPtr[subNetIndex].pktsNbIN = 0;
330        pV4SubTabPtr[subNetIndex].flowNbOUT = 0;
331        pV4SubTabPtr[subNetIndex].bytesNbOUT = 0;
332        pV4SubTabPtr[subNetIndex].pktsNbOUT = 0;
333        pV4SubTabPtr[subNetIndex].firstCoSIN = 0;
334        pV4SubTabPtr[subNetIndex].secondCoSIN = 0;
335        pV4SubTabPtr[subNetIndex].thirdCoSIN = 0;
336        pV4SubTabPtr[subNetIndex].fourthCoSIN = 0;
337        pV4SubTabPtr[subNetIndex].firstCoSOUT = 0;
338        pV4SubTabPtr[subNetIndex].secondCoSOUT = 0;
339        pV4SubTabPtr[subNetIndex].thirdCoSOUT = 0;
340        pV4SubTabPtr[subNetIndex].fourthCoSOUT = 0;
341        subNetIndex++;
342      } else {
343        /* we are in the supernet case */
344        lastSuperNet.beginning = *((unsigned long*)&buffer4[0]);
345        lastSuperNet.end = *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
346#ifdef DEBUG
347        fprintf(stderr,"SUPERNET %lu.%lu.%lu.%lu \n",
348                (*((unsigned long*)&buffer4[0])>>24),
349                (*((unsigned long*)&buffer4[0])<<8>>24),
350                (*((unsigned long*)&buffer4[0])<<16>>24),
351                (*((unsigned long*)&buffer4[0])<<24>>24));
352#endif
353        lastSuperNet.mask = n4;
354        lastSuperNetIndex = superNetIndex;
355        pV4TabPtr[superNetIndex].beginning =
356          *((unsigned long*)&buffer4[0]);
357        pV4TabPtr[superNetIndex].end =
358          *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
359        pV4TabPtr[superNetIndex].mask = n4;
360        pV4TabPtr[superNetIndex].sampling = 0;
361#ifdef IPV4AGGIDR     
362        pV4TabPtr[superNetIndex].routerNb = n5;
363#endif
364        pV4TabPtr[superNetIndex].flowNbIN = 0;
365        pV4TabPtr[superNetIndex].bytesNbIN = 0;
366        pV4TabPtr[superNetIndex].pktsNbIN = 0;
367        pV4TabPtr[superNetIndex].flowNbOUT = 0;
368        pV4TabPtr[superNetIndex].bytesNbOUT = 0;
369        pV4TabPtr[superNetIndex].pktsNbOUT = 0;
370        pV4TabPtr[superNetIndex].firstCoSIN = 0;
371        pV4TabPtr[superNetIndex].secondCoSIN = 0;
372        pV4TabPtr[superNetIndex].thirdCoSIN = 0;
373        pV4TabPtr[superNetIndex].fourthCoSIN = 0;
374        pV4TabPtr[superNetIndex].firstCoSOUT = 0;
375        pV4TabPtr[superNetIndex].secondCoSOUT = 0;
376        pV4TabPtr[superNetIndex].thirdCoSOUT = 0;
377        pV4TabPtr[superNetIndex].fourthCoSOUT = 0;
378        superNetIndex++;
379      }
380    }
381  if( fclose(prefixFile) == 0) {
382    return (superNetIndex);
383  } else {
384    fprintf(stderr,"%s not closed, \n", filename);
385    exit(1);
386    return(superNetIndex);
387  }
388}
389
390/*
391 *
392 */
393int compASStr(a,b)
394{
395  static unsigned short a0;
396  static unsigned short b0;
397  sscanf((char *)a,"%hu\n",&a0);
398  sscanf((char *)b,"%hu\n",&b0);
399  if (a0 > b0) return(1);
400  if (a0 < b0) return(-1);
401  return(0);
402}
403
404/*
405 * getAS()
406 *
407 * read AS file and sort the list
408 */
409unsigned short getAS(char *filename, struct AS *ASTabPtr)
410{
411  FILE *asFile;
412  char line[200];
413  unsigned short counter = 0;
414  unsigned short n0;
415  unsigned short buffer;
416  int i = 0;
417  char ASStrTab[MAX_AS][7];
418
419  if (!(asFile = fopen(filename, "r"))) {
420    fprintf (stderr, "error during file \"%s\" opening\n", filename);
421    exit(1);
422  }
423  for(i=0;i<MAX_AS;i++){
424    ASTabPtr[i].as = 0;
425    ASTabPtr[i].sampling = 0;
426  }
427  while (fgets(line, 7, asFile) != 0)
428    {
429      strcpy(ASStrTab[counter], line);
430      counter++;
431      if (counter > MAX_AS) {
432        fprintf(stderr, "bufferoverflow in getAS function (get_conf.c)\
433change the value of MAX_AS declaration and recompile, counter: %hu \n",
434                        counter);
435        exit(1);
436      }
437    }
438  qsort(ASStrTab, counter, 7, compASStr);
439  for(i=0;i<counter;i++)
440    {
441      sscanf(ASStrTab[i],"%hu\n",
442             &n0);
443      buffer = (unsigned short)n0;
444      ASTabPtr[i].as = *((unsigned short*)&buffer);
445      ASTabPtr[i].sampling = 0;
446    }
447  if( fclose(asFile) == 0) {
448    return (counter);
449  } else {
450    fprintf(stderr,"%s not closed, \n", filename);
451    exit(1);
452    return(counter);
453  }
454}
Note: See TracBrowser for help on using the browser.