Kill off some unused code that was wasting several k, as noticed by
[oweals/busybox.git] / networking / libiproute / rt_names.c
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 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <stdint.h>
16 #include "rt_names.h"
17
18 static 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
53 static 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
71 static int rtnl_rtprot_init;
72
73 static 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
80 const char * rtnl_rtprot_n2a(int id, char *buf, int len)
81 {
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
96 int 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
130 static char * rtnl_rtscope_tab[256] = {
131         "global",
132 };
133
134 static int rtnl_rtscope_init;
135
136 static 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
147 const char * rtnl_rtscope_n2a(int id, char *buf, int len)
148 {
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
163 int 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
197 static char * rtnl_rtrealm_tab[256] = {
198         "unknown",
199 };
200
201 static int rtnl_rtrealm_init;
202
203 static 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
210 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
211 {
212         static char *cache = NULL;
213         static unsigned long res;
214         char *end;
215         int i;
216
217         if (cache && strcmp(cache, arg) == 0) {
218                 *id = res;
219                 return 0;
220         }
221
222         if (!rtnl_rtrealm_init)
223                 rtnl_rtrealm_initialize();
224
225         for (i=0; i<256; i++) {
226                 if (rtnl_rtrealm_tab[i] &&
227                     strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
228                         cache = rtnl_rtrealm_tab[i];
229                         res = i;
230                         *id = res;
231                         return 0;
232                 }
233         }
234
235         res = strtoul(arg, &end, 0);
236         if (!end || end == arg || *end || res > 255)
237                 return -1;
238         *id = res;
239         return 0;
240 }
241
242
243
244 static char * rtnl_rtdsfield_tab[256] = {
245         "0",
246 };
247
248 static int rtnl_rtdsfield_init;
249
250 static void rtnl_rtdsfield_initialize(void)
251 {
252         rtnl_rtdsfield_init = 1;
253         rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
254                             rtnl_rtdsfield_tab, 256);
255 }
256
257 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
258 {
259         if (id<0 || id>=256) {
260                 snprintf(buf, len, "%d", id);
261                 return buf;
262         }
263         if (!rtnl_rtdsfield_tab[id]) {
264                 if (!rtnl_rtdsfield_init)
265                         rtnl_rtdsfield_initialize();
266         }
267         if (rtnl_rtdsfield_tab[id])
268                 return rtnl_rtdsfield_tab[id];
269         snprintf(buf, len, "0x%02x", id);
270         return buf;
271 }
272
273
274 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
275 {
276         static char *cache = NULL;
277         static unsigned long res;
278         char *end;
279         int i;
280
281         if (cache && strcmp(cache, arg) == 0) {
282                 *id = res;
283                 return 0;
284         }
285
286         if (!rtnl_rtdsfield_init)
287                 rtnl_rtdsfield_initialize();
288
289         for (i=0; i<256; i++) {
290                 if (rtnl_rtdsfield_tab[i] &&
291                     strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
292                         cache = rtnl_rtdsfield_tab[i];
293                         res = i;
294                         *id = res;
295                         return 0;
296                 }
297         }
298
299         res = strtoul(arg, &end, 16);
300         if (!end || end == arg || *end || res > 255)
301                 return -1;
302         *id = res;
303         return 0;
304 }
305