diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-15 19:46:43 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-15 19:46:43 +0000 |
| commit | 1cc70225e7bfee447543ea38b08c7caeb6492c8b (patch) | |
| tree | a783bb5976def93f723a75c930671e2b31c1f1e7 /networking/interface.c | |
| parent | 54d14ca1a2aa965d13055719e233032193a4daf2 (diff) | |
| download | busybox-w32-1cc70225e7bfee447543ea38b08c7caeb6492c8b.tar.gz busybox-w32-1cc70225e7bfee447543ea38b08c7caeb6492c8b.tar.bz2 busybox-w32-1cc70225e7bfee447543ea38b08c7caeb6492c8b.zip | |
networking/interface.c: was doing really strange caching of fd's
open for getting ifconfig data - ??! Simplified all that. -200 bytes.
Diffstat (limited to 'networking/interface.c')
| -rw-r--r-- | networking/interface.c | 311 |
1 files changed, 112 insertions, 199 deletions
diff --git a/networking/interface.c b/networking/interface.c index 262b97879..2c5f3284e 100644 --- a/networking/interface.c +++ b/networking/interface.c | |||
| @@ -36,7 +36,7 @@ | |||
| 36 | #include "inet_common.h" | 36 | #include "inet_common.h" |
| 37 | #include "busybox.h" | 37 | #include "busybox.h" |
| 38 | 38 | ||
| 39 | #ifdef CONFIG_FEATURE_IPV6 | 39 | #if ENABLE_FEATURE_IPV6 |
| 40 | # define HAVE_AFINET6 1 | 40 | # define HAVE_AFINET6 1 |
| 41 | #else | 41 | #else |
| 42 | # undef HAVE_AFINET6 | 42 | # undef HAVE_AFINET6 |
| @@ -154,14 +154,13 @@ static int INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap) | |||
| 154 | */ | 154 | */ |
| 155 | } | 155 | } |
| 156 | 156 | ||
| 157 | static struct aftype inet_aftype = { | 157 | static const struct aftype inet_aftype = { |
| 158 | .name = "inet", | 158 | .name = "inet", |
| 159 | .title = "DARPA Internet", | 159 | .title = "DARPA Internet", |
| 160 | .af = AF_INET, | 160 | .af = AF_INET, |
| 161 | .alen = 4, | 161 | .alen = 4, |
| 162 | .sprint = INET_sprint, | 162 | .sprint = INET_sprint, |
| 163 | .input = INET_input, | 163 | .input = INET_input, |
| 164 | .fd = -1 | ||
| 165 | }; | 164 | }; |
| 166 | 165 | ||
| 167 | #ifdef HAVE_AFINET6 | 166 | #ifdef HAVE_AFINET6 |
| @@ -208,14 +207,13 @@ static int INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap) | |||
| 208 | */ | 207 | */ |
| 209 | } | 208 | } |
| 210 | 209 | ||
| 211 | static struct aftype inet6_aftype = { | 210 | static const struct aftype inet6_aftype = { |
| 212 | .name = "inet6", | 211 | .name = "inet6", |
| 213 | .title = "IPv6", | 212 | .title = "IPv6", |
| 214 | .af = AF_INET6, | 213 | .af = AF_INET6, |
| 215 | .alen = sizeof(struct in6_addr), | 214 | .alen = sizeof(struct in6_addr), |
| 216 | .sprint = INET6_sprint, | 215 | .sprint = INET6_sprint, |
| 217 | .input = INET6_input, | 216 | .input = INET6_input, |
| 218 | .fd = -1 | ||
| 219 | }; | 217 | }; |
| 220 | 218 | ||
| 221 | #endif /* HAVE_AFINET6 */ | 219 | #endif /* HAVE_AFINET6 */ |
| @@ -246,13 +244,16 @@ static const char *UNSPEC_sprint(struct sockaddr *sap, int numeric) | |||
| 246 | return UNSPEC_print((unsigned char *)sap->sa_data); | 244 | return UNSPEC_print((unsigned char *)sap->sa_data); |
| 247 | } | 245 | } |
| 248 | 246 | ||
| 249 | static struct aftype unspec_aftype = { | 247 | static const struct aftype unspec_aftype = { |
| 250 | "unspec", "UNSPEC", AF_UNSPEC, 0, | 248 | .name = "unspec", |
| 251 | UNSPEC_print, UNSPEC_sprint, NULL, NULL, | 249 | .title = "UNSPEC", |
| 252 | NULL, | 250 | .af = AF_UNSPEC, |
| 251 | .alen = 0, | ||
| 252 | .print = UNSPEC_print, | ||
| 253 | .sprint = UNSPEC_sprint, | ||
| 253 | }; | 254 | }; |
| 254 | 255 | ||
| 255 | static struct aftype * const aftypes[] = { | 256 | static const struct aftype *const aftypes[] = { |
| 256 | &inet_aftype, | 257 | &inet_aftype, |
| 257 | #ifdef HAVE_AFINET6 | 258 | #ifdef HAVE_AFINET6 |
| 258 | &inet6_aftype, | 259 | &inet6_aftype, |
| @@ -262,9 +263,9 @@ static struct aftype * const aftypes[] = { | |||
| 262 | }; | 263 | }; |
| 263 | 264 | ||
| 264 | /* Check our protocol family table for this family. */ | 265 | /* Check our protocol family table for this family. */ |
| 265 | struct aftype *get_aftype(const char *name) | 266 | const struct aftype *get_aftype(const char *name) |
| 266 | { | 267 | { |
| 267 | struct aftype * const *afp; | 268 | const struct aftype *const *afp; |
| 268 | 269 | ||
| 269 | afp = aftypes; | 270 | afp = aftypes; |
| 270 | while (*afp != NULL) { | 271 | while (*afp != NULL) { |
| @@ -276,9 +277,9 @@ struct aftype *get_aftype(const char *name) | |||
| 276 | } | 277 | } |
| 277 | 278 | ||
| 278 | /* Check our protocol family table for this family. */ | 279 | /* Check our protocol family table for this family. */ |
| 279 | static struct aftype *get_afntype(int af) | 280 | static const struct aftype *get_afntype(int af) |
| 280 | { | 281 | { |
| 281 | struct aftype * const *afp; | 282 | const struct aftype *const *afp; |
| 282 | 283 | ||
| 283 | afp = aftypes; | 284 | afp = aftypes; |
| 284 | while (*afp != NULL) { | 285 | while (*afp != NULL) { |
| @@ -289,20 +290,6 @@ static struct aftype *get_afntype(int af) | |||
| 289 | return NULL; | 290 | return NULL; |
| 290 | } | 291 | } |
| 291 | 292 | ||
| 292 | /* Check our protocol family table for this family and return its socket */ | ||
| 293 | static int get_socket_for_af(int af) | ||
| 294 | { | ||
| 295 | struct aftype * const *afp; | ||
| 296 | |||
| 297 | afp = aftypes; | ||
| 298 | while (*afp != NULL) { | ||
| 299 | if ((*afp)->af == af) | ||
| 300 | return (*afp)->fd; | ||
| 301 | afp++; | ||
| 302 | } | ||
| 303 | return -1; | ||
| 304 | } | ||
| 305 | |||
| 306 | struct user_net_device_stats { | 293 | struct user_net_device_stats { |
| 307 | unsigned long long rx_packets; /* total packets received */ | 294 | unsigned long long rx_packets; /* total packets received */ |
| 308 | unsigned long long tx_packets; /* total packets transmitted */ | 295 | unsigned long long tx_packets; /* total packets transmitted */ |
| @@ -357,62 +344,8 @@ struct interface { | |||
| 357 | int interface_opt_a; /* show all interfaces */ | 344 | int interface_opt_a; /* show all interfaces */ |
| 358 | 345 | ||
| 359 | static struct interface *int_list, *int_last; | 346 | static struct interface *int_list, *int_last; |
| 360 | static int skfd = -1; /* generic raw socket desc. */ | ||
| 361 | |||
| 362 | |||
| 363 | static int sockets_open(int family) | ||
| 364 | { | ||
| 365 | struct aftype * const *aft; | ||
| 366 | int sfd = -1; | ||
| 367 | static int force = -1; | ||
| 368 | |||
| 369 | if (force < 0) { | ||
| 370 | force = 0; | ||
| 371 | if (get_linux_version_code() < KERNEL_VERSION(2,1,0)) | ||
| 372 | force = 1; | ||
| 373 | if (access("/proc/net", R_OK)) | ||
| 374 | force = 1; | ||
| 375 | } | ||
| 376 | for (aft = aftypes; *aft; aft++) { | ||
| 377 | struct aftype *af = *aft; | ||
| 378 | int type = SOCK_DGRAM; | ||
| 379 | 347 | ||
| 380 | if (af->af == AF_UNSPEC) | ||
| 381 | continue; | ||
| 382 | if (family && family != af->af) | ||
| 383 | continue; | ||
| 384 | if (af->fd != -1) { | ||
| 385 | sfd = af->fd; | ||
| 386 | continue; | ||
| 387 | } | ||
| 388 | /* Check some /proc file first to not stress kmod */ | ||
| 389 | if (!family && !force && af->flag_file) { | ||
| 390 | if (access(af->flag_file, R_OK)) | ||
| 391 | continue; | ||
| 392 | } | ||
| 393 | af->fd = socket(af->af, type, 0); | ||
| 394 | if (af->fd >= 0) | ||
| 395 | sfd = af->fd; | ||
| 396 | } | ||
| 397 | if (sfd < 0) { | ||
| 398 | bb_error_msg("no usable address families found"); | ||
| 399 | } | ||
| 400 | return sfd; | ||
| 401 | } | ||
| 402 | 348 | ||
| 403 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
| 404 | static void sockets_close(void) | ||
| 405 | { | ||
| 406 | struct aftype * const *aft; | ||
| 407 | for (aft = aftypes; *aft != NULL; aft++) { | ||
| 408 | struct aftype *af = *aft; | ||
| 409 | if( af->fd != -1 ) { | ||
| 410 | close(af->fd); | ||
| 411 | af->fd = -1; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | } | ||
| 415 | #endif | ||
| 416 | #if 0 | 349 | #if 0 |
| 417 | /* like strcmp(), but knows about numbers */ | 350 | /* like strcmp(), but knows about numbers */ |
| 418 | except that the freshly added calls to xatoul() brf on ethernet aliases with | 351 | except that the freshly added calls to xatoul() brf on ethernet aliases with |
| @@ -467,55 +400,6 @@ static struct interface *add_interface(char *name) | |||
| 467 | return new; | 400 | return new; |
| 468 | } | 401 | } |
| 469 | 402 | ||
| 470 | |||
| 471 | static int if_readconf(void) | ||
| 472 | { | ||
| 473 | int numreqs = 30; | ||
| 474 | struct ifconf ifc; | ||
| 475 | struct ifreq *ifr; | ||
| 476 | int n, err = -1; | ||
| 477 | int skfd2; | ||
| 478 | |||
| 479 | /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets | ||
| 480 | (as of 2.1.128) */ | ||
| 481 | skfd2 = get_socket_for_af(AF_INET); | ||
| 482 | if (skfd2 < 0) { | ||
| 483 | bb_perror_msg(("warning: no inet socket available")); | ||
| 484 | /* Try to soldier on with whatever socket we can get hold of. */ | ||
| 485 | skfd2 = sockets_open(0); | ||
| 486 | if (skfd2 < 0) | ||
| 487 | return -1; | ||
| 488 | } | ||
| 489 | |||
| 490 | ifc.ifc_buf = NULL; | ||
| 491 | for (;;) { | ||
| 492 | ifc.ifc_len = sizeof(struct ifreq) * numreqs; | ||
| 493 | ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len); | ||
| 494 | |||
| 495 | if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) { | ||
| 496 | perror("SIOCGIFCONF"); | ||
| 497 | goto out; | ||
| 498 | } | ||
| 499 | if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) { | ||
| 500 | /* assume it overflowed and try again */ | ||
| 501 | numreqs += 10; | ||
| 502 | continue; | ||
| 503 | } | ||
| 504 | break; | ||
| 505 | } | ||
| 506 | |||
| 507 | ifr = ifc.ifc_req; | ||
| 508 | for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { | ||
| 509 | add_interface(ifr->ifr_name); | ||
| 510 | ifr++; | ||
| 511 | } | ||
| 512 | err = 0; | ||
| 513 | |||
| 514 | out: | ||
| 515 | free(ifc.ifc_buf); | ||
| 516 | return err; | ||
| 517 | } | ||
| 518 | |||
| 519 | static char *get_name(char *name, char *p) | 403 | static char *get_name(char *name, char *p) |
| 520 | { | 404 | { |
| 521 | /* Extract <name> from nul-terminated p where p matches | 405 | /* Extract <name> from nul-terminated p where p matches |
| @@ -550,7 +434,7 @@ static char *get_name(char *name, char *p) | |||
| 550 | * old approach of multiple scanf occurrences with large numbers of | 434 | * old approach of multiple scanf occurrences with large numbers of |
| 551 | * args. */ | 435 | * args. */ |
| 552 | 436 | ||
| 553 | /* static const char * const ss_fmt[] = { */ | 437 | /* static const char *const ss_fmt[] = { */ |
| 554 | /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */ | 438 | /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */ |
| 555 | /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */ | 439 | /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */ |
| 556 | /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */ | 440 | /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */ |
| @@ -558,13 +442,13 @@ static char *get_name(char *name, char *p) | |||
| 558 | 442 | ||
| 559 | /* Lie about the size of the int pointed to for %n. */ | 443 | /* Lie about the size of the int pointed to for %n. */ |
| 560 | #if INT_MAX == LONG_MAX | 444 | #if INT_MAX == LONG_MAX |
| 561 | static const char * const ss_fmt[] = { | 445 | static const char *const ss_fmt[] = { |
| 562 | "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u", | 446 | "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u", |
| 563 | "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u", | 447 | "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u", |
| 564 | "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u" | 448 | "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u" |
| 565 | }; | 449 | }; |
| 566 | #else | 450 | #else |
| 567 | static const char * const ss_fmt[] = { | 451 | static const char *const ss_fmt[] = { |
| 568 | "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu", | 452 | "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu", |
| 569 | "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu", | 453 | "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu", |
| 570 | "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" | 454 | "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" |
| @@ -615,6 +499,54 @@ static inline int procnetdev_version(char *buf) | |||
| 615 | return 0; | 499 | return 0; |
| 616 | } | 500 | } |
| 617 | 501 | ||
| 502 | /* Used only if "/proc/net/dev" isn't available */ | ||
| 503 | static int if_readconf(void) | ||
| 504 | { | ||
| 505 | int numreqs = 30; | ||
| 506 | struct ifconf ifc; | ||
| 507 | struct ifreq *ifr; | ||
| 508 | int n, err = -1; | ||
| 509 | int skfd; | ||
| 510 | |||
| 511 | ifc.ifc_buf = NULL; | ||
| 512 | |||
| 513 | /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets | ||
| 514 | (as of 2.1.128) */ | ||
| 515 | skfd = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 516 | if (skfd < 0) { | ||
| 517 | bb_perror_msg("error: no inet socket available"); | ||
| 518 | return -1; | ||
| 519 | } | ||
| 520 | |||
| 521 | for (;;) { | ||
| 522 | ifc.ifc_len = sizeof(struct ifreq) * numreqs; | ||
| 523 | ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len); | ||
| 524 | |||
| 525 | if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) { | ||
| 526 | perror("SIOCGIFCONF"); | ||
| 527 | goto out; | ||
| 528 | } | ||
| 529 | if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) { | ||
| 530 | /* assume it overflowed and try again */ | ||
| 531 | numreqs += 10; | ||
| 532 | continue; | ||
| 533 | } | ||
| 534 | break; | ||
| 535 | } | ||
| 536 | |||
| 537 | ifr = ifc.ifc_req; | ||
| 538 | for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { | ||
| 539 | add_interface(ifr->ifr_name); | ||
| 540 | ifr++; | ||
| 541 | } | ||
| 542 | err = 0; | ||
| 543 | |||
| 544 | out: | ||
| 545 | close(skfd); | ||
| 546 | free(ifc.ifc_buf); | ||
| 547 | return err; | ||
| 548 | } | ||
| 549 | |||
| 618 | static int if_readlist_proc(char *target) | 550 | static int if_readlist_proc(char *target) |
| 619 | { | 551 | { |
| 620 | static int proc_read; | 552 | static int proc_read; |
| @@ -660,11 +592,7 @@ static int if_readlist_proc(char *target) | |||
| 660 | 592 | ||
| 661 | static int if_readlist(void) | 593 | static int if_readlist(void) |
| 662 | { | 594 | { |
| 663 | int err = if_readlist_proc(NULL); | 595 | return if_readlist_proc(NULL); |
| 664 | |||
| 665 | if (!err) | ||
| 666 | err = if_readconf(); | ||
| 667 | return err; | ||
| 668 | } | 596 | } |
| 669 | 597 | ||
| 670 | static int for_all_interfaces(int (*doit) (struct interface *, void *), | 598 | static int for_all_interfaces(int (*doit) (struct interface *, void *), |
| @@ -687,81 +615,74 @@ static int for_all_interfaces(int (*doit) (struct interface *, void *), | |||
| 687 | static int if_fetch(struct interface *ife) | 615 | static int if_fetch(struct interface *ife) |
| 688 | { | 616 | { |
| 689 | struct ifreq ifr; | 617 | struct ifreq ifr; |
| 690 | int fd; | ||
| 691 | char *ifname = ife->name; | 618 | char *ifname = ife->name; |
| 619 | int skfd; | ||
| 620 | |||
| 621 | skfd = xsocket(AF_INET, SOCK_DGRAM, 0); | ||
| 692 | 622 | ||
| 693 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 623 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 694 | if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) | 624 | if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) { |
| 625 | close(skfd); | ||
| 695 | return -1; | 626 | return -1; |
| 627 | } | ||
| 696 | ife->flags = ifr.ifr_flags; | 628 | ife->flags = ifr.ifr_flags; |
| 697 | 629 | ||
| 698 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 630 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 699 | if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) | 631 | memset(ife->hwaddr, 0, 32); |
| 700 | memset(ife->hwaddr, 0, 32); | 632 | if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0) |
| 701 | else | ||
| 702 | memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8); | 633 | memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8); |
| 703 | 634 | ||
| 704 | ife->type = ifr.ifr_hwaddr.sa_family; | 635 | ife->type = ifr.ifr_hwaddr.sa_family; |
| 705 | 636 | ||
| 706 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 637 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 707 | if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0) | 638 | ife->metric = 0; |
| 708 | ife->metric = 0; | 639 | if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0) |
| 709 | else | ||
| 710 | ife->metric = ifr.ifr_metric; | 640 | ife->metric = ifr.ifr_metric; |
| 711 | 641 | ||
| 712 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 642 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 713 | if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0) | 643 | ife->mtu = 0; |
| 714 | ife->mtu = 0; | 644 | if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0) |
| 715 | else | ||
| 716 | ife->mtu = ifr.ifr_mtu; | 645 | ife->mtu = ifr.ifr_mtu; |
| 717 | 646 | ||
| 647 | memset(&ife->map, 0, sizeof(struct ifmap)); | ||
| 718 | #ifdef SIOCGIFMAP | 648 | #ifdef SIOCGIFMAP |
| 719 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 649 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 720 | if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0) | 650 | if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0) |
| 721 | ife->map = ifr.ifr_map; | 651 | ife->map = ifr.ifr_map; |
| 722 | else | ||
| 723 | #endif | 652 | #endif |
| 724 | memset(&ife->map, 0, sizeof(struct ifmap)); | ||
| 725 | 653 | ||
| 726 | #ifdef HAVE_TXQUEUELEN | 654 | #ifdef HAVE_TXQUEUELEN |
| 727 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 655 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 728 | if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0) | 656 | ife->tx_queue_len = -1; /* unknown value */ |
| 729 | ife->tx_queue_len = -1; /* unknown value */ | 657 | if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0) |
| 730 | else | ||
| 731 | ife->tx_queue_len = ifr.ifr_qlen; | 658 | ife->tx_queue_len = ifr.ifr_qlen; |
| 732 | #else | 659 | #else |
| 733 | ife->tx_queue_len = -1; /* unknown value */ | 660 | ife->tx_queue_len = -1; /* unknown value */ |
| 734 | #endif | 661 | #endif |
| 735 | 662 | ||
| 736 | /* IPv4 address? */ | 663 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 737 | fd = get_socket_for_af(AF_INET); | 664 | ifr.ifr_addr.sa_family = AF_INET; |
| 738 | if (fd >= 0) { | 665 | memset(&ife->addr, 0, sizeof(struct sockaddr)); |
| 666 | if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) { | ||
| 667 | ife->has_ip = 1; | ||
| 668 | ife->addr = ifr.ifr_addr; | ||
| 739 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 669 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 740 | ifr.ifr_addr.sa_family = AF_INET; | 670 | memset(&ife->dstaddr, 0, sizeof(struct sockaddr)); |
| 741 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { | 671 | if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0) |
| 742 | ife->has_ip = 1; | 672 | ife->dstaddr = ifr.ifr_dstaddr; |
| 743 | ife->addr = ifr.ifr_addr; | 673 | |
| 744 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 674 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 745 | if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0) | 675 | memset(&ife->broadaddr, 0, sizeof(struct sockaddr)); |
| 746 | memset(&ife->dstaddr, 0, sizeof(struct sockaddr)); | 676 | if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0) |
| 747 | else | 677 | ife->broadaddr = ifr.ifr_broadaddr; |
| 748 | ife->dstaddr = ifr.ifr_dstaddr; | 678 | |
| 749 | 679 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | |
| 750 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 680 | memset(&ife->netmask, 0, sizeof(struct sockaddr)); |
| 751 | if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0) | 681 | if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0) |
| 752 | memset(&ife->broadaddr, 0, sizeof(struct sockaddr)); | 682 | ife->netmask = ifr.ifr_netmask; |
| 753 | else | ||
| 754 | ife->broadaddr = ifr.ifr_broadaddr; | ||
| 755 | |||
| 756 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | ||
| 757 | if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0) | ||
| 758 | memset(&ife->netmask, 0, sizeof(struct sockaddr)); | ||
| 759 | else | ||
| 760 | ife->netmask = ifr.ifr_netmask; | ||
| 761 | } else | ||
| 762 | memset(&ife->addr, 0, sizeof(struct sockaddr)); | ||
| 763 | } | 683 | } |
| 764 | 684 | ||
| 685 | close(skfd); | ||
| 765 | return 0; | 686 | return 0; |
| 766 | } | 687 | } |
| 767 | 688 | ||
| @@ -887,7 +808,7 @@ static const struct hwtype ppp_hwtype = { | |||
| 887 | .type = ARPHRD_PPP | 808 | .type = ARPHRD_PPP |
| 888 | }; | 809 | }; |
| 889 | 810 | ||
| 890 | #ifdef CONFIG_FEATURE_IPV6 | 811 | #if ENABLE_FEATURE_IPV6 |
| 891 | static const struct hwtype sit_hwtype = { | 812 | static const struct hwtype sit_hwtype = { |
| 892 | .name = "sit", | 813 | .name = "sit", |
| 893 | .title = "IPv6-in-IPv4", | 814 | .title = "IPv6-in-IPv4", |
| @@ -897,19 +818,19 @@ static const struct hwtype sit_hwtype = { | |||
| 897 | } ; | 818 | } ; |
| 898 | #endif | 819 | #endif |
| 899 | 820 | ||
| 900 | static const struct hwtype * const hwtypes[] = { | 821 | static const struct hwtype *const hwtypes[] = { |
| 901 | &loop_hwtype, | 822 | &loop_hwtype, |
| 902 | ðer_hwtype, | 823 | ðer_hwtype, |
| 903 | &ppp_hwtype, | 824 | &ppp_hwtype, |
| 904 | &unspec_hwtype, | 825 | &unspec_hwtype, |
| 905 | #ifdef CONFIG_FEATURE_IPV6 | 826 | #if ENABLE_FEATURE_IPV6 |
| 906 | &sit_hwtype, | 827 | &sit_hwtype, |
| 907 | #endif | 828 | #endif |
| 908 | NULL | 829 | NULL |
| 909 | }; | 830 | }; |
| 910 | 831 | ||
| 911 | #ifdef IFF_PORTSEL | 832 | #ifdef IFF_PORTSEL |
| 912 | static const char * const if_port_text[] = { | 833 | static const char *const if_port_text[] = { |
| 913 | /* Keep in step with <linux/netdevice.h> */ | 834 | /* Keep in step with <linux/netdevice.h> */ |
| 914 | "unknown", | 835 | "unknown", |
| 915 | "10base2", | 836 | "10base2", |
| @@ -925,7 +846,7 @@ static const char * const if_port_text[] = { | |||
| 925 | /* Check our hardware type table for this type. */ | 846 | /* Check our hardware type table for this type. */ |
| 926 | const struct hwtype *get_hwtype(const char *name) | 847 | const struct hwtype *get_hwtype(const char *name) |
| 927 | { | 848 | { |
| 928 | const struct hwtype * const *hwp; | 849 | const struct hwtype *const *hwp; |
| 929 | 850 | ||
| 930 | hwp = hwtypes; | 851 | hwp = hwtypes; |
| 931 | while (*hwp != NULL) { | 852 | while (*hwp != NULL) { |
| @@ -939,7 +860,7 @@ const struct hwtype *get_hwtype(const char *name) | |||
| 939 | /* Check our hardware type table for this type. */ | 860 | /* Check our hardware type table for this type. */ |
| 940 | const struct hwtype *get_hwntype(int type) | 861 | const struct hwtype *get_hwntype(int type) |
| 941 | { | 862 | { |
| 942 | const struct hwtype * const *hwp; | 863 | const struct hwtype *const *hwp; |
| 943 | 864 | ||
| 944 | hwp = hwtypes; | 865 | hwp = hwtypes; |
| 945 | while (*hwp != NULL) { | 866 | while (*hwp != NULL) { |
| @@ -987,7 +908,7 @@ static void print_bytes_scaled(unsigned long long ull, const char *end) | |||
| 987 | printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end); | 908 | printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end); |
| 988 | } | 909 | } |
| 989 | 910 | ||
| 990 | static const char * const ife_print_flags_strs[] = { | 911 | static const char *const ife_print_flags_strs[] = { |
| 991 | "UP ", | 912 | "UP ", |
| 992 | "BROADCAST ", | 913 | "BROADCAST ", |
| 993 | "DEBUG ", | 914 | "DEBUG ", |
| @@ -1028,7 +949,7 @@ static const unsigned short ife_print_flags_mask[] = { | |||
| 1028 | 949 | ||
| 1029 | static void ife_print(struct interface *ptr) | 950 | static void ife_print(struct interface *ptr) |
| 1030 | { | 951 | { |
| 1031 | struct aftype *ap; | 952 | const struct aftype *ap; |
| 1032 | const struct hwtype *hw; | 953 | const struct hwtype *hw; |
| 1033 | int hf; | 954 | int hf; |
| 1034 | int can_compress = 0; | 955 | int can_compress = 0; |
| @@ -1265,15 +1186,7 @@ int display_interfaces(char *ifname) | |||
| 1265 | { | 1186 | { |
| 1266 | int status; | 1187 | int status; |
| 1267 | 1188 | ||
| 1268 | /* Create a channel to the NET kernel. */ | ||
| 1269 | if ((skfd = sockets_open(0)) < 0) { | ||
| 1270 | bb_perror_msg_and_die("socket"); | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | /* Do we have to show the current setup? */ | ||
| 1274 | status = if_print(ifname); | 1189 | status = if_print(ifname); |
| 1275 | #ifdef CONFIG_FEATURE_CLEAN_UP | 1190 | |
| 1276 | sockets_close(); | 1191 | return (status < 0); /* status < 0 == 1 -- error */ |
| 1277 | #endif | ||
| 1278 | exit(status < 0); | ||
| 1279 | } | 1192 | } |
