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 |
|
26 |
#include "util.h" |
27 |
|
28 |
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 |
#else |
38 |
struct hostent h; |
39 |
struct hostent * p; |
40 |
char * buffer; |
41 |
size_t buffer_size; |
42 |
int error; |
43 |
int result; |
44 |
|
45 |
buffer_size = 1024; |
46 |
buffer = xmalloc(buffer_size); |
47 |
while ((result = gethostbyname_r(host, &h, buffer, buffer_size, &p, &error)) == ERANGE) { |
48 |
buffer_size = mulst(buffer_size, 2); |
49 |
buffer = xrealloc(buffer, buffer_size); |
50 |
} |
51 |
if (result != 0 || p == NULL || p->h_addrtype != AF_INET) { |
52 |
free(buffer); |
53 |
return -1; |
54 |
} |
55 |
*a = *((struct in_addr *) p->h_addr); |
56 |
free(buffer); |
57 |
return 0; |
58 |
#endif |
59 |
} |
60 |
|
61 |
#ifndef HAVE_INET_ATON |
62 |
int inet_aton(const char * name, struct in_addr * a) { |
63 |
unsigned long result = inet_addr(name); |
64 |
if (result == INADDR_NONE) { |
65 |
return 0; |
66 |
} |
67 |
else { |
68 |
a->s_addr = result; |
69 |
return 1; |
70 |
} |
71 |
} |
72 |
#endif |