Annotation of /trunk/mkinitrd-magellan/busybox/networking/libiproute/rt_names.c
Parent Directory | Revision Log
Revision 984 -
(hide annotations)
(download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 5599 byte(s)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 5599 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | niro | 532 | /* 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 "libbb.h" | ||
13 | #include "rt_names.h" | ||
14 | |||
15 | niro | 984 | typedef struct rtnl_tab_t { |
16 | const char *cached_str; | ||
17 | unsigned cached_result; | ||
18 | const char *tab[256]; | ||
19 | } rtnl_tab_t; | ||
20 | |||
21 | static void rtnl_tab_initialize(const char *file, const char **tab) | ||
22 | niro | 532 | { |
23 | niro | 816 | char *token[2]; |
24 | parser_t *parser = config_open2(file, fopen_for_read); | ||
25 | niro | 984 | |
26 | niro | 816 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
27 | niro | 984 | unsigned id = bb_strtou(token[0], NULL, 0); |
28 | if (id > 256) { | ||
29 | niro | 816 | bb_error_msg("database %s is corrupted at line %d", |
30 | file, parser->lineno); | ||
31 | break; | ||
32 | niro | 532 | } |
33 | niro | 816 | tab[id] = xstrdup(token[1]); |
34 | niro | 532 | } |
35 | niro | 816 | config_close(parser); |
36 | niro | 532 | } |
37 | |||
38 | niro | 984 | static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base) |
39 | { | ||
40 | unsigned i; | ||
41 | niro | 532 | |
42 | niro | 984 | if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) { |
43 | *id = tab->cached_result; | ||
44 | return 0; | ||
45 | } | ||
46 | |||
47 | for (i = 0; i < 256; i++) { | ||
48 | if (tab->tab[i] | ||
49 | && strcmp(tab->tab[i], arg) == 0 | ||
50 | ) { | ||
51 | tab->cached_str = tab->tab[i]; | ||
52 | tab->cached_result = i; | ||
53 | *id = i; | ||
54 | return 0; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | i = bb_strtou(arg, NULL, base); | ||
59 | if (i > 255) | ||
60 | return -1; | ||
61 | *id = i; | ||
62 | return 0; | ||
63 | } | ||
64 | |||
65 | |||
66 | static rtnl_tab_t *rtnl_rtprot_tab; | ||
67 | |||
68 | niro | 532 | static void rtnl_rtprot_initialize(void) |
69 | { | ||
70 | static const char *const init_tab[] = { | ||
71 | "none", | ||
72 | "redirect", | ||
73 | "kernel", | ||
74 | "boot", | ||
75 | "static", | ||
76 | NULL, | ||
77 | NULL, | ||
78 | NULL, | ||
79 | "gated", | ||
80 | "ra", | ||
81 | "mrt", | ||
82 | "zebra", | ||
83 | "bird", | ||
84 | }; | ||
85 | niro | 984 | |
86 | if (rtnl_rtprot_tab) | ||
87 | return; | ||
88 | rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab)); | ||
89 | memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab)); | ||
90 | rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab); | ||
91 | niro | 532 | } |
92 | |||
93 | niro | 984 | const char* FAST_FUNC rtnl_rtprot_n2a(int id, char *buf) |
94 | niro | 532 | { |
95 | if (id < 0 || id >= 256) { | ||
96 | niro | 984 | sprintf(buf, "%d", id); |
97 | niro | 532 | return buf; |
98 | } | ||
99 | |||
100 | rtnl_rtprot_initialize(); | ||
101 | |||
102 | niro | 984 | if (rtnl_rtprot_tab->tab[id]) |
103 | return rtnl_rtprot_tab->tab[id]; | ||
104 | /* buf is SPRINT_BSIZE big */ | ||
105 | sprintf(buf, "%d", id); | ||
106 | niro | 532 | return buf; |
107 | } | ||
108 | |||
109 | niro | 984 | int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg) |
110 | niro | 532 | { |
111 | rtnl_rtprot_initialize(); | ||
112 | niro | 984 | return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0); |
113 | niro | 532 | } |
114 | |||
115 | |||
116 | niro | 984 | static rtnl_tab_t *rtnl_rtscope_tab; |
117 | niro | 532 | |
118 | static void rtnl_rtscope_initialize(void) | ||
119 | { | ||
120 | niro | 984 | if (rtnl_rtscope_tab) |
121 | return; | ||
122 | rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab)); | ||
123 | rtnl_rtscope_tab->tab[0] = "global"; | ||
124 | rtnl_rtscope_tab->tab[255] = "nowhere"; | ||
125 | rtnl_rtscope_tab->tab[254] = "host"; | ||
126 | rtnl_rtscope_tab->tab[253] = "link"; | ||
127 | rtnl_rtscope_tab->tab[200] = "site"; | ||
128 | rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab); | ||
129 | niro | 532 | } |
130 | |||
131 | niro | 984 | const char* FAST_FUNC rtnl_rtscope_n2a(int id, char *buf) |
132 | niro | 532 | { |
133 | if (id < 0 || id >= 256) { | ||
134 | niro | 984 | sprintf(buf, "%d", id); |
135 | niro | 532 | return buf; |
136 | } | ||
137 | |||
138 | rtnl_rtscope_initialize(); | ||
139 | |||
140 | niro | 984 | if (rtnl_rtscope_tab->tab[id]) |
141 | return rtnl_rtscope_tab->tab[id]; | ||
142 | /* buf is SPRINT_BSIZE big */ | ||
143 | sprintf(buf, "%d", id); | ||
144 | niro | 532 | return buf; |
145 | } | ||
146 | |||
147 | niro | 984 | int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg) |
148 | niro | 532 | { |
149 | rtnl_rtscope_initialize(); | ||
150 | niro | 984 | return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0); |
151 | niro | 532 | } |
152 | |||
153 | |||
154 | niro | 984 | static rtnl_tab_t *rtnl_rtrealm_tab; |
155 | niro | 532 | |
156 | static void rtnl_rtrealm_initialize(void) | ||
157 | { | ||
158 | if (rtnl_rtrealm_tab) return; | ||
159 | niro | 984 | rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab)); |
160 | rtnl_rtrealm_tab->tab[0] = "unknown"; | ||
161 | rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab); | ||
162 | niro | 532 | } |
163 | |||
164 | niro | 984 | int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg) |
165 | niro | 532 | { |
166 | rtnl_rtrealm_initialize(); | ||
167 | niro | 984 | return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0); |
168 | niro | 532 | } |
169 | |||
170 | #if ENABLE_FEATURE_IP_RULE | ||
171 | niro | 984 | const char* FAST_FUNC rtnl_rtrealm_n2a(int id, char *buf) |
172 | niro | 532 | { |
173 | if (id < 0 || id >= 256) { | ||
174 | niro | 984 | sprintf(buf, "%d", id); |
175 | niro | 532 | return buf; |
176 | } | ||
177 | |||
178 | rtnl_rtrealm_initialize(); | ||
179 | |||
180 | niro | 984 | if (rtnl_rtrealm_tab->tab[id]) |
181 | return rtnl_rtrealm_tab->tab[id]; | ||
182 | /* buf is SPRINT_BSIZE big */ | ||
183 | sprintf(buf, "%d", id); | ||
184 | niro | 532 | return buf; |
185 | } | ||
186 | #endif | ||
187 | |||
188 | |||
189 | niro | 984 | static rtnl_tab_t *rtnl_rtdsfield_tab; |
190 | niro | 532 | |
191 | static void rtnl_rtdsfield_initialize(void) | ||
192 | { | ||
193 | if (rtnl_rtdsfield_tab) return; | ||
194 | niro | 984 | rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab)); |
195 | rtnl_rtdsfield_tab->tab[0] = "0"; | ||
196 | rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab); | ||
197 | niro | 532 | } |
198 | |||
199 | niro | 984 | const char* FAST_FUNC rtnl_dsfield_n2a(int id, char *buf) |
200 | niro | 532 | { |
201 | if (id < 0 || id >= 256) { | ||
202 | niro | 984 | sprintf(buf, "%d", id); |
203 | niro | 532 | return buf; |
204 | } | ||
205 | |||
206 | rtnl_rtdsfield_initialize(); | ||
207 | |||
208 | niro | 984 | if (rtnl_rtdsfield_tab->tab[id]) |
209 | return rtnl_rtdsfield_tab->tab[id]; | ||
210 | /* buf is SPRINT_BSIZE big */ | ||
211 | sprintf(buf, "0x%02x", id); | ||
212 | niro | 532 | return buf; |
213 | } | ||
214 | |||
215 | niro | 984 | int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg) |
216 | niro | 532 | { |
217 | rtnl_rtdsfield_initialize(); | ||
218 | niro | 984 | return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16); |
219 | niro | 532 | } |
220 | |||
221 | |||
222 | #if ENABLE_FEATURE_IP_RULE | ||
223 | niro | 984 | static rtnl_tab_t *rtnl_rttable_tab; |
224 | niro | 532 | |
225 | static void rtnl_rttable_initialize(void) | ||
226 | { | ||
227 | if (rtnl_rtdsfield_tab) return; | ||
228 | niro | 984 | rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab)); |
229 | rtnl_rttable_tab->tab[0] = "unspec"; | ||
230 | rtnl_rttable_tab->tab[255] = "local"; | ||
231 | rtnl_rttable_tab->tab[254] = "main"; | ||
232 | rtnl_rttable_tab->tab[253] = "default"; | ||
233 | rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab); | ||
234 | niro | 532 | } |
235 | |||
236 | niro | 984 | const char* FAST_FUNC rtnl_rttable_n2a(int id, char *buf) |
237 | niro | 532 | { |
238 | if (id < 0 || id >= 256) { | ||
239 | niro | 984 | sprintf(buf, "%d", id); |
240 | niro | 532 | return buf; |
241 | } | ||
242 | |||
243 | rtnl_rttable_initialize(); | ||
244 | |||
245 | niro | 984 | if (rtnl_rttable_tab->tab[id]) |
246 | return rtnl_rttable_tab->tab[id]; | ||
247 | /* buf is SPRINT_BSIZE big */ | ||
248 | sprintf(buf, "%d", id); | ||
249 | niro | 532 | return buf; |
250 | } | ||
251 | |||
252 | niro | 984 | int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg) |
253 | niro | 532 | { |
254 | rtnl_rttable_initialize(); | ||
255 | niro | 984 | return rtnl_a2n(rtnl_rttable_tab, id, arg, 0); |
256 | niro | 532 | } |
257 | |||
258 | #endif |