]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - lib/rt_names.c
Import patch iproute2-2.4.7-default.patch
[lisovros/iproute2_canprio.git] / lib / rt_names.c
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
20 static void rtnl_tab_initialize(char *file, char **tab, int size)
21 {
22         char buf[512];
23         FILE *fp;
24
25         fp = fopen(file, "r");
26         if (!fp)
27                 return;
28         while (fgets(buf, sizeof(buf), fp)) {
29                 char *p = buf;
30                 int id;
31                 char namebuf[512];
32
33                 while (*p == ' ' || *p == '\t')
34                         p++;
35                 if (*p == '#' || *p == '\n' || *p == 0)
36                         continue;
37                 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
38                     sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
39                     sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
40                     sscanf(p, "%d %s #", &id, namebuf) != 2) {
41                         fprintf(stderr, "Database %s is corrupted at %s\n",
42                                 file, p);
43                         return;
44                 }
45
46                 if (id<0 || id>size)
47                         continue;
48
49                 tab[id] = strdup(namebuf);
50         }
51         fclose(fp);
52 }
53
54
55 static char * rtnl_rtprot_tab[256] = {
56         "none",
57         "redirect",
58         "kernel",
59         "boot",
60         "static",
61         NULL,
62         NULL,
63         NULL,
64         "gated",
65         "ra",
66         "mrt",
67         "zebra",
68         "bird",
69 };
70
71
72
73 static int rtnl_rtprot_init;
74
75 static void rtnl_rtprot_initialize(void)
76 {
77         rtnl_rtprot_init = 1;
78         rtnl_tab_initialize("/etc/iproute2/rt_protos",
79                             rtnl_rtprot_tab, 256);
80 }
81
82 char * rtnl_rtprot_n2a(int id, char *buf, int len)
83 {
84         if (id<0 || id>=256) {
85                 snprintf(buf, len, "%d", id);
86                 return buf;
87         }
88         if (!rtnl_rtprot_tab[id]) {
89                 if (!rtnl_rtprot_init)
90                         rtnl_rtprot_initialize();
91         }
92         if (rtnl_rtprot_tab[id])
93                 return rtnl_rtprot_tab[id];
94         snprintf(buf, len, "%d", id);
95         return buf;
96 }
97
98 int rtnl_rtprot_a2n(__u32 *id, char *arg)
99 {
100         static char *cache = NULL;
101         static unsigned long res;
102         char *end;
103         int i;
104
105         if (cache && strcmp(cache, arg) == 0) {
106                 *id = res;
107                 return 0;
108         }
109
110         if (!rtnl_rtprot_init)
111                 rtnl_rtprot_initialize();
112
113         for (i=0; i<256; i++) {
114                 if (rtnl_rtprot_tab[i] &&
115                     strcmp(rtnl_rtprot_tab[i], arg) == 0) {
116                         cache = rtnl_rtprot_tab[i];
117                         res = i;
118                         *id = res;
119                         return 0;
120                 }
121         }
122
123         res = strtoul(arg, &end, 0);
124         if (!end || end == arg || *end || res > 255)
125                 return -1;
126         *id = res;
127         return 0;
128 }
129
130
131
132 static char * rtnl_rtscope_tab[256] = {
133         "global",
134 };
135
136 static int rtnl_rtscope_init;
137
138 static void rtnl_rtscope_initialize(void)
139 {
140         rtnl_rtscope_init = 1;
141         rtnl_rtscope_tab[255] = "nowhere";
142         rtnl_rtscope_tab[254] = "host";
143         rtnl_rtscope_tab[253] = "link";
144         rtnl_rtscope_tab[200] = "site";
145         rtnl_tab_initialize("/etc/iproute2/rt_scopes",
146                             rtnl_rtscope_tab, 256);
147 }
148
149 char * rtnl_rtscope_n2a(int id, char *buf, int len)
150 {
151         if (id<0 || id>=256) {
152                 snprintf(buf, len, "%d", id);
153                 return buf;
154         }
155         if (!rtnl_rtscope_tab[id]) {
156                 if (!rtnl_rtscope_init)
157                         rtnl_rtscope_initialize();
158         }
159         if (rtnl_rtscope_tab[id])
160                 return rtnl_rtscope_tab[id];
161         snprintf(buf, len, "%d", id);
162         return buf;
163 }
164
165 int rtnl_rtscope_a2n(__u32 *id, char *arg)
166 {
167         static char *cache = NULL;
168         static unsigned long res;
169         char *end;
170         int i;
171
172         if (cache && strcmp(cache, arg) == 0) {
173                 *id = res;
174                 return 0;
175         }
176
177         if (!rtnl_rtscope_init)
178                 rtnl_rtscope_initialize();
179
180         for (i=0; i<256; i++) {
181                 if (rtnl_rtscope_tab[i] &&
182                     strcmp(rtnl_rtscope_tab[i], arg) == 0) {
183                         cache = rtnl_rtscope_tab[i];
184                         res = i;
185                         *id = res;
186                         return 0;
187                 }
188         }
189
190         res = strtoul(arg, &end, 0);
191         if (!end || end == arg || *end || res > 255)
192                 return -1;
193         *id = res;
194         return 0;
195 }
196
197
198
199 static char * rtnl_rtrealm_tab[256] = {
200         "unknown",
201 };
202
203 static int rtnl_rtrealm_init;
204
205 static void rtnl_rtrealm_initialize(void)
206 {
207         rtnl_rtrealm_init = 1;
208         rtnl_tab_initialize("/etc/iproute2/rt_realms",
209                             rtnl_rtrealm_tab, 256);
210 }
211
212 char * rtnl_rtrealm_n2a(int id, char *buf, int len)
213 {
214         if (id<0 || id>=256) {
215                 snprintf(buf, len, "%d", id);
216                 return buf;
217         }
218         if (!rtnl_rtrealm_tab[id]) {
219                 if (!rtnl_rtrealm_init)
220                         rtnl_rtrealm_initialize();
221         }
222         if (rtnl_rtrealm_tab[id])
223                 return rtnl_rtrealm_tab[id];
224         snprintf(buf, len, "%d", id);
225         return buf;
226 }
227
228
229 int rtnl_rtrealm_a2n(__u32 *id, char *arg)
230 {
231         static char *cache = NULL;
232         static unsigned long res;
233         char *end;
234         int i;
235
236         if (cache && strcmp(cache, arg) == 0) {
237                 *id = res;
238                 return 0;
239         }
240
241         if (!rtnl_rtrealm_init)
242                 rtnl_rtrealm_initialize();
243
244         for (i=0; i<256; i++) {
245                 if (rtnl_rtrealm_tab[i] &&
246                     strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
247                         cache = rtnl_rtrealm_tab[i];
248                         res = i;
249                         *id = res;
250                         return 0;
251                 }
252         }
253
254         res = strtoul(arg, &end, 0);
255         if (!end || end == arg || *end || res > 255)
256                 return -1;
257         *id = res;
258         return 0;
259 }
260
261
262
263 static char * rtnl_rttable_tab[256] = {
264         "unspec",
265 };
266
267 static int rtnl_rttable_init;
268
269 static void rtnl_rttable_initialize(void)
270 {
271         rtnl_rttable_init = 1;
272         rtnl_rttable_tab[255] = "local";
273         rtnl_rttable_tab[254] = "main";
274         rtnl_rttable_tab[253] = "default";
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(__u32 *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(__u32 *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