diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 05fa81f4c..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) @@ -210,7 +211,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 = m_upnp->addRedirect(m_peerAddresses[0].to_string().c_str(), m_listenPort); + 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/UPnP.cpp b/libp2p/UPnP.cpp index 211b185bb..42868d67a 100644 --- a/libp2p/UPnP.cpp +++ b/libp2p/UPnP.cpp @@ -123,16 +123,18 @@ 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; } 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