refactor to use the utility function for bind_socket_to_device() instead of using the bind_to_device socket option directly, to make it best effort. include operation enum in udp_error alert.

This commit is contained in:
arvidn 2018-05-18 18:02:45 +02:00 committed by Arvid Norberg
parent ff300ed224
commit 534276e7a5
4 changed files with 41 additions and 55 deletions

View File

@ -1326,6 +1326,7 @@ TORRENT_VERSION_NAMESPACE_2
udp_error_alert(
aux::stack_allocator& alloc
, udp::endpoint const& ep
, operation_t op
, error_code const& ec);
TORRENT_DEFINE_ALERT(udp_error_alert, 46)
@ -1336,6 +1337,9 @@ TORRENT_VERSION_NAMESPACE_2
// the source address associated with the error (if any)
aux::noexcept_movable<udp::endpoint> endpoint;
// the operation that failed
operation_t operation;
// the error code describing the error
error_code const error;
};

View File

@ -105,7 +105,7 @@ namespace libtorrent {
// TODO: 3 use string_view for device_name
template <class Socket>
address bind_socket_to_device(io_service& ios, Socket& sock
, boost::asio::ip::tcp const& protocol
, tcp const& protocol
, char const* device_name, int port, error_code& ec)
{
tcp::endpoint bind_ep(address_v4::any(), std::uint16_t(port));

View File

@ -1018,15 +1018,19 @@ namespace {
udp_error_alert::udp_error_alert(
aux::stack_allocator&
, udp::endpoint const& ep
, operation_t op
, error_code const& ec)
: endpoint(ep)
, operation(op)
, error(ec)
{}
std::string udp_error_alert::message() const
{
error_code ec;
return "UDP error: " + convert_from_native(error.message()) + " from: " + endpoint.address().to_string(ec);
return "UDP error: " + convert_from_native(error.message())
+ " from: " + endpoint.address().to_string(ec)
+ " op: " + operation_name(operation);
}
external_ip_alert::external_ip_alert(aux::stack_allocator&

View File

@ -1438,24 +1438,14 @@ namespace aux {
// have SO_BINDTODEVICE functionality, use it now.
#if TORRENT_HAS_BINDTODEVICE
ret->sock->set_option(bind_to_device(lep.device.c_str()), ec);
if (ec)
{
#ifndef TORRENT_DISABLE_LOGGING
if (should_log())
{
session_log("bind to device failed (device: %s): %s"
, lep.device.c_str(), ec.message().c_str());
}
#endif // TORRENT_DISABLE_LOGGING
last_op = operation_t::sock_bind_to_device;
if (m_alerts.should_post<listen_failed_alert>())
{
m_alerts.emplace_alert<listen_failed_alert>(lep.device, bind_ep
, last_op, ec, sock_type);
}
return ret;
if (ec && should_log())
{
session_log("bind to device failed (device: %s): %s"
, lep.device.c_str(), ec.message().c_str());
}
#endif // TORRENT_DISABLE_LOGGING
ec.clear();
#endif
}
@ -1589,24 +1579,14 @@ namespace aux {
if (!lep.device.empty())
{
ret->udp_sock->sock.set_option(bind_to_device(lep.device.c_str()), ec);
if (ec)
{
#ifndef TORRENT_DISABLE_LOGGING
if (should_log())
{
session_log("bind to device failed (device: %s): %s"
, lep.device.c_str(), ec.message().c_str());
}
#endif // TORRENT_DISABLE_LOGGING
last_op = operation_t::sock_bind_to_device;
if (m_alerts.should_post<listen_failed_alert>())
{
m_alerts.emplace_alert<listen_failed_alert>(lep.device, bind_ep
, last_op, ec, udp_sock_type);
}
return ret;
if (ec && should_log())
{
session_log("bind to device failed (device: %s): %s"
, lep.device.c_str(), ec.message().c_str());
}
#endif // TORRENT_DISABLE_LOGGING
ec.clear();
}
#endif
ret->udp_sock->sock.bind(udp_bind_ep, ec);
@ -1676,7 +1656,8 @@ namespace aux {
if (err)
{
if (m_alerts.should_post<udp_error_alert>())
m_alerts.emplace_alert<udp_error_alert>(ret->udp_sock->sock.local_endpoint(ec), err);
m_alerts.emplace_alert<udp_error_alert>(ret->udp_sock->sock.local_endpoint(ec)
, operation_t::alloc_recvbuf, err);
}
ret->udp_sock->sock.set_force_proxy(m_settings.get_bool(settings_pack::force_proxy));
@ -2042,7 +2023,8 @@ namespace aux {
}
#endif
if (m_alerts.should_post<udp_error_alert>())
m_alerts.emplace_alert<udp_error_alert>(udp_bind_ep, ec);
m_alerts.emplace_alert<udp_error_alert>(udp_bind_ep
, operation_t::sock_open, ec);
continue;
}
@ -2050,20 +2032,14 @@ namespace aux {
if (!ep.device.empty())
{
udp_sock->sock.set_option(bind_to_device(ep.device.c_str()), ec);
if (ec)
{
#ifndef TORRENT_DISABLE_LOGGING
if (should_log())
{
session_log("bind to device failed (device: %s): %s"
, ep.device.c_str(), ec.message().c_str());
}
#endif // TORRENT_DISABLE_LOGGING
if (m_alerts.should_post<udp_error_alert>())
m_alerts.emplace_alert<udp_error_alert>(udp_bind_ep, ec);
continue;
if (ec && should_log())
{
session_log("bind to device failed (device: %s): %s"
, ep.device.c_str(), ec.message().c_str());
}
#endif // TORRENT_DISABLE_LOGGING
ec.clear();
}
#endif
udp_sock->sock.bind(udp_bind_ep, ec);
@ -2078,7 +2054,8 @@ namespace aux {
}
#endif
if (m_alerts.should_post<udp_error_alert>())
m_alerts.emplace_alert<udp_error_alert>(udp_bind_ep, ec);
m_alerts.emplace_alert<udp_error_alert>(udp_bind_ep
, operation_t::sock_bind, ec);
continue;
}
@ -2087,7 +2064,8 @@ namespace aux {
if (err)
{
if (m_alerts.should_post<udp_error_alert>())
m_alerts.emplace_alert<udp_error_alert>(udp_sock->sock.local_endpoint(ec), err);
m_alerts.emplace_alert<udp_error_alert>(udp_sock->sock.local_endpoint(ec)
, operation_t::alloc_recvbuf, err);
}
udp_sock->sock.set_force_proxy(m_settings.get_bool(settings_pack::force_proxy));
@ -2370,7 +2348,8 @@ namespace aux {
&& ec != boost::asio::error::bad_descriptor
&& m_alerts.should_post<udp_error_alert>())
{
m_alerts.emplace_alert<udp_error_alert>(ep, ec);
m_alerts.emplace_alert<udp_error_alert>(ep
, operation_t::sock_read, ec);
}
#ifndef TORRENT_DISABLE_LOGGING
@ -2455,7 +2434,8 @@ namespace aux {
if (err != boost::asio::error::operation_aborted
&& m_alerts.should_post<udp_error_alert>())
m_alerts.emplace_alert<udp_error_alert>(ep, err);
m_alerts.emplace_alert<udp_error_alert>(ep
, operation_t::sock_read, err);
#ifndef TORRENT_DISABLE_LOGGING
if (should_log())
@ -5044,9 +5024,7 @@ namespace aux {
if (ec) return bind_ep;
bind_ep.address(bind_socket_to_device(m_io_service, s
, remote_address.is_v4()
? boost::asio::ip::tcp::v4()
: boost::asio::ip::tcp::v6()
, remote_address.is_v4() ? tcp::v4() : tcp::v6()
, ifname.c_str(), bind_ep.port(), ec));
return bind_ep;
}