Handle TXT records in avahi_publisher

This commit is contained in:
Guillaume Meunier
2023-12-16 14:16:47 +01:00
parent 94e60d4e8b
commit 49d1a8f26f
2 changed files with 19 additions and 4 deletions
+16 -3
View File
@@ -6,6 +6,7 @@
#include <avahi-common/error.h> #include <avahi-common/error.h>
#include <avahi-common/malloc.h> #include <avahi-common/malloc.h>
#include <avahi-common/simple-watch.h> #include <avahi-common/simple-watch.h>
#include <avahi-common/strlst.h>
#include <iostream> #include <iostream>
#include <string.h> #include <string.h>
@@ -34,16 +35,28 @@ void avahi_publisher::create_service(AvahiClient * client)
if (!name) if (!name)
name = avahi_strdup("WiVRn"); name = avahi_strdup("WiVRn");
std::vector<const char *> txt_array;
for(const auto& i: txt)
{
txt_array.push_back(i.c_str());
}
AvahiStringList* txt_list = txt_array.empty() ? nullptr : avahi_string_list_new_from_array(txt_array.data(), txt_array.size());
int ret; int ret;
do do
{ {
ret = avahi_entry_group_add_service(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name, type.c_str(), nullptr, nullptr, port, nullptr);
ret = avahi_entry_group_add_service_strlst(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name, type.c_str(), nullptr, nullptr, port, txt_list);
if (ret == AVAHI_ERR_COLLISION) if (ret == AVAHI_ERR_COLLISION)
{ {
alt_name(); alt_name();
} }
} while (ret == AVAHI_ERR_COLLISION); } while (ret == AVAHI_ERR_COLLISION);
avahi_string_list_free(txt_list);
if (ret < 0) if (ret < 0)
{ {
std::cerr << "Cannot add service: " << avahi_strerror(ret) << std::endl; std::cerr << "Cannot add service: " << avahi_strerror(ret) << std::endl;
@@ -105,8 +118,8 @@ void avahi_publisher::client_callback(AvahiClient * s,
} }
} }
avahi_publisher::avahi_publisher(const char * name, std::string type, int port) : avahi_publisher::avahi_publisher(const char * name, std::string type, int port, std::vector<std::string> txt) :
name(avahi_strdup(name)), type(std::move(type)), port(port) name(avahi_strdup(name)), type(std::move(type)), port(port), txt(txt)
{ {
avahi_poll = avahi_simple_poll_new(); avahi_poll = avahi_simple_poll_new();
+3 -1
View File
@@ -4,6 +4,7 @@
#include <avahi-common/simple-watch.h> #include <avahi-common/simple-watch.h>
#include <avahi-common/watch.h> #include <avahi-common/watch.h>
#include <string> #include <string>
#include <vector>
class avahi_publisher class avahi_publisher
{ {
@@ -11,6 +12,7 @@ class avahi_publisher
char * name{}; char * name{};
std::string type; std::string type;
int port; int port;
std::vector<std::string> txt;
AvahiSimplePoll * avahi_poll{}; AvahiSimplePoll * avahi_poll{};
AvahiClient * avahi_client{}; AvahiClient * avahi_client{};
@@ -29,7 +31,7 @@ class avahi_publisher
void * userdata /**< The user data that was passed to avahi_client_new() */); void * userdata /**< The user data that was passed to avahi_client_new() */);
public: public:
avahi_publisher(const char * name, std::string type, int port); avahi_publisher(const char * name, std::string type, int port, std::vector<std::string> txt = {});
~avahi_publisher(); ~avahi_publisher();