1 |
/* |
/* |
2 |
http-host.c - thread-safe host lookup |
http-host.c - thread-safe host lookup |
3 |
Copyright (C) 2008 siliconforks.com |
Copyright (C) 2008, 2009 siliconforks.com |
4 |
|
|
5 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
21 |
|
|
22 |
#include "http-server.h" |
#include "http-server.h" |
23 |
|
|
|
#include <errno.h> |
|
|
#include <netdb.h> |
|
|
|
|
24 |
#include "util.h" |
#include "util.h" |
25 |
|
|
26 |
int xgethostbyname(const char * host, struct in_addr * a) { |
int xgethostbyname(const char * host, struct in_addr * a) { |
27 |
#ifdef __CYGWIN__ |
#if defined(__CYGWIN__) || defined(__MINGW32__) |
28 |
/* cygwin's gethostbyname is thread-safe */ |
/* gethostbyname is thread-safe */ |
29 |
struct hostent * p = gethostbyname(host); |
struct hostent * p = gethostbyname(host); |
30 |
if (p == NULL || p->h_addrtype != AF_INET) { |
if (p == NULL || p->h_addrtype != AF_INET) { |
31 |
return -1; |
return -1; |
32 |
} |
} |
33 |
*a = *((struct in_addr *) p->h_addr); |
*a = *((struct in_addr *) p->h_addr); |
34 |
return 0; |
return 0; |
35 |
#else |
#elif HAVE_GETADDRINFO |
36 |
|
struct addrinfo hints; |
37 |
|
hints.ai_flags = 0; |
38 |
|
hints.ai_family = PF_INET; |
39 |
|
hints.ai_socktype = 0; |
40 |
|
hints.ai_protocol = 0; |
41 |
|
hints.ai_addrlen = 0; |
42 |
|
hints.ai_addr = NULL; |
43 |
|
hints.ai_canonname = NULL; |
44 |
|
hints.ai_next = NULL; |
45 |
|
struct addrinfo * p; |
46 |
|
int result = getaddrinfo(host, NULL, &hints, &p); |
47 |
|
if (result != 0 || p == NULL) { |
48 |
|
return -1; |
49 |
|
} |
50 |
|
if (p->ai_family != PF_INET) { |
51 |
|
freeaddrinfo(p); |
52 |
|
return -1; |
53 |
|
} |
54 |
|
struct sockaddr_in * address_and_port = (struct sockaddr_in *) p->ai_addr; |
55 |
|
*a = address_and_port->sin_addr; |
56 |
|
freeaddrinfo(p); |
57 |
|
return 0; |
58 |
|
#elif HAVE_GETHOSTBYNAME_R |
59 |
struct hostent h; |
struct hostent h; |
60 |
struct hostent * p; |
struct hostent * p; |
61 |
char * buffer; |
char * buffer; |
76 |
*a = *((struct in_addr *) p->h_addr); |
*a = *((struct in_addr *) p->h_addr); |
77 |
free(buffer); |
free(buffer); |
78 |
return 0; |
return 0; |
79 |
|
#else |
80 |
|
#error "No thread-safe host lookup available" |
81 |
#endif |
#endif |
82 |
} |
} |
83 |
|
|
84 |
|
#ifndef HAVE_INET_ATON |
85 |
|
int inet_aton(const char * name, struct in_addr * a) { |
86 |
|
unsigned long result = inet_addr(name); |
87 |
|
if (result == INADDR_NONE) { |
88 |
|
return 0; |
89 |
|
} |
90 |
|
else { |
91 |
|
a->s_addr = result; |
92 |
|
return 1; |
93 |
|
} |
94 |
|
} |
95 |
|
#endif |