1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.5 2001/05/26 15:26:15 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.5 2001/05/26 15:26:15 jongfoster
39 * ACL feature now provides more security by immediately dropping
40 * connections from untrusted hosts.
42 * Revision 1.4 2001/05/26 00:37:42 jongfoster
43 * Cosmetic indentation correction.
45 * Revision 1.3 2001/05/25 21:57:54 jongfoster
46 * Now gives a warning under Windows if you try to bind
47 * it to a port that's already in use.
49 * Revision 1.2 2001/05/17 23:01:01 oes
50 * - Cleaned CRLF's from the sources and related files
52 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
53 * Initial import of version 2.9.3 source tree
56 *********************************************************************/
66 #include <sys/types.h>
71 #include <sys/timeb.h>
78 #include <netinet/in.h>
79 #include <sys/ioctl.h>
81 #include <sys/socket.h>
84 #include <netinet/tcp.h>
85 #include <arpa/inet.h>
93 #include "jbsockets.h"
97 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
100 /*********************************************************************
102 * Function : connect_to
104 * Description : Open a socket and connect to it. Will check
105 * that this is allowed according to ACL.
108 * 1 : host = hostname to connect to
109 * 2 : portnum = port to connent on
110 * 3 : csp = Current client state (buffers, headers, etc...)
111 * Not modified, only used for source IP and ACL.
113 * Returns : -1 => failure, else it is the socket file descriptor.
115 *********************************************************************/
116 int connect_to(char *host, int portnum, struct client_state *csp)
118 struct sockaddr_in inaddr;
121 struct timeval tv[1];
122 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
124 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
127 struct access_control_addr dst[1];
128 #endif /* def ACL_FILES */
130 memset((char *)&inaddr, 0, sizeof inaddr);
132 if ((addr = resolve_hostname_to_ip(host)) == -1)
138 dst->addr = ntohl(addr);
141 if (block_acl(dst, csp))
146 #endif /* def ACL_FILES */
148 inaddr.sin_addr.s_addr = addr;
149 inaddr.sin_family = AF_INET;
151 if (sizeof(inaddr.sin_port) == sizeof(short))
153 inaddr.sin_port = htons((short)portnum);
157 inaddr.sin_port = htonl(portnum);
160 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
166 { /* turn off TCP coalescence */
168 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
170 #endif /* def TCP_NODELAY */
172 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
173 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
176 fcntl(fd, F_SETFL, flags);
178 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
180 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
183 if (errno == WSAEINPROGRESS)
184 #else /* ifndef _WIN32 */
185 if (errno == EINPROGRESS)
186 #endif /* ndef _WIN32 */
198 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
202 fcntl(fd, F_SETFL, flags);
204 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
206 /* wait for connection to complete */
213 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
223 /*********************************************************************
225 * Function : write_socket
227 * Description : Write the contents of buf (for n bytes) to socket fd.
230 * 1 : fd = file descriptor (aka. handle) of socket to write to.
231 * 2 : buf = pointer to data to be written.
232 * 3 : len = length of data to be written to the socket "fd".
234 * Returns : Win32: If no error occurs, returns the total number of
235 * bytes sent, which can be less than the number
236 * indicated by len. Otherwise, returns (-1).
237 * Unix: ??? (Please fill me in!)
239 *********************************************************************/
240 int write_socket(int fd, const char *buf, int len)
247 log_error(LOG_LEVEL_LOG, "%n", len, buf);
249 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
250 return( send(fd, buf, len, 0));
252 return( write(fd, buf, len));
258 /*********************************************************************
260 * Function : read_socket
262 * Description : Read from a TCP/IP socket in a platform independent way.
265 * 1 : fd = file descriptor of the socket to read
266 * 2 : buf = pointer to buffer where data will be written
267 * Must be >= len bytes long.
268 * 3 : len = maximum number of bytes to read
270 * Returns : On success, the number of bytes read is returned (zero
271 * indicates end of file), and the file position is advanced
272 * by this number. It is not an error if this number is
273 * smaller than the number of bytes requested; this may hap-
274 * pen for example because fewer bytes are actually available
275 * right now (maybe because we were close to end-of-file, or
276 * because we are reading from a pipe, or from a terminal),
277 * or because read() was interrupted by a signal. On error,
278 * -1 is returned, and errno is set appropriately. In this
279 * case it is left unspecified whether the file position (if
282 *********************************************************************/
283 int read_socket(int fd, char *buf, int len)
290 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
291 return( recv(fd, buf, len, 0));
293 return( read(fd, buf, len));
298 /*********************************************************************
300 * Function : close_socket
302 * Description : Closes a TCP/IP socket
305 * 1 : fd = file descriptor of socket to be closed
309 *********************************************************************/
310 void close_socket(int fd)
312 #if defined(_WIN32) || defined(__BEOS__)
323 /*********************************************************************
325 * Function : bind_port
327 * Description : Call socket, set socket options, and listen.
328 * Called by listen_loop to "boot up" our proxy address.
331 * 1 : hostnam = TCP/IP address to bind/listen to
332 * 2 : portnum = port to listen on
334 * Returns : if success, return file descriptor
335 * if failure, returns -2 if address is in use, otherwise -1
337 *********************************************************************/
338 int bind_port(const char *hostnam, int portnum)
340 struct sockaddr_in inaddr;
344 memset((char *)&inaddr, '\0', sizeof inaddr);
346 inaddr.sin_family = AF_INET;
347 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
349 if (sizeof(inaddr.sin_port) == sizeof(short))
351 inaddr.sin_port = htons((short)portnum);
355 inaddr.sin_port = htonl(portnum);
358 fd = socket(AF_INET, SOCK_STREAM, 0);
367 * FIXME: This is not needed for Win32 - in fact, it stops
368 * duplicate instances of JunkBuster from being caught.
369 * Is this really needed under UNIX, or should it be taked out?
372 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
373 #endif /* ndef _WIN32 */
375 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
379 if (errno == WSAEADDRINUSE)
381 if (errno == EADDRINUSE)
392 while (listen(fd, 5) == -1)
405 /*********************************************************************
407 * Function : accept_connection
409 * Description : Accepts a connection on a socket. Socket must have
410 * been created using bind_port().
413 * 1 : csp = Client state, cfd, ip_addr_str, and
414 * ip_addr_long will be set by this routine.
415 * 2 : fd = file descriptor returned from bind_port
417 * Returns : when a connection is accepted, it returns 1 (TRUE).
418 * On an error it returns 0 (FALSE).
420 *********************************************************************/
421 int accept_connection(struct client_state * csp, int fd)
423 struct sockaddr raddr;
424 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
427 raddrlen = sizeof raddr;
430 afd = accept (fd, &raddr, &raddrlen);
431 } while (afd < 1 && errno == EINTR);
439 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
440 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
446 /*********************************************************************
448 * Function : resolve_hostname_to_ip
450 * Description : Resolve a hostname to an internet tcp/ip address.
451 * NULL or an empty string resolve to INADDR_ANY.
454 * 1 : host = hostname to resolve
456 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
458 *********************************************************************/
459 int resolve_hostname_to_ip(const char *host)
461 struct sockaddr_in inaddr;
462 struct hostent *hostp;
464 if ((host == NULL) || (*host == '\0'))
469 memset((char *) &inaddr, 0, sizeof inaddr);
471 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
473 if ((hostp = gethostbyname(host)) == NULL)
478 if (hostp->h_addrtype != AF_INET)
481 errno = WSAEPROTOTYPE;
488 (char *) &inaddr.sin_addr,
489 (char *) hostp->h_addr,
490 sizeof(inaddr.sin_addr)
493 return(inaddr.sin_addr.s_addr);