libiproute: use itoa() where appropriate
[oweals/busybox.git] / networking / libiproute / rt_names.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version
6  * 2 of the License, or (at your option) any later version.
7  *
8  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  */
10 #include "libbb.h"
11 #include "rt_names.h"
12
13 typedef struct rtnl_tab_t {
14         const char *cached_str;
15         unsigned cached_result;
16         const char *tab[256];
17 } rtnl_tab_t;
18
19 static void rtnl_tab_initialize(const char *file, const char **tab)
20 {
21         char *token[2];
22         parser_t *parser = config_open2(file, fopen_for_read);
23
24         while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
25                 unsigned id = bb_strtou(token[0], NULL, 0);
26                 if (id > 256) {
27                         bb_error_msg("database %s is corrupted at line %d",
28                                 file, parser->lineno);
29                         break;
30                 }
31                 tab[id] = xstrdup(token[1]);
32         }
33         config_close(parser);
34 }
35
36 static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
37 {
38         unsigned i;
39
40         if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
41                 *id = tab->cached_result;
42                 return 0;
43         }
44
45         for (i = 0; i < 256; i++) {
46                 if (tab->tab[i]
47                  && strcmp(tab->tab[i], arg) == 0
48                 ) {
49                         tab->cached_str = tab->tab[i];
50                         tab->cached_result = i;
51                         *id = i;
52                         return 0;
53                 }
54         }
55
56         i = bb_strtou(arg, NULL, base);
57         if (i > 255)
58                 return -1;
59         *id = i;
60         return 0;
61 }
62
63
64 static rtnl_tab_t *rtnl_rtprot_tab;
65
66 static void rtnl_rtprot_initialize(void)
67 {
68         static const char *const init_tab[] = {
69                 "none",
70                 "redirect",
71                 "kernel",
72                 "boot",
73                 "static",
74                 NULL,
75                 NULL,
76                 NULL,
77                 "gated",
78                 "ra",
79                 "mrt",
80                 "zebra",
81                 "bird",
82         };
83
84         if (rtnl_rtprot_tab)
85                 return;
86         rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
87         memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
88         rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab);
89 }
90
91 #if 0 /* UNUSED */
92 const char* FAST_FUNC rtnl_rtprot_n2a(int id)
93 {
94         if (id < 0 || id >= 256) {
95                 return itoa(id);
96         }
97
98         rtnl_rtprot_initialize();
99
100         if (rtnl_rtprot_tab->tab[id])
101                 return rtnl_rtprot_tab->tab[id];
102         return itoa(id);
103 }
104 #endif
105
106 int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg)
107 {
108         rtnl_rtprot_initialize();
109         return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
110 }
111
112
113 static rtnl_tab_t *rtnl_rtscope_tab;
114
115 static void rtnl_rtscope_initialize(void)
116 {
117         if (rtnl_rtscope_tab)
118                 return;
119         rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
120         rtnl_rtscope_tab->tab[0] = "global";
121         rtnl_rtscope_tab->tab[255] = "nowhere";
122         rtnl_rtscope_tab->tab[254] = "host";
123         rtnl_rtscope_tab->tab[253] = "link";
124         rtnl_rtscope_tab->tab[200] = "site";
125         rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab);
126 }
127
128 const char* FAST_FUNC rtnl_rtscope_n2a(int id)
129 {
130         if (id < 0 || id >= 256) {
131                 return itoa(id);
132         }
133
134         rtnl_rtscope_initialize();
135
136         if (rtnl_rtscope_tab->tab[id])
137                 return rtnl_rtscope_tab->tab[id];
138         return itoa(id);
139 }
140
141 int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg)
142 {
143         rtnl_rtscope_initialize();
144         return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
145 }
146
147
148 static rtnl_tab_t *rtnl_rtrealm_tab;
149
150 static void rtnl_rtrealm_initialize(void)
151 {
152         if (rtnl_rtrealm_tab) return;
153         rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
154         rtnl_rtrealm_tab->tab[0] = "unknown";
155         rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab);
156 }
157
158 int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
159 {
160         rtnl_rtrealm_initialize();
161         return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
162 }
163
164 #if ENABLE_FEATURE_IP_RULE
165 const char* FAST_FUNC rtnl_rtrealm_n2a(int id)
166 {
167         if (id < 0 || id >= 256) {
168                 return itoa(id);
169         }
170
171         rtnl_rtrealm_initialize();
172
173         if (rtnl_rtrealm_tab->tab[id])
174                 return rtnl_rtrealm_tab->tab[id];
175         return itoa(id);
176 }
177 #endif
178
179
180 static rtnl_tab_t *rtnl_rtdsfield_tab;
181
182 static void rtnl_rtdsfield_initialize(void)
183 {
184         if (rtnl_rtdsfield_tab) return;
185         rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
186         rtnl_rtdsfield_tab->tab[0] = "0";
187         rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab);
188 }
189
190 const char* FAST_FUNC rtnl_dsfield_n2a(int id)
191 {
192         if (id < 0 || id >= 256) {
193                 return itoa(id);
194         }
195
196         rtnl_rtdsfield_initialize();
197
198         if (rtnl_rtdsfield_tab->tab[id])
199                 return rtnl_rtdsfield_tab->tab[id];
200         return itoa(id);
201 }
202
203 int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg)
204 {
205         rtnl_rtdsfield_initialize();
206         return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
207 }
208
209
210 #if ENABLE_FEATURE_IP_RULE
211 static rtnl_tab_t *rtnl_rttable_tab;
212
213 static void rtnl_rttable_initialize(void)
214 {
215         if (rtnl_rtdsfield_tab) return;
216         rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
217         rtnl_rttable_tab->tab[0] = "unspec";
218         rtnl_rttable_tab->tab[255] = "local";
219         rtnl_rttable_tab->tab[254] = "main";
220         rtnl_rttable_tab->tab[253] = "default";
221         rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab);
222 }
223
224 const char* FAST_FUNC rtnl_rttable_n2a(int id)
225 {
226         if (id < 0 || id >= 256) {
227                 return itoa(id);
228         }
229
230         rtnl_rttable_initialize();
231
232         if (rtnl_rttable_tab->tab[id])
233                 return rtnl_rttable_tab->tab[id];
234         return itoa(id);
235 }
236
237 int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg)
238 {
239         rtnl_rttable_initialize();
240         return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);
241 }
242
243 #endif