"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * utils.c |
| 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame^] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
| 7 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 8 | * |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 9 | * Changes: |
| 10 | * |
| 11 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
| 12 | */ |
| 13 | |
Rob Landley | ecae66a | 2006-06-02 20:53:38 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 15 | #include "utils.h" |
"Vladimir N. Oleynik" | 007a011 | 2005-09-22 11:11:11 +0000 | [diff] [blame] | 16 | #include "inet_common.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 18 | unsigned get_unsigned(char *arg, const char *errmsg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 19 | { |
| 20 | unsigned long res; |
| 21 | char *ptr; |
| 22 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 23 | if (*arg) { |
| 24 | res = strtoul(arg, &ptr, 0); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 25 | //FIXME: "" will be accepted too, is it correct?! |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 26 | if (!*ptr && res <= UINT_MAX) { |
| 27 | return res; |
| 28 | } |
| 29 | } |
| 30 | invarg(arg, errmsg); /* does not return */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 33 | uint32_t get_u32(char *arg, const char *errmsg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 34 | { |
| 35 | unsigned long res; |
| 36 | char *ptr; |
| 37 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 38 | if (*arg) { |
| 39 | res = strtoul(arg, &ptr, 0); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 40 | //FIXME: "" will be accepted too, is it correct?! |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 41 | if (!*ptr && res <= 0xFFFFFFFFUL) { |
| 42 | return res; |
| 43 | } |
| 44 | } |
| 45 | invarg(arg, errmsg); /* does not return */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 48 | uint16_t get_u16(char *arg, const char *errmsg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 49 | { |
| 50 | unsigned long res; |
| 51 | char *ptr; |
| 52 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 53 | if (*arg) { |
| 54 | res = strtoul(arg, &ptr, 0); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 55 | //FIXME: "" will be accepted too, is it correct?! |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 56 | if (!*ptr && res <= 0xFFFF) { |
| 57 | return res; |
| 58 | } |
| 59 | } |
| 60 | invarg(arg, errmsg); /* does not return */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 63 | int get_addr_1(inet_prefix *addr, char *name, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 64 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 65 | memset(addr, 0, sizeof(*addr)); |
| 66 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 67 | if (strcmp(name, bb_str_default) == 0 |
| 68 | || strcmp(name, "all") == 0 |
| 69 | || strcmp(name, "any") == 0 |
| 70 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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; |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 91 | if (inet_pton(AF_INET, name, addr->data) <= 0) |
| 92 | return -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 93 | addr->bytelen = 4; |
| 94 | addr->bitlen = -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 98 | static int get_prefix_1(inet_prefix *dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 99 | { |
| 100 | int err; |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 101 | unsigned plen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 102 | char *slash; |
| 103 | |
| 104 | memset(dst, 0, sizeof(*dst)); |
| 105 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 106 | if (strcmp(arg, bb_str_default) == 0 |
| 107 | || strcmp(arg, "all") == 0 |
| 108 | || strcmp(arg, "any") == 0 |
| 109 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | dst->family = family; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 111 | /*dst->bytelen = 0; - done by memset */ |
| 112 | /*dst->bitlen = 0;*/ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | slash = strchr(arg, '/'); |
| 117 | if (slash) |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 118 | *slash = '\0'; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 119 | err = get_addr_1(dst, arg, family); |
| 120 | if (err == 0) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 121 | dst->bitlen = (dst->family == AF_INET6) ? 128 : 32; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 122 | if (slash) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 123 | inet_prefix netmask_pfx; |
| 124 | |
| 125 | netmask_pfx.family = AF_UNSPEC; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 126 | plen = bb_strtou(slash + 1, NULL, 0); |
| 127 | if ((errno || plen > dst->bitlen) |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 128 | && (get_addr_1(&netmask_pfx, slash + 1, family))) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 129 | err = -1; |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 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; |
Denys Vlasenko | fd2dc53 | 2009-10-04 01:13:35 +0200 | [diff] [blame] | 139 | if (plen <= dst->bitlen) { |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 140 | dst->bitlen = plen; |
| 141 | /* dst->flags |= PREFIXLEN_SPECIFIED; */ |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 142 | } else |
| 143 | err = -1; |
| 144 | } else |
| 145 | err = -1; |
| 146 | } else { |
| 147 | /* plain prefix */ |
| 148 | dst->bitlen = plen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 149 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 152 | if (slash) |
| 153 | *slash = '/'; |
| 154 | return err; |
| 155 | } |
| 156 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 157 | int get_addr(inet_prefix *dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 158 | { |
| 159 | if (family == AF_PACKET) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 160 | bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 161 | } |
| 162 | if (get_addr_1(dst, arg, family)) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 163 | bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 168 | int get_prefix(inet_prefix *dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 169 | { |
| 170 | if (family == AF_PACKET) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 171 | bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 172 | } |
| 173 | if (get_prefix_1(dst, arg, family)) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 174 | bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 179 | uint32_t get_addr32(char *name) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 180 | { |
| 181 | inet_prefix addr; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 182 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 183 | if (get_addr_1(&addr, name, AF_INET)) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 184 | bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 185 | } |
| 186 | return addr.data[0]; |
| 187 | } |
| 188 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 189 | void incomplete_command(void) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 190 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 191 | bb_error_msg_and_die("command line is not complete, try option \"help\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 194 | void invarg(const char *arg, const char *opt) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 195 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 196 | bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 199 | void duparg(const char *key, const char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 200 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 201 | bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 204 | void duparg2(const char *key, const char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 205 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 206 | bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 209 | int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 210 | { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 211 | uint32_t *a1 = a->data; |
| 212 | uint32_t *a2 = b->data; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 213 | int words = bits >> 5; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 214 | |
| 215 | bits &= 0x1f; |
| 216 | |
| 217 | if (words) |
| 218 | if (memcmp(a1, a2, words << 2)) |
| 219 | return -1; |
| 220 | |
| 221 | if (bits) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 222 | uint32_t w1, w2; |
| 223 | uint32_t mask; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 237 | const char *rt_addr_n2a(int af, |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 238 | void *addr, char *buf, int buflen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 249 | #ifdef RESOLVE_HOSTNAMES |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 250 | const char *format_host(int af, int len, void *addr, char *buf, int buflen) |
| 251 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 252 | if (resolve_hosts) { |
| 253 | struct hostent *h_ent; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 254 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 263 | default:; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
Denis Vlasenko | 8c1aaf3 | 2007-06-04 21:03:51 +0000 | [diff] [blame] | 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 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 274 | return rt_addr_n2a(af, addr, buf, buflen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 275 | } |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 276 | #endif |