blob: b6ea3d21e3425c2a2a070e2ea006e1a562895fff [file] [log] [blame]
Damien Miller34132e52000-01-14 15:45:46 +11001/*
Damien Miller53950b62003-06-14 08:43:22 +10002 * 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 Millerc28e38d2003-06-05 18:52:47 +100031 * Pseudo-implementation of RFC2553 name / address resolution functions
Damien Miller34132e52000-01-14 15:45:46 +110032 *
33 * But these functions are not implemented correctly. The minimum subset
Darren Tuckerbc976f92003-06-11 23:56:41 +100034 * is implemented for ssh use only. For example, this routine assumes
Damien Miller34132e52000-01-14 15:45:46 +110035 * that ai_family is AF_INET. Don't use it for another purpose.
Damien Miller34132e52000-01-14 15:45:46 +110036 */
37
Darren Tucker4aff13f2003-06-05 19:37:30 +100038#include "includes.h"
Darren Tuckere6b641a2006-08-17 18:55:27 +100039
40#include <stdlib.h>
Damien Miller62da44f2006-07-24 15:08:35 +100041#include <string.h>
Darren Tucker4aff13f2003-06-05 19:37:30 +100042
Darren Tucker2b4e38b2006-08-05 19:18:08 +100043#include <netinet/in.h>
44#include <arpa/inet.h>
45
Damien Millerc28e38d2003-06-05 18:52:47 +100046#ifndef HAVE_GETNAMEINFO
47int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
48 size_t hostlen, char *serv, size_t servlen, int flags)
49{
50 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
51 struct hostent *hp;
52 char tmpserv[16];
53
54 if (serv != NULL) {
55 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
56 if (strlcpy(serv, tmpserv, servlen) >= servlen)
57 return (EAI_MEMORY);
58 }
59
60 if (host != NULL) {
61 if (flags & NI_NUMERICHOST) {
62 if (strlcpy(host, inet_ntoa(sin->sin_addr),
63 hostlen) >= hostlen)
64 return (EAI_MEMORY);
65 else
66 return (0);
67 } else {
68 hp = gethostbyaddr((char *)&sin->sin_addr,
69 sizeof(struct in_addr), AF_INET);
70 if (hp == NULL)
71 return (EAI_NODATA);
72
73 if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
74 return (EAI_MEMORY);
75 else
76 return (0);
77 }
78 }
79 return (0);
80}
81#endif /* !HAVE_GETNAMEINFO */
Damien Millere9cf3572001-02-09 12:55:35 +110082
Damien Miller34132e52000-01-14 15:45:46 +110083#ifndef HAVE_GAI_STRERROR
Darren Tuckerd5e082f2003-09-22 12:08:23 +100084#ifdef HAVE_CONST_GAI_STRERROR_PROTO
85const char *
86#else
Damien Miller31741252003-05-19 00:13:38 +100087char *
Darren Tuckerd5e082f2003-09-22 12:08:23 +100088#endif
Damien Miller31741252003-05-19 00:13:38 +100089gai_strerror(int err)
Damien Miller34132e52000-01-14 15:45:46 +110090{
Damien Miller31741252003-05-19 00:13:38 +100091 switch (err) {
92 case EAI_NODATA:
93 return ("no address associated with name");
94 case EAI_MEMORY:
95 return ("memory allocation failure.");
Damien Miller10eac0c2003-06-05 09:48:32 +100096 case EAI_NONAME:
97 return ("nodename nor servname provided, or not known");
Damien Miller31741252003-05-19 00:13:38 +100098 default:
99 return ("unknown/invalid error.");
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000100 }
Damien Miller34132e52000-01-14 15:45:46 +1100101}
102#endif /* !HAVE_GAI_STRERROR */
103
104#ifndef HAVE_FREEADDRINFO
Damien Miller31741252003-05-19 00:13:38 +1000105void
106freeaddrinfo(struct addrinfo *ai)
Damien Miller34132e52000-01-14 15:45:46 +1100107{
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000108 struct addrinfo *next;
109
Damien Millerc322bba2003-05-19 10:39:37 +1000110 for(; ai != NULL;) {
111 next = ai->ai_next;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000112 free(ai);
Damien Miller31741252003-05-19 00:13:38 +1000113 ai = next;
114 }
Damien Miller34132e52000-01-14 15:45:46 +1100115}
116#endif /* !HAVE_FREEADDRINFO */
117
118#ifndef HAVE_GETADDRINFO
Damien Miller31741252003-05-19 00:13:38 +1000119static struct
120addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
Damien Miller34132e52000-01-14 15:45:46 +1100121{
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000122 struct addrinfo *ai;
Damien Miller34132e52000-01-14 15:45:46 +1100123
Damien Millerb95bb7f2003-06-05 10:04:12 +1000124 ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));
125 if (ai == NULL)
126 return (NULL);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000127
Damien Miller31741252003-05-19 00:13:38 +1000128 memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000129
130 ai->ai_addr = (struct sockaddr *)(ai + 1);
131 /* XXX -- ssh doesn't use sa_len */
132 ai->ai_addrlen = sizeof(struct sockaddr_in);
133 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
134
135 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
136 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
137
Darren Tuckerabef5622003-05-14 21:48:51 +1000138 /* XXX: the following is not generally correct, but does what we want */
139 if (hints->ai_socktype)
140 ai->ai_socktype = hints->ai_socktype;
141 else
142 ai->ai_socktype = SOCK_STREAM;
143
144 if (hints->ai_protocol)
145 ai->ai_protocol = hints->ai_protocol;
146
Damien Miller31741252003-05-19 00:13:38 +1000147 return (ai);
Damien Miller34132e52000-01-14 15:45:46 +1100148}
149
Damien Miller31741252003-05-19 00:13:38 +1000150int
151getaddrinfo(const char *hostname, const char *servname,
152 const struct addrinfo *hints, struct addrinfo **res)
Damien Miller34132e52000-01-14 15:45:46 +1100153{
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000154 struct hostent *hp;
Damien Miller850b9422003-02-06 10:50:42 +1100155 struct servent *sp;
Damien Miller8e394e72000-07-08 11:50:37 +1000156 struct in_addr in;
Damien Miller850b9422003-02-06 10:50:42 +1100157 int i;
158 long int port;
Damien Miller62b6b172003-03-24 13:35:58 +1100159 u_long addr;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000160
Damien Miller850b9422003-02-06 10:50:42 +1100161 port = 0;
162 if (servname != NULL) {
163 char *cp;
164
165 port = strtol(servname, &cp, 10);
166 if (port > 0 && port <= 65535 && *cp == '\0')
167 port = htons(port);
168 else if ((sp = getservbyname(servname, NULL)) != NULL)
169 port = sp->s_port;
170 else
171 port = 0;
172 }
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000173
174 if (hints && hints->ai_flags & AI_PASSIVE) {
Damien Miller62b6b172003-03-24 13:35:58 +1100175 addr = htonl(0x00000000);
176 if (hostname && inet_aton(hostname, &in) != 0)
177 addr = in.s_addr;
Damien Miller31741252003-05-19 00:13:38 +1000178 *res = malloc_ai(port, addr, hints);
Damien Millerb95bb7f2003-06-05 10:04:12 +1000179 if (*res == NULL)
180 return (EAI_MEMORY);
Damien Miller31741252003-05-19 00:13:38 +1000181 return (0);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000182 }
183
184 if (!hostname) {
Damien Miller31741252003-05-19 00:13:38 +1000185 *res = malloc_ai(port, htonl(0x7f000001), hints);
Damien Millerb95bb7f2003-06-05 10:04:12 +1000186 if (*res == NULL)
187 return (EAI_MEMORY);
Damien Miller31741252003-05-19 00:13:38 +1000188 return (0);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000189 }
190
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000191 if (inet_aton(hostname, &in)) {
Damien Miller31741252003-05-19 00:13:38 +1000192 *res = malloc_ai(port, in.s_addr, hints);
Damien Millerb95bb7f2003-06-05 10:04:12 +1000193 if (*res == NULL)
194 return (EAI_MEMORY);
Damien Miller31741252003-05-19 00:13:38 +1000195 return (0);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000196 }
197
Damien Miller10eac0c2003-06-05 09:48:32 +1000198 /* Don't try DNS if AI_NUMERICHOST is set */
199 if (hints && hints->ai_flags & AI_NUMERICHOST)
200 return (EAI_NONAME);
201
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000202 hp = gethostbyname(hostname);
203 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
Damien Miller31741252003-05-19 00:13:38 +1000204 struct addrinfo *cur, *prev;
205
Damien Millerb95bb7f2003-06-05 10:04:12 +1000206 cur = prev = *res = NULL;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000207 for (i = 0; hp->h_addr_list[i]; i++) {
Damien Miller31741252003-05-19 00:13:38 +1000208 struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
209
210 cur = malloc_ai(port, in->s_addr, hints);
Damien Millerb95bb7f2003-06-05 10:04:12 +1000211 if (cur == NULL) {
212 if (*res != NULL)
213 freeaddrinfo(*res);
214 return (EAI_MEMORY);
215 }
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000216 if (prev)
217 prev->ai_next = cur;
218 else
219 *res = cur;
220
221 prev = cur;
222 }
Damien Miller31741252003-05-19 00:13:38 +1000223 return (0);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000224 }
225
Damien Miller31741252003-05-19 00:13:38 +1000226 return (EAI_NODATA);
Damien Miller34132e52000-01-14 15:45:46 +1100227}
228#endif /* !HAVE_GETADDRINFO */