Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m47s
Foundation / checks (network) (push) Successful in 8m11s
Foundation / checks (client) (push) Successful in 12m20s
Foundation / checks (tsan) (push) Successful in 7m44s
Foundation / checks (checks) (push) Successful in 31m12s
Foundation / checks (postgres) (push) Successful in 33m27s
Foundation / image (push) Successful in 2m14s
Foundation / postgres-tools (push) Successful in 6m24s
Foundation / server-image (push) Successful in 18m6s
evo_network_quic wraps msquic in a single-owner Endpoint: newest-wins datagram channel and one length-framed reliable stream, both bounded, with explicit close causes. The client pins exactly one CA and name with an owned Mbed TLS check that is the sole authority (OpenSSL's verdict advisory), callbacks never call msquic under the lock, and MinimumMtu starts at 1280 so 1,200-byte datagrams fit minimum paths. An owned seeded UDP impairment relay drives loss, duplication, reordering, corruption, MTU and outage tests, passing on MSVC, GCC and Clang ASan/UBSan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
253 lines
8.2 KiB
C++
253 lines
8.2 KiB
C++
#include "udp_relay.hpp"
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <queue>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#ifdef _WIN32
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#endif
|
|
#ifndef NOMINMAX
|
|
#define NOMINMAX
|
|
#endif
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
#else
|
|
#include <arpa/inet.h>
|
|
#include <fcntl.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
namespace evo::network::impair {
|
|
namespace {
|
|
#ifdef _WIN32
|
|
using Native = SOCKET;
|
|
constexpr Native invalid = INVALID_SOCKET;
|
|
void close_native(Native s) {
|
|
closesocket(s);
|
|
}
|
|
bool startup() {
|
|
static std::once_flag once;
|
|
static bool ok = false;
|
|
std::call_once(once, [] {
|
|
WSADATA data{};
|
|
ok = WSAStartup(MAKEWORD(2, 2), &data) == 0;
|
|
});
|
|
return ok;
|
|
}
|
|
int wait_readable(std::array<pollfd, 2>& fds, int ms) {
|
|
return WSAPoll(fds.data(), static_cast<ULONG>(fds.size()), ms);
|
|
}
|
|
using Length = int;
|
|
#else
|
|
using Native = int;
|
|
constexpr Native invalid = -1;
|
|
void close_native(Native s) {
|
|
::close(s);
|
|
}
|
|
bool startup() {
|
|
return true;
|
|
}
|
|
int wait_readable(std::array<pollfd, 2>& fds, int ms) {
|
|
return ::poll(fds.data(), fds.size(), ms);
|
|
}
|
|
using Length = std::size_t;
|
|
#endif
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
struct SplitMix64 {
|
|
std::uint64_t state;
|
|
std::uint64_t next() {
|
|
std::uint64_t z = (state += 0x9E3779B97F4A7C15ULL);
|
|
z = (z ^ (z >> 30U)) * 0xBF58476D1CE4E5B9ULL;
|
|
z = (z ^ (z >> 27U)) * 0x94D049BB133111EBULL;
|
|
return z ^ (z >> 31U);
|
|
}
|
|
double unit() {
|
|
return static_cast<double>(next() >> 11U) * 0x1.0p-53;
|
|
}
|
|
};
|
|
Native bound_udp(std::uint16_t port) {
|
|
const Native s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
if (s == invalid)
|
|
return invalid;
|
|
sockaddr_in local{};
|
|
local.sin_family = AF_INET;
|
|
local.sin_port = htons(port);
|
|
local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
if (::bind(s, reinterpret_cast<const sockaddr*>(&local), sizeof local) != 0) {
|
|
close_native(s);
|
|
return invalid;
|
|
}
|
|
return s;
|
|
}
|
|
} // namespace
|
|
|
|
struct UdpRelay::Impl {
|
|
Native client_side{invalid}, server_side{invalid};
|
|
std::uint16_t port{};
|
|
sockaddr_in target{};
|
|
std::optional<sockaddr_in> client;
|
|
mutable std::mutex mutex;
|
|
Impairment up, down;
|
|
RelayCounters counters;
|
|
SplitMix64 rng{0};
|
|
std::atomic<bool> stop{false};
|
|
std::thread thread;
|
|
struct Pending {
|
|
Clock::time_point due;
|
|
std::uint64_t order;
|
|
bool upstream;
|
|
std::vector<std::uint8_t> bytes;
|
|
bool operator>(const Pending& o) const {
|
|
return due != o.due ? due > o.due : order > o.order;
|
|
}
|
|
};
|
|
std::priority_queue<Pending, std::vector<Pending>, std::greater<>> queue;
|
|
std::uint64_t order{};
|
|
|
|
// Under `mutex`: applies one direction's impairment to a received packet.
|
|
void admit(bool upstream, std::vector<std::uint8_t> bytes, Clock::time_point now) {
|
|
const auto& rule = upstream ? up : down;
|
|
if (bytes.size() > rule.max_payload) {
|
|
++counters.oversized;
|
|
return;
|
|
}
|
|
if (rng.unit() < rule.loss) {
|
|
++counters.dropped;
|
|
return;
|
|
}
|
|
if (!bytes.empty() && rng.unit() < rule.corrupt) {
|
|
bytes[rng.next() % bytes.size()] ^= static_cast<std::uint8_t>(1U + (rng.next() % 255U));
|
|
++counters.corrupted;
|
|
}
|
|
const auto jitter =
|
|
rule.jitter.count() > 0
|
|
? std::chrono::milliseconds{static_cast<std::int64_t>(
|
|
rng.next() % static_cast<std::uint64_t>(rule.jitter.count() + 1))}
|
|
: std::chrono::milliseconds{0};
|
|
auto due = now + rule.delay + jitter;
|
|
if (rng.unit() < rule.reorder) {
|
|
due += rule.reorder_delay;
|
|
++counters.reordered;
|
|
}
|
|
const int copies = rng.unit() < rule.duplicate ? 2 : 1;
|
|
if (copies == 2)
|
|
++counters.duplicated;
|
|
for (int i = 0; i < copies; ++i)
|
|
queue.push({due, order++, upstream, bytes});
|
|
}
|
|
|
|
void run() {
|
|
std::array<std::uint8_t, 65536> buffer{};
|
|
while (!stop.load()) {
|
|
std::array<pollfd, 2> fds{};
|
|
fds[0].fd = client_side;
|
|
fds[0].events = POLLIN;
|
|
fds[1].fd = server_side;
|
|
fds[1].events = POLLIN;
|
|
(void)wait_readable(fds, 1);
|
|
const auto now = Clock::now();
|
|
for (int side = 0; side < 2; ++side) {
|
|
if ((fds[static_cast<std::size_t>(side)].revents & POLLIN) == 0)
|
|
continue;
|
|
sockaddr_in from{};
|
|
socklen_t from_size = sizeof from;
|
|
const auto n = ::recvfrom(side == 0 ? client_side : server_side,
|
|
reinterpret_cast<char*>(buffer.data()),
|
|
static_cast<Length>(buffer.size()), 0,
|
|
reinterpret_cast<sockaddr*>(&from), &from_size);
|
|
if (n <= 0)
|
|
continue;
|
|
const std::lock_guard lock{mutex};
|
|
if (side == 0 && !client)
|
|
client = from; // The first client owns the relay.
|
|
if (side == 0 && (from.sin_port != client->sin_port ||
|
|
from.sin_addr.s_addr != client->sin_addr.s_addr))
|
|
continue;
|
|
admit(side == 0, {buffer.data(), buffer.data() + n}, now);
|
|
}
|
|
std::vector<Pending> ready;
|
|
{
|
|
const std::lock_guard lock{mutex};
|
|
while (!queue.empty() && queue.top().due <= Clock::now()) {
|
|
ready.push_back(queue.top());
|
|
queue.pop();
|
|
}
|
|
}
|
|
for (const auto& p : ready) {
|
|
const Native s = p.upstream ? server_side : client_side;
|
|
const sockaddr_in* to = p.upstream ? &target : (client ? &*client : nullptr);
|
|
if (to == nullptr)
|
|
continue;
|
|
(void)::sendto(s, reinterpret_cast<const char*>(p.bytes.data()),
|
|
static_cast<Length>(p.bytes.size()), 0,
|
|
reinterpret_cast<const sockaddr*>(to), sizeof *to);
|
|
const std::lock_guard lock{mutex};
|
|
++counters.forwarded;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
UdpRelay::UdpRelay(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
|
|
|
|
UdpRelay::~UdpRelay() {
|
|
impl_->stop.store(true);
|
|
if (impl_->thread.joinable())
|
|
impl_->thread.join();
|
|
if (impl_->client_side != invalid)
|
|
close_native(impl_->client_side);
|
|
if (impl_->server_side != invalid)
|
|
close_native(impl_->server_side);
|
|
}
|
|
|
|
std::unique_ptr<UdpRelay> UdpRelay::start(std::uint16_t target_port, Impairment upstream,
|
|
Impairment downstream, std::uint64_t seed) {
|
|
if (!startup() || target_port == 0)
|
|
return nullptr;
|
|
auto impl = std::make_unique<Impl>();
|
|
impl->client_side = bound_udp(0);
|
|
impl->server_side = bound_udp(0);
|
|
if (impl->client_side == invalid || impl->server_side == invalid)
|
|
return nullptr;
|
|
sockaddr_in local{};
|
|
socklen_t size = sizeof local;
|
|
if (::getsockname(impl->client_side, reinterpret_cast<sockaddr*>(&local), &size) != 0)
|
|
return nullptr;
|
|
impl->port = ntohs(local.sin_port);
|
|
impl->target.sin_family = AF_INET;
|
|
impl->target.sin_port = htons(target_port);
|
|
impl->target.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
impl->up = upstream;
|
|
impl->down = downstream;
|
|
impl->rng.state = seed;
|
|
std::unique_ptr<UdpRelay> relay{new UdpRelay{std::move(impl)}};
|
|
auto* raw = relay->impl_.get();
|
|
raw->thread = std::thread{[raw] { raw->run(); }};
|
|
return relay;
|
|
}
|
|
|
|
std::uint16_t UdpRelay::port() const noexcept {
|
|
return impl_->port;
|
|
}
|
|
|
|
void UdpRelay::set(Impairment upstream, Impairment downstream) {
|
|
const std::lock_guard lock{impl_->mutex};
|
|
impl_->up = upstream;
|
|
impl_->down = downstream;
|
|
}
|
|
|
|
RelayCounters UdpRelay::counters() const {
|
|
const std::lock_guard lock{impl_->mutex};
|
|
return impl_->counters;
|
|
}
|
|
} // namespace evo::network::impair
|