diff --git a/include/ZeroTierOne.h b/include/ZeroTierOne.h index 10b6d37f9..4f743f782 100644 --- a/include/ZeroTierOne.h +++ b/include/ZeroTierOne.h @@ -98,6 +98,11 @@ extern "C" { */ #define ZT1_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS 4096 +/** + * Maximum number of direct network paths to a given peer + */ +#define ZT1_MAX_PEER_NETWORK_PATHS 4 + /** * Feature flag: ZeroTier One was built to be thread-safe -- concurrent processXXX() calls are okay */ @@ -501,34 +506,14 @@ typedef struct struct sockaddr_storage address; /** - * Time since last send in milliseconds or -1 for never + * Time of last send in milliseconds or 0 for never */ - long lastSend; + uint64_t lastSend; /** - * Time since last receive in milliseconds or -1 for never + * Time of last receive in milliseconds or 0 for never */ - long lastReceive; - - /** - * Time since last ping sent in milliseconds or -1 for never - */ - long lastPing; - - /** - * Time since last firewall opener sent in milliseconds or -1 for never - */ - long lastFirewallOpener; - - /** - * Total bytes sent - */ - uint64_t bytesSent; - - /** - * Total bytes received - */ - uint64_t bytesReceived; + uint64_t lastReceive; /** * Is path fixed? (i.e. not learned, static) @@ -540,7 +525,7 @@ typedef struct * What trust hierarchy role does this peer have? */ enum ZT1_PeerRole { - ZT1_PEER_ROLE_NODE = 0, // ordinary node + ZT1_PEER_ROLE_LEAF = 0, // ordinary node ZT1_PEER_ROLE_HUB = 1, // locally federated hub ZT1_PEER_ROLE_SUPERNODE = 2 // planetary supernode }; @@ -551,7 +536,7 @@ enum ZT1_PeerRole { typedef struct { /** - * ZeroTier binary address (40 bits) + * ZeroTier address (40 bits) */ uint64_t address; @@ -580,15 +565,15 @@ typedef struct */ enum ZT1_PeerRole role; - /** - * Array of network paths to peer - */ - ZT1_PeerPhysicalPath *paths; - /** * Number of paths (size of paths[]) */ - unsigned long pathCount; + unsigned int pathCount; + + /** + * Known network paths to peer + */ + ZT1_PeerPhysicalPath paths[ZT1_MAX_PEER_NETWORK_PATHS]; } ZT1_Peer; /** diff --git a/node/Node.cpp b/node/Node.cpp index e0eb0c6ce..4b6f76ee7 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -324,6 +324,42 @@ void Node::status(ZT1_NodeStatus *status) ZT1_PeerList *Node::peers() { + std::map< Address,SharedPtr > peers(RR->topology->allPeers()); + + char *buf = (char *)::malloc(sizeof(ZT1_PeerList) + (sizeof(ZT1_Peer) * peers.size())); + if (!buf) + return (ZT1_PeerList *)0; + ZT1_PeerList *pl = (ZT1_PeerList *)buf; + pl->peers = (ZT1_Peer *)(buf + sizeof(ZT1_PeerList)); + + pl->peerCount = 0; + for(std::map< Address,SharedPtr >::iterator pi(peers.begin());pi!=peers.end();++pi) { + ZT1_Peer *p = &(pl->peers[pl->peerCount++]); + p->address = pi->second->address().toInt(); + if (pi->second->remoteVersionKnown()) { + p->versionMajor = pi->second->remoteVersionMajor(); + p->versionMinor = pi->second->remoteVersionMinor(); + p->versionRev = pi->second->remoteVersionRevision(); + } else { + p->versionMajor = -1; + p->versionMinor = -1; + p->versionRev = -1; + } + p->latency = pi->second->latency(); + p->role = RR->topology->isSupernode(pi->second->address()) ? ZT1_PEER_ROLE_SUPERNODE : ZT1_PEER_ROLE_LEAF; + + std::vector paths(pi->second->paths()); + p->pathCount = 0; + for(std::vector::iterator path(paths.begin());path!=paths.end();++path) { + memcpy(&(p->paths[p->pathCount].address),&(path->address()),sizeof(struct sockaddr_storage)); + p->paths[p->pathCount].lastSend = path->lastSend(); + p->paths[p->pathCount].lastReceive = path->lastReceived(); + p->paths[p->pathCount].fixed = path->fixed() ? 1 : 0; + ++p->pathCount; + } + } + + return pl; } ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) diff --git a/node/Peer.cpp b/node/Peer.cpp index 3270a2311..8639da8f2 100644 --- a/node/Peer.cpp +++ b/node/Peer.cpp @@ -87,13 +87,13 @@ void Peer::received( if ((verb == Packet::VERB_OK)&&(inReVerb == Packet::VERB_HELLO)) { // Learn paths if they've been confirmed via a HELLO Path *slot = (Path *)0; - if (np < ZT_PEER_MAX_PATHS) { + if (np < ZT1_MAX_PEER_NETWORK_PATHS) { // Add new path slot = &(_paths[np++]); } else { // Replace oldest non-fixed path uint64_t slotLRmin = 0xffffffffffffffffULL; - for(unsigned int p=0;p paths() const + inline std::vector paths() const { std::vector pp; for(unsigned int p=0,np=_numPaths;p 0)||(_vMinor > 0)||(_vRevision > 0)); } /** * Check whether this peer's version is both known and is at least what is specified @@ -378,8 +373,6 @@ public: return false; } - inline bool remoteVersionKnown() const throw() { return ((_vMajor > 0)||(_vMinor > 0)||(_vRevision > 0)); } - /** * Get most recently active UDP path addresses for IPv4 and/or IPv6 * @@ -451,7 +444,7 @@ private: uint16_t _vMinor; uint16_t _vRevision; Identity _id; - Path _paths[ZT_PEER_MAX_PATHS]; + Path _paths[ZT1_MAX_PEER_NETWORK_PATHS]; unsigned int _numPaths; unsigned int _latency; diff --git a/node/Topology.hpp b/node/Topology.hpp index 242ef93b6..63c81016f 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -187,6 +187,15 @@ public: f(*this,p->second); } + /** + * @return All currently active peers by address + */ + inline std::map< Address,SharedPtr > allPeers() const + { + Mutex::Lock _l(_lock); + return _activePeers; + } + /** * Validate a root topology dictionary against the identities specified in Defaults *