MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / iproute2 / lib / rt_names.c
blob05046c2e0a499eb9b7c559c3cab897bf23e0a5dc
1 /*
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>
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>
19 #include <sys/socket.h>
21 #include <asm/types.h>
22 #include <linux/rtnetlink.h>
24 #include "rt_names.h"
26 static void rtnl_tab_initialize(char *file, char **tab, int size)
28 char buf[512];
29 FILE *fp;
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];
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;
52 if (id<0 || id>size)
53 continue;
55 tab[id] = strdup(namebuf);
57 fclose(fp);
61 static char * rtnl_rtprot_tab[256] = {
62 [RTPROT_UNSPEC] = "none",
63 [RTPROT_REDIRECT] ="redirect",
64 [RTPROT_KERNEL] = "kernel",
65 [RTPROT_BOOT] = "boot",
66 [RTPROT_STATIC] = "static",
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",
75 [RTPROT_NTK] = "ntk",
80 static int rtnl_rtprot_init;
82 static void rtnl_rtprot_initialize(void)
84 rtnl_rtprot_init = 1;
85 rtnl_tab_initialize("/etc/iproute2/rt_protos",
86 rtnl_rtprot_tab, 256);
89 char * rtnl_rtprot_n2a(int id, char *buf, int len)
91 if (id<0 || id>=256) {
92 snprintf(buf, len, "%d", id);
93 return buf;
95 if (!rtnl_rtprot_tab[id]) {
96 if (!rtnl_rtprot_init)
97 rtnl_rtprot_initialize();
99 if (rtnl_rtprot_tab[id])
100 return rtnl_rtprot_tab[id];
101 snprintf(buf, len, "%d", id);
102 return buf;
105 int rtnl_rtprot_a2n(__u32 *id, char *arg)
107 static char *cache = NULL;
108 static unsigned long res;
109 char *end;
110 int i;
112 if (cache && strcmp(cache, arg) == 0) {
113 *id = res;
114 return 0;
117 if (!rtnl_rtprot_init)
118 rtnl_rtprot_initialize();
120 for (i=0; i<256; i++) {
121 if (rtnl_rtprot_tab[i] &&
122 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
123 cache = rtnl_rtprot_tab[i];
124 res = i;
125 *id = res;
126 return 0;
130 res = strtoul(arg, &end, 0);
131 if (!end || end == arg || *end || res > 255)
132 return -1;
133 *id = res;
134 return 0;
139 static char * rtnl_rtscope_tab[256] = {
140 "global",
143 static int rtnl_rtscope_init;
145 static void rtnl_rtscope_initialize(void)
147 rtnl_rtscope_init = 1;
148 rtnl_rtscope_tab[255] = "nowhere";
149 rtnl_rtscope_tab[254] = "host";
150 rtnl_rtscope_tab[253] = "link";
151 rtnl_rtscope_tab[200] = "site";
152 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
153 rtnl_rtscope_tab, 256);
156 char * rtnl_rtscope_n2a(int id, char *buf, int len)
158 if (id<0 || id>=256) {
159 snprintf(buf, len, "%d", id);
160 return buf;
162 if (!rtnl_rtscope_tab[id]) {
163 if (!rtnl_rtscope_init)
164 rtnl_rtscope_initialize();
166 if (rtnl_rtscope_tab[id])
167 return rtnl_rtscope_tab[id];
168 snprintf(buf, len, "%d", id);
169 return buf;
172 int rtnl_rtscope_a2n(__u32 *id, char *arg)
174 static char *cache = NULL;
175 static unsigned long res;
176 char *end;
177 int i;
179 if (cache && strcmp(cache, arg) == 0) {
180 *id = res;
181 return 0;
184 if (!rtnl_rtscope_init)
185 rtnl_rtscope_initialize();
187 for (i=0; i<256; i++) {
188 if (rtnl_rtscope_tab[i] &&
189 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
190 cache = rtnl_rtscope_tab[i];
191 res = i;
192 *id = res;
193 return 0;
197 res = strtoul(arg, &end, 0);
198 if (!end || end == arg || *end || res > 255)
199 return -1;
200 *id = res;
201 return 0;
206 static char * rtnl_rtrealm_tab[256] = {
207 "unknown",
210 static int rtnl_rtrealm_init;
212 static void rtnl_rtrealm_initialize(void)
214 rtnl_rtrealm_init = 1;
215 rtnl_tab_initialize("/etc/iproute2/rt_realms",
216 rtnl_rtrealm_tab, 256);
219 char * rtnl_rtrealm_n2a(int id, char *buf, int len)
221 if (id<0 || id>=256) {
222 snprintf(buf, len, "%d", id);
223 return buf;
225 if (!rtnl_rtrealm_tab[id]) {
226 if (!rtnl_rtrealm_init)
227 rtnl_rtrealm_initialize();
229 if (rtnl_rtrealm_tab[id])
230 return rtnl_rtrealm_tab[id];
231 snprintf(buf, len, "%d", id);
232 return buf;
236 int rtnl_rtrealm_a2n(__u32 *id, char *arg)
238 static char *cache = NULL;
239 static unsigned long res;
240 char *end;
241 int i;
243 if (cache && strcmp(cache, arg) == 0) {
244 *id = res;
245 return 0;
248 if (!rtnl_rtrealm_init)
249 rtnl_rtrealm_initialize();
251 for (i=0; i<256; i++) {
252 if (rtnl_rtrealm_tab[i] &&
253 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
254 cache = rtnl_rtrealm_tab[i];
255 res = i;
256 *id = res;
257 return 0;
261 res = strtoul(arg, &end, 0);
262 if (!end || end == arg || *end || res > 255)
263 return -1;
264 *id = res;
265 return 0;
270 static char * rtnl_rttable_tab[256] = {
271 "unspec",
274 static int rtnl_rttable_init;
276 static void rtnl_rttable_initialize(void)
278 rtnl_rttable_init = 1;
279 rtnl_rttable_tab[255] = "local";
280 rtnl_rttable_tab[254] = "main";
281 rtnl_rttable_tab[253] = "default";
282 rtnl_tab_initialize("/etc/iproute2/rt_tables",
283 rtnl_rttable_tab, 256);
286 char * rtnl_rttable_n2a(int id, char *buf, int len)
288 if (id<0 || id>=256) {
289 snprintf(buf, len, "%d", id);
290 return buf;
292 if (!rtnl_rttable_tab[id]) {
293 if (!rtnl_rttable_init)
294 rtnl_rttable_initialize();
296 if (rtnl_rttable_tab[id])
297 return rtnl_rttable_tab[id];
298 snprintf(buf, len, "%d", id);
299 return buf;
302 int rtnl_rttable_a2n(__u32 *id, char *arg)
304 static char *cache = NULL;
305 static unsigned long res;
306 char *end;
307 int i;
309 if (cache && strcmp(cache, arg) == 0) {
310 *id = res;
311 return 0;
314 if (!rtnl_rttable_init)
315 rtnl_rttable_initialize();
317 for (i=0; i<256; i++) {
318 if (rtnl_rttable_tab[i] &&
319 strcmp(rtnl_rttable_tab[i], arg) == 0) {
320 cache = rtnl_rttable_tab[i];
321 res = i;
322 *id = res;
323 return 0;
327 i = strtoul(arg, &end, 0);
328 if (!end || end == arg || *end || i > 255)
329 return -1;
330 *id = i;
331 return 0;
335 static char * rtnl_rtdsfield_tab[256] = {
336 "0",
339 static int rtnl_rtdsfield_init;
341 static void rtnl_rtdsfield_initialize(void)
343 rtnl_rtdsfield_init = 1;
344 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
345 rtnl_rtdsfield_tab, 256);
348 char * rtnl_dsfield_n2a(int id, char *buf, int len)
350 if (id<0 || id>=256) {
351 snprintf(buf, len, "%d", id);
352 return buf;
354 if (!rtnl_rtdsfield_tab[id]) {
355 if (!rtnl_rtdsfield_init)
356 rtnl_rtdsfield_initialize();
358 if (rtnl_rtdsfield_tab[id])
359 return rtnl_rtdsfield_tab[id];
360 snprintf(buf, len, "0x%02x", id);
361 return buf;
365 int rtnl_dsfield_a2n(__u32 *id, char *arg)
367 static char *cache = NULL;
368 static unsigned long res;
369 char *end;
370 int i;
372 if (cache && strcmp(cache, arg) == 0) {
373 *id = res;
374 return 0;
377 if (!rtnl_rtdsfield_init)
378 rtnl_rtdsfield_initialize();
380 for (i=0; i<256; i++) {
381 if (rtnl_rtdsfield_tab[i] &&
382 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
383 cache = rtnl_rtdsfield_tab[i];
384 res = i;
385 *id = res;
386 return 0;
390 res = strtoul(arg, &end, 16);
391 if (!end || end == arg || *end || res > 255)
392 return -1;
393 *id = res;
394 return 0;