Skip to content

Commit

Permalink
Merge 903596d into 01fbb86
Browse files Browse the repository at this point in the history
  • Loading branch information
wgtdkp authored May 12, 2021
2 parents 01fbb86 + 903596d commit 5743028
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/app/server/Mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ CHIP_ERROR AdvertiseOperational()

auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance();

ChipLogProgress(Discovery, "Advertise operational node 0x%08" PRIx32 "%08" PRIx32 "-0x%08" PRIx32 "%08" PRIx32,
ChipLogProgress(Discovery, "Advertise operational node %08" PRIx32 "%08" PRIx32 "-%08" PRIx32 "%08" PRIx32,
static_cast<uint32_t>(advertiseParameters.GetPeerId().GetFabricId() >> 32),
static_cast<uint32_t>(advertiseParameters.GetPeerId().GetFabricId()),
static_cast<uint32_t>(advertiseParameters.GetPeerId().GetNodeId() >> 32),
Expand Down
9 changes: 5 additions & 4 deletions src/lib/mdns/platform/Mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
namespace chip {
namespace Mdns {

static constexpr uint8_t kMdnsNameMaxSize = 33; // [Node]-[Fabric] ID in hex - 16+1+16
static constexpr uint8_t kMdnsProtocolTextMaxSize = 4 + 1; // "_tcp" or "_udp"
static constexpr uint8_t kMdnsTypeMaxSize = 10; // "_chip", "_chipc" or "_chipd"
static constexpr uint16_t kMdnsTextMaxSize = 64;
static constexpr uint8_t kMdnsNameMaxSize = 33; // [Node]-[Fabric] ID in hex - 16+1+16
static constexpr uint8_t kMdnsProtocolTextMaxSize = 4; // "_tcp" or "_udp"
static constexpr uint8_t kMdnsTypeMaxSize = 6; // "_chip", "_chipc" or "_chipd"
static constexpr uint8_t kMdnsTypeAndProtocolMaxSize = kMdnsTypeMaxSize + kMdnsProtocolTextMaxSize + 1; // <type>.<protocol>
static constexpr uint16_t kMdnsTextMaxSize = 64;

enum class MdnsServiceProtocol : uint8_t
{
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/MdnsImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ CHIP_ERROR MakeAvahiStringListFromTextEntries(TextEntry * entries, size_t size,

for (size_t i = 0; i < size; i++)
{
uint8_t buf[kMdnsTypeMaxSize];
uint8_t buf[chip::Mdns::kMdnsTextMaxSize];
size_t offset = static_cast<size_t>(snprintf(reinterpret_cast<char *>(buf), sizeof(buf), "%s=", entries[i].mKey));

if (offset + entries[i].mDataSize > sizeof(buf))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1074,9 +1074,9 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_AddSrpService(c
Impl()->LockThreadStack();

VerifyOrExit(aInstanceName, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(strlen(aInstanceName) < SrpClient::kMaxInstanceNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);
VerifyOrExit(strlen(aInstanceName) <= SrpClient::kMaxInstanceNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);
VerifyOrExit(aName, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(strlen(aName) < SrpClient::kMaxNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);
VerifyOrExit(strlen(aName) <= SrpClient::kMaxNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);

// Check if service with desired instance name already exists and try to find empty slot in array for new service
for (typename SrpClient::Service & service : mSrpClient.mServices)
Expand Down Expand Up @@ -1130,6 +1130,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_AddSrpService(c
srpService->mService.mTxtEntries = srpService->mTxtEntries;
}

ChipLogProgress(DeviceLayer, "advertising srp service: %s.%s", srpService->mService.mInstanceName, srpService->mService.mName);
error = MapOpenThreadError(otSrpClientAddService(mOTInst, &(srpService->mService)));

exit:
Expand All @@ -1147,9 +1148,9 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_RemoveSrpServic
Impl()->LockThreadStack();

VerifyOrExit(aInstanceName, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(strlen(aInstanceName) < SrpClient::kMaxInstanceNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);
VerifyOrExit(strlen(aInstanceName) <= SrpClient::kMaxInstanceNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);
VerifyOrExit(aName, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(strlen(aName) < SrpClient::kMaxNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);
VerifyOrExit(strlen(aName) <= SrpClient::kMaxNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);

// Check if service to remove exists.
for (typename SrpClient::Service & service : mSrpClient.mServices)
Expand All @@ -1163,6 +1164,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_RemoveSrpServic

VerifyOrExit(srpService, error = MapOpenThreadError(OT_ERROR_NOT_FOUND));

ChipLogProgress(DeviceLayer, "removing srp service: %s.%s", aInstanceName, aName);
error = MapOpenThreadError(otSrpClientRemoveService(mOTInst, &(srpService->mService)));

exit:
Expand Down Expand Up @@ -1199,7 +1201,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_SetupSrpHost(co
Impl()->LockThreadStack();

VerifyOrExit(aHostName, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(strlen(aHostName) < SrpClient::kMaxHostNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);
VerifyOrExit(strlen(aHostName) <= SrpClient::kMaxHostNameSize, error = CHIP_ERROR_INVALID_STRING_LENGTH);

// Avoid adding the same host name multiple times
if (strcmp(mSrpClient.mHostName, aHostName) != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class GenericThreadStackManagerImpl_OpenThread
{
static constexpr uint8_t kMaxServicesNumber = CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES;
static constexpr uint8_t kMaxInstanceNameSize = chip::Mdns::kMdnsNameMaxSize;
static constexpr uint8_t kMaxNameSize = chip::Mdns::kMdnsTypeMaxSize + chip::Mdns::kMdnsProtocolTextMaxSize + 1;
static constexpr uint8_t kMaxHostNameSize = 32;
static constexpr uint8_t kMaxNameSize = chip::Mdns::kMdnsTypeAndProtocolMaxSize;
static constexpr uint8_t kMaxHostNameSize = 16;
// Thread only supports operational discovery
static constexpr uint8_t kMaxTxtEntriesNumber = chip::Mdns::OperationalAdvertisingParameters::kNumAdvertisingTxtEntries;
static constexpr uint8_t kMaxTxtValueSize = chip::Mdns::OperationalAdvertisingParameters::kTxtMaxValueSize;
Expand All @@ -131,14 +131,14 @@ class GenericThreadStackManagerImpl_OpenThread
struct Service
{
otSrpClientService mService;
char mInstanceName[kMaxInstanceNameSize];
char mName[kMaxNameSize];
char mInstanceName[kMaxInstanceNameSize + 1];
char mName[kMaxNameSize + 1];
otDnsTxtEntry mTxtEntries[kMaxTxtEntriesNumber];
uint8_t mTxtValueBuffers[kMaxTxtEntriesNumber][kMaxTxtValueSize];
char mTxtKeyBuffers[kMaxTxtEntriesNumber][kMaxTxtKeySize];
};

char mHostName[kMaxHostNameSize];
char mHostName[kMaxHostNameSize + 1];
otIp6Address mHostAddress;
Service mServices[kMaxServicesNumber];
};
Expand Down
4 changes: 2 additions & 2 deletions src/platform/OpenThread/MdnsImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CHIP_ERROR ChipMdnsPublishService(const MdnsService * service)

VerifyOrExit(service, result = CHIP_ERROR_INVALID_ARGUMENT);

char serviceType[kMdnsTypeMaxSize + kMdnsProtocolTextMaxSize + 1];
char serviceType[chip::Mdns::kMdnsTypeAndProtocolMaxSize + 1];
snprintf(serviceType, sizeof(serviceType), "%s.%s", service->mType, GetProtocolString(service->mProtocol));

// Try to remove service before adding it, as SRP doesn't allow to update existing services.
Expand All @@ -74,7 +74,7 @@ CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service)
if (service == nullptr)
return CHIP_ERROR_INVALID_ARGUMENT;

char serviceType[kMdnsTypeMaxSize + kMdnsProtocolTextMaxSize + 1];
char serviceType[chip::Mdns::kMdnsTypeAndProtocolMaxSize + 1];
snprintf(serviceType, sizeof(serviceType), "%s.%s", service->mType, GetProtocolString(service->mProtocol));

return ThreadStackMgr().RemoveSrpService(service->mName, serviceType);
Expand Down
17 changes: 11 additions & 6 deletions src/platform/tests/TestMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,30 @@ static const nlTest sTests[] = { NL_TEST_DEF("Test Mdns::PubSub", TestMdnsPubSub
int TestMdns()
{
std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
std::condition_variable done;
int retVal = EXIT_FAILURE;

std::thread t([&mtx, &done, &retVal]() {
{
std::lock_guard<std::mutex> localLock(mtx);
nlTestSuite theSuite = { "CHIP DeviceLayer mdns tests", &sTests[0], nullptr, nullptr };

nlTestRunner(&theSuite, nullptr);
retVal = nlTestRunnerStats(&theSuite);
}
done.notify_all();

{
std::lock_guard<std::mutex> localLock(mtx);
done.notify_all();
}
});

if (done.wait_for(lock, std::chrono::seconds(5)) == std::cv_status::timeout)
{
fprintf(stderr, "mDNS test timeout, is avahi daemon running?");
retVal = EXIT_FAILURE;
std::unique_lock<std::mutex> lock(mtx);
if (done.wait_for(lock, std::chrono::seconds(5)) == std::cv_status::timeout)
{
fprintf(stderr, "mDNS test timeout, is avahi daemon running?\n");
retVal = EXIT_FAILURE;
}
}
return retVal;
}
Expand Down

0 comments on commit 5743028

Please sign in to comment.