Prefer non-link-local interface addresses; don't change port on initial address change from TCP probe

This commit is contained in:
Simon Walton 2019-08-22 17:34:41 -07:00
parent 33070b4579
commit 6a9e299981
3 changed files with 23 additions and 11 deletions

View file

@ -1225,8 +1225,8 @@ void LimitedNodeList::connectedForLocalSocketTest() {
auto localHostAddress = localIPTestSocket->localAddress();
if (localHostAddress.protocol() == QAbstractSocket::IPv4Protocol) {
_hasTCPCheckedLocalSocket = true;
setLocalSocket(HifiSockAddr { localHostAddress, _nodeSocket.localPort() });
_hasTCPCheckedLocalSocket = true;
}
localIPTestSocket->deleteLater();
@ -1244,7 +1244,7 @@ void LimitedNodeList::errorTestingLocalSocket() {
setLocalSocket(HifiSockAddr { getGuessedLocalAddress(), _nodeSocket.localPort() });
}
localIPTestSocket->deleteLater();;
localIPTestSocket->deleteLater();
}
}

View file

@ -140,7 +140,7 @@ void NetworkPeer::activatePublicSocket() {
void NetworkPeer::activateSymmetricSocket() {
if (_activeSocket != &_symmetricSocket) {
qCDebug(networking) << "Activating symmetric socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid);
qCDebug(networking) << "Activating symmetric socket (" << _symmetricSocket << ") for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid);
setActiveSocket(&_symmetricSocket);
}
}

View file

@ -9,9 +9,19 @@
#include "NetworkUtils.h"
#include <QtNetwork/QNetworkInterface>
namespace {
const QString LINK_LOCAL_SUBNET {"169.154."};
// Is address local-subnet valid only (rfc 3927):
bool isLinkLocalAddress(const QHostAddress& ip4Addr) {
return ip4Addr.toString().startsWith(LINK_LOCAL_SUBNET);
}
}
QHostAddress getGuessedLocalAddress() {
QHostAddress localAddress;
QHostAddress linkLocalAddress;
foreach(const QNetworkInterface &networkInterface, QNetworkInterface::allInterfaces()) {
if (networkInterface.flags() & QNetworkInterface::IsUp
@ -20,12 +30,16 @@ QHostAddress getGuessedLocalAddress() {
// we've decided that this is the active NIC
// enumerate it's addresses to grab the IPv4 address
foreach(const QNetworkAddressEntry &entry, networkInterface.addressEntries()) {
const auto& addressCandidate = entry.ip();
// make sure it's an IPv4 address that isn't the loopback
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol && !entry.ip().isLoopback()) {
// set our localAddress and break out
localAddress = entry.ip();
break;
if (addressCandidate.protocol() == QAbstractSocket::IPv4Protocol && !addressCandidate.isLoopback()) {
if (isLinkLocalAddress(addressCandidate)) {
linkLocalAddress = addressCandidate; // Last resort
} else {
// set our localAddress and break out
localAddress = addressCandidate;
break;
}
}
}
}
@ -36,7 +50,5 @@ QHostAddress getGuessedLocalAddress() {
}
// return the looked up local address
return localAddress;
return localAddress.isNull() ? linkLocalAddress : localAddress;
}