[wpinet] Handle empty txt block for mdns announcer (#4072)

This commit is contained in:
Thad House
2022-08-03 11:15:56 -07:00
committed by GitHub
parent 19ffebaf3e
commit 0a5eb65231
9 changed files with 129 additions and 25 deletions

View File

@@ -58,6 +58,7 @@ AvahiFunctionTable::AvahiFunctionTable() {
AvahiFunctionLoad(service_resolver_free);
AvahiFunctionLoad(entry_group_new);
AvahiFunctionLoad(entry_group_free);
AvahiFunctionLoad(entry_group_add_service);
AvahiFunctionLoad(entry_group_add_service_strlst);
AvahiFunctionLoad(entry_group_reset);
AvahiFunctionLoad(entry_group_is_empty);

View File

@@ -263,6 +263,12 @@ class AvahiFunctionTable {
(AvahiClient*, AvahiEntryGroupCallback, void*));
AvahiFunction(entry_group_free, int, (AvahiEntryGroup*));
AvahiFunction(entry_group_add_service, int,
(AvahiEntryGroup * group, AvahiIfIndex interface,
AvahiProtocol protocol, AvahiPublishFlags flags,
const char* name, const char* type, const char* domain,
const char* host, uint16_t port, ...));
AvahiFunction(entry_group_add_service_strlst, int,
(AvahiEntryGroup * group, AvahiIfIndex interface,
AvahiProtocol protocol, AvahiPublishFlags flags,

View File

@@ -16,7 +16,7 @@ using namespace wpi;
struct MulticastServiceAnnouncer::Impl {
AvahiFunctionTable& table = AvahiFunctionTable::Get();
std::shared_ptr<AvahiThread> thread = AvahiThread::Get();
AvahiClient* client;
AvahiClient* client = nullptr;
AvahiEntryGroup* group = nullptr;
std::string serviceName;
std::string serviceType;
@@ -46,18 +46,22 @@ MulticastServiceAnnouncer::Impl::Impl(std::string_view serviceName,
this->serviceType = serviceType;
this->port = port;
std::vector<std::string> txts;
for (auto&& i : txt) {
txts.push_back(fmt::format("{}={}", i.first, i.second));
}
if (txt.empty()) {
this->stringList = nullptr;
} else {
std::vector<std::string> txts;
for (auto&& i : txt) {
txts.push_back(fmt::format("{}={}", i.first, i.second));
}
std::vector<const char*> txtArr;
for (auto&& i : txts) {
txtArr.push_back(i.c_str());
}
std::vector<const char*> txtArr;
for (auto&& i : txts) {
txtArr.push_back(i.c_str());
}
this->stringList =
this->table.string_list_new_from_array(txtArr.data(), txtArr.size());
this->stringList =
this->table.string_list_new_from_array(txtArr.data(), txtArr.size());
}
}
static void RegisterService(AvahiClient* client,
@@ -85,11 +89,19 @@ static void RegisterService(AvahiClient* client,
while (true) {
if (impl->table.entry_group_is_empty(impl->group)) {
auto ret = impl->table.entry_group_add_service_strlst(
impl->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
AVAHI_PUBLISH_USE_MULTICAST, impl->serviceName.c_str(),
impl->serviceType.c_str(), "local", nullptr, impl->port,
impl->stringList);
int ret = 0;
if (impl->stringList == nullptr) {
ret = impl->table.entry_group_add_service(
impl->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
AVAHI_PUBLISH_USE_MULTICAST, impl->serviceName.c_str(),
impl->serviceType.c_str(), "local", nullptr, impl->port, nullptr);
} else {
ret = impl->table.entry_group_add_service_strlst(
impl->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
AVAHI_PUBLISH_USE_MULTICAST, impl->serviceName.c_str(),
impl->serviceType.c_str(), "local", nullptr, impl->port,
impl->stringList);
}
if (ret == AVAHI_ERR_COLLISION) {
// Local collision
char* newName =
@@ -121,6 +133,12 @@ static void ClientCallback(AvahiClient* client, AvahiClientState state,
}
}
MulticastServiceAnnouncer::MulticastServiceAnnouncer(
std::string_view serviceName, std::string_view serviceType, int port) {
wpi::span<const std::pair<std::string_view, std::string_view>> txt;
pImpl = std::make_unique<Impl>(serviceName, serviceType, port, txt);
}
MulticastServiceAnnouncer::MulticastServiceAnnouncer(
std::string_view serviceName, std::string_view serviceType, int port,
wpi::span<const std::pair<std::string, std::string>> txt) {