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>
15 typedef struct rtnl_tab_t {
16 const char *cached_str;
17 unsigned cached_result;
21 static void rtnl_tab_initialize(const char *file, const char **tab)
24 parser_t *parser = config_open2(file, fopen_for_read);
26 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
27 unsigned id = bb_strtou(token[0], NULL, 0);
29 bb_error_msg("database %s is corrupted at line %d",
30 file, parser->lineno);
33 tab[id] = xstrdup(token[1]);
38 static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
42 if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
43 *id = tab->cached_result;
47 for (i = 0; i < 256; i++) {
49 && strcmp(tab->tab[i], arg) == 0
51 tab->cached_str = tab->tab[i];
52 tab->cached_result = i;
58 i = bb_strtou(arg, NULL, base);
66 static rtnl_tab_t *rtnl_rtprot_tab;
68 static void rtnl_rtprot_initialize(void)
70 static const char *const init_tab[] = {
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);
93 const char* rtnl_rtprot_n2a(int id, char *buf, int len)
95 if (id < 0 || id >= 256) {
96 snprintf(buf, len, "%d", id);
100 rtnl_rtprot_initialize();
102 if (rtnl_rtprot_tab->tab[id])
103 return rtnl_rtprot_tab->tab[id];
104 snprintf(buf, len, "%d", id);
108 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
110 rtnl_rtprot_initialize();
111 return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
115 static rtnl_tab_t *rtnl_rtscope_tab;
117 static void rtnl_rtscope_initialize(void)
119 if (rtnl_rtscope_tab)
121 rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
122 rtnl_rtscope_tab->tab[0] = "global";
123 rtnl_rtscope_tab->tab[255] = "nowhere";
124 rtnl_rtscope_tab->tab[254] = "host";
125 rtnl_rtscope_tab->tab[253] = "link";
126 rtnl_rtscope_tab->tab[200] = "site";
127 rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab);
130 const char* rtnl_rtscope_n2a(int id, char *buf, int len)
132 if (id < 0 || id >= 256) {
133 snprintf(buf, len, "%d", id);
137 rtnl_rtscope_initialize();
139 if (rtnl_rtscope_tab->tab[id])
140 return rtnl_rtscope_tab->tab[id];
141 snprintf(buf, len, "%d", id);
145 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
147 rtnl_rtscope_initialize();
148 return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
152 static rtnl_tab_t *rtnl_rtrealm_tab;
154 static void rtnl_rtrealm_initialize(void)
156 if (rtnl_rtrealm_tab) return;
157 rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
158 rtnl_rtrealm_tab->tab[0] = "unknown";
159 rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab);
162 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
164 rtnl_rtrealm_initialize();
165 return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
168 #if ENABLE_FEATURE_IP_RULE
169 const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
171 if (id < 0 || id >= 256) {
172 snprintf(buf, len, "%d", id);
176 rtnl_rtrealm_initialize();
178 if (rtnl_rtrealm_tab->tab[id])
179 return rtnl_rtrealm_tab->tab[id];
180 snprintf(buf, len, "%d", id);
186 static rtnl_tab_t *rtnl_rtdsfield_tab;
188 static void rtnl_rtdsfield_initialize(void)
190 if (rtnl_rtdsfield_tab) return;
191 rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
192 rtnl_rtdsfield_tab->tab[0] = "0";
193 rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab);
196 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
198 if (id < 0 || id >= 256) {
199 snprintf(buf, len, "%d", id);
203 rtnl_rtdsfield_initialize();
205 if (rtnl_rtdsfield_tab->tab[id])
206 return rtnl_rtdsfield_tab->tab[id];
207 snprintf(buf, len, "0x%02x", id);
211 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
213 rtnl_rtdsfield_initialize();
214 return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
218 #if ENABLE_FEATURE_IP_RULE
219 static rtnl_tab_t *rtnl_rttable_tab;
221 static void rtnl_rttable_initialize(void)
223 if (rtnl_rtdsfield_tab) return;
224 rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
225 rtnl_rttable_tab->tab[0] = "unspec";
226 rtnl_rttable_tab->tab[255] = "local";
227 rtnl_rttable_tab->tab[254] = "main";
228 rtnl_rttable_tab->tab[253] = "default";
229 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab);
232 const char *rtnl_rttable_n2a(int id, char *buf, int len)
234 if (id < 0 || id >= 256) {
235 snprintf(buf, len, "%d", id);
239 rtnl_rttable_initialize();
241 if (rtnl_rttable_tab->tab[id])
242 return rtnl_rttable_tab->tab[id];
243 snprintf(buf, len, "%d", id);
247 int rtnl_rttable_a2n(uint32_t *id, char *arg)
249 rtnl_rttable_initialize();
250 return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);