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 #ifdef HAVE_SYS_SOCKET_H
73 #include <sys/socket.h>
76 #ifdef HAVE_NETINET_IN_H
77 #include <netinet/in.h>
79 #include "ntp_rfc2553.h"
82 #include "ntp_malloc.h"
83 #include "ntp_string.h"
84 #include "ntp_debug.h"
88 * copy_addrinfo() - copy a single addrinfo to malloc()'d block.
89 * copy_addrinfo_list() - copy an addrinfo list to malloc()'d block.
91 * Copies an addrinfo list and its associated data to a contiguous block
92 * of storage from emalloc(). Callback routines invoked via
93 * getaddrinfo_sometime() have access to the resulting addrinfo list
94 * only until they return. This routine provides an easy way to make a
95 * persistent copy. Although the list provided to gai_sometime_callback
96 * routines is similarly contiguous, to keep this code usable in any
97 * context where we might want to duplicate an addrinfo list, it does
98 * not require the input list be contiguous.
100 * The returned list head pointer is passed to free() to release the
103 * In keeping with the rest of the NTP distribution, sockaddr_u is used
104 * in preference to struct sockaddr_storage, which is a member of the
105 * former union and so compatible.
107 * The rest of ntp_rfc2553.c is conditioned on ISC_PLATFORM_HAVEIPV6
108 * not being defined, copy_addrinfo_*() are exceptions.
110 struct addrinfo * copy_addrinfo_common(const struct addrinfo *, int
111 #ifdef EREALLOC_CALLSITE
120 const struct addrinfo * src
121 #ifdef EREALLOC_CALLSITE
123 const char * caller_file,
128 return copy_addrinfo_common(src, TRUE
129 #ifdef EREALLOC_CALLSITE
131 caller_file, caller_line
138 copy_addrinfo_list_impl(
139 const struct addrinfo * src
140 #ifdef EREALLOC_CALLSITE
142 const char * caller_file,
147 return copy_addrinfo_common(src, FALSE
148 #ifdef EREALLOC_CALLSITE
150 caller_file, caller_line
157 copy_addrinfo_common(
158 const struct addrinfo * src,
160 #ifdef EREALLOC_CALLSITE
162 const char * caller_file,
167 const struct addrinfo * ai_src;
168 const struct addrinfo * ai_nxt;
169 struct addrinfo * ai_cpy;
170 struct addrinfo * dst;
175 size_t canons_octets;
181 for (ai_src = src; NULL != ai_src; ai_src = ai_nxt) {
185 ai_nxt = ai_src->ai_next;
187 if (NULL != ai_src->ai_canonname)
188 canons_octets += 1 + strlen(ai_src->ai_canonname);
191 octets = elements * (sizeof(*ai_cpy) + sizeof(*psau));
192 octets += canons_octets;
194 dst = erealloczsite(NULL, octets, 0, TRUE, caller_file,
197 psau = (void *)(ai_cpy + elements);
198 pcanon = (void *)(psau + elements);
200 for (ai_src = src; NULL != ai_src; ai_src = ai_nxt) {
204 ai_nxt = ai_src->ai_next;
206 DEBUG_INSIST(ai_cpy->ai_canonname == ai_src->ai_canonname);
207 INSIST(ai_src->ai_addrlen <= sizeof(sockaddr_u));
208 memcpy(psau, ai_src->ai_addr, ai_src->ai_addrlen);
209 ai_cpy->ai_addr = &psau->sa;
211 if (NULL != ai_src->ai_canonname) {
212 ai_cpy->ai_canonname = pcanon;
213 str_octets = 1 + strlen(ai_src->ai_canonname);
214 memcpy(pcanon, ai_src->ai_canonname, str_octets);
215 pcanon += str_octets;
217 if (NULL != ai_cpy->ai_next) {
219 ai_cpy->ai_next = NULL;
221 ai_cpy->ai_next = ai_cpy + 1;
225 ENSURE(pcanon == ((char *)dst + octets));
231 #ifndef ISC_PLATFORM_HAVEIPV6
233 static char *ai_errlist[] = {
235 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
236 "Temporary failure in name resolution", /* EAI_AGAIN */
237 "Invalid value for ai_flags", /* EAI_BADFLAGS */
238 "Non-recoverable failure in name resolution", /* EAI_FAIL */
239 "ai_family not supported", /* EAI_FAMILY */
240 "Memory allocation failure", /* EAI_MEMORY */
241 "No address associated with hostname", /* EAI_NODATA */
242 "hostname nor servname provided, or not known", /* EAI_NONAME */
243 "servname not supported for ai_socktype", /* EAI_SERVICE */
244 "ai_socktype not supported", /* EAI_SOCKTYPE */
245 "System error returned in errno", /* EAI_SYSTEM */
246 "Invalid value for hints", /* EAI_BADHINTS */
247 "Resolved protocol is unknown", /* EAI_PROTOCOL */
248 "Unknown error", /* EAI_MAX */
258 struct hostent **Addresses
263 * Encapsulate gethostbyname to control the error code
269 struct hostent **Addresses
272 *Addresses = gethostbyname(name);
277 static int do_nodename (const char *nodename, struct addrinfo *ai,
278 const struct addrinfo *hints);
281 getaddrinfo (const char *nodename, const char *servname,
282 const struct addrinfo *hints, struct addrinfo **res)
286 struct addrinfo *ai = NULL;
288 const char *proto = NULL;
289 int family, socktype, flags, protocol;
293 * If no name is provide just return an error
295 if (nodename == NULL && servname == NULL)
298 ai = calloc(sizeof(struct addrinfo), 1);
303 * Copy default values from hints, if available
306 ai->ai_flags = hints->ai_flags;
307 ai->ai_family = hints->ai_family;
308 ai->ai_socktype = hints->ai_socktype;
309 ai->ai_protocol = hints->ai_protocol;
311 family = hints->ai_family;
312 socktype = hints->ai_socktype;
313 protocol = hints->ai_protocol;
314 flags = hints->ai_flags;
318 switch (hints->ai_socktype) {
329 switch (hints->ai_socktype) {
341 return (EAI_SOCKTYPE);
346 switch (hints->ai_socktype) {
354 return (EAI_SOCKTYPE);
368 rval = do_nodename(nodename, ai, hints);
375 * First, look up the service name (port) if it was
376 * requested. If the socket type wasn't specified, then
377 * try and figure it out.
379 if (servname != NULL) {
382 port = strtol(servname, &e, 10);
385 return (EAI_SOCKTYPE);
386 if (port < 0 || port > 65535)
387 return (EAI_SERVICE);
388 port = htons((unsigned short) port);
390 sp = getservbyname(servname, proto);
392 return (EAI_SERVICE);
395 if (strcmp(sp->s_proto, "tcp") == 0)
396 socktype = SOCK_STREAM;
397 else if (strcmp(sp->s_proto, "udp") == 0)
398 socktype = SOCK_DGRAM;
406 * Set up the port number
408 if (ai->ai_family == AF_INET)
409 ((struct sockaddr_in *)ai->ai_addr)->sin_port = (unsigned short) port;
410 else if (ai->ai_family == AF_INET6)
411 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = (unsigned short) port;
417 freeaddrinfo(struct addrinfo *ai)
419 if (ai->ai_canonname != NULL)
421 free(ai->ai_canonname);
422 ai->ai_canonname = NULL;
424 if (ai->ai_addr != NULL)
434 getnameinfo (const struct sockaddr *sa, u_int salen, char *host,
435 size_t hostlen, char *serv, size_t servlen, int flags)
439 if (sa->sa_family != AF_INET)
442 (const char *)&((const struct sockaddr_in *)sa)->sin_addr,
445 if (h_errno == TRY_AGAIN)
450 if (host != NULL && hostlen > 0)
451 strlcpy(host, hp->h_name, hostlen);
456 gai_strerror(int ecode)
458 if (ecode < 0 || ecode > EAI_MAX)
460 return ai_errlist[ecode];
465 const char *nodename,
467 const struct addrinfo *hints)
469 struct hostent *hp = NULL;
470 struct sockaddr_in *sockin;
471 struct sockaddr_in6 *sockin6;
474 ai->ai_addr = calloc(sizeof(struct sockaddr_storage), 1);
475 if (ai->ai_addr == NULL)
479 * For an empty node name just use the wildcard.
480 * NOTE: We need to assume that the address family is
481 * set elsewhere so that we can set the appropriate wildcard
483 if (nodename == NULL) {
484 if (ai->ai_family == AF_INET)
486 ai->ai_addrlen = sizeof(struct sockaddr_in);
487 sockin = (struct sockaddr_in *)ai->ai_addr;
488 sockin->sin_family = (short) ai->ai_family;
489 sockin->sin_addr.s_addr = htonl(INADDR_ANY);
493 ai->ai_addrlen = sizeof(struct sockaddr_in6);
494 sockin6 = (struct sockaddr_in6 *)ai->ai_addr;
495 sockin6->sin6_family = (short) ai->ai_family;
497 * we have already zeroed out the address
498 * so we don't actually need to do this
499 * This assignment is causing problems so
500 * we don't do what this would do.
501 sockin6->sin6_addr = in6addr_any;
504 #ifdef ISC_PLATFORM_HAVESALEN
505 ai->ai_addr->sa_len = SOCKLEN(ai->ai_addr);
512 * See if we have an IPv6 address
514 if(strchr(nodename, ':') != NULL) {
515 if (inet_pton(AF_INET6, nodename,
516 &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr) == 1) {
517 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_family = AF_INET6;
518 ai->ai_family = AF_INET6;
519 ai->ai_addrlen = sizeof(struct sockaddr_in6);
525 * See if we have an IPv4 address
527 if (inet_pton(AF_INET, nodename,
528 &((struct sockaddr_in *)ai->ai_addr)->sin_addr) == 1) {
529 ((struct sockaddr *)ai->ai_addr)->sa_family = AF_INET;
530 ai->ai_family = AF_INET;
531 ai->ai_addrlen = sizeof(struct sockaddr_in);
536 * If the numeric host flag is set, don't attempt resolution
538 if (hints != NULL && (hints->ai_flags & AI_NUMERICHOST))
545 errval = DNSlookup_name(nodename, AF_INET, &hp);
548 if (errval == TRY_AGAIN || errval == EAI_AGAIN)
550 else if (errval == EAI_NONAME) {
551 if (inet_pton(AF_INET, nodename,
552 &((struct sockaddr_in *)ai->ai_addr)->sin_addr) == 1) {
553 ((struct sockaddr *)ai->ai_addr)->sa_family = AF_INET;
554 ai->ai_family = AF_INET;
555 ai->ai_addrlen = sizeof(struct sockaddr_in);
565 ai->ai_family = hp->h_addrtype;
566 ai->ai_addrlen = sizeof(struct sockaddr);
567 sockin = (struct sockaddr_in *)ai->ai_addr;
568 memcpy(&sockin->sin_addr, hp->h_addr, hp->h_length);
569 ai->ai_addr->sa_family = hp->h_addrtype;
570 #ifdef ISC_PLATFORM_HAVESALEN
571 ai->ai_addr->sa_len = sizeof(struct sockaddr);
573 if (hints != NULL && (hints->ai_flags & AI_CANONNAME))
574 ai->ai_canonname = estrdup(hp->h_name);
578 #endif /* !ISC_PLATFORM_HAVEIPV6 */