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",
120 [RTPROT_DHCP
] = "dhcp",
125 static int rtnl_rtprot_init
;
127 static void rtnl_rtprot_initialize(void)
129 rtnl_rtprot_init
= 1;
130 rtnl_tab_initialize("/etc/iproute2/rt_protos",
131 rtnl_rtprot_tab
, 256);
134 char * rtnl_rtprot_n2a(int id
, char *buf
, int len
)
136 if (id
<0 || id
>=256) {
137 snprintf(buf
, len
, "%d", id
);
140 if (!rtnl_rtprot_tab
[id
]) {
141 if (!rtnl_rtprot_init
)
142 rtnl_rtprot_initialize();
144 if (rtnl_rtprot_tab
[id
])
145 return rtnl_rtprot_tab
[id
];
146 snprintf(buf
, len
, "%d", id
);
150 int rtnl_rtprot_a2n(__u32
*id
, char *arg
)
152 static char *cache
= NULL
;
153 static unsigned long res
;
157 if (cache
&& strcmp(cache
, arg
) == 0) {
162 if (!rtnl_rtprot_init
)
163 rtnl_rtprot_initialize();
165 for (i
=0; i
<256; i
++) {
166 if (rtnl_rtprot_tab
[i
] &&
167 strcmp(rtnl_rtprot_tab
[i
], arg
) == 0) {
168 cache
= rtnl_rtprot_tab
[i
];
175 res
= strtoul(arg
, &end
, 0);
176 if (!end
|| end
== arg
|| *end
|| res
> 255)
184 static char * rtnl_rtscope_tab
[256] = {
188 static int rtnl_rtscope_init
;
190 static void rtnl_rtscope_initialize(void)
192 rtnl_rtscope_init
= 1;
193 rtnl_rtscope_tab
[255] = "nowhere";
194 rtnl_rtscope_tab
[254] = "host";
195 rtnl_rtscope_tab
[253] = "link";
196 rtnl_rtscope_tab
[200] = "site";
197 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
198 rtnl_rtscope_tab
, 256);
201 char * rtnl_rtscope_n2a(int id
, char *buf
, int len
)
203 if (id
<0 || id
>=256) {
204 snprintf(buf
, len
, "%d", id
);
207 if (!rtnl_rtscope_tab
[id
]) {
208 if (!rtnl_rtscope_init
)
209 rtnl_rtscope_initialize();
211 if (rtnl_rtscope_tab
[id
])
212 return rtnl_rtscope_tab
[id
];
213 snprintf(buf
, len
, "%d", id
);
217 int rtnl_rtscope_a2n(__u32
*id
, char *arg
)
219 static char *cache
= NULL
;
220 static unsigned long res
;
224 if (cache
&& strcmp(cache
, arg
) == 0) {
229 if (!rtnl_rtscope_init
)
230 rtnl_rtscope_initialize();
232 for (i
=0; i
<256; i
++) {
233 if (rtnl_rtscope_tab
[i
] &&
234 strcmp(rtnl_rtscope_tab
[i
], arg
) == 0) {
235 cache
= rtnl_rtscope_tab
[i
];
242 res
= strtoul(arg
, &end
, 0);
243 if (!end
|| end
== arg
|| *end
|| res
> 255)
251 static char * rtnl_rtrealm_tab
[256] = {
255 static int rtnl_rtrealm_init
;
257 static void rtnl_rtrealm_initialize(void)
259 rtnl_rtrealm_init
= 1;
260 rtnl_tab_initialize("/etc/iproute2/rt_realms",
261 rtnl_rtrealm_tab
, 256);
264 char * rtnl_rtrealm_n2a(int id
, char *buf
, int len
)
266 if (id
<0 || id
>=256) {
267 snprintf(buf
, len
, "%d", id
);
270 if (!rtnl_rtrealm_tab
[id
]) {
271 if (!rtnl_rtrealm_init
)
272 rtnl_rtrealm_initialize();
274 if (rtnl_rtrealm_tab
[id
])
275 return rtnl_rtrealm_tab
[id
];
276 snprintf(buf
, len
, "%d", id
);
281 int rtnl_rtrealm_a2n(__u32
*id
, char *arg
)
283 static char *cache
= NULL
;
284 static unsigned long res
;
288 if (cache
&& strcmp(cache
, arg
) == 0) {
293 if (!rtnl_rtrealm_init
)
294 rtnl_rtrealm_initialize();
296 for (i
=0; i
<256; i
++) {
297 if (rtnl_rtrealm_tab
[i
] &&
298 strcmp(rtnl_rtrealm_tab
[i
], arg
) == 0) {
299 cache
= rtnl_rtrealm_tab
[i
];
306 res
= strtoul(arg
, &end
, 0);
307 if (!end
|| end
== arg
|| *end
|| res
> 255)
314 static struct rtnl_hash_entry dflt_table_entry
= { .id
= 253, .name
= "default" };
315 static struct rtnl_hash_entry main_table_entry
= { .id
= 254, .name
= "main" };
316 static struct rtnl_hash_entry local_table_entry
= { .id
= 255, .name
= "local" };
318 static struct rtnl_hash_entry
* rtnl_rttable_hash
[256] = {
319 [253] = &dflt_table_entry
,
320 [254] = &main_table_entry
,
321 [255] = &local_table_entry
,
324 static int rtnl_rttable_init
;
326 static void rtnl_rttable_initialize(void)
328 rtnl_rttable_init
= 1;
329 rtnl_hash_initialize("/etc/iproute2/rt_tables",
330 rtnl_rttable_hash
, 256);
333 char * rtnl_rttable_n2a(__u32 id
, char *buf
, int len
)
335 struct rtnl_hash_entry
*entry
;
337 if (id
> RT_TABLE_MAX
) {
338 snprintf(buf
, len
, "%u", id
);
341 if (!rtnl_rttable_init
)
342 rtnl_rttable_initialize();
343 entry
= rtnl_rttable_hash
[id
& 255];
344 while (entry
&& entry
->id
!= id
)
348 snprintf(buf
, len
, "%u", id
);
352 int rtnl_rttable_a2n(__u32
*id
, char *arg
)
354 static char *cache
= NULL
;
355 static unsigned long res
;
356 struct rtnl_hash_entry
*entry
;
360 if (cache
&& strcmp(cache
, arg
) == 0) {
365 if (!rtnl_rttable_init
)
366 rtnl_rttable_initialize();
368 for (i
=0; i
<256; i
++) {
369 entry
= rtnl_rttable_hash
[i
];
370 while (entry
&& strcmp(entry
->name
, arg
))
380 i
= strtoul(arg
, &end
, 0);
381 if (!end
|| end
== arg
|| *end
|| i
> RT_TABLE_MAX
)
388 static char * rtnl_rtdsfield_tab
[256] = {
392 static int rtnl_rtdsfield_init
;
394 static void rtnl_rtdsfield_initialize(void)
396 rtnl_rtdsfield_init
= 1;
397 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
398 rtnl_rtdsfield_tab
, 256);
401 char * rtnl_dsfield_n2a(int id
, char *buf
, int len
)
403 if (id
<0 || id
>=256) {
404 snprintf(buf
, len
, "%d", id
);
407 if (!rtnl_rtdsfield_tab
[id
]) {
408 if (!rtnl_rtdsfield_init
)
409 rtnl_rtdsfield_initialize();
411 if (rtnl_rtdsfield_tab
[id
])
412 return rtnl_rtdsfield_tab
[id
];
413 snprintf(buf
, len
, "0x%02x", id
);
418 int rtnl_dsfield_a2n(__u32
*id
, char *arg
)
420 static char *cache
= NULL
;
421 static unsigned long res
;
425 if (cache
&& strcmp(cache
, arg
) == 0) {
430 if (!rtnl_rtdsfield_init
)
431 rtnl_rtdsfield_initialize();
433 for (i
=0; i
<256; i
++) {
434 if (rtnl_rtdsfield_tab
[i
] &&
435 strcmp(rtnl_rtdsfield_tab
[i
], arg
) == 0) {
436 cache
= rtnl_rtdsfield_tab
[i
];
443 res
= strtoul(arg
, &end
, 16);
444 if (!end
|| end
== arg
|| *end
|| res
> 255)