root/trunk/src/get_conf.c @ 86

Revision 86, 13.6 KB (checked in by andreu, 14 years ago)

ticket #23

  • 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(MULTISAMPLING)
145  unsigned short n6;
146#endif
147#if defined(IPV4AGGIDR)
148  unsigned short n5;
149#endif
150  unsigned char buffer4[4];
151  int i = 0;
152  char prefixStrTab[MAX_IPV4_PREFIX+MAX_IPV4_SUBNET][50];
153  struct PrefixV4 lastSuperNet;
154  unsigned short lastSuperNetIndex = 0;
155  unsigned short superNetIndex = 0;
156  unsigned short subNetIndex = 0;
157
158  if (!(prefixFile = fopen(filename, "r"))) {
159    fprintf (stderr, "error during file \"%s\" opening\n", filename);
160    exit(1);
161  }
162 
163  *subnetNb = 0;
164
165  lastSuperNet.beginning = 0;
166  lastSuperNet.end = 0;
167  lastSuperNet.mask = 0;
168  lastSuperNet.sampling = 0;
169#ifdef IPV4AGGIDR     
170  lastSuperNet.routerNb = 0;
171#endif
172  lastSuperNet.flowNbIN = 0;
173  lastSuperNet.bytesNbIN = 0;
174  lastSuperNet.pktsNbIN = 0;
175  lastSuperNet.flowNbOUT = 0;
176  lastSuperNet.bytesNbOUT = 0;
177  lastSuperNet.pktsNbOUT = 0;
178  lastSuperNet.firstCoSIN = 0;
179  lastSuperNet.secondCoSIN = 0;
180  lastSuperNet.thirdCoSIN = 0;
181  lastSuperNet.fourthCoSIN = 0;
182  lastSuperNet.firstCoSOUT = 0;
183  lastSuperNet.secondCoSOUT = 0;
184  lastSuperNet.thirdCoSOUT = 0;
185  lastSuperNet.fourthCoSOUT = 0;
186
187  for(i=0;i<MAX_IPV4_PREFIX;i++){
188    pV4TabPtr[i].beginning = 0;
189    pV4TabPtr[i].end = 0;
190    pV4TabPtr[i].mask = 0;
191    pV4TabPtr[i].sampling = 0;
192#ifdef IPV4AGGIDR     
193    pV4TabPtr[i].routerNb = 0;
194#endif
195    pV4TabPtr[i].flowNbIN = 0;
196    pV4TabPtr[i].bytesNbIN = 0;
197    pV4TabPtr[i].pktsNbIN = 0;
198    pV4TabPtr[i].flowNbOUT = 0;
199    pV4TabPtr[i].bytesNbOUT = 0;
200    pV4TabPtr[i].pktsNbOUT = 0;
201    pV4TabPtr[i].firstCoSIN = 0;
202    pV4TabPtr[i].secondCoSIN = 0;
203    pV4TabPtr[i].thirdCoSIN = 0;
204    pV4TabPtr[i].fourthCoSIN = 0;
205    pV4TabPtr[i].firstCoSOUT = 0;
206    pV4TabPtr[i].secondCoSOUT = 0;
207    pV4TabPtr[i].thirdCoSOUT = 0;
208    pV4TabPtr[i].fourthCoSOUT = 0;
209  }
210  for(i=0;i<MAX_IPV4_SUBNET;i++){
211    pV4SubTabPtr[i].beginning = 0;
212    pV4SubTabPtr[i].end = 0;
213    pV4SubTabPtr[i].mask = 0;
214    pV4SubTabPtr[i].sampling = 0;
215#ifdef IPV4AGGIDR     
216    pV4SubTabPtr[i].routerNb = 0;
217#endif
218    pV4SubTabPtr[i].flowNbIN = 0;
219    pV4SubTabPtr[i].bytesNbIN = 0;
220    pV4SubTabPtr[i].pktsNbIN = 0;
221    pV4SubTabPtr[i].flowNbOUT = 0;
222    pV4SubTabPtr[i].bytesNbOUT = 0;
223    pV4SubTabPtr[i].pktsNbOUT = 0;
224    pV4SubTabPtr[i].firstCoSIN = 0;
225    pV4SubTabPtr[i].secondCoSIN = 0;
226    pV4SubTabPtr[i].thirdCoSIN = 0;
227    pV4SubTabPtr[i].fourthCoSIN = 0;
228    pV4SubTabPtr[i].firstCoSOUT = 0;
229    pV4SubTabPtr[i].secondCoSOUT = 0;
230    pV4SubTabPtr[i].thirdCoSOUT = 0;
231    pV4SubTabPtr[i].fourthCoSOUT = 0;
232  }
233  while (fgets(line, 50, prefixFile) != 0)
234    {
235      strcpy(prefixStrTab[counter], line);
236      counter++;
237      if (counter == MAX_IPV4_PREFIX+MAX_IPV4_SUBNET) {
238        fprintf(stderr, "bufferoverflow in getPrefixV4 function (get_conf.c)\
239change the value of MAX_IPV4_PREFIX  declaration and recompile \n");
240        exit(1);
241      }
242    }
243  qsort(prefixStrTab, counter, 50, compStr);
244#ifdef DEBUG
245  for(i=0;i<counter;i++) {
246    fprintf(stderr,"i : %hu - line : %s\n", i,prefixStrTab[i]);
247  }
248#endif
249  for(i=0;i<counter;i++)
250    {
251#if defined(IPV4AGGIDR)
252      sscanf(prefixStrTab[i],"%hu.%hu.%hu.%hu/%hu-%hu\n",
253             &n0,&n1,&n2,&n3,&n4,&n5);
254#endif
255#if defined(IPV4AGGIDSNMP)
256#if defined(MULTISAMPLING)
257      sscanf(prefixStrTab[i],"%hu.%hu.%hu.%hu %hu %hu\n",
258             &n0,&n1,&n2,&n3,&n4,&n6);
259#else
260      sscanf(prefixStrTab[i],"%hu.%hu.%hu.%hu %hu\n",
261             &n0,&n1,&n2,&n3,&n4);
262#endif
263#endif
264      buffer4[0] = (unsigned char)n3;
265      buffer4[1] = (unsigned char)n2;
266      buffer4[2] = (unsigned char)n1;
267      buffer4[3] = (unsigned char)n0;
268      /* we sort supernet and subnet */
269      if (lastSuperNet.beginning == 0) {
270        /* case of first supernet */
271        lastSuperNet.beginning = *((unsigned long*)&buffer4[0]);
272        lastSuperNet.end = *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
273#ifdef DEBUG
274        fprintf(stderr,"First SUPERNET : %lu.%lu.%lu.%lu",
275                (lastSuperNet.beginning>>24),
276                (lastSuperNet.beginning<<8>>24),
277                (lastSuperNet.beginning<<16>>24),
278                (lastSuperNet.beginning<<24>>24));
279#endif
280        lastSuperNet.mask = n4;
281        lastSuperNetIndex = i;
282        superNetIndex++;
283        pV4TabPtr[i].beginning =
284          *((unsigned long*)&buffer4[0]);
285        pV4TabPtr[i].end =
286          *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
287        pV4TabPtr[i].mask = n4;
288#if defined(MULTISAMPLING)
289        pV4TabPtr[i].sampling = n6;
290#else
291        pV4TabPtr[i].sampling = 0;
292#endif
293#ifdef IPV4AGGIDR     
294        pV4TabPtr[i].routerNb = n5;
295#endif
296        pV4TabPtr[i].flowNbIN = 0;
297        pV4TabPtr[i].bytesNbIN = 0;
298        pV4TabPtr[i].pktsNbIN = 0;
299        pV4TabPtr[i].flowNbOUT = 0;
300        pV4TabPtr[i].bytesNbOUT = 0;
301        pV4TabPtr[i].pktsNbOUT = 0;
302        pV4TabPtr[i].firstCoSIN = 0;
303        pV4TabPtr[i].secondCoSIN = 0;
304        pV4TabPtr[i].thirdCoSIN = 0;
305        pV4TabPtr[i].fourthCoSIN = 0;
306        pV4TabPtr[i].firstCoSOUT = 0;
307        pV4TabPtr[i].secondCoSOUT = 0;
308        pV4TabPtr[i].thirdCoSOUT = 0;
309        pV4TabPtr[i].fourthCoSOUT = 0; 
310      } else if ( (lastSuperNet.beginning <= *((unsigned long*)&buffer4[0]))
311                  && (lastSuperNet.end>=*((unsigned long*)&buffer4[0]) +~(~0<<(32-n4))) ) {
312        /* we are in the case of subnet */
313        *subnetNb += 1;
314#ifdef DEBUG
315        fprintf(stderr,"SUBNET %lu.%lu.%lu.%lu FROM SUPERNET %lu.%lu.%lu.%lu %lu.%lu.%lu.%lu , subnetnb : %hu\n",
316                (*((unsigned long*)&buffer4[0])>>24),
317                (*((unsigned long*)&buffer4[0])<<8>>24),
318                (*((unsigned long*)&buffer4[0])<<16>>24),
319                (*((unsigned long*)&buffer4[0])<<24>>24),
320                (pV4TabPtr[lastSuperNetIndex].beginning)>>24,
321                (pV4TabPtr[lastSuperNetIndex].beginning)<<8>>24,
322                (pV4TabPtr[lastSuperNetIndex].beginning)<<16>>24,
323                (pV4TabPtr[lastSuperNetIndex].beginning)<<24>>24,
324                (pV4TabPtr[lastSuperNetIndex].end)>>24,
325                (pV4TabPtr[lastSuperNetIndex].end)<<8>>24,
326                (pV4TabPtr[lastSuperNetIndex].end)<<16>>24,
327                (pV4TabPtr[lastSuperNetIndex].end)<<24>>24,
328                *subnetNb);
329#endif
330        pV4TabPtr[lastSuperNetIndex].hasSubnet = 1;
331        pV4SubTabPtr[subNetIndex].beginning =
332          *((unsigned long*)&buffer4[0]);
333        pV4SubTabPtr[subNetIndex].end =
334          *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
335        pV4SubTabPtr[subNetIndex].mask = n4;
336#if defined(MULTISAMPLING)
337        pV4SubTabPtr[subNetIndex].sampling = n6;
338#else
339        pV4SubTabPtr[subNetIndex].sampling = 0;
340#endif
341#ifdef IPV4AGGIDR     
342        pV4SubTabPtr[subNetIndex].routerNb = n5;
343#endif
344        pV4SubTabPtr[subNetIndex].flowNbIN = 0;
345        pV4SubTabPtr[subNetIndex].bytesNbIN = 0;
346        pV4SubTabPtr[subNetIndex].pktsNbIN = 0;
347        pV4SubTabPtr[subNetIndex].flowNbOUT = 0;
348        pV4SubTabPtr[subNetIndex].bytesNbOUT = 0;
349        pV4SubTabPtr[subNetIndex].pktsNbOUT = 0;
350        pV4SubTabPtr[subNetIndex].firstCoSIN = 0;
351        pV4SubTabPtr[subNetIndex].secondCoSIN = 0;
352        pV4SubTabPtr[subNetIndex].thirdCoSIN = 0;
353        pV4SubTabPtr[subNetIndex].fourthCoSIN = 0;
354        pV4SubTabPtr[subNetIndex].firstCoSOUT = 0;
355        pV4SubTabPtr[subNetIndex].secondCoSOUT = 0;
356        pV4SubTabPtr[subNetIndex].thirdCoSOUT = 0;
357        pV4SubTabPtr[subNetIndex].fourthCoSOUT = 0;
358        subNetIndex++;
359      } else {
360        /* we are in the supernet case */
361        lastSuperNet.beginning = *((unsigned long*)&buffer4[0]);
362        lastSuperNet.end = *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
363#ifdef DEBUG
364        fprintf(stderr,"SUPERNET %lu.%lu.%lu.%lu \n",
365                (*((unsigned long*)&buffer4[0])>>24),
366                (*((unsigned long*)&buffer4[0])<<8>>24),
367                (*((unsigned long*)&buffer4[0])<<16>>24),
368                (*((unsigned long*)&buffer4[0])<<24>>24));
369#endif
370        lastSuperNet.mask = n4;
371        lastSuperNetIndex = superNetIndex;
372        pV4TabPtr[superNetIndex].beginning =
373          *((unsigned long*)&buffer4[0]);
374        pV4TabPtr[superNetIndex].end =
375          *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4));
376        pV4TabPtr[superNetIndex].mask = n4;
377#if defined(MULTISAMPLING)
378        pV4TabPtr[superNetIndex].sampling = n6;
379#else
380        pV4TabPtr[superNetIndex].sampling = 0;
381#endif
382#ifdef IPV4AGGIDR     
383        pV4TabPtr[superNetIndex].routerNb = n5;
384#endif
385        pV4TabPtr[superNetIndex].flowNbIN = 0;
386        pV4TabPtr[superNetIndex].bytesNbIN = 0;
387        pV4TabPtr[superNetIndex].pktsNbIN = 0;
388        pV4TabPtr[superNetIndex].flowNbOUT = 0;
389        pV4TabPtr[superNetIndex].bytesNbOUT = 0;
390        pV4TabPtr[superNetIndex].pktsNbOUT = 0;
391        pV4TabPtr[superNetIndex].firstCoSIN = 0;
392        pV4TabPtr[superNetIndex].secondCoSIN = 0;
393        pV4TabPtr[superNetIndex].thirdCoSIN = 0;
394        pV4TabPtr[superNetIndex].fourthCoSIN = 0;
395        pV4TabPtr[superNetIndex].firstCoSOUT = 0;
396        pV4TabPtr[superNetIndex].secondCoSOUT = 0;
397        pV4TabPtr[superNetIndex].thirdCoSOUT = 0;
398        pV4TabPtr[superNetIndex].fourthCoSOUT = 0;
399        superNetIndex++;
400      }
401    }
402  if( fclose(prefixFile) == 0) {
403    return (superNetIndex);
404  } else {
405    fprintf(stderr,"%s not closed, \n", filename);
406    exit(1);
407    return(superNetIndex);
408  }
409}
410
411/*
412 *
413 */
414int compASStr(a,b)
415{
416  static unsigned short a0;
417  static unsigned short b0;
418  sscanf((char *)a,"%hu\n",&a0);
419  sscanf((char *)b,"%hu\n",&b0);
420  if (a0 > b0) return(1);
421  if (a0 < b0) return(-1);
422  return(0);
423}
424
425/*
426 * getAS()
427 *
428 * read AS file and sort the list
429 */
430unsigned short getAS(char *filename, struct AS *ASTabPtr)
431{
432  FILE *asFile;
433  char line[200];
434  unsigned short counter = 0;
435  unsigned short n0;
436#if defined(MULTISAMPLING)
437  unsigned short n1;
438#endif
439  unsigned short buffer;
440  int i = 0;
441  char ASStrTab[MAX_AS][256];
442
443  if (!(asFile = fopen(filename, "r"))) {
444    fprintf (stderr, "error during file \"%s\" opening\n", filename);
445    exit(1);
446  }
447  for(i=0;i<MAX_AS;i++){
448    ASTabPtr[i].as = 0;
449    ASTabPtr[i].sampling = 0;
450  }
451  while (fgets(line, 256, asFile) != 0)
452    {
453      strcpy(ASStrTab[counter], line);
454      counter++;
455      if (counter > MAX_AS) {
456        fprintf(stderr, "bufferoverflow in getAS function (get_conf.c)\
457change the value of MAX_AS declaration and recompile, counter: %hu \n",
458                        counter);
459        exit(1);
460      }
461    }
462  qsort(ASStrTab, counter, 256, compASStr);
463  for(i=0;i<counter;i++)
464    {
465#if defined(MULTISAMPLING)
466      sscanf(ASStrTab[i],"%hu %hu\n",
467             &n0,
468             &n1);
469      buffer = (unsigned short)n0;
470      ASTabPtr[i].as = *((unsigned short*)&buffer);
471      buffer = (unsigned short)n1;
472      ASTabPtr[i].sampling = *((unsigned short*)&buffer);
473#else
474      sscanf(ASStrTab[i],"%hu\n",
475             &n0);
476      buffer = (unsigned short)n0;
477      ASTabPtr[i].as = *((unsigned short*)&buffer);
478      ASTabPtr[i].sampling = 0;
479#endif
480    }
481  if( fclose(asFile) == 0) {
482    return (counter);
483  } else {
484    fprintf(stderr,"%s not closed, \n", filename);
485    exit(1);
486    return(counter);
487  }
488}
Note: See TracBrowser for help on using the browser.