Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/networking/libiproute/libnetlink.c

    r1765 r2725  
    11/* vi: set sw=4 ts=4: */
    22/*
    3  * libnetlink.c RTnetlink service routines.
     3 * This program is free software; you can redistribute it and/or
     4 * modify it under the terms of the GNU General Public License
     5 * as published by the Free Software Foundation; either version
     6 * 2 of the License, or (at your option) any later version.
    47 *
    5  *      This program is free software; you can redistribute it and/or
    6  *      modify it under the terms of the GNU General Public License
    7  *      as published by the Free Software Foundation; either version
    8  *      2 of the License, or (at your option) any later version.
    9  *
    10  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
    11  *
     8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
    129 */
    1310
     
    1815#include "libnetlink.h"
    1916
    20 void rtnl_close(struct rtnl_handle *rth)
    21 {
    22     close(rth->fd);
    23 }
    24 
    25 int xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
     17void FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
    2618{
    2719    socklen_t addr_len;
    2820
    29     memset(rth, 0, sizeof(rth));
    30 
     21    memset(rth, 0, sizeof(*rth));
    3122    rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
    32 
    33     memset(&rth->local, 0, sizeof(rth->local));
    3423    rth->local.nl_family = AF_NETLINK;
    3524    /*rth->local.nl_groups = subscriptions;*/
     
    3726    xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
    3827    addr_len = sizeof(rth->local);
     28    getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len);
     29
     30/* too much paranoia
    3931    if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
    40         bb_perror_msg_and_die("cannot getsockname");
     32        bb_perror_msg_and_die("getsockname");
    4133    if (addr_len != sizeof(rth->local))
    4234        bb_error_msg_and_die("wrong address length %d", addr_len);
    4335    if (rth->local.nl_family != AF_NETLINK)
    4436        bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
     37*/
    4538    rth->seq = time(NULL);
    46     return 0;
    47 }
    48 
    49 int xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
     39}
     40
     41int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
    5042{
    5143    struct {
     
    5345        struct rtgenmsg g;
    5446    } req;
    55     struct sockaddr_nl nladdr;
    56 
    57     memset(&nladdr, 0, sizeof(nladdr));
    58     nladdr.nl_family = AF_NETLINK;
    5947
    6048    req.nlh.nlmsg_len = sizeof(req);
     
    6553    req.g.rtgen_family = family;
    6654
    67     return xsendto(rth->fd, (void*)&req, sizeof(req),
    68                  (struct sockaddr*)&nladdr, sizeof(nladdr));
    69 }
    70 
    71 int rtnl_send(struct rtnl_handle *rth, char *buf, int len)
     55    return rtnl_send(rth, (void*)&req, sizeof(req));
     56}
     57
     58int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
    7259{
    7360    struct sockaddr_nl nladdr;
     
    7966}
    8067
    81 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
     68int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
    8269{
    8370    struct nlmsghdr nlh;
     
    8673    struct msghdr msg = {
    8774        (void*)&nladdr, sizeof(nladdr),
    88         iov,    2,
    89         NULL,   0,
     75        iov,  2,
     76        NULL, 0,
    9077        0
    9178    };
     
    10491
    10592static int rtnl_dump_filter(struct rtnl_handle *rth,
    106         int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
     93        int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
    10794        void *arg1/*,
    10895        int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
    10996        void *arg2*/)
    11097{
    111     char buf[8192];
     98    int retval = -1;
     99    char *buf = xmalloc(8*1024); /* avoid big stack buffer */
    112100    struct sockaddr_nl nladdr;
    113     struct iovec iov = { buf, sizeof(buf) };
     101    struct iovec iov = { buf, 8*1024 };
    114102
    115103    while (1) {
     
    119107        struct msghdr msg = {
    120108            (void*)&nladdr, sizeof(nladdr),
    121             &iov,   1,
    122             NULL,   0,
     109            &iov, 1,
     110            NULL, 0,
    123111            0
    124112        };
     
    134122        if (status == 0) {
    135123            bb_error_msg("EOF on netlink");
    136             return -1;
     124            goto ret;
    137125        }
    138126        if (msg.msg_namelen != sizeof(nladdr)) {
     
    146134            if (nladdr.nl_pid != 0 ||
    147135                h->nlmsg_pid != rth->local.nl_pid ||
    148                 h->nlmsg_seq != rth->dump) {
    149 /*              if (junk) {
    150                     err = junk(&nladdr, h, arg2);
    151                     if (err < 0)
    152                         return err;
    153                 } */
     136                h->nlmsg_seq != rth->dump
     137            ) {
     138//              if (junk) {
     139//                  err = junk(&nladdr, h, arg2);
     140//                  if (err < 0) {
     141//                      retval = err;
     142//                      goto ret;
     143//                  }
     144//              }
    154145                goto skip_it;
    155146            }
    156147
    157148            if (h->nlmsg_type == NLMSG_DONE) {
    158                 return 0;
     149                goto ret_0;
    159150            }
    160151            if (h->nlmsg_type == NLMSG_ERROR) {
     
    166157                    bb_perror_msg("RTNETLINK answers");
    167158                }
    168                 return -1;
     159                goto ret;
    169160            }
    170161            err = filter(&nladdr, h, arg1);
    171             if (err < 0)
    172                 return err;
    173 
    174 skip_it:
     162            if (err < 0) {
     163                retval = err;
     164                goto ret;
     165            }
     166
     167 skip_it:
    175168            h = NLMSG_NEXT(h, status);
    176169        }
     
    182175            bb_error_msg_and_die("remnant of size %d!", status);
    183176        }
    184     }
    185 }
    186 
    187 int xrtnl_dump_filter(struct rtnl_handle *rth,
    188         int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
     177    } /* while (1) */
     178 ret_0:
     179    retval++; /* = 0 */
     180 ret:
     181    free(buf);
     182    return retval;
     183}
     184
     185int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
     186        int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
    189187        void *arg1)
    190188{
     
    195193}
    196194
    197 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
    198           unsigned groups, struct nlmsghdr *answer,
    199           int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
    200           void *jarg)
    201 {
     195int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
     196        pid_t peer, unsigned groups,
     197        struct nlmsghdr *answer,
     198        int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
     199        void *jarg)
     200{
     201/* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
     202#define peer   0
     203#define groups 0
     204#define junk   NULL
     205#define jarg   NULL
     206    int retval = -1;
    202207    int status;
    203208    unsigned seq;
     
    205210    struct sockaddr_nl nladdr;
    206211    struct iovec iov = { (void*)n, n->nlmsg_len };
    207     char   buf[8192];
     212    char   *buf = xmalloc(8*1024); /* avoid big stack buffer */
    208213    struct msghdr msg = {
    209214        (void*)&nladdr, sizeof(nladdr),
    210         &iov,   1,
    211         NULL,   0,
     215        &iov, 1,
     216        NULL, 0,
    212217        0
    213218    };
     
    215220    memset(&nladdr, 0, sizeof(nladdr));
    216221    nladdr.nl_family = AF_NETLINK;
    217     nladdr.nl_pid = peer;
    218     nladdr.nl_groups = groups;
     222//  nladdr.nl_pid = peer;
     223//  nladdr.nl_groups = groups;
    219224
    220225    n->nlmsg_seq = seq = ++rtnl->seq;
     
    225230
    226231    if (status < 0) {
    227         bb_perror_msg("cannot talk to rtnetlink");
    228         return -1;
     232        bb_perror_msg("can't talk to rtnetlink");
     233        goto ret;
    229234    }
    230235
     
    232237
    233238    while (1) {
    234         iov.iov_len = sizeof(buf);
     239        iov.iov_len = 8*1024;
    235240        status = recvmsg(rtnl->fd, &msg, 0);
    236241
     
    244249        if (status == 0) {
    245250            bb_error_msg("EOF on netlink");
    246             return -1;
     251            goto ret;
    247252        }
    248253        if (msg.msg_namelen != sizeof(nladdr)) {
    249254            bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
    250255        }
    251         for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
    252             int l_err;
     256        for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
     257//          int l_err;
    253258            int len = h->nlmsg_len;
    254259            int l = len - sizeof(*h);
    255260
    256             if (l<0 || len>status) {
     261            if (l < 0 || len > status) {
    257262                if (msg.msg_flags & MSG_TRUNC) {
    258263                    bb_error_msg("truncated message");
    259                     return -1;
     264                    goto ret;
    260265                }
    261266                bb_error_msg_and_die("malformed message: len=%d!", len);
     
    264269            if (nladdr.nl_pid != peer ||
    265270                h->nlmsg_pid != rtnl->local.nl_pid ||
    266                 h->nlmsg_seq != seq) {
    267                 if (junk) {
    268                     l_err = junk(&nladdr, h, jarg);
    269                     if (l_err < 0) {
    270                         return l_err;
    271                     }
    272                 }
     271                h->nlmsg_seq != seq
     272            ) {
     273//              if (junk) {
     274//                  l_err = junk(&nladdr, h, jarg);
     275//                  if (l_err < 0) {
     276//                      retval = l_err;
     277//                      goto ret;
     278//                  }
     279//              }
    273280                continue;
    274281            }
     
    276283            if (h->nlmsg_type == NLMSG_ERROR) {
    277284                struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
    278                 if (l < sizeof(struct nlmsgerr)) {
     285                if (l < (int)sizeof(struct nlmsgerr)) {
    279286                    bb_error_msg("ERROR truncated");
    280287                } else {
    281                     errno = -err->error;
     288                    errno = - err->error;
    282289                    if (errno == 0) {
    283290                        if (answer) {
    284291                            memcpy(answer, h, h->nlmsg_len);
    285292                        }
    286                         return 0;
     293                        goto ret_0;
    287294                    }
    288295                    bb_perror_msg("RTNETLINK answers");
    289296                }
    290                 return -1;
     297                goto ret;
    291298            }
    292299            if (answer) {
    293300                memcpy(answer, h, h->nlmsg_len);
    294                 return 0;
     301                goto ret_0;
    295302            }
    296303
     
    307314            bb_error_msg_and_die("remnant of size %d!", status);
    308315        }
    309     }
    310 }
    311 
    312 int addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
     316    } /* while (1) */
     317 ret_0:
     318    retval++; /* = 0 */
     319 ret:
     320    free(buf);
     321    return retval;
     322}
     323
     324int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
    313325{
    314326    int len = RTA_LENGTH(4);
    315327    struct rtattr *rta;
    316     if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
     328
     329    if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
    317330        return -1;
     331    }
    318332    rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
    319333    rta->rta_type = type;
    320334    rta->rta_len = len;
    321     memcpy(RTA_DATA(rta), &data, 4);
     335    move_to_unaligned32(RTA_DATA(rta), data);
    322336    n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
    323337    return 0;
    324338}
    325339
    326 int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
     340int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
    327341{
    328342    int len = RTA_LENGTH(alen);
    329343    struct rtattr *rta;
    330344
    331     if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
     345    if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
    332346        return -1;
     347    }
    333348    rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
    334349    rta->rta_type = type;
     
    339354}
    340355
    341 int rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
     356int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
    342357{
    343358    int len = RTA_LENGTH(4);
     
    350365    subrta->rta_type = type;
    351366    subrta->rta_len = len;
    352     memcpy(RTA_DATA(subrta), &data, 4);
     367    move_to_unaligned32(RTA_DATA(subrta), data);
    353368    rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
    354369    return 0;
    355370}
    356371
    357 int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
     372int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
    358373{
    359374    struct rtattr *subrta;
     
    372387
    373388
    374 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
     389void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
    375390{
    376391    while (RTA_OK(rta, len)) {
     
    383398        bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
    384399    }
    385     return 0;
    386 }
     400}
Note: See TracChangeset for help on using the changeset viewer.