busybox/libbb/inet_common.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * stolen from net-tools-1.59 and stripped down for busybox by
   4 *                      Erik Andersen <andersen@codepoet.org>
   5 *
   6 * Heavily modified by Manuel Novoa III       Mar 12, 2001
   7 *
   8 * Licensed under GPLv2, see file LICENSE in this source tree.
   9 */
  10#include "libbb.h"
  11#include "inet_common.h"
  12
  13#if 0
  14# define dbg(...) bb_error_msg(__VA_ARGS__)
  15#else
  16# define dbg(...) ((void)0)
  17#endif
  18
  19int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
  20{
  21        struct hostent *hp;
  22#if ENABLE_FEATURE_ETC_NETWORKS
  23        struct netent *np;
  24#endif
  25
  26        /* Grmpf. -FvK */
  27        s_in->sin_family = AF_INET;
  28        s_in->sin_port = 0;
  29
  30        /* Default is special, meaning 0.0.0.0. */
  31        if (strcmp(name, "default") == 0) {
  32                s_in->sin_addr.s_addr = INADDR_ANY;
  33                return 1;
  34        }
  35        /* Look to see if it's a dotted quad. */
  36        if (inet_aton(name, &s_in->sin_addr)) {
  37                return 0;
  38        }
  39        /* If we expect this to be a hostname, try hostname database first */
  40        if (hostfirst) {
  41                dbg("gethostbyname(%s)", name);
  42                hp = gethostbyname(name);
  43                if (hp) {
  44                        memcpy(&s_in->sin_addr, hp->h_addr_list[0],
  45                                sizeof(struct in_addr));
  46                        return 0;
  47                }
  48        }
  49#if ENABLE_FEATURE_ETC_NETWORKS
  50        /* Try the NETWORKS database to see if this is a known network. */
  51        dbg("getnetbyname(%s)", name);
  52        np = getnetbyname(name);
  53        if (np) {
  54                s_in->sin_addr.s_addr = htonl(np->n_net);
  55                return 1;
  56        }
  57#endif
  58        if (hostfirst) {
  59                /* Don't try again */
  60                return -1;
  61        }
  62#ifdef DEBUG
  63        res_init();
  64        _res.options |= RES_DEBUG;
  65#endif
  66        dbg("gethostbyname(%s)", name);
  67        hp = gethostbyname(name);
  68        if (!hp) {
  69                return -1;
  70        }
  71        memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  72        return 0;
  73}
  74
  75
  76/* numeric: & 0x8000: "default" instead of "*",
  77 *          & 0x4000: host instead of net,
  78 *          & 0x0fff: don't resolve
  79 */
  80char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
  81{
  82        /* addr-to-name cache */
  83        struct addr {
  84                struct addr *next;
  85                uint32_t nip;
  86                smallint is_host;
  87                char name[1];
  88        };
  89        static struct addr *cache = NULL;
  90
  91        struct addr *pn;
  92        char *name;
  93        uint32_t nip;
  94        smallint is_host;
  95
  96        if (s_in->sin_family != AF_INET) {
  97                dbg("rresolve: unsupported address family %d!", s_in->sin_family);
  98                errno = EAFNOSUPPORT;
  99                return NULL;
 100        }
 101        nip = s_in->sin_addr.s_addr;
 102        dbg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
 103        if (numeric & 0x0FFF)
 104                return xmalloc_sockaddr2dotted_noport((void*)s_in);
 105        if (nip == INADDR_ANY) {
 106                if (numeric & 0x8000)
 107                        return xstrdup("default");
 108                return xstrdup("*");
 109        }
 110
 111        is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
 112
 113        pn = cache;
 114        while (pn) {
 115                if (pn->nip == nip && pn->is_host == is_host) {
 116                        dbg("rresolve: found %s %08x in cache",
 117                                (is_host ? "host" : "net"), (unsigned)nip);
 118                        return xstrdup(pn->name);
 119                }
 120                pn = pn->next;
 121        }
 122
 123        name = NULL;
 124        if (is_host) {
 125                dbg("sockaddr2host_noport(%08x)", (unsigned)nip);
 126                name = xmalloc_sockaddr2host_noport((void*)s_in);
 127        }
 128#if ENABLE_FEATURE_ETC_NETWORKS
 129        else {
 130                struct netent *np;
 131                dbg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
 132                np = getnetbyaddr(ntohl(nip), AF_INET);
 133                if (np)
 134                        name = xstrdup(np->n_name);
 135        }
 136#endif
 137        if (!name)
 138                name = xmalloc_sockaddr2dotted_noport((void*)s_in);
 139
 140        pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
 141        pn->next = cache;
 142        pn->nip = nip;
 143        pn->is_host = is_host;
 144        strcpy(pn->name, name);
 145        cache = pn;
 146
 147        return name;
 148}
 149
 150#if ENABLE_FEATURE_IPV6
 151
 152int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
 153{
 154        struct addrinfo req, *ai = NULL;
 155        int s;
 156
 157        memset(&req, 0, sizeof(req));
 158        req.ai_family = AF_INET6;
 159        s = getaddrinfo(name, NULL, &req, &ai);
 160        if (s != 0) {
 161                bb_error_msg("getaddrinfo: %s: %d", name, s);
 162                return -1;
 163        }
 164        memcpy(sin6, ai->ai_addr, sizeof(*sin6));
 165        freeaddrinfo(ai);
 166        return 0;
 167}
 168
 169#ifndef IN6_IS_ADDR_UNSPECIFIED
 170# define IN6_IS_ADDR_UNSPECIFIED(a) \
 171        (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
 172         ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
 173#endif
 174
 175
 176char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
 177{
 178        if (sin6->sin6_family != AF_INET6) {
 179                dbg("rresolve: unsupported address family %d!",
 180                                sin6->sin6_family);
 181                errno = EAFNOSUPPORT;
 182                return NULL;
 183        }
 184        if (numeric & 0x7FFF) {
 185                return xmalloc_sockaddr2dotted_noport((void*)sin6);
 186        }
 187        if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 188                if (numeric & 0x8000)
 189                        return xstrdup("default");
 190                return xstrdup("*");
 191        }
 192
 193        return xmalloc_sockaddr2host_noport((void*)sin6);
 194}
 195
 196#endif  /* CONFIG_FEATURE_IPV6 */
 197