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