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