blob: 5db7f2c11ed3844f09e13e9a57e2c335010058ec [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
26static void rtnl_tab_initialize(char *file, char **tab, int size)
27{
28 char buf[512];
29 FILE *fp;
30
31 fp = fopen(file, "r");
32 if (!fp)
33 return;
34 while (fgets(buf, sizeof(buf), fp)) {
35 char *p = buf;
36 int id;
37 char namebuf[512];
38
39 while (*p == ' ' || *p == '\t')
40 p++;
41 if (*p == '#' || *p == '\n' || *p == 0)
42 continue;
43 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
44 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
45 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
46 sscanf(p, "%d %s #", &id, namebuf) != 2) {
47 fprintf(stderr, "Database %s is corrupted at %s\n",
48 file, p);
49 return;
50 }
51
52 if (id<0 || id>size)
53 continue;
54
55 tab[id] = strdup(namebuf);
56 }
57 fclose(fp);
58}
59
60
61static char * rtnl_rtprot_tab[256] = {
osdl.org!shemmingerb88fd9f2004-06-08 23:50:43 +000062 [RTPROT_UNSPEC] = "none",
63 [RTPROT_REDIRECT] ="redirect",
64 [RTPROT_KERNEL] = "kernel",
65 [RTPROT_BOOT] = "boot",
66 [RTPROT_STATIC] = "static",
67
68 [RTPROT_GATED] = "gated",
69 [RTPROT_RA] = "ra",
70 [RTPROT_MRT] = "mrt",
71 [RTPROT_ZEBRA] ="zebra",
72 [RTPROT_BIRD] = "bird",
73 [RTPROT_DNROUTED] = "dnrouted",
74 [RTPROT_XORP] = "xorp",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000075};
76
77
78
79static int rtnl_rtprot_init;
80
81static void rtnl_rtprot_initialize(void)
82{
83 rtnl_rtprot_init = 1;
84 rtnl_tab_initialize("/etc/iproute2/rt_protos",
85 rtnl_rtprot_tab, 256);
86}
87
88char * rtnl_rtprot_n2a(int id, char *buf, int len)
89{
90 if (id<0 || id>=256) {
91 snprintf(buf, len, "%d", id);
92 return buf;
93 }
94 if (!rtnl_rtprot_tab[id]) {
95 if (!rtnl_rtprot_init)
96 rtnl_rtprot_initialize();
97 }
98 if (rtnl_rtprot_tab[id])
99 return rtnl_rtprot_tab[id];
100 snprintf(buf, len, "%d", id);
101 return buf;
102}
103
104int rtnl_rtprot_a2n(__u32 *id, char *arg)
105{
106 static char *cache = NULL;
107 static unsigned long res;
108 char *end;
109 int i;
110
111 if (cache && strcmp(cache, arg) == 0) {
112 *id = res;
113 return 0;
114 }
115
116 if (!rtnl_rtprot_init)
117 rtnl_rtprot_initialize();
118
119 for (i=0; i<256; i++) {
120 if (rtnl_rtprot_tab[i] &&
121 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
122 cache = rtnl_rtprot_tab[i];
123 res = i;
124 *id = res;
125 return 0;
126 }
127 }
128
129 res = strtoul(arg, &end, 0);
130 if (!end || end == arg || *end || res > 255)
131 return -1;
132 *id = res;
133 return 0;
134}
135
136
137
138static char * rtnl_rtscope_tab[256] = {
139 "global",
140};
141
142static int rtnl_rtscope_init;
143
144static void rtnl_rtscope_initialize(void)
145{
146 rtnl_rtscope_init = 1;
147 rtnl_rtscope_tab[255] = "nowhere";
148 rtnl_rtscope_tab[254] = "host";
149 rtnl_rtscope_tab[253] = "link";
150 rtnl_rtscope_tab[200] = "site";
151 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
152 rtnl_rtscope_tab, 256);
153}
154
155char * rtnl_rtscope_n2a(int id, char *buf, int len)
156{
157 if (id<0 || id>=256) {
158 snprintf(buf, len, "%d", id);
159 return buf;
160 }
161 if (!rtnl_rtscope_tab[id]) {
162 if (!rtnl_rtscope_init)
163 rtnl_rtscope_initialize();
164 }
165 if (rtnl_rtscope_tab[id])
166 return rtnl_rtscope_tab[id];
167 snprintf(buf, len, "%d", id);
168 return buf;
169}
170
171int rtnl_rtscope_a2n(__u32 *id, char *arg)
172{
173 static char *cache = NULL;
174 static unsigned long res;
175 char *end;
176 int i;
177
178 if (cache && strcmp(cache, arg) == 0) {
179 *id = res;
180 return 0;
181 }
182
183 if (!rtnl_rtscope_init)
184 rtnl_rtscope_initialize();
185
186 for (i=0; i<256; i++) {
187 if (rtnl_rtscope_tab[i] &&
188 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
189 cache = rtnl_rtscope_tab[i];
190 res = i;
191 *id = res;
192 return 0;
193 }
194 }
195
196 res = strtoul(arg, &end, 0);
197 if (!end || end == arg || *end || res > 255)
198 return -1;
199 *id = res;
200 return 0;
201}
202
203
204
205static char * rtnl_rtrealm_tab[256] = {
206 "unknown",
207};
208
209static int rtnl_rtrealm_init;
210
211static void rtnl_rtrealm_initialize(void)
212{
213 rtnl_rtrealm_init = 1;
214 rtnl_tab_initialize("/etc/iproute2/rt_realms",
215 rtnl_rtrealm_tab, 256);
216}
217
218char * rtnl_rtrealm_n2a(int id, char *buf, int len)
219{
220 if (id<0 || id>=256) {
221 snprintf(buf, len, "%d", id);
222 return buf;
223 }
224 if (!rtnl_rtrealm_tab[id]) {
225 if (!rtnl_rtrealm_init)
226 rtnl_rtrealm_initialize();
227 }
228 if (rtnl_rtrealm_tab[id])
229 return rtnl_rtrealm_tab[id];
230 snprintf(buf, len, "%d", id);
231 return buf;
232}
233
234
235int rtnl_rtrealm_a2n(__u32 *id, char *arg)
236{
237 static char *cache = NULL;
238 static unsigned long res;
239 char *end;
240 int i;
241
242 if (cache && strcmp(cache, arg) == 0) {
243 *id = res;
244 return 0;
245 }
246
247 if (!rtnl_rtrealm_init)
248 rtnl_rtrealm_initialize();
249
250 for (i=0; i<256; i++) {
251 if (rtnl_rtrealm_tab[i] &&
252 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
253 cache = rtnl_rtrealm_tab[i];
254 res = i;
255 *id = res;
256 return 0;
257 }
258 }
259
260 res = strtoul(arg, &end, 0);
261 if (!end || end == arg || *end || res > 255)
262 return -1;
263 *id = res;
264 return 0;
265}
266
267
268
269static char * rtnl_rttable_tab[256] = {
270 "unspec",
271};
272
273static int rtnl_rttable_init;
274
275static void rtnl_rttable_initialize(void)
276{
277 rtnl_rttable_init = 1;
278 rtnl_rttable_tab[255] = "local";
279 rtnl_rttable_tab[254] = "main";
org[shemminger]!shemminger010b0f62004-06-07 22:05:31 +0000280 rtnl_rttable_tab[253] = "default";
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000281 rtnl_tab_initialize("/etc/iproute2/rt_tables",
282 rtnl_rttable_tab, 256);
283}
284
285char * rtnl_rttable_n2a(int id, char *buf, int len)
286{
287 if (id<0 || id>=256) {
288 snprintf(buf, len, "%d", id);
289 return buf;
290 }
291 if (!rtnl_rttable_tab[id]) {
292 if (!rtnl_rttable_init)
293 rtnl_rttable_initialize();
294 }
295 if (rtnl_rttable_tab[id])
296 return rtnl_rttable_tab[id];
297 snprintf(buf, len, "%d", id);
298 return buf;
299}
300
301int rtnl_rttable_a2n(__u32 *id, char *arg)
302{
303 static char *cache = NULL;
304 static unsigned long res;
305 char *end;
306 int i;
307
308 if (cache && strcmp(cache, arg) == 0) {
309 *id = res;
310 return 0;
311 }
312
313 if (!rtnl_rttable_init)
314 rtnl_rttable_initialize();
315
316 for (i=0; i<256; i++) {
317 if (rtnl_rttable_tab[i] &&
318 strcmp(rtnl_rttable_tab[i], arg) == 0) {
319 cache = rtnl_rttable_tab[i];
320 res = i;
321 *id = res;
322 return 0;
323 }
324 }
325
326 i = strtoul(arg, &end, 0);
327 if (!end || end == arg || *end || i > 255)
328 return -1;
329 *id = i;
330 return 0;
331}
332
333
334static char * rtnl_rtdsfield_tab[256] = {
335 "0",
336};
337
338static int rtnl_rtdsfield_init;
339
340static void rtnl_rtdsfield_initialize(void)
341{
342 rtnl_rtdsfield_init = 1;
343 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
344 rtnl_rtdsfield_tab, 256);
345}
346
347char * rtnl_dsfield_n2a(int id, char *buf, int len)
348{
349 if (id<0 || id>=256) {
350 snprintf(buf, len, "%d", id);
351 return buf;
352 }
353 if (!rtnl_rtdsfield_tab[id]) {
354 if (!rtnl_rtdsfield_init)
355 rtnl_rtdsfield_initialize();
356 }
357 if (rtnl_rtdsfield_tab[id])
358 return rtnl_rtdsfield_tab[id];
359 snprintf(buf, len, "0x%02x", id);
360 return buf;
361}
362
363
364int rtnl_dsfield_a2n(__u32 *id, char *arg)
365{
366 static char *cache = NULL;
367 static unsigned long res;
368 char *end;
369 int i;
370
371 if (cache && strcmp(cache, arg) == 0) {
372 *id = res;
373 return 0;
374 }
375
376 if (!rtnl_rtdsfield_init)
377 rtnl_rtdsfield_initialize();
378
379 for (i=0; i<256; i++) {
380 if (rtnl_rtdsfield_tab[i] &&
381 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
382 cache = rtnl_rtdsfield_tab[i];
383 res = i;
384 *id = res;
385 return 0;
386 }
387 }
388
389 res = strtoul(arg, &end, 16);
390 if (!end || end == arg || *end || res > 255)
391 return -1;
392 *id = res;
393 return 0;
394}
395