blob: 686326ff9196083051ae0fd28a413fd78086dff0 [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 McGrath275be872002-12-16 07:37:21 +000012
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000013#include "libbb.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000014#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015
Denis Vlasenko981b24d2006-09-28 22:36:23 +000016static void rtnl_tab_initialize(char *file, const char **tab, int size)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017{
18 char buf[512];
19 FILE *fp;
20
21 fp = fopen(file, "r");
22 if (!fp)
23 return;
24 while (fgets(buf, sizeof(buf), fp)) {
25 char *p = buf;
26 int id;
27 char namebuf[512];
28
29 while (*p == ' ' || *p == '\t')
30 p++;
31 if (*p == '#' || *p == '\n' || *p == 0)
32 continue;
33 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
34 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
35 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
36 sscanf(p, "%d %s #", &id, namebuf) != 2) {
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000037 bb_error_msg("database %s is corrupted at %s",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038 file, p);
39 return;
40 }
41
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000042 if (id < 0 || id > size)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000043 continue;
44
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000045 tab[id] = xstrdup(namebuf);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046 }
47 fclose(fp);
48}
49
50
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000051static const char **rtnl_rtprot_tab; /* [256] */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000052
53static void rtnl_rtprot_initialize(void)
54{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000055 static const char *const init_tab[] = {
56 "none",
57 "redirect",
58 "kernel",
59 "boot",
60 "static",
61 NULL,
62 NULL,
63 NULL,
64 "gated",
65 "ra",
66 "mrt",
67 "zebra",
68 "bird",
69 };
70 if (rtnl_rtprot_tab) return;
71 rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0]));
72 memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000073 rtnl_tab_initialize("/etc/iproute2/rt_protos",
74 rtnl_rtprot_tab, 256);
75}
76
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000077
78const char* rtnl_rtprot_n2a(int id, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000080 if (id < 0 || id >= 256) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081 snprintf(buf, len, "%d", id);
82 return buf;
83 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000084
85 rtnl_rtprot_initialize();
86
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000087 if (rtnl_rtprot_tab[id])
88 return rtnl_rtprot_tab[id];
89 snprintf(buf, len, "%d", id);
90 return buf;
91}
92
93int rtnl_rtprot_a2n(uint32_t *id, char *arg)
94{
Denis Vlasenko981b24d2006-09-28 22:36:23 +000095 static const char *cache = NULL;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000096 static unsigned long res;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097 int i;
98
99 if (cache && strcmp(cache, arg) == 0) {
100 *id = res;
101 return 0;
102 }
103
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000104 rtnl_rtprot_initialize();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000106 for (i = 0; i < 256; i++) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107 if (rtnl_rtprot_tab[i] &&
108 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
109 cache = rtnl_rtprot_tab[i];
110 res = i;
111 *id = res;
112 return 0;
113 }
114 }
115
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000116 res = bb_strtoul(arg, NULL, 0);
117 if (errno || res > 255)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 return -1;
119 *id = res;
120 return 0;
121}
122
123
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000124static const char **rtnl_rtscope_tab; /* [256] */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000125
126static void rtnl_rtscope_initialize(void)
127{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000128 if (rtnl_rtscope_tab) return;
129 rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0]));
130 rtnl_rtscope_tab[0] = "global";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000131 rtnl_rtscope_tab[255] = "nowhere";
132 rtnl_rtscope_tab[254] = "host";
133 rtnl_rtscope_tab[253] = "link";
134 rtnl_rtscope_tab[200] = "site";
135 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
136 rtnl_rtscope_tab, 256);
137}
138
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000139
140const char* rtnl_rtscope_n2a(int id, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000141{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000142 if (id < 0 || id >= 256) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000143 snprintf(buf, len, "%d", id);
144 return buf;
145 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000146
147 rtnl_rtscope_initialize();
148
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149 if (rtnl_rtscope_tab[id])
150 return rtnl_rtscope_tab[id];
151 snprintf(buf, len, "%d", id);
152 return buf;
153}
154
155int rtnl_rtscope_a2n(uint32_t *id, char *arg)
156{
Denis Vlasenko981b24d2006-09-28 22:36:23 +0000157 static const char *cache = NULL;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000158 static unsigned long res;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000159 int i;
160
161 if (cache && strcmp(cache, arg) == 0) {
162 *id = res;
163 return 0;
164 }
165
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000166 rtnl_rtscope_initialize();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000168 for (i = 0; i < 256; i++) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169 if (rtnl_rtscope_tab[i] &&
170 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
171 cache = rtnl_rtscope_tab[i];
172 res = i;
173 *id = res;
174 return 0;
175 }
176 }
177
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000178 res = bb_strtoul(arg, NULL, 0);
179 if (errno || res > 255)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180 return -1;
181 *id = res;
182 return 0;
183}
184
185
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000186static const char **rtnl_rtrealm_tab; /* [256] */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187
188static void rtnl_rtrealm_initialize(void)
189{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000190 if (rtnl_rtrealm_tab) return;
191 rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0]));
192 rtnl_rtrealm_tab[0] = "unknown";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193 rtnl_tab_initialize("/etc/iproute2/rt_realms",
194 rtnl_rtrealm_tab, 256);
195}
196
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000197
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000198int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
199{
Denis Vlasenko981b24d2006-09-28 22:36:23 +0000200 static const char *cache = NULL;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201 static unsigned long res;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000202 int i;
203
204 if (cache && strcmp(cache, arg) == 0) {
205 *id = res;
206 return 0;
207 }
208
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000209 rtnl_rtrealm_initialize();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000210
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000211 for (i = 0; i < 256; i++) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000212 if (rtnl_rtrealm_tab[i] &&
213 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
214 cache = rtnl_rtrealm_tab[i];
215 res = i;
216 *id = res;
217 return 0;
218 }
219 }
220
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000221 res = bb_strtoul(arg, NULL, 0);
222 if (errno || res > 255)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000223 return -1;
224 *id = res;
225 return 0;
226}
227
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000228#if ENABLE_FEATURE_IP_RULE
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000229const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000230{
Denis Vlasenkoe27f1562007-01-01 06:00:38 +0000231 if (id < 0 || id >= 256) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000232 snprintf(buf, len, "%d", id);
233 return buf;
234 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000235
236 rtnl_rtrealm_initialize();
237
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000238 if (rtnl_rtrealm_tab[id])
239 return rtnl_rtrealm_tab[id];
240 snprintf(buf, len, "%d", id);
241 return buf;
242}
243#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000244
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000246static const char **rtnl_rtdsfield_tab; /* [256] */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247
248static void rtnl_rtdsfield_initialize(void)
249{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000250 if (rtnl_rtdsfield_tab) return;
251 rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0]));
252 rtnl_rtdsfield_tab[0] = "0";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
254 rtnl_rtdsfield_tab, 256);
255}
256
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000257
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{
Denis Vlasenkoe27f1562007-01-01 06:00:38 +0000260 if (id < 0 || id >= 256) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000261 snprintf(buf, len, "%d", id);
262 return buf;
263 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000264
265 rtnl_rtdsfield_initialize();
266
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267 if (rtnl_rtdsfield_tab[id])
268 return rtnl_rtdsfield_tab[id];
269 snprintf(buf, len, "0x%02x", id);
270 return buf;
271}
272
273
274int rtnl_dsfield_a2n(uint32_t *id, char *arg)
275{
Denis Vlasenko981b24d2006-09-28 22:36:23 +0000276 static const char *cache = NULL;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 static unsigned long res;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 int i;
279
280 if (cache && strcmp(cache, arg) == 0) {
281 *id = res;
282 return 0;
283 }
284
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000285 rtnl_rtdsfield_initialize();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000286
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000287 for (i = 0; i < 256; i++) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288 if (rtnl_rtdsfield_tab[i] &&
289 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
290 cache = rtnl_rtdsfield_tab[i];
291 res = i;
292 *id = res;
293 return 0;
294 }
295 }
296
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000297 res = bb_strtoul(arg, NULL, 16);
298 if (errno || res > 255)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000299 return -1;
300 *id = res;
301 return 0;
302}
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000303
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000304
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000305#if ENABLE_FEATURE_IP_RULE
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000306static const char **rtnl_rttable_tab; /* [256] */
307
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000308static void rtnl_rttable_initialize(void)
309{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000310 if (rtnl_rtdsfield_tab) return;
311 rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0]));
312 rtnl_rttable_tab[0] = "unspec";
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000313 rtnl_rttable_tab[255] = "local";
314 rtnl_rttable_tab[254] = "main";
315 rtnl_rttable_tab[253] = "default";
316 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256);
317}
318
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000319
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000320const char *rtnl_rttable_n2a(int id, char *buf, int len)
321{
322 if (id < 0 || id >= 256) {
323 snprintf(buf, len, "%d", id);
324 return buf;
325 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000326
327 rtnl_rttable_initialize();
328
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000329 if (rtnl_rttable_tab[id])
330 return rtnl_rttable_tab[id];
331 snprintf(buf, len, "%d", id);
332 return buf;
333}
334
335int rtnl_rttable_a2n(uint32_t * id, char *arg)
336{
337 static char *cache = NULL;
338 static unsigned long res;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000339 int i;
340
341 if (cache && strcmp(cache, arg) == 0) {
342 *id = res;
343 return 0;
344 }
345
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000346 rtnl_rttable_initialize();
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000347
348 for (i = 0; i < 256; i++) {
349 if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
350 cache = (char*)rtnl_rttable_tab[i];
351 res = i;
352 *id = res;
353 return 0;
354 }
355 }
356
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000357 i = bb_strtoul(arg, NULL, 0);
358 if (errno || i > 255)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000359 return -1;
360 *id = i;
361 return 0;
362}
363
364#endif