diff --git a/exp/CMakeLists.txt b/exp/CMakeLists.txt index 5b76b4c7a..036169fee 100644 --- a/exp/CMakeLists.txt +++ b/exp/CMakeLists.txt @@ -9,6 +9,7 @@ set(EXECUTABLE exp) add_executable(${EXECUTABLE} ${SRC_LIST}) target_link_libraries(${EXECUTABLE} ethereum) +target_link_libraries(${EXECUTABLE} ethnet) target_link_libraries(${EXECUTABLE} gmp) target_link_libraries(${EXECUTABLE} ${CRYPTOPP_LS}) if(MINIUPNPC_LS) diff --git a/exp/main.cpp b/exp/main.cpp index d79fbb973..910b62ae4 100644 --- a/exp/main.cpp +++ b/exp/main.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #if 0 #include #include "BuildInfo.h" @@ -287,7 +288,7 @@ void parseTree(string const& _s, sp::utree& o_out) throw std::exception(); } #endif -int main(int, char**) +int main(int argc, char** argv) { #if 0 sp::utree out; @@ -297,7 +298,37 @@ int main(int, char**) cout << endl; #endif - cnote << RLP(fromHex("f837c0c0b4600160003556601359506301000000600035040f6018590060005660805460016080530160005760003560805760203560003557")); - cnote << toHex(RLPStream(1).append(bytes(54, 0)).out()); + g_logVerbosity = 20; + + short listenPort = 30303; + string remoteHost; + short remotePort = 30303; + + for (int i = 1; i < argc; ++i) + { + string arg = argv[i]; + if (arg == "-l" && i + 1 < argc) + listenPort = (short)atoi(argv[++i]); + else if (arg == "-r" && i + 1 < argc) + remoteHost = argv[++i]; + else if (arg == "-p" && i + 1 < argc) + remotePort = (short)atoi(argv[++i]); + else + remoteHost = argv[i]; + } + + PeerHost ph("Test", listenPort, "", false, true); + + if (!remoteHost.empty()) + ph.connect(remoteHost, remotePort); + + for (int i = 0; ; ++i) + { + this_thread::sleep_for(chrono::milliseconds(100)); + if (!(i % 100)) + ph.pingAll(); + ph.process(); + } + return 0; } diff --git a/libethereum/Manifest.h b/libethereum/Manifest.h index 94ecd1496..0a873afc6 100644 --- a/libethereum/Manifest.h +++ b/libethereum/Manifest.h @@ -21,6 +21,8 @@ #pragma once +#include +#include #include #include @@ -41,6 +43,21 @@ struct Manifest h256 bloom() const { h256 ret = from.bloom() | to.bloom(); for (auto const& i: internal) ret |= i.bloom(); for (auto const& i: altered) ret |= h256(i).bloom(); return ret; } + std::string toString(unsigned _indent = 0) const + { + std::ostringstream oss; + oss << std::string(_indent * 3, ' ') << from << " -> " << to << " [" << value << "]: {"; + if (internal.size()) + { + oss << std::endl; + for (auto const& m: internal) + oss << m.toString(_indent + 1) << std::endl; + oss << std::string(_indent * 3, ' '); + } + oss << "} I:" << toHex(input) << "; O:" << toHex(output); + return oss.str(); + } + Address from; Address to; u256 value; diff --git a/libethnet/All.h b/libethnet/All.h new file mode 100644 index 000000000..55a5423b7 --- /dev/null +++ b/libethnet/All.h @@ -0,0 +1,6 @@ +#pragma once + +#include "Common.h" +#include "PeerHost.h" +#include "PeerSession.h" + diff --git a/libethnet/PeerHost.cpp b/libethnet/PeerHost.cpp index 1206eb0bf..4ff34b5d4 100644 --- a/libethnet/PeerHost.cpp +++ b/libethnet/PeerHost.cpp @@ -52,9 +52,10 @@ static const set c_rejectAddresses = { {bi::address_v6::from_string("::")} }; -PeerHost::PeerHost(std::string const& _clientVersion, unsigned short _port, string const& _publicAddress, bool _upnp): +PeerHost::PeerHost(std::string const& _clientVersion, unsigned short _port, string const& _publicAddress, bool _upnp, bool _localNetworking): m_clientVersion(_clientVersion), m_listenPort(_port), + m_localNetworking(_localNetworking), m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), _port)), m_socket(m_ioService), m_key(KeyPair::create()) @@ -63,12 +64,13 @@ PeerHost::PeerHost(std::string const& _clientVersion, unsigned short _port, stri determinePublic(_publicAddress, _upnp); ensureAccepting(); m_lastPeersRequest = chrono::steady_clock::time_point::min(); - clog(NetNote) << "Id:" << toHex(m_key.address().ref().cropped(0, 4)); + clog(NetNote) << "Id:" << m_key.address().abridged(); } -PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddress, bool _upnp): +PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddress, bool _upnp, bool _localNetworking): m_clientVersion(_clientVersion), m_listenPort(0), + m_localNetworking(_localNetworking), m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), 0)), m_socket(m_ioService), m_key(KeyPair::create()) @@ -80,7 +82,7 @@ PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddre determinePublic(_publicAddress, _upnp); ensureAccepting(); m_lastPeersRequest = chrono::steady_clock::time_point::min(); - clog(NetNote) << "Id:" << toHex(m_key.address().ref().cropped(0, 4)); + clog(NetNote) << "Id:" << m_key.address().abridged(); } PeerHost::PeerHost(std::string const& _clientVersion): @@ -93,7 +95,7 @@ PeerHost::PeerHost(std::string const& _clientVersion): // populate addresses. populateAddresses(); m_lastPeersRequest = chrono::steady_clock::time_point::min(); - clog(NetNote) << "Id:" << toHex(m_key.address().ref().cropped(0, 4)); + clog(NetNote) << "Id:" << m_key.address().abridged(); } PeerHost::~PeerHost() @@ -164,14 +166,14 @@ void PeerHost::determinePublic(string const& _publicAddress, bool _upnp) bi::tcp::resolver r(m_ioService); if (m_upnp && m_upnp->isValid() && m_peerAddresses.size()) { - clog(NetNote) << "External addr: " << m_upnp->externalIP(); + clog(NetNote) << "External addr:" << m_upnp->externalIP(); int p = m_upnp->addRedirect(m_peerAddresses[0].to_string().c_str(), m_listenPort); if (p) clog(NetNote) << "Punched through NAT and mapped local port" << m_listenPort << "onto external port" << p << "."; else { // couldn't map - clog(NetWarn) << "Couldn't punch through NAT (or no NAT in place). Assuming " << m_listenPort << " is local & external port."; + clog(NetWarn) << "Couldn't punch through NAT (or no NAT in place). Assuming" << m_listenPort << "is local & external port."; p = m_listenPort; } @@ -280,7 +282,7 @@ std::map PeerHost::potentialPeers() { auto ep = j->endpoint(); // Skip peers with a listen port of zero or are on a private network - bool peerOnNet = (j->m_listenPort != 0 && !isPrivateAddress(ep.address())); + bool peerOnNet = (j->m_listenPort != 0 && (!isPrivateAddress(ep.address()) || m_localNetworking)); if (peerOnNet && ep.port() && j->m_id) ret.insert(make_pair(i.first, ep)); } diff --git a/libethnet/PeerHost.h b/libethnet/PeerHost.h index a67e03f48..15e629ebe 100644 --- a/libethnet/PeerHost.h +++ b/libethnet/PeerHost.h @@ -52,9 +52,9 @@ class PeerHost public: /// Start server, listening for connections on the given port. - PeerHost(std::string const& _clientVersion, unsigned short _port, std::string const& _publicAddress = std::string(), bool _upnp = true); + PeerHost(std::string const& _clientVersion, unsigned short _port, std::string const& _publicAddress = std::string(), bool _upnp = true, bool _localNetworking = false); /// Start server, listening for connections on a system-assigned port. - PeerHost(std::string const& _clientVersion, std::string const& _publicAddress = std::string(), bool _upnp = true); + PeerHost(std::string const& _clientVersion, std::string const& _publicAddress = std::string(), bool _upnp = true, bool _localNetworking = false); /// Start server, but don't listen. PeerHost(std::string const& _clientVersion); @@ -126,6 +126,7 @@ protected: std::string m_clientVersion; unsigned short m_listenPort; + bool m_localNetworking = false; ba::io_service m_ioService; bi::tcp::acceptor m_acceptor; diff --git a/libethnet/PeerSession.cpp b/libethnet/PeerSession.cpp index d69100047..7ddc61eec 100644 --- a/libethnet/PeerSession.cpp +++ b/libethnet/PeerSession.cpp @@ -157,7 +157,7 @@ bool PeerSession::interpret(RLP const& _r) bi::address_v4 peerAddress(_r[i][0].toHash>().asArray()); auto ep = bi::tcp::endpoint(peerAddress, _r[i][1].toInt()); Public id = _r[i][2].toHash(); - if (isPrivateAddress(peerAddress)) + if (isPrivateAddress(peerAddress) && !m_server->m_localNetworking) goto CONTINUE; clogS(NetAllDetail) << "Checking: " << ep << "(" << id.abridged() << ")"; @@ -331,7 +331,7 @@ void PeerSession::start() { RLPStream s; prep(s); - s.appendList(9) << HelloPacket + s.appendList(6) << HelloPacket << m_server->protocolVersion() << m_server->m_clientVersion << m_server->caps() diff --git a/test/peer.cpp b/test/peer.cpp index 4825eea30..16f8383e8 100644 --- a/test/peer.cpp +++ b/test/peer.cpp @@ -54,7 +54,6 @@ int peerTest(int argc, char** argv) for (int i = 0; ; ++i) { this_thread::sleep_for(chrono::milliseconds(100)); -// pn.sync(); if (!(i % 10)) ph.pingAll(); }