#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef HAVE_GETADDRINFO
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "gethostbyname.h"
#include "getaddrinfo.h"
static struct addrinfo *
dup_addrinfo (struct addrinfo *info, void *addr, size_t addrlen)
{
struct addrinfo *ret;
ret = malloc (sizeof (struct addrinfo));
if (ret == NULL)
return NULL;
memcpy (ret, info, sizeof (struct addrinfo));
ret->ai_addr = malloc (addrlen);
if (ret->ai_addr == NULL)
{
free (ret);
return NULL;
}
memcpy (ret->ai_addr, addr, addrlen);
ret->ai_addrlen = addrlen;
return ret;
}
int
getaddrinfo (const char *nodename, const char *servname,
const struct addrinfo *hints, struct addrinfo **res)
{
struct hostent *hp;
struct servent *servent;
const char *socktype;
int port;
struct addrinfo hint, result;
struct addrinfo *ai, *sai, *eai;
struct ghbnctx ghbnctx;
char **addrs;
int code;
memset (&result, 0, sizeof result);
if (hints == NULL)
{
memset (&hint, 0, sizeof hint);
hint.ai_family = PF_UNSPEC;
hints = &hint;
}
if (servname == NULL && nodename == NULL)
return EAI_NONAME;
if (servname == NULL)
port = 0;
else {
if (hints->ai_socktype == SOCK_STREAM)
socktype = "tcp";
else if (hints->ai_socktype == SOCK_DGRAM)
socktype = "udp";
else
return EAI_SERVICE;
result.ai_socktype = hints->ai_socktype;
if (isdigit (*servname))
port = strtol (servname, NULL, 10);
else if ((servent = getservbyname (servname, socktype)) != NULL)
port = ntohs (servent->s_port);
else
return EAI_NONAME;
}
if (nodename == NULL)
{
struct sockaddr_in sin;
if (hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET)
result.ai_family = AF_INET;
else
return EAI_FAMILY;
sin.sin_family = result.ai_family;
sin.sin_port = htons (port);
if (hints->ai_flags & AI_PASSIVE)
sin.sin_addr.s_addr = htonl (INADDR_ANY);
else
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
*res = dup_addrinfo (&result, &sin, sizeof sin);
return (*res == NULL) ? EAI_MEMORY : 0;
}
if (hints->ai_flags & AI_NUMERICHOST)
{
struct sockaddr_in sin;
if (hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET)
result.ai_family = AF_INET;
else
return EAI_FAMILY;
sin.sin_family = result.ai_family;
sin.sin_port = htons (port);
sin.sin_addr.s_addr = inet_addr (nodename);
*res = dup_addrinfo (&result, &sin, sizeof sin);
return (*res == NULL) ? EAI_MEMORY : 0;
}
errno = 0;
hp = gethostbyname_ctx (nodename, &ghbnctx);
if (hp == NULL)
{
if (errno != 0)
{
free_ghbnctx (&ghbnctx);
return EAI_SYSTEM;
}
code = h_error_ctx (&ghbnctx);
switch (code)
{
case HOST_NOT_FOUND: code = EAI_NODATA; break;
case NO_DATA: code = EAI_NODATA; break;
#if defined(NO_ADDRESS) && NO_ADDRESS != NO_DATA
case NO_ADDRESS: code = EAI_NODATA; break;
#endif
case NO_RECOVERY: code = EAI_FAIL; break;
case TRY_AGAIN: code = EAI_AGAIN; break;
default: code = EAI_FAIL; break;
}
free_ghbnctx (&ghbnctx);
return code;
}
switch (hp->h_addrtype)
{
case AF_INET:
if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET))
goto eai_family;
break;
#ifdef USE_IPV6
case AF_INET6:
if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET6))
goto eai_family;
break;
#endif
default:
eai_family:
free_ghbnctx (&ghbnctx);
return EAI_FAMILY;
}
sai = eai = NULL;
for (addrs = hp->h_addr_list; *addrs != NULL; addrs++)
{
struct sockaddr sa;
size_t addrlen;
if (hp->h_length < 1)
continue;
sa.sa_family = hp->h_addrtype;
switch (hp->h_addrtype)
{
case AF_INET:
((struct sockaddr_in *) &sa)->sin_port = htons (port);
memcpy (&((struct sockaddr_in *) &sa)->sin_addr,
*addrs, hp->h_length);
addrlen = sizeof (struct sockaddr_in);
break;
#ifdef USE_IPV6
case AF_INET6:
# if SIN6_LEN
((struct sockaddr_in6 *) &sa)->sin6_len = hp->h_length;
# endif
((struct sockaddr_in6 *) &sa)->sin6_port = htons (port);
memcpy (&((struct sockaddr_in6 *) &sa)->sin6_addr,
*addrs, hp->h_length);
addrlen = sizeof (struct sockaddr_in6);
break;
#endif
default:
continue;
}
result.ai_family = hp->h_addrtype;
ai = dup_addrinfo (&result, &sa, addrlen);
if (ai == NULL)
{
free_ghbnctx (&ghbnctx);
freeaddrinfo (sai);
return EAI_MEMORY;
}
if (sai == NULL)
sai = ai;
else
eai->ai_next = ai;
eai = ai;
}
if (sai == NULL)
{
free_ghbnctx (&ghbnctx);
return EAI_NODATA;
}
if (hints->ai_flags & AI_CANONNAME)
{
sai->ai_canonname = malloc (strlen (hp->h_name) + 1);
if (sai->ai_canonname == NULL)
{
free_ghbnctx (&ghbnctx);
freeaddrinfo (sai);
return EAI_MEMORY;
}
strcpy (sai->ai_canonname, hp->h_name);
}
free_ghbnctx (&ghbnctx);
*res = sai;
return 0;
}
void
freeaddrinfo (struct addrinfo *ai)
{
struct addrinfo *next;
while (ai != NULL)
{
next = ai->ai_next;
if (ai->ai_canonname != NULL)
free (ai->ai_canonname);
if (ai->ai_addr != NULL)
free (ai->ai_addr);
free (ai);
ai = next;
}
}
const char *
gai_strerror (int ecode)
{
static const char *eai_descr[] =
{
"no error",
"address family for nodename 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 nodename",
"nodename nor servname provided, or not known",
"servname not supported for ai_socktype",
"ai_socktype not supported",
"system error returned in errno",
"argument buffer overflow",
};
if (ecode < 0 || ecode > (int) (sizeof eai_descr/ sizeof eai_descr[0]))
return "unknown error";
return eai_descr[ecode];
}
#endif