18
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
* KIND, either express or implied.
21
* $Id: sockfilt.c,v 1.51 2008-03-05 18:27:31 yangtse Exp $
21
* $Id: sockfilt.c,v 1.55 2008-10-01 17:34:25 danf Exp $
22
22
***************************************************************************/
119
119
#define DEFAULT_LOGFILE "log/sockfilt.log"
122
const char *serverlogfile = (char *)DEFAULT_LOGFILE;
122
const char *serverlogfile = DEFAULT_LOGFILE;
124
bool verbose = FALSE;
125
bool use_ipv6 = FALSE;
126
unsigned short port = DEFAULT_PORT;
127
unsigned short connectport = 0; /* if non-zero, we activate this mode */
124
static bool verbose = FALSE;
126
static bool use_ipv6 = FALSE;
128
static const char *ipv_inuse = "IPv4";
129
static unsigned short port = DEFAULT_PORT;
130
static unsigned short connectport = 0; /* if non-zero, we activate this mode */
130
133
PASSIVE_LISTEN, /* as a server waiting for connections */
179
182
/* ignore SIGHUP signal */
180
183
if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
181
logmsg("cannot install SIGHUP handler: 5s", strerror(ERRNO));
184
logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
184
187
/* ignore SIGPIPE signal */
185
188
if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
186
logmsg("cannot install SIGPIPE handler: 5s", strerror(ERRNO));
189
logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
189
192
/* ignore SIGALRM signal */
190
193
if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
191
logmsg("cannot install SIGALRM handler: 5s", strerror(ERRNO));
194
logmsg("cannot install SIGALRM handler: %s", strerror(ERRNO));
194
197
/* handle SIGINT signal with our exit_signal_handler */
195
198
if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
196
logmsg("cannot install SIGINT handler: 5s", strerror(ERRNO));
199
logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
198
201
siginterrupt(SIGINT, 1);
201
204
/* handle SIGTERM signal with our exit_signal_handler */
202
205
if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
203
logmsg("cannot install SIGTERM handler: 5s", strerror(ERRNO));
206
logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
205
208
siginterrupt(SIGTERM, 1);
404
407
fd_set fds_write;
406
curl_socket_t sockfd;
409
curl_socket_t sockfd = CURL_SOCKET_BAD;
410
curl_socket_t maxfd = CURL_SOCKET_BAD;
409
412
ssize_t nread_socket;
410
413
ssize_t bytes_written;
411
414
ssize_t buffer_len;
414
417
/* 'buffer' is this excessively large only to be able to support things like
415
418
test 1003 which tests exceedingly large server response lines */
542
545
else if(!memcmp("PORT", buffer, 4)) {
543
546
/* Question asking us what PORT number we are listening to.
544
547
Replies to PORT with "IPv[num]/[port]" */
545
sprintf((char *)buffer, "IPv%d/%d\n", use_ipv6?6:4, (int)port);
548
sprintf((char *)buffer, "%s/%d\n", ipv_inuse, (int)port);
546
549
buffer_len = (ssize_t)strlen((char *)buffer);
547
550
snprintf(data, sizeof(data), "PORT\n%04x\n", buffer_len);
548
551
if(!write_stdout(data, 10))
566
569
buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
567
570
if (buffer_len > (ssize_t)sizeof(buffer)) {
568
logmsg("ERROR: Buffer size (%ld bytes) too small for data size "
569
"(%ld bytes)", (long)sizeof(buffer), (long)buffer_len);
571
logmsg("ERROR: Buffer size (%zu bytes) too small for data size "
572
"(%zd bytes)", sizeof(buffer), buffer_len);
572
logmsg("> %d bytes data, server => client", buffer_len);
575
logmsg("> %zd bytes data, server => client", buffer_len);
574
577
if(!read_stdin(buffer, buffer_len))
585
588
/* send away on the socket */
586
589
bytes_written = swrite(sockfd, buffer, buffer_len);
587
590
if(bytes_written != buffer_len) {
588
logmsg("Not all data was sent. Bytes to send: %d sent: %d",
591
logmsg("Not all data was sent. Bytes to send: %zd sent: %zd",
589
592
buffer_len, bytes_written);
650
653
if(!write_stdout(buffer, nread_socket))
653
logmsg("< %d bytes data, client => server", nread_socket);
656
logmsg("< %zd bytes data, client => server", nread_socket);
654
657
lograw(buffer, nread_socket);
806
809
else if(!strcmp("--ipv6", argv[arg])) {
807
810
#ifdef ENABLE_IPV6
812
816
else if(!strcmp("--ipv4", argv[arg])) {
813
817
/* for completeness, we support this option as well */
817
824
else if(!strcmp("--port", argv[arg])) {
906
913
#endif /* ENABLE_IPV6 */
908
915
error = SOCKERRNO;
909
logmsg("Error connecting to port %d: (%d) %s",
916
logmsg("Error connecting to port %hu: (%d) %s",
910
917
connectport, error, strerror(error));
911
918
goto sockfilt_cleanup;
921
928
msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
924
logmsg("Running IPv%d version",
931
logmsg("Running %s version", ipv_inuse);
928
logmsg("Connected to port %d", connectport);
934
logmsg("Connected to port %hu", connectport);
930
logmsg("Listening on port %d", port);
936
logmsg("Listening on port %hu", port);
932
938
wrotepidfile = write_pidfile(pidname);
933
939
if(!wrotepidfile)