blob: 5181ba4b7dd4704f9256620d00b63aa08345e61a [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
3 * rt_names.c rtnetlink names DB.
4 *
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.
9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000012#include <stdio.h>
13#include <stdlib.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014#include <string.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000015
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016#include <stdint.h>
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000017#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018
19static void rtnl_tab_initialize(char *file, char **tab, int size)
20{
21 char buf[512];
22 FILE *fp;
23
24 fp = fopen(file, "r");
25 if (!fp)
26 return;
27 while (fgets(buf, sizeof(buf), fp)) {
28 char *p = buf;
29 int id;
30 char namebuf[512];
31
32 while (*p == ' ' || *p == '\t')
33 p++;
34 if (*p == '#' || *p == '\n' || *p == 0)
35 continue;
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",
41 file, p);
42 return;
43 }
44
45 if (id<0 || id>size)
46 continue;
47
48 tab[id] = strdup(namebuf);
49 }
50 fclose(fp);
51}
52
53
54static char * rtnl_rtprot_tab[256] = {
55 "none",
56 "redirect",
57 "kernel",
58 "boot",
59 "static",
60 NULL,
61 NULL,
62 NULL,
63 "gated",
64 "ra",
65 "mrt",
66 "zebra",
67 "bird",
68};
69
70
71
72static int rtnl_rtprot_init;
73
74static void rtnl_rtprot_initialize(void)
75{
76 rtnl_rtprot_init = 1;
77 rtnl_tab_initialize("/etc/iproute2/rt_protos",
78 rtnl_rtprot_tab, 256);
79}
80
Eric Andersen14f5c8d2005-04-16 19:39:00 +000081const char * rtnl_rtprot_n2a(int id, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082{
83 if (id<0 || id>=256) {
84 snprintf(buf, len, "%d", id);
85 return buf;
86 }
87 if (!rtnl_rtprot_tab[id]) {
88 if (!rtnl_rtprot_init)
89 rtnl_rtprot_initialize();
90 }
91 if (rtnl_rtprot_tab[id])
92 return rtnl_rtprot_tab[id];
93 snprintf(buf, len, "%d", id);
94 return buf;
95}
96
97int rtnl_rtprot_a2n(uint32_t *id, char *arg)
98{
99 static char *cache = NULL;
100 static unsigned long res;
101 char *end;
102 int i;
103
104 if (cache && strcmp(cache, arg) == 0) {
105 *id = res;
106 return 0;
107 }
108
109 if (!rtnl_rtprot_init)
110 rtnl_rtprot_initialize();
111
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];
116 res = i;
117 *id = res;
118 return 0;
119 }
120 }
121
122 res = strtoul(arg, &end, 0);
123 if (!end || end == arg || *end || res > 255)
124 return -1;
125 *id = res;
126 return 0;
127}
128
129
130
131static char * rtnl_rtscope_tab[256] = {
132 "global",
133};
134
135static int rtnl_rtscope_init;
136
137static void rtnl_rtscope_initialize(void)
138{
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);
146}
147
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000148const char * rtnl_rtscope_n2a(int id, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149{
150 if (id<0 || id>=256) {
151 snprintf(buf, len, "%d", id);
152 return buf;
153 }
154 if (!rtnl_rtscope_tab[id]) {
155 if (!rtnl_rtscope_init)
156 rtnl_rtscope_initialize();
157 }
158 if (rtnl_rtscope_tab[id])
159 return rtnl_rtscope_tab[id];
160 snprintf(buf, len, "%d", id);
161 return buf;
162}
163
164int rtnl_rtscope_a2n(uint32_t *id, char *arg)
165{
166 static char *cache = NULL;
167 static unsigned long res;
168 char *end;
169 int i;
170
171 if (cache && strcmp(cache, arg) == 0) {
172 *id = res;
173 return 0;
174 }
175
176 if (!rtnl_rtscope_init)
177 rtnl_rtscope_initialize();
178
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];
183 res = i;
184 *id = res;
185 return 0;
186 }
187 }
188
189 res = strtoul(arg, &end, 0);
190 if (!end || end == arg || *end || res > 255)
191 return -1;
192 *id = res;
193 return 0;
194}
195
196
197
198static char * rtnl_rtrealm_tab[256] = {
199 "unknown",
200};
201
202static int rtnl_rtrealm_init;
203
204static void rtnl_rtrealm_initialize(void)
205{
206 rtnl_rtrealm_init = 1;
207 rtnl_tab_initialize("/etc/iproute2/rt_realms",
208 rtnl_rtrealm_tab, 256);
209}
210
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000211int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
212{
213 static char *cache = NULL;
214 static unsigned long res;
215 char *end;
216 int i;
217
218 if (cache && strcmp(cache, arg) == 0) {
219 *id = res;
220 return 0;
221 }
222
223 if (!rtnl_rtrealm_init)
224 rtnl_rtrealm_initialize();
225
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];
230 res = i;
231 *id = res;
232 return 0;
233 }
234 }
235
236 res = strtoul(arg, &end, 0);
237 if (!end || end == arg || *end || res > 255)
238 return -1;
239 *id = res;
240 return 0;
241}
242
243
244
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245static char * rtnl_rtdsfield_tab[256] = {
246 "0",
247};
248
249static int rtnl_rtdsfield_init;
250
251static void rtnl_rtdsfield_initialize(void)
252{
253 rtnl_rtdsfield_init = 1;
254 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
255 rtnl_rtdsfield_tab, 256);
256}
257
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000258const char * rtnl_dsfield_n2a(int id, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259{
260 if (id<0 || id>=256) {
261 snprintf(buf, len, "%d", id);
262 return buf;
263 }
264 if (!rtnl_rtdsfield_tab[id]) {
265 if (!rtnl_rtdsfield_init)
266 rtnl_rtdsfield_initialize();
267 }
268 if (rtnl_rtdsfield_tab[id])
269 return rtnl_rtdsfield_tab[id];
270 snprintf(buf, len, "0x%02x", id);
271 return buf;
272}
273
274
275int rtnl_dsfield_a2n(uint32_t *id, char *arg)
276{
277 static char *cache = NULL;
278 static unsigned long res;
279 char *end;
280 int i;
281
282 if (cache && strcmp(cache, arg) == 0) {
283 *id = res;
284 return 0;
285 }
286
287 if (!rtnl_rtdsfield_init)
288 rtnl_rtdsfield_initialize();
289
290 for (i=0; i<256; i++) {
291 if (rtnl_rtdsfield_tab[i] &&
292 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
293 cache = rtnl_rtdsfield_tab[i];
294 res = i;
295 *id = res;
296 return 0;
297 }
298 }
299
300 res = strtoul(arg, &end, 16);
301 if (!end || end == arg || *end || res > 255)
302 return -1;
303 *id = res;
304 return 0;
305}
306