Simplify validation logic too.

This commit is contained in:
Adam Ierymenko 2021-09-20 14:56:49 -04:00
parent c2413fca4d
commit bcf8c30ce0

View file

@ -129,7 +129,7 @@ using json = nlohmann::json;
*/
struct RootPeer
{
ZT_ALWAYS_INLINE RootPeer() : v4s(-1),v6s(-1),lastSend(0),lastReceive(0),lastReceiveV4(0),lastReceiveV6(0),lastEcho(0),lastHello(0),vProto(-1),vMajor(-1),vMinor(-1),vRev(-1),identityValidated(false),identityInvalid(false) {}
ZT_ALWAYS_INLINE RootPeer() : v4s(-1),v6s(-1),lastSend(0),lastReceive(0),lastReceiveV4(0),lastReceiveV6(0),lastEcho(0),lastHello(0),vProto(-1),vMajor(-1),vMinor(-1),vRev(-1),identityValidated(false) {}
ZT_ALWAYS_INLINE ~RootPeer() { Utils::burn(key,sizeof(key)); }
Identity id; // Identity
@ -145,7 +145,6 @@ struct RootPeer
int vProto; // Protocol version or -1 if unknown
int vMajor,vMinor,vRev; // Peer version or -1,-1,-1 if unknown
bool identityValidated; // Identity has been fully verified
bool identityInvalid; // Identity validation failed, to be deleted
AtomicCounter __refCount;
};
@ -886,17 +885,39 @@ int main(int argc,char **argv)
s_run = true;
threads.push_back(std::thread([]() {
while (s_run) {
std::vector< SharedPtr<RootPeer> > toValidate;
while (s_run) {
{
std::lock_guard<std::mutex> l(s_peersToValidate_l);
toValidate.swap(s_peersToValidate);
}
for(auto p=toValidate.begin();p!=toValidate.end();++p) {
if (!(*p)->identityValidated)
(*p)->identityInvalid = !(*p)->id.locallyValidate();
if (likely(!(*p)->identityValidated)) {
if (likely((*p)->id.locallyValidate())) {
(*p)->identityValidated = true;
} else {
{
std::lock_guard<std::mutex> p_l(s_peersByVirtAddr_l);
auto pp = s_peersByVirtAddr.find((*p)->id.address());
if ((pp != s_peersByVirtAddr.end())&&(pp->second == *p)) {
s_peersByVirtAddr.erase(pp);
}
}
{
std::lock_guard<std::mutex> p_l(s_peers_l);
for(auto pp=s_peers.begin();pp!=s_peers.end();++pp) {
if (*p == *pp) {
s_peers.erase(pp);
break;
}
}
}
}
}
}
toValidate.clear();
usleep(1000);
}
}));
for(auto port=s_ports.begin();port!=s_ports.end();++port) {
@ -1038,7 +1059,6 @@ int main(int argc,char **argv)
bool first = true;
std::lock_guard<std::mutex> l(s_peers_l);
for(auto p=s_peers.begin();p!=s_peers.end();++p) {
if (likely(!(*p)->identityInvalid)) {
if (first)
first = false;
else o << ',';
@ -1075,7 +1095,6 @@ int main(int argc,char **argv)
",\"versionMinor\":" << (*p)->vMinor <<
",\"versionRev\":" << (*p)->vRev << "}";
}
}
} catch ( ... ) {}
o << ']';
res.set_content(o.str(),"application/json");
@ -1100,14 +1119,12 @@ int main(int argc,char **argv)
{
std::lock_guard<std::mutex> l(s_peers_l);
for(auto p=s_peers.begin();p!=s_peers.end();++p) {
if (likely(!(*p)->identityInvalid)) {
if ((*p)->v4s >= 0)
ips[(*p)->ip4].insert((*p)->id.address());
if ((*p)->v6s >= 0)
ips[(*p)->ip6].insert((*p)->id.address());
}
}
}
for(auto p=ips.begin();p!=ips.end();++p) {
if (p->first.isV4()) {
@ -1209,7 +1226,6 @@ int main(int argc,char **argv)
}
}
// Remove expired or otherwise invalid peers
try {
std::vector< SharedPtr<RootPeer> > toRemove;
toRemove.reserve(1024);
@ -1218,7 +1234,7 @@ int main(int argc,char **argv)
std::vector< SharedPtr<RootPeer> > newPeers;
newPeers.reserve(s_peers.size());
for(auto p=s_peers.begin();p!=s_peers.end();++p) {
if (((now - (*p)->lastReceive) > ZT_PEER_ACTIVITY_TIMEOUT)||((*p)->identityInvalid)) {
if ((now - (*p)->lastReceive) > ZT_PEER_ACTIVITY_TIMEOUT) {
toRemove.emplace_back();
p->swap(toRemove.back());
} else {