source: MondoRescue/branches/2.2.9/mindi-busybox/networking/libiproute/utils.c@ 2887

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