From 963f11e3130c0dbf60b7d8df4bd119ab0b6bed40 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 8 Sep 2023 14:31:31 +0200 Subject: More graceful handling of disabled listeners Don't assume a lack of TCP listeners means the server will be unreachable. There might be other methods of access, so let the higher levels do that sanity check instead. --- common/network/TcpSocket.cxx | 4 ---- unix/x0vncserver/x0vncserver.cxx | 21 ++++++++++++++++----- unix/xserver/hw/vnc/vncExtInit.cc | 17 ++++++++++++----- vncviewer/vncviewer.cxx | 2 ++ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/common/network/TcpSocket.cxx b/common/network/TcpSocket.cxx index 462b8a7b..15730cfd 100644 --- a/common/network/TcpSocket.cxx +++ b/common/network/TcpSocket.cxx @@ -496,10 +496,6 @@ void network::createTcpListeners(std::list *listeners, } } - if (new_listeners.empty ()) - throw SocketException("createTcpListeners: no addresses available", - EADDRNOTAVAIL); - listeners->splice (listeners->end(), new_listeners); } diff --git a/unix/x0vncserver/x0vncserver.cxx b/unix/x0vncserver/x0vncserver.cxx index b9fe058a..f87eb61e 100644 --- a/unix/x0vncserver/x0vncserver.cxx +++ b/unix/x0vncserver/x0vncserver.cxx @@ -306,16 +306,22 @@ int main(int argc, char** argv) } if ((int)rfbport != -1) { + std::list tcp_listeners; const char *addr = interface; + if (strcasecmp(addr, "all") == 0) addr = 0; if (localhostOnly) - createLocalTcpListeners(&listeners, (int)rfbport); + createLocalTcpListeners(&tcp_listeners, (int)rfbport); else - createTcpListeners(&listeners, addr, (int)rfbport); - vlog.info("Listening for VNC connections on %s interface(s), port %d", - localhostOnly ? "local" : (const char*)interface, - (int)rfbport); + createTcpListeners(&tcp_listeners, addr, (int)rfbport); + + if (!tcp_listeners.empty()) { + listeners.splice (listeners.end(), tcp_listeners); + vlog.info("Listening for VNC connections on %s interface(s), port %d", + localhostOnly ? "local" : (const char*)interface, + (int)rfbport); + } FileTcpFilter fileTcpFilter(hostsFile); if (strlen(hostsFile) != 0) @@ -325,6 +331,11 @@ int main(int argc, char** argv) (*i)->setFilter(&fileTcpFilter); } + if (listeners.empty()) { + vlog.error("No path or port configured for incoming connections"); + return -1; + } + PollingScheduler sched((int)pollingCycle, (int)maxProcessorUsage); while (!caughtSignal) { diff --git a/unix/xserver/hw/vnc/vncExtInit.cc b/unix/xserver/hw/vnc/vncExtInit.cc index 0292a6d1..1dfe76d7 100644 --- a/unix/xserver/hw/vnc/vncExtInit.cc +++ b/unix/xserver/hw/vnc/vncExtInit.cc @@ -229,6 +229,7 @@ void vncExtensionInit(void) } if (!inetd && rfbport != -1) { + std::list tcp_listeners; const char *addr = interface; int port = rfbport; if (port == 0) port = 5900 + atoi(vncGetDisplay()); @@ -236,15 +237,21 @@ void vncExtensionInit(void) if (strcasecmp(addr, "all") == 0) addr = 0; if (localhostOnly) - network::createLocalTcpListeners(&listeners, port); + network::createLocalTcpListeners(&tcp_listeners, port); else - network::createTcpListeners(&listeners, addr, port); + network::createTcpListeners(&tcp_listeners, addr, port); - vlog.info("Listening for VNC connections on %s interface(s), port %d", - localhostOnly ? "local" : (const char*)interface, - port); + if (!tcp_listeners.empty()) { + listeners.splice (listeners.end(), tcp_listeners); + vlog.info("Listening for VNC connections on %s interface(s), port %d", + localhostOnly ? "local" : (const char*)interface, + port); + } } + if (!inetd && listeners.empty()) + throw rdr::Exception("No path or port configured for incoming connections"); + PixelFormat pf = vncGetPixelFormat(scr); vncSetGlueContext(scr); diff --git a/vncviewer/vncviewer.cxx b/vncviewer/vncviewer.cxx index 544a87bb..06a8316d 100644 --- a/vncviewer/vncviewer.cxx +++ b/vncviewer/vncviewer.cxx @@ -756,6 +756,8 @@ int main(int argc, char** argv) port = atoi(vncServerName); createTcpListeners(&listeners, 0, port); + if (listeners.empty()) + throw Exception(_("Unable to listen for incoming connections")); vlog.info(_("Listening on port %d"), port); -- cgit v1.2.3