1 | |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <string.h> |
---|
28 | #include <stdlib.h> |
---|
29 | |
---|
30 | #include "get_conf.h" |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | void initConf(char *routersfile) |
---|
36 | { |
---|
37 | |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | int |
---|
44 | longCmp(const void *elem1, const void *elem2){ |
---|
45 | return (*((unsigned long*)elem1) - *((unsigned long*)elem2)); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | int |
---|
52 | getRegisteredRouters(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[20]; |
---|
61 | char tad[15]; |
---|
62 | char tad2[15]; |
---|
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 %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 | |
---|
113 | int 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 | |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | short getPrefixV4(char *filename, struct PrefixV4 *pV4TabPtr) |
---|
136 | { |
---|
137 | FILE *prefixFile; |
---|
138 | char line[200]; |
---|
139 | int counter = 0; |
---|
140 | unsigned short n0, n1, n2, n3, n4, n5; |
---|
141 | unsigned char buffer4[4]; |
---|
142 | int i = 0; |
---|
143 | char prefixStrTab[10000][50]; |
---|
144 | |
---|
145 | if (!(prefixFile = fopen(filename, "r"))) { |
---|
146 | fprintf (stderr, "error during %s opening\n", filename); |
---|
147 | exit(1); |
---|
148 | } |
---|
149 | while (fgets(line, 50, prefixFile) != 0) |
---|
150 | { |
---|
151 | strcpy(prefixStrTab[counter], line); |
---|
152 | counter++; |
---|
153 | if (counter == 10000) { |
---|
154 | fprintf(stderr, "bufferoverflow in getPrefixV4 function (get_conf.c)\ |
---|
155 | change the value of prefixStrTab declaration and recompile \n"); |
---|
156 | exit(1); |
---|
157 | } |
---|
158 | } |
---|
159 | qsort(prefixStrTab, counter, 50, compStr); |
---|
160 | for(i=0;i<counter;i++) |
---|
161 | { |
---|
162 | sscanf(prefixStrTab[i],"%hu.%hu.%hu.%hu/%hu-%hu\n", |
---|
163 | &n0,&n1,&n2,&n3,&n4,&n5); |
---|
164 | buffer4[0] = (unsigned char)n3; |
---|
165 | buffer4[1] = (unsigned char)n2; |
---|
166 | buffer4[2] = (unsigned char)n1; |
---|
167 | buffer4[3] = (unsigned char)n0; |
---|
168 | pV4TabPtr[i].beginning = |
---|
169 | *((unsigned long*)&buffer4[0]); |
---|
170 | pV4TabPtr[i].end = |
---|
171 | *((unsigned long*)&buffer4[0]) +~(~0<<(32-n4)); |
---|
172 | pV4TabPtr[i].mask = n4; |
---|
173 | pV4TabPtr[i].routerNb = n5; |
---|
174 | } |
---|
175 | if( fclose(prefixFile) == 0) { |
---|
176 | return (1); |
---|
177 | } else { |
---|
178 | fprintf(stderr,"%s not closed, \n", filename); |
---|
179 | exit(1); |
---|
180 | return(0); |
---|
181 | } |
---|
182 | } |
---|