Skip to content
Permalink
Browse files

Merge PR #3127: Implement new hostname resolving infrastructure

  • Loading branch information...
mkrautz committed Jun 11, 2017
2 parents d472e06 + 8f8aa1a commit e913a441c1a55eca70e62e1e3ec17dfd174beeb3
@@ -0,0 +1,42 @@
// Copyright 2005-2017 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "murmur_pch.h"

#include "ServerAddress.h"

ServerAddress::ServerAddress()
: port(0) {}

ServerAddress::ServerAddress(HostAddress host_, unsigned short port_)
: host(host_)
, port(port_) {}

bool ServerAddress::isValid() const {
return host.isValid() && port != 0;
}

bool operator==(const ServerAddress &lhs, const ServerAddress &rhs) {
return lhs.host == rhs.host && lhs.port == rhs.port;
}

bool operator!=(const ServerAddress &lhs, const ServerAddress &rhs) {
return !operator==(lhs, rhs);
}

bool operator<(const ServerAddress &lhs, const ServerAddress &rhs) {
if (lhs.host < rhs.host) {
return true;
} else if (lhs.host == rhs.host) {
if (lhs.port < lhs.port) {
return true;
}
}
return false;
}

uint qHash(const ServerAddress &key) {
return qHash(key.host) ^ uint(key.port);
}
@@ -0,0 +1,48 @@
// Copyright 2005-2017 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_SERVERADDRESS_H_
#define MUMBLE_SERVERADDRESS_H_

#include <QtCore/QString>

#include "HostAddress.h"

/// ServerAddress represents a server
/// address consisting of a HostAddress
/// and a port.
struct ServerAddress {
HostAddress host;
unsigned short port;

/// Construct a default ServerAddress.
/// The default ServerAddress value is considered
/// invalid per the |isValid| method.
ServerAddress();

/// Construct a ServerAddress pointing to |host_| and |port_|.
ServerAddress(HostAddress host_, unsigned short port_);

/// Check whether the ServerAddress is valid.
/// A ServerAddress is valid if it has a valid |host|
/// and if its |port| > 0.
bool isValid() const;
};

/// Check whether the ServerAddresses |lhs| and |rhs| are equal.
bool operator==(const ServerAddress &lhs, const ServerAddress &rhs);

/// Check whether the ServerAddresses |lhs| and |rhs| are not equal.
bool operator!=(const ServerAddress &lhs, const ServerAddress &rhs);

/// Check whether ServerAddress |lhs| should be sorted before |rhs|.
/// This is implemented such that ServerAddress can be used in QMap.
bool operator<(const ServerAddress &lhs, const ServerAddress &rhs);

/// Implementation of qHash for ServerAddress, such that ServerAddress
/// can be used as a key in QHash, QMap, etc.
uint qHash(const ServerAddress &key);

#endif
@@ -0,0 +1,40 @@
// Copyright 2005-2017 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_MUMBLE_SERVERRESOLVER_H_
#define MUMBLE_MUMBLE_SERVERRESOLVER_H_

#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtCore/QList>

#include "Net.h" // for HostAddress
#include "ServerResolverRecord.h"

class ServerResolverPrivate;

class ServerResolver : public QObject {
private:
Q_OBJECT
Q_DISABLE_COPY(ServerResolver)
public:
ServerResolver(QObject *parent = NULL);

QString hostname();
quint16 port();

void resolve(QString hostname, quint16 port);
QList<ServerResolverRecord> records();

signals:
/// Resolved is fired once the ServerResolver
/// has resolved the server address.
void resolved();

private:
ServerResolverPrivate *d;
};

#endif
@@ -0,0 +1,32 @@
// Copyright 2005-2017 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "ServerResolverRecord.h"

ServerResolverRecord::ServerResolverRecord() {
}

ServerResolverRecord::ServerResolverRecord(QString hostname_, quint16 port_, qint64 priority_, QList<HostAddress> addresses_)
: m_hostname(hostname_)
, m_port(port_)
, m_priority(priority_)
, m_addresses(addresses_) {
}

qint64 ServerResolverRecord::priority() {
return m_priority;
}

QString ServerResolverRecord::hostname() {
return m_hostname;
}

quint16 ServerResolverRecord::port() {
return m_port;
}

QList<HostAddress> ServerResolverRecord::addresses() {
return m_addresses;
}
@@ -0,0 +1,31 @@
// Copyright 2005-2017 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_MUMBLE_SERVERRESOLVERRECORD_H_
#define MUMBLE_MUMBLE_SERVERRESOLVERRECORD_H_

#include <QtCore/QString>
#include <QtCore/QList>

#include "Net.h" // for HostAddress

class ServerResolverRecord {
public:
ServerResolverRecord();
ServerResolverRecord(QString hostname_, quint16 port_, qint64 priority_, QList<HostAddress> addresses_);

QString hostname();
quint16 port();
qint64 priority();
QList<HostAddress> addresses();

protected:
QString m_hostname;
quint16 m_port;
qint64 m_priority;
QList<HostAddress> m_addresses;
};

#endif
@@ -0,0 +1,102 @@
// Copyright 2005-2017 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "murmur_pch.h"

#include "ServerResolver.h"

#include <QtNetwork/QHostInfo>

class ServerResolverPrivate : public QObject {
private:
Q_OBJECT
Q_DISABLE_COPY(ServerResolverPrivate)
public:
ServerResolverPrivate(QObject *parent);

void resolve(QString hostname, quint16 port);
QList<ServerResolverRecord> records();

QString m_origHostname;
quint16 m_origPort;

QList<ServerResolverRecord> m_resolved;

signals:
void resolved();

public slots:
void hostResolved(QHostInfo hostInfo);
};

ServerResolverPrivate::ServerResolverPrivate(QObject *parent)
: QObject(parent)
, m_origPort(0) {
}

void ServerResolverPrivate::resolve(QString hostname, quint16 port) {
m_origHostname = hostname;
m_origPort = port;

QHostInfo::lookupHost(hostname, this, SLOT(hostResolved(QHostInfo)));
}

QList<ServerResolverRecord> ServerResolverPrivate::records() {
return m_resolved;
}

void ServerResolverPrivate::hostResolved(QHostInfo hostInfo) {
if (hostInfo.error() == QHostInfo::NoError) {
QList<QHostAddress> resolvedAddresses = hostInfo.addresses();

// Convert QHostAddress -> HostAddress.
QList<HostAddress> addresses;
foreach (QHostAddress ha, resolvedAddresses) {
addresses << HostAddress(ha);
}

m_resolved << ServerResolverRecord(m_origHostname, m_origPort, 0, addresses);
}

emit resolved();
}

ServerResolver::ServerResolver(QObject *parent)
: QObject(parent) {

d = new ServerResolverPrivate(this);
}

QString ServerResolver::hostname() {
if (d) {
return d->m_origHostname;
}

return QString();
}

quint16 ServerResolver::port() {
if (d) {
return d->m_origPort;
}

return 0;
}

void ServerResolver::resolve(QString hostname, quint16 port) {
if (d) {
connect(d, SIGNAL(resolved()), this, SIGNAL(resolved()));
d->resolve(hostname, port);
}
}

QList<ServerResolverRecord> ServerResolver::records() {
if (d) {
return d->records();
}
return QList<ServerResolverRecord>();
}

#include "ServerResolver_nosrv.moc"

0 comments on commit e913a44

Please sign in to comment.
You can’t perform that action at this time.