Skip to content
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
3 changes: 3 additions & 0 deletions src/src/prober.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* IN THE SOFTWARE.
*/

#include <QDebug>
#include <qmdnsengine/abstractserver.h>
#include <qmdnsengine/dns.h>
#include <qmdnsengine/message.h>
Expand Down Expand Up @@ -50,6 +51,7 @@ ProberPrivate::ProberPrivate(Prober *prober, AbstractServer *server, const Recor

timer.setSingleShot(true);

qDebug() << "ProberPrivate::ProberPrivate()" << record.name();
assertRecord();
}

Expand Down Expand Up @@ -94,6 +96,7 @@ void ProberPrivate::onMessageReceived(const Message &message)
void ProberPrivate::onTimeout()
{
confirmed = true;
qDebug() << "ProberPrivate::onTimeout()";
emit q->nameConfirmed(proposedRecord.name());
}

Expand Down
Loading