"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 | * |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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); |
| 25 | if (!*ptr && res <= UINT_MAX) { |
| 26 | return res; |
| 27 | } |
| 28 | } |
| 29 | invarg(arg, errmsg); /* does not return */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 32 | uint32_t get_u32(char *arg, const char *errmsg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 33 | { |
| 34 | unsigned long res; |
| 35 | char *ptr; |
| 36 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 37 | 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 McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 46 | uint16_t get_u16(char *arg, const char *errmsg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 47 | { |
| 48 | unsigned long res; |
| 49 | char *ptr; |
| 50 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 51 | 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 McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 60 | int get_addr_1(inet_prefix *addr, char *name, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 61 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 62 | memset(addr, 0, sizeof(*addr)); |
| 63 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 64 | if (strcmp(name, bb_str_default) == 0 |
| 65 | || strcmp(name, "all") == 0 |
| 66 | || strcmp(name, "any") == 0 |
| 67 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 68 | 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-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 88 | if (inet_pton(AF_INET, name, addr->data) <= 0) |
| 89 | return -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 90 | addr->bytelen = 4; |
| 91 | addr->bitlen = -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 95 | 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] | 96 | { |
| 97 | int err; |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 98 | unsigned plen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 99 | char *slash; |
| 100 | |
| 101 | memset(dst, 0, sizeof(*dst)); |
| 102 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 103 | if (strcmp(arg, bb_str_default) == 0 |
| 104 | || strcmp(arg, "all") == 0 |
| 105 | || strcmp(arg, "any") == 0 |
| 106 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 107 | dst->family = family; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 108 | /*dst->bytelen = 0; - done by memset */ |
| 109 | /*dst->bitlen = 0;*/ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | slash = strchr(arg, '/'); |
| 114 | if (slash) |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 115 | *slash = '\0'; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 116 | err = get_addr_1(dst, arg, family); |
| 117 | if (err == 0) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 118 | dst->bitlen = (dst->family == AF_INET6) ? 128 : 32; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 119 | if (slash) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 120 | inet_prefix netmask_pfx; |
| 121 | |
| 122 | netmask_pfx.family = AF_UNSPEC; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 123 | plen = bb_strtou(slash + 1, NULL, 0); |
| 124 | if ((errno || plen > dst->bitlen) |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 125 | && (get_addr_1(&netmask_pfx, slash + 1, family))) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 126 | err = -1; |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 127 | 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 Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 137 | dst->bitlen = plen; |
| 138 | /* dst->flags |= PREFIXLEN_SPECIFIED; */ |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 139 | } else |
| 140 | err = -1; |
| 141 | } else |
| 142 | err = -1; |
| 143 | } else { |
| 144 | /* plain prefix */ |
| 145 | dst->bitlen = plen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 146 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 149 | if (slash) |
| 150 | *slash = '/'; |
| 151 | return err; |
| 152 | } |
| 153 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 154 | int get_addr(inet_prefix *dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 155 | { |
| 156 | if (family == AF_PACKET) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 157 | 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] | 158 | } |
| 159 | if (get_addr_1(dst, arg, family)) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 160 | 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] | 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 165 | int get_prefix(inet_prefix *dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 166 | { |
| 167 | if (family == AF_PACKET) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 168 | 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] | 169 | } |
| 170 | if (get_prefix_1(dst, arg, family)) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 171 | 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] | 172 | } |
| 173 | return 0; |
| 174 | } |
| 175 | |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 176 | uint32_t get_addr32(char *name) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 177 | { |
| 178 | inet_prefix addr; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 179 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 180 | if (get_addr_1(&addr, name, AF_INET)) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 181 | 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] | 182 | } |
| 183 | return addr.data[0]; |
| 184 | } |
| 185 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 186 | void incomplete_command(void) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 187 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 188 | 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] | 189 | } |
| 190 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 191 | void invarg(const char *arg, const char *opt) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 192 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 193 | bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 196 | void duparg(const char *key, const char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 197 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 198 | 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] | 199 | } |
| 200 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 201 | void duparg2(const char *key, const char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 202 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 203 | 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] | 204 | } |
| 205 | |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 206 | 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] | 207 | { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 208 | uint32_t *a1 = a->data; |
| 209 | uint32_t *a2 = b->data; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 210 | int words = bits >> 5; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 211 | |
| 212 | bits &= 0x1f; |
| 213 | |
| 214 | if (words) |
| 215 | if (memcmp(a1, a2, words << 2)) |
| 216 | return -1; |
| 217 | |
| 218 | if (bits) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 219 | uint32_t w1, w2; |
| 220 | uint32_t mask; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 221 | |
| 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 Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 234 | const char *rt_addr_n2a(int af, |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 235 | void *addr, char *buf, int buflen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 236 | { |
| 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 Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 246 | #ifdef RESOLVE_HOSTNAMES |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 247 | const char *format_host(int af, int len, void *addr, char *buf, int buflen) |
| 248 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 249 | if (resolve_hosts) { |
| 250 | struct hostent *h_ent; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 251 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 252 | 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 McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 260 | default:; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
Denis Vlasenko | 8c1aaf3 | 2007-06-04 21:03:51 +0000 | [diff] [blame] | 263 | 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 McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 271 | return rt_addr_n2a(af, addr, buf, buflen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 272 | } |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 273 | #endif |