Skip to content

Commit

Permalink
[mdns] fix bug that avahi may publish redundant meshcop services (#1104)
Browse files Browse the repository at this point in the history
Avahi may rename the meshcop service and publish it when there's a
name conflict. However, our current logic has a bug that it may
publish redundant meshcop services after some renaming.

For example there are two BRs on the infra link:
- BR1 starts. It publishes instance._meshcop._udp.
- BR2 starts. It publishes instance #1._meshcop._udp.
- BR1 stops. It unpublishes instance._meshcop._udp.
- BR2 wants to update the existing meshcop service. However, it
  actually publishes a new service instance._meshcop._udp. There are
  two meshcop services existing at the same time both published by
  BR2.

To fix this bug, we will keep track of the original name in the avahi
publisher. When we call PublishService, we should always pass in the
original name and let the publisher handle the renaming. In this way
we will be able to find the wanted service when updating it or
unpublishing it.
  • Loading branch information
superwhd committed Nov 22, 2021
1 parent 7d61390 commit d14c225
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/mdns/mdns.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ class Publisher
* to provide specific host name. Otherwise, the caller MUST publish the host
* with method PublishHost.
* @param[in] aPort The port number of this service.
* @param[in] aName The name of this service.
* @param[in] aName The name of this service. Note that the published service name may be different from the
* original name due to renaming. You should always pass in the original name here to make
* sure you are referring to the intended service.
* @param[in] aType The type of this service.
* @param[in] aSubTypeList A list of service subtypes.
* @param[in] aTxtList A list of TXT name/value pairs.
Expand All @@ -252,7 +254,9 @@ class Publisher
/**
* This method un-publishes a service.
*
* @param[in] aName The name of this service.
* @param[in] aName The name of this service. Note that the published service name may be different from the
* original name due to renaming. You should always pass in the original name here to make
* sure you are referring to the intended service.
* @param[in] aType The type of this service.
*
* @retval OTBR_ERROR_NONE Successfully un-published the service.
Expand Down
39 changes: 23 additions & 16 deletions src/mdns/mdns_avahi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ void PublisherAvahi::HandleGroupState(AvahiEntryGroup *aGroup, AvahiEntryGroupSt
uint16_t port = service.mPort;

ResetGroup(service.mGroup);
alternativeName = avahi_alternative_service_name(service.mName.c_str());
service.mName = alternativeName;
alternativeName = avahi_alternative_service_name(service.mCurrentName.c_str());
service.mCurrentName = alternativeName;
avahi_free(alternativeName);
service.mPort = 0; // To mark the service as outdated
PublishService(service.mHostName, port, service.mName, service.mType, service.mSubTypeList,
Expand Down Expand Up @@ -470,8 +470,9 @@ otbrError PublisherAvahi::CreateService(AvahiClient & aClient,
otbrError error = OTBR_ERROR_NONE;
Service newService;

newService.mName = aName;
newService.mType = aType;
newService.mName = aName;
newService.mCurrentName = aName;
newService.mType = aType;
SuccessOrExit(error = CreateGroup(aClient, newService.mGroup));

mServices.push_back(newService);
Expand Down Expand Up @@ -640,9 +641,11 @@ otbrError PublisherAvahi::PublishService(const std::string &aHostName,
}
else
{
otbrLogInfo("Update service %s.%s for host %s", aName.c_str(), aType.c_str(), logHostName.c_str());
otbrLogInfo("Update service %s.%s for host %s", serviceIt->mCurrentName.c_str(), aType.c_str(),
logHostName.c_str());
avahiError = avahi_entry_group_update_service_txt_strlst(serviceIt->mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
AvahiPublishFlags{}, aName.c_str(), aType.c_str(),
AvahiPublishFlags{}, serviceIt->mCurrentName.c_str(),
aType.c_str(),
/* domain */ nullptr, head);
if (avahiError == 0 && mServiceHandler != nullptr)
{
Expand All @@ -653,23 +656,27 @@ otbrError PublisherAvahi::PublishService(const std::string &aHostName,
ExitNow();
}

otbrLogInfo("Create service %s.%s for host %s", aName.c_str(), aType.c_str(), logHostName.c_str());
avahiError = avahi_entry_group_add_service_strlst(serviceIt->mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
AvahiPublishFlags{}, aName.c_str(), aType.c_str(),
/* domain */ nullptr, fullHostName.c_str(), aPort, head);
otbrLogInfo("Create service %s.%s for host %s", serviceIt->mCurrentName.c_str(), aType.c_str(),
logHostName.c_str());
avahiError =
avahi_entry_group_add_service_strlst(serviceIt->mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
AvahiPublishFlags{}, serviceIt->mCurrentName.c_str(), aType.c_str(),
/* domain */ nullptr, fullHostName.c_str(), aPort, head);
SuccessOrExit(avahiError);

for (const std::string &subType : aSubTypeList)
{
otbrLogInfo("Add subtype %s for service %s.%s", subType.c_str(), aName.c_str(), aType.c_str());
otbrLogInfo("Add subtype %s for service %s.%s", subType.c_str(), serviceIt->mCurrentName.c_str(),
aType.c_str());
std::string fullSubType = subType + "._sub." + aType;
avahiError = avahi_entry_group_add_service_subtype(serviceIt->mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
AvahiPublishFlags{}, aName.c_str(), aType.c_str(),
/* domain */ nullptr, fullSubType.c_str());
avahiError =
avahi_entry_group_add_service_subtype(serviceIt->mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
AvahiPublishFlags{}, serviceIt->mCurrentName.c_str(), aType.c_str(),
/* domain */ nullptr, fullSubType.c_str());
SuccessOrExit(avahiError);
}

otbrLogInfo("Commit service %s.%s", aName.c_str(), aType.c_str());
otbrLogInfo("Commit service %s.%s", serviceIt->mCurrentName.c_str(), aType.c_str());
avahiError = avahi_entry_group_commit(serviceIt->mGroup);
SuccessOrExit(avahiError);

Expand Down Expand Up @@ -707,7 +714,7 @@ otbrError PublisherAvahi::UnpublishService(const std::string &aName, const std::
serviceIt = FindService(aName, aType);
VerifyOrExit(serviceIt != mServices.end());

otbrLogInfo("Unpublish service %s.%s", aName.c_str(), aType.c_str());
otbrLogInfo("Unpublish service %s.%s", serviceIt->mCurrentName.c_str(), aType.c_str());
error = FreeGroup(serviceIt->mGroup);
mServices.erase(serviceIt);

Expand Down
1 change: 1 addition & 0 deletions src/mdns/mdns_avahi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class PublisherAvahi : public Publisher
struct Service
{
std::string mName;
std::string mCurrentName; // The current instance name. May be different from mName due to renaming.
std::string mType;
SubTypeList mSubTypeList;
std::string mHostName;
Expand Down

0 comments on commit d14c225

Please sign in to comment.