From 236c7f6a7604b02ccaf59c06f82ac35723347ef9 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Wed, 15 Jun 2022 15:39:38 +0000 Subject: [PATCH] VDPC: Remove any duplicate server entry from the IPv not preferred --- src/vconnect.cc | 8 ++++++- src/vdpc.cc | 64 +++++++++++++++++++++++++++++++++++++++++++++++-- src/vdpc.h | 5 ++++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/vconnect.cc b/src/vconnect.cc index a983689..fc07c93 100644 --- a/src/vconnect.cc +++ b/src/vconnect.cc @@ -174,13 +174,16 @@ void VConnect::threadMethod() // Multiple servers found + + /* + // Special case: If there are two servers returned and they only differ by IP version and IP, select one and connect automatically if ( (numServers == 2) && !vdpc[0].name.compare(vdpc[1].name) // if the names are the same... && (vdpc[0].port == vdpc[1].port) // and the port number is the same... && (vdpc[0].ipVersion != vdpc[1].ipVersion) // and the IP versions DO NOT match... - /* and the IPs don't match - they can't if ipVersions are different */ + / * and the IPs don't match - they can't if ipVersions are different * / ) { int which = 6; @@ -205,6 +208,9 @@ void VConnect::threadMethod() continue; // restart discovery } + */ + + // Now numServers > 1 VServerSelect* vs = new VServerSelect(vdpc, this); // deleted by handleCommand returning BoxStack::DELETE_ME diff --git a/src/vdpc.cc b/src/vdpc.cc index 079fa67..e954567 100644 --- a/src/vdpc.cc +++ b/src/vdpc.cc @@ -27,6 +27,7 @@ #include #include "log.h" +#include "config.h" #include "wol.h" #include "vdpc.h" @@ -35,6 +36,8 @@ static const char* TAG = "VDPC"; bool VDPC::init() { + logger = LogNT::getInstance(); + AddServerCallback addFunc( [this] (int ipVersion, const char* ip, const char* name, USHORT port, ULONG version) { std::lock_guard lg(serversLock); @@ -72,7 +75,7 @@ int VDPC::go() do { - LogNT::getInstance()->debug(TAG, "Sending broadcasts"); + logger->debug(TAG, "Sending broadcasts"); #ifdef IPV4 vdpc4.sendRequest(); @@ -82,7 +85,7 @@ int VDPC::go() #endif waitCond.wait_for(ul, std::chrono::milliseconds(1500), [this]{ return stopNow == true; }); - LogNT::getInstance()->debug(TAG, "go() wait finished"); + logger->debug(TAG, "go() wait finished"); if (stopNow) break; if (servers.size() == 0) Wol::getInstance()->doWakeUp(); @@ -96,6 +99,8 @@ int VDPC::go() vdpc6.stop(); #endif + filterServers(); + std::sort(servers.begin(), servers.end(), ServerSorter()); return servers.size(); } @@ -118,6 +123,61 @@ const VDRServer& VDPC::operator[](ULONG index) const return servers[index]; } +void VDPC::filterServers() +{ + // If the same VDR instance has replied on both IPv4 and IPv6, + // remove one based on the config option server-discovery.prefer-ipv + + // It is assumed that we can only get a maximum of two responses from + // the same server. The only way this doesn't happen is if two different + // VDR-vompservers are running with the same server name in the config. + // This is not supported. Results will not be as you would expect. + + Config* config = Config::getInstance(); + int which = 6; + config->getInt("server-discovery", "prefer-ipv", which); + logger->debug(TAG, "Auto select IPv{}", which); + + bool removed; + do + { + removed = false; + + for(auto i = servers.begin(); i != servers.end(); i++) + { + for(auto j = i + 1; j != servers.end(); j++) + { + if (i->name == j->name) + { + if (i->ipVersion != which) + { + + logger->debug(TAG, "{} found twice, removing {}", i->name, i->ipVersion); + + servers.erase(i); + removed = true; + break; + } + + if (j->ipVersion != which) + { + + logger->debug(TAG, "{} found twice, removing {}", j->name, j->ipVersion); + + servers.erase(j); + removed = true; + break; + } + } + } + + if (removed) break; + } + + } while (removed); +} + + #ifdef IPV4 // ======================================= VDPC4 diff --git a/src/vdpc.h b/src/vdpc.h index aad164c..6dd0583 100644 --- a/src/vdpc.h +++ b/src/vdpc.h @@ -85,9 +85,12 @@ class ServerSorter } }; +class LogNT; + class VDPC { private: + LogNT* logger{}; typedef std::function AddServerCallback; // Nested classes @@ -154,6 +157,8 @@ class VDPC std::mutex waitMutex; bool stopNow{}; + void filterServers(); + #ifdef IPV4 VDPC4 vdpc4; #endif -- 2.39.5