Skip to content

Commit

Permalink
[dns-sd] fix SRP service name length (#6626)
Browse files Browse the repository at this point in the history
* [dns-sd] fix SRP service name length

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Sep 3, 2021
1 parent f8d5990 commit 1318372
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 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

0 comments on commit 1318372

Please sign in to comment.