blob: d5a62975aa468a91de9a515520c6ecfb34433aab [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.
Darren Tuckerc20dccb2016-08-02 09:44:25 +10004 *
Damien Miller53950b62003-06-14 08:43:22 +10005 * 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.
Darren Tuckerc20dccb2016-08-02 09:44:25 +100016 *
Damien Miller53950b62003-06-14 08:43:22 +100017 * 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
Darren Tuckerc20dccb2016-08-02 09:44:25 +100047int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
Darren Tuckerdd1031b2016-08-02 10:01:52 +100048 size_t hostlen, char *serv, size_t servlen, int flags)
Damien Millerc28e38d2003-06-05 18:52:47 +100049{
50 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
51 struct hostent *hp;
52 char tmpserv[16];
53
Damien Millerc4657ef2008-07-14 21:37:36 +100054 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
55 return (EAI_FAMILY);
Damien Millerc28e38d2003-06-05 18:52:47 +100056 if (serv != NULL) {
57 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
58 if (strlcpy(serv, tmpserv, servlen) >= servlen)
59 return (EAI_MEMORY);
60 }
61
62 if (host != NULL) {
63 if (flags & NI_NUMERICHOST) {
64 if (strlcpy(host, inet_ntoa(sin->sin_addr),
65 hostlen) >= hostlen)
66 return (EAI_MEMORY);
67 else
68 return (0);
69 } else {
Darren Tuckerc20dccb2016-08-02 09:44:25 +100070 hp = gethostbyaddr((char *)&sin->sin_addr,
Damien Millerc28e38d2003-06-05 18:52:47 +100071 sizeof(struct in_addr), AF_INET);
72 if (hp == NULL)
73 return (EAI_NODATA);
Darren Tuckerc20dccb2016-08-02 09:44:25 +100074
Damien Millerc28e38d2003-06-05 18:52:47 +100075 if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
76 return (EAI_MEMORY);
77 else
78 return (0);
79 }
80 }
81 return (0);
82}
83#endif /* !HAVE_GETNAMEINFO */
Damien Millere9cf3572001-02-09 12:55:35 +110084
Damien Miller34132e52000-01-14 15:45:46 +110085#ifndef HAVE_GAI_STRERROR
Darren Tuckerd5e082f2003-09-22 12:08:23 +100086#ifdef HAVE_CONST_GAI_STRERROR_PROTO
87const char *
88#else
Damien Miller31741252003-05-19 00:13:38 +100089char *
Darren Tuckerd5e082f2003-09-22 12:08:23 +100090#endif
Damien Miller31741252003-05-19 00:13:38 +100091gai_strerror(int err)
Damien Miller34132e52000-01-14 15:45:46 +110092{
Damien Miller31741252003-05-19 00:13:38 +100093 switch (err) {
94 case EAI_NODATA:
95 return ("no address associated with name");
96 case EAI_MEMORY:
97 return ("memory allocation failure.");
Damien Miller10eac0c2003-06-05 09:48:32 +100098 case EAI_NONAME:
99 return ("nodename nor servname provided, or not known");
Damien Millerc4657ef2008-07-14 21:37:36 +1000100 case EAI_FAMILY:
101 return ("ai_family not supported");
Damien Miller31741252003-05-19 00:13:38 +1000102 default:
103 return ("unknown/invalid error.");
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000104 }
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000105}
Damien Miller34132e52000-01-14 15:45:46 +1100106#endif /* !HAVE_GAI_STRERROR */
107
108#ifndef HAVE_FREEADDRINFO
Damien Miller31741252003-05-19 00:13:38 +1000109void
110freeaddrinfo(struct addrinfo *ai)
Damien Miller34132e52000-01-14 15:45:46 +1100111{
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000112 struct addrinfo *next;
113
Damien Millerc322bba2003-05-19 10:39:37 +1000114 for(; ai != NULL;) {
115 next = ai->ai_next;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000116 free(ai);
Damien Miller31741252003-05-19 00:13:38 +1000117 ai = next;
118 }
Damien Miller34132e52000-01-14 15:45:46 +1100119}
120#endif /* !HAVE_FREEADDRINFO */
121
122#ifndef HAVE_GETADDRINFO
Damien Miller31741252003-05-19 00:13:38 +1000123static struct
124addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
Damien Miller34132e52000-01-14 15:45:46 +1100125{
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000126 struct addrinfo *ai;
Damien Miller34132e52000-01-14 15:45:46 +1100127
Damien Millerb95bb7f2003-06-05 10:04:12 +1000128 ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));
129 if (ai == NULL)
130 return (NULL);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000131
Damien Miller31741252003-05-19 00:13:38 +1000132 memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000133
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000134 ai->ai_addr = (struct sockaddr *)(ai + 1);
135 /* XXX -- ssh doesn't use sa_len */
136 ai->ai_addrlen = sizeof(struct sockaddr_in);
137 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
138
139 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
140 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000141
Darren Tuckerabef5622003-05-14 21:48:51 +1000142 /* XXX: the following is not generally correct, but does what we want */
143 if (hints->ai_socktype)
144 ai->ai_socktype = hints->ai_socktype;
145 else
146 ai->ai_socktype = SOCK_STREAM;
147
148 if (hints->ai_protocol)
149 ai->ai_protocol = hints->ai_protocol;
150
Damien Miller31741252003-05-19 00:13:38 +1000151 return (ai);
Damien Miller34132e52000-01-14 15:45:46 +1100152}
153
Damien Miller31741252003-05-19 00:13:38 +1000154int
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000155getaddrinfo(const char *hostname, const char *servname,
Damien Miller31741252003-05-19 00:13:38 +1000156 const struct addrinfo *hints, struct addrinfo **res)
Damien Miller34132e52000-01-14 15:45:46 +1100157{
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000158 struct hostent *hp;
Damien Miller850b9422003-02-06 10:50:42 +1100159 struct servent *sp;
Damien Miller8e394e72000-07-08 11:50:37 +1000160 struct in_addr in;
Damien Miller850b9422003-02-06 10:50:42 +1100161 int i;
162 long int port;
Damien Miller62b6b172003-03-24 13:35:58 +1100163 u_long addr;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000164
Damien Miller850b9422003-02-06 10:50:42 +1100165 port = 0;
Damien Millerc4657ef2008-07-14 21:37:36 +1000166 if (hints && hints->ai_family != AF_UNSPEC &&
167 hints->ai_family != AF_INET)
168 return (EAI_FAMILY);
Damien Miller850b9422003-02-06 10:50:42 +1100169 if (servname != NULL) {
170 char *cp;
171
172 port = strtol(servname, &cp, 10);
173 if (port > 0 && port <= 65535 && *cp == '\0')
174 port = htons(port);
175 else if ((sp = getservbyname(servname, NULL)) != NULL)
176 port = sp->s_port;
177 else
178 port = 0;
179 }
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000180
181 if (hints && hints->ai_flags & AI_PASSIVE) {
Damien Miller62b6b172003-03-24 13:35:58 +1100182 addr = htonl(0x00000000);
183 if (hostname && inet_aton(hostname, &in) != 0)
184 addr = in.s_addr;
Damien Miller31741252003-05-19 00:13:38 +1000185 *res = malloc_ai(port, addr, hints);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000186 if (*res == NULL)
Damien Millerb95bb7f2003-06-05 10:04:12 +1000187 return (EAI_MEMORY);
Damien Miller31741252003-05-19 00:13:38 +1000188 return (0);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000189 }
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000190
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000191 if (!hostname) {
Damien Miller31741252003-05-19 00:13:38 +1000192 *res = malloc_ai(port, htonl(0x7f000001), hints);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000193 if (*res == NULL)
Damien Millerb95bb7f2003-06-05 10:04:12 +1000194 return (EAI_MEMORY);
Damien Miller31741252003-05-19 00:13:38 +1000195 return (0);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000196 }
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000197
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000198 if (inet_aton(hostname, &in)) {
Damien Miller31741252003-05-19 00:13:38 +1000199 *res = malloc_ai(port, in.s_addr, hints);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000200 if (*res == NULL)
Damien Millerb95bb7f2003-06-05 10:04:12 +1000201 return (EAI_MEMORY);
Damien Miller31741252003-05-19 00:13:38 +1000202 return (0);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000203 }
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000204
Damien Miller10eac0c2003-06-05 09:48:32 +1000205 /* Don't try DNS if AI_NUMERICHOST is set */
206 if (hints && hints->ai_flags & AI_NUMERICHOST)
207 return (EAI_NONAME);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000208
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000209 hp = gethostbyname(hostname);
210 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
Damien Miller31741252003-05-19 00:13:38 +1000211 struct addrinfo *cur, *prev;
212
Damien Millerb95bb7f2003-06-05 10:04:12 +1000213 cur = prev = *res = NULL;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000214 for (i = 0; hp->h_addr_list[i]; i++) {
Damien Miller31741252003-05-19 00:13:38 +1000215 struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
216
217 cur = malloc_ai(port, in->s_addr, hints);
Damien Millerb95bb7f2003-06-05 10:04:12 +1000218 if (cur == NULL) {
219 if (*res != NULL)
220 freeaddrinfo(*res);
221 return (EAI_MEMORY);
222 }
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000223 if (prev)
224 prev->ai_next = cur;
225 else
226 *res = cur;
227
228 prev = cur;
229 }
Damien Miller31741252003-05-19 00:13:38 +1000230 return (0);
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000231 }
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000232
Damien Miller31741252003-05-19 00:13:38 +1000233 return (EAI_NODATA);
Damien Miller34132e52000-01-14 15:45:46 +1100234}
235#endif /* !HAVE_GETADDRINFO */