| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* utils.c |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <inttypes.h> |
| 11 |
#include <math.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <fcntl.h> |
| 14 |
#include <limits.h> |
| 15 |
#include <sys/socket.h> |
| 16 |
#include <netinet/in.h> |
| 17 |
#include <string.h> |
| 18 |
#include <ctype.h> |
| 19 |
#include <netdb.h> |
| 20 |
#include <arpa/inet.h> |
| 21 |
#include <asm/types.h> |
| 22 |
#include <linux/pkt_sched.h> |
| 23 |
#include <linux/param.h> |
| 24 |
#include <linux/if_arp.h> |
| 25 |
#include <linux/mpls.h> |
| 26 |
#include <linux/snmp.h> |
| 27 |
#include <time.h> |
| 28 |
#include <sys/time.h> |
| 29 |
#include <errno.h> |
| 30 |
#ifdef HAVE_LIBCAP |
| 31 |
#include <sys/capability.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "rt_names.h" |
| 35 |
#include "utils.h" |
| 36 |
#include "ll_map.h" |
| 37 |
#include "namespace.h" |
| 38 |
|
| 39 |
int resolve_hosts; |
| 40 |
int timestamp_short; |
| 41 |
int pretty; |
| 42 |
int use_iec; |
| 43 |
int human_readable; |
| 44 |
const char *_SL_ = "\n"; |
| 45 |
|
| 46 |
static int open_fds[5]; |
| 47 |
static int open_fds_cnt; |
| 48 |
|
| 49 |
static int af_byte_len(int af); |
| 50 |
static void print_time(char *buf, int len, __u32 time); |
| 51 |
static void print_time64(char *buf, int len, __s64 time); |
| 52 |
|
| 53 |
int read_prop(const char *dev, char *prop, long *value) |
| 54 |
{ |
| 55 |
char fname[128], buf[80], *endp, *nl; |
| 56 |
FILE *fp; |
| 57 |
long result; |
| 58 |
int ret; |
| 59 |
|
| 60 |
ret = snprintf(fname, sizeof(fname), "/sys/class/net/%s/%s", |
| 61 |
dev, prop); |
| 62 |
|
| 63 |
if (ret <= 0 || ret >= sizeof(fname)) { |
| 64 |
fprintf(stderr, "could not build pathname for property\n"); |
| 65 |
return -1; |
| 66 |
} |
| 67 |
|
| 68 |
fp = fopen(fname, "r"); |
| 69 |
if (fp == NULL) { |
| 70 |
fprintf(stderr, "fopen %s: %s\n", fname, strerror(errno)); |
| 71 |
return -1; |
| 72 |
} |
| 73 |
|
| 74 |
if (!fgets(buf, sizeof(buf), fp)) { |
| 75 |
fprintf(stderr, "property \"%s\" in file %s is currently unknown\n", prop, fname); |
| 76 |
goto out; |
| 77 |
} |
| 78 |
|
| 79 |
nl = strchr(buf, '\n'); |
| 80 |
if (nl) |
| 81 |
*nl = '\0'; |
| 82 |
|
| 83 |
fclose(fp); |
| 84 |
result = strtol(buf, &endp, 0); |
| 85 |
|
| 86 |
if (*endp || buf == endp) { |
| 87 |
fprintf(stderr, "value \"%s\" in file %s is not a number\n", |
| 88 |
buf, fname); |
| 89 |
goto out; |
| 90 |
} |
| 91 |
|
| 92 |
if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE) { |
| 93 |
fprintf(stderr, "strtol %s: %s", fname, strerror(errno)); |
| 94 |
goto out; |
| 95 |
} |
| 96 |
|
| 97 |
*value = result; |
| 98 |
return 0; |
| 99 |
out: |
| 100 |
fclose(fp); |
| 101 |
fprintf(stderr, "Failed to parse %s\n", fname); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
|
| 105 |
static int get_hex(char c) |
| 106 |
{ |
| 107 |
if (c >= 'A' && c <= 'F') |
| 108 |
return c - 'A' + 10; |
| 109 |
if (c >= 'a' && c <= 'f') |
| 110 |
return c - 'a' + 10; |
| 111 |
if (c >= '0' && c <= '9') |
| 112 |
return c - '0'; |
| 113 |
|
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
int get_long(long *val, const char *arg, int base) |
| 118 |
{ |
| 119 |
long res; |
| 120 |
char *ptr; |
| 121 |
|
| 122 |
if (!arg || !*arg) |
| 123 |
return -1; |
| 124 |
|
| 125 |
res = strtol(arg, &ptr, base); |
| 126 |
|
| 127 |
/* If there were no digits at all, strtol() stores |
| 128 |
* the original value of nptr in *endptr (and returns 0). |
| 129 |
* In particular, if *nptr is not '\0' but **endptr is '\0' on return, |
| 130 |
* the entire string is valid. |
| 131 |
*/ |
| 132 |
if (!ptr || ptr == arg || *ptr) |
| 133 |
return -1; |
| 134 |
|
| 135 |
/* If an underflow occurs, strtol() returns LONG_MIN. |
| 136 |
* If an overflow occurs, strtol() returns LONG_MAX. |
| 137 |
* In both cases, errno is set to ERANGE. |
| 138 |
*/ |
| 139 |
if ((res == LONG_MAX || res == LONG_MIN) && errno == ERANGE) |
| 140 |
return -1; |
| 141 |
|
| 142 |
if (val) |
| 143 |
*val = res; |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
int get_integer(int *val, const char *arg, int base) |
| 148 |
{ |
| 149 |
long res; |
| 150 |
|
| 151 |
if (get_long(&res, arg, base) < 0) |
| 152 |
return -1; |
| 153 |
|
| 154 |
/* Outside range of int */ |
| 155 |
if (res < INT_MIN || res > INT_MAX) |
| 156 |
return -1; |
| 157 |
|
| 158 |
*val = res; |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
int mask2bits(__u32 netmask) |
| 163 |
{ |
| 164 |
unsigned int bits = 0; |
| 165 |
__u32 mask = ntohl(netmask); |
| 166 |
__u32 host = ~mask; |
| 167 |
|
| 168 |
/* a valid netmask must be 2^n - 1 */ |
| 169 |
if ((host & (host + 1)) != 0) |
| 170 |
return -1; |
| 171 |
|
| 172 |
for (; mask; mask <<= 1) |
| 173 |
++bits; |
| 174 |
return bits; |
| 175 |
} |
| 176 |
|
| 177 |
static int get_netmask(unsigned int *val, const char *arg, int base) |
| 178 |
{ |
| 179 |
inet_prefix addr; |
| 180 |
|
| 181 |
if (!get_unsigned(val, arg, base)) |
| 182 |
return 0; |
| 183 |
|
| 184 |
/* try converting dotted quad to CIDR */ |
| 185 |
if (!get_addr_1(&addr, arg, AF_INET) && addr.family == AF_INET) { |
| 186 |
int b = mask2bits(addr.data[0]); |
| 187 |
|
| 188 |
if (b >= 0) { |
| 189 |
*val = b; |
| 190 |
return 0; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
int get_unsigned(unsigned int *val, const char *arg, int base) |
| 198 |
{ |
| 199 |
unsigned long res; |
| 200 |
char *ptr; |
| 201 |
|
| 202 |
if (!arg || !*arg) |
| 203 |
return -1; |
| 204 |
|
| 205 |
res = strtoul(arg, &ptr, base); |
| 206 |
|
| 207 |
/* empty string or trailing non-digits */ |
| 208 |
if (!ptr || ptr == arg || *ptr) |
| 209 |
return -1; |
| 210 |
|
| 211 |
/* overflow */ |
| 212 |
if (res == ULONG_MAX && errno == ERANGE) |
| 213 |
return -1; |
| 214 |
|
| 215 |
/* out side range of unsigned */ |
| 216 |
if (res > UINT_MAX) |
| 217 |
return -1; |
| 218 |
|
| 219 |
*val = res; |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
int get_float(float *val, const char *arg) |
| 224 |
{ |
| 225 |
float res; |
| 226 |
char *ptr; |
| 227 |
|
| 228 |
if (!arg || !*arg) |
| 229 |
return -1; |
| 230 |
res = strtof(arg, &ptr); |
| 231 |
if (!ptr || ptr == arg || *ptr) |
| 232 |
return -1; |
| 233 |
*val = res; |
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
int get_float_min_max(float *val, const char *arg, float min, float max) |
| 238 |
{ |
| 239 |
float res; |
| 240 |
char *ptr; |
| 241 |
|
| 242 |
if (!arg || !*arg) |
| 243 |
return -1; |
| 244 |
res = strtof(arg, &ptr); |
| 245 |
if (!ptr || ptr == arg || *ptr) |
| 246 |
return -1; |
| 247 |
if (res < min || res > max) |
| 248 |
return -1; |
| 249 |
*val = res; |
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
* get_time_rtt is "translated" from a similar routine "get_time" in |
| 255 |
* tc_util.c. We don't use the exact same routine because tc passes |
| 256 |
* microseconds to the kernel and the callers of get_time_rtt want to |
| 257 |
* pass milliseconds (standard unit for rtt values since 2.6.27), and |
| 258 |
* have a different assumption for the units of a "raw" number. |
| 259 |
*/ |
| 260 |
int get_time_rtt(unsigned int *val, const char *arg, int *raw) |
| 261 |
{ |
| 262 |
double t; |
| 263 |
unsigned long res; |
| 264 |
char *p; |
| 265 |
|
| 266 |
if (strchr(arg, '.') != NULL) { |
| 267 |
t = strtod(arg, &p); |
| 268 |
if (t < 0.0) |
| 269 |
return -1; |
| 270 |
|
| 271 |
/* no digits? */ |
| 272 |
if (!p || p == arg) |
| 273 |
return -1; |
| 274 |
|
| 275 |
/* over/underflow */ |
| 276 |
if ((t == HUGE_VALF || t == HUGE_VALL) && errno == ERANGE) |
| 277 |
return -1; |
| 278 |
} else { |
| 279 |
res = strtoul(arg, &p, 0); |
| 280 |
|
| 281 |
/* empty string? */ |
| 282 |
if (!p || p == arg) |
| 283 |
return -1; |
| 284 |
|
| 285 |
/* overflow */ |
| 286 |
if (res == ULONG_MAX && errno == ERANGE) |
| 287 |
return -1; |
| 288 |
|
| 289 |
t = (double)res; |
| 290 |
} |
| 291 |
|
| 292 |
if (p == arg) |
| 293 |
return -1; |
| 294 |
*raw = 1; |
| 295 |
|
| 296 |
if (*p) { |
| 297 |
*raw = 0; |
| 298 |
if (strcasecmp(p, "s") == 0 || |
| 299 |
strcasecmp(p, "sec") == 0 || |
| 300 |
strcasecmp(p, "secs") == 0) |
| 301 |
t *= 1000; |
| 302 |
else if (strcasecmp(p, "ms") == 0 || |
| 303 |
strcasecmp(p, "msec") == 0 || |
| 304 |
strcasecmp(p, "msecs") == 0) |
| 305 |
t *= 1.0; /* allow suffix, do nothing */ |
| 306 |
else |
| 307 |
return -1; |
| 308 |
} |
| 309 |
|
| 310 |
/* emulate ceil() without having to bring-in -lm and always be >= 1 */ |
| 311 |
*val = t; |
| 312 |
if (*val < t) |
| 313 |
*val += 1; |
| 314 |
|
| 315 |
return 0; |
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
int get_u64(__u64 *val, const char *arg, int base) |
| 320 |
{ |
| 321 |
unsigned long long res; |
| 322 |
char *ptr; |
| 323 |
|
| 324 |
if (!arg || !*arg) |
| 325 |
return -1; |
| 326 |
|
| 327 |
res = strtoull(arg, &ptr, base); |
| 328 |
|
| 329 |
/* empty string or trailing non-digits */ |
| 330 |
if (!ptr || ptr == arg || *ptr) |
| 331 |
return -1; |
| 332 |
|
| 333 |
/* overflow */ |
| 334 |
if (res == ULLONG_MAX && errno == ERANGE) |
| 335 |
return -1; |
| 336 |
|
| 337 |
*val = res; |
| 338 |
return 0; |
| 339 |
} |
| 340 |
|
| 341 |
int get_u32(__u32 *val, const char *arg, int base) |
| 342 |
{ |
| 343 |
unsigned long res; |
| 344 |
char *ptr; |
| 345 |
|
| 346 |
if (!arg || !*arg) |
| 347 |
return -1; |
| 348 |
res = strtoul(arg, &ptr, base); |
| 349 |
|
| 350 |
/* empty string or trailing non-digits */ |
| 351 |
if (!ptr || ptr == arg || *ptr) |
| 352 |
return -1; |
| 353 |
|
| 354 |
/* overflow */ |
| 355 |
if (res == ULONG_MAX && errno == ERANGE) |
| 356 |
return -1; |
| 357 |
|
| 358 |
/* in case UL > 32 bits */ |
| 359 |
if (res > 0xFFFFFFFFUL) |
| 360 |
return -1; |
| 361 |
|
| 362 |
*val = res; |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
int get_u16(__u16 *val, const char *arg, int base) |
| 367 |
{ |
| 368 |
unsigned long res; |
| 369 |
char *ptr; |
| 370 |
|
| 371 |
if (!arg || !*arg) |
| 372 |
return -1; |
| 373 |
res = strtoul(arg, &ptr, base); |
| 374 |
|
| 375 |
/* empty string or trailing non-digits */ |
| 376 |
if (!ptr || ptr == arg || *ptr) |
| 377 |
return -1; |
| 378 |
|
| 379 |
/* overflow */ |
| 380 |
if (res == ULONG_MAX && errno == ERANGE) |
| 381 |
return -1; |
| 382 |
|
| 383 |
if (res > 0xFFFFUL) |
| 384 |
return -1; |
| 385 |
|
| 386 |
*val = res; |
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
int get_u8(__u8 *val, const char *arg, int base) |
| 391 |
{ |
| 392 |
unsigned long res; |
| 393 |
char *ptr; |
| 394 |
|
| 395 |
if (!arg || !*arg) |
| 396 |
return -1; |
| 397 |
|
| 398 |
res = strtoul(arg, &ptr, base); |
| 399 |
/* empty string or trailing non-digits */ |
| 400 |
if (!ptr || ptr == arg || *ptr) |
| 401 |
return -1; |
| 402 |
|
| 403 |
/* overflow */ |
| 404 |
if (res == ULONG_MAX && errno == ERANGE) |
| 405 |
return -1; |
| 406 |
|
| 407 |
if (res > 0xFFUL) |
| 408 |
return -1; |
| 409 |
|
| 410 |
*val = res; |
| 411 |
return 0; |
| 412 |
} |
| 413 |
|
| 414 |
int get_s64(__s64 *val, const char *arg, int base) |
| 415 |
{ |
| 416 |
long long res; |
| 417 |
char *ptr; |
| 418 |
|
| 419 |
errno = 0; |
| 420 |
|
| 421 |
if (!arg || !*arg) |
| 422 |
return -1; |
| 423 |
res = strtoll(arg, &ptr, base); |
| 424 |
if (!ptr || ptr == arg || *ptr) |
| 425 |
return -1; |
| 426 |
if ((res == LLONG_MIN || res == LLONG_MAX) && errno == ERANGE) |
| 427 |
return -1; |
| 428 |
|
| 429 |
*val = res; |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
int get_s32(__s32 *val, const char *arg, int base) |
| 434 |
{ |
| 435 |
long res; |
| 436 |
char *ptr; |
| 437 |
|
| 438 |
errno = 0; |
| 439 |
|
| 440 |
if (!arg || !*arg) |
| 441 |
return -1; |
| 442 |
res = strtol(arg, &ptr, base); |
| 443 |
if (!ptr || ptr == arg || *ptr) |
| 444 |
return -1; |
| 445 |
if ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) |
| 446 |
return -1; |
| 447 |
if (res > INT32_MAX || res < INT32_MIN) |
| 448 |
return -1; |
| 449 |
|
| 450 |
*val = res; |
| 451 |
return 0; |
| 452 |
} |
| 453 |
|
| 454 |
int get_be64(__be64 *val, const char *arg, int base) |
| 455 |
{ |
| 456 |
__u64 v; |
| 457 |
int ret = get_u64(&v, arg, base); |
| 458 |
|
| 459 |
if (!ret) |
| 460 |
*val = htonll(v); |
| 461 |
|
| 462 |
return ret; |
| 463 |
} |
| 464 |
|
| 465 |
int get_be32(__be32 *val, const char *arg, int base) |
| 466 |
{ |
| 467 |
__u32 v; |
| 468 |
int ret = get_u32(&v, arg, base); |
| 469 |
|
| 470 |
if (!ret) |
| 471 |
*val = htonl(v); |
| 472 |
|
| 473 |
return ret; |
| 474 |
} |
| 475 |
|
| 476 |
int get_be16(__be16 *val, const char *arg, int base) |
| 477 |
{ |
| 478 |
__u16 v; |
| 479 |
int ret = get_u16(&v, arg, base); |
| 480 |
|
| 481 |
if (!ret) |
| 482 |
*val = htons(v); |
| 483 |
|
| 484 |
return ret; |
| 485 |
} |
| 486 |
|
| 487 |
/* This uses a non-standard parsing (ie not inet_aton, or inet_pton) |
| 488 |
* because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8 |
| 489 |
*/ |
| 490 |
static int get_addr_ipv4(__u8 *ap, const char *cp) |
| 491 |
{ |
| 492 |
int i; |
| 493 |
|
| 494 |
for (i = 0; i < 4; i++) { |
| 495 |
unsigned long n; |
| 496 |
char *endp; |
| 497 |
|
| 498 |
n = strtoul(cp, &endp, 0); |
| 499 |
if (n > 255) |
| 500 |
return -1; /* bogus network value */ |
| 501 |
|
| 502 |
if (endp == cp) /* no digits */ |
| 503 |
return -1; |
| 504 |
|
| 505 |
ap[i] = n; |
| 506 |
|
| 507 |
if (*endp == '\0') |
| 508 |
break; |
| 509 |
|
| 510 |
if (i == 3 || *endp != '.') |
| 511 |
return -1; /* extra characters */ |
| 512 |
cp = endp + 1; |
| 513 |
} |
| 514 |
|
| 515 |
return 1; |
| 516 |
} |
| 517 |
|
| 518 |
int get_addr64(__u64 *ap, const char *cp) |
| 519 |
{ |
| 520 |
int i; |
| 521 |
|
| 522 |
union { |
| 523 |
__u16 v16[4]; |
| 524 |
__u64 v64; |
| 525 |
} val; |
| 526 |
|
| 527 |
for (i = 0; i < 4; i++) { |
| 528 |
unsigned long n; |
| 529 |
char *endp; |
| 530 |
|
| 531 |
n = strtoul(cp, &endp, 16); |
| 532 |
if (n > 0xffff) |
| 533 |
return -1; /* bogus network value */ |
| 534 |
|
| 535 |
if (endp == cp) /* no digits */ |
| 536 |
return -1; |
| 537 |
|
| 538 |
val.v16[i] = htons(n); |
| 539 |
|
| 540 |
if (*endp == '\0') |
| 541 |
break; |
| 542 |
|
| 543 |
if (i == 3 || *endp != ':') |
| 544 |
return -1; /* extra characters */ |
| 545 |
cp = endp + 1; |
| 546 |
} |
| 547 |
|
| 548 |
*ap = val.v64; |
| 549 |
|
| 550 |
return 1; |
| 551 |
} |
| 552 |
|
| 553 |
static void set_address_type(inet_prefix *addr) |
| 554 |
{ |
| 555 |
switch (addr->family) { |
| 556 |
case AF_INET: |
| 557 |
if (!addr->data[0]) |
| 558 |
addr->flags |= ADDRTYPE_INET_UNSPEC; |
| 559 |
else if (IN_MULTICAST(ntohl(addr->data[0]))) |
| 560 |
addr->flags |= ADDRTYPE_INET_MULTI; |
| 561 |
else |
| 562 |
addr->flags |= ADDRTYPE_INET; |
| 563 |
break; |
| 564 |
case AF_INET6: |
| 565 |
if (IN6_IS_ADDR_UNSPECIFIED(addr->data)) |
| 566 |
addr->flags |= ADDRTYPE_INET_UNSPEC; |
| 567 |
else if (IN6_IS_ADDR_MULTICAST(addr->data)) |
| 568 |
addr->flags |= ADDRTYPE_INET_MULTI; |
| 569 |
else |
| 570 |
addr->flags |= ADDRTYPE_INET; |
| 571 |
break; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
static int __get_addr_1(inet_prefix *addr, const char *name, int family) |
| 576 |
{ |
| 577 |
memset(addr, 0, sizeof(*addr)); |
| 578 |
|
| 579 |
if (strcmp(name, "default") == 0) { |
| 580 |
if (family == AF_MPLS) |
| 581 |
return -1; |
| 582 |
addr->family = family; |
| 583 |
addr->bytelen = af_byte_len(addr->family); |
| 584 |
addr->bitlen = -2; |
| 585 |
addr->flags |= PREFIXLEN_SPECIFIED; |
| 586 |
return 0; |
| 587 |
} |
| 588 |
|
| 589 |
if (strcmp(name, "all") == 0 || |
| 590 |
strcmp(name, "any") == 0) { |
| 591 |
if (family == AF_MPLS) |
| 592 |
return -1; |
| 593 |
addr->family = family; |
| 594 |
addr->bytelen = 0; |
| 595 |
addr->bitlen = -2; |
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
if (family == AF_PACKET) { |
| 600 |
int len; |
| 601 |
|
| 602 |
len = ll_addr_a2n((char *) &addr->data, sizeof(addr->data), |
| 603 |
name); |
| 604 |
if (len < 0) |
| 605 |
return -1; |
| 606 |
|
| 607 |
addr->family = AF_PACKET; |
| 608 |
addr->bytelen = len; |
| 609 |
addr->bitlen = len * 8; |
| 610 |
return 0; |
| 611 |
} |
| 612 |
|
| 613 |
if (strchr(name, ':')) { |
| 614 |
addr->family = AF_INET6; |
| 615 |
if (family != AF_UNSPEC && family != AF_INET6) |
| 616 |
return -1; |
| 617 |
if (inet_pton(AF_INET6, name, addr->data) <= 0) |
| 618 |
return -1; |
| 619 |
addr->bytelen = 16; |
| 620 |
addr->bitlen = -1; |
| 621 |
return 0; |
| 622 |
} |
| 623 |
|
| 624 |
if (family == AF_MPLS) { |
| 625 |
unsigned int maxlabels; |
| 626 |
int i; |
| 627 |
|
| 628 |
addr->family = AF_MPLS; |
| 629 |
if (mpls_pton(AF_MPLS, name, addr->data, |
| 630 |
sizeof(addr->data)) <= 0) |
| 631 |
return -1; |
| 632 |
addr->bytelen = 4; |
| 633 |
addr->bitlen = 20; |
| 634 |
/* How many bytes do I need? */ |
| 635 |
maxlabels = sizeof(addr->data) / sizeof(struct mpls_label); |
| 636 |
for (i = 0; i < maxlabels; i++) { |
| 637 |
if (ntohl(addr->data[i]) & MPLS_LS_S_MASK) { |
| 638 |
addr->bytelen = (i + 1)*4; |
| 639 |
break; |
| 640 |
} |
| 641 |
} |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
addr->family = AF_INET; |
| 646 |
if (family != AF_UNSPEC && family != AF_INET) |
| 647 |
return -1; |
| 648 |
|
| 649 |
if (get_addr_ipv4((__u8 *)addr->data, name) <= 0) |
| 650 |
return -1; |
| 651 |
|
| 652 |
addr->bytelen = 4; |
| 653 |
addr->bitlen = -1; |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
int get_addr_1(inet_prefix *addr, const char *name, int family) |
| 658 |
{ |
| 659 |
int ret; |
| 660 |
|
| 661 |
ret = __get_addr_1(addr, name, family); |
| 662 |
if (ret) |
| 663 |
return ret; |
| 664 |
|
| 665 |
set_address_type(addr); |
| 666 |
return 0; |
| 667 |
} |
| 668 |
|
| 669 |
int af_bit_len(int af) |
| 670 |
{ |
| 671 |
switch (af) { |
| 672 |
case AF_INET6: |
| 673 |
return 128; |
| 674 |
case AF_INET: |
| 675 |
return 32; |
| 676 |
case AF_MPLS: |
| 677 |
return 20; |
| 678 |
} |
| 679 |
|
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
static int af_byte_len(int af) |
| 684 |
{ |
| 685 |
return af_bit_len(af) / 8; |
| 686 |
} |
| 687 |
|
| 688 |
int get_prefix_1(inet_prefix *dst, char *arg, int family) |
| 689 |
{ |
| 690 |
char *slash; |
| 691 |
int err, bitlen, flags; |
| 692 |
|
| 693 |
slash = strchr(arg, '/'); |
| 694 |
if (slash) |
| 695 |
*slash = 0; |
| 696 |
|
| 697 |
err = get_addr_1(dst, arg, family); |
| 698 |
|
| 699 |
if (slash) |
| 700 |
*slash = '/'; |
| 701 |
|
| 702 |
if (err) |
| 703 |
return err; |
| 704 |
|
| 705 |
bitlen = af_bit_len(dst->family); |
| 706 |
|
| 707 |
flags = 0; |
| 708 |
if (slash) { |
| 709 |
unsigned int plen; |
| 710 |
|
| 711 |
if (dst->bitlen == -2) |
| 712 |
return -1; |
| 713 |
if (get_netmask(&plen, slash + 1, 0)) |
| 714 |
return -1; |
| 715 |
if (plen > bitlen) |
| 716 |
return -1; |
| 717 |
|
| 718 |
flags |= PREFIXLEN_SPECIFIED; |
| 719 |
bitlen = plen; |
| 720 |
} else { |
| 721 |
if (dst->bitlen == -2) |
| 722 |
bitlen = 0; |
| 723 |
} |
| 724 |
|
| 725 |
dst->flags |= flags; |
| 726 |
dst->bitlen = bitlen; |
| 727 |
|
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
static const char *family_name_verbose(int family) |
| 732 |
{ |
| 733 |
if (family == AF_UNSPEC) |
| 734 |
return "any valid"; |
| 735 |
return family_name(family); |
| 736 |
} |
| 737 |
|
| 738 |
int get_addr(inet_prefix *dst, const char *arg, int family) |
| 739 |
{ |
| 740 |
if (get_addr_1(dst, arg, family)) { |
| 741 |
fprintf(stderr, |
| 742 |
"Error: %s address is expected rather than \"%s\".\n", |
| 743 |
family_name_verbose(family), arg); |
| 744 |
exit(1); |
| 745 |
} |
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family) |
| 750 |
{ |
| 751 |
const int len = RTA_PAYLOAD(rta); |
| 752 |
const void *data = RTA_DATA(rta); |
| 753 |
|
| 754 |
switch (len) { |
| 755 |
case 4: |
| 756 |
dst->family = AF_INET; |
| 757 |
dst->bytelen = 4; |
| 758 |
memcpy(dst->data, data, 4); |
| 759 |
break; |
| 760 |
case 16: |
| 761 |
dst->family = AF_INET6; |
| 762 |
dst->bytelen = 16; |
| 763 |
memcpy(dst->data, data, 16); |
| 764 |
break; |
| 765 |
default: |
| 766 |
return -1; |
| 767 |
} |
| 768 |
|
| 769 |
if (family != AF_UNSPEC && family != dst->family) |
| 770 |
return -2; |
| 771 |
|
| 772 |
dst->bitlen = -1; |
| 773 |
dst->flags = 0; |
| 774 |
|
| 775 |
set_address_type(dst); |
| 776 |
return 0; |
| 777 |
} |
| 778 |
|
| 779 |
int get_prefix(inet_prefix *dst, char *arg, int family) |
| 780 |
{ |
| 781 |
if (family == AF_PACKET) { |
| 782 |
fprintf(stderr, |
| 783 |
"Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", |
| 784 |
arg); |
| 785 |
exit(1); |
| 786 |
} |
| 787 |
|
| 788 |
if (get_prefix_1(dst, arg, family)) { |
| 789 |
fprintf(stderr, |
| 790 |
"Error: %s prefix is expected rather than \"%s\".\n", |
| 791 |
family_name_verbose(family), arg); |
| 792 |
exit(1); |
| 793 |
} |
| 794 |
return 0; |
| 795 |
} |
| 796 |
|
| 797 |
__u32 get_addr32(const char *name) |
| 798 |
{ |
| 799 |
inet_prefix addr; |
| 800 |
|
| 801 |
if (get_addr_1(&addr, name, AF_INET)) { |
| 802 |
fprintf(stderr, |
| 803 |
"Error: an IP address is expected rather than \"%s\"\n", |
| 804 |
name); |
| 805 |
exit(1); |
| 806 |
} |
| 807 |
return addr.data[0]; |
| 808 |
} |
| 809 |
|
| 810 |
void incomplete_command(void) |
| 811 |
{ |
| 812 |
fprintf(stderr, "Command line is not complete. Try option \"help\"\n"); |
| 813 |
exit(-1); |
| 814 |
} |
| 815 |
|
| 816 |
void missarg(const char *key) |
| 817 |
{ |
| 818 |
fprintf(stderr, "Error: argument \"%s\" is required\n", key); |
| 819 |
exit(-1); |
| 820 |
} |
| 821 |
|
| 822 |
void invarg(const char *msg, const char *arg) |
| 823 |
{ |
| 824 |
fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg); |
| 825 |
exit(-1); |
| 826 |
} |
| 827 |
|
| 828 |
void duparg(const char *key, const char *arg) |
| 829 |
{ |
| 830 |
fprintf(stderr, |
| 831 |
"Error: duplicate \"%s\": \"%s\" is the second value.\n", |
| 832 |
key, arg); |
| 833 |
exit(-1); |
| 834 |
} |
| 835 |
|
| 836 |
void duparg2(const char *key, const char *arg) |
| 837 |
{ |
| 838 |
fprintf(stderr, |
| 839 |
"Error: either \"%s\" is duplicate, or \"%s\" is garbage.\n", |
| 840 |
key, arg); |
| 841 |
exit(-1); |
| 842 |
} |
| 843 |
|
| 844 |
int nodev(const char *dev) |
| 845 |
{ |
| 846 |
fprintf(stderr, "Cannot find device \"%s\"\n", dev); |
| 847 |
return -1; |
| 848 |
} |
| 849 |
|
| 850 |
/* These checks mimic kernel checks in dev_valid_name */ |
| 851 |
int check_ifname(const char *name) |
| 852 |
{ |
| 853 |
if (*name == '\0' || strnlen(name, IFNAMSIZ) == IFNAMSIZ) |
| 854 |
return -1; |
| 855 |
|
| 856 |
if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 857 |
return -1; |
| 858 |
|
| 859 |
while (*name) { |
| 860 |
if (*name == '/' || isspace(*name)) |
| 861 |
return -1; |
| 862 |
++name; |
| 863 |
} |
| 864 |
return 0; |
| 865 |
} |
| 866 |
|
| 867 |
/* Many less restrictions on altername names */ |
| 868 |
int check_altifname(const char *name) |
| 869 |
{ |
| 870 |
if (*name == '\0' || strnlen(name, ALTIFNAMSIZ) == ALTIFNAMSIZ) |
| 871 |
return -1; |
| 872 |
|
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
/* buf is assumed to be IFNAMSIZ */ |
| 877 |
int get_ifname(char *buf, const char *name) |
| 878 |
{ |
| 879 |
int ret; |
| 880 |
|
| 881 |
ret = check_ifname(name); |
| 882 |
if (ret == 0) |
| 883 |
strncpy(buf, name, IFNAMSIZ); |
| 884 |
|
| 885 |
return ret; |
| 886 |
} |
| 887 |
|
| 888 |
const char *get_ifname_rta(int ifindex, const struct rtattr *rta) |
| 889 |
{ |
| 890 |
const char *name; |
| 891 |
|
| 892 |
if (rta) { |
| 893 |
name = rta_getattr_str(rta); |
| 894 |
} else { |
| 895 |
fprintf(stderr, |
| 896 |
"BUG: device with ifindex %d has nil ifname\n", |
| 897 |
ifindex); |
| 898 |
name = ll_idx_n2a(ifindex); |
| 899 |
} |
| 900 |
|
| 901 |
if (check_ifname(name)) |
| 902 |
return NULL; |
| 903 |
|
| 904 |
return name; |
| 905 |
} |
| 906 |
|
| 907 |
/* Returns 0 if 'prefix' is a not empty prefix of 'string', != 0 otherwise. |
| 908 |
*/ |
| 909 |
int matches(const char *prefix, const char *string) |
| 910 |
{ |
| 911 |
if (!*prefix) |
| 912 |
return 1; |
| 913 |
while (*string && *prefix == *string) { |
| 914 |
prefix++; |
| 915 |
string++; |
| 916 |
} |
| 917 |
|
| 918 |
return *prefix; |
| 919 |
} |
| 920 |
|
| 921 |
static int matches_warn(const char *prefix, const char *string) |
| 922 |
{ |
| 923 |
int rc; |
| 924 |
|
| 925 |
rc = matches(prefix, string); |
| 926 |
if (rc) |
| 927 |
return rc; |
| 928 |
|
| 929 |
if (strlen(prefix) != strlen(string)) |
| 930 |
fprintf(stderr, |
| 931 |
"WARNING: '%s' matches '%s' by prefix.\n" |
| 932 |
"Matching by prefix is deprecated in this context, please use the full string.\n", |
| 933 |
prefix, string); |
| 934 |
|
| 935 |
return 0; |
| 936 |
} |
| 937 |
|
| 938 |
int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits) |
| 939 |
{ |
| 940 |
const __u32 *a1 = a->data; |
| 941 |
const __u32 *a2 = b->data; |
| 942 |
int words = bits >> 0x05; |
| 943 |
|
| 944 |
bits &= 0x1f; |
| 945 |
|
| 946 |
if (words) |
| 947 |
if (memcmp(a1, a2, words << 2)) |
| 948 |
return -1; |
| 949 |
|
| 950 |
if (bits) { |
| 951 |
__u32 w1, w2; |
| 952 |
__u32 mask; |
| 953 |
|
| 954 |
w1 = a1[words]; |
| 955 |
w2 = a2[words]; |
| 956 |
|
| 957 |
mask = htonl((0xffffffff) << (0x20 - bits)); |
| 958 |
|
| 959 |
if ((w1 ^ w2) & mask) |
| 960 |
return 1; |
| 961 |
} |
| 962 |
|
| 963 |
return 0; |
| 964 |
} |
| 965 |
|
| 966 |
int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta) |
| 967 |
{ |
| 968 |
inet_prefix dst; |
| 969 |
|
| 970 |
if (!rta || m->family == AF_UNSPEC || m->bitlen <= 0) |
| 971 |
return 0; |
| 972 |
|
| 973 |
if (get_addr_rta(&dst, rta, m->family)) |
| 974 |
return -1; |
| 975 |
|
| 976 |
return inet_addr_match(&dst, m, m->bitlen); |
| 977 |
} |
| 978 |
|
| 979 |
int __iproute2_hz_internal; |
| 980 |
|
| 981 |
int __get_hz(void) |
| 982 |
{ |
| 983 |
char name[1024]; |
| 984 |
int hz = 0; |
| 985 |
FILE *fp; |
| 986 |
|
| 987 |
if (getenv("HZ")) |
| 988 |
return atoi(getenv("HZ")) ? : HZ; |
| 989 |
|
| 990 |
if (getenv("PROC_NET_PSCHED")) |
| 991 |
snprintf(name, sizeof(name)-1, |
| 992 |
"%s", getenv("PROC_NET_PSCHED")); |
| 993 |
else if (getenv("PROC_ROOT")) |
| 994 |
snprintf(name, sizeof(name)-1, |
| 995 |
"%s/net/psched", getenv("PROC_ROOT")); |
| 996 |
else |
| 997 |
strcpy(name, "/proc/net/psched"); |
| 998 |
|
| 999 |
fp = fopen(name, "r"); |
| 1000 |
|
| 1001 |
if (fp) { |
| 1002 |
unsigned int nom, denom; |
| 1003 |
|
| 1004 |
if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) |
| 1005 |
if (nom == 1000000) |
| 1006 |
hz = denom; |
| 1007 |
fclose(fp); |
| 1008 |
} |
| 1009 |
if (hz) |
| 1010 |
return hz; |
| 1011 |
return HZ; |
| 1012 |
} |
| 1013 |
|
| 1014 |
int __iproute2_user_hz_internal; |
| 1015 |
|
| 1016 |
int __get_user_hz(void) |
| 1017 |
{ |
| 1018 |
return sysconf(_SC_CLK_TCK); |
| 1019 |
} |
| 1020 |
|
| 1021 |
const char *rt_addr_n2a_r(int af, int len, |
| 1022 |
const void *addr, char *buf, int buflen) |
| 1023 |
{ |
| 1024 |
switch (af) { |
| 1025 |
case AF_INET: |
| 1026 |
case AF_INET6: |
| 1027 |
return inet_ntop(af, addr, buf, buflen); |
| 1028 |
case AF_MPLS: |
| 1029 |
return mpls_ntop(af, addr, buf, buflen); |
| 1030 |
case AF_PACKET: |
| 1031 |
return ll_addr_n2a(addr, len, ARPHRD_VOID, buf, buflen); |
| 1032 |
case AF_BRIDGE: |
| 1033 |
{ |
| 1034 |
const union { |
| 1035 |
struct sockaddr sa; |
| 1036 |
struct sockaddr_in sin; |
| 1037 |
struct sockaddr_in6 sin6; |
| 1038 |
} *sa = addr; |
| 1039 |
|
| 1040 |
switch (sa->sa.sa_family) { |
| 1041 |
case AF_INET: |
| 1042 |
return inet_ntop(AF_INET, &sa->sin.sin_addr, |
| 1043 |
buf, buflen); |
| 1044 |
case AF_INET6: |
| 1045 |
return inet_ntop(AF_INET6, &sa->sin6.sin6_addr, |
| 1046 |
buf, buflen); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
/* fallthrough */ |
| 1050 |
default: |
| 1051 |
return "???"; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
const char *rt_addr_n2a(int af, int len, const void *addr) |
| 1056 |
{ |
| 1057 |
static char buf[256]; |
| 1058 |
|
| 1059 |
return rt_addr_n2a_r(af, len, addr, buf, 256); |
| 1060 |
} |
| 1061 |
|
| 1062 |
int read_family(const char *name) |
| 1063 |
{ |
| 1064 |
int family = AF_UNSPEC; |
| 1065 |
|
| 1066 |
if (strcmp(name, "inet") == 0) |
| 1067 |
family = AF_INET; |
| 1068 |
else if (strcmp(name, "inet6") == 0) |
| 1069 |
family = AF_INET6; |
| 1070 |
else if (strcmp(name, "link") == 0) |
| 1071 |
family = AF_PACKET; |
| 1072 |
else if (strcmp(name, "mpls") == 0) |
| 1073 |
family = AF_MPLS; |
| 1074 |
else if (strcmp(name, "bridge") == 0) |
| 1075 |
family = AF_BRIDGE; |
| 1076 |
return family; |
| 1077 |
} |
| 1078 |
|
| 1079 |
const char *family_name(int family) |
| 1080 |
{ |
| 1081 |
if (family == AF_INET) |
| 1082 |
return "inet"; |
| 1083 |
if (family == AF_INET6) |
| 1084 |
return "inet6"; |
| 1085 |
if (family == AF_PACKET) |
| 1086 |
return "link"; |
| 1087 |
if (family == AF_MPLS) |
| 1088 |
return "mpls"; |
| 1089 |
if (family == AF_BRIDGE) |
| 1090 |
return "bridge"; |
| 1091 |
return "???"; |
| 1092 |
} |
| 1093 |
|
| 1094 |
#ifdef RESOLVE_HOSTNAMES |
| 1095 |
struct namerec { |
| 1096 |
struct namerec *next; |
| 1097 |
const char *name; |
| 1098 |
inet_prefix addr; |
| 1099 |
}; |
| 1100 |
|
| 1101 |
#define NHASH 257 |
| 1102 |
static struct namerec *nht[NHASH]; |
| 1103 |
|
| 1104 |
static const char *resolve_address(const void *addr, int len, int af) |
| 1105 |
{ |
| 1106 |
struct namerec *n; |
| 1107 |
struct hostent *h_ent; |
| 1108 |
unsigned int hash; |
| 1109 |
static int notfirst; |
| 1110 |
|
| 1111 |
|
| 1112 |
if (af == AF_INET6 && ((__u32 *)addr)[0] == 0 && |
| 1113 |
((__u32 *)addr)[1] == 0 && ((__u32 *)addr)[2] == htonl(0xffff)) { |
| 1114 |
af = AF_INET; |
| 1115 |
addr += 12; |
| 1116 |
len = 4; |
| 1117 |
} |
| 1118 |
|
| 1119 |
hash = *(__u32 *)(addr + len - 4) % NHASH; |
| 1120 |
|
| 1121 |
for (n = nht[hash]; n; n = n->next) { |
| 1122 |
if (n->addr.family == af && |
| 1123 |
n->addr.bytelen == len && |
| 1124 |
memcmp(n->addr.data, addr, len) == 0) |
| 1125 |
return n->name; |
| 1126 |
} |
| 1127 |
n = malloc(sizeof(*n)); |
| 1128 |
if (n == NULL) |
| 1129 |
return NULL; |
| 1130 |
n->addr.family = af; |
| 1131 |
n->addr.bytelen = len; |
| 1132 |
n->name = NULL; |
| 1133 |
memcpy(n->addr.data, addr, len); |
| 1134 |
n->next = nht[hash]; |
| 1135 |
nht[hash] = n; |
| 1136 |
if (++notfirst == 1) |
| 1137 |
sethostent(1); |
| 1138 |
fflush(stdout); |
| 1139 |
|
| 1140 |
h_ent = gethostbyaddr(addr, len, af); |
| 1141 |
if (h_ent != NULL) |
| 1142 |
n->name = strdup(h_ent->h_name); |
| 1143 |
|
| 1144 |
/* Even if we fail, "negative" entry is remembered. */ |
| 1145 |
return n->name; |
| 1146 |
} |
| 1147 |
#endif |
| 1148 |
|
| 1149 |
const char *format_host_r(int af, int len, const void *addr, |
| 1150 |
char *buf, int buflen) |
| 1151 |
{ |
| 1152 |
#ifdef RESOLVE_HOSTNAMES |
| 1153 |
if (resolve_hosts) { |
| 1154 |
const char *n; |
| 1155 |
|
| 1156 |
len = len <= 0 ? af_byte_len(af) : len; |
| 1157 |
|
| 1158 |
if (len > 0 && |
| 1159 |
(n = resolve_address(addr, len, af)) != NULL) |
| 1160 |
return n; |
| 1161 |
} |
| 1162 |
#endif |
| 1163 |
return rt_addr_n2a_r(af, len, addr, buf, buflen); |
| 1164 |
} |
| 1165 |
|
| 1166 |
const char *format_host(int af, int len, const void *addr) |
| 1167 |
{ |
| 1168 |
static char buf[256]; |
| 1169 |
|
| 1170 |
return format_host_r(af, len, addr, buf, 256); |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen) |
| 1175 |
{ |
| 1176 |
char *ptr = buf; |
| 1177 |
int i; |
| 1178 |
|
| 1179 |
for (i = 0; i < len; i++) { |
| 1180 |
if (blen < 3) |
| 1181 |
break; |
| 1182 |
sprintf(ptr, "%02x", str[i]); |
| 1183 |
ptr += 2; |
| 1184 |
blen -= 2; |
| 1185 |
} |
| 1186 |
return buf; |
| 1187 |
} |
| 1188 |
|
| 1189 |
__u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len) |
| 1190 |
{ |
| 1191 |
unsigned int cnt = 0; |
| 1192 |
char *endptr; |
| 1193 |
|
| 1194 |
if (strlen(str) % 2) |
| 1195 |
return NULL; |
| 1196 |
while (cnt < blen && strlen(str) > 1) { |
| 1197 |
unsigned int tmp; |
| 1198 |
char tmpstr[3]; |
| 1199 |
|
| 1200 |
strncpy(tmpstr, str, 2); |
| 1201 |
tmpstr[2] = '\0'; |
| 1202 |
errno = 0; |
| 1203 |
tmp = strtoul(tmpstr, &endptr, 16); |
| 1204 |
if (errno != 0 || tmp > 0xFF || *endptr != '\0') |
| 1205 |
return NULL; |
| 1206 |
buf[cnt++] = tmp; |
| 1207 |
str += 2; |
| 1208 |
} |
| 1209 |
|
| 1210 |
if (len) |
| 1211 |
*len = cnt; |
| 1212 |
|
| 1213 |
return buf; |
| 1214 |
} |
| 1215 |
|
| 1216 |
int hex2mem(const char *buf, uint8_t *mem, int count) |
| 1217 |
{ |
| 1218 |
int i, j; |
| 1219 |
int c; |
| 1220 |
|
| 1221 |
for (i = 0, j = 0; i < count; i++, j += 2) { |
| 1222 |
c = get_hex(buf[j]); |
| 1223 |
if (c < 0) |
| 1224 |
return -1; |
| 1225 |
|
| 1226 |
mem[i] = c << 4; |
| 1227 |
|
| 1228 |
c = get_hex(buf[j + 1]); |
| 1229 |
if (c < 0) |
| 1230 |
return -1; |
| 1231 |
|
| 1232 |
mem[i] |= c; |
| 1233 |
} |
| 1234 |
|
| 1235 |
return 0; |
| 1236 |
} |
| 1237 |
|
| 1238 |
int addr64_n2a(__u64 addr, char *buff, size_t len) |
| 1239 |
{ |
| 1240 |
__u16 *words = (__u16 *)&addr; |
| 1241 |
__u16 v; |
| 1242 |
int i, ret; |
| 1243 |
size_t written = 0; |
| 1244 |
char *sep = ":"; |
| 1245 |
|
| 1246 |
for (i = 0; i < 4; i++) { |
| 1247 |
v = ntohs(words[i]); |
| 1248 |
|
| 1249 |
if (i == 3) |
| 1250 |
sep = ""; |
| 1251 |
|
| 1252 |
ret = snprintf(&buff[written], len - written, "%x%s", v, sep); |
| 1253 |
if (ret < 0) |
| 1254 |
return ret; |
| 1255 |
|
| 1256 |
written += ret; |
| 1257 |
} |
| 1258 |
|
| 1259 |
return written; |
| 1260 |
} |
| 1261 |
|
| 1262 |
/* Print buffer and escape bytes that are !isprint or among 'escape' */ |
| 1263 |
void print_escape_buf(const __u8 *buf, size_t len, const char *escape) |
| 1264 |
{ |
| 1265 |
size_t i; |
| 1266 |
|
| 1267 |
for (i = 0; i < len; ++i) { |
| 1268 |
if (isprint(buf[i]) && buf[i] != '\\' && |
| 1269 |
!strchr(escape, buf[i])) |
| 1270 |
printf("%c", buf[i]); |
| 1271 |
else |
| 1272 |
printf("\\%03o", buf[i]); |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
int print_timestamp(FILE *fp) |
| 1277 |
{ |
| 1278 |
struct timeval tv; |
| 1279 |
struct tm *tm; |
| 1280 |
|
| 1281 |
gettimeofday(&tv, NULL); |
| 1282 |
tm = localtime(&tv.tv_sec); |
| 1283 |
|
| 1284 |
if (timestamp_short) { |
| 1285 |
char tshort[40]; |
| 1286 |
|
| 1287 |
strftime(tshort, sizeof(tshort), "%Y-%m-%dT%H:%M:%S", tm); |
| 1288 |
fprintf(fp, "[%s.%06ld] ", tshort, tv.tv_usec); |
| 1289 |
} else { |
| 1290 |
char *tstr = asctime(tm); |
| 1291 |
|
| 1292 |
tstr[strlen(tstr)-1] = 0; |
| 1293 |
fprintf(fp, "Timestamp: %s %ld usec\n", |
| 1294 |
tstr, tv.tv_usec); |
| 1295 |
} |
| 1296 |
|
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
|
| 1300 |
unsigned int print_name_and_link(const char *fmt, |
| 1301 |
const char *name, struct rtattr *tb[]) |
| 1302 |
{ |
| 1303 |
const char *link = NULL; |
| 1304 |
unsigned int m_flag = 0; |
| 1305 |
SPRINT_BUF(b1); |
| 1306 |
|
| 1307 |
if (tb[IFLA_LINK]) { |
| 1308 |
int iflink = rta_getattr_u32(tb[IFLA_LINK]); |
| 1309 |
|
| 1310 |
if (iflink) { |
| 1311 |
if (tb[IFLA_LINK_NETNSID]) { |
| 1312 |
if (is_json_context()) { |
| 1313 |
print_int(PRINT_JSON, |
| 1314 |
"link_index", NULL, iflink); |
| 1315 |
} else { |
| 1316 |
link = ll_idx_n2a(iflink); |
| 1317 |
} |
| 1318 |
} else { |
| 1319 |
link = ll_index_to_name(iflink); |
| 1320 |
|
| 1321 |
if (is_json_context()) { |
| 1322 |
print_string(PRINT_JSON, |
| 1323 |
"link", NULL, link); |
| 1324 |
link = NULL; |
| 1325 |
} |
| 1326 |
|
| 1327 |
m_flag = ll_index_to_flags(iflink); |
| 1328 |
m_flag = !(m_flag & IFF_UP); |
| 1329 |
} |
| 1330 |
} else { |
| 1331 |
if (is_json_context()) |
| 1332 |
print_null(PRINT_JSON, "link", NULL, NULL); |
| 1333 |
else |
| 1334 |
link = "NONE"; |
| 1335 |
} |
| 1336 |
|
| 1337 |
if (link) { |
| 1338 |
snprintf(b1, sizeof(b1), "%s@%s", name, link); |
| 1339 |
name = b1; |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", fmt, name); |
| 1344 |
|
| 1345 |
return m_flag; |
| 1346 |
} |
| 1347 |
|
| 1348 |
int cmdlineno; |
| 1349 |
|
| 1350 |
/* Like glibc getline but handle continuation lines and comments */ |
| 1351 |
static ssize_t getcmdline(char **linep, size_t *lenp, FILE *in) |
| 1352 |
{ |
| 1353 |
ssize_t cc; |
| 1354 |
char *cp; |
| 1355 |
|
| 1356 |
cc = getline(linep, lenp, in); |
| 1357 |
if (cc < 0) |
| 1358 |
return cc; /* eof or error */ |
| 1359 |
++cmdlineno; |
| 1360 |
|
| 1361 |
cp = strchr(*linep, '#'); |
| 1362 |
if (cp) |
| 1363 |
*cp = '\0'; |
| 1364 |
|
| 1365 |
while ((cp = strstr(*linep, "\\\n")) != NULL) { |
| 1366 |
char *line1 = NULL; |
| 1367 |
size_t len1 = 0; |
| 1368 |
ssize_t cc1; |
| 1369 |
|
| 1370 |
cc1 = getline(&line1, &len1, in); |
| 1371 |
if (cc1 < 0) { |
| 1372 |
fprintf(stderr, "Missing continuation line\n"); |
| 1373 |
return cc1; |
| 1374 |
} |
| 1375 |
|
| 1376 |
++cmdlineno; |
| 1377 |
*cp = 0; |
| 1378 |
|
| 1379 |
cp = strchr(line1, '#'); |
| 1380 |
if (cp) |
| 1381 |
*cp = '\0'; |
| 1382 |
|
| 1383 |
*lenp = strlen(*linep) + strlen(line1) + 1; |
| 1384 |
*linep = realloc(*linep, *lenp); |
| 1385 |
if (!*linep) { |
| 1386 |
fprintf(stderr, "Out of memory\n"); |
| 1387 |
*lenp = 0; |
| 1388 |
return -1; |
| 1389 |
} |
| 1390 |
cc += cc1 - 2; |
| 1391 |
strcat(*linep, line1); |
| 1392 |
free(line1); |
| 1393 |
} |
| 1394 |
return cc; |
| 1395 |
} |
| 1396 |
|
| 1397 |
/* split command line into argument vector */ |
| 1398 |
static int makeargs(char *line, char *argv[], int maxargs) |
| 1399 |
{ |
| 1400 |
static const char ws[] = " \t\r\n"; |
| 1401 |
char *cp = line; |
| 1402 |
int argc = 0; |
| 1403 |
|
| 1404 |
while (*cp) { |
| 1405 |
/* skip leading whitespace */ |
| 1406 |
cp += strspn(cp, ws); |
| 1407 |
|
| 1408 |
if (*cp == '\0') |
| 1409 |
break; |
| 1410 |
|
| 1411 |
if (argc >= (maxargs - 1)) { |
| 1412 |
fprintf(stderr, "Too many arguments to command\n"); |
| 1413 |
exit(1); |
| 1414 |
} |
| 1415 |
|
| 1416 |
/* word begins with quote */ |
| 1417 |
if (*cp == '\'' || *cp == '"') { |
| 1418 |
char quote = *cp++; |
| 1419 |
|
| 1420 |
argv[argc++] = cp; |
| 1421 |
/* find ending quote */ |
| 1422 |
cp = strchr(cp, quote); |
| 1423 |
if (cp == NULL) { |
| 1424 |
fprintf(stderr, "Unterminated quoted string\n"); |
| 1425 |
exit(1); |
| 1426 |
} |
| 1427 |
} else { |
| 1428 |
argv[argc++] = cp; |
| 1429 |
|
| 1430 |
/* find end of word */ |
| 1431 |
cp += strcspn(cp, ws); |
| 1432 |
if (*cp == '\0') |
| 1433 |
break; |
| 1434 |
} |
| 1435 |
|
| 1436 |
/* separate words */ |
| 1437 |
*cp++ = 0; |
| 1438 |
} |
| 1439 |
argv[argc] = NULL; |
| 1440 |
|
| 1441 |
return argc; |
| 1442 |
} |
| 1443 |
|
| 1444 |
void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n) |
| 1445 |
{ |
| 1446 |
char *tstr; |
| 1447 |
time_t secs = ((__u32 *)NLMSG_DATA(n))[0]; |
| 1448 |
long usecs = ((__u32 *)NLMSG_DATA(n))[1]; |
| 1449 |
|
| 1450 |
tstr = asctime(localtime(&secs)); |
| 1451 |
tstr[strlen(tstr)-1] = 0; |
| 1452 |
fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs); |
| 1453 |
} |
| 1454 |
|
| 1455 |
char *int_to_str(int val, char *buf) |
| 1456 |
{ |
| 1457 |
sprintf(buf, "%d", val); |
| 1458 |
return buf; |
| 1459 |
} |
| 1460 |
|
| 1461 |
char *uint_to_str(unsigned int val, char *buf) |
| 1462 |
{ |
| 1463 |
sprintf(buf, "%u", val); |
| 1464 |
return buf; |
| 1465 |
} |
| 1466 |
|
| 1467 |
int get_guid(__u64 *guid, const char *arg) |
| 1468 |
{ |
| 1469 |
unsigned long tmp; |
| 1470 |
char *endptr; |
| 1471 |
int i; |
| 1472 |
|
| 1473 |
#define GUID_STR_LEN 23 |
| 1474 |
/* Verify strict format: format string must be |
| 1475 |
* xx:xx:xx:xx:xx:xx:xx:xx where xx can be an arbitrary |
| 1476 |
* hex digit |
| 1477 |
*/ |
| 1478 |
|
| 1479 |
if (strlen(arg) != GUID_STR_LEN) |
| 1480 |
return -1; |
| 1481 |
|
| 1482 |
/* make sure columns are in place */ |
| 1483 |
for (i = 0; i < 7; i++) |
| 1484 |
if (arg[2 + i * 3] != ':') |
| 1485 |
return -1; |
| 1486 |
|
| 1487 |
*guid = 0; |
| 1488 |
for (i = 0; i < 8; i++) { |
| 1489 |
tmp = strtoul(arg + i * 3, &endptr, 16); |
| 1490 |
if (endptr != arg + i * 3 + 2) |
| 1491 |
return -1; |
| 1492 |
|
| 1493 |
if (tmp > 255) |
| 1494 |
return -1; |
| 1495 |
|
| 1496 |
*guid |= tmp << (56 - 8 * i); |
| 1497 |
} |
| 1498 |
|
| 1499 |
return 0; |
| 1500 |
} |
| 1501 |
|
| 1502 |
/* This is a necessary workaround for multicast route dumps */ |
| 1503 |
int get_real_family(int rtm_type, int rtm_family) |
| 1504 |
{ |
| 1505 |
if (rtm_type != RTN_MULTICAST) |
| 1506 |
return rtm_family; |
| 1507 |
|
| 1508 |
if (rtm_family == RTNL_FAMILY_IPMR) |
| 1509 |
return AF_INET; |
| 1510 |
|
| 1511 |
if (rtm_family == RTNL_FAMILY_IP6MR) |
| 1512 |
return AF_INET6; |
| 1513 |
|
| 1514 |
return rtm_family; |
| 1515 |
} |
| 1516 |
|
| 1517 |
/* Based on copy_rtnl_link_stats() from kernel at net/core/rtnetlink.c */ |
| 1518 |
static void copy_rtnl_link_stats64(struct rtnl_link_stats64 *stats64, |
| 1519 |
const struct rtnl_link_stats *stats) |
| 1520 |
{ |
| 1521 |
__u64 *a = (__u64 *)stats64; |
| 1522 |
const __u32 *b = (const __u32 *)stats; |
| 1523 |
const __u32 *e = b + sizeof(*stats) / sizeof(*b); |
| 1524 |
|
| 1525 |
while (b < e) |
| 1526 |
*a++ = *b++; |
| 1527 |
} |
| 1528 |
|
| 1529 |
#define IPSTATS_MIB_MAX_LEN (__IPSTATS_MIB_MAX * sizeof(__u64)) |
| 1530 |
static void get_snmp_counters(struct rtnl_link_stats64 *stats64, |
| 1531 |
struct rtattr *s) |
| 1532 |
{ |
| 1533 |
__u64 *mib = (__u64 *)RTA_DATA(s); |
| 1534 |
|
| 1535 |
memset(stats64, 0, sizeof(*stats64)); |
| 1536 |
|
| 1537 |
stats64->rx_packets = mib[IPSTATS_MIB_INPKTS]; |
| 1538 |
stats64->rx_bytes = mib[IPSTATS_MIB_INOCTETS]; |
| 1539 |
stats64->tx_packets = mib[IPSTATS_MIB_OUTPKTS]; |
| 1540 |
stats64->tx_bytes = mib[IPSTATS_MIB_OUTOCTETS]; |
| 1541 |
stats64->rx_errors = mib[IPSTATS_MIB_INDISCARDS]; |
| 1542 |
stats64->tx_errors = mib[IPSTATS_MIB_OUTDISCARDS]; |
| 1543 |
stats64->multicast = mib[IPSTATS_MIB_INMCASTPKTS]; |
| 1544 |
stats64->rx_frame_errors = mib[IPSTATS_MIB_CSUMERRORS]; |
| 1545 |
} |
| 1546 |
|
| 1547 |
int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64, |
| 1548 |
struct rtattr *tb[]) |
| 1549 |
{ |
| 1550 |
struct rtnl_link_stats stats; |
| 1551 |
void *s; |
| 1552 |
struct rtattr *rta; |
| 1553 |
int size, len; |
| 1554 |
|
| 1555 |
if (tb[IFLA_STATS64]) { |
| 1556 |
rta = tb[IFLA_STATS64]; |
| 1557 |
size = sizeof(struct rtnl_link_stats64); |
| 1558 |
s = stats64; |
| 1559 |
} else if (tb[IFLA_STATS]) { |
| 1560 |
rta = tb[IFLA_STATS]; |
| 1561 |
size = sizeof(struct rtnl_link_stats); |
| 1562 |
s = &stats; |
| 1563 |
} else if (tb[IFLA_PROTINFO]) { |
| 1564 |
struct rtattr *ptb[IPSTATS_MIB_MAX_LEN + 1]; |
| 1565 |
|
| 1566 |
parse_rtattr_nested(ptb, IPSTATS_MIB_MAX_LEN, |
| 1567 |
tb[IFLA_PROTINFO]); |
| 1568 |
if (ptb[IFLA_INET6_STATS]) |
| 1569 |
get_snmp_counters(stats64, ptb[IFLA_INET6_STATS]); |
| 1570 |
return sizeof(*stats64); |
| 1571 |
} else { |
| 1572 |
return -1; |
| 1573 |
} |
| 1574 |
|
| 1575 |
len = RTA_PAYLOAD(rta); |
| 1576 |
if (len < size) |
| 1577 |
memset(s + len, 0, size - len); |
| 1578 |
else |
| 1579 |
len = size; |
| 1580 |
|
| 1581 |
memcpy(s, RTA_DATA(rta), len); |
| 1582 |
|
| 1583 |
if (s != stats64) |
| 1584 |
copy_rtnl_link_stats64(stats64, s); |
| 1585 |
return size; |
| 1586 |
} |
| 1587 |
|
| 1588 |
#ifdef NEED_STRLCPY |
| 1589 |
size_t strlcpy(char *dst, const char *src, size_t size) |
| 1590 |
{ |
| 1591 |
size_t srclen = strlen(src); |
| 1592 |
|
| 1593 |
if (size) { |
| 1594 |
size_t minlen = min(srclen, size - 1); |
| 1595 |
|
| 1596 |
memcpy(dst, src, minlen); |
| 1597 |
dst[minlen] = '\0'; |
| 1598 |
} |
| 1599 |
return srclen; |
| 1600 |
} |
| 1601 |
|
| 1602 |
size_t strlcat(char *dst, const char *src, size_t size) |
| 1603 |
{ |
| 1604 |
size_t dlen = strlen(dst); |
| 1605 |
|
| 1606 |
if (dlen >= size) |
| 1607 |
return dlen + strlen(src); |
| 1608 |
|
| 1609 |
return dlen + strlcpy(dst + dlen, src, size - dlen); |
| 1610 |
} |
| 1611 |
#endif |
| 1612 |
|
| 1613 |
void drop_cap(void) |
| 1614 |
{ |
| 1615 |
#ifdef HAVE_LIBCAP |
| 1616 |
/* don't hamstring root/sudo */ |
| 1617 |
if (getuid() != 0 && geteuid() != 0) { |
| 1618 |
cap_t capabilities; |
| 1619 |
cap_value_t net_admin = CAP_NET_ADMIN; |
| 1620 |
cap_flag_t inheritable = CAP_INHERITABLE; |
| 1621 |
cap_flag_value_t is_set; |
| 1622 |
|
| 1623 |
capabilities = cap_get_proc(); |
| 1624 |
if (!capabilities) |
| 1625 |
exit(EXIT_FAILURE); |
| 1626 |
if (cap_get_flag(capabilities, net_admin, inheritable, |
| 1627 |
&is_set) != 0) |
| 1628 |
exit(EXIT_FAILURE); |
| 1629 |
/* apps with ambient caps can fork and call ip */ |
| 1630 |
if (is_set == CAP_CLEAR) { |
| 1631 |
if (cap_clear(capabilities) != 0) |
| 1632 |
exit(EXIT_FAILURE); |
| 1633 |
if (cap_set_proc(capabilities) != 0) |
| 1634 |
exit(EXIT_FAILURE); |
| 1635 |
} |
| 1636 |
cap_free(capabilities); |
| 1637 |
} |
| 1638 |
#endif |
| 1639 |
} |
| 1640 |
|
| 1641 |
int get_time(unsigned int *time, const char *str) |
| 1642 |
{ |
| 1643 |
double t; |
| 1644 |
char *p; |
| 1645 |
|
| 1646 |
t = strtod(str, &p); |
| 1647 |
if (p == str) |
| 1648 |
return -1; |
| 1649 |
|
| 1650 |
if (*p) { |
| 1651 |
if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec") == 0 || |
| 1652 |
strcasecmp(p, "secs") == 0) |
| 1653 |
t *= TIME_UNITS_PER_SEC; |
| 1654 |
else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec") == 0 || |
| 1655 |
strcasecmp(p, "msecs") == 0) |
| 1656 |
t *= TIME_UNITS_PER_SEC/1000; |
| 1657 |
else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec") == 0 || |
| 1658 |
strcasecmp(p, "usecs") == 0) |
| 1659 |
t *= TIME_UNITS_PER_SEC/1000000; |
| 1660 |
else |
| 1661 |
return -1; |
| 1662 |
} |
| 1663 |
|
| 1664 |
*time = t; |
| 1665 |
return 0; |
| 1666 |
} |
| 1667 |
|
| 1668 |
static void print_time(char *buf, int len, __u32 time) |
| 1669 |
{ |
| 1670 |
double tmp = time; |
| 1671 |
|
| 1672 |
if (tmp >= TIME_UNITS_PER_SEC) |
| 1673 |
snprintf(buf, len, "%.3gs", tmp/TIME_UNITS_PER_SEC); |
| 1674 |
else if (tmp >= TIME_UNITS_PER_SEC/1000) |
| 1675 |
snprintf(buf, len, "%.3gms", tmp/(TIME_UNITS_PER_SEC/1000)); |
| 1676 |
else |
| 1677 |
snprintf(buf, len, "%uus", time); |
| 1678 |
} |
| 1679 |
|
| 1680 |
char *sprint_time(__u32 time, char *buf) |
| 1681 |
{ |
| 1682 |
print_time(buf, SPRINT_BSIZE-1, time); |
| 1683 |
return buf; |
| 1684 |
} |
| 1685 |
|
| 1686 |
/* 64 bit times are represented internally in nanoseconds */ |
| 1687 |
int get_time64(__s64 *time, const char *str) |
| 1688 |
{ |
| 1689 |
double nsec; |
| 1690 |
char *p; |
| 1691 |
|
| 1692 |
nsec = strtod(str, &p); |
| 1693 |
if (p == str) |
| 1694 |
return -1; |
| 1695 |
|
| 1696 |
if (*p) { |
| 1697 |
if (strcasecmp(p, "s") == 0 || |
| 1698 |
strcasecmp(p, "sec") == 0 || |
| 1699 |
strcasecmp(p, "secs") == 0) |
| 1700 |
nsec *= NSEC_PER_SEC; |
| 1701 |
else if (strcasecmp(p, "ms") == 0 || |
| 1702 |
strcasecmp(p, "msec") == 0 || |
| 1703 |
strcasecmp(p, "msecs") == 0) |
| 1704 |
nsec *= NSEC_PER_MSEC; |
| 1705 |
else if (strcasecmp(p, "us") == 0 || |
| 1706 |
strcasecmp(p, "usec") == 0 || |
| 1707 |
strcasecmp(p, "usecs") == 0) |
| 1708 |
nsec *= NSEC_PER_USEC; |
| 1709 |
else if (strcasecmp(p, "ns") == 0 || |
| 1710 |
strcasecmp(p, "nsec") == 0 || |
| 1711 |
strcasecmp(p, "nsecs") == 0) |
| 1712 |
nsec *= 1; |
| 1713 |
else |
| 1714 |
return -1; |
| 1715 |
} |
| 1716 |
|
| 1717 |
*time = nsec; |
| 1718 |
return 0; |
| 1719 |
} |
| 1720 |
|
| 1721 |
static void print_time64(char *buf, int len, __s64 time) |
| 1722 |
{ |
| 1723 |
double nsec = time; |
| 1724 |
|
| 1725 |
if (time >= NSEC_PER_SEC) |
| 1726 |
snprintf(buf, len, "%.3gs", nsec/NSEC_PER_SEC); |
| 1727 |
else if (time >= NSEC_PER_MSEC) |
| 1728 |
snprintf(buf, len, "%.3gms", nsec/NSEC_PER_MSEC); |
| 1729 |
else if (time >= NSEC_PER_USEC) |
| 1730 |
snprintf(buf, len, "%.3gus", nsec/NSEC_PER_USEC); |
| 1731 |
else |
| 1732 |
snprintf(buf, len, "%lldns", time); |
| 1733 |
} |
| 1734 |
|
| 1735 |
char *sprint_time64(__s64 time, char *buf) |
| 1736 |
{ |
| 1737 |
print_time64(buf, SPRINT_BSIZE-1, time); |
| 1738 |
return buf; |
| 1739 |
} |
| 1740 |
|
| 1741 |
int do_batch(const char *name, bool force, |
| 1742 |
int (*cmd)(int argc, char *argv[], void *data), void *data) |
| 1743 |
{ |
| 1744 |
char *line = NULL; |
| 1745 |
size_t len = 0; |
| 1746 |
int ret = EXIT_SUCCESS; |
| 1747 |
|
| 1748 |
if (name && strcmp(name, "-") != 0) { |
| 1749 |
if (freopen(name, "r", stdin) == NULL) { |
| 1750 |
fprintf(stderr, |
| 1751 |
"Cannot open file \"%s\" for reading: %s\n", |
| 1752 |
name, strerror(errno)); |
| 1753 |
return EXIT_FAILURE; |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
cmdlineno = 0; |
| 1758 |
while (getcmdline(&line, &len, stdin) != -1) { |
| 1759 |
char *largv[MAX_ARGS]; |
| 1760 |
int largc; |
| 1761 |
|
| 1762 |
largc = makeargs(line, largv, MAX_ARGS); |
| 1763 |
if (!largc) |
| 1764 |
continue; /* blank line */ |
| 1765 |
|
| 1766 |
if (cmd(largc, largv, data)) { |
| 1767 |
fprintf(stderr, "Command failed %s:%d\n", |
| 1768 |
name, cmdlineno); |
| 1769 |
ret = EXIT_FAILURE; |
| 1770 |
if (!force) |
| 1771 |
break; |
| 1772 |
} |
| 1773 |
} |
| 1774 |
|
| 1775 |
free(line); |
| 1776 |
|
| 1777 |
return ret; |
| 1778 |
} |
| 1779 |
|
| 1780 |
static int |
| 1781 |
__parse_one_of(const char *msg, const char *realval, |
| 1782 |
const char * const *list, size_t len, int *p_err, |
| 1783 |
int (*matcher)(const char *, const char *)) |
| 1784 |
{ |
| 1785 |
int i; |
| 1786 |
|
| 1787 |
for (i = 0; i < len; i++) { |
| 1788 |
if (list[i] && matcher(realval, list[i]) == 0) { |
| 1789 |
*p_err = 0; |
| 1790 |
return i; |
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
fprintf(stderr, "Error: argument of \"%s\" must be one of ", msg); |
| 1795 |
for (i = 0; i < len; i++) |
| 1796 |
if (list[i]) |
| 1797 |
fprintf(stderr, "\"%s\", ", list[i]); |
| 1798 |
fprintf(stderr, "not \"%s\"\n", realval); |
| 1799 |
*p_err = -EINVAL; |
| 1800 |
return 0; |
| 1801 |
} |
| 1802 |
|
| 1803 |
int parse_one_of(const char *msg, const char *realval, const char * const *list, |
| 1804 |
size_t len, int *p_err) |
| 1805 |
{ |
| 1806 |
return __parse_one_of(msg, realval, list, len, p_err, matches_warn); |
| 1807 |
} |
| 1808 |
|
| 1809 |
int parse_one_of_deprecated(const char *msg, const char *realval, |
| 1810 |
const char * const *list, |
| 1811 |
size_t len, int *p_err) |
| 1812 |
{ |
| 1813 |
return __parse_one_of(msg, realval, list, len, p_err, matches); |
| 1814 |
} |
| 1815 |
|
| 1816 |
bool parse_on_off(const char *msg, const char *realval, int *p_err) |
| 1817 |
{ |
| 1818 |
static const char * const values_on_off[] = { "off", "on" }; |
| 1819 |
|
| 1820 |
return __parse_one_of(msg, realval, values_on_off, |
| 1821 |
ARRAY_SIZE(values_on_off), p_err, strcmp); |
| 1822 |
} |
| 1823 |
|
| 1824 |
int str_to_bool(const char *str, bool *p_val) |
| 1825 |
{ |
| 1826 |
static const char * const values[] = { |
| 1827 |
"false", "true", |
| 1828 |
"0", "1", |
| 1829 |
"disable", "enable" |
| 1830 |
}; |
| 1831 |
int err, index; |
| 1832 |
|
| 1833 |
index = parse_one_of(NULL, str, values, ARRAY_SIZE(values), &err); |
| 1834 |
if (err) |
| 1835 |
return err; |
| 1836 |
|
| 1837 |
*p_val = index & 1; |
| 1838 |
return 0; |
| 1839 |
} |
| 1840 |
|
| 1841 |
int parse_mapping_gen(int *argcp, char ***argvp, |
| 1842 |
int (*key_cb)(__u32 *keyp, const char *key), |
| 1843 |
int (*mapping_cb)(__u32 key, char *value, void *data), |
| 1844 |
void *mapping_cb_data) |
| 1845 |
{ |
| 1846 |
int argc = *argcp; |
| 1847 |
char **argv = *argvp; |
| 1848 |
int ret = 0; |
| 1849 |
|
| 1850 |
while (argc > 0) { |
| 1851 |
char *colon = strchr(*argv, ':'); |
| 1852 |
__u32 key; |
| 1853 |
|
| 1854 |
if (!colon) |
| 1855 |
break; |
| 1856 |
*colon = '\0'; |
| 1857 |
|
| 1858 |
if (key_cb(&key, *argv)) { |
| 1859 |
ret = 1; |
| 1860 |
break; |
| 1861 |
} |
| 1862 |
if (mapping_cb(key, colon + 1, mapping_cb_data)) { |
| 1863 |
ret = 1; |
| 1864 |
break; |
| 1865 |
} |
| 1866 |
|
| 1867 |
argc--, argv++; |
| 1868 |
} |
| 1869 |
|
| 1870 |
*argcp = argc; |
| 1871 |
*argvp = argv; |
| 1872 |
return ret; |
| 1873 |
} |
| 1874 |
|
| 1875 |
static int parse_mapping_num(__u32 *keyp, const char *key) |
| 1876 |
{ |
| 1877 |
return get_u32(keyp, key, 0); |
| 1878 |
} |
| 1879 |
|
| 1880 |
int parse_mapping_num_all(__u32 *keyp, const char *key) |
| 1881 |
{ |
| 1882 |
if (matches(key, "all") == 0) { |
| 1883 |
*keyp = (__u32) -1; |
| 1884 |
return 0; |
| 1885 |
} |
| 1886 |
return parse_mapping_num(keyp, key); |
| 1887 |
} |
| 1888 |
|
| 1889 |
int parse_mapping(int *argcp, char ***argvp, bool allow_all, |
| 1890 |
int (*mapping_cb)(__u32 key, char *value, void *data), |
| 1891 |
void *mapping_cb_data) |
| 1892 |
{ |
| 1893 |
if (allow_all) |
| 1894 |
return parse_mapping_gen(argcp, argvp, parse_mapping_num_all, |
| 1895 |
mapping_cb, mapping_cb_data); |
| 1896 |
else |
| 1897 |
return parse_mapping_gen(argcp, argvp, parse_mapping_num, |
| 1898 |
mapping_cb, mapping_cb_data); |
| 1899 |
} |
| 1900 |
|
| 1901 |
int str_map_lookup_str(const struct str_num_map *map, const char *needle) |
| 1902 |
{ |
| 1903 |
if (!needle) |
| 1904 |
return -EINVAL; |
| 1905 |
|
| 1906 |
/* Process array which is NULL terminated by the string. */ |
| 1907 |
while (map && map->str) { |
| 1908 |
if (strcmp(map->str, needle) == 0) |
| 1909 |
return map->num; |
| 1910 |
|
| 1911 |
map++; |
| 1912 |
} |
| 1913 |
return -EINVAL; |
| 1914 |
} |
| 1915 |
|
| 1916 |
const char *str_map_lookup_uint(const struct str_num_map *map, unsigned int val) |
| 1917 |
{ |
| 1918 |
unsigned int num = val; |
| 1919 |
|
| 1920 |
while (map && map->str) { |
| 1921 |
if (num == map->num) |
| 1922 |
return map->str; |
| 1923 |
|
| 1924 |
map++; |
| 1925 |
} |
| 1926 |
return NULL; |
| 1927 |
} |
| 1928 |
|
| 1929 |
const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val) |
| 1930 |
{ |
| 1931 |
unsigned int num = val; |
| 1932 |
|
| 1933 |
while (map && map->str) { |
| 1934 |
if (num == map->num) |
| 1935 |
return map->str; |
| 1936 |
|
| 1937 |
map++; |
| 1938 |
} |
| 1939 |
return NULL; |
| 1940 |
} |
| 1941 |
|
| 1942 |
const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val) |
| 1943 |
{ |
| 1944 |
unsigned int num = val; |
| 1945 |
|
| 1946 |
while (map && map->str) { |
| 1947 |
if (num == map->num) |
| 1948 |
return map->str; |
| 1949 |
|
| 1950 |
map++; |
| 1951 |
} |
| 1952 |
return NULL; |
| 1953 |
} |
| 1954 |
|
| 1955 |
unsigned int get_str_char_count(const char *str, int match) |
| 1956 |
{ |
| 1957 |
unsigned int count = 0; |
| 1958 |
const char *pos = str; |
| 1959 |
|
| 1960 |
while ((pos = strchr(pos, match))) { |
| 1961 |
count++; |
| 1962 |
pos++; |
| 1963 |
} |
| 1964 |
return count; |
| 1965 |
} |
| 1966 |
|
| 1967 |
int str_split_by_char(char *str, char **before, char **after, int match) |
| 1968 |
{ |
| 1969 |
char *slash; |
| 1970 |
|
| 1971 |
slash = strrchr(str, match); |
| 1972 |
if (!slash) |
| 1973 |
return -EINVAL; |
| 1974 |
*slash = '\0'; |
| 1975 |
*before = str; |
| 1976 |
*after = slash + 1; |
| 1977 |
return 0; |
| 1978 |
} |
| 1979 |
|
| 1980 |
struct indent_mem *alloc_indent_mem(void) |
| 1981 |
{ |
| 1982 |
struct indent_mem *mem = malloc(sizeof(*mem)); |
| 1983 |
|
| 1984 |
if (!mem) |
| 1985 |
return NULL; |
| 1986 |
strcpy(mem->indent_str, ""); |
| 1987 |
mem->indent_level = 0; |
| 1988 |
return mem; |
| 1989 |
} |
| 1990 |
|
| 1991 |
void free_indent_mem(struct indent_mem *mem) |
| 1992 |
{ |
| 1993 |
free(mem); |
| 1994 |
} |
| 1995 |
|
| 1996 |
#define INDENT_STR_STEP 2 |
| 1997 |
|
| 1998 |
void inc_indent(struct indent_mem *mem) |
| 1999 |
{ |
| 2000 |
if (mem->indent_level + INDENT_STR_STEP > INDENT_STR_MAXLEN) |
| 2001 |
return; |
| 2002 |
mem->indent_level += INDENT_STR_STEP; |
| 2003 |
memset(mem->indent_str, ' ', sizeof(mem->indent_str)); |
| 2004 |
mem->indent_str[mem->indent_level] = '\0'; |
| 2005 |
} |
| 2006 |
|
| 2007 |
void dec_indent(struct indent_mem *mem) |
| 2008 |
{ |
| 2009 |
if (mem->indent_level - INDENT_STR_STEP < 0) |
| 2010 |
return; |
| 2011 |
mem->indent_level -= INDENT_STR_STEP; |
| 2012 |
mem->indent_str[mem->indent_level] = '\0'; |
| 2013 |
} |
| 2014 |
|
| 2015 |
void print_indent(struct indent_mem *mem) |
| 2016 |
{ |
| 2017 |
if (mem->indent_level) |
| 2018 |
printf("%s", mem->indent_str); |
| 2019 |
} |
| 2020 |
|
| 2021 |
const char *proto_n2a(unsigned short id, char *buf, int len, |
| 2022 |
const struct proto *proto_tb, size_t tb_len) |
| 2023 |
{ |
| 2024 |
int i; |
| 2025 |
|
| 2026 |
id = ntohs(id); |
| 2027 |
|
| 2028 |
for (i = 0; !numeric && i < tb_len; i++) { |
| 2029 |
if (proto_tb[i].id == id) |
| 2030 |
return proto_tb[i].name; |
| 2031 |
} |
| 2032 |
|
| 2033 |
snprintf(buf, len, "[%d]", id); |
| 2034 |
|
| 2035 |
return buf; |
| 2036 |
} |
| 2037 |
|
| 2038 |
int proto_a2n(unsigned short *id, const char *buf, |
| 2039 |
const struct proto *proto_tb, size_t tb_len) |
| 2040 |
{ |
| 2041 |
int i; |
| 2042 |
|
| 2043 |
for (i = 0; i < tb_len; i++) { |
| 2044 |
if (strcasecmp(proto_tb[i].name, buf) == 0) { |
| 2045 |
*id = htons(proto_tb[i].id); |
| 2046 |
return 0; |
| 2047 |
} |
| 2048 |
} |
| 2049 |
if (get_be16(id, buf, 0)) |
| 2050 |
return -1; |
| 2051 |
|
| 2052 |
return 0; |
| 2053 |
} |
| 2054 |
|
| 2055 |
FILE *generic_proc_open(const char *env, const char *name) |
| 2056 |
{ |
| 2057 |
const char *p = getenv(env); |
| 2058 |
char store[128]; |
| 2059 |
|
| 2060 |
if (!p) { |
| 2061 |
p = getenv("PROC_ROOT") ? : "/proc"; |
| 2062 |
snprintf(store, sizeof(store) - 1, "%s/%s", p, name); |
| 2063 |
p = store; |
| 2064 |
} |
| 2065 |
|
| 2066 |
return fopen(p, "r"); |
| 2067 |
} |
| 2068 |
|
| 2069 |
void print_num(FILE *fp, unsigned int width, uint64_t count) |
| 2070 |
{ |
| 2071 |
const char *prefix = "kMGTPE"; |
| 2072 |
const unsigned int base = use_iec ? 1024 : 1000; |
| 2073 |
uint64_t powi = 1; |
| 2074 |
uint16_t powj = 1; |
| 2075 |
uint8_t precision = 2; |
| 2076 |
char buf[64]; |
| 2077 |
|
| 2078 |
if (!human_readable || count < base) { |
| 2079 |
fprintf(fp, "%*"PRIu64" ", width, count); |
| 2080 |
return; |
| 2081 |
} |
| 2082 |
|
| 2083 |
/* increase value by a factor of 1000/1024 and print |
| 2084 |
* if result is something a human can read |
| 2085 |
*/ |
| 2086 |
for (;;) { |
| 2087 |
powi *= base; |
| 2088 |
if (count / base < powi) |
| 2089 |
break; |
| 2090 |
|
| 2091 |
if (!prefix[1]) |
| 2092 |
break; |
| 2093 |
++prefix; |
| 2094 |
} |
| 2095 |
|
| 2096 |
/* try to guess a good number of digits for precision */ |
| 2097 |
for (; precision > 0; precision--) { |
| 2098 |
powj *= 10; |
| 2099 |
if (count / powi < powj) |
| 2100 |
break; |
| 2101 |
} |
| 2102 |
|
| 2103 |
snprintf(buf, sizeof(buf), "%.*f%c%s", precision, |
| 2104 |
(double) count / powi, *prefix, use_iec ? "i" : ""); |
| 2105 |
|
| 2106 |
fprintf(fp, "%*s ", width, buf); |
| 2107 |
} |
| 2108 |
|
| 2109 |
int open_fds_add(int fd) |
| 2110 |
{ |
| 2111 |
if (open_fds_cnt >= ARRAY_SIZE(open_fds)) |
| 2112 |
return -1; |
| 2113 |
|
| 2114 |
open_fds[open_fds_cnt++] = fd; |
| 2115 |
return 0; |
| 2116 |
} |
| 2117 |
|
| 2118 |
void open_fds_close(void) |
| 2119 |
{ |
| 2120 |
int i; |
| 2121 |
|
| 2122 |
for (i = 0; i < open_fds_cnt; i++) |
| 2123 |
close(open_fds[i]); |
| 2124 |
|
| 2125 |
open_fds_cnt = 0; |
| 2126 |
} |