diff --git a/src/include/qmdnsengine/hostname.h b/src/include/qmdnsengine/hostname.h index fe3c79b..90f4ec9 100644 --- a/src/include/qmdnsengine/hostname.h +++ b/src/include/qmdnsengine/hostname.h @@ -29,8 +29,7 @@ #include "qmdnsengine_export.h" -namespace QMdnsEngine -{ +namespace QMdnsEngine { class AbstractServer; @@ -51,17 +50,20 @@ class QMDNSENGINE_EXPORT HostnamePrivate; * }); * @endcode */ -class QMDNSENGINE_EXPORT Hostname : public QObject -{ +class QMDNSENGINE_EXPORT Hostname : public QObject { Q_OBJECT -public: - + public: /** * @brief Create a new hostname */ Hostname(AbstractServer *server, QObject *parent = 0); + /** + * @brief Create a new hostname with desired value to register + */ + Hostname(AbstractServer *server, const QByteArray &desired, QObject *parent = 0); + /** * @brief Determine if a hostname has been registered * @@ -77,7 +79,7 @@ class QMDNSENGINE_EXPORT Hostname : public QObject */ QByteArray hostname() const; -Q_SIGNALS: + Q_SIGNALS: /** * @brief Indicate that the current hostname has changed @@ -85,11 +87,9 @@ class QMDNSENGINE_EXPORT Hostname : public QObject */ void hostnameChanged(const QByteArray &hostname); -private: - + private: HostnamePrivate *const d; }; - } #endif // QMDNSENGINE_HOSTNAME_H diff --git a/src/src/hostname.cpp b/src/src/hostname.cpp index 94d9931..a8c5a29 100644 --- a/src/src/hostname.cpp +++ b/src/src/hostname.cpp @@ -39,10 +39,10 @@ using namespace QMdnsEngine; HostnamePrivate::HostnamePrivate(Hostname *hostname, AbstractServer *server) - : QObject(hostname), - server(server), - q(hostname) -{ + : HostnamePrivate(hostname, QByteArray(), server) {} + +HostnamePrivate::HostnamePrivate(Hostname *hostname, const QByteArray &desired, AbstractServer *server) + : QObject(hostname), server(server), desiredHostname(desired), q(hostname) { connect(server, &AbstractServer::messageReceived, this, &HostnamePrivate::onMessageReceived); connect(®istrationTimer, &QTimer::timeout, this, &HostnamePrivate::onRegistrationTimeout); connect(&rebroadcastTimer, &QTimer::timeout, this, &HostnamePrivate::onRebroadcastTimeout); @@ -57,17 +57,16 @@ HostnamePrivate::HostnamePrivate(Hostname *hostname, AbstractServer *server) onRebroadcastTimeout(); } -void HostnamePrivate::assertHostname() -{ +void HostnamePrivate::assertHostname() { // Begin with the local hostname and replace any "." with "-" (I'm looking // at you, macOS) - QByteArray localHostname = QHostInfo::localHostName().toUtf8(); + QByteArray localHostname = desiredHostname.isEmpty() ? QHostInfo::localHostName().toUtf8() : desiredHostname; localHostname = localHostname.replace('.', '-'); // If the suffix > 1, then append a "-2", "-3", etc. to the hostname to // aid in finding one that is unique and not in use - hostname = (hostnameSuffix == 1 ? localHostname: - localHostname + "-" + QByteArray::number(hostnameSuffix)) + ".local."; + hostname = + (hostnameSuffix == 1 ? localHostname : localHostname + "-" + QByteArray::number(hostnameSuffix)) + ".local."; // Compose a query for A and AAAA records matching the hostname Query ipv4Query; @@ -86,8 +85,7 @@ void HostnamePrivate::assertHostname() registrationTimer.start(); } -bool HostnamePrivate::generateRecord(const QHostAddress &srcAddress, quint16 type, Record &record) -{ +bool HostnamePrivate::generateRecord(const QHostAddress &srcAddress, quint16 type, Record &record) { // Attempt to find the interface that corresponds with the provided // address and determine this device's address from the interface @@ -99,7 +97,7 @@ bool HostnamePrivate::generateRecord(const QHostAddress &srcAddress, quint16 typ for (const QNetworkAddressEntry &newEntry : entries) { QHostAddress address = newEntry.ip(); if ((address.protocol() == QAbstractSocket::IPv4Protocol && type == A) || - (address.protocol() == QAbstractSocket::IPv6Protocol && type == AAAA)) { + (address.protocol() == QAbstractSocket::IPv6Protocol && type == AAAA)) { record.setName(hostname); record.setType(type); record.setAddress(address); @@ -112,8 +110,7 @@ bool HostnamePrivate::generateRecord(const QHostAddress &srcAddress, quint16 typ return false; } -void HostnamePrivate::onMessageReceived(const Message &message) -{ +void HostnamePrivate::onMessageReceived(const Message &message) { if (message.isResponse()) { if (hostnameRegistered) { return; @@ -146,8 +143,7 @@ void HostnamePrivate::onMessageReceived(const Message &message) } } -void HostnamePrivate::onRegistrationTimeout() -{ +void HostnamePrivate::onRegistrationTimeout() { hostnameRegistered = true; if (hostname != hostnamePrev) { emit q->hostnameChanged(hostname); @@ -157,8 +153,7 @@ void HostnamePrivate::onRegistrationTimeout() rebroadcastTimer.start(); } -void HostnamePrivate::onRebroadcastTimeout() -{ +void HostnamePrivate::onRebroadcastTimeout() { hostnamePrev = hostname; hostnameRegistered = false; hostnameSuffix = 1; @@ -166,18 +161,11 @@ void HostnamePrivate::onRebroadcastTimeout() assertHostname(); } -Hostname::Hostname(AbstractServer *server, QObject *parent) - : QObject(parent), - d(new HostnamePrivate(this, server)) -{ -} +Hostname::Hostname(AbstractServer *server, QObject *parent) : QObject(parent), d(new HostnamePrivate(this, server)) {} -bool Hostname::isRegistered() const -{ - return d->hostnameRegistered; -} +Hostname::Hostname(AbstractServer *server, const QByteArray &desired, QObject *parent) + : QObject(parent), d(new HostnamePrivate(this, desired, server)) {} -QByteArray Hostname::hostname() const -{ - return d->hostname; -} +bool Hostname::isRegistered() const { return d->hostnameRegistered; } + +QByteArray Hostname::hostname() const { return d->hostname; } diff --git a/src/src/hostname_p.h b/src/src/hostname_p.h index 33261db..d1bfaa5 100644 --- a/src/src/hostname_p.h +++ b/src/src/hostname_p.h @@ -30,21 +30,19 @@ class QHostAddress; -namespace QMdnsEngine -{ +namespace QMdnsEngine { class AbstractServer; class Hostname; class Message; class Record; -class HostnamePrivate : public QObject -{ +class HostnamePrivate : public QObject { Q_OBJECT -public: - + public: HostnamePrivate(Hostname *hostname, AbstractServer *server); + HostnamePrivate(Hostname *hostname, const QByteArray &desired, AbstractServer *server); void assertHostname(); bool generateRecord(const QHostAddress &srcAddress, quint16 type, Record &record); @@ -53,23 +51,22 @@ class HostnamePrivate : public QObject QByteArray hostnamePrev; QByteArray hostname; + QByteArray desiredHostname; bool hostnameRegistered; int hostnameSuffix; QTimer registrationTimer; QTimer rebroadcastTimer; -private Q_SLOTS: + private Q_SLOTS: void onMessageReceived(const Message &message); void onRegistrationTimeout(); void onRebroadcastTimeout(); -private: - + private: Hostname *const q; }; - } #endif // QMDNSENGINE_HOSTNAME_P_H diff --git a/src/src/provider.cpp b/src/src/provider.cpp index 756d896..4fff244 100644 --- a/src/src/provider.cpp +++ b/src/src/provider.cpp @@ -22,6 +22,7 @@ * IN THE SOFTWARE. */ +#include #include #include #include @@ -32,17 +33,12 @@ #include #include "provider_p.h" +#include "localipaddress.h" using namespace QMdnsEngine; ProviderPrivate::ProviderPrivate(QObject *parent, AbstractServer *server, Hostname *hostname) - : QObject(parent), - server(server), - hostname(hostname), - prober(nullptr), - initialized(false), - confirmed(false) -{ + : QObject(parent), server(server), hostname(hostname), prober(nullptr), initialized(false), confirmed(false) { connect(server, &AbstractServer::messageReceived, this, &ProviderPrivate::onMessageReceived); connect(hostname, &Hostname::hostnameChanged, this, &ProviderPrivate::onHostnameChanged); @@ -53,15 +49,13 @@ ProviderPrivate::ProviderPrivate(QObject *parent, AbstractServer *server, Hostna txtProposed.setType(TXT); } -ProviderPrivate::~ProviderPrivate() -{ +ProviderPrivate::~ProviderPrivate() { if (confirmed) { farewell(); } } -void ProviderPrivate::announce() -{ +void ProviderPrivate::announce() { // Broadcast a message with each of the records Message message; @@ -72,8 +66,7 @@ void ProviderPrivate::announce() server->sendMessageToAll(message); } -void ProviderPrivate::confirm() -{ +void ProviderPrivate::confirm() { // Confirm that the desired name is unique through probing if (prober) { @@ -81,7 +74,6 @@ void ProviderPrivate::confirm() } prober = new Prober(server, srvProposed, this); connect(prober, &Prober::nameConfirmed, [this](const QByteArray &name) { - // If existing records were confirmed, indicate that they are no // longer valid if (confirmed) { @@ -103,8 +95,7 @@ void ProviderPrivate::confirm() }); } -void ProviderPrivate::farewell() -{ +void ProviderPrivate::farewell() { // Send a message indicating that the existing records are no longer valid // by setting their TTL to 0 @@ -114,19 +105,24 @@ void ProviderPrivate::farewell() announce(); } -void ProviderPrivate::publish() -{ +void ProviderPrivate::publish() { // Copy the proposed records over and announce them browsePtrRecord = browsePtrProposed; ptrRecord = ptrProposed; srvRecord = srvProposed; txtRecord = txtProposed; + + // zwift + ARecord.setType(A); + ARecord.setName(srvRecord.target()); + // set directly when we receive a message + // ARecord.setAddress(QHostAddress("172.31.100.161")); + announce(); } -void ProviderPrivate::onMessageReceived(const Message &message) -{ +void ProviderPrivate::onMessageReceived(const Message &message) { if (!confirmed || message.isResponse()) { return; } @@ -136,6 +132,8 @@ void ProviderPrivate::onMessageReceived(const Message &message) bool sendSrv = false; bool sendTxt = false; + ARecord.setAddress(localipaddress::getIP(message.address())); + // Determine which records to send based on the queries const auto queries = message.queries(); for (const Query &query : queries) { @@ -179,6 +177,9 @@ void ProviderPrivate::onMessageReceived(const Message &message) } if (sendSrv) { reply.addRecord(srvRecord); + + // zwift need it + reply.addRecord(ARecord); } if (sendTxt) { reply.addRecord(txtRecord); @@ -187,8 +188,7 @@ void ProviderPrivate::onMessageReceived(const Message &message) } } -void ProviderPrivate::onHostnameChanged(const QByteArray &newHostname) -{ +void ProviderPrivate::onHostnameChanged(const QByteArray &newHostname) { // Update the proposed SRV record srvProposed.setTarget(newHostname); @@ -199,13 +199,9 @@ void ProviderPrivate::onHostnameChanged(const QByteArray &newHostname) } Provider::Provider(AbstractServer *server, Hostname *hostname, QObject *parent) - : QObject(parent), - d(new ProviderPrivate(this, server, hostname)) -{ -} + : QObject(parent), d(new ProviderPrivate(this, server, hostname)) {} -void Provider::update(const Service &service) -{ +void Provider::update(const Service &service) { d->initialized = true; // Clean the service name diff --git a/src/src/provider_p.h b/src/src/provider_p.h index 28578f2..543f2f7 100644 --- a/src/src/provider_p.h +++ b/src/src/provider_p.h @@ -30,27 +30,24 @@ #include #include -namespace QMdnsEngine -{ +namespace QMdnsEngine { class AbstractServer; class Hostname; class Message; class Prober; -class ProviderPrivate : public QObject -{ +class ProviderPrivate : public QObject { Q_OBJECT -public: - + public: ProviderPrivate(QObject *parent, AbstractServer *server, Hostname *hostname); virtual ~ProviderPrivate(); void announce(); void confirm(); void farewell(); - void publish(); + void publish(); AbstractServer *server; Hostname *hostname; @@ -64,18 +61,19 @@ class ProviderPrivate : public QObject Record ptrRecord; Record srvRecord; Record txtRecord; + Record ARecord; Record browsePtrProposed; Record ptrProposed; Record srvProposed; Record txtProposed; -private Q_SLOTS: + private Q_SLOTS: void onMessageReceived(const Message &message); void onHostnameChanged(const QByteArray &hostname); }; -} +} // namespace QMdnsEngine #endif // QMDNSENGINE_PROVIDER_P_H