1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.18 2001/09/21 23:02:02 david__schmidt 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.18 2001/09/21 23:02:02 david__schmidt
39 * Cleaning up 2 compiler warnings on OS/2.
41 * Revision 1.17 2001/09/13 20:11:46 jongfoster
42 * Fixing 2 compiler warnings under Win32
44 * Revision 1.16 2001/07/30 22:08:36 jongfoster
45 * Tidying up #defines:
46 * - All feature #defines are now of the form FEATURE_xxx
47 * - Permanently turned off WIN_GUI_EDIT
48 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
50 * Revision 1.15 2001/07/29 17:40:43 jongfoster
51 * Fixed compiler warning by adding a cast
53 * Revision 1.14 2001/07/18 13:47:59 oes
54 * Eliminated dirty hack for getsockbyname()
56 * Revision 1.13 2001/07/15 13:56:57 jongfoster
57 * Removing unused local variable.
59 * Revision 1.12 2001/07/01 17:04:11 oes
60 * Bugfix: accept_connection no longer uses the obsolete hstrerror() function
62 * Revision 1.11 2001/06/29 21:45:41 oes
63 * Indentation, CRLF->LF, Tab-> Space
65 * Revision 1.10 2001/06/29 13:29:15 oes
66 * - Added remote (server) host IP to csp->http->host_ip_addr_str
67 * - Added detection of local socket IP and fqdn
68 * - Removed logentry from cancelled commit
70 * Revision 1.9 2001/06/07 23:06:09 jongfoster
71 * The host parameter to connect_to() is now const.
73 * Revision 1.8 2001/06/03 19:12:07 oes
76 * Revision 1.7 2001/05/28 16:14:00 jongfoster
77 * Fixing bug in LOG_LEVEL_LOG
79 * Revision 1.6 2001/05/26 17:28:32 jongfoster
82 * Revision 1.5 2001/05/26 15:26:15 jongfoster
83 * ACL feature now provides more security by immediately dropping
84 * connections from untrusted hosts.
86 * Revision 1.4 2001/05/26 00:37:42 jongfoster
87 * Cosmetic indentation correction.
89 * Revision 1.3 2001/05/25 21:57:54 jongfoster
90 * Now gives a warning under Windows if you try to bind
91 * it to a port that's already in use.
93 * Revision 1.2 2001/05/17 23:01:01 oes
94 * - Cleaned CRLF's from the sources and related files
96 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
97 * Initial import of version 2.9.3 source tree
100 *********************************************************************/
110 #include <sys/types.h>
115 #include <sys/timeb.h>
123 #include <sys/time.h>
124 #include <netinet/in.h>
125 #include <sys/ioctl.h>
127 #include <sys/socket.h>
130 #include <netinet/tcp.h>
132 #include <arpa/inet.h>
138 #if defined(__EMX__) || defined (__OS2__)
139 #include <sys/select.h> /* OS/2/EMX needs a little help with select */
146 #include "jbsockets.h"
150 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
153 /*********************************************************************
155 * Function : connect_to
157 * Description : Open a socket and connect to it. Will check
158 * that this is allowed according to ACL.
161 * 1 : host = hostname to connect to
162 * 2 : portnum = port to connent on
163 * 3 : csp = Current client state (buffers, headers, etc...)
164 * Not modified, only used for source IP and ACL.
166 * Returns : -1 => failure, else it is the socket file descriptor.
168 *********************************************************************/
169 int connect_to(const char *host, int portnum, struct client_state *csp)
171 struct sockaddr_in inaddr;
174 struct timeval tv[1];
175 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
177 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
180 struct access_control_addr dst[1];
181 #endif /* def FEATURE_ACL */
183 memset((char *)&inaddr, 0, sizeof inaddr);
185 if ((addr = resolve_hostname_to_ip(host)) == -1)
187 csp->http->host_ip_addr_str = strdup("unknown");
192 dst->addr = ntohl(addr);
195 if (block_acl(dst, csp))
204 #endif /* def FEATURE_ACL */
206 inaddr.sin_addr.s_addr = addr;
207 inaddr.sin_family = AF_INET;
208 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
211 if (sizeof(inaddr.sin_port) == sizeof(short))
212 #endif /* ndef _WIN32 */
214 inaddr.sin_port = htons((short)portnum);
219 inaddr.sin_port = htonl(portnum);
221 #endif /* ndef _WIN32 */
223 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
229 { /* turn off TCP coalescence */
231 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
233 #endif /* def TCP_NODELAY */
235 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
236 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
239 fcntl(fd, F_SETFL, flags);
241 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
243 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
246 if (errno == WSAEINPROGRESS)
248 if (sock_errno() == EINPROGRESS)
249 #else /* ifndef _WIN32 */
250 if (errno == EINPROGRESS)
251 #endif /* ndef _WIN32 || __OS2__ */
257 if (sock_errno() != EINTR)
267 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
271 fcntl(fd, F_SETFL, flags);
273 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */
275 /* wait for connection to complete */
282 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
292 /*********************************************************************
294 * Function : write_socket
296 * Description : Write the contents of buf (for n bytes) to socket fd.
299 * 1 : fd = file descriptor (aka. handle) of socket to write to.
300 * 2 : buf = pointer to data to be written.
301 * 3 : len = length of data to be written to the socket "fd".
303 * Returns : Win32 & Unix: If no error occurs, returns the total number of
304 * bytes sent, which can be less than the number
305 * indicated by len. Otherwise, returns (-1).
307 *********************************************************************/
308 int write_socket(int fd, const char *buf, int len)
315 log_error(LOG_LEVEL_LOG, "%N", len, buf);
317 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA) || defined(__OS2__)
318 return( send(fd, buf, len, 0));
320 return( write(fd, buf, len));
326 /*********************************************************************
328 * Function : read_socket
330 * Description : Read from a TCP/IP socket in a platform independent way.
333 * 1 : fd = file descriptor of the socket to read
334 * 2 : buf = pointer to buffer where data will be written
335 * Must be >= len bytes long.
336 * 3 : len = maximum number of bytes to read
338 * Returns : On success, the number of bytes read is returned (zero
339 * indicates end of file), and the file position is advanced
340 * by this number. It is not an error if this number is
341 * smaller than the number of bytes requested; this may hap-
342 * pen for example because fewer bytes are actually available
343 * right now (maybe because we were close to end-of-file, or
344 * because we are reading from a pipe, or from a terminal),
345 * or because read() was interrupted by a signal. On error,
346 * -1 is returned, and errno is set appropriately. In this
347 * case it is left unspecified whether the file position (if
350 *********************************************************************/
351 int read_socket(int fd, char *buf, int len)
358 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA) || defined(__OS2__)
359 return( recv(fd, buf, len, 0));
361 return( read(fd, buf, len));
366 /*********************************************************************
368 * Function : close_socket
370 * Description : Closes a TCP/IP socket
373 * 1 : fd = file descriptor of socket to be closed
377 *********************************************************************/
378 void close_socket(int fd)
380 #if defined(_WIN32) || defined(__BEOS__)
384 #elif defined(__OS2__)
393 /*********************************************************************
395 * Function : bind_port
397 * Description : Call socket, set socket options, and listen.
398 * Called by listen_loop to "boot up" our proxy address.
401 * 1 : hostnam = TCP/IP address to bind/listen to
402 * 2 : portnum = port to listen on
404 * Returns : if success, return file descriptor
405 * if failure, returns -2 if address is in use, otherwise -1
407 *********************************************************************/
408 int bind_port(const char *hostnam, int portnum)
410 struct sockaddr_in inaddr;
414 memset((char *)&inaddr, '\0', sizeof inaddr);
416 inaddr.sin_family = AF_INET;
417 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
420 if (sizeof(inaddr.sin_port) == sizeof(short))
421 #endif /* ndef _WIN32 */
423 inaddr.sin_port = htons((short)portnum);
428 inaddr.sin_port = htonl(portnum);
430 #endif /* ndef _WIN32 */
432 fd = socket(AF_INET, SOCK_STREAM, 0);
441 * FIXME: This is not needed for Win32 - in fact, it stops
442 * duplicate instances of JunkBuster from being caught.
443 * Is this really needed under UNIX, or should it be taked out?
446 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
447 #endif /* ndef _WIN32 */
449 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
453 if (errno == WSAEADDRINUSE)
455 if (errno == EADDRINUSE)
466 while (listen(fd, 5) == -1)
479 /*********************************************************************
481 * Function : accept_connection
483 * Description : Accepts a connection on a socket. Socket must have
484 * been created using bind_port().
487 * 1 : csp = Client state, cfd, ip_addr_str, and
488 * ip_addr_long will be set by this routine.
489 * 2 : fd = file descriptor returned from bind_port
491 * Returns : when a connection is accepted, it returns 1 (TRUE).
492 * On an error it returns 0 (FALSE).
494 *********************************************************************/
495 int accept_connection(struct client_state * csp, int fd)
497 struct sockaddr_in client, server;
498 struct hostent *host = NULL;
499 int afd, c_length, s_length;
501 c_length = s_length = sizeof(client);
505 afd = accept (fd, (struct sockaddr *) &client, &c_length);
506 } while (afd < 1 && errno == EINTR);
514 * Determine the IP-Adress that the client used to reach us
515 * and the hostname associated with that address
517 if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
519 csp->my_ip_addr_str = strdup(inet_ntoa(server.sin_addr));
521 host = gethostbyaddr((const char *)&server.sin_addr,
522 sizeof(server.sin_addr), AF_INET);
525 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
529 csp->my_hostname = strdup(host->h_name);
534 csp->ip_addr_str = strdup(inet_ntoa(client.sin_addr));
535 csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
542 /*********************************************************************
544 * Function : resolve_hostname_to_ip
546 * Description : Resolve a hostname to an internet tcp/ip address.
547 * NULL or an empty string resolve to INADDR_ANY.
550 * 1 : host = hostname to resolve
552 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
554 *********************************************************************/
555 int resolve_hostname_to_ip(const char *host)
557 struct sockaddr_in inaddr;
558 struct hostent *hostp;
560 if ((host == NULL) || (*host == '\0'))
565 memset((char *) &inaddr, 0, sizeof inaddr);
567 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
569 if ((hostp = gethostbyname(host)) == NULL)
574 if (hostp->h_addrtype != AF_INET)
577 errno = WSAEPROTOTYPE;
584 (char *) &inaddr.sin_addr,
585 (char *) hostp->h_addr,
586 sizeof(inaddr.sin_addr)
589 return(inaddr.sin_addr.s_addr);