blob: 2b646f0eab7aa7e39963487a23ffcb03aaa230bf [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/*
3 * utils.c
4 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 *
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 * Changes:
10 *
11 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
12 */
13
Rob Landleyecae66a2006-06-02 20:53:38 +000014#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include "utils.h"
"Vladimir N. Oleynik"007a0112005-09-22 11:11:11 +000016#include "inet_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017
Denis Vlasenko76140a72009-03-05 09:21:57 +000018unsigned get_unsigned(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000019{
20 unsigned long res;
21 char *ptr;
22
Denis Vlasenko76140a72009-03-05 09:21:57 +000023 if (*arg) {
24 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020025//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000026 if (!*ptr && res <= UINT_MAX) {
27 return res;
28 }
29 }
30 invarg(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000031}
32
Denis Vlasenko76140a72009-03-05 09:21:57 +000033uint32_t get_u32(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034{
35 unsigned long res;
36 char *ptr;
37
Denis Vlasenko76140a72009-03-05 09:21:57 +000038 if (*arg) {
39 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020040//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000041 if (!*ptr && res <= 0xFFFFFFFFUL) {
42 return res;
43 }
44 }
45 invarg(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046}
47
Denis Vlasenko76140a72009-03-05 09:21:57 +000048uint16_t get_u16(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049{
50 unsigned long res;
51 char *ptr;
52
Denis Vlasenko76140a72009-03-05 09:21:57 +000053 if (*arg) {
54 res = strtoul(arg, &ptr, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020055//FIXME: "" will be accepted too, is it correct?!
Denis Vlasenko76140a72009-03-05 09:21:57 +000056 if (!*ptr && res <= 0xFFFF) {
57 return res;
58 }
59 }
60 invarg(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061}
62
Denis Vlasenko787a4922009-03-03 14:55:29 +000063int get_addr_1(inet_prefix *addr, char *name, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000064{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065 memset(addr, 0, sizeof(*addr));
66
Denys Vlasenko0004e992010-09-01 12:01:17 +020067 if (strcmp(name, "default") == 0
Denis Vlasenko787a4922009-03-03 14:55:29 +000068 || strcmp(name, "all") == 0
69 || strcmp(name, "any") == 0
70 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071 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;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +000091 if (inet_pton(AF_INET, name, addr->data) <= 0)
92 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093 addr->bytelen = 4;
94 addr->bitlen = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095 return 0;
96}
97
Denis Vlasenko787a4922009-03-03 14:55:29 +000098static int get_prefix_1(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099{
100 int err;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000101 unsigned plen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102 char *slash;
103
104 memset(dst, 0, sizeof(*dst));
105
Denys Vlasenko0004e992010-09-01 12:01:17 +0200106 if (strcmp(arg, "default") == 0
Denis Vlasenko787a4922009-03-03 14:55:29 +0000107 || strcmp(arg, "all") == 0
108 || strcmp(arg, "any") == 0
109 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000110 dst->family = family;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000111 /*dst->bytelen = 0; - done by memset */
112 /*dst->bitlen = 0;*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113 return 0;
114 }
115
116 slash = strchr(arg, '/');
117 if (slash)
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000118 *slash = '\0';
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119 err = get_addr_1(dst, arg, family);
120 if (err == 0) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000121 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122 if (slash) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000123 inet_prefix netmask_pfx;
124
125 netmask_pfx.family = AF_UNSPEC;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000126 plen = bb_strtou(slash + 1, NULL, 0);
127 if ((errno || plen > dst->bitlen)
Denis Vlasenko787a4922009-03-03 14:55:29 +0000128 && (get_addr_1(&netmask_pfx, slash + 1, family)))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000129 err = -1;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000130 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;
Denys Vlasenkofd2dc532009-10-04 01:13:35 +0200139 if (plen <= dst->bitlen) {
Denis Vlasenko787a4922009-03-03 14:55:29 +0000140 dst->bitlen = plen;
141 /* dst->flags |= PREFIXLEN_SPECIFIED; */
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000142 } else
143 err = -1;
144 } else
145 err = -1;
146 } else {
147 /* plain prefix */
148 dst->bitlen = plen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150 }
151 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000152 if (slash)
153 *slash = '/';
154 return err;
155}
156
Denis Vlasenko787a4922009-03-03 14:55:29 +0000157int get_addr(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000158{
159 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000160 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 +0000161 }
162 if (get_addr_1(dst, arg, family)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000163 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000164 }
165 return 0;
166}
167
Denis Vlasenko787a4922009-03-03 14:55:29 +0000168int get_prefix(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169{
170 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000171 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 +0000172 }
173 if (get_prefix_1(dst, arg, family)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000174 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175 }
176 return 0;
177}
178
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000179uint32_t get_addr32(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180{
181 inet_prefix addr;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000182
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000183 if (get_addr_1(&addr, name, AF_INET)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000184 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000185 }
186 return addr.data[0];
187}
188
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000189void incomplete_command(void)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000191 bb_error_msg_and_die("command line is not complete, try option \"help\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000192}
193
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000194void invarg(const char *arg, const char *opt)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000195{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000196 bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000197}
198
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000199void duparg(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000201 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000202}
203
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000204void duparg2(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000205{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000206 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000207}
208
Denis Vlasenko787a4922009-03-03 14:55:29 +0000209int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000210{
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000211 uint32_t *a1 = a->data;
212 uint32_t *a2 = b->data;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000213 int words = bits >> 5;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000214
215 bits &= 0x1f;
216
217 if (words)
218 if (memcmp(a1, a2, words << 2))
219 return -1;
220
221 if (bits) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000222 uint32_t w1, w2;
223 uint32_t mask;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224
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
Denis Vlasenko76140a72009-03-05 09:21:57 +0000237const char *rt_addr_n2a(int af,
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000238 void *addr, char *buf, int buflen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000239{
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
Denis Vlasenko76140a72009-03-05 09:21:57 +0000249#ifdef RESOLVE_HOSTNAMES
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000250const char *format_host(int af, int len, void *addr, char *buf, int buflen)
251{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252 if (resolve_hosts) {
253 struct hostent *h_ent;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000254
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 if (len <= 0) {
256 switch (af) {
257 case AF_INET:
258 len = 4;
259 break;
260 case AF_INET6:
261 len = 16;
262 break;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000263 default:;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 }
265 }
Denis Vlasenko8c1aaf32007-06-04 21:03:51 +0000266 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 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 }
273 }
Denis Vlasenko76140a72009-03-05 09:21:57 +0000274 return rt_addr_n2a(af, addr, buf, buflen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000275}
Denis Vlasenko76140a72009-03-05 09:21:57 +0000276#endif