1 /* vi: set sw=4 ts=4: */
3 * rt_names.c rtnetlink names DB.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 static void rtnl_tab_initialize(char *file, const char **tab, int size)
24 fp = fopen(file, "r");
27 while (fgets(buf, sizeof(buf), fp)) {
32 while (*p == ' ' || *p == '\t')
34 if (*p == '#' || *p == '\n' || *p == 0)
36 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
37 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
38 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
39 sscanf(p, "%d %s #", &id, namebuf) != 2) {
40 fprintf(stderr, "Database %s is corrupted at %s\n",
48 tab[id] = strdup(namebuf);
54 static const char * rtnl_rtprot_tab[256] = {
72 static int rtnl_rtprot_init;
74 static void rtnl_rtprot_initialize(void)
77 rtnl_tab_initialize("/etc/iproute2/rt_protos",
78 rtnl_rtprot_tab, 256);
81 const char * rtnl_rtprot_n2a(int id, char *buf, int len)
83 if (id<0 || id>=256) {
84 snprintf(buf, len, "%d", id);
87 if (!rtnl_rtprot_tab[id]) {
88 if (!rtnl_rtprot_init)
89 rtnl_rtprot_initialize();
91 if (rtnl_rtprot_tab[id])
92 return rtnl_rtprot_tab[id];
93 snprintf(buf, len, "%d", id);
97 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
99 static const char *cache = NULL;
100 static unsigned long res;
104 if (cache && strcmp(cache, arg) == 0) {
109 if (!rtnl_rtprot_init)
110 rtnl_rtprot_initialize();
112 for (i=0; i<256; i++) {
113 if (rtnl_rtprot_tab[i] &&
114 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
115 cache = rtnl_rtprot_tab[i];
122 res = strtoul(arg, &end, 0);
123 if (!end || end == arg || *end || res > 255)
131 static const char * rtnl_rtscope_tab[256] = {
135 static int rtnl_rtscope_init;
137 static void rtnl_rtscope_initialize(void)
139 rtnl_rtscope_init = 1;
140 rtnl_rtscope_tab[255] = "nowhere";
141 rtnl_rtscope_tab[254] = "host";
142 rtnl_rtscope_tab[253] = "link";
143 rtnl_rtscope_tab[200] = "site";
144 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
145 rtnl_rtscope_tab, 256);
148 const char * rtnl_rtscope_n2a(int id, char *buf, int len)
150 if (id<0 || id>=256) {
151 snprintf(buf, len, "%d", id);
154 if (!rtnl_rtscope_tab[id]) {
155 if (!rtnl_rtscope_init)
156 rtnl_rtscope_initialize();
158 if (rtnl_rtscope_tab[id])
159 return rtnl_rtscope_tab[id];
160 snprintf(buf, len, "%d", id);
164 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
166 static const char *cache = NULL;
167 static unsigned long res;
171 if (cache && strcmp(cache, arg) == 0) {
176 if (!rtnl_rtscope_init)
177 rtnl_rtscope_initialize();
179 for (i=0; i<256; i++) {
180 if (rtnl_rtscope_tab[i] &&
181 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
182 cache = rtnl_rtscope_tab[i];
189 res = strtoul(arg, &end, 0);
190 if (!end || end == arg || *end || res > 255)
198 static const char * rtnl_rtrealm_tab[256] = {
202 static int rtnl_rtrealm_init;
204 static void rtnl_rtrealm_initialize(void)
206 rtnl_rtrealm_init = 1;
207 rtnl_tab_initialize("/etc/iproute2/rt_realms",
208 rtnl_rtrealm_tab, 256);
211 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
213 static const char *cache = NULL;
214 static unsigned long res;
218 if (cache && strcmp(cache, arg) == 0) {
223 if (!rtnl_rtrealm_init)
224 rtnl_rtrealm_initialize();
226 for (i=0; i<256; i++) {
227 if (rtnl_rtrealm_tab[i] &&
228 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
229 cache = rtnl_rtrealm_tab[i];
236 res = strtoul(arg, &end, 0);
237 if (!end || end == arg || *end || res > 255)
243 #if ENABLE_FEATURE_IP_RULE
244 const char * rtnl_rtrealm_n2a(int id, char *buf, int len)
246 if (id<0 || id>=256) {
247 snprintf(buf, len, "%d", id);
250 if (!rtnl_rtrealm_tab[id]) {
251 if (!rtnl_rtrealm_init)
252 rtnl_rtrealm_initialize();
254 if (rtnl_rtrealm_tab[id])
255 return rtnl_rtrealm_tab[id];
256 snprintf(buf, len, "%d", id);
261 static const char * rtnl_rtdsfield_tab[256] = {
265 static int rtnl_rtdsfield_init;
267 static void rtnl_rtdsfield_initialize(void)
269 rtnl_rtdsfield_init = 1;
270 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
271 rtnl_rtdsfield_tab, 256);
274 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
276 if (id<0 || id>=256) {
277 snprintf(buf, len, "%d", id);
280 if (!rtnl_rtdsfield_tab[id]) {
281 if (!rtnl_rtdsfield_init)
282 rtnl_rtdsfield_initialize();
284 if (rtnl_rtdsfield_tab[id])
285 return rtnl_rtdsfield_tab[id];
286 snprintf(buf, len, "0x%02x", id);
291 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
293 static const char *cache = NULL;
294 static unsigned long res;
298 if (cache && strcmp(cache, arg) == 0) {
303 if (!rtnl_rtdsfield_init)
304 rtnl_rtdsfield_initialize();
306 for (i=0; i<256; i++) {
307 if (rtnl_rtdsfield_tab[i] &&
308 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
309 cache = rtnl_rtdsfield_tab[i];
316 res = strtoul(arg, &end, 16);
317 if (!end || end == arg || *end || res > 255)
323 #if ENABLE_FEATURE_IP_RULE
324 static int rtnl_rttable_init;
325 static const char * rtnl_rttable_tab[256] = {
328 static void rtnl_rttable_initialize(void)
330 rtnl_rttable_init = 1;
331 rtnl_rttable_tab[255] = "local";
332 rtnl_rttable_tab[254] = "main";
333 rtnl_rttable_tab[253] = "default";
334 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256);
337 const char *rtnl_rttable_n2a(int id, char *buf, int len)
339 if (id < 0 || id >= 256) {
340 snprintf(buf, len, "%d", id);
343 if (!rtnl_rttable_tab[id]) {
344 if (!rtnl_rttable_init)
345 rtnl_rttable_initialize();
347 if (rtnl_rttable_tab[id])
348 return rtnl_rttable_tab[id];
349 snprintf(buf, len, "%d", id);
353 int rtnl_rttable_a2n(uint32_t * id, char *arg)
355 static char *cache = NULL;
356 static unsigned long res;
360 if (cache && strcmp(cache, arg) == 0) {
365 if (!rtnl_rttable_init)
366 rtnl_rttable_initialize();
368 for (i = 0; i < 256; i++) {
369 if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
370 cache = (char*)rtnl_rttable_tab[i];
377 i = strtoul(arg, &end, 0);
378 if (!end || end == arg || *end || i > 255)