Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | 53950b6 | 2003-06-14 08:43:22 +1000 | [diff] [blame] | 2 | * Copyright (C) 2000-2003 Damien Miller. All rights reserved. |
| 3 | * Copyright (C) 1999 WIDE Project. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of the project nor the names of its contributors |
| 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | /* |
Damien Miller | c28e38d | 2003-06-05 18:52:47 +1000 | [diff] [blame] | 31 | * Pseudo-implementation of RFC2553 name / address resolution functions |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 32 | * |
| 33 | * But these functions are not implemented correctly. The minimum subset |
Darren Tucker | bc976f9 | 2003-06-11 23:56:41 +1000 | [diff] [blame] | 34 | * is implemented for ssh use only. For example, this routine assumes |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 35 | * that ai_family is AF_INET. Don't use it for another purpose. |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 36 | */ |
| 37 | |
Darren Tucker | 4aff13f | 2003-06-05 19:37:30 +1000 | [diff] [blame] | 38 | #include "includes.h" |
Damien Miller | 62da44f | 2006-07-24 15:08:35 +1000 | [diff] [blame] | 39 | #include <string.h> |
Darren Tucker | 4aff13f | 2003-06-05 19:37:30 +1000 | [diff] [blame] | 40 | |
Darren Tucker | 2b4e38b | 2006-08-05 19:18:08 +1000 | [diff] [blame^] | 41 | #include <netinet/in.h> |
| 42 | #include <arpa/inet.h> |
| 43 | |
Damien Miller | c28e38d | 2003-06-05 18:52:47 +1000 | [diff] [blame] | 44 | #ifndef HAVE_GETNAMEINFO |
| 45 | int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, |
| 46 | size_t hostlen, char *serv, size_t servlen, int flags) |
| 47 | { |
| 48 | struct sockaddr_in *sin = (struct sockaddr_in *)sa; |
| 49 | struct hostent *hp; |
| 50 | char tmpserv[16]; |
| 51 | |
| 52 | if (serv != NULL) { |
| 53 | snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port)); |
| 54 | if (strlcpy(serv, tmpserv, servlen) >= servlen) |
| 55 | return (EAI_MEMORY); |
| 56 | } |
| 57 | |
| 58 | if (host != NULL) { |
| 59 | if (flags & NI_NUMERICHOST) { |
| 60 | if (strlcpy(host, inet_ntoa(sin->sin_addr), |
| 61 | hostlen) >= hostlen) |
| 62 | return (EAI_MEMORY); |
| 63 | else |
| 64 | return (0); |
| 65 | } else { |
| 66 | hp = gethostbyaddr((char *)&sin->sin_addr, |
| 67 | sizeof(struct in_addr), AF_INET); |
| 68 | if (hp == NULL) |
| 69 | return (EAI_NODATA); |
| 70 | |
| 71 | if (strlcpy(host, hp->h_name, hostlen) >= hostlen) |
| 72 | return (EAI_MEMORY); |
| 73 | else |
| 74 | return (0); |
| 75 | } |
| 76 | } |
| 77 | return (0); |
| 78 | } |
| 79 | #endif /* !HAVE_GETNAMEINFO */ |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 80 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 81 | #ifndef HAVE_GAI_STRERROR |
Darren Tucker | d5e082f | 2003-09-22 12:08:23 +1000 | [diff] [blame] | 82 | #ifdef HAVE_CONST_GAI_STRERROR_PROTO |
| 83 | const char * |
| 84 | #else |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 85 | char * |
Darren Tucker | d5e082f | 2003-09-22 12:08:23 +1000 | [diff] [blame] | 86 | #endif |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 87 | gai_strerror(int err) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 88 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 89 | switch (err) { |
| 90 | case EAI_NODATA: |
| 91 | return ("no address associated with name"); |
| 92 | case EAI_MEMORY: |
| 93 | return ("memory allocation failure."); |
Damien Miller | 10eac0c | 2003-06-05 09:48:32 +1000 | [diff] [blame] | 94 | case EAI_NONAME: |
| 95 | return ("nodename nor servname provided, or not known"); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 96 | default: |
| 97 | return ("unknown/invalid error."); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 98 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 99 | } |
| 100 | #endif /* !HAVE_GAI_STRERROR */ |
| 101 | |
| 102 | #ifndef HAVE_FREEADDRINFO |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 103 | void |
| 104 | freeaddrinfo(struct addrinfo *ai) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 105 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 106 | struct addrinfo *next; |
| 107 | |
Damien Miller | c322bba | 2003-05-19 10:39:37 +1000 | [diff] [blame] | 108 | for(; ai != NULL;) { |
| 109 | next = ai->ai_next; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 110 | free(ai); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 111 | ai = next; |
| 112 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 113 | } |
| 114 | #endif /* !HAVE_FREEADDRINFO */ |
| 115 | |
| 116 | #ifndef HAVE_GETADDRINFO |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 117 | static struct |
| 118 | addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 119 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 120 | struct addrinfo *ai; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 121 | |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 122 | ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in)); |
| 123 | if (ai == NULL) |
| 124 | return (NULL); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 125 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 126 | memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in)); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 127 | |
| 128 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 129 | /* XXX -- ssh doesn't use sa_len */ |
| 130 | ai->ai_addrlen = sizeof(struct sockaddr_in); |
| 131 | ai->ai_addr->sa_family = ai->ai_family = AF_INET; |
| 132 | |
| 133 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port; |
| 134 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr; |
| 135 | |
Darren Tucker | abef562 | 2003-05-14 21:48:51 +1000 | [diff] [blame] | 136 | /* XXX: the following is not generally correct, but does what we want */ |
| 137 | if (hints->ai_socktype) |
| 138 | ai->ai_socktype = hints->ai_socktype; |
| 139 | else |
| 140 | ai->ai_socktype = SOCK_STREAM; |
| 141 | |
| 142 | if (hints->ai_protocol) |
| 143 | ai->ai_protocol = hints->ai_protocol; |
| 144 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 145 | return (ai); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 146 | } |
| 147 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 148 | int |
| 149 | getaddrinfo(const char *hostname, const char *servname, |
| 150 | const struct addrinfo *hints, struct addrinfo **res) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 151 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 152 | struct hostent *hp; |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 153 | struct servent *sp; |
Damien Miller | 8e394e7 | 2000-07-08 11:50:37 +1000 | [diff] [blame] | 154 | struct in_addr in; |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 155 | int i; |
| 156 | long int port; |
Damien Miller | 62b6b17 | 2003-03-24 13:35:58 +1100 | [diff] [blame] | 157 | u_long addr; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 158 | |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 159 | port = 0; |
| 160 | if (servname != NULL) { |
| 161 | char *cp; |
| 162 | |
| 163 | port = strtol(servname, &cp, 10); |
| 164 | if (port > 0 && port <= 65535 && *cp == '\0') |
| 165 | port = htons(port); |
| 166 | else if ((sp = getservbyname(servname, NULL)) != NULL) |
| 167 | port = sp->s_port; |
| 168 | else |
| 169 | port = 0; |
| 170 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 171 | |
| 172 | if (hints && hints->ai_flags & AI_PASSIVE) { |
Damien Miller | 62b6b17 | 2003-03-24 13:35:58 +1100 | [diff] [blame] | 173 | addr = htonl(0x00000000); |
| 174 | if (hostname && inet_aton(hostname, &in) != 0) |
| 175 | addr = in.s_addr; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 176 | *res = malloc_ai(port, addr, hints); |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 177 | if (*res == NULL) |
| 178 | return (EAI_MEMORY); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 179 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (!hostname) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 183 | *res = malloc_ai(port, htonl(0x7f000001), hints); |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 184 | if (*res == NULL) |
| 185 | return (EAI_MEMORY); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 186 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 187 | } |
| 188 | |
Damien Miller | 7a0e5dc | 2000-07-11 12:15:54 +1000 | [diff] [blame] | 189 | if (inet_aton(hostname, &in)) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 190 | *res = malloc_ai(port, in.s_addr, hints); |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 191 | if (*res == NULL) |
| 192 | return (EAI_MEMORY); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 193 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 194 | } |
| 195 | |
Damien Miller | 10eac0c | 2003-06-05 09:48:32 +1000 | [diff] [blame] | 196 | /* Don't try DNS if AI_NUMERICHOST is set */ |
| 197 | if (hints && hints->ai_flags & AI_NUMERICHOST) |
| 198 | return (EAI_NONAME); |
| 199 | |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 200 | hp = gethostbyname(hostname); |
| 201 | if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 202 | struct addrinfo *cur, *prev; |
| 203 | |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 204 | cur = prev = *res = NULL; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 205 | for (i = 0; hp->h_addr_list[i]; i++) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 206 | struct in_addr *in = (struct in_addr *)hp->h_addr_list[i]; |
| 207 | |
| 208 | cur = malloc_ai(port, in->s_addr, hints); |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 209 | if (cur == NULL) { |
| 210 | if (*res != NULL) |
| 211 | freeaddrinfo(*res); |
| 212 | return (EAI_MEMORY); |
| 213 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 214 | if (prev) |
| 215 | prev->ai_next = cur; |
| 216 | else |
| 217 | *res = cur; |
| 218 | |
| 219 | prev = cur; |
| 220 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 221 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 222 | } |
| 223 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 224 | return (EAI_NODATA); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 225 | } |
| 226 | #endif /* !HAVE_GETADDRINFO */ |