From 3dca04aacb5d1f915525bcf30938c566f7d23f08 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 6 Feb 2014 11:30:53 -0800 Subject: [PATCH] have the domain-server generate connection secrets, closes #1837 --- domain-server/src/DomainServer.cpp | 41 +++++++++++++++++++----- domain-server/src/DomainServer.h | 4 ++- domain-server/src/DomainServerNodeData.h | 26 +++++++++++++++ libraries/shared/src/Node.cpp | 1 + libraries/shared/src/Node.h | 5 ++- libraries/shared/src/NodeList.cpp | 7 ++-- 6 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 domain-server/src/DomainServerNodeData.h diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 8c4c9348c0..921763d261 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -21,6 +21,8 @@ #include #include +#include "DomainServerNodeData.h" + #include "DomainServer.h" const int RESTART_HOLD_TIME_MSECS = 5 * 1000; @@ -57,8 +59,9 @@ DomainServer::DomainServer(int argc, char* argv[]) : populateDefaultStaticAssignmentsExcludingTypes(parsedTypes); NodeList* nodeList = NodeList::createInstance(NodeType::DomainServer, domainServerPort); - - connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), this, SLOT(nodeKilled(SharedNodePointer))); + + connect(nodeList, &NodeList::nodeAdded, this, &DomainServer::nodeAdded); + connect(nodeList, &NodeList::nodeKilled, this, &DomainServer::nodeKilled); QTimer* silentNodeTimer = new QTimer(this); connect(silentNodeTimer, SIGNAL(timeout()), nodeList, SLOT(removeSilentNodes())); @@ -270,9 +273,7 @@ void DomainServer::readAvailableDatagrams() { if (nodeUUID.isNull()) { // this is a check in from an unidentified node // we need to generate a session UUID for this node - qDebug() << "received a check-in from an unidentified node"; nodeUUID = QUuid::createUuid(); - qDebug() << "UUID set to" << nodeUUID; } SharedNodePointer checkInNode = nodeList->addOrUpdateNode(nodeUUID, @@ -303,14 +304,33 @@ void DomainServer::readAvailableDatagrams() { QDataStream broadcastDataStream(&broadcastPacket, QIODevice::Append); broadcastDataStream << checkInNode->getUUID(); + DomainServerNodeData* nodeData = reinterpret_cast(checkInNode->getLinkedData()); + if (numInterestTypes > 0) { // if the node has any interest types, send back those nodes as well - foreach (const SharedNodePointer& node, nodeList->getNodeHash()) { - if (node->getUUID() != nodeUUID && - memchr(nodeTypesOfInterest, node->getType(), numInterestTypes)) { + foreach (const SharedNodePointer& otherNode, nodeList->getNodeHash()) { + if (otherNode->getUUID() != nodeUUID && + memchr(nodeTypesOfInterest, otherNode->getType(), numInterestTypes)) { // don't send avatar nodes to other avatars, that will come from avatar mixer - broadcastDataStream << *node.data(); + broadcastDataStream << *otherNode.data(); + + // pack the secret that these two nodes will use to communicate with each other + QUuid secretUUID = nodeData->getSessionSecretHash().value(otherNode->getUUID()); + if (secretUUID.isNull()) { + // generate a new secret UUID these two nodes can use + secretUUID = QUuid::createUuid(); + + // set that on the current Node's sessionSecretHash + nodeData->getSessionSecretHash().insert(otherNode->getUUID(), secretUUID); + + // set it on the other Node's sessionSecretHash + reinterpret_cast(otherNode->getLinkedData()) + ->getSessionSecretHash().insert(nodeUUID, secretUUID); + + } + + broadcastDataStream << secretUUID; } } } @@ -556,6 +576,11 @@ void DomainServer::refreshStaticAssignmentAndAddToQueue(SharedAssignmentPointer& _staticAssignmentHash.remove(oldUUID); } +void DomainServer::nodeAdded(SharedNodePointer node) { + // we don't use updateNodeWithData, so add the DomainServerNodeData to the node here + node->setLinkedData(new DomainServerNodeData()); +} + void DomainServer::nodeKilled(SharedNodePointer node) { // if this node's UUID matches a static assignment we need to throw it back in the assignment queue SharedAssignmentPointer matchedAssignment = _staticAssignmentHash.value(node->getUUID()); diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index 60251b3bb4..580a758df8 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -30,7 +30,9 @@ public: void exit(int retCode = 0); public slots: - /// Called by NodeList to inform us that a node has been killed. + /// Called by NodeList to inform us a node has been added + void nodeAdded(SharedNodePointer node); + /// Called by NodeList to inform us a node has been killed void nodeKilled(SharedNodePointer node); private: diff --git a/domain-server/src/DomainServerNodeData.h b/domain-server/src/DomainServerNodeData.h new file mode 100644 index 0000000000..ea56e31f1c --- /dev/null +++ b/domain-server/src/DomainServerNodeData.h @@ -0,0 +1,26 @@ +// +// DomainServerNodeData.h +// hifi +// +// Created by Stephen Birarda on 2/6/2014. +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. +// + +#ifndef __hifi__DomainServerNodeData__ +#define __hifi__DomainServerNodeData__ + +#include + +#include + +class DomainServerNodeData : public NodeData { +public: + DomainServerNodeData() : _sessionSecretHash() {}; + int parseData(const QByteArray& packet) { return 0; } + + QHash& getSessionSecretHash() { return _sessionSecretHash; } +private: + QHash _sessionSecretHash; +}; + +#endif /* defined(__hifi__DomainServerNodeData__) */ diff --git a/libraries/shared/src/Node.cpp b/libraries/shared/src/Node.cpp index 8efbf5782a..bb45265f8b 100644 --- a/libraries/shared/src/Node.cpp +++ b/libraries/shared/src/Node.cpp @@ -52,6 +52,7 @@ Node::Node(const QUuid& uuid, char type, const HifiSockAddr& publicSocket, const _publicSocket(publicSocket), _localSocket(localSocket), _activeSocket(NULL), + _connectionSecret(), _bytesReceivedMovingAverage(NULL), _linkedData(NULL), _isAlive(true), diff --git a/libraries/shared/src/Node.h b/libraries/shared/src/Node.h index 952e1b1be2..43ec5baf81 100644 --- a/libraries/shared/src/Node.h +++ b/libraries/shared/src/Node.h @@ -56,7 +56,6 @@ public: char getType() const { return _type; } void setType(char type) { _type = type; } - const QUuid& getUUID() const { return _uuid; } void setUUID(const QUuid& uuid) { _uuid = uuid; } @@ -76,6 +75,9 @@ public: void activatePublicSocket(); void activateLocalSocket(); + + const QUuid& getConnectionSecret() const { return _connectionSecret; } + void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; } NodeData* getLinkedData() const { return _linkedData; } void setLinkedData(NodeData* linkedData) { _linkedData = linkedData; } @@ -109,6 +111,7 @@ private: HifiSockAddr _publicSocket; HifiSockAddr _localSocket; HifiSockAddr* _activeSocket; + QUuid _connectionSecret; SimpleMovingAverage* _bytesReceivedMovingAverage; NodeData* _linkedData; bool _isAlive; diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index c83a908a7f..4b5fdfeaa6 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -511,7 +511,7 @@ int NodeList::processDomainServerList(const QByteArray& packet) { // setup variables to read into from QDataStream qint8 nodeType; - QUuid nodeUUID; + QUuid nodeUUID, connectionUUID; HifiSockAddr nodePublicSocket; HifiSockAddr nodeLocalSocket; @@ -534,7 +534,10 @@ int NodeList::processDomainServerList(const QByteArray& packet) { nodePublicSocket.setAddress(_domainSockAddr.getAddress()); } - addOrUpdateNode(nodeUUID, nodeType, nodePublicSocket, nodeLocalSocket); + SharedNodePointer node = addOrUpdateNode(nodeUUID, nodeType, nodePublicSocket, nodeLocalSocket); + + packetStream >> connectionUUID; + node->setConnectionSecret(connectionUUID); } return readNodes;