]> git.proxmox.com Git - mirror_iproute2.git/blame - lib/rt_names.c
Preparation for 32 bit table IDs
[mirror_iproute2.git] / lib / rt_names.c
CommitLineData
aba5acdf
SH
1/*
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>
b88fd9f4
SH
19#include <sys/socket.h>
20
21#include <asm/types.h>
22#include <linux/rtnetlink.h>
23
24#include "rt_names.h"
aba5acdf
SH
25
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] = {
b88fd9f4
SH
62 [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",
40b6c62c 75 [RTPROT_NTK] = "ntk",
aba5acdf
SH
76};
77
78
79
80static int rtnl_rtprot_init;
81
82static void rtnl_rtprot_initialize(void)
83{
84 rtnl_rtprot_init = 1;
85 rtnl_tab_initialize("/etc/iproute2/rt_protos",
86 rtnl_rtprot_tab, 256);
87}
88
89char * rtnl_rtprot_n2a(int id, char *buf, int len)
90{
91 if (id<0 || id>=256) {
92 snprintf(buf, len, "%d", id);
93 return buf;
94 }
95 if (!rtnl_rtprot_tab[id]) {
96 if (!rtnl_rtprot_init)
97 rtnl_rtprot_initialize();
98 }
99 if (rtnl_rtprot_tab[id])
100 return rtnl_rtprot_tab[id];
101 snprintf(buf, len, "%d", id);
102 return buf;
103}
104
105int rtnl_rtprot_a2n(__u32 *id, char *arg)
106{
107 static char *cache = NULL;
108 static unsigned long res;
109 char *end;
110 int i;
111
112 if (cache && strcmp(cache, arg) == 0) {
113 *id = res;
114 return 0;
115 }
116
117 if (!rtnl_rtprot_init)
118 rtnl_rtprot_initialize();
119
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;
127 }
128 }
129
130 res = strtoul(arg, &end, 0);
131 if (!end || end == arg || *end || res > 255)
132 return -1;
133 *id = res;
134 return 0;
135}
136
137
138
139static char * rtnl_rtscope_tab[256] = {
140 "global",
141};
142
143static int rtnl_rtscope_init;
144
145static void rtnl_rtscope_initialize(void)
146{
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);
154}
155
156char * rtnl_rtscope_n2a(int id, char *buf, int len)
157{
158 if (id<0 || id>=256) {
159 snprintf(buf, len, "%d", id);
160 return buf;
161 }
162 if (!rtnl_rtscope_tab[id]) {
163 if (!rtnl_rtscope_init)
164 rtnl_rtscope_initialize();
165 }
166 if (rtnl_rtscope_tab[id])
167 return rtnl_rtscope_tab[id];
168 snprintf(buf, len, "%d", id);
169 return buf;
170}
171
172int rtnl_rtscope_a2n(__u32 *id, char *arg)
173{
174 static char *cache = NULL;
175 static unsigned long res;
176 char *end;
177 int i;
178
179 if (cache && strcmp(cache, arg) == 0) {
180 *id = res;
181 return 0;
182 }
183
184 if (!rtnl_rtscope_init)
185 rtnl_rtscope_initialize();
186
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;
194 }
195 }
196
197 res = strtoul(arg, &end, 0);
198 if (!end || end == arg || *end || res > 255)
199 return -1;
200 *id = res;
201 return 0;
202}
203
204
205
206static char * rtnl_rtrealm_tab[256] = {
207 "unknown",
208};
209
210static int rtnl_rtrealm_init;
211
212static void rtnl_rtrealm_initialize(void)
213{
214 rtnl_rtrealm_init = 1;
215 rtnl_tab_initialize("/etc/iproute2/rt_realms",
216 rtnl_rtrealm_tab, 256);
217}
218
219char * rtnl_rtrealm_n2a(int id, char *buf, int len)
220{
221 if (id<0 || id>=256) {
222 snprintf(buf, len, "%d", id);
223 return buf;
224 }
225 if (!rtnl_rtrealm_tab[id]) {
226 if (!rtnl_rtrealm_init)
227 rtnl_rtrealm_initialize();
228 }
229 if (rtnl_rtrealm_tab[id])
230 return rtnl_rtrealm_tab[id];
231 snprintf(buf, len, "%d", id);
232 return buf;
233}
234
235
236int rtnl_rtrealm_a2n(__u32 *id, char *arg)
237{
238 static char *cache = NULL;
239 static unsigned long res;
240 char *end;
241 int i;
242
243 if (cache && strcmp(cache, arg) == 0) {
244 *id = res;
245 return 0;
246 }
247
248 if (!rtnl_rtrealm_init)
249 rtnl_rtrealm_initialize();
250
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;
258 }
259 }
260
261 res = strtoul(arg, &end, 0);
262 if (!end || end == arg || *end || res > 255)
263 return -1;
264 *id = res;
265 return 0;
266}
267
268
269
270static char * rtnl_rttable_tab[256] = {
271 "unspec",
272};
273
274static int rtnl_rttable_init;
275
276static void rtnl_rttable_initialize(void)
277{
278 rtnl_rttable_init = 1;
279 rtnl_rttable_tab[255] = "local";
280 rtnl_rttable_tab[254] = "main";
010b0f65 281 rtnl_rttable_tab[253] = "default";
aba5acdf
SH
282 rtnl_tab_initialize("/etc/iproute2/rt_tables",
283 rtnl_rttable_tab, 256);
284}
285
286char * rtnl_rttable_n2a(int id, char *buf, int len)
287{
288 if (id<0 || id>=256) {
289 snprintf(buf, len, "%d", id);
290 return buf;
291 }
292 if (!rtnl_rttable_tab[id]) {
293 if (!rtnl_rttable_init)
294 rtnl_rttable_initialize();
295 }
296 if (rtnl_rttable_tab[id])
297 return rtnl_rttable_tab[id];
298 snprintf(buf, len, "%d", id);
299 return buf;
300}
301
302int rtnl_rttable_a2n(__u32 *id, char *arg)
303{
304 static char *cache = NULL;
305 static unsigned long res;
306 char *end;
307 int i;
308
309 if (cache && strcmp(cache, arg) == 0) {
310 *id = res;
311 return 0;
312 }
313
314 if (!rtnl_rttable_init)
315 rtnl_rttable_initialize();
316
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;
324 }
325 }
326
327 i = strtoul(arg, &end, 0);
328 if (!end || end == arg || *end || i > 255)
329 return -1;
330 *id = i;
331 return 0;
332}
333
334
335static char * rtnl_rtdsfield_tab[256] = {
336 "0",
337};
338
339static int rtnl_rtdsfield_init;
340
341static void rtnl_rtdsfield_initialize(void)
342{
343 rtnl_rtdsfield_init = 1;
344 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
345 rtnl_rtdsfield_tab, 256);
346}
347
348char * rtnl_dsfield_n2a(int id, char *buf, int len)
349{
350 if (id<0 || id>=256) {
351 snprintf(buf, len, "%d", id);
352 return buf;
353 }
354 if (!rtnl_rtdsfield_tab[id]) {
355 if (!rtnl_rtdsfield_init)
356 rtnl_rtdsfield_initialize();
357 }
358 if (rtnl_rtdsfield_tab[id])
359 return rtnl_rtdsfield_tab[id];
360 snprintf(buf, len, "0x%02x", id);
361 return buf;
362}
363
364
365int rtnl_dsfield_a2n(__u32 *id, char *arg)
366{
367 static char *cache = NULL;
368 static unsigned long res;
369 char *end;
370 int i;
371
372 if (cache && strcmp(cache, arg) == 0) {
373 *id = res;
374 return 0;
375 }
376
377 if (!rtnl_rtdsfield_init)
378 rtnl_rtdsfield_initialize();
379
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;
387 }
388 }
389
390 res = strtoul(arg, &end, 16);
391 if (!end || end == arg || *end || res > 255)
392 return -1;
393 *id = res;
394 return 0;
395}
396