2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-12-06 00:53:31 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <algorithm> // std::min
|
|
|
|
#include <cerrno>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <cstddef> // std::byte
|
2023-11-03 17:03:26 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-12-06 16:28:28 +00:00
|
|
|
|
2022-12-06 00:53:31 +00:00
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/error.h"
|
|
|
|
#include "libtransmission/log.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/net.h"
|
2023-11-21 15:02:03 +00:00
|
|
|
#include "libtransmission/peer-socket.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/session.h"
|
2023-11-21 15:02:03 +00:00
|
|
|
#include "libtransmission/tr-assert.h"
|
2022-12-06 16:28:28 +00:00
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
#define tr_logAddErrorIo(io, msg) tr_logAddError(msg, (io)->display_name())
|
|
|
|
#define tr_logAddWarnIo(io, msg) tr_logAddWarn(msg, (io)->display_name())
|
|
|
|
#define tr_logAddDebugIo(io, msg) tr_logAddDebug(msg, (io)->display_name())
|
|
|
|
#define tr_logAddTraceIo(io, msg) tr_logAddTrace(msg, (io)->display_name())
|
2022-12-06 16:28:28 +00:00
|
|
|
|
2023-07-12 22:29:47 +00:00
|
|
|
tr_peer_socket::tr_peer_socket(tr_session const* session, tr_socket_address const& socket_address, tr_socket_t sock)
|
2022-12-06 16:28:28 +00:00
|
|
|
: handle{ sock }
|
2023-07-12 22:29:47 +00:00
|
|
|
, socket_address_{ socket_address }
|
2022-12-06 16:28:28 +00:00
|
|
|
, type_{ Type::TCP }
|
|
|
|
{
|
|
|
|
TR_ASSERT(sock != TR_BAD_SOCKET);
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
++n_open_sockets_;
|
2023-07-05 01:29:12 +00:00
|
|
|
session->setSocketTOS(sock, address().type);
|
2022-12-06 16:28:28 +00:00
|
|
|
|
|
|
|
if (auto const& algo = session->peerCongestionAlgorithm(); !std::empty(algo))
|
|
|
|
{
|
|
|
|
tr_netSetCongestionControl(sock, algo.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_logAddTraceIo(this, fmt::format("socket (tcp) is {}", handle.tcp));
|
|
|
|
}
|
|
|
|
|
2023-07-12 22:29:47 +00:00
|
|
|
tr_peer_socket::tr_peer_socket(tr_socket_address const& socket_address, struct UTPSocket* const sock)
|
|
|
|
: socket_address_{ socket_address }
|
2022-12-06 16:28:28 +00:00
|
|
|
, type_{ Type::UTP }
|
|
|
|
{
|
|
|
|
TR_ASSERT(sock != nullptr);
|
2023-01-04 21:37:55 +00:00
|
|
|
|
|
|
|
++n_open_sockets_;
|
2022-12-06 16:28:28 +00:00
|
|
|
handle.utp = sock;
|
|
|
|
|
|
|
|
tr_logAddTraceIo(this, fmt::format("socket (µTP) is {}", fmt::ptr(handle.utp)));
|
|
|
|
}
|
2022-12-06 00:53:31 +00:00
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
void tr_peer_socket::close()
|
2022-12-06 00:53:31 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (is_tcp() && (handle.tcp != TR_BAD_SOCKET))
|
2022-12-06 00:53:31 +00:00
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
--n_open_sockets_;
|
|
|
|
tr_net_close_socket(handle.tcp);
|
2022-12-06 00:53:31 +00:00
|
|
|
}
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
else if (is_utp())
|
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
--n_open_sockets_;
|
2022-12-06 00:53:31 +00:00
|
|
|
utp_set_userdata(handle.utp, nullptr);
|
|
|
|
utp_close(handle.utp);
|
|
|
|
}
|
|
|
|
#endif
|
2022-12-06 16:28:28 +00:00
|
|
|
|
|
|
|
type_ = Type::None;
|
|
|
|
handle = {};
|
2022-12-06 00:53:31 +00:00
|
|
|
}
|
2022-12-16 07:23:12 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
size_t tr_peer_socket::try_write(OutBuf& buf, size_t max, tr_error* error) const
|
2022-12-16 07:23:12 +00:00
|
|
|
{
|
|
|
|
if (max == size_t{})
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_tcp())
|
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
return buf.to_socket(handle.tcp, max, error);
|
2022-12-16 07:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
if (is_utp())
|
|
|
|
{
|
|
|
|
errno = 0;
|
2023-05-18 21:56:29 +00:00
|
|
|
// NB: utp_write() does not modify its 2nd arg, but a wart in
|
|
|
|
// libutp's public API requires it to be non-const anyway :shrug:
|
|
|
|
auto const n_written = utp_write(handle.utp, const_cast<std::byte*>(std::data(buf)), std::min(std::size(buf), max));
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
|
2022-12-18 18:09:02 +00:00
|
|
|
if (n_written > 0)
|
2022-12-16 07:23:12 +00:00
|
|
|
{
|
2022-12-18 18:09:02 +00:00
|
|
|
buf.drain(n_written);
|
|
|
|
return static_cast<size_t>(n_written);
|
2022-12-16 07:23:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error != nullptr && n_written < 0 && error_code != 0)
|
2022-12-16 07:23:12 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set_from_errno(error_code);
|
2022-12-16 07:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
size_t tr_peer_socket::try_read(InBuf& buf, size_t max, [[maybe_unused]] bool buf_is_empty, tr_error* error) const
|
2022-12-16 07:23:12 +00:00
|
|
|
{
|
|
|
|
if (max == size_t{})
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_tcp())
|
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
return buf.add_socket(handle.tcp, max, error);
|
2022-12-16 07:23:12 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 06:33:53 +00:00
|
|
|
// Unlike conventional BSD-style socket API, libutp pushes incoming data to the
|
|
|
|
// caller via a callback, instead of allowing the caller to pull data from
|
|
|
|
// its buffers. Therefore, reading data from a uTP socket is not handled here.
|
2022-12-16 07:23:12 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2023-01-04 21:37:55 +00:00
|
|
|
|
2023-10-24 19:24:52 +00:00
|
|
|
bool tr_peer_socket::limit_reached(tr_session const* const session) noexcept
|
2023-01-04 21:37:55 +00:00
|
|
|
{
|
|
|
|
return n_open_sockets_.load() >= session->peerLimit();
|
|
|
|
}
|