diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 786045f9c..7e1898329 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -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 endpoint; + // the operation that failed + operation_t operation; + // the error code describing the error error_code const error; }; diff --git a/include/libtorrent/enum_net.hpp b/include/libtorrent/enum_net.hpp index cbfe0bbaa..9d6a490ef 100644 --- a/include/libtorrent/enum_net.hpp +++ b/include/libtorrent/enum_net.hpp @@ -105,7 +105,7 @@ namespace libtorrent { // TODO: 3 use string_view for device_name template 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)); diff --git a/src/alert.cpp b/src/alert.cpp index af5518f80..8b5854e3d 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -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& diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 29b9af72f..76363caea 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -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()) - { - m_alerts.emplace_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()) - { - m_alerts.emplace_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()) - m_alerts.emplace_alert(ret->udp_sock->sock.local_endpoint(ec), err); + m_alerts.emplace_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()) - m_alerts.emplace_alert(udp_bind_ep, ec); + m_alerts.emplace_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()) - m_alerts.emplace_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()) - m_alerts.emplace_alert(udp_bind_ep, ec); + m_alerts.emplace_alert(udp_bind_ep + , operation_t::sock_bind, ec); continue; } @@ -2087,7 +2064,8 @@ namespace aux { if (err) { if (m_alerts.should_post()) - m_alerts.emplace_alert(udp_sock->sock.local_endpoint(ec), err); + m_alerts.emplace_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()) { - m_alerts.emplace_alert(ep, ec); + m_alerts.emplace_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()) - m_alerts.emplace_alert(ep, err); + m_alerts.emplace_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; }