2005-04-18 08:57:44 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2007-02-16 19:19:35 +01:00
|
|
|
* Copyright (C) 1998 - 2007, 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.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
*/
|
|
|
|
#include "setup.h" /* portability help from the lib directory */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef _XOPEN_SOURCE_EXTENDED
|
|
|
|
/* This define is "almost" required to build on HPUX 11 */
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
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"
|
|
|
|
#include "inet_pton.h"
|
2005-05-01 01:30:55 +02:00
|
|
|
#include "util.h"
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
/* include memdebug.h last */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
#define DEFAULT_PORT 8999
|
|
|
|
|
|
|
|
#ifndef DEFAULT_LOGFILE
|
|
|
|
#define DEFAULT_LOGFILE "log/sockfilt.log"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
static volatile int sigpipe; /* Why? It's not used */
|
|
|
|
#endif
|
|
|
|
|
2005-05-01 01:30:55 +02:00
|
|
|
const char *serverlogfile = (char *)DEFAULT_LOGFILE;
|
2005-04-18 08:57:44 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
static void sigpipe_handler(int sig)
|
|
|
|
{
|
|
|
|
(void)sig; /* prevent warning */
|
|
|
|
sigpipe = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char use_ipv6=FALSE;
|
|
|
|
unsigned short port = DEFAULT_PORT;
|
|
|
|
unsigned short connectport = 0; /* if non-zero, we activate this mode */
|
|
|
|
|
|
|
|
enum sockmode {
|
2005-05-07 15:52:07 +02:00
|
|
|
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 */
|
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()
|
|
|
|
*/
|
|
|
|
static int juggle(curl_socket_t *sockfdp,
|
|
|
|
curl_socket_t listenfd,
|
|
|
|
enum sockmode *mode)
|
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
fd_set fds_read;
|
|
|
|
fd_set fds_write;
|
|
|
|
fd_set fds_err;
|
2006-07-17 05:38:13 +02:00
|
|
|
curl_socket_t sockfd;
|
2005-04-18 08:57:44 +02:00
|
|
|
curl_socket_t maxfd;
|
2006-07-13 20:50:51 +02:00
|
|
|
ssize_t rc;
|
2006-07-17 05:38:13 +02:00
|
|
|
ssize_t nread_stdin;
|
|
|
|
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;
|
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
|
|
|
|
|
|
|
timeout.tv_sec = 120;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
FD_ZERO(&fds_read);
|
|
|
|
FD_ZERO(&fds_write);
|
|
|
|
FD_ZERO(&fds_err);
|
|
|
|
|
|
|
|
FD_SET(fileno(stdin), &fds_read);
|
|
|
|
|
|
|
|
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);
|
|
|
|
maxfd = sockfd;
|
|
|
|
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);
|
|
|
|
maxfd = sockfd;
|
|
|
|
}
|
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);
|
|
|
|
maxfd = sockfd;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
do {
|
2006-07-13 20:50:51 +02:00
|
|
|
rc = select(maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
|
2007-02-16 17:01:19 +01:00
|
|
|
} while((rc == -1) && (SOCKERRNO == EINTR));
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2006-07-13 20:50:51 +02:00
|
|
|
switch(rc) {
|
2005-04-18 08:57:44 +02:00
|
|
|
case -1:
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
case 0: /* timeout! */
|
|
|
|
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
|
|
|
|
*/
|
2006-07-17 05:38:13 +02:00
|
|
|
nread_stdin = read(fileno(stdin), buffer, 5);
|
|
|
|
if(5 == nread_stdin) {
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2005-05-07 10:55:37 +02:00
|
|
|
logmsg("Received %c%c%c%c (on stdin)",
|
2005-04-18 08:57:44 +02:00
|
|
|
buffer[0], buffer[1], buffer[2], buffer[3] );
|
|
|
|
|
|
|
|
if(!memcmp("PING", buffer, 4)) {
|
|
|
|
/* send reply on stdout, just proving we are alive */
|
|
|
|
write(fileno(stdout), "PONG\n", 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(!memcmp("PORT", buffer, 4)) {
|
2006-07-17 05:38:13 +02:00
|
|
|
/* Question asking us what PORT number we are listening to.
|
|
|
|
Replies to PORT with "IPv[num]/[port]" */
|
|
|
|
sprintf((char *)buffer, "IPv%d/%d\n", use_ipv6?6:4, (int)port);
|
|
|
|
buffer_len = (ssize_t)strlen((char *)buffer);
|
2007-08-22 15:57:49 +02:00
|
|
|
snprintf(data, sizeof(data), "PORT\n%04x\n", buffer_len);
|
2005-04-18 08:57:44 +02:00
|
|
|
write(fileno(stdout), data, 10);
|
2006-07-17 05:38:13 +02:00
|
|
|
write(fileno(stdout), buffer, buffer_len);
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
else if(!memcmp("QUIT", buffer, 4)) {
|
|
|
|
/* just die */
|
|
|
|
logmsg("quits");
|
2005-05-07 15:52:07 +02:00
|
|
|
return FALSE;
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
else if(!memcmp("DATA", buffer, 4)) {
|
|
|
|
/* data IN => data OUT */
|
|
|
|
|
|
|
|
if(5 != read(fileno(stdin), buffer, 5))
|
|
|
|
return FALSE;
|
2006-07-17 05:38:13 +02:00
|
|
|
buffer[5] = '\0';
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2006-07-17 05:38:13 +02:00
|
|
|
buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
|
|
|
|
if (buffer_len > (ssize_t)sizeof(buffer)) {
|
|
|
|
logmsg("Buffer size %d too small for data size %d",
|
|
|
|
(int)sizeof(buffer), buffer_len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
logmsg("> %d bytes data, server => client", buffer_len);
|
2007-08-22 15:57:49 +02:00
|
|
|
|
2007-08-22 16:09:13 +02:00
|
|
|
if(buffer_len > (ssize_t)sizeof(buffer)) {
|
|
|
|
logmsg("ERROR: %d bytes of data does not fit within the %d "
|
|
|
|
"bytes buffer", buffer_len, sizeof(buffer));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-08-22 15:57:49 +02:00
|
|
|
/*
|
|
|
|
* To properly support huge data chunks, we need to repeat the call
|
|
|
|
* to read() until we're done or it fails.
|
|
|
|
*/
|
|
|
|
nread_stdin = 0;
|
|
|
|
do {
|
|
|
|
/* get data in the buffer at the correct position */
|
2007-08-30 20:26:19 +02:00
|
|
|
rc = read(fileno(stdin), &buffer[nread_stdin],
|
|
|
|
buffer_len - nread_stdin);
|
2007-08-22 15:57:49 +02:00
|
|
|
logmsg("read %d bytes", rc);
|
|
|
|
if(rc <= 0)
|
|
|
|
return FALSE;
|
|
|
|
nread_stdin += rc;
|
|
|
|
} while (nread_stdin < buffer_len);
|
|
|
|
|
2006-07-17 05:38:13 +02:00
|
|
|
lograw(buffer, buffer_len);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
if(*mode == PASSIVE_LISTEN) {
|
2005-05-07 10:55:37 +02:00
|
|
|
logmsg("*** We are disconnected!");
|
2005-04-18 08:57:44 +02:00
|
|
|
write(fileno(stdout), "DISC\n", 5);
|
|
|
|
}
|
2006-07-12 11:39:35 +02:00
|
|
|
else {
|
2005-04-18 08:57:44 +02:00
|
|
|
/* send away on the socket */
|
2006-07-17 05:38:13 +02:00
|
|
|
bytes_written = swrite(sockfd, buffer, buffer_len);
|
|
|
|
if(bytes_written != buffer_len) {
|
2006-07-13 20:50:51 +02:00
|
|
|
logmsg("Not all data was sent. Bytes to send: %d sent: %d",
|
2006-07-17 05:38:13 +02:00
|
|
|
buffer_len, bytes_written);
|
2006-07-12 11:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
else if(!memcmp("DISC", buffer, 4)) {
|
|
|
|
/* disconnect! */
|
|
|
|
write(fileno(stdout), "DISC\n", 5);
|
|
|
|
if(sockfd != CURL_SOCKET_BAD) {
|
|
|
|
logmsg("====> Client forcibly disconnected");
|
|
|
|
sclose(sockfd);
|
|
|
|
*sockfdp = CURL_SOCKET_BAD;
|
2005-05-07 15:52:07 +02:00
|
|
|
if(*mode == PASSIVE_CONNECT)
|
|
|
|
*mode = PASSIVE_LISTEN;
|
|
|
|
else
|
|
|
|
*mode = ACTIVE_DISCONNECT;
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
logmsg("attempt to close already dead connection");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2006-07-17 05:38:13 +02:00
|
|
|
else if(-1 == nread_stdin) {
|
|
|
|
logmsg("read %d from stdin, exiting", nread_stdin);
|
2005-05-07 15:52:07 +02:00
|
|
|
return FALSE;
|
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)) ) {
|
|
|
|
|
|
|
|
if(*mode == PASSIVE_LISTEN) {
|
|
|
|
/* there's no stream set up yet, this is an indication that there's a
|
|
|
|
client connecting. */
|
|
|
|
sockfd = accept(sockfd, NULL, NULL);
|
2006-07-17 05:38:13 +02:00
|
|
|
if(CURL_SOCKET_BAD == sockfd)
|
2007-02-19 03:03:58 +01:00
|
|
|
logmsg("accept() failed");
|
2005-04-18 08:57:44 +02:00
|
|
|
else {
|
|
|
|
logmsg("====> Client connect");
|
|
|
|
write(fileno(stdout), "CNCT\n", 5);
|
|
|
|
*sockfdp = sockfd; /* store the new socket */
|
|
|
|
*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
|
|
|
|
2006-07-17 05:38:13 +02:00
|
|
|
if(nread_socket <= 0) {
|
2005-04-18 08:57:44 +02:00
|
|
|
logmsg("====> Client disconnect");
|
|
|
|
write(fileno(stdout), "DISC\n", 5);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-08-22 15:57:49 +02:00
|
|
|
snprintf(data, sizeof(data), "DATA\n%04x\n", nread_socket);
|
2005-04-18 08:57:44 +02:00
|
|
|
write(fileno(stdout), data, 10);
|
2006-07-17 05:38:13 +02:00
|
|
|
write(fileno(stdout), buffer, nread_socket);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
2006-07-17 05:38:13 +02:00
|
|
|
logmsg("< %d bytes data, client => server", nread_socket);
|
|
|
|
lograw(buffer, nread_socket);
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
struct sockaddr_in me;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_in6 me6;
|
|
|
|
#endif /* ENABLE_IPV6 */
|
|
|
|
int flag = 1;
|
|
|
|
int rc;
|
2007-07-12 19:03:45 +02:00
|
|
|
int maxretr = 12;
|
|
|
|
int delay= 10;
|
2007-07-12 03:07:49 +02:00
|
|
|
|
|
|
|
rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(void *)&flag, sizeof(flag));
|
|
|
|
while ((rc < 0) && maxretr) {
|
|
|
|
maxretr--;
|
2007-07-12 19:03:45 +02:00
|
|
|
go_sleep(delay);
|
|
|
|
delay *= 2; /* double the sleep for next attempt */
|
2007-07-12 03:07:49 +02:00
|
|
|
rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(void *)&flag, sizeof(flag));
|
|
|
|
}
|
|
|
|
if (rc < 0) {
|
2005-05-25 14:04:24 +02:00
|
|
|
perror("setsockopt(SO_REUSEADDR)");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6) {
|
|
|
|
#endif
|
|
|
|
me.sin_family = AF_INET;
|
|
|
|
me.sin_addr.s_addr = INADDR_ANY;
|
2007-09-27 03:45:22 +02:00
|
|
|
me.sin_port = htons(*listenport);
|
2005-05-25 14:04:24 +02:00
|
|
|
rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memset(&me6, 0, sizeof(struct sockaddr_in6));
|
|
|
|
me6.sin6_family = AF_INET6;
|
|
|
|
me6.sin6_addr = in6addr_any;
|
2007-09-27 03:45:22 +02:00
|
|
|
me6.sin6_port = htons(*listenport);
|
2005-05-25 14:04:24 +02:00
|
|
|
rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_IPV6 */
|
|
|
|
if(rc < 0) {
|
|
|
|
perror("binding stream socket");
|
|
|
|
logmsg("Error binding socket");
|
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
|
|
|
|
2007-09-27 03:45:22 +02:00
|
|
|
if(!*listenport) {
|
2005-05-25 14:04:24 +02:00
|
|
|
/* The system picked a port number, now figure out which port we actually
|
|
|
|
got */
|
|
|
|
/* we succeeded to bind */
|
|
|
|
struct sockaddr_in add;
|
|
|
|
socklen_t socksize = sizeof(add);
|
|
|
|
|
|
|
|
if(getsockname(sock, (struct sockaddr *) &add,
|
|
|
|
&socksize)<0) {
|
2007-02-19 03:03:58 +01:00
|
|
|
logmsg("getsockname() failed with error: %d", SOCKERRNO);
|
2005-05-25 14:04:24 +02:00
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
2007-09-27 03:45:22 +02:00
|
|
|
*listenport = ntohs(add.sin_port);
|
2005-05-25 14:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* start accepting connections */
|
2006-07-19 17:28:30 +02:00
|
|
|
rc = listen(sock, 4);
|
|
|
|
if(0 != rc) {
|
2007-02-16 17:01:19 +01:00
|
|
|
logmsg("listen() failed with error: %d", SOCKERRNO);
|
2006-07-19 17:28:30 +02:00
|
|
|
sclose(sock);
|
|
|
|
return CURL_SOCKET_BAD;
|
|
|
|
}
|
2005-05-25 14:04:24 +02:00
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2007-09-27 03:45:22 +02:00
|
|
|
static curl_socket_t mksock(bool ipv6)
|
2005-05-25 14:04:24 +02:00
|
|
|
{
|
|
|
|
curl_socket_t sock;
|
|
|
|
#ifdef ENABLE_IPV6
|
2007-09-27 03:45:22 +02:00
|
|
|
if(!ipv6)
|
2005-05-26 00:12:57 +02:00
|
|
|
#else
|
2007-09-27 03:45:22 +02:00
|
|
|
(void)ipv6;
|
2005-05-25 14:04:24 +02:00
|
|
|
#endif
|
2005-05-26 00:12:57 +02:00
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
2005-05-25 14:04:24 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else
|
|
|
|
sock = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
#endif
|
|
|
|
|
2006-07-19 17:28:30 +02:00
|
|
|
if (CURL_SOCKET_BAD == sock) {
|
2005-05-25 14:04:24 +02:00
|
|
|
perror("opening stream socket");
|
|
|
|
logmsg("Error opening socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-18 08:57:44 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct sockaddr_in me;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_in6 me6;
|
|
|
|
#endif /* ENABLE_IPV6 */
|
2005-05-25 14:04:24 +02:00
|
|
|
curl_socket_t sock;
|
|
|
|
curl_socket_t msgsock;
|
2005-04-18 08:57:44 +02:00
|
|
|
FILE *pidfile;
|
|
|
|
char *pidname= (char *)".sockfilt.pid";
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
use_ipv6=TRUE;
|
|
|
|
#endif
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
else if(!strcmp("--ipv4", argv[arg])) {
|
|
|
|
/* for completeness, we support this option as well */
|
|
|
|
use_ipv6=FALSE;
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
else if(!strcmp("--port", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg) {
|
|
|
|
port = (unsigned short)atoi(argv[arg]);
|
|
|
|
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) {
|
|
|
|
connectport = (unsigned short)atoi(argv[arg]);
|
|
|
|
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"
|
|
|
|
" --logfile [file]\n"
|
|
|
|
" --pidfile [file]\n"
|
|
|
|
" --ipv4\n"
|
|
|
|
" --ipv6\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);
|
|
|
|
#else
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
#ifdef HAVE_SIGNAL
|
|
|
|
signal(SIGPIPE, sigpipe_handler);
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SIGINTERRUPT
|
|
|
|
siginterrupt(SIGPIPE, 1);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-05-25 14:04:24 +02:00
|
|
|
sock = mksock(use_ipv6);
|
2006-07-19 17:28:30 +02:00
|
|
|
if (CURL_SOCKET_BAD == sock) {
|
2007-02-16 17:01:19 +01:00
|
|
|
logmsg("Error opening socket: %d", SOCKERRNO);
|
2006-07-19 17:28:30 +02:00
|
|
|
return 1;
|
|
|
|
}
|
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
|
|
|
|
memset(&me, 0, sizeof(me));
|
|
|
|
me.sin_family = AF_INET;
|
|
|
|
me.sin_port = htons(connectport);
|
|
|
|
me.sin_addr.s_addr = INADDR_ANY;
|
2007-09-17 20:12:11 +02:00
|
|
|
if (!addr)
|
|
|
|
addr = "127.0.0.1";
|
|
|
|
Curl_inet_pton(AF_INET, addr, &me.sin_addr);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
rc = connect(sock, (struct sockaddr *) &me, sizeof(me));
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memset(&me6, 0, sizeof(me6));
|
|
|
|
me6.sin6_family = AF_INET6;
|
|
|
|
me6.sin6_port = htons(connectport);
|
2007-09-17 20:12:11 +02:00
|
|
|
if (!addr)
|
|
|
|
addr = "::1";
|
|
|
|
Curl_inet_pton(AF_INET6, addr, &me6.sin6_addr);
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
rc = connect(sock, (struct sockaddr *) &me6, sizeof(me6));
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_IPV6 */
|
|
|
|
if(rc) {
|
|
|
|
perror("connecting stream socket");
|
|
|
|
logmsg("Error connecting to port %d", port);
|
2006-07-19 17:28:30 +02:00
|
|
|
sclose(sock);
|
2005-05-07 15:52:07 +02:00
|
|
|
return 1;
|
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);
|
|
|
|
if(CURL_SOCKET_BAD == sock)
|
2005-05-07 15:52:07 +02:00
|
|
|
return 1;
|
2005-05-25 14:04:24 +02:00
|
|
|
msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
|
2005-04-18 08:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logmsg("Running IPv%d version",
|
|
|
|
(use_ipv6?6:4));
|
|
|
|
|
|
|
|
if(connectport)
|
|
|
|
logmsg("Connected to port %d", connectport);
|
|
|
|
else
|
|
|
|
logmsg("Listening on port %d", port);
|
|
|
|
|
2005-05-07 15:52:07 +02:00
|
|
|
pidfile = fopen(pidname, "w");
|
|
|
|
if(pidfile) {
|
|
|
|
int pid = (int)getpid();
|
|
|
|
fprintf(pidfile, "%d\n", pid);
|
|
|
|
fclose(pidfile);
|
|
|
|
logmsg("Wrote pid %d to %s", pid, pidname);
|
|
|
|
}
|
2006-07-19 17:28:30 +02:00
|
|
|
else {
|
2007-02-19 03:03:58 +01:00
|
|
|
error = ERRNO;
|
|
|
|
logmsg("fopen() failed with error: %d %s\n", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s\n", pidname);
|
|
|
|
logmsg("Couldn't write pid file\n");
|
2006-07-19 17:28:30 +02:00
|
|
|
sclose(sock);
|
|
|
|
return 1;
|
|
|
|
}
|
2005-05-07 15:52:07 +02:00
|
|
|
|
2005-05-25 14:04:24 +02:00
|
|
|
while(juggle(&msgsock, sock, &mode));
|
2005-04-18 08:57:44 +02:00
|
|
|
|
|
|
|
sclose(sock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|