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);
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);
32 tab[id] = xstrdup(token[1]);
38 static const char **rtnl_rtprot_tab; /* [256] */
40 static void rtnl_rtprot_initialize(void)
42 static const char *const init_tab[] = {
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);
65 const char* rtnl_rtprot_n2a(int id, char *buf, int len)
67 if (id < 0 || id >= 256) {
68 snprintf(buf, len, "%d", id);
72 rtnl_rtprot_initialize();
74 if (rtnl_rtprot_tab[id])
75 return rtnl_rtprot_tab[id];
76 snprintf(buf, len, "%d", id);
80 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
82 static const char *cache = NULL;
83 static unsigned long res;
86 if (cache && strcmp(cache, arg) == 0) {
91 rtnl_rtprot_initialize();
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];
103 res = bb_strtoul(arg, NULL, 0);
104 if (errno || res > 255)
111 static const char **rtnl_rtscope_tab; /* [256] */
113 static void rtnl_rtscope_initialize(void)
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);
127 const char* rtnl_rtscope_n2a(int id, char *buf, int len)
129 if (id < 0 || id >= 256) {
130 snprintf(buf, len, "%d", id);
134 rtnl_rtscope_initialize();
136 if (rtnl_rtscope_tab[id])
137 return rtnl_rtscope_tab[id];
138 snprintf(buf, len, "%d", id);
142 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
144 static const char *cache = NULL;
145 static unsigned long res;
148 if (cache && strcmp(cache, arg) == 0) {
153 rtnl_rtscope_initialize();
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];
165 res = bb_strtoul(arg, NULL, 0);
166 if (errno || res > 255)
173 static const char **rtnl_rtrealm_tab; /* [256] */
175 static void rtnl_rtrealm_initialize(void)
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);
185 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
187 static const char *cache = NULL;
188 static unsigned long res;
191 if (cache && strcmp(cache, arg) == 0) {
196 rtnl_rtrealm_initialize();
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];
208 res = bb_strtoul(arg, NULL, 0);
209 if (errno || res > 255)
215 #if ENABLE_FEATURE_IP_RULE
216 const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
218 if (id < 0 || id >= 256) {
219 snprintf(buf, len, "%d", id);
223 rtnl_rtrealm_initialize();
225 if (rtnl_rtrealm_tab[id])
226 return rtnl_rtrealm_tab[id];
227 snprintf(buf, len, "%d", id);
233 static const char **rtnl_rtdsfield_tab; /* [256] */
235 static void rtnl_rtdsfield_initialize(void)
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);
245 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
247 if (id < 0 || id >= 256) {
248 snprintf(buf, len, "%d", id);
252 rtnl_rtdsfield_initialize();
254 if (rtnl_rtdsfield_tab[id])
255 return rtnl_rtdsfield_tab[id];
256 snprintf(buf, len, "0x%02x", id);
261 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
263 static const char *cache = NULL;
264 static unsigned long res;
267 if (cache && strcmp(cache, arg) == 0) {
272 rtnl_rtdsfield_initialize();
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];
284 res = bb_strtoul(arg, NULL, 16);
285 if (errno || res > 255)
292 #if ENABLE_FEATURE_IP_RULE
293 static const char **rtnl_rttable_tab; /* [256] */
295 static void rtnl_rttable_initialize(void)
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);
307 const char *rtnl_rttable_n2a(int id, char *buf, int len)
309 if (id < 0 || id >= 256) {
310 snprintf(buf, len, "%d", id);
314 rtnl_rttable_initialize();
316 if (rtnl_rttable_tab[id])
317 return rtnl_rttable_tab[id];
318 snprintf(buf, len, "%d", id);
322 int rtnl_rttable_a2n(uint32_t * id, char *arg)
324 static char *cache = NULL;
325 static unsigned long res;
328 if (cache && strcmp(cache, arg) == 0) {
333 rtnl_rttable_initialize();
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];
344 i = bb_strtoul(arg, NULL, 0);
345 if (errno || i > 255)