Skip to content

Commit d0866b8

Browse files
cliffamznpull[bot]
authored andcommitted
Default to use an IPV4 address (#25668)
* 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. * Expand on the error messaging to clarify ipv4 selection
1 parent f586d77 commit d0866b8

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

examples/tv-casting-app/tv-casting-common/include/CastingServer.h

+13
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,19 @@ class CastingServer
430430
static void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
431431
void ReadServerClusters(chip::EndpointId endpointId);
432432

433+
/**
434+
* @brief Retrieve the IP Address to use for the UDC request.
435+
* This function will look for an IPv4 address in the list of IPAddresses passed in if available and return
436+
* that address if found. If there are no available IPv4 addresses, it will default to the first available address.
437+
* This logic is similar to the one used by the UDC server that prefers IPv4 addresses.
438+
*
439+
* @param ipAddresses - The list of ip addresses available to use
440+
* @param numIPs - The number of ip addresses available in the array
441+
*
442+
* @returns The IPv4 address in the array if available, otherwise will return the first address in the list.
443+
*/
444+
static chip::Inet::IPAddress * getIpAddressForUDCRequest(chip::Inet::IPAddress ipAddresses[], const size_t numIPs);
445+
433446
PersistenceManager mPersistenceManager;
434447
bool mInited = false;
435448
bool mUdcInProgress = false;

examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,35 @@ CHIP_ERROR CastingServer::SendUserDirectedCommissioningRequest(chip::Transport::
145145
return Server::GetInstance().SendUserDirectedCommissioningRequest(commissioner);
146146
}
147147

148+
chip::Inet::IPAddress * CastingServer::getIpAddressForUDCRequest(chip::Inet::IPAddress ipAddresses[], const size_t numIPs)
149+
{
150+
size_t ipIndexToUse = 0;
151+
for (size_t i = 0; i < numIPs; i++)
152+
{
153+
if (ipAddresses[i].IsIPv4())
154+
{
155+
ipIndexToUse = i;
156+
ChipLogProgress(AppServer, "Found IPv4 address at index: %lu - prioritizing use of IPv4", ipIndexToUse);
157+
break;
158+
}
159+
160+
if (i == (numIPs - 1))
161+
{
162+
ChipLogProgress(AppServer, "Could not find an IPv4 address, defaulting to the first address in IP list");
163+
}
164+
}
165+
166+
return &ipAddresses[ipIndexToUse];
167+
}
168+
148169
CHIP_ERROR CastingServer::SendUserDirectedCommissioningRequest(Dnssd::DiscoveredNodeData * selectedCommissioner)
149170
{
150171
mUdcInProgress = true;
151172
// Send User Directed commissioning request
173+
chip::Inet::IPAddress * ipAddressToUse =
174+
getIpAddressForUDCRequest(selectedCommissioner->resolutionData.ipAddress, selectedCommissioner->resolutionData.numIPs);
152175
ReturnErrorOnFailure(SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress::UDP(
153-
selectedCommissioner->resolutionData.ipAddress[0], selectedCommissioner->resolutionData.port,
154-
selectedCommissioner->resolutionData.interfaceId)));
176+
*ipAddressToUse, selectedCommissioner->resolutionData.port, selectedCommissioner->resolutionData.interfaceId)));
155177
mTargetVideoPlayerVendorId = selectedCommissioner->commissionData.vendorId;
156178
mTargetVideoPlayerProductId = selectedCommissioner->commissionData.productId;
157179
mTargetVideoPlayerDeviceType = selectedCommissioner->commissionData.deviceType;

0 commit comments

Comments
 (0)