2013-01-17 14:11:15 +01:00
|
|
|
#include "tinysvcmdns.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "mdnsd.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <QHostInfo>
|
|
|
|
#include <QNetworkInterface>
|
2013-02-19 15:52:17 +01:00
|
|
|
#include <QtEndian>
|
2013-01-17 14:11:15 +01:00
|
|
|
|
|
|
|
#include "core/logging.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
uint32_t GetLocalIPAddress() {
|
|
|
|
QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
|
|
|
|
foreach (const QHostAddress& address, addresses) {
|
|
|
|
// TODO: Add ipv6 support to tinysvcmdns.
|
|
|
|
if (address.protocol() == QAbstractSocket::IPv4Protocol &&
|
|
|
|
!address.isInSubnet(QHostAddress::parseSubnet("127.0.0.1/8"))) {
|
2013-02-19 15:52:17 +01:00
|
|
|
return qToBigEndian(address.toIPv4Address());
|
2013-01-17 14:11:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TinySVCMDNS::TinySVCMDNS()
|
|
|
|
: mdnsd_(NULL) {
|
|
|
|
uint32_t ip_address = GetLocalIPAddress();
|
|
|
|
if (ip_address == 0) {
|
|
|
|
qLog(Warning) << "Could not publish service over mDNS as there is no"
|
|
|
|
<< "non-local IPv4 interface";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mdnsd_ = mdnsd_start();
|
2013-02-19 15:52:17 +01:00
|
|
|
QString host = QHostInfo::localHostName();
|
2013-01-17 14:11:15 +01:00
|
|
|
mdnsd_set_hostname(
|
|
|
|
mdnsd_,
|
2013-02-19 15:52:17 +01:00
|
|
|
QString(host + ".local").toUtf8().constData(),
|
2013-01-17 14:11:15 +01:00
|
|
|
ip_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
TinySVCMDNS::~TinySVCMDNS() {
|
|
|
|
if (mdnsd_) {
|
|
|
|
mdnsd_stop(mdnsd_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TinySVCMDNS::Publish(
|
|
|
|
const QString& domain,
|
|
|
|
const QString& type,
|
|
|
|
const QString& name,
|
|
|
|
quint16 port) {
|
|
|
|
if (!mdnsd_) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-18 18:08:52 +01:00
|
|
|
|
|
|
|
// Some pointless text, so tinymDNS publishes the service correctly.
|
|
|
|
const char* txt[] = {
|
|
|
|
"cat=nyan",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-01-17 14:11:15 +01:00
|
|
|
mdnsd_register_svc(
|
|
|
|
mdnsd_,
|
|
|
|
name.toUtf8().constData(),
|
|
|
|
QString(type + ".local").toUtf8().constData(),
|
|
|
|
port,
|
|
|
|
NULL,
|
2013-02-18 18:08:52 +01:00
|
|
|
txt);
|
2013-01-17 14:11:15 +01:00
|
|
|
}
|