From b3f4e7777be4155518e9d2c1bb7feb89c4ee371e Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Apr 2015 17:58:52 +0100 Subject: [PATCH] Minor semantic updates. Support unspecified address for NodeIPEndpoint. --- libp2p/Common.h | 8 ++++---- libp2p/Host.cpp | 4 ++-- libp2p/NodeTable.h | 6 ++++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libp2p/Common.h b/libp2p/Common.h index 54dfc8f9d..0b5724737 100644 --- a/libp2p/Common.h +++ b/libp2p/Common.h @@ -163,10 +163,10 @@ using PeerSessionInfos = std::vector; */ struct NodeIPEndpoint { - enum InlineRLP + enum RLPAppend { - CreateList, - InlineList + List, + Inline }; /// Setting true causes isAllowed to return true for all addresses. (Used by test fixtures) @@ -186,7 +186,7 @@ struct NodeIPEndpoint bool isAllowed() const { return NodeIPEndpoint::test_allowLocal ? !address.is_unspecified() : isPublicAddress(address); } - void streamRLP(RLPStream& _s, bool _inline = CreateList) const { if (_inline == CreateList) _s.appendList(3); if (address.is_v4()) _s << address.to_v4().to_bytes(); else _s << address.to_v6().to_bytes(); _s << udpPort << tcpPort; } + void streamRLP(RLPStream& _s, RLPAppend _inline = List) const { if (_inline == List) _s.appendList(3); if (address.is_v4()) _s << address.to_v4().to_bytes(); else if (address.is_v6()) _s << address.to_v6().to_bytes(); else _s << ""; _s << udpPort << tcpPort; } void interpretRLP(RLP const& _r) { if (_r[0].size() == 0) address = bi::address(); else if (_r[0].size() == 4) address = bi::address_v4(_r[0].toArray()); else address = bi::address_v6(_r[0].toArray()); udpPort = _r[1].toInt(); tcpPort = _r[2].toInt(); } }; diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 08289bea8..aea51ddd0 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -686,7 +686,7 @@ bytes Host::saveNetwork() const if (chrono::system_clock::now() - p.m_lastConnected < chrono::seconds(3600 * 48) && !!p.endpoint && p.id != id() && (p.required || p.endpoint.isAllowed())) { network.appendList(11); - p.endpoint.streamRLP(network, NodeIPEndpoint::InlineList); + p.endpoint.streamRLP(network, NodeIPEndpoint::Inline); network << p.id << p.required << chrono::duration_cast(p.m_lastConnected.time_since_epoch()).count() << chrono::duration_cast(p.m_lastAttempted.time_since_epoch()).count() @@ -702,7 +702,7 @@ bytes Host::saveNetwork() const for (auto const& entry: state) { network.appendList(4); - entry.endpoint.streamRLP(network, NodeIPEndpoint::InlineList); + entry.endpoint.streamRLP(network, NodeIPEndpoint::Inline); network << entry.id; count++; } diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 4bb9a2178..76cd32097 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -299,8 +299,10 @@ struct InvalidRLP: public Exception {}; */ struct PingNode: RLPXDatagram { - PingNode(bi::udp::endpoint _ep): RLPXDatagram(_ep), source(UnspecifiedNodeIPEndpoint), destination(UnspecifiedNodeIPEndpoint) {} + /// Constructor used for sending PingNode. PingNode(NodeIPEndpoint _src, NodeIPEndpoint _dest): RLPXDatagram(_dest), source(_src), destination(_dest), ts(futureFromEpoch(std::chrono::seconds(60))) {} + /// Constructor used to create empty PingNode for parsing inbound packets. + PingNode(bi::udp::endpoint _ep): RLPXDatagram(_ep), source(UnspecifiedNodeIPEndpoint), destination(UnspecifiedNodeIPEndpoint) {} static const uint8_t type = 1; @@ -368,7 +370,7 @@ struct Neighbours: RLPXDatagram Neighbour(RLP const& _r): endpoint(_r) { node = h512(_r[3].toBytes()); } NodeIPEndpoint endpoint; NodeId node; - void streamRLP(RLPStream& _s) const { _s.appendList(4); endpoint.streamRLP(_s, NodeIPEndpoint::InlineList); _s << node; } + void streamRLP(RLPStream& _s) const { _s.appendList(4); endpoint.streamRLP(_s, NodeIPEndpoint::Inline); _s << node; } }; Neighbours(bi::udp::endpoint _ep): RLPXDatagram(_ep), ts(futureFromEpoch(std::chrono::seconds(30))) {}