handle domain connection via ICE from NodeList

This commit is contained in:
Stephen Birarda 2014-10-02 16:51:12 -07:00
parent c7aaf0ce4a
commit 63877b0756
7 changed files with 46 additions and 15 deletions

View file

@ -983,8 +983,10 @@ void DomainServer::updateNetworkingInfoWithDataServer(const QString& newSetting,
domainUpdateJSON.toUtf8());
}
const HifiSockAddr ICE_SERVER_SOCK_ADDR = HifiSockAddr(QHostAddress::LocalHost, ICE_SERVER_DEFAULT_PORT);
void DomainServer::sendHearbeatToIceServer() {
LimitedNodeList::getInstance()->sendHeartbeatToIceServer();
LimitedNodeList::getInstance()->sendHeartbeatToIceServer(ICE_SERVER_SOCK_ADDR);
}
void DomainServer::processDatagram(const QByteArray& receivedPacket, const HifiSockAddr& senderSockAddr) {
@ -1028,7 +1030,7 @@ void DomainServer::processDatagram(const QByteArray& receivedPacket, const HifiS
case PacketTypeStunResponse:
nodeList->processSTUNResponse(receivedPacket);
break;
case PacketTypePing: {
case PacketTypeUnverifiedPing: {
QByteArray pingReplyPacket = nodeList->constructPingReplyPacket(receivedPacket);
nodeList->writeUnverifiedDatagram(pingReplyPacket, senderSockAddr);

View file

@ -138,11 +138,20 @@ void DomainHandler::setIceServerHostnameAndID(const QString& iceServerHostname,
setUUID(id);
_iceServerSockAddr = HifiSockAddr(iceServerHostname, ICE_SERVER_DEFAULT_PORT);
qDebug() << "Domain ID changed to" << uuidStringWithoutCurlyBraces(_uuid)
<< "- ICE required via ice server at" << iceServerHostname;
qDebug() << "ICE required to connect to domain via ice server at" << iceServerHostname;
}
}
void DomainHandler::activateICELocalSocket() {
_sockAddr = _icePeer.getLocalSocket();
_hostname = _sockAddr.getAddress().toString();
}
void DomainHandler::activateICEPublicSocket() {
_sockAddr = _icePeer.getPublicSocket();
_hostname = _sockAddr.getAddress().toString();
}
void DomainHandler::completedHostnameLookup(const QHostInfo& hostInfo) {
for (int i = 0; i < hostInfo.addresses().size(); i++) {
if (hostInfo.addresses()[i].protocol() == QAbstractSocket::IPv4Protocol) {

View file

@ -58,6 +58,8 @@ public:
bool requiresICE() const { return !_iceServerSockAddr.isNull(); }
const HifiSockAddr& getICEServerSockAddr() const { return _iceServerSockAddr; }
const NetworkPeer& getICEPeer() const { return _icePeer; }
void activateICELocalSocket();
void activateICEPublicSocket();
bool isConnected() const { return _isConnected; }
void setIsConnected(bool isConnected);

View file

@ -459,8 +459,8 @@ unsigned LimitedNodeList::broadcastToNodes(const QByteArray& packet, const NodeS
return n;
}
QByteArray LimitedNodeList::constructPingPacket(PingType_t pingType) {
QByteArray pingPacket = byteArrayWithPopulatedHeader(PacketTypePing);
QByteArray LimitedNodeList::constructPingPacket(PingType_t pingType, bool isVerified) {
QByteArray pingPacket = byteArrayWithPopulatedHeader(isVerified ? PacketTypePing : PacketTypeUnverifiedPing);
QDataStream packetStream(&pingPacket, QIODevice::Append);
@ -480,7 +480,10 @@ QByteArray LimitedNodeList::constructPingReplyPacket(const QByteArray& pingPacke
quint64 timeFromOriginalPing;
pingPacketStream >> timeFromOriginalPing;
QByteArray replyPacket = byteArrayWithPopulatedHeader(PacketTypePingReply);
PacketType replyType = (packetTypeForPacket(pingPacket) == PacketTypePing)
? PacketTypePingReply : PacketTypeUnverifiedPingReply;
QByteArray replyPacket = byteArrayWithPopulatedHeader(replyType);
QDataStream packetStream(&replyPacket, QIODevice::Append);
packetStream << typeFromOriginalPing << timeFromOriginalPing << usecTimestampNow();

View file

@ -112,7 +112,7 @@ public:
void getPacketStats(float &packetsPerSecond, float &bytesPerSecond);
void resetPacketStats();
QByteArray constructPingPacket(PingType_t pingType = PingType::Agnostic);
QByteArray constructPingPacket(PingType_t pingType = PingType::Agnostic, bool isVerified = true);
QByteArray constructPingReplyPacket(const QByteArray& pingPacket);
virtual void sendSTUNRequest();

View file

@ -161,12 +161,24 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr
// set the ping time for this node for stat collection
timePingReply(packet, sendingNode);
} else if (uuidFromPacketHeader(packet) == _domainHandler.getUUID()) {
qDebug() << "RECEIVED A REPLY FROM DOMAIN";
}
break;
}
case PacketTypeUnverifiedPingReply: {
qDebug() << "Received reply from domain-server on" << senderSockAddr;
// for now we're unsafely assuming this came back from the domain
if (senderSockAddr == _domainHandler.getICEPeer().getLocalSocket()) {
qDebug() << "Connecting to domain using local socket";
_domainHandler.activateICELocalSocket();
} else if (senderSockAddr == _domainHandler.getICEPeer().getPublicSocket()) {
qDebug() << "Conecting to domain using public socket";
_domainHandler.activateICEPublicSocket();
} else {
qDebug() << "Reply does not match either local or public socket for domain. Will not connect.";
}
}
case PacketTypeStunResponse: {
// 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
@ -251,7 +263,7 @@ void NodeList::sendDomainServerCheckIn() {
// we don't know our public socket and we need to send it to the domain server
// send a STUN request to figure it out
sendSTUNRequest();
} else if (!_domainHandler.isConnected() && _domainHandler.requiresICE()) {
} else if (_domainHandler.getIP().isNull() && _domainHandler.requiresICE()) {
handleICEConnectionToDomainServer();
} else if (!_domainHandler.getIP().isNull()) {
@ -319,10 +331,10 @@ void NodeList::handleICEConnectionToDomainServer() {
<< uuidStringWithoutCurlyBraces(_domainHandler.getUUID());
// send the ping packet to the local and public sockets for this node
QByteArray localPingPacket = constructPingPacket(PingType::Local);
QByteArray localPingPacket = constructPingPacket(PingType::Local, false);
writeDatagram(localPingPacket, _domainHandler.getICEPeer().getLocalSocket(), iceUUID);
QByteArray publicPingPacket = constructPingPacket(PingType::Public);
QByteArray publicPingPacket = constructPingPacket(PingType::Public, false);
writeDatagram(publicPingPacket, _domainHandler.getICEPeer().getPublicSocket(), iceUUID);
}
}

View file

@ -72,7 +72,9 @@ enum PacketType {
PacketTypeEntityEditNack, // 48
PacketTypeSignedTransactionPayment,
PacketTypeIceServerHeartbeat,
PacketTypeIceServerHeartbeatResponse
PacketTypeIceServerHeartbeatResponse,
PacketTypeUnverifiedPing,
PacketTypeUnverifiedPingReply
};
typedef char PacketVersion;
@ -83,7 +85,8 @@ const QSet<PacketType> NON_VERIFIED_PACKETS = QSet<PacketType>()
<< PacketTypeCreateAssignment << PacketTypeRequestAssignment << PacketTypeStunResponse
<< PacketTypeNodeJsonStats << PacketTypeVoxelQuery << PacketTypeParticleQuery << PacketTypeEntityQuery
<< PacketTypeOctreeDataNack << PacketTypeVoxelEditNack << PacketTypeParticleEditNack << PacketTypeEntityEditNack
<< PacketTypeIceServerHeartbeat << PacketTypeIceServerHeartbeatResponse;
<< PacketTypeIceServerHeartbeat << PacketTypeIceServerHeartbeatResponse
<< PacketTypeUnverifiedPing << PacketTypeUnverifiedPingReply;
const int NUM_BYTES_MD5_HASH = 16;
const int NUM_STATIC_HEADER_BYTES = sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID;