Eliminate a little copypasta.

This commit is contained in:
Adam Ierymenko 2017-02-04 00:04:44 -08:00
parent dcb1233b0d
commit d9e4ba1280
2 changed files with 59 additions and 68 deletions

View File

@ -225,87 +225,66 @@ void Switch::onRemotePacket(const InetAddress &localAddr,const InetAddress &from
SharedPtr<Peer> relayTo = RR->topology->getPeer(destination); SharedPtr<Peer> relayTo = RR->topology->getPeer(destination);
if ((relayTo)&&((relayTo->sendDirect(packet.data(),packet.size(),now,false)))) { if ((relayTo)&&((relayTo->sendDirect(packet.data(),packet.size(),now,false)))) {
if (source != RR->identity.address()) { // don't send RENDEZVOUS for cluster frontplane relays if ((source != RR->identity.address())&&(_shouldUnite(now,source,destination))) { // don't send RENDEZVOUS for cluster frontplane relays
const InetAddress *hintToSource = (InetAddress *)0;
const InetAddress *hintToDest = (InetAddress *)0;
bool shouldUnite; InetAddress destV4,destV6;
{ InetAddress sourceV4,sourceV6;
Mutex::Lock _l(_lastUniteAttempt_m); relayTo->getRendezvousAddresses(now,destV4,destV6);
uint64_t &lastUniteAt = _lastUniteAttempt[_LastUniteKey(source,destination)];
shouldUnite = ((now - lastUniteAt) >= ZT_MIN_UNITE_INTERVAL);
if (shouldUnite)
lastUniteAt = now;
}
if (shouldUnite) { const SharedPtr<Peer> sourcePeer(RR->topology->getPeer(source));
const InetAddress *hintToSource = (InetAddress *)0; if (sourcePeer) {
const InetAddress *hintToDest = (InetAddress *)0; sourcePeer->getRendezvousAddresses(now,sourceV4,sourceV6);
if ((destV6)&&(sourceV6)) {
hintToSource = &destV6;
hintToDest = &sourceV6;
} else if ((destV4)&&(sourceV4)) {
hintToSource = &destV4;
hintToDest = &sourceV4;
}
InetAddress destV4,destV6; if ((hintToSource)&&(hintToDest)) {
InetAddress sourceV4,sourceV6; TRACE(">> RENDEZVOUS: %s(%s) <> %s(%s)",p1.toString().c_str(),p1a->toString().c_str(),p2.toString().c_str(),p2a->toString().c_str());
relayTo->getRendezvousAddresses(now,destV4,destV6); unsigned int alt = (unsigned int)RR->node->prng() & 1; // randomize which hint we send first for obscure NAT-t reasons
const unsigned int completed = alt + 2;
const SharedPtr<Peer> sourcePeer(RR->topology->getPeer(source)); while (alt != completed) {
if (sourcePeer) { if ((alt & 1) == 0) {
sourcePeer->getRendezvousAddresses(now,sourceV4,sourceV6); Packet outp(source,RR->identity.address(),Packet::VERB_RENDEZVOUS);
if ((destV6)&&(sourceV6)) { outp.append((uint8_t)0);
hintToSource = &destV6; destination.appendTo(outp);
hintToDest = &sourceV6; outp.append((uint16_t)hintToSource->port());
} else if ((destV4)&&(sourceV4)) { if (hintToSource->ss_family == AF_INET6) {
hintToSource = &destV4; outp.append((uint8_t)16);
hintToDest = &sourceV4; outp.append(hintToSource->rawIpData(),16);
}
if ((hintToSource)&&(hintToDest)) {
TRACE(">> RENDEZVOUS: %s(%s) <> %s(%s)",p1.toString().c_str(),p1a->toString().c_str(),p2.toString().c_str(),p2a->toString().c_str());
unsigned int alt = (unsigned int)RR->node->prng() & 1; // randomize which hint we send first for obscure NAT-t reasons
const unsigned int completed = alt + 2;
while (alt != completed) {
if ((alt & 1) == 0) {
Packet outp(source,RR->identity.address(),Packet::VERB_RENDEZVOUS);
outp.append((uint8_t)0);
destination.appendTo(outp);
outp.append((uint16_t)hintToSource->port());
if (hintToSource->ss_family == AF_INET6) {
outp.append((uint8_t)16);
outp.append(hintToSource->rawIpData(),16);
} else {
outp.append((uint8_t)4);
outp.append(hintToSource->rawIpData(),4);
}
send(outp,true);
} else { } else {
Packet outp(destination,RR->identity.address(),Packet::VERB_RENDEZVOUS); outp.append((uint8_t)4);
outp.append((uint8_t)0); outp.append(hintToSource->rawIpData(),4);
source.appendTo(outp);
outp.append((uint16_t)hintToDest->port());
if (hintToDest->ss_family == AF_INET6) {
outp.append((uint8_t)16);
outp.append(hintToDest->rawIpData(),16);
} else {
outp.append((uint8_t)4);
outp.append(hintToDest->rawIpData(),4);
}
send(outp,true);
} }
++alt; send(outp,true);
} else {
Packet outp(destination,RR->identity.address(),Packet::VERB_RENDEZVOUS);
outp.append((uint8_t)0);
source.appendTo(outp);
outp.append((uint16_t)hintToDest->port());
if (hintToDest->ss_family == AF_INET6) {
outp.append((uint8_t)16);
outp.append(hintToDest->rawIpData(),16);
} else {
outp.append((uint8_t)4);
outp.append(hintToDest->rawIpData(),4);
}
send(outp,true);
} }
++alt;
} }
} }
} }
} }
} else { } else {
#ifdef ZT_ENABLE_CLUSTER #ifdef ZT_ENABLE_CLUSTER
if ((RR->cluster)&&(source != RR->identity.address())) { if ((RR->cluster)&&(source != RR->identity.address())) {
bool shouldUnite; RR->cluster->relayViaCluster(source,destination,packet.data(),packet.size(),_shouldUnite(now,source,destination));
{
Mutex::Lock _l(_lastUniteAttempt_m);
uint64_t &luts = _lastUniteAttempt[_LastUniteKey(source,destination)];
shouldUnite = ((now - luts) >= ZT_MIN_UNITE_INTERVAL);
if (shouldUnite)
luts = now;
}
RR->cluster->relayViaCluster(source,destination,packet.data(),packet.size(),shouldUnite);
return; return;
} }
#endif #endif
@ -747,6 +726,17 @@ unsigned long Switch::doTimerTasks(uint64_t now)
return nextDelay; return nextDelay;
} }
bool Switch::_shouldUnite(const uint64_t now,const Address &source,const Address &destination)
{
Mutex::Lock _l(_lastUniteAttempt_m);
uint64_t &ts = _lastUniteAttempt[_LastUniteKey(source,destination)];
if ((now - ts) >= ZT_MIN_UNITE_INTERVAL) {
ts = now;
return true;
}
return false;
}
Address Switch::_sendWhoisRequest(const Address &addr,const Address *peersAlreadyConsulted,unsigned int numPeersAlreadyConsulted) Address Switch::_sendWhoisRequest(const Address &addr,const Address *peersAlreadyConsulted,unsigned int numPeersAlreadyConsulted)
{ {
SharedPtr<Peer> upstream(RR->topology->getUpstreamPeer(peersAlreadyConsulted,numPeersAlreadyConsulted,false)); SharedPtr<Peer> upstream(RR->topology->getUpstreamPeer(peersAlreadyConsulted,numPeersAlreadyConsulted,false));

View File

@ -124,6 +124,7 @@ public:
unsigned long doTimerTasks(uint64_t now); unsigned long doTimerTasks(uint64_t now);
private: private:
bool _shouldUnite(const uint64_t now,const Address &source,const Address &destination);
Address _sendWhoisRequest(const Address &addr,const Address *peersAlreadyConsulted,unsigned int numPeersAlreadyConsulted); Address _sendWhoisRequest(const Address &addr,const Address *peersAlreadyConsulted,unsigned int numPeersAlreadyConsulted);
bool _trySend(Packet &packet,bool encrypt); // packet is modified if return is true bool _trySend(Packet &packet,bool encrypt); // packet is modified if return is true