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