1 |
/* |
2 |
http-host.c - thread-safe host lookup |
3 |
Copyright (C) 2008 siliconforks.com |
4 |
|
5 |
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 |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include <config.h> |
21 |
|
22 |
#include "http-server.h" |
23 |
|
24 |
#include <errno.h> |
25 |
#include <netdb.h> |
26 |
|
27 |
#include "util.h" |
28 |
|
29 |
int xgethostbyname(const char * host, struct in_addr * a) { |
30 |
#ifdef __CYGWIN__ |
31 |
/* cygwin's gethostbyname is thread-safe */ |
32 |
struct hostent * p = gethostbyname(host); |
33 |
if (p == NULL || p->h_addrtype != AF_INET) { |
34 |
return -1; |
35 |
} |
36 |
*a = *((struct in_addr *) p->h_addr); |
37 |
return 0; |
38 |
#else |
39 |
struct hostent h; |
40 |
struct hostent * p; |
41 |
char * buffer; |
42 |
size_t buffer_size; |
43 |
int error; |
44 |
int result; |
45 |
|
46 |
buffer_size = 1024; |
47 |
buffer = xmalloc(buffer_size); |
48 |
while ((result = gethostbyname_r(host, &h, buffer, buffer_size, &p, &error)) == ERANGE) { |
49 |
buffer_size = mulst(buffer_size, 2); |
50 |
buffer = xrealloc(buffer, buffer_size); |
51 |
} |
52 |
if (result != 0 || p == NULL || p->h_addrtype != AF_INET) { |
53 |
free(buffer); |
54 |
return -1; |
55 |
} |
56 |
*a = *((struct in_addr *) p->h_addr); |
57 |
free(buffer); |
58 |
return 0; |
59 |
#endif |
60 |
} |