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