1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.16 2001/07/30 22:08:36 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.16 2001/07/30 22:08:36 jongfoster
39 * Tidying up #defines:
40 * - All feature #defines are now of the form FEATURE_xxx
41 * - Permanently turned off WIN_GUI_EDIT
42 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
44 * Revision 1.15 2001/07/29 17:40:43 jongfoster
45 * Fixed compiler warning by adding a cast
47 * Revision 1.14 2001/07/18 13:47:59 oes
48 * Eliminated dirty hack for getsockbyname()
50 * Revision 1.13 2001/07/15 13:56:57 jongfoster
51 * Removing unused local variable.
53 * Revision 1.12 2001/07/01 17:04:11 oes
54 * Bugfix: accept_connection no longer uses the obsolete hstrerror() function
56 * Revision 1.11 2001/06/29 21:45:41 oes
57 * Indentation, CRLF->LF, Tab-> Space
59 * Revision 1.10 2001/06/29 13:29:15 oes
60 * - Added remote (server) host IP to csp->http->host_ip_addr_str
61 * - Added detection of local socket IP and fqdn
62 * - Removed logentry from cancelled commit
64 * Revision 1.9 2001/06/07 23:06:09 jongfoster
65 * The host parameter to connect_to() is now const.
67 * Revision 1.8 2001/06/03 19:12:07 oes
70 * Revision 1.7 2001/05/28 16:14:00 jongfoster
71 * Fixing bug in LOG_LEVEL_LOG
73 * Revision 1.6 2001/05/26 17:28:32 jongfoster
76 * Revision 1.5 2001/05/26 15:26:15 jongfoster
77 * ACL feature now provides more security by immediately dropping
78 * connections from untrusted hosts.
80 * Revision 1.4 2001/05/26 00:37:42 jongfoster
81 * Cosmetic indentation correction.
83 * Revision 1.3 2001/05/25 21:57:54 jongfoster
84 * Now gives a warning under Windows if you try to bind
85 * it to a port that's already in use.
87 * Revision 1.2 2001/05/17 23:01:01 oes
88 * - Cleaned CRLF's from the sources and related files
90 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
91 * Initial import of version 2.9.3 source tree
94 *********************************************************************/
104 #include <sys/types.h>
109 #include <sys/timeb.h>
115 #include <sys/time.h>
116 #include <netinet/in.h>
117 #include <sys/ioctl.h>
119 #include <sys/socket.h>
122 #include <netinet/tcp.h>
123 #include <arpa/inet.h>
131 #include "jbsockets.h"
135 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
138 /*********************************************************************
140 * Function : connect_to
142 * Description : Open a socket and connect to it. Will check
143 * that this is allowed according to ACL.
146 * 1 : host = hostname to connect to
147 * 2 : portnum = port to connent on
148 * 3 : csp = Current client state (buffers, headers, etc...)
149 * Not modified, only used for source IP and ACL.
151 * Returns : -1 => failure, else it is the socket file descriptor.
153 *********************************************************************/
154 int connect_to(const char *host, int portnum, struct client_state *csp)
156 struct sockaddr_in inaddr;
159 struct timeval tv[1];
160 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
162 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
165 struct access_control_addr dst[1];
166 #endif /* def FEATURE_ACL */
168 memset((char *)&inaddr, 0, sizeof inaddr);
170 if ((addr = resolve_hostname_to_ip(host)) == -1)
172 csp->http->host_ip_addr_str = strdup("unknown");
177 dst->addr = ntohl(addr);
180 if (block_acl(dst, csp))
185 #endif /* def FEATURE_ACL */
187 inaddr.sin_addr.s_addr = addr;
188 inaddr.sin_family = AF_INET;
189 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
192 if (sizeof(inaddr.sin_port) == sizeof(short))
193 #endif /* ndef _WIN32 */
195 inaddr.sin_port = htons((short)portnum);
200 inaddr.sin_port = htonl(portnum);
202 #endif /* ndef _WIN32 */
204 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
210 { /* turn off TCP coalescence */
212 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
214 #endif /* def TCP_NODELAY */
216 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
217 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
220 fcntl(fd, F_SETFL, flags);
222 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
224 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
227 if (errno == WSAEINPROGRESS)
228 #else /* ifndef _WIN32 */
229 if (errno == EINPROGRESS)
230 #endif /* ndef _WIN32 */
242 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
246 fcntl(fd, F_SETFL, flags);
248 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
250 /* wait for connection to complete */
257 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
267 /*********************************************************************
269 * Function : write_socket
271 * Description : Write the contents of buf (for n bytes) to socket fd.
274 * 1 : fd = file descriptor (aka. handle) of socket to write to.
275 * 2 : buf = pointer to data to be written.
276 * 3 : len = length of data to be written to the socket "fd".
278 * Returns : Win32 & Unix: If no error occurs, returns the total number of
279 * bytes sent, which can be less than the number
280 * indicated by len. Otherwise, returns (-1).
282 *********************************************************************/
283 int write_socket(int fd, const char *buf, int len)
290 log_error(LOG_LEVEL_LOG, "%N", len, buf);
292 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
293 return( send(fd, buf, len, 0));
295 return( write(fd, buf, len));
301 /*********************************************************************
303 * Function : read_socket
305 * Description : Read from a TCP/IP socket in a platform independent way.
308 * 1 : fd = file descriptor of the socket to read
309 * 2 : buf = pointer to buffer where data will be written
310 * Must be >= len bytes long.
311 * 3 : len = maximum number of bytes to read
313 * Returns : On success, the number of bytes read is returned (zero
314 * indicates end of file), and the file position is advanced
315 * by this number. It is not an error if this number is
316 * smaller than the number of bytes requested; this may hap-
317 * pen for example because fewer bytes are actually available
318 * right now (maybe because we were close to end-of-file, or
319 * because we are reading from a pipe, or from a terminal),
320 * or because read() was interrupted by a signal. On error,
321 * -1 is returned, and errno is set appropriately. In this
322 * case it is left unspecified whether the file position (if
325 *********************************************************************/
326 int read_socket(int fd, char *buf, int len)
333 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
334 return( recv(fd, buf, len, 0));
336 return( read(fd, buf, len));
341 /*********************************************************************
343 * Function : close_socket
345 * Description : Closes a TCP/IP socket
348 * 1 : fd = file descriptor of socket to be closed
352 *********************************************************************/
353 void close_socket(int fd)
355 #if defined(_WIN32) || defined(__BEOS__)
366 /*********************************************************************
368 * Function : bind_port
370 * Description : Call socket, set socket options, and listen.
371 * Called by listen_loop to "boot up" our proxy address.
374 * 1 : hostnam = TCP/IP address to bind/listen to
375 * 2 : portnum = port to listen on
377 * Returns : if success, return file descriptor
378 * if failure, returns -2 if address is in use, otherwise -1
380 *********************************************************************/
381 int bind_port(const char *hostnam, int portnum)
383 struct sockaddr_in inaddr;
387 memset((char *)&inaddr, '\0', sizeof inaddr);
389 inaddr.sin_family = AF_INET;
390 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
393 if (sizeof(inaddr.sin_port) == sizeof(short))
394 #endif /* ndef _WIN32 */
396 inaddr.sin_port = htons((short)portnum);
401 inaddr.sin_port = htonl(portnum);
403 #endif /* ndef _WIN32 */
405 fd = socket(AF_INET, SOCK_STREAM, 0);
414 * FIXME: This is not needed for Win32 - in fact, it stops
415 * duplicate instances of JunkBuster from being caught.
416 * Is this really needed under UNIX, or should it be taked out?
419 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
420 #endif /* ndef _WIN32 */
422 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
426 if (errno == WSAEADDRINUSE)
428 if (errno == EADDRINUSE)
439 while (listen(fd, 5) == -1)
452 /*********************************************************************
454 * Function : accept_connection
456 * Description : Accepts a connection on a socket. Socket must have
457 * been created using bind_port().
460 * 1 : csp = Client state, cfd, ip_addr_str, and
461 * ip_addr_long will be set by this routine.
462 * 2 : fd = file descriptor returned from bind_port
464 * Returns : when a connection is accepted, it returns 1 (TRUE).
465 * On an error it returns 0 (FALSE).
467 *********************************************************************/
468 int accept_connection(struct client_state * csp, int fd)
470 struct sockaddr_in client, server;
471 struct hostent *host = NULL;
472 int afd, c_length, s_length;
474 c_length = s_length = sizeof(client);
478 afd = accept (fd, (struct sockaddr *) &client, &c_length);
479 } while (afd < 1 && errno == EINTR);
487 * Determine the IP-Adress that the client used to reach us
488 * and the hostname associated with that address
490 if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
492 csp->my_ip_addr_str = strdup(inet_ntoa(server.sin_addr));
494 host = gethostbyaddr((const char *)&server.sin_addr,
495 sizeof(server.sin_addr), AF_INET);
498 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
502 csp->my_hostname = strdup(host->h_name);
507 csp->ip_addr_str = strdup(inet_ntoa(client.sin_addr));
508 csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
515 /*********************************************************************
517 * Function : resolve_hostname_to_ip
519 * Description : Resolve a hostname to an internet tcp/ip address.
520 * NULL or an empty string resolve to INADDR_ANY.
523 * 1 : host = hostname to resolve
525 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
527 *********************************************************************/
528 int resolve_hostname_to_ip(const char *host)
530 struct sockaddr_in inaddr;
531 struct hostent *hostp;
533 if ((host == NULL) || (*host == '\0'))
538 memset((char *) &inaddr, 0, sizeof inaddr);
540 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
542 if ((hostp = gethostbyname(host)) == NULL)
547 if (hostp->h_addrtype != AF_INET)
550 errno = WSAEPROTOTYPE;
557 (char *) &inaddr.sin_addr,
558 (char *) hostp->h_addr,
559 sizeof(inaddr.sin_addr)
562 return(inaddr.sin_addr.s_addr);