Contents of /trunk/mkinitrd-magellan/busybox/networking/libiproute/utils.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 6263 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 6263 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * utils.c |
4 | * |
5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
6 | * |
7 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
8 | * |
9 | * Changes: |
10 | * |
11 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
12 | */ |
13 | |
14 | #include "libbb.h" |
15 | |
16 | #include <string.h> |
17 | #include <unistd.h> |
18 | |
19 | #include "utils.h" |
20 | #include "inet_common.h" |
21 | |
22 | int get_integer(int *val, char *arg, int base) |
23 | { |
24 | long res; |
25 | char *ptr; |
26 | |
27 | if (!arg || !*arg) |
28 | return -1; |
29 | res = strtol(arg, &ptr, base); |
30 | if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN) |
31 | return -1; |
32 | *val = res; |
33 | return 0; |
34 | } |
35 | |
36 | int get_unsigned(unsigned *val, char *arg, int base) |
37 | { |
38 | unsigned long res; |
39 | char *ptr; |
40 | |
41 | if (!arg || !*arg) |
42 | return -1; |
43 | res = strtoul(arg, &ptr, base); |
44 | if (!ptr || ptr == arg || *ptr || res > UINT_MAX) |
45 | return -1; |
46 | *val = res; |
47 | return 0; |
48 | } |
49 | |
50 | int get_u32(uint32_t * val, char *arg, int base) |
51 | { |
52 | unsigned long res; |
53 | char *ptr; |
54 | |
55 | if (!arg || !*arg) |
56 | return -1; |
57 | res = strtoul(arg, &ptr, base); |
58 | if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL) |
59 | return -1; |
60 | *val = res; |
61 | return 0; |
62 | } |
63 | |
64 | int get_u16(uint16_t * val, char *arg, int base) |
65 | { |
66 | unsigned long res; |
67 | char *ptr; |
68 | |
69 | if (!arg || !*arg) |
70 | return -1; |
71 | res = strtoul(arg, &ptr, base); |
72 | if (!ptr || ptr == arg || *ptr || res > 0xFFFF) |
73 | return -1; |
74 | *val = res; |
75 | return 0; |
76 | } |
77 | |
78 | int get_u8(uint8_t * val, char *arg, int base) |
79 | { |
80 | unsigned long res; |
81 | char *ptr; |
82 | |
83 | if (!arg || !*arg) |
84 | return -1; |
85 | res = strtoul(arg, &ptr, base); |
86 | if (!ptr || ptr == arg || *ptr || res > 0xFF) |
87 | return -1; |
88 | *val = res; |
89 | return 0; |
90 | } |
91 | |
92 | int get_s16(int16_t * val, char *arg, int base) |
93 | { |
94 | long res; |
95 | char *ptr; |
96 | |
97 | if (!arg || !*arg) |
98 | return -1; |
99 | res = strtol(arg, &ptr, base); |
100 | if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000) |
101 | return -1; |
102 | *val = res; |
103 | return 0; |
104 | } |
105 | |
106 | int get_s8(int8_t * val, char *arg, int base) |
107 | { |
108 | long res; |
109 | char *ptr; |
110 | |
111 | if (!arg || !*arg) |
112 | return -1; |
113 | res = strtol(arg, &ptr, base); |
114 | if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80) |
115 | return -1; |
116 | *val = res; |
117 | return 0; |
118 | } |
119 | |
120 | int get_addr_1(inet_prefix * addr, char *name, int family) |
121 | { |
122 | char *cp; |
123 | unsigned char *ap = (unsigned char *) addr->data; |
124 | int i; |
125 | |
126 | memset(addr, 0, sizeof(*addr)); |
127 | |
128 | if (strcmp(name, bb_str_default) == 0 || |
129 | strcmp(name, "all") == 0 || strcmp(name, "any") == 0) { |
130 | addr->family = family; |
131 | addr->bytelen = (family == AF_INET6 ? 16 : 4); |
132 | addr->bitlen = -1; |
133 | return 0; |
134 | } |
135 | |
136 | if (strchr(name, ':')) { |
137 | addr->family = AF_INET6; |
138 | if (family != AF_UNSPEC && family != AF_INET6) |
139 | return -1; |
140 | if (inet_pton(AF_INET6, name, addr->data) <= 0) |
141 | return -1; |
142 | addr->bytelen = 16; |
143 | addr->bitlen = -1; |
144 | return 0; |
145 | } |
146 | |
147 | addr->family = AF_INET; |
148 | if (family != AF_UNSPEC && family != AF_INET) |
149 | return -1; |
150 | addr->bytelen = 4; |
151 | addr->bitlen = -1; |
152 | for (cp = name, i = 0; *cp; cp++) { |
153 | if (*cp <= '9' && *cp >= '0') { |
154 | ap[i] = 10 * ap[i] + (*cp - '0'); |
155 | continue; |
156 | } |
157 | if (*cp == '.' && ++i <= 3) |
158 | continue; |
159 | return -1; |
160 | } |
161 | return 0; |
162 | } |
163 | |
164 | int get_prefix_1(inet_prefix * dst, char *arg, int family) |
165 | { |
166 | int err; |
167 | int plen; |
168 | char *slash; |
169 | |
170 | memset(dst, 0, sizeof(*dst)); |
171 | |
172 | if (strcmp(arg, bb_str_default) == 0 || strcmp(arg, "any") == 0) { |
173 | dst->family = family; |
174 | dst->bytelen = 0; |
175 | dst->bitlen = 0; |
176 | return 0; |
177 | } |
178 | |
179 | slash = strchr(arg, '/'); |
180 | if (slash) |
181 | *slash = 0; |
182 | err = get_addr_1(dst, arg, family); |
183 | if (err == 0) { |
184 | switch (dst->family) { |
185 | case AF_INET6: |
186 | dst->bitlen = 128; |
187 | break; |
188 | default: |
189 | case AF_INET: |
190 | dst->bitlen = 32; |
191 | } |
192 | if (slash) { |
193 | if (get_integer(&plen, slash + 1, 0) || plen > dst->bitlen) { |
194 | err = -1; |
195 | goto done; |
196 | } |
197 | dst->bitlen = plen; |
198 | } |
199 | } |
200 | done: |
201 | if (slash) |
202 | *slash = '/'; |
203 | return err; |
204 | } |
205 | |
206 | int get_addr(inet_prefix * dst, char *arg, int family) |
207 | { |
208 | if (family == AF_PACKET) { |
209 | bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context", arg); |
210 | } |
211 | if (get_addr_1(dst, arg, family)) { |
212 | bb_error_msg_and_die("an inet address is expected rather than \"%s\"", arg); |
213 | } |
214 | return 0; |
215 | } |
216 | |
217 | int get_prefix(inet_prefix * dst, char *arg, int family) |
218 | { |
219 | if (family == AF_PACKET) { |
220 | bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context", arg); |
221 | } |
222 | if (get_prefix_1(dst, arg, family)) { |
223 | bb_error_msg_and_die("an inet address is expected rather than \"%s\"", arg); |
224 | } |
225 | return 0; |
226 | } |
227 | |
228 | uint32_t get_addr32(char *name) |
229 | { |
230 | inet_prefix addr; |
231 | |
232 | if (get_addr_1(&addr, name, AF_INET)) { |
233 | bb_error_msg_and_die("an IP address is expected rather than \"%s\"", name); |
234 | } |
235 | return addr.data[0]; |
236 | } |
237 | |
238 | void incomplete_command(void) |
239 | { |
240 | bb_error_msg("command line is not complete, try option \"help\""); |
241 | exit(-1); |
242 | } |
243 | |
244 | void invarg(const char * const arg, const char * const opt) |
245 | { |
246 | bb_error_msg(bb_msg_invalid_arg, arg, opt); |
247 | exit(-1); |
248 | } |
249 | |
250 | void duparg(char *key, char *arg) |
251 | { |
252 | bb_error_msg("duplicate \"%s\": \"%s\" is the second value", key, arg); |
253 | exit(-1); |
254 | } |
255 | |
256 | void duparg2(char *key, char *arg) |
257 | { |
258 | bb_error_msg("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg); |
259 | exit(-1); |
260 | } |
261 | |
262 | int matches(char *cmd, char *pattern) |
263 | { |
264 | int len = strlen(cmd); |
265 | |
266 | return strncmp(pattern, cmd, len); |
267 | } |
268 | |
269 | int inet_addr_match(inet_prefix * a, inet_prefix * b, int bits) |
270 | { |
271 | uint32_t *a1 = a->data; |
272 | uint32_t *a2 = b->data; |
273 | int words = bits >> 0x05; |
274 | |
275 | bits &= 0x1f; |
276 | |
277 | if (words) |
278 | if (memcmp(a1, a2, words << 2)) |
279 | return -1; |
280 | |
281 | if (bits) { |
282 | uint32_t w1, w2; |
283 | uint32_t mask; |
284 | |
285 | w1 = a1[words]; |
286 | w2 = a2[words]; |
287 | |
288 | mask = htonl((0xffffffff) << (0x20 - bits)); |
289 | |
290 | if ((w1 ^ w2) & mask) |
291 | return 1; |
292 | } |
293 | |
294 | return 0; |
295 | } |
296 | |
297 | const char *rt_addr_n2a(int af, int ATTRIBUTE_UNUSED len, |
298 | void *addr, char *buf, int buflen) |
299 | { |
300 | switch (af) { |
301 | case AF_INET: |
302 | case AF_INET6: |
303 | return inet_ntop(af, addr, buf, buflen); |
304 | default: |
305 | return "???"; |
306 | } |
307 | } |
308 | |
309 | |
310 | const char *format_host(int af, int len, void *addr, char *buf, int buflen) |
311 | { |
312 | #ifdef RESOLVE_HOSTNAMES |
313 | if (resolve_hosts) { |
314 | struct hostent *h_ent; |
315 | |
316 | if (len <= 0) { |
317 | switch (af) { |
318 | case AF_INET: |
319 | len = 4; |
320 | break; |
321 | case AF_INET6: |
322 | len = 16; |
323 | break; |
324 | default:; |
325 | } |
326 | } |
327 | if (len > 0 && (h_ent = gethostbyaddr(addr, len, af)) != NULL) { |
328 | snprintf(buf, buflen - 1, "%s", h_ent->h_name); |
329 | return buf; |
330 | } |
331 | } |
332 | #endif |
333 | return rt_addr_n2a(af, len, addr, buf, buflen); |
334 | } |