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"
|
|
|
|
|
2013-03-09 13:29:46 +01:00
|
|
|
void TinySVCMDNS::CreateMdnsd(uint32_t ipv4, QString ipv6) {
|
2013-03-08 15:20:31 +01:00
|
|
|
// Get our hostname
|
2013-02-19 15:52:17 +01:00
|
|
|
QString host = QHostInfo::localHostName();
|
2013-03-09 12:04:17 +01:00
|
|
|
|
|
|
|
// Start the service
|
|
|
|
mdnsd* mdnsd = mdnsd_start_bind(ipv4);
|
|
|
|
|
|
|
|
// Set our hostname
|
2014-02-07 16:34:20 +01:00
|
|
|
mdnsd_set_hostname(mdnsd, QString(host + ".local").toUtf8().constData(),
|
|
|
|
ipv4);
|
2013-03-09 12:04:17 +01:00
|
|
|
|
|
|
|
// Add to the list
|
|
|
|
mdnsd_.append(mdnsd);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
TinySVCMDNS::TinySVCMDNS() {
|
2013-03-08 15:20:31 +01:00
|
|
|
// Get all network interfaces
|
2014-02-07 16:34:20 +01:00
|
|
|
QList<QNetworkInterface> network_interfaces =
|
|
|
|
QNetworkInterface::allInterfaces();
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QNetworkInterface network_interface : network_interfaces) {
|
2013-03-08 15:20:31 +01:00
|
|
|
// Only use up and non loopback interfaces
|
2014-02-07 16:34:20 +01:00
|
|
|
if (network_interface.flags().testFlag(network_interface.IsUp) &&
|
|
|
|
!network_interface.flags().testFlag(network_interface.IsLoopBack)) {
|
2013-03-08 15:20:31 +01:00
|
|
|
uint32_t ipv4 = 0;
|
2013-03-09 12:04:17 +01:00
|
|
|
QString ipv6;
|
2013-03-19 22:24:56 +01:00
|
|
|
|
|
|
|
qLog(Debug) << "Interface" << network_interface.humanReadableName();
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2013-03-08 15:20:31 +01:00
|
|
|
// Now check all network addresses for this device
|
2014-02-07 16:34:20 +01:00
|
|
|
QList<QNetworkAddressEntry> network_address_entries =
|
|
|
|
network_interface.addressEntries();
|
2013-03-08 19:04:26 +01:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QNetworkAddressEntry network_address_entry :
|
|
|
|
network_address_entries) {
|
2013-03-08 15:20:31 +01:00
|
|
|
QHostAddress host_address = network_address_entry.ip();
|
|
|
|
if (host_address.protocol() == QAbstractSocket::IPv4Protocol) {
|
|
|
|
ipv4 = qToBigEndian(host_address.toIPv4Address());
|
2013-03-19 22:24:56 +01:00
|
|
|
qLog(Debug) << " ipv4:" << host_address.toString();
|
2013-03-09 12:04:17 +01:00
|
|
|
} else if (host_address.protocol() == QAbstractSocket::IPv6Protocol) {
|
|
|
|
ipv6 = host_address.toString();
|
2013-03-19 22:24:56 +01:00
|
|
|
qLog(Debug) << " ipv6:" << host_address.toString();
|
2013-03-08 15:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2013-03-08 15:20:31 +01:00
|
|
|
// Now start the service
|
2013-03-09 12:04:17 +01:00
|
|
|
CreateMdnsd(ipv4, ipv6);
|
2013-03-08 15:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-17 14:11:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TinySVCMDNS::~TinySVCMDNS() {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (mdnsd* mdnsd : mdnsd_) {
|
|
|
|
mdnsd_stop(mdnsd);
|
|
|
|
}
|
2013-01-17 14:11:15 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void TinySVCMDNS::PublishInternal(const QString& domain, const QString& type,
|
|
|
|
const QByteArray& name, quint16 port) {
|
2013-02-18 18:08:52 +01:00
|
|
|
|
|
|
|
// Some pointless text, so tinymDNS publishes the service correctly.
|
2014-02-07 16:34:20 +01:00
|
|
|
const char* txt[] = {"cat=nyan", nullptr};
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (mdnsd* mdnsd : mdnsd_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
mdnsd_register_svc(mdnsd, name.constData(),
|
|
|
|
QString(type + ".local").toUtf8().constData(), port,
|
|
|
|
nullptr, txt);
|
2013-03-08 15:20:31 +01:00
|
|
|
}
|
2013-01-17 14:11:15 +01:00
|
|
|
}
|