/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Compatability shims with the rfc2553 API to simplify ntp.
*/
/*
* copy_addrinfo() - copy a single addrinfo to malloc()'d block.
* copy_addrinfo_list() - copy an addrinfo list to malloc()'d block.
*
* Copies an addrinfo list and its associated data to a contiguous block
* of storage from emalloc(). Callback routines invoked via
* getaddrinfo_sometime() have access to the resulting addrinfo list
* only until they return. This routine provides an easy way to make a
* persistent copy. Although the list provided to gai_sometime_callback
* routines is similarly contiguous, to keep this code usable in any
* context where we might want to duplicate an addrinfo list, it does
* not require the input list be contiguous.
*
* The returned list head pointer is passed to free() to release the
* entire list.
*
* In keeping with the rest of the NTP distribution, sockaddr_u is used
* in preference to struct sockaddr_storage, which is a member of the
* former union and so compatible.
*
* The rest of ntp_rfc2553.c is conditioned on ISC_PLATFORM_HAVEIPV6
* not being defined, copy_addrinfo_*() are exceptions.
*/
struct addrinfo * copy_addrinfo_common(const struct addrinfo *, int
#ifdef EREALLOC_CALLSITE
,
const char *, int
#endif
);
static char *ai_errlist[] = {
"Success",
"Address family for hostname not supported", /* EAI_ADDRFAMILY */
"Temporary failure in name resolution", /* EAI_AGAIN */
"Invalid value for ai_flags", /* EAI_BADFLAGS */
"Non-recoverable failure in name resolution", /* EAI_FAIL */
"ai_family not supported", /* EAI_FAMILY */
"Memory allocation failure", /* EAI_MEMORY */
"No address associated with hostname", /* EAI_NODATA */
"hostname nor servname provided, or not known", /* EAI_NONAME */
"servname not supported for ai_socktype", /* EAI_SERVICE */
"ai_socktype not supported", /* EAI_SOCKTYPE */
"System error returned in errno", /* EAI_SYSTEM */
"Invalid value for hints", /* EAI_BADHINTS */
"Resolved protocol is unknown", /* EAI_PROTOCOL */
"Unknown error", /* EAI_MAX */
};
/*
* Local declaration
*/
int
DNSlookup_name(
const char *name,
int ai_family,
struct hostent **Addresses
);
#ifndef SYS_WINNT
/*
* Encapsulate gethostbyname to control the error code
*/
int
DNSlookup_name(
const char *name,
int ai_family,
struct hostent **Addresses
)
{
*Addresses = gethostbyname(name);
return (h_errno);
}
#endif
/*
* First, look up the service name (port) if it was
* requested. If the socket type wasn't specified, then
* try and figure it out.
*/
if (servname != NULL) {
char *e;
port = strtol(servname, &e, 10);
if (*e == '\0') {
if (socktype == 0)
return (EAI_SOCKTYPE);
if (port < 0 || port > 65535)
return (EAI_SERVICE);
port = htons((unsigned short) port);
} else {
sp = getservbyname(servname, proto);
if (sp == NULL)
return (EAI_SERVICE);
port = sp->s_port;
if (socktype == 0) {
if (strcmp(sp->s_proto, "tcp") == 0)
socktype = SOCK_STREAM;
else if (strcmp(sp->s_proto, "udp") == 0)
socktype = SOCK_DGRAM;
}
}
} else
port = 0;
/*
*
* Set up the port number
*/
if (ai->ai_family == AF_INET)
((struct sockaddr_in *)ai->ai_addr)->sin_port = (unsigned short) port;
else if (ai->ai_family == AF_INET6)
((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = (unsigned short) port;
*res = ai;
return (0);
}
void
freeaddrinfo(struct addrinfo *ai)
{
if (ai->ai_canonname != NULL)
{
free(ai->ai_canonname);
ai->ai_canonname = NULL;
}
if (ai->ai_addr != NULL)
{
free(ai->ai_addr);
ai->ai_addr = NULL;
}
free(ai);
ai = NULL;
}
/*
* For an empty node name just use the wildcard.
* NOTE: We need to assume that the address family is
* set elsewhere so that we can set the appropriate wildcard
*/
if (nodename == NULL) {
if (ai->ai_family == AF_INET)
{
ai->ai_addrlen = sizeof(struct sockaddr_in);
sockin = (struct sockaddr_in *)ai->ai_addr;
sockin->sin_family = (short) ai->ai_family;
sockin->sin_addr.s_addr = htonl(INADDR_ANY);
}
else
{
ai->ai_addrlen = sizeof(struct sockaddr_in6);
sockin6 = (struct sockaddr_in6 *)ai->ai_addr;
sockin6->sin6_family = (short) ai->ai_family;
/*
* we have already zeroed out the address
* so we don't actually need to do this
* This assignment is causing problems so
* we don't do what this would do.
sockin6->sin6_addr = in6addr_any;
*/
}
#ifdef ISC_PLATFORM_HAVESALEN
ai->ai_addr->sa_len = SOCKLEN(ai->ai_addr);
#endif
return (0);
}
/*
* See if we have an IPv6 address
*/
if(strchr(nodename, ':') != NULL) {
if (inet_pton(AF_INET6, nodename,
&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr) == 1) {
((struct sockaddr_in6 *)ai->ai_addr)->sin6_family = AF_INET6;
ai->ai_family = AF_INET6;
ai->ai_addrlen = sizeof(struct sockaddr_in6);
return (0);
}
}
/*
* See if we have an IPv4 address
*/
if (inet_pton(AF_INET, nodename,
&((struct sockaddr_in *)ai->ai_addr)->sin_addr) == 1) {
((struct sockaddr *)ai->ai_addr)->sa_family = AF_INET;
ai->ai_family = AF_INET;
ai->ai_addrlen = sizeof(struct sockaddr_in);
return (0);
}
/*
* If the numeric host flag is set, don't attempt resolution
*/
if (hints != NULL && (hints->ai_flags & AI_NUMERICHOST))
return (EAI_NONAME);