1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.8 2001/06/03 11:03:48 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 11:03:48 oes
45 * adapted to new enlist_unique arg format
49 * introduced confdir option
51 * filters.c filtrers.h
53 * extracted-CGI relevant stuff
61 * support for new cgi mechansim
65 * functions for new list type: "map"
66 * extended enlist_unique
73 * deleted const struct interceptors
81 * added struct http_response
82 * changes struct interceptors to struct cgi_dispatcher
83 * moved HTML stuff to cgi.h
92 * Revision 1.7 2001/05/28 16:14:00 jongfoster
93 * Fixing bug in LOG_LEVEL_LOG
95 * Revision 1.6 2001/05/26 17:28:32 jongfoster
98 * Revision 1.5 2001/05/26 15:26:15 jongfoster
99 * ACL feature now provides more security by immediately dropping
100 * connections from untrusted hosts.
102 * Revision 1.4 2001/05/26 00:37:42 jongfoster
103 * Cosmetic indentation correction.
105 * Revision 1.3 2001/05/25 21:57:54 jongfoster
106 * Now gives a warning under Windows if you try to bind
107 * it to a port that's already in use.
109 * Revision 1.2 2001/05/17 23:01:01 oes
110 * - Cleaned CRLF's from the sources and related files
112 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
113 * Initial import of version 2.9.3 source tree
116 *********************************************************************/
126 #include <sys/types.h>
131 #include <sys/timeb.h>
137 #include <sys/time.h>
138 #include <netinet/in.h>
139 #include <sys/ioctl.h>
141 #include <sys/socket.h>
144 #include <netinet/tcp.h>
145 #include <arpa/inet.h>
153 #include "jbsockets.h"
157 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
160 /*********************************************************************
162 * Function : connect_to
164 * Description : Open a socket and connect to it. Will check
165 * that this is allowed according to ACL.
168 * 1 : host = hostname to connect to
169 * 2 : portnum = port to connent on
170 * 3 : csp = Current client state (buffers, headers, etc...)
171 * Not modified, only used for source IP and ACL.
173 * Returns : -1 => failure, else it is the socket file descriptor.
175 *********************************************************************/
176 int connect_to(char *host, int portnum, struct client_state *csp)
178 struct sockaddr_in inaddr;
181 struct timeval tv[1];
182 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
184 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
187 struct access_control_addr dst[1];
188 #endif /* def ACL_FILES */
190 memset((char *)&inaddr, 0, sizeof inaddr);
192 if ((addr = resolve_hostname_to_ip(host)) == -1)
198 dst->addr = ntohl(addr);
201 if (block_acl(dst, csp))
206 #endif /* def ACL_FILES */
208 inaddr.sin_addr.s_addr = addr;
209 inaddr.sin_family = AF_INET;
211 if (sizeof(inaddr.sin_port) == sizeof(short))
213 inaddr.sin_port = htons((short)portnum);
217 inaddr.sin_port = htonl(portnum);
220 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
226 { /* turn off TCP coalescence */
228 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
230 #endif /* def TCP_NODELAY */
232 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
233 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
236 fcntl(fd, F_SETFL, flags);
238 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
240 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
243 if (errno == WSAEINPROGRESS)
244 #else /* ifndef _WIN32 */
245 if (errno == EINPROGRESS)
246 #endif /* ndef _WIN32 */
258 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
262 fcntl(fd, F_SETFL, flags);
264 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
266 /* wait for connection to complete */
273 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
283 /*********************************************************************
285 * Function : write_socket
287 * Description : Write the contents of buf (for n bytes) to socket fd.
290 * 1 : fd = file descriptor (aka. handle) of socket to write to.
291 * 2 : buf = pointer to data to be written.
292 * 3 : len = length of data to be written to the socket "fd".
294 * Returns : Win32 & Unix: If no error occurs, returns the total number of
295 * bytes sent, which can be less than the number
296 * indicated by len. Otherwise, returns (-1).
298 *********************************************************************/
299 int write_socket(int fd, const char *buf, int len)
306 log_error(LOG_LEVEL_LOG, "%N", len, buf);
308 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
309 return( send(fd, buf, len, 0));
311 return( write(fd, buf, len));
317 /*********************************************************************
319 * Function : read_socket
321 * Description : Read from a TCP/IP socket in a platform independent way.
324 * 1 : fd = file descriptor of the socket to read
325 * 2 : buf = pointer to buffer where data will be written
326 * Must be >= len bytes long.
327 * 3 : len = maximum number of bytes to read
329 * Returns : On success, the number of bytes read is returned (zero
330 * indicates end of file), and the file position is advanced
331 * by this number. It is not an error if this number is
332 * smaller than the number of bytes requested; this may hap-
333 * pen for example because fewer bytes are actually available
334 * right now (maybe because we were close to end-of-file, or
335 * because we are reading from a pipe, or from a terminal),
336 * or because read() was interrupted by a signal. On error,
337 * -1 is returned, and errno is set appropriately. In this
338 * case it is left unspecified whether the file position (if
341 *********************************************************************/
342 int read_socket(int fd, char *buf, int len)
349 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
350 return( recv(fd, buf, len, 0));
352 return( read(fd, buf, len));
357 /*********************************************************************
359 * Function : close_socket
361 * Description : Closes a TCP/IP socket
364 * 1 : fd = file descriptor of socket to be closed
368 *********************************************************************/
369 void close_socket(int fd)
371 #if defined(_WIN32) || defined(__BEOS__)
382 /*********************************************************************
384 * Function : bind_port
386 * Description : Call socket, set socket options, and listen.
387 * Called by listen_loop to "boot up" our proxy address.
390 * 1 : hostnam = TCP/IP address to bind/listen to
391 * 2 : portnum = port to listen on
393 * Returns : if success, return file descriptor
394 * if failure, returns -2 if address is in use, otherwise -1
396 *********************************************************************/
397 int bind_port(const char *hostnam, int portnum)
399 struct sockaddr_in inaddr;
403 memset((char *)&inaddr, '\0', sizeof inaddr);
405 inaddr.sin_family = AF_INET;
406 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
408 if (sizeof(inaddr.sin_port) == sizeof(short))
410 inaddr.sin_port = htons((short)portnum);
414 inaddr.sin_port = htonl(portnum);
417 fd = socket(AF_INET, SOCK_STREAM, 0);
426 * FIXME: This is not needed for Win32 - in fact, it stops
427 * duplicate instances of JunkBuster from being caught.
428 * Is this really needed under UNIX, or should it be taked out?
431 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
432 #endif /* ndef _WIN32 */
434 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
438 if (errno == WSAEADDRINUSE)
440 if (errno == EADDRINUSE)
451 while (listen(fd, 5) == -1)
464 /*********************************************************************
466 * Function : accept_connection
468 * Description : Accepts a connection on a socket. Socket must have
469 * been created using bind_port().
472 * 1 : csp = Client state, cfd, ip_addr_str, and
473 * ip_addr_long will be set by this routine.
474 * 2 : fd = file descriptor returned from bind_port
476 * Returns : when a connection is accepted, it returns 1 (TRUE).
477 * On an error it returns 0 (FALSE).
479 *********************************************************************/
480 int accept_connection(struct client_state * csp, int fd)
482 struct sockaddr raddr;
483 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
486 raddrlen = sizeof raddr;
489 afd = accept (fd, &raddr, &raddrlen);
490 } while (afd < 1 && errno == EINTR);
498 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
499 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
505 /*********************************************************************
507 * Function : resolve_hostname_to_ip
509 * Description : Resolve a hostname to an internet tcp/ip address.
510 * NULL or an empty string resolve to INADDR_ANY.
513 * 1 : host = hostname to resolve
515 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
517 *********************************************************************/
518 int resolve_hostname_to_ip(const char *host)
520 struct sockaddr_in inaddr;
521 struct hostent *hostp;
523 if ((host == NULL) || (*host == '\0'))
528 memset((char *) &inaddr, 0, sizeof inaddr);
530 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
532 if ((hostp = gethostbyname(host)) == NULL)
537 if (hostp->h_addrtype != AF_INET)
540 errno = WSAEPROTOTYPE;
547 (char *) &inaddr.sin_addr,
548 (char *) hostp->h_addr,
549 sizeof(inaddr.sin_addr)
552 return(inaddr.sin_addr.s_addr);