1 /* vi: set sw=4 ts=4: */
3 * rt_names.c rtnetlink names DB.
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.
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
16 /* so far all callers have size == 256 */
17 #define rtnl_tab_initialize(file, tab, size) rtnl_tab_initialize(file, tab)
19 static void rtnl_tab_initialize(const char *file, const char **tab, int size)
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);
30 tab[id] = xstrdup(token[1]);
36 static const char **rtnl_rtprot_tab; /* [256] */
38 static void rtnl_rtprot_initialize(void)
40 static const char *const init_tab[] = {
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);
63 const char* rtnl_rtprot_n2a(int id, char *buf, int len)
65 if (id < 0 || id >= 256) {
66 snprintf(buf, len, "%d", id);
70 rtnl_rtprot_initialize();
72 if (rtnl_rtprot_tab[id])
73 return rtnl_rtprot_tab[id];
74 snprintf(buf, len, "%d", id);
78 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
80 static const char *cache = NULL;
81 static unsigned long res;
84 if (cache && strcmp(cache, arg) == 0) {
89 rtnl_rtprot_initialize();
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];
101 res = bb_strtoul(arg, NULL, 0);
102 if (errno || res > 255)
109 static const char **rtnl_rtscope_tab; /* [256] */
111 static void rtnl_rtscope_initialize(void)
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);
125 const char* rtnl_rtscope_n2a(int id, char *buf, int len)
127 if (id < 0 || id >= 256) {
128 snprintf(buf, len, "%d", id);
132 rtnl_rtscope_initialize();
134 if (rtnl_rtscope_tab[id])
135 return rtnl_rtscope_tab[id];
136 snprintf(buf, len, "%d", id);
140 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
142 static const char *cache = NULL;
143 static unsigned long res;
146 if (cache && strcmp(cache, arg) == 0) {
151 rtnl_rtscope_initialize();
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];
163 res = bb_strtoul(arg, NULL, 0);
164 if (errno || res > 255)
171 static const char **rtnl_rtrealm_tab; /* [256] */
173 static void rtnl_rtrealm_initialize(void)
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);
183 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
185 static const char *cache = NULL;
186 static unsigned long res;
189 if (cache && strcmp(cache, arg) == 0) {
194 rtnl_rtrealm_initialize();
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];
206 res = bb_strtoul(arg, NULL, 0);
207 if (errno || res > 255)
213 #if ENABLE_FEATURE_IP_RULE
214 const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
216 if (id < 0 || id >= 256) {
217 snprintf(buf, len, "%d", id);
221 rtnl_rtrealm_initialize();
223 if (rtnl_rtrealm_tab[id])
224 return rtnl_rtrealm_tab[id];
225 snprintf(buf, len, "%d", id);
231 static const char **rtnl_rtdsfield_tab; /* [256] */
233 static void rtnl_rtdsfield_initialize(void)
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);
243 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
245 if (id < 0 || id >= 256) {
246 snprintf(buf, len, "%d", id);
250 rtnl_rtdsfield_initialize();
252 if (rtnl_rtdsfield_tab[id])
253 return rtnl_rtdsfield_tab[id];
254 snprintf(buf, len, "0x%02x", id);
259 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
261 static const char *cache = NULL;
262 static unsigned long res;
265 if (cache && strcmp(cache, arg) == 0) {
270 rtnl_rtdsfield_initialize();
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];
282 res = bb_strtoul(arg, NULL, 16);
283 if (errno || res > 255)
290 #if ENABLE_FEATURE_IP_RULE
291 static const char **rtnl_rttable_tab; /* [256] */
293 static void rtnl_rttable_initialize(void)
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);
305 const char *rtnl_rttable_n2a(int id, char *buf, int len)
307 if (id < 0 || id >= 256) {
308 snprintf(buf, len, "%d", id);
312 rtnl_rttable_initialize();
314 if (rtnl_rttable_tab[id])
315 return rtnl_rttable_tab[id];
316 snprintf(buf, len, "%d", id);
320 int rtnl_rttable_a2n(uint32_t * id, char *arg)
322 static char *cache = NULL;
323 static unsigned long res;
326 if (cache && strcmp(cache, arg) == 0) {
331 rtnl_rttable_initialize();
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];
342 i = bb_strtoul(arg, NULL, 0);
343 if (errno || i > 255)