Skip to content

Commit

Permalink
Initial attempt for sockets listening on both IPv4 and IPv6. Not work…
Browse files Browse the repository at this point in the history
…ing yet.
  • Loading branch information
ksuprynowicz committed Nov 10, 2023
1 parent 95b6e03 commit def34c6
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion assignment-client/src/octree/OctreeServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void OctreeServer::initHTTPManager(int port) {
QString documentRoot = QString("%1/web").arg(PathUtils::getAppDataPath());

// setup an httpManager with us as the request handler and the parent
_httpManager.reset(new HTTPManager(QHostAddress::AnyIPv4, port, documentRoot, this));
_httpManager.reset(new HTTPManager(QHostAddress::Any, port, documentRoot, this));
}

bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url, bool skipSubHandler) {
Expand Down
10 changes: 6 additions & 4 deletions domain-server/src/DomainServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ bool DomainServer::forwardMetaverseAPIRequest(HTTPConnection* connection,
DomainServer::DomainServer(int argc, char* argv[]) :
QCoreApplication(argc, argv),
_gatekeeper(this),
_httpManager(QHostAddress::AnyIPv4, DOMAIN_SERVER_HTTP_PORT,
_httpManager(QHostAddress::Any, DOMAIN_SERVER_HTTP_PORT,
QString("%1/resources/web/").arg(QCoreApplication::applicationDirPath()), this)
{
static const QString CRASH_REPORTER = "crash_reporting.enable_crash_reporter";
Expand Down Expand Up @@ -594,7 +594,7 @@ bool DomainServer::optionallyReadX509KeyAndCertificate() {
qCritical() << "SSL Private Key Not Loading. Bad password or key format?";
}

_httpsManager.reset(new HTTPSManager(QHostAddress::AnyIPv4, DOMAIN_SERVER_HTTPS_PORT, sslCertificate, privateKey, QString(), this));
_httpsManager.reset(new HTTPSManager(QHostAddress::Any, DOMAIN_SERVER_HTTPS_PORT, sslCertificate, privateKey, QString(), this));

qDebug() << "TCP server listening for HTTPS connections on" << DOMAIN_SERVER_HTTPS_PORT;

Expand Down Expand Up @@ -930,7 +930,7 @@ void DomainServer::setupNodeListAndAssignments() {
// Sets up the WebRTC signaling server that's hosted by the domain server.
void DomainServer::setUpWebRTCSignalingServer() {
// Bind the WebRTC signaling server's WebSocket to its port.
bool isBound = _webrtcSignalingServer->bind(QHostAddress::AnyIPv4, DEFAULT_DOMAIN_SERVER_WS_PORT);
bool isBound = _webrtcSignalingServer->bind(QHostAddress::Any, DEFAULT_DOMAIN_SERVER_WS_PORT);
if (!isBound) {
qWarning() << "WebRTC signaling server not bound to port. WebRTC connections are not supported.";
return;
Expand Down Expand Up @@ -3564,10 +3564,12 @@ void DomainServer::handleICEHostInfo(const QHostInfo& hostInfo) {
_iceAddressLookupID = INVALID_ICE_LOOKUP_ID;

// enumerate the returned addresses and collect only valid IPv4 addresses
// TODO(IPv6): Does it need to be IPv4 and if so why?
QList<QHostAddress> sanitizedAddresses = hostInfo.addresses();
auto it = sanitizedAddresses.begin();
while (it != sanitizedAddresses.end()) {
if (!it->isNull() && it->protocol() == QAbstractSocket::IPv4Protocol) {
//if (!it->isNull() && it->protocol() == QAbstractSocket::IPv4Protocol) {
if (!it->isNull() && it->protocol() == QAbstractSocket::AnyIPProtocol) {
++it;
} else {
it = sanitizedAddresses.erase(it);
Expand Down
2 changes: 1 addition & 1 deletion ice-server/src/IceServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ IceServer::IceServer(int argc, char* argv[]) :
{
// start the ice-server socket
qDebug() << "ice-server socket is listening on" << ICE_SERVER_DEFAULT_PORT;
_serverSocket.bind(SocketType::UDP, QHostAddress::AnyIPv4, ICE_SERVER_DEFAULT_PORT);
_serverSocket.bind(SocketType::UDP, QHostAddress::Any, ICE_SERVER_DEFAULT_PORT);

// set processPacket as the verified packet callback for the udt::Socket
_serverSocket.setPacketHandler([this](std::unique_ptr<udt::Packet> packet) { processPacket(std::move(packet)); });
Expand Down
3 changes: 2 additions & 1 deletion libraries/networking/src/DomainHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ QString DomainHandler::getViewPointFromNamedPath(QString namedPath) {

void DomainHandler::completedHostnameLookup(const QHostInfo& hostInfo) {
for (int i = 0; i < hostInfo.addresses().size(); i++) {
if (hostInfo.addresses()[i].protocol() == QAbstractSocket::IPv4Protocol) {
// TODO(IPv6): do IPv4 and v6 need to be treated separately?
if (hostInfo.addresses()[i].protocol() == QAbstractSocket::AnyIPProtocol) {
_sockAddr.setAddress(hostInfo.addresses()[i]);

DependencyManager::get<NodeList>()->flagTimeForConnectionStep(LimitedNodeList::ConnectionStep::SetDomainSocket);
Expand Down
14 changes: 9 additions & 5 deletions libraries/networking/src/LimitedNodeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ LimitedNodeList::LimitedNodeList(int socketListenPort, int dtlsListenPort) :
{
qRegisterMetaType<ConnectionStep>("ConnectionStep");
auto port = (socketListenPort != INVALID_PORT) ? socketListenPort : LIMITED_NODELIST_LOCAL_PORT.get();
_nodeSocket.bind(SocketType::UDP, QHostAddress::AnyIPv4, port);
_nodeSocket.bind(SocketType::UDP, QHostAddress::Any, port);
quint16 assignedPort = _nodeSocket.localPort(SocketType::UDP);
if (socketListenPort != INVALID_PORT && socketListenPort != 0 && socketListenPort != assignedPort) {
qCCritical(networking) << "PAGE: NodeList is unable to assign requested UDP port of" << socketListenPort;
Expand All @@ -67,14 +67,14 @@ LimitedNodeList::LimitedNodeList(int socketListenPort, int dtlsListenPort) :
// only create the DTLS socket during constructor if a custom port is passed
_dtlsSocket = new QUdpSocket(this);

_dtlsSocket->bind(QHostAddress::AnyIPv4, dtlsListenPort);
_dtlsSocket->bind(QHostAddress::Any, dtlsListenPort);
if (dtlsListenPort != 0 && _dtlsSocket->localPort() != dtlsListenPort) {
qCDebug(networking) << "NodeList is unable to assign requested DTLS port of" << dtlsListenPort;
}
qCDebug(networking) << "NodeList DTLS socket is listening on" << _dtlsSocket->localPort();
}

_nodeSocket.bind(SocketType::WebRTC, QHostAddress::AnyIPv4);
_nodeSocket.bind(SocketType::WebRTC, QHostAddress::Any);

// check for local socket updates every so often
const int LOCAL_SOCKET_UPDATE_INTERVAL_MSECS = 5 * 1000;
Expand Down Expand Up @@ -221,7 +221,7 @@ QUdpSocket& LimitedNodeList::getDTLSSocket() {
// DTLS socket getter called but no DTLS socket exists, create it now
_dtlsSocket = new QUdpSocket(this);

_dtlsSocket->bind(QHostAddress::AnyIPv4, 0, QAbstractSocket::DontShareAddress);
_dtlsSocket->bind(QHostAddress::Any, 0, QAbstractSocket::DontShareAddress);

// we're using DTLS and our socket is good to go, so make the required DTLS changes
// DTLS requires that IP_DONTFRAG be set
Expand Down Expand Up @@ -1055,6 +1055,7 @@ bool LimitedNodeList::parseSTUNResponse(udt::BasePacket* packet,
if (memcmp(packet->getData() + attributeStartIndex, &XOR_MAPPED_ADDRESS_TYPE, sizeof(XOR_MAPPED_ADDRESS_TYPE)) == 0) {
const int NUM_BYTES_STUN_ATTR_TYPE_AND_LENGTH = 4;
const int NUM_BYTES_FAMILY_ALIGN = 1;
// TODO(IPv6): this probably needs work
const uint8_t IPV4_FAMILY_NETWORK_ORDER = htons(0x01) >> 8;

int byteIndex = attributeStartIndex + NUM_BYTES_STUN_ATTR_TYPE_AND_LENGTH + NUM_BYTES_FAMILY_ALIGN;
Expand All @@ -1064,6 +1065,7 @@ bool LimitedNodeList::parseSTUNResponse(udt::BasePacket* packet,

byteIndex += sizeof(addressFamily);

// TODO(IPv6): this probably needs work
if (addressFamily == IPV4_FAMILY_NETWORK_ORDER) {
// grab the X-Port
uint16_t xorMappedPort = 0;
Expand Down Expand Up @@ -1241,7 +1243,9 @@ void LimitedNodeList::connectedForLocalSocketTest() {
if (localIPTestSocket) {
auto localHostAddress = localIPTestSocket->localAddress();

if (localHostAddress.protocol() == QAbstractSocket::IPv4Protocol) {
// TODO(IPv6):
//if (localHostAddress.protocol() == QAbstractSocket::IPv4Protocol) {
if (localHostAddress.protocol() == QAbstractSocket::AnyIPProtocol) {
setLocalSocket(SockAddr { SocketType::UDP, localHostAddress, _nodeSocket.localPort(SocketType::UDP) });
_hasTCPCheckedLocalSocket = true;
}
Expand Down
7 changes: 5 additions & 2 deletions libraries/networking/src/SockAddr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ SockAddr::SockAddr(SocketType socketType, const QString& hostname, quint16 hostO
_port(hostOrderPort)
{
// if we parsed an IPv4 address out of the hostname, don't look it up
if (_address.protocol() != QAbstractSocket::IPv4Protocol) {
// TODO(IPv6): is IPv4 needed here?
//if (_address.protocol() != QAbstractSocket::IPv4Protocol) {
if (_address.protocol() != QAbstractSocket::AnyIPProtocol) {
// lookup the IP by the hostname
if (shouldBlockForLookup) {
qCDebug(networking) << "Synchronously looking up IP address for hostname" << hostname;
Expand Down Expand Up @@ -101,7 +103,8 @@ void SockAddr::handleLookupResult(const QHostInfo& hostInfo) {
} else {
foreach(const QHostAddress& address, hostInfo.addresses()) {
// just take the first IPv4 address
if (address.protocol() == QAbstractSocket::IPv4Protocol) {
// TODO(IPv6): what to do about IPv4 and IPv6?
if (address.protocol() == QAbstractSocket::AnyIPProtocol) {
_address = address;
qCDebug(networking) << "QHostInfo lookup result for"
<< hostInfo.hostName() << "with lookup ID" << hostInfo.lookupId() << "is" << address.toString();
Expand Down
3 changes: 2 additions & 1 deletion libraries/networking/src/SockAddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ namespace std {
size_t operator()(const SockAddr& sockAddr) const {
// use XOR of implemented std::hash templates for new hash
// depending on the type of address we're looking at


// TODO(IPv6): does this need to be modified or is it fine?
if (sockAddr.getAddress().protocol() == QAbstractSocket::IPv4Protocol) {
return hash<uint32_t>()((uint32_t) sockAddr.getAddress().toIPv4Address())
^ hash<uint16_t>()((uint16_t) sockAddr.getPort());
Expand Down
1 change: 1 addition & 0 deletions libraries/networking/src/udt/BasePacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class BasePacket : public ExtendedIODevice {
qint64 getDataSize() const { return (_payloadStart - _packet.get()) + _payloadSize; }

// Returns the size of the packet, including the header AND the UDP/IP header
// TODO(IPv6): Is UDP header size different for IPv6?
qint64 getWireSize() const { return getDataSize() + UDP_IPV4_HEADER_SIZE; }

// Returns the size of the payload only
Expand Down
2 changes: 2 additions & 0 deletions libraries/networking/src/udt/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

namespace udt {

// TODO(IPv6): Is UDP header size different for IPv6?
static const int UDP_IPV4_HEADER_SIZE = 28;
static const int MAX_PACKET_SIZE_WITH_UDP_HEADER = 1492;
// TODO(IPv6): Is UDP header size different for IPv6?
static const int MAX_PACKET_SIZE = MAX_PACKET_SIZE_WITH_UDP_HEADER - UDP_IPV4_HEADER_SIZE;
static const int MAX_PACKETS_IN_FLIGHT = 25600;
static const int CONNECTION_RECEIVE_BUFFER_SIZE_PACKETS = 8192;
Expand Down
2 changes: 1 addition & 1 deletion libraries/networking/src/udt/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void Socket::rebind(SocketType socketType) {

void Socket::rebind(SocketType socketType, quint16 localPort) {
_networkSocket.abort(socketType);
bind(socketType, QHostAddress::AnyIPv4, localPort);
bind(socketType, QHostAddress::Any, localPort);
}

#if defined(WEBRTC_DATA_CHANNELS)
Expand Down
4 changes: 3 additions & 1 deletion libraries/shared/src/shared/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ QHostAddress getGuessedLocalAddress() {
foreach(const QNetworkAddressEntry &entry, networkInterface.addressEntries()) {
const auto& addressCandidate = entry.ip();
// make sure it's an IPv4 address that isn't the loopback
if (addressCandidate.protocol() == QAbstractSocket::IPv4Protocol && !addressCandidate.isLoopback()) {
// TODO(IPv6): does it matter if it's IPv4?
//if (addressCandidate.protocol() == QAbstractSocket::IPv4Protocol && !addressCandidate.isLoopback()) {
if (addressCandidate.protocol() == QAbstractSocket::AnyIPProtocol && !addressCandidate.isLoopback()) {
if (isLinkLocalAddress(addressCandidate)) {
linkLocalAddress = addressCandidate; // Last resort
} else {
Expand Down
2 changes: 1 addition & 1 deletion tools/ice-client/src/ICEClientApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void ICEClientApp::openSocket() {

_socket = new udt::Socket();
unsigned int localPort = 0;
_socket->bind(SocketType::UDP, QHostAddress::AnyIPv4, localPort);
_socket->bind(SocketType::UDP, QHostAddress::Any, localPort);
_socket->setPacketHandler([this](std::unique_ptr<udt::Packet> packet) { processPacket(std::move(packet)); });
_socket->addUnfilteredHandler(_stunSockAddr,
[this](std::unique_ptr<udt::BasePacket> packet) {
Expand Down
4 changes: 3 additions & 1 deletion tools/nitpick/src/TestRunnerMobile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ QString TestRunnerMobile::getServerIP() {
for (int i = 0; i < interfaces.count(); i++) {
QList<QNetworkAddressEntry> entries = interfaces.at(i).addressEntries();
for (int j = 0; j < entries.count(); j++) {
if (entries.at(j).ip().protocol() == QAbstractSocket::IPv4Protocol) {
// TODO(IPv6): does IPv6 need to be handled differently
//if (entries.at(j).ip().protocol() == QAbstractSocket::IPv4Protocol) {
if (entries.at(j).ip().protocol() == QAbstractSocket::AnyIPProtocol) {
qint64 hostIP = convertToBinary(entries.at(j).ip().toString());
qint64 hostMask = convertToBinary(entries.at(j).netmask().toString());
qint64 hostSubnet = hostMask & hostIP;
Expand Down
2 changes: 1 addition & 1 deletion tools/udt-test/src/UDTTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ UDTTest::UDTTest(int& argc, char** argv) :
// randomize the seed for packet size randomization
srand(time(NULL));

_socket.bind(QHostAddress::AnyIPv4, _argumentParser.value(PORT_OPTION).toUInt());
_socket.bind(QHostAddress::Any, _argumentParser.value(PORT_OPTION).toUInt());
qDebug() << "Test socket is listening on" << _socket.localPort();

if (_argumentParser.isSet(TARGET_OPTION)) {
Expand Down

0 comments on commit def34c6

Please sign in to comment.