fixed natpmp retry issue
This commit is contained in:
parent
ecf13f2475
commit
29c5b99bba
|
@ -95,6 +95,7 @@ private:
|
||||||
, external_port(0)
|
, external_port(0)
|
||||||
, protocol(none)
|
, protocol(none)
|
||||||
, map_sent(false)
|
, map_sent(false)
|
||||||
|
, outstanding_request(false)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
// indicates that the mapping has changed
|
// indicates that the mapping has changed
|
||||||
|
@ -117,6 +118,9 @@ private:
|
||||||
|
|
||||||
// set to true when the first map request is sent
|
// set to true when the first map request is sent
|
||||||
bool map_sent;
|
bool map_sent;
|
||||||
|
|
||||||
|
// set to true while we're waiting for a response
|
||||||
|
bool outstanding_request;
|
||||||
};
|
};
|
||||||
|
|
||||||
portmap_callback_t m_callback;
|
portmap_callback_t m_callback;
|
||||||
|
|
|
@ -48,6 +48,12 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <boost/asio/ip/host_name.hpp>
|
#include <boost/asio/ip/host_name.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
//#define NATPMP_LOG
|
||||||
|
|
||||||
|
#ifdef NATPMP_LOG
|
||||||
|
#include <iostream>
|
||||||
|
#endif
|
||||||
|
|
||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
|
||||||
natpmp::natpmp(io_service& ios, address const& listen_interface
|
natpmp::natpmp(io_service& ios, address const& listen_interface
|
||||||
|
@ -197,19 +203,33 @@ int natpmp::add_mapping(protocol_type p, int external_port, int local_port)
|
||||||
|
|
||||||
int mapping_index = i - m_mappings.begin();
|
int mapping_index = i - m_mappings.begin();
|
||||||
|
|
||||||
|
#ifdef NATPMP_LOG
|
||||||
|
ptime now = time_now();
|
||||||
|
for (std::vector<mapping_t>::iterator m = m_mappings.begin()
|
||||||
|
, end(m_mappings.end()); m != end; ++m)
|
||||||
|
{
|
||||||
|
std::cout << " ADD MAPPING: " << mapping_index << " [ "
|
||||||
|
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp")
|
||||||
|
<< " port: " << i->external_port
|
||||||
|
<< " local-port: " << i->local_port
|
||||||
|
<< " action: " << (i->action == mapping_t::action_none ? "none" : i->action == mapping_t::action_add ? "add" : "delete")
|
||||||
|
<< " ttl: " << total_seconds(i->expires - now)
|
||||||
|
<< " ]" << std::endl;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
update_mapping(mapping_index, l);
|
update_mapping(mapping_index, l);
|
||||||
return mapping_index;
|
return mapping_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
void natpmp::try_next_mapping(int i, mutex::scoped_lock& l)
|
void natpmp::try_next_mapping(int i, mutex::scoped_lock& l)
|
||||||
{
|
{
|
||||||
/*
|
#ifdef NATPMP_LOG
|
||||||
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
|
||||||
ptime now = time_now();
|
ptime now = time_now();
|
||||||
for (std::vector<mapping_t>::iterator m = m_mappings.begin()
|
for (std::vector<mapping_t>::iterator m = m_mappings.begin()
|
||||||
, end(m_mappings.end()); m != end; ++m)
|
, end(m_mappings.end()); m != end; ++m)
|
||||||
{
|
{
|
||||||
m_log << " " << (m - m_mappings.begin()) << " [ "
|
std::cout << " " << (m - m_mappings.begin()) << " [ "
|
||||||
"proto: " << (m->protocol == none ? "none" : m->protocol == tcp ? "tcp" : "udp")
|
"proto: " << (m->protocol == none ? "none" : m->protocol == tcp ? "tcp" : "udp")
|
||||||
<< " port: " << m->external_port
|
<< " port: " << m->external_port
|
||||||
<< " local-port: " << m->local_port
|
<< " local-port: " << m->local_port
|
||||||
|
@ -218,7 +238,6 @@ void natpmp::try_next_mapping(int i, mutex::scoped_lock& l)
|
||||||
<< " ]" << std::endl;
|
<< " ]" << std::endl;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
*/
|
|
||||||
if (i < int(m_mappings.size()) - 1)
|
if (i < int(m_mappings.size()) - 1)
|
||||||
{
|
{
|
||||||
update_mapping(i + 1, l);
|
update_mapping(i + 1, l);
|
||||||
|
@ -237,15 +256,15 @@ void natpmp::try_next_mapping(int i, mutex::scoped_lock& l)
|
||||||
m_send_timer.cancel(ec);
|
m_send_timer.cancel(ec);
|
||||||
m_socket.close(ec);
|
m_socket.close(ec);
|
||||||
}
|
}
|
||||||
//#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
#ifdef NATPMP_LOG
|
||||||
// m_log << " done" << (m_abort?" shutting down":"") << std::endl;
|
std::cout << " done" << (m_abort?" shutting down":"") << std::endl;
|
||||||
//#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
#ifdef NATPMP_LOG
|
||||||
// m_log << " updating " << (m - m_mappings.begin()) << std::endl;
|
std::cout << " updating " << (m - m_mappings.begin()) << std::endl;
|
||||||
//#endif
|
#endif
|
||||||
|
|
||||||
update_mapping(m - m_mappings.begin(), l);
|
update_mapping(m - m_mappings.begin(), l);
|
||||||
}
|
}
|
||||||
|
@ -260,9 +279,9 @@ void natpmp::update_mapping(int i, mutex::scoped_lock& l)
|
||||||
m_send_timer.cancel(ec);
|
m_send_timer.cancel(ec);
|
||||||
m_socket.close(ec);
|
m_socket.close(ec);
|
||||||
}
|
}
|
||||||
//#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
#ifdef NATPMP_LOG
|
||||||
// m_log << " done" << (m_abort?" shutting down":"") << std::endl;
|
std::cout << " done" << (m_abort?" shutting down":"") << std::endl;
|
||||||
//#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,9 +322,9 @@ void natpmp::send_map_request(int i, mutex::scoped_lock& l)
|
||||||
write_uint32(ttl, out); // port mapping lifetime
|
write_uint32(ttl, out); // port mapping lifetime
|
||||||
|
|
||||||
char msg[200];
|
char msg[200];
|
||||||
snprintf(msg, sizeof(msg), "==> port map [ action: %s"
|
snprintf(msg, sizeof(msg), "==> port map [ mapping: %d action: %s"
|
||||||
" proto: %s local: %u external: %u ttl: %u ]"
|
" proto: %s local: %u external: %u ttl: %u ]"
|
||||||
, m.action == mapping_t::action_add ? "add" : "delete"
|
, i, m.action == mapping_t::action_add ? "add" : "delete"
|
||||||
, m.protocol == udp ? "udp" : "tcp"
|
, m.protocol == udp ? "udp" : "tcp"
|
||||||
, m.local_port, m.external_port, ttl);
|
, m.local_port, m.external_port, ttl);
|
||||||
log(msg, l);
|
log(msg, l);
|
||||||
|
@ -313,6 +332,7 @@ void natpmp::send_map_request(int i, mutex::scoped_lock& l)
|
||||||
error_code ec;
|
error_code ec;
|
||||||
m_socket.send_to(asio::buffer(buf, 12), m_nat_endpoint, 0, ec);
|
m_socket.send_to(asio::buffer(buf, 12), m_nat_endpoint, 0, ec);
|
||||||
m.map_sent = true;
|
m.map_sent = true;
|
||||||
|
m.outstanding_request = true;
|
||||||
if (m_abort)
|
if (m_abort)
|
||||||
{
|
{
|
||||||
// when we're shutting down, ignore the
|
// when we're shutting down, ignore the
|
||||||
|
@ -422,6 +442,7 @@ void natpmp::on_reply(error_code const& e
|
||||||
if (private_port != i->local_port) continue;
|
if (private_port != i->local_port) continue;
|
||||||
if (protocol != i->protocol) continue;
|
if (protocol != i->protocol) continue;
|
||||||
if (!i->map_sent) continue;
|
if (!i->map_sent) continue;
|
||||||
|
if (!i->outstanding_request) continue;
|
||||||
m = &*i;
|
m = &*i;
|
||||||
index = i - m_mappings.begin();
|
index = i - m_mappings.begin();
|
||||||
break;
|
break;
|
||||||
|
@ -433,6 +454,8 @@ void natpmp::on_reply(error_code const& e
|
||||||
log(msg, l);
|
log(msg, l);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
m->outstanding_request = false;
|
||||||
|
|
||||||
log(msg, l);
|
log(msg, l);
|
||||||
|
|
||||||
if (public_port == 0 || lifetime == 0)
|
if (public_port == 0 || lifetime == 0)
|
||||||
|
@ -486,13 +509,12 @@ void natpmp::update_expiration_timer(mutex::scoped_lock& l)
|
||||||
if (m_abort) return;
|
if (m_abort) return;
|
||||||
|
|
||||||
ptime now = time_now() + milliseconds(100);
|
ptime now = time_now() + milliseconds(100);
|
||||||
/*
|
#ifdef NATPMP_LOG
|
||||||
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
std::cout << time_now_string() << " update_expiration_timer " << std::endl;
|
||||||
m_log << time_now_string() << "update_expiration_timer " << std::endl;
|
|
||||||
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
||||||
, end(m_mappings.end()); i != end; ++i)
|
, end(m_mappings.end()); i != end; ++i)
|
||||||
{
|
{
|
||||||
m_log << " " << (i - m_mappings.begin()) << " [ "
|
std::cout << " " << (i - m_mappings.begin()) << " [ "
|
||||||
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp")
|
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp")
|
||||||
<< " port: " << i->external_port
|
<< " port: " << i->external_port
|
||||||
<< " local-port: " << i->local_port
|
<< " local-port: " << i->local_port
|
||||||
|
@ -501,7 +523,6 @@ void natpmp::update_expiration_timer(mutex::scoped_lock& l)
|
||||||
<< " ]" << std::endl;
|
<< " ]" << std::endl;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
*/
|
|
||||||
ptime min_expire = now + seconds(3600);
|
ptime min_expire = now + seconds(3600);
|
||||||
int min_index = -1;
|
int min_index = -1;
|
||||||
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
||||||
|
@ -531,14 +552,12 @@ void natpmp::update_expiration_timer(mutex::scoped_lock& l)
|
||||||
|
|
||||||
if (min_index >= 0)
|
if (min_index >= 0)
|
||||||
{
|
{
|
||||||
/*
|
#ifdef NATPMP_LOG
|
||||||
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
std::cout << time_now_string() << " next expiration ["
|
||||||
m_log << time_now_string() << " next expiration ["
|
|
||||||
" i: " << min_index
|
" i: " << min_index
|
||||||
<< " ttl: " << total_seconds(min_expire - time_now())
|
<< " ttl: " << total_seconds(min_expire - time_now())
|
||||||
<< " ]" << std::endl;
|
<< " ]" << std::endl;
|
||||||
#endif
|
#endif
|
||||||
*/
|
|
||||||
error_code ec;
|
error_code ec;
|
||||||
if (m_next_refresh >= 0) m_refresh_timer.cancel(ec);
|
if (m_next_refresh >= 0) m_refresh_timer.cancel(ec);
|
||||||
m_refresh_timer.expires_from_now(min_expire - now, ec);
|
m_refresh_timer.expires_from_now(min_expire - now, ec);
|
||||||
|
@ -569,19 +588,16 @@ void natpmp::close_impl(mutex::scoped_lock& l)
|
||||||
{
|
{
|
||||||
m_abort = true;
|
m_abort = true;
|
||||||
log("closing", l);
|
log("closing", l);
|
||||||
/*
|
#ifdef NATPMP_LOG
|
||||||
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
std::cout << time_now_string() << " close" << std::endl;
|
||||||
m_log << time_now_string() << " close" << std::endl;
|
|
||||||
#endif
|
#endif
|
||||||
*/
|
|
||||||
if (m_disabled) return;
|
if (m_disabled) return;
|
||||||
ptime now = time_now();
|
ptime now = time_now();
|
||||||
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
||||||
, end(m_mappings.end()); i != end; ++i)
|
, end(m_mappings.end()); i != end; ++i)
|
||||||
{
|
{
|
||||||
/*
|
#ifdef NATPMP_LOG
|
||||||
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
std::cout << " " << (i - m_mappings.begin()) << " [ "
|
||||||
m_log << " " << (i - m_mappings.begin()) << " [ "
|
|
||||||
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp")
|
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp")
|
||||||
<< " port: " << i->external_port
|
<< " port: " << i->external_port
|
||||||
<< " local-port: " << i->local_port
|
<< " local-port: " << i->local_port
|
||||||
|
@ -589,7 +605,6 @@ void natpmp::close_impl(mutex::scoped_lock& l)
|
||||||
<< " ttl: " << total_seconds(i->expires - now)
|
<< " ttl: " << total_seconds(i->expires - now)
|
||||||
<< " ]" << std::endl;
|
<< " ]" << std::endl;
|
||||||
#endif
|
#endif
|
||||||
*/
|
|
||||||
if (i->protocol == none) continue;
|
if (i->protocol == none) continue;
|
||||||
i->action = mapping_t::action_delete;
|
i->action = mapping_t::action_delete;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue