Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/networking/libiproute/ipaddress.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/networking/libiproute/ipaddress.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 1 2 /* 2 3 * ipaddress.c "ip address". … … 10 11 */ 11 12 12 #include "libbb.h" 13 #include <sys/socket.h> 14 #include <sys/ioctl.h> 15 13 //#include <sys/socket.h> 14 //#include <sys/ioctl.h> 16 15 #include <fnmatch.h> 17 #include <string.h>18 #include <unistd.h>19 20 16 #include <net/if.h> 21 17 #include <net/if_arp.h> 22 18 19 #include "ip_common.h" /* #include "libbb.h" is inside */ 23 20 #include "rt_names.h" 24 21 #include "utils.h" 25 #include "ip_common.h" 26 27 28 static struct 29 { 22 23 24 typedef struct filter_t { 30 25 int ifindex; 31 26 int family; … … 42 37 int flushe; 43 38 struct rtnl_handle *rth; 44 } filter; 39 } filter_t; 40 41 #define filter (*(filter_t*)&bb_common_bufsiz1) 42 45 43 46 44 static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) … … 49 47 flags &= ~IFF_RUNNING; 50 48 #define _PF(f) if (flags&IFF_##f) { \ 51 flags &= ~IFF_##f 49 flags &= ~IFF_##f; \ 52 50 fprintf(fp, #f "%s", flags ? "," : ""); } 53 51 _PF(LOOPBACK); … … 86 84 87 85 memset(&ifr, 0, sizeof(ifr)); 88 strcpy(ifr.ifr_name, name); 89 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { 90 perror("SIOCGIFXQLEN"); 86 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 87 if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) { 91 88 close(s); 92 89 return; … … 99 96 100 97 static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, 101 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)98 const struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg) 102 99 { 103 100 FILE *fp = (FILE*)arg; … … 125 122 return -1; 126 123 } 127 if (filter.label && 128 (!filter.family || filter.family == AF_PACKET) && 129 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) 130 return 0; 124 if (filter.label 125 && (!filter.family || filter.family == AF_PACKET) 126 && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0) 127 ) { 128 return 0; 129 } 131 130 132 131 if (n->nlmsg_type == RTM_DELLINK) … … 166 165 if (!filter.family || filter.family == AF_PACKET) { 167 166 SPRINT_BUF(b1); 168 fprintf(fp, "%s", _SL_); 169 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); 167 fprintf(fp, "%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); 170 168 171 169 if (tb[IFLA_ADDRESS]) { … … 186 184 } 187 185 } 188 fp rintf(fp, "\n");186 fputc('\n', fp); 189 187 fflush(fp); 190 188 return 0; … … 194 192 { 195 193 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { 196 perror("Failed to send flush request\n");194 bb_perror_msg("failed to send flush request"); 197 195 return -1; 198 196 } … … 341 339 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); 342 340 char buf[128]; 343 fp rintf(fp, "%s", _SL_);341 fputc(_SL_, fp); 344 342 if (ci->ifa_valid == 0xFFFFFFFFU) 345 343 sprintf(buf, "valid_lft forever"); … … 352 350 fprintf(fp, " %s", buf); 353 351 } 354 fp rintf(fp, "\n");352 fputc('\n', fp); 355 353 fflush(fp); 356 354 return 0; … … 366 364 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) 367 365 { 368 for ( ;ainfo ;ainfo = ainfo->next) {366 for (; ainfo; ainfo = ainfo->next) { 369 367 struct nlmsghdr *n = &ainfo->h; 370 368 struct ifaddrmsg *ifa = NLMSG_DATA(n); … … 412 410 } 413 411 412 /* Return value becomes exitcode. It's okay to not return at all */ 414 413 int ipaddr_list_or_flush(int argc, char **argv, int flush) 415 414 { 416 static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 };415 static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0"; 417 416 418 417 struct nlmsg_list *linfo = NULL; … … 431 430 if (flush) { 432 431 if (argc <= 0) { 433 bb_error_msg(bb_msg_requires_arg, "flush"); 434 return -1; 432 bb_error_msg_and_die(bb_msg_requires_arg, "flush"); 435 433 } 436 434 if (filter.family == AF_PACKET) { 437 bb_error_msg("Cannot flush link addresses."); 438 return -1; 435 bb_error_msg_and_die("cannot flush link addresses"); 439 436 } 440 437 } 441 438 442 439 while (argc > 0) { 443 const int option_num = compare_string_array(option, *argv);440 const int option_num = index_in_strings(option, *argv); 444 441 switch (option_num) { 445 442 case 0: /* to */ … … 484 481 } 485 482 486 if (rtnl_open(&rth, 0) < 0) 487 exit(1); 488 489 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { 490 bb_perror_msg_and_die("Cannot send dump request"); 491 } 492 493 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) { 494 bb_error_msg_and_die("Dump terminated"); 495 } 483 xrtnl_open(&rth); 484 485 xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK); 486 xrtnl_dump_filter(&rth, store_nlmsg, &linfo); 496 487 497 488 if (filter_dev) { 498 filter.ifindex = ll_name_to_index(filter_dev); 499 if (filter.ifindex <= 0) { 500 bb_error_msg("Device \"%s\" does not exist", filter_dev); 501 return -1; 502 } 489 filter.ifindex = xll_name_to_index(filter_dev); 503 490 } 504 491 … … 512 499 513 500 for (;;) { 514 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { 515 perror("Cannot send dump request"); 516 exit(1); 517 } 501 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR); 518 502 filter.flushed = 0; 519 if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) { 520 fprintf(stderr, "Flush terminated\n"); 521 exit(1); 522 } 503 xrtnl_dump_filter(&rth, print_addrinfo, stdout); 523 504 if (filter.flushed == 0) { 524 fflush(stdout);525 505 return 0; 526 506 } 527 507 if (flush_update() < 0) 528 exit(1);508 return 1; 529 509 } 530 510 } 531 511 532 512 if (filter.family != AF_PACKET) { 533 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { 534 bb_perror_msg_and_die("Cannot send dump request"); 535 } 536 537 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) { 538 bb_error_msg_and_die("Dump terminated"); 539 } 513 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR); 514 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo); 540 515 } 541 516 … … 601 576 } 602 577 603 for (l =linfo; l; l = l->next) {578 for (l = linfo; l; l = l->next) { 604 579 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { 605 580 struct ifinfomsg *ifi = NLMSG_DATA(&l->h); … … 607 582 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); 608 583 } 609 fflush(stdout); 610 } 611 612 exit(0);584 fflush(stdout); /* why? */ 585 } 586 587 return 0; 613 588 } 614 589 … … 616 591 { 617 592 if (lcl->family == AF_INET) { 618 if (lcl->bytelen >= 1 && *( __u8*)&lcl->data == 127)593 if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127) 619 594 return RT_SCOPE_HOST; 620 595 } … … 622 597 } 623 598 599 /* Return value becomes exitcode. It's okay to not return at all */ 624 600 static int ipaddr_modify(int cmd, int argc, char **argv) 625 601 { 626 static const char *const option[] = { 627 "peer", "remote", "broadcast", "brd", 628 "anycast", "scope", "dev", "label", "local", 0 629 }; 630 602 static const char option[] ALIGN1 = 603 "peer\0""remote\0""broadcast\0""brd\0" 604 "anycast\0""scope\0""dev\0""label\0""local\0"; 631 605 struct rtnl_handle rth; 632 606 struct { 633 struct nlmsghdr 634 struct ifaddrmsg 635 char 607 struct nlmsghdr n; 608 struct ifaddrmsg ifa; 609 char buf[256]; 636 610 } req; 637 char 638 char 611 char *d = NULL; 612 char *l = NULL; 639 613 inet_prefix lcl; 640 614 inet_prefix peer; … … 643 617 int brd_len = 0; 644 618 int any_len = 0; 645 intscoped = 0;619 bool scoped = 0; 646 620 647 621 memset(&req, 0, sizeof(req)); … … 653 627 654 628 while (argc > 0) { 655 const int option_num = compare_string_array(option, *argv);629 const int option_num = index_in_strings(option, *argv); 656 630 switch (option_num) { 657 631 case 0: /* peer */ … … 678 652 duparg("broadcast", *argv); 679 653 } 680 if ( strcmp(*argv, "+") == 0) {654 if (LONE_CHAR(*argv, '+')) { 681 655 brd_len = -1; 682 656 } 683 else if ( strcmp(*argv, "-") == 0) {657 else if (LONE_DASH(*argv)) { 684 658 brd_len = -2; 685 659 } else { … … 748 722 return -1; 749 723 } 750 if (l && matches(d, l) != 0) {724 if (l && strncmp(d, l, strlen(d)) != 0) { 751 725 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l); 752 726 } … … 763 737 int i; 764 738 if (req.ifa.ifa_family != AF_INET) { 765 bb_error_msg("Broadcast can be set only for IPv4 addresses"); 766 return -1; 739 bb_error_msg_and_die("broadcast can be set only for IPv4 addresses"); 767 740 } 768 741 brd = peer; … … 781 754 req.ifa.ifa_scope = default_scope(&lcl); 782 755 783 if (rtnl_open(&rth, 0) < 0) 784 exit(1); 756 xrtnl_open(&rth); 785 757 786 758 ll_init_map(&rth); 787 759 788 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { 789 bb_error_msg("Cannot find device \"%s\"", d); 790 return -1; 791 } 760 req.ifa.ifa_index = xll_name_to_index(d); 792 761 793 762 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) 794 exit(2); 795 796 exit(0); 797 } 798 763 return 2; 764 765 return 0; 766 } 767 768 /* Return value becomes exitcode. It's okay to not return at all */ 799 769 int do_ipaddr(int argc, char **argv) 800 770 { 801 static const char *const commands[] = { 802 "add", "delete", "list", "show", "lst", "flush", 0 803 }; 804 805 int command_num = 2; 771 static const char commands[] ALIGN1 = 772 "add\0""delete\0""list\0""show\0""lst\0""flush\0"; 773 774 int command_num = 2; /* default command is list */ 806 775 807 776 if (*argv) { 808 command_num = compare_string_array(commands, *argv); 809 } 810 switch (command_num) { 811 case 0: /* add */ 812 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1); 813 case 1: /* delete */ 814 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1); 815 case 2: /* list */ 816 case 3: /* show */ 817 case 4: /* lst */ 818 return ipaddr_list_or_flush(argc-1, argv+1, 0); 819 case 5: /* flush */ 820 return ipaddr_list_or_flush(argc-1, argv+1, 1); 821 } 822 bb_error_msg_and_die("Unknown command %s", *argv); 823 } 777 command_num = index_in_substrings(commands, *argv); 778 } 779 if (command_num < 0 || command_num > 5) 780 bb_error_msg_and_die("unknown command %s", *argv); 781 --argc; 782 ++argv; 783 if (command_num == 0) /* add */ 784 return ipaddr_modify(RTM_NEWADDR, argc, argv); 785 else if (command_num == 1) /* delete */ 786 return ipaddr_modify(RTM_DELADDR, argc, argv); 787 else if (command_num == 5) /* flush */ 788 return ipaddr_list_or_flush(argc, argv, 1); 789 else /* 2 == list, 3 == show, 4 == lst */ 790 return ipaddr_list_or_flush(argc, argv, 0); 791 }
Note:
See TracChangeset
for help on using the changeset viewer.