diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 00cb2192f..bff217231 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -1089,14 +1089,14 @@ namespace aux { , int port , span p , error_code& ec - , int flags); + , udp_send_flags_t flags); void send_udp_packet_hostname_listen(aux::listen_socket_handle const& sock , char const* hostname , int port , span p , error_code& ec - , int flags) + , udp_send_flags_t const flags) { listen_socket_t* s = sock.get(); if (!s) @@ -1111,13 +1111,13 @@ namespace aux { , udp::endpoint const& ep , span p , error_code& ec - , int flags); + , udp_send_flags_t flags); void send_udp_packet_listen(aux::listen_socket_handle const& sock , udp::endpoint const& ep , span p , error_code& ec - , int flags) + , udp_send_flags_t const flags) { listen_socket_t* s = sock.get(); if (!s) diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index 03a5a2d7e..44cb15021 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include namespace libtorrent { @@ -62,7 +63,7 @@ namespace libtorrent { namespace dht { { using send_fun_t = std::function, error_code&, int)>; + , span, error_code&, udp_send_flags_t)>; dht_tracker(dht_observer* observer , io_service& ios diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 39d562154..9791da2c1 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -63,6 +63,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/debug.hpp" #include "libtorrent/error_code.hpp" #include "libtorrent/aux_/listen_socket_handle.hpp" +#include "libtorrent/udp_socket.hpp" namespace libtorrent { @@ -341,11 +342,11 @@ namespace libtorrent { typedef std::function - , error_code&, int)> send_fun_t; + , error_code&, udp_send_flags_t)> send_fun_t; typedef std::function - , error_code&, int)> send_fun_hostname_t; + , error_code&, udp_send_flags_t)> send_fun_hostname_t; tracker_manager(send_fun_t const& send_fun , send_fun_hostname_t const& send_fun_hostname @@ -391,11 +392,11 @@ namespace libtorrent { void send_hostname(aux::listen_socket_handle const& sock , char const* hostname, int port, span p - , error_code& ec, int flags = 0); + , error_code& ec, udp_send_flags_t flags = {}); void send(aux::listen_socket_handle const& sock , udp::endpoint const& ep, span p - , error_code& ec, int flags = 0); + , error_code& ec, udp_send_flags_t flags = {}); private: diff --git a/include/libtorrent/udp_socket.hpp b/include/libtorrent/udp_socket.hpp index 3c7deadc8..4293dbf0f 100644 --- a/include/libtorrent/udp_socket.hpp +++ b/include/libtorrent/udp_socket.hpp @@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/proxy_settings.hpp" #include "libtorrent/debug.hpp" #include "libtorrent/span.hpp" +#include "libtorrent/flags.hpp" #include #include @@ -47,17 +48,18 @@ namespace libtorrent { struct socks5; + struct udp_send_flags_tag; + using udp_send_flags_t = flags::bitfield_flag; + class TORRENT_EXTRA_EXPORT udp_socket : single_threaded { public: explicit udp_socket(io_service& ios); - enum flags_t { - peer_connection = 1 - , tracker_connection = 2 - , dont_queue = 4 - , dont_fragment = 8 - }; + static constexpr udp_send_flags_t peer_connection{1}; + static constexpr udp_send_flags_t tracker_connection{2}; + static constexpr udp_send_flags_t dont_queue{4}; + static constexpr udp_send_flags_t dont_fragment{8}; bool is_open() const { return m_abort == false; } io_service& get_io_service() { return m_socket.get_io_service(); } @@ -85,10 +87,10 @@ namespace libtorrent { // this is only valid when using a socks5 proxy void send_hostname(char const* hostname, int port, span p - , error_code& ec, int flags = 0); + , error_code& ec, udp_send_flags_t flags = {}); void send(udp::endpoint const& ep, span p - , error_code& ec, int flags = 0); + , error_code& ec, udp_send_flags_t flags = {}); void open(udp const& protocol, error_code& ec); void bind(udp::endpoint const& ep, error_code& ec); void close(); @@ -136,8 +138,8 @@ namespace libtorrent { udp_socket(udp_socket const&); udp_socket& operator=(udp_socket const&); - void wrap(udp::endpoint const& ep, span p, error_code& ec, int flags); - void wrap(char const* hostname, int port, span p, error_code& ec, int flags); + void wrap(udp::endpoint const& ep, span p, error_code& ec, udp_send_flags_t flags); + void wrap(char const* hostname, int port, span p, error_code& ec, udp_send_flags_t flags); bool unwrap(udp::endpoint& from, span& buf); udp::socket m_socket; diff --git a/include/libtorrent/utp_socket_manager.hpp b/include/libtorrent/utp_socket_manager.hpp index 5296e09b7..af837c3a4 100644 --- a/include/libtorrent/utp_socket_manager.hpp +++ b/include/libtorrent/utp_socket_manager.hpp @@ -59,20 +59,18 @@ namespace libtorrent { struct utp_socket_manager { - typedef std::function + using send_fun_t = std::function , udp::endpoint const& , span - , error_code&, int)> send_fun_t; + , error_code&, udp_send_flags_t)>; - typedef std::function const&)> - incoming_utp_callback_t; + using incoming_utp_callback_t = std::function const&)>; utp_socket_manager(send_fun_t const& send_fun , incoming_utp_callback_t const& cb , io_service& ios , aux::session_settings const& sett - , counters& cnt, void* ssl_context - ); + , counters& cnt, void* ssl_context); ~utp_socket_manager(); // return false if this is not a uTP packet @@ -90,11 +88,9 @@ namespace libtorrent { void tick(time_point now); - // flags for send_packet - enum { dont_fragment = 1 }; void send_packet(std::weak_ptr sock, udp::endpoint const& ep , char const* p, int len - , error_code& ec, int flags = 0); + , error_code& ec, udp_send_flags_t flags = {}); void subscribe_writable(utp_socket_impl* s); void remove_udp_socket(std::weak_ptr sock); diff --git a/simulation/test_dht_rate_limit.cpp b/simulation/test_dht_rate_limit.cpp index 8fc51356e..599ebce8e 100644 --- a/simulation/test_dht_rate_limit.cpp +++ b/simulation/test_dht_rate_limit.cpp @@ -86,7 +86,7 @@ struct obs : dht::dht_observer }; void send_packet(lt::udp_socket& sock, lt::aux::listen_socket_handle const&, udp::endpoint const& ep - , span p, error_code& ec, int flags) + , span p, error_code& ec, udp_send_flags_t const flags) { sock.send(ep, p, ec, flags); } diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index e0eb36cc9..568906e75 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -679,13 +679,13 @@ namespace libtorrent { namespace dht { { return v.first.get_local_endpoint().protocol().family() == addr.protocol().family(); }); if (n != m_nodes.end()) - m_send_fun(n->first, addr, m_send_buf, ec, 0); + m_send_fun(n->first, addr, m_send_buf, ec, {}); else ec = boost::asio::error::address_family_not_supported; } else { - m_send_fun(s, addr, m_send_buf, ec, 0); + m_send_fun(s, addr, m_send_buf, ec, {}); } if (ec) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index edb317377..9a3a4c3b6 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2326,7 +2326,7 @@ namespace { , int const port , span p , error_code& ec - , int const flags) + , udp_send_flags_t const flags) { auto si = sock.lock(); if (!si) @@ -2353,7 +2353,7 @@ namespace { , udp::endpoint const& ep , span p , error_code& ec - , int const flags) + , udp_send_flags_t const flags) { auto si = sock.lock(); if (!si) @@ -5677,7 +5677,7 @@ namespace { , udp::endpoint const& ep , span p , error_code& ec - , int flags) + , udp_send_flags_t const flags) { send_udp_packet_listen(sock, ep, p, ec, flags); } , m_dht_settings , m_stats_counters diff --git a/src/tracker_manager.cpp b/src/tracker_manager.cpp index 99e95a36f..05c04baec 100644 --- a/src/tracker_manager.cpp +++ b/src/tracker_manager.cpp @@ -377,7 +377,7 @@ namespace libtorrent { void tracker_manager::send_hostname(aux::listen_socket_handle const& sock , char const* hostname, int const port - , span p, error_code& ec, int const flags) + , span p, error_code& ec, udp_send_flags_t const flags) { TORRENT_ASSERT(is_single_thread()); m_send_fun_hostname(sock, hostname, port, p, ec, flags); @@ -386,7 +386,7 @@ namespace libtorrent { void tracker_manager::send(aux::listen_socket_handle const& sock , udp::endpoint const& ep , span p - , error_code& ec, int const flags) + , error_code& ec, udp_send_flags_t const flags) { TORRENT_ASSERT(is_single_thread()); m_send_fun(sock, ep, p, ec, flags); diff --git a/src/udp_socket.cpp b/src/udp_socket.cpp index 2b26e2fd7..a6607033a 100644 --- a/src/udp_socket.cpp +++ b/src/udp_socket.cpp @@ -223,7 +223,7 @@ int udp_socket::read(span pkts, error_code& ec) } void udp_socket::send_hostname(char const* hostname, int const port - , span p, error_code& ec, int const flags) + , span p, error_code& ec, udp_send_flags_t const flags) { TORRENT_ASSERT(is_single_thread()); @@ -254,7 +254,7 @@ void udp_socket::send_hostname(char const* hostname, int const port } void udp_socket::send(udp::endpoint const& ep, span p - , error_code& ec, int const flags) + , error_code& ec, udp_send_flags_t const flags) { TORRENT_ASSERT(is_single_thread()); @@ -268,7 +268,7 @@ void udp_socket::send(udp::endpoint const& ep, span p const bool allow_proxy = ((flags & peer_connection) && m_proxy_settings.proxy_peer_connections) || ((flags & tracker_connection) && m_proxy_settings.proxy_tracker_connections) - || (flags & (tracker_connection | peer_connection)) == 0 + || !(flags & (tracker_connection | peer_connection)) ; if (allow_proxy && m_socks5_connection && m_socks5_connection->active()) @@ -281,14 +281,14 @@ void udp_socket::send(udp::endpoint const& ep, span p if (m_force_proxy) return; // set the DF flag for the socket and clear it again in the destructor - set_dont_frag df(m_socket, (flags & dont_fragment) != 0 + set_dont_frag df(m_socket, (flags & dont_fragment) && ep.protocol() == udp::v4()); m_socket.send_to(boost::asio::buffer(p.data(), p.size()), ep, 0, ec); } void udp_socket::wrap(udp::endpoint const& ep, span p - , error_code& ec, int const flags) + , error_code& ec, udp_send_flags_t const flags) { TORRENT_UNUSED(flags); using namespace libtorrent::detail; @@ -306,14 +306,14 @@ void udp_socket::wrap(udp::endpoint const& ep, span p iovec[1] = boost::asio::const_buffer(p.data(), p.size()); // set the DF flag for the socket and clear it again in the destructor - set_dont_frag df(m_socket, (flags & dont_fragment) != 0 + set_dont_frag df(m_socket, (flags & dont_fragment) && ep.protocol() == udp::v4()); m_socket.send_to(iovec, m_socks5_connection->target(), 0, ec); } void udp_socket::wrap(char const* hostname, int const port, span p - , error_code& ec, int const flags) + , error_code& ec, udp_send_flags_t const flags) { TORRENT_UNUSED(flags); using namespace libtorrent::detail; @@ -335,7 +335,7 @@ void udp_socket::wrap(char const* hostname, int const port, span p iovec[1] = boost::asio::const_buffer(p.data(), p.size()); // set the DF flag for the socket and clear it again in the destructor - set_dont_frag df(m_socket, (flags & dont_fragment) != 0 + set_dont_frag df(m_socket, (flags & dont_fragment) && m_socket.local_endpoint(ec).protocol() == udp::v4()); m_socket.send_to(iovec, m_socks5_connection->target(), 0, ec); @@ -762,4 +762,9 @@ void socks5::close() m_timer.cancel(); } +constexpr udp_send_flags_t udp_socket::peer_connection; +constexpr udp_send_flags_t udp_socket::tracker_connection; +constexpr udp_send_flags_t udp_socket::dont_queue; +constexpr udp_send_flags_t udp_socket::dont_fragment; + } diff --git a/src/utp_socket_manager.cpp b/src/utp_socket_manager.cpp index 400d62d91..bbb4347c2 100644 --- a/src/utp_socket_manager.cpp +++ b/src/utp_socket_manager.cpp @@ -140,7 +140,7 @@ namespace libtorrent { void utp_socket_manager::send_packet(std::weak_ptr sock , udp::endpoint const& ep, char const* p - , int const len, error_code& ec, int flags) + , int const len, error_code& ec, udp_send_flags_t const flags) { #if !defined TORRENT_HAS_DONT_FRAGMENT && !defined TORRENT_DEBUG_MTU TORRENT_UNUSED(flags); @@ -152,7 +152,7 @@ namespace libtorrent { #endif m_send_fun(sock, ep, {p, std::size_t(len)}, ec - , ((flags & dont_fragment) ? udp_socket::dont_fragment : 0) + , (flags & udp_socket::dont_fragment) | udp_socket::peer_connection); } diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index 4b84717f5..30e20571c 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -1964,7 +1964,7 @@ bool utp_socket_impl::send_pkt(int const flags) error_code ec; m_sm.send_packet(m_sock, udp::endpoint(m_remote_address, m_port) , reinterpret_cast(h), p->size, ec - , p->mtu_probe ? utp_socket_manager::dont_fragment : 0); + , p->mtu_probe ? udp_socket::dont_fragment : udp_send_flags_t{}); ++m_out_packets; m_sm.inc_stats_counter(counters::utp_packets_out); @@ -1992,7 +1992,7 @@ bool utp_socket_impl::send_pkt(int const flags) UTP_LOGV("%8p: re-sending\n", static_cast(this)); #endif m_sm.send_packet(m_sock, udp::endpoint(m_remote_address, m_port) - , reinterpret_cast(h), p->size, ec, 0); + , reinterpret_cast(h), p->size, ec, {}); } if (ec == error::would_block || ec == error::try_again)