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(const char *file, const char **tab, int size)
21 fp = fopen_for_read(file);
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
38 bb_error_msg("database %s is corrupted at %s",
43 if (id < 0 || id > size)
46 tab[id] = xstrdup(namebuf);
52 static const char **rtnl_rtprot_tab; /* [256] */
54 static void rtnl_rtprot_initialize(void)
56 static const char *const init_tab[] = {
71 if (rtnl_rtprot_tab) return;
72 rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0]));
73 memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab));
74 rtnl_tab_initialize("/etc/iproute2/rt_protos",
75 rtnl_rtprot_tab, 256);
79 const char* rtnl_rtprot_n2a(int id, char *buf, int len)
81 if (id < 0 || id >= 256) {
82 snprintf(buf, len, "%d", id);
86 rtnl_rtprot_initialize();
88 if (rtnl_rtprot_tab[id])
89 return rtnl_rtprot_tab[id];
90 snprintf(buf, len, "%d", id);
94 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
96 static const char *cache = NULL;
97 static unsigned long res;
100 if (cache && strcmp(cache, arg) == 0) {
105 rtnl_rtprot_initialize();
107 for (i = 0; i < 256; i++) {
108 if (rtnl_rtprot_tab[i] &&
109 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
110 cache = rtnl_rtprot_tab[i];
117 res = bb_strtoul(arg, NULL, 0);
118 if (errno || res > 255)
125 static const char **rtnl_rtscope_tab; /* [256] */
127 static void rtnl_rtscope_initialize(void)
129 if (rtnl_rtscope_tab) return;
130 rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0]));
131 rtnl_rtscope_tab[0] = "global";
132 rtnl_rtscope_tab[255] = "nowhere";
133 rtnl_rtscope_tab[254] = "host";
134 rtnl_rtscope_tab[253] = "link";
135 rtnl_rtscope_tab[200] = "site";
136 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
137 rtnl_rtscope_tab, 256);
141 const char* rtnl_rtscope_n2a(int id, char *buf, int len)
143 if (id < 0 || id >= 256) {
144 snprintf(buf, len, "%d", id);
148 rtnl_rtscope_initialize();
150 if (rtnl_rtscope_tab[id])
151 return rtnl_rtscope_tab[id];
152 snprintf(buf, len, "%d", id);
156 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
158 static const char *cache = NULL;
159 static unsigned long res;
162 if (cache && strcmp(cache, arg) == 0) {
167 rtnl_rtscope_initialize();
169 for (i = 0; i < 256; i++) {
170 if (rtnl_rtscope_tab[i] &&
171 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
172 cache = rtnl_rtscope_tab[i];
179 res = bb_strtoul(arg, NULL, 0);
180 if (errno || res > 255)
187 static const char **rtnl_rtrealm_tab; /* [256] */
189 static void rtnl_rtrealm_initialize(void)
191 if (rtnl_rtrealm_tab) return;
192 rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0]));
193 rtnl_rtrealm_tab[0] = "unknown";
194 rtnl_tab_initialize("/etc/iproute2/rt_realms",
195 rtnl_rtrealm_tab, 256);
199 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
201 static const char *cache = NULL;
202 static unsigned long res;
205 if (cache && strcmp(cache, arg) == 0) {
210 rtnl_rtrealm_initialize();
212 for (i = 0; i < 256; i++) {
213 if (rtnl_rtrealm_tab[i] &&
214 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
215 cache = rtnl_rtrealm_tab[i];
222 res = bb_strtoul(arg, NULL, 0);
223 if (errno || res > 255)
229 #if ENABLE_FEATURE_IP_RULE
230 const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
232 if (id < 0 || id >= 256) {
233 snprintf(buf, len, "%d", id);
237 rtnl_rtrealm_initialize();
239 if (rtnl_rtrealm_tab[id])
240 return rtnl_rtrealm_tab[id];
241 snprintf(buf, len, "%d", id);
247 static const char **rtnl_rtdsfield_tab; /* [256] */
249 static void rtnl_rtdsfield_initialize(void)
251 if (rtnl_rtdsfield_tab) return;
252 rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0]));
253 rtnl_rtdsfield_tab[0] = "0";
254 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
255 rtnl_rtdsfield_tab, 256);
259 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
261 if (id < 0 || id >= 256) {
262 snprintf(buf, len, "%d", id);
266 rtnl_rtdsfield_initialize();
268 if (rtnl_rtdsfield_tab[id])
269 return rtnl_rtdsfield_tab[id];
270 snprintf(buf, len, "0x%02x", id);
275 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
277 static const char *cache = NULL;
278 static unsigned long res;
281 if (cache && strcmp(cache, arg) == 0) {
286 rtnl_rtdsfield_initialize();
288 for (i = 0; i < 256; i++) {
289 if (rtnl_rtdsfield_tab[i] &&
290 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
291 cache = rtnl_rtdsfield_tab[i];
298 res = bb_strtoul(arg, NULL, 16);
299 if (errno || res > 255)
306 #if ENABLE_FEATURE_IP_RULE
307 static const char **rtnl_rttable_tab; /* [256] */
309 static void rtnl_rttable_initialize(void)
311 if (rtnl_rtdsfield_tab) return;
312 rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0]));
313 rtnl_rttable_tab[0] = "unspec";
314 rtnl_rttable_tab[255] = "local";
315 rtnl_rttable_tab[254] = "main";
316 rtnl_rttable_tab[253] = "default";
317 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256);
321 const char *rtnl_rttable_n2a(int id, char *buf, int len)
323 if (id < 0 || id >= 256) {
324 snprintf(buf, len, "%d", id);
328 rtnl_rttable_initialize();
330 if (rtnl_rttable_tab[id])
331 return rtnl_rttable_tab[id];
332 snprintf(buf, len, "%d", id);
336 int rtnl_rttable_a2n(uint32_t * id, char *arg)
338 static char *cache = NULL;
339 static unsigned long res;
342 if (cache && strcmp(cache, arg) == 0) {
347 rtnl_rttable_initialize();
349 for (i = 0; i < 256; i++) {
350 if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
351 cache = (char*)rtnl_rttable_tab[i];
358 i = bb_strtoul(arg, NULL, 0);
359 if (errno || i > 255)