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