Revert "don't allow a HifiSockAddr lookup to block application"

This commit is contained in:
Stephen Birarda 2014-11-03 12:54:28 -08:00
parent 09f20b00df
commit fabc3aea57
4 changed files with 17 additions and 36 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 <qdatastream.h> #include <QtCore/QDataStream>
#include <qhostinfo.h> #include <QtNetwork/QHostInfo>
#include <qnetworkinterface.h> #include <QtNetwork/QNetworkInterface>
#include "HifiSockAddr.h" #include "HifiSockAddr.h"
@ -36,15 +36,14 @@ 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) {
_address(hostname), // lookup the IP by the hostname
_port(hostOrderPort) QHostInfo hostInfo = QHostInfo::fromName(hostname);
{ foreach(const QHostAddress& address, hostInfo.addresses()) {
// if we parsed an IPv4 address out of the hostname, don't look it up if (address.protocol() == QAbstractSocket::IPv4Protocol) {
if (_address.protocol() != QAbstractSocket::IPv4Protocol) { _address = address;
// sync lookup the IP by the hostname _port = hostOrderPort;
int lookupID = QHostInfo::lookupHost(hostname, this, SLOT(handleLookupResult(QHostInfo))); }
qDebug() << "Looking up IP address for hostname" << hostname << "- lookup ID is" << lookupID;
} }
} }
@ -76,22 +75,6 @@ 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,10 +19,9 @@
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
#include <QtNetwork/QHostInfo> #include <QtNetwork/QHostAddress>
class HifiSockAddr : public QObject { class HifiSockAddr {
Q_OBJECT
public: public:
HifiSockAddr(); HifiSockAddr();
HifiSockAddr(const QHostAddress& address, quint16 port); HifiSockAddr(const QHostAddress& address, quint16 port);
@ -52,8 +51,6 @@ 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,7 +73,6 @@ 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()
@ -584,8 +583,11 @@ 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,7 +163,6 @@ 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;