/[jscoverage]/trunk/http-host.c
ViewVC logotype

Annotation of /trunk/http-host.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 129 - (hide annotations)
Mon Jun 2 23:15:29 2008 UTC (11 years, 2 months ago) by siliconforks
File MIME type: text/plain
File size: 2631 byte(s)
Use getaddrinfo on machines which have it.

1 siliconforks 116 /*
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 siliconforks 125 #if defined(__CYGWIN__) || defined(__MINGW32__)
30     /* gethostbyname is thread-safe */
31 siliconforks 118 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 siliconforks 129 #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 siliconforks 116 struct hostent h;
62     struct hostent * p;
63     char * buffer;
64     size_t buffer_size;
65     int error;
66     int result;
67    
68     buffer_size = 1024;
69     buffer = xmalloc(buffer_size);
70     while ((result = gethostbyname_r(host, &h, buffer, buffer_size, &p, &error)) == ERANGE) {
71     buffer_size = mulst(buffer_size, 2);
72     buffer = xrealloc(buffer, buffer_size);
73     }
74     if (result != 0 || p == NULL || p->h_addrtype != AF_INET) {
75     free(buffer);
76     return -1;
77     }
78     *a = *((struct in_addr *) p->h_addr);
79     free(buffer);
80     return 0;
81 siliconforks 129 #else
82     #error "No thread-safe host lookup available"
83 siliconforks 118 #endif
84 siliconforks 116 }
85 siliconforks 125
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

  ViewVC Help
Powered by ViewVC 1.1.24