make udp send flags type safe

This commit is contained in:
arvidn 2017-07-26 10:38:40 -07:00 committed by Arvid Norberg
parent e19736f925
commit 8d0f38930e
12 changed files with 53 additions and 48 deletions

View File

@ -1089,14 +1089,14 @@ namespace aux {
, int port
, span<char const> 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<char const> 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<char const> 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<char const> p
, error_code& ec
, int flags)
, udp_send_flags_t const flags)
{
listen_socket_t* s = sock.get();
if (!s)

View File

@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/deadline_timer.hpp>
#include <libtorrent/span.hpp>
#include <libtorrent/io_service.hpp>
#include <libtorrent/udp_socket.hpp>
namespace libtorrent {
@ -62,7 +63,7 @@ namespace libtorrent { namespace dht {
{
using send_fun_t = std::function<void(
aux::listen_socket_handle const&, udp::endpoint const&
, span<char const>, error_code&, int)>;
, span<char const>, error_code&, udp_send_flags_t)>;
dht_tracker(dht_observer* observer
, io_service& ios

View File

@ -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<void(aux::listen_socket_handle const&
, udp::endpoint const&
, span<char const>
, error_code&, int)> send_fun_t;
, error_code&, udp_send_flags_t)> send_fun_t;
typedef std::function<void(aux::listen_socket_handle const&
, char const*, int
, span<char const>
, 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<char const> 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<char const> p
, error_code& ec, int flags = 0);
, error_code& ec, udp_send_flags_t flags = {});
private:

View File

@ -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 <array>
#include <memory>
@ -47,17 +48,18 @@ namespace libtorrent {
struct socks5;
struct udp_send_flags_tag;
using udp_send_flags_t = flags::bitfield_flag<std::uint8_t, udp_send_flags_tag>;
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<char const> p
, error_code& ec, int flags = 0);
, error_code& ec, udp_send_flags_t flags = {});
void send(udp::endpoint const& ep, span<char const> 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<char const> p, error_code& ec, int flags);
void wrap(char const* hostname, int port, span<char const> p, error_code& ec, int flags);
void wrap(udp::endpoint const& ep, span<char const> p, error_code& ec, udp_send_flags_t flags);
void wrap(char const* hostname, int port, span<char const> p, error_code& ec, udp_send_flags_t flags);
bool unwrap(udp::endpoint& from, span<char>& buf);
udp::socket m_socket;

View File

@ -59,20 +59,18 @@ namespace libtorrent {
struct utp_socket_manager
{
typedef std::function<void(std::weak_ptr<utp_socket_interface>
using send_fun_t = std::function<void(std::weak_ptr<utp_socket_interface>
, udp::endpoint const&
, span<char const>
, error_code&, int)> send_fun_t;
, error_code&, udp_send_flags_t)>;
typedef std::function<void(std::shared_ptr<socket_type> const&)>
incoming_utp_callback_t;
using incoming_utp_callback_t = std::function<void(std::shared_ptr<socket_type> 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<utp_socket_interface> 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<utp_socket_interface> sock);

View File

@ -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<char const> p, error_code& ec, int flags)
, span<char const> p, error_code& ec, udp_send_flags_t const flags)
{
sock.send(ep, p, ec, flags);
}

View File

@ -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)

View File

@ -2326,7 +2326,7 @@ namespace {
, int const port
, span<char const> 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<char const> 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<char const> 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

View File

@ -377,7 +377,7 @@ namespace libtorrent {
void tracker_manager::send_hostname(aux::listen_socket_handle const& sock
, char const* hostname, int const port
, span<char const> p, error_code& ec, int const flags)
, span<char const> 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<char const> 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);

View File

@ -223,7 +223,7 @@ int udp_socket::read(span<packet> pkts, error_code& ec)
}
void udp_socket::send_hostname(char const* hostname, int const port
, span<char const> p, error_code& ec, int const flags)
, span<char const> 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<char const> 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<char const> 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<char const> 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<char const> 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<char const> 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<char const> 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<char const> 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;
}

View File

@ -140,7 +140,7 @@ namespace libtorrent {
void utp_socket_manager::send_packet(std::weak_ptr<utp_socket_interface> 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);
}

View File

@ -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<char const*>(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<void*>(this));
#endif
m_sm.send_packet(m_sock, udp::endpoint(m_remote_address, m_port)
, reinterpret_cast<char const*>(h), p->size, ec, 0);
, reinterpret_cast<char const*>(h), p->size, ec, {});
}
if (ec == error::would_block || ec == error::try_again)