1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.4 2001/05/26 00:37:42 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.4 2001/05/26 00:37:42 jongfoster
39 * Cosmetic indentation correction.
41 * Revision 1.3 2001/05/25 21:57:54 jongfoster
42 * Now gives a warning under Windows if you try to bind
43 * it to a port that's already in use.
45 * Revision 1.2 2001/05/17 23:01:01 oes
46 * - Cleaned CRLF's from the sources and related files
48 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
49 * Initial import of version 2.9.3 source tree
52 *********************************************************************/
62 #include <sys/types.h>
67 #include <sys/timeb.h>
74 #include <netinet/in.h>
75 #include <sys/ioctl.h>
77 #include <sys/socket.h>
80 #include <netinet/tcp.h>
81 #include <arpa/inet.h>
89 #include "jbsockets.h"
92 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
95 /*********************************************************************
97 * Function : connect_to
99 * Description : Open a socket and connect to it. Will check
100 * that this is allowed according to ACL.
103 * 1 : host = hostname to connect to
104 * 2 : portnum = port to connent on
105 * 3 : csp = Current client state (buffers, headers, etc...)
106 * Not modified, only used for source IP and ACL.
108 * Returns : -1 => failure, else it is the socket file descriptor.
110 *********************************************************************/
111 int connect_to(char *host, int portnum, struct client_state *csp)
113 struct sockaddr_in inaddr;
116 struct timeval tv[1];
117 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
119 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
122 struct access_control_addr dst[1];
123 #endif /* def ACL_FILES */
125 memset((char *)&inaddr, 0, sizeof inaddr);
127 if ((addr = resolve_hostname_to_ip(host)) == -1)
133 dst->addr = ntohl(addr);
136 if (block_acl(dst, csp))
141 #endif /* def ACL_FILES */
143 inaddr.sin_addr.s_addr = addr;
144 inaddr.sin_family = AF_INET;
146 if (sizeof(inaddr.sin_port) == sizeof(short))
148 inaddr.sin_port = htons((short)portnum);
152 inaddr.sin_port = htonl(portnum);
155 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
161 { /* turn off TCP coalescence */
163 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
165 #endif /* def TCP_NODELAY */
167 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
168 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
171 fcntl(fd, F_SETFL, flags);
173 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
175 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
178 if (errno == WSAEINPROGRESS)
179 #else /* ifndef _WIN32 */
180 if (errno == EINPROGRESS)
181 #endif /* ndef _WIN32 */
193 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
197 fcntl(fd, F_SETFL, flags);
199 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
201 /* wait for connection to complete */
208 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
218 /*********************************************************************
220 * Function : write_socket
222 * Description : Write the contents of buf (for n bytes) to socket fd.
225 * 1 : fd = file descriptor (aka. handle) of socket to write to.
226 * 2 : buf = pointer to data to be written.
227 * 3 : len = length of data to be written to the socket "fd".
229 * Returns : Win32: If no error occurs, returns the total number of
230 * bytes sent, which can be less than the number
231 * indicated by len. Otherwise, returns (-1).
232 * Unix: ??? (Please fill me in!)
234 *********************************************************************/
235 int write_socket(int fd, const char *buf, int len)
242 /* if (DEBUG(LOG)) fwrite(buf, n, 1, logfp); */
244 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
245 return( send(fd, buf, len, 0));
247 return( write(fd, buf, len));
253 /*********************************************************************
255 * Function : read_socket
257 * Description : Read from a TCP/IP socket in a platform independent way.
260 * 1 : fd = file descriptor of the socket to read
261 * 2 : buf = pointer to buffer where data will be written
262 * Must be >= len bytes long.
263 * 3 : len = maximum number of bytes to read
265 * Returns : On success, the number of bytes read is returned (zero
266 * indicates end of file), and the file position is advanced
267 * by this number. It is not an error if this number is
268 * smaller than the number of bytes requested; this may hap-
269 * pen for example because fewer bytes are actually available
270 * right now (maybe because we were close to end-of-file, or
271 * because we are reading from a pipe, or from a terminal),
272 * or because read() was interrupted by a signal. On error,
273 * -1 is returned, and errno is set appropriately. In this
274 * case it is left unspecified whether the file position (if
277 *********************************************************************/
278 int read_socket(int fd, char *buf, int len)
285 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
286 return( recv(fd, buf, len, 0));
288 return( read(fd, buf, len));
293 /*********************************************************************
295 * Function : close_socket
297 * Description : Closes a TCP/IP socket
300 * 1 : fd = file descriptor of socket to be closed
304 *********************************************************************/
305 void close_socket(int fd)
307 #if defined(_WIN32) || defined(__BEOS__)
318 /*********************************************************************
320 * Function : bind_port
322 * Description : Call socket, set socket options, and listen.
323 * Called by listen_loop to "boot up" our proxy address.
326 * 1 : hostnam = TCP/IP address to bind/listen to
327 * 2 : portnum = port to listen on
329 * Returns : if success, return file descriptor
330 * if failure, returns -2 if address is in use, otherwise -1
332 *********************************************************************/
333 int bind_port(const char *hostnam, int portnum)
335 struct sockaddr_in inaddr;
339 memset((char *)&inaddr, '\0', sizeof inaddr);
341 inaddr.sin_family = AF_INET;
342 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
344 if (sizeof(inaddr.sin_port) == sizeof(short))
346 inaddr.sin_port = htons((short)portnum);
350 inaddr.sin_port = htonl(portnum);
353 fd = socket(AF_INET, SOCK_STREAM, 0);
362 * FIXME: This is not needed for Win32 - in fact, it stops
363 * duplicate instances of JunkBuster from being caught.
364 * Is this really needed under UNIX, or should it be taked out?
367 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
368 #endif /* ndef _WIN32 */
370 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
374 if (errno == WSAEADDRINUSE)
376 if (errno == EADDRINUSE)
387 while (listen(fd, 5) == -1)
400 /*********************************************************************
402 * Function : accept_connection
404 * Description : Accepts a connection on a socket. Socket must have
405 * been created using bind_port().
408 * 1 : csp = Client state, cfd, ip_addr_str, and
409 * ip_addr_long will be set by this routine.
410 * 2 : fd = file descriptor returned from bind_port
412 * Returns : when a connection is accepted, it returns 1 (TRUE).
413 * On an error it returns 0 (FALSE).
415 *********************************************************************/
416 int accept_connection(struct client_state * csp, int fd)
418 struct sockaddr raddr;
419 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
422 raddrlen = sizeof raddr;
425 afd = accept (fd, &raddr, &raddrlen);
426 } while (afd < 1 && errno == EINTR);
434 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
435 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
441 /*********************************************************************
443 * Function : resolve_hostname_to_ip
445 * Description : Resolve a hostname to an internet tcp/ip address.
446 * NULL or an empty string resolve to INADDR_ANY.
449 * 1 : host = hostname to resolve
451 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
453 *********************************************************************/
454 int resolve_hostname_to_ip(const char *host)
456 struct sockaddr_in inaddr;
457 struct hostent *hostp;
459 if ((host == NULL) || (*host == '\0'))
464 memset((char *) &inaddr, 0, sizeof inaddr);
466 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
468 if ((hostp = gethostbyname(host)) == NULL)
473 if (hostp->h_addrtype != AF_INET)
476 errno = WSAEPROTOTYPE;
483 (char *) &inaddr.sin_addr,
484 (char *) hostp->h_addr,
485 sizeof(inaddr.sin_addr)
488 return(inaddr.sin_addr.s_addr);