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 |
struct hostent h; |
31 |
struct hostent * p; |
32 |
char * buffer; |
33 |
size_t buffer_size; |
34 |
int error; |
35 |
int result; |
36 |
|
37 |
buffer_size = 1024; |
38 |
buffer = xmalloc(buffer_size); |
39 |
while ((result = gethostbyname_r(host, &h, buffer, buffer_size, &p, &error)) == ERANGE) { |
40 |
buffer_size = mulst(buffer_size, 2); |
41 |
buffer = xrealloc(buffer, buffer_size); |
42 |
} |
43 |
if (result != 0 || p == NULL || p->h_addrtype != AF_INET) { |
44 |
free(buffer); |
45 |
return -1; |
46 |
} |
47 |
*a = *((struct in_addr *) p->h_addr); |
48 |
free(buffer); |
49 |
return 0; |
50 |
} |