From 3534469df94dd2cfc19a50d01f0181bb92510c63 Mon Sep 17 00:00:00 2001 From: Cliff Chung <116232729+cliffamzn@users.noreply.github.com> Date: Thu, 2 Mar 2023 08:58:24 -0800 Subject: [PATCH 1/2] Default to use an IPV4 address On some devices it appears that the commissioner may not have an appropriate IPV6 address and attempts to send a user directed commissioning request to a device using its IPV6 address. Its a bit unclear how this situation happens but in theory it could occur. In most cases this should be safe to default to an ipv4 address if it is available. The implementation of the user directed commissioning server does something similar. --- .../tv-casting-common/include/CastingServer.h | 13 ++++++++++ .../tv-casting-common/src/CastingServer.cpp | 26 +++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/examples/tv-casting-app/tv-casting-common/include/CastingServer.h b/examples/tv-casting-app/tv-casting-common/include/CastingServer.h index 3430a4f3bac7f1..67df18c93d4866 100644 --- a/examples/tv-casting-app/tv-casting-common/include/CastingServer.h +++ b/examples/tv-casting-app/tv-casting-common/include/CastingServer.h @@ -430,6 +430,19 @@ class CastingServer static void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); void ReadServerClusters(chip::EndpointId endpointId); + /** + * @brief Retrieve the IP Address to use for the UDC request. + * This function will look for an IPv4 address in the list of IPAddresses passed in if available and return + * that address if found. If there are no available IPv4 addresses, it will default to the first available address. + * This logic is similar to the one used by the UDC server that prefers IPv4 addresses. + * + * @param ipAddresses - The list of ip addresses available to use + * @param numIPs - The number of ip addresses available in the array + * + * @returns The IPv4 address in the array if available, otherwise will return the first address in the list. + */ + static chip::Inet::IPAddress * getIpAddressForUDCRequest(chip::Inet::IPAddress ipAddresses[], const size_t numIPs); + PersistenceManager mPersistenceManager; bool mInited = false; bool mUdcInProgress = false; diff --git a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp index b6f90d9a8f5451..3545a78bf03ce5 100644 --- a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp @@ -145,13 +145,35 @@ CHIP_ERROR CastingServer::SendUserDirectedCommissioningRequest(chip::Transport:: return Server::GetInstance().SendUserDirectedCommissioningRequest(commissioner); } +chip::Inet::IPAddress * CastingServer::getIpAddressForUDCRequest(chip::Inet::IPAddress ipAddresses[], const size_t numIPs) +{ + size_t ipIndexToUse = 0; + for (size_t i = 0; i < numIPs; i++) + { + if (ipAddresses[i].IsIPv4()) + { + ipIndexToUse = i; + ChipLogProgress(AppServer, "Found iPv4 address at index: %lu", ipIndexToUse); + break; + } + + if (i == (numIPs - 1)) + { + ChipLogProgress(AppServer, "Could not find an iPv4 address, defaulting to the first address in IP list"); + } + } + + return &ipAddresses[ipIndexToUse]; +} + CHIP_ERROR CastingServer::SendUserDirectedCommissioningRequest(Dnssd::DiscoveredNodeData * selectedCommissioner) { mUdcInProgress = true; // Send User Directed commissioning request + chip::Inet::IPAddress * ipAddressToUse = + getIpAddressForUDCRequest(selectedCommissioner->resolutionData.ipAddress, selectedCommissioner->resolutionData.numIPs); ReturnErrorOnFailure(SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress::UDP( - selectedCommissioner->resolutionData.ipAddress[0], selectedCommissioner->resolutionData.port, - selectedCommissioner->resolutionData.interfaceId))); + *ipAddressToUse, selectedCommissioner->resolutionData.port, selectedCommissioner->resolutionData.interfaceId))); mTargetVideoPlayerVendorId = selectedCommissioner->commissionData.vendorId; mTargetVideoPlayerProductId = selectedCommissioner->commissionData.productId; mTargetVideoPlayerDeviceType = selectedCommissioner->commissionData.deviceType; From ad98cf4bfdf7fa86fd62817b52a016781febd51c Mon Sep 17 00:00:00 2001 From: cliffamzn Date: Thu, 16 Mar 2023 13:38:11 -0700 Subject: [PATCH 2/2] Expand on the error messaging to clarify ipv4 selection --- .../tv-casting-app/tv-casting-common/src/CastingServer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp index 3545a78bf03ce5..76f0fe9269b419 100644 --- a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp @@ -153,13 +153,13 @@ chip::Inet::IPAddress * CastingServer::getIpAddressForUDCRequest(chip::Inet::IPA if (ipAddresses[i].IsIPv4()) { ipIndexToUse = i; - ChipLogProgress(AppServer, "Found iPv4 address at index: %lu", ipIndexToUse); + ChipLogProgress(AppServer, "Found IPv4 address at index: %lu - prioritizing use of IPv4", ipIndexToUse); break; } if (i == (numIPs - 1)) { - ChipLogProgress(AppServer, "Could not find an iPv4 address, defaulting to the first address in IP list"); + ChipLogProgress(AppServer, "Could not find an IPv4 address, defaulting to the first address in IP list"); } }