From 4787cb45d490ad84472b81a12928f7c627ebc2df Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Fri, 13 Apr 2012 10:36:16 -0500 Subject: [PATCH] C websockify: add --run-once option. --- other/websocket.c | 16 ++++++++++++++-- other/websocket.h | 1 + other/websockify.c | 10 ++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/other/websocket.c b/other/websocket.c index 5160bc7..c365409 100644 --- a/other/websocket.c +++ b/other/websocket.c @@ -754,11 +754,23 @@ void start_server() { handler_msg("got client connection from %s\n", inet_ntoa(cli_addr.sin_addr)); - handler_msg("forking handler process\n"); - pid = fork(); + if (!settings.run_once) { + handler_msg("forking handler process\n"); + pid = fork(); + } if (pid == 0) { // handler process ws_ctx = do_handshake(csock); + if (settings.run_once) { + if (ws_ctx == NULL) { + // Not a real WebSocket connection + continue; + } else { + // Successful connection, stop listening for new + // connections + close(lsock); + } + } if (ws_ctx == NULL) { handler_msg("No connection after handshake\n"); break; // Child process exits diff --git a/other/websocket.h b/other/websocket.h index 8062da7..7da5275 100644 --- a/other/websocket.h +++ b/other/websocket.h @@ -61,6 +61,7 @@ typedef struct { char *key; int ssl_only; int daemon; + int run_once; } settings_t; diff --git a/other/websockify.c b/other/websockify.c index a261e81..4032171 100644 --- a/other/websockify.c +++ b/other/websockify.c @@ -274,13 +274,14 @@ void proxy_handler(ws_ctx_t *ws_ctx) { int main(int argc, char *argv[]) { int fd, c, option_index = 0; - static int ssl_only = 0, daemon = 0, verbose = 0; + static int ssl_only = 0, daemon = 0, run_once = 0, verbose = 0; char *found; static struct option long_options[] = { {"verbose", no_argument, &verbose, 'v'}, {"ssl-only", no_argument, &ssl_only, 1 }, {"daemon", no_argument, &daemon, 'D'}, /* ---- */ + {"run-once", no_argument, 0, 'r'}, {"cert", required_argument, 0, 'c'}, {"key", required_argument, 0, 'k'}, {0, 0, 0, 0} @@ -294,7 +295,7 @@ int main(int argc, char *argv[]) settings.key = ""; while (1) { - c = getopt_long (argc, argv, "vDc:k:", + c = getopt_long (argc, argv, "vDrc:k:", long_options, &option_index); /* Detect the end */ @@ -311,6 +312,9 @@ int main(int argc, char *argv[]) case 'D': daemon = 1; break; + case 'r': + run_once = 1; + break; case 'c': settings.cert = realpath(optarg, NULL); if (! settings.cert) { @@ -330,6 +334,7 @@ int main(int argc, char *argv[]) settings.verbose = verbose; settings.ssl_only = ssl_only; settings.daemon = daemon; + settings.run_once = run_once; if ((argc-optind) != 2) { usage("Invalid number of arguments\n"); @@ -370,6 +375,7 @@ int main(int argc, char *argv[]) //printf(" verbose: %d\n", settings.verbose); //printf(" ssl_only: %d\n", settings.ssl_only); //printf(" daemon: %d\n", settings.daemon); + //printf(" run_once: %d\n", settings.run_once); //printf(" cert: %s\n", settings.cert); //printf(" key: %s\n", settings.key);