- fix ip route rejecting dotted quads as prefix
[oweals/busybox.git] / networking / libiproute / ipaddress.c
index ee9825ba2e76d9bbe93d54d3ab17c8a0c3e7dc7c..288dccae72dc2f41efe2948072bdfc5bbe1de906 100644 (file)
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
 /*
  * ipaddress.c         "ip address".
  *
  *     Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
  */
 
-#include "libbb.h"
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-
 #include <fnmatch.h>
-#include <string.h>
-#include <unistd.h>
-
 #include <net/if.h>
 #include <net/if_arp.h>
 
+#include "ip_common.h"  /* #include "libbb.h" is inside */
 #include "rt_names.h"
 #include "utils.h"
-#include "ip_common.h"
 
+#ifndef IFF_LOWER_UP
+/* from linux/if.h */
+#define IFF_LOWER_UP   0x10000         /* driver signals L1 up*/
+#endif
 
-static struct
-{
-       int ifindex;
-       int family;
-       int oneline;
-       int showqueue;
-       inet_prefix pfx;
-       int scope, scopemask;
-       int flags, flagmask;
-       int up;
+typedef struct filter_t {
        char *label;
-       int flushed;
        char *flushb;
+       struct rtnl_handle *rth;
+       int scope, scopemask;
+       int flags, flagmask;
        int flushp;
        int flushe;
-       struct rtnl_handle *rth;
-} filter;
+       int ifindex;
+       family_t family;
+       smallint showqueue;
+       smallint oneline;
+       smallint up;
+       smallint flushed;
+       inet_prefix pfx;
+} filter_t;
+
+#define filter (*(filter_t*)&bb_common_bufsiz1)
 
-static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
+
+static void print_link_flags(unsigned flags, unsigned mdown)
 {
-       fprintf(fp, "<");
+       static const int flag_masks[] = {
+               IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT,
+               IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP };
+       static const char flag_labels[] ALIGN1 =
+               "LOOPBACK\0""BROADCAST\0""POINTOPOINT\0"
+               "MULTICAST\0""NOARP\0""UP\0""LOWER_UP\0";
+
+       bb_putchar('<');
        flags &= ~IFF_RUNNING;
-#define _PF(f) if (flags&IFF_##f) { \
-                 flags &= ~IFF_##f ; \
-                 fprintf(fp, #f "%s", flags ? "," : ""); }
-       _PF(LOOPBACK);
-       _PF(BROADCAST);
-       _PF(POINTOPOINT);
-       _PF(MULTICAST);
-       _PF(NOARP);
 #if 0
        _PF(ALLMULTI);
        _PF(PROMISC);
@@ -66,13 +65,12 @@ static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
        _PF(PORTSEL);
        _PF(NOTRAILERS);
 #endif
-       _PF(UP);
-#undef _PF
+       flags = print_flags_separated(flag_masks, flag_labels, flags, ",");
        if (flags)
-               fprintf(fp, "%x", flags);
+               printf("%x", flags);
        if (mdown)
-               fprintf(fp, ",M-DOWN");
-       fprintf(fp, "> ");
+               printf(",M-DOWN");
+       printf("> ");
 }
 
 static void print_queuelen(char *name)
@@ -85,9 +83,8 @@ static void print_queuelen(char *name)
                return;
 
        memset(&ifr, 0, sizeof(ifr));
-       strcpy(ifr.ifr_name, name);
-       if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
-               perror("SIOCGIFXQLEN");
+       strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+       if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
                close(s);
                return;
        }
@@ -97,10 +94,8 @@ static void print_queuelen(char *name)
                printf("qlen %d", ifr.ifr_qlen);
 }
 
-static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
-               struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
+static int print_linkinfo(const struct nlmsghdr *n)
 {
-       FILE *fp = (FILE*)arg;
        struct ifinfomsg *ifi = NLMSG_DATA(n);
        struct rtattr * tb[IFLA_MAX+1];
        int len = n->nlmsg_len;
@@ -115,7 +110,7 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
 
        if (filter.ifindex && ifi->ifi_index != filter.ifindex)
                return 0;
-       if (filter.up && !(ifi->ifi_flags&IFF_UP))
+       if (filter.up && !(ifi->ifi_flags & IFF_UP))
                return 0;
 
        memset(tb, 0, sizeof(tb));
@@ -124,40 +119,42 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
                bb_error_msg("nil ifname");
                return -1;
        }
-       if (filter.label &&
-           (!filter.family || filter.family == AF_PACKET) &&
-           fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
+       if (filter.label
+        && (!filter.family || filter.family == AF_PACKET)
+        && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
+       ) {
                return 0;
+       }
 
        if (n->nlmsg_type == RTM_DELLINK)
-               fprintf(fp, "Deleted ");
+               printf("Deleted ");
 
-       fprintf(fp, "%d: %s", ifi->ifi_index,
+       printf("%d: %s", ifi->ifi_index,
                tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
 
        if (tb[IFLA_LINK]) {
                SPRINT_BUF(b1);
                int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
                if (iflink == 0)
-                       fprintf(fp, "@NONE: ");
+                       printf("@NONE: ");
                else {
-                       fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
+                       printf("@%s: ", ll_idx_n2a(iflink, b1));
                        m_flag = ll_index_to_flags(iflink);
                        m_flag = !(m_flag & IFF_UP);
                }
        } else {
-               fprintf(fp, ": ");
+               printf(": ");
        }
-       print_link_flags(fp, ifi->ifi_flags, m_flag);
+       print_link_flags(ifi->ifi_flags, m_flag);
 
        if (tb[IFLA_MTU])
-               fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
+               printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
        if (tb[IFLA_QDISC])
-               fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
+               printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
 #ifdef IFLA_MASTER
        if (tb[IFLA_MASTER]) {
                SPRINT_BUF(b1);
-               fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
+               printf("master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
        }
 #endif
        if (filter.showqueue)
@@ -165,45 +162,43 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
 
        if (!filter.family || filter.family == AF_PACKET) {
                SPRINT_BUF(b1);
-               fprintf(fp, "%s", _SL_);
-               fprintf(fp, "    link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
+               printf("%c    link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
 
                if (tb[IFLA_ADDRESS]) {
-                       fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
+                       fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
                                                      RTA_PAYLOAD(tb[IFLA_ADDRESS]),
                                                      ifi->ifi_type,
-                                                     b1, sizeof(b1)));
+                                                     b1, sizeof(b1)), stdout);
                }
                if (tb[IFLA_BROADCAST]) {
-                       if (ifi->ifi_flags&IFF_POINTOPOINT)
-                               fprintf(fp, " peer ");
+                       if (ifi->ifi_flags & IFF_POINTOPOINT)
+                               printf(" peer ");
                        else
-                               fprintf(fp, " brd ");
-                       fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
+                               printf(" brd ");
+                       fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
                                                      RTA_PAYLOAD(tb[IFLA_BROADCAST]),
                                                      ifi->ifi_type,
-                                                     b1, sizeof(b1)));
+                                                     b1, sizeof(b1)), stdout);
                }
        }
-       fprintf(fp, "\n");
-       fflush(fp);
+       bb_putchar('\n');
+       /*fflush(stdout);*/
        return 0;
 }
 
 static int flush_update(void)
 {
        if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
-               perror("Failed to send flush request\n");
+               bb_perror_msg("failed to send flush request");
                return -1;
        }
        filter.flushp = 0;
        return 0;
 }
 
-static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
-               struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
+static int print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
+               struct nlmsghdr *n, void *arg UNUSED_PARAM)
 {
-       FILE *fp = (FILE*)arg;
        struct ifaddrmsg *ifa = NLMSG_DATA(n);
        int len = n->nlmsg_len;
        struct rtattr * rta_tb[IFA_MAX+1];
@@ -231,9 +226,9 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
 
        if (filter.ifindex && filter.ifindex != ifa->ifa_index)
                return 0;
-       if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
+       if ((filter.scope ^ ifa->ifa_scope) & filter.scopemask)
                return 0;
-       if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
+       if ((filter.flags ^ ifa->ifa_flags) & filter.flagmask)
                return 0;
        if (filter.label) {
                const char *label;
@@ -267,33 +262,33 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
                fn->nlmsg_flags = NLM_F_REQUEST;
                fn->nlmsg_seq = ++filter.rth->seq;
                filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
-               filter.flushed++;
+               filter.flushed = 1;
                return 0;
        }
 
        if (n->nlmsg_type == RTM_DELADDR)
-               fprintf(fp, "Deleted ");
+               printf("Deleted ");
 
        if (filter.oneline)
-               fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
+               printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
        if (ifa->ifa_family == AF_INET)
-               fprintf(fp, "    inet ");
+               printf("    inet ");
        else if (ifa->ifa_family == AF_INET6)
-               fprintf(fp, "    inet6 ");
+               printf("    inet6 ");
        else
-               fprintf(fp, "    family %d ", ifa->ifa_family);
+               printf("    family %d ", ifa->ifa_family);
 
        if (rta_tb[IFA_LOCAL]) {
-               fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
+               fputs(rt_addr_n2a(ifa->ifa_family,
                                              RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
                                              RTA_DATA(rta_tb[IFA_LOCAL]),
-                                             abuf, sizeof(abuf)));
+                                             abuf, sizeof(abuf)), stdout);
 
                if (rta_tb[IFA_ADDRESS] == NULL ||
                    memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
-                       fprintf(fp, "/%d ", ifa->ifa_prefixlen);
+                       printf("/%d ", ifa->ifa_prefixlen);
                } else {
-                       fprintf(fp, " peer %s/%d ",
+                       printf(" peer %s/%d ",
                                rt_addr_n2a(ifa->ifa_family,
                                            RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
                                            RTA_DATA(rta_tb[IFA_ADDRESS]),
@@ -303,44 +298,44 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
        }
 
        if (rta_tb[IFA_BROADCAST]) {
-               fprintf(fp, "brd %s ",
+               printf("brd %s ",
                        rt_addr_n2a(ifa->ifa_family,
                                    RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
                                    RTA_DATA(rta_tb[IFA_BROADCAST]),
                                    abuf, sizeof(abuf)));
        }
        if (rta_tb[IFA_ANYCAST]) {
-               fprintf(fp, "any %s ",
+               printf("any %s ",
                        rt_addr_n2a(ifa->ifa_family,
                                    RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
                                    RTA_DATA(rta_tb[IFA_ANYCAST]),
                                    abuf, sizeof(abuf)));
        }
-       fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
-       if (ifa->ifa_flags&IFA_F_SECONDARY) {
+       printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
+       if (ifa->ifa_flags & IFA_F_SECONDARY) {
                ifa->ifa_flags &= ~IFA_F_SECONDARY;
-               fprintf(fp, "secondary ");
+               printf("secondary ");
        }
-       if (ifa->ifa_flags&IFA_F_TENTATIVE) {
+       if (ifa->ifa_flags & IFA_F_TENTATIVE) {
                ifa->ifa_flags &= ~IFA_F_TENTATIVE;
-               fprintf(fp, "tentative ");
+               printf("tentative ");
        }
-       if (ifa->ifa_flags&IFA_F_DEPRECATED) {
+       if (ifa->ifa_flags & IFA_F_DEPRECATED) {
                ifa->ifa_flags &= ~IFA_F_DEPRECATED;
-               fprintf(fp, "deprecated ");
+               printf("deprecated ");
        }
-       if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
-               fprintf(fp, "dynamic ");
+       if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
+               printf("dynamic ");
        } else
                ifa->ifa_flags &= ~IFA_F_PERMANENT;
        if (ifa->ifa_flags)
-               fprintf(fp, "flags %02x ", ifa->ifa_flags);
+               printf("flags %02x ", ifa->ifa_flags);
        if (rta_tb[IFA_LABEL])
-               fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
+               fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
        if (rta_tb[IFA_CACHEINFO]) {
                struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
                char buf[128];
-               fprintf(fp, "%s", _SL_);
+               bb_putchar(_SL_);
                if (ci->ifa_valid == 0xFFFFFFFFU)
                        sprintf(buf, "valid_lft forever");
                else
@@ -349,10 +344,10 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
                        sprintf(buf+strlen(buf), " preferred_lft forever");
                else
                        sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
-               fprintf(fp, "       %s", buf);
+               printf("       %s", buf);
        }
-       fprintf(fp, "\n");
-       fflush(fp);
+       bb_putchar('\n');
+       /*fflush(stdout);*/
        return 0;
 }
 
@@ -363,9 +358,9 @@ struct nlmsg_list
        struct nlmsghdr   h;
 };
 
-static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
+static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
 {
-       for ( ;ainfo ;  ainfo = ainfo->next) {
+       for (; ainfo; ainfo = ainfo->next) {
                struct nlmsghdr *n = &ainfo->h;
                struct ifaddrmsg *ifa = NLMSG_DATA(n);
 
@@ -379,13 +374,13 @@ static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *
                    (filter.family && filter.family != ifa->ifa_family))
                        continue;
 
-               print_addrinfo(NULL, n, fp);
+               print_addrinfo(NULL, n, NULL);
        }
        return 0;
 }
 
 
-static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
+static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 {
        struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
        struct nlmsg_list *h;
@@ -398,7 +393,8 @@ static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
        memcpy(&h->h, n, n->nlmsg_len);
        h->next = NULL;
 
-       for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
+       for (lp = linfo; *lp; lp = &(*lp)->next)
+               continue;
        *lp = h;
 
        ll_remember_index(who, n, NULL);
@@ -411,9 +407,10 @@ static void ipaddr_reset_filter(int _oneline)
        filter.oneline = _oneline;
 }
 
-int ipaddr_list_or_flush(int argc, char **argv, int flush)
+/* Return value becomes exitcode. It's okay to not return at all */
+int ipaddr_list_or_flush(char **argv, int flush)
 {
-       static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 };
+       static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
 
        struct nlmsg_list *linfo = NULL;
        struct nlmsg_list *ainfo = NULL;
@@ -429,18 +426,16 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush)
                filter.family = preferred_family;
 
        if (flush) {
-               if (argc <= 0) {
-                       bb_error_msg(bb_msg_requires_arg, "flush");
-                       return -1;
+               if (!*argv) {
+                       bb_error_msg_and_die(bb_msg_requires_arg, "flush");
                }
                if (filter.family == AF_PACKET) {
-                       bb_error_msg("Cannot flush link addresses.");
-                       return -1;
+                       bb_error_msg_and_die("cannot flush link addresses");
                }
        }
 
-       while (argc > 0) {
-               const int option_num = compare_string_array(option, *argv);
+       while (*argv) {
+               const int option_num = index_in_strings(option, *argv);
                switch (option_num) {
                        case 0: /* to */
                                NEXT_ARG();
@@ -480,26 +475,15 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush)
                                filter_dev = *argv;
                }
                argv++;
-               argc--;
        }
 
-       if (rtnl_open(&rth, 0) < 0)
-               exit(1);
+       xrtnl_open(&rth);
 
-       if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
-               bb_perror_msg_and_die("Cannot send dump request");
-       }
-
-       if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
-               bb_error_msg_and_die("Dump terminated");
-       }
+       xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
+       xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
 
        if (filter_dev) {
-               filter.ifindex = ll_name_to_index(filter_dev);
-               if (filter.ifindex <= 0) {
-                       bb_error_msg("Device \"%s\" does not exist", filter_dev);
-                       return -1;
-               }
+               filter.ifindex = xll_name_to_index(filter_dev);
        }
 
        if (flush) {
@@ -511,57 +495,45 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush)
                filter.rth = &rth;
 
                for (;;) {
-                       if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
-                               perror("Cannot send dump request");
-                               exit(1);
-                       }
+                       xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
                        filter.flushed = 0;
-                       if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
-                               fprintf(stderr, "Flush terminated\n");
-                               exit(1);
-                       }
+                       xrtnl_dump_filter(&rth, print_addrinfo, NULL);
                        if (filter.flushed == 0) {
-                               fflush(stdout);
                                return 0;
                        }
                        if (flush_update() < 0)
-                               exit(1);
+                               return 1;
                }
        }
 
        if (filter.family != AF_PACKET) {
-               if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
-                       bb_perror_msg_and_die("Cannot send dump request");
-               }
-
-               if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
-                       bb_error_msg_and_die("Dump terminated");
-               }
+               xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
+               xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
        }
 
 
        if (filter.family && filter.family != AF_PACKET) {
                struct nlmsg_list **lp;
-               lp=&linfo;
+               lp = &linfo;
 
                if (filter.oneline)
                        no_link = 1;
 
-               while ((l=*lp)!=NULL) {
+               while ((l = *lp) != NULL) {
                        int ok = 0;
                        struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
                        struct nlmsg_list *a;
 
-                       for (a=ainfo; a; a=a->next) {
+                       for (a = ainfo; a; a = a->next) {
                                struct nlmsghdr *n = &a->h;
                                struct ifaddrmsg *ifa = NLMSG_DATA(n);
 
                                if (ifa->ifa_index != ifi->ifi_index ||
                                    (filter.family && filter.family != ifa->ifa_family))
                                        continue;
-                               if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
+                               if ((filter.scope ^ ifa->ifa_scope) & filter.scopemask)
                                        continue;
-                               if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
+                               if ((filter.flags ^ ifa->ifa_flags) & filter.flagmask)
                                        continue;
                                if (filter.pfx.family || filter.label) {
                                        struct rtattr *tb[IFA_MAX+1];
@@ -600,49 +572,47 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush)
                }
        }
 
-       for (l=linfo; l; l = l->next) {
-               if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
+       for (l = linfo; l; l = l->next) {
+               if (no_link || print_linkinfo(&l->h) == 0) {
                        struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
                        if (filter.family != AF_PACKET)
-                               print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
+                               print_selected_addrinfo(ifi->ifi_index, ainfo);
                }
-               fflush(stdout);
        }
 
-       exit(0);
+       return 0;
 }
 
 static int default_scope(inet_prefix *lcl)
 {
        if (lcl->family == AF_INET) {
-               if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
+               if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
                        return RT_SCOPE_HOST;
        }
        return 0;
 }
 
-static int ipaddr_modify(int cmd, int argc, char **argv)
+/* Return value becomes exitcode. It's okay to not return at all */
+static int ipaddr_modify(int cmd, char **argv)
 {
-       static const char *const option[] = {
-               "peer", "remote", "broadcast", "brd",
-               "anycast", "scope", "dev", "label", "local", 0
-       };
-
+       static const char option[] ALIGN1 =
+               "peer\0""remote\0""broadcast\0""brd\0"
+               "anycast\0""scope\0""dev\0""label\0""local\0";
        struct rtnl_handle rth;
        struct {
-               struct nlmsghdr         n;
-               struct ifaddrmsg        ifa;
-               char                    buf[256];
+               struct nlmsghdr  n;
+               struct ifaddrmsg ifa;
+               char             buf[256];
        } req;
-       char  *d = NULL;
-       char  *l = NULL;
+       char *d = NULL;
+       char *l = NULL;
        inet_prefix lcl;
        inet_prefix peer;
        int local_len = 0;
        int peer_len = 0;
        int brd_len = 0;
        int any_len = 0;
-       int scoped = 0;
+       bool scoped = 0;
 
        memset(&req, 0, sizeof(req));
 
@@ -651,8 +621,8 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
        req.n.nlmsg_type = cmd;
        req.ifa.ifa_family = preferred_family;
 
-       while (argc > 0) {
-               const int option_num = compare_string_array(option, *argv);
+       while (*argv) {
+               const int option_num = index_in_strings(option, *argv);
                switch (option_num) {
                        case 0: /* peer */
                        case 1: /* remote */
@@ -677,10 +647,9 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
                                if (brd_len) {
                                        duparg("broadcast", *argv);
                                }
-                               if (strcmp(*argv, "+") == 0) {
+                               if (LONE_CHAR(*argv, '+')) {
                                        brd_len = -1;
-                               }
-                               else if (strcmp(*argv, "-") == 0) {
+                               } else if (LONE_DASH(*argv)) {
                                        brd_len = -2;
                                } else {
                                        get_addr(&addr, *argv, req.ifa.ifa_family);
@@ -739,15 +708,14 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
                                addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
                                local_len = lcl.bytelen;
                }
-               argc--;
                argv++;
        }
 
        if (d == NULL) {
-               bb_error_msg(bb_msg_requires_arg,"\"dev\"");
+               bb_error_msg(bb_msg_requires_arg, "\"dev\"");
                return -1;
        }
-       if (l && matches(d, l) != 0) {
+       if (l && strncmp(d, l, strlen(d)) != 0) {
                bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
        }
 
@@ -762,8 +730,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
                inet_prefix brd;
                int i;
                if (req.ifa.ifa_family != AF_INET) {
-                       bb_error_msg("Broadcast can be set only for IPv4 addresses");
-                       return -1;
+                       bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
                }
                brd = peer;
                if (brd.bitlen <= 30) {
@@ -780,44 +747,38 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
        if (!scoped && cmd != RTM_DELADDR)
                req.ifa.ifa_scope = default_scope(&lcl);
 
-       if (rtnl_open(&rth, 0) < 0)
-               exit(1);
+       xrtnl_open(&rth);
 
        ll_init_map(&rth);
 
-       if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
-               bb_error_msg("Cannot find device \"%s\"", d);
-               return -1;
-       }
+       req.ifa.ifa_index = xll_name_to_index(d);
 
        if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
-               exit(2);
+               return 2;
 
-       exit(0);
+       return 0;
 }
 
-int do_ipaddr(int argc, char **argv)
+/* Return value becomes exitcode. It's okay to not return at all */
+int do_ipaddr(char **argv)
 {
-       static const char *const commands[] = {
-               "add", "delete", "list", "show", "lst", "flush", 0
-       };
+       static const char commands[] ALIGN1 =
+               "add\0""delete\0""list\0""show\0""lst\0""flush\0";
 
-       int command_num = 2;
+       int command_num = 2; /* default command is list */
 
        if (*argv) {
-               command_num = compare_string_array(commands, *argv);
-       }
-       switch (command_num) {
-               case 0: /* add */
-                       return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
-               case 1: /* delete */
-                       return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
-               case 2: /* list */
-               case 3: /* show */
-               case 4: /* lst */
-                       return ipaddr_list_or_flush(argc-1, argv+1, 0);
-               case 5: /* flush */
-                       return ipaddr_list_or_flush(argc-1, argv+1, 1);
-       }
-       bb_error_msg_and_die("Unknown command %s", *argv);
+               command_num = index_in_substrings(commands, *argv);
+               if (command_num < 0 || command_num > 5)
+                       bb_error_msg_and_die("unknown command %s", *argv);
+               argv++;
+       }
+       if (command_num == 0) /* add */
+               return ipaddr_modify(RTM_NEWADDR, argv);
+       if (command_num == 1) /* delete */
+               return ipaddr_modify(RTM_DELADDR, argv);
+       if (command_num == 5) /* flush */
+               return ipaddr_list_or_flush(argv, 1);
+       /* 2 == list, 3 == show, 4 == lst */
+       return ipaddr_list_or_flush(argv, 0);
 }