*: conversion to config parser
[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
13 #include "libbb.h"
14 #include "rt_names.h"
15
16 /* so far all callers have size == 256 */
17 #define rtnl_tab_initialize(file, tab, size) rtnl_tab_initialize(file, tab)
18 #define size 256
19 static void rtnl_tab_initialize(const char *file, const char **tab, int size)
20 {
21         char *token[2];
22         parser_t *parser = config_open2(file, fopen_for_read);
23         if (!parser)
24                 return;
25         while (config_read(parser, token, 2, 2, "# \t", 0)) {
26                 int id = bb_strtou(token[0], NULL, 0);
27                 if (id < 0 || id > size) {
28                         bb_error_msg("database %s is corrupted at line %d",
29                                 file, parser->lineno);
30                         break;
31                 }
32                 tab[id] = xstrdup(token[1]);
33         }
34         config_close(parser);
35 }
36 #undef size
37
38 static const char **rtnl_rtprot_tab; /* [256] */
39
40 static void rtnl_rtprot_initialize(void)
41 {
42         static const char *const init_tab[] = {
43                 "none",
44                 "redirect",
45                 "kernel",
46                 "boot",
47                 "static",
48                 NULL,
49                 NULL,
50                 NULL,
51                 "gated",
52                 "ra",
53                 "mrt",
54                 "zebra",
55                 "bird",
56         };
57         if (rtnl_rtprot_tab) return;
58         rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0]));
59         memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab));
60         rtnl_tab_initialize("/etc/iproute2/rt_protos",
61                             rtnl_rtprot_tab, 256);
62 }
63
64
65 const char* rtnl_rtprot_n2a(int id, char *buf, int len)
66 {
67         if (id < 0 || id >= 256) {
68                 snprintf(buf, len, "%d", id);
69                 return buf;
70         }
71
72         rtnl_rtprot_initialize();
73
74         if (rtnl_rtprot_tab[id])
75                 return rtnl_rtprot_tab[id];
76         snprintf(buf, len, "%d", id);
77         return buf;
78 }
79
80 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
81 {
82         static const char *cache = NULL;
83         static unsigned long res;
84         int i;
85
86         if (cache && strcmp(cache, arg) == 0) {
87                 *id = res;
88                 return 0;
89         }
90
91         rtnl_rtprot_initialize();
92
93         for (i = 0; i < 256; i++) {
94                 if (rtnl_rtprot_tab[i] &&
95                     strcmp(rtnl_rtprot_tab[i], arg) == 0) {
96                         cache = rtnl_rtprot_tab[i];
97                         res = i;
98                         *id = res;
99                         return 0;
100                 }
101         }
102
103         res = bb_strtoul(arg, NULL, 0);
104         if (errno || res > 255)
105                 return -1;
106         *id = res;
107         return 0;
108 }
109
110
111 static const char **rtnl_rtscope_tab; /* [256] */
112
113 static void rtnl_rtscope_initialize(void)
114 {
115         if (rtnl_rtscope_tab) return;
116         rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0]));
117         rtnl_rtscope_tab[0] = "global";
118         rtnl_rtscope_tab[255] = "nowhere";
119         rtnl_rtscope_tab[254] = "host";
120         rtnl_rtscope_tab[253] = "link";
121         rtnl_rtscope_tab[200] = "site";
122         rtnl_tab_initialize("/etc/iproute2/rt_scopes",
123                             rtnl_rtscope_tab, 256);
124 }
125
126
127 const char* rtnl_rtscope_n2a(int id, char *buf, int len)
128 {
129         if (id < 0 || id >= 256) {
130                 snprintf(buf, len, "%d", id);
131                 return buf;
132         }
133
134         rtnl_rtscope_initialize();
135
136         if (rtnl_rtscope_tab[id])
137                 return rtnl_rtscope_tab[id];
138         snprintf(buf, len, "%d", id);
139         return buf;
140 }
141
142 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
143 {
144         static const char *cache = NULL;
145         static unsigned long res;
146         int i;
147
148         if (cache && strcmp(cache, arg) == 0) {
149                 *id = res;
150                 return 0;
151         }
152
153         rtnl_rtscope_initialize();
154
155         for (i = 0; i < 256; i++) {
156                 if (rtnl_rtscope_tab[i] &&
157                     strcmp(rtnl_rtscope_tab[i], arg) == 0) {
158                         cache = rtnl_rtscope_tab[i];
159                         res = i;
160                         *id = res;
161                         return 0;
162                 }
163         }
164
165         res = bb_strtoul(arg, NULL, 0);
166         if (errno || res > 255)
167                 return -1;
168         *id = res;
169         return 0;
170 }
171
172
173 static const char **rtnl_rtrealm_tab; /* [256] */
174
175 static void rtnl_rtrealm_initialize(void)
176 {
177         if (rtnl_rtrealm_tab) return;
178         rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0]));
179         rtnl_rtrealm_tab[0] = "unknown";
180         rtnl_tab_initialize("/etc/iproute2/rt_realms",
181                             rtnl_rtrealm_tab, 256);
182 }
183
184
185 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
186 {
187         static const char *cache = NULL;
188         static unsigned long res;
189         int i;
190
191         if (cache && strcmp(cache, arg) == 0) {
192                 *id = res;
193                 return 0;
194         }
195
196         rtnl_rtrealm_initialize();
197
198         for (i = 0; i < 256; i++) {
199                 if (rtnl_rtrealm_tab[i] &&
200                     strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
201                         cache = rtnl_rtrealm_tab[i];
202                         res = i;
203                         *id = res;
204                         return 0;
205                 }
206         }
207
208         res = bb_strtoul(arg, NULL, 0);
209         if (errno || res > 255)
210                 return -1;
211         *id = res;
212         return 0;
213 }
214
215 #if ENABLE_FEATURE_IP_RULE
216 const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
217 {
218         if (id < 0 || id >= 256) {
219                 snprintf(buf, len, "%d", id);
220                 return buf;
221         }
222
223         rtnl_rtrealm_initialize();
224
225         if (rtnl_rtrealm_tab[id])
226                 return rtnl_rtrealm_tab[id];
227         snprintf(buf, len, "%d", id);
228         return buf;
229 }
230 #endif
231
232
233 static const char **rtnl_rtdsfield_tab; /* [256] */
234
235 static void rtnl_rtdsfield_initialize(void)
236 {
237         if (rtnl_rtdsfield_tab) return;
238         rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0]));
239         rtnl_rtdsfield_tab[0] = "0";
240         rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
241                             rtnl_rtdsfield_tab, 256);
242 }
243
244
245 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
246 {
247         if (id < 0 || id >= 256) {
248                 snprintf(buf, len, "%d", id);
249                 return buf;
250         }
251
252         rtnl_rtdsfield_initialize();
253
254         if (rtnl_rtdsfield_tab[id])
255                 return rtnl_rtdsfield_tab[id];
256         snprintf(buf, len, "0x%02x", id);
257         return buf;
258 }
259
260
261 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
262 {
263         static const char *cache = NULL;
264         static unsigned long res;
265         int i;
266
267         if (cache && strcmp(cache, arg) == 0) {
268                 *id = res;
269                 return 0;
270         }
271
272         rtnl_rtdsfield_initialize();
273
274         for (i = 0; i < 256; i++) {
275                 if (rtnl_rtdsfield_tab[i] &&
276                     strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
277                         cache = rtnl_rtdsfield_tab[i];
278                         res = i;
279                         *id = res;
280                         return 0;
281                 }
282         }
283
284         res = bb_strtoul(arg, NULL, 16);
285         if (errno || res > 255)
286                 return -1;
287         *id = res;
288         return 0;
289 }
290
291
292 #if ENABLE_FEATURE_IP_RULE
293 static const char **rtnl_rttable_tab; /* [256] */
294
295 static void rtnl_rttable_initialize(void)
296 {
297         if (rtnl_rtdsfield_tab) return;
298         rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0]));
299         rtnl_rttable_tab[0] = "unspec";
300         rtnl_rttable_tab[255] = "local";
301         rtnl_rttable_tab[254] = "main";
302         rtnl_rttable_tab[253] = "default";
303         rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256);
304 }
305
306
307 const char *rtnl_rttable_n2a(int id, char *buf, int len)
308 {
309         if (id < 0 || id >= 256) {
310                 snprintf(buf, len, "%d", id);
311                 return buf;
312         }
313
314         rtnl_rttable_initialize();
315
316         if (rtnl_rttable_tab[id])
317                 return rtnl_rttable_tab[id];
318         snprintf(buf, len, "%d", id);
319         return buf;
320 }
321
322 int rtnl_rttable_a2n(uint32_t * id, char *arg)
323 {
324         static char *cache = NULL;
325         static unsigned long res;
326         int i;
327
328         if (cache && strcmp(cache, arg) == 0) {
329                 *id = res;
330                 return 0;
331         }
332
333         rtnl_rttable_initialize();
334
335         for (i = 0; i < 256; i++) {
336                 if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
337                         cache = (char*)rtnl_rttable_tab[i];
338                         res = i;
339                         *id = res;
340                         return 0;
341                 }
342         }
343
344         i = bb_strtoul(arg, NULL, 0);
345         if (errno || i > 255)
346                 return -1;
347         *id = i;
348         return 0;
349 }
350
351 #endif