diff options
Diffstat (limited to 'networking/libiproute/ipaddress.c')
-rw-r--r-- | networking/libiproute/ipaddress.c | 71 |
1 files changed, 35 insertions, 36 deletions
diff --git a/networking/libiproute/ipaddress.c b/networking/libiproute/ipaddress.c index bc9963f1f..ab9706492 100644 --- a/networking/libiproute/ipaddress.c +++ b/networking/libiproute/ipaddress.c | |||
@@ -26,8 +26,7 @@ | |||
26 | #include "ip_common.h" | 26 | #include "ip_common.h" |
27 | 27 | ||
28 | 28 | ||
29 | static struct | 29 | typedef struct filter_t { |
30 | { | ||
31 | int ifindex; | 30 | int ifindex; |
32 | int family; | 31 | int family; |
33 | int oneline; | 32 | int oneline; |
@@ -42,7 +41,10 @@ static struct | |||
42 | int flushp; | 41 | int flushp; |
43 | int flushe; | 42 | int flushe; |
44 | struct rtnl_handle *rth; | 43 | struct rtnl_handle *rth; |
45 | } filter; | 44 | } filter_t; |
45 | |||
46 | #define filter (*(filter_t*)&bb_common_bufsiz1) | ||
47 | |||
46 | 48 | ||
47 | static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) | 49 | static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) |
48 | { | 50 | { |
@@ -88,7 +90,7 @@ static void print_queuelen(char *name) | |||
88 | memset(&ifr, 0, sizeof(ifr)); | 90 | memset(&ifr, 0, sizeof(ifr)); |
89 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); | 91 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
90 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { | 92 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { |
91 | perror("SIOCGIFXQLEN"); | 93 | bb_perror_msg("SIOCGIFXQLEN"); |
92 | close(s); | 94 | close(s); |
93 | return; | 95 | return; |
94 | } | 96 | } |
@@ -125,10 +127,12 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, | |||
125 | bb_error_msg("nil ifname"); | 127 | bb_error_msg("nil ifname"); |
126 | return -1; | 128 | return -1; |
127 | } | 129 | } |
128 | if (filter.label && | 130 | if (filter.label |
129 | (!filter.family || filter.family == AF_PACKET) && | 131 | && (!filter.family || filter.family == AF_PACKET) |
130 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) | 132 | && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0) |
133 | ) { | ||
131 | return 0; | 134 | return 0; |
135 | } | ||
132 | 136 | ||
133 | if (n->nlmsg_type == RTM_DELLINK) | 137 | if (n->nlmsg_type == RTM_DELLINK) |
134 | fprintf(fp, "Deleted "); | 138 | fprintf(fp, "Deleted "); |
@@ -166,8 +170,7 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, | |||
166 | 170 | ||
167 | if (!filter.family || filter.family == AF_PACKET) { | 171 | if (!filter.family || filter.family == AF_PACKET) { |
168 | SPRINT_BUF(b1); | 172 | SPRINT_BUF(b1); |
169 | fprintf(fp, "%s", _SL_); | 173 | fprintf(fp, "%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); |
170 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); | ||
171 | 174 | ||
172 | if (tb[IFLA_ADDRESS]) { | 175 | if (tb[IFLA_ADDRESS]) { |
173 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), | 176 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
@@ -186,7 +189,7 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, | |||
186 | b1, sizeof(b1))); | 189 | b1, sizeof(b1))); |
187 | } | 190 | } |
188 | } | 191 | } |
189 | fprintf(fp, "\n"); | 192 | fputc('\n', fp); |
190 | fflush(fp); | 193 | fflush(fp); |
191 | return 0; | 194 | return 0; |
192 | } | 195 | } |
@@ -194,7 +197,7 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, | |||
194 | static int flush_update(void) | 197 | static int flush_update(void) |
195 | { | 198 | { |
196 | if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { | 199 | if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { |
197 | perror("Failed to send flush request\n"); | 200 | bb_perror_msg("failed to send flush request"); |
198 | return -1; | 201 | return -1; |
199 | } | 202 | } |
200 | filter.flushp = 0; | 203 | filter.flushp = 0; |
@@ -341,7 +344,7 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, | |||
341 | if (rta_tb[IFA_CACHEINFO]) { | 344 | if (rta_tb[IFA_CACHEINFO]) { |
342 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); | 345 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); |
343 | char buf[128]; | 346 | char buf[128]; |
344 | fprintf(fp, "%s", _SL_); | 347 | fputc(_SL_, fp); |
345 | if (ci->ifa_valid == 0xFFFFFFFFU) | 348 | if (ci->ifa_valid == 0xFFFFFFFFU) |
346 | sprintf(buf, "valid_lft forever"); | 349 | sprintf(buf, "valid_lft forever"); |
347 | else | 350 | else |
@@ -352,7 +355,7 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, | |||
352 | sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered); | 355 | sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered); |
353 | fprintf(fp, " %s", buf); | 356 | fprintf(fp, " %s", buf); |
354 | } | 357 | } |
355 | fprintf(fp, "\n"); | 358 | fputc('\n', fp); |
356 | fflush(fp); | 359 | fflush(fp); |
357 | return 0; | 360 | return 0; |
358 | } | 361 | } |
@@ -366,7 +369,7 @@ struct nlmsg_list | |||
366 | 369 | ||
367 | static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) | 370 | static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) |
368 | { | 371 | { |
369 | for ( ;ainfo ; ainfo = ainfo->next) { | 372 | for (; ainfo; ainfo = ainfo->next) { |
370 | struct nlmsghdr *n = &ainfo->h; | 373 | struct nlmsghdr *n = &ainfo->h; |
371 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | 374 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
372 | 375 | ||
@@ -412,6 +415,7 @@ static void ipaddr_reset_filter(int _oneline) | |||
412 | filter.oneline = _oneline; | 415 | filter.oneline = _oneline; |
413 | } | 416 | } |
414 | 417 | ||
418 | /* Return value becomes exitcode. It's okay to not return at all */ | ||
415 | int ipaddr_list_or_flush(int argc, char **argv, int flush) | 419 | int ipaddr_list_or_flush(int argc, char **argv, int flush) |
416 | { | 420 | { |
417 | static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 }; | 421 | static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 }; |
@@ -431,12 +435,10 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush) | |||
431 | 435 | ||
432 | if (flush) { | 436 | if (flush) { |
433 | if (argc <= 0) { | 437 | if (argc <= 0) { |
434 | bb_error_msg(bb_msg_requires_arg, "flush"); | 438 | bb_error_msg_and_die(bb_msg_requires_arg, "flush"); |
435 | return -1; | ||
436 | } | 439 | } |
437 | if (filter.family == AF_PACKET) { | 440 | if (filter.family == AF_PACKET) { |
438 | bb_error_msg("cannot flush link addresses"); | 441 | bb_error_msg_and_die("cannot flush link addresses"); |
439 | return -1; | ||
440 | } | 442 | } |
441 | } | 443 | } |
442 | 444 | ||
@@ -498,8 +500,7 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush) | |||
498 | if (filter_dev) { | 500 | if (filter_dev) { |
499 | filter.ifindex = ll_name_to_index(filter_dev); | 501 | filter.ifindex = ll_name_to_index(filter_dev); |
500 | if (filter.ifindex <= 0) { | 502 | if (filter.ifindex <= 0) { |
501 | bb_error_msg("device \"%s\" does not exist", filter_dev); | 503 | bb_error_msg_and_die("device \"%s\" does not exist", filter_dev); |
502 | return -1; | ||
503 | } | 504 | } |
504 | } | 505 | } |
505 | 506 | ||
@@ -513,20 +514,17 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush) | |||
513 | 514 | ||
514 | for (;;) { | 515 | for (;;) { |
515 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { | 516 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
516 | perror("Cannot send dump request"); | 517 | bb_perror_msg_and_die("cannot send dump request"); |
517 | exit(1); | ||
518 | } | 518 | } |
519 | filter.flushed = 0; | 519 | filter.flushed = 0; |
520 | if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) { | 520 | if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) { |
521 | fprintf(stderr, "Flush terminated\n"); | 521 | bb_error_msg_and_die("flush terminated"); |
522 | exit(1); | ||
523 | } | 522 | } |
524 | if (filter.flushed == 0) { | 523 | if (filter.flushed == 0) { |
525 | fflush(stdout); | ||
526 | return 0; | 524 | return 0; |
527 | } | 525 | } |
528 | if (flush_update() < 0) | 526 | if (flush_update() < 0) |
529 | exit(1); | 527 | return 1; |
530 | } | 528 | } |
531 | } | 529 | } |
532 | 530 | ||
@@ -601,16 +599,16 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush) | |||
601 | } | 599 | } |
602 | } | 600 | } |
603 | 601 | ||
604 | for (l=linfo; l; l = l->next) { | 602 | for (l = linfo; l; l = l->next) { |
605 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { | 603 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { |
606 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); | 604 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
607 | if (filter.family != AF_PACKET) | 605 | if (filter.family != AF_PACKET) |
608 | print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); | 606 | print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); |
609 | } | 607 | } |
610 | fflush(stdout); | 608 | fflush(stdout); /* why? */ |
611 | } | 609 | } |
612 | 610 | ||
613 | exit(0); | 611 | return 0; |
614 | } | 612 | } |
615 | 613 | ||
616 | static int default_scope(inet_prefix *lcl) | 614 | static int default_scope(inet_prefix *lcl) |
@@ -622,6 +620,7 @@ static int default_scope(inet_prefix *lcl) | |||
622 | return 0; | 620 | return 0; |
623 | } | 621 | } |
624 | 622 | ||
623 | /* Return value becomes exitcode. It's okay to not return at all */ | ||
625 | static int ipaddr_modify(int cmd, int argc, char **argv) | 624 | static int ipaddr_modify(int cmd, int argc, char **argv) |
626 | { | 625 | { |
627 | static const char *const option[] = { | 626 | static const char *const option[] = { |
@@ -763,8 +762,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv) | |||
763 | inet_prefix brd; | 762 | inet_prefix brd; |
764 | int i; | 763 | int i; |
765 | if (req.ifa.ifa_family != AF_INET) { | 764 | if (req.ifa.ifa_family != AF_INET) { |
766 | bb_error_msg("broadcast can be set only for IPv4 addresses"); | 765 | bb_error_msg_and_die("broadcast can be set only for IPv4 addresses"); |
767 | return -1; | ||
768 | } | 766 | } |
769 | brd = peer; | 767 | brd = peer; |
770 | if (brd.bitlen <= 30) { | 768 | if (brd.bitlen <= 30) { |
@@ -786,17 +784,18 @@ static int ipaddr_modify(int cmd, int argc, char **argv) | |||
786 | 784 | ||
787 | ll_init_map(&rth); | 785 | ll_init_map(&rth); |
788 | 786 | ||
789 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { | 787 | req.ifa.ifa_index = ll_name_to_index(d); |
790 | bb_error_msg("cannot find device \"%s\"", d); | 788 | if (req.ifa.ifa_index == 0) { |
791 | return -1; | 789 | bb_error_msg_and_die("cannot find device \"%s\"", d); |
792 | } | 790 | } |
793 | 791 | ||
794 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | 792 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
795 | exit(2); | 793 | return 2; |
796 | 794 | ||
797 | exit(0); | 795 | return 0; |
798 | } | 796 | } |
799 | 797 | ||
798 | /* Return value becomes exitcode. It's okay to not return at all */ | ||
800 | int do_ipaddr(int argc, char **argv) | 799 | int do_ipaddr(int argc, char **argv) |
801 | { | 800 | { |
802 | static const char *const commands[] = { | 801 | static const char *const commands[] = { |