blob: 620f3115a2a999bd02bd179e4b0f2296fb37c021 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * rt_names.c rtnetlink names DB.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <syslog.h>
16#include <fcntl.h>
17#include <string.h>
18#include <sys/time.h>
osdl.org!shemmingerb88fd9f2004-06-08 23:50:43 +000019#include <sys/socket.h>
20
21#include <asm/types.h>
22#include <linux/rtnetlink.h>
23
24#include "rt_names.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000025
Patrick McHardy9c47d872006-08-11 00:14:50 +020026struct rtnl_hash_entry {
27 struct rtnl_hash_entry *next;
28 char * name;
29 unsigned int id;
30};
31
32static void
33rtnl_hash_initialize(char *file, struct rtnl_hash_entry **hash, int size)
34{
35 struct rtnl_hash_entry *entry;
36 char buf[512];
37 FILE *fp;
38
39 fp = fopen(file, "r");
40 if (!fp)
41 return;
42 while (fgets(buf, sizeof(buf), fp)) {
43 char *p = buf;
44 int id;
45 char namebuf[512];
46
47 while (*p == ' ' || *p == '\t')
48 p++;
49 if (*p == '#' || *p == '\n' || *p == 0)
50 continue;
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",
56 file, p);
57 return;
58 }
59
60 if (id<0)
61 continue;
62 entry = malloc(sizeof(*entry));
63 entry->id = id;
64 entry->name = strdup(namebuf);
65 entry->next = hash[id & (size - 1)];
66 hash[id & (size - 1)] = entry;
67 }
68 fclose(fp);
69}
70
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000071static void rtnl_tab_initialize(char *file, char **tab, int size)
72{
73 char buf[512];
74 FILE *fp;
75
76 fp = fopen(file, "r");
77 if (!fp)
78 return;
79 while (fgets(buf, sizeof(buf), fp)) {
80 char *p = buf;
81 int id;
82 char namebuf[512];
83
84 while (*p == ' ' || *p == '\t')
85 p++;
86 if (*p == '#' || *p == '\n' || *p == 0)
87 continue;
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",
93 file, p);
94 return;
95 }
96
97 if (id<0 || id>size)
98 continue;
99
100 tab[id] = strdup(namebuf);
101 }
102 fclose(fp);
103}
104
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000105static char * rtnl_rtprot_tab[256] = {
osdl.org!shemmingerb88fd9f2004-06-08 23:50:43 +0000106 [RTPROT_UNSPEC] = "none",
107 [RTPROT_REDIRECT] ="redirect",
108 [RTPROT_KERNEL] = "kernel",
109 [RTPROT_BOOT] = "boot",
110 [RTPROT_STATIC] = "static",
111
112 [RTPROT_GATED] = "gated",
113 [RTPROT_RA] = "ra",
114 [RTPROT_MRT] = "mrt",
115 [RTPROT_ZEBRA] ="zebra",
116 [RTPROT_BIRD] = "bird",
117 [RTPROT_DNROUTED] = "dnrouted",
118 [RTPROT_XORP] = "xorp",
shemminger40b6c622006-03-10 23:44:04 +0000119 [RTPROT_NTK] = "ntk",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000120};
121
122
123
124static int rtnl_rtprot_init;
125
126static void rtnl_rtprot_initialize(void)
127{
128 rtnl_rtprot_init = 1;
129 rtnl_tab_initialize("/etc/iproute2/rt_protos",
130 rtnl_rtprot_tab, 256);
131}
132
133char * rtnl_rtprot_n2a(int id, char *buf, int len)
134{
135 if (id<0 || id>=256) {
136 snprintf(buf, len, "%d", id);
137 return buf;
138 }
139 if (!rtnl_rtprot_tab[id]) {
140 if (!rtnl_rtprot_init)
141 rtnl_rtprot_initialize();
142 }
143 if (rtnl_rtprot_tab[id])
144 return rtnl_rtprot_tab[id];
145 snprintf(buf, len, "%d", id);
146 return buf;
147}
148
149int rtnl_rtprot_a2n(__u32 *id, char *arg)
150{
151 static char *cache = NULL;
152 static unsigned long res;
153 char *end;
154 int i;
155
156 if (cache && strcmp(cache, arg) == 0) {
157 *id = res;
158 return 0;
159 }
160
161 if (!rtnl_rtprot_init)
162 rtnl_rtprot_initialize();
163
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];
168 res = i;
169 *id = res;
170 return 0;
171 }
172 }
173
174 res = strtoul(arg, &end, 0);
175 if (!end || end == arg || *end || res > 255)
176 return -1;
177 *id = res;
178 return 0;
179}
180
181
182
183static char * rtnl_rtscope_tab[256] = {
184 "global",
185};
186
187static int rtnl_rtscope_init;
188
189static void rtnl_rtscope_initialize(void)
190{
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);
198}
199
200char * rtnl_rtscope_n2a(int id, char *buf, int len)
201{
202 if (id<0 || id>=256) {
203 snprintf(buf, len, "%d", id);
204 return buf;
205 }
206 if (!rtnl_rtscope_tab[id]) {
207 if (!rtnl_rtscope_init)
208 rtnl_rtscope_initialize();
209 }
210 if (rtnl_rtscope_tab[id])
211 return rtnl_rtscope_tab[id];
212 snprintf(buf, len, "%d", id);
213 return buf;
214}
215
216int rtnl_rtscope_a2n(__u32 *id, char *arg)
217{
218 static char *cache = NULL;
219 static unsigned long res;
220 char *end;
221 int i;
222
223 if (cache && strcmp(cache, arg) == 0) {
224 *id = res;
225 return 0;
226 }
227
228 if (!rtnl_rtscope_init)
229 rtnl_rtscope_initialize();
230
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];
235 res = i;
236 *id = res;
237 return 0;
238 }
239 }
240
241 res = strtoul(arg, &end, 0);
242 if (!end || end == arg || *end || res > 255)
243 return -1;
244 *id = res;
245 return 0;
246}
247
248
249
250static char * rtnl_rtrealm_tab[256] = {
251 "unknown",
252};
253
254static int rtnl_rtrealm_init;
255
256static void rtnl_rtrealm_initialize(void)
257{
258 rtnl_rtrealm_init = 1;
259 rtnl_tab_initialize("/etc/iproute2/rt_realms",
260 rtnl_rtrealm_tab, 256);
261}
262
263char * rtnl_rtrealm_n2a(int id, char *buf, int len)
264{
265 if (id<0 || id>=256) {
266 snprintf(buf, len, "%d", id);
267 return buf;
268 }
269 if (!rtnl_rtrealm_tab[id]) {
270 if (!rtnl_rtrealm_init)
271 rtnl_rtrealm_initialize();
272 }
273 if (rtnl_rtrealm_tab[id])
274 return rtnl_rtrealm_tab[id];
275 snprintf(buf, len, "%d", id);
276 return buf;
277}
278
279
280int rtnl_rtrealm_a2n(__u32 *id, char *arg)
281{
282 static char *cache = NULL;
283 static unsigned long res;
284 char *end;
285 int i;
286
287 if (cache && strcmp(cache, arg) == 0) {
288 *id = res;
289 return 0;
290 }
291
292 if (!rtnl_rtrealm_init)
293 rtnl_rtrealm_initialize();
294
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];
299 res = i;
300 *id = res;
301 return 0;
302 }
303 }
304
305 res = strtoul(arg, &end, 0);
306 if (!end || end == arg || *end || res > 255)
307 return -1;
308 *id = res;
309 return 0;
310}
311
312
Patrick McHardy9c47d872006-08-11 00:14:50 +0200313static struct rtnl_hash_entry dflt_table_entry = { .id = 253, .name = "default" };
314static struct rtnl_hash_entry main_table_entry = { .id = 254, .name = "main" };
315static struct rtnl_hash_entry local_table_entry = { .id = 255, .name = "local" };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000316
Patrick McHardy9c47d872006-08-11 00:14:50 +0200317static struct rtnl_hash_entry * rtnl_rttable_hash[256] = {
318 [253] = &dflt_table_entry,
319 [254] = &main_table_entry,
320 [255] = &local_table_entry,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000321};
322
323static int rtnl_rttable_init;
324
325static void rtnl_rttable_initialize(void)
326{
327 rtnl_rttable_init = 1;
Patrick McHardy9c47d872006-08-11 00:14:50 +0200328 rtnl_hash_initialize("/etc/iproute2/rt_tables",
329 rtnl_rttable_hash, 256);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000330}
331
332char * rtnl_rttable_n2a(int id, char *buf, int len)
333{
Patrick McHardy9c47d872006-08-11 00:14:50 +0200334 struct rtnl_hash_entry *entry;
335
336 if (id >= RT_TABLE_MAX) {
337 snprintf(buf, len, "%u", id);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000338 return buf;
339 }
Patrick McHardy9c47d872006-08-11 00:14:50 +0200340 if (!rtnl_rttable_init)
341 rtnl_rttable_initialize();
342 entry = rtnl_rttable_hash[id & 255];
343 while (entry && entry->id != id)
344 entry = entry->next;
345 if (entry)
346 return entry->name;
347 snprintf(buf, len, "%u", id);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000348 return buf;
349}
350
351int rtnl_rttable_a2n(__u32 *id, char *arg)
352{
353 static char *cache = NULL;
354 static unsigned long res;
Patrick McHardy9c47d872006-08-11 00:14:50 +0200355 struct rtnl_hash_entry *entry;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000356 char *end;
357 int i;
358
359 if (cache && strcmp(cache, arg) == 0) {
360 *id = res;
361 return 0;
362 }
363
364 if (!rtnl_rttable_init)
365 rtnl_rttable_initialize();
366
367 for (i=0; i<256; i++) {
Patrick McHardy9c47d872006-08-11 00:14:50 +0200368 entry = rtnl_rttable_hash[i];
369 while (entry && strcmp(entry->name, arg))
370 entry = entry->next;
371 if (entry) {
372 cache = entry->name;
373 res = entry->id;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000374 *id = res;
375 return 0;
376 }
377 }
378
379 i = strtoul(arg, &end, 0);
Patrick McHardy9c47d872006-08-11 00:14:50 +0200380 if (!end || end == arg || *end || i > RT_TABLE_MAX)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000381 return -1;
382 *id = i;
383 return 0;
384}
385
386
387static char * rtnl_rtdsfield_tab[256] = {
388 "0",
389};
390
391static int rtnl_rtdsfield_init;
392
393static void rtnl_rtdsfield_initialize(void)
394{
395 rtnl_rtdsfield_init = 1;
396 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
397 rtnl_rtdsfield_tab, 256);
398}
399
400char * rtnl_dsfield_n2a(int id, char *buf, int len)
401{
402 if (id<0 || id>=256) {
403 snprintf(buf, len, "%d", id);
404 return buf;
405 }
406 if (!rtnl_rtdsfield_tab[id]) {
407 if (!rtnl_rtdsfield_init)
408 rtnl_rtdsfield_initialize();
409 }
410 if (rtnl_rtdsfield_tab[id])
411 return rtnl_rtdsfield_tab[id];
412 snprintf(buf, len, "0x%02x", id);
413 return buf;
414}
415
416
417int rtnl_dsfield_a2n(__u32 *id, char *arg)
418{
419 static char *cache = NULL;
420 static unsigned long res;
421 char *end;
422 int i;
423
424 if (cache && strcmp(cache, arg) == 0) {
425 *id = res;
426 return 0;
427 }
428
429 if (!rtnl_rtdsfield_init)
430 rtnl_rtdsfield_initialize();
431
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];
436 res = i;
437 *id = res;
438 return 0;
439 }
440 }
441
442 res = strtoul(arg, &end, 16);
443 if (!end || end == arg || *end || res > 255)
444 return -1;
445 *id = res;
446 return 0;
447}
448