aboutsummaryrefslogtreecommitdiff
path: root/networking/interface.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-03-05 16:56:16 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-03-05 16:56:16 +0100
commit8a5299fcfd54ae3b895b66249d6d105e956192cb (patch)
tree28e0588bb6018d420168b7195fc4fbc40e0b8a09 /networking/interface.c
parent804ce5a6fed63a2da6268a2d06f1ee2075435297 (diff)
downloadbusybox-w32-8a5299fcfd54ae3b895b66249d6d105e956192cb.tar.gz
busybox-w32-8a5299fcfd54ae3b895b66249d6d105e956192cb.tar.bz2
busybox-w32-8a5299fcfd54ae3b895b66249d6d105e956192cb.zip
networking/interface.c: code shrink
function old new delta ife_print 1293 1296 +3 display_interfaces 145 146 +1 if_readconf 162 141 -21 if_readlist_proc 631 560 -71 do_if_fetch 753 643 -110 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/3 up/down: 4/-202) Total: -198 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/interface.c')
-rw-r--r--networking/interface.c137
1 files changed, 76 insertions, 61 deletions
diff --git a/networking/interface.c b/networking/interface.c
index 89427f2f4..0bbef9879 100644
--- a/networking/interface.c
+++ b/networking/interface.c
@@ -318,20 +318,27 @@ struct interface {
318 char name[IFNAMSIZ]; /* interface name */ 318 char name[IFNAMSIZ]; /* interface name */
319 short type; /* if type */ 319 short type; /* if type */
320 short flags; /* various flags */ 320 short flags; /* various flags */
321 int tx_queue_len; /* transmit queue length */
322
323 /* these should be contiguous, zeroed in one go in if_fetch(): */
324#define FIRST_TO_ZERO metric
321 int metric; /* routing metric */ 325 int metric; /* routing metric */
322 int mtu; /* MTU value */ 326 int mtu; /* MTU value */
323 int tx_queue_len; /* transmit queue length */
324 struct ifmap map; /* hardware setup */ 327 struct ifmap map; /* hardware setup */
325 struct sockaddr addr; /* IP address */ 328 struct sockaddr addr; /* IP address */
326 struct sockaddr dstaddr; /* P-P IP address */ 329 struct sockaddr dstaddr; /* P-P IP address */
327 struct sockaddr broadaddr; /* IP broadcast address */ 330 struct sockaddr broadaddr; /* IP broadcast address */
328 struct sockaddr netmask; /* IP network mask */ 331 struct sockaddr netmask; /* IP network mask */
329 int has_ip;
330 char hwaddr[32]; /* HW address */ 332 char hwaddr[32]; /* HW address */
331 int statistics_valid; 333#define LAST_TO_ZERO hwaddr
334
335 smallint has_ip;
336 smallint statistics_valid;
332 struct user_net_device_stats stats; /* statistics */ 337 struct user_net_device_stats stats; /* statistics */
338#if 0 /* UNUSED */
333 int keepalive; /* keepalive value for SLIP */ 339 int keepalive; /* keepalive value for SLIP */
334 int outfill; /* outfill value for SLIP */ 340 int outfill; /* outfill value for SLIP */
341#endif
335}; 342};
336 343
337 344
@@ -342,7 +349,7 @@ static struct interface *int_list, *int_last;
342 349
343#if 0 350#if 0
344/* like strcmp(), but knows about numbers */ 351/* like strcmp(), but knows about numbers */
345except that the freshly added calls to xatoul() brf on ethernet aliases with 352except that the freshly added calls to xatoul() barf on ethernet aliases with
346uClibc with e.g.: ife->name='lo' name='eth0:1' 353uClibc with e.g.: ife->name='lo' name='eth0:1'
347static int nstrcmp(const char *a, const char *b) 354static int nstrcmp(const char *a, const char *b)
348{ 355{
@@ -394,32 +401,31 @@ static struct interface *add_interface(char *name)
394 return new; 401 return new;
395} 402}
396 403
397static char *get_name(char *name, char *p) 404static char *get_name(char name[IFNAMSIZ], char *p)
398{ 405{
399 /* Extract <name> from nul-terminated p where p matches 406 /* Extract NAME from nul-terminated p of the form "<whitespace>NAME:"
400 * <name>: after leading whitespace. 407 * If match is not made, set NAME to "" and return unchanged p.
401 * If match is not made, set name empty and return unchanged p
402 */ 408 */
403 char *nameend; 409 char *nameend;
404 char *namestart = skip_whitespace(p); 410 char *namestart;
405 411
406 nameend = namestart; 412 nameend = namestart = skip_whitespace(p);
407 while (*nameend && *nameend != ':' && !isspace(*nameend)) 413
408 nameend++; 414 for (;;) {
409 if (*nameend == ':') { 415 if ((nameend - namestart) >= IFNAMSIZ)
410 if ((nameend - namestart) < IFNAMSIZ) { 416 break; /* interface name too large - return "" */
417 if (*nameend == ':') {
411 memcpy(name, namestart, nameend - namestart); 418 memcpy(name, namestart, nameend - namestart);
412 name[nameend - namestart] = '\0'; 419 name[nameend - namestart] = '\0';
413 p = nameend; 420 return nameend + 1;
414 } else {
415 /* Interface name too large */
416 name[0] = '\0';
417 } 421 }
418 } else { 422 nameend++;
419 /* trailing ':' not found - return empty */ 423 /* isspace, NUL, any control char? */
420 name[0] = '\0'; 424 if ((unsigned char)*nameend <= (unsigned char)' ')
425 break; /* trailing ':' not found - return "" */
421 } 426 }
422 return p + 1; 427 name[0] = '\0';
428 return p;
423} 429}
424 430
425/* If scanf supports size qualifiers for %n conversions, then we can 431/* If scanf supports size qualifiers for %n conversions, then we can
@@ -435,7 +441,10 @@ static char *get_name(char *name, char *p)
435/* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */ 441/* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */
436/* }; */ 442/* }; */
437 443
438 /* Lie about the size of the int pointed to for %n. */ 444/* We use %n for unavailable data in older versions of /proc/net/dev formats.
445 * This results in bogus stores to ife->FOO members corresponding to
446 * %n specifiers (even the size of integers may not match).
447 */
439#if INT_MAX == LONG_MAX 448#if INT_MAX == LONG_MAX
440static const char *const ss_fmt[] = { 449static const char *const ss_fmt[] = {
441 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u", 450 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
@@ -448,7 +457,6 @@ static const char *const ss_fmt[] = {
448 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu", 457 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
449 "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" 458 "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu"
450}; 459};
451
452#endif 460#endif
453 461
454static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn) 462static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
@@ -456,22 +464,22 @@ static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
456 memset(&ife->stats, 0, sizeof(struct user_net_device_stats)); 464 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
457 465
458 sscanf(bp, ss_fmt[procnetdev_vsn], 466 sscanf(bp, ss_fmt[procnetdev_vsn],
459 &ife->stats.rx_bytes, /* missing for 0 */ 467 &ife->stats.rx_bytes, /* missing for v0 */
460 &ife->stats.rx_packets, 468 &ife->stats.rx_packets,
461 &ife->stats.rx_errors, 469 &ife->stats.rx_errors,
462 &ife->stats.rx_dropped, 470 &ife->stats.rx_dropped,
463 &ife->stats.rx_fifo_errors, 471 &ife->stats.rx_fifo_errors,
464 &ife->stats.rx_frame_errors, 472 &ife->stats.rx_frame_errors,
465 &ife->stats.rx_compressed, /* missing for <= 1 */ 473 &ife->stats.rx_compressed, /* missing for v0, v1 */
466 &ife->stats.rx_multicast, /* missing for <= 1 */ 474 &ife->stats.rx_multicast, /* missing for v0, v1 */
467 &ife->stats.tx_bytes, /* missing for 0 */ 475 &ife->stats.tx_bytes, /* missing for v0 */
468 &ife->stats.tx_packets, 476 &ife->stats.tx_packets,
469 &ife->stats.tx_errors, 477 &ife->stats.tx_errors,
470 &ife->stats.tx_dropped, 478 &ife->stats.tx_dropped,
471 &ife->stats.tx_fifo_errors, 479 &ife->stats.tx_fifo_errors,
472 &ife->stats.collisions, 480 &ife->stats.collisions,
473 &ife->stats.tx_carrier_errors, 481 &ife->stats.tx_carrier_errors,
474 &ife->stats.tx_compressed /* missing for <= 1 */ 482 &ife->stats.tx_compressed /* missing for v0, v1 */
475 ); 483 );
476 484
477 if (procnetdev_vsn <= 1) { 485 if (procnetdev_vsn <= 1) {
@@ -499,24 +507,21 @@ static int if_readconf(void)
499 int numreqs = 30; 507 int numreqs = 30;
500 struct ifconf ifc; 508 struct ifconf ifc;
501 struct ifreq *ifr; 509 struct ifreq *ifr;
502 int n, err = -1; 510 int n, err;
503 int skfd; 511 int skfd;
504 512
505 ifc.ifc_buf = NULL; 513 ifc.ifc_buf = NULL;
506 514
507 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets 515 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
508 (as of 2.1.128) */ 516 (as of 2.1.128) */
509 skfd = socket(AF_INET, SOCK_DGRAM, 0); 517 skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
510 if (skfd < 0) {
511 bb_perror_msg("error: no inet socket available");
512 return -1;
513 }
514 518
515 for (;;) { 519 for (;;) {
516 ifc.ifc_len = sizeof(struct ifreq) * numreqs; 520 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
517 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len); 521 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
518 522
519 if (ioctl_or_warn(skfd, SIOCGIFCONF, &ifc) < 0) { 523 err = ioctl_or_warn(skfd, SIOCGIFCONF, &ifc);
524 if (err < 0) {
520 goto out; 525 goto out;
521 } 526 }
522 if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) { 527 if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) {
@@ -565,7 +570,7 @@ static int if_readlist_proc(char *target)
565 570
566 err = 0; 571 err = 0;
567 while (fgets(buf, sizeof buf, fh)) { 572 while (fgets(buf, sizeof buf, fh)) {
568 char *s, name[128]; 573 char *s, name[IFNAMSIZ];
569 574
570 s = get_name(name, buf); 575 s = get_name(name, buf);
571 ife = add_interface(name); 576 ife = add_interface(name);
@@ -574,11 +579,15 @@ static int if_readlist_proc(char *target)
574 if (target && strcmp(target, name) == 0) 579 if (target && strcmp(target, name) == 0)
575 break; 580 break;
576 } 581 }
582
583#if 0 /* we trust kernel to not be buggy, /proc/net/dev reads never fail */
577 if (ferror(fh)) { 584 if (ferror(fh)) {
578 bb_perror_msg(_PATH_PROCNET_DEV); 585 bb_perror_msg(_PATH_PROCNET_DEV);
579 err = -1; 586 err = -1;
580 proc_read = 0; 587 proc_read = 0;
581 } 588 }
589#endif
590
582 fclose(fh); 591 fclose(fh);
583 return err; 592 return err;
584} 593}
@@ -608,24 +617,29 @@ static int if_fetch(struct interface *ife)
608 } 617 }
609 ife->flags = ifr.ifr_flags; 618 ife->flags = ifr.ifr_flags;
610 619
620 /* set up default values if ioctl's would fail */
621 ife->tx_queue_len = -1; /* unknown value */
622 memset(&ife->FIRST_TO_ZERO, 0,
623 offsetof(struct interface, LAST_TO_ZERO)
624 - offsetof(struct interface, FIRST_TO_ZERO)
625 + sizeof(ife->LAST_TO_ZERO)
626 );
627
611 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 628 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
612 memset(ife->hwaddr, 0, 32);
613 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0) 629 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
614 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8); 630 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
615 631
632//er.... why this _isnt_ inside if()?
616 ife->type = ifr.ifr_hwaddr.sa_family; 633 ife->type = ifr.ifr_hwaddr.sa_family;
617 634
618 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 635 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
619 ife->metric = 0;
620 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0) 636 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
621 ife->metric = ifr.ifr_metric; 637 ife->metric = ifr.ifr_metric;
622 638
623 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 639 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
624 ife->mtu = 0;
625 if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0) 640 if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
626 ife->mtu = ifr.ifr_mtu; 641 ife->mtu = ifr.ifr_mtu;
627 642
628 memset(&ife->map, 0, sizeof(struct ifmap));
629#ifdef SIOCGIFMAP 643#ifdef SIOCGIFMAP
630 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 644 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
631 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0) 645 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
@@ -634,31 +648,24 @@ static int if_fetch(struct interface *ife)
634 648
635#ifdef HAVE_TXQUEUELEN 649#ifdef HAVE_TXQUEUELEN
636 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 650 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
637 ife->tx_queue_len = -1; /* unknown value */
638 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0) 651 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
639 ife->tx_queue_len = ifr.ifr_qlen; 652 ife->tx_queue_len = ifr.ifr_qlen;
640#else
641 ife->tx_queue_len = -1; /* unknown value */
642#endif 653#endif
643 654
644 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 655 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
645 ifr.ifr_addr.sa_family = AF_INET; 656 ifr.ifr_addr.sa_family = AF_INET;
646 memset(&ife->addr, 0, sizeof(struct sockaddr));
647 if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) { 657 if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
648 ife->has_ip = 1; 658 ife->has_ip = 1;
649 ife->addr = ifr.ifr_addr; 659 ife->addr = ifr.ifr_addr;
650 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 660 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
651 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
652 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0) 661 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
653 ife->dstaddr = ifr.ifr_dstaddr; 662 ife->dstaddr = ifr.ifr_dstaddr;
654 663
655 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 664 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
656 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
657 if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0) 665 if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
658 ife->broadaddr = ifr.ifr_broadaddr; 666 ife->broadaddr = ifr.ifr_broadaddr;
659 667
660 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 668 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
661 memset(&ife->netmask, 0, sizeof(struct sockaddr));
662 if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0) 669 if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
663 ife->netmask = ifr.ifr_netmask; 670 ife->netmask = ifr.ifr_netmask;
664 } 671 }
@@ -1020,10 +1027,12 @@ static void ife_print(struct interface *ptr)
1020 1027
1021 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */ 1028 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1022 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1); 1029 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
1030#if 0
1023#ifdef SIOCSKEEPALIVE 1031#ifdef SIOCSKEEPALIVE
1024 if (ptr->outfill || ptr->keepalive) 1032 if (ptr->outfill || ptr->keepalive)
1025 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive); 1033 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
1026#endif 1034#endif
1035#endif
1027 bb_putchar('\n'); 1036 bb_putchar('\n');
1028 1037
1029 /* If needed, display the interface statistics. */ 1038 /* If needed, display the interface statistics. */
@@ -1105,8 +1114,11 @@ static int for_all_interfaces(int (*doit) (struct interface *, void *),
1105{ 1114{
1106 struct interface *ife; 1115 struct interface *ife;
1107 1116
1108 if (!int_list && (if_readlist() < 0)) 1117 if (!int_list) {
1109 return -1; 1118 int err = if_readlist();
1119 if (err < 0)
1120 return err;
1121 }
1110 for (ife = int_list; ife; ife = ife->next) { 1122 for (ife = int_list; ife; ife = ife->next) {
1111 int err = doit(ife, cookie); 1123 int err = doit(ife, cookie);
1112 if (err) 1124 if (err)
@@ -1124,8 +1136,11 @@ static int if_print(char *ifname)
1124 1136
1125 if (!ifname) { 1137 if (!ifname) {
1126 /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/ 1138 /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/
1127 if (!int_list && (if_readlist() < 0)) 1139 if (!int_list) {
1128 return -1; 1140 int err = if_readlist();
1141 if (err < 0)
1142 return err;
1143 }
1129 for (ife = int_list; ife; ife = ife->next) { 1144 for (ife = int_list; ife; ife = ife->next) {
1130 int err = do_if_print(ife); /*, &interface_opt_a);*/ 1145 int err = do_if_print(ife); /*, &interface_opt_a);*/
1131 if (err) 1146 if (err)
@@ -1140,6 +1155,15 @@ static int if_print(char *ifname)
1140 return res; 1155 return res;
1141} 1156}
1142 1157
1158int FAST_FUNC display_interfaces(char *ifname)
1159{
1160 int status;
1161
1162 status = if_print(ifname);
1163
1164 return (status < 0); /* status < 0 == 1 -- error */
1165}
1166
1143#if ENABLE_FEATURE_HWIB 1167#if ENABLE_FEATURE_HWIB
1144/* Input an Infiniband address and convert to binary. */ 1168/* Input an Infiniband address and convert to binary. */
1145int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap) 1169int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
@@ -1153,12 +1177,3 @@ int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
1153 return 0; 1177 return 0;
1154} 1178}
1155#endif 1179#endif
1156
1157int FAST_FUNC display_interfaces(char *ifname)
1158{
1159 int status;
1160
1161 status = if_print(ifname);
1162
1163 return (status < 0); /* status < 0 == 1 -- error */
1164}