2 * rt_names.c rtnetlink names DB.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 #include <sys/socket.h>
21 #include <asm/types.h>
22 #include <linux/rtnetlink.h>
26 static void rtnl_tab_initialize(char *file, char **tab, int size)
31 fp = fopen(file, "r");
34 while (fgets(buf, sizeof(buf), fp)) {
39 while (*p == ' ' || *p == '\t')
41 if (*p == '#' || *p == '\n' || *p == 0)
43 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
44 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
45 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
46 sscanf(p, "%d %s #", &id, namebuf) != 2) {
47 fprintf(stderr, "Database %s is corrupted at %s\n",
55 tab[id] = strdup(namebuf);
61 static char * rtnl_rtprot_tab[256] = {
62 [RTPROT_UNSPEC] = "none",
63 [RTPROT_REDIRECT] ="redirect",
64 [RTPROT_KERNEL] = "kernel",
65 [RTPROT_BOOT] = "boot",
66 [RTPROT_STATIC] = "static",
68 [RTPROT_GATED] = "gated",
71 [RTPROT_ZEBRA] ="zebra",
72 [RTPROT_BIRD] = "bird",
73 [RTPROT_DNROUTED] = "dnrouted",
75 [RTPROT_XORP] = "xorp",
81 static int rtnl_rtprot_init;
83 static void rtnl_rtprot_initialize(void)
86 rtnl_tab_initialize("/etc/iproute2/rt_protos",
87 rtnl_rtprot_tab, 256);
90 char * rtnl_rtprot_n2a(int id, char *buf, int len)
92 if (id<0 || id>=256) {
93 snprintf(buf, len, "%d", id);
96 if (!rtnl_rtprot_tab[id]) {
97 if (!rtnl_rtprot_init)
98 rtnl_rtprot_initialize();
100 if (rtnl_rtprot_tab[id])
101 return rtnl_rtprot_tab[id];
102 snprintf(buf, len, "%d", id);
106 int rtnl_rtprot_a2n(__u32 *id, char *arg)
108 static char *cache = NULL;
109 static unsigned long res;
113 if (cache && strcmp(cache, arg) == 0) {
118 if (!rtnl_rtprot_init)
119 rtnl_rtprot_initialize();
121 for (i=0; i<256; i++) {
122 if (rtnl_rtprot_tab[i] &&
123 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
124 cache = rtnl_rtprot_tab[i];
131 res = strtoul(arg, &end, 0);
132 if (!end || end == arg || *end || res > 255)
140 static char * rtnl_rtscope_tab[256] = {
144 static int rtnl_rtscope_init;
146 static void rtnl_rtscope_initialize(void)
148 rtnl_rtscope_init = 1;
149 rtnl_rtscope_tab[255] = "nowhere";
150 rtnl_rtscope_tab[254] = "host";
151 rtnl_rtscope_tab[253] = "link";
152 rtnl_rtscope_tab[200] = "site";
153 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
154 rtnl_rtscope_tab, 256);
157 char * rtnl_rtscope_n2a(int id, char *buf, int len)
159 if (id<0 || id>=256) {
160 snprintf(buf, len, "%d", id);
163 if (!rtnl_rtscope_tab[id]) {
164 if (!rtnl_rtscope_init)
165 rtnl_rtscope_initialize();
167 if (rtnl_rtscope_tab[id])
168 return rtnl_rtscope_tab[id];
169 snprintf(buf, len, "%d", id);
173 int rtnl_rtscope_a2n(__u32 *id, char *arg)
175 static char *cache = NULL;
176 static unsigned long res;
180 if (cache && strcmp(cache, arg) == 0) {
185 if (!rtnl_rtscope_init)
186 rtnl_rtscope_initialize();
188 for (i=0; i<256; i++) {
189 if (rtnl_rtscope_tab[i] &&
190 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
191 cache = rtnl_rtscope_tab[i];
198 res = strtoul(arg, &end, 0);
199 if (!end || end == arg || *end || res > 255)
207 static char * rtnl_rtrealm_tab[256] = {
211 static int rtnl_rtrealm_init;
213 static void rtnl_rtrealm_initialize(void)
215 rtnl_rtrealm_init = 1;
216 rtnl_tab_initialize("/etc/iproute2/rt_realms",
217 rtnl_rtrealm_tab, 256);
220 char * rtnl_rtrealm_n2a(int id, char *buf, int len)
222 if (id<0 || id>=256) {
223 snprintf(buf, len, "%d", id);
226 if (!rtnl_rtrealm_tab[id]) {
227 if (!rtnl_rtrealm_init)
228 rtnl_rtrealm_initialize();
230 if (rtnl_rtrealm_tab[id])
231 return rtnl_rtrealm_tab[id];
232 snprintf(buf, len, "%d", id);
237 int rtnl_rtrealm_a2n(__u32 *id, char *arg)
239 static char *cache = NULL;
240 static unsigned long res;
244 if (cache && strcmp(cache, arg) == 0) {
249 if (!rtnl_rtrealm_init)
250 rtnl_rtrealm_initialize();
252 for (i=0; i<256; i++) {
253 if (rtnl_rtrealm_tab[i] &&
254 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
255 cache = rtnl_rtrealm_tab[i];
262 res = strtoul(arg, &end, 0);
263 if (!end || end == arg || *end || res > 255)
271 static char * rtnl_rttable_tab[256] = {
275 static int rtnl_rttable_init;
277 static void rtnl_rttable_initialize(void)
279 rtnl_rttable_init = 1;
280 rtnl_rttable_tab[255] = "local";
281 rtnl_rttable_tab[254] = "main";
282 rtnl_rttable_tab[253] = "default";
283 rtnl_tab_initialize("/etc/iproute2/rt_tables",
284 rtnl_rttable_tab, 256);
287 char * rtnl_rttable_n2a(int id, char *buf, int len)
289 if (id<0 || id>=256) {
290 snprintf(buf, len, "%d", id);
293 if (!rtnl_rttable_tab[id]) {
294 if (!rtnl_rttable_init)
295 rtnl_rttable_initialize();
297 if (rtnl_rttable_tab[id])
298 return rtnl_rttable_tab[id];
299 snprintf(buf, len, "%d", id);
303 int rtnl_rttable_a2n(__u32 *id, char *arg)
305 static char *cache = NULL;
306 static unsigned long res;
310 if (cache && strcmp(cache, arg) == 0) {
315 if (!rtnl_rttable_init)
316 rtnl_rttable_initialize();
318 for (i=0; i<256; i++) {
319 if (rtnl_rttable_tab[i] &&
320 strcmp(rtnl_rttable_tab[i], arg) == 0) {
321 cache = rtnl_rttable_tab[i];
328 i = strtoul(arg, &end, 0);
329 if (!end || end == arg || *end || i > 255)
336 static char * rtnl_rtdsfield_tab[256] = {
340 static int rtnl_rtdsfield_init;
342 static void rtnl_rtdsfield_initialize(void)
344 rtnl_rtdsfield_init = 1;
345 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
346 rtnl_rtdsfield_tab, 256);
349 char * rtnl_dsfield_n2a(int id, char *buf, int len)
351 if (id<0 || id>=256) {
352 snprintf(buf, len, "%d", id);
355 if (!rtnl_rtdsfield_tab[id]) {
356 if (!rtnl_rtdsfield_init)
357 rtnl_rtdsfield_initialize();
359 if (rtnl_rtdsfield_tab[id])
360 return rtnl_rtdsfield_tab[id];
361 snprintf(buf, len, "0x%02x", id);
366 int rtnl_dsfield_a2n(__u32 *id, char *arg)
368 static char *cache = NULL;
369 static unsigned long res;
373 if (cache && strcmp(cache, arg) == 0) {
378 if (!rtnl_rtdsfield_init)
379 rtnl_rtdsfield_initialize();
381 for (i=0; i<256; i++) {
382 if (rtnl_rtdsfield_tab[i] &&
383 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
384 cache = rtnl_rtdsfield_tab[i];
391 res = strtoul(arg, &end, 16);
392 if (!end || end == arg || *end || res > 255)