blob: d0fe30605c815a3e9afc0c6ea02d59d7332b0dce [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 * Changes:
8 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02009 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000010 */
11
Rob Landleyecae66a2006-06-02 20:53:38 +000012#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013#include "utils.h"
"Vladimir N. Oleynik"007a0112005-09-22 11:11:11 +000014#include "inet_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015
Denis Vlasenko76140a72009-03-05 09:21:57 +000016unsigned get_unsigned(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017{
18 unsigned long res;
19 char *ptr;
20
Denis Vlasenko76140a72009-03-05 09:21:57 +000021 if (*arg) {
22 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020023//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000024 if (!*ptr && res <= UINT_MAX) {
25 return res;
26 }
27 }
28 invarg(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000029}
30
Denis Vlasenko76140a72009-03-05 09:21:57 +000031uint32_t get_u32(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032{
33 unsigned long res;
34 char *ptr;
35
Denis Vlasenko76140a72009-03-05 09:21:57 +000036 if (*arg) {
37 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020038//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000039 if (!*ptr && res <= 0xFFFFFFFFUL) {
40 return res;
41 }
42 }
43 invarg(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000044}
45
Denis Vlasenko76140a72009-03-05 09:21:57 +000046uint16_t get_u16(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047{
48 unsigned long res;
49 char *ptr;
50
Denis Vlasenko76140a72009-03-05 09:21:57 +000051 if (*arg) {
52 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020053//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000054 if (!*ptr && res <= 0xFFFF) {
55 return res;
56 }
57 }
58 invarg(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000059}
60
Denis Vlasenko787a4922009-03-03 14:55:29 +000061int get_addr_1(inet_prefix *addr, char *name, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 memset(addr, 0, sizeof(*addr));
64
Denys Vlasenko0004e992010-09-01 12:01:17 +020065 if (strcmp(name, "default") == 0
Denis Vlasenko787a4922009-03-03 14:55:29 +000066 || strcmp(name, "all") == 0
67 || strcmp(name, "any") == 0
68 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000069 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
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000086 if (family != AF_UNSPEC && family != AF_INET)
87 return -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +010088
89 /* Try to parse it as IPv4 */
90 addr->family = AF_INET;
91#if 0 /* Doesn't handle e.g. "10.10", for example, "ip r l root 10.10/16" */
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +000092 if (inet_pton(AF_INET, name, addr->data) <= 0)
93 return -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +010094#else
95 {
96 unsigned i = 0;
97 unsigned n = 0;
98 const char *cp = name - 1;
99 while (*++cp) {
100 if ((unsigned char)(*cp - '0') <= 9) {
101 n = 10 * n + (unsigned char)(*cp - '0');
102 if (n >= 256)
103 return -1;
104 ((uint8_t*)addr->data)[i] = n;
105 continue;
106 }
107 if (*cp == '.' && ++i <= 3) {
108 n = 0;
109 continue;
110 }
111 return -1;
112 }
113 }
114#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115 addr->bytelen = 4;
116 addr->bitlen = -1;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100117
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 return 0;
119}
120
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100121static void get_prefix_1(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000123 char *slash;
124
125 memset(dst, 0, sizeof(*dst));
126
Denys Vlasenko0004e992010-09-01 12:01:17 +0200127 if (strcmp(arg, "default") == 0
Denis Vlasenko787a4922009-03-03 14:55:29 +0000128 || strcmp(arg, "all") == 0
129 || strcmp(arg, "any") == 0
130 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000131 dst->family = family;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000132 /*dst->bytelen = 0; - done by memset */
133 /*dst->bitlen = 0;*/
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100134 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000135 }
136
137 slash = strchr(arg, '/');
138 if (slash)
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000139 *slash = '\0';
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100140
141 if (get_addr_1(dst, arg, family) == 0) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000142 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000143 if (slash) {
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100144 unsigned plen;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000145 inet_prefix netmask_pfx;
146
147 netmask_pfx.family = AF_UNSPEC;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000148 plen = bb_strtou(slash + 1, NULL, 0);
149 if ((errno || plen > dst->bitlen)
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100150 && get_addr_1(&netmask_pfx, slash + 1, family) != 0
151 ) {
152 goto bad;
153 }
154 if (netmask_pfx.family == AF_INET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000155 /* fill in prefix length of dotted quad */
156 uint32_t mask = ntohl(netmask_pfx.data[0]);
157 uint32_t host = ~mask;
158
159 /* a valid netmask must be 2^n - 1 */
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100160 if (host & (host + 1))
161 goto bad;
162
163 for (plen = 0; mask; mask <<= 1)
164 ++plen;
165 if (plen > dst->bitlen)
166 goto bad;
167 /* dst->flags |= PREFIXLEN_SPECIFIED; */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100169 dst->bitlen = plen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 }
171 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100172
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000173 if (slash)
174 *slash = '/';
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100175 return;
176 bad:
177 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178}
179
Denis Vlasenko787a4922009-03-03 14:55:29 +0000180int get_addr(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000181{
182 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000183 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 }
185 if (get_addr_1(dst, arg, family)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000186 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187 }
188 return 0;
189}
190
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100191void get_prefix(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000192{
193 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000194 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000195 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100196 get_prefix_1(dst, arg, family);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000197}
198
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000199uint32_t get_addr32(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200{
201 inet_prefix addr;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000202
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203 if (get_addr_1(&addr, name, AF_INET)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000204 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000205 }
206 return addr.data[0];
207}
208
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000209void incomplete_command(void)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000210{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000211 bb_error_msg_and_die("command line is not complete, try option \"help\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000212}
213
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000214void invarg(const char *arg, const char *opt)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000216 bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000217}
218
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000219void duparg(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000220{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000221 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222}
223
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000224void duparg2(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000225{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000226 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227}
228
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100229int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000230{
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100231 const uint32_t *a1 = a->data;
232 const uint32_t *a2 = b->data;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000233 int words = bits >> 5;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234
235 bits &= 0x1f;
236
237 if (words)
238 if (memcmp(a1, a2, words << 2))
239 return -1;
240
241 if (bits) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000242 uint32_t w1, w2;
243 uint32_t mask;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000244
245 w1 = a1[words];
246 w2 = a2[words];
247
248 mask = htonl((0xffffffff) << (0x20 - bits));
249
250 if ((w1 ^ w2) & mask)
251 return 1;
252 }
253
254 return 0;
255}
256
Denis Vlasenko76140a72009-03-05 09:21:57 +0000257const char *rt_addr_n2a(int af,
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000258 void *addr, char *buf, int buflen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259{
260 switch (af) {
261 case AF_INET:
262 case AF_INET6:
263 return inet_ntop(af, addr, buf, buflen);
264 default:
265 return "???";
266 }
267}
268
Denis Vlasenko76140a72009-03-05 09:21:57 +0000269#ifdef RESOLVE_HOSTNAMES
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270const char *format_host(int af, int len, void *addr, char *buf, int buflen)
271{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 if (resolve_hosts) {
273 struct hostent *h_ent;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000274
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000275 if (len <= 0) {
276 switch (af) {
277 case AF_INET:
278 len = 4;
279 break;
280 case AF_INET6:
281 len = 16;
282 break;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000283 default:;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000284 }
285 }
Denis Vlasenko8c1aaf32007-06-04 21:03:51 +0000286 if (len > 0) {
287 h_ent = gethostbyaddr(addr, len, af);
288 if (h_ent != NULL) {
289 safe_strncpy(buf, h_ent->h_name, buflen);
290 return buf;
291 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292 }
293 }
Denis Vlasenko76140a72009-03-05 09:21:57 +0000294 return rt_addr_n2a(af, addr, buf, buflen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000295}
Denis Vlasenko76140a72009-03-05 09:21:57 +0000296#endif