unsigned short connectport = 0; /* if non-zero, we activate this mode */
enum sockmode {
- 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 */
+ 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 */
};
/*
maxfd = 0;
}
break;
+
+ case ACTIVE_DISCONNECT:
+ logmsg("disconnected, no socket to read on");
+ maxfd = 0;
+ sockfd = CURL_SOCKET_BAD;
+ break;
}
do {
else if(!memcmp("QUIT", buffer, 4)) {
/* just die */
logmsg("quits");
- exit(0);
+ return FALSE;
}
else if(!memcmp("DATA", buffer, 4)) {
/* data IN => data OUT */
logmsg("====> Client forcibly disconnected");
sclose(sockfd);
*sockfdp = CURL_SOCKET_BAD;
+ if(*mode == PASSIVE_CONNECT)
+ *mode = PASSIVE_LISTEN;
+ else
+ *mode = ACTIVE_DISCONNECT;
}
else
logmsg("attempt to close already dead connection");
}
else {
logmsg("read %d from stdin, exiting", (int)nread);
- exit(0);
+ return FALSE;
}
}
*sockfdp = CURL_SOCKET_BAD;
if(*mode == PASSIVE_CONNECT)
*mode = PASSIVE_LISTEN;
+ else
+ *mode = ACTIVE_DISCONNECT;
return TRUE;
}
" --ipv4\n"
" --ipv6\n"
" --port [port]");
- exit(0);
+ return 0;
}
}
if (sock < 0) {
perror("opening stream socket");
logmsg("Error opening socket");
- exit(1);
+ return 1;
}
if(connectport) {
if(rc) {
perror("connecting stream socket");
logmsg("Error connecting to port %d", port);
- exit(1);
+ return 1;
}
logmsg("====> Client connect");
msgsock = sock; /* use this as stream */
if(rc < 0) {
perror("binding stream socket");
logmsg("Error binding socket");
- exit(1);
+ return 1;
}
if(!port) {
}
- pidfile = fopen(pidname, "w");
- if(pidfile) {
- fprintf(pidfile, "%d\n", (int)getpid());
- fclose(pidfile);
- }
- else
- fprintf(stderr, "Couldn't write pid file\n");
-
logmsg("Running IPv%d version",
(use_ipv6?6:4));
else
logmsg("Listening on port %d", port);
+ 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);
+ }
+ else
+ fprintf(stderr, "Couldn't write pid file\n");
+
do {
ok = juggle(&msgsock, sock, &mode);
} while(ok);