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",
62 entry
= malloc(sizeof(*entry
));
64 entry
->name
= strdup(namebuf
);
65 entry
->next
= hash
[id
& (size
- 1)];
66 hash
[id
& (size
- 1)] = entry
;
71 static void rtnl_tab_initialize(char *file
, char **tab
, int size
)
76 fp
= fopen(file
, "r");
79 while (fgets(buf
, sizeof(buf
), fp
)) {
84 while (*p
== ' ' || *p
== '\t')
86 if (*p
== '#' || *p
== '\n' || *p
== 0)
88 if (sscanf(p
, "0x%x %s\n", &id
, namebuf
) != 2 &&
89 sscanf(p
, "0x%x %s #", &id
, namebuf
) != 2 &&
90 sscanf(p
, "%d %s\n", &id
, namebuf
) != 2 &&
91 sscanf(p
, "%d %s #", &id
, namebuf
) != 2) {
92 fprintf(stderr
, "Database %s is corrupted at %s\n",
100 tab
[id
] = strdup(namebuf
);
105 static char * rtnl_rtprot_tab
[256] = {
106 [RTPROT_UNSPEC
] = "none",
107 [RTPROT_REDIRECT
] ="redirect",
108 [RTPROT_KERNEL
] = "kernel",
109 [RTPROT_BOOT
] = "boot",
110 [RTPROT_STATIC
] = "static",
112 [RTPROT_GATED
] = "gated",
114 [RTPROT_MRT
] = "mrt",
115 [RTPROT_ZEBRA
] ="zebra",
116 [RTPROT_BIRD
] = "bird",
117 [RTPROT_DNROUTED
] = "dnrouted",
118 [RTPROT_XORP
] = "xorp",
119 [RTPROT_NTK
] = "ntk",
124 static int rtnl_rtprot_init
;
126 static void rtnl_rtprot_initialize(void)
128 rtnl_rtprot_init
= 1;
129 rtnl_tab_initialize("/etc/iproute2/rt_protos",
130 rtnl_rtprot_tab
, 256);
133 char * rtnl_rtprot_n2a(int id
, char *buf
, int len
)
135 if (id
<0 || id
>=256) {
136 snprintf(buf
, len
, "%d", id
);
139 if (!rtnl_rtprot_tab
[id
]) {
140 if (!rtnl_rtprot_init
)
141 rtnl_rtprot_initialize();
143 if (rtnl_rtprot_tab
[id
])
144 return rtnl_rtprot_tab
[id
];
145 snprintf(buf
, len
, "%d", id
);
149 int rtnl_rtprot_a2n(__u32
*id
, char *arg
)
151 static char *cache
= NULL
;
152 static unsigned long res
;
156 if (cache
&& strcmp(cache
, arg
) == 0) {
161 if (!rtnl_rtprot_init
)
162 rtnl_rtprot_initialize();
164 for (i
=0; i
<256; i
++) {
165 if (rtnl_rtprot_tab
[i
] &&
166 strcmp(rtnl_rtprot_tab
[i
], arg
) == 0) {
167 cache
= rtnl_rtprot_tab
[i
];
174 res
= strtoul(arg
, &end
, 0);
175 if (!end
|| end
== arg
|| *end
|| res
> 255)
183 static char * rtnl_rtscope_tab
[256] = {
187 static int rtnl_rtscope_init
;
189 static void rtnl_rtscope_initialize(void)
191 rtnl_rtscope_init
= 1;
192 rtnl_rtscope_tab
[255] = "nowhere";
193 rtnl_rtscope_tab
[254] = "host";
194 rtnl_rtscope_tab
[253] = "link";
195 rtnl_rtscope_tab
[200] = "site";
196 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
197 rtnl_rtscope_tab
, 256);
200 char * rtnl_rtscope_n2a(int id
, char *buf
, int len
)
202 if (id
<0 || id
>=256) {
203 snprintf(buf
, len
, "%d", id
);
206 if (!rtnl_rtscope_tab
[id
]) {
207 if (!rtnl_rtscope_init
)
208 rtnl_rtscope_initialize();
210 if (rtnl_rtscope_tab
[id
])
211 return rtnl_rtscope_tab
[id
];
212 snprintf(buf
, len
, "%d", id
);
216 int rtnl_rtscope_a2n(__u32
*id
, char *arg
)
218 static char *cache
= NULL
;
219 static unsigned long res
;
223 if (cache
&& strcmp(cache
, arg
) == 0) {
228 if (!rtnl_rtscope_init
)
229 rtnl_rtscope_initialize();
231 for (i
=0; i
<256; i
++) {
232 if (rtnl_rtscope_tab
[i
] &&
233 strcmp(rtnl_rtscope_tab
[i
], arg
) == 0) {
234 cache
= rtnl_rtscope_tab
[i
];
241 res
= strtoul(arg
, &end
, 0);
242 if (!end
|| end
== arg
|| *end
|| res
> 255)
250 static char * rtnl_rtrealm_tab
[256] = {
254 static int rtnl_rtrealm_init
;
256 static void rtnl_rtrealm_initialize(void)
258 rtnl_rtrealm_init
= 1;
259 rtnl_tab_initialize("/etc/iproute2/rt_realms",
260 rtnl_rtrealm_tab
, 256);
263 char * rtnl_rtrealm_n2a(int id
, char *buf
, int len
)
265 if (id
<0 || id
>=256) {
266 snprintf(buf
, len
, "%d", id
);
269 if (!rtnl_rtrealm_tab
[id
]) {
270 if (!rtnl_rtrealm_init
)
271 rtnl_rtrealm_initialize();
273 if (rtnl_rtrealm_tab
[id
])
274 return rtnl_rtrealm_tab
[id
];
275 snprintf(buf
, len
, "%d", id
);
280 int rtnl_rtrealm_a2n(__u32
*id
, char *arg
)
282 static char *cache
= NULL
;
283 static unsigned long res
;
287 if (cache
&& strcmp(cache
, arg
) == 0) {
292 if (!rtnl_rtrealm_init
)
293 rtnl_rtrealm_initialize();
295 for (i
=0; i
<256; i
++) {
296 if (rtnl_rtrealm_tab
[i
] &&
297 strcmp(rtnl_rtrealm_tab
[i
], arg
) == 0) {
298 cache
= rtnl_rtrealm_tab
[i
];
305 res
= strtoul(arg
, &end
, 0);
306 if (!end
|| end
== arg
|| *end
|| res
> 255)
313 static struct rtnl_hash_entry dflt_table_entry
= { .id
= 253, .name
= "default" };
314 static struct rtnl_hash_entry main_table_entry
= { .id
= 254, .name
= "main" };
315 static struct rtnl_hash_entry local_table_entry
= { .id
= 255, .name
= "local" };
317 static struct rtnl_hash_entry
* rtnl_rttable_hash
[256] = {
318 [253] = &dflt_table_entry
,
319 [254] = &main_table_entry
,
320 [255] = &local_table_entry
,
323 static int rtnl_rttable_init
;
325 static void rtnl_rttable_initialize(void)
327 rtnl_rttable_init
= 1;
328 rtnl_hash_initialize("/etc/iproute2/rt_tables",
329 rtnl_rttable_hash
, 256);
332 char * rtnl_rttable_n2a(__u32 id
, char *buf
, int len
)
334 struct rtnl_hash_entry
*entry
;
336 if (id
>= RT_TABLE_MAX
) {
337 snprintf(buf
, len
, "%u", id
);
340 if (!rtnl_rttable_init
)
341 rtnl_rttable_initialize();
342 entry
= rtnl_rttable_hash
[id
& 255];
343 while (entry
&& entry
->id
!= id
)
347 snprintf(buf
, len
, "%u", id
);
351 int rtnl_rttable_a2n(__u32
*id
, char *arg
)
353 static char *cache
= NULL
;
354 static unsigned long res
;
355 struct rtnl_hash_entry
*entry
;
359 if (cache
&& strcmp(cache
, arg
) == 0) {
364 if (!rtnl_rttable_init
)
365 rtnl_rttable_initialize();
367 for (i
=0; i
<256; i
++) {
368 entry
= rtnl_rttable_hash
[i
];
369 while (entry
&& strcmp(entry
->name
, arg
))
379 i
= strtoul(arg
, &end
, 0);
380 if (!end
|| end
== arg
|| *end
|| i
> RT_TABLE_MAX
)
387 static char * rtnl_rtdsfield_tab
[256] = {
391 static int rtnl_rtdsfield_init
;
393 static void rtnl_rtdsfield_initialize(void)
395 rtnl_rtdsfield_init
= 1;
396 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
397 rtnl_rtdsfield_tab
, 256);
400 char * rtnl_dsfield_n2a(int id
, char *buf
, int len
)
402 if (id
<0 || id
>=256) {
403 snprintf(buf
, len
, "%d", id
);
406 if (!rtnl_rtdsfield_tab
[id
]) {
407 if (!rtnl_rtdsfield_init
)
408 rtnl_rtdsfield_initialize();
410 if (rtnl_rtdsfield_tab
[id
])
411 return rtnl_rtdsfield_tab
[id
];
412 snprintf(buf
, len
, "0x%02x", id
);
417 int rtnl_dsfield_a2n(__u32
*id
, char *arg
)
419 static char *cache
= NULL
;
420 static unsigned long res
;
424 if (cache
&& strcmp(cache
, arg
) == 0) {
429 if (!rtnl_rtdsfield_init
)
430 rtnl_rtdsfield_initialize();
432 for (i
=0; i
<256; i
++) {
433 if (rtnl_rtdsfield_tab
[i
] &&
434 strcmp(rtnl_rtdsfield_tab
[i
], arg
) == 0) {
435 cache
= rtnl_rtdsfield_tab
[i
];
442 res
= strtoul(arg
, &end
, 16);
443 if (!end
|| end
== arg
|| *end
|| res
> 255)