Browse Source

fixed upnp port mapping

cl-refactor
arkpar 10 years ago
parent
commit
5d09a767e0
  1. 7
      libp2p/Host.cpp
  2. 10
      libp2p/UPnP.cpp
  3. 4
      libp2p/_libp2p.cpp

7
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

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

4
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
Loading…
Cancel
Save