From dc5d1dac6c1848fe9b661f1d44d471abbcb2c963 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Fri, 25 Aug 2023 09:36:23 -0700 Subject: [PATCH] exit if we can't bind at least one of IPV4 or IPV6 for control plane port --- service/OneService.cpp | 62 ++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/service/OneService.cpp b/service/OneService.cpp index 161177931..dfa5df056 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -2197,36 +2197,46 @@ public: exit(-1); } + bool v4controlPlaneBound = false; _controlPlane.set_address_family(AF_INET); - if(!_controlPlane.bind_to_port("0.0.0.0", _primaryPort)) { + if(_controlPlane.bind_to_port("0.0.0.0", _primaryPort)) { + _serverThread = std::thread([&] { + _serverThreadRunning = true; + fprintf(stderr, "Starting Control Plane...\n"); + if(!_controlPlane.listen_after_bind()) { + fprintf(stderr, "Error on listen_after_bind()\n"); + } + fprintf(stderr, "Control Plane Stopped\n"); + _serverThreadRunning = false; + }); + v4controlPlaneBound = true; + } else { fprintf(stderr, "Error binding control plane to 0.0.0.0:%d\n", _primaryPort); + v4controlPlaneBound = false; + } + + bool v6controlPlaneBound = false; + _controlPlaneV6.set_address_family(AF_INET6); + if(_controlPlaneV6.bind_to_port("::", _primaryPort)) { + _serverThreadV6 = std::thread([&] { + _serverThreadRunningV6 = true; + fprintf(stderr, "Starting V6 Control Plane...\n"); + if(!_controlPlaneV6.listen_after_bind()) { + fprintf(stderr, "Error on V6 listen_after_bind()\n"); + } + fprintf(stderr, "V6 Control Plane Stopped\n"); + _serverThreadRunningV6 = false; + }); + v6controlPlaneBound = true; + } else { + fprintf(stderr, "Error binding control plane to [::]:%d\n", _primaryPort); + v6controlPlaneBound = false; + } + + if (!v4controlPlaneBound && !v6controlPlaneBound) { + fprintf(stderr, "ERROR: Could not bind control plane. Exiting...\n"); exit(-1); } - _controlPlaneV6.set_address_family(AF_INET6); - if(!_controlPlaneV6.bind_to_port("::", _primaryPort)) { - fprintf(stderr, "Error binding control plane to :::%d\n", _primaryPort); - // NOTE: Don't exit here in case of single stack ipv4 machine - } - - _serverThread = std::thread([&] { - _serverThreadRunning = true; - fprintf(stderr, "Starting Control Plane...\n"); - if(!_controlPlane.listen_after_bind()) { - fprintf(stderr, "Error on listen_after_bind()\n"); - } - fprintf(stderr, "Control Plane Stopped\n"); - _serverThreadRunning = false; - }); - - _serverThreadV6 = std::thread([&] { - _serverThreadRunningV6 = true; - fprintf(stderr, "Starting V6 Control Plane...\n"); - if(!_controlPlaneV6.listen_after_bind()) { - fprintf(stderr, "Error on V6 listen_after_bind()\n"); - } - fprintf(stderr, "V6 Control Plane Stopped\n"); - _serverThreadRunningV6 = false; - }); } // Must be called after _localConfig is read or modified