From bbf2588a5670e7daf28572a441f505bead57fc8f Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 10 Nov 2025 14:39:48 -0800 Subject: [PATCH] [mdns] restart probing on Register() if in conflict state (#12128) This commit enhances mDNS to allow reprobing for registrations currently in a conflict state. Upon an explicit `Register()` call, the mDNS module will now restart the probing process. This allows the device to attempt to claim the name again if the conflict has been resolved on the network. Unit tests are updated to verify this behavior. --- src/core/net/mdns.cpp | 34 ++++++++++++++++++++++------------ src/core/net/mdns.hpp | 1 + tests/unit/test_mdns.cpp | 36 ++---------------------------------- 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/src/core/net/mdns.cpp b/src/core/net/mdns.cpp index cb784dd79..0f34ea69b 100644 --- a/src/core/net/mdns.cpp +++ b/src/core/net/mdns.cpp @@ -1215,10 +1215,7 @@ void Core::Entry::SetState(State aState) void Core::Entry::Register(const Key &aKey, const Callback &aCallback) { - if (GetState() == kRemoving) - { - StartProbing(); - } + DecideToProbeOnRegister(); mKeyRecord.UpdateTtl(DetermineTtl(aKey.mTtl, kDefaultKeyTtl)); mKeyRecord.UpdateProperty(mKeyData, aKey.mKeyData, aKey.mKeyDataLength); @@ -1322,6 +1319,25 @@ void Core::Entry::InvokeCallbacks(void) } } +void Core::Entry::DecideToProbeOnRegister(void) +{ + // Checks whether we should start probing when `Register()` is + // called. If a conflict was previously detected, we send a probe + // again upon an explicit `Register()` request. + + switch (mState) + { + case kRegistered: + case kProbing: + break; + + case kRemoving: + case kConflict: + StartProbing(); + break; + } +} + void Core::Entry::StartProbing(void) { SetState(kProbing); @@ -1951,10 +1967,7 @@ exit: void Core::HostEntry::Register(const Host &aHost, const Callback &aCallback) { - if (GetState() == kRemoving) - { - StartProbing(); - } + DecideToProbeOnRegister(); SetCallback(aCallback); @@ -2543,10 +2556,7 @@ void Core::ServiceEntry::Register(const Service &aService, const Callback &aCall const char *hostName; uint32_t ttl = DetermineTtl(aService.mTtl, kDefaultServiceTtl); - if (GetState() == kRemoving) - { - StartProbing(); - } + DecideToProbeOnRegister(); SetCallback(aCallback); diff --git a/src/core/net/mdns.hpp b/src/core/net/mdns.hpp index 2d960dbd9..cacebb908 100644 --- a/src/core/net/mdns.hpp +++ b/src/core/net/mdns.hpp @@ -1172,6 +1172,7 @@ private: void SetCallback(const Callback &aCallback); void ClearCallback(void) { mCallback.Clear(); } void MarkToInvokeCallbackUnconditionally(void); + void DecideToProbeOnRegister(void); void StartProbing(void); void SetStateToConflict(void); void SetStateToRemoving(void); diff --git a/tests/unit/test_mdns.cpp b/tests/unit/test_mdns.cpp index 8d63536a0..f5ce29f22 100644 --- a/tests/unit/test_mdns.cpp +++ b/tests/unit/test_mdns.cpp @@ -5319,23 +5319,7 @@ void TestHostConflict(void) VerifyOrQuit(!sConflictCallback.mWasCalled); Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); - Log("Register the conflicted `HostEntry` again, and make sure no probes are sent"); - - sRegCallbacks[1].Reset(); - sConflictCallback.Reset(); - sDnsMessages.Clear(); - - SuccessOrQuit(mdns->RegisterHost(host, 1, HandleCallback)); - AdvanceTime(5000); - - VerifyOrQuit(sRegCallbacks[1].mWasCalled); - VerifyOrQuit(sRegCallbacks[1].mError == kErrorDuplicated); - VerifyOrQuit(!sConflictCallback.mWasCalled); - - Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); - Log("Unregister the conflicted host and register it again immediately, make sure we see probes"); - - SuccessOrQuit(mdns->UnregisterHost(host)); + Log("Register the conflicted `HostEntry` again, and make sure probes are sent"); sConflictCallback.Reset(); sRegCallbacks[0].Reset(); @@ -5481,23 +5465,7 @@ void TestServiceConflict(void) VerifyOrQuit(!sConflictCallback.mWasCalled); Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); - Log("Register the conflicted `ServiceEntry` again, and make sure no probes are sent"); - - sRegCallbacks[1].Reset(); - sConflictCallback.Reset(); - sDnsMessages.Clear(); - - SuccessOrQuit(mdns->RegisterService(service, 1, HandleCallback)); - AdvanceTime(5000); - - VerifyOrQuit(sRegCallbacks[1].mWasCalled); - VerifyOrQuit(sRegCallbacks[1].mError == kErrorDuplicated); - VerifyOrQuit(!sConflictCallback.mWasCalled); - - Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); - Log("Unregister the conflicted host and register it again immediately, make sure we see probes"); - - SuccessOrQuit(mdns->UnregisterService(service)); + Log("Register the conflicted `ServiceEntry` again, and make sure probes are sent"); sConflictCallback.Reset(); sRegCallbacks[0].Reset();