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",
74 [RTPROT_XORP] = "xorp",
80 static int rtnl_rtprot_init;
82 static void rtnl_rtprot_initialize(void)
85 rtnl_tab_initialize("/etc/iproute2/rt_protos",
86 rtnl_rtprot_tab, 256);
89 char * rtnl_rtprot_n2a(int id, char *buf, int len)
91 if (id<0 || id>=256) {
92 snprintf(buf, len, "%d", id);
95 if (!rtnl_rtprot_tab[id]) {
96 if (!rtnl_rtprot_init)
97 rtnl_rtprot_initialize();
99 if (rtnl_rtprot_tab[id])
100 return rtnl_rtprot_tab[id];
101 snprintf(buf, len, "%d", id);
105 int rtnl_rtprot_a2n(__u32 *id, char *arg)
107 static char *cache = NULL;
108 static unsigned long res;
112 if (cache && strcmp(cache, arg) == 0) {
117 if (!rtnl_rtprot_init)
118 rtnl_rtprot_initialize();
120 for (i=0; i<256; i++) {
121 if (rtnl_rtprot_tab[i] &&
122 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
123 cache = rtnl_rtprot_tab[i];
130 res = strtoul(arg, &end, 0);
131 if (!end || end == arg || *end || res > 255)
139 static char * rtnl_rtscope_tab[256] = {
143 static int rtnl_rtscope_init;
145 static void rtnl_rtscope_initialize(void)
147 rtnl_rtscope_init = 1;
148 rtnl_rtscope_tab[255] = "nowhere";
149 rtnl_rtscope_tab[254] = "host";
150 rtnl_rtscope_tab[253] = "link";
151 rtnl_rtscope_tab[200] = "site";
152 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
153 rtnl_rtscope_tab, 256);
156 char * rtnl_rtscope_n2a(int id, char *buf, int len)
158 if (id<0 || id>=256) {
159 snprintf(buf, len, "%d", id);
162 if (!rtnl_rtscope_tab[id]) {
163 if (!rtnl_rtscope_init)
164 rtnl_rtscope_initialize();
166 if (rtnl_rtscope_tab[id])
167 return rtnl_rtscope_tab[id];
168 snprintf(buf, len, "%d", id);
172 int rtnl_rtscope_a2n(__u32 *id, char *arg)
174 static char *cache = NULL;
175 static unsigned long res;
179 if (cache && strcmp(cache, arg) == 0) {
184 if (!rtnl_rtscope_init)
185 rtnl_rtscope_initialize();
187 for (i=0; i<256; i++) {
188 if (rtnl_rtscope_tab[i] &&
189 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
190 cache = rtnl_rtscope_tab[i];
197 res = strtoul(arg, &end, 0);
198 if (!end || end == arg || *end || res > 255)
206 static char * rtnl_rtrealm_tab[256] = {
210 static int rtnl_rtrealm_init;
212 static void rtnl_rtrealm_initialize(void)
214 rtnl_rtrealm_init = 1;
215 rtnl_tab_initialize("/etc/iproute2/rt_realms",
216 rtnl_rtrealm_tab, 256);
219 char * rtnl_rtrealm_n2a(int id, char *buf, int len)
221 if (id<0 || id>=256) {
222 snprintf(buf, len, "%d", id);
225 if (!rtnl_rtrealm_tab[id]) {
226 if (!rtnl_rtrealm_init)
227 rtnl_rtrealm_initialize();
229 if (rtnl_rtrealm_tab[id])
230 return rtnl_rtrealm_tab[id];
231 snprintf(buf, len, "%d", id);
236 int rtnl_rtrealm_a2n(__u32 *id, char *arg)
238 static char *cache = NULL;
239 static unsigned long res;
243 if (cache && strcmp(cache, arg) == 0) {
248 if (!rtnl_rtrealm_init)
249 rtnl_rtrealm_initialize();
251 for (i=0; i<256; i++) {
252 if (rtnl_rtrealm_tab[i] &&
253 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
254 cache = rtnl_rtrealm_tab[i];
261 res = strtoul(arg, &end, 0);
262 if (!end || end == arg || *end || res > 255)
270 static char * rtnl_rttable_tab[256] = {
274 static int rtnl_rttable_init;
276 static void rtnl_rttable_initialize(void)
278 rtnl_rttable_init = 1;
279 rtnl_rttable_tab[255] = "local";
280 rtnl_rttable_tab[254] = "main";
281 rtnl_rttable_tab[253] = "default";
282 rtnl_tab_initialize("/etc/iproute2/rt_tables",
283 rtnl_rttable_tab, 256);
286 char * rtnl_rttable_n2a(int id, char *buf, int len)
288 if (id<0 || id>=256) {
289 snprintf(buf, len, "%d", id);
292 if (!rtnl_rttable_tab[id]) {
293 if (!rtnl_rttable_init)
294 rtnl_rttable_initialize();
296 if (rtnl_rttable_tab[id])
297 return rtnl_rttable_tab[id];
298 snprintf(buf, len, "%d", id);
302 int rtnl_rttable_a2n(__u32 *id, char *arg)
304 static char *cache = NULL;
305 static unsigned long res;
309 if (cache && strcmp(cache, arg) == 0) {
314 if (!rtnl_rttable_init)
315 rtnl_rttable_initialize();
317 for (i=0; i<256; i++) {
318 if (rtnl_rttable_tab[i] &&
319 strcmp(rtnl_rttable_tab[i], arg) == 0) {
320 cache = rtnl_rttable_tab[i];
327 i = strtoul(arg, &end, 0);
328 if (!end || end == arg || *end || i > 255)
335 static char * rtnl_rtdsfield_tab[256] = {
339 static int rtnl_rtdsfield_init;
341 static void rtnl_rtdsfield_initialize(void)
343 rtnl_rtdsfield_init = 1;
344 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
345 rtnl_rtdsfield_tab, 256);
348 char * rtnl_dsfield_n2a(int id, char *buf, int len)
350 if (id<0 || id>=256) {
351 snprintf(buf, len, "%d", id);
354 if (!rtnl_rtdsfield_tab[id]) {
355 if (!rtnl_rtdsfield_init)
356 rtnl_rtdsfield_initialize();
358 if (rtnl_rtdsfield_tab[id])
359 return rtnl_rtdsfield_tab[id];
360 snprintf(buf, len, "0x%02x", id);
365 int rtnl_dsfield_a2n(__u32 *id, char *arg)
367 static char *cache = NULL;
368 static unsigned long res;
372 if (cache && strcmp(cache, arg) == 0) {
377 if (!rtnl_rtdsfield_init)
378 rtnl_rtdsfield_initialize();
380 for (i=0; i<256; i++) {
381 if (rtnl_rtdsfield_tab[i] &&
382 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
383 cache = rtnl_rtdsfield_tab[i];
390 res = strtoul(arg, &end, 16);
391 if (!end || end == arg || *end || res > 255)