Merge pull request #3717 from birarda/network-timeout

don't allow a HifiSockAddr lookup to block application
This commit is contained in:
Brad Hefta-Gaub 2014-11-03 12:38:05 -08:00
commit c52b116ab1
4 changed files with 36 additions and 17 deletions

View file

@ -9,9 +9,9 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
#include <QtCore/QDataStream> #include <qdatastream.h>
#include <QtNetwork/QHostInfo> #include <qhostinfo.h>
#include <QtNetwork/QNetworkInterface> #include <qnetworkinterface.h>
#include "HifiSockAddr.h" #include "HifiSockAddr.h"
@ -36,14 +36,15 @@ HifiSockAddr::HifiSockAddr(const HifiSockAddr& otherSockAddr) {
_port = otherSockAddr._port; _port = otherSockAddr._port;
} }
HifiSockAddr::HifiSockAddr(const QString& hostname, quint16 hostOrderPort) { HifiSockAddr::HifiSockAddr(const QString& hostname, quint16 hostOrderPort) :
// lookup the IP by the hostname _address(hostname),
QHostInfo hostInfo = QHostInfo::fromName(hostname); _port(hostOrderPort)
foreach(const QHostAddress& address, hostInfo.addresses()) { {
if (address.protocol() == QAbstractSocket::IPv4Protocol) { // if we parsed an IPv4 address out of the hostname, don't look it up
_address = address; if (_address.protocol() != QAbstractSocket::IPv4Protocol) {
_port = hostOrderPort; // sync lookup the IP by the hostname
} int lookupID = QHostInfo::lookupHost(hostname, this, SLOT(handleLookupResult(QHostInfo)));
qDebug() << "Looking up IP address for hostname" << hostname << "- lookup ID is" << lookupID;
} }
} }
@ -75,6 +76,22 @@ bool HifiSockAddr::operator==(const HifiSockAddr& rhsSockAddr) const {
return _address == rhsSockAddr._address && _port == rhsSockAddr._port; return _address == rhsSockAddr._address && _port == rhsSockAddr._port;
} }
void HifiSockAddr::handleLookupResult(const QHostInfo& hostInfo) {
if (hostInfo.error() != QHostInfo::NoError) {
qDebug() << "Lookup failed for" << hostInfo.lookupId() << ":" << hostInfo.errorString();
}
foreach(const QHostAddress& address, hostInfo.addresses()) {
// just take the first IPv4 address
if (address.protocol() == QAbstractSocket::IPv4Protocol) {
_address = address;
qDebug() << "QHostInfo lookup result for"
<< hostInfo.hostName() << "with lookup ID" << hostInfo.lookupId() << "is" << address.toString();
break;
}
}
}
QDebug operator<<(QDebug debug, const HifiSockAddr& sockAddr) { QDebug operator<<(QDebug debug, const HifiSockAddr& sockAddr) {
debug.nospace() << sockAddr._address.toString().toLocal8Bit().constData() << ":" << sockAddr._port; debug.nospace() << sockAddr._address.toString().toLocal8Bit().constData() << ":" << sockAddr._port;
return debug.space(); return debug.space();

View file

@ -19,9 +19,10 @@
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
#include <QtNetwork/QHostAddress> #include <QtNetwork/QHostInfo>
class HifiSockAddr { class HifiSockAddr : public QObject {
Q_OBJECT
public: public:
HifiSockAddr(); HifiSockAddr();
HifiSockAddr(const QHostAddress& address, quint16 port); HifiSockAddr(const QHostAddress& address, quint16 port);
@ -51,6 +52,8 @@ public:
friend QDebug operator<<(QDebug debug, const HifiSockAddr& sockAddr); friend QDebug operator<<(QDebug debug, const HifiSockAddr& sockAddr);
friend QDataStream& operator<<(QDataStream& dataStream, const HifiSockAddr& sockAddr); friend QDataStream& operator<<(QDataStream& dataStream, const HifiSockAddr& sockAddr);
friend QDataStream& operator>>(QDataStream& dataStream, HifiSockAddr& sockAddr); friend QDataStream& operator>>(QDataStream& dataStream, HifiSockAddr& sockAddr);
private slots:
void handleLookupResult(const QHostInfo& hostInfo);
private: private:
QHostAddress _address; QHostAddress _address;
quint16 _port; quint16 _port;

View file

@ -73,6 +73,7 @@ LimitedNodeList::LimitedNodeList(unsigned short socketListenPort, unsigned short
_dtlsSocket(NULL), _dtlsSocket(NULL),
_localSockAddr(), _localSockAddr(),
_publicSockAddr(), _publicSockAddr(),
_stunSockAddr(STUN_SERVER_HOSTNAME, STUN_SERVER_PORT),
_numCollectedPackets(0), _numCollectedPackets(0),
_numCollectedBytes(0), _numCollectedBytes(0),
_packetStatTimer() _packetStatTimer()
@ -583,11 +584,8 @@ void LimitedNodeList::sendSTUNRequest() {
QUuid randomUUID = QUuid::createUuid(); QUuid randomUUID = QUuid::createUuid();
memcpy(stunRequestPacket + packetIndex, randomUUID.toRfc4122().data(), NUM_TRANSACTION_ID_BYTES); memcpy(stunRequestPacket + packetIndex, randomUUID.toRfc4122().data(), NUM_TRANSACTION_ID_BYTES);
// lookup the IP for the STUN server
HifiSockAddr stunSockAddr(STUN_SERVER_HOSTNAME, STUN_SERVER_PORT);
_nodeSocket.writeDatagram((char*) stunRequestPacket, sizeof(stunRequestPacket), _nodeSocket.writeDatagram((char*) stunRequestPacket, sizeof(stunRequestPacket),
stunSockAddr.getAddress(), stunSockAddr.getPort()); _stunSockAddr.getAddress(), _stunSockAddr.getPort());
} }
void LimitedNodeList::rebindNodeSocket() { void LimitedNodeList::rebindNodeSocket() {

View file

@ -163,6 +163,7 @@ protected:
QUdpSocket* _dtlsSocket; QUdpSocket* _dtlsSocket;
HifiSockAddr _localSockAddr; HifiSockAddr _localSockAddr;
HifiSockAddr _publicSockAddr; HifiSockAddr _publicSockAddr;
HifiSockAddr _stunSockAddr;
int _numCollectedPackets; int _numCollectedPackets;
int _numCollectedBytes; int _numCollectedBytes;
QElapsedTimer _packetStatTimer; QElapsedTimer _packetStatTimer;