1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.8 2001/06/03 19:12:07 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.8 2001/06/03 19:12:07 oes
41 * Revision 1.8 2001/06/03 11:03:48 oes
48 * adapted to new enlist_unique arg format
52 * introduced confdir option
54 * filters.c filtrers.h
56 * extracted-CGI relevant stuff
64 * support for new cgi mechansim
68 * functions for new list type: "map"
69 * extended enlist_unique
76 * deleted const struct interceptors
84 * added struct http_response
85 * changes struct interceptors to struct cgi_dispatcher
86 * moved HTML stuff to cgi.h
95 * Revision 1.7 2001/05/28 16:14:00 jongfoster
96 * Fixing bug in LOG_LEVEL_LOG
98 * Revision 1.6 2001/05/26 17:28:32 jongfoster
101 * Revision 1.5 2001/05/26 15:26:15 jongfoster
102 * ACL feature now provides more security by immediately dropping
103 * connections from untrusted hosts.
105 * Revision 1.4 2001/05/26 00:37:42 jongfoster
106 * Cosmetic indentation correction.
108 * Revision 1.3 2001/05/25 21:57:54 jongfoster
109 * Now gives a warning under Windows if you try to bind
110 * it to a port that's already in use.
112 * Revision 1.2 2001/05/17 23:01:01 oes
113 * - Cleaned CRLF's from the sources and related files
115 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
116 * Initial import of version 2.9.3 source tree
119 *********************************************************************/
129 #include <sys/types.h>
134 #include <sys/timeb.h>
140 #include <sys/time.h>
141 #include <netinet/in.h>
142 #include <sys/ioctl.h>
144 #include <sys/socket.h>
147 #include <netinet/tcp.h>
148 #include <arpa/inet.h>
156 #include "jbsockets.h"
160 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
163 /*********************************************************************
165 * Function : connect_to
167 * Description : Open a socket and connect to it. Will check
168 * that this is allowed according to ACL.
171 * 1 : host = hostname to connect to
172 * 2 : portnum = port to connent on
173 * 3 : csp = Current client state (buffers, headers, etc...)
174 * Not modified, only used for source IP and ACL.
176 * Returns : -1 => failure, else it is the socket file descriptor.
178 *********************************************************************/
179 int connect_to(const char *host, int portnum, struct client_state *csp)
181 struct sockaddr_in inaddr;
184 struct timeval tv[1];
185 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
187 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
190 struct access_control_addr dst[1];
191 #endif /* def ACL_FILES */
193 memset((char *)&inaddr, 0, sizeof inaddr);
195 if ((addr = resolve_hostname_to_ip(host)) == -1)
201 dst->addr = ntohl(addr);
204 if (block_acl(dst, csp))
209 #endif /* def ACL_FILES */
211 inaddr.sin_addr.s_addr = addr;
212 inaddr.sin_family = AF_INET;
214 if (sizeof(inaddr.sin_port) == sizeof(short))
216 inaddr.sin_port = htons((short)portnum);
220 inaddr.sin_port = htonl(portnum);
223 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
229 { /* turn off TCP coalescence */
231 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
233 #endif /* def TCP_NODELAY */
235 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
236 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
239 fcntl(fd, F_SETFL, flags);
241 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
243 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
246 if (errno == WSAEINPROGRESS)
247 #else /* ifndef _WIN32 */
248 if (errno == EINPROGRESS)
249 #endif /* ndef _WIN32 */
261 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
265 fcntl(fd, F_SETFL, flags);
267 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
269 /* wait for connection to complete */
276 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
286 /*********************************************************************
288 * Function : write_socket
290 * Description : Write the contents of buf (for n bytes) to socket fd.
293 * 1 : fd = file descriptor (aka. handle) of socket to write to.
294 * 2 : buf = pointer to data to be written.
295 * 3 : len = length of data to be written to the socket "fd".
297 * Returns : Win32 & Unix: If no error occurs, returns the total number of
298 * bytes sent, which can be less than the number
299 * indicated by len. Otherwise, returns (-1).
301 *********************************************************************/
302 int write_socket(int fd, const char *buf, int len)
309 log_error(LOG_LEVEL_LOG, "%N", len, buf);
311 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
312 return( send(fd, buf, len, 0));
314 return( write(fd, buf, len));
320 /*********************************************************************
322 * Function : read_socket
324 * Description : Read from a TCP/IP socket in a platform independent way.
327 * 1 : fd = file descriptor of the socket to read
328 * 2 : buf = pointer to buffer where data will be written
329 * Must be >= len bytes long.
330 * 3 : len = maximum number of bytes to read
332 * Returns : On success, the number of bytes read is returned (zero
333 * indicates end of file), and the file position is advanced
334 * by this number. It is not an error if this number is
335 * smaller than the number of bytes requested; this may hap-
336 * pen for example because fewer bytes are actually available
337 * right now (maybe because we were close to end-of-file, or
338 * because we are reading from a pipe, or from a terminal),
339 * or because read() was interrupted by a signal. On error,
340 * -1 is returned, and errno is set appropriately. In this
341 * case it is left unspecified whether the file position (if
344 *********************************************************************/
345 int read_socket(int fd, char *buf, int len)
352 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
353 return( recv(fd, buf, len, 0));
355 return( read(fd, buf, len));
360 /*********************************************************************
362 * Function : close_socket
364 * Description : Closes a TCP/IP socket
367 * 1 : fd = file descriptor of socket to be closed
371 *********************************************************************/
372 void close_socket(int fd)
374 #if defined(_WIN32) || defined(__BEOS__)
385 /*********************************************************************
387 * Function : bind_port
389 * Description : Call socket, set socket options, and listen.
390 * Called by listen_loop to "boot up" our proxy address.
393 * 1 : hostnam = TCP/IP address to bind/listen to
394 * 2 : portnum = port to listen on
396 * Returns : if success, return file descriptor
397 * if failure, returns -2 if address is in use, otherwise -1
399 *********************************************************************/
400 int bind_port(const char *hostnam, int portnum)
402 struct sockaddr_in inaddr;
406 memset((char *)&inaddr, '\0', sizeof inaddr);
408 inaddr.sin_family = AF_INET;
409 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
411 if (sizeof(inaddr.sin_port) == sizeof(short))
413 inaddr.sin_port = htons((short)portnum);
417 inaddr.sin_port = htonl(portnum);
420 fd = socket(AF_INET, SOCK_STREAM, 0);
429 * FIXME: This is not needed for Win32 - in fact, it stops
430 * duplicate instances of JunkBuster from being caught.
431 * Is this really needed under UNIX, or should it be taked out?
434 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
435 #endif /* ndef _WIN32 */
437 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
441 if (errno == WSAEADDRINUSE)
443 if (errno == EADDRINUSE)
454 while (listen(fd, 5) == -1)
467 /*********************************************************************
469 * Function : accept_connection
471 * Description : Accepts a connection on a socket. Socket must have
472 * been created using bind_port().
475 * 1 : csp = Client state, cfd, ip_addr_str, and
476 * ip_addr_long will be set by this routine.
477 * 2 : fd = file descriptor returned from bind_port
479 * Returns : when a connection is accepted, it returns 1 (TRUE).
480 * On an error it returns 0 (FALSE).
482 *********************************************************************/
483 int accept_connection(struct client_state * csp, int fd)
485 struct sockaddr raddr;
486 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
489 raddrlen = sizeof raddr;
492 afd = accept (fd, &raddr, &raddrlen);
493 } while (afd < 1 && errno == EINTR);
501 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
502 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
508 /*********************************************************************
510 * Function : resolve_hostname_to_ip
512 * Description : Resolve a hostname to an internet tcp/ip address.
513 * NULL or an empty string resolve to INADDR_ANY.
516 * 1 : host = hostname to resolve
518 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
520 *********************************************************************/
521 int resolve_hostname_to_ip(const char *host)
523 struct sockaddr_in inaddr;
524 struct hostent *hostp;
526 if ((host == NULL) || (*host == '\0'))
531 memset((char *) &inaddr, 0, sizeof inaddr);
533 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
535 if ((hostp = gethostbyname(host)) == NULL)
540 if (hostp->h_addrtype != AF_INET)
543 errno = WSAEPROTOTYPE;
550 (char *) &inaddr.sin_addr,
551 (char *) hostp->h_addr,
552 sizeof(inaddr.sin_addr)
555 return(inaddr.sin_addr.s_addr);