Add zeroconf TxtRecords (#449)

* txtrecords

* typo
This commit is contained in:
brindosch 2017-07-05 16:54:41 +02:00 committed by GitHub
parent c0336b640f
commit e98122c9cb
4 changed files with 34 additions and 8 deletions

View File

@ -43,7 +43,7 @@ public:
BonjourServiceRegister(QObject *parent = 0);
~BonjourServiceRegister();
void registerService(const BonjourRecord &record, quint16 servicePort);
void registerService(const BonjourRecord &record, quint16 servicePort, std::vector<std::pair<std::string, std::string>> txt);
inline BonjourRecord registeredRecord() const {return finalRecord; }
signals:

View File

@ -48,7 +48,7 @@ BonjourServiceRegister::~BonjourServiceRegister()
}
}
void BonjourServiceRegister::registerService(const BonjourRecord &record, quint16 servicePort)
void BonjourServiceRegister::registerService(const BonjourRecord &record, quint16 servicePort, std::vector<std::pair<std::string, std::string>> txt)
{
if (dnssref)
{
@ -62,10 +62,25 @@ void BonjourServiceRegister::registerService(const BonjourRecord &record, quint1
}
#endif
// create txt record
TXTRecordRef txtRec;
TXTRecordCreate(&txtRec,0,NULL);
// add txt records
if(!txt.empty())
{
for(std::vector<std::pair<std::string, std::string> >::const_iterator it = txt.begin(); it != txt.end(); ++it)
{
//Debug(Logger::getInstance("BonJour"), "TXTRecord: key:%s, value:%s",it->first.c_str(),it->second.c_str());
uint8_t txtLen = (uint8_t)strlen(it->second.c_str());
TXTRecordSetValue(&txtRec, it->first.c_str(), txtLen, it->second.c_str());
}
}
DNSServiceErrorType err = DNSServiceRegister(&dnssref, 0, 0, record.serviceName.toUtf8().constData(),
record.registeredType.toUtf8().constData(),
(record.replyDomain.isEmpty() ? 0 : record.replyDomain.toUtf8().constData()),
0, bigEndianPort, 0, 0, bonjourRegisterService, this);
0, bigEndianPort, TXTRecordGetLength(&txtRec), TXTRecordGetBytesPtr(&txtRec), bonjourRegisterService, this);
if (err != kDNSServiceErr_NoError)
{
emit error(err);

View File

@ -11,6 +11,7 @@
#include <QHostInfo>
#include <bonjour/bonjourserviceregister.h>
#include <bonjour/bonjourrecord.h>
#include <HyperionConfig.h>
#include <exception>
StaticFileServing::StaticFileServing (Hyperion *hyperion, QString baseUrl, quint16 port, QObject * parent)
@ -47,10 +48,16 @@ void StaticFileServing::onServerStarted (quint16 port)
const QJsonObject & generalConfig = _hyperion->getQJsonConfig()["general"].toObject();
const QString mDNSDescr = generalConfig["name"].toString("") + "@" + QHostInfo::localHostName() + ":" + QString::number(port);
// txt record for zeroconf
QString id = _hyperion->id;
std::string version = HYPERION_VERSION;
std::vector<std::pair<std::string, std::string> > txtRecord = {{"id",id.toStdString()},{"version",version}};
BonjourServiceRegister *bonjourRegister_http = new BonjourServiceRegister();
bonjourRegister_http->registerService(
BonjourRecord(mDNSDescr, "_hyperiond-http._tcp", QString()),
port
port,
txtRecord
);
Debug(_log, "Web Config mDNS responder started");
}

View File

@ -375,26 +375,30 @@ void HyperionDaemon::startNetworkServices()
// zeroconf description - $leddevicename@$hostname
const QJsonObject & generalConfig = _qconfig["general"].toObject();
const QString mDNSDescr = generalConfig["name"].toString("") + "@" + QHostInfo::localHostName();
// txt record for zeroconf
QString id = _hyperion->id;
std::string version = HYPERION_VERSION;
std::vector<std::pair<std::string, std::string> > txtRecord = {{"id",id.toStdString()},{"version",version}};
// zeroconf udp listener
// zeroconf udp listener
if (_udpListener != nullptr)
{
BonjourServiceRegister *bonjourRegister_udp = new BonjourServiceRegister();
bonjourRegister_udp->registerService(
BonjourRecord(mDNSDescr + ":" + QString::number(_udpListener->getPort()), "_hyperiond-udp._udp", QString()), _udpListener->getPort() );
BonjourRecord(mDNSDescr + ":" + QString::number(_udpListener->getPort()), "_hyperiond-udp._udp", QString()), _udpListener->getPort(), txtRecord);
Debug(_log, "UDP LIstener mDNS responder started");
}
// zeroconf json
BonjourServiceRegister *bonjourRegister_json = new BonjourServiceRegister();
bonjourRegister_json->registerService(
BonjourRecord(mDNSDescr + ":" + QString::number(_jsonServer->getPort()), "_hyperiond-json._tcp", QString()), _jsonServer->getPort());
BonjourRecord(mDNSDescr + ":" + QString::number(_jsonServer->getPort()), "_hyperiond-json._tcp", QString()), _jsonServer->getPort(), txtRecord);
Debug(_log, "Json mDNS responder started");
// zeroconf proto
BonjourServiceRegister *bonjourRegister_proto = new BonjourServiceRegister();
bonjourRegister_proto->registerService(
BonjourRecord(mDNSDescr + ":" + QString::number(_jsonServer->getPort()), "_hyperiond-proto._tcp", QString()), _protoServer->getPort());
BonjourRecord(mDNSDescr + ":" + QString::number(_jsonServer->getPort()), "_hyperiond-proto._tcp", QString()), _protoServer->getPort(), txtRecord);
Debug(_log, "Proto mDNS responder started");
}