2005-04-18 08:57:44 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-01-26 09:44:16 +01:00
|
|
|
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-04-18 08:57:44 +02:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-10 17:32:06 +02:00
|
|
|
#include "server_setup.h"
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
/* Purpose
|
|
|
|
*
|
2005-05-01 01:30:55 +02:00
|
|
|
* 1. Accept a TCP connection on a custom port (ipv4 or ipv6), or connect
|
|
|
|
* to a given (localhost) port.
|
2005-04-18 08:57:44 +02:00
|
|
|
*
|
|
|
|
* 2. Get commands on STDIN. Pass data on to the TCP stream.
|
|
|
|
* Get data from TCP stream and pass on to STDOUT.
|
|
|
|
*
|
|
|
|
* This program is made to perform all the socket/stream/connection stuff for
|
|
|
|
* the test suite's (perl) FTP server. Previously the perl code did all of
|
|
|
|
* this by its own, but I decided to let this program do the socket layer
|
|
|
|
* because of several things:
|
|
|
|
*
|
|
|
|
* o We want the perl code to work with rather old perl installations, thus
|
|
|
|
* we cannot use recent perl modules or features.
|
|
|
|
*
|
|
|
|
* o We want IPv6 support for systems that provide it, and doing optional IPv6
|
|
|
|
* support in perl seems if not impossible so at least awkward.
|
|
|
|
*
|
|
|
|
* o We want FTP-SSL support, which means that a connection that starts with
|
|
|
|
* plain sockets needs to be able to "go SSL" in the midst. This would also
|
|
|
|
* require some nasty perl stuff I'd rather avoid.
|
|
|
|
*
|
|
|
|
* (Source originally based on sws.c)
|
|
|
|
*/
|
2008-02-28 11:15:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal handling notes for sockfilt
|
|
|
|
* ----------------------------------
|
|
|
|
*
|
|
|
|
* This program is a single-threaded process.
|
|
|
|
*
|
|
|
|
* This program is intended to be highly portable and as such it must be kept as
|
|
|
|
* simple as possible, due to this the only signal handling mechanisms used will
|
|
|
|
* be those of ANSI C, and used only in the most basic form which is good enough
|
|
|
|
* for the purpose of this program.
|
|
|
|
*
|
|
|
|
* For the above reason and the specific needs of this program signals SIGHUP,
|
|
|
|
* SIGPIPE and SIGALRM will be simply ignored on systems where this can be done.
|
|
|
|
* If possible, signals SIGINT and SIGTERM will be handled by this program as an
|
|
|
|
* indication to cleanup and finish execution as soon as possible. This will be
|
|
|
|
* achieved with a single signal handler 'exit_signal_handler' for both signals.
|
|
|
|
*
|
|
|
|
* The 'exit_signal_handler' upon the first SIGINT or SIGTERM received signal
|
|
|
|
* will just set to one the global var 'got_exit_signal' storing in global var
|
|
|
|
* 'exit_signal' the signal that triggered this change.
|
|
|
|
*
|
|
|
|
* Nothing fancy that could introduce problems is used, the program at certain
|
|
|
|
* points in its normal flow checks if var 'got_exit_signal' is set and in case
|
|
|
|
* this is true it just makes its way out of loops and functions in structured
|
|
|
|
* and well behaved manner to achieve proper program cleanup and termination.
|
|
|
|
*
|
|
|
|
* Even with the above mechanism implemented it is worthwile to note that other
|
|
|
|
* signals might still be received, or that there might be systems on which it
|
|
|
|
* is not possible to trap and ignore some of the above signals. This implies
|
|
|
|
* that for increased portability and reliability the program must be coded as
|
|
|
|
* if no signal was being ignored or handled at all. Enjoy it!
|
|
|
|
*/
|
|
|
|
|
2008-02-28 01:55:06 +01:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
2005-04-18 08:57:44 +02:00
|
|
|
#include <signal.h>
|
2008-02-28 01:55:06 +01:00
|
|
|
#endif
|
2005-04-18 08:57:44 +02:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2009-04-28 01:59:41 +02:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
2005-04-18 08:57:44 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2013-01-07 07:34:32 +01:00
|
|
|
|
2005-05-01 14:56:09 +02:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* make the curlx header define all printf() functions to use the curlx_*
|
|
|
|
versions instead */
|
2005-04-18 08:57:44 +02:00
|
|
|
#include "curlx.h" /* from the private lib dir */
|
|
|
|
#include "getpart.h"
|
2013-01-04 02:50:28 +01:00
|
|
|
#include "inet_pton.h"
|
2005-05-01 01:30:55 +02:00
|
|
|
#include "util.h"
|
2010-11-19 19:20:38 +01:00
|
|
|
#include "server_sockaddr.h"
|
2013-01-09 13:03:53 +01:00
|
|
|
#include "warnless.h"
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2013-01-04 02:50:28 +01:00
|
|
|
/* include memdebug.h last */
|
|
|
|
#include "memdebug.h"
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2013-01-09 15:10:23 +01:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
#undef EINTR
|
|
|
|
#define EINTR 4 /* errno.h value */
|
|
|
|
#undef EAGAIN
|
|
|
|
#define EAGAIN 11 /* errno.h value */
|
|
|
|
#undef ENOMEM
|
|
|
|
#define ENOMEM 12 /* errno.h value */
|
|
|
|
#undef EINVAL
|
|
|
|
#define EINVAL 22 /* errno.h value */
|
|
|
|
#endif
|
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
#define DEFAULT_PORT 8999
|
|
|
|
|
|
|
|
#ifndef DEFAULT_LOGFILE
|
|
|
|
#define DEFAULT_LOGFILE "log/sockfilt.log"
|
|
|
|
#endif
|
|
|
|
|
2008-09-04 07:29:10 +02:00
|
|
|
const char *serverlogfile = DEFAULT_LOGFILE;
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-09-20 14:33:02 +02:00
|
|
|
static bool verbose = FALSE;
|
2011-10-31 07:29:13 +01:00
|
|
|
static bool bind_only = FALSE;
|
2008-09-26 13:21:22 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
2008-09-20 14:33:02 +02:00
|
|
|
static bool use_ipv6 = FALSE;
|
2008-09-26 13:21:22 +02:00
|
|
|
#endif
|
|
|
|
static const char *ipv_inuse = "IPv4";
|
2008-09-20 14:33:02 +02:00
|
|
|
static unsigned short port = DEFAULT_PORT;
|
|
|
|
static unsigned short connectport = 0; /* if non-zero, we activate this mode */
|
2008-02-26 16:06:44 +01:00
|
|
|
|
|
|
|
enum sockmode {
|
|
|
|
PASSIVE_LISTEN, /* as a server waiting for connections */
|
|
|
|
PASSIVE_CONNECT, /* as a server, connected to a client */
|
|
|
|
ACTIVE, /* as a client, connected to a server */
|
|
|
|
ACTIVE_DISCONNECT /* as a client, disconnected from server */
|
|
|
|
};
|
|
|
|
|
2008-02-28 11:15:21 +01:00
|
|
|
/* do-nothing macro replacement for systems which lack siginterrupt() */
|
|
|
|
|
|
|
|
#ifndef HAVE_SIGINTERRUPT
|
|
|
|
#define siginterrupt(x,y) do {} while(0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vars used to keep around previous signal handlers */
|
|
|
|
|
|
|
|
typedef RETSIGTYPE (*SIGHANDLER_T)(int);
|
|
|
|
|
2009-04-08 20:31:54 +02:00
|
|
|
#ifdef SIGHUP
|
2008-02-28 11:15:21 +01:00
|
|
|
static SIGHANDLER_T old_sighup_handler = SIG_ERR;
|
2009-04-08 20:31:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
2008-02-28 11:15:21 +01:00
|
|
|
static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
|
2009-04-08 20:31:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGALRM
|
2008-02-28 11:15:21 +01:00
|
|
|
static SIGHANDLER_T old_sigalrm_handler = SIG_ERR;
|
2009-04-08 20:31:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGINT
|
2008-02-28 11:15:21 +01:00
|
|
|
static SIGHANDLER_T old_sigint_handler = SIG_ERR;
|
2009-04-08 20:31:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGTERM
|
2008-02-28 11:15:21 +01:00
|
|
|
static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
|
2009-04-08 20:31:54 +02:00
|
|
|
#endif
|
2008-02-28 11:15:21 +01:00
|
|
|
|
2013-01-09 22:19:03 +01:00
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
static SIGHANDLER_T old_sigbreak_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
2008-02-28 11:15:21 +01:00
|
|
|
/* var which if set indicates that the program should finish execution */
|
|
|
|
|
|
|
|
SIG_ATOMIC_T got_exit_signal = 0;
|
|
|
|
|
|
|
|
/* if next is set indicates the first signal handled in exit_signal_handler */
|
|
|
|
|
|
|
|
static volatile int exit_signal = 0;
|
|
|
|
|
|
|
|
/* signal handler that will be triggered to indicate that the program
|
|
|
|
should finish its execution in a controlled manner as soon as possible.
|
|
|
|
The first time this is called it will set got_exit_signal to one and
|
|
|
|
store in exit_signal the signal that triggered its execution. */
|
|
|
|
|
|
|
|
static RETSIGTYPE exit_signal_handler(int signum)
|
|
|
|
{
|
2013-01-09 15:10:23 +01:00
|
|
|
int old_errno = errno;
|
2008-02-28 11:15:21 +01:00
|
|
|
if(got_exit_signal == 0) {
|
|
|
|
got_exit_signal = 1;
|
|
|
|
exit_signal = signum;
|
|
|
|
}
|
|
|
|
(void)signal(signum, exit_signal_handler);
|
2013-01-09 15:10:23 +01:00
|
|
|
errno = old_errno;
|
2008-02-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void install_signal_handlers(void)
|
|
|
|
{
|
|
|
|
#ifdef SIGHUP
|
|
|
|
/* ignore SIGHUP signal */
|
|
|
|
if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
|
2013-01-09 15:10:23 +01:00
|
|
|
logmsg("cannot install SIGHUP handler: %s", strerror(errno));
|
2008-02-28 11:15:21 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
/* ignore SIGPIPE signal */
|
|
|
|
if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
|
2013-01-09 15:10:23 +01:00
|
|
|
logmsg("cannot install SIGPIPE handler: %s", strerror(errno));
|
2008-02-28 11:15:21 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
/* ignore SIGALRM signal */
|
|
|
|
if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
|
2013-01-09 15:10:23 +01:00
|
|
|
logmsg("cannot install SIGALRM handler: %s", strerror(errno));
|
2008-02-28 11:15:21 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
/* handle SIGINT signal with our exit_signal_handler */
|
|
|
|
if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
|
2013-01-09 15:10:23 +01:00
|
|
|
logmsg("cannot install SIGINT handler: %s", strerror(errno));
|
2008-02-28 11:15:21 +01:00
|
|
|
else
|
|
|
|
siginterrupt(SIGINT, 1);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
/* handle SIGTERM signal with our exit_signal_handler */
|
|
|
|
if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
|
2013-01-09 15:10:23 +01:00
|
|
|
logmsg("cannot install SIGTERM handler: %s", strerror(errno));
|
2008-02-28 11:15:21 +01:00
|
|
|
else
|
|
|
|
siginterrupt(SIGTERM, 1);
|
|
|
|
#endif
|
2013-01-09 22:19:03 +01:00
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
/* handle SIGBREAK signal with our exit_signal_handler */
|
|
|
|
if((old_sigbreak_handler = signal(SIGBREAK, exit_signal_handler)) == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGBREAK handler: %s", strerror(errno));
|
|
|
|
else
|
|
|
|
siginterrupt(SIGBREAK, 1);
|
|
|
|
#endif
|
2008-02-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void restore_signal_handlers(void)
|
|
|
|
{
|
|
|
|
#ifdef SIGHUP
|
|
|
|
if(SIG_ERR != old_sighup_handler)
|
|
|
|
(void)signal(SIGHUP, old_sighup_handler);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
if(SIG_ERR != old_sigpipe_handler)
|
|
|
|
(void)signal(SIGPIPE, old_sigpipe_handler);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
if(SIG_ERR != old_sigalrm_handler)
|
|
|
|
(void)signal(SIGALRM, old_sigalrm_handler);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
if(SIG_ERR != old_sigint_handler)
|
|
|
|
(void)signal(SIGINT, old_sigint_handler);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
if(SIG_ERR != old_sigterm_handler)
|
|
|
|
(void)signal(SIGTERM, old_sigterm_handler);
|
|
|
|
#endif
|
2013-01-09 22:19:03 +01:00
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
if(SIG_ERR != old_sigbreak_handler)
|
|
|
|
(void)signal(SIGBREAK, old_sigbreak_handler);
|
|
|
|
#endif
|
2008-02-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 22:50:01 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
/*
|
|
|
|
* read-wrapper to support reading from stdin on Windows.
|
|
|
|
*/
|
|
|
|
static ssize_t read_wincon(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
HANDLE handle = NULL;
|
|
|
|
DWORD mode, rcount = 0;
|
|
|
|
BOOL success;
|
|
|
|
|
|
|
|
if(fd == fileno(stdin)) {
|
|
|
|
handle = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return read(fd, buf, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(GetConsoleMode(handle, &mode)) {
|
|
|
|
success = ReadConsole(handle, buf, count, &rcount, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
success = ReadFile(handle, buf, count, &rcount, NULL);
|
|
|
|
}
|
|
|
|
if(success) {
|
|
|
|
return rcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = GetLastError();
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-16 11:52:00 +01:00
|
|
|
#undef read
|
2013-04-04 22:50:01 +02:00
|
|
|
#define read(a,b,c) read_wincon(a,b,c)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write-wrapper to support writing to stdout and stderr on Windows.
|
|
|
|
*/
|
|
|
|
static ssize_t write_wincon(int fd, const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
HANDLE handle = NULL;
|
|
|
|
DWORD mode, wcount = 0;
|
|
|
|
BOOL success;
|
|
|
|
|
|
|
|
if(fd == fileno(stdout)) {
|
|
|
|
handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
}
|
|
|
|
else if(fd == fileno(stderr)) {
|
|
|
|
handle = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return write(fd, buf, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(GetConsoleMode(handle, &mode)) {
|
|
|
|
success = WriteConsole(handle, buf, count, &wcount, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
success = WriteFile(handle, buf, count, &wcount, NULL);
|
|
|
|
}
|
|
|
|
if(success) {
|
|
|
|
return wcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = GetLastError();
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-16 11:52:00 +01:00
|
|
|
#undef write
|
2013-04-04 22:50:01 +02:00
|
|
|
#define write(a,b,c) write_wincon(a,b,c)
|
|
|
|
#endif
|
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
/*
|
|
|
|
* fullread is a wrapper around the read() function. This will repeat the call
|
|
|
|
* to read() until it actually has read the complete number of bytes indicated
|
|
|
|
* in nbytes or it fails with a condition that cannot be handled with a simple
|
|
|
|
* retry of the read call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t fullread(int filedes, void *buffer, size_t nbytes)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
ssize_t rc;
|
|
|
|
ssize_t nread = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
rc = read(filedes, (unsigned char *)buffer + nread, nbytes - nread);
|
|
|
|
|
2008-02-28 11:15:21 +01:00
|
|
|
if(got_exit_signal) {
|
|
|
|
logmsg("signalled to die");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
if(rc < 0) {
|
2013-01-09 15:10:23 +01:00
|
|
|
error = errno;
|
2008-02-26 19:13:59 +01:00
|
|
|
if((error == EINTR) || (error == EAGAIN))
|
|
|
|
continue;
|
2013-01-11 00:03:37 +01:00
|
|
|
logmsg("reading from file descriptor: %d,", filedes);
|
2013-01-06 22:23:32 +01:00
|
|
|
logmsg("unrecoverable read() failure: (%d) %s",
|
|
|
|
error, strerror(error));
|
2008-02-26 19:13:59 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rc == 0) {
|
|
|
|
logmsg("got 0 reading from stdin");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nread += rc;
|
|
|
|
|
|
|
|
} while((size_t)nread < nbytes);
|
|
|
|
|
|
|
|
if(verbose)
|
2008-09-04 07:29:10 +02:00
|
|
|
logmsg("read %zd bytes", nread);
|
2008-02-26 19:13:59 +01:00
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fullwrite is a wrapper around the write() function. This will repeat the
|
|
|
|
* call to write() until it actually has written the complete number of bytes
|
|
|
|
* indicated in nbytes or it fails with a condition that cannot be handled
|
|
|
|
* with a simple retry of the write call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t fullwrite(int filedes, const void *buffer, size_t nbytes)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
ssize_t wc;
|
|
|
|
ssize_t nwrite = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
wc = write(filedes, (unsigned char *)buffer + nwrite, nbytes - nwrite);
|
|
|
|
|
2008-02-28 11:15:21 +01:00
|
|
|
if(got_exit_signal) {
|
|
|
|
logmsg("signalled to die");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
if(wc < 0) {
|
2013-01-09 15:10:23 +01:00
|
|
|
error = errno;
|
2008-02-26 19:13:59 +01:00
|
|
|
if((error == EINTR) || (error == EAGAIN))
|
|
|
|
continue;
|
2013-01-11 00:03:37 +01:00
|
|
|
logmsg("writing to file descriptor: %d,", filedes);
|
2013-01-06 22:23:32 +01:00
|
|
|
logmsg("unrecoverable write() failure: (%d) %s",
|
|
|
|
error, strerror(error));
|
2008-02-26 19:13:59 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wc == 0) {
|
|
|
|
logmsg("put 0 writing to stdout");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nwrite += wc;
|
|
|
|
|
|
|
|
} while((size_t)nwrite < nbytes);
|
|
|
|
|
|
|
|
if(verbose)
|
2008-09-04 07:29:10 +02:00
|
|
|
logmsg("wrote %zd bytes", nwrite);
|
2008-02-26 19:13:59 +01:00
|
|
|
|
|
|
|
return nwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read_stdin tries to read from stdin nbytes into the given buffer. This is a
|
|
|
|
* blocking function that will only return TRUE when nbytes have actually been
|
|
|
|
* read or FALSE when an unrecoverable error has been detected. Failure of this
|
2008-02-27 15:54:18 +01:00
|
|
|
* function is an indication that the sockfilt process should terminate.
|
2008-02-26 19:13:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static bool read_stdin(void *buffer, size_t nbytes)
|
|
|
|
{
|
|
|
|
ssize_t nread = fullread(fileno(stdin), buffer, nbytes);
|
|
|
|
if(nread != (ssize_t)nbytes) {
|
|
|
|
logmsg("exiting...");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write_stdout tries to write to stdio nbytes from the given buffer. This is a
|
|
|
|
* blocking function that will only return TRUE when nbytes have actually been
|
|
|
|
* written or FALSE when an unrecoverable error has been detected. Failure of
|
2008-02-27 15:54:18 +01:00
|
|
|
* this function is an indication that the sockfilt process should terminate.
|
2008-02-26 19:13:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static bool write_stdout(const void *buffer, size_t nbytes)
|
|
|
|
{
|
|
|
|
ssize_t nwrite = fullwrite(fileno(stdout), buffer, nbytes);
|
|
|
|
if(nwrite != (ssize_t)nbytes) {
|
|
|
|
logmsg("exiting...");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:50:51 +02:00
|
|
|
static void lograw(unsigned char *buffer, ssize_t len)
|
2005-04-18 08:57:44 +02:00
|
|
|
{
|
|
|
|
char data[120];
|
2006-07-13 20:50:51 +02:00
|
|
|
ssize_t i;
|
2005-04-18 08:57:44 +02:00
|
|
|
unsigned char *ptr = buffer;
|
|
|
|
char *optr = data;
|
2006-07-13 20:50:51 +02:00
|
|
|
ssize_t width=0;
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
for(i=0; i<len; i++) {
|
2005-05-25 14:04:24 +02:00
|
|
|
switch(ptr[i]) {
|
|
|
|
case '\n':
|
|
|
|
sprintf(optr, "\\n");
|
|
|
|
width += 2;
|
|
|
|
optr += 2;
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
sprintf(optr, "\\r");
|
|
|
|
width += 2;
|
|
|
|
optr += 2;
|
|
|
|
break;
|
|
|
|
default:
|
2006-10-23 21:14:54 +02:00
|
|
|
sprintf(optr, "%c", (ISGRAPH(ptr[i]) || ptr[i]==0x20) ?ptr[i]:'.');
|
2005-05-25 14:04:24 +02:00
|
|
|
width++;
|
|
|
|
optr++;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
if(width>60) {
|
2005-05-25 14:04:24 +02:00
|
|
|
logmsg("'%s'", data);
|
2005-04-18 08:57:44 +02:00
|
|
|
width = 0;
|
|
|
|
optr = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(width)
|
2005-05-25 14:04:24 +02:00
|
|
|
logmsg("'%s'", data);
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 19:50:50 +01:00
|
|
|
#ifdef USE_WINSOCK
|
2012-12-25 15:15:24 +01:00
|
|
|
/*
|
|
|
|
* WinSock select() does not support standard file descriptors,
|
|
|
|
* it can only check SOCKETs. The following function is an attempt
|
|
|
|
* to re-create a select() function with support for other handle types.
|
2013-01-08 19:50:50 +01:00
|
|
|
*
|
2012-12-25 15:15:24 +01:00
|
|
|
* select() function with support for WINSOCK2 sockets and all
|
2014-04-22 17:21:40 +02:00
|
|
|
* other handle types supported by WaitForMultipleObjectsEx() as
|
|
|
|
* well as disk files, anonymous and names pipes, and character input.
|
2012-12-25 15:15:24 +01:00
|
|
|
*
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms687028.aspx
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms741572.aspx
|
|
|
|
*/
|
2014-04-22 17:21:40 +02:00
|
|
|
struct select_ws_wait_data {
|
|
|
|
HANDLE handle; /* actual handle to wait for during select */
|
|
|
|
HANDLE event; /* internal event to abort waiting thread */
|
|
|
|
};
|
|
|
|
static DWORD WINAPI select_ws_wait_thread(LPVOID lpParameter)
|
2014-01-26 00:58:30 +01:00
|
|
|
{
|
2014-04-22 17:21:40 +02:00
|
|
|
struct select_ws_wait_data *data;
|
2014-04-20 22:15:36 +02:00
|
|
|
HANDLE handle, handles[2];
|
2014-04-20 18:26:24 +02:00
|
|
|
INPUT_RECORD inputrecord;
|
2014-04-20 22:15:36 +02:00
|
|
|
LARGE_INTEGER size, pos;
|
|
|
|
DWORD type, length;
|
|
|
|
|
2014-04-22 17:21:40 +02:00
|
|
|
/* retrieve handles from internal structure */
|
|
|
|
data = (struct select_ws_wait_data *) lpParameter;
|
|
|
|
if(data) {
|
|
|
|
handle = data->handle;
|
|
|
|
handles[0] = data->event;
|
|
|
|
handles[1] = handle;
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
2014-04-20 22:15:36 +02:00
|
|
|
|
2014-04-22 17:21:40 +02:00
|
|
|
/* retrieve the type of file to wait on */
|
|
|
|
type = GetFileType(handle);
|
2014-04-20 22:15:36 +02:00
|
|
|
switch(type) {
|
|
|
|
case FILE_TYPE_DISK:
|
2014-04-22 17:21:40 +02:00
|
|
|
/* The handle represents a file on disk, this means:
|
|
|
|
* - WaitForMultipleObjectsEx will always be signalled for it.
|
|
|
|
* - comparison of current position in file and total size of
|
|
|
|
* the file can be used to check if we reached the end yet.
|
|
|
|
*
|
|
|
|
* Approach: Loop till either the internal event is signalled
|
|
|
|
* or if the end of the file has already been reached.
|
|
|
|
*/
|
2014-04-20 22:15:36 +02:00
|
|
|
while(WaitForMultipleObjectsEx(2, handles, FALSE, INFINITE, FALSE)
|
|
|
|
== WAIT_OBJECT_0 + 1) {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* get total size of file */
|
2014-04-20 22:15:36 +02:00
|
|
|
size.QuadPart = 0;
|
|
|
|
if(GetFileSizeEx(handle, &size)) {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* get the current position within the file */
|
2014-04-20 22:15:36 +02:00
|
|
|
pos.QuadPart = 0;
|
|
|
|
if(SetFilePointerEx(handle, pos, &pos, FILE_CURRENT)) {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* compare position with size, abort if not equal */
|
|
|
|
if(size.QuadPart == pos.QuadPart) {
|
|
|
|
/* sleep and continue waiting */
|
2014-04-20 22:15:36 +02:00
|
|
|
SleepEx(100, FALSE);
|
2014-04-22 17:21:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-20 22:15:36 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-22 17:21:40 +02:00
|
|
|
/* there is some data available, stop waiting */
|
|
|
|
break;
|
2014-04-20 22:15:36 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-01-26 00:58:30 +01:00
|
|
|
|
2014-04-20 22:15:36 +02:00
|
|
|
case FILE_TYPE_CHAR:
|
2014-04-22 17:21:40 +02:00
|
|
|
/* The handle represents a character input, this means:
|
|
|
|
* - WaitForMultipleObjectsEx will be signalled on any kind of input,
|
|
|
|
* including mouse and window size events we do not care about.
|
|
|
|
*
|
|
|
|
* Approach: Loop till either the internal event is signalled
|
|
|
|
* or we get signalled for an actual key-event.
|
|
|
|
*/
|
2014-04-20 22:15:36 +02:00
|
|
|
while(WaitForMultipleObjectsEx(2, handles, FALSE, INFINITE, FALSE)
|
|
|
|
== WAIT_OBJECT_0 + 1) {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* check if this is an actual console handle */
|
2014-04-20 22:15:36 +02:00
|
|
|
if(GetConsoleMode(handle, &length)) {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* retrieve an event from the console buffer */
|
2014-04-20 22:15:36 +02:00
|
|
|
length = 0;
|
|
|
|
if(PeekConsoleInput(handle, &inputrecord, 1, &length)) {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* check if the event is not an actual key-event */
|
|
|
|
if(length == 1 && inputrecord.EventType != KEY_EVENT) {
|
|
|
|
/* purge the non-key-event and continue waiting */
|
2014-04-20 22:15:36 +02:00
|
|
|
ReadConsoleInput(handle, &inputrecord, 1, &length);
|
2014-04-22 17:21:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-20 22:15:36 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-22 17:21:40 +02:00
|
|
|
/* there is some data available, stop waiting */
|
|
|
|
break;
|
2014-04-20 22:15:36 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-01-26 00:58:30 +01:00
|
|
|
|
2014-04-20 22:15:36 +02:00
|
|
|
case FILE_TYPE_PIPE:
|
2014-04-22 17:21:40 +02:00
|
|
|
/* The handle represents an anonymous or named pipe, this means:
|
|
|
|
* - WaitForMultipleObjectsEx will always be signalled for it.
|
|
|
|
* - peek into the pipe and retrieve the amount of data available.
|
|
|
|
*
|
|
|
|
* Approach: Loop till either the internal event is signalled
|
|
|
|
* or there is data in the pipe available for reading.
|
|
|
|
*/
|
2014-04-20 22:15:36 +02:00
|
|
|
while(WaitForMultipleObjectsEx(2, handles, FALSE, INFINITE, FALSE)
|
|
|
|
== WAIT_OBJECT_0 + 1) {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* peek into the pipe and retrieve the amount of data available */
|
2014-04-22 14:52:33 +02:00
|
|
|
if(PeekNamedPipe(handle, NULL, 0, NULL, &length, NULL)) {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* if there is no data available, sleep and continue waiting */
|
|
|
|
if(length == 0) {
|
2014-04-22 14:52:33 +02:00
|
|
|
SleepEx(100, FALSE);
|
2014-04-22 17:21:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-22 14:52:33 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-04-22 17:21:40 +02:00
|
|
|
/* if the pipe has been closed, sleep and continue waiting */
|
|
|
|
if(GetLastError() == ERROR_BROKEN_PIPE) {
|
2014-04-20 22:15:36 +02:00
|
|
|
SleepEx(100, FALSE);
|
2014-04-22 17:21:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-20 22:15:36 +02:00
|
|
|
}
|
2014-04-22 17:21:40 +02:00
|
|
|
/* there is some data available, stop waiting */
|
|
|
|
break;
|
2014-04-20 18:26:24 +02:00
|
|
|
}
|
2014-04-20 22:15:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-04-22 17:21:40 +02:00
|
|
|
/* The handle has an unknown type, try to wait on it */
|
2014-04-20 22:15:36 +02:00
|
|
|
WaitForMultipleObjectsEx(2, handles, FALSE, INFINITE, FALSE);
|
|
|
|
break;
|
2014-04-20 18:26:24 +02:00
|
|
|
}
|
2014-01-26 00:58:30 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-22 17:21:40 +02:00
|
|
|
static HANDLE select_ws_wait(HANDLE handle, HANDLE event)
|
|
|
|
{
|
|
|
|
struct select_ws_wait_data *data;
|
|
|
|
HANDLE thread = NULL;
|
|
|
|
|
|
|
|
/* allocate internal waiting data structure */
|
|
|
|
data = malloc(sizeof(struct select_ws_wait_data));
|
|
|
|
if(data) {
|
|
|
|
data->handle = handle;
|
|
|
|
data->event = event;
|
|
|
|
|
|
|
|
/* launch waiting thread */
|
|
|
|
thread = CreateThread(NULL, 0,
|
|
|
|
&select_ws_wait_thread,
|
|
|
|
data, 0, NULL);
|
|
|
|
|
|
|
|
/* free data if thread failed to launch */
|
|
|
|
if(!thread) {
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread;
|
|
|
|
}
|
2012-12-25 15:15:24 +01:00
|
|
|
static int select_ws(int nfds, fd_set *readfds, fd_set *writefds,
|
|
|
|
fd_set *exceptfds, struct timeval *timeout)
|
|
|
|
{
|
2014-01-26 09:44:16 +01:00
|
|
|
DWORD milliseconds, wait, idx;
|
2012-12-25 15:15:24 +01:00
|
|
|
WSAEVENT wsaevent, *wsaevents;
|
|
|
|
WSANETWORKEVENTS wsanetevents;
|
2014-04-22 17:21:40 +02:00
|
|
|
HANDLE handle, *handles, *threads;
|
2013-01-07 07:47:54 +01:00
|
|
|
curl_socket_t sock, *fdarr, *wsasocks;
|
2014-01-26 00:58:30 +01:00
|
|
|
long networkevents;
|
2012-12-26 20:03:35 +01:00
|
|
|
int error, fds;
|
2014-04-22 17:21:40 +02:00
|
|
|
HANDLE waitevent = NULL;
|
|
|
|
DWORD nfd = 0, thd = 0, wsa = 0;
|
2012-12-26 17:17:52 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
2013-01-06 22:29:52 +01:00
|
|
|
/* check if the input value is valid */
|
2012-12-26 17:17:52 +01:00
|
|
|
if(nfds < 0) {
|
2013-01-09 15:10:23 +01:00
|
|
|
errno = EINVAL;
|
2012-12-26 17:17:52 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-06 22:29:52 +01:00
|
|
|
/* check if we got descriptors, sleep in case we got none */
|
2012-12-26 17:17:52 +01:00
|
|
|
if(!nfds) {
|
2013-01-06 22:29:52 +01:00
|
|
|
Sleep((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000));
|
2012-12-26 17:17:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-12-25 15:15:24 +01:00
|
|
|
|
2014-04-22 17:21:40 +02:00
|
|
|
/* create internal event to signal waiting threads */
|
|
|
|
waitevent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if(!waitevent) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-25 15:15:24 +01:00
|
|
|
/* allocate internal array for the original input handles */
|
2012-12-26 20:03:35 +01:00
|
|
|
fdarr = malloc(nfds * sizeof(curl_socket_t));
|
2012-12-25 15:15:24 +01:00
|
|
|
if(fdarr == NULL) {
|
2013-01-09 15:10:23 +01:00
|
|
|
errno = ENOMEM;
|
2012-12-25 15:15:24 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate internal array for the internal event handles */
|
2012-12-26 17:17:52 +01:00
|
|
|
handles = malloc(nfds * sizeof(HANDLE));
|
2012-12-25 15:15:24 +01:00
|
|
|
if(handles == NULL) {
|
2014-04-20 18:22:28 +02:00
|
|
|
free(fdarr);
|
2013-01-09 15:10:23 +01:00
|
|
|
errno = ENOMEM;
|
2013-01-06 22:29:52 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-22 17:21:40 +02:00
|
|
|
/* allocate internal array for the internal threads handles */
|
|
|
|
threads = malloc(nfds * sizeof(HANDLE));
|
|
|
|
if(threads == NULL) {
|
|
|
|
free(handles);
|
|
|
|
free(fdarr);
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-06 22:29:52 +01:00
|
|
|
/* allocate internal array for the internal socket handles */
|
|
|
|
wsasocks = malloc(nfds * sizeof(curl_socket_t));
|
|
|
|
if(wsasocks == NULL) {
|
2014-04-22 17:21:40 +02:00
|
|
|
free(threads);
|
2014-04-20 18:22:28 +02:00
|
|
|
free(handles);
|
|
|
|
free(fdarr);
|
2013-01-09 15:10:23 +01:00
|
|
|
errno = ENOMEM;
|
2012-12-25 15:15:24 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate internal array for the internal WINSOCK2 events */
|
2012-12-26 17:17:52 +01:00
|
|
|
wsaevents = malloc(nfds * sizeof(WSAEVENT));
|
2012-12-25 15:15:24 +01:00
|
|
|
if(wsaevents == NULL) {
|
2014-04-22 17:21:40 +02:00
|
|
|
free(threads);
|
2014-04-20 18:22:28 +02:00
|
|
|
free(wsasocks);
|
|
|
|
free(handles);
|
|
|
|
free(fdarr);
|
2013-01-09 15:10:23 +01:00
|
|
|
errno = ENOMEM;
|
2012-12-25 15:15:24 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loop over the handles in the input descriptor sets */
|
|
|
|
for(fds = 0; fds < nfds; fds++) {
|
|
|
|
networkevents = 0;
|
|
|
|
handles[nfd] = 0;
|
|
|
|
|
|
|
|
if(FD_ISSET(fds, readfds))
|
2013-01-06 22:29:52 +01:00
|
|
|
networkevents |= FD_READ|FD_ACCEPT|FD_CLOSE;
|
2012-12-25 15:15:24 +01:00
|
|
|
|
|
|
|
if(FD_ISSET(fds, writefds))
|
2013-01-06 22:29:52 +01:00
|
|
|
networkevents |= FD_WRITE|FD_CONNECT;
|
2012-12-25 15:15:24 +01:00
|
|
|
|
|
|
|
if(FD_ISSET(fds, exceptfds))
|
2013-04-06 23:09:50 +02:00
|
|
|
networkevents |= FD_OOB|FD_CLOSE;
|
2012-12-25 15:15:24 +01:00
|
|
|
|
2013-01-06 22:29:52 +01:00
|
|
|
/* only wait for events for which we actually care */
|
2012-12-25 15:15:24 +01:00
|
|
|
if(networkevents) {
|
2013-01-09 13:03:53 +01:00
|
|
|
fdarr[nfd] = curlx_sitosk(fds);
|
2012-12-25 15:15:24 +01:00
|
|
|
if(fds == fileno(stdin)) {
|
2014-04-22 17:21:40 +02:00
|
|
|
handle = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
handle = select_ws_wait(handle, waitevent);
|
|
|
|
handles[nfd] = handle;
|
|
|
|
threads[thd] = handle;
|
|
|
|
thd++;
|
2012-12-25 15:15:24 +01:00
|
|
|
}
|
|
|
|
else if(fds == fileno(stdout)) {
|
|
|
|
handles[nfd] = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
}
|
|
|
|
else if(fds == fileno(stderr)) {
|
|
|
|
handles[nfd] = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
wsaevent = WSACreateEvent();
|
|
|
|
if(wsaevent != WSA_INVALID_EVENT) {
|
|
|
|
error = WSAEventSelect(fds, wsaevent, networkevents);
|
|
|
|
if(error != SOCKET_ERROR) {
|
2014-04-22 17:21:40 +02:00
|
|
|
handle = (HANDLE) wsaevent;
|
|
|
|
handles[nfd] = handle;
|
2013-01-09 13:03:53 +01:00
|
|
|
wsasocks[wsa] = curlx_sitosk(fds);
|
2013-01-06 22:29:52 +01:00
|
|
|
wsaevents[wsa] = wsaevent;
|
|
|
|
wsa++;
|
2012-12-25 15:15:24 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
WSACloseEvent(wsaevent);
|
2014-04-22 17:21:40 +02:00
|
|
|
handle = (HANDLE) curlx_sitosk(fds);
|
|
|
|
handle = select_ws_wait(handle, waitevent);
|
|
|
|
handles[nfd] = handle;
|
|
|
|
threads[thd] = handle;
|
|
|
|
thd++;
|
2012-12-25 15:15:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nfd++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert struct timeval to milliseconds */
|
|
|
|
if(timeout) {
|
|
|
|
milliseconds = ((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
milliseconds = INFINITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for one of the internal handles to trigger */
|
|
|
|
wait = WaitForMultipleObjectsEx(nfd, handles, FALSE, milliseconds, FALSE);
|
|
|
|
|
2014-04-22 17:21:40 +02:00
|
|
|
/* signal the event handle for the waiting threads */
|
|
|
|
SetEvent(waitevent);
|
2014-04-20 22:15:36 +02:00
|
|
|
|
2012-12-25 15:15:24 +01:00
|
|
|
/* loop over the internal handles returned in the descriptors */
|
|
|
|
for(idx = 0; idx < nfd; idx++) {
|
2013-01-06 22:29:52 +01:00
|
|
|
handle = handles[idx];
|
2013-01-08 19:50:50 +01:00
|
|
|
sock = fdarr[idx];
|
2013-01-09 13:03:53 +01:00
|
|
|
fds = curlx_sktosi(sock);
|
2013-01-06 22:29:52 +01:00
|
|
|
|
|
|
|
/* check if the current internal handle was triggered */
|
2013-04-05 13:31:12 +02:00
|
|
|
if(wait != WAIT_FAILED && (wait - WAIT_OBJECT_0) <= idx &&
|
2013-01-06 22:29:52 +01:00
|
|
|
WaitForSingleObjectEx(handle, 0, FALSE) == WAIT_OBJECT_0) {
|
2014-01-26 00:58:30 +01:00
|
|
|
/* first handle stdin, stdout and stderr */
|
2013-01-06 22:29:52 +01:00
|
|
|
if(fds == fileno(stdin)) {
|
|
|
|
/* stdin is never ready for write or exceptional */
|
2013-01-07 07:47:54 +01:00
|
|
|
FD_CLR(sock, writefds);
|
|
|
|
FD_CLR(sock, exceptfds);
|
2013-01-06 22:29:52 +01:00
|
|
|
}
|
|
|
|
else if(fds == fileno(stdout) || fds == fileno(stderr)) {
|
|
|
|
/* stdout and stderr are never ready for read or exceptional */
|
2013-01-07 07:47:54 +01:00
|
|
|
FD_CLR(sock, readfds);
|
|
|
|
FD_CLR(sock, exceptfds);
|
2013-01-06 22:29:52 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* try to handle the event with the WINSOCK2 functions */
|
2014-04-22 14:53:16 +02:00
|
|
|
wsanetevents.lNetworkEvents = 0;
|
2013-04-05 13:31:12 +02:00
|
|
|
error = WSAEnumNetworkEvents(fds, handle, &wsanetevents);
|
2013-01-06 22:29:52 +01:00
|
|
|
if(error != SOCKET_ERROR) {
|
|
|
|
/* remove from descriptor set if not ready for read/accept/close */
|
|
|
|
if(!(wsanetevents.lNetworkEvents & (FD_READ|FD_ACCEPT|FD_CLOSE)))
|
2013-01-07 07:47:54 +01:00
|
|
|
FD_CLR(sock, readfds);
|
2013-01-06 22:29:52 +01:00
|
|
|
|
|
|
|
/* remove from descriptor set if not ready for write/connect */
|
|
|
|
if(!(wsanetevents.lNetworkEvents & (FD_WRITE|FD_CONNECT)))
|
2013-01-07 07:47:54 +01:00
|
|
|
FD_CLR(sock, writefds);
|
2013-01-06 22:29:52 +01:00
|
|
|
|
2013-04-06 23:09:50 +02:00
|
|
|
/* HACK:
|
|
|
|
* use exceptfds together with readfds to signal
|
|
|
|
* that the connection was closed by the client.
|
|
|
|
*
|
|
|
|
* Reason: FD_CLOSE is only signaled once, sometimes
|
|
|
|
* at the same time as FD_READ with data being available.
|
|
|
|
* This means that recv/sread is not reliable to detect
|
|
|
|
* that the connection is closed.
|
|
|
|
*/
|
2013-01-06 22:29:52 +01:00
|
|
|
/* remove from descriptor set if not exceptional */
|
2013-04-06 23:09:50 +02:00
|
|
|
if(!(wsanetevents.lNetworkEvents & (FD_OOB|FD_CLOSE)))
|
2013-01-07 07:47:54 +01:00
|
|
|
FD_CLR(sock, exceptfds);
|
2013-01-06 22:29:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the event has not been filtered using specific tests */
|
2013-01-07 07:47:54 +01:00
|
|
|
if(FD_ISSET(sock, readfds) || FD_ISSET(sock, writefds) ||
|
|
|
|
FD_ISSET(sock, exceptfds)) {
|
2013-01-06 22:29:52 +01:00
|
|
|
ret++;
|
|
|
|
}
|
2012-12-25 15:15:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-01-06 22:29:52 +01:00
|
|
|
/* remove from all descriptor sets since this handle did not trigger */
|
2013-01-07 07:47:54 +01:00
|
|
|
FD_CLR(sock, readfds);
|
|
|
|
FD_CLR(sock, writefds);
|
|
|
|
FD_CLR(sock, exceptfds);
|
2012-12-25 15:15:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 22:29:52 +01:00
|
|
|
for(idx = 0; idx < wsa; idx++) {
|
|
|
|
WSAEventSelect(wsasocks[idx], NULL, 0);
|
2012-12-25 15:15:24 +01:00
|
|
|
WSACloseEvent(wsaevents[idx]);
|
2013-01-06 22:29:52 +01:00
|
|
|
}
|
2012-12-25 15:15:24 +01:00
|
|
|
|
2014-04-22 17:21:40 +02:00
|
|
|
for(idx = 0; idx < thd; idx++) {
|
|
|
|
WaitForSingleObject(threads[thd], INFINITE);
|
|
|
|
CloseHandle(threads[thd]);
|
2014-04-20 22:15:36 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 17:21:40 +02:00
|
|
|
CloseHandle(waitevent);
|
|
|
|
|
2012-12-25 15:15:24 +01:00
|
|
|
free(wsaevents);
|
2013-01-06 22:29:52 +01:00
|
|
|
free(wsasocks);
|
2014-04-22 17:21:40 +02:00
|
|
|
free(threads);
|
2012-12-25 15:15:24 +01:00
|
|
|
free(handles);
|
|
|
|
free(fdarr);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-12-26 17:17:52 +01:00
|
|
|
#define select(a,b,c,d,e) select_ws(a,b,c,d,e)
|
2013-01-08 19:50:50 +01:00
|
|
|
#endif /* USE_WINSOCK */
|
2012-12-25 15:15:24 +01:00
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
/*
|
|
|
|
sockfdp is a pointer to an established stream or CURL_SOCKET_BAD
|
|
|
|
|
|
|
|
if sockfd is CURL_SOCKET_BAD, listendfd is a listening socket we must
|
|
|
|
accept()
|
|
|
|
*/
|
2008-02-19 19:51:08 +01:00
|
|
|
static bool juggle(curl_socket_t *sockfdp,
|
|
|
|
curl_socket_t listenfd,
|
|
|
|
enum sockmode *mode)
|
2005-04-18 08:57:44 +02:00
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
fd_set fds_read;
|
|
|
|
fd_set fds_write;
|
|
|
|
fd_set fds_err;
|
2008-10-01 19:34:24 +02:00
|
|
|
curl_socket_t sockfd = CURL_SOCKET_BAD;
|
2012-12-26 17:17:52 +01:00
|
|
|
int maxfd = -99;
|
2006-07-13 20:50:51 +02:00
|
|
|
ssize_t rc;
|
2006-07-17 05:38:13 +02:00
|
|
|
ssize_t nread_socket;
|
2006-07-13 20:50:51 +02:00
|
|
|
ssize_t bytes_written;
|
2006-07-17 05:38:13 +02:00
|
|
|
ssize_t buffer_len;
|
2008-10-01 19:34:24 +02:00
|
|
|
int error = 0;
|
2007-08-22 15:57:49 +02:00
|
|
|
|
|
|
|
/* 'buffer' is this excessively large only to be able to support things like
|
|
|
|
test 1003 which tests exceedingly large server response lines */
|
|
|
|
unsigned char buffer[17010];
|
|
|
|
char data[16];
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-28 11:15:21 +01:00
|
|
|
if(got_exit_signal) {
|
|
|
|
logmsg("signalled to die, exiting...");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-02-17 05:36:08 +01:00
|
|
|
#ifdef HAVE_GETPPID
|
|
|
|
/* As a last resort, quit if sockfilt process becomes orphan. Just in case
|
|
|
|
parent ftpserver process has died without killing its sockfilt children */
|
2008-02-26 19:13:59 +01:00
|
|
|
if(getppid() <= 1) {
|
|
|
|
logmsg("process becomes orphan, exiting");
|
2008-02-17 05:36:08 +01:00
|
|
|
return FALSE;
|
2008-02-26 19:13:59 +01:00
|
|
|
}
|
2008-02-17 05:36:08 +01:00
|
|
|
#endif
|
|
|
|
|
2008-02-22 10:31:18 +01:00
|
|
|
timeout.tv_sec = 120;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
FD_ZERO(&fds_read);
|
|
|
|
FD_ZERO(&fds_write);
|
|
|
|
FD_ZERO(&fds_err);
|
|
|
|
|
2012-12-26 20:03:35 +01:00
|
|
|
FD_SET((curl_socket_t)fileno(stdin), &fds_read);
|
2012-12-25 15:18:01 +01:00
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
switch(*mode) {
|
2006-07-17 05:38:13 +02:00
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
case PASSIVE_LISTEN:
|
2006-07-17 05:38:13 +02:00
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
/* server mode */
|
|
|
|
sockfd = listenfd;
|
|
|
|
/* there's always a socket to wait for */
|
|
|
|
FD_SET(sockfd, &fds_read);
|
2012-12-26 17:17:52 +01:00
|
|
|
maxfd = (int)sockfd;
|
2005-04-18 08:57:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PASSIVE_CONNECT:
|
2006-07-17 05:38:13 +02:00
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
sockfd = *sockfdp;
|
2006-07-17 05:38:13 +02:00
|
|
|
if(CURL_SOCKET_BAD == sockfd) {
|
2005-04-18 10:49:46 +02:00
|
|
|
/* eeek, we are supposedly connected and then this cannot be -1 ! */
|
|
|
|
logmsg("socket is -1! on %s:%d", __FILE__, __LINE__);
|
2005-04-18 10:59:46 +02:00
|
|
|
maxfd = 0; /* stdin */
|
2005-04-18 10:49:46 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* there's always a socket to wait for */
|
|
|
|
FD_SET(sockfd, &fds_read);
|
2013-04-06 23:09:50 +02:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
FD_SET(sockfd, &fds_err);
|
|
|
|
#endif
|
2012-12-26 17:17:52 +01:00
|
|
|
maxfd = (int)sockfd;
|
2005-04-18 10:49:46 +02:00
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTIVE:
|
|
|
|
|
2006-07-17 05:38:13 +02:00
|
|
|
sockfd = *sockfdp;
|
2005-04-18 08:57:44 +02:00
|
|
|
/* sockfd turns CURL_SOCKET_BAD when our connection has been closed */
|
2006-07-17 05:38:13 +02:00
|
|
|
if(CURL_SOCKET_BAD != sockfd) {
|
2005-04-18 08:57:44 +02:00
|
|
|
FD_SET(sockfd, &fds_read);
|
2013-04-06 23:09:50 +02:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
FD_SET(sockfd, &fds_err);
|
|
|
|
#endif
|
2012-12-26 17:17:52 +01:00
|
|
|
maxfd = (int)sockfd;
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
logmsg("No socket to read on");
|
|
|
|
maxfd = 0;
|
|
|
|
}
|
|
|
|
break;
|
2005-05-07 15:52:07 +02:00
|
|
|
|
|
|
|
case ACTIVE_DISCONNECT:
|
2006-07-17 05:38:13 +02:00
|
|
|
|
2005-05-07 15:52:07 +02:00
|
|
|
logmsg("disconnected, no socket to read on");
|
|
|
|
maxfd = 0;
|
|
|
|
sockfd = CURL_SOCKET_BAD;
|
|
|
|
break;
|
2006-07-17 05:38:13 +02:00
|
|
|
|
|
|
|
} /* switch(*mode) */
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
do {
|
2008-02-26 19:13:59 +01:00
|
|
|
|
2012-12-26 17:17:52 +01:00
|
|
|
/* select() blocking behavior call on blocking descriptors please */
|
|
|
|
|
|
|
|
rc = select(maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
|
2008-02-26 19:13:59 +01:00
|
|
|
|
2008-02-28 11:15:21 +01:00
|
|
|
if(got_exit_signal) {
|
|
|
|
logmsg("signalled to die, exiting...");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:10:23 +01:00
|
|
|
} while((rc == -1) && ((error = errno) == EINTR));
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-28 11:13:07 +01:00
|
|
|
if(rc < 0) {
|
|
|
|
logmsg("select() failed with error: (%d) %s",
|
|
|
|
error, strerror(error));
|
2005-04-18 08:57:44 +02:00
|
|
|
return FALSE;
|
2008-02-28 11:13:07 +01:00
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
if(rc == 0)
|
|
|
|
/* timeout */
|
2005-04-18 08:57:44 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
|
|
if(FD_ISSET(fileno(stdin), &fds_read)) {
|
|
|
|
/* read from stdin, commands/data to be dealt with and possibly passed on
|
|
|
|
to the socket
|
|
|
|
|
|
|
|
protocol:
|
|
|
|
|
|
|
|
4 letter command + LF [mandatory]
|
|
|
|
|
|
|
|
4-digit hexadecimal data length + LF [if the command takes data]
|
|
|
|
data [the data being as long as set above]
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
|
|
|
|
DATA - plain pass-thru data
|
|
|
|
*/
|
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
if(!read_stdin(buffer, 5))
|
|
|
|
return FALSE;
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
logmsg("Received %c%c%c%c (on stdin)",
|
|
|
|
buffer[0], buffer[1], buffer[2], buffer[3] );
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
if(!memcmp("PING", buffer, 4)) {
|
|
|
|
/* send reply on stdout, just proving we are alive */
|
|
|
|
if(!write_stdout("PONG\n", 5))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(!memcmp("PORT", buffer, 4)) {
|
|
|
|
/* Question asking us what PORT number we are listening to.
|
|
|
|
Replies to PORT with "IPv[num]/[port]" */
|
2009-05-13 06:16:00 +02:00
|
|
|
sprintf((char *)buffer, "%s/%hu\n", ipv_inuse, port);
|
2008-02-26 19:13:59 +01:00
|
|
|
buffer_len = (ssize_t)strlen((char *)buffer);
|
2009-05-13 06:16:00 +02:00
|
|
|
snprintf(data, sizeof(data), "PORT\n%04zx\n", buffer_len);
|
2008-02-26 19:13:59 +01:00
|
|
|
if(!write_stdout(data, 10))
|
|
|
|
return FALSE;
|
|
|
|
if(!write_stdout(buffer, buffer_len))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if(!memcmp("QUIT", buffer, 4)) {
|
|
|
|
/* just die */
|
|
|
|
logmsg("quits");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if(!memcmp("DATA", buffer, 4)) {
|
|
|
|
/* data IN => data OUT */
|
|
|
|
|
|
|
|
if(!read_stdin(buffer, 5))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
buffer[5] = '\0';
|
|
|
|
|
|
|
|
buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
|
|
|
|
if (buffer_len > (ssize_t)sizeof(buffer)) {
|
2008-09-04 07:29:10 +02:00
|
|
|
logmsg("ERROR: Buffer size (%zu bytes) too small for data size "
|
|
|
|
"(%zd bytes)", sizeof(buffer), buffer_len);
|
2005-05-07 15:52:07 +02:00
|
|
|
return FALSE;
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
2008-09-04 07:29:10 +02:00
|
|
|
logmsg("> %zd bytes data, server => client", buffer_len);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
if(!read_stdin(buffer, buffer_len))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lograw(buffer, buffer_len);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-26 19:13:59 +01:00
|
|
|
if(*mode == PASSIVE_LISTEN) {
|
|
|
|
logmsg("*** We are disconnected!");
|
|
|
|
if(!write_stdout("DISC\n", 5))
|
2006-07-17 05:38:13 +02:00
|
|
|
return FALSE;
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
2008-02-26 19:13:59 +01:00
|
|
|
else {
|
|
|
|
/* send away on the socket */
|
|
|
|
bytes_written = swrite(sockfd, buffer, buffer_len);
|
|
|
|
if(bytes_written != buffer_len) {
|
2008-09-04 07:29:10 +02:00
|
|
|
logmsg("Not all data was sent. Bytes to send: %zd sent: %zd",
|
2008-02-26 19:13:59 +01:00
|
|
|
buffer_len, bytes_written);
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-26 19:13:59 +01:00
|
|
|
else if(!memcmp("DISC", buffer, 4)) {
|
|
|
|
/* disconnect! */
|
|
|
|
if(!write_stdout("DISC\n", 5))
|
|
|
|
return FALSE;
|
|
|
|
if(sockfd != CURL_SOCKET_BAD) {
|
|
|
|
logmsg("====> Client forcibly disconnected");
|
|
|
|
sclose(sockfd);
|
|
|
|
*sockfdp = CURL_SOCKET_BAD;
|
|
|
|
if(*mode == PASSIVE_CONNECT)
|
|
|
|
*mode = PASSIVE_LISTEN;
|
|
|
|
else
|
|
|
|
*mode = ACTIVE_DISCONNECT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logmsg("attempt to close already dead connection");
|
|
|
|
return TRUE;
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-17 05:38:13 +02:00
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
if((sockfd != CURL_SOCKET_BAD) && (FD_ISSET(sockfd, &fds_read)) ) {
|
|
|
|
|
2013-01-08 19:50:50 +01:00
|
|
|
curl_socket_t newfd = CURL_SOCKET_BAD; /* newly accepted socket */
|
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
if(*mode == PASSIVE_LISTEN) {
|
|
|
|
/* there's no stream set up yet, this is an indication that there's a
|
|
|
|
client connecting. */
|
2013-01-08 19:50:50 +01:00
|
|
|
newfd = accept(sockfd, NULL, NULL);
|
|
|
|
if(CURL_SOCKET_BAD == newfd) {
|
2013-01-06 22:23:32 +01:00
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("accept(%d, NULL, NULL) failed with error: (%d) %s",
|
2013-01-08 19:50:50 +01:00
|
|
|
sockfd, error, strerror(error));
|
2013-01-06 22:23:32 +01:00
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
else {
|
|
|
|
logmsg("====> Client connect");
|
2008-02-26 19:13:59 +01:00
|
|
|
if(!write_stdout("CNCT\n", 5))
|
|
|
|
return FALSE;
|
2013-01-08 19:50:50 +01:00
|
|
|
*sockfdp = newfd; /* store the new socket */
|
2005-04-18 08:57:44 +02:00
|
|
|
*mode = PASSIVE_CONNECT; /* we have connected */
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read from socket, pass on data to stdout */
|
2006-07-17 05:38:13 +02:00
|
|
|
nread_socket = sread(sockfd, buffer, sizeof(buffer));
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2013-04-06 23:09:50 +02:00
|
|
|
if(nread_socket > 0) {
|
|
|
|
snprintf(data, sizeof(data), "DATA\n%04zx\n", nread_socket);
|
|
|
|
if(!write_stdout(data, 10))
|
|
|
|
return FALSE;
|
|
|
|
if(!write_stdout(buffer, nread_socket))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
logmsg("< %zd bytes data, client => server", nread_socket);
|
|
|
|
lograw(buffer, nread_socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nread_socket <= 0
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
|| FD_ISSET(sockfd, &fds_err)
|
|
|
|
#endif
|
|
|
|
) {
|
2005-04-18 08:57:44 +02:00
|
|
|
logmsg("====> Client disconnect");
|
2008-02-26 19:13:59 +01:00
|
|
|
if(!write_stdout("DISC\n", 5))
|
|
|
|
return FALSE;
|
2005-04-18 08:57:44 +02:00
|
|
|
sclose(sockfd);
|
|
|
|
*sockfdp = CURL_SOCKET_BAD;
|
|
|
|
if(*mode == PASSIVE_CONNECT)
|
|
|
|
*mode = PASSIVE_LISTEN;
|
2005-05-07 15:52:07 +02:00
|
|
|
else
|
|
|
|
*mode = ACTIVE_DISCONNECT;
|
2005-04-18 08:57:44 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-05-25 14:04:24 +02:00
|
|
|
static curl_socket_t sockdaemon(curl_socket_t sock,
|
2007-09-27 03:45:22 +02:00
|
|
|
unsigned short *listenport)
|
2005-05-25 14:04:24 +02:00
|
|
|
{
|
|
|
|
/* passive daemon style */
|
2010-11-19 19:20:38 +01:00
|
|
|
srvr_sockaddr_union_t listener;
|
2009-05-14 15:00:54 +02:00
|
|
|
int flag;
|
2005-05-25 14:04:24 +02:00
|
|
|
int rc;
|
2008-02-18 21:13:30 +01:00
|
|
|
int totdelay = 0;
|
|
|
|
int maxretr = 10;
|
|
|
|
int delay= 20;
|
|
|
|
int attempt = 0;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
attempt++;
|
2009-05-14 15:00:54 +02:00
|
|
|
flag = 1;
|
2007-07-12 03:07:49 +02:00
|
|
|
rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
2009-05-14 15:00:54 +02:00
|
|
|
(void *)&flag, sizeof(flag));
|
2008-02-18 21:13:30 +01:00
|
|
|
if(rc) {
|
|
|
|
error = SOCKERRNO;
|
2009-05-14 15:00:54 +02:00
|
|
|
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
|
|
|
|
error, strerror(error));
|
2008-02-18 21:13:30 +01:00
|
|
|
if(maxretr) {
|
|
|
|
rc = wait_ms(delay);
|
|
|
|
if(rc) {
|
|
|
|
/* should not happen */
|
2013-01-09 15:10:23 +01:00
|
|
|
error = errno;
|
2009-05-14 15:00:54 +02:00
|
|
|
logmsg("wait_ms() failed with error: (%d) %s",
|
|
|
|
error, strerror(error));
|
2008-02-18 21:13:30 +01:00
|
|
|
sclose(sock);
|
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
2008-02-28 11:15:21 +01:00
|
|
|
if(got_exit_signal) {
|
|
|
|
logmsg("signalled to die, exiting...");
|
|
|
|
sclose(sock);
|
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
2008-02-18 21:13:30 +01:00
|
|
|
totdelay += delay;
|
|
|
|
delay *= 2; /* double the sleep for next attempt */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(rc && maxretr--);
|
|
|
|
|
|
|
|
if(rc) {
|
|
|
|
logmsg("setsockopt(SO_REUSEADDR) failed %d times in %d ms. Error: (%d) %s",
|
|
|
|
attempt, totdelay, error, strerror(error));
|
|
|
|
logmsg("Continuing anyway...");
|
2005-05-25 14:04:24 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 15:00:54 +02:00
|
|
|
/* When the specified listener port is zero, it is actually a
|
|
|
|
request to let the system choose a non-zero available port. */
|
|
|
|
|
2005-05-25 14:04:24 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6) {
|
|
|
|
#endif
|
2010-11-19 19:20:38 +01:00
|
|
|
memset(&listener.sa4, 0, sizeof(listener.sa4));
|
|
|
|
listener.sa4.sin_family = AF_INET;
|
|
|
|
listener.sa4.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
listener.sa4.sin_port = htons(*listenport);
|
|
|
|
rc = bind(sock, &listener.sa, sizeof(listener.sa4));
|
2005-05-25 14:04:24 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
}
|
|
|
|
else {
|
2010-11-19 19:20:38 +01:00
|
|
|
memset(&listener.sa6, 0, sizeof(listener.sa6));
|
|
|
|
listener.sa6.sin6_family = AF_INET6;
|
|
|
|
listener.sa6.sin6_addr = in6addr_any;
|
|
|
|
listener.sa6.sin6_port = htons(*listenport);
|
|
|
|
rc = bind(sock, &listener.sa, sizeof(listener.sa6));
|
2005-05-25 14:04:24 +02:00
|
|
|
}
|
|
|
|
#endif /* ENABLE_IPV6 */
|
2008-02-18 21:13:30 +01:00
|
|
|
if(rc) {
|
|
|
|
error = SOCKERRNO;
|
2010-01-10 05:24:46 +01:00
|
|
|
logmsg("Error binding socket on port %hu: (%d) %s",
|
|
|
|
*listenport, error, strerror(error));
|
2008-02-18 21:13:30 +01:00
|
|
|
sclose(sock);
|
2005-05-25 14:04:24 +02:00
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
|
|
|
|
2007-09-27 03:45:22 +02:00
|
|
|
if(!*listenport) {
|
2009-05-14 15:00:54 +02:00
|
|
|
/* The system was supposed to choose a port number, figure out which
|
|
|
|
port we actually got and update the listener port value with it. */
|
|
|
|
curl_socklen_t la_size;
|
2010-11-19 19:20:38 +01:00
|
|
|
srvr_sockaddr_union_t localaddr;
|
2009-05-14 15:00:54 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
2010-11-19 19:20:38 +01:00
|
|
|
if(!use_ipv6)
|
2009-05-14 15:00:54 +02:00
|
|
|
#endif
|
2010-11-19 19:20:38 +01:00
|
|
|
la_size = sizeof(localaddr.sa4);
|
2009-05-14 15:00:54 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
2010-11-19 19:20:38 +01:00
|
|
|
else
|
|
|
|
la_size = sizeof(localaddr.sa6);
|
2009-05-14 15:00:54 +02:00
|
|
|
#endif
|
2010-11-19 19:20:38 +01:00
|
|
|
memset(&localaddr.sa, 0, (size_t)la_size);
|
|
|
|
if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
|
2008-02-18 21:13:30 +01:00
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("getsockname() failed with error: (%d) %s",
|
|
|
|
error, strerror(error));
|
|
|
|
sclose(sock);
|
2005-05-25 14:04:24 +02:00
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
2010-11-19 19:20:38 +01:00
|
|
|
switch (localaddr.sa.sa_family) {
|
2009-05-14 15:00:54 +02:00
|
|
|
case AF_INET:
|
2010-11-19 19:20:38 +01:00
|
|
|
*listenport = ntohs(localaddr.sa4.sin_port);
|
2009-05-14 15:00:54 +02:00
|
|
|
break;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
case AF_INET6:
|
2010-11-19 19:20:38 +01:00
|
|
|
*listenport = ntohs(localaddr.sa6.sin6_port);
|
2009-05-14 15:00:54 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!*listenport) {
|
|
|
|
/* Real failure, listener port shall not be zero beyond this point. */
|
2009-05-18 04:19:21 +02:00
|
|
|
logmsg("Apparently getsockname() succeeded, with listener port zero.");
|
|
|
|
logmsg("A valid reason for this failure is a binary built without");
|
|
|
|
logmsg("proper network library linkage. This might not be the only");
|
|
|
|
logmsg("reason, but double check it before anything else.");
|
2009-05-14 15:00:54 +02:00
|
|
|
sclose(sock);
|
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
2005-05-25 14:04:24 +02:00
|
|
|
}
|
|
|
|
|
2011-10-31 07:29:13 +01:00
|
|
|
/* bindonly option forces no listening */
|
|
|
|
if(bind_only) {
|
|
|
|
logmsg("instructed to bind port without listening");
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2005-05-25 14:04:24 +02:00
|
|
|
/* start accepting connections */
|
2008-02-18 21:13:30 +01:00
|
|
|
rc = listen(sock, 5);
|
2006-07-19 17:28:30 +02:00
|
|
|
if(0 != rc) {
|
2008-02-18 21:13:30 +01:00
|
|
|
error = SOCKERRNO;
|
2013-01-06 22:23:32 +01:00
|
|
|
logmsg("listen(%d, 5) failed with error: (%d) %s",
|
|
|
|
sock, error, strerror(error));
|
2006-07-19 17:28:30 +02:00
|
|
|
sclose(sock);
|
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
2005-05-25 14:04:24 +02:00
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2010-11-19 19:20:38 +01:00
|
|
|
srvr_sockaddr_union_t me;
|
2008-02-28 11:13:07 +01:00
|
|
|
curl_socket_t sock = CURL_SOCKET_BAD;
|
|
|
|
curl_socket_t msgsock = CURL_SOCKET_BAD;
|
|
|
|
int wrotepidfile = 0;
|
2005-04-18 08:57:44 +02:00
|
|
|
char *pidname= (char *)".sockfilt.pid";
|
2009-10-10 14:29:32 +02:00
|
|
|
bool juggle_again;
|
2005-04-18 08:57:44 +02:00
|
|
|
int rc;
|
2007-02-19 03:03:58 +01:00
|
|
|
int error;
|
2005-04-18 08:57:44 +02:00
|
|
|
int arg=1;
|
|
|
|
enum sockmode mode = PASSIVE_LISTEN; /* default */
|
2007-09-17 20:12:11 +02:00
|
|
|
const char *addr = NULL;
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
while(argc>arg) {
|
|
|
|
if(!strcmp("--version", argv[arg])) {
|
|
|
|
printf("sockfilt IPv4%s\n",
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
"/IPv6"
|
|
|
|
#else
|
|
|
|
""
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-02-26 19:13:59 +01:00
|
|
|
else if(!strcmp("--verbose", argv[arg])) {
|
|
|
|
verbose = TRUE;
|
|
|
|
arg++;
|
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
else if(!strcmp("--pidfile", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg)
|
|
|
|
pidname = argv[arg++];
|
|
|
|
}
|
|
|
|
else if(!strcmp("--logfile", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg)
|
2005-05-01 01:30:55 +02:00
|
|
|
serverlogfile = argv[arg++];
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
else if(!strcmp("--ipv6", argv[arg])) {
|
|
|
|
#ifdef ENABLE_IPV6
|
2008-09-26 13:21:22 +02:00
|
|
|
ipv_inuse = "IPv6";
|
|
|
|
use_ipv6 = TRUE;
|
2005-04-18 08:57:44 +02:00
|
|
|
#endif
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
else if(!strcmp("--ipv4", argv[arg])) {
|
|
|
|
/* for completeness, we support this option as well */
|
2008-09-26 13:21:22 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
ipv_inuse = "IPv4";
|
|
|
|
use_ipv6 = FALSE;
|
|
|
|
#endif
|
2005-04-18 08:57:44 +02:00
|
|
|
arg++;
|
|
|
|
}
|
2011-10-31 07:29:13 +01:00
|
|
|
else if(!strcmp("--bindonly", argv[arg])) {
|
|
|
|
bind_only = TRUE;
|
|
|
|
arg++;
|
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
else if(!strcmp("--port", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg) {
|
2010-02-04 18:17:19 +01:00
|
|
|
char *endptr;
|
2010-02-18 13:31:24 +01:00
|
|
|
unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
|
2010-02-04 18:17:19 +01:00
|
|
|
if((endptr != argv[arg] + strlen(argv[arg])) ||
|
2010-02-18 13:31:24 +01:00
|
|
|
((ulnum != 0UL) && ((ulnum < 1025UL) || (ulnum > 65535UL)))) {
|
2010-02-04 18:17:19 +01:00
|
|
|
fprintf(stderr, "sockfilt: invalid --port argument (%s)\n",
|
|
|
|
argv[arg]);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-22 19:56:29 +01:00
|
|
|
port = curlx_ultous(ulnum);
|
2005-04-18 08:57:44 +02:00
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!strcmp("--connect", argv[arg])) {
|
|
|
|
/* Asked to actively connect to the specified local port instead of
|
|
|
|
doing a passive server-style listening. */
|
|
|
|
arg++;
|
|
|
|
if(argc>arg) {
|
2010-02-04 18:17:19 +01:00
|
|
|
char *endptr;
|
2010-02-18 13:31:24 +01:00
|
|
|
unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
|
2010-02-04 18:17:19 +01:00
|
|
|
if((endptr != argv[arg] + strlen(argv[arg])) ||
|
2010-02-18 13:31:24 +01:00
|
|
|
(ulnum < 1025UL) || (ulnum > 65535UL)) {
|
2010-02-04 18:17:19 +01:00
|
|
|
fprintf(stderr, "sockfilt: invalid --connect argument (%s)\n",
|
|
|
|
argv[arg]);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-22 19:56:29 +01:00
|
|
|
connectport = curlx_ultous(ulnum);
|
2005-04-18 08:57:44 +02:00
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
}
|
2007-09-17 20:12:11 +02:00
|
|
|
else if(!strcmp("--addr", argv[arg])) {
|
|
|
|
/* Set an IP address to use with --connect; otherwise use localhost */
|
|
|
|
arg++;
|
|
|
|
if(argc>arg) {
|
|
|
|
addr = argv[arg];
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
else {
|
|
|
|
puts("Usage: sockfilt [option]\n"
|
|
|
|
" --version\n"
|
2008-02-26 19:13:59 +01:00
|
|
|
" --verbose\n"
|
2005-04-18 08:57:44 +02:00
|
|
|
" --logfile [file]\n"
|
|
|
|
" --pidfile [file]\n"
|
|
|
|
" --ipv4\n"
|
|
|
|
" --ipv6\n"
|
2011-10-31 07:29:13 +01:00
|
|
|
" --bindonly\n"
|
2007-09-17 20:12:11 +02:00
|
|
|
" --port [port]\n"
|
|
|
|
" --connect [port]\n"
|
|
|
|
" --addr [address]");
|
2005-05-07 15:52:07 +02:00
|
|
|
return 0;
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-11 18:01:16 +02:00
|
|
|
#ifdef WIN32
|
2005-04-18 08:57:44 +02:00
|
|
|
win32_init();
|
|
|
|
atexit(win32_cleanup);
|
2013-01-06 22:25:18 +01:00
|
|
|
|
|
|
|
setmode(fileno(stdin), O_BINARY);
|
|
|
|
setmode(fileno(stdout), O_BINARY);
|
|
|
|
setmode(fileno(stderr), O_BINARY);
|
2005-04-18 08:57:44 +02:00
|
|
|
#endif
|
|
|
|
|
2008-02-28 11:15:21 +01:00
|
|
|
install_signal_handlers();
|
|
|
|
|
2008-02-18 21:13:30 +01:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6)
|
|
|
|
#endif
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else
|
|
|
|
sock = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
#endif
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-18 21:13:30 +01:00
|
|
|
if(CURL_SOCKET_BAD == sock) {
|
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("Error creating socket: (%d) %s",
|
|
|
|
error, strerror(error));
|
2011-10-30 17:12:20 +01:00
|
|
|
write_stdout("FAIL\n", 5);
|
2008-02-28 11:13:07 +01:00
|
|
|
goto sockfilt_cleanup;
|
2006-07-19 17:28:30 +02:00
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
if(connectport) {
|
|
|
|
/* Active mode, we should connect to the given port number */
|
|
|
|
mode = ACTIVE;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6) {
|
|
|
|
#endif
|
2010-11-19 19:20:38 +01:00
|
|
|
memset(&me.sa4, 0, sizeof(me.sa4));
|
|
|
|
me.sa4.sin_family = AF_INET;
|
|
|
|
me.sa4.sin_port = htons(connectport);
|
|
|
|
me.sa4.sin_addr.s_addr = INADDR_ANY;
|
2007-09-17 20:12:11 +02:00
|
|
|
if (!addr)
|
|
|
|
addr = "127.0.0.1";
|
2010-11-19 19:20:38 +01:00
|
|
|
Curl_inet_pton(AF_INET, addr, &me.sa4.sin_addr);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2010-11-19 19:20:38 +01:00
|
|
|
rc = connect(sock, &me.sa, sizeof(me.sa4));
|
2005-04-18 08:57:44 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
}
|
|
|
|
else {
|
2010-11-19 19:20:38 +01:00
|
|
|
memset(&me.sa6, 0, sizeof(me.sa6));
|
|
|
|
me.sa6.sin6_family = AF_INET6;
|
|
|
|
me.sa6.sin6_port = htons(connectport);
|
2007-09-17 20:12:11 +02:00
|
|
|
if (!addr)
|
|
|
|
addr = "::1";
|
2010-11-19 19:20:38 +01:00
|
|
|
Curl_inet_pton(AF_INET6, addr, &me.sa6.sin6_addr);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2010-11-19 19:20:38 +01:00
|
|
|
rc = connect(sock, &me.sa, sizeof(me.sa6));
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
#endif /* ENABLE_IPV6 */
|
|
|
|
if(rc) {
|
2008-02-18 21:13:30 +01:00
|
|
|
error = SOCKERRNO;
|
2008-09-04 07:29:10 +02:00
|
|
|
logmsg("Error connecting to port %hu: (%d) %s",
|
2008-03-05 19:27:31 +01:00
|
|
|
connectport, error, strerror(error));
|
2011-10-30 17:12:20 +01:00
|
|
|
write_stdout("FAIL\n", 5);
|
2008-02-28 11:13:07 +01:00
|
|
|
goto sockfilt_cleanup;
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
logmsg("====> Client connect");
|
|
|
|
msgsock = sock; /* use this as stream */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* passive daemon style */
|
2005-05-25 14:04:24 +02:00
|
|
|
sock = sockdaemon(sock, &port);
|
2011-10-30 17:12:20 +01:00
|
|
|
if(CURL_SOCKET_BAD == sock) {
|
|
|
|
write_stdout("FAIL\n", 5);
|
2008-02-28 11:13:07 +01:00
|
|
|
goto sockfilt_cleanup;
|
2011-10-30 17:12:20 +01:00
|
|
|
}
|
2005-05-25 14:04:24 +02:00
|
|
|
msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
|
2008-09-26 13:21:22 +02:00
|
|
|
logmsg("Running %s version", ipv_inuse);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
if(connectport)
|
2008-09-04 07:29:10 +02:00
|
|
|
logmsg("Connected to port %hu", connectport);
|
2011-10-31 07:29:13 +01:00
|
|
|
else if(bind_only)
|
|
|
|
logmsg("Bound without listening on port %hu", port);
|
2005-04-18 08:57:44 +02:00
|
|
|
else
|
2008-09-04 07:29:10 +02:00
|
|
|
logmsg("Listening on port %hu", port);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-28 11:13:07 +01:00
|
|
|
wrotepidfile = write_pidfile(pidname);
|
2011-10-30 17:12:20 +01:00
|
|
|
if(!wrotepidfile) {
|
|
|
|
write_stdout("FAIL\n", 5);
|
2008-02-28 11:13:07 +01:00
|
|
|
goto sockfilt_cleanup;
|
2011-10-30 17:12:20 +01:00
|
|
|
}
|
2005-05-07 15:52:07 +02:00
|
|
|
|
2009-10-10 14:29:32 +02:00
|
|
|
do {
|
|
|
|
juggle_again = juggle(&msgsock, sock, &mode);
|
|
|
|
} while(juggle_again);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-28 11:13:07 +01:00
|
|
|
sockfilt_cleanup:
|
|
|
|
|
|
|
|
if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
|
|
|
|
sclose(msgsock);
|
|
|
|
|
|
|
|
if(sock != CURL_SOCKET_BAD)
|
2008-02-28 11:15:21 +01:00
|
|
|
sclose(sock);
|
2008-02-28 11:13:07 +01:00
|
|
|
|
|
|
|
if(wrotepidfile)
|
2008-02-28 11:15:21 +01:00
|
|
|
unlink(pidname);
|
|
|
|
|
|
|
|
restore_signal_handlers();
|
|
|
|
|
|
|
|
if(got_exit_signal) {
|
|
|
|
logmsg("============> sockfilt exits with signal (%d)", exit_signal);
|
|
|
|
/*
|
|
|
|
* To properly set the return status of the process we
|
|
|
|
* must raise the same signal SIGINT or SIGTERM that we
|
|
|
|
* caught and let the old handler take care of it.
|
|
|
|
*/
|
|
|
|
raise(exit_signal);
|
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2008-02-28 11:13:07 +01:00
|
|
|
logmsg("============> sockfilt quits");
|
2005-04-18 08:57:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|