Clementine-audio-player-Mac.../src/networkremote/tinysvcmdns.cpp

81 lines
2.4 KiB
C++
Raw Normal View History

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) {
// 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
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);
}
TinySVCMDNS::TinySVCMDNS() {
// Get all network interfaces
QList<QNetworkInterface> network_interfaces =
QNetworkInterface::allInterfaces();
for (QNetworkInterface network_interface : network_interfaces) {
// Only use up and non loopback interfaces
if (network_interface.flags().testFlag(network_interface.IsUp) &&
!network_interface.flags().testFlag(network_interface.IsLoopBack)) {
uint32_t ipv4 = 0;
2013-03-09 12:04:17 +01:00
QString ipv6;
qLog(Debug) << "Interface" << network_interface.humanReadableName();
// Now check all network addresses for this device
QList<QNetworkAddressEntry> network_address_entries =
network_interface.addressEntries();
2013-03-08 19:04:26 +01:00
for (QNetworkAddressEntry network_address_entry :
network_address_entries) {
QHostAddress host_address = network_address_entry.ip();
if (host_address.protocol() == QAbstractSocket::IPv4Protocol) {
ipv4 = qToBigEndian(host_address.toIPv4Address());
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();
qLog(Debug) << " ipv6:" << host_address.toString();
}
}
// Now start the service
2013-03-09 12:04:17 +01:00
CreateMdnsd(ipv4, ipv6);
}
}
2013-01-17 14:11:15 +01:00
}
TinySVCMDNS::~TinySVCMDNS() {
for (mdnsd* mdnsd : mdnsd_) {
mdnsd_stop(mdnsd);
}
2013-01-17 14:11:15 +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.
const char* txt[] = {"cat=nyan", nullptr};
for (mdnsd* mdnsd : mdnsd_) {
mdnsd_register_svc(mdnsd, name.constData(),
QString(type + ".local").toUtf8().constData(), port,
nullptr, txt);
}
2013-01-17 14:11:15 +01:00
}