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/libbb/inet_common.c

    r821 r1765  
     1/* vi: set sw=4 ts=4: */
    12/*
    23 * stolen from net-tools-1.59 and stripped down for busybox by
     
    56 * Heavily modified by Manuel Novoa III       Mar 12, 2001
    67 *
    7  * Version:     $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $
    88 *
    99 */
     
    1111#include "libbb.h"
    1212#include "inet_common.h"
    13 #include <stdio.h>
    14 #include <errno.h>
    15 #include <stdlib.h>
    16 #include <string.h>
    17 #include <unistd.h>
    18 
    19 #ifdef DEBUG
    20 # include <resolv.h>
    21 #endif
    22 
    23 
    24 const char bb_INET_default[] = "default";
    2513
    2614int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
    2715{
    2816    struct hostent *hp;
     17#if ENABLE_FEATURE_ETC_NETWORKS
    2918    struct netent *np;
     19#endif
    3020
    3121    /* Grmpf. -FvK */
     
    3424
    3525    /* Default is special, meaning 0.0.0.0. */
    36     if (!strcmp(name, bb_INET_default)) {
     26    if (!strcmp(name, bb_str_default)) {
    3727        s_in->sin_addr.s_addr = INADDR_ANY;
    38         return (1);
     28        return 1;
    3929    }
    4030    /* Look to see if it's a dotted quad. */
     
    4535#ifdef DEBUG
    4636    if (hostfirst) {
    47         bb_error_msg("gethostbyname (%s)", name);
    48     }
    49 #endif
    50     if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
    51         memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
    52                sizeof(struct in_addr));
    53         return 0;
    54     }
     37        bb_error_msg("gethostbyname(%s)", name);
     38    }
     39#endif
     40    if (hostfirst) {
     41        hp = gethostbyname(name);
     42        if (hp != NULL) {
     43            memcpy(&s_in->sin_addr, hp->h_addr_list[0],
     44                sizeof(struct in_addr));
     45            return 0;
     46        }
     47    }
     48#if ENABLE_FEATURE_ETC_NETWORKS
    5549    /* Try the NETWORKS database to see if this is a known network. */
    5650#ifdef DEBUG
    57     bb_error_msg("getnetbyname (%s)", name);
    58 #endif
    59     if ((np = getnetbyname(name)) != (struct netent *) NULL) {
     51    bb_error_msg("getnetbyname(%s)", name);
     52#endif
     53    np = getnetbyname(name);
     54    if (np != NULL) {
    6055        s_in->sin_addr.s_addr = htonl(np->n_net);
    6156        return 1;
    6257    }
     58#endif
    6359    if (hostfirst) {
    6460        /* Don't try again */
     
    7167
    7268#ifdef DEBUG
    73     bb_error_msg("gethostbyname (%s)", name);
    74 #endif
    75     if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
     69    bb_error_msg("gethostbyname(%s)", name);
     70#endif
     71    hp = gethostbyname(name);
     72    if (hp == NULL) {
    7673        return -1;
    7774    }
    78     memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
    79            sizeof(struct in_addr));
    80 
     75    memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
    8176    return 0;
    8277}
    8378
    84 /* cache */
    85 struct addr {
    86     struct sockaddr_in addr;
    87     char *name;
    88     int host;
    89     struct addr *next;
    90 };
    91 
    92 static struct addr *INET_nn = NULL; /* addr-to-name cache           */
    9379
    9480/* numeric: & 0x8000: default instead of *,
     
    9682 *          & 0x0fff: don't resolve
    9783 */
    98 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
    99                   int numeric, unsigned int netmask)
    100 {
    101     struct hostent *ent;
    102     struct netent *np;
     84char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
     85{
     86    /* addr-to-name cache */
     87    struct addr {
     88        struct addr *next;
     89        struct sockaddr_in addr;
     90        int host;
     91        char name[1];
     92    };
     93    static struct addr *cache = NULL;
     94
    10395    struct addr *pn;
    104     unsigned long ad, host_ad;
     96    char *name;
     97    uint32_t ad, host_ad;
    10598    int host = 0;
    10699
     
    108101    if (s_in->sin_family != AF_INET) {
    109102#ifdef DEBUG
    110         bb_error_msg("rresolve: unsupport address family %d !",
     103        bb_error_msg("rresolve: unsupported address family %d!",
    111104                  s_in->sin_family);
    112105#endif
    113106        errno = EAFNOSUPPORT;
    114         return (-1);
    115     }
    116     ad = (unsigned long) s_in->sin_addr.s_addr;
    117 #ifdef DEBUG
    118     bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
     107        return NULL;
     108    }
     109    ad = s_in->sin_addr.s_addr;
     110#ifdef DEBUG
     111    bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
    119112#endif
    120113    if (ad == INADDR_ANY) {
    121114        if ((numeric & 0x0FFF) == 0) {
    122115            if (numeric & 0x8000)
    123                 safe_strncpy(name, bb_INET_default, len);
    124             else
    125                 safe_strncpy(name, "*", len);
    126             return (0);
     116                return xstrdup(bb_str_default);
     117            return xstrdup("*");
    127118        }
    128119    }
    129     if (numeric & 0x0FFF) {
    130         safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
    131         return (0);
    132     }
     120    if (numeric & 0x0FFF)
     121        return xstrdup(inet_ntoa(s_in->sin_addr));
    133122
    134123    if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
    135124        host = 1;
    136 #if 0
    137     INET_nn = NULL;
    138 #endif
    139     pn = INET_nn;
    140     while (pn != NULL) {
     125    pn = cache;
     126    while (pn) {
    141127        if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
    142             safe_strncpy(name, pn->name, len);
    143 #ifdef DEBUG
    144             bb_error_msg("rresolve: found %s %08lx in cache",
    145                       (host ? "host" : "net"), ad);
    146 #endif
    147             return (0);
     128#ifdef DEBUG
     129            bb_error_msg("rresolve: found %s %08x in cache",
     130                      (host ? "host" : "net"), (unsigned)ad);
     131#endif
     132            return xstrdup(pn->name);
    148133        }
    149134        pn = pn->next;
     
    151136
    152137    host_ad = ntohl(ad);
    153     np = NULL;
    154     ent = NULL;
     138    name = NULL;
    155139    if (host) {
    156 #ifdef DEBUG
    157         bb_error_msg("gethostbyaddr (%08lx)", ad);
     140        struct hostent *ent;
     141#ifdef DEBUG
     142        bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
    158143#endif
    159144        ent = gethostbyaddr((char *) &ad, 4, AF_INET);
    160         if (ent != NULL) {
    161             safe_strncpy(name, ent->h_name, len);
    162         }
    163     } else {
    164 #ifdef DEBUG
    165         bb_error_msg("getnetbyaddr (%08lx)", host_ad);
     145        if (ent)
     146            name = xstrdup(ent->h_name);
     147    } else if (ENABLE_FEATURE_ETC_NETWORKS) {
     148        struct netent *np;
     149#ifdef DEBUG
     150        bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
    166151#endif
    167152        np = getnetbyaddr(host_ad, AF_INET);
    168         if (np != NULL) {
    169             safe_strncpy(name, np->n_name, len);
    170         }
    171     }
    172     if ((ent == NULL) && (np == NULL)) {
    173         safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
    174     }
    175     pn = (struct addr *) xmalloc(sizeof(struct addr));
     153        if (np)
     154            name = xstrdup(np->n_name);
     155    }
     156    if (!name)
     157        name = xstrdup(inet_ntoa(s_in->sin_addr));
     158    pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
     159    pn->next = cache;
    176160    pn->addr = *s_in;
    177     pn->next = INET_nn;
    178161    pn->host = host;
    179     pn->name = bb_xstrdup(name);
    180     INET_nn = pn;
    181 
    182     return (0);
    183 }
    184 
    185 #ifdef CONFIG_FEATURE_IPV6
     162    strcpy(pn->name, name);
     163    cache = pn;
     164    return name;
     165}
     166
     167#if ENABLE_FEATURE_IPV6
    186168
    187169int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
     
    192174    memset(&req, '\0', sizeof req);
    193175    req.ai_family = AF_INET6;
    194     if ((s = getaddrinfo(name, NULL, &req, &ai))) {
     176    s = getaddrinfo(name, NULL, &req, &ai);
     177    if (s) {
    195178        bb_error_msg("getaddrinfo: %s: %d", name, s);
    196179        return -1;
    197180    }
    198181    memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
    199 
    200182    freeaddrinfo(ai);
    201 
    202     return (0);
     183    return 0;
    203184}
    204185
     
    210191
    211192
    212 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
    213                    int numeric)
    214 {
     193char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
     194{
     195    char name[128];
    215196    int s;
    216197
     
    218199    if (sin6->sin6_family != AF_INET6) {
    219200#ifdef DEBUG
    220         bb_error_msg(_("rresolve: unsupport address family %d !\n"),
     201        bb_error_msg("rresolve: unsupport address family %d!",
    221202                  sin6->sin6_family);
    222203#endif
    223204        errno = EAFNOSUPPORT;
    224         return (-1);
     205        return NULL;
    225206    }
    226207    if (numeric & 0x7FFF) {
    227         inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
    228         return (0);
     208        inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name));
     209        return xstrdup(name);
    229210    }
    230211    if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
    231         if (numeric & 0x8000) {
    232             strcpy(name, "default");
    233         } else {
    234             strcpy(name, "*");
    235         }
    236         return (0);
    237     }
    238 
    239     s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
     212        if (numeric & 0x8000)
     213            return xstrdup(bb_str_default);
     214        return xstrdup("*");
     215    }
     216
     217    s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
     218                name, sizeof(name), NULL, 0, 0);
    240219    if (s) {
    241220        bb_error_msg("getnameinfo failed");
    242         return -1;
    243     }
    244     return (0);
     221        return NULL;
     222    }
     223    return xstrdup(name);
    245224}
    246225
Note: See TracChangeset for help on using the changeset viewer.