30RCSID(
"$Id: a8d8852aca395f83b384899f3219afa6deedbfdf $")
36#include <sys/socket.h>
40#ifndef HAVE_GETNAMEINFO
41# undef LOCAL_GETHOSTBYNAMERSTYLE
42# ifndef GETHOSTBYNAMERSTYLE
43# define LOCAL_GETHOSTBYNAMERSTYLE 1
44#elif (GETHOSTBYNAMERSTYLE != SYSVSTYLE) && (GETHOSTBYNAMERSTYLE != GNUSTYLE)
45# define LOCAL_GETHOSTBYNAMERSTYLE 1
49#ifndef HAVE_GETADDRINFO
50# undef LOCAL_GETHOSTBYADDRR
51# ifndef GETHOSTBYADDRRSTYLE
52# define LOCAL_GETHOSTBYADDRR 1
53# elif (GETHOSTBYADDRRSTYLE != SYSVSTYLE) && (GETHOSTBYADDRRSTYLE != GNUSTYLE)
54# define LOCAL_GETHOSTBYADDRR 1
65#ifdef LOCAL_GETHOSTBYNAMERSTYLE
70#ifdef LOCAL_GETHOSTBYNAMERSTYLE
94#if defined(LOCAL_GETHOSTBYNAMER) || defined(LOCAL_GETHOSTBYADDRR)
95static int copy_hostent(
struct hostent *from,
struct hostent *to,
char *
buffer,
size_t buflen,
int *error)
103 to->h_addrtype = from->h_addrtype;
104 to->h_length = from->h_length;
105 to->h_name = (
char *) ptr;
108 len = strlen(from->h_name) + 1;
114 memcpy(ptr, from->h_name, len);
118 mask &= _Alignof(
char **) - 1;
120 mask &= _Alignof(
char **) - 1;
122 if (
mask > ((
size_t) (end - ptr)))
goto overflow;
131 to->h_aliases = (
char **) (uintptr_t) ptr;
133 for (i = 0; from->h_aliases[i] != NULL; i++) {
137 len = (i + 1) *
sizeof(
char *);
138 if (len > (
size_t) (end - ptr))
goto overflow;
141 for (i = 0; from->h_aliases[i]; i++) {
142 len = strlen(from->h_aliases[i]) + 1;
143 if (len > (
size_t) (end - ptr))
goto overflow;
145 to->h_aliases[i] = ptr;
146 memcpy(ptr, from->h_aliases[i], len);
149 to->h_aliases[i] = NULL;
157 mask &= _Alignof(
char **) - 1;
159 mask &= _Alignof(
char **) - 1;
161 if (
mask > ((
size_t) (end - ptr)))
goto overflow;
165 to->h_addr_list = (
char **) (uintptr_t) ptr;
167 for (i = 0; from->h_addr_list[i] != NULL; i++) {
172 if (len > (
size_t) (end - ptr))
goto overflow;
178 len = i * from->h_length;
179 if (len > (
size_t) (end - ptr))
goto overflow;
181 for (i = 0; from->h_addr_list[i] != NULL; i++) {
182 to->h_addr_list[i] = ptr;
183 memcpy(ptr, from->h_addr_list[i], from->h_length);
184 ptr += from->h_length;
186 to->h_addr_list[i] = NULL;
192#ifdef LOCAL_GETHOSTBYNAMERSTYLE
193static struct hostent *
195 char *
buffer,
int buflen,
int *error)
205 hp = gethostbyname(hostname);
221#ifdef LOCAL_GETHOSTBYADDRR
223 char *
buffer,
int buflen,
int *error)
233 hp = gethostbyaddr(addr, len,
type);
255#ifndef HAVE_GETADDRINFO
260 MEM(ai = (
struct addrinfo *)talloc_zero_array(NULL,
uint8_t,
261 sizeof(
struct addrinfo) +
sizeof(
struct sockaddr_in)));
262 ai->ai_addr = (
struct sockaddr *)(ai + 1);
263 ai->ai_addrlen =
sizeof(
struct sockaddr_in);
264# ifdef HAVE_SOCKADDR_SA_LEN
265 ai->ai_addr->sa_len =
sizeof(
struct sockaddr_in);
267 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
268 ((
struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
269 ((
struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
270 ai->ai_socktype = socktype;
271 ai->ai_protocol = proto;
280 return "memory allocation failure";
283 return "ai_family not supported";
286 return "hostname nor servname provided, or not known";
289 return "servname not supported for ai_socktype";
292 return "unknown error";
298 struct addrinfo *next;
303 }
while ((ai = next) != NULL);
306int getaddrinfo(
char const *hostname,
char const *servname,
struct addrinfo
const *hints,
struct addrinfo **res)
308 struct addrinfo *cur, *prev = NULL;
310 struct hostent result;
312 int i, socktype, proto;
317 if (hints && (hints->ai_family != PF_INET) && (hints->ai_family != PF_UNSPEC))
return EAI_FAMILY;
319 socktype = (hints && hints->ai_socktype) ? hints->ai_socktype : SOCK_STREAM;
320 if (hints && hints->ai_protocol) {
321 proto = hints->ai_protocol;
337 if (isdigit((
uint8_t)*servname)) {
338 port = htons(atoi(servname));
341 char const *pe_proto;
356 if ((se = getservbyname(servname, pe_proto)) == NULL)
return EAI_SERVICE;
363 if (hints && hints->ai_flags & AI_PASSIVE) {
364 *res =
alloc_ai(port, htonl(0x00000000), socktype, proto);
366 *res =
alloc_ai(port, htonl(0x7f000001), socktype, proto);
368 if (!*res)
return EAI_MEMORY;
375 *res =
alloc_ai(port,
in.s_addr, socktype, proto);
376 if (!*res)
return EAI_MEMORY;
381 if (hints && hints->ai_flags & AI_NUMERICHOST)
return EAI_NONAME;
384#ifdef GETHOSTBYNAMERSTYLE
385# if GETHOSTBYNAMERSTYLE == SYSVSTYLE
387# elif GETHOSTBYNAMERSTYLE == GNUSTYLE
396 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
397 for (i = 0; hp->h_addr_list[i]; i++) {
398 if ((cur =
alloc_ai(port, ((
struct in_addr *)hp->h_addr_list[i])->s_addr,
399 socktype, proto)) == NULL) {
412 if (hints && hints->ai_flags & AI_CANONNAME && *res) {
413 if (((*res)->ai_canonname =
talloc_strdup(*res, hp->h_name)) == NULL) {
425#ifndef HAVE_GETNAMEINFO
426int getnameinfo(
struct sockaddr
const *sa, socklen_t salen,
char *host,
size_t hostlen,
char *serv,
size_t servlen,
429 const struct sockaddr_in *sin = (
struct sockaddr_in const *)sa;
431 struct hostent result;
437 snprintf(tmpserv,
sizeof(tmpserv),
"%d", ntohs(sin->sin_port));
438 if (strlen(tmpserv) > servlen)
return EAI_MEMORY;
443 if (flags & NI_NUMERICHOST) {
445 if (flags & NI_NAMEREQD)
return EAI_NONAME;
446 if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
return EAI_MEMORY;
448 strcpy(host, inet_ntoa(sin->sin_addr));
452#ifdef GETHOSTBYADDRRSTYLE
453# if GETHOSTBYADDRRSTYLE == SYSVSTYLE
455 salen, AF_INET, &result,
buffer,
sizeof(
buffer), &error);
456# elif GETHOSTBYADDRRSTYLE == GNUSTYLE
470 if (strlen(hp->h_name) >= hostlen)
return EAI_MEMORY;
476 if (flags & NI_NAMEREQD)
return EAI_NONAME;
477 if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
return EAI_MEMORY;
479 strcpy(host, inet_ntoa(sin->sin_addr));
static int const char char buffer[256]
strcpy(log_entry->msg, buffer)
int getaddrinfo(char const *hostname, char const *servname, struct addrinfo const *hints, struct addrinfo **res)
static pthread_mutex_t fr_hostbyname_mutex
char const * gai_strerror(int ecode)
void freeaddrinfo(struct addrinfo *ai)
static int copy_hostent(struct hostent *from, struct hostent *to, char *buffer, size_t buflen, int *error)
int getnameinfo(struct sockaddr const *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, unsigned int flags)
static struct addrinfo * alloc_ai(uint16_t port, u_long addr, int socktype, int proto)
static pthread_mutex_t fr_hostbyaddr_mutex
static struct hostent * gethostbyaddr_r(char const *addr, int len, int type, struct hostent *result, char *buffer, int buflen, int *error)
static struct hostent * gethostbyname_r(char const *hostname, struct hostent *result, char *buffer, int buflen, int *error)
int inet_aton(char const *cp, struct in_addr *inp)
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
fr_aka_sim_id_type_t type
#define talloc_strdup(_ctx, _str)