"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 | |
| 18 | int get_integer(int *val, char *arg, int base) |
| 19 | { |
| 20 | long res; |
| 21 | char *ptr; |
| 22 | |
| 23 | if (!arg || !*arg) |
| 24 | return -1; |
| 25 | res = strtol(arg, &ptr, base); |
| 26 | if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN) |
| 27 | return -1; |
| 28 | *val = res; |
| 29 | return 0; |
| 30 | } |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 31 | //XXX: FIXME: use some libbb function instead |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 32 | int get_unsigned(unsigned *val, char *arg, int base) |
| 33 | { |
| 34 | unsigned long res; |
| 35 | char *ptr; |
| 36 | |
| 37 | if (!arg || !*arg) |
| 38 | return -1; |
| 39 | res = strtoul(arg, &ptr, base); |
| 40 | if (!ptr || ptr == arg || *ptr || res > UINT_MAX) |
| 41 | return -1; |
| 42 | *val = res; |
| 43 | return 0; |
| 44 | } |
| 45 | |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 46 | int get_u32(uint32_t * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 47 | { |
| 48 | unsigned long res; |
| 49 | char *ptr; |
| 50 | |
| 51 | if (!arg || !*arg) |
| 52 | return -1; |
| 53 | res = strtoul(arg, &ptr, base); |
| 54 | if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL) |
| 55 | return -1; |
| 56 | *val = res; |
| 57 | return 0; |
| 58 | } |
| 59 | |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 60 | int get_u16(uint16_t * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 61 | { |
| 62 | unsigned long res; |
| 63 | char *ptr; |
| 64 | |
| 65 | if (!arg || !*arg) |
| 66 | return -1; |
| 67 | res = strtoul(arg, &ptr, base); |
| 68 | if (!ptr || ptr == arg || *ptr || res > 0xFFFF) |
| 69 | return -1; |
| 70 | *val = res; |
| 71 | return 0; |
| 72 | } |
| 73 | |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 74 | int get_u8(uint8_t * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 75 | { |
| 76 | unsigned long res; |
| 77 | char *ptr; |
| 78 | |
| 79 | if (!arg || !*arg) |
| 80 | return -1; |
| 81 | res = strtoul(arg, &ptr, base); |
| 82 | if (!ptr || ptr == arg || *ptr || res > 0xFF) |
| 83 | return -1; |
| 84 | *val = res; |
| 85 | return 0; |
| 86 | } |
| 87 | |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 88 | int get_s16(int16_t * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 89 | { |
| 90 | long res; |
| 91 | char *ptr; |
| 92 | |
| 93 | if (!arg || !*arg) |
| 94 | return -1; |
| 95 | res = strtol(arg, &ptr, base); |
| 96 | if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000) |
| 97 | return -1; |
| 98 | *val = res; |
| 99 | return 0; |
| 100 | } |
| 101 | |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 102 | int get_s8(int8_t * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 103 | { |
| 104 | long res; |
| 105 | char *ptr; |
| 106 | |
| 107 | if (!arg || !*arg) |
| 108 | return -1; |
| 109 | res = strtol(arg, &ptr, base); |
| 110 | if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80) |
| 111 | return -1; |
| 112 | *val = res; |
| 113 | return 0; |
| 114 | } |
| 115 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 116 | int get_addr_1(inet_prefix * addr, char *name, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 117 | { |
| 118 | char *cp; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 119 | unsigned char *ap = (unsigned char *) addr->data; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 120 | int i; |
| 121 | |
| 122 | memset(addr, 0, sizeof(*addr)); |
| 123 | |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 124 | if (strcmp(name, bb_str_default) == 0 || |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 125 | strcmp(name, "all") == 0 || strcmp(name, "any") == 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 126 | addr->family = family; |
| 127 | addr->bytelen = (family == AF_INET6 ? 16 : 4); |
| 128 | addr->bitlen = -1; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | if (strchr(name, ':')) { |
| 133 | addr->family = AF_INET6; |
| 134 | if (family != AF_UNSPEC && family != AF_INET6) |
| 135 | return -1; |
| 136 | if (inet_pton(AF_INET6, name, addr->data) <= 0) |
| 137 | return -1; |
| 138 | addr->bytelen = 16; |
| 139 | addr->bitlen = -1; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | addr->family = AF_INET; |
| 144 | if (family != AF_UNSPEC && family != AF_INET) |
| 145 | return -1; |
| 146 | addr->bytelen = 4; |
| 147 | addr->bitlen = -1; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 148 | for (cp = name, i = 0; *cp; cp++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 149 | if (*cp <= '9' && *cp >= '0') { |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 150 | ap[i] = 10 * ap[i] + (*cp - '0'); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | if (*cp == '.' && ++i <= 3) |
| 154 | continue; |
| 155 | return -1; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 160 | int get_prefix_1(inet_prefix * dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 161 | { |
| 162 | int err; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 163 | int plen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 164 | char *slash; |
| 165 | |
| 166 | memset(dst, 0, sizeof(*dst)); |
| 167 | |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 168 | if (strcmp(arg, bb_str_default) == 0 || strcmp(arg, "any") == 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 169 | dst->family = family; |
| 170 | dst->bytelen = 0; |
| 171 | dst->bitlen = 0; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | slash = strchr(arg, '/'); |
| 176 | if (slash) |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 177 | *slash = '\0'; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 178 | err = get_addr_1(dst, arg, family); |
| 179 | if (err == 0) { |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 180 | switch (dst->family) { |
| 181 | case AF_INET6: |
| 182 | dst->bitlen = 128; |
| 183 | break; |
| 184 | default: |
| 185 | case AF_INET: |
| 186 | dst->bitlen = 32; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 187 | } |
| 188 | if (slash) { |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 189 | if (get_integer(&plen, slash + 1, 0) || plen > dst->bitlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 190 | err = -1; |
| 191 | goto done; |
| 192 | } |
| 193 | dst->bitlen = plen; |
| 194 | } |
| 195 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 196 | done: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 197 | if (slash) |
| 198 | *slash = '/'; |
| 199 | return err; |
| 200 | } |
| 201 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 202 | int get_addr(inet_prefix * dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 203 | { |
| 204 | if (family == AF_PACKET) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 205 | bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 206 | } |
| 207 | if (get_addr_1(dst, arg, family)) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 208 | bb_error_msg_and_die("an inet address is expected rather than \"%s\"", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 213 | int get_prefix(inet_prefix * dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 214 | { |
| 215 | if (family == AF_PACKET) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 216 | bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 217 | } |
| 218 | if (get_prefix_1(dst, arg, family)) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 219 | bb_error_msg_and_die("an inet address is expected rather than \"%s\"", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 224 | uint32_t get_addr32(char *name) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 225 | { |
| 226 | inet_prefix addr; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 227 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 228 | if (get_addr_1(&addr, name, AF_INET)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 229 | bb_error_msg_and_die("an IP address is expected rather than \"%s\"", name); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 230 | } |
| 231 | return addr.data[0]; |
| 232 | } |
| 233 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 234 | void incomplete_command(void) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 235 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 236 | 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] | 237 | } |
| 238 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 239 | void invarg(const char *arg, const char *opt) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 240 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 241 | bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 244 | void duparg(const char *key, const char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 245 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 246 | 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] | 247 | } |
| 248 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 249 | void duparg2(const char *key, const char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 250 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 251 | 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] | 252 | } |
| 253 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 254 | 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] | 255 | { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 256 | uint32_t *a1 = a->data; |
| 257 | uint32_t *a2 = b->data; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 258 | int words = bits >> 0x05; |
| 259 | |
| 260 | bits &= 0x1f; |
| 261 | |
| 262 | if (words) |
| 263 | if (memcmp(a1, a2, words << 2)) |
| 264 | return -1; |
| 265 | |
| 266 | if (bits) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 267 | uint32_t w1, w2; |
| 268 | uint32_t mask; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 269 | |
| 270 | w1 = a1[words]; |
| 271 | w2 = a2[words]; |
| 272 | |
| 273 | mask = htonl((0xffffffff) << (0x20 - bits)); |
| 274 | |
| 275 | if ((w1 ^ w2) & mask) |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 282 | const char *rt_addr_n2a(int af, int UNUSED_PARAM len, |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 283 | void *addr, char *buf, int buflen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 284 | { |
| 285 | switch (af) { |
| 286 | case AF_INET: |
| 287 | case AF_INET6: |
| 288 | return inet_ntop(af, addr, buf, buflen); |
| 289 | default: |
| 290 | return "???"; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | const char *format_host(int af, int len, void *addr, char *buf, int buflen) |
| 296 | { |
| 297 | #ifdef RESOLVE_HOSTNAMES |
| 298 | if (resolve_hosts) { |
| 299 | struct hostent *h_ent; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 300 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 301 | if (len <= 0) { |
| 302 | switch (af) { |
| 303 | case AF_INET: |
| 304 | len = 4; |
| 305 | break; |
| 306 | case AF_INET6: |
| 307 | len = 16; |
| 308 | break; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 309 | default:; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
Denis Vlasenko | 8c1aaf3 | 2007-06-04 21:03:51 +0000 | [diff] [blame] | 312 | if (len > 0) { |
| 313 | h_ent = gethostbyaddr(addr, len, af); |
| 314 | if (h_ent != NULL) { |
| 315 | safe_strncpy(buf, h_ent->h_name, buflen); |
| 316 | return buf; |
| 317 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | #endif |
| 321 | return rt_addr_n2a(af, len, addr, buf, buflen); |
| 322 | } |