2023-02-11 20:49:42 +00:00
|
|
|
|
// This file Copyright © 2010-2023 Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
|
// License text can be found in the licenses/ folder.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
|
#include <algorithm>
|
2021-11-15 23:03:55 +00:00
|
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
|
#include <cerrno>
|
|
|
|
|
#include <climits>
|
2022-04-22 21:15:06 +00:00
|
|
|
|
#include <cstdint>
|
2021-10-17 20:17:18 +00:00
|
|
|
|
#include <cstring>
|
2021-12-15 21:25:42 +00:00
|
|
|
|
#include <ctime>
|
2022-05-27 01:29:10 +00:00
|
|
|
|
#include <iterator> // std::back_inserter
|
2021-12-15 21:25:42 +00:00
|
|
|
|
#include <string_view>
|
2022-02-10 21:35:28 +00:00
|
|
|
|
#include <utility> // std::pair
|
2007-07-29 18:11:21 +00:00
|
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
|
#include <sys/types.h>
|
2007-08-02 19:43:29 +00:00
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
#include <ws2tcpip.h>
|
2011-04-02 16:43:17 +00:00
|
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
#include <netinet/tcp.h> /* TCP_CONGESTION */
|
2007-08-02 19:43:29 +00:00
|
|
|
|
#endif
|
2007-07-14 16:29:21 +00:00
|
|
|
|
|
2010-12-24 08:58:41 +00:00
|
|
|
|
#include <event2/util.h>
|
2011-03-24 22:57:39 +00:00
|
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
|
#include <fmt/core.h>
|
|
|
|
|
|
2012-05-30 17:47:29 +00:00
|
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
|
#include "libtransmission/net.h"
|
|
|
|
|
#include "libtransmission/peer-socket.h"
|
|
|
|
|
#include "libtransmission/session.h"
|
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
|
#include "libtransmission/tr-macros.h"
|
|
|
|
|
#include "libtransmission/tr-utp.h"
|
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
|
#include "libtransmission/variant.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
|
|
2022-12-25 13:55:51 +00:00
|
|
|
|
using namespace std::literals;
|
|
|
|
|
|
2009-01-02 21:45:55 +00:00
|
|
|
|
#ifndef IN_MULTICAST
|
2021-08-15 09:41:48 +00:00
|
|
|
|
#define IN_MULTICAST(a) (((a)&0xf0000000) == 0xe0000000)
|
2009-01-02 21:45:55 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-25 00:53:01 +00:00
|
|
|
|
std::string tr_net_strerror(int err)
|
2010-06-30 21:24:36 +00:00
|
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2022-02-25 00:53:01 +00:00
|
|
|
|
auto buf = std::array<char, 512>{};
|
2023-01-24 01:24:52 +00:00
|
|
|
|
(void)FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, 0, std::data(buf), std::size(buf), nullptr);
|
2022-02-25 00:53:01 +00:00
|
|
|
|
return std::string{ tr_strvStrip(std::data(buf)) };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2022-02-25 00:53:01 +00:00
|
|
|
|
return std::string{ tr_strerror(err) };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
|
// - TCP Sockets
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
[[nodiscard]] std::optional<tr_tos_t> tr_tos_t::from_string(std::string_view name)
|
2022-02-10 21:35:28 +00:00
|
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
|
auto const needle = tr_strlower(tr_strvStrip(name));
|
|
|
|
|
|
|
|
|
|
for (auto const& [value, key] : Names)
|
2022-02-10 21:35:28 +00:00
|
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
|
if (needle == key)
|
|
|
|
|
{
|
|
|
|
|
return tr_tos_t(value);
|
|
|
|
|
}
|
2022-11-02 14:04:22 +00:00
|
|
|
|
}
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
|
|
if (auto value = tr_parseNum<int>(needle); value)
|
|
|
|
|
{
|
|
|
|
|
return tr_tos_t(*value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
2022-02-10 21:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
|
std::string tr_tos_t::toString() const
|
2022-02-10 21:35:28 +00:00
|
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
|
for (auto const& [value, key] : Names)
|
2022-02-10 21:35:28 +00:00
|
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
|
if (value_ == value)
|
|
|
|
|
{
|
|
|
|
|
return std::string{ key };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::to_string(value_);
|
2022-02-10 21:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
|
void tr_netSetTOS([[maybe_unused]] tr_socket_t s, [[maybe_unused]] int tos, tr_address_type type)
|
2008-04-12 00:29:49 +00:00
|
|
|
|
{
|
2022-02-10 21:35:28 +00:00
|
|
|
|
if (s == TR_BAD_SOCKET)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-02 10:01:39 +00:00
|
|
|
|
if (type == TR_AF_INET)
|
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
#if defined(IP_TOS) && !defined(_WIN32)
|
|
|
|
|
|
2017-08-02 10:01:39 +00:00
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_TOS, (void const*)&tos, sizeof(tos)) == -1)
|
|
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
|
tr_logAddDebug(fmt::format("Can't set TOS '{}': {}", tos, tr_net_strerror(sockerrno)));
|
2017-08-02 10:01:39 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2015-07-01 00:54:41 +00:00
|
|
|
|
}
|
2017-08-02 10:01:39 +00:00
|
|
|
|
else if (type == TR_AF_INET6)
|
|
|
|
|
{
|
|
|
|
|
#if defined(IPV6_TCLASS) && !defined(_WIN32)
|
2019-07-21 11:09:04 +00:00
|
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, (void const*)&tos, sizeof(tos)) == -1)
|
2017-08-02 10:01:39 +00:00
|
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
|
tr_logAddDebug(fmt::format("Can't set IPv6 QoS '{}': {}", tos, tr_net_strerror(sockerrno)));
|
2017-08-02 10:01:39 +00:00
|
|
|
|
}
|
2008-04-12 00:29:49 +00:00
|
|
|
|
#endif
|
2017-08-02 10:01:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* program should never reach here! */
|
2022-03-15 14:52:16 +00:00
|
|
|
|
tr_logAddDebug("Something goes wrong while setting TOS/Traffic-Class");
|
2017-08-02 10:01:39 +00:00
|
|
|
|
}
|
2008-04-12 00:29:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
|
void tr_netSetCongestionControl([[maybe_unused]] tr_socket_t s, [[maybe_unused]] char const* algorithm)
|
2010-04-22 01:49:16 +00:00
|
|
|
|
{
|
|
|
|
|
#ifdef TCP_CONGESTION
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_CONGESTION, (void const*)algorithm, strlen(algorithm) + 1) == -1)
|
2015-07-01 00:54:41 +00:00
|
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
|
tr_logAddDebug(fmt::format("Can't set congestion control algorithm '{}': {}", algorithm, tr_net_strerror(sockerrno)));
|
2015-07-01 00:54:41 +00:00
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2010-04-22 01:49:16 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
|
static tr_socket_t createSocket(int domain, int type)
|
2008-12-15 00:17:08 +00:00
|
|
|
|
{
|
2022-04-22 21:15:06 +00:00
|
|
|
|
auto const sockfd = socket(domain, type, 0);
|
|
|
|
|
if (sockfd == TR_BAD_SOCKET)
|
|
|
|
|
{
|
|
|
|
|
if (sockerrno != EAFNOSUPPORT)
|
|
|
|
|
{
|
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
|
_("Couldn't create socket: {error} ({error_code})"),
|
|
|
|
|
fmt::arg("error", tr_net_strerror(sockerrno)),
|
|
|
|
|
fmt::arg("error_code", sockerrno)));
|
|
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
2022-04-22 21:15:06 +00:00
|
|
|
|
return TR_BAD_SOCKET;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
|
if (evutil_make_socket_nonblocking(sockfd) == -1)
|
2022-04-22 21:15:06 +00:00
|
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
|
tr_net_close_socket(sockfd);
|
2022-05-25 23:16:15 +00:00
|
|
|
|
return TR_BAD_SOCKET;
|
2022-04-22 21:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (static bool buf_logged = false; !buf_logged)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
socklen_t size = sizeof(i);
|
|
|
|
|
|
|
|
|
|
if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>(&i), &size) != -1)
|
|
|
|
|
{
|
|
|
|
|
tr_logAddTrace(fmt::format("SO_SNDBUF size is {}", i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
size = sizeof(i);
|
|
|
|
|
|
|
|
|
|
if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char*>(&i), &size) != -1)
|
|
|
|
|
{
|
|
|
|
|
tr_logAddTrace(fmt::format("SO_RCVBUF size is {}", i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf_logged = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2022-04-22 21:15:06 +00:00
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
|
TR_ASSERT(addr.is_valid());
|
2023-01-04 21:37:55 +00:00
|
|
|
|
TR_ASSERT(!tr_peer_socket::limit_reached(session));
|
2022-07-09 23:44:20 +00:00
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
|
if (tr_peer_socket::limit_reached(session) || !session->allowsTCP() || !addr.is_valid_for_peers(port))
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-04-22 21:15:06 +00:00
|
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-09 23:44:20 +00:00
|
|
|
|
static auto constexpr Domains = std::array<int, NUM_TR_AF_INET_TYPES>{ AF_INET, AF_INET6 };
|
2023-01-04 21:37:55 +00:00
|
|
|
|
auto const s = createSocket(Domains[addr.type], SOCK_STREAM);
|
2015-03-18 07:34:26 +00:00
|
|
|
|
if (s == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-04-22 21:15:06 +00:00
|
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2022-04-22 21:15:06 +00:00
|
|
|
|
// seeds don't need a big read buffer, so make it smaller
|
|
|
|
|
if (client_is_seed)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2009-12-02 20:03:27 +00:00
|
|
|
|
int n = 8192;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&n), sizeof(n)) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
|
tr_logAddDebug(fmt::format("Unable to set SO_RCVBUF on socket {}: {}", s, tr_net_strerror(sockerrno)));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
}
|
2009-12-02 05:30:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
auto const [sock, addrlen] = addr.to_sockaddr(port);
|
2009-08-10 20:04:08 +00:00
|
|
|
|
|
2022-04-22 21:15:06 +00:00
|
|
|
|
// set source address
|
2023-04-16 00:30:20 +00:00
|
|
|
|
auto const source_addr = session->publicAddress(addr.type);
|
2022-12-09 02:27:52 +00:00
|
|
|
|
auto const [source_sock, sourcelen] = source_addr.to_sockaddr({});
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2022-10-25 03:13:09 +00:00
|
|
|
|
if (bind(s, reinterpret_cast<sockaddr const*>(&source_sock), sourcelen) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
|
tr_logAddWarn(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
|
_("Couldn't set source address {address} on {socket}: {error} ({error_code})"),
|
2022-12-08 22:44:19 +00:00
|
|
|
|
fmt::arg("address", source_addr.display_name()),
|
2022-03-15 14:52:16 +00:00
|
|
|
|
fmt::arg("socket", s),
|
2022-03-16 00:51:36 +00:00
|
|
|
|
fmt::arg("error", tr_net_strerror(sockerrno)),
|
|
|
|
|
fmt::arg("error_code", sockerrno)));
|
2023-01-04 21:37:55 +00:00
|
|
|
|
tr_net_close_socket(s);
|
2022-04-22 21:15:06 +00:00
|
|
|
|
return {};
|
2009-04-15 21:05:58 +00:00
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2022-04-22 21:15:06 +00:00
|
|
|
|
auto ret = tr_peer_socket{};
|
2022-10-25 03:13:09 +00:00
|
|
|
|
if (connect(s, reinterpret_cast<sockaddr const*>(&sock), addrlen) == -1 &&
|
2014-07-04 00:00:07 +00:00
|
|
|
|
#ifdef _WIN32
|
2017-04-30 16:25:26 +00:00
|
|
|
|
sockerrno != WSAEWOULDBLOCK &&
|
2007-08-02 23:33:40 +00:00
|
|
|
|
#endif
|
2017-04-30 16:25:26 +00:00
|
|
|
|
sockerrno != EINPROGRESS)
|
2006-07-16 19:39:23 +00:00
|
|
|
|
{
|
2023-01-27 06:00:00 +00:00
|
|
|
|
if (auto const tmperrno = sockerrno;
|
|
|
|
|
(tmperrno != ECONNREFUSED && tmperrno != ENETUNREACH && tmperrno != EHOSTUNREACH) || addr.is_ipv4())
|
2015-07-01 00:54:41 +00:00
|
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
|
tr_logAddWarn(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
|
_("Couldn't connect socket {socket} to {address}:{port}: {error} ({error_code})"),
|
2022-03-15 14:52:16 +00:00
|
|
|
|
fmt::arg("socket", s),
|
2022-12-08 22:44:19 +00:00
|
|
|
|
fmt::arg("address", addr.display_name()),
|
2022-04-21 15:58:13 +00:00
|
|
|
|
fmt::arg("port", port.host()),
|
2022-03-16 00:51:36 +00:00
|
|
|
|
fmt::arg("error", tr_net_strerror(tmperrno)),
|
|
|
|
|
fmt::arg("error_code", tmperrno)));
|
2015-07-01 00:54:41 +00:00
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
|
tr_net_close_socket(s);
|
2017-06-28 15:46:06 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-12-06 16:28:28 +00:00
|
|
|
|
ret = tr_peer_socket{ session, addr, port, s };
|
2006-07-16 19:39:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
|
tr_logAddTrace(fmt::format("New OUTGOING connection {} ({})", s, addr.display_name(port)));
|
2008-08-16 21:06:57 +00:00
|
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
}
|
2007-07-31 14:26:44 +00:00
|
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
|
static tr_socket_t tr_netBindTCPImpl(tr_address const& addr, tr_port port, bool suppress_msgs, int* err_out)
|
2006-07-16 19:39:23 +00:00
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
|
TR_ASSERT(addr.is_valid());
|
2022-07-09 23:44:20 +00:00
|
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
|
static auto constexpr Domains = std::array<int, NUM_TR_AF_INET_TYPES>{ AF_INET, AF_INET6 };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
|
auto const fd = socket(Domains[addr.type], SOCK_STREAM, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
if (fd == TR_BAD_SOCKET)
|
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
|
*err_out = sockerrno;
|
2015-03-18 07:34:26 +00:00
|
|
|
|
return TR_BAD_SOCKET;
|
2009-07-09 18:14:33 +00:00
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
|
if (evutil_make_socket_nonblocking(fd) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
|
*err_out = sockerrno;
|
2023-01-04 21:37:55 +00:00
|
|
|
|
tr_net_close_socket(fd);
|
2015-03-18 07:34:26 +00:00
|
|
|
|
return TR_BAD_SOCKET;
|
2009-05-14 13:42:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
|
int optval = 1;
|
2021-09-12 17:41:49 +00:00
|
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<char const*>(&optval), sizeof(optval));
|
|
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char const*>(&optval), sizeof(optval));
|
2008-12-01 20:21:06 +00:00
|
|
|
|
|
2009-02-07 00:34:10 +00:00
|
|
|
|
#ifdef IPV6_V6ONLY
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
if (addr.is_ipv6() &&
|
2021-09-12 17:41:49 +00:00
|
|
|
|
(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char const*>(&optval), sizeof(optval)) == -1) &&
|
2020-11-01 21:47:57 +00:00
|
|
|
|
(sockerrno != ENOPROTOOPT)) // if the kernel doesn't support it, ignore it
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
|
*err_out = sockerrno;
|
2023-01-04 21:37:55 +00:00
|
|
|
|
tr_net_close_socket(fd);
|
2020-11-01 21:47:57 +00:00
|
|
|
|
return TR_BAD_SOCKET;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-07 00:34:10 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
auto const [sock, addrlen] = addr.to_sockaddr(port);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2023-01-28 21:26:23 +00:00
|
|
|
|
if (bind(fd, reinterpret_cast<sockaddr const*>(&sock), addrlen) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
|
int const err = sockerrno;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
|
if (!suppress_msgs)
|
2010-02-06 14:43:28 +00:00
|
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
|
err == EADDRINUSE ?
|
2022-03-16 00:51:36 +00:00
|
|
|
|
_("Couldn't bind port {port} on {address}: {error} ({error_code}) -- Is another copy of Transmission already running?") :
|
|
|
|
|
_("Couldn't bind port {port} on {address}: {error} ({error_code})"),
|
2022-12-08 22:44:19 +00:00
|
|
|
|
fmt::arg("address", addr.display_name()),
|
2022-04-21 15:58:13 +00:00
|
|
|
|
fmt::arg("port", port.host()),
|
2022-03-16 00:51:36 +00:00
|
|
|
|
fmt::arg("error", tr_net_strerror(err)),
|
|
|
|
|
fmt::arg("error_code", err)));
|
2010-02-06 14:43:28 +00:00
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
|
tr_net_close_socket(fd);
|
2022-09-07 16:04:28 +00:00
|
|
|
|
*err_out = err;
|
2015-03-18 07:34:26 +00:00
|
|
|
|
return TR_BAD_SOCKET;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
}
|
2009-05-14 13:42:29 +00:00
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
|
if (!suppress_msgs)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
|
tr_logAddDebug(fmt::format(FMT_STRING("Bound socket {:d} to port {:d} on {:s}"), fd, port.host(), addr.display_name()));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
}
|
2009-05-14 13:42:29 +00:00
|
|
|
|
|
2017-02-18 00:47:11 +00:00
|
|
|
|
#ifdef TCP_FASTOPEN
|
|
|
|
|
|
|
|
|
|
#ifndef SOL_TCP
|
|
|
|
|
#define SOL_TCP IPPROTO_TCP
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
optval = 5;
|
2021-09-12 17:41:49 +00:00
|
|
|
|
(void)setsockopt(fd, SOL_TCP, TCP_FASTOPEN, reinterpret_cast<char const*>(&optval), sizeof(optval));
|
2017-02-18 00:47:11 +00:00
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-07-29 03:14:07 +00:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
if (listen(fd, SOMAXCONN) == -1)
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
|
/* Listen queue backlog will be capped to the operating system's limit. */
|
|
|
|
|
if (listen(fd, INT_MAX) == -1)
|
|
|
|
|
#endif /* _WIN32 */
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
|
*err_out = sockerrno;
|
2023-01-04 21:37:55 +00:00
|
|
|
|
tr_net_close_socket(fd);
|
2015-03-18 07:34:26 +00:00
|
|
|
|
return TR_BAD_SOCKET;
|
2009-05-14 13:42:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
|
tr_socket_t tr_netBindTCP(tr_address const& addr, tr_port port, bool suppress_msgs)
|
2009-07-09 18:14:33 +00:00
|
|
|
|
{
|
2021-10-24 17:40:15 +00:00
|
|
|
|
int unused = 0;
|
2022-09-07 16:04:28 +00:00
|
|
|
|
return tr_netBindTCPImpl(addr, port, suppress_msgs, &unused);
|
2009-07-09 18:14:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 06:20:39 +00:00
|
|
|
|
std::optional<std::tuple<tr_address, tr_port, tr_socket_t>> tr_netAccept(tr_session* session, tr_socket_t listening_sockfd)
|
2006-07-16 19:39:23 +00:00
|
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
|
TR_ASSERT(session != nullptr);
|
2009-05-14 13:42:29 +00:00
|
|
|
|
|
2022-04-22 21:15:06 +00:00
|
|
|
|
// accept the incoming connection
|
2022-09-23 05:51:15 +00:00
|
|
|
|
auto sock = sockaddr_storage{};
|
2022-04-22 21:15:06 +00:00
|
|
|
|
socklen_t len = sizeof(struct sockaddr_storage);
|
2023-01-28 21:26:23 +00:00
|
|
|
|
auto const sockfd = accept(listening_sockfd, reinterpret_cast<sockaddr*>(&sock), &len);
|
2022-04-22 21:15:06 +00:00
|
|
|
|
if (sockfd == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
2022-10-14 06:20:39 +00:00
|
|
|
|
return {};
|
2009-05-14 13:42:29 +00:00
|
|
|
|
}
|
2008-12-16 21:06:47 +00:00
|
|
|
|
|
2022-04-22 21:15:06 +00:00
|
|
|
|
// get the address and port,
|
|
|
|
|
// make the socket unblocking,
|
|
|
|
|
// and confirm we don't have too many peers
|
2022-12-09 02:27:52 +00:00
|
|
|
|
auto const addrport = tr_address::from_sockaddr(reinterpret_cast<struct sockaddr*>(&sock));
|
2023-01-04 21:37:55 +00:00
|
|
|
|
if (!addrport || evutil_make_socket_nonblocking(sockfd) == -1 || tr_peer_socket::limit_reached(session))
|
2022-04-22 21:15:06 +00:00
|
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
|
tr_net_close_socket(sockfd);
|
2022-10-14 06:20:39 +00:00
|
|
|
|
return {};
|
2022-04-22 21:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 06:20:39 +00:00
|
|
|
|
return std::make_tuple(addrport->first, addrport->second, sockfd);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
|
void tr_net_close_socket(tr_socket_t sockfd)
|
2009-10-27 20:27:27 +00:00
|
|
|
|
{
|
2022-04-22 21:15:06 +00:00
|
|
|
|
evutil_closesocket(sockfd);
|
2009-10-27 20:27:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
|
// ---
|
2009-12-15 17:39:19 +00:00
|
|
|
|
|
2023-01-27 20:25:08 +00:00
|
|
|
|
namespace
|
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
|
namespace is_valid_for_peers_helpers
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_ipv4_mapped_address(tr_address const* addr)
|
2009-12-15 17:39:19 +00:00
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
|
return addr->is_ipv6() && IN6_IS_ADDR_V4MAPPED(&addr->addr.addr6);
|
2009-12-15 17:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
[[nodiscard]] constexpr auto is_ipv6_link_local_address(tr_address const* addr)
|
2009-12-15 17:39:19 +00:00
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
|
return addr->is_ipv6() && IN6_IS_ADDR_LINKLOCAL(&addr->addr.addr6);
|
2009-12-15 17:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* isMartianAddr was written by Juliusz Chroboczek,
|
|
|
|
|
and is covered under the same license as third-party/dht/dht.c. */
|
2022-12-09 02:27:52 +00:00
|
|
|
|
[[nodiscard]] auto is_martian_addr(tr_address const& addr)
|
2009-12-14 02:07:47 +00:00
|
|
|
|
{
|
2022-08-26 18:35:28 +00:00
|
|
|
|
static auto constexpr Zeroes = std::array<unsigned char, 16>{};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
switch (addr.type)
|
2009-12-14 02:07:47 +00:00
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
case TR_AF_INET:
|
|
|
|
|
{
|
2023-01-28 21:26:23 +00:00
|
|
|
|
auto const* const address = reinterpret_cast<unsigned char const*>(&addr.addr.addr4);
|
2017-04-30 16:25:26 +00:00
|
|
|
|
return address[0] == 0 || address[0] == 127 || (address[0] & 0xE0) == 0xE0;
|
2009-12-15 17:39:19 +00:00
|
|
|
|
}
|
2009-12-14 02:07:47 +00:00
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
|
case TR_AF_INET6:
|
|
|
|
|
{
|
2023-01-28 21:26:23 +00:00
|
|
|
|
auto const* const address = reinterpret_cast<unsigned char const*>(&addr.addr.addr6);
|
2022-08-26 18:35:28 +00:00
|
|
|
|
return address[0] == 0xFF ||
|
|
|
|
|
(memcmp(address, std::data(Zeroes), 15) == 0 && (address[15] == 0 || address[15] == 1));
|
2009-12-15 17:39:19 +00:00
|
|
|
|
}
|
2009-12-14 02:07:47 +00:00
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
|
default:
|
|
|
|
|
return true;
|
2009-12-14 02:07:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-12-15 17:39:19 +00:00
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
} // namespace is_valid_for_peers_helpers
|
2023-01-27 20:25:08 +00:00
|
|
|
|
} // namespace
|
2022-12-09 02:27:52 +00:00
|
|
|
|
|
|
|
|
|
bool tr_address::is_valid_for_peers(tr_port port) const noexcept
|
2009-12-15 17:39:19 +00:00
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
|
using namespace is_valid_for_peers_helpers;
|
|
|
|
|
|
|
|
|
|
return is_valid() && !std::empty(port) && !is_ipv6_link_local_address(this) && !is_ipv4_mapped_address(this) &&
|
|
|
|
|
!is_martian_addr(*this);
|
2009-12-15 17:39:19 +00:00
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
|
// --- tr_port
|
2022-04-21 15:58:13 +00:00
|
|
|
|
|
2022-10-24 18:40:12 +00:00
|
|
|
|
std::pair<tr_port, std::byte const*> tr_port::fromCompact(std::byte const* compact) noexcept
|
2022-04-21 15:58:13 +00:00
|
|
|
|
{
|
2022-04-21 22:06:00 +00:00
|
|
|
|
static auto constexpr PortLen = size_t{ 2 };
|
|
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
|
static_assert(PortLen == sizeof(uint16_t));
|
|
|
|
|
auto nport = uint16_t{};
|
2022-10-24 18:40:12 +00:00
|
|
|
|
std::copy_n(compact, PortLen, reinterpret_cast<std::byte*>(&nport));
|
2022-04-21 15:58:13 +00:00
|
|
|
|
compact += PortLen;
|
2022-04-21 22:06:00 +00:00
|
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
|
return std::make_pair(tr_port::fromNetwork(nport), compact);
|
|
|
|
|
}
|
2022-04-21 22:06:00 +00:00
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
|
// --- tr_address
|
2022-04-21 22:06:00 +00:00
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
std::optional<tr_address> tr_address::from_string(std::string_view address_sv)
|
2022-04-21 23:37:02 +00:00
|
|
|
|
{
|
2022-07-25 22:25:55 +00:00
|
|
|
|
auto const address_sz = tr_strbuf<char, TR_ADDRSTRLEN>{ address_sv };
|
|
|
|
|
|
2022-04-21 23:37:02 +00:00
|
|
|
|
auto addr = tr_address{};
|
|
|
|
|
|
2023-03-28 17:59:42 +00:00
|
|
|
|
addr.addr.addr4 = {};
|
|
|
|
|
if (evutil_inet_pton(AF_INET, address_sz, &addr.addr.addr4) == 1)
|
2022-07-08 18:23:41 +00:00
|
|
|
|
{
|
2022-07-25 22:25:55 +00:00
|
|
|
|
addr.type = TR_AF_INET;
|
|
|
|
|
return addr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 17:59:42 +00:00
|
|
|
|
addr.addr.addr6 = {};
|
|
|
|
|
if (evutil_inet_pton(AF_INET6, address_sz, &addr.addr.addr6) == 1)
|
2022-07-25 22:25:55 +00:00
|
|
|
|
{
|
|
|
|
|
addr.type = TR_AF_INET6;
|
|
|
|
|
return addr;
|
2022-07-08 18:23:41 +00:00
|
|
|
|
}
|
2022-04-21 23:37:02 +00:00
|
|
|
|
|
2022-07-25 22:25:55 +00:00
|
|
|
|
return {};
|
2022-04-21 23:37:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
|
std::string_view tr_address::display_name(char* out, size_t outlen, tr_port port) const
|
2022-04-21 23:37:02 +00:00
|
|
|
|
{
|
2022-07-10 01:03:40 +00:00
|
|
|
|
if (std::empty(port))
|
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
|
return is_ipv4() ? evutil_inet_ntop(AF_INET, &addr, out, outlen) : evutil_inet_ntop(AF_INET6, &addr, out, outlen);
|
2022-07-10 01:03:40 +00:00
|
|
|
|
}
|
2022-07-09 23:44:20 +00:00
|
|
|
|
|
2022-07-10 01:03:40 +00:00
|
|
|
|
auto buf = std::array<char, INET6_ADDRSTRLEN>{};
|
2022-12-08 22:44:19 +00:00
|
|
|
|
auto const addr_sv = display_name(std::data(buf), std::size(buf));
|
2022-07-10 01:03:40 +00:00
|
|
|
|
auto const [end, size] = fmt::format_to_n(out, outlen - 1, FMT_STRING("[{:s}]:{:d}"), addr_sv, port.host());
|
|
|
|
|
return { out, size };
|
2022-04-21 23:37:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename OutputIt>
|
2022-12-08 22:44:19 +00:00
|
|
|
|
OutputIt tr_address::display_name(OutputIt out, tr_port port) const
|
2022-04-21 23:37:02 +00:00
|
|
|
|
{
|
2022-07-10 01:03:40 +00:00
|
|
|
|
auto addrbuf = std::array<char, TR_ADDRSTRLEN + 16>{};
|
2022-12-08 22:44:19 +00:00
|
|
|
|
auto const addr_sv = display_name(std::data(addrbuf), std::size(addrbuf), port);
|
2022-07-10 01:03:40 +00:00
|
|
|
|
return std::copy(std::begin(addr_sv), std::end(addr_sv), out);
|
2022-04-21 23:37:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
|
template char* tr_address::display_name<char*>(char*, tr_port) const;
|
2022-04-21 23:37:02 +00:00
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
|
[[nodiscard]] std::string tr_address::display_name(tr_port port) const
|
2022-04-21 23:37:02 +00:00
|
|
|
|
{
|
|
|
|
|
auto buf = std::string{};
|
2022-07-10 01:03:40 +00:00
|
|
|
|
buf.reserve(INET6_ADDRSTRLEN + 16);
|
2022-12-08 22:44:19 +00:00
|
|
|
|
this->display_name(std::back_inserter(buf), port);
|
2022-04-21 23:37:02 +00:00
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
std::pair<tr_address, std::byte const*> tr_address::from_compact_ipv4(std::byte const* compact) noexcept
|
2022-04-21 22:06:00 +00:00
|
|
|
|
{
|
|
|
|
|
static auto constexpr Addr4Len = size_t{ 4 };
|
|
|
|
|
|
|
|
|
|
auto address = tr_address{};
|
|
|
|
|
static_assert(sizeof(address.addr.addr4) == Addr4Len);
|
|
|
|
|
address.type = TR_AF_INET;
|
2022-10-24 18:40:12 +00:00
|
|
|
|
std::copy_n(compact, Addr4Len, reinterpret_cast<std::byte*>(&address.addr));
|
2022-04-21 22:06:00 +00:00
|
|
|
|
compact += Addr4Len;
|
|
|
|
|
|
|
|
|
|
return std::make_pair(address, compact);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
std::pair<tr_address, std::byte const*> tr_address::from_compact_ipv6(std::byte const* compact) noexcept
|
2022-04-21 22:06:00 +00:00
|
|
|
|
{
|
|
|
|
|
static auto constexpr Addr6Len = size_t{ 16 };
|
|
|
|
|
|
|
|
|
|
auto address = tr_address{};
|
|
|
|
|
address.type = TR_AF_INET6;
|
2022-10-24 18:40:12 +00:00
|
|
|
|
std::copy_n(compact, Addr6Len, reinterpret_cast<std::byte*>(&address.addr.addr6.s6_addr));
|
2022-04-21 22:06:00 +00:00
|
|
|
|
compact += Addr6Len;
|
|
|
|
|
|
|
|
|
|
return std::make_pair(address, compact);
|
|
|
|
|
}
|
2022-04-21 23:37:02 +00:00
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
|
std::optional<std::pair<tr_address, tr_port>> tr_address::from_sockaddr(struct sockaddr const* from)
|
|
|
|
|
{
|
|
|
|
|
if (from == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (from->sa_family == AF_INET)
|
|
|
|
|
{
|
|
|
|
|
auto const* const sin = reinterpret_cast<struct sockaddr_in const*>(from);
|
|
|
|
|
auto addr = tr_address{};
|
|
|
|
|
addr.type = TR_AF_INET;
|
|
|
|
|
addr.addr.addr4 = sin->sin_addr;
|
|
|
|
|
return std::make_pair(addr, tr_port::fromNetwork(sin->sin_port));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (from->sa_family == AF_INET6)
|
|
|
|
|
{
|
|
|
|
|
auto const* const sin6 = reinterpret_cast<struct sockaddr_in6 const*>(from);
|
|
|
|
|
auto addr = tr_address{};
|
|
|
|
|
addr.type = TR_AF_INET6;
|
|
|
|
|
addr.addr.addr6 = sin6->sin6_addr;
|
|
|
|
|
return std::make_pair(addr, tr_port::fromNetwork(sin6->sin6_port));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<sockaddr_storage, socklen_t> tr_address::to_sockaddr(tr_port port) const noexcept
|
|
|
|
|
{
|
|
|
|
|
auto ss = sockaddr_storage{};
|
|
|
|
|
|
|
|
|
|
if (is_ipv4())
|
|
|
|
|
{
|
|
|
|
|
auto* const ss4 = reinterpret_cast<sockaddr_in*>(&ss);
|
|
|
|
|
ss4->sin_addr = addr.addr4;
|
|
|
|
|
ss4->sin_family = AF_INET;
|
|
|
|
|
ss4->sin_port = port.network();
|
|
|
|
|
return { ss, sizeof(sockaddr_in) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* const ss6 = reinterpret_cast<sockaddr_in6*>(&ss);
|
|
|
|
|
ss6->sin6_addr = addr.addr6;
|
|
|
|
|
ss6->sin6_family = AF_INET6;
|
|
|
|
|
ss6->sin6_flowinfo = 0;
|
|
|
|
|
ss6->sin6_port = port.network();
|
|
|
|
|
return { ss, sizeof(sockaddr_in6) };
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 20:25:08 +00:00
|
|
|
|
int tr_address::compare(tr_address const& that) const noexcept // <=>
|
2022-12-09 02:27:52 +00:00
|
|
|
|
{
|
|
|
|
|
// IPv6 addresses are always "greater than" IPv4
|
2023-01-27 20:25:08 +00:00
|
|
|
|
if (this->type != that.type)
|
2022-12-09 02:27:52 +00:00
|
|
|
|
{
|
2023-01-27 20:25:08 +00:00
|
|
|
|
return this->is_ipv4() ? 1 : -1;
|
2022-12-09 02:27:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 20:25:08 +00:00
|
|
|
|
return this->is_ipv4() ? memcmp(&this->addr.addr4, &that.addr.addr4, sizeof(this->addr.addr4)) :
|
|
|
|
|
memcmp(&this->addr.addr6.s6_addr, &that.addr.addr6.s6_addr, sizeof(this->addr.addr6.s6_addr));
|
2022-04-21 23:37:02 +00:00
|
|
|
|
}
|
2022-12-25 00:53:50 +00:00
|
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Reserved_IP_addresses
|
|
|
|
|
[[nodiscard]] bool tr_address::is_global_unicast_address() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (is_ipv4())
|
|
|
|
|
{
|
|
|
|
|
auto const* const a = reinterpret_cast<uint8_t const*>(&addr.addr4.s_addr);
|
|
|
|
|
|
|
|
|
|
// [0.0.0.0–0.255.255.255]
|
|
|
|
|
// Current network.
|
|
|
|
|
if (a[0] == 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [10.0.0.0 – 10.255.255.255]
|
|
|
|
|
// Used for local communications within a private network.
|
|
|
|
|
if (a[0] == 10)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [100.64.0.0–100.127.255.255]
|
|
|
|
|
// Shared address space for communications between a service provider
|
|
|
|
|
// and its subscribers when using a carrier-grade NAT.
|
|
|
|
|
if ((a[0] == 100) && (64 <= a[1] && a[1] <= 127))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [169.254.0.0–169.254.255.255]
|
|
|
|
|
// Used for link-local addresses[5] between two hosts on a single link
|
|
|
|
|
// when no IP address is otherwise specified, such as would have
|
|
|
|
|
// normally been retrieved from a DHCP server.
|
|
|
|
|
if (a[0] == 169 && a[1] == 254)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [172.16.0.0–172.31.255.255]
|
|
|
|
|
// Used for local communications within a private network.
|
|
|
|
|
if ((a[0] == 172) && (16 <= a[1] && a[1] <= 31))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [192.0.0.0–192.0.0.255]
|
|
|
|
|
// IETF Protocol Assignments.
|
|
|
|
|
if (a[0] == 192 && a[1] == 0 && a[2] == 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [192.0.2.0–192.0.2.255]
|
|
|
|
|
// Assigned as TEST-NET-1, documentation and examples.
|
|
|
|
|
if (a[0] == 192 && a[1] == 0 && a[2] == 2)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [192.88.99.0–192.88.99.255]
|
|
|
|
|
// Reserved. Formerly used for IPv6 to IPv4 relay.
|
|
|
|
|
if (a[0] == 192 && a[1] == 88 && a[2] == 99)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [192.168.0.0–192.168.255.255]
|
|
|
|
|
// Used for local communications within a private network.
|
|
|
|
|
if (a[0] == 192 && a[1] == 168)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [198.18.0.0–198.19.255.255]
|
|
|
|
|
// Used for benchmark testing of inter-network communications
|
|
|
|
|
// between two separate subnets.
|
|
|
|
|
if (a[0] == 198 && (18 <= a[1] && a[1] <= 19))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [198.51.100.0–198.51.100.255]
|
|
|
|
|
// Assigned as TEST-NET-2, documentation and examples.
|
|
|
|
|
if (a[0] == 198 && a[1] == 51 && a[2] == 100)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [203.0.113.0–203.0.113.255]
|
|
|
|
|
// Assigned as TEST-NET-3, documentation and examples.
|
|
|
|
|
if (a[0] == 203 && a[1] == 0 && a[2] == 113)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [224.0.0.0–239.255.255.255]
|
|
|
|
|
// In use for IP multicast. (Former Class D network.)
|
|
|
|
|
if (224 <= a[0] && a[0] <= 230)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [233.252.0.0-233.252.0.255]
|
|
|
|
|
// Assigned as MCAST-TEST-NET, documentation and examples.
|
|
|
|
|
if (a[0] == 233 && a[1] == 252 && a[2] == 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [240.0.0.0–255.255.255.254]
|
|
|
|
|
// Reserved for future use. (Former Class E network.)
|
|
|
|
|
// [255.255.255.255]
|
|
|
|
|
// Reserved for the "limited broadcast" destination address.
|
|
|
|
|
if (240 <= a[0])
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_ipv6())
|
|
|
|
|
{
|
|
|
|
|
auto const* const a = addr.addr6.s6_addr;
|
|
|
|
|
|
|
|
|
|
// TODO: 2000::/3 is commonly used for global unicast but technically
|
|
|
|
|
// other spaces would be allowable too, so we should test those here.
|
2023-04-30 18:29:29 +00:00
|
|
|
|
// See RFC 4291 in the Section 2.4 listing global unicast as everything
|
2022-12-25 00:53:50 +00:00
|
|
|
|
// that's not link-local, multicast, loopback, or unspecified.
|
|
|
|
|
return (a[0] & 0xE0) == 0x20;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|