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