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