30 RCSID(
"$Id: 56294125bc1d3b9b148cef87e9469f87973f19bd $")
34 #include <sys/param.h>
36 #ifndef HAVE_GETNAMEINFO
37 # undef LOCAL_GETHOSTBYNAMERSTYLE
38 # ifndef GETHOSTBYNAMERSTYLE
39 # define LOCAL_GETHOSTBYNAMERSTYLE 1
40 #elif (GETHOSTBYNAMERSTYLE != SYSVSTYLE) && (GETHOSTBYNAMERSTYLE != GNUSTYLE)
41 # define LOCAL_GETHOSTBYNAMERSTYLE 1
45 #ifndef HAVE_GETADDRINFO
46 # undef LOCAL_GETHOSTBYADDRR
47 # ifndef GETHOSTBYADDRRSTYLE
48 # define LOCAL_GETHOSTBYADDRR 1
49 # elif (GETHOSTBYADDRRSTYLE != SYSVSTYLE) && (GETHOSTBYADDRRSTYLE != GNUSTYLE)
50 # define LOCAL_GETHOSTBYADDRR 1
61 #ifdef LOCAL_GETHOSTBYNAMERSTYLE
66 #ifdef LOCAL_GETHOSTBYNAMERSTYLE
90 #if defined(LOCAL_GETHOSTBYNAMER) || defined(LOCAL_GETHOSTBYADDRR)
91 # define BUFFER_OVERFLOW 255
92 static int copy_hostent(
struct hostent *from,
struct hostent *to,
char *
buffer,
int buflen,
int *error)
98 to->h_addrtype = from->h_addrtype;
99 to->h_length = from->h_length;
100 to->h_name = (
char *)ptr;
103 len = strlen(from->h_name) + 1;
104 strcpy(ptr, from->h_name);
108 to->h_aliases = (
char**)ptr;
109 for (i = 0; from->h_aliases[i]; i++);
110 ptr += (i+1) *
sizeof(
char *);
112 for (i = 0; from->h_aliases[i]; i++) {
113 len = strlen(from->h_aliases[i])+1;
114 if ((ptr-
buffer) + len < buflen) {
115 to->h_aliases[i] = ptr;
116 strcpy(ptr, from->h_aliases[i]);
123 to->h_aliases[i] = NULL;
126 to->h_addr_list = (
char**)ptr;
127 for (i = 0; (
int *)from->h_addr_list[i] != 0; i++);
128 ptr += (i + 1) *
sizeof(
int *);
130 for (i = 0; (
int *)from->h_addr_list[i] != 0; i++) {
133 if ((ptr-
buffer)+len < buflen) {
134 to->h_addr_list[i] = ptr;
135 memcpy(ptr, from->h_addr_list[i], len);
142 to->h_addr_list[i] = 0;
147 #ifdef LOCAL_GETHOSTBYNAMERSTYLE
148 static struct hostent *
150 char *
buffer,
int buflen,
int *error)
161 if ((!hp) || (hp->h_addrtype != AF_INET) || (hp->h_length != 4)) {
176 #ifdef LOCAL_GETHOSTBYADDRR
178 char *
buffer,
int buflen,
int *error)
188 hp = gethostbyaddr(addr, len,
type);
189 if ((!hp) || (hp->h_addrtype != AF_INET) || (hp->h_length != 4)) {
210 #ifndef HAVE_GETADDRINFO
215 MEM(ai = (
struct addrinfo *)talloc_zero_array(NULL,
uint8_t,
216 sizeof(
struct addrinfo) +
sizeof(
struct sockaddr_in)));
217 ai->ai_addr = (
struct sockaddr *)(ai + 1);
218 ai->ai_addrlen =
sizeof(
struct sockaddr_in);
219 # ifdef HAVE_SOCKADDR_SA_LEN
220 ai->ai_addr->sa_len =
sizeof(
struct sockaddr_in);
222 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
223 ((
struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
224 ((
struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
225 ai->ai_socktype = socktype;
226 ai->ai_protocol =
proto;
235 return "memory allocation failure";
238 return "ai_family not supported";
241 return "hostname nor servname provided, or not known";
244 return "servname not supported for ai_socktype";
247 return "unknown error";
253 struct addrinfo *
next;
258 }
while ((ai =
next) != NULL);
261 int getaddrinfo(
char const *
hostname,
char const *servname,
struct addrinfo
const *hints,
struct addrinfo **res)
263 struct addrinfo *cur, *prev = NULL;
265 struct hostent result;
267 int i, socktype,
proto;
272 if (hints && (hints->ai_family != PF_INET) && (hints->ai_family != PF_UNSPEC))
return EAI_FAMILY;
274 socktype = (hints && hints->ai_socktype) ? hints->ai_socktype : SOCK_STREAM;
275 if (hints && hints->ai_protocol) {
276 proto = hints->ai_protocol;
292 if (isdigit((
uint8_t)*servname)) {
293 port = htons(atoi(servname));
296 char const *pe_proto;
311 if ((se = getservbyname(servname, pe_proto)) == NULL)
return EAI_SERVICE;
318 if (hints && hints->ai_flags & AI_PASSIVE) {
323 if (!*res)
return EAI_MEMORY;
331 if (!*res)
return EAI_MEMORY;
336 if (hints && hints->ai_flags & AI_NUMERICHOST)
return EAI_NONAME;
339 #ifdef GETHOSTBYNAMERSTYLE
340 # if GETHOSTBYNAMERSTYLE == SYSVSTYLE
342 # elif GETHOSTBYNAMERSTYLE == GNUSTYLE
351 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
352 for (i = 0; hp->h_addr_list[i]; i++) {
353 if ((cur =
alloc_ai(port, ((
struct in_addr *)hp->h_addr_list[i])->s_addr,
354 socktype,
proto)) == NULL) {
367 if (hints && hints->ai_flags & AI_CANONNAME && *res) {
368 if (((*res)->ai_canonname = talloc_strdup(*res, hp->h_name)) == NULL) {
380 #ifndef HAVE_GETNAMEINFO
381 int getnameinfo(
struct sockaddr
const *sa, socklen_t salen,
char *host,
size_t hostlen,
char *serv,
size_t servlen,
384 const struct sockaddr_in *sin = (
struct sockaddr_in const *)sa;
386 struct hostent result;
392 snprintf(tmpserv,
sizeof(tmpserv),
"%d", ntohs(sin->sin_port));
393 if (strlen(tmpserv) > servlen)
return EAI_MEMORY;
398 if (flags & NI_NUMERICHOST) {
400 if (flags & NI_NAMEREQD)
return EAI_NONAME;
401 if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
return EAI_MEMORY;
403 strcpy(host, inet_ntoa(sin->sin_addr));
407 #ifdef GETHOSTBYADDRRSTYLE
408 # if GETHOSTBYADDRRSTYLE == SYSVSTYLE
410 salen, AF_INET, &result,
buffer,
sizeof(
buffer), &error);
411 # elif GETHOSTBYADDRRSTYLE == GNUSTYLE
425 if (strlen(hp->h_name) >= hostlen)
return EAI_MEMORY;
431 if (flags & NI_NAMEREQD)
return EAI_NONAME;
432 if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
return EAI_MEMORY;
434 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
static int copy_hostent(struct hostent *from, struct hostent *to, char *buffer, int buflen, int *error)
void freeaddrinfo(struct addrinfo *ai)
char const * gai_strerror(int ecode)
static struct hostent * gethostbyname_r(char const *hostname, struct hostent *result, char *buffer, int 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 pthread_mutex_t fr_hostbyaddr_mutex
static struct addrinfo * alloc_ai(uint16_t port, u_long addr, int socktype, int proto)
static struct hostent * gethostbyaddr_r(char const *addr, int len, int type, struct hostent *result, char *buffer, int buflen, int *error)
int inet_aton(char const *cp, struct in_addr *inp)
static char const * hostname(char *buf, size_t buflen, uint32_t ipaddr)
static char const * proto(int id, int porttype)
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
fr_aka_sim_id_type_t type