1 /* vi: set sw=4 ts=4: */
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 #define CONFDIR CONFIG_FEATURE_IP_ROUTE_DIR
15 typedef struct rtnl_tab_t {
16 const char *cached_str;
17 unsigned cached_result;
18 /* upstream version switched to a hash table and removed
19 * id < 256 limit. For now bbox bumps this array size from 256
20 * to 1024. If you plan to change this to a hash table,
21 * consider merging several hash tables we have (for example,
22 * awk has resizable one!
24 #define RT_TABLE_MAX 1023
25 const char *tab[RT_TABLE_MAX+1];
28 static void rtnl_tab_initialize(const char *file, const char **tab)
31 char fullname[sizeof(CONFDIR"/rt_dsfield") + 8];
34 sprintf(fullname, CONFDIR"/rt_%s", file);
35 parser = config_open2(fullname, fopen_for_read);
36 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
37 unsigned id = bb_strtou(token[0], NULL, 0);
38 if (id > RT_TABLE_MAX) {
39 bb_error_msg("database %s is corrupted at line %d",
40 file, parser->lineno);
43 tab[id] = xstrdup(token[1]);
48 static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
52 if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
53 *id = tab->cached_result;
57 for (i = 0; i <= RT_TABLE_MAX; i++) {
59 && strcmp(tab->tab[i], arg) == 0
61 tab->cached_str = tab->tab[i];
62 tab->cached_result = i;
68 i = bb_strtou(arg, NULL, base);
76 static rtnl_tab_t *rtnl_rtprot_tab;
78 static void rtnl_rtprot_initialize(void)
80 static const char *const init_tab[] = {
98 rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
99 memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
100 rtnl_tab_initialize("protos", rtnl_rtprot_tab->tab);
104 const char* FAST_FUNC rtnl_rtprot_n2a(int id)
106 if (id < 0 || id > RT_TABLE_MAX) {
110 rtnl_rtprot_initialize();
112 if (rtnl_rtprot_tab->tab[id])
113 return rtnl_rtprot_tab->tab[id];
118 int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg)
120 rtnl_rtprot_initialize();
121 return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
125 static rtnl_tab_t *rtnl_rtscope_tab;
127 static void rtnl_rtscope_initialize(void)
129 if (rtnl_rtscope_tab)
131 rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
132 rtnl_rtscope_tab->tab[0] = "global";
133 rtnl_rtscope_tab->tab[255] = "nowhere";
134 rtnl_rtscope_tab->tab[254] = "host";
135 rtnl_rtscope_tab->tab[253] = "link";
136 rtnl_rtscope_tab->tab[200] = "site";
137 rtnl_tab_initialize("scopes", rtnl_rtscope_tab->tab);
140 const char* FAST_FUNC rtnl_rtscope_n2a(int id)
142 if (id < 0 || id > RT_TABLE_MAX) {
146 rtnl_rtscope_initialize();
148 if (rtnl_rtscope_tab->tab[id])
149 return rtnl_rtscope_tab->tab[id];
153 int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg)
155 rtnl_rtscope_initialize();
156 return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
160 static rtnl_tab_t *rtnl_rtrealm_tab;
162 static void rtnl_rtrealm_initialize(void)
164 if (rtnl_rtrealm_tab) return;
165 rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
166 rtnl_rtrealm_tab->tab[0] = "unknown";
167 rtnl_tab_initialize("realms", rtnl_rtrealm_tab->tab);
170 int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
172 rtnl_rtrealm_initialize();
173 return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
176 #if ENABLE_FEATURE_IP_RULE
177 const char* FAST_FUNC rtnl_rtrealm_n2a(int id)
179 if (id < 0 || id > RT_TABLE_MAX) {
183 rtnl_rtrealm_initialize();
185 if (rtnl_rtrealm_tab->tab[id])
186 return rtnl_rtrealm_tab->tab[id];
192 static rtnl_tab_t *rtnl_rtdsfield_tab;
194 static void rtnl_rtdsfield_initialize(void)
196 if (rtnl_rtdsfield_tab) return;
197 rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
198 rtnl_rtdsfield_tab->tab[0] = "0";
199 rtnl_tab_initialize("dsfield", rtnl_rtdsfield_tab->tab);
202 const char* FAST_FUNC rtnl_dsfield_n2a(int id)
204 if (id < 0 || id > RT_TABLE_MAX) {
208 rtnl_rtdsfield_initialize();
210 if (rtnl_rtdsfield_tab->tab[id])
211 return rtnl_rtdsfield_tab->tab[id];
215 int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg)
217 rtnl_rtdsfield_initialize();
218 return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
222 #if ENABLE_FEATURE_IP_RULE
223 static rtnl_tab_t *rtnl_rttable_tab;
225 static void rtnl_rttable_initialize(void)
227 if (rtnl_rttable_tab)
230 rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
231 rtnl_rttable_tab->tab[0] = "unspec";
232 rtnl_rttable_tab->tab[255] = "local";
233 rtnl_rttable_tab->tab[254] = "main";
234 rtnl_rttable_tab->tab[253] = "default";
235 rtnl_tab_initialize("tables", rtnl_rttable_tab->tab);
238 const char* FAST_FUNC rtnl_rttable_n2a(int id)
240 if (id < 0 || id > RT_TABLE_MAX) {
244 rtnl_rttable_initialize();
246 if (rtnl_rttable_tab->tab[id])
247 return rtnl_rttable_tab->tab[id];
251 int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg)
253 rtnl_rttable_initialize();
254 return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);