summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2014-08-19 10:09:21 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2014-08-19 10:09:21 -0700
commit282114e96c6b405636a8e4f7011ced5ddd26357e (patch)
tree813df83b8e2a2ff3e217018c01c24bf0beea72a6 /node
parent95d123cfc2730e34b25512bca81caafa3dd3df35 (diff)
downloadinfinitytier-282114e96c6b405636a8e4f7011ced5ddd26357e.tar.gz
infinitytier-282114e96c6b405636a8e4f7011ced5ddd26357e.zip
Makefile changes, and make Topology::getBestSupernode() return the "next" supernode if I am a supernode. Also some comment cleanup.
Diffstat (limited to 'node')
-rw-r--r--node/Switch.cpp29
-rw-r--r--node/Topology.cpp119
2 files changed, 81 insertions, 67 deletions
diff --git a/node/Switch.cpp b/node/Switch.cpp
index 1a28d4a5..fa8a22c0 100644
--- a/node/Switch.cpp
+++ b/node/Switch.cpp
@@ -639,12 +639,12 @@ void Switch::cancelWhoisRequest(const Address &addr)
void Switch::doAnythingWaitingForPeer(const SharedPtr<Peer> &peer)
{
- {
+ { // cancel pending WHOIS since we now know this peer
Mutex::Lock _l(_outstandingWhoisRequests_m);
_outstandingWhoisRequests.erase(peer->address());
}
- {
+ { // finish processing any packets waiting on peer's public key / identity
Mutex::Lock _l(_rxQueue_m);
for(std::list< SharedPtr<PacketDecoder> >::iterator rxi(_rxQueue.begin());rxi!=_rxQueue.end();) {
if ((*rxi)->tryDecode(_r))
@@ -653,7 +653,7 @@ void Switch::doAnythingWaitingForPeer(const SharedPtr<Peer> &peer)
}
}
- {
+ { // finish sending any packets waiting on peer's public key / identity
Mutex::Lock _l(_txQueue_m);
std::pair< std::multimap< Address,TXQueueEntry >::iterator,std::multimap< Address,TXQueueEntry >::iterator > waitingTxQueueItems(_txQueue.equal_range(peer->address()));
for(std::multimap< Address,TXQueueEntry >::iterator txi(waitingTxQueueItems.first);txi!=waitingTxQueueItems.second;) {
@@ -690,8 +690,11 @@ void Switch::_handleRemotePacketFragment(const SharedPtr<Socket> &fromSock,const
if (fragment.hops() < ZT_RELAY_MAX_HOPS) {
fragment.incrementHops();
+ // Note: we don't bother initiating NAT-t for fragments, since heads will set that off.
+ // It wouldn't hurt anything, just redundant and unnecessary.
SharedPtr<Peer> relayTo = _r->topology->getPeer(destination);
if ((!relayTo)||(relayTo->send(_r,fragment.data(),fragment.size(),Utils::now()) == Path::PATH_TYPE_NULL)) {
+ // Don't know peer or no direct path -- so relay via supernode
relayTo = _r->topology->getBestSupernode();
if (relayTo)
relayTo->send(_r,fragment.data(),fragment.size(),Utils::now());
@@ -772,11 +775,7 @@ void Switch::_handleRemotePacketHead(const SharedPtr<Socket> &fromSock,const Ine
if ((fromSock->udp())&&(relayedVia == Path::PATH_TYPE_UDP))
unite(source,destination,false);
} else {
- // If we've received a packet not for us and we don't have
- // a direct path to its recipient, pass it to (another)
- // supernode. This can happen due to Internet weather -- the
- // most direct supernode may not be reachable, yet another
- // further away may be.
+ // Don't know peer or no direct path -- so relay via supernode
relayTo = _r->topology->getBestSupernode(&source,1,true);
if (relayTo)
relayTo->send(_r,packet->data(),packet->size(),Utils::now());
@@ -903,21 +902,9 @@ bool Switch::_trySend(const Packet &packet,bool encrypt)
remaining -= chunkSize;
}
}
-
-/* #ifdef ZT_TRACE
- if (via != peer) {
- TRACE(">> %s to %s via %s (%d)",Packet::verbString(packet.verb()),peer->address().toString().c_str(),via->address().toString().c_str(),(int)packet.size());
- } else {
- TRACE(">> %s to %s (%d)",Packet::verbString(packet.verb()),peer->address().toString().c_str(),(int)packet.size());
- }
-#endif */
-
return true;
}
- return false;
- }
-
- requestWhois(packet.destination());
+ } else requestWhois(packet.destination());
return false;
}
diff --git a/node/Topology.cpp b/node/Topology.cpp
index 109cbca2..183ee06a 100644
--- a/node/Topology.cpp
+++ b/node/Topology.cpp
@@ -161,65 +161,92 @@ void Topology::saveIdentity(const Identity &id)
SharedPtr<Peer> Topology::getBestSupernode(const Address *avoid,unsigned int avoidCount,bool strictAvoid) const
{
SharedPtr<Peer> bestSupernode;
- unsigned int l,bestSupernodeLatency = 65536;
uint64_t now = Utils::now();
- uint64_t lds,ldr;
-
Mutex::Lock _l(_supernodes_m);
- // First look for a best supernode by comparing latencies, but exclude
- // supernodes that have not responded to direct messages in order to
- // try to exclude any that are dead or unreachable.
- for(std::vector< SharedPtr<Peer> >::const_iterator sn(_supernodePeers.begin());sn!=_supernodePeers.end();) {
- // Skip explicitly avoided relays
- for(unsigned int i=0;i<avoidCount;++i) {
- if (avoid[i] == (*sn)->address())
- goto keep_searching_for_supernodes;
+ if (_amSupernode) {
+ /* If I am a supernode, the "best" supernode is the one whose address
+ * is numerically greater than mine (with wrap at top of list). This
+ * causes packets searching for a route to pretty much literally
+ * circumnavigate the globe rather than bouncing between just two. */
+
+ if (_supernodeAddresses.size() > 1) { // gotta be one other than me for this to work
+ std::set<Address>::const_iterator sna(_supernodeAddresses.find(_r->identity.address()));
+ if (sna != _supernodeAddresses.end()) { // sanity check -- _amSupernode should've been false in this case
+ for(;;) {
+ if (++sna == _supernodeAddresses.end())
+ sna = _supernodeAddresses.begin(); // wrap around at end
+ if (*sna != _r->identity.address()) { // pick one other than us -- starting from me+1 in sorted set order
+ SharedPtr<Peer> p(getPeer(*sna));
+ if ((p)&&(p->hasActiveDirectPath(now))) {
+ bestSupernode = p;
+ break;
+ }
+ }
+ }
+ }
}
+ } else {
+ /* If I am not a supernode, the best supernode is the active one with
+ * the lowest latency. */
+
+ unsigned int l,bestSupernodeLatency = 65536;
+ uint64_t lds,ldr;
+
+ // First look for a best supernode by comparing latencies, but exclude
+ // supernodes that have not responded to direct messages in order to
+ // try to exclude any that are dead or unreachable.
+ for(std::vector< SharedPtr<Peer> >::const_iterator sn(_supernodePeers.begin());sn!=_supernodePeers.end();) {
+ // Skip explicitly avoided relays
+ for(unsigned int i=0;i<avoidCount;++i) {
+ if (avoid[i] == (*sn)->address())
+ goto keep_searching_for_supernodes;
+ }
+
+ // Skip possibly comatose or unreachable relays
+ lds = (*sn)->lastDirectSend();
+ ldr = (*sn)->lastDirectReceive();
+ if ((lds)&&(lds > ldr)&&((lds - ldr) > ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD))
+ goto keep_searching_for_supernodes;
- // Skip possibly comatose or unreachable relays
- lds = (*sn)->lastDirectSend();
- ldr = (*sn)->lastDirectReceive();
- if ((lds)&&(lds > ldr)&&((lds - ldr) > ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD))
- goto keep_searching_for_supernodes;
-
- if ((*sn)->hasActiveDirectPath(now)) {
- l = (*sn)->latency();
- if (bestSupernode) {
- if ((l)&&(l < bestSupernodeLatency)) {
- bestSupernodeLatency = l;
+ if ((*sn)->hasActiveDirectPath(now)) {
+ l = (*sn)->latency();
+ if (bestSupernode) {
+ if ((l)&&(l < bestSupernodeLatency)) {
+ bestSupernodeLatency = l;
+ bestSupernode = *sn;
+ }
+ } else {
+ if (l)
+ bestSupernodeLatency = l;
bestSupernode = *sn;
}
- } else {
- if (l)
- bestSupernodeLatency = l;
- bestSupernode = *sn;
}
- }
keep_searching_for_supernodes:
- ++sn;
- }
-
- if (bestSupernode) {
- bestSupernode->use(now);
- return bestSupernode;
- } else if (strictAvoid)
- return SharedPtr<Peer>();
+ ++sn;
+ }
- // If we have nothing from above, just pick one without avoidance criteria.
- for(std::vector< SharedPtr<Peer> >::const_iterator sn=_supernodePeers.begin();sn!=_supernodePeers.end();++sn) {
- if ((*sn)->hasActiveDirectPath(now)) {
- unsigned int l = (*sn)->latency();
- if (bestSupernode) {
- if ((l)&&(l < bestSupernodeLatency)) {
- bestSupernodeLatency = l;
+ if (bestSupernode) {
+ bestSupernode->use(now);
+ return bestSupernode;
+ } else if (strictAvoid)
+ return SharedPtr<Peer>();
+
+ // If we have nothing from above, just pick one without avoidance criteria.
+ for(std::vector< SharedPtr<Peer> >::const_iterator sn=_supernodePeers.begin();sn!=_supernodePeers.end();++sn) {
+ if ((*sn)->hasActiveDirectPath(now)) {
+ unsigned int l = (*sn)->latency();
+ if (bestSupernode) {
+ if ((l)&&(l < bestSupernodeLatency)) {
+ bestSupernodeLatency = l;
+ bestSupernode = *sn;
+ }
+ } else {
+ if (l)
+ bestSupernodeLatency = l;
bestSupernode = *sn;
}
- } else {
- if (l)
- bestSupernodeLatency = l;
- bestSupernode = *sn;
}
}
}