More warnings in debug mode
[privoxy.git] / jbsockets.c
1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.25 2002/03/09 20:03:52 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
5  *
6  * Purpose     :  Contains wrappers for system-specific sockets code,
7  *                so that the rest of JunkBuster can be more
8  *                OS-independent.  Contains #ifdefs to make this work
9  *                on many platforms.
10  *
11  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
12  *                IJBSWA team.  http://ijbswa.sourceforge.net
13  *
14  *                Based on the Internet Junkbuster originally written
15  *                by and Copyright (C) 1997 Anonymous Coders and 
16  *                Junkbusters Corporation.  http://www.junkbusters.com
17  *
18  *                This program is free software; you can redistribute it 
19  *                and/or modify it under the terms of the GNU General
20  *                Public License as published by the Free Software
21  *                Foundation; either version 2 of the License, or (at
22  *                your option) any later version.
23  *
24  *                This program is distributed in the hope that it will
25  *                be useful, but WITHOUT ANY WARRANTY; without even the
26  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
27  *                PARTICULAR PURPOSE.  See the GNU General Public
28  *                License for more details.
29  *
30  *                The GNU General Public License should be included with
31  *                this file.  If not, you can view it at
32  *                http://www.gnu.org/copyleft/gpl.html
33  *                or write to the Free Software Foundation, Inc., 59
34  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
35  *
36  * Revisions   :
37  *    $Log: jbsockets.c,v $
38  *    Revision 1.25  2002/03/09 20:03:52  jongfoster
39  *    - Making various functions return int rather than size_t.
40  *      (Undoing a recent change).  Since size_t is unsigned on
41  *      Windows, functions like read_socket that return -1 on
42  *      error cannot return a size_t.
43  *
44  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
45  *      crashes, and also frequently caused JB to jump to 100%
46  *      CPU and stay there.  (Because it thought it had just
47  *      read ((unsigned)-1) == 4Gb of data...)
48  *
49  *    - The signature of write_socket has changed, it now simply
50  *      returns success=0/failure=nonzero.
51  *
52  *    - Trying to get rid of a few warnings --with-debug on
53  *      Windows, I've introduced a new type "jb_socket".  This is
54  *      used for the socket file descriptors.  On Windows, this
55  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
56  *      an int.  The error value can't be -1 any more, so it's
57  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
58  *      Windows it maps to the #define INVALID_SOCKET.)
59  *
60  *    - The signature of bind_port has changed.
61  *
62  *    Revision 1.24  2002/03/07 03:51:36  oes
63  *     - Improved handling of failed DNS lookups
64  *     - Fixed compiler warnings etc
65  *
66  *    Revision 1.23  2002/03/05 00:36:01  jongfoster
67  *    Fixing bug 514988 - unable to restart JunkBuster
68  *
69  *    Revision 1.22  2002/03/04 02:08:02  david__schmidt
70  *    Enable web editing of actions file on OS/2 (it had been broken all this time!)
71  *
72  *    Revision 1.21  2002/01/09 14:32:33  oes
73  *    Added support for gethostbyname_r and gethostbyaddr_r.
74  *
75  *    Revision 1.20  2001/11/16 00:48:48  jongfoster
76  *    Enabling duplicate-socket detection for all platforms, not
77  *    just Win32.
78  *
79  *    Revision 1.19  2001/10/25 03:40:47  david__schmidt
80  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
81  *    threads to call select() simultaneously.  So, it's time to do a real, live,
82  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
83  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
84  *
85  *    Revision 1.18  2001/09/21 23:02:02  david__schmidt
86  *    Cleaning up 2 compiler warnings on OS/2.
87  *
88  *    Revision 1.17  2001/09/13 20:11:46  jongfoster
89  *    Fixing 2 compiler warnings under Win32
90  *
91  *    Revision 1.16  2001/07/30 22:08:36  jongfoster
92  *    Tidying up #defines:
93  *    - All feature #defines are now of the form FEATURE_xxx
94  *    - Permanently turned off WIN_GUI_EDIT
95  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
96  *
97  *    Revision 1.15  2001/07/29 17:40:43  jongfoster
98  *    Fixed compiler warning by adding a cast
99  *
100  *    Revision 1.14  2001/07/18 13:47:59  oes
101  *    Eliminated dirty hack for getsockbyname()
102  *
103  *    Revision 1.13  2001/07/15 13:56:57  jongfoster
104  *    Removing unused local variable.
105  *
106  *    Revision 1.12  2001/07/01 17:04:11  oes
107  *    Bugfix: accept_connection no longer uses the obsolete hstrerror() function
108  *
109  *    Revision 1.11  2001/06/29 21:45:41  oes
110  *    Indentation, CRLF->LF, Tab-> Space
111  *
112  *    Revision 1.10  2001/06/29 13:29:15  oes
113  *    - Added remote (server) host IP to csp->http->host_ip_addr_str
114  *    - Added detection of local socket IP and fqdn
115  *    - Removed logentry from cancelled commit
116  *
117  *    Revision 1.9  2001/06/07 23:06:09  jongfoster
118  *    The host parameter to connect_to() is now const.
119  *
120  *    Revision 1.8  2001/06/03 19:12:07  oes
121  *    filled comment
122  *
123  *    Revision 1.7  2001/05/28 16:14:00  jongfoster
124  *    Fixing bug in LOG_LEVEL_LOG
125  *
126  *    Revision 1.6  2001/05/26 17:28:32  jongfoster
127  *    Fixed LOG_LEVEL_LOG
128  *
129  *    Revision 1.5  2001/05/26 15:26:15  jongfoster
130  *    ACL feature now provides more security by immediately dropping
131  *    connections from untrusted hosts.
132  *
133  *    Revision 1.4  2001/05/26 00:37:42  jongfoster
134  *    Cosmetic indentation correction.
135  *
136  *    Revision 1.3  2001/05/25 21:57:54  jongfoster
137  *    Now gives a warning under Windows if you try to bind
138  *    it to a port that's already in use.
139  *
140  *    Revision 1.2  2001/05/17 23:01:01  oes
141  *     - Cleaned CRLF's from the sources and related files
142  *
143  *    Revision 1.1.1.1  2001/05/15 13:58:54  oes
144  *    Initial import of version 2.9.3 source tree
145  *
146  *
147  *********************************************************************/
148 \f
149
150 #include "config.h"
151
152 #include <stdlib.h>
153 #include <stdio.h>
154 #include <string.h>
155 #include <errno.h>
156 #include <fcntl.h>
157 #include <sys/types.h>
158
159 #ifdef _WIN32
160
161 #include <windows.h>
162 #include <sys/timeb.h>
163 #include <io.h>
164
165 #else
166
167 #ifndef __OS2__
168 #include <unistd.h>
169 #endif
170 #include <sys/time.h>
171 #include <netinet/in.h>
172 #include <sys/ioctl.h>
173 #include <netdb.h>
174 #include <sys/socket.h>
175
176 #ifndef __BEOS__
177 #include <netinet/tcp.h>
178 #ifndef __OS2__
179 #include <arpa/inet.h>
180 #endif
181 #else
182 #include <socket.h>
183 #endif
184
185 #if defined(__EMX__) || defined (__OS2__)
186 #include <sys/select.h>  /* OS/2/EMX needs a little help with select */
187 #ifdef __OS2__
188 #include <nerrno.h>
189 #endif
190 #endif
191
192 #endif
193
194 #include "project.h"
195 #include "jbsockets.h"
196 #include "filters.h"
197 #include "errlog.h"
198
199 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
200
201
202 /*********************************************************************
203  *
204  * Function    :  connect_to
205  *
206  * Description :  Open a socket and connect to it.  Will check
207  *                that this is allowed according to ACL.
208  *
209  * Parameters  :
210  *          1  :  host = hostname to connect to
211  *          2  :  portnum = port to connent on
212  *          3  :  csp = Current client state (buffers, headers, etc...)
213  *                      Not modified, only used for source IP and ACL.
214  *
215  * Returns     :  JB_INVALID_SOCKET => failure, else it is the socket
216  *                file descriptor.
217  *
218  *********************************************************************/
219 jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
220 {
221    struct sockaddr_in inaddr;
222    jb_socket fd;
223    int addr;
224    fd_set wfds;
225    struct timeval tv[1];
226 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
227    int   flags;
228 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
229
230 #ifdef FEATURE_ACL
231    struct access_control_addr dst[1];
232 #endif /* def FEATURE_ACL */
233
234    memset((char *)&inaddr, 0, sizeof inaddr);
235
236    if ((addr = resolve_hostname_to_ip(host)) == INADDR_NONE)
237    {
238       csp->http->host_ip_addr_str = strdup("unknown");
239       return(JB_INVALID_SOCKET);
240    }
241
242 #ifdef FEATURE_ACL
243    dst->addr = ntohl((unsigned long) addr);
244    dst->port = portnum;
245
246    if (block_acl(dst, csp))
247    {
248 #ifdef __OS2__
249       errno = SOCEPERM;
250 #else
251       errno = EPERM;
252 #endif
253       return(JB_INVALID_SOCKET);
254    }
255 #endif /* def FEATURE_ACL */
256
257    inaddr.sin_addr.s_addr = addr;
258    inaddr.sin_family      = AF_INET;
259    csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
260
261 #ifndef _WIN32
262    if (sizeof(inaddr.sin_port) == sizeof(short))
263 #endif /* ndef _WIN32 */
264    {
265       inaddr.sin_port = htons((unsigned short) portnum);
266    }
267 #ifndef _WIN32
268    else
269    {
270       inaddr.sin_port = htonl((unsigned long)portnum);
271    }
272 #endif /* ndef _WIN32 */
273
274 #ifdef _WIN32
275    if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) == JB_INVALID_SOCKET)
276 #else
277    if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
278 #endif
279    {
280       return(JB_INVALID_SOCKET);
281    }
282
283 #ifdef TCP_NODELAY
284    {  /* turn off TCP coalescence */
285       int mi = 1;
286       setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
287    }
288 #endif /* def TCP_NODELAY */
289
290 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
291    if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
292    {
293       flags |= O_NDELAY;
294       fcntl(fd, F_SETFL, flags);
295    }
296 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
297
298    while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == JB_INVALID_SOCKET)
299    {
300 #ifdef _WIN32
301       if (errno == WSAEINPROGRESS)
302 #elif __OS2__ 
303       if (sock_errno() == EINPROGRESS)
304 #else /* ifndef _WIN32 */
305       if (errno == EINPROGRESS)
306 #endif /* ndef _WIN32 || __OS2__ */
307       {
308          break;
309       }
310
311 #ifdef __OS2__ 
312       if (sock_errno() != EINTR)
313 #else
314       if (errno != EINTR)
315 #endif /* __OS2__ */
316       {
317          close_socket(fd);
318          return(JB_INVALID_SOCKET);
319       }
320    }
321
322 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
323    if (flags != -1)
324    {
325       flags &= ~O_NDELAY;
326       fcntl(fd, F_SETFL, flags);
327    }
328 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
329
330    /* wait for connection to complete */
331    FD_ZERO(&wfds);
332    FD_SET(fd, &wfds);
333
334    tv->tv_sec  = 30;
335    tv->tv_usec = 0;
336
337    /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */
338    if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0)
339    {
340       close_socket(fd);
341       return(JB_INVALID_SOCKET);
342    }
343    return(fd);
344
345 }
346
347
348 /*********************************************************************
349  *
350  * Function    :  write_socket
351  *
352  * Description :  Write the contents of buf (for n bytes) to socket fd.
353  *
354  * Parameters  :
355  *          1  :  fd = file descriptor (aka. handle) of socket to write to.
356  *          2  :  buf = pointer to data to be written.
357  *          3  :  len = length of data to be written to the socket "fd".
358  *
359  * Returns     :  0 on success (entire buffer sent).
360  *                nonzero on error.
361  *
362  *********************************************************************/
363 int write_socket(jb_socket fd, const char *buf, int len)
364 {
365    if (len == 0)
366    {
367       return 0;
368    }
369
370    if (len < 0)
371    {
372       return 1;
373    }
374
375    log_error(LOG_LEVEL_LOG, "%N", len, buf);
376
377 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
378    return (send(fd, buf, len, 0) != len);
379 #elif defined(__OS2__)
380    /*
381     * Break the data up into SOCKET_SEND_MAX chunks for sending...
382     * OS/2 seemed to complain when the chunks were too large.
383     */
384 #define SOCKET_SEND_MAX 65000
385    {
386       int write_len = 0, send_len, send_rc = 0, i = 0;
387       while ((i < len) && (send_rc != -1))
388       {
389          if ((i + SOCKET_SEND_MAX) > len)
390             send_len = len - i;
391          else
392             send_len = SOCKET_SEND_MAX;
393          send_rc = send(fd,(char*)buf + i, send_len, 0);
394          if (send_rc == -1)
395             return 1;
396          i = i + send_len;
397       }
398       return 0;
399    }
400 #else
401    return (write(fd, buf, len) != len);
402 #endif
403
404 }
405
406
407 /*********************************************************************
408  *
409  * Function    :  read_socket
410  *
411  * Description :  Read from a TCP/IP socket in a platform independent way.
412  *
413  * Parameters  :
414  *          1  :  fd = file descriptor of the socket to read
415  *          2  :  buf = pointer to buffer where data will be written
416  *                Must be >= len bytes long.
417  *          3  :  len = maximum number of bytes to read
418  *
419  * Returns     :  On success, the number of bytes read is returned (zero
420  *                indicates end of file), and the file position is advanced
421  *                by this number.  It is not an error if this number is
422  *                smaller than the number of bytes requested; this may hap-
423  *                pen for example because fewer bytes are actually available
424  *                right now (maybe because we were close to end-of-file, or
425  *                because we are reading from a pipe, or from a terminal,
426  *                or because read() was interrupted by a signal).  On error,
427  *                -1 is returned, and errno is set appropriately.  In this
428  *                case it is left unspecified whether the file position (if
429  *                any) changes.
430  *
431  *********************************************************************/
432 int read_socket(jb_socket fd, char *buf, int len)
433 {
434    if (len <= 0)
435    {
436       return(0);
437    }
438
439 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA) || defined(__OS2__)
440    return(recv(fd, buf, len, 0));
441 #else
442    return(read(fd, buf, len));
443 #endif
444 }
445
446
447 /*********************************************************************
448  *
449  * Function    :  close_socket
450  *
451  * Description :  Closes a TCP/IP socket
452  *
453  * Parameters  :
454  *          1  :  fd = file descriptor of socket to be closed
455  *
456  * Returns     :  void
457  *
458  *********************************************************************/
459 void close_socket(jb_socket fd)
460 {
461 #if defined(_WIN32) || defined(__BEOS__)
462    closesocket(fd);
463 #elif defined(AMIGA)
464    CloseSocket(fd); 
465 #elif defined(__OS2__)
466    soclose(fd);
467 #else
468    close(fd);
469 #endif
470
471 }
472
473
474 /*********************************************************************
475  *
476  * Function    :  bind_port
477  *
478  * Description :  Call socket, set socket options, and listen.
479  *                Called by listen_loop to "boot up" our proxy address.
480  *
481  * Parameters  :
482  *          1  :  hostnam = TCP/IP address to bind/listen to
483  *          2  :  portnum = port to listen on
484  *          3  :  pfd = pointer used to return file descriptor.
485  *
486  * Returns     :  if success, returns 0 and sets *pfd.
487  *                if failure, returns -3 if address is in use,
488  *                                    -2 if address unresolvable,
489  *                                    -1 otherwise
490  *********************************************************************/
491 int bind_port(const char *hostnam, int portnum, jb_socket *pfd)
492 {
493    struct sockaddr_in inaddr;
494    jb_socket fd;
495 #ifndef _WIN32
496    int one = 1;
497 #endif /* ndef _WIN32 */
498
499    *pfd = JB_INVALID_SOCKET;
500
501    memset((char *)&inaddr, '\0', sizeof inaddr);
502
503    inaddr.sin_family      = AF_INET;
504    inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
505
506    if (inaddr.sin_addr.s_addr == INADDR_NONE)
507    {
508       return(-2);
509    }
510
511 #ifndef _WIN32
512    if (sizeof(inaddr.sin_port) == sizeof(short))
513 #endif /* ndef _WIN32 */
514    {
515       inaddr.sin_port = htons((unsigned short) portnum);
516    }
517 #ifndef _WIN32
518    else
519    {
520       inaddr.sin_port = htonl((unsigned long) portnum);
521    }
522 #endif /* ndef _WIN32 */
523
524    fd = socket(AF_INET, SOCK_STREAM, 0);
525
526 #ifdef _WIN32
527    if (fd == JB_INVALID_SOCKET)
528 #else
529    if (fd < 0)
530 #endif
531    {
532       return(-1);
533    }
534
535 #ifndef _WIN32
536    /*
537     * This is not needed for Win32 - in fact, it stops
538     * duplicate instances of JunkBuster from being caught.
539     *
540     * On UNIX, we assume the user is sensible enough not
541     * to start JunkBuster multiple times on the same IP.
542     * Without this, stopping and restarting JunkBuster
543     * from a script fails.
544     * Note: SO_REUSEADDR is meant to only take over
545     * sockets which are *not* in listen state in Linux,
546     * e.g. sockets in TIME_WAIT. YMMV.
547     */
548    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
549 #endif /* ndef _WIN32 */
550
551    if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
552    {
553       close_socket (fd);
554 #ifdef _WIN32
555       if (errno == WSAEADDRINUSE)
556 #else
557       if (errno == EADDRINUSE)
558 #endif
559       {
560          return(-3);
561       }
562       else
563       {
564          return(-1);
565       }
566    }
567
568    while (listen(fd, 5) == -1)
569    {
570       if (errno != EINTR)
571       {
572          return(-1);
573       }
574    }
575
576    *pfd = fd;
577    return 0;
578
579 }
580
581
582 /*********************************************************************
583  *
584  * Function    :  accept_connection
585  *
586  * Description :  Accepts a connection on a socket.  Socket must have
587  *                been created using bind_port().
588  *
589  * Parameters  :
590  *          1  :  csp = Client state, cfd, ip_addr_str, and 
591  *                ip_addr_long will be set by this routine.
592  *          2  :  fd  = file descriptor returned from bind_port
593  *
594  * Returns     :  when a connection is accepted, it returns 1 (TRUE).
595  *                On an error it returns 0 (FALSE).
596  *
597  *********************************************************************/
598 int accept_connection(struct client_state * csp, jb_socket fd)
599 {
600    struct sockaddr_in client, server;
601    struct hostent *host = NULL;
602    jb_socket afd;
603    size_t c_length, s_length;
604 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS) ||  defined(HAVE_GETHOSTBYADDR_R_7_ARGS) || defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
605    struct hostent result;
606 #if defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
607    struct hostent_data hdata;
608 #else
609    char hbuf[HOSTENT_BUFFER_SIZE];
610    int thd_err;
611 #endif /* def HAVE_GETHOSTBYADDR_R_5_ARGS */
612 #endif /* def HAVE_GETHOSTBYADDR_R_(8|7|5)_ARGS */
613
614    c_length = s_length = sizeof(client);
615
616 #ifdef _WIN32
617    afd = accept (fd, (struct sockaddr *) &client, &c_length);
618    if (afd == JB_INVALID_SOCKET)
619    {
620       return 0;
621    }
622 #else
623    do
624    {
625       afd = accept (fd, (struct sockaddr *) &client, &c_length);
626    } while (afd < 1 && errno == EINTR);
627    if (afd < 0)
628    {
629       return 0;
630    }
631 #endif
632
633    /* 
634     * Determine the IP-Adress that the client used to reach us
635     * and the hostname associated with that address
636     */
637    if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
638    {
639       csp->my_ip_addr_str = strdup(inet_ntoa(server.sin_addr));
640 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS)
641       gethostbyaddr_r((const char *)&server.sin_addr,
642                       sizeof(server.sin_addr), AF_INET,
643                       &result, hbuf, HOSTENT_BUFFER_SIZE,
644                       &host, &thd_err);
645 #elif defined(HAVE_GETHOSTBYADDR_R_7_ARGS)
646       host = gethostbyaddr_r((const char *)&server.sin_addr,
647                       sizeof(server.sin_addr), AF_INET,
648                       &result, hbuf, HOSTENT_BUFFER_SIZE, &thd_err);
649 #elif defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
650       if (0 == gethostbyaddr_r((const char *)&server.sin_addr,
651                                sizeof(server.sin_addr), AF_INET,
652                                &result, &hdata))
653       {
654          host = &result;
655       }
656       else
657       {
658          host = NULL;
659       }
660 #else
661       host = gethostbyaddr((const char *)&server.sin_addr, 
662                            sizeof(server.sin_addr), AF_INET);
663 #endif
664       if (host == NULL)
665       {
666          log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
667       }
668       else
669       {
670          csp->my_hostname = strdup(host->h_name);
671       }
672    }
673
674    csp->cfd    = afd;
675    csp->ip_addr_str  = strdup(inet_ntoa(client.sin_addr));
676    csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
677
678    return 1;
679
680 }
681
682
683 /*********************************************************************
684  *
685  * Function    :  resolve_hostname_to_ip
686  *
687  * Description :  Resolve a hostname to an internet tcp/ip address.
688  *                NULL or an empty string resolve to INADDR_ANY.
689  *
690  * Parameters  :
691  *          1  :  host = hostname to resolve
692  *
693  * Returns     :  INADDR_NONE => failure, INADDR_ANY or tcp/ip address if succesful.
694  *
695  *********************************************************************/
696 unsigned long resolve_hostname_to_ip(const char *host)
697 {
698    struct sockaddr_in inaddr;
699    struct hostent *hostp;
700 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS) || defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
701    struct hostent result;
702 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
703    char hbuf[HOSTENT_BUFFER_SIZE];
704    int thd_err;
705 #else /* defined(HAVE_GETHOSTBYNAME_R_3_ARGS) */
706    struct hostent_data hdata;
707 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5)_ARGS */
708 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
709
710    if ((host == NULL) || (*host == '\0'))
711    {
712       return(INADDR_ANY);
713    }
714
715    memset((char *) &inaddr, 0, sizeof inaddr);
716
717    if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
718    {
719 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS)
720       gethostbyname_r(host, &result, hbuf,
721                       HOSTENT_BUFFER_SIZE, &hostp, &thd_err);
722 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
723       hostp = gethostbyname_r(host, &result, hbuf,
724                       HOSTENT_BUFFER_SIZE, &thd_err);
725 #elif defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
726       if (0 == gethostbyname_r(host, &result, &hdata))
727       {
728          hostp = &result;
729       }
730       else
731       {
732          hostp = NULL;
733       }
734 #else
735       hostp = gethostbyname(host);
736 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
737       if (hostp == NULL)
738       {
739          errno = EINVAL;
740          log_error(LOG_LEVEL_ERROR, "could not resolve hostname %s", host);
741          return(INADDR_NONE);
742       }
743       if (hostp->h_addrtype != AF_INET)
744       {
745 #ifdef _WIN32
746          errno = WSAEPROTOTYPE;
747 #else
748          errno = EPROTOTYPE;
749 #endif 
750          log_error(LOG_LEVEL_ERROR, "hostname %s resolves to unknown address type.", host);
751          return(INADDR_NONE);
752       }
753       memcpy(
754          (char *) &inaddr.sin_addr,
755          (char *) hostp->h_addr,
756          sizeof(inaddr.sin_addr)
757       );
758    }
759    return(inaddr.sin_addr.s_addr);
760
761 }
762
763
764 /*
765   Local Variables:
766   tab-width: 3
767   end:
768 */