diff --git a/ChangeLog b/ChangeLog index e29a1c766..912a4771e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * separated address and port fields in listen alerts * added support for parsing new x.pe parameter from BEP 9 * peer_blocked_alert now derives from peer_alert * transitioned exception types to system_error diff --git a/bindings/python/src/alert.cpp b/bindings/python/src/alert.cpp index 9137c8aac..02c578cc7 100644 --- a/bindings/python/src/alert.cpp +++ b/bindings/python/src/alert.cpp @@ -480,7 +480,11 @@ void bind_alert() class_, noncopyable>( "listen_failed_alert", no_init) +#ifndef TORRENT_NO_DEPRECATE .def_readonly("endpoint", &listen_failed_alert::endpoint) +#endif + .def_readonly("address", &listen_failed_alert::address) + .def_readonly("port", &listen_failed_alert::port) .def("listen_interface", &listen_failed_alert::listen_interface) .def_readonly("error", &listen_failed_alert::error) .def_readonly("operation", &listen_failed_alert::operation) @@ -489,7 +493,12 @@ void bind_alert() class_, noncopyable>( "listen_succeeded_alert", no_init) +#ifndef TORRENT_NO_DEPRECATE .def_readonly("endpoint", &listen_succeeded_alert::endpoint) +#endif + .def_readonly("address", &listen_succeeded_alert::address) + .def_readonly("port", &listen_succeeded_alert::port) + .def_readonly("sock_type", &listen_succeeded_alert::sock_type) ; class_, noncopyable>( diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index b82522c70..755fe1e49 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -1242,9 +1242,15 @@ namespace libtorrent }; // This alert is generated when none of the ports, given in the port range, to - // session can be opened for listening. The ``endpoint`` member is the - // interface and port that failed, ``error`` is the error code describing - // the failure. + // session can be opened for listening. The ``listen_interface`` member is the + // interface that failed, ``error`` is the error code describing the failure. + // + // In the case an endpoint was created before generating the alert, it is + // represented by ``address`` and ``port``. The combinations of socket type + // and operation in which such address and port are not valid are: + // accept - i2p + // accept - socks5 + // enum_if - tcp // // libtorrent may sometimes try to listen on port 0, if all other ports failed. // Port 0 asks the operating system to pick a port that's free). If that fails @@ -1255,6 +1261,15 @@ namespace libtorrent enum socket_type_t { tcp, tcp_ssl, udp, i2p, socks5, utp_ssl }; // internal + listen_failed_alert( + aux::stack_allocator& alloc + , std::string const& iface + , libtorrent::address const& listen_addr + , int listen_port + , int op + , error_code const& ec + , socket_type_t t); + listen_failed_alert( aux::stack_allocator& alloc , std::string const& iface @@ -1263,6 +1278,21 @@ namespace libtorrent , error_code const& ec , socket_type_t t); + listen_failed_alert( + aux::stack_allocator& alloc + , std::string const& iface + , udp::endpoint const& ep + , int op + , error_code const& ec + , socket_type_t t); + + listen_failed_alert( + aux::stack_allocator& alloc + , std::string const& iface + , int op + , error_code const& ec + , socket_type_t t); + TORRENT_DEFINE_ALERT_PRIO(listen_failed_alert, 48) static const int static_category = alert::status_notification | alert::error_notification; @@ -1285,8 +1315,18 @@ namespace libtorrent // the type of listen socket this alert refers to. socket_type_t sock_type; + // the address libtorrent attempted to listen on + // see alert's documentation for validity of this value + libtorrent::address address; + + // the port libtorrent attempted to listen on + // see alert's documentation for validity of this value + int port; + +#ifndef TORRENT_NO_DEPRECATE // the address and port libtorrent attempted to listen on tcp::endpoint endpoint; +#endif private: std::reference_wrapper m_alloc; @@ -1301,7 +1341,17 @@ namespace libtorrent enum socket_type_t { tcp, tcp_ssl, udp, i2p, socks5, utp_ssl }; // internal - listen_succeeded_alert(aux::stack_allocator& alloc, tcp::endpoint const& ep + listen_succeeded_alert(aux::stack_allocator& alloc + , libtorrent::address const& listen_addr + , int listen_port + , socket_type_t t); + + listen_succeeded_alert(aux::stack_allocator& alloc + , tcp::endpoint const& ep + , socket_type_t t); + + listen_succeeded_alert(aux::stack_allocator& alloc + , udp::endpoint const& ep , socket_type_t t); TORRENT_DEFINE_ALERT_PRIO(listen_succeeded_alert, 49) @@ -1309,9 +1359,18 @@ namespace libtorrent static const int static_category = alert::status_notification; virtual std::string message() const override; + // the address libtorrent ended up listening on. This address + // refers to the local interface. + libtorrent::address address; + + // the port libtorrent ended up listening on. + int port; + +#ifndef TORRENT_NO_DEPRECATE // the endpoint libtorrent ended up listening on. The address // refers to the local interface and the port is the listen port. tcp::endpoint endpoint; +#endif // the type of listen socket this alert refers to. socket_type_t sock_type; diff --git a/include/libtorrent/socket_io.hpp b/include/libtorrent/socket_io.hpp index 4da8ea093..18a812ae8 100644 --- a/include/libtorrent/socket_io.hpp +++ b/include/libtorrent/socket_io.hpp @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { TORRENT_EXTRA_EXPORT std::string print_address(address const& addr); + TORRENT_EXTRA_EXPORT std::string print_endpoint(address const& addr, int port); TORRENT_EXTRA_EXPORT std::string print_endpoint(tcp::endpoint const& ep); TORRENT_EXTRA_EXPORT std::string print_endpoint(udp::endpoint const& ep); TORRENT_EXTRA_EXPORT tcp::endpoint parse_endpoint(std::string str, error_code& ec); diff --git a/simulation/test_socks5.cpp b/simulation/test_socks5.cpp index 5f1888475..e37ca45f0 100644 --- a/simulation/test_socks5.cpp +++ b/simulation/test_socks5.cpp @@ -200,8 +200,8 @@ TORRENT_TEST(socks4_tcp_listen_alert) { if (a->sock_type == listen_succeeded_alert::socks5) { - TEST_EQUAL(a->endpoint.address(), addr("50.50.50.50")); - TEST_EQUAL(a->endpoint.port(), 6881); + TEST_EQUAL(a->address, addr("50.50.50.50")); + TEST_EQUAL(a->port, 6881); listen_alert = true; } } @@ -230,8 +230,8 @@ TORRENT_TEST(socks5_tcp_listen_alert) { if (a->sock_type == listen_succeeded_alert::socks5) { - TEST_EQUAL(a->endpoint.address(), addr("50.50.50.50")); - TEST_EQUAL(a->endpoint.port(), 6881); + TEST_EQUAL(a->address, addr("50.50.50.50")); + TEST_EQUAL(a->port, 6881); listen_alert = true; } } @@ -267,7 +267,7 @@ TORRENT_TEST(socks5_tcp_announce) { if (a->sock_type == listen_succeeded_alert::socks5) { - alert_port = a->endpoint.port(); + alert_port = a->port; } } }, diff --git a/src/alert.cpp b/src/alert.cpp index b6a9ab307..ecff77dba 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -829,18 +829,70 @@ namespace libtorrent { listen_failed_alert::listen_failed_alert( aux::stack_allocator& alloc , std::string const& iface - , tcp::endpoint const& ep + , libtorrent::address const& listen_addr + , int listen_port , int op , error_code const& ec , socket_type_t t) : error(ec) , operation(op) , sock_type(t) - , endpoint(ep) + , address(listen_addr) + , port(listen_port) +#ifndef TORRENT_NO_DEPRECATE + , endpoint(listen_addr, listen_port) +#endif , m_alloc(alloc) , m_interface_idx(alloc.copy_string(iface)) {} + listen_failed_alert::listen_failed_alert( + aux::stack_allocator& alloc + , std::string const& iface + , tcp::endpoint const& ep + , int op + , error_code const& ec + , socket_type_t t) + : listen_failed_alert(alloc + , iface + , ep.address() + , ep.port() + , op + , ec + , t) + {} + + listen_failed_alert::listen_failed_alert( + aux::stack_allocator& alloc + , std::string const& iface + , udp::endpoint const& ep + , int op + , error_code const& ec + , socket_type_t t) + : listen_failed_alert(alloc + , iface + , ep.address() + , ep.port() + , op + , ec + , t) + {} + + listen_failed_alert::listen_failed_alert( + aux::stack_allocator& alloc + , std::string const& iface + , int op + , error_code const& ec + , socket_type_t t) + : listen_failed_alert(alloc + , iface + , libtorrent::address() + , 0 + , op + , ec + , t) + {} + char const* listen_failed_alert::listen_interface() const { return m_alloc.get().ptr(m_interface_idx); @@ -861,7 +913,7 @@ namespace libtorrent { }; char ret[300]; std::snprintf(ret, sizeof(ret), "listening on %s (device: %s) failed: [%s] [%s] %s" - , print_endpoint(endpoint).c_str() + , print_endpoint(address, port).c_str() , listen_interface() , op_str[operation] , sock_type_str[sock_type] @@ -916,17 +968,41 @@ namespace libtorrent { } listen_succeeded_alert::listen_succeeded_alert(aux::stack_allocator& - , tcp::endpoint const& ep, socket_type_t t) - : endpoint(ep) + , libtorrent::address const& listen_addr + , int listen_port + , socket_type_t t) + : address(listen_addr) + , port(listen_port) +#ifndef TORRENT_NO_DEPRECATE + , endpoint(listen_addr, listen_port) +#endif , sock_type(t) {} + listen_succeeded_alert::listen_succeeded_alert(aux::stack_allocator& alloc + , tcp::endpoint const& ep + , socket_type_t t) + : listen_succeeded_alert(alloc + , ep.address() + , ep.port() + , t) + {} + + listen_succeeded_alert::listen_succeeded_alert(aux::stack_allocator& alloc + , udp::endpoint const& ep + , socket_type_t t) + : listen_succeeded_alert(alloc + , ep.address() + , ep.port() + , t) + {} + std::string listen_succeeded_alert::message() const { char const* type_str[] = { "TCP", "SSL/TCP", "UDP", "i2p", "socks5", "SSL/uTP" }; char ret[200]; std::snprintf(ret, sizeof(ret), "successfully listening on [%s] %s" - , type_str[sock_type], print_endpoint(endpoint).c_str()); + , type_str[sock_type], print_endpoint(address, port).c_str()); return ret; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index ddfc3ca99..dde713544 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1897,7 +1897,7 @@ namespace aux { if (m_alerts.should_post()) { m_alerts.emplace_alert(device - , tcp::endpoint(), listen_failed_alert::enum_if, ec + , listen_failed_alert::enum_if, ec , listen_failed_alert::tcp); } continue; @@ -1964,7 +1964,7 @@ namespace aux { : listen_succeeded_alert::udp; m_alerts.emplace_alert( - tcp::endpoint(udp_ep.address(), udp_ep.port()), socket_type); + udp_ep, socket_type); } } } @@ -2066,7 +2066,7 @@ namespace aux { if (e == boost::asio::error::operation_aborted) return; if (m_alerts.should_post()) m_alerts.emplace_alert("socks5" - , tcp::endpoint(), listen_failed_alert::accept, e + , listen_failed_alert::accept, e , listen_failed_alert::socks5); return; } @@ -2099,7 +2099,7 @@ namespace aux { { if (m_alerts.should_post()) m_alerts.emplace_alert("socks5" - , tcp::endpoint(), listen_failed_alert::accept, e + , listen_failed_alert::accept, e , listen_failed_alert::socks5); return; } @@ -2188,7 +2188,6 @@ namespace aux { if (m_alerts.should_post()) { m_alerts.emplace_alert("i2p" - , tcp::endpoint() , listen_failed_alert::accept , e, listen_failed_alert::i2p); } diff --git a/src/socket_io.cpp b/src/socket_io.cpp index 03e86290c..f058ef472 100644 --- a/src/socket_io.cpp +++ b/src/socket_io.cpp @@ -64,23 +64,27 @@ namespace libtorrent return ret; } - std::string print_endpoint(tcp::endpoint const& ep) + std::string print_endpoint(address const& addr, int port) { error_code ec; char buf[200]; - address const& addr = ep.address(); #if TORRENT_USE_IPV6 if (addr.is_v6()) - std::snprintf(buf, sizeof(buf), "[%s]:%d", addr.to_string(ec).c_str(), ep.port()); + std::snprintf(buf, sizeof(buf), "[%s]:%d", addr.to_string(ec).c_str(), port); else #endif - std::snprintf(buf, sizeof(buf), "%s:%d", addr.to_string(ec).c_str(), ep.port()); + std::snprintf(buf, sizeof(buf), "%s:%d", addr.to_string(ec).c_str(), port); return buf; } + std::string print_endpoint(tcp::endpoint const& ep) + { + return print_endpoint(ep.address(), ep.port()); + } + std::string print_endpoint(udp::endpoint const& ep) { - return print_endpoint(tcp::endpoint(ep.address(), ep.port())); + return print_endpoint(ep.address(), ep.port()); } tcp::endpoint parse_endpoint(std::string str, error_code& ec)