Skip to content

Commit

Permalink
Fix mac address get for dnssd (#15221)
Browse files Browse the repository at this point in the history
* Fix mac address get for dnssd

* Stop asserting that the primary mac getting returns a default - it should not

* remove one more use of the default mac

* Use DRBG_get_bytes to get a random mac

* Remove the NO_ERROR assumption for getting primary mac. Emulators have no active IP interface to get a mac from
  • Loading branch information
andy31415 authored and pull[bot] committed Oct 17, 2023
1 parent b0113eb commit 1312908
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
12 changes: 10 additions & 2 deletions src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ CHIP_ERROR DnssdServer::AdvertiseOperational()
{
uint8_t macBuffer[DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
MutableByteSpan mac(macBuffer);
chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac);
if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to get primary mac address of device. Generating a random one.");
Crypto::DRBG_get_bytes(macBuffer, sizeof(macBuffer));
}

const auto advertiseParameters = chip::Dnssd::OperationalAdvertisingParameters()
.SetPeerId(fabricInfo.GetPeerId())
Expand Down Expand Up @@ -293,7 +297,11 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi

uint8_t macBuffer[DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
MutableByteSpan mac(macBuffer);
chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac);
if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to get primary mac address of device. Generating a random one.");
Crypto::DRBG_get_bytes(macBuffer, sizeof(macBuffer));
}
advertiseParameters.SetMac(mac);

uint16_t value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,15 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryMACAddress(Mu
ChipLogDetail(DeviceLayer, "Using Thread extended MAC for hostname.");
return CHIP_NO_ERROR;
}
#else
if (DeviceLayer::ConfigurationMgr().GetPrimaryWiFiMACAddress(buf.data()) == CHIP_NO_ERROR)
#endif

if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryWiFiMACAddress(buf.data()) == CHIP_NO_ERROR)
{
ChipLogDetail(DeviceLayer, "Using wifi MAC for hostname");
return CHIP_NO_ERROR;
}
#endif

ChipLogError(DeviceLayer, "MAC is not known, using a default.");
uint8_t temp[ConfigurationManager::kMaxMACAddressLength] = { 0xEE, 0xAA, 0xBA, 0xDA, 0xBA, 0xD0, 0xDD, 0xCA };
memcpy(buf.data(), temp, buf.size());
return CHIP_NO_ERROR;
return CHIP_ERROR_NOT_FOUND;
}

template <class ConfigClass>
Expand Down
26 changes: 5 additions & 21 deletions src/platform/tests/TestConfigurationMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ static void TestConfigurationMgr_Breadcrumb(nlTestSuite * inSuite, void * inCont

static void TestConfigurationMgr_GetPrimaryMACAddress(nlTestSuite * inSuite, void * inContext)
{
CHIP_ERROR err = CHIP_NO_ERROR;
const uint8_t defaultMacAddress[8] = { 0xEE, 0xAA, 0xBA, 0xDA, 0xBA, 0xD0, 0xDD, 0xCA };
CHIP_ERROR err = CHIP_NO_ERROR;
uint8_t macBuffer8Bytes[8];
uint8_t macBuffer6Bytes[6];
MutableByteSpan mac8Bytes(macBuffer8Bytes);
Expand All @@ -199,32 +198,17 @@ static void TestConfigurationMgr_GetPrimaryMACAddress(nlTestSuite * inSuite, voi
{
NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INVALID_ARGUMENT);
}
else
{
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

// Verify default MAC address value
NL_TEST_ASSERT(inSuite,
strncmp(reinterpret_cast<char *>(mac8Bytes.data()), reinterpret_cast<const char *>(defaultMacAddress),
mac8Bytes.size()) == 0);
}

err = ConfigurationMgr().GetPrimaryMACAddress(mac6Bytes);
if (mac6Bytes.size() != ConfigurationManager::kPrimaryMACAddressLength)
{
NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INVALID_ARGUMENT);
}
else
{
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

#ifndef __MBED__
// Verify default MAC address value
NL_TEST_ASSERT(inSuite,
strncmp(reinterpret_cast<char *>(mac6Bytes.data()), reinterpret_cast<const char *>(defaultMacAddress),
mac6Bytes.size()) == 0);
#endif
}
// NOTICE for above:
// no validation for CHIP_NO_ERROR:
// - there is no guarantee in CI that a valid IP address exists,
// expecially if running in emulators (zephyr and qemu)
}

/**
Expand Down

0 comments on commit 1312908

Please sign in to comment.