From d85fbbfb745d790f74de50a013b418b6b4fc453c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 10 Jul 2015 17:45:23 -0700 Subject: [PATCH] update NodeList for new packet receive code --- domain-server/src/DomainServer.cpp | 10 +- libraries/networking/src/DomainHandler.cpp | 18 +- libraries/networking/src/DomainHandler.h | 5 +- libraries/networking/src/LimitedNodeList.cpp | 25 +- libraries/networking/src/LimitedNodeList.h | 6 +- libraries/networking/src/NodeList.cpp | 249 +++++++------------ libraries/networking/src/NodeList.h | 20 +- libraries/networking/src/PacketHeaders.cpp | 12 +- 8 files changed, 143 insertions(+), 202 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index a77156a8c9..00efd818a5 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -933,15 +933,17 @@ void DomainServer::sendDomainListToNode(const SharedNodePointer& node, const Hif // always send the node their own UUID back QDataStream domainListStream(&domainListPackets); - const int NUM_DOMAIN_LIST_EXTENDED_HEADER_BYTES = NUM_BYTES_RFC4122_UUID + 2; + const int NUM_DOMAIN_LIST_EXTENDED_HEADER_BYTES = NUM_BYTES_RFC4122_UUID + NUM_BYTES_RFC4122_UUID + 2; // setup the extended header for the domain list packets // this data is at the beginning of each of the domain list packets QByteArray extendedHeader(NUM_DOMAIN_LIST_EXTENDED_HEADER_BYTES, 0); - extendedHeader.replace(0, NUM_BYTES_RFC4122_UUID, node->getUUID().toRfc4122()); + QDataStream extendedHeaderStream(&extendedHeader, &QIODevice::Append); - extendedHeader[NUM_BYTES_RFC4122_UUID] = (char) node->getCanAdjustLocks(); - extendedHeader[NUM_BYTES_RFC4122_UUID + 1] = (char) node->getCanRez(); + extendedHeaderStream << limitedNodeList->getSessionUUID().toRfc4122(); + extendedHeaderStream << node->getUUID().toRfc4122(); + extendedHeaderStream << (quint8) node->getCanAdjustLocks(); + extendedHeaderStream << (quint8) node->getCanRez(); domainListPackets.setExtendedHeader(extendedHeader); diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index 38d1ade2ad..6d76e09028 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -281,12 +281,10 @@ void DomainHandler::settingsRequestFinished() { settingsReply->deleteLater(); } -void DomainHandler::parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket) { +void DomainHandler::processDTLSRequirementPacket(QSharedPointer dtlsRequirementPacket) { // figure out the port that the DS wants us to use for us to talk to them with DTLS - int numBytesPacketHeader = numBytesForPacketHeader(dtlsRequirementPacket); - - unsigned short dtlsPort = 0; - memcpy(&dtlsPort, dtlsRequirementPacket.data() + numBytesPacketHeader, sizeof(dtlsPort)); + unsigned short dtlsPort; + dtlsRequirementPacket->readPrimitive(&dtlsPort); qCDebug(networking) << "domain-server DTLS port changed to" << dtlsPort << "- Enabling DTLS."; @@ -295,9 +293,13 @@ void DomainHandler::parseDTLSRequirementPacket(const QByteArray& dtlsRequirement // initializeDTLSSession(); } -void DomainHandler::processICEResponsePacket(const QByteArray& icePacket) { - QDataStream iceResponseStream(icePacket); - iceResponseStream.skipRawData(numBytesForPacketHeader(icePacket)); +void DomainHandler::processICEResponsePacket(QSharedPointer icePacket) { + if (!_icePeer.hasSockets()) { + // bail on processing this packet if our ice peer doesn't have sockets + return; + } + + QDataStream iceResponseStream(icePacket.data()); iceResponseStream >> _icePeer; diff --git a/libraries/networking/src/DomainHandler.h b/libraries/networking/src/DomainHandler.h index 0c1698f5ec..716138a4f1 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -21,6 +21,7 @@ #include "HifiSockAddr.h" #include "NetworkPeer.h" +#include "NLPacket.h" const unsigned short DEFAULT_DOMAIN_SERVER_PORT = 40102; const unsigned short DEFAULT_DOMAIN_SERVER_DTLS_PORT = 40103; @@ -69,8 +70,8 @@ public: void requestDomainSettings(); const QJsonObject& getSettingsObject() const { return _settingsObject; } - void parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket); - void processICEResponsePacket(const QByteArray& icePacket); + void processDTLSRequirementPacket(QSharedPointer dtlsRequirementPacket); + void processICEResponsePacket(QSharedPointer icePacket); void setPendingPath(const QString& pendingPath) { _pendingPath = pendingPath; } const QString& getPendingPath() { return _pendingPath; } diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index 962f8dbc30..764a9eed7e 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -369,9 +369,8 @@ std::unique_ptr LimitedNodeList::constructPingPacket(PingType_t pingTy return pingPacket; } -std::unique_ptr LimitedNodeList::constructPingReplyPacket(const QByteArray& pingPacket) { - QDataStream pingPacketStream(pingPacket); - pingPacketStream.skipRawData(numBytesForPacketHeader(pingPacket)); +std::unique_ptr LimitedNodeList::constructPingReplyPacket(QSharedPointer pingPacket) { + QDataStream pingPacketStream(pingPacket.data()); PingType_t typeFromOriginalPing; pingPacketStream >> typeFromOriginalPing; @@ -400,11 +399,11 @@ std::unique_ptr LimitedNodeList::constructICEPingPacket(PingType_t pin return icePingPacket; } -std::unique_ptr LimitedNodeList::constructICEPingReplyPacket(const QByteArray& pingPacket, const QUuid& iceID) { +std::unique_ptr LimitedNodeList::constructICEPingReplyPacket(QSharedPointer pingPacket, const QUuid& iceID) { // pull out the ping type so we can reply back with that PingType_t pingType; - memcpy(&pingType, pingPacket.data() + NUM_BYTES_RFC4122_UUID, sizeof(PingType_t)); + memcpy(&pingType, pingPacket->getPayload() + NUM_BYTES_RFC4122_UUID, sizeof(PingType_t)); int packetSize = NUM_BYTES_RFC4122_UUID + sizeof(PingType_t); auto icePingReplyPacket = NLPacket::create(PacketType::ICEPingReply, packetSize); @@ -514,7 +513,7 @@ void LimitedNodeList::rebindNodeSocket() { _nodeSocket.bind(QHostAddress::AnyIPv4, oldPort); } -bool LimitedNodeList::processSTUNResponse(const QByteArray& packet) { +bool LimitedNodeList::processSTUNResponse(QSharedPointer packet) { // check the cookie to make sure this is actually a STUN response // and read the first attribute and make sure it is a XOR_MAPPED_ADDRESS const int NUM_BYTES_MESSAGE_TYPE_AND_LENGTH = 4; @@ -524,13 +523,13 @@ bool LimitedNodeList::processSTUNResponse(const QByteArray& packet) { int attributeStartIndex = NUM_BYTES_STUN_HEADER; - if (memcmp(packet.data() + NUM_BYTES_MESSAGE_TYPE_AND_LENGTH, + if (memcmp(packet->getData() + NUM_BYTES_MESSAGE_TYPE_AND_LENGTH, &RFC_5389_MAGIC_COOKIE_NETWORK_ORDER, sizeof(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER)) == 0) { // enumerate the attributes to find XOR_MAPPED_ADDRESS_TYPE - while (attributeStartIndex < packet.size()) { - if (memcmp(packet.data() + attributeStartIndex, &XOR_MAPPED_ADDRESS_TYPE, sizeof(XOR_MAPPED_ADDRESS_TYPE)) == 0) { + while (attributeStartIndex < packet->getSizeWithHeader()) { + 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; const uint8_t IPV4_FAMILY_NETWORK_ORDER = htons(0x01) >> 8; @@ -538,14 +537,14 @@ bool LimitedNodeList::processSTUNResponse(const QByteArray& packet) { int byteIndex = attributeStartIndex + NUM_BYTES_STUN_ATTR_TYPE_AND_LENGTH + NUM_BYTES_FAMILY_ALIGN; uint8_t addressFamily = 0; - memcpy(&addressFamily, packet.data() + byteIndex, sizeof(addressFamily)); + memcpy(&addressFamily, packet->getData() + byteIndex, sizeof(addressFamily)); byteIndex += sizeof(addressFamily); if (addressFamily == IPV4_FAMILY_NETWORK_ORDER) { // grab the X-Port uint16_t xorMappedPort = 0; - memcpy(&xorMappedPort, packet.data() + byteIndex, sizeof(xorMappedPort)); + memcpy(&xorMappedPort, packet->getData() + byteIndex, sizeof(xorMappedPort)); uint16_t newPublicPort = ntohs(xorMappedPort) ^ (ntohl(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER) >> 16); @@ -553,7 +552,7 @@ bool LimitedNodeList::processSTUNResponse(const QByteArray& packet) { // grab the X-Address uint32_t xorMappedAddress = 0; - memcpy(&xorMappedAddress, packet.data() + byteIndex, sizeof(xorMappedAddress)); + memcpy(&xorMappedAddress, packet->getData() + byteIndex, sizeof(xorMappedAddress)); uint32_t stunAddress = ntohl(xorMappedAddress) ^ ntohl(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER); @@ -583,7 +582,7 @@ bool LimitedNodeList::processSTUNResponse(const QByteArray& packet) { const int NUM_BYTES_ATTRIBUTE_TYPE = 2; uint16_t attributeLength = 0; - memcpy(&attributeLength, packet.data() + attributeStartIndex + NUM_BYTES_ATTRIBUTE_TYPE, + memcpy(&attributeLength, packet->getData() + attributeStartIndex + NUM_BYTES_ATTRIBUTE_TYPE, sizeof(attributeLength)); attributeLength = ntohs(attributeLength); diff --git a/libraries/networking/src/LimitedNodeList.h b/libraries/networking/src/LimitedNodeList.h index b2dd95f71c..f48f2aaa31 100644 --- a/libraries/networking/src/LimitedNodeList.h +++ b/libraries/networking/src/LimitedNodeList.h @@ -169,12 +169,12 @@ public: void resetPacketStats(); std::unique_ptr constructPingPacket(PingType_t pingType = PingType::Agnostic); - std::unique_ptr constructPingReplyPacket(const QByteArray& pingPacket); + std::unique_ptr constructPingReplyPacket(QSharedPointer pingPacket); std::unique_ptr constructICEPingPacket(PingType_t pingType, const QUuid& iceID); - std::unique_ptr constructICEPingReplyPacket(const QByteArray& pingPacket, const QUuid& iceID); + std::unique_ptr constructICEPingReplyPacket(QSharedPointer pingPacket, const QUuid& iceID); - virtual bool processSTUNResponse(const QByteArray& packet); + virtual bool processSTUNResponse(QSharedPointer packet); void sendHeartbeatToIceServer(const HifiSockAddr& iceServerSockAddr); void sendPeerQueryToIceServer(const HifiSockAddr& iceServerSockAddr, const QUuid& clientID, const QUuid& peerID); diff --git a/libraries/networking/src/NodeList.cpp b/libraries/networking/src/NodeList.cpp index 61c8e90b0d..8d897e82b5 100644 --- a/libraries/networking/src/NodeList.cpp +++ b/libraries/networking/src/NodeList.cpp @@ -92,16 +92,16 @@ NodeList::NodeList(char newOwnerType, unsigned short socketListenPort, unsigned startSTUNPublicSocketUpdate(); auto& packetReceiver = getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::DomainList, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::DomainServerAddedNode, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::DomainServerRequireDTLS, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::ICEServerPeerInformation, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::Ping, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::PingReply, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::ICEPing, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::ICEPingReply, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::StunResponse, this, "processReceivedPacket"); - packetReceiver.registerPacketListener(PacketType::DomainServerPathResponse, this, "processReceivedPacket"); + packetReceiver.registerPacketListener(PacketType::DomainList, this, "processDomainServerList"); + packetReceiver.registerPacketListener(PacketType::DomainServerAddedNode, this, "processDomainServerAddedNode"); + packetReceiver.registerPacketListener(PacketType::DomainServerRequireDTLS, &_domainHandler, "processDTLSRequirementPacket"); + packetReceiver.registerPacketListener(PacketType::DomainServerPathResponse, this, "processDomainServerPathQueryResponse"); + packetReceiver.registerPacketListener(PacketType::ICEServerPeerInformation, &_domainHandler, "processICEResponsePacket"); + packetReceiver.registerPacketListener(PacketType::Ping, this, "processPingPacket"); + packetReceiver.registerPacketListener(PacketType::PingReply, this, "processPingReplyPacket"); + packetReceiver.registerPacketListener(PacketType::ICEPing, this, "processICEPingPacket"); + packetReceiver.registerPacketListener(PacketType::ICEPingReply, this, "processICEPingReplyPacket"); + packetReceiver.registerPacketListener(PacketType::StunResponse, this, "processSTUNResponse"); } qint64 NodeList::sendStats(const QJsonObject& statsObject, const HifiSockAddr& destination) { @@ -128,9 +128,8 @@ qint64 NodeList::sendStatsToDomainServer(const QJsonObject& statsObject) { return sendStats(statsObject, _domainHandler.getSockAddr()); } -void NodeList::timePingReply(const QByteArray& packet, const SharedNodePointer& sendingNode) { - QDataStream packetStream(packet); - packetStream.skipRawData(numBytesForPacketHeader(packet)); +void NodeList::timePingReply(QSharedPointer packet, const SharedNodePointer& sendingNode) { + QDataStream packetStream(packet.data()); quint8 pingType; quint64 ourOriginalTime, othersReplyTime; @@ -164,108 +163,52 @@ void NodeList::timePingReply(const QByteArray& packet, const SharedNodePointer& } } -// TODO: Break this out into individual packet types -void NodeList::processReceivedPacket(std::unique_ptr, HifiSockAddr senderSockAddr) { - qDebug() << "Got packet!"; +void NodeList::processPingPacket(QSharedPointer packet, SharedNodePointer sendingNode) { + // send back a reply + auto replyPacket = constructPingReplyPacket(packet); + const HifiSockAddr& senderSockAddr = packet->getSenderSockAddr(); + sendPacket(std::move(replyPacket), sendingNode, senderSockAddr); + + // If we don't have a symmetric socket for this node and this socket doesn't match + // what we have for public and local then set it as the symmetric. + // This allows a server on a reachable port to communicate with nodes on symmetric NATs + if (sendingNode->getSymmetricSocket().isNull()) { + if (senderSockAddr != sendingNode->getLocalSocket() && senderSockAddr != sendingNode->getPublicSocket()) { + sendingNode->setSymmetricSocket(senderSockAddr); + } + } } -void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteArray& packet) { - PacketType::Value packetType = packetTypeForPacket(packet); - switch (packetType) { - case PacketType::DomainList: - case PacketType::DomainServerAddedNode: { - if (!_domainHandler.getSockAddr().isNull()) { - // only process a packet from domain-server if we're talking to a domain - // TODO: how do we make sure this is actually the domain we want the list from (DTLS probably) - if (packetType == PacketType::DomainList) { - processDomainServerList(packet); - } else if (packetType == PacketType::DomainServerAddedNode) { - processDomainServerAddedNode(packet); - } - } - break; - } - case PacketType::DomainServerRequireDTLS: { - _domainHandler.parseDTLSRequirementPacket(packet); - break; - } - case PacketType::ICEServerPeerInformation: { - if (!_domainHandler.getICEPeer().hasSockets()) { - _domainHandler.processICEResponsePacket(packet); - } - break; - } - case PacketType::Ping: { - // send back a reply - SharedNodePointer matchingNode = sendingNodeForPacket(packet); - if (matchingNode) { - matchingNode->setLastHeardMicrostamp(usecTimestampNow()); - auto replyPacket = constructPingReplyPacket(packet); - sendPacket(std::move(replyPacket), matchingNode, senderSockAddr); +void NodeList::processPingReplyPacket(QSharedPointer packet, SharedNodePointer sendingNode) { + // activate the appropriate socket for this node, if not yet updated + activateSocketFromNodeCommunication(packet, sendingNode); - // If we don't have a symmetric socket for this node and this socket doesn't match - // what we have for public and local then set it as the symmetric. - // This allows a server on a reachable port to communicate with nodes on symmetric NATs - if (matchingNode->getSymmetricSocket().isNull()) { - if (senderSockAddr != matchingNode->getLocalSocket() && senderSockAddr != matchingNode->getPublicSocket()) { - matchingNode->setSymmetricSocket(senderSockAddr); - } - } - } + // set the ping time for this node for stat collection + timePingReply(packet, sendingNode); +} - break; +void NodeList::processICEPingPacket(QSharedPointer packet) { + // send back a reply + auto replyPacket = constructICEPingReplyPacket(packet, _domainHandler.getICEClientID()); + sendPacket(std::move(replyPacket), packet->getSenderSockAddr()); +} + +void NodeList::processICEPingReplyPacket(QSharedPointer packet) { + const HifiSockAddr& senderSockAddr = packet->getSenderSockAddr(); + qCDebug(networking) << "Received reply from domain-server on" << senderSockAddr; + + if (_domainHandler.getIP().isNull()) { + // for now we're unsafely assuming this came back from the domain + if (senderSockAddr == _domainHandler.getICEPeer().getLocalSocket()) { + qCDebug(networking) << "Connecting to domain using local socket"; + _domainHandler.activateICELocalSocket(); + } else if (senderSockAddr == _domainHandler.getICEPeer().getPublicSocket()) { + qCDebug(networking) << "Conecting to domain using public socket"; + _domainHandler.activateICEPublicSocket(); + } else { + qCDebug(networking) << "Reply does not match either local or public socket for domain. Will not connect."; } - case PacketType::PingReply: { - SharedNodePointer sendingNode = sendingNodeForPacket(packet); - if (sendingNode) { - sendingNode->setLastHeardMicrostamp(usecTimestampNow()); - - // activate the appropriate socket for this node, if not yet updated - activateSocketFromNodeCommunication(packet, sendingNode); - - // set the ping time for this node for stat collection - timePingReply(packet, sendingNode); - } - - break; - } - case PacketType::ICEPing: { - // send back a reply - auto replyPacket = constructICEPingReplyPacket(packet, _domainHandler.getICEClientID()); - sendPacket(std::move(replyPacket), senderSockAddr); - break; - } - case PacketType::ICEPingReply: { - qCDebug(networking) << "Received reply from domain-server on" << senderSockAddr; - - if (_domainHandler.getIP().isNull()) { - // for now we're unsafely assuming this came back from the domain - if (senderSockAddr == _domainHandler.getICEPeer().getLocalSocket()) { - qCDebug(networking) << "Connecting to domain using local socket"; - _domainHandler.activateICELocalSocket(); - } else if (senderSockAddr == _domainHandler.getICEPeer().getPublicSocket()) { - qCDebug(networking) << "Conecting to domain using public socket"; - _domainHandler.activateICEPublicSocket(); - } else { - qCDebug(networking) << "Reply does not match either local or public socket for domain. Will not connect."; - } - - } - } - case PacketType::StunResponse: { - // a STUN packet begins with 00, we've checked the second zero with packetVersionMatch - // pass it along so it can be processed into our public address and port - processSTUNResponse(packet); - break; - } - case PacketType::DomainServerPathResponse: { - handleDSPathQueryResponse(packet); - break; - } - default: - LimitedNodeList::processNodeData(senderSockAddr, packet); - break; } } @@ -436,43 +379,30 @@ void NodeList::sendDSPathQuery(const QString& newPath) { } } -void NodeList::handleDSPathQueryResponse(const QByteArray& packet) { +void NodeList::processDomainServerPathQueryResponse(QSharedPointer packet) { // This is a response to a path query we theoretically made. // In the future we may want to check that this was actually from our DS and for a query we actually made. - int numHeaderBytes = numBytesForPacketHeaderGivenPacketType(PacketType::DomainServerPathResponse); - const char* startPosition = packet.data() + numHeaderBytes; - const char* currentPosition = startPosition; - // figure out how many bytes the path query is qint16 numPathBytes; - memcpy(&numPathBytes, currentPosition, sizeof(numPathBytes)); - currentPosition += sizeof(numPathBytes); + packet->readPrimitive(&numPathBytes); - // make sure it is safe to pull the path - if (numPathBytes <= packet.size() - numHeaderBytes - (currentPosition - startPosition)) { - // pull the path from the packet - QString pathQuery = QString::fromUtf8(currentPosition, numPathBytes); - currentPosition += numPathBytes; + // pull the path from the packet + QString pathQuery = QString::fromUtf8(packet->read(numPathBytes)); - // figure out how many bytes the viewpoint is - qint16 numViewpointBytes; - memcpy(&numViewpointBytes, currentPosition, sizeof(numViewpointBytes)); - currentPosition += sizeof(numViewpointBytes); + // figure out how many bytes the viewpoint is + qint16 numViewpointBytes; + packet->readPrimitive(&numViewpointBytes); - // make sure it is safe to pull the viewpoint - if (numViewpointBytes <= packet.size() - numHeaderBytes - (currentPosition - startPosition)) { - // pull the viewpoint from the packet - QString viewpoint = QString::fromUtf8(currentPosition, numViewpointBytes); + // pull the viewpoint from the packet + QString viewpoint = QString::fromUtf8(packet->read(numViewpointBytes)); - // Hand it off to the AddressManager so it can handle it as a relative viewpoint - if (DependencyManager::get()->goToViewpointForPath(viewpoint, pathQuery)) { - qCDebug(networking) << "Going to viewpoint" << viewpoint << "which was the lookup result for path" << pathQuery; - } else { - qCDebug(networking) << "Could not go to viewpoint" << viewpoint - << "which was the lookup result for path" << pathQuery; - } - } + // Hand it off to the AddressManager so it can handle it as a relative viewpoint + if (DependencyManager::get()->goToViewpointForPath(viewpoint, pathQuery)) { + qCDebug(networking) << "Going to viewpoint" << viewpoint << "which was the lookup result for path" << pathQuery; + } else { + qCDebug(networking) << "Could not go to viewpoint" << viewpoint + << "which was the lookup result for path" << pathQuery; } } @@ -527,49 +457,51 @@ void NodeList::pingPunchForDomainServer() { } } -int NodeList::processDomainServerList(const QByteArray& packet) { +void NodeList::processDomainServerList(QSharedPointer packet) { + if (_domainHandler.getSockAddr().isNull()) { + // refuse to process this packet if we aren't currently connected to the DS + return; + } + // this is a packet from the domain server, reset the count of un-replied check-ins _numNoReplyDomainCheckIns = 0; DependencyManager::get()->flagTimeForConnectionStep(LimitedNodeList::ConnectionStep::ReceiveDSList); + QDataStream packetStream(packet.data()); + + // grab the domain's ID from the beginning of the packet + QUuid domainUUID; + packetStream >> domainUUID; + // if this was the first domain-server list from this domain, we've now connected if (!_domainHandler.isConnected()) { - _domainHandler.setUUID(uuidFromPacketHeader(packet)); + _domainHandler.setUUID(domainUUID); _domainHandler.setIsConnected(true); } - int readNodes = 0; - - QDataStream packetStream(packet); - packetStream.skipRawData(numBytesForPacketHeader(packet)); - // pull our owner UUID from the packet, it's always the first thing QUuid newUUID; packetStream >> newUUID; setSessionUUID(newUUID); - // TODO: when fixing this read these are actually chars now, not bools - bool thisNodeCanAdjustLocks; + quint8 thisNodeCanAdjustLocks; packetStream >> thisNodeCanAdjustLocks; - setThisNodeCanAdjustLocks(thisNodeCanAdjustLocks); + setThisNodeCanAdjustLocks((bool) thisNodeCanAdjustLocks); - bool thisNodeCanRez; + quint8 thisNodeCanRez; packetStream >> thisNodeCanRez; - setThisNodeCanRez(thisNodeCanRez); + setThisNodeCanRez((bool) thisNodeCanRez); // pull each node in the packet - while (packetStream.device()->pos() < packet.size()) { + while (packetStream.device()->pos() < packet->getSizeUsed()) { parseNodeFromPacketStream(packetStream); } - - return readNodes; } -void NodeList::processDomainServerAddedNode(const QByteArray& packet) { - // setup a QDataStream, skip the header - QDataStream packetStream(packet); - packetStream.skipRawData(numBytesForPacketHeader(packet)); +void NodeList::processDomainServerAddedNode(QSharedPointer packet) { + // setup a QDataStream + QDataStream packetStream(packet.data()); // use our shared method to pull out the new node parseNodeFromPacketStream(packetStream); @@ -663,10 +595,9 @@ void NodeList::handleNodePingTimeout() { } } -void NodeList::activateSocketFromNodeCommunication(const QByteArray& packet, const SharedNodePointer& sendingNode) { +void NodeList::activateSocketFromNodeCommunication(QSharedPointer packet, const SharedNodePointer& sendingNode) { // deconstruct this ping packet to see if it is a public or local reply - QDataStream packetStream(packet); - packetStream.skipRawData(numBytesForPacketHeader(packet)); + QDataStream packetStream(packet.data()); quint8 pingType; packetStream >> pingType; diff --git a/libraries/networking/src/NodeList.h b/libraries/networking/src/NodeList.h index 6ac08fcbd9..4749f9c5ce 100644 --- a/libraries/networking/src/NodeList.h +++ b/libraries/networking/src/NodeList.h @@ -71,6 +71,16 @@ public slots: void reset(); void sendDomainServerCheckIn(); void handleDSPathQuery(const QString& newPath); + + void processDomainServerList(QSharedPointer packet); + void processDomainServerAddedNode(QSharedPointer packet); + void processDomainServerPathQueryResponse(QSharedPointer packet); + + void processPingPacket(QSharedPointer packet, SharedNodePointer sendingNode); + void processPingReplyPacket(QSharedPointer packet, SharedNodePointer sendingNode); + + void processICEPingPacket(QSharedPointer packet); + void processICEPingReplyPacket(QSharedPointer packet); signals: void limitOfSilentDomainCheckInsReached(); private slots: @@ -89,15 +99,11 @@ private: void processDomainServerAuthRequest(const QByteArray& packet); void requestAuthForDomainServer(); - void activateSocketFromNodeCommunication(const QByteArray& packet, const SharedNodePointer& sendingNode); - void timePingReply(const QByteArray& packet, const SharedNodePointer& sendingNode); - - void handleDSPathQueryResponse(const QByteArray& packet); + void activateSocketFromNodeCommunication(QSharedPointer packet, const SharedNodePointer& sendingNode); + void timePingReply(QSharedPointer packet, const SharedNodePointer& sendingNode); void sendDSPathQuery(const QString& newPath); - - int processDomainServerList(const QByteArray& packet); - void processDomainServerAddedNode(const QByteArray& packet); + void parseNodeFromPacketStream(QDataStream& packetStream); void pingPunchForInactiveNode(const SharedNodePointer& node); diff --git a/libraries/networking/src/PacketHeaders.cpp b/libraries/networking/src/PacketHeaders.cpp index 03a7260315..f6001711c0 100644 --- a/libraries/networking/src/PacketHeaders.cpp +++ b/libraries/networking/src/PacketHeaders.cpp @@ -18,21 +18,21 @@ using namespace PacketType; const QSet NON_VERIFIED_PACKETS = QSet() - << DomainServerRequireDTLS << DomainConnectRequest - << DomainList << DomainListRequest << DomainConnectionDenied << CreateAssignment << RequestAssignment << StunResponse << NodeJsonStats << EntityQuery << OctreeDataNack << EntityEditNack << Ping - << PingReply << StopNode - << DomainServerPathQuery << DomainServerPathResponse - << DomainServerAddedNode; + << PingReply << StopNode; const QSet SEQUENCE_NUMBERED_PACKETS = QSet() << AvatarData; const QSet NON_SOURCED_PACKETS = QSet() + << DomainServerRequireDTLS << DomainConnectRequest + << DomainList << DomainListRequest << DomainConnectionDenied + << DomainServerPathQuery << DomainServerPathResponse + << DomainServerAddedNode << ICEServerPeerInformation << ICEServerQuery << ICEServerHeartbeat - << ICEPing << ICEPingReply << DomainConnectRequest; + << ICEPing << ICEPingReply; int arithmeticCodingValueFromBuffer(const char* checkValue) { if (((uchar) *checkValue) < 255) {