1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
4 * File : $Source: /home/administrator/cvs/ijb/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 $
39 *********************************************************************/
49 #include <sys/types.h>
54 #include <sys/timeb.h>
61 #include <netinet/in.h>
62 #include <sys/ioctl.h>
64 #include <sys/socket.h>
67 #include <netinet/tcp.h>
68 #include <arpa/inet.h>
76 #include "jbsockets.h"
79 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
82 /*********************************************************************
84 * Function : connect_to
86 * Description : Open a socket and connect to it. Will check
87 * that this is allowed according to ACL.
90 * 1 : host = hostname to connect to
91 * 2 : portnum = port to connent on
92 * 3 : csp = Current client state (buffers, headers, etc...)
93 * Not modified, only used for source IP and ACL.
95 * Returns : -1 => failure, else it is the socket file descriptor.
97 *********************************************************************/
98 int connect_to(char *host, int portnum, struct client_state *csp)
100 struct sockaddr_in inaddr;
103 struct timeval tv[1];
104 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
106 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
109 struct access_control_addr src[1], dst[1];
110 #endif /* def ACL_FILES */
112 memset((char *)&inaddr, 0, sizeof inaddr);
114 if ((addr = resolve_hostname_to_ip(host)) == -1)
120 src->addr = csp->ip_addr_long;
123 dst->addr = ntohl(addr);
126 if (block_acl(src, dst, csp))
131 #endif /* def ACL_FILES */
133 inaddr.sin_addr.s_addr = addr;
134 inaddr.sin_family = AF_INET;
136 if (sizeof(inaddr.sin_port) == sizeof(short))
138 inaddr.sin_port = htons((short)portnum);
142 inaddr.sin_port = htonl(portnum);
145 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
151 { /* turn off TCP coalescence */
153 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
155 #endif /* def TCP_NODELAY */
157 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
\r
158 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
161 fcntl(fd, F_SETFL, flags);
163 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
165 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
168 if (errno == WSAEINPROGRESS)
169 #else /* ifndef _WIN32 */
170 if (errno == EINPROGRESS)
171 #endif /* ndef _WIN32 */
183 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
187 fcntl(fd, F_SETFL, flags);
189 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
191 /* wait for connection to complete */
198 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
208 /*********************************************************************
210 * Function : write_socket
212 * Description : Write the contents of buf (for n bytes) to socket fd.
215 * 1 : fd = file descriptor (aka. handle) of socket to write to.
216 * 2 : buf = pointer to data to be written.
217 * 3 : len = length of data to be written to the socket "fd".
219 * Returns : Win32: If no error occurs, returns the total number of
220 * bytes sent, which can be less than the number
221 * indicated by len. Otherwise, returns (-1).
222 * Unix: ??? (Please fill me in!)
224 *********************************************************************/
225 int write_socket(int fd, const char *buf, int len)
227 if (len <= 0) return(0);
229 /* if (DEBUG(LOG)) fwrite(buf, n, 1, logfp); */
231 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
232 return( send(fd, buf, len, 0));
234 return( write(fd, buf, len));
240 /*********************************************************************
242 * Function : read_socket
244 * Description : Read from a TCP/IP socket in a platform independent way.
247 * 1 : fd = file descriptor of the socket to read
248 * 2 : buf = pointer to buffer where data will be written
249 * Must be >= len bytes long.
250 * 3 : len = maximum number of bytes to read
252 * Returns : On success, the number of bytes read is returned (zero
253 * indicates end of file), and the file position is advanced
254 * by this number. It is not an error if this number is
255 * smaller than the number of bytes requested; this may hap-
256 * pen for example because fewer bytes are actually available
257 * right now (maybe because we were close to end-of-file, or
258 * because we are reading from a pipe, or from a terminal),
259 * or because read() was interrupted by a signal. On error,
260 * -1 is returned, and errno is set appropriately. In this
261 * case it is left unspecified whether the file position (if
264 *********************************************************************/
265 int read_socket(int fd, char *buf, int len)
272 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
273 return( recv(fd, buf, len, 0));
275 return( read(fd, buf, len));
280 /*********************************************************************
282 * Function : close_socket
284 * Description : Closes a TCP/IP socket
287 * 1 : fd = file descriptor of socket to be closed
291 *********************************************************************/
292 void close_socket(int fd)
294 #if defined(_WIN32) || defined(__BEOS__)
296 #elif defined(AMIGA)
\r
305 /*********************************************************************
307 * Function : bind_port
309 * Description : Call socket, set socket options, and listen.
310 * Called by listen_loop to "boot up" our proxy address.
313 * 1 : hostnam = TCP/IP address to bind/listen to
314 * 2 : portnum = port to listen on
316 * Returns : if success, return file descriptor
317 * if failure, returns -2 if address is in use, otherwise -1
319 *********************************************************************/
320 int bind_port(const char *hostnam, int portnum)
322 struct sockaddr_in inaddr;
326 memset((char *)&inaddr, '\0', sizeof inaddr);
328 inaddr.sin_family = AF_INET;
329 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
331 if (sizeof(inaddr.sin_port) == sizeof(short))
333 inaddr.sin_port = htons((short)portnum);
337 inaddr.sin_port = htonl(portnum);
340 fd = socket(AF_INET, SOCK_STREAM, 0);
347 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
349 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
353 if (errno == WSAEADDRINUSE)
355 if (errno == EADDRINUSE)
366 while (listen(fd, 5) == -1)
379 /*********************************************************************
381 * Function : accept_connection
383 * Description : Accepts a connection on a socket. Socket must have
384 * been created using bind_port().
387 * 1 : csp = Client state, cfd, ip_addr_str, and
388 * ip_addr_long will be set by this routine.
389 * 2 : fd = file descriptor returned from bind_port
391 * Returns : when a connection is accepted, it returns 1 (TRUE).
392 * On an error it returns 0 (FALSE).
394 *********************************************************************/
395 int accept_connection(struct client_state * csp, int fd)
397 struct sockaddr raddr;
398 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
401 raddrlen = sizeof raddr;
404 afd = accept (fd, &raddr, &raddrlen);
405 } while (afd < 1 && errno == EINTR);
413 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
414 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
420 /*********************************************************************
422 * Function : resolve_hostname_to_ip
424 * Description : Resolve a hostname to an internet tcp/ip address.
425 * NULL or an empty string resolve to INADDR_ANY.
428 * 1 : host = hostname to resolve
430 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
432 *********************************************************************/
433 int resolve_hostname_to_ip(const char *host)
435 struct sockaddr_in inaddr;
436 struct hostent *hostp;
438 if ((host == NULL) || (*host == '\0'))
443 memset((char *) &inaddr, 0, sizeof inaddr);
445 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
447 if ((hostp = gethostbyname(host)) == NULL)
452 if (hostp->h_addrtype != AF_INET)
455 errno = WSAEPROTOTYPE;
462 (char *) &inaddr.sin_addr,
463 (char *) hostp->h_addr,
464 sizeof(inaddr.sin_addr)
467 return(inaddr.sin_addr.s_addr);