#include "port_before.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <resolv.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <irs.h>
#include "port_after.h"
#include "irs_data.h"
#undef T_A6
#define SUCCESS 0
#define ANY 0
#define YES 1
#define NO 0
static const char in_addrany[] = { 0, 0, 0, 0 };
static const char in6_addrany[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static const char in_loopback[] = { 127, 0, 0, 1 };
static const char in6_loopback[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
};
static const struct afd {
int a_af;
int a_addrlen;
int a_socklen;
int a_off;
const char *a_addrany;
const char *a_loopback;
int a_scoped;
} afdl [] = {
{PF_INET6, sizeof(struct in6_addr),
sizeof(struct sockaddr_in6),
offsetof(struct sockaddr_in6, sin6_addr),
in6_addrany, in6_loopback, 1},
{PF_INET, sizeof(struct in_addr),
sizeof(struct sockaddr_in),
offsetof(struct sockaddr_in, sin_addr),
in_addrany, in_loopback, 0},
{0, 0, 0, 0, NULL, NULL, 0},
};
struct explore {
int e_af;
int e_socktype;
int e_protocol;
const char *e_protostr;
int e_wild;
#define WILD_AF(ex) ((ex)->e_wild & 0x01)
#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
};
static const struct explore explore[] = {
#if 0
{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
#endif
{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
{ -1, 0, 0, NULL, 0 },
};
#define PTON_MAX 16
static int str_isnumber __P((const char *));
static int explore_fqdn __P((const struct addrinfo *, const char *,
const char *, struct addrinfo **));
static int explore_copy __P((const struct addrinfo *, const struct addrinfo *,
struct addrinfo **));
static int explore_null __P((const struct addrinfo *,
const char *, struct addrinfo **));
static int explore_numeric __P((const struct addrinfo *, const char *,
const char *, struct addrinfo **));
static int explore_numeric_scope __P((const struct addrinfo *, const char *,
const char *, struct addrinfo **));
static int get_canonname __P((const struct addrinfo *,
struct addrinfo *, const char *));
static struct addrinfo *get_ai __P((const struct addrinfo *,
const struct afd *, const char *));
static struct addrinfo *copy_ai __P((const struct addrinfo *));
static int get_portmatch __P((const struct addrinfo *, const char *));
static int get_port __P((const struct addrinfo *, const char *, int));
static const struct afd *find_afd __P((int));
static int addrconfig __P((int));
static int ip6_str2scopeid __P((char *, struct sockaddr_in6 *));
static struct net_data *init __P((void));
struct addrinfo *hostent2addrinfo __P((struct hostent *,
const struct addrinfo *));
struct addrinfo *addr2addrinfo __P((const struct addrinfo *,
const char *));
#if 0
static const char *ai_errlist[] = {
"Success",
"Address family for hostname not supported",
"Temporary failure in name resolution",
"Invalid value for ai_flags",
"Non-recoverable failure in name resolution",
"ai_family not supported",
"Memory allocation failure",
"No address associated with hostname",
"hostname nor servname provided, or not known",
"servname not supported for ai_socktype",
"ai_socktype not supported",
"System error returned in errno",
"Invalid value for hints",
"Resolved protocol is unknown",
"Unknown error",
};
#endif
#define GET_AI(ai, afd, addr) \
do { \
\
(ai) = get_ai(pai, (afd), (addr)); \
if ((ai) == NULL) { \
error = EAI_MEMORY; \
goto free; \
} \
} while (0)
#define GET_PORT(ai, serv) \
do { \
\
error = get_port((ai), (serv), 0); \
if (error != 0) \
goto free; \
} while (0)
#define GET_CANONNAME(ai, str) \
do { \
\
error = get_canonname(pai, (ai), (str)); \
if (error != 0) \
goto free; \
} while (0)
#define ERR(err) \
do { \
\
error = (err); \
goto bad; \
\
} while (0)
#define MATCH_FAMILY(x, y, w) \
((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
#define MATCH(x, y, w) \
((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
#if 0
char *
gai_strerror(ecode)
int ecode;
{
if (ecode < 0 || ecode > EAI_MAX)
ecode = EAI_MAX;
return ai_errlist[ecode];
}
#endif
void
freeaddrinfo(ai)
struct addrinfo *ai;
{
struct addrinfo *next;
do {
next = ai->ai_next;
if (ai->ai_canonname)
free(ai->ai_canonname);
free(ai);
ai = next;
} while (ai);
}
static int
str_isnumber(p)
const char *p;
{
char *ep;
if (*p == '\0')
return NO;
ep = NULL;
(void)strtoul(p, &ep, 10);
if (ep && *ep == '\0')
return YES;
else
return NO;
}
int
getaddrinfo(hostname, servname, hints, res)
const char *hostname, *servname;
const struct addrinfo *hints;
struct addrinfo **res;
{
struct addrinfo sentinel;
struct addrinfo *cur;
int error = 0;
struct addrinfo ai, ai0, *afai = NULL;
struct addrinfo *pai;
const struct explore *ex;
memset(&sentinel, 0, sizeof(sentinel));
cur = &sentinel;
pai = &ai;
pai->ai_flags = 0;
pai->ai_family = PF_UNSPEC;
pai->ai_socktype = ANY;
pai->ai_protocol = ANY;
pai->ai_addrlen = 0;
pai->ai_canonname = NULL;
pai->ai_addr = NULL;
pai->ai_next = NULL;
if (hostname == NULL && servname == NULL)
return EAI_NONAME;
if (hints) {
if (hints->ai_addrlen || hints->ai_canonname ||
hints->ai_addr || hints->ai_next)
ERR(EAI_BADHINTS);
if (hints->ai_flags & ~AI_MASK)
ERR(EAI_BADFLAGS);
switch (hints->ai_family) {
case PF_UNSPEC:
case PF_INET:
case PF_INET6:
break;
default:
ERR(EAI_FAMILY);
}
memcpy(pai, hints, sizeof(*pai));
if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
for (ex = explore; ex->e_af >= 0; ex++) {
if (pai->ai_family != ex->e_af)
continue;
if (ex->e_socktype == ANY)
continue;
if (ex->e_protocol == ANY)
continue;
if (pai->ai_socktype == ex->e_socktype &&
pai->ai_protocol != ex->e_protocol) {
ERR(EAI_BADHINTS);
}
}
}
}
switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
case AI_V4MAPPED:
case AI_ALL | AI_V4MAPPED:
if (pai->ai_family != AF_INET6)
pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
break;
case AI_ALL:
#if 1
ERR(EAI_BADFLAGS);
#else
pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
break;
#endif
}
if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
#ifdef PF_INET6
|| MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
#endif
) {
ai0 = *pai;
if (pai->ai_family == PF_UNSPEC) {
#ifdef PF_INET6
pai->ai_family = PF_INET6;
#else
pai->ai_family = PF_INET;
#endif
}
error = get_portmatch(pai, servname);
if (error)
ERR(error);
*pai = ai0;
}
ai0 = *pai;
for (ex = explore; ex->e_af >= 0; ex++) {
*pai = ai0;
if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
continue;
if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
continue;
if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
continue;
if (pai->ai_family == PF_UNSPEC)
pai->ai_family = ex->e_af;
if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
pai->ai_socktype = ex->e_socktype;
if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
pai->ai_protocol = ex->e_protocol;
if (get_portmatch(pai, servname) != 0)
continue;
if (hostname == NULL) {
if (!addrconfig(pai->ai_family))
continue;
error = explore_null(pai, servname, &cur->ai_next);
} else
error = explore_numeric_scope(pai, hostname, servname,
&cur->ai_next);
if (error)
goto free;
while (cur && cur->ai_next)
cur = cur->ai_next;
}
if (sentinel.ai_next)
goto good;
if (pai->ai_flags & AI_NUMERICHOST)
ERR(EAI_NONAME);
if (hostname == NULL)
ERR(EAI_NONAME);
*pai = ai0;
error = explore_fqdn(pai, hostname, servname, &afai);
if (error) {
if (afai != NULL)
freeaddrinfo(afai);
goto free;
}
if (afai == NULL) {
error = EAI_NONAME;
goto free;
}
for (ex = explore; ex->e_af >= 0; ex++) {
*pai = ai0;
if (pai->ai_family == PF_UNSPEC)
pai->ai_family = ex->e_af;
if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
continue;
if (!MATCH(pai->ai_socktype, ex->e_socktype,
WILD_SOCKTYPE(ex))) {
continue;
}
if (!MATCH(pai->ai_protocol, ex->e_protocol,
WILD_PROTOCOL(ex))) {
continue;
}
#ifdef AI_ADDRCONFIG
if ((pai->ai_flags & AI_ADDRCONFIG) != 0 &&
!addrconfig(pai->ai_family))
continue;
#endif
if (pai->ai_family == PF_UNSPEC)
pai->ai_family = ex->e_af;
if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
pai->ai_socktype = ex->e_socktype;
if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
pai->ai_protocol = ex->e_protocol;
if (get_portmatch(pai, servname) != 0)
continue;
if ((error = explore_copy(pai, afai, &cur->ai_next)) != 0) {
freeaddrinfo(afai);
goto free;
}
while (cur && cur->ai_next)
cur = cur->ai_next;
}
freeaddrinfo(afai);
if (error != 0)
abort();
if (sentinel.ai_next) {
good:
*res = sentinel.ai_next;
return(SUCCESS);
} else {
error = EAI_NONAME;
}
free:
bad:
if (sentinel.ai_next)
freeaddrinfo(sentinel.ai_next);
*res = NULL;
return(error);
}
static int
explore_fqdn(pai, hostname, servname, res)
const struct addrinfo *pai;
const char *hostname;
const char *servname;
struct addrinfo **res;
{
struct addrinfo *result;
struct addrinfo *cur;
struct net_data *net_data = init();
struct irs_ho *ho;
int error = 0;
char tmp[NS_MAXDNAME];
const char *cp;
result = NULL;
if (get_portmatch(pai, servname) != 0)
return(0);
if (!net_data || !(ho = net_data->ho))
return(0);
#if 0
if (net_data->ho_stayopen && net_data->ho_last &&
net_data->ho_last->h_addrtype == af) {
if (ns_samename(name, net_data->ho_last->h_name) == 1)
return (net_data->ho_last);
for (hap = net_data->ho_last->h_aliases; hap && *hap; hap++)
if (ns_samename(name, *hap) == 1)
return (net_data->ho_last);
}
#endif
if (!strchr(hostname, '.') &&
(cp = res_hostalias(net_data->res, hostname,
tmp, sizeof(tmp))))
hostname = cp;
result = (*ho->addrinfo)(ho, hostname, pai);
if (!net_data->ho_stayopen) {
(*ho->minimize)(ho);
}
if (result == NULL) {
int e = h_errno;
switch(e) {
case NETDB_INTERNAL:
error = EAI_SYSTEM;
break;
case TRY_AGAIN:
error = EAI_AGAIN;
break;
case NO_RECOVERY:
error = EAI_FAIL;
break;
case HOST_NOT_FOUND:
case NO_DATA:
error = EAI_NONAME;
break;
default:
case NETDB_SUCCESS:
error = EAI_NONAME;
break;
}
goto free;
}
for (cur = result; cur; cur = cur->ai_next) {
GET_PORT(cur, servname);
}
*res = result;
return(0);
free:
if (result)
freeaddrinfo(result);
return error;
}
static int
explore_copy(pai, src0, res)
const struct addrinfo *pai;
const struct addrinfo *src0;
struct addrinfo **res;
{
int error;
struct addrinfo sentinel, *cur;
const struct addrinfo *src;
error = 0;
sentinel.ai_next = NULL;
cur = &sentinel;
for (src = src0; src != NULL; src = src->ai_next) {
if (src->ai_family != pai->ai_family)
continue;
cur->ai_next = copy_ai(src);
if (!cur->ai_next) {
error = EAI_MEMORY;
goto fail;
}
cur->ai_next->ai_socktype = pai->ai_socktype;
cur->ai_next->ai_protocol = pai->ai_protocol;
cur = cur->ai_next;
}
*res = sentinel.ai_next;
return 0;
fail:
freeaddrinfo(sentinel.ai_next);
return error;
}
static int
explore_null(pai, servname, res)
const struct addrinfo *pai;
const char *servname;
struct addrinfo **res;
{
const struct afd *afd;
struct addrinfo *cur;
struct addrinfo sentinel;
int error;
*res = NULL;
sentinel.ai_next = NULL;
cur = &sentinel;
afd = find_afd(pai->ai_family);
if (afd == NULL)
return 0;
if (pai->ai_flags & AI_PASSIVE) {
GET_AI(cur->ai_next, afd, afd->a_addrany);
GET_PORT(cur->ai_next, servname);
} else {
GET_AI(cur->ai_next, afd, afd->a_loopback);
GET_PORT(cur->ai_next, servname);
}
cur = cur->ai_next;
*res = sentinel.ai_next;
return 0;
free:
if (sentinel.ai_next)
freeaddrinfo(sentinel.ai_next);
return error;
}
static int
explore_numeric(pai, hostname, servname, res)
const struct addrinfo *pai;
const char *hostname;
const char *servname;
struct addrinfo **res;
{
const struct afd *afd;
struct addrinfo *cur;
struct addrinfo sentinel;
int error;
char pton[PTON_MAX];
*res = NULL;
sentinel.ai_next = NULL;
cur = &sentinel;
afd = find_afd(pai->ai_family);
if (afd == NULL)
return 0;
switch (afd->a_af) {
#if 0
case AF_INET:
if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
if (pai->ai_family == afd->a_af ||
pai->ai_family == PF_UNSPEC ) {
GET_AI(cur->ai_next, afd, pton);
GET_PORT(cur->ai_next, servname);
while (cur && cur->ai_next)
cur = cur->ai_next;
} else
ERR(EAI_FAMILY);
}
break;
#endif
default:
if (inet_pton(afd->a_af, hostname, pton) == 1) {
if (pai->ai_family == afd->a_af ||
pai->ai_family == PF_UNSPEC ) {
GET_AI(cur->ai_next, afd, pton);
GET_PORT(cur->ai_next, servname);
while (cur && cur->ai_next)
cur = cur->ai_next;
} else
ERR(EAI_FAMILY);
}
break;
}
*res = sentinel.ai_next;
return 0;
free:
bad:
if (sentinel.ai_next)
freeaddrinfo(sentinel.ai_next);
return error;
}
static int
explore_numeric_scope(pai, hostname, servname, res)
const struct addrinfo *pai;
const char *hostname;
const char *servname;
struct addrinfo **res;
{
#ifndef SCOPE_DELIMITER
return explore_numeric(pai, hostname, servname, res);
#else
const struct afd *afd;
struct addrinfo *cur;
int error;
char *cp, *hostname2 = NULL, *scope, *addr;
struct sockaddr_in6 *sin6;
afd = find_afd(pai->ai_family);
if (afd == NULL)
return 0;
if (!afd->a_scoped)
return explore_numeric(pai, hostname, servname, res);
cp = strchr(hostname, SCOPE_DELIMITER);
if (cp == NULL)
return explore_numeric(pai, hostname, servname, res);
hostname2 = strdup(hostname);
if (hostname2 == NULL)
return EAI_MEMORY;
hostname2[cp - hostname] = '\0';
addr = hostname2;
scope = cp + 1;
error = explore_numeric(pai, addr, servname, res);
if (error == 0) {
int scopeid;
for (cur = *res; cur; cur = cur->ai_next) {
if (cur->ai_family != AF_INET6)
continue;
sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
if ((scopeid = ip6_str2scopeid(scope, sin6)) == -1) {
free(hostname2);
return(EAI_NONAME);
}
#ifdef HAVE_SIN6_SCOPE_ID
sin6->sin6_scope_id = scopeid;
#endif
}
}
free(hostname2);
return error;
#endif
}
static int
get_canonname(pai, ai, str)
const struct addrinfo *pai;
struct addrinfo *ai;
const char *str;
{
if ((pai->ai_flags & AI_CANONNAME) != 0) {
ai->ai_canonname = (char *)malloc(strlen(str) + 1);
if (ai->ai_canonname == NULL)
return EAI_MEMORY;
strcpy(ai->ai_canonname, str);
}
return 0;
}
static struct addrinfo *
get_ai(pai, afd, addr)
const struct addrinfo *pai;
const struct afd *afd;
const char *addr;
{
char *p;
struct addrinfo *ai;
ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
+ (afd->a_socklen));
if (ai == NULL)
return NULL;
memcpy(ai, pai, sizeof(struct addrinfo));
ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
#ifdef HAVE_SA_LEN
ai->ai_addr->sa_len = afd->a_socklen;
#endif
ai->ai_addrlen = afd->a_socklen;
ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
p = (char *)(void *)(ai->ai_addr);
memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
return ai;
}
static struct addrinfo *
copy_ai(pai)
const struct addrinfo *pai;
{
struct addrinfo *ai;
size_t l;
l = sizeof(*ai) + pai->ai_addrlen;
if ((ai = (struct addrinfo *)malloc(l)) == NULL)
return NULL;
memset(ai, 0, l);
memcpy(ai, pai, sizeof(*ai));
ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
memcpy(ai->ai_addr, pai->ai_addr, pai->ai_addrlen);
if (pai->ai_canonname) {
l = strlen(pai->ai_canonname) + 1;
if ((ai->ai_canonname = malloc(l)) == NULL) {
free(ai);
return NULL;
}
#ifdef HAVE_STRLCPY
strlcpy(ai->ai_canonname, pai->ai_canonname, l);
#else
strncpy(ai->ai_canonname, pai->ai_canonname, l);
#endif
} else {
ai->ai_canonname = NULL;
}
ai->ai_next = NULL;
return ai;
}
static int
get_portmatch(const struct addrinfo *ai, const char *servname) {
return get_port((const struct addrinfo *)ai, servname, 1);
}
static int
get_port(const struct addrinfo *ai, const char *servname, int matchonly) {
const char *proto;
struct servent *sp;
int port;
int allownumeric;
if (servname == NULL)
return 0;
switch (ai->ai_family) {
case AF_INET:
#ifdef AF_INET6
case AF_INET6:
#endif
break;
default:
return 0;
}
switch (ai->ai_socktype) {
case SOCK_RAW:
return EAI_SERVICE;
case SOCK_DGRAM:
case SOCK_STREAM:
allownumeric = 1;
break;
case ANY:
allownumeric = 0;
break;
default:
return EAI_SOCKTYPE;
}
if (str_isnumber(servname)) {
if (!allownumeric)
return EAI_SERVICE;
port = htons(atoi(servname));
if (port < 0 || port > 65535)
return EAI_SERVICE;
} else {
switch (ai->ai_socktype) {
case SOCK_DGRAM:
proto = "udp";
break;
case SOCK_STREAM:
proto = "tcp";
break;
default:
proto = NULL;
break;
}
if ((sp = getservbyname(servname, proto)) == NULL)
return EAI_SERVICE;
port = sp->s_port;
}
if (!matchonly) {
switch (ai->ai_family) {
case AF_INET:
((struct sockaddr_in *)(void *)
ai->ai_addr)->sin_port = port;
break;
case AF_INET6:
((struct sockaddr_in6 *)(void *)
ai->ai_addr)->sin6_port = port;
break;
}
}
return 0;
}
static const struct afd *
find_afd(af)
int af;
{
const struct afd *afd;
if (af == PF_UNSPEC)
return NULL;
for (afd = afdl; afd->a_af; afd++) {
if (afd->a_af == af)
return afd;
}
return NULL;
}
static int
addrconfig(af)
int af;
{
int s;
s = socket(af, SOCK_DGRAM, 0);
if (s < 0) {
if (errno != EMFILE)
return 0;
} else
close(s);
return 1;
}
static int
ip6_str2scopeid(scope, sin6)
char *scope;
struct sockaddr_in6 *sin6;
{
int scopeid;
struct in6_addr *a6 = &sin6->sin6_addr;
char *ep;
if (*scope == '\0')
return -1;
#ifdef USE_IFNAMELINKID
if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
scopeid = if_nametoindex(scope);
if (scopeid == 0)
goto trynumeric;
return(scopeid);
}
#endif
if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
goto trynumeric;
if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
goto trynumeric;
else
goto trynumeric;
trynumeric:
scopeid = (int)strtoul(scope, &ep, 10);
if (*ep == '\0')
return scopeid;
else
return -1;
}
struct addrinfo *
hostent2addrinfo(hp, pai)
struct hostent *hp;
const struct addrinfo *pai;
{
int i, af, error = 0;
char **aplist = NULL, *ap;
struct addrinfo sentinel, *cur;
const struct afd *afd;
af = hp->h_addrtype;
if (pai->ai_family != AF_UNSPEC && af != pai->ai_family)
return(NULL);
afd = find_afd(af);
if (afd == NULL)
return(NULL);
aplist = hp->h_addr_list;
memset(&sentinel, 0, sizeof(sentinel));
cur = &sentinel;
for (i = 0; (ap = aplist[i]) != NULL; i++) {
#if 0
af = hp->h_addr_list;
if (af == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
af = AF_INET;
ap = ap + sizeof(struct in6_addr)
- sizeof(struct in_addr);
}
afd = find_afd(af);
if (afd == NULL)
continue;
#endif
GET_AI(cur->ai_next, afd, ap);
if ((pai->ai_flags & AI_CANONNAME) != 0) {
GET_CANONNAME(cur->ai_next, hp->h_name);
}
while (cur && cur->ai_next)
cur = cur->ai_next;
continue;
free:
if (cur->ai_next)
freeaddrinfo(cur->ai_next);
cur->ai_next = NULL;
}
return(sentinel.ai_next);
}
struct addrinfo *
addr2addrinfo(pai, cp)
const struct addrinfo *pai;
const char *cp;
{
const struct afd *afd;
afd = find_afd(pai->ai_family);
if (afd == NULL)
return(NULL);
return(get_ai(pai, afd, cp));
}
static struct net_data *
init()
{
struct net_data *net_data;
if (!(net_data = net_data_init(NULL)))
goto error;
if (!net_data->ho) {
net_data->ho = (*net_data->irs->ho_map)(net_data->irs);
if (!net_data->ho || !net_data->res) {
error:
errno = EIO;
if (net_data && net_data->res)
RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
return (NULL);
}
(*net_data->ho->res_set)(net_data->ho, net_data->res, NULL);
}
return (net_data);
}