2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 * Copyright (c) 1982, 1986, 1990, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * Compatability shims with the rfc2553 API to simplify ntp.
70 #include <sys/types.h>
72 #include <sys/socket.h>
74 #ifdef HAVE_NETINET_IN_H
75 #include <netinet/in.h>
77 #include "ntp_rfc2553.h"
80 #include "ntp_malloc.h"
81 #include "ntp_stdlib.h"
82 #include "ntp_string.h"
84 #ifndef ISC_PLATFORM_HAVEIPV6
86 static char *ai_errlist[] = {
88 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
89 "Temporary failure in name resolution", /* EAI_AGAIN */
90 "Invalid value for ai_flags", /* EAI_BADFLAGS */
91 "Non-recoverable failure in name resolution", /* EAI_FAIL */
92 "ai_family not supported", /* EAI_FAMILY */
93 "Memory allocation failure", /* EAI_MEMORY */
94 "No address associated with hostname", /* EAI_NODATA */
95 "hostname nor servname provided, or not known", /* EAI_NONAME */
96 "servname not supported for ai_socktype", /* EAI_SERVICE */
97 "ai_socktype not supported", /* EAI_SOCKTYPE */
98 "System error returned in errno", /* EAI_SYSTEM */
99 "Invalid value for hints", /* EAI_BADHINTS */
100 "Resolved protocol is unknown", /* EAI_PROTOCOL */
101 "Unknown error", /* EAI_MAX */
111 struct hostent **Addresses
116 * Encapsulate gethostbyname to control the error code
122 struct hostent **Addresses
125 *Addresses = gethostbyname(name);
130 static int do_nodename P((const char *nodename, struct addrinfo *ai,
131 const struct addrinfo *hints));
134 getaddrinfo (const char *nodename, const char *servname,
135 const struct addrinfo *hints, struct addrinfo **res)
139 struct addrinfo *ai = NULL;
141 const char *proto = NULL;
142 int family, socktype, flags, protocol;
146 * If no name is provide just return an error
148 if (nodename == NULL && servname == NULL)
151 ai = calloc(sizeof(struct addrinfo), 1);
156 * Copy default values from hints, if available
159 ai->ai_flags = hints->ai_flags;
160 ai->ai_family = hints->ai_family;
161 ai->ai_socktype = hints->ai_socktype;
162 ai->ai_protocol = hints->ai_protocol;
164 family = hints->ai_family;
165 socktype = hints->ai_socktype;
166 protocol = hints->ai_protocol;
167 flags = hints->ai_flags;
171 switch (hints->ai_socktype) {
182 switch (hints->ai_socktype) {
194 return (EAI_SOCKTYPE);
199 switch (hints->ai_socktype) {
207 return (EAI_SOCKTYPE);
221 rval = do_nodename(nodename, ai, hints);
228 * First, look up the service name (port) if it was
229 * requested. If the socket type wasn't specified, then
230 * try and figure it out.
232 if (servname != NULL) {
235 port = strtol(servname, &e, 10);
238 return (EAI_SOCKTYPE);
239 if (port < 0 || port > 65535)
240 return (EAI_SERVICE);
241 port = htons((unsigned short) port);
243 sp = getservbyname(servname, proto);
245 return (EAI_SERVICE);
248 if (strcmp(sp->s_proto, "tcp") == 0)
249 socktype = SOCK_STREAM;
250 else if (strcmp(sp->s_proto, "udp") == 0)
251 socktype = SOCK_DGRAM;
259 * Set up the port number
261 if (ai->ai_family == AF_INET)
262 ((struct sockaddr_in *)ai->ai_addr)->sin_port = (unsigned short) port;
263 else if (ai->ai_family == AF_INET6)
264 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = (unsigned short) port;
270 freeaddrinfo(struct addrinfo *ai)
272 if (ai->ai_canonname != NULL)
274 free(ai->ai_canonname);
275 ai->ai_canonname = NULL;
277 if (ai->ai_addr != NULL)
287 getnameinfo (const struct sockaddr *sa, u_int salen, char *host,
288 size_t hostlen, char *serv, size_t servlen, int flags)
293 if (sa->sa_family != AF_INET)
296 (const char *)&((const struct sockaddr_in *)sa)->sin_addr,
299 if (h_errno == TRY_AGAIN)
304 if (host != NULL && hostlen > 0) {
306 * Don't exceed buffer
308 namelen = min(strlen(hp->h_name), hostlen - 1);
310 strncpy(host, hp->h_name, namelen);
311 host[namelen] = '\0';
318 gai_strerror(int ecode)
320 if (ecode < 0 || ecode > EAI_MAX)
322 return ai_errlist[ecode];
327 const char *nodename,
329 const struct addrinfo *hints)
331 struct hostent *hp = NULL;
332 struct sockaddr_in *sockin;
333 struct sockaddr_in6 *sockin6;
336 ai->ai_addr = calloc(sizeof(struct sockaddr_storage), 1);
337 if (ai->ai_addr == NULL)
341 * For an empty node name just use the wildcard.
342 * NOTE: We need to assume that the address family is
343 * set elsewhere so that we can set the appropriate wildcard
345 if (nodename == NULL) {
346 ai->ai_addrlen = sizeof(struct sockaddr_storage);
347 if (ai->ai_family == AF_INET)
349 sockin = (struct sockaddr_in *)ai->ai_addr;
350 sockin->sin_family = (short) ai->ai_family;
351 sockin->sin_addr.s_addr = htonl(INADDR_ANY);
355 sockin6 = (struct sockaddr_in6 *)ai->ai_addr;
356 sockin6->sin6_family = (short) ai->ai_family;
358 * we have already zeroed out the address
359 * so we don't actually need to do this
360 * This assignment is causing problems so
361 * we don't do what this would do.
362 sockin6->sin6_addr = in6addr_any;
365 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
366 ai->ai_addr->sa_len = SOCKLEN(ai->ai_addr);
373 * See if we have an IPv6 address
375 if(strchr(nodename, ':') != NULL) {
376 if (inet_pton(AF_INET6, nodename,
377 &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr) == 1) {
378 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_family = AF_INET6;
379 ai->ai_family = AF_INET6;
380 ai->ai_addrlen = sizeof(struct sockaddr_in6);
386 * See if we have an IPv4 address
388 if (inet_pton(AF_INET, nodename,
389 &((struct sockaddr_in *)ai->ai_addr)->sin_addr) == 1) {
390 ((struct sockaddr *)ai->ai_addr)->sa_family = AF_INET;
391 ai->ai_family = AF_INET;
392 ai->ai_addrlen = sizeof(struct sockaddr_in);
397 * If the numeric host flag is set, don't attempt resolution
399 if (hints != NULL && (hints->ai_flags & AI_NUMERICHOST))
406 errval = DNSlookup_name(nodename, AF_INET, &hp);
409 if (errval == TRY_AGAIN || errval == EAI_AGAIN)
411 else if (errval == EAI_NONAME) {
412 if (inet_pton(AF_INET, nodename,
413 &((struct sockaddr_in *)ai->ai_addr)->sin_addr) == 1) {
414 ((struct sockaddr *)ai->ai_addr)->sa_family = AF_INET;
415 ai->ai_family = AF_INET;
416 ai->ai_addrlen = sizeof(struct sockaddr_in);
426 ai->ai_family = hp->h_addrtype;
427 ai->ai_addrlen = sizeof(struct sockaddr);
428 sockin = (struct sockaddr_in *)ai->ai_addr;
429 memcpy(&sockin->sin_addr, hp->h_addr, hp->h_length);
430 ai->ai_addr->sa_family = hp->h_addrtype;
431 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
432 ai->ai_addr->sa_len = sizeof(struct sockaddr);
434 if (hints != NULL && hints->ai_flags & AI_CANONNAME) {
435 ai->ai_canonname = malloc(strlen(hp->h_name) + 1);
436 if (ai->ai_canonname == NULL)
438 strcpy(ai->ai_canonname, hp->h_name);
443 #endif /* !ISC_PLATFORM_HAVEIPV6 */