Merge pull request #16095 from SimonWalton-HiFi/change-of-local-interface

BUGZ-1317: Improved handling of local-interface addresses
This commit is contained in:
Shannon Romano 2019-08-26 11:57:57 -07:00 committed by GitHub
commit 7466c3f2ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 14 deletions

View file

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

View file

@ -140,7 +140,7 @@ void NetworkPeer::activatePublicSocket() {
void NetworkPeer::activateSymmetricSocket() { void NetworkPeer::activateSymmetricSocket() {
if (_activeSocket != &_symmetricSocket) { 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); setActiveSocket(&_symmetricSocket);
} }
} }

View file

@ -263,7 +263,7 @@ Connection* Socket::findOrCreateConnection(const HifiSockAddr& sockAddr, bool fi
if (filterCreate && _connectionCreationFilterOperator && !_connectionCreationFilterOperator(sockAddr)) { if (filterCreate && _connectionCreationFilterOperator && !_connectionCreationFilterOperator(sockAddr)) {
// the connection creation filter did not allow us to create a new connection // the connection creation filter did not allow us to create a new connection
#ifdef UDT_CONNECTION_DEBUG #ifdef UDT_CONNECTION_DEBUG
qCDebug(networking) << "Socket::findOrCreateConnection refusing to create connection for" << sockAddr qCDebug(networking) << "Socket::findOrCreateConnection refusing to create Connection class for" << sockAddr
<< "due to connection creation filter"; << "due to connection creation filter";
#endif // UDT_CONNECTION_DEBUG #endif // UDT_CONNECTION_DEBUG
return nullptr; return nullptr;
@ -279,7 +279,7 @@ Connection* Socket::findOrCreateConnection(const HifiSockAddr& sockAddr, bool fi
QObject::connect(connection.get(), &Connection::receiverHandshakeRequestComplete, QObject::connect(connection.get(), &Connection::receiverHandshakeRequestComplete,
this, &Socket::clientHandshakeRequestComplete); this, &Socket::clientHandshakeRequestComplete);
qCDebug(networking) << "Creating new connection to" << sockAddr; qCDebug(networking) << "Creating new Connection class for" << sockAddr;
it = _connectionsHash.insert(it, std::make_pair(sockAddr, std::move(connection))); it = _connectionsHash.insert(it, std::make_pair(sockAddr, std::move(connection)));
} }

View file

@ -56,7 +56,6 @@ static FilePersistThread* _persistThreadInstance;
QString getLogRollerFilename() { QString getLogRollerFilename() {
QString result = FileUtils::standardPath(LOGS_DIRECTORY); QString result = FileUtils::standardPath(LOGS_DIRECTORY);
QHostAddress clientAddress = getGuessedLocalAddress();
QDateTime now = QDateTime::currentDateTime(); QDateTime now = QDateTime::currentDateTime();
QString fileSessionID; QString fileSessionID;

View file

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