From ccc73b920ecab667aa6ddb7d47e17c12f4b0ab8c Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 8 Apr 2015 18:45:21 -0700 Subject: Node peer list function for CAPI, and some Peer cleanup. --- node/Node.cpp | 36 ++++++++++++++++++++++++++++++++++++ node/Peer.cpp | 8 ++++---- node/Peer.hpp | 13 +++---------- node/Topology.hpp | 9 +++++++++ 4 files changed, 52 insertions(+), 14 deletions(-) (limited to 'node') diff --git a/node/Node.cpp b/node/Node.cpp index e0eb0c6c..4b6f76ee 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 3270a231..8639da8f 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 242ef93b..63c81016 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 * -- cgit v1.2.3