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 struct rtnl_hash_entry {
27 struct rtnl_hash_entry *next;
33 rtnl_hash_initialize(char *file, struct rtnl_hash_entry **hash, int size)
35 struct rtnl_hash_entry *entry;
39 fp = fopen(file, "r");
42 while (fgets(buf, sizeof(buf), fp)) {
47 while (*p == ' ' || *p == '\t')
49 if (*p == '#' || *p == '\n' || *p == 0)
51 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
52 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
53 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
54 sscanf(p, "%d %s #", &id, namebuf) != 2) {
55 fprintf(stderr, "Database %s is corrupted at %s\n",
63 entry = malloc(sizeof(*entry));
65 entry->name = strdup(namebuf);
66 entry->next = hash[id & (size - 1)];
67 hash[id & (size - 1)] = entry;
72 static void rtnl_tab_initialize(char *file, char **tab, int size)
77 fp = fopen(file, "r");
80 while (fgets(buf, sizeof(buf), fp)) {
85 while (*p == ' ' || *p == '\t')
87 if (*p == '#' || *p == '\n' || *p == 0)
89 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
90 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
91 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
92 sscanf(p, "%d %s #", &id, namebuf) != 2) {
93 fprintf(stderr, "Database %s is corrupted at %s\n",
102 tab[id] = strdup(namebuf);
107 static char * rtnl_rtprot_tab[256] = {
108 [RTPROT_UNSPEC] = "none",
109 [RTPROT_REDIRECT] ="redirect",
110 [RTPROT_KERNEL] = "kernel",
111 [RTPROT_BOOT] = "boot",
112 [RTPROT_STATIC] = "static",
114 [RTPROT_GATED] = "gated",
116 [RTPROT_MRT] = "mrt",
117 [RTPROT_ZEBRA] ="zebra",
118 [RTPROT_BIRD] = "bird",
119 [RTPROT_DNROUTED] = "dnrouted",
120 [RTPROT_XORP] = "xorp",
121 [RTPROT_NTK] = "ntk",
122 [RTPROT_DHCP] = "dhcp",
127 static int rtnl_rtprot_init;
129 static void rtnl_rtprot_initialize(void)
131 rtnl_rtprot_init = 1;
132 rtnl_tab_initialize("/etc/iproute2/rt_protos",
133 rtnl_rtprot_tab, 256);
136 char * rtnl_rtprot_n2a(int id, char *buf, int len)
138 if (id<0 || id>=256) {
139 snprintf(buf, len, "%d", id);
142 if (!rtnl_rtprot_tab[id]) {
143 if (!rtnl_rtprot_init)
144 rtnl_rtprot_initialize();
146 if (rtnl_rtprot_tab[id])
147 return rtnl_rtprot_tab[id];
148 snprintf(buf, len, "%d", id);
152 int rtnl_rtprot_a2n(__u32 *id, char *arg)
154 static char *cache = NULL;
155 static unsigned long res;
159 if (cache && strcmp(cache, arg) == 0) {
164 if (!rtnl_rtprot_init)
165 rtnl_rtprot_initialize();
167 for (i=0; i<256; i++) {
168 if (rtnl_rtprot_tab[i] &&
169 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
170 cache = rtnl_rtprot_tab[i];
177 res = strtoul(arg, &end, 0);
178 if (!end || end == arg || *end || res > 255)
186 static char * rtnl_rtscope_tab[256] = {
190 static int rtnl_rtscope_init;
192 static void rtnl_rtscope_initialize(void)
194 rtnl_rtscope_init = 1;
195 rtnl_rtscope_tab[255] = "nowhere";
196 rtnl_rtscope_tab[254] = "host";
197 rtnl_rtscope_tab[253] = "link";
198 rtnl_rtscope_tab[200] = "site";
199 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
200 rtnl_rtscope_tab, 256);
203 char * rtnl_rtscope_n2a(int id, char *buf, int len)
205 if (id<0 || id>=256) {
206 snprintf(buf, len, "%d", id);
209 if (!rtnl_rtscope_tab[id]) {
210 if (!rtnl_rtscope_init)
211 rtnl_rtscope_initialize();
213 if (rtnl_rtscope_tab[id])
214 return rtnl_rtscope_tab[id];
215 snprintf(buf, len, "%d", id);
219 int rtnl_rtscope_a2n(__u32 *id, char *arg)
221 static char *cache = NULL;
222 static unsigned long res;
226 if (cache && strcmp(cache, arg) == 0) {
231 if (!rtnl_rtscope_init)
232 rtnl_rtscope_initialize();
234 for (i=0; i<256; i++) {
235 if (rtnl_rtscope_tab[i] &&
236 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
237 cache = rtnl_rtscope_tab[i];
244 res = strtoul(arg, &end, 0);
245 if (!end || end == arg || *end || res > 255)
253 static char * rtnl_rtrealm_tab[256] = {
257 static int rtnl_rtrealm_init;
259 static void rtnl_rtrealm_initialize(void)
261 rtnl_rtrealm_init = 1;
262 rtnl_tab_initialize("/etc/iproute2/rt_realms",
263 rtnl_rtrealm_tab, 256);
266 char * rtnl_rtrealm_n2a(int id, char *buf, int len)
268 if (id<0 || id>=256) {
269 snprintf(buf, len, "%d", id);
272 if (!rtnl_rtrealm_tab[id]) {
273 if (!rtnl_rtrealm_init)
274 rtnl_rtrealm_initialize();
276 if (rtnl_rtrealm_tab[id])
277 return rtnl_rtrealm_tab[id];
278 snprintf(buf, len, "%d", id);
283 int rtnl_rtrealm_a2n(__u32 *id, char *arg)
285 static char *cache = NULL;
286 static unsigned long res;
290 if (cache && strcmp(cache, arg) == 0) {
295 if (!rtnl_rtrealm_init)
296 rtnl_rtrealm_initialize();
298 for (i=0; i<256; i++) {
299 if (rtnl_rtrealm_tab[i] &&
300 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
301 cache = rtnl_rtrealm_tab[i];
308 res = strtoul(arg, &end, 0);
309 if (!end || end == arg || *end || res > 255)
316 static struct rtnl_hash_entry dflt_table_entry = { .id = 253, .name = "default" };
317 static struct rtnl_hash_entry main_table_entry = { .id = 254, .name = "main" };
318 static struct rtnl_hash_entry local_table_entry = { .id = 255, .name = "local" };
320 static struct rtnl_hash_entry * rtnl_rttable_hash[256] = {
321 [253] = &dflt_table_entry,
322 [254] = &main_table_entry,
323 [255] = &local_table_entry,
326 static int rtnl_rttable_init;
328 static void rtnl_rttable_initialize(void)
330 rtnl_rttable_init = 1;
331 rtnl_hash_initialize("/etc/iproute2/rt_tables",
332 rtnl_rttable_hash, 256);
335 char * rtnl_rttable_n2a(__u32 id, char *buf, int len)
337 struct rtnl_hash_entry *entry;
339 if (id > RT_TABLE_MAX) {
340 snprintf(buf, len, "%u", id);
343 if (!rtnl_rttable_init)
344 rtnl_rttable_initialize();
345 entry = rtnl_rttable_hash[id & 255];
346 while (entry && entry->id != id)
350 snprintf(buf, len, "%u", id);
354 int rtnl_rttable_a2n(__u32 *id, char *arg)
356 static char *cache = NULL;
357 static unsigned long res;
358 struct rtnl_hash_entry *entry;
362 if (cache && strcmp(cache, arg) == 0) {
367 if (!rtnl_rttable_init)
368 rtnl_rttable_initialize();
370 for (i=0; i<256; i++) {
371 entry = rtnl_rttable_hash[i];
372 while (entry && strcmp(entry->name, arg))
382 i = strtoul(arg, &end, 0);
383 if (!end || end == arg || *end || i > RT_TABLE_MAX)
390 static char * rtnl_rtdsfield_tab[256] = {
394 static int rtnl_rtdsfield_init;
396 static void rtnl_rtdsfield_initialize(void)
398 rtnl_rtdsfield_init = 1;
399 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
400 rtnl_rtdsfield_tab, 256);
403 char * rtnl_dsfield_n2a(int id, char *buf, int len)
405 if (id<0 || id>=256) {
406 snprintf(buf, len, "%d", id);
409 if (!rtnl_rtdsfield_tab[id]) {
410 if (!rtnl_rtdsfield_init)
411 rtnl_rtdsfield_initialize();
413 if (rtnl_rtdsfield_tab[id])
414 return rtnl_rtdsfield_tab[id];
415 snprintf(buf, len, "0x%02x", id);
420 int rtnl_dsfield_a2n(__u32 *id, char *arg)
422 static char *cache = NULL;
423 static unsigned long res;
427 if (cache && strcmp(cache, arg) == 0) {
432 if (!rtnl_rtdsfield_init)
433 rtnl_rtdsfield_initialize();
435 for (i=0; i<256; i++) {
436 if (rtnl_rtdsfield_tab[i] &&
437 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
438 cache = rtnl_rtdsfield_tab[i];
445 res = strtoul(arg, &end, 16);
446 if (!end || end == arg || *end || res > 255)
453 static struct rtnl_hash_entry dflt_group_entry = { .id = 0, .name = "default" };
455 static struct rtnl_hash_entry * rtnl_group_hash[256] = {
456 [0] = &dflt_group_entry,
459 static int rtnl_group_init;
461 static void rtnl_group_initialize(void)
464 rtnl_hash_initialize("/etc/iproute2/group",
465 rtnl_group_hash, 256);
468 int rtnl_group_a2n(int *id, char *arg)
470 static char *cache = NULL;
471 static unsigned long res;
472 struct rtnl_hash_entry *entry;
476 if (cache && strcmp(cache, arg) == 0) {
481 if (!rtnl_group_init)
482 rtnl_group_initialize();
484 for (i=0; i<256; i++) {
485 entry = rtnl_group_hash[i];
486 while (entry && strcmp(entry->name, arg))
496 i = strtol(arg, &end, 0);
497 if (!end || end == arg || *end || i < 0)