From 5615337d77ebb7b5c7a93a448d60708e5fd5dadf Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Tue, 15 Jun 2021 14:30:27 -0400 Subject: [PATCH] Change mdns broadcast to allow treatement as success if sends (#7555) * Change mdns broadcast to allow treatement as success if anything succeeds * Fix typo * Add progress log for mdns broadcast success --- src/lib/mdns/minimal/Server.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib/mdns/minimal/Server.cpp b/src/lib/mdns/minimal/Server.cpp index e7f9f59160cefc..db9a695327911d 100644 --- a/src/lib/mdns/minimal/Server.cpp +++ b/src/lib/mdns/minimal/Server.cpp @@ -263,6 +263,17 @@ CHIP_ERROR ServerBase::BroadcastSend(chip::System::PacketBufferHandle && data, u CHIP_ERROR ServerBase::BroadcastSend(chip::System::PacketBufferHandle && data, uint16_t port) { + // Broadcast requires sending data multiple times, each of which may error + // out, yet broadcast only has a single error code. + // + // The general logic of error handling is: + // - if no send done at all, return error + // - if at least one broadcast succeeds, assume success overall + // + some internal consistency validations for state error. + + bool hadSuccesfulSend = false; + CHIP_ERROR lastError = CHIP_ERROR_NO_ENDPOINT; + for (size_t i = 0; i < mEndpointCount; i++) { EndpointInfo * info = &mEndpoints[i]; @@ -291,22 +302,28 @@ CHIP_ERROR ServerBase::BroadcastSend(chip::System::PacketBufferHandle && data, u #endif else { + // This is a general error of internal consistency: every address has a known type + // Fail completely otherwise. return CHIP_ERROR_INCORRECT_STATE; } - if (err == chip::System::MapErrorPOSIX(ENETUNREACH)) + if (err == CHIP_NO_ERROR) { - // Send attempted to an unreachable network. Generally should not happen if - // interfaces are configured properly, however such a failure to broadcast - // may not be critical either. - ChipLogError(Discovery, "Attempt to mDNS broadcast to an unreachable destination."); + hadSuccesfulSend = true; + ChipLogProgress(Discovery, "mDNS broadcast success"); } - else if (err != CHIP_NO_ERROR) + else { - return err; + ChipLogError(Discovery, "Attempt to mDNS broadcast failed: %s", chip::ErrorStr(err)); + lastError = err; } } + if (!hadSuccesfulSend) + { + return lastError; + } + return CHIP_NO_ERROR; }