Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/networking/libiproute/utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (show annotations) (download)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 5699 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
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 #include "utils.h"
16 #include "inet_common.h"
17
18 unsigned get_unsigned(char *arg, const char *errmsg)
19 {
20 unsigned long res;
21 char *ptr;
22
23 if (*arg) {
24 res = strtoul(arg, &ptr, 0);
25 //FIXME: "" will be accepted too, is it correct?!
26 if (!*ptr && res <= UINT_MAX) {
27 return res;
28 }
29 }
30 invarg(arg, errmsg); /* does not return */
31 }
32
33 uint32_t get_u32(char *arg, const char *errmsg)
34 {
35 unsigned long res;
36 char *ptr;
37
38 if (*arg) {
39 res = strtoul(arg, &ptr, 0);
40 //FIXME: "" will be accepted too, is it correct?!
41 if (!*ptr && res <= 0xFFFFFFFFUL) {
42 return res;
43 }
44 }
45 invarg(arg, errmsg); /* does not return */
46 }
47
48 uint16_t get_u16(char *arg, const char *errmsg)
49 {
50 unsigned long res;
51 char *ptr;
52
53 if (*arg) {
54 res = strtoul(arg, &ptr, 0);
55 //FIXME: "" will be accepted too, is it correct?!
56 if (!*ptr && res <= 0xFFFF) {
57 return res;
58 }
59 }
60 invarg(arg, errmsg); /* does not return */
61 }
62
63 int get_addr_1(inet_prefix *addr, char *name, int family)
64 {
65 memset(addr, 0, sizeof(*addr));
66
67 if (strcmp(name, bb_str_default) == 0
68 || strcmp(name, "all") == 0
69 || strcmp(name, "any") == 0
70 ) {
71 addr->family = family;
72 addr->bytelen = (family == AF_INET6 ? 16 : 4);
73 addr->bitlen = -1;
74 return 0;
75 }
76
77 if (strchr(name, ':')) {
78 addr->family = AF_INET6;
79 if (family != AF_UNSPEC && family != AF_INET6)
80 return -1;
81 if (inet_pton(AF_INET6, name, addr->data) <= 0)
82 return -1;
83 addr->bytelen = 16;
84 addr->bitlen = -1;
85 return 0;
86 }
87
88 addr->family = AF_INET;
89 if (family != AF_UNSPEC && family != AF_INET)
90 return -1;
91 if (inet_pton(AF_INET, name, addr->data) <= 0)
92 return -1;
93 addr->bytelen = 4;
94 addr->bitlen = -1;
95 return 0;
96 }
97
98 static int get_prefix_1(inet_prefix *dst, char *arg, int family)
99 {
100 int err;
101 unsigned plen;
102 char *slash;
103
104 memset(dst, 0, sizeof(*dst));
105
106 if (strcmp(arg, bb_str_default) == 0
107 || strcmp(arg, "all") == 0
108 || strcmp(arg, "any") == 0
109 ) {
110 dst->family = family;
111 /*dst->bytelen = 0; - done by memset */
112 /*dst->bitlen = 0;*/
113 return 0;
114 }
115
116 slash = strchr(arg, '/');
117 if (slash)
118 *slash = '\0';
119 err = get_addr_1(dst, arg, family);
120 if (err == 0) {
121 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
122 if (slash) {
123 inet_prefix netmask_pfx;
124
125 netmask_pfx.family = AF_UNSPEC;
126 plen = bb_strtou(slash + 1, NULL, 0);
127 if ((errno || plen > dst->bitlen)
128 && (get_addr_1(&netmask_pfx, slash + 1, family)))
129 err = -1;
130 else if (netmask_pfx.family == AF_INET) {
131 /* fill in prefix length of dotted quad */
132 uint32_t mask = ntohl(netmask_pfx.data[0]);
133 uint32_t host = ~mask;
134
135 /* a valid netmask must be 2^n - 1 */
136 if (!(host & (host + 1))) {
137 for (plen = 0; mask; mask <<= 1)
138 ++plen;
139 if (plen <= dst->bitlen) {
140 dst->bitlen = plen;
141 /* dst->flags |= PREFIXLEN_SPECIFIED; */
142 } else
143 err = -1;
144 } else
145 err = -1;
146 } else {
147 /* plain prefix */
148 dst->bitlen = plen;
149 }
150 }
151 }
152 if (slash)
153 *slash = '/';
154 return err;
155 }
156
157 int get_addr(inet_prefix *dst, char *arg, int family)
158 {
159 if (family == AF_PACKET) {
160 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
161 }
162 if (get_addr_1(dst, arg, family)) {
163 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
164 }
165 return 0;
166 }
167
168 int get_prefix(inet_prefix *dst, char *arg, int family)
169 {
170 if (family == AF_PACKET) {
171 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
172 }
173 if (get_prefix_1(dst, arg, family)) {
174 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
175 }
176 return 0;
177 }
178
179 uint32_t get_addr32(char *name)
180 {
181 inet_prefix addr;
182
183 if (get_addr_1(&addr, name, AF_INET)) {
184 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
185 }
186 return addr.data[0];
187 }
188
189 void incomplete_command(void)
190 {
191 bb_error_msg_and_die("command line is not complete, try option \"help\"");
192 }
193
194 void invarg(const char *arg, const char *opt)
195 {
196 bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt);
197 }
198
199 void duparg(const char *key, const char *arg)
200 {
201 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
202 }
203
204 void duparg2(const char *key, const char *arg)
205 {
206 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
207 }
208
209 int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
210 {
211 uint32_t *a1 = a->data;
212 uint32_t *a2 = b->data;
213 int words = bits >> 5;
214
215 bits &= 0x1f;
216
217 if (words)
218 if (memcmp(a1, a2, words << 2))
219 return -1;
220
221 if (bits) {
222 uint32_t w1, w2;
223 uint32_t mask;
224
225 w1 = a1[words];
226 w2 = a2[words];
227
228 mask = htonl((0xffffffff) << (0x20 - bits));
229
230 if ((w1 ^ w2) & mask)
231 return 1;
232 }
233
234 return 0;
235 }
236
237 const char *rt_addr_n2a(int af,
238 void *addr, char *buf, int buflen)
239 {
240 switch (af) {
241 case AF_INET:
242 case AF_INET6:
243 return inet_ntop(af, addr, buf, buflen);
244 default:
245 return "???";
246 }
247 }
248
249 #ifdef RESOLVE_HOSTNAMES
250 const char *format_host(int af, int len, void *addr, char *buf, int buflen)
251 {
252 if (resolve_hosts) {
253 struct hostent *h_ent;
254
255 if (len <= 0) {
256 switch (af) {
257 case AF_INET:
258 len = 4;
259 break;
260 case AF_INET6:
261 len = 16;
262 break;
263 default:;
264 }
265 }
266 if (len > 0) {
267 h_ent = gethostbyaddr(addr, len, af);
268 if (h_ent != NULL) {
269 safe_strncpy(buf, h_ent->h_name, buflen);
270 return buf;
271 }
272 }
273 }
274 return rt_addr_n2a(af, addr, buf, buflen);
275 }
276 #endif