Mercurial > hg > toybox
comparison toys/pending/ifconfig.c @ 879:bc7aa2d123b9
And another round of ifconfig cleanup.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 25 Apr 2013 00:19:51 -0500 |
parents | d4263ebf6f9b |
children | aca8323e2690 |
comparison
equal
deleted
inserted
replaced
878:d4263ebf6f9b | 879:bc7aa2d123b9 |
---|---|
62 }; | 62 }; |
63 | 63 |
64 #define HW_NAME_LEN 20 | 64 #define HW_NAME_LEN 20 |
65 #define HW_TITLE_LEN 30 | 65 #define HW_TITLE_LEN 30 |
66 | 66 |
67 typedef struct _hw_info { | |
68 char hw_name[HW_NAME_LEN]; | |
69 char hw_title[HW_TITLE_LEN]; | |
70 int hw_addrlen; | |
71 } HW_INFO; | |
72 | |
73 #define NO_RANGE -1 | |
74 #define IO_MAP_INDEX 0x100 | 67 #define IO_MAP_INDEX 0x100 |
75 | 68 |
76 //from kernel header ipv6.h | 69 //from kernel header ipv6.h |
77 #define IPV6_ADDR_ANY 0x0000U | 70 #define IPV6_ADDR_ANY 0x0000U |
78 #define IPV6_ADDR_LOOPBACK 0x0010U | 71 #define IPV6_ADDR_LOOPBACK 0x0010U |
494 il->mtu = ifre.ifr_mtu; | 487 il->mtu = ifre.ifr_mtu; |
495 | 488 |
496 if(ioctl(sokfd, SIOCGIFMAP, &ifre) == 0) | 489 if(ioctl(sokfd, SIOCGIFMAP, &ifre) == 0) |
497 il->map = ifre.ifr_map; | 490 il->map = ifre.ifr_map; |
498 | 491 |
499 il->txqueuelen = NO_RANGE; | 492 il->txqueuelen = -1; |
500 if(ioctl(sokfd, SIOCGIFTXQLEN, &ifre) >= 0) | 493 if(ioctl(sokfd, SIOCGIFTXQLEN, &ifre) >= 0) |
501 il->txqueuelen = ifre.ifr_qlen; | 494 il->txqueuelen = ifre.ifr_qlen; |
502 | 495 |
503 ifre.ifr_addr.sa_family = AF_INET; | 496 ifre.ifr_addr.sa_family = AF_INET; |
504 | 497 |
515 il->netmask = ifre.ifr_netmask; | 508 il->netmask = ifre.ifr_netmask; |
516 } | 509 } |
517 close(sokfd); | 510 close(sokfd); |
518 } | 511 } |
519 | 512 |
520 static void get_hw_info(int hw_type, HW_INFO *hw_info) | 513 static void show_ip_addr(char *name, struct sockaddr *skaddr) |
521 { | 514 { |
522 memset(hw_info, 0, sizeof(HW_INFO)); | 515 char *s = "[NOT SET]"; |
523 | 516 |
524 switch(hw_type) { | 517 if(skaddr->sa_family != 0xFFFF && skaddr->sa_family) |
525 case ARPHRD_LOOPBACK: //Loopback device. | 518 s = inet_ntoa(((struct sockaddr_in *)skaddr)->sin_addr); |
526 strncpy(hw_info->hw_name, "loop", HW_NAME_LEN); | 519 |
527 strncpy(hw_info->hw_title, "Local Loopback", HW_TITLE_LEN); | 520 xprintf(" %s:%s ", name, s); |
528 hw_info->hw_addrlen = 0; | |
529 break; | |
530 case ARPHRD_ETHER: //Ethernet | |
531 strncpy(hw_info->hw_name, "ether", HW_NAME_LEN); | |
532 strncpy(hw_info->hw_title, "Ethernet", HW_TITLE_LEN); | |
533 hw_info->hw_addrlen = ETH_ALEN; | |
534 break; | |
535 case ARPHRD_PPP: //ARPHRD_PPP | |
536 strncpy(hw_info->hw_name, "ppp", HW_NAME_LEN); | |
537 strncpy(hw_info->hw_title, "Point-to-Point Protocol", HW_TITLE_LEN); | |
538 hw_info->hw_addrlen = 0; | |
539 break; | |
540 case ARPHRD_INFINIBAND: //InfiniBand | |
541 strncpy(hw_info->hw_name, "infiniband", HW_NAME_LEN); | |
542 strncpy(hw_info->hw_title, "InfiniBand", HW_TITLE_LEN); | |
543 hw_info->hw_addrlen = 20; | |
544 break; | |
545 case ARPHRD_SIT: //sit0 device - IPv6-in-IPv4 | |
546 strncpy(hw_info->hw_name, "sit", HW_NAME_LEN); | |
547 strncpy(hw_info->hw_title, "IPv6-in-IPv4", HW_TITLE_LEN); | |
548 hw_info->hw_addrlen = 0; | |
549 break; | |
550 case -1: | |
551 strncpy(hw_info->hw_name, "unspec", HW_NAME_LEN); | |
552 strncpy(hw_info->hw_title, "UNSPEC", HW_TITLE_LEN); | |
553 hw_info->hw_addrlen = 0; | |
554 break; | |
555 default: | |
556 break; | |
557 } | |
558 } | |
559 | |
560 static void print_hw_addr(int hw_type, HW_INFO hw_info, struct if_list *il) | |
561 { | |
562 char *address = il->hwaddr.sa_data; | |
563 | |
564 if(!address || !hw_info.hw_addrlen) return; | |
565 xprintf("HWaddr "); | |
566 if(hw_type == ARPHRD_ETHER) { | |
567 int i; | |
568 | |
569 for (i=0; i<6; i++) xprintf(":%02X"+!i, address[i]); | |
570 } | |
571 } | |
572 | |
573 static char *get_ip_addr(struct sockaddr *skaddr) | |
574 { | |
575 struct sockaddr_in *sin = (struct sockaddr_in *)skaddr; | |
576 | |
577 if(skaddr->sa_family == 0xFFFF || !skaddr->sa_family) return "[NOT SET]"; | |
578 if(sin->sin_family != AF_INET) { | |
579 errno = EAFNOSUPPORT; | |
580 return NULL; | |
581 } | |
582 | |
583 return inet_ntoa(sin->sin_addr); | |
584 } | |
585 | |
586 static void print_ip_addr(struct if_list *il) | |
587 { | |
588 char *af_name; | |
589 int af = il->addr.sa_family; | |
590 | |
591 if (af == AF_INET) af_name = "inet"; | |
592 else if (af == AF_INET6) af_name = "inet6"; | |
593 else if (af == AF_UNSPEC) af_name = "unspec"; | |
594 | |
595 xprintf("%10s%s addr:%s ", " ", af_name, get_ip_addr(&il->addr)); | |
596 if(il->flags & IFF_POINTOPOINT) | |
597 xprintf(" P-t-P:%s ", get_ip_addr(&il->dstaddr)); | |
598 if(il->flags & IFF_BROADCAST) | |
599 xprintf(" Bcast:%s ", get_ip_addr(&il->broadaddr)); | |
600 xprintf(" Mask:%s\n", get_ip_addr(&il->netmask)); | |
601 } | 521 } |
602 | 522 |
603 static void print_ip6_addr(struct if_list *il) | 523 static void print_ip6_addr(struct if_list *il) |
604 { | 524 { |
605 char iface_name[IFNAMSIZ] = {0,}; | 525 char iface_name[IFNAMSIZ] = {0,}; |
647 fclose(fp); | 567 fclose(fp); |
648 } | 568 } |
649 | 569 |
650 static void display_ifconfig(struct if_list *il) | 570 static void display_ifconfig(struct if_list *il) |
651 { | 571 { |
652 HW_INFO hw_info; | 572 struct { |
653 | 573 int type; |
654 get_hw_info(il->hw_type, &hw_info); | 574 char *title; |
655 xprintf("%-9s Link encap:%s ", il->name, hw_info.hw_title); | 575 } types[] = { |
656 print_hw_addr(il->hw_type, hw_info, il); | 576 {ARPHRD_LOOPBACK, "Local Loopback"}, {ARPHRD_ETHER, "Ethernet"}, |
577 {ARPHRD_PPP, "Point-to-Point Protocol"}, {ARPHRD_INFINIBAND, "InfiniBand"}, | |
578 {ARPHRD_SIT, "IPv6-in-IPv4"}, {-1, "UNSPEC"} | |
579 }; | |
580 int i; | |
581 | |
582 for (i=0; i < (sizeof(types)/sizeof(*types))-1; i++) | |
583 if (il->hw_type == types[i].type) break; | |
584 | |
585 xprintf("%-9s Link encap:%s ", il->name, types[i].title); | |
586 if(il->hwaddr.sa_data && il->hw_type == ARPHRD_ETHER) { | |
587 xprintf("HWaddr "); | |
588 for (i=0; i<6; i++) xprintf(":%02X"+!i, il->hwaddr.sa_data[i]); | |
589 } | |
657 xputc('\n'); | 590 xputc('\n'); |
658 | 591 |
659 //print addr, p-p addr, broadcast addr and mask addr. | 592 if(il->hasaddr) { |
660 if(il->hasaddr) print_ip_addr(il); | 593 int af = il->addr.sa_family; |
661 | 594 char *name = "unspec"; |
662 //for ipv6 to do. | 595 |
596 if (af == AF_INET) name = "inet"; | |
597 else if (af == AF_INET6) name = "inet6"; | |
598 xprintf("%10c%s", ' ', name); | |
599 | |
600 show_ip_addr("addr", &il->addr); | |
601 if(il->flags & IFF_POINTOPOINT) show_ip_addr("P-t-P", &il->dstaddr); | |
602 if(il->flags & IFF_BROADCAST) show_ip_addr("Bcast", &il->broadaddr); | |
603 show_ip_addr("Mask", &il->netmask); | |
604 xputc('\n'); | |
605 } | |
606 | |
663 print_ip6_addr(il); | 607 print_ip6_addr(il); |
664 xprintf("%10s", " "); | 608 xprintf("%10c", ' '); |
665 //print flags | |
666 | 609 |
667 if (il->flags) { | 610 if (il->flags) { |
668 unsigned short mask = 1; | 611 unsigned short mask = 1; |
669 char **s, *str[] = { | 612 char **s, *str[] = { |
670 "UP", "BROADCAST", "DEBUG", "LOOPBACK", "POINTOPOINT", "NOTRAILERS", | 613 "UP", "BROADCAST", "DEBUG", "LOOPBACK", "POINTOPOINT", "NOTRAILERS", |