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/ll_map.c

    r821 r1765  
     1/* vi: set sw=4 ts=4: */
    12/*
    23 * ll_map.c
     
    1112 */
    1213
     14#include <net/if.h> /* struct ifreq and co. */
     15
    1316#include "libbb.h"
    14 #include <string.h>
    15 
    1617#include "libnetlink.h"
    1718#include "ll_map.h"
    1819
    19 struct idxmap
    20 {
    21     struct idxmap * next;
    22     int     index;
    23     int     type;
    24     int     alen;
    25     unsigned    flags;
    26     unsigned char   addr[8];
    27     char        name[16];
     20struct idxmap {
     21    struct idxmap *next;
     22    int            index;
     23    int            type;
     24    int            alen;
     25    unsigned       flags;
     26    unsigned char  addr[8];
     27    char           name[16];
    2828};
    2929
    3030static struct idxmap *idxmap[16];
     31
     32static struct idxmap *find_by_index(int idx)
     33{
     34    struct idxmap *im;
     35
     36    for (im = idxmap[idx & 0xF]; im; im = im->next)
     37        if (im->index == idx)
     38            return im;
     39    return NULL;
     40}
    3141
    3242int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
     
    4353        return -1;
    4454
    45 
    4655    memset(tb, 0, sizeof(tb));
    4756    parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
     
    4958        return 0;
    5059
    51     h = ifi->ifi_index&0xF;
     60    h = ifi->ifi_index & 0xF;
    5261
    53     for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)
     62    for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
    5463        if (im->index == ifi->ifi_index)
    55             break;
     64            goto found;
    5665
    57     if (im == NULL) {
    58         im = xmalloc(sizeof(*im));
    59         im->next = *imp;
    60         im->index = ifi->ifi_index;
    61         *imp = im;
    62     }
    63 
     66    im = xmalloc(sizeof(*im));
     67    im->next = *imp;
     68    im->index = ifi->ifi_index;
     69    *imp = im;
     70 found:
    6471    im->type = ifi->ifi_type;
    6572    im->flags = ifi->ifi_flags;
     
    8491    if (idx == 0)
    8592        return "*";
    86     for (im = idxmap[idx&0xF]; im; im = im->next)
    87         if (im->index == idx)
    88             return im->name;
     93    im = find_by_index(idx);
     94    if (im)
     95        return im->name;
    8996    snprintf(buf, 16, "if%d", idx);
    9097    return buf;
     
    99106}
    100107
     108#ifdef UNUSED
    101109int ll_index_to_type(int idx)
    102110{
     
    105113    if (idx == 0)
    106114        return -1;
    107     for (im = idxmap[idx&0xF]; im; im = im->next)
    108         if (im->index == idx)
    109             return im->type;
     115    im = find_by_index(idx);
     116    if (im)
     117        return im->type;
    110118    return -1;
    111119}
     120#endif
    112121
    113122unsigned ll_index_to_flags(int idx)
     
    117126    if (idx == 0)
    118127        return 0;
    119 
    120     for (im = idxmap[idx&0xF]; im; im = im->next)
    121         if (im->index == idx)
    122             return im->flags;
     128    im = find_by_index(idx);
     129    if (im)
     130        return im->flags;
    123131    return 0;
    124132}
    125133
    126 int ll_name_to_index(char *name)
     134int xll_name_to_index(const char *const name)
    127135{
     136    int ret = 0;
     137    int sock_fd;
     138
     139/* caching is not warranted - no users which repeatedly call it */
     140#ifdef UNUSED
    128141    static char ncache[16];
    129142    static int icache;
     143
    130144    struct idxmap *im;
    131145    int i;
    132146
    133147    if (name == NULL)
    134         return 0;
    135     if (icache && strcmp(name, ncache) == 0)
    136         return icache;
    137     for (i=0; i<16; i++) {
     148        goto out;
     149    if (icache && strcmp(name, ncache) == 0) {
     150        ret = icache;
     151        goto out;
     152    }
     153    for (i = 0; i < 16; i++) {
    138154        for (im = idxmap[i]; im; im = im->next) {
    139155            if (strcmp(im->name, name) == 0) {
    140156                icache = im->index;
    141157                strcpy(ncache, name);
    142                 return im->index;
     158                ret = im->index;
     159                goto out;
    143160            }
    144161        }
    145162    }
    146     return 0;
     163    /* We have not found the interface in our cache, but the kernel
     164     * may still know about it. One reason is that we may be using
     165     * module on-demand loading, which means that the kernel will
     166     * load the module and make the interface exist only when
     167     * we explicitely request it (check for dev_load() in net/core/dev.c).
     168     * I can think of other similar scenario, but they are less common...
     169     * Jean II */
     170#endif
     171
     172    sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
     173    if (sock_fd) {
     174        struct ifreq ifr;
     175        int tmp;
     176
     177        strncpy(ifr.ifr_name, name, IFNAMSIZ);
     178        ifr.ifr_ifindex = -1;
     179        tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
     180        close(sock_fd);
     181        if (tmp >= 0)
     182            /* In theory, we should redump the interface list
     183             * to update our cache, this is left as an exercise
     184             * to the reader... Jean II */
     185            ret = ifr.ifr_ifindex;
     186    }
     187/* out:*/
     188    if (ret <= 0)
     189        bb_error_msg_and_die("cannot find device \"%s\"", name);
     190    return ret;
    147191}
    148192
    149193int ll_init_map(struct rtnl_handle *rth)
    150194{
    151     if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
    152         perror("Cannot send dump request");
    153         exit(1);
    154     }
    155 
    156     if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
    157         fprintf(stderr, "Dump terminated\n");
    158         exit(1);
    159     }
     195    xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
     196    xrtnl_dump_filter(rth, ll_remember_index, &idxmap);
    160197    return 0;
    161198}
Note: See TracChangeset for help on using the changeset viewer.