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