1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.14 2001/07/18 13:47:59 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.14 2001/07/18 13:47:59 oes
39 * Eliminated dirty hack for getsockbyname()
41 * Revision 1.13 2001/07/15 13:56:57 jongfoster
42 * Removing unused local variable.
44 * Revision 1.12 2001/07/01 17:04:11 oes
45 * Bugfix: accept_connection no longer uses the obsolete hstrerror() function
47 * Revision 1.11 2001/06/29 21:45:41 oes
48 * Indentation, CRLF->LF, Tab-> Space
50 * Revision 1.10 2001/06/29 13:29:15 oes
51 * - Added remote (server) host IP to csp->http->host_ip_addr_str
52 * - Added detection of local socket IP and fqdn
53 * - Removed logentry from cancelled commit
55 * Revision 1.9 2001/06/07 23:06:09 jongfoster
56 * The host parameter to connect_to() is now const.
58 * Revision 1.8 2001/06/03 19:12:07 oes
61 * Revision 1.7 2001/05/28 16:14:00 jongfoster
62 * Fixing bug in LOG_LEVEL_LOG
64 * Revision 1.6 2001/05/26 17:28:32 jongfoster
67 * Revision 1.5 2001/05/26 15:26:15 jongfoster
68 * ACL feature now provides more security by immediately dropping
69 * connections from untrusted hosts.
71 * Revision 1.4 2001/05/26 00:37:42 jongfoster
72 * Cosmetic indentation correction.
74 * Revision 1.3 2001/05/25 21:57:54 jongfoster
75 * Now gives a warning under Windows if you try to bind
76 * it to a port that's already in use.
78 * Revision 1.2 2001/05/17 23:01:01 oes
79 * - Cleaned CRLF's from the sources and related files
81 * Revision 1.1.1.1 2001/05/15 13:58:54 oes
82 * Initial import of version 2.9.3 source tree
85 *********************************************************************/
95 #include <sys/types.h>
100 #include <sys/timeb.h>
106 #include <sys/time.h>
107 #include <netinet/in.h>
108 #include <sys/ioctl.h>
110 #include <sys/socket.h>
113 #include <netinet/tcp.h>
114 #include <arpa/inet.h>
122 #include "jbsockets.h"
126 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
129 /*********************************************************************
131 * Function : connect_to
133 * Description : Open a socket and connect to it. Will check
134 * that this is allowed according to ACL.
137 * 1 : host = hostname to connect to
138 * 2 : portnum = port to connent on
139 * 3 : csp = Current client state (buffers, headers, etc...)
140 * Not modified, only used for source IP and ACL.
142 * Returns : -1 => failure, else it is the socket file descriptor.
144 *********************************************************************/
145 int connect_to(const char *host, int portnum, struct client_state *csp)
147 struct sockaddr_in inaddr;
150 struct timeval tv[1];
151 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
153 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
156 struct access_control_addr dst[1];
157 #endif /* def ACL_FILES */
159 memset((char *)&inaddr, 0, sizeof inaddr);
161 if ((addr = resolve_hostname_to_ip(host)) == -1)
163 csp->http->host_ip_addr_str = strdup("unknown");
168 dst->addr = ntohl(addr);
171 if (block_acl(dst, csp))
176 #endif /* def ACL_FILES */
178 inaddr.sin_addr.s_addr = addr;
179 inaddr.sin_family = AF_INET;
180 csp->http->host_ip_addr_str = strdup(inet_ntoa(inaddr.sin_addr));
182 if (sizeof(inaddr.sin_port) == sizeof(short))
184 inaddr.sin_port = htons((short)portnum);
188 inaddr.sin_port = htonl(portnum);
191 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
197 { /* turn off TCP coalescence */
199 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
201 #endif /* def TCP_NODELAY */
203 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
204 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
207 fcntl(fd, F_SETFL, flags);
209 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
211 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
214 if (errno == WSAEINPROGRESS)
215 #else /* ifndef _WIN32 */
216 if (errno == EINPROGRESS)
217 #endif /* ndef _WIN32 */
229 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
233 fcntl(fd, F_SETFL, flags);
235 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
237 /* wait for connection to complete */
244 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
254 /*********************************************************************
256 * Function : write_socket
258 * Description : Write the contents of buf (for n bytes) to socket fd.
261 * 1 : fd = file descriptor (aka. handle) of socket to write to.
262 * 2 : buf = pointer to data to be written.
263 * 3 : len = length of data to be written to the socket "fd".
265 * Returns : Win32 & Unix: If no error occurs, returns the total number of
266 * bytes sent, which can be less than the number
267 * indicated by len. Otherwise, returns (-1).
269 *********************************************************************/
270 int write_socket(int fd, const char *buf, int len)
277 log_error(LOG_LEVEL_LOG, "%N", len, buf);
279 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
280 return( send(fd, buf, len, 0));
282 return( write(fd, buf, len));
288 /*********************************************************************
290 * Function : read_socket
292 * Description : Read from a TCP/IP socket in a platform independent way.
295 * 1 : fd = file descriptor of the socket to read
296 * 2 : buf = pointer to buffer where data will be written
297 * Must be >= len bytes long.
298 * 3 : len = maximum number of bytes to read
300 * Returns : On success, the number of bytes read is returned (zero
301 * indicates end of file), and the file position is advanced
302 * by this number. It is not an error if this number is
303 * smaller than the number of bytes requested; this may hap-
304 * pen for example because fewer bytes are actually available
305 * right now (maybe because we were close to end-of-file, or
306 * because we are reading from a pipe, or from a terminal),
307 * or because read() was interrupted by a signal. On error,
308 * -1 is returned, and errno is set appropriately. In this
309 * case it is left unspecified whether the file position (if
312 *********************************************************************/
313 int read_socket(int fd, char *buf, int len)
320 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
321 return( recv(fd, buf, len, 0));
323 return( read(fd, buf, len));
328 /*********************************************************************
330 * Function : close_socket
332 * Description : Closes a TCP/IP socket
335 * 1 : fd = file descriptor of socket to be closed
339 *********************************************************************/
340 void close_socket(int fd)
342 #if defined(_WIN32) || defined(__BEOS__)
353 /*********************************************************************
355 * Function : bind_port
357 * Description : Call socket, set socket options, and listen.
358 * Called by listen_loop to "boot up" our proxy address.
361 * 1 : hostnam = TCP/IP address to bind/listen to
362 * 2 : portnum = port to listen on
364 * Returns : if success, return file descriptor
365 * if failure, returns -2 if address is in use, otherwise -1
367 *********************************************************************/
368 int bind_port(const char *hostnam, int portnum)
370 struct sockaddr_in inaddr;
374 memset((char *)&inaddr, '\0', sizeof inaddr);
376 inaddr.sin_family = AF_INET;
377 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
379 if (sizeof(inaddr.sin_port) == sizeof(short))
381 inaddr.sin_port = htons((short)portnum);
385 inaddr.sin_port = htonl(portnum);
388 fd = socket(AF_INET, SOCK_STREAM, 0);
397 * FIXME: This is not needed for Win32 - in fact, it stops
398 * duplicate instances of JunkBuster from being caught.
399 * Is this really needed under UNIX, or should it be taked out?
402 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
403 #endif /* ndef _WIN32 */
405 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
409 if (errno == WSAEADDRINUSE)
411 if (errno == EADDRINUSE)
422 while (listen(fd, 5) == -1)
435 /*********************************************************************
437 * Function : accept_connection
439 * Description : Accepts a connection on a socket. Socket must have
440 * been created using bind_port().
443 * 1 : csp = Client state, cfd, ip_addr_str, and
444 * ip_addr_long will be set by this routine.
445 * 2 : fd = file descriptor returned from bind_port
447 * Returns : when a connection is accepted, it returns 1 (TRUE).
448 * On an error it returns 0 (FALSE).
450 *********************************************************************/
451 int accept_connection(struct client_state * csp, int fd)
453 struct sockaddr_in client, server;
454 struct hostent *host = NULL;
455 int afd, c_length, s_length;
457 c_length = s_length = sizeof(client);
461 afd = accept (fd, (struct sockaddr *) &client, &c_length);
462 } while (afd < 1 && errno == EINTR);
470 * Determine the IP-Adress that the client used to reach us
471 * and the hostname associated with that address
473 if (!getsockname(afd, (struct sockaddr *) &server, &s_length))
475 csp->my_ip_addr_str = strdup(inet_ntoa(server.sin_addr));
477 host = gethostbyaddr((const char *)&server.sin_addr,
478 sizeof(server.sin_addr), AF_INET);
481 log_error(LOG_LEVEL_ERROR, "Unable to get my own hostname: %E\n");
485 csp->my_hostname = strdup(host->h_name);
490 csp->ip_addr_str = strdup(inet_ntoa(client.sin_addr));
491 csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
498 /*********************************************************************
500 * Function : resolve_hostname_to_ip
502 * Description : Resolve a hostname to an internet tcp/ip address.
503 * NULL or an empty string resolve to INADDR_ANY.
506 * 1 : host = hostname to resolve
508 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
510 *********************************************************************/
511 int resolve_hostname_to_ip(const char *host)
513 struct sockaddr_in inaddr;
514 struct hostent *hostp;
516 if ((host == NULL) || (*host == '\0'))
521 memset((char *) &inaddr, 0, sizeof inaddr);
523 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
525 if ((hostp = gethostbyname(host)) == NULL)
530 if (hostp->h_addrtype != AF_INET)
533 errno = WSAEPROTOTYPE;
540 (char *) &inaddr.sin_addr,
541 (char *) hostp->h_addr,
542 sizeof(inaddr.sin_addr)
545 return(inaddr.sin_addr.s_addr);