Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zwift #40

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/include/qmdnsengine/hostname.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

#include "qmdnsengine_export.h"

namespace QMdnsEngine
{
namespace QMdnsEngine {

class AbstractServer;

Expand All @@ -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
*
Expand All @@ -77,19 +79,17 @@ class QMDNSENGINE_EXPORT Hostname : public QObject
*/
QByteArray hostname() const;

Q_SIGNALS:
Q_SIGNALS:

/**
* @brief Indicate that the current hostname has changed
* @param hostname new hostname
*/
void hostnameChanged(const QByteArray &hostname);

private:

private:
HostnamePrivate *const d;
};

}

#endif // QMDNSENGINE_HOSTNAME_H
50 changes: 19 additions & 31 deletions src/src/hostname.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(&registrationTimer, &QTimer::timeout, this, &HostnamePrivate::onRegistrationTimeout);
connect(&rebroadcastTimer, &QTimer::timeout, this, &HostnamePrivate::onRebroadcastTimeout);
Expand All @@ -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;
Expand All @@ -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

Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -157,27 +153,19 @@ void HostnamePrivate::onRegistrationTimeout()
rebroadcastTimer.start();
}

void HostnamePrivate::onRebroadcastTimeout()
{
void HostnamePrivate::onRebroadcastTimeout() {
hostnamePrev = hostname;
hostnameRegistered = false;
hostnameSuffix = 1;

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; }
17 changes: 7 additions & 10 deletions src/src/hostname_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
52 changes: 24 additions & 28 deletions src/src/provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* IN THE SOFTWARE.
*/

#include <QNetworkInterface>
#include <qmdnsengine/abstractserver.h>
#include <qmdnsengine/dns.h>
#include <qmdnsengine/hostname.h>
Expand All @@ -32,17 +33,12 @@
#include <qmdnsengine/query.h>

#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);

Expand All @@ -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;
Expand All @@ -72,16 +66,14 @@ void ProviderPrivate::announce()
server->sendMessageToAll(message);
}

void ProviderPrivate::confirm()
{
void ProviderPrivate::confirm() {
// Confirm that the desired name is unique through probing

if (prober) {
delete prober;
}
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) {
Expand All @@ -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

Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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
Expand Down
Loading