Add txt records to discovered services

This commit is contained in:
Jonathan Bagg
2017-03-15 20:32:28 -04:00
parent 9a6235509c
commit 195e740ceb
6 changed files with 46 additions and 7 deletions

View File

@ -97,6 +97,12 @@ If you are browsing for services published using both ipv4 and ipv6 ( QAbstractS
Only one browser can be in use per instance of QzeroConf.
**Txt records** are placed into a QMap called txt within the discovered service. For example, the txt record value of "Qt=The Best!" can be retrieved with the code...
```c++
qDebug() << zcs->txt["Qt"]
```
### Build Dependencies
Qt5

View File

@ -140,7 +140,7 @@ public:
const char *host_name,
const AvahiAddress *address,
uint16_t port,
AvahiStringList *,
AvahiStringList *txt,
AvahiLookupResultFlags,
AVAHI_GCC_UNUSED void* userdata)
{
@ -161,12 +161,21 @@ public:
zcs->host = host_name;
zcs->interfaceIndex = interface;
zcs->port = port;
while (txt) // get txt records
{
QByteArray avahiText((const char *)txt->text, txt->size);
QList<QByteArray> pair = avahiText.split('=');
if (pair.size() == 2)
zcs->txt[pair.at(0)] = pair.at(1);
else
zcs->txt[pair.at(0)] = "";
txt = txt->next;
}
ref->pub->services.insert(key, zcs);
}
char a[AVAHI_ADDRESS_STR_MAX];
avahi_address_snprint(a, sizeof(a), address);
// fixme maybe add type, and txt records
if (protocol == AVAHI_PROTO_INET6)
zcs->ipv6 = QHostAddress(a);
else if (protocol == AVAHI_PROTO_INET)

View File

@ -152,7 +152,7 @@ public:
const char *host_name,
const AvahiAddress *address,
uint16_t port,
AvahiStringList *,
AvahiStringList *txt,
AvahiLookupResultFlags,
AVAHI_GCC_UNUSED void* userdata)
{
@ -173,12 +173,21 @@ public:
zcs->host = host_name;
zcs->interfaceIndex = interface;
zcs->port = port;
while (txt) // get txt records
{
QByteArray avahiText((const char *)txt->text, txt->size);
QList<QByteArray> pair = avahiText.split('=');
if (pair.size() == 2)
zcs->txt[pair.at(0)] = pair.at(1);
else
zcs->txt[pair.at(0)] = "";
txt = txt->next;
}
ref->pub->services.insert(key, zcs);
}
char a[AVAHI_ADDRESS_STR_MAX];
avahi_address_snprint(a, sizeof(a), address);
// fixme maybe add type, and txt records
if (protocol == AVAHI_PROTO_INET6)
zcs->ipv6 = QHostAddress(a);
else if (protocol == AVAHI_PROTO_INET)

View File

@ -142,9 +142,6 @@ void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags
const char *hostName, quint16 port, quint16 txtLen,
const char * txtRecord, void *userdata)
{
Q_UNUSED(txtLen);
Q_UNUSED(txtRecord);
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
if (err != kDNSServiceErr_NoError) {
@ -152,6 +149,22 @@ void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags
return;
}
qint16 recLen;
while (txtLen > 0) // add txt records
{
recLen = txtRecord[0];
txtRecord++;
QByteArray avahiText((const char *)txtRecord, recLen);
QList<QByteArray> pair = avahiText.split('=');
if (pair.size() == 2)
ref->newService->txt[pair.at(0)] = pair.at(1);
else
ref->newService->txt[pair.at(0)] = "";
txtLen-= recLen + 1;
txtRecord+= recLen;
}
ref->newService->port = qFromBigEndian<quint16>(port);
err = DNSServiceGetAddrInfo(&ref->resolver, kDNSServiceFlagsForceMulticast, interfaceIndex, ref->protocol, hostName, (DNSServiceGetAddrInfoReply) addressReply, ref);
if (err == kDNSServiceErr_NoError) {

View File

@ -106,6 +106,7 @@ void mainWindow::startPublish()
return;
publishEnabled = 1;
zeroConf.clearServiceTxtRecords();
zeroConf.addServiceTxtRecord("Qt", "the best!");
zeroConf.addServiceTxtRecord("ZeroConf is nice too");
zeroConf.startServicePublish(buildName().toUtf8(), "_qtzeroconf_test._tcp", "local", 11437);

View File

@ -52,6 +52,7 @@ struct QZeroConfService
QHostAddress ipv6;
quint32 interfaceIndex;
quint16 port;
QMap <QByteArray, QByteArray> txt;
};
class QZeroConfPrivate;