2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2017-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef __TRANSMISSION__
|
|
|
|
#error only libtransmission should #include this header.
|
|
|
|
#endif
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
#include <atomic>
|
2023-06-30 04:51:55 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-12-06 16:28:28 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <utility> // for std::make_pair()
|
|
|
|
|
2022-12-06 00:53:31 +00:00
|
|
|
#include "transmission.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
#include "error.h"
|
2022-12-06 00:53:31 +00:00
|
|
|
#include "net.h"
|
|
|
|
#include "tr-assert.h"
|
2022-12-16 07:23:12 +00:00
|
|
|
#include "tr-buffer.h"
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-12-06 00:53:31 +00:00
|
|
|
struct UTPSocket;
|
|
|
|
struct tr_session;
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-12-06 16:28:28 +00:00
|
|
|
class tr_peer_socket
|
2017-06-28 15:46:06 +00:00
|
|
|
{
|
2022-12-06 16:28:28 +00:00
|
|
|
public:
|
2023-06-25 17:33:32 +00:00
|
|
|
using InBuf = libtransmission::BufferWriter<std::byte>;
|
|
|
|
using OutBuf = libtransmission::BufferReader<std::byte>;
|
2023-06-22 23:42:48 +00:00
|
|
|
|
2022-12-06 00:53:31 +00:00
|
|
|
tr_peer_socket() = default;
|
2022-12-23 16:56:27 +00:00
|
|
|
tr_peer_socket(tr_session const* session, tr_address const& address, tr_port port, tr_socket_t sock);
|
2022-12-06 16:28:28 +00:00
|
|
|
tr_peer_socket(tr_address const& address, tr_port port, struct UTPSocket* const sock);
|
2023-01-20 08:28:56 +00:00
|
|
|
tr_peer_socket(tr_peer_socket&& s)
|
|
|
|
{
|
|
|
|
*this = std::move(s);
|
|
|
|
}
|
2022-12-06 16:28:28 +00:00
|
|
|
tr_peer_socket(tr_peer_socket const&) = delete;
|
2023-01-20 08:28:56 +00:00
|
|
|
tr_peer_socket& operator=(tr_peer_socket&& s)
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
handle = s.handle;
|
|
|
|
address_ = s.address_;
|
|
|
|
port_ = s.port_;
|
|
|
|
type_ = s.type_;
|
|
|
|
// invalidate s.type_, s.handle so s.close() won't break anything
|
|
|
|
s.type_ = Type::None;
|
|
|
|
s.handle = {};
|
|
|
|
return *this;
|
|
|
|
}
|
2022-12-06 16:28:28 +00:00
|
|
|
tr_peer_socket& operator=(tr_peer_socket const&) = delete;
|
2023-01-20 08:28:56 +00:00
|
|
|
~tr_peer_socket()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
2023-01-04 21:37:55 +00:00
|
|
|
void close();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-06-25 17:33:32 +00:00
|
|
|
size_t try_read(InBuf& buf, size_t max, bool buf_is_empty, tr_error** error) const;
|
|
|
|
size_t try_write(OutBuf& buf, size_t max, tr_error** error) const;
|
2022-12-16 07:23:12 +00:00
|
|
|
|
2022-12-06 00:53:31 +00:00
|
|
|
[[nodiscard]] constexpr std::pair<tr_address, tr_port> socketAddress() const noexcept
|
|
|
|
{
|
|
|
|
return std::make_pair(address_, port_);
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-12-06 00:53:31 +00:00
|
|
|
[[nodiscard]] constexpr auto const& address() const noexcept
|
|
|
|
{
|
|
|
|
return address_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& port() const noexcept
|
|
|
|
{
|
|
|
|
return port_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputIt>
|
2022-12-08 22:44:19 +00:00
|
|
|
OutputIt display_name(OutputIt out)
|
2022-12-06 00:53:31 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return address_.display_name(out, port_);
|
2022-12-06 00:53:31 +00:00
|
|
|
}
|
2022-02-19 18:10:43 +00:00
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
[[nodiscard]] std::string_view display_name(char* out, size_t outlen) const
|
2022-12-06 00:53:31 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return address_.display_name(out, outlen, port_);
|
2022-12-06 00:53:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
[[nodiscard]] std::string display_name() const
|
2022-12-06 00:53:31 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return address_.display_name(port_);
|
2022-12-06 00:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_utp() const noexcept
|
|
|
|
{
|
|
|
|
return type_ == Type::UTP;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_tcp() const noexcept
|
|
|
|
{
|
|
|
|
return type_ == Type::TCP;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_valid() const noexcept
|
|
|
|
{
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
return is_tcp() || is_utp();
|
|
|
|
#else
|
|
|
|
return is_tcp();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
[[nodiscard]] constexpr size_t guess_packet_overhead(size_t n_bytes) const noexcept
|
|
|
|
{
|
|
|
|
if (is_tcp())
|
|
|
|
{
|
|
|
|
// https://web.archive.org/web/20140912230020/http://sd.wareonearth.com:80/~phil/net/overhead/
|
|
|
|
// TCP over Ethernet:
|
|
|
|
// Assuming no header compression (e.g. not PPP)
|
|
|
|
// Add 20 IPv4 header or 40 IPv6 header (no options)
|
|
|
|
// Add 20 TCP header
|
|
|
|
// Add 12 bytes optional TCP timestamps
|
|
|
|
// Max TCP Payload data rates over ethernet are thus:
|
|
|
|
// (1500-40)/ (38+1500) = 94.9285 % IPv4, minimal headers
|
|
|
|
// (1500-52)/ (38+1500) = 94.1482 % IPv4, TCP timestamps
|
|
|
|
// (1500-52)/ (42+1500) = 93.9040 % 802.1q, IPv4, TCP timestamps
|
|
|
|
// (1500-60)/ (38+1500) = 93.6281 % IPv6, minimal headers
|
|
|
|
// (1500-72)/ (38+1500) = 92.8479 % IPv6, TCP timestamps
|
|
|
|
// (1500-72)/ (42+1500) = 92.6070 % 802.1q, IPv6, TCP timestamps
|
|
|
|
|
|
|
|
// So, let's guess around 7% overhead
|
|
|
|
return n_bytes / 14U;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only guess for TCP; uTP tracks its overhead via UTP_ON_OVERHEAD_STATISTICS
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-06 00:53:31 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
tr_socket_t tcp;
|
|
|
|
struct UTPSocket* utp;
|
|
|
|
} handle = {};
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
[[nodiscard]] static bool limit_reached(tr_session* const session) noexcept;
|
|
|
|
|
2022-12-06 00:53:31 +00:00
|
|
|
private:
|
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
TCP,
|
|
|
|
UTP
|
|
|
|
};
|
|
|
|
|
|
|
|
tr_address address_;
|
|
|
|
tr_port port_;
|
|
|
|
|
|
|
|
enum Type type_ = Type::None;
|
2023-01-04 21:37:55 +00:00
|
|
|
|
|
|
|
static inline std::atomic<size_t> n_open_sockets_ = {};
|
2022-12-06 00:53:31 +00:00
|
|
|
};
|
|
|
|
|
2022-12-06 16:28:28 +00:00
|
|
|
tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const& addr, tr_port port, bool client_is_seed);
|