blob: c84d018eb73ae90524c7ebe5814d73d242ccd62a [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 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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);
25 if (!*ptr && res <= UINT_MAX) {
26 return res;
27 }
28 }
29 invarg(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000030}
31
Denis Vlasenko76140a72009-03-05 09:21:57 +000032uint32_t get_u32(char *arg, const char *errmsg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000033{
34 unsigned long res;
35 char *ptr;
36
Denis Vlasenko76140a72009-03-05 09:21:57 +000037 if (*arg) {
38 res = strtoul(arg, &ptr, 0);
39 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);
53 if (!*ptr && res <= 0xFFFF) {
54 return res;
55 }
56 }
57 invarg(arg, errmsg); /* does not return */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058}
59
Denis Vlasenko787a4922009-03-03 14:55:29 +000060int get_addr_1(inet_prefix *addr, char *name, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062 memset(addr, 0, sizeof(*addr));
63
Denis Vlasenko787a4922009-03-03 14:55:29 +000064 if (strcmp(name, bb_str_default) == 0
65 || strcmp(name, "all") == 0
66 || strcmp(name, "any") == 0
67 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068 addr->family = family;
69 addr->bytelen = (family == AF_INET6 ? 16 : 4);
70 addr->bitlen = -1;
71 return 0;
72 }
73
74 if (strchr(name, ':')) {
75 addr->family = AF_INET6;
76 if (family != AF_UNSPEC && family != AF_INET6)
77 return -1;
78 if (inet_pton(AF_INET6, name, addr->data) <= 0)
79 return -1;
80 addr->bytelen = 16;
81 addr->bitlen = -1;
82 return 0;
83 }
84
85 addr->family = AF_INET;
86 if (family != AF_UNSPEC && family != AF_INET)
87 return -1;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +000088 if (inet_pton(AF_INET, name, addr->data) <= 0)
89 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 addr->bytelen = 4;
91 addr->bitlen = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000092 return 0;
93}
94
Denis Vlasenko787a4922009-03-03 14:55:29 +000095static int get_prefix_1(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000096{
97 int err;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +000098 unsigned plen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099 char *slash;
100
101 memset(dst, 0, sizeof(*dst));
102
Denis Vlasenko787a4922009-03-03 14:55:29 +0000103 if (strcmp(arg, bb_str_default) == 0
104 || strcmp(arg, "all") == 0
105 || strcmp(arg, "any") == 0
106 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107 dst->family = family;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000108 /*dst->bytelen = 0; - done by memset */
109 /*dst->bitlen = 0;*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000110 return 0;
111 }
112
113 slash = strchr(arg, '/');
114 if (slash)
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000115 *slash = '\0';
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000116 err = get_addr_1(dst, arg, family);
117 if (err == 0) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000118 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119 if (slash) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000120 inet_prefix netmask_pfx;
121
122 netmask_pfx.family = AF_UNSPEC;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000123 plen = bb_strtou(slash + 1, NULL, 0);
124 if ((errno || plen > dst->bitlen)
Denis Vlasenko787a4922009-03-03 14:55:29 +0000125 && (get_addr_1(&netmask_pfx, slash + 1, family)))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126 err = -1;
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000127 else if (netmask_pfx.family == AF_INET) {
128 /* fill in prefix length of dotted quad */
129 uint32_t mask = ntohl(netmask_pfx.data[0]);
130 uint32_t host = ~mask;
131
132 /* a valid netmask must be 2^n - 1 */
133 if (!(host & (host + 1))) {
134 for (plen = 0; mask; mask <<= 1)
135 ++plen;
136 if (plen >= 0 && plen <= dst->bitlen) {
Denis Vlasenko787a4922009-03-03 14:55:29 +0000137 dst->bitlen = plen;
138 /* dst->flags |= PREFIXLEN_SPECIFIED; */
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000139 } else
140 err = -1;
141 } else
142 err = -1;
143 } else {
144 /* plain prefix */
145 dst->bitlen = plen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000146 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000147 }
148 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149 if (slash)
150 *slash = '/';
151 return err;
152}
153
Denis Vlasenko787a4922009-03-03 14:55:29 +0000154int get_addr(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155{
156 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000157 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 +0000158 }
159 if (get_addr_1(dst, arg, family)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000160 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 }
162 return 0;
163}
164
Denis Vlasenko787a4922009-03-03 14:55:29 +0000165int get_prefix(inet_prefix *dst, char *arg, int family)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166{
167 if (family == AF_PACKET) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000168 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 +0000169 }
170 if (get_prefix_1(dst, arg, family)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000171 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000172 }
173 return 0;
174}
175
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000176uint32_t get_addr32(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000177{
178 inet_prefix addr;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000179
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180 if (get_addr_1(&addr, name, AF_INET)) {
Bernhard Reutner-Fischer8fbd8ac2008-10-21 12:42:45 +0000181 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000182 }
183 return addr.data[0];
184}
185
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000186void incomplete_command(void)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000188 bb_error_msg_and_die("command line is not complete, try option \"help\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000189}
190
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000191void invarg(const char *arg, const char *opt)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000192{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000193 bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000194}
195
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000196void duparg(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000197{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000198 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199}
200
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000201void duparg2(const char *key, const char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000202{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000203 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000204}
205
Denis Vlasenko787a4922009-03-03 14:55:29 +0000206int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000207{
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000208 uint32_t *a1 = a->data;
209 uint32_t *a2 = b->data;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000210 int words = bits >> 5;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000211
212 bits &= 0x1f;
213
214 if (words)
215 if (memcmp(a1, a2, words << 2))
216 return -1;
217
218 if (bits) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000219 uint32_t w1, w2;
220 uint32_t mask;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221
222 w1 = a1[words];
223 w2 = a2[words];
224
225 mask = htonl((0xffffffff) << (0x20 - bits));
226
227 if ((w1 ^ w2) & mask)
228 return 1;
229 }
230
231 return 0;
232}
233
Denis Vlasenko76140a72009-03-05 09:21:57 +0000234const char *rt_addr_n2a(int af,
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000235 void *addr, char *buf, int buflen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000236{
237 switch (af) {
238 case AF_INET:
239 case AF_INET6:
240 return inet_ntop(af, addr, buf, buflen);
241 default:
242 return "???";
243 }
244}
245
Denis Vlasenko76140a72009-03-05 09:21:57 +0000246#ifdef RESOLVE_HOSTNAMES
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247const char *format_host(int af, int len, void *addr, char *buf, int buflen)
248{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249 if (resolve_hosts) {
250 struct hostent *h_ent;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000251
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252 if (len <= 0) {
253 switch (af) {
254 case AF_INET:
255 len = 4;
256 break;
257 case AF_INET6:
258 len = 16;
259 break;
Glenn L McGrath50c00f42002-11-18 07:26:42 +0000260 default:;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000261 }
262 }
Denis Vlasenko8c1aaf32007-06-04 21:03:51 +0000263 if (len > 0) {
264 h_ent = gethostbyaddr(addr, len, af);
265 if (h_ent != NULL) {
266 safe_strncpy(buf, h_ent->h_name, buflen);
267 return buf;
268 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000269 }
270 }
Denis Vlasenko76140a72009-03-05 09:21:57 +0000271 return rt_addr_n2a(af, addr, buf, buflen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272}
Denis Vlasenko76140a72009-03-05 09:21:57 +0000273#endif