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