header inclusion cleanup
[platform/upstream/curl.git] / tests / server / sockfilt.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23
24 /* Purpose
25  *
26  * 1. Accept a TCP connection on a custom port (ipv4 or ipv6), or connect
27  *    to a given (localhost) port.
28  *
29  * 2. Get commands on STDIN. Pass data on to the TCP stream.
30  *    Get data from TCP stream and pass on to STDOUT.
31  *
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:
36  *
37  * o We want the perl code to work with rather old perl installations, thus
38  *   we cannot use recent perl modules or features.
39  *
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.
42  *
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.
46  *
47  * (Source originally based on sws.c)
48  */
49 #include "setup.h" /* portability help from the lib directory */
50
51 #ifdef HAVE_SIGNAL_H
52 #include <signal.h>
53 #endif
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef HAVE_NETINET_IN_H
61 #include <netinet/in.h>
62 #endif
63 #ifdef _XOPEN_SOURCE_EXTENDED
64 /* This define is "almost" required to build on HPUX 11 */
65 #include <arpa/inet.h>
66 #endif
67 #ifdef HAVE_NETDB_H
68 #include <netdb.h>
69 #endif
70
71 #define ENABLE_CURLX_PRINTF
72 /* make the curlx header define all printf() functions to use the curlx_*
73    versions instead */
74 #include "curlx.h" /* from the private lib dir */
75 #include "getpart.h"
76 #include "inet_pton.h"
77 #include "util.h"
78
79 /* include memdebug.h last */
80 #include "memdebug.h"
81
82 #define DEFAULT_PORT 8999
83
84 #ifndef DEFAULT_LOGFILE
85 #define DEFAULT_LOGFILE "log/sockfilt.log"
86 #endif
87
88 #ifdef SIGPIPE
89 static volatile int sigpipe;  /* Why? It's not used */
90 #endif
91
92 const char *serverlogfile = (char *)DEFAULT_LOGFILE;
93
94 bool verbose = FALSE;
95 bool use_ipv6 = FALSE;
96 unsigned short port = DEFAULT_PORT;
97 unsigned short connectport = 0; /* if non-zero, we activate this mode */
98
99 enum sockmode {
100   PASSIVE_LISTEN,    /* as a server waiting for connections */
101   PASSIVE_CONNECT,   /* as a server, connected to a client */
102   ACTIVE,            /* as a client, connected to a server */
103   ACTIVE_DISCONNECT  /* as a client, disconnected from server */
104 };
105
106 /*
107  * fullread is a wrapper around the read() function. This will repeat the call
108  * to read() until it actually has read the complete number of bytes indicated
109  * in nbytes or it fails with a condition that cannot be handled with a simple
110  * retry of the read call.
111  */
112
113 static ssize_t fullread(int filedes, void *buffer, size_t nbytes)
114 {
115   int error;
116   ssize_t rc;
117   ssize_t nread = 0;
118
119   do {
120     rc = read(filedes, (unsigned char *)buffer + nread, nbytes - nread);
121
122     if(rc < 0) {
123       error = ERRNO;
124       if((error == EINTR) || (error == EAGAIN))
125         continue;
126       logmsg("unrecoverable read() failure: %s", strerror(error));
127       return -1;
128     }
129
130     if(rc == 0) {
131       logmsg("got 0 reading from stdin");
132       return 0;
133     }
134
135     nread += rc;
136
137   } while((size_t)nread < nbytes);
138
139   if(verbose)
140     logmsg("read %ld bytes", (long)nread);
141
142   return nread;
143 }
144
145 /*
146  * fullwrite is a wrapper around the write() function. This will repeat the
147  * call to write() until it actually has written the complete number of bytes
148  * indicated in nbytes or it fails with a condition that cannot be handled
149  * with a simple retry of the write call.
150  */
151
152 static ssize_t fullwrite(int filedes, const void *buffer, size_t nbytes)
153 {
154   int error;
155   ssize_t wc;
156   ssize_t nwrite = 0;
157
158   do {
159     wc = write(filedes, (unsigned char *)buffer + nwrite, nbytes - nwrite);
160
161     if(wc < 0) {
162       error = ERRNO;
163       if((error == EINTR) || (error == EAGAIN))
164         continue;
165       logmsg("unrecoverable write() failure: %s", strerror(error));
166       return -1;
167     }
168
169     if(wc == 0) {
170       logmsg("put 0 writing to stdout");
171       return 0;
172     }
173
174     nwrite += wc;
175
176   } while((size_t)nwrite < nbytes);
177
178   if(verbose)
179     logmsg("wrote %ld bytes", (long)nwrite);
180
181   return nwrite;
182 }
183
184 /*
185  * read_stdin tries to read from stdin nbytes into the given buffer. This is a
186  * blocking function that will only return TRUE when nbytes have actually been
187  * read or FALSE when an unrecoverable error has been detected. Failure of this
188  * function is an indication that the sockfilt process should terminate.
189  */
190
191 static bool read_stdin(void *buffer, size_t nbytes)
192 {
193   ssize_t nread = fullread(fileno(stdin), buffer, nbytes);
194   if(nread != (ssize_t)nbytes) {
195     logmsg("exiting...");
196     return FALSE;
197   }
198   return TRUE;
199 }
200
201 /*
202  * write_stdout tries to write to stdio nbytes from the given buffer. This is a
203  * blocking function that will only return TRUE when nbytes have actually been
204  * written or FALSE when an unrecoverable error has been detected. Failure of
205  * this function is an indication that the sockfilt process should terminate.
206  */
207
208 static bool write_stdout(const void *buffer, size_t nbytes)
209 {
210   ssize_t nwrite = fullwrite(fileno(stdout), buffer, nbytes);
211   if(nwrite != (ssize_t)nbytes) {
212     logmsg("exiting...");
213     return FALSE;
214   }
215   return TRUE;
216 }
217
218 static void lograw(unsigned char *buffer, ssize_t len)
219 {
220   char data[120];
221   ssize_t i;
222   unsigned char *ptr = buffer;
223   char *optr = data;
224   ssize_t width=0;
225
226   for(i=0; i<len; i++) {
227     switch(ptr[i]) {
228     case '\n':
229       sprintf(optr, "\\n");
230       width += 2;
231       optr += 2;
232       break;
233     case '\r':
234       sprintf(optr, "\\r");
235       width += 2;
236       optr += 2;
237       break;
238     default:
239       sprintf(optr, "%c", (ISGRAPH(ptr[i]) || ptr[i]==0x20) ?ptr[i]:'.');
240       width++;
241       optr++;
242       break;
243     }
244
245     if(width>60) {
246       logmsg("'%s'", data);
247       width = 0;
248       optr = data;
249     }
250   }
251   if(width)
252     logmsg("'%s'", data);
253 }
254
255 #ifdef SIGPIPE
256 static void sigpipe_handler(int sig)
257 {
258   (void)sig; /* prevent warning */
259   sigpipe = 1;
260 }
261 #endif
262
263 /*
264   sockfdp is a pointer to an established stream or CURL_SOCKET_BAD
265
266   if sockfd is CURL_SOCKET_BAD, listendfd is a listening socket we must
267   accept()
268 */
269 static bool juggle(curl_socket_t *sockfdp,
270                    curl_socket_t listenfd,
271                    enum sockmode *mode)
272 {
273   struct timeval timeout;
274   fd_set fds_read;
275   fd_set fds_write;
276   fd_set fds_err;
277   curl_socket_t sockfd;
278   curl_socket_t maxfd;
279   ssize_t rc;
280   ssize_t nread_socket;
281   ssize_t bytes_written;
282   ssize_t buffer_len;
283
284  /* 'buffer' is this excessively large only to be able to support things like
285     test 1003 which tests exceedingly large server response lines */
286   unsigned char buffer[17010];
287   char data[16];
288
289 #ifdef HAVE_GETPPID
290   /* As a last resort, quit if sockfilt process becomes orphan. Just in case
291      parent ftpserver process has died without killing its sockfilt children */
292   if(getppid() <= 1) {
293     logmsg("process becomes orphan, exiting");
294     return FALSE;
295   }
296 #endif
297
298   timeout.tv_sec = 120;
299   timeout.tv_usec = 0;
300
301   FD_ZERO(&fds_read);
302   FD_ZERO(&fds_write);
303   FD_ZERO(&fds_err);
304
305   FD_SET(fileno(stdin), &fds_read);
306
307   switch(*mode) {
308
309   case PASSIVE_LISTEN:
310
311     /* server mode */
312     sockfd = listenfd;
313     /* there's always a socket to wait for */
314     FD_SET(sockfd, &fds_read);
315     maxfd = sockfd;
316     break;
317
318   case PASSIVE_CONNECT:
319
320     sockfd = *sockfdp;
321     if(CURL_SOCKET_BAD == sockfd) {
322       /* eeek, we are supposedly connected and then this cannot be -1 ! */
323       logmsg("socket is -1! on %s:%d", __FILE__, __LINE__);
324       maxfd = 0; /* stdin */
325     }
326     else {
327       /* there's always a socket to wait for */
328       FD_SET(sockfd, &fds_read);
329       maxfd = sockfd;
330     }
331     break;
332
333   case ACTIVE:
334
335     sockfd = *sockfdp;
336     /* sockfd turns CURL_SOCKET_BAD when our connection has been closed */
337     if(CURL_SOCKET_BAD != sockfd) {
338       FD_SET(sockfd, &fds_read);
339       maxfd = sockfd;
340     }
341     else {
342       logmsg("No socket to read on");
343       maxfd = 0;
344     }
345     break;
346
347   case ACTIVE_DISCONNECT:
348
349     logmsg("disconnected, no socket to read on");
350     maxfd = 0;
351     sockfd = CURL_SOCKET_BAD;
352     break;
353
354   } /* switch(*mode) */
355
356
357   do {
358
359     rc = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
360
361   } while((rc == -1) && (SOCKERRNO == EINTR));
362
363   if(rc < 0)
364     return FALSE;
365
366   if(rc == 0)
367     /* timeout */
368     return TRUE;
369
370
371   if(FD_ISSET(fileno(stdin), &fds_read)) {
372     /* read from stdin, commands/data to be dealt with and possibly passed on
373        to the socket
374
375        protocol:
376
377        4 letter command + LF [mandatory]
378
379        4-digit hexadecimal data length + LF [if the command takes data]
380        data                       [the data being as long as set above]
381
382        Commands:
383
384        DATA - plain pass-thru data
385     */
386
387     if(!read_stdin(buffer, 5))
388       return FALSE;
389
390     logmsg("Received %c%c%c%c (on stdin)",
391            buffer[0], buffer[1], buffer[2], buffer[3] );
392
393     if(!memcmp("PING", buffer, 4)) {
394       /* send reply on stdout, just proving we are alive */
395       if(!write_stdout("PONG\n", 5))
396         return FALSE;
397     }
398
399     else if(!memcmp("PORT", buffer, 4)) {
400       /* Question asking us what PORT number we are listening to.
401          Replies to PORT with "IPv[num]/[port]" */
402       sprintf((char *)buffer, "IPv%d/%d\n", use_ipv6?6:4, (int)port);
403       buffer_len = (ssize_t)strlen((char *)buffer);
404       snprintf(data, sizeof(data), "PORT\n%04x\n", buffer_len);
405       if(!write_stdout(data, 10))
406         return FALSE;
407       if(!write_stdout(buffer, buffer_len))
408         return FALSE;
409     }
410     else if(!memcmp("QUIT", buffer, 4)) {
411       /* just die */
412       logmsg("quits");
413       return FALSE;
414     }
415     else if(!memcmp("DATA", buffer, 4)) {
416       /* data IN => data OUT */
417
418       if(!read_stdin(buffer, 5))
419         return FALSE;
420
421       buffer[5] = '\0';
422
423       buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
424       if (buffer_len > (ssize_t)sizeof(buffer)) {
425         logmsg("ERROR: Buffer size (%ld bytes) too small for data size "
426                "(%ld bytes)", (long)sizeof(buffer), (long)buffer_len);
427         return FALSE;
428       }
429       logmsg("> %d bytes data, server => client", buffer_len);
430
431       if(!read_stdin(buffer, buffer_len))
432         return FALSE;
433
434       lograw(buffer, buffer_len);
435
436       if(*mode == PASSIVE_LISTEN) {
437         logmsg("*** We are disconnected!");
438         if(!write_stdout("DISC\n", 5))
439           return FALSE;
440       }
441       else {
442         /* send away on the socket */
443         bytes_written = swrite(sockfd, buffer, buffer_len);
444         if(bytes_written != buffer_len) {
445           logmsg("Not all data was sent. Bytes to send: %d sent: %d", 
446                  buffer_len, bytes_written);
447         }
448       }
449     }
450     else if(!memcmp("DISC", buffer, 4)) {
451       /* disconnect! */
452       if(!write_stdout("DISC\n", 5))
453         return FALSE;
454       if(sockfd != CURL_SOCKET_BAD) {
455         logmsg("====> Client forcibly disconnected");
456         sclose(sockfd);
457         *sockfdp = CURL_SOCKET_BAD;
458         if(*mode == PASSIVE_CONNECT)
459           *mode = PASSIVE_LISTEN;
460         else
461           *mode = ACTIVE_DISCONNECT;
462       }
463       else
464         logmsg("attempt to close already dead connection");
465       return TRUE;
466     }
467   }
468
469
470   if((sockfd != CURL_SOCKET_BAD) && (FD_ISSET(sockfd, &fds_read)) ) {
471
472     if(*mode == PASSIVE_LISTEN) {
473       /* there's no stream set up yet, this is an indication that there's a
474          client connecting. */
475       sockfd = accept(sockfd, NULL, NULL);
476       if(CURL_SOCKET_BAD == sockfd)
477         logmsg("accept() failed");
478       else {
479         logmsg("====> Client connect");
480         if(!write_stdout("CNCT\n", 5))
481           return FALSE;
482         *sockfdp = sockfd; /* store the new socket */
483         *mode = PASSIVE_CONNECT; /* we have connected */
484       }
485       return TRUE;
486     }
487
488     /* read from socket, pass on data to stdout */
489     nread_socket = sread(sockfd, buffer, sizeof(buffer));
490
491     if(nread_socket <= 0) {
492       logmsg("====> Client disconnect");
493       if(!write_stdout("DISC\n", 5))
494         return FALSE;
495       sclose(sockfd);
496       *sockfdp = CURL_SOCKET_BAD;
497       if(*mode == PASSIVE_CONNECT)
498         *mode = PASSIVE_LISTEN;
499       else
500         *mode = ACTIVE_DISCONNECT;
501       return TRUE;
502     }
503
504     snprintf(data, sizeof(data), "DATA\n%04x\n", nread_socket);
505     if(!write_stdout(data, 10))
506       return FALSE;
507     if(!write_stdout(buffer, nread_socket))
508       return FALSE;
509
510     logmsg("< %d bytes data, client => server", nread_socket);
511     lograw(buffer, nread_socket);
512   }
513
514   return TRUE;
515 }
516
517 static curl_socket_t sockdaemon(curl_socket_t sock,
518                                 unsigned short *listenport)
519 {
520   /* passive daemon style */
521   struct sockaddr_in me;
522 #ifdef ENABLE_IPV6
523   struct sockaddr_in6 me6;
524 #endif /* ENABLE_IPV6 */
525   int flag = 1;
526   int rc;
527   int totdelay = 0;
528   int maxretr = 10;
529   int delay= 20;
530   int attempt = 0;
531   int error = 0;
532
533   do {
534     attempt++;
535     rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
536          (void *)&flag, sizeof(flag));
537     if(rc) {
538       error = SOCKERRNO;
539       if(maxretr) {
540         rc = wait_ms(delay);
541         if(rc) {
542           /* should not happen */
543           error = SOCKERRNO;
544           logmsg("wait_ms() failed: (%d) %s", error, strerror(error));
545           sclose(sock);
546           return CURL_SOCKET_BAD;
547         }
548         totdelay += delay;
549         delay *= 2; /* double the sleep for next attempt */
550       }
551     }
552   } while(rc && maxretr--);
553
554   if(rc) {
555     logmsg("setsockopt(SO_REUSEADDR) failed %d times in %d ms. Error: (%d) %s",
556            attempt, totdelay, error, strerror(error));
557     logmsg("Continuing anyway...");
558   }
559
560 #ifdef ENABLE_IPV6
561   if(!use_ipv6) {
562 #endif
563     memset(&me, 0, sizeof(me));
564     me.sin_family = AF_INET;
565     me.sin_addr.s_addr = INADDR_ANY;
566     me.sin_port = htons(*listenport);
567     rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
568 #ifdef ENABLE_IPV6
569   }
570   else {
571     memset(&me6, 0, sizeof(me6));
572     me6.sin6_family = AF_INET6;
573     me6.sin6_addr = in6addr_any;
574     me6.sin6_port = htons(*listenport);
575     rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
576   }
577 #endif /* ENABLE_IPV6 */
578   if(rc) {
579     error = SOCKERRNO;
580     logmsg("Error binding socket: (%d) %s", error, strerror(error));
581     sclose(sock);
582     return CURL_SOCKET_BAD;
583   }
584
585   if(!*listenport) {
586     /* The system picked a port number, now figure out which port we actually
587        got */
588     /* we succeeded to bind */
589     struct sockaddr_in add;
590     socklen_t socksize = sizeof(add);
591
592     if(getsockname(sock, (struct sockaddr *) &add,
593                    &socksize)<0) {
594       error = SOCKERRNO;
595       logmsg("getsockname() failed with error: (%d) %s",
596              error, strerror(error));
597       sclose(sock);
598       return CURL_SOCKET_BAD;
599     }
600     *listenport = ntohs(add.sin_port);
601   }
602
603   /* start accepting connections */
604   rc = listen(sock, 5);
605   if(0 != rc) {
606     error = SOCKERRNO;
607     logmsg("listen() failed with error: (%d) %s",
608            error, strerror(error));
609     sclose(sock);
610     return CURL_SOCKET_BAD;
611   }
612
613   return sock;
614 }
615
616
617 int main(int argc, char *argv[])
618 {
619   struct sockaddr_in me;
620 #ifdef ENABLE_IPV6
621   struct sockaddr_in6 me6;
622 #endif /* ENABLE_IPV6 */
623   curl_socket_t sock;
624   curl_socket_t msgsock;
625   char *pidname= (char *)".sockfilt.pid";
626   int rc;
627   int error;
628   int arg=1;
629   enum sockmode mode = PASSIVE_LISTEN; /* default */
630   const char *addr = NULL;
631
632   while(argc>arg) {
633     if(!strcmp("--version", argv[arg])) {
634       printf("sockfilt IPv4%s\n",
635 #ifdef ENABLE_IPV6
636              "/IPv6"
637 #else
638              ""
639 #endif
640              );
641       return 0;
642     }
643     else if(!strcmp("--verbose", argv[arg])) {
644       verbose = TRUE;
645       arg++;
646     }
647     else if(!strcmp("--pidfile", argv[arg])) {
648       arg++;
649       if(argc>arg)
650         pidname = argv[arg++];
651     }
652     else if(!strcmp("--logfile", argv[arg])) {
653       arg++;
654       if(argc>arg)
655         serverlogfile = argv[arg++];
656     }
657     else if(!strcmp("--ipv6", argv[arg])) {
658 #ifdef ENABLE_IPV6
659       use_ipv6=TRUE;
660 #endif
661       arg++;
662     }
663     else if(!strcmp("--ipv4", argv[arg])) {
664       /* for completeness, we support this option as well */
665       use_ipv6=FALSE;
666       arg++;
667     }
668     else if(!strcmp("--port", argv[arg])) {
669       arg++;
670       if(argc>arg) {
671         port = (unsigned short)atoi(argv[arg]);
672         arg++;
673       }
674     }
675     else if(!strcmp("--connect", argv[arg])) {
676       /* Asked to actively connect to the specified local port instead of
677          doing a passive server-style listening. */
678       arg++;
679       if(argc>arg) {
680         connectport = (unsigned short)atoi(argv[arg]);
681         arg++;
682       }
683     }
684     else if(!strcmp("--addr", argv[arg])) {
685       /* Set an IP address to use with --connect; otherwise use localhost */
686       arg++;
687       if(argc>arg) {
688         addr = argv[arg];
689         arg++;
690       }
691     }
692     else {
693       puts("Usage: sockfilt [option]\n"
694            " --version\n"
695            " --verbose\n"
696            " --logfile [file]\n"
697            " --pidfile [file]\n"
698            " --ipv4\n"
699            " --ipv6\n"
700            " --port [port]\n"
701            " --connect [port]\n"
702            " --addr [address]");
703       return 0;
704     }
705   }
706
707 #ifdef WIN32
708   win32_init();
709   atexit(win32_cleanup);
710 #else
711
712 #ifdef SIGPIPE
713 #ifdef HAVE_SIGNAL
714   signal(SIGPIPE, sigpipe_handler);
715 #endif
716 #ifdef HAVE_SIGINTERRUPT
717   siginterrupt(SIGPIPE, 1);
718 #endif
719 #endif
720 #endif
721
722 #ifdef ENABLE_IPV6
723   if(!use_ipv6)
724 #endif
725     sock = socket(AF_INET, SOCK_STREAM, 0);
726 #ifdef ENABLE_IPV6
727   else
728     sock = socket(AF_INET6, SOCK_STREAM, 0);
729 #endif
730
731   if(CURL_SOCKET_BAD == sock) {
732     error = SOCKERRNO;
733     logmsg("Error creating socket: (%d) %s",
734            error, strerror(error));
735     return 1;
736   }
737
738   if(connectport) {
739     /* Active mode, we should connect to the given port number */
740     mode = ACTIVE;
741 #ifdef ENABLE_IPV6
742     if(!use_ipv6) {
743 #endif
744       memset(&me, 0, sizeof(me));
745       me.sin_family = AF_INET;
746       me.sin_port = htons(connectport);
747       me.sin_addr.s_addr = INADDR_ANY;
748       if (!addr)
749         addr = "127.0.0.1";
750       Curl_inet_pton(AF_INET, addr, &me.sin_addr);
751
752       rc = connect(sock, (struct sockaddr *) &me, sizeof(me));
753 #ifdef ENABLE_IPV6
754     }
755     else {
756       memset(&me6, 0, sizeof(me6));
757       me6.sin6_family = AF_INET6;
758       me6.sin6_port = htons(connectport);
759       if (!addr)
760         addr = "::1";
761       Curl_inet_pton(AF_INET6, addr, &me6.sin6_addr);
762
763       rc = connect(sock, (struct sockaddr *) &me6, sizeof(me6));
764     }
765 #endif /* ENABLE_IPV6 */
766     if(rc) {
767       error = SOCKERRNO;
768       logmsg("Error connecting to port %d: (%d) %s",
769              port, error, strerror(error));
770       sclose(sock);
771       return 1;
772     }
773     logmsg("====> Client connect");
774     msgsock = sock; /* use this as stream */
775   }
776   else {
777     /* passive daemon style */
778     sock = sockdaemon(sock, &port);
779     if(CURL_SOCKET_BAD == sock)
780       return 1;
781     msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
782   }
783
784   logmsg("Running IPv%d version",
785          (use_ipv6?6:4));
786
787   if(connectport)
788     logmsg("Connected to port %d", connectport);
789   else
790     logmsg("Listening on port %d", port);
791
792   if(!write_pidfile(pidname)) {
793     sclose(sock);
794     return 1;
795   }
796
797   while(juggle(&msgsock, sock, &mode));
798
799   sclose(sock);
800   unlink(pidname);
801
802   logmsg("sockfilt exits");
803   return 0;
804 }
805