Skip to content

Commit

Permalink
set DSCP_TRAFFIC_TYPE socket option on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jan 9, 2022
1 parent 7c97c5e commit dba0b90
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 44 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ set(libtorrent_aux_include_files
session_settings.hpp
session_udp_sockets.hpp
set_socket_buffer.hpp
set_traffic_class.hpp
socket_type.hpp
storage_piece_set.hpp
storage_utils.hpp
Expand Down
1 change: 1 addition & 0 deletions include/libtorrent/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ nobase_include_HEADERS = \
aux_/session_settings.hpp \
aux_/session_udp_sockets.hpp \
aux_/set_socket_buffer.hpp \
aux_/set_traffic_class.hpp \
aux_/proxy_settings.hpp \
aux_/session_interface.hpp \
aux_/suggest_piece.hpp \
Expand Down
60 changes: 60 additions & 0 deletions include/libtorrent/aux_/set_traffic_class.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright (c) 2022, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef TORRENT_SET_TRAFFIC_CLASS_HPP
#define TORRENT_SET_TRAFFIC_CLASS_HPP

#include "libtorrent/error_code.hpp"
#include "libtorrent/socket.hpp"

namespace libtorrent {
namespace aux {

template <typename Socket>
void set_traffic_class(Socket& s, int v, error_code& ec)
{
#ifdef IP_DSCP_TRAFFIC_TYPE
s.set_option(dscp_traffic_type((v & 0xff) >> 2), ec);
if (!ec) return;
ec.clear();
#endif
#if defined IPV6_TCLASS
if (is_v6(s.local_endpoint(ec)))
s.set_option(traffic_class(v & 0xfc), ec);
else if (!ec)
#endif
s.set_option(type_of_service(v & 0xfc), ec);
}

}}

#endif
16 changes: 16 additions & 0 deletions include/libtorrent/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ namespace libtorrent {
tos_t m_value;
};

#ifdef IP_DSCP_TRAFFIC_TYPE
struct dscp_traffic_type
{
explicit dscp_traffic_type(char val) : m_value(DWORD(val)) {}
template<class Protocol>
int level(Protocol const&) const { return IPPROTO_IP; }
template<class Protocol>
int name(Protocol const&) const { return IP_DSCP_TRAFFIC_TYPE; }
template<class Protocol>
DWORD const* data(Protocol const&) const { return &m_value; }
template<class Protocol>
size_t size(Protocol const&) const { return sizeof(m_value); }
DWORD m_value;
};
#endif

#if defined IP_DONTFRAG || defined IP_MTU_DISCOVER || defined IP_DONTFRAGMENT
#define TORRENT_HAS_DONT_FRAGMENT
#endif
Expand Down
40 changes: 11 additions & 29 deletions src/peer_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/buffer.hpp"
#include "libtorrent/aux_/array.hpp"
#include "libtorrent/aux_/set_socket_buffer.hpp"
#include "libtorrent/aux_/set_traffic_class.hpp"

#if TORRENT_USE_ASSERTS
#include <set>
Expand Down Expand Up @@ -346,23 +347,18 @@ namespace libtorrent {
disconnect(ec, operation_t::getname);
return;
}
if (is_v4(m_remote) && m_settings.get_int(settings_pack::peer_tos) != 0)
if (m_settings.get_int(settings_pack::peer_tos) != 0)
{
m_socket->set_option(type_of_service(char(m_settings.get_int(settings_pack::peer_tos))), ec);
int const tos = m_settings.get_int(settings_pack::peer_tos);
aux::set_traffic_class(*m_socket, tos, ec);
#ifndef TORRENT_DISABLE_LOGGING
if (should_log(peer_log_alert::outgoing))
if (ec && should_log(peer_log_alert::outgoing))
{
peer_log(peer_log_alert::outgoing, "SET_TOS", "tos: %d e: %s"
, m_settings.get_int(settings_pack::peer_tos), ec.message().c_str());
, tos, ec.message().c_str());
}
#endif
}
#if defined IPV6_TCLASS
else if (is_v6(m_remote) && m_settings.get_int(settings_pack::peer_tos) != 0)
{
m_socket->set_option(traffic_class(char(m_settings.get_int(settings_pack::peer_tos))), ec);
}
#endif
}

#ifndef TORRENT_DISABLE_LOGGING
Expand Down Expand Up @@ -6189,32 +6185,18 @@ namespace libtorrent {
return;
}

if (is_v4(m_remote) && m_settings.get_int(settings_pack::peer_tos) != 0)
if (m_settings.get_int(settings_pack::peer_tos) != 0)
{
error_code err;
m_socket->set_option(type_of_service(char(m_settings.get_int(settings_pack::peer_tos))), err);
int const tos = m_settings.get_int(settings_pack::peer_tos);
aux::set_traffic_class(*m_socket, tos, ec);
#ifndef TORRENT_DISABLE_LOGGING
if (should_log(peer_log_alert::outgoing))
if (ec && should_log(peer_log_alert::outgoing))
{
peer_log(peer_log_alert::outgoing, "SET_TOS", "tos: %d e: %s"
, m_settings.get_int(settings_pack::peer_tos), err.message().c_str());
, tos, ec.message().c_str());
}
#endif
}
#if defined IPV6_TCLASS
else if (is_v6(m_remote) && m_settings.get_int(settings_pack::peer_tos) != 0)
{
error_code err;
m_socket->set_option(traffic_class(char(m_settings.get_int(settings_pack::peer_tos))), err);
#ifndef TORRENT_DISABLE_LOGGING
if (should_log(peer_log_alert::outgoing))
{
peer_log(peer_log_alert::outgoing, "SET_TOS", "tos: %d e: %s"
, m_settings.get_int(settings_pack::peer_tos), err.message().c_str());
}
#endif
}
#endif

#ifndef TORRENT_DISABLE_EXTENSIONS
for (auto const& ext : m_extensions)
Expand Down
18 changes: 3 additions & 15 deletions src/session_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/set_socket_buffer.hpp"
#include "libtorrent/aux_/generate_peer_id.hpp"
#include "libtorrent/aux_/ffs.hpp"
#include "libtorrent/aux_/set_traffic_class.hpp"

#ifndef TORRENT_DISABLE_LOGGING

Expand Down Expand Up @@ -6337,19 +6338,6 @@ namespace {
#endif // DEPRECATE


namespace {
template <typename Socket>
void set_tos(Socket& s, int v, error_code& ec)
{
#if defined IPV6_TCLASS
if (is_v6(s.local_endpoint(ec)))
s.set_option(traffic_class(char(v)), ec);
else if (!ec)
#endif
s.set_option(type_of_service(char(v)), ec);
}
}

// TODO: 2 this should be factored into the udp socket, so we only have the
// code once
void session_impl::update_peer_tos()
Expand All @@ -6360,7 +6348,7 @@ namespace {
if (l->sock)
{
error_code ec;
set_tos(*l->sock, tos, ec);
set_traffic_class(*l->sock, tos, ec);

#ifndef TORRENT_DISABLE_LOGGING
if (should_log())
Expand All @@ -6375,7 +6363,7 @@ namespace {
if (l->udp_sock)
{
error_code ec;
set_tos(l->udp_sock->sock, tos, ec);
set_traffic_class(l->udp_sock->sock, tos, ec);

#ifndef TORRENT_DISABLE_LOGGING
if (should_log())
Expand Down

0 comments on commit dba0b90

Please sign in to comment.