Browse Source

Allow local networking option.

cl-refactor
Gav Wood 11 years ago
parent
commit
70a2a7b1e1
  1. 1
      exp/CMakeLists.txt
  2. 37
      exp/main.cpp
  3. 17
      libethereum/Manifest.h
  4. 6
      libethnet/All.h
  5. 18
      libethnet/PeerHost.cpp
  6. 5
      libethnet/PeerHost.h
  7. 4
      libethnet/PeerSession.cpp
  8. 1
      test/peer.cpp

1
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)

37
exp/main.cpp

@ -30,6 +30,7 @@
#include <libethential/Common.h>
#include <libethential/CommonData.h>
#include <libethential/RLP.h>
#include <libethnet/All.h>
#if 0
#include <libevm/VM.h>
#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;
}

17
libethereum/Manifest.h

@ -21,6 +21,8 @@
#pragma once
#include <iostream>
#include <sstream>
#include <libethential/RLP.h>
#include <libethcore/CommonEth.h>
@ -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;

6
libethnet/All.h

@ -0,0 +1,6 @@
#pragma once
#include "Common.h"
#include "PeerHost.h"
#include "PeerSession.h"

18
libethnet/PeerHost.cpp

@ -52,9 +52,10 @@ static const set<bi::address> 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<Public, bi::tcp::endpoint> 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));
}

5
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;

4
libethnet/PeerSession.cpp

@ -157,7 +157,7 @@ bool PeerSession::interpret(RLP const& _r)
bi::address_v4 peerAddress(_r[i][0].toHash<FixedHash<4>>().asArray());
auto ep = bi::tcp::endpoint(peerAddress, _r[i][1].toInt<short>());
Public id = _r[i][2].toHash<Public>();
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()

1
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();
}

Loading…
Cancel
Save