From 0b32a5d93556564d9f0b23cbcd5d468f0b130b85 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 15 Jul 2015 11:42:35 -0700 Subject: [PATCH] Write session uuid and connection hash in packet header --- libraries/networking/src/LimitedNodeList.cpp | 47 ++++++++++++++------ libraries/networking/src/LimitedNodeList.h | 12 +++-- libraries/networking/src/NLPacket.cpp | 4 +- libraries/networking/src/NLPacket.h | 6 +-- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index 33139e66e8..090c3bdd8b 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -214,7 +214,19 @@ bool LimitedNodeList::packetSourceAndHashMatch(const NLPacket& packet, SharedNod return false; } -qint64 LimitedNodeList::writeDatagram(const NLPacket& packet, const HifiSockAddr& destinationSockAddr) { +// NLPacket helper for filling the header +void writePacketheader(const NLPacket& packet, const QUuid& sessionUUID = QUuid(), const QUuid& connectionSecret = QUuid()) { + if (!NON_SOURCED_PACKETS.contains(packet.getType())) { + const_cast(packet).writeSourceID(sessionUUID); + } + if (!connectionSecret.isNull() && !NON_VERIFIED_PACKETS.contains(packet.getType())) { + const_cast(packet).writeVerificationHash(packet.payloadHashWithConnectionUUID(connectionSecret)); + } +} + +qint64 LimitedNodeList::writeDatagram(const NLPacket& packet, const HifiSockAddr& destinationSockAddr, + const QUuid& connectionSecret) { + writePacketheader(packet, getSessionUUID(), connectionSecret); return writeDatagram({packet.getData(), static_cast(packet.getSizeWithHeader())}, destinationSockAddr); } @@ -235,31 +247,35 @@ qint64 LimitedNodeList::writeDatagram(const QByteArray& datagram, const HifiSock } qint64 LimitedNodeList::sendUnreliablePacket(const NLPacket& packet, const Node& destinationNode) { - if (!destinationNode.getActiveSocket()) { + const HifiSockAddr* activeSocket = destinationNode.getActiveSocket(); + if (!activeSocket) { // we don't have a socket to send to, return 0 return 0; } // use the node's active socket as the destination socket - return sendUnreliablePacket(packet, *destinationNode.getActiveSocket()); + return sendUnreliablePacket(packet, *activeSocket, destinationNode.getConnectionSecret()); } -qint64 LimitedNodeList::sendUnreliablePacket(const NLPacket& packet, const HifiSockAddr& sockAddr) { - return writeDatagram(packet, sockAddr); +qint64 LimitedNodeList::sendUnreliablePacket(const NLPacket& packet, const HifiSockAddr& sockAddr, + const QUuid& connectionSecret) { + return writeDatagram(packet, sockAddr, connectionSecret); } qint64 LimitedNodeList::sendPacket(std::unique_ptr packet, const Node& destinationNode) { - if (!destinationNode.getActiveSocket()) { + const HifiSockAddr* activeSocket = destinationNode.getActiveSocket(); + if (!activeSocket) { // we don't have a socket to send to, return 0 return 0; } // use the node's active socket as the destination socket - return sendPacket(std::move(packet), *destinationNode.getActiveSocket()); + return sendPacket(std::move(packet), *activeSocket, destinationNode.getConnectionSecret()); } -qint64 LimitedNodeList::sendPacket(std::unique_ptr packet, const HifiSockAddr& sockAddr) { - return writeDatagram(*packet, sockAddr); +qint64 LimitedNodeList::sendPacket(std::unique_ptr packet, const HifiSockAddr& sockAddr, + const QUuid& connectionSecret) { + return writeDatagram(*packet, sockAddr, connectionSecret); } qint64 LimitedNodeList::sendPacketList(NLPacketList& packetList, const Node& destinationNode) { @@ -268,17 +284,20 @@ qint64 LimitedNodeList::sendPacketList(NLPacketList& packetList, const Node& des // we don't have a socket to send to, return 0 return 0; } - return sendPacketList(packetList, *activeSocket); + + // use the node's active socket as the destination socket + return sendPacketList(packetList, *activeSocket, destinationNode.getConnectionSecret()); } -qint64 LimitedNodeList::sendPacketList(NLPacketList& packetList, const HifiSockAddr& sockAddr) { - qint64 bytesSent { 0 }; +qint64 LimitedNodeList::sendPacketList(NLPacketList& packetList, const HifiSockAddr& sockAddr, + const QUuid& connectionSecret) { + qint64 bytesSent{ 0 }; // close the last packet in the list packetList.closeCurrentPacket(); while (!packetList._packets.empty()) { - bytesSent += sendPacket(std::move(packetList.takeFront()), sockAddr); + bytesSent += sendPacket(std::move(packetList.takeFront()), sockAddr, connectionSecret); } return bytesSent; @@ -289,7 +308,7 @@ qint64 LimitedNodeList::sendPacket(std::unique_ptr packet, const Node& // use the node's active socket as the destination socket if there is no overriden socket address auto& destinationSockAddr = (overridenSockAddr.isNull()) ? *destinationNode.getActiveSocket() : overridenSockAddr; - return sendPacket(std::move(packet), destinationSockAddr); + return sendPacket(std::move(packet), destinationSockAddr, destinationNode.getConnectionSecret()); } PacketSequenceNumber LimitedNodeList::getNextSequenceNumberForPacket(const QUuid& nodeUUID, PacketType::Value packetType) { diff --git a/libraries/networking/src/LimitedNodeList.h b/libraries/networking/src/LimitedNodeList.h index 59e5d73acd..cd3a0f0594 100644 --- a/libraries/networking/src/LimitedNodeList.h +++ b/libraries/networking/src/LimitedNodeList.h @@ -120,13 +120,16 @@ public: PacketReceiver& getPacketReceiver() { return _packetReceiver; } qint64 sendUnreliablePacket(const NLPacket& packet, const Node& destinationNode); - qint64 sendUnreliablePacket(const NLPacket& packet, const HifiSockAddr& sockAddr); + qint64 sendUnreliablePacket(const NLPacket& packet, const HifiSockAddr& sockAddr, + const QUuid& connectionSecret = QUuid()); qint64 sendPacket(std::unique_ptr packet, const Node& destinationNode); - qint64 sendPacket(std::unique_ptr packet, const HifiSockAddr& sockAddr); + qint64 sendPacket(std::unique_ptr packet, const HifiSockAddr& sockAddr, + const QUuid& connectionSecret = QUuid()); qint64 sendPacketList(NLPacketList& packetList, const Node& destinationNode); - qint64 sendPacketList(NLPacketList& packetList, const HifiSockAddr& sockAddr); + qint64 sendPacketList(NLPacketList& packetList, const HifiSockAddr& sockAddr, + const QUuid& connectionSecret = QUuid()); void (*linkedDataCreateCallback)(Node *); @@ -245,7 +248,8 @@ protected: LimitedNodeList(LimitedNodeList const&); // Don't implement, needed to avoid copies of singleton void operator=(LimitedNodeList const&); // Don't implement, needed to avoid copies of singleton - qint64 writeDatagram(const NLPacket& packet, const HifiSockAddr& destinationSockAddr); + qint64 writeDatagram(const NLPacket& packet, const HifiSockAddr& destinationSockAddr, + const QUuid& connectionSecret = QUuid()); qint64 writeDatagram(const QByteArray& datagram, const HifiSockAddr& destinationSockAddr); PacketSequenceNumber getNextSequenceNumberForPacket(const QUuid& nodeUUID, PacketType::Value packetType); diff --git a/libraries/networking/src/NLPacket.cpp b/libraries/networking/src/NLPacket.cpp index d1fabb1c15..e4f37b68c8 100644 --- a/libraries/networking/src/NLPacket.cpp +++ b/libraries/networking/src/NLPacket.cpp @@ -115,7 +115,7 @@ void NLPacket::readVerificationHash() { } } -void NLPacket::setSourceID(const QUuid& sourceID) { +void NLPacket::writeSourceID(const QUuid& sourceID) { Q_ASSERT(!NON_SOURCED_PACKETS.contains(_type)); auto offset = Packet::totalHeadersSize(); @@ -124,7 +124,7 @@ void NLPacket::setSourceID(const QUuid& sourceID) { _sourceID = sourceID; } -void NLPacket::setVerificationHash(const QByteArray& verificationHash) { +void NLPacket::writeVerificationHash(const QByteArray& verificationHash) { Q_ASSERT(!NON_SOURCED_PACKETS.contains(_type) && !NON_VERIFIED_PACKETS.contains(_type)); auto offset = Packet::totalHeadersSize() + NUM_BYTES_RFC4122_UUID; diff --git a/libraries/networking/src/NLPacket.h b/libraries/networking/src/NLPacket.h index 0c9d31c66e..e9f397766d 100644 --- a/libraries/networking/src/NLPacket.h +++ b/libraries/networking/src/NLPacket.h @@ -33,6 +33,9 @@ public: const QUuid& getSourceID() const { return _sourceID; } const QByteArray& getVerificationHash() const { return _verificationHash; } + + void writeSourceID(const QUuid& sourceID); + void writeVerificationHash(const QByteArray& verificationHash); QByteArray payloadHashWithConnectionUUID(const QUuid& connectionUUID) const; @@ -43,10 +46,7 @@ protected: NLPacket(const NLPacket& other); void readSourceID(); - void setSourceID(const QUuid& sourceID); - void readVerificationHash(); - void setVerificationHash(const QByteArray& verificationHash); QUuid _sourceID; QByteArray _verificationHash;