attempt to fix logic around around actually closing udp sockets (specifically the SSL UDP socket)

This commit is contained in:
arvidn 2016-02-21 20:00:55 -05:00
parent d8b88ba381
commit 72a3771b87
3 changed files with 105 additions and 36 deletions

View File

@ -79,14 +79,7 @@ namespace libtorrent
, dont_queue = 8
};
bool is_open() const
{
return m_ipv4_sock.is_open()
#if TORRENT_USE_IPV6
|| m_ipv6_sock.is_open()
#endif
;
}
bool is_open() const { return m_abort == false; }
io_service& get_io_service() { return m_ipv4_sock.get_io_service(); }
void subscribe(udp_socket_observer* o);
@ -144,7 +137,7 @@ namespace libtorrent
udp::endpoint proxy_addr() const { return m_proxy_addr; }
protected:
private:
struct queued_packet
{
@ -170,12 +163,12 @@ namespace libtorrent
;
}
private:
// non-copyable
udp_socket(udp_socket const&);
udp_socket& operator=(udp_socket const&);
void close_impl();
// observers on this udp socket
std::vector<udp_socket_observer*> m_observers;
std::vector<udp_socket_observer*> m_added_observers;

View File

@ -2040,7 +2040,7 @@ retry:
}
#ifdef TORRENT_USE_OPENSSL
int ssl_port = m_settings.get_int(settings_pack::ssl_listen);
int const ssl_port = m_settings.get_int(settings_pack::ssl_listen);
udp::endpoint ssl_bind_if(m_listen_interface.address(), ssl_port);
// if ssl port is 0, we don't want to listen on an SSL port
@ -2070,6 +2070,8 @@ retry:
}
else
{
m_ssl_udp_socket.close();
// if there are mappings for the SSL socket, delete them now
if (m_ssl_udp_mapping[0] != -1 && m_natpmp)
{
@ -2081,7 +2083,6 @@ retry:
m_upnp->delete_mapping(m_ssl_udp_mapping[1]);
m_ssl_udp_mapping[1] = -1;
}
}
#endif // TORRENT_USE_OPENSSL
@ -2193,7 +2194,11 @@ retry:
if (m_tcp_mapping[0] != -1) m_natpmp->delete_mapping(m_tcp_mapping[0]);
m_tcp_mapping[0] = m_natpmp->add_mapping(natpmp::tcp, tcp_port, tcp_port);
#ifdef TORRENT_USE_OPENSSL
if (m_ssl_tcp_mapping[0] != -1) m_natpmp->delete_mapping(m_ssl_tcp_mapping[0]);
if (m_ssl_tcp_mapping[0] != -1)
{
m_natpmp->delete_mapping(m_ssl_tcp_mapping[0]);
m_ssl_tcp_mapping[0] = -1;
}
if (ssl_port > 0) m_ssl_tcp_mapping[0] = m_natpmp->add_mapping(natpmp::tcp
, ssl_port, ssl_port);
#endif
@ -2203,7 +2208,11 @@ retry:
if (m_tcp_mapping[1] != -1) m_upnp->delete_mapping(m_tcp_mapping[1]);
m_tcp_mapping[1] = m_upnp->add_mapping(upnp::tcp, tcp_port, tcp_port);
#ifdef TORRENT_USE_OPENSSL
if (m_ssl_tcp_mapping[1] != -1) m_upnp->delete_mapping(m_ssl_tcp_mapping[1]);
if (m_ssl_tcp_mapping[1] != -1)
{
m_upnp->delete_mapping(m_ssl_tcp_mapping[1]);
m_ssl_tcp_mapping[1] = -1;
}
if (ssl_port > 0) m_ssl_tcp_mapping[1] = m_upnp->add_mapping(upnp::tcp
, ssl_port, ssl_port);
#endif
@ -5378,6 +5387,11 @@ retry:
boost::uint16_t session_impl::ssl_listen_port() const
{
#ifdef TORRENT_USE_OPENSSL
// honor the SSL listen port being disabled
if (m_settings.get_int(settings_pack::ssl_listen) == 0)
return 0;
// if peer connections are set up to be received over a socks
// proxy, and it's the same one as we're using for the tracker
// just tell the tracker the socks5 port we're listening on
@ -6636,7 +6650,7 @@ retry:
, this, _1, 0));
m_natpmp->start();
int ssl_port = ssl_listen_port();
int const ssl_port = ssl_listen_port();
if (m_listen_interface.port() > 0)
{

View File

@ -78,7 +78,7 @@ udp_socket::udp_socket(io_service& ios)
, m_queue_packets(false)
, m_tunnel_packets(false)
, m_force_proxy(false)
, m_abort(false)
, m_abort(true)
, m_outstanding_ops(0)
#if TORRENT_USE_IPV6
, m_v6_write_subscribed(false)
@ -307,7 +307,11 @@ void udp_socket::on_read(error_code const& ec, udp::socket* s)
}
return;
}
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
@ -493,7 +497,11 @@ void udp_socket::on_read_impl(udp::endpoint const& ep
return;
}
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
return;
}
@ -516,7 +524,11 @@ void udp_socket::on_read_impl(udp::endpoint const& ep
void udp_socket::setup_read(udp::socket* s)
{
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
#if TORRENT_USE_IPV6
if (s == &m_ipv6_sock)
@ -774,17 +786,13 @@ void udp_socket::bind(udp::endpoint const& ep, error_code& ec)
CHECK_MAGIC;
TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT(m_abort == false);
if (m_abort)
{
ec = boost::asio::error::operation_aborted;
return;
}
m_abort = false;
if (m_ipv4_sock.is_open()) m_ipv4_sock.close(ec);
#if TORRENT_USE_IPV6
if (m_ipv6_sock.is_open()) m_ipv6_sock.close(ec);
#endif
ec.clear();
if (ep.address().is_v4())
{
@ -858,7 +866,11 @@ void udp_socket::set_proxy_settings(aux::proxy_settings const& ps)
m_proxy_settings = ps;
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
if (ps.type == settings_pack::socks5
|| ps.type == settings_pack::socks5_pw)
@ -880,6 +892,19 @@ void udp_socket::set_proxy_settings(aux::proxy_settings const& ps)
}
}
void udp_socket::close_impl()
{
if (m_outstanding_ops == 0)
{
error_code ec;
m_ipv4_sock.close(ec);
#if TORRENT_USE_IPV6
m_ipv6_sock.close(ec);
#endif
m_socks5_sock.close(ec);
}
}
void udp_socket::on_name_lookup(error_code const& e, tcp::resolver::iterator i)
{
#if defined TORRENT_ASIO_DEBUGGING
@ -897,7 +922,12 @@ void udp_socket::on_name_lookup(error_code const& e, tcp::resolver::iterator i)
+ m_outstanding_resolve
+ m_outstanding_socks);
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
if (e == boost::asio::error::operation_aborted) return;
@ -976,7 +1006,11 @@ void udp_socket::on_connect_timeout(error_code const& ec)
m_queue_packets = false;
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
TORRENT_ASSERT(is_single_thread());
@ -1009,7 +1043,11 @@ void udp_socket::on_connected(error_code const& e)
if (e == boost::asio::error::operation_aborted) return;
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
if (e)
{
@ -1068,7 +1106,11 @@ void udp_socket::handshake1(error_code const& e)
+ m_outstanding_timeout
+ m_outstanding_resolve
+ m_outstanding_socks);
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
if (e)
{
@ -1104,7 +1146,11 @@ void udp_socket::handshake2(error_code const& e)
+ m_outstanding_timeout
+ m_outstanding_resolve
+ m_outstanding_socks);
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
if (e)
@ -1185,7 +1231,11 @@ void udp_socket::handshake3(error_code const& e)
+ m_outstanding_timeout
+ m_outstanding_resolve
+ m_outstanding_socks);
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
if (e)
{
@ -1221,7 +1271,11 @@ void udp_socket::handshake4(error_code const& e)
+ m_outstanding_timeout
+ m_outstanding_resolve
+ m_outstanding_socks);
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
if (e)
{
@ -1287,7 +1341,11 @@ void udp_socket::connect1(error_code const& e)
+ m_outstanding_timeout
+ m_outstanding_resolve
+ m_outstanding_socks);
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
if (e)
{
@ -1394,7 +1452,11 @@ void udp_socket::hung_up(error_code const& e)
+ m_outstanding_timeout
+ m_outstanding_resolve
+ m_outstanding_socks);
if (m_abort) return;
if (m_abort)
{
close_impl();
return;
}
CHECK_MAGIC;
TORRENT_ASSERT(is_single_thread());