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>
27 #define CONFDIR "/etc/iproute2"
30 struct rtnl_hash_entry {
31 struct rtnl_hash_entry *next;
37 rtnl_hash_initialize(char *file, struct rtnl_hash_entry **hash, int size)
39 struct rtnl_hash_entry *entry;
43 fp = fopen(file, "r");
46 while (fgets(buf, sizeof(buf), fp)) {
51 while (*p == ' ' || *p == '\t')
53 if (*p == '#' || *p == '\n' || *p == 0)
55 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
56 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
57 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
58 sscanf(p, "%d %s #", &id, namebuf) != 2) {
59 fprintf(stderr, "Database %s is corrupted at %s\n",
67 entry = malloc(sizeof(*entry));
69 entry->name = strdup(namebuf);
70 entry->next = hash[id & (size - 1)];
71 hash[id & (size - 1)] = entry;
76 static void rtnl_tab_initialize(char *file, char **tab, int size)
81 fp = fopen(file, "r");
84 while (fgets(buf, sizeof(buf), fp)) {
89 while (*p == ' ' || *p == '\t')
91 if (*p == '#' || *p == '\n' || *p == 0)
93 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
94 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
95 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
96 sscanf(p, "%d %s #", &id, namebuf) != 2) {
97 fprintf(stderr, "Database %s is corrupted at %s\n",
106 tab[id] = strdup(namebuf);
111 static char * rtnl_rtprot_tab[256] = {
112 [RTPROT_UNSPEC] = "none",
113 [RTPROT_REDIRECT] ="redirect",
114 [RTPROT_KERNEL] = "kernel",
115 [RTPROT_BOOT] = "boot",
116 [RTPROT_STATIC] = "static",
118 [RTPROT_GATED] = "gated",
120 [RTPROT_MRT] = "mrt",
121 [RTPROT_ZEBRA] ="zebra",
122 [RTPROT_BIRD] = "bird",
123 [RTPROT_DNROUTED] = "dnrouted",
124 [RTPROT_XORP] = "xorp",
125 [RTPROT_NTK] = "ntk",
126 [RTPROT_DHCP] = "dhcp",
131 static int rtnl_rtprot_init;
133 static void rtnl_rtprot_initialize(void)
135 rtnl_rtprot_init = 1;
136 rtnl_tab_initialize(CONFDIR "/rt_protos",
137 rtnl_rtprot_tab, 256);
140 char * rtnl_rtprot_n2a(int id, char *buf, int len)
142 if (id<0 || id>=256) {
143 snprintf(buf, len, "%d", id);
146 if (!rtnl_rtprot_tab[id]) {
147 if (!rtnl_rtprot_init)
148 rtnl_rtprot_initialize();
150 if (rtnl_rtprot_tab[id])
151 return rtnl_rtprot_tab[id];
152 snprintf(buf, len, "%d", id);
156 int rtnl_rtprot_a2n(__u32 *id, char *arg)
158 static char *cache = NULL;
159 static unsigned long res;
163 if (cache && strcmp(cache, arg) == 0) {
168 if (!rtnl_rtprot_init)
169 rtnl_rtprot_initialize();
171 for (i=0; i<256; i++) {
172 if (rtnl_rtprot_tab[i] &&
173 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
174 cache = rtnl_rtprot_tab[i];
181 res = strtoul(arg, &end, 0);
182 if (!end || end == arg || *end || res > 255)
190 static char * rtnl_rtscope_tab[256] = {
194 static int rtnl_rtscope_init;
196 static void rtnl_rtscope_initialize(void)
198 rtnl_rtscope_init = 1;
199 rtnl_rtscope_tab[255] = "nowhere";
200 rtnl_rtscope_tab[254] = "host";
201 rtnl_rtscope_tab[253] = "link";
202 rtnl_rtscope_tab[200] = "site";
203 rtnl_tab_initialize(CONFDIR "/rt_scopes",
204 rtnl_rtscope_tab, 256);
207 char * rtnl_rtscope_n2a(int id, char *buf, int len)
209 if (id<0 || id>=256) {
210 snprintf(buf, len, "%d", id);
213 if (!rtnl_rtscope_tab[id]) {
214 if (!rtnl_rtscope_init)
215 rtnl_rtscope_initialize();
217 if (rtnl_rtscope_tab[id])
218 return rtnl_rtscope_tab[id];
219 snprintf(buf, len, "%d", id);
223 int rtnl_rtscope_a2n(__u32 *id, char *arg)
225 static char *cache = NULL;
226 static unsigned long res;
230 if (cache && strcmp(cache, arg) == 0) {
235 if (!rtnl_rtscope_init)
236 rtnl_rtscope_initialize();
238 for (i=0; i<256; i++) {
239 if (rtnl_rtscope_tab[i] &&
240 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
241 cache = rtnl_rtscope_tab[i];
248 res = strtoul(arg, &end, 0);
249 if (!end || end == arg || *end || res > 255)
257 static char * rtnl_rtrealm_tab[256] = {
261 static int rtnl_rtrealm_init;
263 static void rtnl_rtrealm_initialize(void)
265 rtnl_rtrealm_init = 1;
266 rtnl_tab_initialize(CONFDIR "/rt_realms",
267 rtnl_rtrealm_tab, 256);
270 char * rtnl_rtrealm_n2a(int id, char *buf, int len)
272 if (id<0 || id>=256) {
273 snprintf(buf, len, "%d", id);
276 if (!rtnl_rtrealm_tab[id]) {
277 if (!rtnl_rtrealm_init)
278 rtnl_rtrealm_initialize();
280 if (rtnl_rtrealm_tab[id])
281 return rtnl_rtrealm_tab[id];
282 snprintf(buf, len, "%d", id);
287 int rtnl_rtrealm_a2n(__u32 *id, char *arg)
289 static char *cache = NULL;
290 static unsigned long res;
294 if (cache && strcmp(cache, arg) == 0) {
299 if (!rtnl_rtrealm_init)
300 rtnl_rtrealm_initialize();
302 for (i=0; i<256; i++) {
303 if (rtnl_rtrealm_tab[i] &&
304 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
305 cache = rtnl_rtrealm_tab[i];
312 res = strtoul(arg, &end, 0);
313 if (!end || end == arg || *end || res > 255)
320 static struct rtnl_hash_entry dflt_table_entry = { .id = 253, .name = "default" };
321 static struct rtnl_hash_entry main_table_entry = { .id = 254, .name = "main" };
322 static struct rtnl_hash_entry local_table_entry = { .id = 255, .name = "local" };
324 static struct rtnl_hash_entry * rtnl_rttable_hash[256] = {
325 [253] = &dflt_table_entry,
326 [254] = &main_table_entry,
327 [255] = &local_table_entry,
330 static int rtnl_rttable_init;
332 static void rtnl_rttable_initialize(void)
334 rtnl_rttable_init = 1;
335 rtnl_hash_initialize(CONFDIR "/rt_tables",
336 rtnl_rttable_hash, 256);
339 char * rtnl_rttable_n2a(__u32 id, char *buf, int len)
341 struct rtnl_hash_entry *entry;
343 if (id > RT_TABLE_MAX) {
344 snprintf(buf, len, "%u", id);
347 if (!rtnl_rttable_init)
348 rtnl_rttable_initialize();
349 entry = rtnl_rttable_hash[id & 255];
350 while (entry && entry->id != id)
354 snprintf(buf, len, "%u", id);
358 int rtnl_rttable_a2n(__u32 *id, char *arg)
360 static char *cache = NULL;
361 static unsigned long res;
362 struct rtnl_hash_entry *entry;
366 if (cache && strcmp(cache, arg) == 0) {
371 if (!rtnl_rttable_init)
372 rtnl_rttable_initialize();
374 for (i=0; i<256; i++) {
375 entry = rtnl_rttable_hash[i];
376 while (entry && strcmp(entry->name, arg))
386 i = strtoul(arg, &end, 0);
387 if (!end || end == arg || *end || i > RT_TABLE_MAX)
394 static char * rtnl_rtdsfield_tab[256] = {
398 static int rtnl_rtdsfield_init;
400 static void rtnl_rtdsfield_initialize(void)
402 rtnl_rtdsfield_init = 1;
403 rtnl_tab_initialize(CONFDIR "/rt_dsfield",
404 rtnl_rtdsfield_tab, 256);
407 char * rtnl_dsfield_n2a(int id, char *buf, int len)
409 if (id<0 || id>=256) {
410 snprintf(buf, len, "%d", id);
413 if (!rtnl_rtdsfield_tab[id]) {
414 if (!rtnl_rtdsfield_init)
415 rtnl_rtdsfield_initialize();
417 if (rtnl_rtdsfield_tab[id])
418 return rtnl_rtdsfield_tab[id];
419 snprintf(buf, len, "0x%02x", id);
424 int rtnl_dsfield_a2n(__u32 *id, char *arg)
426 static char *cache = NULL;
427 static unsigned long res;
431 if (cache && strcmp(cache, arg) == 0) {
436 if (!rtnl_rtdsfield_init)
437 rtnl_rtdsfield_initialize();
439 for (i=0; i<256; i++) {
440 if (rtnl_rtdsfield_tab[i] &&
441 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
442 cache = rtnl_rtdsfield_tab[i];
449 res = strtoul(arg, &end, 16);
450 if (!end || end == arg || *end || res > 255)
457 static struct rtnl_hash_entry dflt_group_entry = { .id = 0, .name = "default" };
459 static struct rtnl_hash_entry * rtnl_group_hash[256] = {
460 [0] = &dflt_group_entry,
463 static int rtnl_group_init;
465 static void rtnl_group_initialize(void)
468 rtnl_hash_initialize("/etc/iproute2/group",
469 rtnl_group_hash, 256);
472 int rtnl_group_a2n(int *id, char *arg)
474 static char *cache = NULL;
475 static unsigned long res;
476 struct rtnl_hash_entry *entry;
480 if (cache && strcmp(cache, arg) == 0) {
485 if (!rtnl_group_init)
486 rtnl_group_initialize();
488 for (i=0; i<256; i++) {
489 entry = rtnl_group_hash[i];
490 while (entry && strcmp(entry->name, arg))
500 i = strtol(arg, &end, 0);
501 if (!end || end == arg || *end || i < 0)