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 typedef struct rtnl_tab_t {
14 const char *cached_str;
15 unsigned cached_result;
16 /* upstream version switched to a hash table and removed
17 * id < 256 limit. For now bbox bumps this array size from 256
18 * to 1024. If you plan to change this to a hash table,
19 * consider merging several hash tables we have (for example,
20 * awk has resizable one!
22 #define RT_TABLE_MAX 1023
23 const char *tab[RT_TABLE_MAX+1];
26 static void rtnl_tab_initialize(const char *file, const char **tab)
29 parser_t *parser = config_open2(file, fopen_for_read);
31 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
32 unsigned id = bb_strtou(token[0], NULL, 0);
33 if (id > RT_TABLE_MAX) {
34 bb_error_msg("database %s is corrupted at line %d",
35 file, parser->lineno);
38 tab[id] = xstrdup(token[1]);
43 static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
47 if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
48 *id = tab->cached_result;
52 for (i = 0; i <= RT_TABLE_MAX; i++) {
54 && strcmp(tab->tab[i], arg) == 0
56 tab->cached_str = tab->tab[i];
57 tab->cached_result = i;
63 i = bb_strtou(arg, NULL, base);
71 static rtnl_tab_t *rtnl_rtprot_tab;
73 static void rtnl_rtprot_initialize(void)
75 static const char *const init_tab[] = {
93 rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
94 memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
95 rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab);
99 const char* FAST_FUNC rtnl_rtprot_n2a(int id)
101 if (id < 0 || id > RT_TABLE_MAX) {
105 rtnl_rtprot_initialize();
107 if (rtnl_rtprot_tab->tab[id])
108 return rtnl_rtprot_tab->tab[id];
113 int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg)
115 rtnl_rtprot_initialize();
116 return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
120 static rtnl_tab_t *rtnl_rtscope_tab;
122 static void rtnl_rtscope_initialize(void)
124 if (rtnl_rtscope_tab)
126 rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
127 rtnl_rtscope_tab->tab[0] = "global";
128 rtnl_rtscope_tab->tab[255] = "nowhere";
129 rtnl_rtscope_tab->tab[254] = "host";
130 rtnl_rtscope_tab->tab[253] = "link";
131 rtnl_rtscope_tab->tab[200] = "site";
132 rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab);
135 const char* FAST_FUNC rtnl_rtscope_n2a(int id)
137 if (id < 0 || id > RT_TABLE_MAX) {
141 rtnl_rtscope_initialize();
143 if (rtnl_rtscope_tab->tab[id])
144 return rtnl_rtscope_tab->tab[id];
148 int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg)
150 rtnl_rtscope_initialize();
151 return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
155 static rtnl_tab_t *rtnl_rtrealm_tab;
157 static void rtnl_rtrealm_initialize(void)
159 if (rtnl_rtrealm_tab) return;
160 rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
161 rtnl_rtrealm_tab->tab[0] = "unknown";
162 rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab);
165 int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
167 rtnl_rtrealm_initialize();
168 return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
171 #if ENABLE_FEATURE_IP_RULE
172 const char* FAST_FUNC rtnl_rtrealm_n2a(int id)
174 if (id < 0 || id > RT_TABLE_MAX) {
178 rtnl_rtrealm_initialize();
180 if (rtnl_rtrealm_tab->tab[id])
181 return rtnl_rtrealm_tab->tab[id];
187 static rtnl_tab_t *rtnl_rtdsfield_tab;
189 static void rtnl_rtdsfield_initialize(void)
191 if (rtnl_rtdsfield_tab) return;
192 rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
193 rtnl_rtdsfield_tab->tab[0] = "0";
194 rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab);
197 const char* FAST_FUNC rtnl_dsfield_n2a(int id)
199 if (id < 0 || id > RT_TABLE_MAX) {
203 rtnl_rtdsfield_initialize();
205 if (rtnl_rtdsfield_tab->tab[id])
206 return rtnl_rtdsfield_tab->tab[id];
210 int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg)
212 rtnl_rtdsfield_initialize();
213 return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
217 #if ENABLE_FEATURE_IP_RULE
218 static rtnl_tab_t *rtnl_rttable_tab;
220 static void rtnl_rttable_initialize(void)
222 if (rtnl_rttable_tab)
225 rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
226 rtnl_rttable_tab->tab[0] = "unspec";
227 rtnl_rttable_tab->tab[255] = "local";
228 rtnl_rttable_tab->tab[254] = "main";
229 rtnl_rttable_tab->tab[253] = "default";
230 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab);
233 const char* FAST_FUNC rtnl_rttable_n2a(int id)
235 if (id < 0 || id > RT_TABLE_MAX) {
239 rtnl_rttable_initialize();
241 if (rtnl_rttable_tab->tab[id])
242 return rtnl_rttable_tab->tab[id];
246 int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg)
248 rtnl_rttable_initialize();
249 return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);