From e4e43089425e1abf1583094db29dff1d023b2ffd Mon Sep 17 00:00:00 2001 From: Jonathan Bagg Date: Fri, 10 Mar 2017 20:08:54 -0500 Subject: [PATCH] Add txt records to service publication. --- avahiclient.cpp | 28 +++++++++++++++++++++++++--- avahicore.cpp | 25 ++++++++++++++++++++++++- bonjour.cpp | 22 +++++++++++++++++++++- bonjour_p.h | 1 + example/window.cpp | 3 +++ qzeroconf.h | 3 +++ 6 files changed, 77 insertions(+), 5 deletions(-) diff --git a/avahiclient.cpp b/avahiclient.cpp index 3f350e3..79d154e 100644 --- a/avahiclient.cpp +++ b/avahiclient.cpp @@ -39,6 +39,7 @@ public: { qint32 error; + txt = NULL; pub = parent; group = NULL; browser = NULL; @@ -72,7 +73,7 @@ public: emit ref->pub->error(QZeroConf::serviceRegistrationFailed); break; case AVAHI_ENTRY_GROUP_UNCOMMITED: - ret = avahi_entry_group_add_service(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UPDATE, ref->name.toUtf8(), ref->type.toUtf8(), ref->domain.toUtf8(), NULL, ref->port, NULL); + ret = avahi_entry_group_add_service_strlst(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UPDATE, ref->name.toUtf8(), ref->type.toUtf8(), ref->domain.toUtf8(), NULL, ref->port, ref->txt); if (ret < 0) { avahi_entry_group_free(g); ref->group = NULL; @@ -197,10 +198,10 @@ public: const AvahiPoll *poll; AvahiClient *client; AvahiEntryGroup *group; + AvahiServiceBrowser *browser; + AvahiStringList *txt; QString name, type, domain; quint16 port; - - AvahiServiceBrowser *browser; }; @@ -211,6 +212,7 @@ QZeroConf::QZeroConf() QZeroConf::~QZeroConf() { + avahi_string_list_free(pri->txt); pri->broswerCleanUp(); if (pri->client) avahi_client_free(pri->client); @@ -242,6 +244,26 @@ void QZeroConf::stopServicePublish(void) } } +// http://www.zeroconf.org/rendezvous/txtrecords.html + +void QZeroConf::addServiceTxtRecord(QString nameOnly) +{ + pri->txt = avahi_string_list_add(pri->txt, nameOnly.toUtf8()); +} + +void QZeroConf::addServiceTxtRecord(QString name, QString value) +{ + name.append("="); + name.append(value); + addServiceTxtRecord(name); +} + +void QZeroConf::clearServiceTxtRecords() +{ + avahi_string_list_free(pri->txt); + pri->txt = NULL; +} + void QZeroConf::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol) { AvahiProtocol avahiProtocol; diff --git a/avahicore.cpp b/avahicore.cpp index c547067..2787c8c 100644 --- a/avahicore.cpp +++ b/avahicore.cpp @@ -43,6 +43,7 @@ public: pub = parent; group = NULL; browser = NULL; + txt = NULL; ready = 0; registerWaiting = 0; @@ -214,7 +215,7 @@ public: return; } - ret = avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UPDATE, name, type, domain, NULL, port, NULL); + ret = avahi_server_add_service_strlst(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UPDATE, name, type, domain, NULL, port, txt); if (ret < 0) { avahi_s_entry_group_free(group); group = NULL; @@ -236,6 +237,7 @@ public: AvahiServerConfig config; AvahiSEntryGroup *group; AvahiSServiceBrowser *browser; + AvahiStringList *txt; bool ready, registerWaiting; QString name, type, domain; qint32 port; @@ -249,6 +251,7 @@ QZeroConf::QZeroConf() QZeroConf::~QZeroConf() { + avahi_string_list_free(pri->txt); pri->broswerCleanUp(); avahi_server_config_free(&pri->config); if (pri->server) @@ -281,6 +284,26 @@ void QZeroConf::stopServicePublish(void) } } +// http://www.zeroconf.org/rendezvous/txtrecords.html + +void QZeroConf::addServiceTxtRecord(QString nameOnly) +{ + pri->txt = avahi_string_list_add(pri->txt, nameOnly.toUtf8()); +} + +void QZeroConf::addServiceTxtRecord(QString name, QString value) +{ + name.append("="); + name.append(value); + addServiceTxtRecord(name); +} + +void QZeroConf::clearServiceTxtRecords() +{ + avahi_string_list_free(pri->txt); + pri->txt = NULL; +} + void QZeroConf::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol) { AvahiProtocol avahiProtocol; diff --git a/bonjour.cpp b/bonjour.cpp index a0e9e32..5639f5b 100644 --- a/bonjour.cpp +++ b/bonjour.cpp @@ -279,7 +279,9 @@ void QZeroConf::startServicePublish(const char *name, const char *type, const ch type, domain, NULL, - qFromBigEndian(port), NULL, NULL, (DNSServiceRegisterReply) QZeroConfPrivate::registerCallback, pri); + qFromBigEndian(port), + pri->txt.size(), pri->txt.data(), + (DNSServiceRegisterReply) QZeroConfPrivate::registerCallback, pri); if (err == kDNSServiceErr_NoError) { int sockfd = DNSServiceRefSockFD(pri->dnssRef); @@ -303,6 +305,24 @@ void QZeroConf::stopServicePublish(void) pri->cleanUp(pri->dnssRef); } +void QZeroConf::addServiceTxtRecord(QString nameOnly) +{ + pri->txt.append((quint8) nameOnly.size()); + pri->txt.append(nameOnly.toUtf8()); +} + +void QZeroConf::addServiceTxtRecord(QString name, QString value) +{ + name.append("="); + name.append(value); + addServiceTxtRecord(name); +} + +void QZeroConf::clearServiceTxtRecords() +{ + pri->txt.clear(); +} + void QZeroConf::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol) { DNSServiceErrorType err; diff --git a/bonjour_p.h b/bonjour_p.h index 0023e78..907634c 100644 --- a/bonjour_p.h +++ b/bonjour_p.h @@ -66,6 +66,7 @@ public: QSocketNotifier *bs, *browserSocket, *resolverSocket, *addressSocket; QZeroConfService *newService; QList work; + QByteArray txt; public slots: void bsRead(); diff --git a/example/window.cpp b/example/window.cpp index a04057e..6b1dc9a 100644 --- a/example/window.cpp +++ b/example/window.cpp @@ -105,6 +105,9 @@ void mainWindow::startPublish() if (publishEnabled) return; publishEnabled = 1; + + zeroConf.addServiceTxtRecord("Qt", "the best!"); + zeroConf.addServiceTxtRecord("ZeroConf is nice too"); zeroConf.startServicePublish(buildName().toUtf8(), "_qtzeroconf_test._tcp", "local", 11437); } diff --git a/qzeroconf.h b/qzeroconf.h index c496206..141a253 100644 --- a/qzeroconf.h +++ b/qzeroconf.h @@ -79,6 +79,9 @@ public: } void startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol); void stopBrowser(void); + void addServiceTxtRecord(QString nameOnly); + void addServiceTxtRecord(QString name, QString value); + void clearServiceTxtRecords(); signals: void servicePublished(void);