diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index de4c19c2d..09c4db633 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -610,8 +610,7 @@ void Host::startedWorking() else 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, m_listenPort > 0 ? m_listenPort : 30303)); + m_nodeTable.reset(new NodeTable(m_ioService, m_alias, bi::address::from_string(listenAddress()), listenPort() > 0 ? listenPort() : 30303)); m_nodeTable->setEventHandler(new HostNodeTableHandler(*this)); 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: if/how to save private addresses // 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); if (p.peerEndpoint().address().is_v4()) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index a79eae12a..4710c5471 100644 --- a/libp2p/NodeTable.cpp +++ b/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, 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): - m_node(Node(_alias.pub(), bi::udp::endpoint())), +NodeTable::NodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _udpAddress, uint16_t _udp): + m_node(Node(_alias.pub(), bi::udp::endpoint(_udpAddress, _udp))), m_secret(_alias.sec()), 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_bucketRefreshTimer(m_io), m_evictionCheckTimer(m_io) diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 4c2e52bd6..49be27045 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -138,8 +138,8 @@ public: /// Version of Discovery protocol static unsigned const c_version = 2; - /// Constructor requiring host for I/O, credentials, and IP Address,port to listen on. - NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _udpPort = 30303); + /// Constructor requiring host for I/O, credentials, and IP Address and port to listen on. + NodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _udpAddress, uint16_t _udpPort = 30303); ~NodeTable(); /// Returns distance based on xor metric two node ids. Used by NodeEntry and NodeTable. diff --git a/libp2p/UDP.h b/libp2p/UDP.h index 3c3399acb..091f3cc2a 100644 --- a/libp2p/UDP.h +++ b/libp2p/UDP.h @@ -113,6 +113,10 @@ public: enum { maxDatagramSize = MaxDatagramSize }; 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); }; virtual ~UDPSocket() { disconnect(); } diff --git a/test/net.cpp b/test/net.cpp index 3a69a627b..e5cca9b66 100644 --- a/test/net.cpp +++ b/test/net.cpp @@ -53,7 +53,7 @@ protected: struct TestNodeTable: public NodeTable { /// 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> createTestNodes(unsigned _count) { @@ -106,10 +106,10 @@ struct TestNodeTable: public NodeTable */ 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(); } - void setup() { for (auto n: testNodes) nodeTables.push_back(make_shared(m_io,n.first,n.second)); } + void setup() { for (auto n: testNodes) nodeTables.push_back(make_shared(m_io,n.first, bi::address::from_string("127.0.0.1"),n.second)); } void pingAll() { for (auto& t: nodeTables) t->pingTestNodes(testNodes); } diff --git a/test/peer.cpp b/test/peer.cpp index 9a68401e0..3ad2e2946 100644 --- a/test/peer.cpp +++ b/test/peer.cpp @@ -35,8 +35,8 @@ BOOST_AUTO_TEST_CASE(host) auto oldLogVerbosity = g_logVerbosity; g_logVerbosity = 10; - NetworkPreferences host1prefs(30301, "127.0.0.1", true, true); - NetworkPreferences host2prefs(30302, "127.0.0.1", true, true); + NetworkPreferences host1prefs(30301, "127.0.0.1", false, true); + NetworkPreferences host2prefs(30302, "127.0.0.1", false, true); Host host1("Test", host1prefs); host1.start(); @@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(save_nodes) std::list hosts; 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); // starting host is required so listenport is available h->start();