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