1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.23 2002/03/05 00:36:01 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
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
11 * Copyright : Written by and Copyright (C) 2001 the SourceForge
12 * IJBSWA team. http://ijbswa.sourceforge.net
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
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.
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.
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.
37 * $Log: jbsockets.c,v $
38 * Revision 1.23 2002/03/05 00:36:01 jongfoster
39 * Fixing bug 514988 - unable to restart JunkBuster
41 * Revision 1.22 2002/03/04 02:08:02 david__schmidt
42 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
44 * Revision 1.21 2002/01/09 14:32:33 oes
45 * Added support for gethostbyname_r and gethostbyaddr_r.
47 * Revision 1.20 2001/11/16 00:48:48 jongfoster
48 * Enabling duplicate-socket detection for all platforms, not
51 * Revision 1.19 2001/10/25 03:40:47 david__schmidt
52 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
53 * threads to call select() simultaneously. So, it's time to do a real, live,
54 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
55 * (native). Both versions will work, but using __OS2__ offers multi-threading.
57 * Revision 1.18 2001/09/21 23:02:02 david__schmidt
58 * Cleaning up 2 compiler warnings on OS/2.
60 * Revision 1.17 2001/09/13 20:11:46 jongfoster
61 * Fixing 2 compiler warnings under Win32
63 * Revision 1.16 2001/07/30 22:08:36 jongfoster
64 * Tidying up #defines:
65 * - All feature #defines are now of the form FEATURE_xxx
66 * - Permanently turned off WIN_GUI_EDIT
67 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
69 * Revision 1.15 2001/07/29 17:40:43 jongfoster
70 * Fixed compiler warning by adding a cast
72 * Revision 1.14 2001/07/18 13:47:59 oes
73 * Eliminated dirty hack for getsockbyname()
75 * Revision 1.13 2001/07/15 13:56:57 jongfoster
76 * Removing unused local variable.
78 * Revision 1.12 2001/07/01 17:04:11 oes
79 * Bugfix: accept_connection no longer uses the obsolete hstrerror() function
81 * Revision 1.11 2001/06/29 21:45:41 oes
82 * Indentation, CRLF->LF, Tab-> Space
84 * Revision 1.10 2001/06/29 13:29:15 oes
85 * - Added remote (server) host IP to csp->http->host_ip_addr_str
86 * - Added detection of local socket IP and fqdn
87 * - Removed logentry from cancelled commit
89 * Revision 1.9 2001/06/07 23:06:09 jongfoster
90 * The host parameter to connect_to() is now const.
92 * Revision 1.8 2001/06/03 19:12:07 oes
95 * Revision 1.7 2001/05/28 16:14:00 jongfoster
96 * Fixing bug in LOG_LEVEL_LOG
98 * Revision 1.6 2001/05/26 17:28:32 jongfoster
101 * Revision 1.5 2001/05/26 15:26:15 jongfoster
102 * ACL feature now provides more security by immediately dropping
103 * connections from untrusted hosts.
105 * Revision 1.4 2001/05/26 00:37:42 jongfoster
106 * Cosmetic indentation correction.
108 * Revision 1.3 2001/05/25 21:57:54 jongfoster
109 * Now gives a warning under Windows if you try to bind
110 * it to a port that's already in use.
112 * Revision 1.2 2001/05/17 23:01:01 oes
113 * - Cleaned CRLF's from the sources and related files
115 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
116 * Initial import of version 2.9.3 source tree
119 *********************************************************************/
129 #include <sys/types.h>
134 #include <sys/timeb.h>
142 #include <sys/time.h>
143 #include <netinet/in.h>
144 #include <sys/ioctl.h>
146 #include <sys/socket.h>
149 #include <netinet/tcp.h>
151 #include <arpa/inet.h>
157 #if defined(__EMX__) || defined (__OS2__)
158 #include <sys/select.h> /* OS/2/EMX needs a little help with select */
165 #include "jbsockets.h"
169 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
172 /*********************************************************************
174 * Function : connect_to
176 * Description : Open a socket and connect to it. Will check
177 * that this is allowed according to ACL.
180 * 1 : host = hostname to connect to
181 * 2 : portnum = port to connent on
182 * 3 : csp = Current client state (buffers, headers, etc...)
183 * Not modified, only used for source IP and ACL.
185 * Returns : -1 => failure, else it is the socket file descriptor.
187 *********************************************************************/
188 int connect_to(const char *host, int portnum, struct client_state *csp)
190 struct sockaddr_in inaddr;
193 struct timeval tv[1];
194 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
196 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
199 struct access_control_addr dst[1];
200 #endif /* def FEATURE_ACL */
202 memset((char *)&inaddr, 0, sizeof inaddr);
204 if ((addr = resolve_hostname_to_ip(host)) == INADDR_NONE)
206 csp->http->host_ip_addr_str = strdup("unknown");
211 dst->addr = ntohl((unsigned long) addr);
214 if (block_acl(dst, csp))
223 #endif /* def FEATURE_ACL */
225 inaddr.sin_addr.s_addr = addr;
226 inaddr.sin_family = AF_INET;
227 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
230 if (sizeof(inaddr.sin_port) == sizeof(short))
231 #endif /* ndef _WIN32 */
233 inaddr.sin_port = htons((unsigned short) portnum);
238 inaddr.sin_port = htonl((unsigned long)portnum);
240 #endif /* ndef _WIN32 */
242 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
248 { /* turn off TCP coalescence */
250 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
252 #endif /* def TCP_NODELAY */
254 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
255 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
258 fcntl(fd, F_SETFL, flags);
260 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
262 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
265 if (errno == WSAEINPROGRESS)
267 if (sock_errno() == EINPROGRESS)
268 #else /* ifndef _WIN32 */
269 if (errno == EINPROGRESS)
270 #endif /* ndef _WIN32 || __OS2__ */
276 if (sock_errno() != EINTR)
286 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
290 fcntl(fd, F_SETFL, flags);
292 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
294 /* wait for connection to complete */
301 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
311 /*********************************************************************
313 * Function : write_socket
315 * Description : Write the contents of buf (for n bytes) to socket fd.
318 * 1 : fd = file descriptor (aka. handle) of socket to write to.
319 * 2 : buf = pointer to data to be written.
320 * 3 : len = length of data to be written to the socket "fd".
322 * Returns : Win32 & Unix: If no error occurs, returns the total number of
323 * bytes sent, which can be less than the number
324 * indicated by len. Otherwise, returns (-1).
326 *********************************************************************/
327 size_t write_socket(int fd, const char *buf, size_t len)
334 log_error(LOG_LEVEL_LOG, "%N", len, buf);
336 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
337 return(send(fd, buf, len, 0));
338 #elif defined(__OS2__)
340 * Break the data up into SOCKET_SEND_MAX chunks for sending...
341 * OS/2 seemed to complain when the chunks were too large.
343 #define SOCKET_SEND_MAX 65000
345 int write_len = 0, send_len, send_rc = 0, i = 0;
346 while ((i < len) && (send_rc != -1))
348 if ((i + SOCKET_SEND_MAX) > len)
351 send_len = SOCKET_SEND_MAX;
352 send_rc = send(fd,(char*)buf + i, send_len, 0);
360 return(write(fd, buf, len));
366 /*********************************************************************
368 * Function : read_socket
370 * Description : Read from a TCP/IP socket in a platform independent way.
373 * 1 : fd = file descriptor of the socket to read
374 * 2 : buf = pointer to buffer where data will be written
375 * Must be >= len bytes long.
376 * 3 : len = maximum number of bytes to read
378 * Returns : On success, the number of bytes read is returned (zero
379 * indicates end of file), and the file position is advanced
380 * by this number. It is not an error if this number is
381 * smaller than the number of bytes requested; this may hap-
382 * pen for example because fewer bytes are actually available
383 * right now (maybe because we were close to end-of-file, or
384 * because we are reading from a pipe, or from a terminal),
385 * or because read() was interrupted by a signal. On error,
386 * -1 is returned, and errno is set appropriately. In this
387 * case it is left unspecified whether the file position (if
390 *********************************************************************/
391 size_t read_socket(int fd, char *buf, size_t len)
398 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA) || defined(__OS2__)
399 return(recv(fd, buf, len, 0));
401 return(read(fd, buf, len));
406 /*********************************************************************
408 * Function : close_socket
410 * Description : Closes a TCP/IP socket
413 * 1 : fd = file descriptor of socket to be closed
417 *********************************************************************/
418 void close_socket(int fd)
420 #if defined(_WIN32) || defined(__BEOS__)
424 #elif defined(__OS2__)
433 /*********************************************************************
435 * Function : bind_port
437 * Description : Call socket, set socket options, and listen.
438 * Called by listen_loop to "boot up" our proxy address.
441 * 1 : hostnam = TCP/IP address to bind/listen to
442 * 2 : portnum = port to listen on
444 * Returns : if success, return file descriptor
445 * if failure, returns -3 if address is in use,
446 * -2 if address unresolvable,
449 *********************************************************************/
450 int bind_port(const char *hostnam, int portnum)
452 struct sockaddr_in inaddr;
456 #endif /* ndef _WIN32 */
458 memset((char *)&inaddr, '\0', sizeof inaddr);
460 inaddr.sin_family = AF_INET;
461 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
463 if (inaddr.sin_addr.s_addr == INADDR_NONE)
469 if (sizeof(inaddr.sin_port) == sizeof(short))
470 #endif /* ndef _WIN32 */
472 inaddr.sin_port = htons((unsigned short) portnum);
477 inaddr.sin_port = htonl((unsigned long) portnum);
479 #endif /* ndef _WIN32 */
481 fd = socket(AF_INET, SOCK_STREAM, 0);
490 * This is not needed for Win32 - in fact, it stops
491 * duplicate instances of JunkBuster from being caught.
493 * On UNIX, we assume the user is sensible enough not
494 * to start JunkBuster multiple times on the same IP.
495 * Without this, stopping and restarting JunkBuster
496 * from a script fails.
497 * Note: SO_REUSEADDR is meant to only take over
498 * sockets which are *not* in listen state in Linux,
499 * e.g. sockets in TIME_WAIT. YMMV.
501 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
502 #endif /* ndef _WIN32 */
504 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
508 if (errno == WSAEADDRINUSE)
510 if (errno == EADDRINUSE)
521 while (listen(fd, 5) == -1)
534 /*********************************************************************
536 * Function : accept_connection
538 * Description : Accepts a connection on a socket. Socket must have
539 * been created using bind_port().
542 * 1 : csp = Client state, cfd, ip_addr_str, and
543 * ip_addr_long will be set by this routine.
544 * 2 : fd = file descriptor returned from bind_port
546 * Returns : when a connection is accepted, it returns 1 (TRUE).
547 * On an error it returns 0 (FALSE).
549 *********************************************************************/
550 int accept_connection(struct client_state * csp, int fd)
552 struct sockaddr_in client, server;
553 struct hostent *host = NULL;
555 size_t c_length, s_length;
556 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS) || defined(HAVE_GETHOSTBYADDR_R_7_ARGS) || defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
557 struct hostent result;
558 #if defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
559 struct hostent_data hdata;
561 char hbuf[HOSTENT_BUFFER_SIZE];
563 #endif /* def HAVE_GETHOSTBYADDR_R_5_ARGS */
564 #endif /* def HAVE_GETHOSTBYADDR_R_(8|7|5)_ARGS */
566 c_length = s_length = sizeof(client);
570 afd = accept (fd, (struct sockaddr *) &client, &c_length);
571 } while (afd < 1 && errno == EINTR);
579 * Determine the IP-Adress that the client used to reach us
580 * and the hostname associated with that address
582 if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
584 csp->my_ip_addr_str = strdup(inet_ntoa(server.sin_addr));
585 #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS)
586 gethostbyaddr_r((const char *)&server.sin_addr,
587 sizeof(server.sin_addr), AF_INET,
588 &result, hbuf, HOSTENT_BUFFER_SIZE,
590 #elif defined(HAVE_GETHOSTBYADDR_R_7_ARGS)
591 host = gethostbyaddr_r((const char *)&server.sin_addr,
592 sizeof(server.sin_addr), AF_INET,
593 &result, hbuf, HOSTENT_BUFFER_SIZE, &thd_err);
594 #elif defined(HAVE_GETHOSTBYADDR_R_5_ARGS)
595 if (0 == gethostbyaddr_r((const char *)&server.sin_addr,
596 sizeof(server.sin_addr), AF_INET,
606 host = gethostbyaddr((const char *)&server.sin_addr,
607 sizeof(server.sin_addr), AF_INET);
611 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
615 csp->my_hostname = strdup(host->h_name);
620 csp->ip_addr_str = strdup(inet_ntoa(client.sin_addr));
621 csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
628 /*********************************************************************
630 * Function : resolve_hostname_to_ip
632 * Description : Resolve a hostname to an internet tcp/ip address.
633 * NULL or an empty string resolve to INADDR_ANY.
636 * 1 : host = hostname to resolve
638 * Returns : INADDR_NONE => failure, INADDR_ANY or tcp/ip address if succesful.
640 *********************************************************************/
641 unsigned long resolve_hostname_to_ip(const char *host)
643 struct sockaddr_in inaddr;
644 struct hostent *hostp;
645 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS) || defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
646 struct hostent result;
647 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS) || defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
648 char hbuf[HOSTENT_BUFFER_SIZE];
650 #else /* defined(HAVE_GETHOSTBYNAME_R_3_ARGS) */
651 struct hostent_data hdata;
652 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5)_ARGS */
653 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
655 if ((host == NULL) || (*host == '\0'))
660 memset((char *) &inaddr, 0, sizeof inaddr);
662 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
664 #if defined(HAVE_GETHOSTBYNAME_R_6_ARGS)
665 gethostbyname_r(host, &result, hbuf,
666 HOSTENT_BUFFER_SIZE, &hostp, &thd_err);
667 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARGS)
668 hostp = gethostbyname_r(host, &result, hbuf,
669 HOSTENT_BUFFER_SIZE, &thd_err);
670 #elif defined(HAVE_GETHOSTBYNAME_R_3_ARGS)
671 if (0 == gethostbyname_r(host, &result, &hdata))
680 hostp = gethostbyname(host);
681 #endif /* def HAVE_GETHOSTBYNAME_R_(6|5|3)_ARGS */
685 log_error(LOG_LEVEL_ERROR, "could not resolve hostname %s", host);
688 if (hostp->h_addrtype != AF_INET)
691 errno = WSAEPROTOTYPE;
695 log_error(LOG_LEVEL_ERROR, "hostname %s resolves to unknown address type.", host);
699 (char *) &inaddr.sin_addr,
700 (char *) hostp->h_addr,
701 sizeof(inaddr.sin_addr)
704 return(inaddr.sin_addr.s_addr);