"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * stolen from net-tools-1.59 and stripped down for busybox by |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 4 | * Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 5 | * |
| 6 | * Heavily modified by Manuel Novoa III Mar 12, 2001 |
| 7 | * |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 8 | * |
| 9 | */ |
| 10 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 12 | #include "inet_common.h" |
Eric Andersen | 77d22c4 | 2002-06-06 12:11:55 +0000 | [diff] [blame] | 13 | |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 14 | int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst) |
| 15 | { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 16 | struct hostent *hp; |
| 17 | struct netent *np; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 18 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 19 | /* Grmpf. -FvK */ |
| 20 | s_in->sin_family = AF_INET; |
| 21 | s_in->sin_port = 0; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 22 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 23 | /* Default is special, meaning 0.0.0.0. */ |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 24 | if (!strcmp(name, bb_str_default)) { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 25 | s_in->sin_addr.s_addr = INADDR_ANY; |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 26 | return 1; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 27 | } |
| 28 | /* Look to see if it's a dotted quad. */ |
| 29 | if (inet_aton(name, &s_in->sin_addr)) { |
| 30 | return 0; |
| 31 | } |
| 32 | /* If we expect this to be a hostname, try hostname database first */ |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 33 | #ifdef DEBUG |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 34 | if (hostfirst) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 35 | bb_error_msg("gethostbyname(%s)", name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 36 | } |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 37 | #endif |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 38 | if (hostfirst) { |
| 39 | hp = gethostbyname(name); |
| 40 | if (hp != NULL) { |
| 41 | memcpy(&s_in->sin_addr, hp->h_addr_list[0], |
| 42 | sizeof(struct in_addr)); |
| 43 | return 0; |
| 44 | } |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 45 | } |
| 46 | /* Try the NETWORKS database to see if this is a known network. */ |
| 47 | #ifdef DEBUG |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 48 | bb_error_msg("getnetbyname(%s)", name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 49 | #endif |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 50 | np = getnetbyname(name); |
| 51 | if (np != NULL) { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 52 | s_in->sin_addr.s_addr = htonl(np->n_net); |
| 53 | return 1; |
| 54 | } |
| 55 | if (hostfirst) { |
| 56 | /* Don't try again */ |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 57 | return -1; |
| 58 | } |
| 59 | #ifdef DEBUG |
| 60 | res_init(); |
| 61 | _res.options |= RES_DEBUG; |
| 62 | #endif |
| 63 | |
| 64 | #ifdef DEBUG |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 65 | bb_error_msg("gethostbyname(%s)", name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 66 | #endif |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 67 | hp = gethostbyname(name); |
| 68 | if (hp == NULL) { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 69 | return -1; |
| 70 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 71 | memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 72 | return 0; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 75 | |
| 76 | /* numeric: & 0x8000: default instead of *, |
| 77 | * & 0x4000: host instead of net, |
| 78 | * & 0x0fff: don't resolve |
| 79 | */ |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 80 | char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask) |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 81 | { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 82 | /* addr-to-name cache */ |
| 83 | struct addr { |
| 84 | struct addr *next; |
| 85 | struct sockaddr_in addr; |
| 86 | int host; |
| 87 | char name[1]; |
| 88 | }; |
| 89 | static struct addr *cache = NULL; |
| 90 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 91 | struct addr *pn; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 92 | char *name; |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 93 | uint32_t ad, host_ad; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 94 | int host = 0; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 95 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 96 | /* Grmpf. -FvK */ |
| 97 | if (s_in->sin_family != AF_INET) { |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 98 | #ifdef DEBUG |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 99 | bb_error_msg("rresolve: unsupported address family %d!", |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 100 | s_in->sin_family); |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 101 | #endif |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 102 | errno = EAFNOSUPPORT; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 103 | return NULL; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 104 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 105 | ad = s_in->sin_addr.s_addr; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 106 | #ifdef DEBUG |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 107 | bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 108 | #endif |
| 109 | if (ad == INADDR_ANY) { |
| 110 | if ((numeric & 0x0FFF) == 0) { |
| 111 | if (numeric & 0x8000) |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 112 | return xstrdup(bb_str_default); |
| 113 | return xstrdup("*"); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 116 | if (numeric & 0x0FFF) |
| 117 | return xstrdup(inet_ntoa(s_in->sin_addr)); |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 118 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 119 | if ((ad & (~netmask)) != 0 || (numeric & 0x4000)) |
| 120 | host = 1; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 121 | pn = cache; |
| 122 | while (pn) { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 123 | if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 124 | #ifdef DEBUG |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 125 | bb_error_msg("rresolve: found %s %08x in cache", |
| 126 | (host ? "host" : "net"), (unsigned)ad); |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 127 | #endif |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 128 | return xstrdup(pn->name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 129 | } |
| 130 | pn = pn->next; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 131 | } |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 132 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 133 | host_ad = ntohl(ad); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 134 | name = NULL; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 135 | if (host) { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 136 | struct hostent *ent; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 137 | #ifdef DEBUG |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 138 | bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad); |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 139 | #endif |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 140 | ent = gethostbyaddr((char *) &ad, 4, AF_INET); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 141 | if (ent) |
| 142 | name = xstrdup(ent->h_name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 143 | } else { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 144 | /* Hmmm... this is very rare to have named nets, |
| 145 | * and this getnetbyaddr() call is the only one in bbox. |
| 146 | * Maybe get rid of or make configurable? */ |
| 147 | struct netent *np; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 148 | #ifdef DEBUG |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 149 | bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad); |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 150 | #endif |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 151 | np = getnetbyaddr(host_ad, AF_INET); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 152 | if (np) |
| 153 | name = xstrdup(np->n_name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 154 | } |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 155 | if (!name) |
| 156 | name = xstrdup(inet_ntoa(s_in->sin_addr)); |
| 157 | pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */ |
| 158 | pn->next = cache; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 159 | pn->addr = *s_in; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 160 | pn->host = host; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 161 | strcpy(pn->name, name); |
| 162 | cache = pn; |
| 163 | return name; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 164 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 165 | |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 166 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 167 | |
Manuel Novoa III | 7d0c519 | 2004-03-10 07:42:38 +0000 | [diff] [blame] | 168 | int INET6_resolve(const char *name, struct sockaddr_in6 *sin6) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 169 | { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 170 | struct addrinfo req, *ai; |
| 171 | int s; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 172 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 173 | memset(&req, '\0', sizeof req); |
| 174 | req.ai_family = AF_INET6; |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 175 | s = getaddrinfo(name, NULL, &req, &ai); |
| 176 | if (s) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 177 | bb_error_msg("getaddrinfo: %s: %d", name, s); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 178 | return -1; |
| 179 | } |
| 180 | memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6)); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 181 | freeaddrinfo(ai); |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 182 | return 0; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | #ifndef IN6_IS_ADDR_UNSPECIFIED |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 186 | # define IN6_IS_ADDR_UNSPECIFIED(a) \ |
Bernhard Reutner-Fischer | 38583fd | 2006-05-19 12:44:16 +0000 | [diff] [blame] | 187 | (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \ |
| 188 | ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 189 | #endif |
| 190 | |
| 191 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 192 | char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 193 | { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 194 | char name[128]; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 195 | int s; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 196 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 197 | /* Grmpf. -FvK */ |
| 198 | if (sin6->sin6_family != AF_INET6) { |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 199 | #ifdef DEBUG |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 200 | bb_error_msg("rresolve: unsupport address family %d!", |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 201 | sin6->sin6_family); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 202 | #endif |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 203 | errno = EAFNOSUPPORT; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 204 | return NULL; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 205 | } |
| 206 | if (numeric & 0x7FFF) { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 207 | inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name)); |
| 208 | return xstrdup(name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 209 | } |
| 210 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 211 | if (numeric & 0x8000) |
| 212 | return xstrdup(bb_str_default); |
| 213 | return xstrdup("*"); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 214 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 216 | s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 217 | name, sizeof(name), NULL, 0, 0); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 218 | if (s) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 219 | bb_error_msg("getnameinfo failed"); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 220 | return NULL; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 221 | } |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame^] | 222 | return xstrdup(name); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 225 | #endif /* CONFIG_FEATURE_IPV6 */ |