#include "udp_relay.hpp" #include #include #include #include #include #include #include #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #ifndef NOMINMAX #define NOMINMAX #endif #include #include // After : timeBeginPeriod for 1 ms waits (default timer tick is ~15.6 ms). #include #else #include #include #include #include #include #include #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& fds, int ms) { return WSAPoll(fds.data(), static_cast(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& 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(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(&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 client; Clock::time_point client_seen{}; mutable std::mutex mutex; Impairment up, down; RelayCounters counters; SplitMix64 rng{0}; std::atomic stop{false}; std::thread thread; struct Pending { Clock::time_point due; std::uint64_t order; bool upstream; std::vector bytes; bool operator>(const Pending& o) const { return due != o.due ? due > o.due : order > o.order; } }; std::priority_queue, std::greater<>> queue; std::uint64_t order{}; // Under `mutex`: applies one direction's impairment to a received packet. void admit(bool upstream, std::vector 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(1U + (rng.next() % 255U)); ++counters.corrupted; } const auto jitter = rule.jitter.count() > 0 ? std::chrono::milliseconds{static_cast( rng.next() % static_cast(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 buffer{}; while (!stop.load()) { std::array 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(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(buffer.data()), static_cast(buffer.size()), 0, reinterpret_cast(&from), &from_size); if (n <= 0) continue; const std::lock_guard lock{mutex}; // One client at a time: the first owns the relay; another sender takes over // only after the owner has been silent for a second (a reconnect, M10.6c). const bool owner = client && from.sin_port == client->sin_port && from.sin_addr.s_addr == client->sin_addr.s_addr; if (side == 0 && !owner) { if (client && now - client_seen < std::chrono::seconds{1}) continue; client = from; } if (side == 0) client_seen = now; admit(side == 0, {buffer.data(), buffer.data() + n}, now); } std::vector 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(p.bytes.data()), static_cast(p.bytes.size()), 0, reinterpret_cast(to), sizeof *to); const std::lock_guard lock{mutex}; ++counters.forwarded; } } } }; UdpRelay::UdpRelay(std::unique_ptr impl) : impl_{std::move(impl)} {} UdpRelay::~UdpRelay() { impl_->stop.store(true); #ifdef _WIN32 timeEndPeriod(1); #endif 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::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->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(&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; #ifdef _WIN32 // Delays are emulated with 1 ms waits; Windows' default timer tick (~15.6 ms) would // add its own latency to every forwarded packet. Balanced by the destructor. timeBeginPeriod(1); #endif std::unique_ptr 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