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
;
19 static void rtnl_tab_initialize(const char *file
, const char **tab
)
22 parser_t
*parser
= config_open2(file
, fopen_for_read
);
24 while (config_read(parser
, token
, 2, 2, "# \t", PARSE_NORMAL
)) {
25 unsigned id
= bb_strtou(token
[0], NULL
, 0);
27 bb_error_msg("database %s is corrupted at line %d",
28 file
, parser
->lineno
);
31 tab
[id
] = xstrdup(token
[1]);
36 static int rtnl_a2n(rtnl_tab_t
*tab
, uint32_t *id
, const char *arg
, int base
)
40 if (tab
->cached_str
&& strcmp(tab
->cached_str
, arg
) == 0) {
41 *id
= tab
->cached_result
;
45 for (i
= 0; i
< 256; i
++) {
47 && strcmp(tab
->tab
[i
], arg
) == 0
49 tab
->cached_str
= tab
->tab
[i
];
50 tab
->cached_result
= i
;
56 i
= bb_strtou(arg
, NULL
, base
);
64 static rtnl_tab_t
*rtnl_rtprot_tab
;
66 static void rtnl_rtprot_initialize(void)
68 static const char *const init_tab
[] = {
86 rtnl_rtprot_tab
= xzalloc(sizeof(*rtnl_rtprot_tab
));
87 memcpy(rtnl_rtprot_tab
->tab
, init_tab
, sizeof(init_tab
));
88 rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab
->tab
);
91 const char* FAST_FUNC
rtnl_rtprot_n2a(int id
, char *buf
)
93 if (id
< 0 || id
>= 256) {
94 sprintf(buf
, "%d", id
);
98 rtnl_rtprot_initialize();
100 if (rtnl_rtprot_tab
->tab
[id
])
101 return rtnl_rtprot_tab
->tab
[id
];
102 /* buf is SPRINT_BSIZE big */
103 sprintf(buf
, "%d", id
);
107 int FAST_FUNC
rtnl_rtprot_a2n(uint32_t *id
, char *arg
)
109 rtnl_rtprot_initialize();
110 return rtnl_a2n(rtnl_rtprot_tab
, id
, arg
, 0);
114 static rtnl_tab_t
*rtnl_rtscope_tab
;
116 static void rtnl_rtscope_initialize(void)
118 if (rtnl_rtscope_tab
)
120 rtnl_rtscope_tab
= xzalloc(sizeof(*rtnl_rtscope_tab
));
121 rtnl_rtscope_tab
->tab
[0] = "global";
122 rtnl_rtscope_tab
->tab
[255] = "nowhere";
123 rtnl_rtscope_tab
->tab
[254] = "host";
124 rtnl_rtscope_tab
->tab
[253] = "link";
125 rtnl_rtscope_tab
->tab
[200] = "site";
126 rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab
->tab
);
129 const char* FAST_FUNC
rtnl_rtscope_n2a(int id
, char *buf
)
131 if (id
< 0 || id
>= 256) {
132 sprintf(buf
, "%d", id
);
136 rtnl_rtscope_initialize();
138 if (rtnl_rtscope_tab
->tab
[id
])
139 return rtnl_rtscope_tab
->tab
[id
];
140 /* buf is SPRINT_BSIZE big */
141 sprintf(buf
, "%d", id
);
145 int FAST_FUNC
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 FAST_FUNC
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* FAST_FUNC
rtnl_rtrealm_n2a(int id
, char *buf
)
171 if (id
< 0 || id
>= 256) {
172 sprintf(buf
, "%d", id
);
176 rtnl_rtrealm_initialize();
178 if (rtnl_rtrealm_tab
->tab
[id
])
179 return rtnl_rtrealm_tab
->tab
[id
];
180 /* buf is SPRINT_BSIZE big */
181 sprintf(buf
, "%d", 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
, char *buf
)
199 if (id
< 0 || id
>= 256) {
200 sprintf(buf
, "%d", id
);
204 rtnl_rtdsfield_initialize();
206 if (rtnl_rtdsfield_tab
->tab
[id
])
207 return rtnl_rtdsfield_tab
->tab
[id
];
208 /* buf is SPRINT_BSIZE big */
209 sprintf(buf
, "0x%02x", id
);
213 int FAST_FUNC
rtnl_dsfield_a2n(uint32_t *id
, char *arg
)
215 rtnl_rtdsfield_initialize();
216 return rtnl_a2n(rtnl_rtdsfield_tab
, id
, arg
, 16);
220 #if ENABLE_FEATURE_IP_RULE
221 static rtnl_tab_t
*rtnl_rttable_tab
;
223 static void rtnl_rttable_initialize(void)
225 if (rtnl_rtdsfield_tab
) return;
226 rtnl_rttable_tab
= xzalloc(sizeof(*rtnl_rttable_tab
));
227 rtnl_rttable_tab
->tab
[0] = "unspec";
228 rtnl_rttable_tab
->tab
[255] = "local";
229 rtnl_rttable_tab
->tab
[254] = "main";
230 rtnl_rttable_tab
->tab
[253] = "default";
231 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab
->tab
);
234 const char* FAST_FUNC
rtnl_rttable_n2a(int id
, char *buf
)
236 if (id
< 0 || id
>= 256) {
237 sprintf(buf
, "%d", id
);
241 rtnl_rttable_initialize();
243 if (rtnl_rttable_tab
->tab
[id
])
244 return rtnl_rttable_tab
->tab
[id
];
245 /* buf is SPRINT_BSIZE big */
246 sprintf(buf
, "%d", id
);
250 int FAST_FUNC
rtnl_rttable_a2n(uint32_t *id
, char *arg
)
252 rtnl_rttable_initialize();
253 return rtnl_a2n(rtnl_rttable_tab
, id
, arg
, 0);