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>
17 static void rtnl_tab_initialize(char *file, char **tab, int size)
22 fp = fopen(file, "r");
25 while (fgets(buf, sizeof(buf), fp)) {
30 while (*p == ' ' || *p == '\t')
32 if (*p == '#' || *p == '\n' || *p == 0)
34 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
35 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
36 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
37 sscanf(p, "%d %s #", &id, namebuf) != 2) {
38 fprintf(stderr, "Database %s is corrupted at %s\n",
46 tab[id] = strdup(namebuf);
52 static char * rtnl_rtprot_tab[256] = {
70 static int rtnl_rtprot_init;
72 static void rtnl_rtprot_initialize(void)
75 rtnl_tab_initialize("/etc/iproute2/rt_protos",
76 rtnl_rtprot_tab, 256);
79 char * rtnl_rtprot_n2a(int id, char *buf, int len)
81 if (id<0 || id>=256) {
82 snprintf(buf, len, "%d", id);
85 if (!rtnl_rtprot_tab[id]) {
86 if (!rtnl_rtprot_init)
87 rtnl_rtprot_initialize();
89 if (rtnl_rtprot_tab[id])
90 return rtnl_rtprot_tab[id];
91 snprintf(buf, len, "%d", id);
95 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
97 static char *cache = NULL;
98 static unsigned long res;
102 if (cache && strcmp(cache, arg) == 0) {
107 if (!rtnl_rtprot_init)
108 rtnl_rtprot_initialize();
110 for (i=0; i<256; i++) {
111 if (rtnl_rtprot_tab[i] &&
112 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
113 cache = rtnl_rtprot_tab[i];
120 res = strtoul(arg, &end, 0);
121 if (!end || end == arg || *end || res > 255)
129 static char * rtnl_rtscope_tab[256] = {
133 static int rtnl_rtscope_init;
135 static void rtnl_rtscope_initialize(void)
137 rtnl_rtscope_init = 1;
138 rtnl_rtscope_tab[255] = "nowhere";
139 rtnl_rtscope_tab[254] = "host";
140 rtnl_rtscope_tab[253] = "link";
141 rtnl_rtscope_tab[200] = "site";
142 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
143 rtnl_rtscope_tab, 256);
146 char * rtnl_rtscope_n2a(int id, char *buf, int len)
148 if (id<0 || id>=256) {
149 snprintf(buf, len, "%d", id);
152 if (!rtnl_rtscope_tab[id]) {
153 if (!rtnl_rtscope_init)
154 rtnl_rtscope_initialize();
156 if (rtnl_rtscope_tab[id])
157 return rtnl_rtscope_tab[id];
158 snprintf(buf, len, "%d", id);
162 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
164 static char *cache = NULL;
165 static unsigned long res;
169 if (cache && strcmp(cache, arg) == 0) {
174 if (!rtnl_rtscope_init)
175 rtnl_rtscope_initialize();
177 for (i=0; i<256; i++) {
178 if (rtnl_rtscope_tab[i] &&
179 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
180 cache = rtnl_rtscope_tab[i];
187 res = strtoul(arg, &end, 0);
188 if (!end || end == arg || *end || res > 255)
196 static char * rtnl_rtrealm_tab[256] = {
200 static int rtnl_rtrealm_init;
202 static void rtnl_rtrealm_initialize(void)
204 rtnl_rtrealm_init = 1;
205 rtnl_tab_initialize("/etc/iproute2/rt_realms",
206 rtnl_rtrealm_tab, 256);
209 char * rtnl_rtrealm_n2a(int id, char *buf, int len)
211 if (id<0 || id>=256) {
212 snprintf(buf, len, "%d", id);
215 if (!rtnl_rtrealm_tab[id]) {
216 if (!rtnl_rtrealm_init)
217 rtnl_rtrealm_initialize();
219 if (rtnl_rtrealm_tab[id])
220 return rtnl_rtrealm_tab[id];
221 snprintf(buf, len, "%d", id);
226 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
228 static char *cache = NULL;
229 static unsigned long res;
233 if (cache && strcmp(cache, arg) == 0) {
238 if (!rtnl_rtrealm_init)
239 rtnl_rtrealm_initialize();
241 for (i=0; i<256; i++) {
242 if (rtnl_rtrealm_tab[i] &&
243 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
244 cache = rtnl_rtrealm_tab[i];
251 res = strtoul(arg, &end, 0);
252 if (!end || end == arg || *end || res > 255)
260 static char * rtnl_rttable_tab[256] = {
264 static int rtnl_rttable_init;
266 static void rtnl_rttable_initialize(void)
268 rtnl_rttable_init = 1;
269 rtnl_rttable_tab[255] = "local";
270 rtnl_rttable_tab[254] = "main";
271 rtnl_tab_initialize("/etc/iproute2/rt_tables",
272 rtnl_rttable_tab, 256);
275 char * rtnl_rttable_n2a(int id, char *buf, int len)
277 if (id<0 || id>=256) {
278 snprintf(buf, len, "%d", id);
281 if (!rtnl_rttable_tab[id]) {
282 if (!rtnl_rttable_init)
283 rtnl_rttable_initialize();
285 if (rtnl_rttable_tab[id])
286 return rtnl_rttable_tab[id];
287 snprintf(buf, len, "%d", id);
291 int rtnl_rttable_a2n(uint32_t *id, char *arg)
293 static char *cache = NULL;
294 static unsigned long res;
298 if (cache && strcmp(cache, arg) == 0) {
303 if (!rtnl_rttable_init)
304 rtnl_rttable_initialize();
306 for (i=0; i<256; i++) {
307 if (rtnl_rttable_tab[i] &&
308 strcmp(rtnl_rttable_tab[i], arg) == 0) {
309 cache = rtnl_rttable_tab[i];
316 i = strtoul(arg, &end, 0);
317 if (!end || end == arg || *end || i > 255)
324 static char * rtnl_rtdsfield_tab[256] = {
328 static int rtnl_rtdsfield_init;
330 static void rtnl_rtdsfield_initialize(void)
332 rtnl_rtdsfield_init = 1;
333 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
334 rtnl_rtdsfield_tab, 256);
337 char * rtnl_dsfield_n2a(int id, char *buf, int len)
339 if (id<0 || id>=256) {
340 snprintf(buf, len, "%d", id);
343 if (!rtnl_rtdsfield_tab[id]) {
344 if (!rtnl_rtdsfield_init)
345 rtnl_rtdsfield_initialize();
347 if (rtnl_rtdsfield_tab[id])
348 return rtnl_rtdsfield_tab[id];
349 snprintf(buf, len, "0x%02x", id);
354 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
356 static char *cache = NULL;
357 static unsigned long res;
361 if (cache && strcmp(cache, arg) == 0) {
366 if (!rtnl_rtdsfield_init)
367 rtnl_rtdsfield_initialize();
369 for (i=0; i<256; i++) {
370 if (rtnl_rtdsfield_tab[i] &&
371 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
372 cache = rtnl_rtdsfield_tab[i];
379 res = strtoul(arg, &end, 16);
380 if (!end || end == arg || *end || res > 255)