Browse Source

Better p2p handling - no connecting to LAN addresses.

cl-refactor
Gav Wood 11 years ago
parent
commit
3051d6eb12
  1. 5
      eth/main.cpp
  2. 4
      libethereum/Client.cpp
  3. 2
      libethereum/Client.h
  4. 21
      libethereum/PeerNetwork.cpp
  5. 4
      libethereum/PeerNetwork.h

5
eth/main.cpp

@ -59,6 +59,7 @@ int main(int argc, char** argv)
unsigned verbosity = 1; unsigned verbosity = 1;
NodeMode mode = NodeMode::Full; NodeMode mode = NodeMode::Full;
unsigned peers = 5; unsigned peers = 5;
string publicIP;
// Our address. // Our address.
KeyPair us = KeyPair::create(); KeyPair us = KeyPair::create();
@ -84,6 +85,8 @@ int main(int argc, char** argv)
string arg = argv[i]; string arg = argv[i];
if ((arg == "-l" || arg == "--listen" || arg == "--listen-port") && i + 1 < argc) if ((arg == "-l" || arg == "--listen" || arg == "--listen-port") && i + 1 < argc)
listenPort = atoi(argv[++i]); listenPort = atoi(argv[++i]);
else if ((arg == "-u" || arg == "--public-ip" || arg == "--public") && i + 1 < argc)
publicIP = argv[++i];
else if ((arg == "-r" || arg == "--remote") && i + 1 < argc) else if ((arg == "-r" || arg == "--remote") && i + 1 < argc)
remoteHost = argv[++i]; remoteHost = argv[++i];
else if ((arg == "-p" || arg == "--port") && i + 1 < argc) else if ((arg == "-p" || arg == "--port") && i + 1 < argc)
@ -185,7 +188,7 @@ int main(int argc, char** argv)
} }
else else
{ {
c.startNetwork(listenPort, remoteHost, remotePort, verbosity, mode, peers); c.startNetwork(listenPort, remoteHost, remotePort, verbosity, mode, peers, publicIP);
eth::uint n = c.blockChain().details().number; eth::uint n = c.blockChain().details().number;
while (true) while (true)
{ {

4
libethereum/Client.cpp

@ -50,11 +50,11 @@ Client::~Client()
usleep(10000); usleep(10000);
} }
void Client::startNetwork(short _listenPort, std::string const& _seedHost, short _port, unsigned _verbosity, NodeMode _mode, unsigned _peers) void Client::startNetwork(short _listenPort, std::string const& _seedHost, short _port, unsigned _verbosity, NodeMode _mode, unsigned _peers, string const& _publicIP)
{ {
if (m_net) if (m_net)
return; return;
m_net = new PeerServer(m_clientVersion, m_bc, 0, _listenPort, _mode); m_net = new PeerServer(m_clientVersion, m_bc, 0, _listenPort, _mode, _publicIP);
m_net->setIdealPeerCount(_peers); m_net->setIdealPeerCount(_peers);
m_net->setVerbosity(_verbosity); m_net->setVerbosity(_verbosity);
if (_seedHost.size()) if (_seedHost.size())

2
libethereum/Client.h

@ -60,7 +60,7 @@ public:
std::vector<PeerInfo> peers() { return m_net ? m_net->peers() : std::vector<PeerInfo>(); } std::vector<PeerInfo> peers() { return m_net ? m_net->peers() : std::vector<PeerInfo>(); }
unsigned peerCount() const { return m_net ? m_net->peerCount() : 0; } unsigned peerCount() const { return m_net ? m_net->peerCount() : 0; }
void startNetwork(short _listenPort = 30303, std::string const& _seedHost = std::string(), short _port = 30303, unsigned _verbosity = 0, NodeMode _mode = NodeMode::Full, unsigned _peers = 5); void startNetwork(short _listenPort = 30303, std::string const& _seedHost = std::string(), short _port = 30303, unsigned _verbosity = 4, NodeMode _mode = NodeMode::Full, unsigned _peers = 5, std::string const& _publicIP = std::string());
void connect(std::string const& _seedHost, short _port = 30303); void connect(std::string const& _seedHost, short _port = 30303);
void stopNetwork(); void stopNetwork();

21
libethereum/PeerNetwork.cpp

@ -662,7 +662,7 @@ struct UPnP
class NoNetworking: public std::exception {}; class NoNetworking: public std::exception {};
PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, uint _networkId, short _port, NodeMode _m): PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, uint _networkId, short _port, NodeMode _m, string const& _publicAddress):
m_clientVersion(_clientVersion), m_clientVersion(_clientVersion),
m_mode(_m), m_mode(_m),
m_listenPort(_port), m_listenPort(_port),
@ -672,7 +672,7 @@ PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch,
m_requiredNetworkId(_networkId) m_requiredNetworkId(_networkId)
{ {
populateAddresses(); populateAddresses();
determinePublic(); determinePublic(_publicAddress);
ensureAccepting(); ensureAccepting();
if (m_verbosity) if (m_verbosity)
cout << "Mode: " << (_m == NodeMode::PeerServer ? "PeerServer" : "Full") << endl; cout << "Mode: " << (_m == NodeMode::PeerServer ? "PeerServer" : "Full") << endl;
@ -699,7 +699,7 @@ PeerServer::~PeerServer()
delete m_upnp; delete m_upnp;
} }
void PeerServer::determinePublic() void PeerServer::determinePublic(string const& _publicAddress)
{ {
m_upnp = new UPnP; m_upnp = new UPnP;
if (m_upnp->isValid() && m_peerAddresses.size()) if (m_upnp->isValid() && m_peerAddresses.size())
@ -714,9 +714,14 @@ void PeerServer::determinePublic()
p = m_listenPort; p = m_listenPort;
} }
auto it = r.resolve({m_upnp->externalIP(), toString(p)}); if (m_upnp->externalIP() == string("0.0.0.0") && _publicAddress.empty())
m_public = it->endpoint(); m_public = bi::tcp::endpoint(bi::address(), p);
m_addresses.push_back(m_public.address().to_v4()); else
{
auto it = r.resolve({_publicAddress.empty() ? m_upnp->externalIP() : _publicAddress, toString(p)});
m_public = it->endpoint();
m_addresses.push_back(m_public.address().to_v4());
}
} }
/* int er; /* int er;
UPNPDev* dlist = upnpDiscover(250, 0, 0, 0, 0, &er); UPNPDev* dlist = upnpDiscover(250, 0, 0, 0, 0, &er);
@ -762,8 +767,8 @@ void PeerServer::populateAddresses()
std::vector<bi::tcp::endpoint> PeerServer::potentialPeers() std::vector<bi::tcp::endpoint> PeerServer::potentialPeers()
{ {
std::vector<bi::tcp::endpoint> ret; std::vector<bi::tcp::endpoint> ret;
for (auto i: m_peerAddresses) if (!m_public.address().is_unspecified())
ret.push_back(bi::tcp::endpoint(i, listenPort())); ret.push_back(m_public);
for (auto i: m_peers) for (auto i: m_peers)
if (auto j = i.lock()) if (auto j = i.lock())
ret.push_back(j->endpoint()); ret.push_back(j->endpoint());

4
libethereum/PeerNetwork.h

@ -125,7 +125,7 @@ class PeerServer
public: public:
/// Start server, listening for connections on the given port. /// Start server, listening for connections on the given port.
PeerServer(std::string const& _clientVersion, BlockChain const& _ch, uint _networkId, short _port, NodeMode _m = NodeMode::Full); PeerServer(std::string const& _clientVersion, BlockChain const& _ch, uint _networkId, short _port, NodeMode _m = NodeMode::Full, std::string const& _publicAddress = std::string());
/// Start server, but don't listen. /// Start server, but don't listen.
PeerServer(std::string const& _clientVersion, uint _networkId); PeerServer(std::string const& _clientVersion, uint _networkId);
~PeerServer(); ~PeerServer();
@ -161,7 +161,7 @@ public:
private: private:
void seal(bytes& _b); void seal(bytes& _b);
void populateAddresses(); void populateAddresses();
void determinePublic(); void determinePublic(std::string const& _publicAddress);
void ensureAccepting(); void ensureAccepting();
std::vector<bi::tcp::endpoint> potentialPeers(); std::vector<bi::tcp::endpoint> potentialPeers();

Loading…
Cancel
Save