mirror of
https://github.com/overte-org/overte.git
synced 2025-08-07 21:30:33 +02:00
add a fallback if STUN to outside server isn't possible
This commit is contained in:
parent
42de553e32
commit
87d6c0085f
3 changed files with 71 additions and 41 deletions
|
@ -518,6 +518,19 @@ int DomainServer::run() {
|
||||||
int numBytesPrivateSocket = unpackSocket(packetData + packetIndex, (sockaddr*) &nodePublicAddress);
|
int numBytesPrivateSocket = unpackSocket(packetData + packetIndex, (sockaddr*) &nodePublicAddress);
|
||||||
packetIndex += numBytesPrivateSocket;
|
packetIndex += numBytesPrivateSocket;
|
||||||
|
|
||||||
|
if (nodePublicAddress.sin_addr.s_addr == 0) {
|
||||||
|
// this node wants to use us its STUN server
|
||||||
|
// so set the node public address to whatever we perceive the public address to be
|
||||||
|
|
||||||
|
nodePublicAddress = senderAddress;
|
||||||
|
|
||||||
|
// if the sender is on our box then leave its public address to 0 so that
|
||||||
|
// other users attempt to reach it on the same address they have for the domain-server
|
||||||
|
if (senderAddress.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
|
||||||
|
nodePublicAddress.sin_addr.s_addr = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int numBytesPublicSocket = unpackSocket(packetData + packetIndex, (sockaddr*) &nodeLocalAddress);
|
int numBytesPublicSocket = unpackSocket(packetData + packetIndex, (sockaddr*) &nodeLocalAddress);
|
||||||
packetIndex += numBytesPublicSocket;
|
packetIndex += numBytesPublicSocket;
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,8 @@ NodeList::NodeList(char newOwnerType, unsigned short int newSocketListenPort) :
|
||||||
_checkInPacket(NULL),
|
_checkInPacket(NULL),
|
||||||
_numBytesCheckInPacket(0),
|
_numBytesCheckInPacket(0),
|
||||||
_publicAddress(),
|
_publicAddress(),
|
||||||
_publicPort(0)
|
_publicPort(0),
|
||||||
|
_shouldUseDomainServerAsSTUN(0)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -311,11 +312,15 @@ void NodeList::setNodeTypesOfInterest(const char* nodeTypesOfInterest, int numNo
|
||||||
|
|
||||||
const uint32_t RFC_5389_MAGIC_COOKIE = 0x2112A442;
|
const uint32_t RFC_5389_MAGIC_COOKIE = 0x2112A442;
|
||||||
const int NUM_BYTES_STUN_HEADER = 20;
|
const int NUM_BYTES_STUN_HEADER = 20;
|
||||||
|
const int NUM_STUN_REQUESTS_BEFORE_FALLBACK = 5;
|
||||||
|
|
||||||
void NodeList::sendSTUNRequest() {
|
void NodeList::sendSTUNRequest() {
|
||||||
const char STUN_SERVER_HOSTNAME[] = "stun.highfidelity.io";
|
const char STUN_SERVER_HOSTNAME[] = "stun.highfidelity.io";
|
||||||
const unsigned short STUN_SERVER_PORT = 3478;
|
const unsigned short STUN_SERVER_PORT = 3478;
|
||||||
|
|
||||||
|
static int failedStunRequests = 0;
|
||||||
|
|
||||||
|
if (failedStunRequests < NUM_STUN_REQUESTS_BEFORE_FALLBACK) {
|
||||||
unsigned char stunRequestPacket[NUM_BYTES_STUN_HEADER];
|
unsigned char stunRequestPacket[NUM_BYTES_STUN_HEADER];
|
||||||
|
|
||||||
int packetIndex = 0;
|
int packetIndex = 0;
|
||||||
|
@ -358,6 +363,17 @@ void NodeList::sendSTUNRequest() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
failedStunRequests++;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we're here this was the last failed STUN request
|
||||||
|
// use our DS as our stun server
|
||||||
|
qDebug("Failed to lookup public address via STUN server at %s:%hu. Using DS for STUN.\n",
|
||||||
|
STUN_SERVER_HOSTNAME, STUN_SERVER_PORT);
|
||||||
|
_shouldUseDomainServerAsSTUN = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeList::processSTUNResponse(unsigned char* packetData, size_t dataBytes) {
|
void NodeList::processSTUNResponse(unsigned char* packetData, size_t dataBytes) {
|
||||||
|
@ -455,7 +471,7 @@ void NodeList::sendDomainServerCheckIn() {
|
||||||
printedDomainServerIP = true;
|
printedDomainServerIP = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_publicAddress.isNull()) {
|
if (_publicAddress.isNull() && !_shouldUseDomainServerAsSTUN) {
|
||||||
// we don't know our public socket and we need to send it to the domain server
|
// 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
|
// send a STUN request to figure it out
|
||||||
sendSTUNRequest();
|
sendSTUNRequest();
|
||||||
|
|
|
@ -170,6 +170,7 @@ private:
|
||||||
int _numBytesCheckInPacket;
|
int _numBytesCheckInPacket;
|
||||||
QHostAddress _publicAddress;
|
QHostAddress _publicAddress;
|
||||||
uint16_t _publicPort;
|
uint16_t _publicPort;
|
||||||
|
bool _shouldUseDomainServerAsSTUN;
|
||||||
|
|
||||||
void activateSocketFromPingReply(sockaddr *nodeAddress);
|
void activateSocketFromPingReply(sockaddr *nodeAddress);
|
||||||
void timePingReply(sockaddr *nodeAddress, unsigned char *packetData);
|
void timePingReply(sockaddr *nodeAddress, unsigned char *packetData);
|
||||||
|
|
Loading…
Reference in a new issue