Browse Source

NodeTable constructor requires and listens to explicit IP address instead of defaulting to 0.0.0.0 (all addresses).

cl-refactor
subtly 10 years ago
parent
commit
ee7608fe60
  1. 5
      libp2p/Host.cpp
  2. 6
      libp2p/NodeTable.cpp
  3. 4
      libp2p/NodeTable.h
  4. 4
      libp2p/UDP.h
  5. 6
      test/net.cpp
  6. 6
      test/peer.cpp

5
libp2p/Host.cpp

@ -610,8 +610,7 @@ void Host::startedWorking()
else else
clog(NetNote) << "p2p.start.notice id:" << id().abridged() << "Listen port is invalid or unavailable. Node Table using default port (30303)."; clog(NetNote) << "p2p.start.notice id:" << id().abridged() << "Listen port is invalid or unavailable. Node Table using default port (30303).";
// TODO: add m_tcpPublic endpoint; sort out endpoint stuff for nodetable m_nodeTable.reset(new NodeTable(m_ioService, m_alias, bi::address::from_string(listenAddress()), listenPort() > 0 ? listenPort() : 30303));
m_nodeTable.reset(new NodeTable(m_ioService, m_alias, m_listenPort > 0 ? m_listenPort : 30303));
m_nodeTable->setEventHandler(new HostNodeTableHandler(*this)); m_nodeTable->setEventHandler(new HostNodeTableHandler(*this));
restoreNetwork(&m_restoreNetwork); restoreNetwork(&m_restoreNetwork);
@ -675,7 +674,7 @@ bytes Host::saveNetwork() const
// TODO: alpha: Figure out why it ever shares these ports.//p.address.port() >= 30300 && p.address.port() <= 30305 && // TODO: alpha: Figure out why it ever shares these ports.//p.address.port() >= 30300 && p.address.port() <= 30305 &&
// TODO: alpha: if/how to save private addresses // TODO: alpha: if/how to save private addresses
// Only save peers which have connected within 2 days, with properly-advertised port and public IP address // Only save peers which have connected within 2 days, with properly-advertised port and public IP address
if (chrono::system_clock::now() - p.m_lastConnected < chrono::seconds(3600 * 48) && p.peerEndpoint().port() > 0 && p.peerEndpoint().port() < /*49152*/32768 && p.id != id() && !isPrivateAddress(p.peerEndpoint().address())) if (chrono::system_clock::now() - p.m_lastConnected < chrono::seconds(3600 * 48) && p.peerEndpoint().port() > 0 && p.peerEndpoint().port() < /*49152*/32768 && p.id != id() && !isPrivateAddress(p.endpoint.udp.address()) && !isPrivateAddress(p.endpoint.tcp.address()))
{ {
network.appendList(10); network.appendList(10);
if (p.peerEndpoint().address().is_v4()) if (p.peerEndpoint().address().is_v4())

6
libp2p/NodeTable.cpp

@ -27,11 +27,11 @@ using namespace dev::p2p;
NodeEntry::NodeEntry(Node _src, Public _pubk, NodeIPEndpoint _gw): Node(_pubk, _gw), distance(NodeTable::distance(_src.id,_pubk)) {} NodeEntry::NodeEntry(Node _src, Public _pubk, NodeIPEndpoint _gw): Node(_pubk, _gw), distance(NodeTable::distance(_src.id,_pubk)) {}
NodeEntry::NodeEntry(Node _src, Public _pubk, bi::udp::endpoint _udp): Node(_pubk, NodeIPEndpoint(_udp)), distance(NodeTable::distance(_src.id,_pubk)) {} NodeEntry::NodeEntry(Node _src, Public _pubk, bi::udp::endpoint _udp): Node(_pubk, NodeIPEndpoint(_udp)), distance(NodeTable::distance(_src.id,_pubk)) {}
NodeTable::NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _udp): NodeTable::NodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _udpAddress, uint16_t _udp):
m_node(Node(_alias.pub(), bi::udp::endpoint())), m_node(Node(_alias.pub(), bi::udp::endpoint(_udpAddress, _udp))),
m_secret(_alias.sec()), m_secret(_alias.sec()),
m_io(_io), m_io(_io),
m_socket(new NodeSocket(m_io, *this, _udp)), m_socket(new NodeSocket(m_io, *this, m_node.endpoint.udp)),
m_socketPointer(m_socket.get()), m_socketPointer(m_socket.get()),
m_bucketRefreshTimer(m_io), m_bucketRefreshTimer(m_io),
m_evictionCheckTimer(m_io) m_evictionCheckTimer(m_io)

4
libp2p/NodeTable.h

@ -138,8 +138,8 @@ public:
/// Version of Discovery protocol /// Version of Discovery protocol
static unsigned const c_version = 2; static unsigned const c_version = 2;
/// Constructor requiring host for I/O, credentials, and IP Address,port to listen on. /// Constructor requiring host for I/O, credentials, and IP Address and port to listen on.
NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _udpPort = 30303); NodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _udpAddress, uint16_t _udpPort = 30303);
~NodeTable(); ~NodeTable();
/// Returns distance based on xor metric two node ids. Used by NodeEntry and NodeTable. /// Returns distance based on xor metric two node ids. Used by NodeEntry and NodeTable.

4
libp2p/UDP.h

@ -113,6 +113,10 @@ public:
enum { maxDatagramSize = MaxDatagramSize }; enum { maxDatagramSize = MaxDatagramSize };
static_assert(maxDatagramSize < 65507, "UDP datagrams cannot be larger than 65507 bytes"); static_assert(maxDatagramSize < 65507, "UDP datagrams cannot be larger than 65507 bytes");
/// Create socket for specific endpoint.
UDPSocket(ba::io_service& _io, UDPSocketEvents& _host, bi::udp::endpoint _endpoint): m_host(_host), m_endpoint(_endpoint), m_socket(_io) { m_started.store(false); m_closed.store(true); };
/// Create socket which listens to all ports.
UDPSocket(ba::io_service& _io, UDPSocketEvents& _host, unsigned _port): m_host(_host), m_endpoint(bi::udp::v4(), _port), m_socket(_io) { m_started.store(false); m_closed.store(true); }; UDPSocket(ba::io_service& _io, UDPSocketEvents& _host, unsigned _port): m_host(_host), m_endpoint(bi::udp::v4(), _port), m_socket(_io) { m_started.store(false); m_closed.store(true); };
virtual ~UDPSocket() { disconnect(); } virtual ~UDPSocket() { disconnect(); }

6
test/net.cpp

@ -53,7 +53,7 @@ protected:
struct TestNodeTable: public NodeTable struct TestNodeTable: public NodeTable
{ {
/// Constructor /// Constructor
TestNodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _port = 30300): NodeTable(_io, _alias, _port) {} TestNodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _addr, uint16_t _port = 30300): NodeTable(_io, _alias, _addr, _port) {}
static std::vector<std::pair<KeyPair,unsigned>> createTestNodes(unsigned _count) static std::vector<std::pair<KeyPair,unsigned>> createTestNodes(unsigned _count)
{ {
@ -106,10 +106,10 @@ struct TestNodeTable: public NodeTable
*/ */
struct TestNodeTableHost: public TestHost struct TestNodeTableHost: public TestHost
{ {
TestNodeTableHost(unsigned _count = 8): m_alias(KeyPair::create()), nodeTable(new TestNodeTable(m_io, m_alias)), testNodes(TestNodeTable::createTestNodes(_count)) {}; TestNodeTableHost(unsigned _count = 8): m_alias(KeyPair::create()), nodeTable(new TestNodeTable(m_io, m_alias, bi::address::from_string("127.0.0.1"))), testNodes(TestNodeTable::createTestNodes(_count)) {};
~TestNodeTableHost() { m_io.stop(); stopWorking(); } ~TestNodeTableHost() { m_io.stop(); stopWorking(); }
void setup() { for (auto n: testNodes) nodeTables.push_back(make_shared<TestNodeTable>(m_io,n.first,n.second)); } void setup() { for (auto n: testNodes) nodeTables.push_back(make_shared<TestNodeTable>(m_io,n.first, bi::address::from_string("127.0.0.1"),n.second)); }
void pingAll() { for (auto& t: nodeTables) t->pingTestNodes(testNodes); } void pingAll() { for (auto& t: nodeTables) t->pingTestNodes(testNodes); }

6
test/peer.cpp

@ -35,8 +35,8 @@ BOOST_AUTO_TEST_CASE(host)
auto oldLogVerbosity = g_logVerbosity; auto oldLogVerbosity = g_logVerbosity;
g_logVerbosity = 10; g_logVerbosity = 10;
NetworkPreferences host1prefs(30301, "127.0.0.1", true, true); NetworkPreferences host1prefs(30301, "127.0.0.1", false, true);
NetworkPreferences host2prefs(30302, "127.0.0.1", true, true); NetworkPreferences host2prefs(30302, "127.0.0.1", false, true);
Host host1("Test", host1prefs); Host host1("Test", host1prefs);
host1.start(); host1.start();
@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(save_nodes)
std::list<Host*> hosts; std::list<Host*> hosts;
for (auto i:{0,1,2,3,4,5}) for (auto i:{0,1,2,3,4,5})
{ {
Host* h = new Host("Test", NetworkPreferences(30300 + i, "127.0.0.1", true, true)); Host* h = new Host("Test", NetworkPreferences(30300 + i, "127.0.0.1", false, true));
h->setIdealPeerCount(10); h->setIdealPeerCount(10);
// starting host is required so listenport is available // starting host is required so listenport is available
h->start(); h->start();

Loading…
Cancel
Save