1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.10 2001/06/29 13:29:15 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.10 2001/06/29 13:29:15 oes
39 * - Added remote (server) host IP to csp->http->host_ip_addr_str
40 * - Added detection of local socket IP and fqdn
41 * - Removed logentry from cancelled commit
43 * Revision 1.9 2001/06/07 23:06:09 jongfoster
44 * The host parameter to connect_to() is now const.
46 * Revision 1.8 2001/06/03 19:12:07 oes
49 * Revision 1.7 2001/05/28 16:14:00 jongfoster
50 * Fixing bug in LOG_LEVEL_LOG
52 * Revision 1.6 2001/05/26 17:28:32 jongfoster
55 * Revision 1.5 2001/05/26 15:26:15 jongfoster
56 * ACL feature now provides more security by immediately dropping
57 * connections from untrusted hosts.
59 * Revision 1.4 2001/05/26 00:37:42 jongfoster
60 * Cosmetic indentation correction.
62 * Revision 1.3 2001/05/25 21:57:54 jongfoster
63 * Now gives a warning under Windows if you try to bind
64 * it to a port that's already in use.
66 * Revision 1.2 2001/05/17 23:01:01 oes
67 * - Cleaned CRLF's from the sources and related files
69 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
70 * Initial import of version 2.9.3 source tree
73 *********************************************************************/
83 #include <sys/types.h>
88 #include <sys/timeb.h>
95 #include <netinet/in.h>
96 #include <sys/ioctl.h>
98 #include <sys/socket.h>
101 #include <netinet/tcp.h>
102 #include <arpa/inet.h>
110 #include "jbsockets.h"
114 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
117 /*********************************************************************
119 * Function : connect_to
121 * Description : Open a socket and connect to it. Will check
122 * that this is allowed according to ACL.
125 * 1 : host = hostname to connect to
126 * 2 : portnum = port to connent on
127 * 3 : csp = Current client state (buffers, headers, etc...)
128 * Not modified, only used for source IP and ACL.
130 * Returns : -1 => failure, else it is the socket file descriptor.
132 *********************************************************************/
133 int connect_to(const char *host, int portnum, struct client_state *csp)
135 struct sockaddr_in inaddr;
138 struct timeval tv[1];
139 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
141 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
144 struct access_control_addr dst[1];
145 #endif /* def ACL_FILES */
147 memset((char *)&inaddr, 0, sizeof inaddr);
149 if ((addr = resolve_hostname_to_ip(host)) == -1)
151 csp->http->host_ip_addr_str = strdup("unknown");
156 dst->addr = ntohl(addr);
159 if (block_acl(dst, csp))
164 #endif /* def ACL_FILES */
166 inaddr.sin_addr.s_addr = addr;
167 inaddr.sin_family = AF_INET;
168 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
170 if (sizeof(inaddr.sin_port) == sizeof(short))
172 inaddr.sin_port = htons((short)portnum);
176 inaddr.sin_port = htonl(portnum);
179 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
185 { /* turn off TCP coalescence */
187 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
189 #endif /* def TCP_NODELAY */
191 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
192 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
195 fcntl(fd, F_SETFL, flags);
197 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
199 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
202 if (errno == WSAEINPROGRESS)
203 #else /* ifndef _WIN32 */
204 if (errno == EINPROGRESS)
205 #endif /* ndef _WIN32 */
217 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
221 fcntl(fd, F_SETFL, flags);
223 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
225 /* wait for connection to complete */
232 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
242 /*********************************************************************
244 * Function : write_socket
246 * Description : Write the contents of buf (for n bytes) to socket fd.
249 * 1 : fd = file descriptor (aka. handle) of socket to write to.
250 * 2 : buf = pointer to data to be written.
251 * 3 : len = length of data to be written to the socket "fd".
253 * Returns : Win32 & Unix: If no error occurs, returns the total number of
254 * bytes sent, which can be less than the number
255 * indicated by len. Otherwise, returns (-1).
257 *********************************************************************/
258 int write_socket(int fd, const char *buf, int len)
265 log_error(LOG_LEVEL_LOG, "%N", len, buf);
267 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
268 return( send(fd, buf, len, 0));
270 return( write(fd, buf, len));
276 /*********************************************************************
278 * Function : read_socket
280 * Description : Read from a TCP/IP socket in a platform independent way.
283 * 1 : fd = file descriptor of the socket to read
284 * 2 : buf = pointer to buffer where data will be written
285 * Must be >= len bytes long.
286 * 3 : len = maximum number of bytes to read
288 * Returns : On success, the number of bytes read is returned (zero
289 * indicates end of file), and the file position is advanced
290 * by this number. It is not an error if this number is
291 * smaller than the number of bytes requested; this may hap-
292 * pen for example because fewer bytes are actually available
293 * right now (maybe because we were close to end-of-file, or
294 * because we are reading from a pipe, or from a terminal),
295 * or because read() was interrupted by a signal. On error,
296 * -1 is returned, and errno is set appropriately. In this
297 * case it is left unspecified whether the file position (if
300 *********************************************************************/
301 int read_socket(int fd, char *buf, int len)
308 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
309 return( recv(fd, buf, len, 0));
311 return( read(fd, buf, len));
316 /*********************************************************************
318 * Function : close_socket
320 * Description : Closes a TCP/IP socket
323 * 1 : fd = file descriptor of socket to be closed
327 *********************************************************************/
328 void close_socket(int fd)
330 #if defined(_WIN32) || defined(__BEOS__)
341 /*********************************************************************
343 * Function : bind_port
345 * Description : Call socket, set socket options, and listen.
346 * Called by listen_loop to "boot up" our proxy address.
349 * 1 : hostnam = TCP/IP address to bind/listen to
350 * 2 : portnum = port to listen on
352 * Returns : if success, return file descriptor
353 * if failure, returns -2 if address is in use, otherwise -1
355 *********************************************************************/
356 int bind_port(const char *hostnam, int portnum)
358 struct sockaddr_in inaddr;
362 memset((char *)&inaddr, '\0', sizeof inaddr);
364 inaddr.sin_family = AF_INET;
365 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
367 if (sizeof(inaddr.sin_port) == sizeof(short))
369 inaddr.sin_port = htons((short)portnum);
373 inaddr.sin_port = htonl(portnum);
376 fd = socket(AF_INET, SOCK_STREAM, 0);
385 * FIXME: This is not needed for Win32 - in fact, it stops
386 * duplicate instances of JunkBuster from being caught.
387 * Is this really needed under UNIX, or should it be taked out?
390 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
391 #endif /* ndef _WIN32 */
393 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
397 if (errno == WSAEADDRINUSE)
399 if (errno == EADDRINUSE)
410 while (listen(fd, 5) == -1)
423 /*********************************************************************
425 * Function : accept_connection
427 * Description : Accepts a connection on a socket. Socket must have
428 * been created using bind_port().
431 * 1 : csp = Client state, cfd, ip_addr_str, and
432 * ip_addr_long will be set by this routine.
433 * 2 : fd = file descriptor returned from bind_port
435 * Returns : when a connection is accepted, it returns 1 (TRUE).
436 * On an error it returns 0 (FALSE).
438 *********************************************************************/
439 int accept_connection(struct client_state * csp, int fd)
441 struct sockaddr raddr, laddr;
442 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
443 struct sockaddr_in *lap = (struct sockaddr_in *) &laddr;
444 struct hostent *host = NULL;
445 int afd, raddrlen, laddrlen;
449 raddrlen = sizeof raddr;
452 afd = accept (fd, &raddr, &raddrlen);
453 } while (afd < 1 && errno == EINTR);
461 * Determine the IP-Adress that the client used to reach us
462 * and the hostname associated with that address
464 if (!getsockname(afd, &laddr, &laddrlen))
466 csp->my_ip_addr_str = strdup(inet_ntoa(lap->sin_addr));
468 host = gethostbyaddr(laddr.sa_data + 2, 4, AF_INET);
471 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %s\n", hstrerror(h_errno));
475 csp->my_hostname = strdup(host->h_name);
480 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
481 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
488 /*********************************************************************
490 * Function : resolve_hostname_to_ip
492 * Description : Resolve a hostname to an internet tcp/ip address.
493 * NULL or an empty string resolve to INADDR_ANY.
496 * 1 : host = hostname to resolve
498 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
500 *********************************************************************/
501 int resolve_hostname_to_ip(const char *host)
503 struct sockaddr_in inaddr;
504 struct hostent *hostp;
506 if ((host == NULL) || (*host == '\0'))
511 memset((char *) &inaddr, 0, sizeof inaddr);
513 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
515 if ((hostp = gethostbyname(host)) == NULL)
520 if (hostp->h_addrtype != AF_INET)
523 errno = WSAEPROTOTYPE;
530 (char *) &inaddr.sin_addr,
531 (char *) hostp->h_addr,
532 sizeof(inaddr.sin_addr)
535 return(inaddr.sin_addr.s_addr);