"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" |
| 15 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 16 | #include <string.h> |
Eric Andersen | 496411b | 2003-02-12 10:56:53 +0000 | [diff] [blame] | 17 | #include <unistd.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 18 | |
| 19 | #include "utils.h" |
"Vladimir N. Oleynik" | 007a011 | 2005-09-22 11:11:11 +0000 | [diff] [blame] | 20 | #include "inet_common.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 21 | |
| 22 | int get_integer(int *val, char *arg, int base) |
| 23 | { |
| 24 | long res; |
| 25 | char *ptr; |
| 26 | |
| 27 | if (!arg || !*arg) |
| 28 | return -1; |
| 29 | res = strtol(arg, &ptr, base); |
| 30 | if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN) |
| 31 | return -1; |
| 32 | *val = res; |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | int get_unsigned(unsigned *val, char *arg, int base) |
| 37 | { |
| 38 | unsigned long res; |
| 39 | char *ptr; |
| 40 | |
| 41 | if (!arg || !*arg) |
| 42 | return -1; |
| 43 | res = strtoul(arg, &ptr, base); |
| 44 | if (!ptr || ptr == arg || *ptr || res > UINT_MAX) |
| 45 | return -1; |
| 46 | *val = res; |
| 47 | return 0; |
| 48 | } |
| 49 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 50 | int get_u32(__u32 * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 51 | { |
| 52 | unsigned long res; |
| 53 | char *ptr; |
| 54 | |
| 55 | if (!arg || !*arg) |
| 56 | return -1; |
| 57 | res = strtoul(arg, &ptr, base); |
| 58 | if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL) |
| 59 | return -1; |
| 60 | *val = res; |
| 61 | return 0; |
| 62 | } |
| 63 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 64 | int get_u16(__u16 * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 65 | { |
| 66 | unsigned long res; |
| 67 | char *ptr; |
| 68 | |
| 69 | if (!arg || !*arg) |
| 70 | return -1; |
| 71 | res = strtoul(arg, &ptr, base); |
| 72 | if (!ptr || ptr == arg || *ptr || res > 0xFFFF) |
| 73 | return -1; |
| 74 | *val = res; |
| 75 | return 0; |
| 76 | } |
| 77 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 78 | int get_u8(__u8 * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 79 | { |
| 80 | unsigned long res; |
| 81 | char *ptr; |
| 82 | |
| 83 | if (!arg || !*arg) |
| 84 | return -1; |
| 85 | res = strtoul(arg, &ptr, base); |
| 86 | if (!ptr || ptr == arg || *ptr || res > 0xFF) |
| 87 | return -1; |
| 88 | *val = res; |
| 89 | return 0; |
| 90 | } |
| 91 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 92 | int get_s16(__s16 * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 93 | { |
| 94 | long res; |
| 95 | char *ptr; |
| 96 | |
| 97 | if (!arg || !*arg) |
| 98 | return -1; |
| 99 | res = strtol(arg, &ptr, base); |
| 100 | if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000) |
| 101 | return -1; |
| 102 | *val = res; |
| 103 | return 0; |
| 104 | } |
| 105 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 106 | int get_s8(__s8 * val, char *arg, int base) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 107 | { |
| 108 | long res; |
| 109 | char *ptr; |
| 110 | |
| 111 | if (!arg || !*arg) |
| 112 | return -1; |
| 113 | res = strtol(arg, &ptr, base); |
| 114 | if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80) |
| 115 | return -1; |
| 116 | *val = res; |
| 117 | return 0; |
| 118 | } |
| 119 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 120 | int get_addr_1(inet_prefix * addr, char *name, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 121 | { |
| 122 | char *cp; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 123 | unsigned char *ap = (unsigned char *) addr->data; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 124 | int i; |
| 125 | |
| 126 | memset(addr, 0, sizeof(*addr)); |
| 127 | |
"Vladimir N. Oleynik" | 007a011 | 2005-09-22 11:11:11 +0000 | [diff] [blame] | 128 | if (strcmp(name, bb_INET_default) == 0 || |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 129 | strcmp(name, "all") == 0 || strcmp(name, "any") == 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 130 | addr->family = family; |
| 131 | addr->bytelen = (family == AF_INET6 ? 16 : 4); |
| 132 | addr->bitlen = -1; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | if (strchr(name, ':')) { |
| 137 | addr->family = AF_INET6; |
| 138 | if (family != AF_UNSPEC && family != AF_INET6) |
| 139 | return -1; |
| 140 | if (inet_pton(AF_INET6, name, addr->data) <= 0) |
| 141 | return -1; |
| 142 | addr->bytelen = 16; |
| 143 | addr->bitlen = -1; |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | addr->family = AF_INET; |
| 148 | if (family != AF_UNSPEC && family != AF_INET) |
| 149 | return -1; |
| 150 | addr->bytelen = 4; |
| 151 | addr->bitlen = -1; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 152 | for (cp = name, i = 0; *cp; cp++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 153 | if (*cp <= '9' && *cp >= '0') { |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 154 | ap[i] = 10 * ap[i] + (*cp - '0'); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 155 | continue; |
| 156 | } |
| 157 | if (*cp == '.' && ++i <= 3) |
| 158 | continue; |
| 159 | return -1; |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 164 | int get_prefix_1(inet_prefix * dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 165 | { |
| 166 | int err; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 167 | int plen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 168 | char *slash; |
| 169 | |
| 170 | memset(dst, 0, sizeof(*dst)); |
| 171 | |
"Vladimir N. Oleynik" | 007a011 | 2005-09-22 11:11:11 +0000 | [diff] [blame] | 172 | if (strcmp(arg, bb_INET_default) == 0 || strcmp(arg, "any") == 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 173 | dst->family = family; |
| 174 | dst->bytelen = 0; |
| 175 | dst->bitlen = 0; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | slash = strchr(arg, '/'); |
| 180 | if (slash) |
| 181 | *slash = 0; |
| 182 | err = get_addr_1(dst, arg, family); |
| 183 | if (err == 0) { |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 184 | switch (dst->family) { |
| 185 | case AF_INET6: |
| 186 | dst->bitlen = 128; |
| 187 | break; |
| 188 | default: |
| 189 | case AF_INET: |
| 190 | dst->bitlen = 32; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 191 | } |
| 192 | if (slash) { |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 193 | if (get_integer(&plen, slash + 1, 0) || plen > dst->bitlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 194 | err = -1; |
| 195 | goto done; |
| 196 | } |
| 197 | dst->bitlen = plen; |
| 198 | } |
| 199 | } |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 200 | done: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 201 | if (slash) |
| 202 | *slash = '/'; |
| 203 | return err; |
| 204 | } |
| 205 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 206 | int get_addr(inet_prefix * dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 207 | { |
| 208 | if (family == AF_PACKET) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 209 | 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] | 210 | } |
| 211 | if (get_addr_1(dst, arg, family)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 212 | 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] | 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 217 | int get_prefix(inet_prefix * dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 218 | { |
| 219 | if (family == AF_PACKET) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 220 | 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] | 221 | } |
| 222 | if (get_prefix_1(dst, arg, family)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 223 | 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] | 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | __u32 get_addr32(char *name) |
| 229 | { |
| 230 | inet_prefix addr; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 231 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 232 | if (get_addr_1(&addr, name, AF_INET)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 233 | 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] | 234 | } |
| 235 | return addr.data[0]; |
| 236 | } |
| 237 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 238 | void incomplete_command(void) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 239 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 240 | bb_error_msg("Command line is not complete. Try option \"help\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 241 | exit(-1); |
| 242 | } |
| 243 | |
Bernhard Reutner-Fischer | f1bd42a | 2006-06-14 16:51:50 +0000 | [diff] [blame] | 244 | void invarg(const char * const arg, const char * const opt) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 245 | { |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 246 | bb_error_msg(bb_msg_invalid_arg, arg, opt); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 247 | exit(-1); |
| 248 | } |
| 249 | |
| 250 | void duparg(char *key, char *arg) |
| 251 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 252 | bb_error_msg("duplicate \"%s\": \"%s\" is the second value.", key, arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 253 | exit(-1); |
| 254 | } |
| 255 | |
| 256 | void duparg2(char *key, char *arg) |
| 257 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 258 | bb_error_msg("either \"%s\" is duplicate, or \"%s\" is a garbage.", key, arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 259 | exit(-1); |
| 260 | } |
| 261 | |
| 262 | int matches(char *cmd, char *pattern) |
| 263 | { |
| 264 | int len = strlen(cmd); |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 265 | |
Glenn L McGrath | 82d2cd4 | 2002-11-28 12:45:48 +0000 | [diff] [blame] | 266 | if (len > strlen(pattern)) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 267 | return -1; |
Glenn L McGrath | 82d2cd4 | 2002-11-28 12:45:48 +0000 | [diff] [blame] | 268 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 269 | return memcmp(pattern, cmd, len); |
| 270 | } |
| 271 | |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 272 | 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] | 273 | { |
| 274 | __u32 *a1 = a->data; |
| 275 | __u32 *a2 = b->data; |
| 276 | int words = bits >> 0x05; |
| 277 | |
| 278 | bits &= 0x1f; |
| 279 | |
| 280 | if (words) |
| 281 | if (memcmp(a1, a2, words << 2)) |
| 282 | return -1; |
| 283 | |
| 284 | if (bits) { |
| 285 | __u32 w1, w2; |
| 286 | __u32 mask; |
| 287 | |
| 288 | w1 = a1[words]; |
| 289 | w2 = a2[words]; |
| 290 | |
| 291 | mask = htonl((0xffffffff) << (0x20 - bits)); |
| 292 | |
| 293 | if ((w1 ^ w2) & mask) |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int __iproute2_hz_internal; |
| 301 | |
| 302 | int __get_hz(void) |
| 303 | { |
| 304 | int hz = 0; |
| 305 | FILE *fp = fopen("/proc/net/psched", "r"); |
| 306 | |
| 307 | if (fp) { |
| 308 | unsigned nom, denom; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 309 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 310 | if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) |
| 311 | if (nom == 1000000) |
| 312 | hz = denom; |
| 313 | fclose(fp); |
| 314 | } |
| 315 | if (hz) |
| 316 | return hz; |
Eric Andersen | 496411b | 2003-02-12 10:56:53 +0000 | [diff] [blame] | 317 | return sysconf(_SC_CLK_TCK); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 320 | const char *rt_addr_n2a(int af, int ATTRIBUTE_UNUSED len, |
| 321 | void *addr, char *buf, int buflen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 322 | { |
| 323 | switch (af) { |
| 324 | case AF_INET: |
| 325 | case AF_INET6: |
| 326 | return inet_ntop(af, addr, buf, buflen); |
| 327 | default: |
| 328 | return "???"; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | |
| 333 | const char *format_host(int af, int len, void *addr, char *buf, int buflen) |
| 334 | { |
| 335 | #ifdef RESOLVE_HOSTNAMES |
| 336 | if (resolve_hosts) { |
| 337 | struct hostent *h_ent; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 338 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 339 | if (len <= 0) { |
| 340 | switch (af) { |
| 341 | case AF_INET: |
| 342 | len = 4; |
| 343 | break; |
| 344 | case AF_INET6: |
| 345 | len = 16; |
| 346 | break; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 347 | default:; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 350 | if (len > 0 && (h_ent = gethostbyaddr(addr, len, af)) != NULL) { |
| 351 | snprintf(buf, buflen - 1, "%s", h_ent->h_name); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 352 | return buf; |
| 353 | } |
| 354 | } |
| 355 | #endif |
| 356 | return rt_addr_n2a(af, len, addr, buf, buflen); |
| 357 | } |