22 |
#include "http-server.h" |
#include "http-server.h" |
23 |
|
|
24 |
#include <errno.h> |
#include <errno.h> |
|
#include <netdb.h> |
|
25 |
|
|
26 |
#include "util.h" |
#include "util.h" |
27 |
|
|
28 |
int xgethostbyname(const char * host, struct in_addr * a) { |
int xgethostbyname(const char * host, struct in_addr * a) { |
29 |
|
#if defined(__CYGWIN__) || defined(__MINGW32__) |
30 |
|
/* gethostbyname is thread-safe */ |
31 |
|
struct hostent * p = gethostbyname(host); |
32 |
|
if (p == NULL || p->h_addrtype != AF_INET) { |
33 |
|
return -1; |
34 |
|
} |
35 |
|
*a = *((struct in_addr *) p->h_addr); |
36 |
|
return 0; |
37 |
|
#elif HAVE_GETADDRINFO |
38 |
|
struct addrinfo hints; |
39 |
|
hints.ai_flags = 0; |
40 |
|
hints.ai_family = PF_INET; |
41 |
|
hints.ai_socktype = 0; |
42 |
|
hints.ai_protocol = 0; |
43 |
|
hints.ai_addrlen = 0; |
44 |
|
hints.ai_addr = NULL; |
45 |
|
hints.ai_canonname = NULL; |
46 |
|
hints.ai_next = NULL; |
47 |
|
struct addrinfo * p; |
48 |
|
int result = getaddrinfo(host, NULL, &hints, &p); |
49 |
|
if (result != 0 || p == NULL) { |
50 |
|
return -1; |
51 |
|
} |
52 |
|
if (p->ai_family != PF_INET) { |
53 |
|
freeaddrinfo(p); |
54 |
|
return -1; |
55 |
|
} |
56 |
|
struct sockaddr_in * address_and_port = (struct sockaddr_in *) p->ai_addr; |
57 |
|
*a = address_and_port->sin_addr; |
58 |
|
freeaddrinfo(p); |
59 |
|
return 0; |
60 |
|
#elif HAVE_GETHOSTBYNAME_R |
61 |
struct hostent h; |
struct hostent h; |
62 |
struct hostent * p; |
struct hostent * p; |
63 |
char * buffer; |
char * buffer; |
78 |
*a = *((struct in_addr *) p->h_addr); |
*a = *((struct in_addr *) p->h_addr); |
79 |
free(buffer); |
free(buffer); |
80 |
return 0; |
return 0; |
81 |
|
#else |
82 |
|
#error "No thread-safe host lookup available" |
83 |
|
#endif |
84 |
|
} |
85 |
|
|
86 |
|
#ifndef HAVE_INET_ATON |
87 |
|
int inet_aton(const char * name, struct in_addr * a) { |
88 |
|
unsigned long result = inet_addr(name); |
89 |
|
if (result == INADDR_NONE) { |
90 |
|
return 0; |
91 |
|
} |
92 |
|
else { |
93 |
|
a->s_addr = result; |
94 |
|
return 1; |
95 |
|
} |
96 |
} |
} |
97 |
|
#endif |