#include <config.h>
#include <sys/types.h>
#include <ctype.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <isc/net.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include "ntp_rfc2553.h"
#include "ntpd.h"
#include "ntp_malloc.h"
#include "ntp_stdlib.h"
#include "ntp_string.h"
#ifndef ISC_PLATFORM_HAVEIPV6
static 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",
};
int
DNSlookup_name(
const char *name,
int ai_family,
struct hostent **Addresses
);
#ifndef SYS_WINNT
int
DNSlookup_name(
const char *name,
int ai_family,
struct hostent **Addresses
)
{
*Addresses = gethostbyname(name);
return (h_errno);
}
#endif
static int do_nodename (const char *nodename, struct addrinfo *ai,
const struct addrinfo *hints);
int
getaddrinfo (const char *nodename, const char *servname,
const struct addrinfo *hints, struct addrinfo **res)
{
int rval;
struct servent *sp;
struct addrinfo *ai = NULL;
int port;
const char *proto = NULL;
int family, socktype, flags, protocol;
if (nodename == NULL && servname == NULL)
return (EAI_NONAME);
ai = calloc(sizeof(struct addrinfo), 1);
if (ai == NULL)
return (EAI_MEMORY);
if (hints != NULL) {
ai->ai_flags = hints->ai_flags;
ai->ai_family = hints->ai_family;
ai->ai_socktype = hints->ai_socktype;
ai->ai_protocol = hints->ai_protocol;
family = hints->ai_family;
socktype = hints->ai_socktype;
protocol = hints->ai_protocol;
flags = hints->ai_flags;
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;
}
rval = do_nodename(nodename, ai, hints);
if (rval != 0) {
freeaddrinfo(ai);
return (rval);
}
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;
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;
}
int
getnameinfo (const struct sockaddr *sa, u_int salen, char *host,
size_t hostlen, char *serv, size_t servlen, int flags)
{
struct hostent *hp;
int namelen;
if (sa->sa_family != AF_INET)
return (EAI_FAMILY);
hp = gethostbyaddr(
(const char *)&((const struct sockaddr_in *)sa)->sin_addr,
4, AF_INET);
if (hp == NULL) {
if (h_errno == TRY_AGAIN)
return (EAI_AGAIN);
else
return (EAI_FAIL);
}
if (host != NULL && hostlen > 0) {
namelen = min(strlen(hp->h_name), hostlen - 1);
if (namelen > 0) {
strncpy(host, hp->h_name, namelen);
host[namelen] = '\0';
}
}
return (0);
}
char *
gai_strerror(int ecode)
{
if (ecode < 0 || ecode > EAI_MAX)
ecode = EAI_MAX;
return ai_errlist[ecode];
}
static int
do_nodename(
const char *nodename,
struct addrinfo *ai,
const struct addrinfo *hints)
{
struct hostent *hp = NULL;
struct sockaddr_in *sockin;
struct sockaddr_in6 *sockin6;
int errval;
ai->ai_addr = calloc(sizeof(struct sockaddr_storage), 1);
if (ai->ai_addr == NULL)
return (EAI_MEMORY);
if (nodename == NULL) {
ai->ai_addrlen = sizeof(struct sockaddr_storage);
if (ai->ai_family == AF_INET)
{
sockin = (struct sockaddr_in *)ai->ai_addr;
sockin->sin_family = (short) ai->ai_family;
sockin->sin_addr.s_addr = htonl(INADDR_ANY);
}
else
{
sockin6 = (struct sockaddr_in6 *)ai->ai_addr;
sockin6->sin6_family = (short) ai->ai_family;
}
#ifdef ISC_PLATFORM_HAVESALEN
ai->ai_addr->sa_len = SOCKLEN(ai->ai_addr);
#endif
return (0);
}
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);
}
}
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 (hints != NULL && (hints->ai_flags & AI_NUMERICHOST))
return (EAI_NONAME);
errval = DNSlookup_name(nodename, AF_INET, &hp);
if (hp == NULL) {
if (errval == TRY_AGAIN || errval == EAI_AGAIN)
return (EAI_AGAIN);
else if (errval == EAI_NONAME) {
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);
}
return (errval);
}
else
{
return (errval);
}
}
ai->ai_family = hp->h_addrtype;
ai->ai_addrlen = sizeof(struct sockaddr);
sockin = (struct sockaddr_in *)ai->ai_addr;
memcpy(&sockin->sin_addr, hp->h_addr, hp->h_length);
ai->ai_addr->sa_family = hp->h_addrtype;
#ifdef ISC_PLATFORM_HAVESALEN
ai->ai_addr->sa_len = sizeof(struct sockaddr);
#endif
if (hints != NULL && hints->ai_flags & AI_CANONNAME)
ai->ai_canonname = estrdup(hp->h_name);
return (0);
}
#endif