aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--networking/libiproute/ll_map.c31
-rw-r--r--networking/libiproute/rt_names.c268
2 files changed, 104 insertions, 195 deletions
diff --git a/networking/libiproute/ll_map.c b/networking/libiproute/ll_map.c
index 62528cc83..3c4ef2c32 100644
--- a/networking/libiproute/ll_map.c
+++ b/networking/libiproute/ll_map.c
@@ -27,15 +27,16 @@ struct idxmap {
27 char name[16]; 27 char name[16];
28}; 28};
29 29
30static struct idxmap *idxmap[16]; 30static struct idxmap **idxmap; /* treat as *idxmap[16] */
31 31
32static struct idxmap *find_by_index(int idx) 32static struct idxmap *find_by_index(int idx)
33{ 33{
34 struct idxmap *im; 34 struct idxmap *im;
35 35
36 for (im = idxmap[idx & 0xF]; im; im = im->next) 36 if (idxmap)
37 if (im->index == idx) 37 for (im = idxmap[idx & 0xF]; im; im = im->next)
38 return im; 38 if (im->index == idx)
39 return im;
39 return NULL; 40 return NULL;
40} 41}
41 42
@@ -59,8 +60,10 @@ int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
59 if (tb[IFLA_IFNAME] == NULL) 60 if (tb[IFLA_IFNAME] == NULL)
60 return 0; 61 return 0;
61 62
62 h = ifi->ifi_index & 0xF; 63 if (!idxmap)
64 idxmap = xzalloc(sizeof(idxmap[0]) * 16);
63 65
66 h = ifi->ifi_index & 0xF;
64 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next) 67 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
65 if (im->index == ifi->ifi_index) 68 if (im->index == ifi->ifi_index)
66 goto found; 69 goto found;
@@ -152,13 +155,15 @@ int FAST_FUNC xll_name_to_index(const char *name)
152 ret = icache; 155 ret = icache;
153 goto out; 156 goto out;
154 } 157 }
155 for (i = 0; i < 16; i++) { 158 if (idxmap) {
156 for (im = idxmap[i]; im; im = im->next) { 159 for (i = 0; i < 16; i++) {
157 if (strcmp(im->name, name) == 0) { 160 for (im = idxmap[i]; im; im = im->next) {
158 icache = im->index; 161 if (strcmp(im->name, name) == 0) {
159 strcpy(ncache, name); 162 icache = im->index;
160 ret = im->index; 163 strcpy(ncache, name);
161 goto out; 164 ret = im->index;
165 goto out;
166 }
162 } 167 }
163 } 168 }
164 } 169 }
@@ -195,6 +200,6 @@ int FAST_FUNC xll_name_to_index(const char *name)
195int FAST_FUNC ll_init_map(struct rtnl_handle *rth) 200int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
196{ 201{
197 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK); 202 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
198 xrtnl_dump_filter(rth, ll_remember_index, &idxmap); 203 xrtnl_dump_filter(rth, ll_remember_index, NULL);
199 return 0; 204 return 0;
200} 205}
diff --git a/networking/libiproute/rt_names.c b/networking/libiproute/rt_names.c
index e4d10613b..2699dba3d 100644
--- a/networking/libiproute/rt_names.c
+++ b/networking/libiproute/rt_names.c
@@ -9,20 +9,23 @@
9 * 9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 */ 11 */
12
13#include "libbb.h" 12#include "libbb.h"
14#include "rt_names.h" 13#include "rt_names.h"
15 14
16/* so far all callers have size == 256 */ 15typedef struct rtnl_tab_t {
17#define rtnl_tab_initialize(file, tab, size) rtnl_tab_initialize(file, tab) 16 const char *cached_str;
18#define size 256 17 unsigned cached_result;
19static void rtnl_tab_initialize(const char *file, const char **tab, int size) 18 const char *tab[256];
19} rtnl_tab_t;
20
21static void rtnl_tab_initialize(const char *file, const char **tab)
20{ 22{
21 char *token[2]; 23 char *token[2];
22 parser_t *parser = config_open2(file, fopen_for_read); 24 parser_t *parser = config_open2(file, fopen_for_read);
25
23 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { 26 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
24 int id = bb_strtou(token[0], NULL, 0); 27 unsigned id = bb_strtou(token[0], NULL, 0);
25 if (id < 0 || id > size) { 28 if (id > 256) {
26 bb_error_msg("database %s is corrupted at line %d", 29 bb_error_msg("database %s is corrupted at line %d",
27 file, parser->lineno); 30 file, parser->lineno);
28 break; 31 break;
@@ -31,9 +34,36 @@ static void rtnl_tab_initialize(const char *file, const char **tab, int size)
31 } 34 }
32 config_close(parser); 35 config_close(parser);
33} 36}
34#undef size
35 37
36static const char **rtnl_rtprot_tab; /* [256] */ 38static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
39{
40 unsigned i;
41
42 if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
43 *id = tab->cached_result;
44 return 0;
45 }
46
47 for (i = 0; i < 256; i++) {
48 if (tab->tab[i]
49 && strcmp(tab->tab[i], arg) == 0
50 ) {
51 tab->cached_str = tab->tab[i];
52 tab->cached_result = i;
53 *id = i;
54 return 0;
55 }
56 }
57
58 i = bb_strtou(arg, NULL, base);
59 if (i > 255)
60 return -1;
61 *id = i;
62 return 0;
63}
64
65
66static rtnl_tab_t *rtnl_rtprot_tab;
37 67
38static void rtnl_rtprot_initialize(void) 68static void rtnl_rtprot_initialize(void)
39{ 69{
@@ -52,13 +82,13 @@ static void rtnl_rtprot_initialize(void)
52 "zebra", 82 "zebra",
53 "bird", 83 "bird",
54 }; 84 };
55 if (rtnl_rtprot_tab) return;
56 rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0]));
57 memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab));
58 rtnl_tab_initialize("/etc/iproute2/rt_protos",
59 rtnl_rtprot_tab, 256);
60}
61 85
86 if (rtnl_rtprot_tab)
87 return;
88 rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
89 memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
90 rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab);
91}
62 92
63const char* rtnl_rtprot_n2a(int id, char *buf, int len) 93const char* rtnl_rtprot_n2a(int id, char *buf, int len)
64{ 94{
@@ -69,59 +99,34 @@ const char* rtnl_rtprot_n2a(int id, char *buf, int len)
69 99
70 rtnl_rtprot_initialize(); 100 rtnl_rtprot_initialize();
71 101
72 if (rtnl_rtprot_tab[id]) 102 if (rtnl_rtprot_tab->tab[id])
73 return rtnl_rtprot_tab[id]; 103 return rtnl_rtprot_tab->tab[id];
74 snprintf(buf, len, "%d", id); 104 snprintf(buf, len, "%d", id);
75 return buf; 105 return buf;
76} 106}
77 107
78int rtnl_rtprot_a2n(uint32_t *id, char *arg) 108int rtnl_rtprot_a2n(uint32_t *id, char *arg)
79{ 109{
80 static const char *cache = NULL;
81 static unsigned long res;
82 int i;
83
84 if (cache && strcmp(cache, arg) == 0) {
85 *id = res;
86 return 0;
87 }
88
89 rtnl_rtprot_initialize(); 110 rtnl_rtprot_initialize();
90 111 return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
91 for (i = 0; i < 256; i++) {
92 if (rtnl_rtprot_tab[i] &&
93 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
94 cache = rtnl_rtprot_tab[i];
95 res = i;
96 *id = res;
97 return 0;
98 }
99 }
100
101 res = bb_strtoul(arg, NULL, 0);
102 if (errno || res > 255)
103 return -1;
104 *id = res;
105 return 0;
106} 112}
107 113
108 114
109static const char **rtnl_rtscope_tab; /* [256] */ 115static rtnl_tab_t *rtnl_rtscope_tab;
110 116
111static void rtnl_rtscope_initialize(void) 117static void rtnl_rtscope_initialize(void)
112{ 118{
113 if (rtnl_rtscope_tab) return; 119 if (rtnl_rtscope_tab)
114 rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0])); 120 return;
115 rtnl_rtscope_tab[0] = "global"; 121 rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
116 rtnl_rtscope_tab[255] = "nowhere"; 122 rtnl_rtscope_tab->tab[0] = "global";
117 rtnl_rtscope_tab[254] = "host"; 123 rtnl_rtscope_tab->tab[255] = "nowhere";
118 rtnl_rtscope_tab[253] = "link"; 124 rtnl_rtscope_tab->tab[254] = "host";
119 rtnl_rtscope_tab[200] = "site"; 125 rtnl_rtscope_tab->tab[253] = "link";
120 rtnl_tab_initialize("/etc/iproute2/rt_scopes", 126 rtnl_rtscope_tab->tab[200] = "site";
121 rtnl_rtscope_tab, 256); 127 rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab);
122} 128}
123 129
124
125const char* rtnl_rtscope_n2a(int id, char *buf, int len) 130const char* rtnl_rtscope_n2a(int id, char *buf, int len)
126{ 131{
127 if (id < 0 || id >= 256) { 132 if (id < 0 || id >= 256) {
@@ -131,83 +136,33 @@ const char* rtnl_rtscope_n2a(int id, char *buf, int len)
131 136
132 rtnl_rtscope_initialize(); 137 rtnl_rtscope_initialize();
133 138
134 if (rtnl_rtscope_tab[id]) 139 if (rtnl_rtscope_tab->tab[id])
135 return rtnl_rtscope_tab[id]; 140 return rtnl_rtscope_tab->tab[id];
136 snprintf(buf, len, "%d", id); 141 snprintf(buf, len, "%d", id);
137 return buf; 142 return buf;
138} 143}
139 144
140int rtnl_rtscope_a2n(uint32_t *id, char *arg) 145int rtnl_rtscope_a2n(uint32_t *id, char *arg)
141{ 146{
142 static const char *cache = NULL;
143 static unsigned long res;
144 int i;
145
146 if (cache && strcmp(cache, arg) == 0) {
147 *id = res;
148 return 0;
149 }
150
151 rtnl_rtscope_initialize(); 147 rtnl_rtscope_initialize();
152 148 return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
153 for (i = 0; i < 256; i++) {
154 if (rtnl_rtscope_tab[i] &&
155 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
156 cache = rtnl_rtscope_tab[i];
157 res = i;
158 *id = res;
159 return 0;
160 }
161 }
162
163 res = bb_strtoul(arg, NULL, 0);
164 if (errno || res > 255)
165 return -1;
166 *id = res;
167 return 0;
168} 149}
169 150
170 151
171static const char **rtnl_rtrealm_tab; /* [256] */ 152static rtnl_tab_t *rtnl_rtrealm_tab;
172 153
173static void rtnl_rtrealm_initialize(void) 154static void rtnl_rtrealm_initialize(void)
174{ 155{
175 if (rtnl_rtrealm_tab) return; 156 if (rtnl_rtrealm_tab) return;
176 rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0])); 157 rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
177 rtnl_rtrealm_tab[0] = "unknown"; 158 rtnl_rtrealm_tab->tab[0] = "unknown";
178 rtnl_tab_initialize("/etc/iproute2/rt_realms", 159 rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab);
179 rtnl_rtrealm_tab, 256);
180} 160}
181 161
182
183int rtnl_rtrealm_a2n(uint32_t *id, char *arg) 162int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
184{ 163{
185 static const char *cache = NULL;
186 static unsigned long res;
187 int i;
188
189 if (cache && strcmp(cache, arg) == 0) {
190 *id = res;
191 return 0;
192 }
193
194 rtnl_rtrealm_initialize(); 164 rtnl_rtrealm_initialize();
195 165 return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
196 for (i = 0; i < 256; i++) {
197 if (rtnl_rtrealm_tab[i] &&
198 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
199 cache = rtnl_rtrealm_tab[i];
200 res = i;
201 *id = res;
202 return 0;
203 }
204 }
205
206 res = bb_strtoul(arg, NULL, 0);
207 if (errno || res > 255)
208 return -1;
209 *id = res;
210 return 0;
211} 166}
212 167
213#if ENABLE_FEATURE_IP_RULE 168#if ENABLE_FEATURE_IP_RULE
@@ -220,26 +175,24 @@ const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
220 175
221 rtnl_rtrealm_initialize(); 176 rtnl_rtrealm_initialize();
222 177
223 if (rtnl_rtrealm_tab[id]) 178 if (rtnl_rtrealm_tab->tab[id])
224 return rtnl_rtrealm_tab[id]; 179 return rtnl_rtrealm_tab->tab[id];
225 snprintf(buf, len, "%d", id); 180 snprintf(buf, len, "%d", id);
226 return buf; 181 return buf;
227} 182}
228#endif 183#endif
229 184
230 185
231static const char **rtnl_rtdsfield_tab; /* [256] */ 186static rtnl_tab_t *rtnl_rtdsfield_tab;
232 187
233static void rtnl_rtdsfield_initialize(void) 188static void rtnl_rtdsfield_initialize(void)
234{ 189{
235 if (rtnl_rtdsfield_tab) return; 190 if (rtnl_rtdsfield_tab) return;
236 rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0])); 191 rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
237 rtnl_rtdsfield_tab[0] = "0"; 192 rtnl_rtdsfield_tab->tab[0] = "0";
238 rtnl_tab_initialize("/etc/iproute2/rt_dsfield", 193 rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab);
239 rtnl_rtdsfield_tab, 256);
240} 194}
241 195
242
243const char * rtnl_dsfield_n2a(int id, char *buf, int len) 196const char * rtnl_dsfield_n2a(int id, char *buf, int len)
244{ 197{
245 if (id < 0 || id >= 256) { 198 if (id < 0 || id >= 256) {
@@ -249,59 +202,33 @@ const char * rtnl_dsfield_n2a(int id, char *buf, int len)
249 202
250 rtnl_rtdsfield_initialize(); 203 rtnl_rtdsfield_initialize();
251 204
252 if (rtnl_rtdsfield_tab[id]) 205 if (rtnl_rtdsfield_tab->tab[id])
253 return rtnl_rtdsfield_tab[id]; 206 return rtnl_rtdsfield_tab->tab[id];
254 snprintf(buf, len, "0x%02x", id); 207 snprintf(buf, len, "0x%02x", id);
255 return buf; 208 return buf;
256} 209}
257 210
258
259int rtnl_dsfield_a2n(uint32_t *id, char *arg) 211int rtnl_dsfield_a2n(uint32_t *id, char *arg)
260{ 212{
261 static const char *cache = NULL;
262 static unsigned long res;
263 int i;
264
265 if (cache && strcmp(cache, arg) == 0) {
266 *id = res;
267 return 0;
268 }
269
270 rtnl_rtdsfield_initialize(); 213 rtnl_rtdsfield_initialize();
271 214 return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
272 for (i = 0; i < 256; i++) {
273 if (rtnl_rtdsfield_tab[i] &&
274 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
275 cache = rtnl_rtdsfield_tab[i];
276 res = i;
277 *id = res;
278 return 0;
279 }
280 }
281
282 res = bb_strtoul(arg, NULL, 16);
283 if (errno || res > 255)
284 return -1;
285 *id = res;
286 return 0;
287} 215}
288 216
289 217
290#if ENABLE_FEATURE_IP_RULE 218#if ENABLE_FEATURE_IP_RULE
291static const char **rtnl_rttable_tab; /* [256] */ 219static rtnl_tab_t *rtnl_rttable_tab;
292 220
293static void rtnl_rttable_initialize(void) 221static void rtnl_rttable_initialize(void)
294{ 222{
295 if (rtnl_rtdsfield_tab) return; 223 if (rtnl_rtdsfield_tab) return;
296 rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0])); 224 rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
297 rtnl_rttable_tab[0] = "unspec"; 225 rtnl_rttable_tab->tab[0] = "unspec";
298 rtnl_rttable_tab[255] = "local"; 226 rtnl_rttable_tab->tab[255] = "local";
299 rtnl_rttable_tab[254] = "main"; 227 rtnl_rttable_tab->tab[254] = "main";
300 rtnl_rttable_tab[253] = "default"; 228 rtnl_rttable_tab->tab[253] = "default";
301 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256); 229 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab);
302} 230}
303 231
304
305const char *rtnl_rttable_n2a(int id, char *buf, int len) 232const char *rtnl_rttable_n2a(int id, char *buf, int len)
306{ 233{
307 if (id < 0 || id >= 256) { 234 if (id < 0 || id >= 256) {
@@ -311,39 +238,16 @@ const char *rtnl_rttable_n2a(int id, char *buf, int len)
311 238
312 rtnl_rttable_initialize(); 239 rtnl_rttable_initialize();
313 240
314 if (rtnl_rttable_tab[id]) 241 if (rtnl_rttable_tab->tab[id])
315 return rtnl_rttable_tab[id]; 242 return rtnl_rttable_tab->tab[id];
316 snprintf(buf, len, "%d", id); 243 snprintf(buf, len, "%d", id);
317 return buf; 244 return buf;
318} 245}
319 246
320int rtnl_rttable_a2n(uint32_t * id, char *arg) 247int rtnl_rttable_a2n(uint32_t *id, char *arg)
321{ 248{
322 static char *cache = NULL;
323 static unsigned long res;
324 int i;
325
326 if (cache && strcmp(cache, arg) == 0) {
327 *id = res;
328 return 0;
329 }
330
331 rtnl_rttable_initialize(); 249 rtnl_rttable_initialize();
332 250 return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);
333 for (i = 0; i < 256; i++) {
334 if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
335 cache = (char*)rtnl_rttable_tab[i];
336 res = i;
337 *id = res;
338 return 0;
339 }
340 }
341
342 i = bb_strtoul(arg, NULL, 0);
343 if (errno || i > 255)
344 return -1;
345 *id = i;
346 return 0;
347} 251}
348 252
349#endif 253#endif