1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
22 ***************************************************************************/
26 * 1. Accept a TCP connection on a custom port (ipv4 or ipv6), or connect
27 * to a given (localhost) port.
29 * 2. Get commands on STDIN. Pass data on to the TCP stream.
30 * Get data from TCP stream and pass on to STDOUT.
32 * This program is made to perform all the socket/stream/connection stuff for
33 * the test suite's (perl) FTP server. Previously the perl code did all of
34 * this by its own, but I decided to let this program do the socket layer
35 * because of several things:
37 * o We want the perl code to work with rather old perl installations, thus
38 * we cannot use recent perl modules or features.
40 * o We want IPv6 support for systems that provide it, and doing optional IPv6
41 * support in perl seems if not impossible so at least awkward.
43 * o We want FTP-SSL support, which means that a connection that starts with
44 * plain sockets needs to be able to "go SSL" in the midst. This would also
45 * require some nasty perl stuff I'd rather avoid.
47 * (Source originally based on sws.c)
49 #include "setup.h" /* portability help from the lib directory */
59 #include <sys/types.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
67 #ifdef HAVE_NETINET_IN_H
68 #include <netinet/in.h>
70 #ifdef _XOPEN_SOURCE_EXTENDED
71 /* This define is "almost" required to build on HPUX 11 */
72 #include <arpa/inet.h>
78 #define ENABLE_CURLX_PRINTF
79 /* make the curlx header define all printf() functions to use the curlx_*
81 #include "curlx.h" /* from the private lib dir */
83 #include "inet_pton.h"
86 /* include memdebug.h last */
89 #define DEFAULT_PORT 8999
91 #ifndef DEFAULT_LOGFILE
92 #define DEFAULT_LOGFILE "log/sockfilt.log"
96 static volatile int sigpipe; /* Why? It's not used */
99 const char *serverlogfile = (char *)DEFAULT_LOGFILE;
101 static void lograw(unsigned char *buffer, ssize_t len)
105 unsigned char *ptr = buffer;
109 for(i=0; i<len; i++) {
112 sprintf(optr, "\\n");
117 sprintf(optr, "\\r");
122 sprintf(optr, "%c", (ISGRAPH(ptr[i]) || ptr[i]==0x20) ?ptr[i]:'.');
129 logmsg("'%s'", data);
135 logmsg("'%s'", data);
139 static void sigpipe_handler(int sig)
141 (void)sig; /* prevent warning */
147 unsigned short port = DEFAULT_PORT;
148 unsigned short connectport = 0; /* if non-zero, we activate this mode */
151 PASSIVE_LISTEN, /* as a server waiting for connections */
152 PASSIVE_CONNECT, /* as a server, connected to a client */
153 ACTIVE, /* as a client, connected to a server */
154 ACTIVE_DISCONNECT /* as a client, disconnected from server */
158 sockfdp is a pointer to an established stream or CURL_SOCKET_BAD
160 if sockfd is CURL_SOCKET_BAD, listendfd is a listening socket we must
163 static int juggle(curl_socket_t *sockfdp,
164 curl_socket_t listenfd,
167 struct timeval timeout;
171 curl_socket_t sockfd;
175 ssize_t nread_socket;
176 ssize_t bytes_written;
178 unsigned char buffer[256]; /* FIX: bigger buffer */
181 timeout.tv_sec = 120;
188 FD_SET(fileno(stdin), &fds_read);
196 /* there's always a socket to wait for */
197 FD_SET(sockfd, &fds_read);
201 case PASSIVE_CONNECT:
204 if(CURL_SOCKET_BAD == sockfd) {
205 /* eeek, we are supposedly connected and then this cannot be -1 ! */
206 logmsg("socket is -1! on %s:%d", __FILE__, __LINE__);
207 maxfd = 0; /* stdin */
210 /* there's always a socket to wait for */
211 FD_SET(sockfd, &fds_read);
219 /* sockfd turns CURL_SOCKET_BAD when our connection has been closed */
220 if(CURL_SOCKET_BAD != sockfd) {
221 FD_SET(sockfd, &fds_read);
225 logmsg("No socket to read on");
230 case ACTIVE_DISCONNECT:
232 logmsg("disconnected, no socket to read on");
234 sockfd = CURL_SOCKET_BAD;
237 } /* switch(*mode) */
240 rc = select(maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
241 } while((rc == -1) && (SOCKERRNO == EINTR));
247 case 0: /* timeout! */
252 if(FD_ISSET(fileno(stdin), &fds_read)) {
253 /* read from stdin, commands/data to be dealt with and possibly passed on
258 4 letter command + LF [mandatory]
260 4-digit hexadecimal data length + LF [if the command takes data]
261 data [the data being as long as set above]
265 DATA - plain pass-thru data
267 nread_stdin = read(fileno(stdin), buffer, 5);
268 if(5 == nread_stdin) {
270 logmsg("Received %c%c%c%c (on stdin)",
271 buffer[0], buffer[1], buffer[2], buffer[3] );
273 if(!memcmp("PING", buffer, 4)) {
274 /* send reply on stdout, just proving we are alive */
275 write(fileno(stdout), "PONG\n", 5);
278 else if(!memcmp("PORT", buffer, 4)) {
279 /* Question asking us what PORT number we are listening to.
280 Replies to PORT with "IPv[num]/[port]" */
281 sprintf((char *)buffer, "IPv%d/%d\n", use_ipv6?6:4, (int)port);
282 buffer_len = (ssize_t)strlen((char *)buffer);
283 sprintf(data, "PORT\n%04x\n", buffer_len);
284 write(fileno(stdout), data, 10);
285 write(fileno(stdout), buffer, buffer_len);
287 else if(!memcmp("QUIT", buffer, 4)) {
292 else if(!memcmp("DATA", buffer, 4)) {
293 /* data IN => data OUT */
295 if(5 != read(fileno(stdin), buffer, 5))
299 buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
300 if (buffer_len > (ssize_t)sizeof(buffer)) {
301 logmsg("Buffer size %d too small for data size %d",
302 (int)sizeof(buffer), buffer_len);
305 nread_stdin = read(fileno(stdin), buffer, buffer_len);
306 if(nread_stdin != buffer_len)
309 logmsg("> %d bytes data, server => client", buffer_len);
310 lograw(buffer, buffer_len);
312 if(*mode == PASSIVE_LISTEN) {
313 logmsg("*** We are disconnected!");
314 write(fileno(stdout), "DISC\n", 5);
317 /* send away on the socket */
318 bytes_written = swrite(sockfd, buffer, buffer_len);
319 if(bytes_written != buffer_len) {
320 logmsg("Not all data was sent. Bytes to send: %d sent: %d",
321 buffer_len, bytes_written);
325 else if(!memcmp("DISC", buffer, 4)) {
327 write(fileno(stdout), "DISC\n", 5);
328 if(sockfd != CURL_SOCKET_BAD) {
329 logmsg("====> Client forcibly disconnected");
331 *sockfdp = CURL_SOCKET_BAD;
332 if(*mode == PASSIVE_CONNECT)
333 *mode = PASSIVE_LISTEN;
335 *mode = ACTIVE_DISCONNECT;
338 logmsg("attempt to close already dead connection");
342 else if(-1 == nread_stdin) {
343 logmsg("read %d from stdin, exiting", nread_stdin);
349 if((sockfd != CURL_SOCKET_BAD) && (FD_ISSET(sockfd, &fds_read)) ) {
351 if(*mode == PASSIVE_LISTEN) {
352 /* there's no stream set up yet, this is an indication that there's a
353 client connecting. */
354 sockfd = accept(sockfd, NULL, NULL);
355 if(CURL_SOCKET_BAD == sockfd)
356 logmsg("accept() failed\n");
358 logmsg("====> Client connect");
359 write(fileno(stdout), "CNCT\n", 5);
360 *sockfdp = sockfd; /* store the new socket */
361 *mode = PASSIVE_CONNECT; /* we have connected */
366 /* read from socket, pass on data to stdout */
367 nread_socket = sread(sockfd, buffer, sizeof(buffer));
369 if(nread_socket <= 0) {
370 logmsg("====> Client disconnect");
371 write(fileno(stdout), "DISC\n", 5);
373 *sockfdp = CURL_SOCKET_BAD;
374 if(*mode == PASSIVE_CONNECT)
375 *mode = PASSIVE_LISTEN;
377 *mode = ACTIVE_DISCONNECT;
381 sprintf(data, "DATA\n%04x\n", nread_socket);
382 write(fileno(stdout), data, 10);
383 write(fileno(stdout), buffer, nread_socket);
385 logmsg("< %d bytes data, client => server", nread_socket);
386 lograw(buffer, nread_socket);
392 static curl_socket_t sockdaemon(curl_socket_t sock,
393 unsigned short *port)
395 /* passive daemon style */
396 struct sockaddr_in me;
398 struct sockaddr_in6 me6;
399 #endif /* ENABLE_IPV6 */
404 (sock, SOL_SOCKET, SO_REUSEADDR, (void *)&flag,
406 perror("setsockopt(SO_REUSEADDR)");
412 me.sin_family = AF_INET;
413 me.sin_addr.s_addr = INADDR_ANY;
414 me.sin_port = htons(*port);
415 rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
419 memset(&me6, 0, sizeof(struct sockaddr_in6));
420 me6.sin6_family = AF_INET6;
421 me6.sin6_addr = in6addr_any;
422 me6.sin6_port = htons(*port);
423 rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
425 #endif /* ENABLE_IPV6 */
427 perror("binding stream socket");
428 logmsg("Error binding socket");
429 return CURL_SOCKET_BAD;
433 /* The system picked a port number, now figure out which port we actually
435 /* we succeeded to bind */
436 struct sockaddr_in add;
437 socklen_t socksize = sizeof(add);
439 if(getsockname(sock, (struct sockaddr *) &add,
441 fprintf(stderr, "getsockname() failed");
442 return CURL_SOCKET_BAD;
444 *port = ntohs(add.sin_port);
447 /* start accepting connections */
448 rc = listen(sock, 4);
450 logmsg("listen() failed with error: %d", SOCKERRNO);
452 return CURL_SOCKET_BAD;
458 static curl_socket_t mksock(bool use_ipv6)
466 sock = socket(AF_INET, SOCK_STREAM, 0);
469 sock = socket(AF_INET6, SOCK_STREAM, 0);
472 if (CURL_SOCKET_BAD == sock) {
473 perror("opening stream socket");
474 logmsg("Error opening socket");
481 int main(int argc, char *argv[])
483 struct sockaddr_in me;
485 struct sockaddr_in6 me6;
486 #endif /* ENABLE_IPV6 */
488 curl_socket_t msgsock;
490 char *pidname= (char *)".sockfilt.pid";
493 enum sockmode mode = PASSIVE_LISTEN; /* default */
496 if(!strcmp("--version", argv[arg])) {
497 printf("sockfilt IPv4%s\n",
506 else if(!strcmp("--pidfile", argv[arg])) {
509 pidname = argv[arg++];
511 else if(!strcmp("--logfile", argv[arg])) {
514 serverlogfile = argv[arg++];
516 else if(!strcmp("--ipv6", argv[arg])) {
522 else if(!strcmp("--ipv4", argv[arg])) {
523 /* for completeness, we support this option as well */
527 else if(!strcmp("--port", argv[arg])) {
530 port = (unsigned short)atoi(argv[arg]);
534 else if(!strcmp("--connect", argv[arg])) {
535 /* Asked to actively connect to the specified local port instead of
536 doing a passive server-style listening. */
539 connectport = (unsigned short)atoi(argv[arg]);
544 puts("Usage: sockfilt [option]\n"
546 " --logfile [file]\n"
547 " --pidfile [file]\n"
557 atexit(win32_cleanup);
562 signal(SIGPIPE, sigpipe_handler);
564 #ifdef HAVE_SIGINTERRUPT
565 siginterrupt(SIGPIPE, 1);
571 sock = mksock(use_ipv6);
572 if (CURL_SOCKET_BAD == sock) {
573 logmsg("Error opening socket: %d", SOCKERRNO);
578 /* Active mode, we should connect to the given port number */
583 memset(&me, 0, sizeof(me));
584 me.sin_family = AF_INET;
585 me.sin_port = htons(connectport);
586 me.sin_addr.s_addr = INADDR_ANY;
587 Curl_inet_pton(AF_INET, "127.0.0.1", &me.sin_addr);
589 rc = connect(sock, (struct sockaddr *) &me, sizeof(me));
593 memset(&me6, 0, sizeof(me6));
594 me6.sin6_family = AF_INET6;
595 me6.sin6_port = htons(connectport);
596 Curl_inet_pton(AF_INET6, "::1", &me6.sin6_addr);
598 rc = connect(sock, (struct sockaddr *) &me6, sizeof(me6));
600 #endif /* ENABLE_IPV6 */
602 perror("connecting stream socket");
603 logmsg("Error connecting to port %d", port);
607 logmsg("====> Client connect");
608 msgsock = sock; /* use this as stream */
611 /* passive daemon style */
612 sock = sockdaemon(sock, &port);
613 if(CURL_SOCKET_BAD == sock)
615 msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
618 logmsg("Running IPv%d version",
622 logmsg("Connected to port %d", connectport);
624 logmsg("Listening on port %d", port);
626 pidfile = fopen(pidname, "w");
628 int pid = (int)getpid();
629 fprintf(pidfile, "%d\n", pid);
631 logmsg("Wrote pid %d to %s", pid, pidname);
634 fprintf(stderr, "Couldn't write pid file\n");
639 while(juggle(&msgsock, sock, &mode));