1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.6 2001/05/26 17:28:32 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.6 2001/05/26 17:28:32 jongfoster
41 * Revision 1.5 2001/05/26 15:26:15 jongfoster
42 * ACL feature now provides more security by immediately dropping
43 * connections from untrusted hosts.
45 * Revision 1.4 2001/05/26 00:37:42 jongfoster
46 * Cosmetic indentation correction.
48 * Revision 1.3 2001/05/25 21:57:54 jongfoster
49 * Now gives a warning under Windows if you try to bind
50 * it to a port that's already in use.
52 * Revision 1.2 2001/05/17 23:01:01 oes
53 * - Cleaned CRLF's from the sources and related files
55 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
56 * Initial import of version 2.9.3 source tree
59 *********************************************************************/
69 #include <sys/types.h>
74 #include <sys/timeb.h>
81 #include <netinet/in.h>
82 #include <sys/ioctl.h>
84 #include <sys/socket.h>
87 #include <netinet/tcp.h>
88 #include <arpa/inet.h>
96 #include "jbsockets.h"
100 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
103 /*********************************************************************
105 * Function : connect_to
107 * Description : Open a socket and connect to it. Will check
108 * that this is allowed according to ACL.
111 * 1 : host = hostname to connect to
112 * 2 : portnum = port to connent on
113 * 3 : csp = Current client state (buffers, headers, etc...)
114 * Not modified, only used for source IP and ACL.
116 * Returns : -1 => failure, else it is the socket file descriptor.
118 *********************************************************************/
119 int connect_to(char *host, int portnum, struct client_state *csp)
121 struct sockaddr_in inaddr;
124 struct timeval tv[1];
125 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
127 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
130 struct access_control_addr dst[1];
131 #endif /* def ACL_FILES */
133 memset((char *)&inaddr, 0, sizeof inaddr);
135 if ((addr = resolve_hostname_to_ip(host)) == -1)
141 dst->addr = ntohl(addr);
144 if (block_acl(dst, csp))
149 #endif /* def ACL_FILES */
151 inaddr.sin_addr.s_addr = addr;
152 inaddr.sin_family = AF_INET;
154 if (sizeof(inaddr.sin_port) == sizeof(short))
156 inaddr.sin_port = htons((short)portnum);
160 inaddr.sin_port = htonl(portnum);
163 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
169 { /* turn off TCP coalescence */
171 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
173 #endif /* def TCP_NODELAY */
175 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
176 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
179 fcntl(fd, F_SETFL, flags);
181 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
183 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
186 if (errno == WSAEINPROGRESS)
187 #else /* ifndef _WIN32 */
188 if (errno == EINPROGRESS)
189 #endif /* ndef _WIN32 */
201 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
205 fcntl(fd, F_SETFL, flags);
207 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
209 /* wait for connection to complete */
216 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
226 /*********************************************************************
228 * Function : write_socket
230 * Description : Write the contents of buf (for n bytes) to socket fd.
233 * 1 : fd = file descriptor (aka. handle) of socket to write to.
234 * 2 : buf = pointer to data to be written.
235 * 3 : len = length of data to be written to the socket "fd".
237 * Returns : Win32: If no error occurs, returns the total number of
238 * bytes sent, which can be less than the number
239 * indicated by len. Otherwise, returns (-1).
240 * Unix: ??? (Please fill me in!)
242 *********************************************************************/
243 int write_socket(int fd, const char *buf, int len)
250 log_error(LOG_LEVEL_LOG, "%N", len, buf);
252 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
253 return( send(fd, buf, len, 0));
255 return( write(fd, buf, len));
261 /*********************************************************************
263 * Function : read_socket
265 * Description : Read from a TCP/IP socket in a platform independent way.
268 * 1 : fd = file descriptor of the socket to read
269 * 2 : buf = pointer to buffer where data will be written
270 * Must be >= len bytes long.
271 * 3 : len = maximum number of bytes to read
273 * Returns : On success, the number of bytes read is returned (zero
274 * indicates end of file), and the file position is advanced
275 * by this number. It is not an error if this number is
276 * smaller than the number of bytes requested; this may hap-
277 * pen for example because fewer bytes are actually available
278 * right now (maybe because we were close to end-of-file, or
279 * because we are reading from a pipe, or from a terminal),
280 * or because read() was interrupted by a signal. On error,
281 * -1 is returned, and errno is set appropriately. In this
282 * case it is left unspecified whether the file position (if
285 *********************************************************************/
286 int read_socket(int fd, char *buf, int len)
293 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
294 return( recv(fd, buf, len, 0));
296 return( read(fd, buf, len));
301 /*********************************************************************
303 * Function : close_socket
305 * Description : Closes a TCP/IP socket
308 * 1 : fd = file descriptor of socket to be closed
312 *********************************************************************/
313 void close_socket(int fd)
315 #if defined(_WIN32) || defined(__BEOS__)
326 /*********************************************************************
328 * Function : bind_port
330 * Description : Call socket, set socket options, and listen.
331 * Called by listen_loop to "boot up" our proxy address.
334 * 1 : hostnam = TCP/IP address to bind/listen to
335 * 2 : portnum = port to listen on
337 * Returns : if success, return file descriptor
338 * if failure, returns -2 if address is in use, otherwise -1
340 *********************************************************************/
341 int bind_port(const char *hostnam, int portnum)
343 struct sockaddr_in inaddr;
347 memset((char *)&inaddr, '\0', sizeof inaddr);
349 inaddr.sin_family = AF_INET;
350 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
352 if (sizeof(inaddr.sin_port) == sizeof(short))
354 inaddr.sin_port = htons((short)portnum);
358 inaddr.sin_port = htonl(portnum);
361 fd = socket(AF_INET, SOCK_STREAM, 0);
370 * FIXME: This is not needed for Win32 - in fact, it stops
371 * duplicate instances of JunkBuster from being caught.
372 * Is this really needed under UNIX, or should it be taked out?
375 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
376 #endif /* ndef _WIN32 */
378 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
382 if (errno == WSAEADDRINUSE)
384 if (errno == EADDRINUSE)
395 while (listen(fd, 5) == -1)
408 /*********************************************************************
410 * Function : accept_connection
412 * Description : Accepts a connection on a socket. Socket must have
413 * been created using bind_port().
416 * 1 : csp = Client state, cfd, ip_addr_str, and
417 * ip_addr_long will be set by this routine.
418 * 2 : fd = file descriptor returned from bind_port
420 * Returns : when a connection is accepted, it returns 1 (TRUE).
421 * On an error it returns 0 (FALSE).
423 *********************************************************************/
424 int accept_connection(struct client_state * csp, int fd)
426 struct sockaddr raddr;
427 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
430 raddrlen = sizeof raddr;
433 afd = accept (fd, &raddr, &raddrlen);
434 } while (afd < 1 && errno == EINTR);
442 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
443 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
449 /*********************************************************************
451 * Function : resolve_hostname_to_ip
453 * Description : Resolve a hostname to an internet tcp/ip address.
454 * NULL or an empty string resolve to INADDR_ANY.
457 * 1 : host = hostname to resolve
459 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
461 *********************************************************************/
462 int resolve_hostname_to_ip(const char *host)
464 struct sockaddr_in inaddr;
465 struct hostent *hostp;
467 if ((host == NULL) || (*host == '\0'))
472 memset((char *) &inaddr, 0, sizeof inaddr);
474 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
476 if ((hostp = gethostbyname(host)) == NULL)
481 if (hostp->h_addrtype != AF_INET)
484 errno = WSAEPROTOTYPE;
491 (char *) &inaddr.sin_addr,
492 (char *) hostp->h_addr,
493 sizeof(inaddr.sin_addr)
496 return(inaddr.sin_addr.s_addr);