/*
* Copyright (C) 2009, 2012-2017 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* $Id: getaddrinfo.c,v 1.3 2009/09/02 23:48:02 tbox Exp $ */
/*! \file */
/**
* getaddrinfo() is used to get a list of IP addresses and port
* numbers for host hostname and service servname as defined in RFC3493.
* hostname and servname are pointers to null-terminated strings
* or NULL. hostname is either a host name or a numeric host address
* string: a dotted decimal IPv4 address or an IPv6 address. servname is
* either a decimal port number or a service name as listed in
*
* If the operating system does not provide a struct addrinfo, the
* following structure is used:
*
* \code
* struct addrinfo {
* int ai_flags; // AI_PASSIVE, AI_CANONNAME
* int ai_family; // PF_xxx
* int ai_socktype; // SOCK_xxx
* int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6
* size_t ai_addrlen; // length of ai_addr
* char *ai_canonname; // canonical name for hostname
* struct sockaddr *ai_addr; // binary address
* struct addrinfo *ai_next; // next structure in linked list
* };
* \endcode
*
*
* hints is an optional pointer to a struct addrinfo. This structure can
* be used to provide hints concerning the type of socket that the caller
* supports or wishes to use. The caller can supply the following
* structure elements in *hints:
*
* <ul>
* <li>ai_family:
* The protocol family that should be used. When ai_family is set
* to PF_UNSPEC, it means the caller will accept any protocol
* family supported by the operating system.</li>
*
* <li>ai_socktype:
* denotes the type of socket -- SOCK_STREAM, SOCK_DGRAM or
* SOCK_RAW -- that is wanted. When ai_socktype is zero the caller
* will accept any socket type.</li>
*
* <li>ai_protocol:
* indicates which transport protocol is wanted: IPPROTO_UDP or
* IPPROTO_TCP. If ai_protocol is zero the caller will accept any
* protocol.</li>
*
* <li>ai_flags:
* Flag bits. If the AI_CANONNAME bit is set, a successful call to
* getaddrinfo() will return a null-terminated string
* containing the canonical name of the specified hostname in
* ai_canonname of the first addrinfo structure returned. Setting
* the AI_PASSIVE bit indicates that the returned socket address
* structure is intended for used in a call to bind(2). In this
* case, if the hostname argument is a NULL pointer, then the IP
* address portion of the socket address structure will be set to
* INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6
* address.<br /><br />
*
* When ai_flags does not set the AI_PASSIVE bit, the returned
* socket address structure will be ready for use in a call to
* connect(2) for a connection-oriented protocol or connect(2),
* sendto(2), or sendmsg(2) if a connectionless protocol was
* chosen. The IP address portion of the socket address structure
* will be set to the loopback address if hostname is a NULL
* pointer and AI_PASSIVE is not set in ai_flags.<br /><br />
*
* If ai_flags is set to AI_NUMERICHOST it indicates that hostname
* should be treated as a numeric string defining an IPv4 or IPv6
* address and no name resolution should be attempted.
* </li></ul>
*
* All other elements of the struct addrinfo passed via hints must be
* zero.
*
* A hints of NULL is treated as if the caller provided a struct addrinfo
* initialized to zero with ai_familyset to PF_UNSPEC.
*
* After a successful call to getaddrinfo(), *res is a pointer to a
* linked list of one or more addrinfo structures. Each struct addrinfo
* in this list cn be processed by following the ai_next pointer, until a
* NULL pointer is encountered. The three members ai_family, ai_socktype,
* and ai_protocol in each returned addrinfo structure contain the
* corresponding arguments for a call to socket(2). For each addrinfo
* structure in the list, the ai_addr member points to a filled-in socket
* address structure of length ai_addrlen.
*
* All of the information returned by getaddrinfo() is dynamically
* allocated: the addrinfo structures, and the socket address structures
* and canonical host name strings pointed to by the addrinfostructures.
* Memory allocated for the dynamically allocated structures created by a
* successful call to getaddrinfo() is released by freeaddrinfo().
* ai is a pointer to a struct addrinfo created by a call to getaddrinfo().
*
* \section irsreturn RETURN VALUES
*
* getaddrinfo() returns zero on success or one of the error codes
* listed in gai_strerror() if an error occurs. If both hostname and
* servname are NULL getaddrinfo() returns #EAI_NONAME.
*
* \section irssee SEE ALSO
*
* getaddrinfo(), freeaddrinfo(),
* gai_strerror(), RFC3493, getservbyname(3), connect(2),
* sendto(2), sendmsg(2), socket(2).
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <isc/sockaddr.h>
#include <dns/fixedname.h>
#include <dns/rdataset.h>
#include <dns/rdatastruct.h>
#include <dns/rdatatype.h>
/*! \struct addrinfo
*/
static struct addrinfo
#ifdef AF_LOCAL
#endif
static int
int, int));
/*%
* Get a list of IP addresses and port numbers for host hostname and
* service servname.
*/
int
{
const char *proto;
int err = 0;
int port, i;
int, int);
return (EAI_NONAME);
return (EAI_BADFLAGS);
return (EAI_SYSTEM);
}
switch (family) {
case AF_UNSPEC:
switch (hints->ai_socktype) {
case SOCK_STREAM:
proto = "tcp";
break;
case SOCK_DGRAM:
proto = "udp";
break;
}
break;
case AF_INET:
case AF_INET6:
switch (hints->ai_socktype) {
case 0:
break;
case SOCK_STREAM:
proto = "tcp";
break;
case SOCK_DGRAM:
proto = "udp";
break;
case SOCK_RAW:
break;
default:
return (EAI_SOCKTYPE);
}
break;
#ifdef AF_LOCAL
case AF_LOCAL:
switch (hints->ai_socktype) {
case 0:
break;
case SOCK_STREAM:
break;
case SOCK_DGRAM:
break;
default:
return (EAI_SOCKTYPE);
}
break;
#endif
default:
return (EAI_FAMILY);
}
} else {
protocol = 0;
family = 0;
socktype = 0;
flags = 0;
}
#ifdef AF_LOCAL
/*!
* First, deal with AF_LOCAL. If the family was not set,
* then assume AF_LOCAL if the first character of the
*/
#endif
/*
* Ok, only AF_INET and AF_INET6 left.
*/
/*
* First, look up the service name (port) if it was
* requested. If the socket type wasn't specified, then
* try and figure it out.
*/
char *e;
if (*e == '\0') {
if (socktype == 0)
return (EAI_SOCKTYPE);
return (EAI_SERVICE);
} else {
return (EAI_SERVICE);
if (socktype == 0) {
}
}
} else
port = 0;
/*
* Next, deal with just a service name, and no hostname.
* (we verified that one of them was non-null up above).
*/
return (EAI_MEMORY);
}
return (EAI_MEMORY);
}
}
return (0);
}
/*
* If the family isn't specified or AI_NUMERICHOST specified, check
* first to see if it is a numeric address.
* Though the gethostbyname2() routine will recognize numeric addresses,
* it will only recognize the format that it is being called for. Thus,
* a numeric AF_INET address will be treated by the AF_INET6 call as
* a domain name, and vice versa. Checking for both numerics here
* avoids that.
*/
#ifdef IRS_HAVE_SIN6_SCOPE_ID
char *p, *ep;
#endif
#ifdef IRS_HAVE_SIN6_SCOPE_ID
/*
* Scope identifier portion.
*/
ntmp[0] = '\0';
/*
* Vendors may want to support non-numeric
* scopeid around here.
*/
if (p != NULL)
&ep, 10);
*p = '\0';
else {
ntmp[0] = '\0';
scopeid = 0;
}
} else
scopeid = 0;
#endif
== 1) {
/*
* Convert to a V4 mapped address.
*/
goto inet6_addr;
}
goto common;
#ifdef IRS_HAVE_SIN6_SCOPE_ID
} else if (ntmp[0] != '\0' &&
return (EAI_NONAME);
goto common;
#endif
return (EAI_NONAME);
sizeof(struct sockaddr_in6) :
sizeof(struct sockaddr_in)));
return (EAI_MEMORY);
if ((flags & AI_CANONNAME) != 0) {
#ifdef IRS_HAVE_SIN6_SCOPE_ID
#endif
NI_NUMERICHOST) == 0) {
return (EAI_MEMORY);
}
} else {
/* XXX raise error? */
}
}
goto done;
} else if ((flags & AI_NUMERICHOST) != 0) {
return (EAI_NONAME);
}
}
for (i = 0; i < FOUND_MAX; i++) {
break;
if (err != 0) {
}
break;
}
}
} else
if (err == 0)
err = EAI_NONAME;
return (err);
}
done:
return (0);
}
typedef struct gai_restrans {
int error;
typedef struct gai_resstate {
typedef struct gai_statehead {
int ai_family;
int ai_flags;
int ai_socktype;
int ai_port;
unsigned int activestates;
static isc_result_t
{
unsigned int namelen;
isc_buffer_t b;
return (ISC_R_NOMEMORY);
/* Construct base domain name */
isc_buffer_add(&b, namelen);
if (result != ISC_R_SUCCESS) {
return (result);
}
/* Construct query name */
isc_buffer_add(&b, namelen);
if (result != ISC_R_SUCCESS) {
return (result);
}
if (need_v4) {
return (ISC_R_NOMEMORY);
}
}
if (need_v6) {
return (ISC_R_NOMEMORY);
}
}
return (ISC_R_SUCCESS);
}
static isc_result_t
{
if (result != ISC_R_SUCCESS)
return (result);
&resstate);
if (result != ISC_R_SUCCESS)
break;
head->activestates++;
}
/*
* Insert the original hostname either at the head or the tail of the
* state list, depending on the number of labels contained in the
* original name and the 'ndots' configuration parameter.
*/
} else
head->activestates++;
if (result != ISC_R_SUCCESS) {
}
}
}
}
return (result);
}
static void
} else {
}
case ISC_R_SUCCESS:
case DNS_R_NCACHENXDOMAIN: /* treat this as a fatal error? */
case DNS_R_NCACHENXRRSET:
break;
default:
case DNS_R_SIGINVALID:
case DNS_R_SIGEXPIRED:
case DNS_R_SIGFUTURE:
case DNS_R_KEYUNAUTHORIZED:
case DNS_R_MUSTBESECURE:
case DNS_R_COVERINGNSEC:
case DNS_R_NOTAUTHORITATIVE:
case DNS_R_NOVALIDKEY:
case DNS_R_NOVALIDDS:
case DNS_R_NOVALIDSIG:
break;
default:
}
goto done;
}
/* Parse the response and construct the addrinfo chain */
if (wantcname) {
isc_buffer_t b;
if (result != ISC_R_SUCCESS) {
goto done;
}
isc_buffer_putuint8(&b, '\0');
}
continue;
continue;
result == ISC_R_SUCCESS;
sizeof(struct sockaddr_in6) :
sizeof(struct sockaddr_in)));
error = EAI_MEMORY;
goto done;
}
/*
* Set AF-specific parameters
*/
switch (family) {
case AF_INET:
&rdata_a,
NULL);
break;
case AF_INET6:
NULL);
break;
}
if (wantcname) {
error = EAI_MEMORY;
goto done;
}
}
}
}
}
done:
/* Make sure that error == 0 iff we have a non-empty list */
if (error == 0) {
error = EAI_NONAME;
} else {
}
}
/* Check whether we are done */
/*
* We're done for this state. If there is no other outstanding
* state, we can exit.
*/
return;
}
/*
* There are outstanding states, but if we are at the head
* of the state list (i.e., at the highest search priority)
* and have any answer, we can stop now by canceling the
* others.
*/
}
} else {
/*
* This search fails, so we move to the tail
* of the list so that the next entry will
* have the highest priority.
*/
}
}
}
}
static int
{
int terror = 0;
int error = 0;
/* get IRS context and the associated parameters */
if (result != ISC_R_SUCCESS)
return (EAI_FAIL);
/* construct resolution states */
head.activestates = 0;
if (result != ISC_R_SUCCESS) {
return (EAI_FAIL);
}
if (result != ISC_R_SUCCESS) {
return (EAI_FAIL);
}
0, task,
if (result == ISC_R_SUCCESS) {
} else
}
0, task,
if (result == ISC_R_SUCCESS) {
} else
}
}
if (!all_fail) {
/* Start all the events */
} else
/* Cleanup */
}
}
}
}
}
/*
* If the entire lookup fails, we need to choose an appropriate
* error code from individual codes. We'll try to provide as
* specific a code as possible. In general, we are going to
* find an error code other than EAI_NONAME (which is too
* generic and may actually not be problematic in some cases).
* EAI_NONAME will be set below if no better code is found.
*/
}
}
if (error == 0)
error = EAI_NONAME;
}
#if 1 /* XXX: enabled for finding leaks. should be cleaned up later. */
#endif
return (error);
}
static char *
char *s;
const char *d;
return (NULL);
for (s = string; *s != '\0'; s++) {
sc = *s;
*s++ = '\0';
*stringp = s;
return (string);
}
}
return (string);
}
static void
int, int))
{
int found;
if (family) {
switch (family) {
case AF_INET:
break;
case AF_INET6:
break;
}
} else {
found = 0;
/*
* We ignore any unknown names.
*/
if ((found & FOUND_IPV6) == 0)
found |= FOUND_IPV6;
if ((found & FOUND_IPV4) == 0)
found |= FOUND_IPV4;
}
}
/*
* Add in anything that we didn't find.
*/
if ((found & FOUND_IPV4) == 0)
if ((found & FOUND_IPV6) == 0)
}
return;
}
static int
{
return (EAI_MEMORY);
return (0);
}
static int
{
return (EAI_MEMORY);
return (0);
}
/*% Free address info. */
void
}
static void
if (ai->ai_canonname)
}
}
#ifdef AF_LOCAL
static int
if (socktype == 0)
return (EAI_SOCKTYPE);
return (EAI_MEMORY);
/*
* ai->ai_flags, ai->ai_protocol, ai->ai_canonname,
* and ai->ai_next were initialized to zero.
*/
return (0);
}
#endif
/*!
* Allocate an addrinfo structure, and a sockaddr structure
* of the specificed length. We initialize:
* ai_addrlen
* ai_family
* ai_addr
* ai_addr->sa_family
* ai_addr->sa_len (IRS_PLATFORM_HAVESALEN)
* and everything else is initialized to zero.
*/
static struct addrinfo *
return (NULL);
return (NULL);
}
#ifdef IRS_PLATFORM_HAVESALEN
#endif
return (ai);
}
static struct addrinfo *
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)));
return (NULL);
return (ai);
return (ai);
}
static struct addrinfo *
/*
* Grab one off the old list.
*/
/*
* Put it on the front of the new list.
*/
}
return (nai);
}
static struct addrinfo *
return (ai2);
return (ai1);
;
return (ai1);
}