Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (17 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/networking/libiproute/ipaddress.c

    r821 r1765  
     1/* vi: set sw=4 ts=4: */
    12/*
    23 * ipaddress.c      "ip address".
     
    1011 */
    1112
    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>
    1615#include <fnmatch.h>
    17 #include <string.h>
    18 #include <unistd.h>
    19 
    2016#include <net/if.h>
    2117#include <net/if_arp.h>
    2218
     19#include "ip_common.h"  /* #include "libbb.h" is inside */
    2320#include "rt_names.h"
    2421#include "utils.h"
    25 #include "ip_common.h"
    26 
    27 
    28 static struct
    29 {
     22
     23
     24typedef struct filter_t {
    3025    int ifindex;
    3126    int family;
     
    4237    int flushe;
    4338    struct rtnl_handle *rth;
    44 } filter;
     39} filter_t;
     40
     41#define filter (*(filter_t*)&bb_common_bufsiz1)
     42
    4543
    4644static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
     
    4947    flags &= ~IFF_RUNNING;
    5048#define _PF(f) if (flags&IFF_##f) { \
    51           flags &= ~IFF_##f ; \
     49          flags &= ~IFF_##f; \
    5250          fprintf(fp, #f "%s", flags ? "," : ""); }
    5351    _PF(LOOPBACK);
     
    8684
    8785    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) {
    9188        close(s);
    9289        return;
     
    9996
    10097static 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)
    10299{
    103100    FILE *fp = (FILE*)arg;
     
    125122        return -1;
    126123    }
    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    }
    131130
    132131    if (n->nlmsg_type == RTM_DELLINK)
     
    166165    if (!filter.family || filter.family == AF_PACKET) {
    167166        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)));
    170168
    171169        if (tb[IFLA_ADDRESS]) {
     
    186184        }
    187185    }
    188     fprintf(fp, "\n");
     186    fputc('\n', fp);
    189187    fflush(fp);
    190188    return 0;
     
    194192{
    195193    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");
    197195        return -1;
    198196    }
     
    341339        struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
    342340        char buf[128];
    343         fprintf(fp, "%s", _SL_);
     341        fputc(_SL_, fp);
    344342        if (ci->ifa_valid == 0xFFFFFFFFU)
    345343            sprintf(buf, "valid_lft forever");
     
    352350        fprintf(fp, "       %s", buf);
    353351    }
    354     fprintf(fp, "\n");
     352    fputc('\n', fp);
    355353    fflush(fp);
    356354    return 0;
     
    366364static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
    367365{
    368     for ( ;ainfo ; ainfo = ainfo->next) {
     366    for (; ainfo; ainfo = ainfo->next) {
    369367        struct nlmsghdr *n = &ainfo->h;
    370368        struct ifaddrmsg *ifa = NLMSG_DATA(n);
     
    412410}
    413411
     412/* Return value becomes exitcode. It's okay to not return at all */
    414413int ipaddr_list_or_flush(int argc, char **argv, int flush)
    415414{
    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";
    417416
    418417    struct nlmsg_list *linfo = NULL;
     
    431430    if (flush) {
    432431        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");
    435433        }
    436434        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");
    439436        }
    440437    }
    441438
    442439    while (argc > 0) {
    443         const int option_num = compare_string_array(option, *argv);
     440        const int option_num = index_in_strings(option, *argv);
    444441        switch (option_num) {
    445442            case 0: /* to */
     
    484481    }
    485482
    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);
    496487
    497488    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);
    503490    }
    504491
     
    512499
    513500        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);
    518502            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);
    523504            if (filter.flushed == 0) {
    524                 fflush(stdout);
    525505                return 0;
    526506            }
    527507            if (flush_update() < 0)
    528                 exit(1);
     508                return 1;
    529509        }
    530510    }
    531511
    532512    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);
    540515    }
    541516
     
    601576    }
    602577
    603     for (l=linfo; l; l = l->next) {
     578    for (l = linfo; l; l = l->next) {
    604579        if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
    605580            struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
     
    607582                print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
    608583        }
    609         fflush(stdout);
    610     }
    611 
    612     exit(0);
     584        fflush(stdout); /* why? */
     585    }
     586
     587    return 0;
    613588}
    614589
     
    616591{
    617592    if (lcl->family == AF_INET) {
    618         if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
     593        if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
    619594            return RT_SCOPE_HOST;
    620595    }
     
    622597}
    623598
     599/* Return value becomes exitcode. It's okay to not return at all */
    624600static int ipaddr_modify(int cmd, int argc, char **argv)
    625601{
    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";
    631605    struct rtnl_handle rth;
    632606    struct {
    633         struct nlmsghdr     n;
    634         struct ifaddrmsg    ifa;
    635         char            buf[256];
     607        struct nlmsghdr  n;
     608        struct ifaddrmsg ifa;
     609        char             buf[256];
    636610    } req;
    637     char  *d = NULL;
    638     char  *l = NULL;
     611    char *d = NULL;
     612    char *l = NULL;
    639613    inet_prefix lcl;
    640614    inet_prefix peer;
     
    643617    int brd_len = 0;
    644618    int any_len = 0;
    645     int scoped = 0;
     619    bool scoped = 0;
    646620
    647621    memset(&req, 0, sizeof(req));
     
    653627
    654628    while (argc > 0) {
    655         const int option_num = compare_string_array(option, *argv);
     629        const int option_num = index_in_strings(option, *argv);
    656630        switch (option_num) {
    657631            case 0: /* peer */
     
    678652                    duparg("broadcast", *argv);
    679653                }
    680                 if (strcmp(*argv, "+") == 0) {
     654                if (LONE_CHAR(*argv, '+')) {
    681655                    brd_len = -1;
    682656                }
    683                 else if (strcmp(*argv, "-") == 0) {
     657                else if (LONE_DASH(*argv)) {
    684658                    brd_len = -2;
    685659                } else {
     
    748722        return -1;
    749723    }
    750     if (l && matches(d, l) != 0) {
     724    if (l && strncmp(d, l, strlen(d)) != 0) {
    751725        bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
    752726    }
     
    763737        int i;
    764738        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");
    767740        }
    768741        brd = peer;
     
    781754        req.ifa.ifa_scope = default_scope(&lcl);
    782755
    783     if (rtnl_open(&rth, 0) < 0)
    784         exit(1);
     756    xrtnl_open(&rth);
    785757
    786758    ll_init_map(&rth);
    787759
    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);
    792761
    793762    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 */
    799769int do_ipaddr(int argc, char **argv)
    800770{
    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 */
    806775
    807776    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.