Write session uuid and connection hash in packet header

This commit is contained in:
Atlante45 2015-07-15 11:42:35 -07:00
parent a7fa0a733b
commit 0b32a5d935
4 changed files with 46 additions and 23 deletions

View file

@ -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<NLPacket&>(packet).writeSourceID(sessionUUID);
}
if (!connectionSecret.isNull() && !NON_VERIFIED_PACKETS.contains(packet.getType())) {
const_cast<NLPacket&>(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<int>(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<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 sendPacket(std::move(packet), *destinationNode.getActiveSocket());
return sendPacket(std::move(packet), *activeSocket, destinationNode.getConnectionSecret());
}
qint64 LimitedNodeList::sendPacket(std::unique_ptr<NLPacket> packet, const HifiSockAddr& sockAddr) {
return writeDatagram(*packet, sockAddr);
qint64 LimitedNodeList::sendPacket(std::unique_ptr<NLPacket> 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<NLPacket>()), sockAddr);
bytesSent += sendPacket(std::move(packetList.takeFront<NLPacket>()), sockAddr, connectionSecret);
}
return bytesSent;
@ -289,7 +308,7 @@ qint64 LimitedNodeList::sendPacket(std::unique_ptr<NLPacket> 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) {

View file

@ -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<NLPacket> packet, const Node& destinationNode);
qint64 sendPacket(std::unique_ptr<NLPacket> packet, const HifiSockAddr& sockAddr);
qint64 sendPacket(std::unique_ptr<NLPacket> 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);

View file

@ -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;

View file

@ -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;