From 8234d064a89f929cd9a06cf8ef44dcdcbfc8cb84 Mon Sep 17 00:00:00 2001 From: Dan Walters <8643018+sretlawd@users.noreply.github.com> Date: Tue, 26 Oct 2021 20:59:31 -0500 Subject: [PATCH] fix: apply optional peer socket TOS to UDP sockets (#1043) This effectively makes the TOS setting apply to uTP and DHT traffic. Co-authored-by: Charles Kerr --- libtransmission/session.cc | 2 ++ libtransmission/tr-udp.cc | 20 ++++++++++++++++++++ libtransmission/tr-udp.h | 1 + 3 files changed, 23 insertions(+) diff --git a/libtransmission/session.cc b/libtransmission/session.cc index 6375acb97..9812bca55 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -2250,6 +2250,8 @@ static void toggle_utp(void* vsession) tr_udpSetSocketBuffers(session); + tr_udpSetSocketTOS(session); + /* But don't call tr_utpClose -- see reset_timer in tr-utp.c for an explanation. */ } diff --git a/libtransmission/tr-udp.cc b/libtransmission/tr-udp.cc index 4e1f8d738..0bb3c4970 100644 --- a/libtransmission/tr-udp.cc +++ b/libtransmission/tr-udp.cc @@ -124,6 +124,24 @@ void tr_udpSetSocketBuffers(tr_session* session) } } +void tr_udpSetSocketTOS(tr_session* session) +{ + if (session->peerSocketTOS == 0) + { + return; + } + + if (session->udp_socket != TR_BAD_SOCKET) + { + tr_netSetTOS(session->udp_socket, session->peerSocketTOS, TR_AF_INET); + } + + if (session->udp6_socket != TR_BAD_SOCKET) + { + tr_netSetTOS(session->udp6_socket, session->peerSocketTOS, TR_AF_INET6); + } +} + /* BEP-32 has a rather nice explanation of why we need to bind to one IPv6 address, if I may say so myself. */ // TODO: remove goto, it prevents reducing scope of local variables @@ -359,6 +377,8 @@ void tr_udpInit(tr_session* ss) tr_udpSetSocketBuffers(ss); + tr_udpSetSocketTOS(ss); + if (ss->isDHTEnabled) { tr_dhtInit(ss); diff --git a/libtransmission/tr-udp.h b/libtransmission/tr-udp.h index d29862b04..eb037fde5 100644 --- a/libtransmission/tr-udp.h +++ b/libtransmission/tr-udp.h @@ -30,5 +30,6 @@ THE SOFTWARE. void tr_udpInit(tr_session*); void tr_udpUninit(tr_session*); void tr_udpSetSocketBuffers(tr_session*); +void tr_udpSetSocketTOS(tr_session*); bool tau_handle_message(tr_session* session, uint8_t const* msg, size_t msglen);