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 static void rtnl_tab_initialize(char *file, const char **tab, int size)
21 fp = fopen(file, "r");
24 while (fgets(buf, sizeof(buf), fp)) {
29 while (*p == ' ' || *p == '\t')
31 if (*p == '#' || *p == '\n' || *p == 0)
33 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
34 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
35 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
36 sscanf(p, "%d %s #", &id, namebuf) != 2) {
37 bb_error_msg("database %s is corrupted at %s",
42 if (id < 0 || id > size)
45 tab[id] = xstrdup(namebuf);
51 static const char **rtnl_rtprot_tab; /* [256] */
53 static void rtnl_rtprot_initialize(void)
55 static const char *const init_tab[] = {
70 if (rtnl_rtprot_tab) return;
71 rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0]));
72 memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab));
73 rtnl_tab_initialize("/etc/iproute2/rt_protos",
74 rtnl_rtprot_tab, 256);
78 const char* rtnl_rtprot_n2a(int id, char *buf, int len)
80 if (id < 0 || id >= 256) {
81 snprintf(buf, len, "%d", id);
85 rtnl_rtprot_initialize();
87 if (rtnl_rtprot_tab[id])
88 return rtnl_rtprot_tab[id];
89 snprintf(buf, len, "%d", id);
93 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
95 static const char *cache = NULL;
96 static unsigned long res;
99 if (cache && strcmp(cache, arg) == 0) {
104 rtnl_rtprot_initialize();
106 for (i = 0; i < 256; i++) {
107 if (rtnl_rtprot_tab[i] &&
108 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
109 cache = rtnl_rtprot_tab[i];
116 res = bb_strtoul(arg, NULL, 0);
117 if (errno || res > 255)
124 static const char **rtnl_rtscope_tab; /* [256] */
126 static void rtnl_rtscope_initialize(void)
128 if (rtnl_rtscope_tab) return;
129 rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0]));
130 rtnl_rtscope_tab[0] = "global";
131 rtnl_rtscope_tab[255] = "nowhere";
132 rtnl_rtscope_tab[254] = "host";
133 rtnl_rtscope_tab[253] = "link";
134 rtnl_rtscope_tab[200] = "site";
135 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
136 rtnl_rtscope_tab, 256);
140 const char* rtnl_rtscope_n2a(int id, char *buf, int len)
142 if (id < 0 || id >= 256) {
143 snprintf(buf, len, "%d", id);
147 rtnl_rtscope_initialize();
149 if (rtnl_rtscope_tab[id])
150 return rtnl_rtscope_tab[id];
151 snprintf(buf, len, "%d", id);
155 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
157 static const char *cache = NULL;
158 static unsigned long res;
161 if (cache && strcmp(cache, arg) == 0) {
166 rtnl_rtscope_initialize();
168 for (i = 0; i < 256; i++) {
169 if (rtnl_rtscope_tab[i] &&
170 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
171 cache = rtnl_rtscope_tab[i];
178 res = bb_strtoul(arg, NULL, 0);
179 if (errno || res > 255)
186 static const char **rtnl_rtrealm_tab; /* [256] */
188 static void rtnl_rtrealm_initialize(void)
190 if (rtnl_rtrealm_tab) return;
191 rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0]));
192 rtnl_rtrealm_tab[0] = "unknown";
193 rtnl_tab_initialize("/etc/iproute2/rt_realms",
194 rtnl_rtrealm_tab, 256);
198 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
200 static const char *cache = NULL;
201 static unsigned long res;
204 if (cache && strcmp(cache, arg) == 0) {
209 rtnl_rtrealm_initialize();
211 for (i = 0; i < 256; i++) {
212 if (rtnl_rtrealm_tab[i] &&
213 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
214 cache = rtnl_rtrealm_tab[i];
221 res = bb_strtoul(arg, NULL, 0);
222 if (errno || res > 255)
228 #if ENABLE_FEATURE_IP_RULE
229 const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
231 if (id < 0 || id >= 256) {
232 snprintf(buf, len, "%d", id);
236 rtnl_rtrealm_initialize();
238 if (rtnl_rtrealm_tab[id])
239 return rtnl_rtrealm_tab[id];
240 snprintf(buf, len, "%d", id);
246 static const char **rtnl_rtdsfield_tab; /* [256] */
248 static void rtnl_rtdsfield_initialize(void)
250 if (rtnl_rtdsfield_tab) return;
251 rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0]));
252 rtnl_rtdsfield_tab[0] = "0";
253 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
254 rtnl_rtdsfield_tab, 256);
258 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
260 if (id < 0 || id >= 256) {
261 snprintf(buf, len, "%d", id);
265 rtnl_rtdsfield_initialize();
267 if (rtnl_rtdsfield_tab[id])
268 return rtnl_rtdsfield_tab[id];
269 snprintf(buf, len, "0x%02x", id);
274 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
276 static const char *cache = NULL;
277 static unsigned long res;
280 if (cache && strcmp(cache, arg) == 0) {
285 rtnl_rtdsfield_initialize();
287 for (i = 0; i < 256; i++) {
288 if (rtnl_rtdsfield_tab[i] &&
289 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
290 cache = rtnl_rtdsfield_tab[i];
297 res = bb_strtoul(arg, NULL, 16);
298 if (errno || res > 255)
305 #if ENABLE_FEATURE_IP_RULE
306 static const char **rtnl_rttable_tab; /* [256] */
308 static void rtnl_rttable_initialize(void)
310 if (rtnl_rtdsfield_tab) return;
311 rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0]));
312 rtnl_rttable_tab[0] = "unspec";
313 rtnl_rttable_tab[255] = "local";
314 rtnl_rttable_tab[254] = "main";
315 rtnl_rttable_tab[253] = "default";
316 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256);
320 const char *rtnl_rttable_n2a(int id, char *buf, int len)
322 if (id < 0 || id >= 256) {
323 snprintf(buf, len, "%d", id);
327 rtnl_rttable_initialize();
329 if (rtnl_rttable_tab[id])
330 return rtnl_rttable_tab[id];
331 snprintf(buf, len, "%d", id);
335 int rtnl_rttable_a2n(uint32_t * id, char *arg)
337 static char *cache = NULL;
338 static unsigned long res;
341 if (cache && strcmp(cache, arg) == 0) {
346 rtnl_rttable_initialize();
348 for (i = 0; i < 256; i++) {
349 if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
350 cache = (char*)rtnl_rttable_tab[i];
357 i = bb_strtoul(arg, NULL, 0);
358 if (errno || i > 255)