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