From 5d09a767e02005d691260e2430c980a4ea320f3f Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 8 Oct 2014 01:39:49 +0400 Subject: [PATCH 1/4] fixed upnp port mapping --- libp2p/Host.cpp | 7 ++++++- libp2p/UPnP.cpp | 10 ++++++---- libp2p/_libp2p.cpp | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 410699a55..e030e9a3b 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -209,7 +209,12 @@ void Host::determinePublic(string const& _publicAddress, bool _upnp) if (m_upnp && m_upnp->isValid() && m_peerAddresses.size()) { clog(NetNote) << "External addr:" << m_upnp->externalIP(); - int p = m_upnp->addRedirect(m_peerAddresses[0].to_string().c_str(), m_listenPort); + int p = 0; + for (auto const& addr : m_peerAddresses) + { + if (p = m_upnp->addRedirect(addr.to_string().c_str(), m_listenPort)) + break; + } if (p) clog(NetNote) << "Punched through NAT and mapped local port" << m_listenPort << "onto external port" << p << "."; else diff --git a/libp2p/UPnP.cpp b/libp2p/UPnP.cpp index 5ac3e9427..342dc58cd 100644 --- a/libp2p/UPnP.cpp +++ b/libp2p/UPnP.cpp @@ -122,21 +122,23 @@ int UPnP::addRedirect(char const* _addr, int _port) // Try direct mapping first (port external, port internal). char port_str[16]; + char ext_port_str[16]; sprintf(port_str, "%d", _port); if (!UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, port_str, port_str, _addr, "ethereum", "TCP", NULL, NULL)) return _port; // Failed - now try (random external, port internal) and cycle up to 10 times. + srand(time(NULL)); for (unsigned i = 0; i < 10; ++i) { - _port = rand() % 65535 - 1024 + 1024; - sprintf(port_str, "%d", _port); - if (!UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, NULL, port_str, _addr, "ethereum", "TCP", NULL, NULL)) + _port = rand() % (65535 - 1024) + 1024; + sprintf(ext_port_str, "%d", _port); + if (!UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, ext_port_str, port_str, _addr, "ethereum", "TCP", NULL, NULL)) return _port; } // Failed. Try asking the router to give us a free external port. - if (UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, port_str, NULL, _addr, "ethereum", "TCP", NULL, NULL)) + if (UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, NULL, port_str, _addr, "ethereum", "TCP", NULL, NULL)) // Failed. Exit. return 0; diff --git a/libp2p/_libp2p.cpp b/libp2p/_libp2p.cpp index 440ba362b..59de254a5 100644 --- a/libp2p/_libp2p.cpp +++ b/libp2p/_libp2p.cpp @@ -1,9 +1,11 @@ #ifdef _MSC_VER +#define ETH_MINIUPNPC 1 #include "All.h" #include "Capability.cpp" #include "Common.cpp" #include "Host.cpp" #include "HostCapability.cpp" +#undef clogS #include "Session.cpp" #include "UPnP.cpp" -#endif +#endif \ No newline at end of file From 9d29f7d4becc673bab1ecccf6be43d294dbe2a94 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 8 Oct 2014 17:19:11 +0400 Subject: [PATCH 2/4] moved ETH_MINIUPNPC to props on windows; fixed style --- libp2p/Host.cpp | 4 +--- libp2p/_libp2p.cpp | 4 +--- windows/LibEthereum.props | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 77e40bb2f..a369a64b5 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -210,12 +210,10 @@ void Host::determinePublic(string const& _publicAddress, bool _upnp) if (m_upnp && m_upnp->isValid() && m_peerAddresses.size()) { clog(NetNote) << "External addr:" << m_upnp->externalIP(); - int p = 0; + int p; for (auto const& addr : m_peerAddresses) - { if (p = m_upnp->addRedirect(addr.to_string().c_str(), m_listenPort)) break; - } if (p) clog(NetNote) << "Punched through NAT and mapped local port" << m_listenPort << "onto external port" << p << "."; else diff --git a/libp2p/_libp2p.cpp b/libp2p/_libp2p.cpp index 59de254a5..440ba362b 100644 --- a/libp2p/_libp2p.cpp +++ b/libp2p/_libp2p.cpp @@ -1,11 +1,9 @@ #ifdef _MSC_VER -#define ETH_MINIUPNPC 1 #include "All.h" #include "Capability.cpp" #include "Common.cpp" #include "Host.cpp" #include "HostCapability.cpp" -#undef clogS #include "Session.cpp" #include "UPnP.cpp" -#endif \ No newline at end of file +#endif diff --git a/windows/LibEthereum.props b/windows/LibEthereum.props index 136a8edab..8c0bcac90 100644 --- a/windows/LibEthereum.props +++ b/windows/LibEthereum.props @@ -15,7 +15,7 @@ true false include/$(ProjectName);$(IntDir);../../cryptopp;..;../libethcore;../libethereum;../libethsupport;$(BoostDir);../../leveldb/include;../secp256k1;../../miniupnp - ETH_BUILD_PLATFORM=Windows/VS2013;ETH_BUILD_TYPE=$(Configuration)-$(Platform);STATICLIB;LEVELDB_PLATFORM_WINDOWS;USE_NUM_BOOST;USE_FIELD_10X26;USE_FIELD_INV_BUILTIN;_WIN32_WINNT=0x0501;WIN32;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + ETH_BUILD_PLATFORM=Windows/VS2013;ETH_BUILD_TYPE=$(Configuration)-$(Platform);ETH_MINIUPNPC=1;STATICLIB;LEVELDB_PLATFORM_WINDOWS;USE_NUM_BOOST;USE_FIELD_10X26;USE_FIELD_INV_BUILTIN;_WIN32_WINNT=0x0501;WIN32;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true true Level3 From da331ee67e17713436bc3a9ba9e12e95622b4985 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 8 Oct 2014 17:39:53 +0400 Subject: [PATCH 3/4] fixed disconnect; gcc warning --- libp2p/Host.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index a369a64b5..b509ecc2a 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -182,6 +182,7 @@ void Host::disconnectPeers() } delete m_upnp; + m_upnp = nullptr; } void Host::seal(bytes& _b) @@ -212,7 +213,7 @@ void Host::determinePublic(string const& _publicAddress, bool _upnp) clog(NetNote) << "External addr:" << m_upnp->externalIP(); int p; for (auto const& addr : m_peerAddresses) - if (p = m_upnp->addRedirect(addr.to_string().c_str(), m_listenPort)) + if ((p = m_upnp->addRedirect(addr.to_string().c_str(), m_listenPort))) break; if (p) clog(NetNote) << "Punched through NAT and mapped local port" << m_listenPort << "onto external port" << p << "."; From 68c64dc05219bd5b64447f59064dd21b83605cc8 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 8 Oct 2014 17:46:15 +0400 Subject: [PATCH 4/4] reverted UPNP_AddPortMapping args --- libp2p/UPnP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libp2p/UPnP.cpp b/libp2p/UPnP.cpp index 61de9ee59..42868d67a 100644 --- a/libp2p/UPnP.cpp +++ b/libp2p/UPnP.cpp @@ -139,7 +139,7 @@ int UPnP::addRedirect(char const* _addr, int _port) } // Failed. Try asking the router to give us a free external port. - if (UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, NULL, port_str, _addr, "ethereum", "TCP", NULL, NULL)) + if (UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, port_str, NULL, _addr, "ethereum", "TCP", NULL, NULL)) // Failed. Exit. return 0;