clang-format root

This commit is contained in:
Adam Ierymenko 2024-09-26 13:41:59 -04:00
parent 0a6c22a9b8
commit 2316a45a45
2 changed files with 1304 additions and 1141 deletions

75
.clang-format Normal file
View file

@ -0,0 +1,75 @@
---
BasedOnStyle: LLVM
BreakBeforeBraces: Stroustrup
IndentWidth: 4
TabWidth: 4
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'false'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlines: Right
AlignOperands: 'true'
AlignTrailingComments: 'true'
AllowAllArgumentsOnNextLine: 'false'
AllowAllConstructorInitializersOnNextLine: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AlwaysBreakAfterReturnType: None
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '4'
Cpp11BracedListStyle: 'false'
FixNamespaceComments: 'true'
IncludeBlocks: Regroup
IndentCaseLabels: 'true'
IndentPPDirectives: None
IndentWrappedFunctionNames: 'false'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: None
PointerAlignment: Left
ReflowComments: 'true'
SortIncludes: 'true'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterLogicalNot: 'true'
SpaceAfterTemplateKeyword: 'true'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCpp11BracedList: 'true'
SpaceBeforeCtorInitializerColon: 'true'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '3'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'true'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
UseTab: 'Never'
---
Language: Cpp
Standard: Cpp03
ColumnLimit: '240'
---
Language: ObjC
ColumnLimit: '240'
---
Language: Java
ColumnLimit: '240'
---
Language: CSharp
ColumnLimit: '240'
...

View file

@ -48,63 +48,56 @@
* they appear with the first alive sibling being used. * they appear with the first alive sibling being used.
*/ */
#include "../ext/cpp-httplib/httplib.h"
#include "../ext/json/json.hpp"
#include "../node/Address.hpp"
#include "../node/CertificateOfMembership.hpp"
#include "../node/Constants.hpp" #include "../node/Constants.hpp"
#include "../node/Identity.hpp"
#include "../node/InetAddress.hpp"
#include "../node/Meter.hpp"
#include "../node/MulticastGroup.hpp"
#include "../node/Mutex.hpp"
#include "../node/Packet.hpp"
#include "../node/SharedPtr.hpp"
#include "../node/Utils.hpp"
#include "../osdep/BlockingQueue.hpp"
#include "../osdep/OSUtils.hpp"
#include "geoip-html.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <atomic>
#include <errno.h>
#include <fcntl.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <mutex>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <netinet/ip6.h> #include <netinet/ip6.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <netinet/udp.h> #include <netinet/udp.h>
#include "../ext/json/json.hpp"
#include "../ext/cpp-httplib/httplib.h"
#include "../node/Packet.hpp"
#include "../node/Utils.hpp"
#include "../node/Address.hpp"
#include "../node/Identity.hpp"
#include "../node/InetAddress.hpp"
#include "../node/Mutex.hpp"
#include "../node/SharedPtr.hpp"
#include "../node/MulticastGroup.hpp"
#include "../node/CertificateOfMembership.hpp"
#include "../node/Meter.hpp"
#include "../osdep/OSUtils.hpp"
#include "../osdep/BlockingQueue.hpp"
#include <string>
#include <thread>
#include <map>
#include <set> #include <set>
#include <vector> #include <signal.h>
#include <iostream> #include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <thread>
#include <unistd.h>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
#include <atomic>
#include <mutex>
#include <list>
#include <sstream>
#include <iomanip>
#include "geoip-html.h"
using namespace ZeroTier; using namespace ZeroTier;
using json = nlohmann::json; using json = nlohmann::json;
@ -127,10 +120,14 @@ using json = nlohmann::json;
* AtomicCounter all satisfy this. Take care when adding fields that * AtomicCounter all satisfy this. Take care when adding fields that
* this remains true. * this remains true.
*/ */
struct RootPeer 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)
{ {
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)); } ZT_ALWAYS_INLINE ~RootPeer()
{
Utils::burn(key, sizeof(key));
}
Identity id; // Identity Identity id; // Identity
uint8_t key[32]; // Shared secret key uint8_t key[32]; // Shared secret key
@ -150,33 +147,65 @@ struct RootPeer
}; };
// Hashers for std::unordered_map // Hashers for std::unordered_map
struct IdentityHasher { ZT_ALWAYS_INLINE std::size_t operator()(const Identity &id) const { return (std::size_t)id.hashCode(); } }; struct IdentityHasher {
struct AddressHasher { ZT_ALWAYS_INLINE std::size_t operator()(const Address &a) const { return (std::size_t)a.toInt(); } }; ZT_ALWAYS_INLINE std::size_t operator()(const Identity& id) const
struct InetAddressHasher { ZT_ALWAYS_INLINE std::size_t operator()(const InetAddress &ip) const { return (std::size_t)ip.hashCode(); } }; {
struct MulticastGroupHasher { ZT_ALWAYS_INLINE std::size_t operator()(const MulticastGroup &mg) const { return (std::size_t)mg.hashCode(); } }; return (std::size_t)id.hashCode();
}
};
struct AddressHasher {
ZT_ALWAYS_INLINE std::size_t operator()(const Address& a) const
{
return (std::size_t)a.toInt();
}
};
struct InetAddressHasher {
ZT_ALWAYS_INLINE std::size_t operator()(const InetAddress& ip) const
{
return (std::size_t)ip.hashCode();
}
};
struct MulticastGroupHasher {
ZT_ALWAYS_INLINE std::size_t operator()(const MulticastGroup& mg) const
{
return (std::size_t)mg.hashCode();
}
};
// An ordered tuple key representing an introduction of one peer to another // An ordered tuple key representing an introduction of one peer to another
struct RendezvousKey struct RendezvousKey {
{
RendezvousKey(const Address& aa, const Address& bb) RendezvousKey(const Address& aa, const Address& bb)
{ {
if (aa > bb) { if (aa > bb) {
a = aa; a = aa;
b = bb; b = bb;
} else { }
else {
a = bb; a = bb;
b = aa; b = aa;
} }
} }
Address a, b; Address a, b;
ZT_ALWAYS_INLINE bool operator==(const RendezvousKey &k) const { return ((a == k.a)&&(b == k.b)); } ZT_ALWAYS_INLINE bool operator==(const RendezvousKey& k) const
ZT_ALWAYS_INLINE bool operator!=(const RendezvousKey &k) const { return ((a != k.a)||(b != k.b)); } {
struct Hasher { ZT_ALWAYS_INLINE std::size_t operator()(const RendezvousKey &k) const { return (std::size_t)(k.a.toInt() ^ k.b.toInt()); } }; return ((a == k.a) && (b == k.b));
}
ZT_ALWAYS_INLINE bool operator!=(const RendezvousKey& k) const
{
return ((a != k.a) || (b != k.b));
}
struct Hasher {
ZT_ALWAYS_INLINE std::size_t operator()(const RendezvousKey& k) const
{
return (std::size_t)(k.a.toInt() ^ k.b.toInt());
}
};
}; };
struct RendezvousStats struct RendezvousStats {
RendezvousStats() : count(0), ts(0)
{ {
RendezvousStats() : count(0),ts(0) {} }
int64_t count; int64_t count;
int64_t ts; int64_t ts;
}; };
@ -274,7 +303,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
const uint64_t origId = pkt.packetId(); const uint64_t origId = pkt.packetId();
pkt.reset(source, s_self.address(), Packet::VERB_ERROR); pkt.reset(source, s_self.address(), Packet::VERB_ERROR);
pkt.append((uint8_t)Packet::VERB_HELLO); pkt.append((uint8_t)Packet::VERB_HELLO);
pkt.append(origId);; pkt.append(origId);
;
pkt.append((uint8_t)Packet::ERROR_IDENTITY_COLLISION); pkt.append((uint8_t)Packet::ERROR_IDENTITY_COLLISION);
pkt.armor(key, true); pkt.armor(key, true);
sendto(sock, pkt.data(), pkt.size(), SENDTO_FLAGS, (const struct sockaddr*)ip, (socklen_t)((ip->ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))); sendto(sock, pkt.data(), pkt.size(), SENDTO_FLAGS, (const struct sockaddr*)ip, (socklen_t)((ip->ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6)));
@ -282,7 +312,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
return; return;
} }
} else { }
else {
peer.set(new RootPeer); peer.set(new RootPeer);
peer->identityValidated = false; peer->identityValidated = false;
@ -335,7 +366,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
printf("%s packet rejected: decompression failed" ZT_EOL_S, ip->toString(ipstr)); printf("%s packet rejected: decompression failed" ZT_EOL_S, ip->toString(ipstr));
return; return;
} }
} else { }
else {
return; return;
} }
} }
@ -348,7 +380,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
peer->lastReceiveV4 = now; peer->lastReceiveV4 = now;
if ((now - peer->lastReceiveV6) > ZT_PEER_ACTIVITY_TIMEOUT) if ((now - peer->lastReceiveV6) > ZT_PEER_ACTIVITY_TIMEOUT)
peer->v6s = -1; peer->v6s = -1;
} else if (ip->isV6()) { }
else if (ip->isV6()) {
peer->ip6 = ip; peer->ip6 = ip;
peer->v6s = sock; peer->v6s = sock;
peer->lastReceiveV6 = now; peer->lastReceiveV6 = now;
@ -392,7 +425,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
s_outputRate.log(now, pkt.size()); s_outputRate.log(now, pkt.size());
peer->lastSend = now; peer->lastSend = now;
} }
} catch ( ... ) { }
catch (...) {
printf("* unexpected exception handling HELLO from %s" ZT_EOL_S, ip->toString(ipstr)); printf("* unexpected exception handling HELLO from %s" ZT_EOL_S, ip->toString(ipstr));
} }
break; break;
@ -413,7 +447,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
s_outputRate.log(now, outp.size()); s_outputRate.log(now, outp.size());
peer->lastSend = now; peer->lastSend = now;
} }
} catch ( ... ) { }
catch (...) {
printf("* unexpected exception handling ECHO from %s" ZT_EOL_S, ip->toString(ipstr)); printf("* unexpected exception handling ECHO from %s" ZT_EOL_S, ip->toString(ipstr));
} }
@ -444,7 +479,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
s_outputRate.log(now, pkt.size()); s_outputRate.log(now, pkt.size());
peer->lastSend = now; peer->lastSend = now;
} }
} catch ( ... ) { }
catch (...) {
printf("* unexpected exception handling ECHO from %s" ZT_EOL_S, ip->toString(ipstr)); printf("* unexpected exception handling ECHO from %s" ZT_EOL_S, ip->toString(ipstr));
} }
@ -456,7 +492,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
const MulticastGroup mg(MAC(pkt.field(ptr + 8, 6), 6), pkt.template at<uint32_t>(ptr + 14)); const MulticastGroup mg(MAC(pkt.field(ptr + 8, 6), 6), pkt.template at<uint32_t>(ptr + 14));
s_multicastSubscriptions[nwid][mg][source] = now; s_multicastSubscriptions[nwid][mg][source] = now;
} }
} catch ( ... ) { }
catch (...) {
printf("* unexpected exception handling MULTICAST_LIKE from %s" ZT_EOL_S, ip->toString(ipstr)); printf("* unexpected exception handling MULTICAST_LIKE from %s" ZT_EOL_S, ip->toString(ipstr));
} }
break; break;
@ -507,7 +544,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
} }
} }
} }
} catch ( ... ) { }
catch (...) {
printf("* unexpected exception handling MULTICAST_GATHER from %s" ZT_EOL_S, ip->toString(ipstr)); printf("* unexpected exception handling MULTICAST_GATHER from %s" ZT_EOL_S, ip->toString(ipstr));
} }
break; break;
@ -530,7 +568,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
s_discardedForwardRate.log(now, pkt.size()); s_discardedForwardRate.log(now, pkt.size());
return; return;
} }
} else { }
else {
if ((hops = (int)pkt.incrementHops()) > s_relayMaxHops) { if ((hops = (int)pkt.incrementHops()) > s_relayMaxHops) {
// printf("%s refused to forward to %s: max hop count exceeded" ZT_EOL_S,ip->toString(ipstr),dest.toString(astr)); // printf("%s refused to forward to %s: max hop count exceeded" ZT_EOL_S,ip->toString(ipstr),dest.toString(astr));
s_discardedForwardRate.log(now, pkt.size()); s_discardedForwardRate.log(now, pkt.size());
@ -633,7 +672,8 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
s_forwardRate.log(now, pkt.size()); s_forwardRate.log(now, pkt.size());
forwardTo->lastSend = now; forwardTo->lastSend = now;
} }
} else if (forwardTo->v4s >= 0) { }
else if (forwardTo->v4s >= 0) {
if (sendto(forwardTo->v4s, pkt.data(), pkt.size(), SENDTO_FLAGS, (const struct sockaddr*)&(forwardTo->ip4), (socklen_t)sizeof(struct sockaddr_in)) > 0) { if (sendto(forwardTo->v4s, pkt.data(), pkt.size(), SENDTO_FLAGS, (const struct sockaddr*)&(forwardTo->ip4), (socklen_t)sizeof(struct sockaddr_in)) > 0) {
s_outputRate.log(now, pkt.size()); s_outputRate.log(now, pkt.size());
s_forwardRate.log(now, pkt.size()); s_forwardRate.log(now, pkt.size());
@ -667,19 +707,24 @@ static int bindSocket(struct sockaddr *const bindAddr)
} }
if (bindAddr->sa_family == AF_INET6) { if (bindAddr->sa_family == AF_INET6) {
f = 1; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f)); f = 1;
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&f, sizeof(f));
#ifdef IPV6_MTU_DISCOVER #ifdef IPV6_MTU_DISCOVER
f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&f,sizeof(f)); f = 0;
setsockopt(s, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &f, sizeof(f));
#endif #endif
#ifdef IPV6_DONTFRAG #ifdef IPV6_DONTFRAG
f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_DONTFRAG,&f,sizeof(f)); f = 0;
setsockopt(s, IPPROTO_IPV6, IPV6_DONTFRAG, &f, sizeof(f));
#endif #endif
} }
#ifdef IP_DONTFRAG #ifdef IP_DONTFRAG
f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f)); f = 0;
setsockopt(s, IPPROTO_IP, IP_DONTFRAG, &f, sizeof(f));
#endif #endif
#ifdef IP_MTU_DISCOVER #ifdef IP_MTU_DISCOVER
f = IP_PMTUDISC_DONT; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f)); f = IP_PMTUDISC_DONT;
setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &f, sizeof(f));
#endif #endif
/* /*
@ -691,10 +736,12 @@ static int bindSocket(struct sockaddr *const bindAddr)
*/ */
#ifdef SO_REUSEPORT #ifdef SO_REUSEPORT
f = 1; setsockopt(s,SOL_SOCKET,SO_REUSEPORT,(void *)&f,sizeof(f)); f = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void*)&f, sizeof(f));
#endif #endif
#ifndef __LINUX__ // linux wants just SO_REUSEPORT #ifndef __LINUX__ // linux wants just SO_REUSEPORT
f = 1; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f)); f = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&f, sizeof(f));
#endif #endif
#ifdef __LINUX__ #ifdef __LINUX__
@ -763,10 +810,12 @@ int main(int argc,char **argv)
} }
try { try {
s_config = json::parse(configStr); s_config = json::parse(configStr);
} catch (std::exception &exc) { }
catch (std::exception& exc) {
printf("FATAL: config file at %s invalid: %s" ZT_EOL_S, argv[2], exc.what()); printf("FATAL: config file at %s invalid: %s" ZT_EOL_S, argv[2], exc.what());
return 1; return 1;
} catch ( ... ) { }
catch (...) {
printf("FATAL: config file at %s invalid: unknown exception" ZT_EOL_S, argv[2]); printf("FATAL: config file at %s invalid: unknown exception" ZT_EOL_S, argv[2]);
return 1; return 1;
} }
@ -787,7 +836,8 @@ int main(int argc,char **argv)
} }
s_ports.push_back(port); s_ports.push_back(port);
} }
} else { }
else {
int port = jport; int port = jport;
if ((port <= 0) || (port > 65535)) { if ((port <= 0) || (port > 65535)) {
printf("FATAL: invalid port in config file %d" ZT_EOL_S, port); printf("FATAL: invalid port in config file %d" ZT_EOL_S, port);
@ -795,7 +845,9 @@ int main(int argc,char **argv)
} }
s_ports.push_back(port); s_ports.push_back(port);
} }
} catch ( ... ) {} }
catch (...) {
}
if (s_ports.empty()) if (s_ports.empty())
s_ports.push_back(ZT_DEFAULT_PORT); s_ports.push_back(ZT_DEFAULT_PORT);
std::sort(s_ports.begin(), s_ports.end()); std::sort(s_ports.begin(), s_ports.end());
@ -807,14 +859,16 @@ int main(int argc,char **argv)
printf("FATAL: invalid HTTP port in config file %d" ZT_EOL_S, httpPort); printf("FATAL: invalid HTTP port in config file %d" ZT_EOL_S, httpPort);
return 1; return 1;
} }
} catch ( ... ) { }
catch (...) {
httpPort = ZT_DEFAULT_PORT; httpPort = ZT_DEFAULT_PORT;
} }
std::string planetFilePath; std::string planetFilePath;
try { try {
planetFilePath = s_config["planetFile"]; planetFilePath = s_config["planetFile"];
} catch ( ... ) { }
catch (...) {
planetFilePath = ""; planetFilePath = "";
} }
@ -824,7 +878,8 @@ int main(int argc,char **argv)
s_statsRoot = s_statsRoot.substr(0, s_statsRoot.length() - 1); s_statsRoot = s_statsRoot.substr(0, s_statsRoot.length() - 1);
if (s_statsRoot.length() > 0) if (s_statsRoot.length() > 0)
OSUtils::mkdir(s_statsRoot); OSUtils::mkdir(s_statsRoot);
} catch ( ... ) { }
catch (...) {
s_statsRoot = ""; s_statsRoot = "";
} }
@ -835,7 +890,8 @@ int main(int argc,char **argv)
s_relayMaxHops = ZT_PROTO_MAX_HOPS; s_relayMaxHops = ZT_PROTO_MAX_HOPS;
else if (s_relayMaxHops < 0) else if (s_relayMaxHops < 0)
s_relayMaxHops = 0; s_relayMaxHops = 0;
} catch ( ... ) { }
catch (...) {
s_relayMaxHops = ZT_RELAY_MAX_HOPS; s_relayMaxHops = ZT_RELAY_MAX_HOPS;
} }
@ -872,21 +928,27 @@ int main(int argc,char **argv)
if ((start) && (end) && (start.ss_family == end.ss_family) && (lat >= -90.0F) && (lat <= 90.0F) && (lon >= -180.0F) && (lon <= 180.0F)) { if ((start) && (end) && (start.ss_family == end.ss_family) && (lat >= -90.0F) && (lat <= 90.0F) && (lon >= -180.0F) && (lon <= 180.0F)) {
if (start.ss_family == AF_INET) { if (start.ss_family == AF_INET) {
s_geoIp4[std::pair<uint32_t, uint32_t>(ip4ToH32(start), ip4ToH32(end))] = std::pair<float, float>(lat, lon); s_geoIp4[std::pair<uint32_t, uint32_t>(ip4ToH32(start), ip4ToH32(end))] = std::pair<float, float>(lat, lon);
} else if (start.ss_family == AF_INET6) { }
else if (start.ss_family == AF_INET6) {
s_geoIp6[std::pair<std::array<uint64_t, 2>, std::array<uint64_t, 2> >(ip6ToH128(start), ip6ToH128(end))] = std::pair<float, float>(lat, lon); s_geoIp6[std::pair<std::array<uint64_t, 2>, std::array<uint64_t, 2> >(ip6ToH128(start), ip6ToH128(end))] = std::pair<float, float>(lat, lon);
} }
} }
} }
s_geoInit = true; s_geoInit = true;
} catch ( ... ) {} }
catch (...) {
}
fclose(gf); fclose(gf);
})); }));
} }
} }
} catch ( ... ) {} }
catch (...) {
}
unsigned int ncores = std::thread::hardware_concurrency(); unsigned int ncores = std::thread::hardware_concurrency();
if (ncores == 0) ncores = 1; if (ncores == 0)
ncores = 1;
s_run = true; s_run = true;
@ -901,7 +963,8 @@ int main(int argc,char **argv)
if (likely(! (*p)->identityValidated)) { if (likely(! (*p)->identityValidated)) {
if (likely((*p)->id.locallyValidate())) { if (likely((*p)->id.locallyValidate())) {
(*p)->identityValidated = true; (*p)->identityValidated = true;
} else { }
else {
{ {
std::lock_guard<std::mutex> p_l(s_peersByVirtAddr_l); std::lock_guard<std::mutex> p_l(s_peersByVirtAddr_l);
auto pp = s_peersByVirtAddr.find((*p)->id.address()); auto pp = s_peersByVirtAddr.find((*p)->id.address());
@ -950,8 +1013,10 @@ int main(int argc,char **argv)
sockets.push_back(s6); sockets.push_back(s6);
sockets.push_back(s4); sockets.push_back(s4);
if (v4Sock < 0) v4Sock = s4; if (v4Sock < 0)
if (v6Sock < 0) v6Sock = s6; v4Sock = s4;
if (v6Sock < 0)
v6Sock = s6;
threads.push_back(std::thread([s6, s4]() { threads.push_back(std::thread([s6, s4]() {
struct sockaddr_in6 in6; struct sockaddr_in6 in6;
@ -965,18 +1030,22 @@ int main(int argc,char **argv)
try { try {
pkt->setSize((unsigned int)pl); pkt->setSize((unsigned int)pl);
handlePacket(s6, reinterpret_cast<const InetAddress*>(&in6), *pkt); handlePacket(s6, reinterpret_cast<const InetAddress*>(&in6), *pkt);
} catch (std::exception &exc) { }
catch (std::exception& exc) {
char ipstr[128]; char ipstr[128];
printf("WARNING: unexpected exception handling packet from %s: %s" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in6)->toString(ipstr), exc.what()); printf("WARNING: unexpected exception handling packet from %s: %s" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in6)->toString(ipstr), exc.what());
} catch (int exc) { }
catch (int exc) {
char ipstr[128]; char ipstr[128];
printf("WARNING: unexpected exception handling packet from %s: ZT exception code %d" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in6)->toString(ipstr), exc); printf("WARNING: unexpected exception handling packet from %s: ZT exception code %d" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in6)->toString(ipstr), exc);
} catch ( ... ) { }
catch (...) {
char ipstr[128]; char ipstr[128];
printf("WARNING: unexpected exception handling packet from %s: unknown exception" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in6)->toString(ipstr)); printf("WARNING: unexpected exception handling packet from %s: unknown exception" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in6)->toString(ipstr));
} }
} }
} else if (!s_run) { }
else if (! s_run) {
break; break;
} }
} }
@ -995,18 +1064,22 @@ int main(int argc,char **argv)
try { try {
pkt->setSize((unsigned int)pl); pkt->setSize((unsigned int)pl);
handlePacket(s4, reinterpret_cast<const InetAddress*>(&in4), *pkt); handlePacket(s4, reinterpret_cast<const InetAddress*>(&in4), *pkt);
} catch (std::exception &exc) { }
catch (std::exception& exc) {
char ipstr[128]; char ipstr[128];
printf("WARNING: unexpected exception handling packet from %s: %s" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in4)->toString(ipstr), exc.what()); printf("WARNING: unexpected exception handling packet from %s: %s" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in4)->toString(ipstr), exc.what());
} catch (int exc) { }
catch (int exc) {
char ipstr[128]; char ipstr[128];
printf("WARNING: unexpected exception handling packet from %s: ZT exception code %d" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in4)->toString(ipstr), exc); printf("WARNING: unexpected exception handling packet from %s: ZT exception code %d" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in4)->toString(ipstr), exc);
} catch ( ... ) { }
catch (...) {
char ipstr[128]; char ipstr[128];
printf("WARNING: unexpected exception handling packet from %s: unknown exception" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in4)->toString(ipstr)); printf("WARNING: unexpected exception handling packet from %s: unknown exception" ZT_EOL_S, reinterpret_cast<const InetAddress*>(&in4)->toString(ipstr));
} }
} }
} else if (!s_run) { }
else if (! s_run) {
break; break;
} }
} }
@ -1067,41 +1140,45 @@ int main(int argc,char **argv)
for (auto p = s_peers.begin(); p != s_peers.end(); ++p) { for (auto p = s_peers.begin(); p != s_peers.end(); ++p) {
if (first) if (first)
first = false; first = false;
else o << ','; else
o << o << ',';
"{\"address\":\"" << (*p)->id.address().toString(tmp) << "\"" o << "{\"address\":\"" << (*p)->id.address().toString(tmp)
<< "\""
",\"latency\":-1" ",\"latency\":-1"
",\"paths\":["; ",\"paths\":[";
if ((*p)->v4s >= 0) { if ((*p)->v4s >= 0) {
o << o << "{\"active\":true"
"{\"active\":true" ",\"address\":\""
",\"address\":\"" << (*p)->ip4.toIpString(tmp) << "\\/" << (*p)->ip4.port() << "\"" << (*p)->ip4.toIpString(tmp) << "\\/" << (*p)->ip4.port()
<< "\""
",\"expired\":false" ",\"expired\":false"
",\"lastReceive\":" << (*p)->lastReceive << ",\"lastReceive\":"
",\"lastSend\":" << (*p)->lastSend << << (*p)->lastReceive << ",\"lastSend\":" << (*p)->lastSend
",\"preferred\":true" << ",\"preferred\":true"
",\"trustedPathId\":0}"; ",\"trustedPathId\":0}";
} }
if ((*p)->v6s >= 0) { if ((*p)->v6s >= 0) {
if ((*p)->v4s >= 0) if ((*p)->v4s >= 0)
o << ','; o << ',';
o << o << "{\"active\":true"
"{\"active\":true" ",\"address\":\""
",\"address\":\"" << (*p)->ip6.toIpString(tmp) << "\\/" << (*p)->ip6.port() << "\"" << (*p)->ip6.toIpString(tmp) << "\\/" << (*p)->ip6.port()
<< "\""
",\"expired\":false" ",\"expired\":false"
",\"lastReceive\":" << (*p)->lastReceive << ",\"lastReceive\":"
",\"lastSend\":" << (*p)->lastSend << << (*p)->lastReceive << ",\"lastSend\":" << (*p)->lastSend << ",\"preferred\":" << (((*p)->ip4) ? "false" : "true") << ",\"trustedPathId\":0}";
",\"preferred\":" << (((*p)->ip4) ? "false" : "true") <<
",\"trustedPathId\":0}";
} }
o << "]" o << "]"
",\"role\":\"LEAF\"" ",\"role\":\"LEAF\""
",\"version\":\"" << (*p)->vMajor << '.' << (*p)->vMinor << '.' << (*p)->vRev << "\"" ",\"version\":\""
",\"versionMajor\":" << (*p)->vMajor << << (*p)->vMajor << '.' << (*p)->vMinor << '.' << (*p)->vRev
",\"versionMinor\":" << (*p)->vMinor << << "\""
",\"versionRev\":" << (*p)->vRev << "}"; ",\"versionMajor\":"
<< (*p)->vMajor << ",\"versionMinor\":" << (*p)->vMinor << ",\"versionRev\":" << (*p)->vRev << "}";
}
}
catch (...) {
} }
} catch ( ... ) {}
o << ']'; o << ']';
res.set_content(o.str(), "application/json"); res.set_content(o.str(), "application/json");
}); });
@ -1119,7 +1196,8 @@ int main(int argc,char **argv)
bool firstCoord = true; bool firstCoord = true;
std::pair<uint32_t, uint32_t> k4(0, 0xffffffff); std::pair<uint32_t, uint32_t> k4(0, 0xffffffff);
std::pair<std::array<uint64_t, 2>, std::array<uint64_t, 2> > k6; std::pair<std::array<uint64_t, 2>, std::array<uint64_t, 2> > k6;
k6.second[0] = 0xffffffffffffffffULL; k6.second[1] = 0xffffffffffffffffULL; k6.second[0] = 0xffffffffffffffffULL;
k6.second[1] = 0xffffffffffffffffULL;
std::unordered_map<InetAddress, std::set<Address>, InetAddressHasher> ips; std::unordered_map<InetAddress, std::set<Address>, InetAddressHasher> ips;
{ {
@ -1145,7 +1223,8 @@ int main(int argc,char **argv)
bestRangeSize = range; bestRangeSize = range;
bestRangeLatLon = geo->second; bestRangeLatLon = geo->second;
} }
} else if ((geo->first.first < k4.first)&&(geo->first.second < k4.first)) { }
else if ((geo->first.first < k4.first) && (geo->first.second < k4.first)) {
break; break;
} }
++geo; ++geo;
@ -1164,7 +1243,8 @@ int main(int argc,char **argv)
} }
o << "\"}"; o << "\"}";
} }
} else if (p->first.isV6()) { }
else if (p->first.isV6()) {
k6.first = ip6ToH128(p->first); k6.first = ip6ToH128(p->first);
auto geo = std::map<std::pair<std::array<uint64_t, 2>, std::array<uint64_t, 2> >, std::pair<float, float> >::reverse_iterator(s_geoIp6.upper_bound(k6)); auto geo = std::map<std::pair<std::array<uint64_t, 2>, std::array<uint64_t, 2> >, std::pair<float, float> >::reverse_iterator(s_geoIp6.upper_bound(k6));
while (geo != s_geoIp6.rend()) { while (geo != s_geoIp6.rend()) {
@ -1182,14 +1262,16 @@ int main(int argc,char **argv)
} }
o << "\"}"; o << "\"}";
break; break;
} else if ((geo->first.first < k6.first)&&(geo->first.second < k6.first)) { }
else if ((geo->first.first < k6.first) && (geo->first.second < k6.first)) {
break; break;
} }
++geo; ++geo;
} }
} }
} }
} catch ( ... ) { }
catch (...) {
res.set_content("Internal error: unexpected exception resolving GeoIP locations", "text/plain"); res.set_content("Internal error: unexpected exception resolving GeoIP locations", "text/plain");
return; return;
} }
@ -1220,15 +1302,18 @@ int main(int argc,char **argv)
for (auto c = b->second.begin(); c != b->second.end();) { for (auto c = b->second.begin(); c != b->second.end();) {
if ((now - c->second) > ZT_MULTICAST_LIKE_EXPIRE) if ((now - c->second) > ZT_MULTICAST_LIKE_EXPIRE)
b->second.erase(c++); b->second.erase(c++);
else ++c; else
++c;
} }
if (b->second.empty()) if (b->second.empty())
a->second.erase(b++); a->second.erase(b++);
else ++b; else
++b;
} }
if (a->second.empty()) if (a->second.empty())
s_multicastSubscriptions.erase(a++); s_multicastSubscriptions.erase(a++);
else ++a; else
++a;
} }
} }
@ -1243,7 +1328,8 @@ int main(int argc,char **argv)
if ((now - (*p)->lastReceive) > ZT_PEER_ACTIVITY_TIMEOUT) { if ((now - (*p)->lastReceive) > ZT_PEER_ACTIVITY_TIMEOUT) {
toRemove.emplace_back(); toRemove.emplace_back();
p->swap(toRemove.back()); p->swap(toRemove.back());
} else { }
else {
newPeers.emplace_back(); newPeers.emplace_back();
p->swap(newPeers.back()); p->swap(newPeers.back());
} }
@ -1259,7 +1345,9 @@ int main(int argc,char **argv)
} }
} }
} }
} catch ( ... ) {} }
catch (...) {
}
// Remove old rendezvous entries // Remove old rendezvous entries
{ {
@ -1267,7 +1355,8 @@ int main(int argc,char **argv)
for (auto lr = s_rendezvousTracking.begin(); lr != s_rendezvousTracking.end();) { for (auto lr = s_rendezvousTracking.begin(); lr != s_rendezvousTracking.end();) {
if ((now - lr->second.ts) > ZT_PEER_ACTIVITY_TIMEOUT) if ((now - lr->second.ts) > ZT_PEER_ACTIVITY_TIMEOUT)
s_rendezvousTracking.erase(lr++); s_rendezvousTracking.erase(lr++);
else ++lr; else
++lr;
} }
} }
} }
@ -1284,7 +1373,8 @@ int main(int argc,char **argv)
s_planet = planetData; s_planet = planetData;
} }
} }
} catch ( ... ) { }
catch (...) {
std::lock_guard<std::mutex> pl(s_planet_l); std::lock_guard<std::mutex> pl(s_planet_l);
s_planet.clear(); s_planet.clear();
} }
@ -1309,23 +1399,20 @@ int main(int argc,char **argv)
for (auto p = sp.begin(); p != sp.end(); ++p) { for (auto p = sp.begin(); p != sp.end(); ++p) {
if ((*p)->v4s >= 0) { if ((*p)->v4s >= 0) {
(*p)->ip4.toString(ip4); (*p)->ip4.toString(ip4);
} else { }
else {
ip4[0] = '-'; ip4[0] = '-';
ip4[1] = 0; ip4[1] = 0;
} }
if ((*p)->v6s >= 0) { if ((*p)->v6s >= 0) {
(*p)->ip6.toString(ip6); (*p)->ip6.toString(ip6);
} else { }
else {
ip6[0] = '-'; ip6[0] = '-';
ip6[1] = 0; ip6[1] = 0;
} }
OSUtils::ztsnprintf(ver, sizeof(ver), "%d.%d.%d", (*p)->vMajor, (*p)->vMinor, (*p)->vRev); OSUtils::ztsnprintf(ver, sizeof(ver), "%d.%d.%d", (*p)->vMajor, (*p)->vMinor, (*p)->vRev);
fprintf(pf,"%.10llx %21s %45s %10.4f %6s" ZT_EOL_S, fprintf(pf, "%.10llx %21s %45s %10.4f %6s" ZT_EOL_S, (unsigned long long)(*p)->id.address().toInt(), ip4, ip6, fabs((double)(now - (*p)->lastReceive) / 1000.0), ver);
(unsigned long long)(*p)->id.address().toInt(),
ip4,
ip6,
fabs((double)(now - (*p)->lastReceive) / 1000.0),
ver);
} }
} }
@ -1353,7 +1440,8 @@ int main(int argc,char **argv)
fprintf(sf, "Recent P2P Graph Edges : %llu" ZT_EOL_S, (unsigned long long)s_rendezvousTracking.size()); fprintf(sf, "Recent P2P Graph Edges : %llu" ZT_EOL_S, (unsigned long long)s_rendezvousTracking.size());
if (s_rendezvousTracking.empty()) { if (s_rendezvousTracking.empty()) {
fprintf(sf, "Recent P2P Success Rate : 100.0000%%" ZT_EOL_S); fprintf(sf, "Recent P2P Success Rate : 100.0000%%" ZT_EOL_S);
} else { }
else {
fprintf(sf, "Recent P2P Success Rate : %.4f%%" ZT_EOL_S, (1.0 - ((double)unsuccessfulp2p / (double)s_rendezvousTracking.size())) * 100.0); fprintf(sf, "Recent P2P Success Rate : %.4f%%" ZT_EOL_S, (1.0 - ((double)unsuccessfulp2p / (double)s_rendezvousTracking.size())) * 100.0);
} }
s_rendezvousTracking_l.unlock(); s_rendezvousTracking_l.unlock();