1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.2 2001/05/17 23:01:01 oes 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.2 2001/05/17 23:01:01 oes
39 * - Cleaned CRLF's from the sources and related files
41 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
42 * Initial import of version 2.9.3 source tree
45 *********************************************************************/
55 #include <sys/types.h>
60 #include <sys/timeb.h>
67 #include <netinet/in.h>
68 #include <sys/ioctl.h>
70 #include <sys/socket.h>
73 #include <netinet/tcp.h>
74 #include <arpa/inet.h>
82 #include "jbsockets.h"
85 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
88 /*********************************************************************
90 * Function : connect_to
92 * Description : Open a socket and connect to it. Will check
93 * that this is allowed according to ACL.
96 * 1 : host = hostname to connect to
97 * 2 : portnum = port to connent on
98 * 3 : csp = Current client state (buffers, headers, etc...)
99 * Not modified, only used for source IP and ACL.
101 * Returns : -1 => failure, else it is the socket file descriptor.
103 *********************************************************************/
104 int connect_to(char *host, int portnum, struct client_state *csp)
106 struct sockaddr_in inaddr;
109 struct timeval tv[1];
110 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
112 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
115 struct access_control_addr src[1], dst[1];
116 #endif /* def ACL_FILES */
118 memset((char *)&inaddr, 0, sizeof inaddr);
120 if ((addr = resolve_hostname_to_ip(host)) == -1)
126 src->addr = csp->ip_addr_long;
129 dst->addr = ntohl(addr);
132 if (block_acl(src, dst, csp))
137 #endif /* def ACL_FILES */
139 inaddr.sin_addr.s_addr = addr;
140 inaddr.sin_family = AF_INET;
142 if (sizeof(inaddr.sin_port) == sizeof(short))
144 inaddr.sin_port = htons((short)portnum);
148 inaddr.sin_port = htonl(portnum);
151 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
157 { /* turn off TCP coalescence */
159 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
161 #endif /* def TCP_NODELAY */
163 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
164 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
167 fcntl(fd, F_SETFL, flags);
169 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
171 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
174 if (errno == WSAEINPROGRESS)
175 #else /* ifndef _WIN32 */
176 if (errno == EINPROGRESS)
177 #endif /* ndef _WIN32 */
189 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
193 fcntl(fd, F_SETFL, flags);
195 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
197 /* wait for connection to complete */
204 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
214 /*********************************************************************
216 * Function : write_socket
218 * Description : Write the contents of buf (for n bytes) to socket fd.
221 * 1 : fd = file descriptor (aka. handle) of socket to write to.
222 * 2 : buf = pointer to data to be written.
223 * 3 : len = length of data to be written to the socket "fd".
225 * Returns : Win32: If no error occurs, returns the total number of
226 * bytes sent, which can be less than the number
227 * indicated by len. Otherwise, returns (-1).
228 * Unix: ??? (Please fill me in!)
230 *********************************************************************/
231 int write_socket(int fd, const char *buf, int len)
233 if (len <= 0) return(0);
235 /* if (DEBUG(LOG)) fwrite(buf, n, 1, logfp); */
237 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
238 return( send(fd, buf, len, 0));
240 return( write(fd, buf, len));
246 /*********************************************************************
248 * Function : read_socket
250 * Description : Read from a TCP/IP socket in a platform independent way.
253 * 1 : fd = file descriptor of the socket to read
254 * 2 : buf = pointer to buffer where data will be written
255 * Must be >= len bytes long.
256 * 3 : len = maximum number of bytes to read
258 * Returns : On success, the number of bytes read is returned (zero
259 * indicates end of file), and the file position is advanced
260 * by this number. It is not an error if this number is
261 * smaller than the number of bytes requested; this may hap-
262 * pen for example because fewer bytes are actually available
263 * right now (maybe because we were close to end-of-file, or
264 * because we are reading from a pipe, or from a terminal),
265 * or because read() was interrupted by a signal. On error,
266 * -1 is returned, and errno is set appropriately. In this
267 * case it is left unspecified whether the file position (if
270 *********************************************************************/
271 int read_socket(int fd, char *buf, int len)
278 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
279 return( recv(fd, buf, len, 0));
281 return( read(fd, buf, len));
286 /*********************************************************************
288 * Function : close_socket
290 * Description : Closes a TCP/IP socket
293 * 1 : fd = file descriptor of socket to be closed
297 *********************************************************************/
298 void close_socket(int fd)
300 #if defined(_WIN32) || defined(__BEOS__)
311 /*********************************************************************
313 * Function : bind_port
315 * Description : Call socket, set socket options, and listen.
316 * Called by listen_loop to "boot up" our proxy address.
319 * 1 : hostnam = TCP/IP address to bind/listen to
320 * 2 : portnum = port to listen on
322 * Returns : if success, return file descriptor
323 * if failure, returns -2 if address is in use, otherwise -1
325 *********************************************************************/
326 int bind_port(const char *hostnam, int portnum)
328 struct sockaddr_in inaddr;
332 memset((char *)&inaddr, '\0', sizeof inaddr);
334 inaddr.sin_family = AF_INET;
335 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
337 if (sizeof(inaddr.sin_port) == sizeof(short))
339 inaddr.sin_port = htons((short)portnum);
343 inaddr.sin_port = htonl(portnum);
346 fd = socket(AF_INET, SOCK_STREAM, 0);
355 * FIXME: This is not needed for Win32 - in fact, it stops
356 * duplicate instances of JunkBuster from being caught.
357 * Is this really needed under UNIX, or should it be taked out?
360 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
361 #endif /* ndef _WIN32 */
363 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
367 if (errno == WSAEADDRINUSE)
369 if (errno == EADDRINUSE)
380 while (listen(fd, 5) == -1)
393 /*********************************************************************
395 * Function : accept_connection
397 * Description : Accepts a connection on a socket. Socket must have
398 * been created using bind_port().
401 * 1 : csp = Client state, cfd, ip_addr_str, and
402 * ip_addr_long will be set by this routine.
403 * 2 : fd = file descriptor returned from bind_port
405 * Returns : when a connection is accepted, it returns 1 (TRUE).
406 * On an error it returns 0 (FALSE).
408 *********************************************************************/
409 int accept_connection(struct client_state * csp, int fd)
411 struct sockaddr raddr;
412 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
415 raddrlen = sizeof raddr;
418 afd = accept (fd, &raddr, &raddrlen);
419 } while (afd < 1 && errno == EINTR);
427 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
428 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
434 /*********************************************************************
436 * Function : resolve_hostname_to_ip
438 * Description : Resolve a hostname to an internet tcp/ip address.
439 * NULL or an empty string resolve to INADDR_ANY.
442 * 1 : host = hostname to resolve
444 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
446 *********************************************************************/
447 int resolve_hostname_to_ip(const char *host)
449 struct sockaddr_in inaddr;
450 struct hostent *hostp;
452 if ((host == NULL) || (*host == '\0'))
457 memset((char *) &inaddr, 0, sizeof inaddr);
459 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
461 if ((hostp = gethostbyname(host)) == NULL)
466 if (hostp->h_addrtype != AF_INET)
469 errno = WSAEPROTOTYPE;
476 (char *) &inaddr.sin_addr,
477 (char *) hostp->h_addr,
478 sizeof(inaddr.sin_addr)
481 return(inaddr.sin_addr.s_addr);