if a udp socket is already bound to the IP and port we want to bind it to, don't reopen it

This commit is contained in:
arvidn 2016-03-05 15:42:29 -05:00
parent d705dc2953
commit e9a5985609
1 changed files with 73 additions and 61 deletions

View File

@ -2069,11 +2069,17 @@ retry:
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
int const 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); udp::endpoint ssl_bind_if(m_listen_interface.address(), ssl_port);
tcp::endpoint ssl_bind_ep(m_listen_interface.address(), ssl_port);
// if ssl port is 0, we don't want to listen on an SSL port // if ssl port is 0, we don't want to listen on an SSL port
if (ssl_port != 0) if (ssl_port != 0)
{ {
// TODO: 2 use bind_to_device in udp_socket // if the socket is already open with the port we want, just leave it
error_code err;
if (!m_ssl_udp_socket.is_open()
|| m_ssl_udp_socket.local_endpoint(err) != ssl_bind_ep
|| err)
{
m_ssl_udp_socket.bind(ssl_bind_if, ec); m_ssl_udp_socket.bind(ssl_bind_if, ec);
if (ec) if (ec)
{ {
@ -2083,7 +2089,6 @@ retry:
#endif #endif
if (m_alerts.should_post<listen_failed_alert>()) if (m_alerts.should_post<listen_failed_alert>())
{ {
error_code err;
m_alerts.emplace_alert<listen_failed_alert>(ssl_bind_if.address().to_string() m_alerts.emplace_alert<listen_failed_alert>(ssl_bind_if.address().to_string()
, ssl_port, listen_failed_alert::bind, ec, listen_failed_alert::utp_ssl); , ssl_port, listen_failed_alert::bind, ec, listen_failed_alert::utp_ssl);
} }
@ -2111,11 +2116,19 @@ retry:
m_ssl_udp_mapping[1] = -1; m_ssl_udp_mapping[1] = -1;
} }
} }
}
#endif // TORRENT_USE_OPENSSL #endif // TORRENT_USE_OPENSSL
// TODO: 2 use bind_to_device in udp_socket udp::endpoint const udp_bind_ep(m_listen_interface.address()
m_udp_socket.bind(udp::endpoint(m_listen_interface.address() , m_listen_interface.port());
, m_listen_interface.port()), ec);
// if the socket is already open with the port we want, just leave it
error_code err;
if (!m_udp_socket.is_open()
|| m_udp_socket.local_endpoint(err) != m_listen_interface
|| err)
{
m_udp_socket.bind(udp_bind_ep, ec);
if (ec) if (ec)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -2124,7 +2137,6 @@ retry:
#endif #endif
if (m_alerts.should_post<listen_failed_alert>()) if (m_alerts.should_post<listen_failed_alert>())
{ {
error_code err;
m_alerts.emplace_alert<listen_failed_alert>(m_listen_interface.address().to_string() m_alerts.emplace_alert<listen_failed_alert>(m_listen_interface.address().to_string()
, m_listen_interface.port() , m_listen_interface.port()
, listen_failed_alert::bind , listen_failed_alert::bind
@ -2147,6 +2159,7 @@ retry:
maybe_update_udp_mapping(0, false, m_listen_interface.port(), m_listen_interface.port()); maybe_update_udp_mapping(0, false, m_listen_interface.port(), m_listen_interface.port());
maybe_update_udp_mapping(1, false, m_listen_interface.port(), m_listen_interface.port()); maybe_update_udp_mapping(1, false, m_listen_interface.port(), m_listen_interface.port());
} }
}
// we made it! now post all the listen_succeeded_alerts // we made it! now post all the listen_succeeded_alerts
@ -2159,9 +2172,9 @@ retry:
if (!m_alerts.should_post<listen_succeeded_alert>()) continue; if (!m_alerts.should_post<listen_succeeded_alert>()) continue;
error_code err; error_code error;
tcp::endpoint bind_ep = i->sock->local_endpoint(err); tcp::endpoint bind_ep = i->sock->local_endpoint(error);
if (err) continue; if (error) continue;
m_alerts.emplace_alert<listen_succeeded_alert>(bind_ep, socket_type); m_alerts.emplace_alert<listen_succeeded_alert>(bind_ep, socket_type);
} }
@ -2171,8 +2184,7 @@ retry:
{ {
if (m_alerts.should_post<listen_succeeded_alert>()) if (m_alerts.should_post<listen_succeeded_alert>())
m_alerts.emplace_alert<listen_succeeded_alert>( m_alerts.emplace_alert<listen_succeeded_alert>(
tcp::endpoint(ssl_bind_if.address(), ssl_bind_if.port()) ssl_bind_ep, listen_succeeded_alert::utp_ssl);
, listen_succeeded_alert::utp_ssl);
} }
#endif #endif