1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.9 2001/06/07 23:06:09 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.9 2001/06/07 23:06:09 jongfoster
39 * The host parameter to connect_to() is now const.
41 * Revision 1.8 2001/06/03 19:12:07 oes
44 * Revision 1.7 2001/05/28 16:14:00 jongfoster
45 * Fixing bug in LOG_LEVEL_LOG
47 * Revision 1.6 2001/05/26 17:28:32 jongfoster
50 * Revision 1.5 2001/05/26 15:26:15 jongfoster
51 * ACL feature now provides more security by immediately dropping
52 * connections from untrusted hosts.
54 * Revision 1.4 2001/05/26 00:37:42 jongfoster
55 * Cosmetic indentation correction.
57 * Revision 1.3 2001/05/25 21:57:54 jongfoster
58 * Now gives a warning under Windows if you try to bind
59 * it to a port that's already in use.
61 * Revision 1.2 2001/05/17 23:01:01 oes
62 * - Cleaned CRLF's from the sources and related files
64 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
65 * Initial import of version 2.9.3 source tree
68 *********************************************************************/
78 #include <sys/types.h>
83 #include <sys/timeb.h>
90 #include <netinet/in.h>
91 #include <sys/ioctl.h>
93 #include <sys/socket.h>
96 #include <netinet/tcp.h>
97 #include <arpa/inet.h>
105 #include "jbsockets.h"
109 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
112 /*********************************************************************
114 * Function : connect_to
116 * Description : Open a socket and connect to it. Will check
117 * that this is allowed according to ACL.
120 * 1 : host = hostname to connect to
121 * 2 : portnum = port to connent on
122 * 3 : csp = Current client state (buffers, headers, etc...)
123 * Not modified, only used for source IP and ACL.
125 * Returns : -1 => failure, else it is the socket file descriptor.
127 *********************************************************************/
128 int connect_to(const char *host, int portnum, struct client_state *csp)
130 struct sockaddr_in inaddr;
133 struct timeval tv[1];
134 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
136 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
139 struct access_control_addr dst[1];
140 #endif /* def ACL_FILES */
142 memset((char *)&inaddr, 0, sizeof inaddr);
144 if ((addr = resolve_hostname_to_ip(host)) == -1)
146 csp->http->host_ip_addr_str = strdup("unknown");
151 dst->addr = ntohl(addr);
154 if (block_acl(dst, csp))
159 #endif /* def ACL_FILES */
161 inaddr.sin_addr.s_addr = addr;
162 inaddr.sin_family = AF_INET;
163 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
165 if (sizeof(inaddr.sin_port) == sizeof(short))
167 inaddr.sin_port = htons((short)portnum);
171 inaddr.sin_port = htonl(portnum);
174 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
180 { /* turn off TCP coalescence */
182 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
184 #endif /* def TCP_NODELAY */
186 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
187 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
190 fcntl(fd, F_SETFL, flags);
192 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
194 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
197 if (errno == WSAEINPROGRESS)
198 #else /* ifndef _WIN32 */
199 if (errno == EINPROGRESS)
200 #endif /* ndef _WIN32 */
212 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
216 fcntl(fd, F_SETFL, flags);
218 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
220 /* wait for connection to complete */
227 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
237 /*********************************************************************
239 * Function : write_socket
241 * Description : Write the contents of buf (for n bytes) to socket fd.
244 * 1 : fd = file descriptor (aka. handle) of socket to write to.
245 * 2 : buf = pointer to data to be written.
246 * 3 : len = length of data to be written to the socket "fd".
248 * Returns : Win32 & Unix: If no error occurs, returns the total number of
249 * bytes sent, which can be less than the number
250 * indicated by len. Otherwise, returns (-1).
252 *********************************************************************/
253 int write_socket(int fd, const char *buf, int len)
260 log_error(LOG_LEVEL_LOG, "%N", len, buf);
262 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
263 return( send(fd, buf, len, 0));
265 return( write(fd, buf, len));
271 /*********************************************************************
273 * Function : read_socket
275 * Description : Read from a TCP/IP socket in a platform independent way.
278 * 1 : fd = file descriptor of the socket to read
279 * 2 : buf = pointer to buffer where data will be written
280 * Must be >= len bytes long.
281 * 3 : len = maximum number of bytes to read
283 * Returns : On success, the number of bytes read is returned (zero
284 * indicates end of file), and the file position is advanced
285 * by this number. It is not an error if this number is
286 * smaller than the number of bytes requested; this may hap-
287 * pen for example because fewer bytes are actually available
288 * right now (maybe because we were close to end-of-file, or
289 * because we are reading from a pipe, or from a terminal),
290 * or because read() was interrupted by a signal. On error,
291 * -1 is returned, and errno is set appropriately. In this
292 * case it is left unspecified whether the file position (if
295 *********************************************************************/
296 int read_socket(int fd, char *buf, int len)
303 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
304 return( recv(fd, buf, len, 0));
306 return( read(fd, buf, len));
311 /*********************************************************************
313 * Function : close_socket
315 * Description : Closes a TCP/IP socket
318 * 1 : fd = file descriptor of socket to be closed
322 *********************************************************************/
323 void close_socket(int fd)
325 #if defined(_WIN32) || defined(__BEOS__)
336 /*********************************************************************
338 * Function : bind_port
340 * Description : Call socket, set socket options, and listen.
341 * Called by listen_loop to "boot up" our proxy address.
344 * 1 : hostnam = TCP/IP address to bind/listen to
345 * 2 : portnum = port to listen on
347 * Returns : if success, return file descriptor
348 * if failure, returns -2 if address is in use, otherwise -1
350 *********************************************************************/
351 int bind_port(const char *hostnam, int portnum)
353 struct sockaddr_in inaddr;
357 memset((char *)&inaddr, '\0', sizeof inaddr);
359 inaddr.sin_family = AF_INET;
360 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
362 if (sizeof(inaddr.sin_port) == sizeof(short))
364 inaddr.sin_port = htons((short)portnum);
368 inaddr.sin_port = htonl(portnum);
371 fd = socket(AF_INET, SOCK_STREAM, 0);
380 * FIXME: This is not needed for Win32 - in fact, it stops
381 * duplicate instances of JunkBuster from being caught.
382 * Is this really needed under UNIX, or should it be taked out?
385 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
386 #endif /* ndef _WIN32 */
388 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
392 if (errno == WSAEADDRINUSE)
394 if (errno == EADDRINUSE)
405 while (listen(fd, 5) == -1)
418 /*********************************************************************
420 * Function : accept_connection
422 * Description : Accepts a connection on a socket. Socket must have
423 * been created using bind_port().
426 * 1 : csp = Client state, cfd, ip_addr_str, and
427 * ip_addr_long will be set by this routine.
428 * 2 : fd = file descriptor returned from bind_port
430 * Returns : when a connection is accepted, it returns 1 (TRUE).
431 * On an error it returns 0 (FALSE).
433 *********************************************************************/
434 int accept_connection(struct client_state * csp, int fd)
436 struct sockaddr raddr, laddr;
437 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
438 struct sockaddr_in *lap = (struct sockaddr_in *) &laddr;
439 struct hostent *host = NULL;
440 int afd, raddrlen, laddrlen;
444 raddrlen = sizeof raddr;
447 afd = accept (fd, &raddr, &raddrlen);
448 } while (afd < 1 && errno == EINTR);
456 * Determine the IP-Adress that the client used to reach us
457 * and the hostname associated with that address
459 if (!getsockname(afd, &laddr, &laddrlen))
461 csp->my_ip_addr_str = strdup(inet_ntoa(lap->sin_addr));
463 host = gethostbyaddr(laddr.sa_data + 2, 4, AF_INET);
466 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %s\n", hstrerror(h_errno));
470 csp->my_hostname = strdup(host->h_name);
475 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
476 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
482 /*********************************************************************
484 * Function : resolve_hostname_to_ip
486 * Description : Resolve a hostname to an internet tcp/ip address.
487 * NULL or an empty string resolve to INADDR_ANY.
490 * 1 : host = hostname to resolve
492 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
494 *********************************************************************/
495 int resolve_hostname_to_ip(const char *host)
497 struct sockaddr_in inaddr;
498 struct hostent *hostp;
500 if ((host == NULL) || (*host == '\0'))
505 memset((char *) &inaddr, 0, sizeof inaddr);
507 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
509 if ((hostp = gethostbyname(host)) == NULL)
514 if (hostp->h_addrtype != AF_INET)
517 errno = WSAEPROTOTYPE;
524 (char *) &inaddr.sin_addr,
525 (char *) hostp->h_addr,
526 sizeof(inaddr.sin_addr)
529 return(inaddr.sin_addr.s_addr);