diff options
Diffstat (limited to 'networking/libiproute/rt_names.c')
-rw-r--r-- | networking/libiproute/rt_names.c | 389 |
1 files changed, 389 insertions, 0 deletions
diff --git a/networking/libiproute/rt_names.c b/networking/libiproute/rt_names.c new file mode 100644 index 000000000..2a7d85cdb --- /dev/null +++ b/networking/libiproute/rt_names.c | |||
@@ -0,0 +1,389 @@ | |||
1 | /* | ||
2 | * rt_names.c rtnetlink names DB. | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU General Public License | ||
6 | * as published by the Free Software Foundation; either version | ||
7 | * 2 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | ||
10 | */ | ||
11 | |||
12 | #include <stdio.h> | ||
13 | #include <stdlib.h> | ||
14 | #include <unistd.h> | ||
15 | #include <syslog.h> | ||
16 | #include <fcntl.h> | ||
17 | #include <string.h> | ||
18 | #include <sys/time.h> | ||
19 | #include <stdint.h> | ||
20 | |||
21 | static void rtnl_tab_initialize(char *file, char **tab, int size) | ||
22 | { | ||
23 | char buf[512]; | ||
24 | FILE *fp; | ||
25 | |||
26 | fp = fopen(file, "r"); | ||
27 | if (!fp) | ||
28 | return; | ||
29 | while (fgets(buf, sizeof(buf), fp)) { | ||
30 | char *p = buf; | ||
31 | int id; | ||
32 | char namebuf[512]; | ||
33 | |||
34 | while (*p == ' ' || *p == '\t') | ||
35 | p++; | ||
36 | if (*p == '#' || *p == '\n' || *p == 0) | ||
37 | continue; | ||
38 | if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 && | ||
39 | sscanf(p, "0x%x %s #", &id, namebuf) != 2 && | ||
40 | sscanf(p, "%d %s\n", &id, namebuf) != 2 && | ||
41 | sscanf(p, "%d %s #", &id, namebuf) != 2) { | ||
42 | fprintf(stderr, "Database %s is corrupted at %s\n", | ||
43 | file, p); | ||
44 | return; | ||
45 | } | ||
46 | |||
47 | if (id<0 || id>size) | ||
48 | continue; | ||
49 | |||
50 | tab[id] = strdup(namebuf); | ||
51 | } | ||
52 | fclose(fp); | ||
53 | } | ||
54 | |||
55 | |||
56 | static char * rtnl_rtprot_tab[256] = { | ||
57 | "none", | ||
58 | "redirect", | ||
59 | "kernel", | ||
60 | "boot", | ||
61 | "static", | ||
62 | NULL, | ||
63 | NULL, | ||
64 | NULL, | ||
65 | "gated", | ||
66 | "ra", | ||
67 | "mrt", | ||
68 | "zebra", | ||
69 | "bird", | ||
70 | }; | ||
71 | |||
72 | |||
73 | |||
74 | static int rtnl_rtprot_init; | ||
75 | |||
76 | static void rtnl_rtprot_initialize(void) | ||
77 | { | ||
78 | rtnl_rtprot_init = 1; | ||
79 | rtnl_tab_initialize("/etc/iproute2/rt_protos", | ||
80 | rtnl_rtprot_tab, 256); | ||
81 | } | ||
82 | |||
83 | char * rtnl_rtprot_n2a(int id, char *buf, int len) | ||
84 | { | ||
85 | if (id<0 || id>=256) { | ||
86 | snprintf(buf, len, "%d", id); | ||
87 | return buf; | ||
88 | } | ||
89 | if (!rtnl_rtprot_tab[id]) { | ||
90 | if (!rtnl_rtprot_init) | ||
91 | rtnl_rtprot_initialize(); | ||
92 | } | ||
93 | if (rtnl_rtprot_tab[id]) | ||
94 | return rtnl_rtprot_tab[id]; | ||
95 | snprintf(buf, len, "%d", id); | ||
96 | return buf; | ||
97 | } | ||
98 | |||
99 | int rtnl_rtprot_a2n(uint32_t *id, char *arg) | ||
100 | { | ||
101 | static char *cache = NULL; | ||
102 | static unsigned long res; | ||
103 | char *end; | ||
104 | int i; | ||
105 | |||
106 | if (cache && strcmp(cache, arg) == 0) { | ||
107 | *id = res; | ||
108 | return 0; | ||
109 | } | ||
110 | |||
111 | if (!rtnl_rtprot_init) | ||
112 | rtnl_rtprot_initialize(); | ||
113 | |||
114 | for (i=0; i<256; i++) { | ||
115 | if (rtnl_rtprot_tab[i] && | ||
116 | strcmp(rtnl_rtprot_tab[i], arg) == 0) { | ||
117 | cache = rtnl_rtprot_tab[i]; | ||
118 | res = i; | ||
119 | *id = res; | ||
120 | return 0; | ||
121 | } | ||
122 | } | ||
123 | |||
124 | res = strtoul(arg, &end, 0); | ||
125 | if (!end || end == arg || *end || res > 255) | ||
126 | return -1; | ||
127 | *id = res; | ||
128 | return 0; | ||
129 | } | ||
130 | |||
131 | |||
132 | |||
133 | static char * rtnl_rtscope_tab[256] = { | ||
134 | "global", | ||
135 | }; | ||
136 | |||
137 | static int rtnl_rtscope_init; | ||
138 | |||
139 | static void rtnl_rtscope_initialize(void) | ||
140 | { | ||
141 | rtnl_rtscope_init = 1; | ||
142 | rtnl_rtscope_tab[255] = "nowhere"; | ||
143 | rtnl_rtscope_tab[254] = "host"; | ||
144 | rtnl_rtscope_tab[253] = "link"; | ||
145 | rtnl_rtscope_tab[200] = "site"; | ||
146 | rtnl_tab_initialize("/etc/iproute2/rt_scopes", | ||
147 | rtnl_rtscope_tab, 256); | ||
148 | } | ||
149 | |||
150 | char * rtnl_rtscope_n2a(int id, char *buf, int len) | ||
151 | { | ||
152 | if (id<0 || id>=256) { | ||
153 | snprintf(buf, len, "%d", id); | ||
154 | return buf; | ||
155 | } | ||
156 | if (!rtnl_rtscope_tab[id]) { | ||
157 | if (!rtnl_rtscope_init) | ||
158 | rtnl_rtscope_initialize(); | ||
159 | } | ||
160 | if (rtnl_rtscope_tab[id]) | ||
161 | return rtnl_rtscope_tab[id]; | ||
162 | snprintf(buf, len, "%d", id); | ||
163 | return buf; | ||
164 | } | ||
165 | |||
166 | int rtnl_rtscope_a2n(uint32_t *id, char *arg) | ||
167 | { | ||
168 | static char *cache = NULL; | ||
169 | static unsigned long res; | ||
170 | char *end; | ||
171 | int i; | ||
172 | |||
173 | if (cache && strcmp(cache, arg) == 0) { | ||
174 | *id = res; | ||
175 | return 0; | ||
176 | } | ||
177 | |||
178 | if (!rtnl_rtscope_init) | ||
179 | rtnl_rtscope_initialize(); | ||
180 | |||
181 | for (i=0; i<256; i++) { | ||
182 | if (rtnl_rtscope_tab[i] && | ||
183 | strcmp(rtnl_rtscope_tab[i], arg) == 0) { | ||
184 | cache = rtnl_rtscope_tab[i]; | ||
185 | res = i; | ||
186 | *id = res; | ||
187 | return 0; | ||
188 | } | ||
189 | } | ||
190 | |||
191 | res = strtoul(arg, &end, 0); | ||
192 | if (!end || end == arg || *end || res > 255) | ||
193 | return -1; | ||
194 | *id = res; | ||
195 | return 0; | ||
196 | } | ||
197 | |||
198 | |||
199 | |||
200 | static char * rtnl_rtrealm_tab[256] = { | ||
201 | "unknown", | ||
202 | }; | ||
203 | |||
204 | static int rtnl_rtrealm_init; | ||
205 | |||
206 | static void rtnl_rtrealm_initialize(void) | ||
207 | { | ||
208 | rtnl_rtrealm_init = 1; | ||
209 | rtnl_tab_initialize("/etc/iproute2/rt_realms", | ||
210 | rtnl_rtrealm_tab, 256); | ||
211 | } | ||
212 | |||
213 | char * rtnl_rtrealm_n2a(int id, char *buf, int len) | ||
214 | { | ||
215 | if (id<0 || id>=256) { | ||
216 | snprintf(buf, len, "%d", id); | ||
217 | return buf; | ||
218 | } | ||
219 | if (!rtnl_rtrealm_tab[id]) { | ||
220 | if (!rtnl_rtrealm_init) | ||
221 | rtnl_rtrealm_initialize(); | ||
222 | } | ||
223 | if (rtnl_rtrealm_tab[id]) | ||
224 | return rtnl_rtrealm_tab[id]; | ||
225 | snprintf(buf, len, "%d", id); | ||
226 | return buf; | ||
227 | } | ||
228 | |||
229 | |||
230 | int rtnl_rtrealm_a2n(uint32_t *id, char *arg) | ||
231 | { | ||
232 | static char *cache = NULL; | ||
233 | static unsigned long res; | ||
234 | char *end; | ||
235 | int i; | ||
236 | |||
237 | if (cache && strcmp(cache, arg) == 0) { | ||
238 | *id = res; | ||
239 | return 0; | ||
240 | } | ||
241 | |||
242 | if (!rtnl_rtrealm_init) | ||
243 | rtnl_rtrealm_initialize(); | ||
244 | |||
245 | for (i=0; i<256; i++) { | ||
246 | if (rtnl_rtrealm_tab[i] && | ||
247 | strcmp(rtnl_rtrealm_tab[i], arg) == 0) { | ||
248 | cache = rtnl_rtrealm_tab[i]; | ||
249 | res = i; | ||
250 | *id = res; | ||
251 | return 0; | ||
252 | } | ||
253 | } | ||
254 | |||
255 | res = strtoul(arg, &end, 0); | ||
256 | if (!end || end == arg || *end || res > 255) | ||
257 | return -1; | ||
258 | *id = res; | ||
259 | return 0; | ||
260 | } | ||
261 | |||
262 | |||
263 | |||
264 | static char * rtnl_rttable_tab[256] = { | ||
265 | "unspec", | ||
266 | }; | ||
267 | |||
268 | static int rtnl_rttable_init; | ||
269 | |||
270 | static void rtnl_rttable_initialize(void) | ||
271 | { | ||
272 | rtnl_rttable_init = 1; | ||
273 | rtnl_rttable_tab[255] = "local"; | ||
274 | rtnl_rttable_tab[254] = "main"; | ||
275 | rtnl_tab_initialize("/etc/iproute2/rt_tables", | ||
276 | rtnl_rttable_tab, 256); | ||
277 | } | ||
278 | |||
279 | char * rtnl_rttable_n2a(int id, char *buf, int len) | ||
280 | { | ||
281 | if (id<0 || id>=256) { | ||
282 | snprintf(buf, len, "%d", id); | ||
283 | return buf; | ||
284 | } | ||
285 | if (!rtnl_rttable_tab[id]) { | ||
286 | if (!rtnl_rttable_init) | ||
287 | rtnl_rttable_initialize(); | ||
288 | } | ||
289 | if (rtnl_rttable_tab[id]) | ||
290 | return rtnl_rttable_tab[id]; | ||
291 | snprintf(buf, len, "%d", id); | ||
292 | return buf; | ||
293 | } | ||
294 | |||
295 | int rtnl_rttable_a2n(uint32_t *id, char *arg) | ||
296 | { | ||
297 | static char *cache = NULL; | ||
298 | static unsigned long res; | ||
299 | char *end; | ||
300 | int i; | ||
301 | |||
302 | if (cache && strcmp(cache, arg) == 0) { | ||
303 | *id = res; | ||
304 | return 0; | ||
305 | } | ||
306 | |||
307 | if (!rtnl_rttable_init) | ||
308 | rtnl_rttable_initialize(); | ||
309 | |||
310 | for (i=0; i<256; i++) { | ||
311 | if (rtnl_rttable_tab[i] && | ||
312 | strcmp(rtnl_rttable_tab[i], arg) == 0) { | ||
313 | cache = rtnl_rttable_tab[i]; | ||
314 | res = i; | ||
315 | *id = res; | ||
316 | return 0; | ||
317 | } | ||
318 | } | ||
319 | |||
320 | i = strtoul(arg, &end, 0); | ||
321 | if (!end || end == arg || *end || i > 255) | ||
322 | return -1; | ||
323 | *id = i; | ||
324 | return 0; | ||
325 | } | ||
326 | |||
327 | |||
328 | static char * rtnl_rtdsfield_tab[256] = { | ||
329 | "0", | ||
330 | }; | ||
331 | |||
332 | static int rtnl_rtdsfield_init; | ||
333 | |||
334 | static void rtnl_rtdsfield_initialize(void) | ||
335 | { | ||
336 | rtnl_rtdsfield_init = 1; | ||
337 | rtnl_tab_initialize("/etc/iproute2/rt_dsfield", | ||
338 | rtnl_rtdsfield_tab, 256); | ||
339 | } | ||
340 | |||
341 | char * rtnl_dsfield_n2a(int id, char *buf, int len) | ||
342 | { | ||
343 | if (id<0 || id>=256) { | ||
344 | snprintf(buf, len, "%d", id); | ||
345 | return buf; | ||
346 | } | ||
347 | if (!rtnl_rtdsfield_tab[id]) { | ||
348 | if (!rtnl_rtdsfield_init) | ||
349 | rtnl_rtdsfield_initialize(); | ||
350 | } | ||
351 | if (rtnl_rtdsfield_tab[id]) | ||
352 | return rtnl_rtdsfield_tab[id]; | ||
353 | snprintf(buf, len, "0x%02x", id); | ||
354 | return buf; | ||
355 | } | ||
356 | |||
357 | |||
358 | int rtnl_dsfield_a2n(uint32_t *id, char *arg) | ||
359 | { | ||
360 | static char *cache = NULL; | ||
361 | static unsigned long res; | ||
362 | char *end; | ||
363 | int i; | ||
364 | |||
365 | if (cache && strcmp(cache, arg) == 0) { | ||
366 | *id = res; | ||
367 | return 0; | ||
368 | } | ||
369 | |||
370 | if (!rtnl_rtdsfield_init) | ||
371 | rtnl_rtdsfield_initialize(); | ||
372 | |||
373 | for (i=0; i<256; i++) { | ||
374 | if (rtnl_rtdsfield_tab[i] && | ||
375 | strcmp(rtnl_rtdsfield_tab[i], arg) == 0) { | ||
376 | cache = rtnl_rtdsfield_tab[i]; | ||
377 | res = i; | ||
378 | *id = res; | ||
379 | return 0; | ||
380 | } | ||
381 | } | ||
382 | |||
383 | res = strtoul(arg, &end, 16); | ||
384 | if (!end || end == arg || *end || res > 255) | ||
385 | return -1; | ||
386 | *id = res; | ||
387 | return 0; | ||
388 | } | ||
389 | |||