1 const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.1.1.1 2001/05/15 13:58:54 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.1.1.1 2001/05/15 13:58:54 oes
39 * Initial import of version 2.9.3 source tree
42 *********************************************************************/
52 #include <sys/types.h>
57 #include <sys/timeb.h>
64 #include <netinet/in.h>
65 #include <sys/ioctl.h>
67 #include <sys/socket.h>
70 #include <netinet/tcp.h>
71 #include <arpa/inet.h>
79 #include "jbsockets.h"
82 const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
85 /*********************************************************************
87 * Function : connect_to
89 * Description : Open a socket and connect to it. Will check
90 * that this is allowed according to ACL.
93 * 1 : host = hostname to connect to
94 * 2 : portnum = port to connent on
95 * 3 : csp = Current client state (buffers, headers, etc...)
96 * Not modified, only used for source IP and ACL.
98 * Returns : -1 => failure, else it is the socket file descriptor.
100 *********************************************************************/
101 int connect_to(char *host, int portnum, struct client_state *csp)
103 struct sockaddr_in inaddr;
106 struct timeval tv[1];
107 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
109 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
112 struct access_control_addr src[1], dst[1];
113 #endif /* def ACL_FILES */
115 memset((char *)&inaddr, 0, sizeof inaddr);
117 if ((addr = resolve_hostname_to_ip(host)) == -1)
123 src->addr = csp->ip_addr_long;
126 dst->addr = ntohl(addr);
129 if (block_acl(src, dst, csp))
134 #endif /* def ACL_FILES */
136 inaddr.sin_addr.s_addr = addr;
137 inaddr.sin_family = AF_INET;
139 if (sizeof(inaddr.sin_port) == sizeof(short))
141 inaddr.sin_port = htons((short)portnum);
145 inaddr.sin_port = htonl(portnum);
148 if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
154 { /* turn off TCP coalescence */
156 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
158 #endif /* def TCP_NODELAY */
160 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
161 if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
164 fcntl(fd, F_SETFL, flags);
166 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
168 while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1)
171 if (errno == WSAEINPROGRESS)
172 #else /* ifndef _WIN32 */
173 if (errno == EINPROGRESS)
174 #endif /* ndef _WIN32 */
186 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
190 fcntl(fd, F_SETFL, flags);
192 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
194 /* wait for connection to complete */
201 if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0)
211 /*********************************************************************
213 * Function : write_socket
215 * Description : Write the contents of buf (for n bytes) to socket fd.
218 * 1 : fd = file descriptor (aka. handle) of socket to write to.
219 * 2 : buf = pointer to data to be written.
220 * 3 : len = length of data to be written to the socket "fd".
222 * Returns : Win32: If no error occurs, returns the total number of
223 * bytes sent, which can be less than the number
224 * indicated by len. Otherwise, returns (-1).
225 * Unix: ??? (Please fill me in!)
227 *********************************************************************/
228 int write_socket(int fd, const char *buf, int len)
230 if (len <= 0) return(0);
232 /* if (DEBUG(LOG)) fwrite(buf, n, 1, logfp); */
234 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
235 return( send(fd, buf, len, 0));
237 return( write(fd, buf, len));
243 /*********************************************************************
245 * Function : read_socket
247 * Description : Read from a TCP/IP socket in a platform independent way.
250 * 1 : fd = file descriptor of the socket to read
251 * 2 : buf = pointer to buffer where data will be written
252 * Must be >= len bytes long.
253 * 3 : len = maximum number of bytes to read
255 * Returns : On success, the number of bytes read is returned (zero
256 * indicates end of file), and the file position is advanced
257 * by this number. It is not an error if this number is
258 * smaller than the number of bytes requested; this may hap-
259 * pen for example because fewer bytes are actually available
260 * right now (maybe because we were close to end-of-file, or
261 * because we are reading from a pipe, or from a terminal),
262 * or because read() was interrupted by a signal. On error,
263 * -1 is returned, and errno is set appropriately. In this
264 * case it is left unspecified whether the file position (if
267 *********************************************************************/
268 int read_socket(int fd, char *buf, int len)
275 #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA)
276 return( recv(fd, buf, len, 0));
278 return( read(fd, buf, len));
283 /*********************************************************************
285 * Function : close_socket
287 * Description : Closes a TCP/IP socket
290 * 1 : fd = file descriptor of socket to be closed
294 *********************************************************************/
295 void close_socket(int fd)
297 #if defined(_WIN32) || defined(__BEOS__)
308 /*********************************************************************
310 * Function : bind_port
312 * Description : Call socket, set socket options, and listen.
313 * Called by listen_loop to "boot up" our proxy address.
316 * 1 : hostnam = TCP/IP address to bind/listen to
317 * 2 : portnum = port to listen on
319 * Returns : if success, return file descriptor
320 * if failure, returns -2 if address is in use, otherwise -1
322 *********************************************************************/
323 int bind_port(const char *hostnam, int portnum)
325 struct sockaddr_in inaddr;
329 memset((char *)&inaddr, '\0', sizeof inaddr);
331 inaddr.sin_family = AF_INET;
332 inaddr.sin_addr.s_addr = resolve_hostname_to_ip(hostnam);
334 if (sizeof(inaddr.sin_port) == sizeof(short))
336 inaddr.sin_port = htons((short)portnum);
340 inaddr.sin_port = htonl(portnum);
343 fd = socket(AF_INET, SOCK_STREAM, 0);
350 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
352 if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0)
356 if (errno == WSAEADDRINUSE)
358 if (errno == EADDRINUSE)
369 while (listen(fd, 5) == -1)
382 /*********************************************************************
384 * Function : accept_connection
386 * Description : Accepts a connection on a socket. Socket must have
387 * been created using bind_port().
390 * 1 : csp = Client state, cfd, ip_addr_str, and
391 * ip_addr_long will be set by this routine.
392 * 2 : fd = file descriptor returned from bind_port
394 * Returns : when a connection is accepted, it returns 1 (TRUE).
395 * On an error it returns 0 (FALSE).
397 *********************************************************************/
398 int accept_connection(struct client_state * csp, int fd)
400 struct sockaddr raddr;
401 struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
404 raddrlen = sizeof raddr;
407 afd = accept (fd, &raddr, &raddrlen);
408 } while (afd < 1 && errno == EINTR);
416 csp->ip_addr_str = strdup(inet_ntoa(rap->sin_addr));
417 csp->ip_addr_long = ntohl(rap->sin_addr.s_addr);
423 /*********************************************************************
425 * Function : resolve_hostname_to_ip
427 * Description : Resolve a hostname to an internet tcp/ip address.
428 * NULL or an empty string resolve to INADDR_ANY.
431 * 1 : host = hostname to resolve
433 * Returns : -1 => failure, INADDR_ANY or tcp/ip address if succesful.
435 *********************************************************************/
436 int resolve_hostname_to_ip(const char *host)
438 struct sockaddr_in inaddr;
439 struct hostent *hostp;
441 if ((host == NULL) || (*host == '\0'))
446 memset((char *) &inaddr, 0, sizeof inaddr);
448 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
450 if ((hostp = gethostbyname(host)) == NULL)
455 if (hostp->h_addrtype != AF_INET)
458 errno = WSAEPROTOTYPE;
465 (char *) &inaddr.sin_addr,
466 (char *) hostp->h_addr,
467 sizeof(inaddr.sin_addr)
470 return(inaddr.sin_addr.s_addr);