From 0123b4381f306a4e9d75173812e731b431b215b5 Mon Sep 17 00:00:00 2001 From: Ilan Pegoraro Date: Thu, 27 Jun 2019 12:14:50 +0100 Subject: [PATCH] vhost: bind: make failure to bind optionally fatal When creating a vhost and the port is already bound to another process this flag would allow the user code to choose to have the lws_create_vhost function to fail and return a null pointer. --- include/libwebsockets/lws-context-vhost.h | 4 ++++ lib/roles/http/server/server.c | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/libwebsockets/lws-context-vhost.h b/include/libwebsockets/lws-context-vhost.h index e7bc620c8..152a79a26 100644 --- a/include/libwebsockets/lws-context-vhost.h +++ b/include/libwebsockets/lws-context-vhost.h @@ -203,6 +203,10 @@ enum lws_context_options { * on the client using http when he meant https... it's not * recommended. */ + LWS_SERVER_OPTION_FAIL_UPON_UNABLE_TO_BIND = (1 << 30), + /**< (VH) When instantiating a new vhost and the specified port is + * already in use, a null value shall be return to signal the error. + */ /****** add new things just above ---^ ******/ }; diff --git a/lib/roles/http/server/server.c b/lib/roles/http/server/server.c index 2523d7018..2433149b8 100644 --- a/lib/roles/http/server/server.c +++ b/lib/roles/http/server/server.c @@ -130,13 +130,15 @@ done_list: if (info) /* first time */ lwsl_err("VH %s: iface %s port %d DOESN'T EXIST\n", vhost->name, vhost->iface, vhost->listen_port); - return 1; + return (info->options & LWS_SERVER_OPTION_FAIL_UPON_UNABLE_TO_BIND) == LWS_SERVER_OPTION_FAIL_UPON_UNABLE_TO_BIND? + -1 : 1; case LWS_ITOSA_NOT_USABLE: /* can't add it */ if (info) /* first time */ lwsl_err("VH %s: iface %s port %d NOT USABLE\n", vhost->name, vhost->iface, vhost->listen_port); - return 1; + return (info->options & LWS_SERVER_OPTION_FAIL_UPON_UNABLE_TO_BIND) == LWS_SERVER_OPTION_FAIL_UPON_UNABLE_TO_BIND? + -1 : 1; } }