Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mdns] fix bug that avahi may publish redundant meshcop services #1104

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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