From 57bfa92d46111098a1dbabb5a63eb01c8b172ff1 Mon Sep 17 00:00:00 2001 From: Eduardo Montoya Date: Wed, 25 May 2022 20:15:59 +0200 Subject: [PATCH] [srp-client] prefer the numerically lowest server address (#7737) --- src/core/net/srp_client.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/core/net/srp_client.cpp b/src/core/net/srp_client.cpp index f9fad2e4b..e148d659a 100644 --- a/src/core/net/srp_client.cpp +++ b/src/core/net/srp_client.cpp @@ -1783,7 +1783,6 @@ Error Client::SelectUnicastEntry(DnsSrpUnicast::Origin aOrigin, DnsSrpUnicast::I Error error = kErrorNotFound; DnsSrpUnicast::Info unicastInfo; NetworkData::Service::Manager::Iterator iterator; - uint16_t numServers = 0; #if OPENTHREAD_CONFIG_SRP_CLIENT_SAVE_SELECTED_SERVER_ENABLE Settings::SrpClientInfo savedInfo; bool hasSavedServerInfo = false; @@ -1820,16 +1819,10 @@ Error Client::SelectUnicastEntry(DnsSrpUnicast::Origin aOrigin, DnsSrpUnicast::I ExitNow(); } #endif - numServers++; - // Choose a server randomly (with uniform distribution) from - // the list of servers. As we iterate through server entries, - // with probability `1/numServers`, we choose to switch the - // current selected server with the new entry. This approach - // results in a uniform/same probability of selection among - // all server entries. + // Prefer the numerically lowest server address - if ((numServers == 1) || (Random::NonCrypto::GetUint16InRange(0, numServers) == 0)) + if ((error == kErrorNotFound) || (unicastInfo.mSockAddr.GetAddress() < aInfo.mSockAddr.GetAddress())) { aInfo = unicastInfo; error = kErrorNone;