From 3b8ca6518111225d11da65694beb567d17174e88 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sat, 27 Jun 2026 22:17:21 +0800 Subject: [PATCH 01/29] lwip+socket+tcp work --- CMakeLists.txt | 2 + candy-cli/src/config.cc | 5 + candy-cli/src/config.h | 1 + candy.cfg | 9 + candy/CMakeLists.txt | 1 + candy/src/candy/client.cc | 1 + candy/src/core/client.cc | 27 ++ candy/src/core/client.h | 10 + candy/src/core/message.h | 1 + candy/src/netstack/lwip-port/arch/cc.h | 34 ++ candy/src/netstack/lwip-port/arch/perf.h | 7 + candy/src/netstack/lwip-port/arch/sys_arch.h | 7 + candy/src/netstack/lwip-port/sys_arch.c | 40 ++ candy/src/netstack/lwipopts.h | 61 +++ candy/src/netstack/netstack.cc | 323 ++++++++++++++ candy/src/netstack/netstack.h | 102 +++++ candy/src/netstack/reactor.cc | 242 +++++++++++ candy/src/netstack/reactor.h | 71 ++++ candy/src/netstack/session.cc | 8 + candy/src/netstack/session.h | 29 ++ candy/src/netstack/session_tcp.cc | 418 +++++++++++++++++++ candy/src/netstack/session_tcp.h | 99 +++++ candy/src/tun/tun.cc | 80 ++++ candy/src/tun/tun.h | 36 ++ cmake/lwip.cmake | 60 +++ 25 files changed, 1674 insertions(+) create mode 100644 candy/src/netstack/lwip-port/arch/cc.h create mode 100644 candy/src/netstack/lwip-port/arch/perf.h create mode 100644 candy/src/netstack/lwip-port/arch/sys_arch.h create mode 100644 candy/src/netstack/lwip-port/sys_arch.c create mode 100644 candy/src/netstack/lwipopts.h create mode 100644 candy/src/netstack/netstack.cc create mode 100644 candy/src/netstack/netstack.h create mode 100644 candy/src/netstack/reactor.cc create mode 100644 candy/src/netstack/reactor.h create mode 100644 candy/src/netstack/session.cc create mode 100644 candy/src/netstack/session.h create mode 100644 candy/src/netstack/session_tcp.cc create mode 100644 candy/src/netstack/session_tcp.h create mode 100644 cmake/lwip.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 20054c8a..204fd687 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,8 @@ endif() set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/lwip.cmake) + add_subdirectory(candy) add_subdirectory(candy-cli) add_subdirectory(candy-service) diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index b5eada23..a0899663 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -37,6 +37,7 @@ Poco::JSON::Object arguments::json() { config.set("port", this->port); config.set("vmac", virtualMac(this->name)); config.set("expt", loadTunAddress(this->name)); + config.set("forward-mode", this->forwardMode); } if (this->mode == "server") { @@ -125,6 +126,8 @@ int arguments::parse(int argc, char *argv[]) { "If omitted, auto-detected from the first physical network interface. (client only)") .metavar(""); + program.add_argument("--forward-mode").help("packet forward mode: kernel or userspace"); + program.add_group("Server options"); program.add_argument("-d", "--dhcp") @@ -182,6 +185,7 @@ int arguments::parse(int argc, char *argv[]) { program.set_if_used("--mtu", this->mtu); program.set_if_used("--discovery", this->discovery); program.set_if_used("--route", this->routeCost); + program.set_if_used("--forward-mode", this->forwardMode); bool needShowUsage = [&]() { if (this->mode != "client" && this->mode != "server") @@ -239,6 +243,7 @@ void arguments::parseFile(std::string cfgFile) { {"port", [&](const std::string &value) { this->port = std::stoi(value); }}, {"mtu", [&](const std::string &value) { this->mtu = std::stoi(value); }}, {"localhost", [&](const std::string &value) { this->localhost = value; }}, + {"forward-mode", [&](const std::string &value) { this->forwardMode = value; }}, }; auto trim = [](std::string str) { if (str.length() >= 2 && str.front() == '\"' && str.back() == '\"') { diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index dbd5e5b6..f8f2772a 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -31,6 +31,7 @@ struct arguments { int discovery = 0; int routeCost = 0; int mtu = 1400; + std::string forwardMode = "kernel"; }; int saveTunAddress(const std::string &name, const std::string &cidr); diff --git a/candy.cfg b/candy.cfg index 10c161b2..2a7ad572 100644 --- a/candy.cfg +++ b/candy.cfg @@ -65,3 +65,12 @@ route = 5 # [Optional] Maximum Transmission Unit #mtu=1400 + +# [Optional] Packet forward mode for landing gateway: "kernel" or "userspace" +# kernel (default): write decapsulated packets to the kernel tun device, relying +# on external iptables MASQUERADE / ip_forward for cross-LAN access. +# userspace: terminate the inner packet with an embedded userspace network stack +# (lwIP) and dial the target with a local kernel socket, replacing iptables +# MASQUERADE. Phase 1 supports TCP only. +#forward-mode = "kernel" + diff --git a/candy/CMakeLists.txt b/candy/CMakeLists.txt index 0cf000c5..bdb07613 100644 --- a/candy/CMakeLists.txt +++ b/candy/CMakeLists.txt @@ -17,6 +17,7 @@ endif() target_link_libraries(candy-library PRIVATE Poco::Foundation Poco::JSON Poco::Net Poco::NetSSL) target_link_libraries(candy-library PRIVATE Threads::Threads) +target_link_libraries(candy-library PRIVATE lwip) if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") target_link_libraries(candy-library PRIVATE ws2_32) diff --git a/candy/src/candy/client.cc b/candy/src/candy/client.cc index 004c712a..4143be2b 100644 --- a/candy/src/candy/client.cc +++ b/candy/src/candy/client.cc @@ -99,6 +99,7 @@ bool run(const std::string &id, const Poco::JSON::Object &config) { client->setRouteCost(config.getValue("route")), client->setMtu(config.getValue("mtu")); client->setPort(config.getValue("port")); client->setLocalhost(config.getValue("localhost")); + client->setForwardMode(config.optValue("forward-mode", "kernel")); client->run(); } candy::logger().information(Poco::format("run exit: id=%s ", id)); diff --git a/candy/src/core/client.cc b/candy/src/core/client.cc index 7e5847d9..8c41a781 100644 --- a/candy/src/core/client.cc +++ b/candy/src/core/client.cc @@ -62,6 +62,18 @@ MsgQueue &Client::getWsMsgQueue() { return this->wsMsgQueue; } +NetStack &Client::getNetStack() { + return this->netstack; +} + +std::string Client::getForwardMode() const { + return this->forwardMode; +} + +int Client::getMtu() const { + return this->mtu; +} + void Client::setPassword(const std::string &password) { ws.setPassword(password); peerManager.setPassword(password); @@ -104,12 +116,23 @@ void Client::setLocalhost(std::string ip) { } void Client::setMtu(int mtu) { + this->mtu = mtu; tun.setMTU(mtu); } +void Client::setForwardMode(const std::string &mode) { + this->forwardMode = mode; +} + void Client::run() { this->running.store(true); + if (this->forwardMode == "userspace") { + if (netstack.run(this)) { + return; + } + } + if (ws.run(this)) { return; } @@ -123,6 +146,10 @@ void Client::run() { ws.wait(); tun.wait(); peerManager.wait(); + if (this->forwardMode == "userspace") { + netstack.shutdown(); + netstack.wait(); + } wsMsgQueue.clear(); tunMsgQueue.clear(); diff --git a/candy/src/core/client.h b/candy/src/core/client.h index 61fd6795..2d16326d 100644 --- a/candy/src/core/client.h +++ b/candy/src/core/client.h @@ -3,6 +3,7 @@ #define CANDY_CORE_CLIENT_H #include "core/message.h" +#include "netstack/netstack.h" #include "peer/manager.h" #include "tun/tun.h" #include "utils/atomic.h" @@ -42,6 +43,8 @@ class Client { void setExptTunAddress(const std::string &cidr); void setVirtualMac(const std::string &vmac); + void setForwardMode(const std::string &mode); + void run(); bool isRunning(); void shutdown(); @@ -58,15 +61,22 @@ class Client { MsgQueue &getPeerMsgQueue(); MsgQueue &getWsMsgQueue(); + NetStack &getNetStack(); + std::string getForwardMode() const; + int getMtu() const; + private: MsgQueue tunMsgQueue, peerMsgQueue, wsMsgQueue; Tun tun; PeerManager peerManager; WebSocketClient ws; + NetStack netstack; private: std::string tunName; + std::string forwardMode = "kernel"; + int mtu = 1400; }; } // namespace candy diff --git a/candy/src/core/message.h b/candy/src/core/message.h index 0d3f6fa8..fb263ef2 100644 --- a/candy/src/core/message.h +++ b/candy/src/core/message.h @@ -16,6 +16,7 @@ enum class MsgKind { TRYP2P, PUBINFO, DISCOVERY, + NETSTACK_RX, }; struct Msg { diff --git a/candy/src/netstack/lwip-port/arch/cc.h b/candy/src/netstack/lwip-port/arch/cc.h new file mode 100644 index 00000000..78b6a6c6 --- /dev/null +++ b/candy/src/netstack/lwip-port/arch/cc.h @@ -0,0 +1,34 @@ +#ifndef CANDY_NETSTACK_LWIP_PORT_ARCH_CC_H +#define CANDY_NETSTACK_LWIP_PORT_ARCH_CC_H + +#if defined(_WIN32) || defined(_WIN64) +#else +#if defined __ANDROID__ +#define LWIP_UNIX_ANDROID +#elif defined __linux__ +#define LWIP_UNIX_LINUX +#elif defined __APPLE__ +#define LWIP_UNIX_MACH +#endif +#endif + +#define LWIP_TIMEVAL_PRIVATE 0 +#include + +#define LWIP_ERRNO_INCLUDE + +#if defined(LWIP_UNIX_LINUX) || defined(LWIP_UNIX_ANDROID) +#define LWIP_ERRNO_STDINCLUDE 1 +#endif + +extern unsigned int lwip_port_rand(void); +#define LWIP_RAND() (lwip_port_rand()) + +#if defined(LWIP_UNIX_MACH) +#include +#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS +#endif + +typedef unsigned int sys_prot_t; + +#endif diff --git a/candy/src/netstack/lwip-port/arch/perf.h b/candy/src/netstack/lwip-port/arch/perf.h new file mode 100644 index 00000000..6961de23 --- /dev/null +++ b/candy/src/netstack/lwip-port/arch/perf.h @@ -0,0 +1,7 @@ +#ifndef CANDY_NETSTACK_LWIP_PORT_ARCH_PERF_H +#define CANDY_NETSTACK_LWIP_PORT_ARCH_PERF_H + +#define PERF_START +#define PERF_STOP(x) + +#endif diff --git a/candy/src/netstack/lwip-port/arch/sys_arch.h b/candy/src/netstack/lwip-port/arch/sys_arch.h new file mode 100644 index 00000000..55418daa --- /dev/null +++ b/candy/src/netstack/lwip-port/arch/sys_arch.h @@ -0,0 +1,7 @@ +#ifndef CANDY_NETSTACK_LWIP_PORT_ARCH_SYS_ARCH_H +#define CANDY_NETSTACK_LWIP_PORT_ARCH_SYS_ARCH_H + +#define SYS_MBOX_NULL NULL +#define SYS_SEM_NULL NULL + +#endif diff --git a/candy/src/netstack/lwip-port/sys_arch.c b/candy/src/netstack/lwip-port/sys_arch.c new file mode 100644 index 00000000..9cd35b45 --- /dev/null +++ b/candy/src/netstack/lwip-port/sys_arch.c @@ -0,0 +1,40 @@ +#include "lwip/opt.h" +#include "lwip/sys.h" + +#include +#include + +#if defined(__APPLE__) +#include +#endif + +u32_t lwip_port_rand(void) { + return (u32_t)rand(); +} + +static void get_monotonic_time(struct timespec *ts) { +#if defined(__APPLE__) + u64_t t = mach_absolute_time(); + mach_timebase_info_data_t timebase_info = {0, 0}; + mach_timebase_info(&timebase_info); + u64_t nano = (t * timebase_info.numer) / (timebase_info.denom); + u64_t sec = nano / 1000000000L; + nano -= sec * 1000000000L; + ts->tv_sec = sec; + ts->tv_nsec = nano; +#else + clock_gettime(CLOCK_MONOTONIC, ts); +#endif +} + +u32_t sys_now(void) { + struct timespec ts; + get_monotonic_time(&ts); + return (u32_t)(ts.tv_sec * 1000L + ts.tv_nsec / 1000000L); +} + +u32_t sys_jiffies(void) { + struct timespec ts; + get_monotonic_time(&ts); + return (u32_t)(ts.tv_sec * 1000000000L + ts.tv_nsec); +} diff --git a/candy/src/netstack/lwipopts.h b/candy/src/netstack/lwipopts.h new file mode 100644 index 00000000..9e9fbfcf --- /dev/null +++ b/candy/src/netstack/lwipopts.h @@ -0,0 +1,61 @@ +#ifndef CANDY_NETSTACK_LWIPOPTS_H +#define CANDY_NETSTACK_LWIPOPTS_H + +#define NO_SYS 1 +#define LWIP_NETCONN 0 +#define LWIP_SOCKET 0 +#define SYS_LIGHTWEIGHT_PROT 0 + +#define LWIP_TIMERS 1 + +#define IP_FORWARD 0 + +#define LWIP_TCP 1 +#define LWIP_UDP 0 +#define LWIP_RAW 0 +#define LWIP_ICMP 1 +#define LWIP_DNS 0 +#define LWIP_DHCP 0 +#define LWIP_AUTOIP 0 +#define LWIP_IGMP 0 +#define LWIP_ARP 0 +#define LWIP_ETHERNET 0 +#define LWIP_NETIF_API 0 +#define LWIP_NETIF_LOOPBACK 0 +#define LWIP_HAVE_LOOPIF 0 + +#define LWIP_IPV4 1 +#define LWIP_IPV6 0 + +#define MEM_ALIGNMENT 4U +#define MEM_LIBC_MALLOC 1 +#define MEMP_MEM_MALLOC 1 + +#define MEMP_NUM_TCP_PCB 4096 +#define MEMP_NUM_TCP_PCB_LISTEN 16 +#define MEMP_NUM_TCP_SEG 4096 +#define MEMP_NUM_PBUF 4096 +#define MEMP_NUM_REASSDATA 1 +#define PBUF_POOL_SIZE 0 + +#define TCP_MSS 1460 +#define TCP_WND (32 * TCP_MSS) +#define TCP_SND_BUF (32 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * TCP_SND_BUF) / TCP_MSS) +#define TCP_QUEUE_OOSEQ 1 + +#define LWIP_NETIF_STATUS_CALLBACK 0 +#define LWIP_NETIF_LINK_CALLBACK 0 +#define LWIP_NETIF_HOSTNAME 0 + +#define CHECKSUM_GEN_IP 1 +#define CHECKSUM_GEN_TCP 1 +#define CHECKSUM_CHECK_IP 1 +#define CHECKSUM_CHECK_TCP 1 + +#define LWIP_STATS 0 +#define LWIP_STATS_DISPLAY 0 + +#define LWIP_DEBUG 0 + +#endif diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc new file mode 100644 index 00000000..ab484c8c --- /dev/null +++ b/candy/src/netstack/netstack.cc @@ -0,0 +1,323 @@ +// SPDX-License-Identifier: MIT +#include "netstack/netstack.h" +#include "core/client.h" +#include "core/message.h" +#include "netstack/session_tcp.h" +#include +#include +#include +#include + +#include "lwip/init.h" +#include "lwip/pbuf.h" +#include "lwip/priv/tcp_priv.h" +#include "lwip/tcp.h" +#include "lwip/timeouts.h" +#include "netif/etharp.h" + +namespace candy { + +NetStack::NetStack() : client(nullptr), running(false), listenPcb(nullptr) { + std::memset(&this->lwipNetif, 0, sizeof(this->lwipNetif)); +} + +NetStack::~NetStack() { + shutdown(); + wait(); +} + +Client *NetStack::getClient() { + return this->client; +} + +Reactor &NetStack::getReactor() { + return this->reactor; +} + +int NetStack::run(Client *client) { + this->client = client; + this->running.store(true); + + if (this->reactor.start()) { + spdlog::critical("netstack reactor start failed"); + return -1; + } + + this->stackThread = std::thread([this] { + spdlog::debug("start thread: netstack"); + try { + if (initStack()) { + spdlog::critical("netstack init failed"); + getClient()->shutdown(); + } else { + loop(); + teardownStack(); + } + } catch (const std::exception &e) { + spdlog::error("netstack thread exception: {}", e.what()); + getClient()->shutdown(); + } + spdlog::debug("stop thread: netstack"); + }); + return 0; +} + +int NetStack::wait() { + if (this->stackThread.joinable()) { + this->stackThread.join(); + } + this->reactor.stop(); + return 0; +} + +void NetStack::shutdown() { + this->running.store(false); + this->stackTaskCond.notify_all(); +} + +void NetStack::input(std::string packet) { + if (!this->running.load()) { + return; + } + postToStack([this, packet = std::move(packet)]() mutable { handleInput(std::move(packet)); }); +} + +void NetStack::postToStack(std::function task) { + { + std::unique_lock lock(this->stackTaskMutex); + this->stackTasks.push(std::move(task)); + } + this->stackTaskCond.notify_one(); +} + +int NetStack::initStack() { + static std::once_flag lwipInitFlag; + std::call_once(lwipInitFlag, [] { lwip_init(); }); + + ip4_addr_t addr, mask, gw; + IP4_ADDR(&addr, 10, 255, 255, 254); + IP4_ADDR(&mask, 255, 255, 255, 255); + ip4_addr_set_any(&gw); + + if (netif_add(&this->lwipNetif, &addr, &mask, &gw, this, netifInitTrampoline, ip_input) == nullptr) { + spdlog::critical("netstack netif_add failed"); + return -1; + } + netif_set_up(&this->lwipNetif); + netif_set_link_up(&this->lwipNetif); + netif_set_default(&this->lwipNetif); + netif_set_flags(&this->lwipNetif, NETIF_FLAG_PRETEND_TCP); + + // netif MTU 必须扣除 IPIP 外层头(20B),否则落地端按 1500 协商出的满载内层包 + // 封装后超过隧道 tun MTU(client mtu),导致大包被丢弃、TCP 重传超时。 + int tunMtu = this->client ? this->client->getMtu() : 1400; + int netifMtu = tunMtu - (int)sizeof(IP4Header); + if (netifMtu < 576) { + netifMtu = 576; + } + this->lwipNetif.mtu = (u16_t)netifMtu; + + struct tcp_pcb *pcb = tcp_new_ip_type(IPADDR_TYPE_ANY); + if (pcb == nullptr) { + spdlog::critical("netstack tcp_new failed"); + return -1; + } + tcp_bind_netif(pcb, &this->lwipNetif); + if (tcp_bind(pcb, nullptr, 0) != ERR_OK) { + spdlog::critical("netstack tcp_bind failed"); + return -1; + } + this->listenPcb = tcp_listen(pcb); + if (this->listenPcb == nullptr) { + spdlog::critical("netstack tcp_listen failed"); + return -1; + } + tcp_arg(this->listenPcb, this); + tcp_accept(this->listenPcb, acceptTrampoline); + return 0; +} + +void NetStack::teardownStack() { + // lwIP 进程级单例:重连时必须清理本次注册的 netif / pcb / 会话, + // 否则 netif 会在全局 netif_list 中累积,最终触发 "too many netifs" 断言崩溃。 + { + std::unique_lock lock(this->sessionMutex); + for (auto &kv : this->sessions) { + if (kv.second) { + kv.second->shutdownFromStack(); + } + } + this->sessions.clear(); + } + { + std::unique_lock lock(this->peerMapMutex); + this->vnetPeerMap.clear(); + } + if (this->listenPcb != nullptr) { + tcp_close(this->listenPcb); + this->listenPcb = nullptr; + } + netif_remove(&this->lwipNetif); + std::memset(&this->lwipNetif, 0, sizeof(this->lwipNetif)); +} + +void NetStack::loop() { + while (this->running.load()) { + std::function task; + { + std::unique_lock lock(this->stackTaskMutex); + this->stackTaskCond.wait_for(lock, std::chrono::milliseconds(250), + [this] { return !this->stackTasks.empty() || !this->running.load(); }); + if (!this->stackTasks.empty()) { + task = std::move(this->stackTasks.front()); + this->stackTasks.pop(); + } + } + if (task) { + task(); + } + sys_check_timeouts(); + } +} + +void NetStack::handleInput(std::string packet) { + if (packet.size() < sizeof(IP4Header)) { + return; + } + IP4Header *header = (IP4Header *)packet.data(); + IP4 vnetPeer; + if (header->isIPIP()) { + vnetPeer = header->saddr; + packet.erase(0, sizeof(IP4Header)); + } + if (packet.size() < sizeof(IP4Header)) { + return; + } + IP4Header *inner = (IP4Header *)packet.data(); + spdlog::debug("netstack input: vnetPeer={} {} -> {} proto={}", vnetPeer.toString(), inner->saddr.toString(), + inner->daddr.toString(), (int)inner->protocol); + feedToLwip(packet, vnetPeer); +} + +void NetStack::feedToLwip(const std::string &innerPacket, IP4 vnetPeer) { + IP4Header *header = (IP4Header *)innerPacket.data(); + if (!vnetPeer.empty()) { + auto now = std::chrono::steady_clock::now(); + std::unique_lock lock(this->peerMapMutex); + this->vnetPeerMap[uint32_t(header->saddr)] = PeerEntry{vnetPeer, now}; + // 惰性老化清理:表过大时清掉 180s 空闲过期项,避免长期运行无界累积。 + if (this->vnetPeerMap.size() > 4096) { + for (auto it = this->vnetPeerMap.begin(); it != this->vnetPeerMap.end();) { + if (now - it->second.lastSeen > std::chrono::seconds(180)) { + it = this->vnetPeerMap.erase(it); + } else { + ++it; + } + } + } + } + + struct pbuf *p = pbuf_alloc(PBUF_RAW, innerPacket.size(), PBUF_RAM); + if (p == nullptr) { + spdlog::warn("netstack pbuf_alloc failed"); + return; + } + pbuf_take(p, innerPacket.data(), innerPacket.size()); + err_t e = this->lwipNetif.input(p, &this->lwipNetif); + if (e != ERR_OK) { + spdlog::warn("netstack netif.input failed: {}", (int)e); + pbuf_free(p); + } +} + +void NetStack::output(const std::string &innerPacket) { + if (innerPacket.size() < sizeof(IP4Header)) { + return; + } + IP4Header *inner = (IP4Header *)innerPacket.data(); + IP4 vnetPeer; + { + std::unique_lock lock(this->peerMapMutex); + auto it = this->vnetPeerMap.find(uint32_t(inner->daddr)); + if (it != this->vnetPeerMap.end()) { + vnetPeer = it->second.peer; + it->second.lastSeen = std::chrono::steady_clock::now(); + } + } + if (vnetPeer.empty()) { + spdlog::warn("netstack output drop: no vnetPeer for {}", inner->daddr.toString()); + return; + } + + std::string buffer = innerPacket; + buffer.insert(0, sizeof(IP4Header), 0); + IP4Header *outer = (IP4Header *)buffer.data(); + outer->protocol = 0x04; + outer->saddr = this->client->address(); + outer->daddr = vnetPeer; + + this->client->getPeerMsgQueue().write(Msg(MsgKind::PACKET, std::move(buffer))); +} + +void NetStack::removeSession(struct tcp_pcb *pcb) { + std::unique_lock lock(this->sessionMutex); + this->sessions.erase(pcb); +} + +err_t NetStack::netifInitTrampoline(struct netif *netif) { + NetStack *self = (NetStack *)netif->state; + return self->onNetifInit(netif); +} + +err_t NetStack::outputTrampoline(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr) { + NetStack *self = (NetStack *)netif->state; + return self->onOutput(netif, p, ipaddr); +} + +err_t NetStack::acceptTrampoline(void *arg, struct tcp_pcb *newpcb, err_t err) { + NetStack *self = (NetStack *)arg; + return self->onAccept(newpcb, err); +} + +err_t NetStack::onNetifInit(struct netif *netif) { + netif->name[0] = 'c'; + netif->name[1] = 'd'; + netif->mtu = 1500; + netif->output = outputTrampoline; + return ERR_OK; +} + +err_t NetStack::onOutput(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr) { + std::string buffer; + buffer.resize(p->tot_len); + pbuf_copy_partial(p, buffer.data(), p->tot_len, 0); + if (buffer.size() >= sizeof(IP4Header)) { + IP4Header *h = (IP4Header *)buffer.data(); + spdlog::debug("netstack onOutput: {} -> {} proto={} len={}", h->saddr.toString(), h->daddr.toString(), + (int)h->protocol, buffer.size()); + } + output(buffer); + return ERR_OK; +} + +err_t NetStack::onAccept(struct tcp_pcb *newpcb, err_t err) { + spdlog::debug("netstack onAccept: err={} pcb={}", (int)err, (void *)newpcb); + if (err != ERR_OK || newpcb == nullptr) { + return ERR_VAL; + } + if (!this->running.load()) { + return ERR_RST; + } + + auto session = std::make_shared(this, newpcb); + if (session->start()) { + return ERR_RST; + } + { + std::unique_lock lock(this->sessionMutex); + this->sessions[newpcb] = session; + } + return ERR_OK; +} + +} // namespace candy diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h new file mode 100644 index 00000000..b3154b13 --- /dev/null +++ b/candy/src/netstack/netstack.h @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_NETSTACK_H +#define CANDY_NETSTACK_NETSTACK_H + +#include "core/net.h" +#include "netstack/reactor.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "lwip/ip_addr.h" +#include "lwip/netif.h" +#include "lwip/tcp.h" + +namespace candy { + +class Client; +class SessionTcp; + +// NetStack:独占 lwIP(NO_SYS=1 单线程 raw API)的模块。 +// 持有一个 NetStack 线程驱动 lwIP 收发与定时器,一个 Reactor 线程管理落地 fd。 +// 落地端在 userspace 模式下,把 IPIP 内层包喂进 lwIP 终结,TCP 由内核 socket 直连目标。 +class NetStack { +public: + NetStack(); + ~NetStack(); + + int run(Client *client); + int wait(); + void shutdown(); + + // 把待终结的 IP 包投递给 NetStack(来自 tun 落地分支,含 IPIP 外层)。 + void input(std::string packet); + + // 供 Session 使用:在 NetStack 线程内执行任务(保证 lwIP API 线程安全)。 + void postToStack(std::function task); + Reactor &getReactor(); + + // 回包:lwIP netif->output 产生的 IP 包,按连接上下文重新 IPIP 封装送回源端。 + void output(const std::string &innerPacket); + + // Session 结束时从会话表移除。 + void removeSession(struct tcp_pcb *pcb); + + Client *getClient(); + +private: + int initStack(); + void teardownStack(); + void loop(); + void handleInput(std::string packet); + void feedToLwip(const std::string &innerPacket, IP4 vnetPeer); + + // lwIP 回调跳板 + static err_t netifInitTrampoline(struct netif *netif); + static err_t outputTrampoline(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr); + static err_t acceptTrampoline(void *arg, struct tcp_pcb *newpcb, err_t err); + + err_t onNetifInit(struct netif *netif); + err_t onOutput(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr); + err_t onAccept(struct tcp_pcb *newpcb, err_t err); + +private: + Client *client; + + std::thread stackThread; + std::atomic running; + + struct netif lwipNetif; + struct tcp_pcb *listenPcb; + + Reactor reactor; + + // NetStack 线程任务队列(用带超时读驱动定时器) + std::mutex stackTaskMutex; + std::condition_variable stackTaskCond; + std::queue> stackTasks; + + // 内层源 IP(dev1) -> vnetPeer(源网关虚拟IP),供回包寻址。 + // 带最近活跃时间戳,惰性老化清理(180s 空闲过期),避免长期运行无界累积。 + struct PeerEntry { + IP4 peer; + std::chrono::steady_clock::time_point lastSeen; + }; + std::mutex peerMapMutex; + std::unordered_map vnetPeerMap; + + std::mutex sessionMutex; + std::unordered_map> sessions; +}; + +} // namespace candy + +#endif diff --git a/candy/src/netstack/reactor.cc b/candy/src/netstack/reactor.cc new file mode 100644 index 00000000..5e7d0177 --- /dev/null +++ b/candy/src/netstack/reactor.cc @@ -0,0 +1,242 @@ +// SPDX-License-Identifier: MIT +#include "netstack/reactor.h" +#include + +#if defined(__linux__) +#include +#include +#include +#endif + +namespace candy { + +#if defined(__linux__) + +static uint32_t toEpollEvents(ReactorEvent events) { + uint32_t ev = 0; + if (events & ReactorEvent::READ) { + ev |= EPOLLIN; + } + if (events & ReactorEvent::WRITE) { + ev |= EPOLLOUT; + } + return ev; +} + +static ReactorEvent fromEpollEvents(uint32_t ev) { + ReactorEvent events = ReactorEvent::NONE; + if (ev & EPOLLIN) { + events = events | ReactorEvent::READ; + } + if (ev & EPOLLOUT) { + events = events | ReactorEvent::WRITE; + } + if (ev & (EPOLLERR | EPOLLHUP)) { + events = events | ReactorEvent::ERROR; + } + return events; +} + +Reactor::Reactor() : epollFd(-1), wakeupFd(-1), running(false) {} + +Reactor::~Reactor() { + stop(); +} + +int Reactor::start() { + this->epollFd = epoll_create1(EPOLL_CLOEXEC); + if (this->epollFd < 0) { + spdlog::error("reactor epoll_create1 failed: {}", strerror(errno)); + return -1; + } + + this->wakeupFd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); + if (this->wakeupFd < 0) { + spdlog::error("reactor eventfd failed: {}", strerror(errno)); + ::close(this->epollFd); + this->epollFd = -1; + return -1; + } + + struct epoll_event ee = {}; + ee.events = EPOLLIN; + ee.data.fd = this->wakeupFd; + if (epoll_ctl(this->epollFd, EPOLL_CTL_ADD, this->wakeupFd, &ee) != 0) { + spdlog::error("reactor add wakeupFd failed: {}", strerror(errno)); + ::close(this->wakeupFd); + ::close(this->epollFd); + this->wakeupFd = -1; + this->epollFd = -1; + return -1; + } + + this->running.store(true); + this->thread = std::thread([this] { this->loop(); }); + return 0; +} + +void Reactor::stop() { + if (!this->running.exchange(false)) { + if (this->thread.joinable()) { + this->thread.join(); + } + return; + } + wakeup(); + if (this->thread.joinable()) { + this->thread.join(); + } + if (this->wakeupFd >= 0) { + ::close(this->wakeupFd); + this->wakeupFd = -1; + } + if (this->epollFd >= 0) { + ::close(this->epollFd); + this->epollFd = -1; + } +} + +int Reactor::add(int fd, ReactorEvent events, EventHandler handler) { + { + std::unique_lock lock(this->handlerMutex); + this->handlers[fd] = std::move(handler); + } + struct epoll_event ee = {}; + ee.events = toEpollEvents(events); + ee.data.fd = fd; + if (epoll_ctl(this->epollFd, EPOLL_CTL_ADD, fd, &ee) != 0) { + spdlog::error("reactor add fd {} failed: {}", fd, strerror(errno)); + std::unique_lock lock(this->handlerMutex); + this->handlers.erase(fd); + return -1; + } + return 0; +} + +int Reactor::mod(int fd, ReactorEvent events) { + struct epoll_event ee = {}; + ee.events = toEpollEvents(events); + ee.data.fd = fd; + if (epoll_ctl(this->epollFd, EPOLL_CTL_MOD, fd, &ee) != 0) { + spdlog::error("reactor mod fd {} failed: {}", fd, strerror(errno)); + return -1; + } + return 0; +} + +int Reactor::del(int fd) { + epoll_ctl(this->epollFd, EPOLL_CTL_DEL, fd, nullptr); + std::unique_lock lock(this->handlerMutex); + this->handlers.erase(fd); + return 0; +} + +void Reactor::post(Task task) { + { + std::unique_lock lock(this->taskMutex); + this->tasks.push(std::move(task)); + } + wakeup(); +} + +void Reactor::wakeup() { + uint64_t one = 1; + if (this->wakeupFd >= 0) { + ssize_t n = ::write(this->wakeupFd, &one, sizeof(one)); + (void)n; + } +} + +void Reactor::drainWakeup() { + uint64_t buf; + while (::read(this->wakeupFd, &buf, sizeof(buf)) > 0) { + } +} + +void Reactor::drainTasks() { + std::queue pending; + { + std::unique_lock lock(this->taskMutex); + std::swap(pending, this->tasks); + } + while (!pending.empty()) { + pending.front()(); + pending.pop(); + } +} + +void Reactor::loop() { + spdlog::debug("start thread: netstack reactor"); + constexpr int MAX_EVENTS = 256; + struct epoll_event events[MAX_EVENTS]; + while (this->running.load()) { + int n = epoll_wait(this->epollFd, events, MAX_EVENTS, -1); + if (n < 0) { + if (errno == EINTR) { + continue; + } + spdlog::error("reactor epoll_wait failed: {}", strerror(errno)); + break; + } + for (int i = 0; i < n; ++i) { + int fd = events[i].data.fd; + if (fd == this->wakeupFd) { + drainWakeup(); + drainTasks(); + continue; + } + EventHandler handler; + { + std::unique_lock lock(this->handlerMutex); + auto it = this->handlers.find(fd); + if (it == this->handlers.end()) { + continue; + } + handler = it->second; + } + handler(fromEpollEvents(events[i].events)); + } + } + spdlog::debug("stop thread: netstack reactor"); +} + +#else + +Reactor::Reactor() : epollFd(-1), wakeupFd(-1), running(false) {} + +Reactor::~Reactor() { + stop(); +} + +int Reactor::start() { + spdlog::error("reactor not implemented on this platform yet"); + return -1; +} + +void Reactor::stop() {} + +int Reactor::add(int, ReactorEvent, EventHandler) { + return -1; +} + +int Reactor::mod(int, ReactorEvent) { + return -1; +} + +int Reactor::del(int) { + return -1; +} + +void Reactor::post(Task) {} + +void Reactor::loop() {} + +void Reactor::wakeup() {} + +void Reactor::drainTasks() {} + +void Reactor::drainWakeup() {} + +#endif + +} // namespace candy diff --git a/candy/src/netstack/reactor.h b/candy/src/netstack/reactor.h new file mode 100644 index 00000000..b8aa9612 --- /dev/null +++ b/candy/src/netstack/reactor.h @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_REACTOR_H +#define CANDY_NETSTACK_REACTOR_H + +#include +#include +#include +#include +#include +#include +#include + +namespace candy { + +enum class ReactorEvent : uint32_t { + NONE = 0, + READ = 1 << 0, + WRITE = 1 << 1, + ERROR = 1 << 2, +}; + +inline ReactorEvent operator|(ReactorEvent a, ReactorEvent b) { + return static_cast(static_cast(a) | static_cast(b)); +} + +inline bool operator&(ReactorEvent a, ReactorEvent b) { + return (static_cast(a) & static_cast(b)) != 0; +} + +// Reactor:基于 epoll 的事件循环(阶段一 Linux 优先;macOS kqueue / Windows IOCP 后续补)。 +// 管理所有落地 fd(内核 socket)的可读/可写/错误事件,非阻塞、事件回调驱动。 +// 跨线程提交任务用 post(),由 reactor 线程在事件循环中执行(经 eventfd 唤醒)。 +class Reactor { +public: + using EventHandler = std::function; + using Task = std::function; + + Reactor(); + ~Reactor(); + + int start(); + void stop(); + + int add(int fd, ReactorEvent events, EventHandler handler); + int mod(int fd, ReactorEvent events); + int del(int fd); + + void post(Task task); + +private: + void loop(); + void wakeup(); + void drainTasks(); + void drainWakeup(); + + int epollFd; + int wakeupFd; + + std::thread thread; + std::atomic running; + + std::mutex handlerMutex; + std::unordered_map handlers; + + std::mutex taskMutex; + std::queue tasks; +}; + +} // namespace candy + +#endif diff --git a/candy/src/netstack/session.cc b/candy/src/netstack/session.cc new file mode 100644 index 00000000..bcdae316 --- /dev/null +++ b/candy/src/netstack/session.cc @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: MIT +#include "netstack/session.h" + +namespace candy { + +Session::Session(NetStack *stack) : stack(stack), closing(false) {} + +} // namespace candy diff --git a/candy/src/netstack/session.h b/candy/src/netstack/session.h new file mode 100644 index 00000000..7fe5e362 --- /dev/null +++ b/candy/src/netstack/session.h @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_SESSION_H +#define CANDY_NETSTACK_SESSION_H + +#include +#include + +namespace candy { + +class NetStack; + +// Session 基类:会话生命周期的公共抽象。 +// 阶段一仅有 TCP 会话;UDP/ICMP 会话在阶段二加入。 +class Session { +public: + explicit Session(NetStack *stack); + virtual ~Session() = default; + + // 启动落地:建立内核 socket、发起连接、注册到 reactor。 + virtual int start() = 0; + +protected: + NetStack *stack; + std::atomic closing; +}; + +} // namespace candy + +#endif diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc new file mode 100644 index 00000000..cd32024a --- /dev/null +++ b/candy/src/netstack/session_tcp.cc @@ -0,0 +1,418 @@ +// SPDX-License-Identifier: MIT +#include "netstack/session_tcp.h" +#include "netstack/netstack.h" +#include +#include +#include + +#include "lwip/pbuf.h" +#include "lwip/tcp.h" + +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#include +#include +#include +#endif + +namespace candy { + +// fd -> lwIP 方向背压水位:积压达到 HIGH_WATER 暂停从落地 fd 读取,回落到 LOW_WATER 恢复。 +// 取值需高于 lwIP 单条 TCP 发送缓冲(TCP_SND_BUF≈46KB),留出在途 + 任务队列余量。 +static constexpr size_t HIGH_WATER = 256 * 1024; +static constexpr size_t LOW_WATER = 64 * 1024; + +SessionTcp::SessionTcp(NetStack *stack, struct tcp_pcb *pcb) + : Session(stack), pcb(pcb), fd(-1), connected(false), origDstPort(0), wantWrite(false), fdEof(false) { + std::memcpy(&this->origDst, &ip_2_ip4(&pcb->local_ip)->addr, sizeof(uint32_t)); + this->origDstPort = pcb->local_port; + std::memcpy(&this->origSrc, &ip_2_ip4(&pcb->remote_ip)->addr, sizeof(uint32_t)); +} + +SessionTcp::~SessionTcp() { + if (this->fd >= 0) { + ::close(this->fd); + this->fd = -1; + } +} + +int SessionTcp::start() { + this->self = shared_from_this(); + + tcp_arg(this->pcb, this); + tcp_recv(this->pcb, recvTrampoline); + tcp_sent(this->pcb, sentTrampoline); + tcp_err(this->pcb, errTrampoline); + +#if defined(__linux__) || defined(__APPLE__) + this->fd = ::socket(AF_INET, SOCK_STREAM, 0); + if (this->fd < 0) { + spdlog::warn("session tcp socket failed: {}", strerror(errno)); + return -1; + } + int flags = ::fcntl(this->fd, F_GETFL, 0); + ::fcntl(this->fd, F_SETFL, flags | O_NONBLOCK); + + struct sockaddr_in dst = {}; + dst.sin_family = AF_INET; + dst.sin_port = htons(this->origDstPort); + dst.sin_addr.s_addr = uint32_t(this->origDst); + + int ret = ::connect(this->fd, (struct sockaddr *)&dst, sizeof(dst)); + if (ret != 0 && errno != EINPROGRESS) { + spdlog::warn("session tcp connect {}:{} failed: {}", this->origDst.toString(), this->origDstPort, + strerror(errno)); + ::close(this->fd); + this->fd = -1; + return -1; + } + + spdlog::debug("session tcp: {} -> {}:{}", this->origSrc.toString(), this->origDst.toString(), this->origDstPort); + + auto holder = shared_from_this(); + int fd = this->fd; + this->stack->getReactor().add(fd, ReactorEvent::WRITE, [holder](ReactorEvent ev) { holder->onFdEvent((uint32_t)ev); }); + return 0; +#else + return -1; +#endif +} + +// ===================== NetStack 线程 ===================== + +err_t SessionTcp::recvTrampoline(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { + SessionTcp *self = (SessionTcp *)arg; + return self->onRecv(pcb, p, err); +} + +err_t SessionTcp::sentTrampoline(void *arg, struct tcp_pcb *pcb, u16_t len) { + SessionTcp *self = (SessionTcp *)arg; + return self->onSent(pcb, len); +} + +void SessionTcp::errTrampoline(void *arg, err_t err) { + SessionTcp *self = (SessionTcp *)arg; + self->onErr(err); +} + +err_t SessionTcp::onRecv(struct tcp_pcb *pcb, struct pbuf *p, err_t err) { + if (p == nullptr || err != ERR_OK) { + // 对端(lwIP 侧)关闭:通知 reactor 关闭落地 fd + if (p != nullptr) { + pbuf_free(p); + } + closeFromStack(); + return ERR_OK; + } + + std::string data; + data.resize(p->tot_len); + pbuf_copy_partial(p, data.data(), p->tot_len, 0); + pbuf_free(p); + + { + std::unique_lock lock(this->bufMutex); + this->forwardBuf.append(data); + } + // 注意:不在此处 tcp_recved!接收窗口的推进延迟到数据真正写进落地 fd 之后 + // (flushForwardLocked 累加 pendingRecved -> NetStack 线程 ackRecvedToLwip)。 + // 否则会向源端谎报"已收下",落地端写得慢时 forwardBuf 无界膨胀直至 OOM。 + + auto holder = shared_from_this(); + this->stack->getReactor().post([holder] { holder->flushForwardLocked(); }); + return ERR_OK; +} + +err_t SessionTcp::onSent(struct tcp_pcb *pcb, u16_t len) { + // sndbuf 释放了空间,继续把待发缓冲灌进 lwIP。 + pumpToLwip(); + return ERR_OK; +} + +void SessionTcp::onErr(err_t err) { + // pcb 已被 lwIP 释放 + this->pcb = nullptr; + closeFromReactor(); +} + +void SessionTcp::writeToLwip(const std::string &data) { + if (this->pcb == nullptr) { + return; + } + this->backwardBuf.append(data); + pumpToLwip(); +} + +void SessionTcp::pumpToLwip() { + // 仅在 NetStack 线程调用。把 backwardBuf 尽量写入 lwIP 发送缓冲, + // 写不下的保留,等 onSent 回调再续;并据缓冲水位对落地 fd 读做背压。 + if (this->pcb == nullptr) { + return; + } + size_t offset = 0; + bool written = false; + while (offset < this->backwardBuf.size()) { + u16_t space = tcp_sndbuf(this->pcb); + if (space == 0) { + break; + } + u16_t chunk = (u16_t)std::min((size_t)space, this->backwardBuf.size() - offset); + err_t e = tcp_write(this->pcb, this->backwardBuf.data() + offset, chunk, TCP_WRITE_FLAG_COPY); + if (e == ERR_MEM) { + break; + } + if (e != ERR_OK) { + return; + } + offset += chunk; + written = true; + } + if (offset > 0) { + this->backwardBuf.erase(0, offset); + // 这部分已真正进入 lwIP 发送缓冲,从积压中扣除。 + size_t prev = this->backwardBytes.fetch_sub(offset); + (void)prev; + } + if (written) { + tcp_output(this->pcb); + } + updateReadInterest(); + + // 落地端已 EOF 且待发缓冲已排空:此时才能安全关闭 lwIP 侧, + // 否则会丢掉尾部数据(之前 ~80% 处 connection closed prematurely 的根因)。 + if (this->fdEof && this->backwardBuf.empty()) { + closeFromStack(); + } +} + +void SessionTcp::onFdEofFromStack() { + this->fdEof = true; + // 尝试把残留数据灌完;若已空则 pumpToLwip 内部会触发 closeFromStack。 + pumpToLwip(); + if (this->backwardBuf.empty()) { + closeFromStack(); + } +} + +void SessionTcp::ackRecvedToLwip() { + // 仅 NetStack 线程调用。把已写进落地 fd 的字节向 lwIP 确认,推进接收窗口。 + // 多个 flush 的确认会被合并在一次调用里消费(exchange 取走累计值)。 + if (this->pcb == nullptr) { + return; + } + size_t acked = this->pendingRecved.exchange(0); + if (acked == 0) { + return; + } + // tcp_recved 的 len 是 u16_t,分批确认避免溢出。 + while (acked > 0) { + u16_t chunk = (u16_t)std::min(acked, (size_t)0xFFFF); + tcp_recved(this->pcb, chunk); + acked -= chunk; + } +} + +void SessionTcp::updateReadInterest() { + // 背压恢复(NetStack 线程):积压回落到低水位且当前处于暂停态时,恢复从落地 fd 读取。 + // 暂停动作在 Reactor 线程 onFdReadable 内同步完成(读到高水位即停),二者形成闭环: + // Reactor 读出 -> backwardBytes += -> 到高水位停读 + // NetStack pump 进 lwIP -> backwardBytes -= -> 到低水位恢复读 + // 如此 fd 读取速率被 lwIP 发送+ACK 回收节奏严格约束,不再淹没单线程 NetStack。 + if (this->readPaused.load() && this->backwardBytes.load() <= LOW_WATER) { + this->readPaused.store(false); + int fd = this->fd; + auto holder = shared_from_this(); + this->stack->getReactor().post([holder, fd] { + if (fd >= 0 && !holder->closing.load()) { + holder->stack->getReactor().mod(fd, ReactorEvent::READ); + } + }); + } +} + +void SessionTcp::closeFromStack() { + if (this->pcb != nullptr) { + tcp_arg(this->pcb, nullptr); + tcp_recv(this->pcb, nullptr); + tcp_sent(this->pcb, nullptr); + tcp_err(this->pcb, nullptr); + if (tcp_close(this->pcb) != ERR_OK) { + tcp_abort(this->pcb); + } + } + struct tcp_pcb *closedPcb = this->pcb; + this->pcb = nullptr; + + NetStack *stack = this->stack; + auto holder = shared_from_this(); + stack->getReactor().post([holder] { holder->closeFromReactor(); }); + if (closedPcb != nullptr) { + stack->removeSession(closedPcb); + } +} + +void SessionTcp::shutdownFromStack() { + // 栈拆除路径:在 NetStack 线程内直接 abort pcb(此时 netif 即将移除, + // 不能再依赖正常 tcp_close 的四次挥手)。fd 交由 reactor 关闭。 + if (this->pcb != nullptr) { + tcp_arg(this->pcb, nullptr); + tcp_recv(this->pcb, nullptr); + tcp_sent(this->pcb, nullptr); + tcp_err(this->pcb, nullptr); + tcp_abort(this->pcb); + this->pcb = nullptr; + } + if (!this->closing.exchange(true)) { + auto holder = shared_from_this(); + this->stack->getReactor().post([holder] { +#if defined(__linux__) || defined(__APPLE__) + if (holder->fd >= 0) { + holder->stack->getReactor().del(holder->fd); + ::close(holder->fd); + holder->fd = -1; + } +#endif + holder->self.reset(); + }); + } +} + +// ===================== Reactor 线程 ===================== + +void SessionTcp::onFdEvent(uint32_t events) { + ReactorEvent ev = (ReactorEvent)events; + if (!this->connected) { + if (ev & ReactorEvent::ERROR) { + closeFromReactor(); + return; + } + if (ev & ReactorEvent::WRITE) { + onConnected(); + } + return; + } + // 已连接:优先把可读数据/EOF 读干净(read 返回 0 走优雅排空,返回 -1 走关闭), + // 避免 ERROR/HUP 与可读数据同时到达时抢先关闭而丢失尾部数据。 + if (ev & (ReactorEvent::READ | ReactorEvent::ERROR)) { + onFdReadable(); + } + if (ev & ReactorEvent::WRITE) { + onFdWritable(); + } +} + +void SessionTcp::onConnected() { +#if defined(__linux__) || defined(__APPLE__) + int err = 0; + socklen_t len = sizeof(err); + if (::getsockopt(this->fd, SOL_SOCKET, SO_ERROR, &err, &len) != 0 || err != 0) { + spdlog::warn("session tcp connect result error: {}", strerror(err ? err : errno)); + closeFromReactor(); + return; + } + this->connected = true; + this->stack->getReactor().mod(this->fd, ReactorEvent::READ); + flushForwardLocked(); +#endif +} + +void SessionTcp::flushForwardLocked() { +#if defined(__linux__) || defined(__APPLE__) + if (this->fd < 0 || !this->connected) { + return; + } + size_t acked = 0; + { + std::unique_lock lock(this->bufMutex); + while (!this->forwardBuf.empty()) { + ssize_t n = ::write(this->fd, this->forwardBuf.data(), this->forwardBuf.size()); + if (n > 0) { + this->forwardBuf.erase(0, n); + acked += (size_t)n; + continue; + } + if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { + this->wantWrite = true; + this->stack->getReactor().mod(this->fd, ReactorEvent::READ | ReactorEvent::WRITE); + break; + } + lock.unlock(); + closeFromReactor(); + return; + } + if (this->forwardBuf.empty() && this->wantWrite) { + this->wantWrite = false; + this->stack->getReactor().mod(this->fd, ReactorEvent::READ); + } + } + // 已落地的字节交由 NetStack 线程向 lwIP 确认(推进接收窗口)。 + // 这才是真正的"收到",从而把源端发送速率约束在落地 fd 的消费能力之内。 + if (acked > 0) { + this->pendingRecved.fetch_add(acked); + auto holder = shared_from_this(); + this->stack->postToStack([holder] { holder->ackRecvedToLwip(); }); + } +#endif +} + +void SessionTcp::onFdWritable() { + flushForwardLocked(); +} + +void SessionTcp::onFdReadable() { +#if defined(__linux__) || defined(__APPLE__) + char buf[65536]; + while (true) { + // 同步背压:积压超过高水位立即停读,避免单线程 NetStack 被 writeToLwip 任务淹没。 + // 恢复由 NetStack 线程在 pumpToLwip(onSent 驱动)排空到低水位后触发。 + if (this->backwardBytes.load() >= HIGH_WATER) { + this->readPaused.store(true); + this->stack->getReactor().mod(this->fd, ReactorEvent::NONE); + return; + } + ssize_t n = ::read(this->fd, buf, sizeof(buf)); + if (n > 0) { + this->backwardBytes.fetch_add((size_t)n); + std::string data(buf, n); + auto holder = shared_from_this(); + this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->writeToLwip(data); }); + continue; + } + if (n == 0) { + // 落地端关闭:等 backwardBuf 灌完再关 lwIP 侧,避免尾部数据丢失。 + auto holder = shared_from_this(); + this->stack->postToStack([holder] { holder->onFdEofFromStack(); }); + this->stack->getReactor().del(this->fd); + return; + } + if (errno == EAGAIN || errno == EWOULDBLOCK) { + return; + } + closeFromReactor(); + return; + } +#endif +} + +void SessionTcp::closeFromReactor() { + if (this->closing.exchange(true)) { + return; + } +#if defined(__linux__) || defined(__APPLE__) + if (this->fd >= 0) { + this->stack->getReactor().del(this->fd); + ::close(this->fd); + this->fd = -1; + } +#endif + if (this->pcb != nullptr) { + auto holder = shared_from_this(); + this->stack->postToStack([holder] { holder->closeFromStack(); }); + } + // 释放自持引用(最后一个引用消失时析构) + auto keep = this->self; + this->self.reset(); +} + +} // namespace candy diff --git a/candy/src/netstack/session_tcp.h b/candy/src/netstack/session_tcp.h new file mode 100644 index 00000000..50171960 --- /dev/null +++ b/candy/src/netstack/session_tcp.h @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_SESSION_TCP_H +#define CANDY_NETSTACK_SESSION_TCP_H + +#include "core/net.h" +#include "netstack/session.h" +#include +#include +#include +#include +#include + +#include "lwip/tcp.h" + +namespace candy { + +// SessionTcp:一条被 lwIP 终结的 TCP 连接对应一个内核 socket 落地。 +// +// 线程契约: +// - 所有 tcp_*(pcb) 调用只在 NetStack 线程执行; +// - 所有 fd 的 read/write 只在 Reactor 线程执行; +// - 两侧通过 NetStack::postToStack / Reactor::post 投递任务跨线程交互。 +// +// 数据流: +// 正向 lwIP->fd:tcp_recv 回调(NetStack线程) 收数据 -> 缓存 -> 投递 reactor 在可写时 write(fd) +// 反向 fd->lwIP:fd 可读(reactor线程) read -> 投递 NetStack 执行 tcp_write + tcp_output +class SessionTcp : public Session, public std::enable_shared_from_this { +public: + SessionTcp(NetStack *stack, struct tcp_pcb *pcb); + ~SessionTcp() override; + + int start() override; + + // 仅供 NetStack 线程在栈拆除(重连)时调用,强制关闭 pcb 与 fd。 + void shutdownFromStack(); + +private: + // ---- NetStack 线程 ---- + static err_t recvTrampoline(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err); + static err_t sentTrampoline(void *arg, struct tcp_pcb *pcb, u16_t len); + static void errTrampoline(void *arg, err_t err); + + err_t onRecv(struct tcp_pcb *pcb, struct pbuf *p, err_t err); + err_t onSent(struct tcp_pcb *pcb, u16_t len); + void onErr(err_t err); + + void writeToLwip(const std::string &data); + void pumpToLwip(); + void onFdEofFromStack(); + void closeFromStack(); + // 把已写进落地 fd 的字节数向 lwIP 确认(推进接收窗口)。仅 NetStack 线程调用。 + void ackRecvedToLwip(); + + // ---- Reactor 线程 ---- + void onFdEvent(uint32_t events); + void onConnected(); + void onFdReadable(); + void onFdWritable(); + void closeFromReactor(); + + void flushForwardLocked(); + void updateReadInterest(); + +private: + struct tcp_pcb *pcb; + int fd; + bool connected; + + IP4 origDst; + uint16_t origDstPort; + IP4 origSrc; + + std::mutex bufMutex; + std::string forwardBuf; + bool wantWrite; + + // lwIP -> fd 方向背压核心:已写进落地 fd、待向 lwIP 确认(tcp_recved)的字节数。 + // 由 Reactor 线程累加、NetStack 线程在 ackRecvedToLwip 中消费,跨线程用原子传递。 + // 延迟确认使 lwIP 接收窗口只在数据真正落地后才推进,从而把源端发送速率自动约束在 + // 落地 fd 的消费能力之内,forwardBuf 天然被限制在一个 TCP_WND 内,杜绝无界膨胀。 + std::atomic pendingRecved{0}; + + // fd -> lwIP 方向待发缓冲(仅 NetStack 线程访问)。 + // tcp_sndbuf 满时暂存,onSent 释放空间后继续 pump,避免静默丢包。 + std::string backwardBuf; + std::atomic readPaused{false}; + bool fdEof; + // fd -> lwIP 方向"已从落地 fd 读出但尚未被 lwIP 确认排空"的字节积压。 + // 由 Reactor 线程(读出后 +)与 NetStack 线程(pump 写入 lwIP 后 -)共同维护, + // 用于在 Reactor 读取侧做同步背压:超过高水位立即停读,回落到低水位再恢复, + // 避免单线程 NetStack 被海量 writeToLwip 任务淹没、导致回程 ACK 饿死、发送窗口卡死。 + std::atomic backwardBytes{0}; + + std::shared_ptr self; +}; + +} // namespace candy + +#endif diff --git a/candy/src/tun/tun.cc b/candy/src/tun/tun.cc index 767ae459..65a6719d 100644 --- a/candy/src/tun/tun.cc +++ b/candy/src/tun/tun.cc @@ -81,6 +81,12 @@ int Tun::handleTunDevice() { return 0; } + // userspace 模式:记录本网关发起(本地 LAN -> 虚拟网)的 TCP 流, + // 以便收到 IPIP 返程包时识别并 L3 转发回本地 LAN,而非误终结。 + if (getClient().getForwardMode() == "userspace") { + trackOutboundFlow(buffer.substr(sizeof(IP4Header))); + } + this->client->getPeerMsgQueue().write(Msg(MsgKind::PACKET, std::move(buffer))); return 0; } @@ -114,6 +120,22 @@ int Tun::handlePacket(Msg msg) { return 0; } IP4Header *header = (IP4Header *)msg.data.data(); + + // userspace 模式:落地端把"新入站连接"的 IPIP 整包交给 NetStack 终结(参照1), + // 由 NetStack 记录 vnetPeer 并剥 IPIP、喂 lwIP;不写内核 tun。 + // 但若该 IPIP 内层是本网关"发起流"的返程(如 SYN-ACK),本网关是发起端, + // 必须剥 IPIP 后写内核 tun,L3 转发回本地 LAN,而非误喂自己的 lwIP。 + // kernel 模式:保持现状,剥 IPIP 外层后写内核 tun。 + if (getClient().getForwardMode() == "userspace" && header->isIPIP()) { + std::string inner = msg.data.substr(sizeof(IP4Header)); + if (!isReturnFlow(inner)) { + getClient().getNetStack().input(std::move(msg.data)); + return 0; + } + write(inner); + return 0; + } + if (header->isIPIP()) { msg.data.erase(0, sizeof(IP4Header)); header = (IP4Header *)msg.data.data(); @@ -178,4 +200,62 @@ Client &Tun::getClient() { return *this->client; } +// 仅跟踪 TCP(阶段一)。返回是否成功解析出五元组。 +static bool parseFlow(const std::string &inner, FlowKey &key) { + if (inner.size() < sizeof(IP4Header)) { + return false; + } + const IP4Header *ip = (const IP4Header *)inner.data(); + if (ip->protocol != 0x06) { // TCP + return false; + } + size_t ihl = (ip->version_ihl & 0x0f) * 4; + if (inner.size() < ihl + 4) { + return false; + } + const uint8_t *l4 = (const uint8_t *)inner.data() + ihl; + key.src = uint32_t(ip->saddr); + key.dst = uint32_t(ip->daddr); + key.sport = ((uint16_t)l4[0] << 8) | l4[1]; + key.dport = ((uint16_t)l4[2] << 8) | l4[3]; + key.proto = ip->protocol; + return true; +} + +void Tun::trackOutboundFlow(const std::string &innerPacket) { + FlowKey key; + if (!parseFlow(innerPacket, key)) { + return; + } + auto now = std::chrono::steady_clock::now(); + std::unique_lock lock(this->flowMutex); + this->flowTable[key] = now; + // 顺带做惰性老化清理,避免表无限增长(180s 空闲过期)。 + if (this->flowTable.size() > 4096) { + for (auto it = this->flowTable.begin(); it != this->flowTable.end();) { + if (now - it->second > std::chrono::seconds(180)) { + it = this->flowTable.erase(it); + } else { + ++it; + } + } + } +} + +bool Tun::isReturnFlow(const std::string &innerPacket) { + FlowKey in; + if (!parseFlow(innerPacket, in)) { + return false; + } + // 返程包的五元组是发起流的反向:src/dst、sport/dport 互换。 + FlowKey rev{in.dst, in.src, in.dport, in.sport, in.proto}; + std::unique_lock lock(this->flowMutex); + auto it = this->flowTable.find(rev); + if (it == this->flowTable.end()) { + return false; + } + it->second = std::chrono::steady_clock::now(); + return true; +} + } // namespace candy diff --git a/candy/src/tun/tun.h b/candy/src/tun/tun.h index 81738c16..7a3a2401 100644 --- a/candy/src/tun/tun.h +++ b/candy/src/tun/tun.h @@ -5,15 +5,42 @@ #include "core/message.h" #include "core/net.h" #include +#include +#include #include #include #include #include +#include namespace candy { class Client; +// 发起流五元组:用于区分"本网关发起的流"(其返程应 L3 转发回本地 LAN) +// 与"新入站连接"(应在本落地端用 lwIP 终结)。仅 userspace 模式下使用。 +struct FlowKey { + uint32_t src; + uint32_t dst; + uint16_t sport; + uint16_t dport; + uint8_t proto; + + bool operator==(const FlowKey &o) const { + return src == o.src && dst == o.dst && sport == o.sport && dport == o.dport && proto == o.proto; + } +}; + +struct FlowKeyHash { + std::size_t operator()(const FlowKey &k) const noexcept { + std::size_t h = std::hash{}(k.src); + h ^= std::hash{}(k.dst) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash{}((uint32_t(k.sport) << 16) | k.dport) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash{}(k.proto) + 0x9e3779b9 + (h << 6) + (h >> 2); + return h; + } +}; + class Tun { public: Tun(); @@ -56,6 +83,15 @@ class Tun { std::shared_mutex sysRtMutex; std::list sysRtTable; + // userspace 模式发起流跟踪:记录本网关发起(本地 LAN -> 虚拟网)的流, + // 用于判定收到的 IPIP 内层包是"返程"(L3 转发回本地 LAN)还是 + // "新入站连接"(落地端 lwIP 终结)。 + void trackOutboundFlow(const std::string &innerPacket); + bool isReturnFlow(const std::string &innerPacket); + + std::mutex flowMutex; + std::unordered_map flowTable; + private: std::any impl; diff --git a/cmake/lwip.cmake b/cmake/lwip.cmake new file mode 100644 index 00000000..13614286 --- /dev/null +++ b/cmake/lwip.cmake @@ -0,0 +1,60 @@ +include(FetchContent) + +set(LWIP_GIT_REPOSITORY "https://github.com/heiher/lwip.git") +set(LWIP_GIT_TAG "8c69dfbe537835d5f2a5fd8c08c859f667b108ea") + +FetchContent_Declare( + lwip + GIT_REPOSITORY ${LWIP_GIT_REPOSITORY} + GIT_TAG ${LWIP_GIT_TAG} +) +FetchContent_GetProperties(lwip) +if(NOT lwip_POPULATED) + FetchContent_Populate(lwip) +endif() + +set(LWIP_SRC_DIR ${lwip_SOURCE_DIR}/src) +set(LWIP_PORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/candy/src/netstack/lwip-port) +set(LWIP_OPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/candy/src/netstack) + +set(LWIP_CORE_SOURCES + ${LWIP_SRC_DIR}/core/init.c + ${LWIP_SRC_DIR}/core/def.c + ${LWIP_SRC_DIR}/core/inet_chksum.c + ${LWIP_SRC_DIR}/core/ip.c + ${LWIP_SRC_DIR}/core/mem.c + ${LWIP_SRC_DIR}/core/memp.c + ${LWIP_SRC_DIR}/core/netif.c + ${LWIP_SRC_DIR}/core/pbuf.c + ${LWIP_SRC_DIR}/core/raw.c + ${LWIP_SRC_DIR}/core/stats.c + ${LWIP_SRC_DIR}/core/sys.c + ${LWIP_SRC_DIR}/core/tcp.c + ${LWIP_SRC_DIR}/core/tcp_in.c + ${LWIP_SRC_DIR}/core/tcp_out.c + ${LWIP_SRC_DIR}/core/udp.c + ${LWIP_SRC_DIR}/core/timeouts.c +) + +set(LWIP_CORE_IPV4_SOURCES + ${LWIP_SRC_DIR}/core/ipv4/icmp.c + ${LWIP_SRC_DIR}/core/ipv4/ip4.c + ${LWIP_SRC_DIR}/core/ipv4/ip4_addr.c + ${LWIP_SRC_DIR}/core/ipv4/ip4_frag.c +) + +set(LWIP_PORT_SOURCES + ${LWIP_PORT_DIR}/sys_arch.c +) + +add_library(lwip STATIC + ${LWIP_CORE_SOURCES} + ${LWIP_CORE_IPV4_SOURCES} + ${LWIP_PORT_SOURCES} +) + +target_include_directories(lwip PUBLIC + ${LWIP_OPTS_DIR} + ${LWIP_PORT_DIR} + ${LWIP_SRC_DIR}/include +) From 01e83692da475917c4b1e3bf1b800d0718fe9146 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sat, 27 Jun 2026 23:40:53 +0800 Subject: [PATCH 02/29] fix memory leap --- candy/src/netstack/session_tcp.cc | 35 ++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc index cd32024a..2e371e3c 100644 --- a/candy/src/netstack/session_tcp.cc +++ b/candy/src/netstack/session_tcp.cc @@ -109,16 +109,25 @@ err_t SessionTcp::onRecv(struct tcp_pcb *pcb, struct pbuf *p, err_t err) { std::string data; data.resize(p->tot_len); pbuf_copy_partial(p, data.data(), p->tot_len, 0); + u16_t recvLen = p->tot_len; pbuf_free(p); + size_t bufSize; { std::unique_lock lock(this->bufMutex); this->forwardBuf.append(data); + bufSize = this->forwardBuf.size(); } // 注意:不在此处 tcp_recved!接收窗口的推进延迟到数据真正写进落地 fd 之后 // (flushForwardLocked 累加 pendingRecved -> NetStack 线程 ackRecvedToLwip)。 // 否则会向源端谎报"已收下",落地端写得慢时 forwardBuf 无界膨胀直至 OOM。 + // 背压埋点(lwIP->fd):记录本次收量、forwardBuf 积压、当前未确认窗口(pendingRecved)。 + // forwardBuf 应稳定在一个 TCP_WND 内;若持续增长说明落地端写不动、背压失效。 + spdlog::debug("[bp][fwd] onRecv {} -> {}:{} recv={} forwardBuf={} pendingRecved={} sndwnd={}", + this->origSrc.toString(), this->origDst.toString(), this->origDstPort, recvLen, bufSize, + this->pendingRecved.load(), (this->pcb ? tcp_sndbuf(this->pcb) : 0)); + auto holder = shared_from_this(); this->stack->getReactor().post([holder] { holder->flushForwardLocked(); }); return ERR_OK; @@ -131,8 +140,15 @@ err_t SessionTcp::onSent(struct tcp_pcb *pcb, u16_t len) { } void SessionTcp::onErr(err_t err) { - // pcb 已被 lwIP 释放 + // pcb 已被 lwIP 释放(RST/abort 路径,不会再有 onRecv(p==null) 走正常关闭)。 + // 必须在此显式从会话表移除:否则 sessions[pcb] 仍持有一份 shared_ptr, + // 而 closeFromReactor 因 pcb 已空不会触发 closeFromStack -> removeSession, + // 导致 SessionTcp(连同 forwardBuf/backwardBuf)泄漏至下次重连 teardown。 + struct tcp_pcb *deadPcb = this->pcb; this->pcb = nullptr; + if (deadPcb != nullptr) { + this->stack->removeSession(deadPcb); + } closeFromReactor(); } @@ -221,6 +237,9 @@ void SessionTcp::updateReadInterest() { // 如此 fd 读取速率被 lwIP 发送+ACK 回收节奏严格约束,不再淹没单线程 NetStack。 if (this->readPaused.load() && this->backwardBytes.load() <= LOW_WATER) { this->readPaused.store(false); + // 背压埋点(fd->lwIP):积压回落到低水位,恢复读取(水位下降沿,低频事件)。 + spdlog::debug("[bp][bwd] resume read {}:{} -> {} backwardBytes={} (<=LOW_WATER={})", this->origDst.toString(), + this->origDstPort, this->origSrc.toString(), this->backwardBytes.load(), LOW_WATER); int fd = this->fd; auto holder = shared_from_this(); this->stack->getReactor().post([holder, fd] { @@ -323,6 +342,8 @@ void SessionTcp::flushForwardLocked() { return; } size_t acked = 0; + size_t remain = 0; + bool blocked = false; { std::unique_lock lock(this->bufMutex); while (!this->forwardBuf.empty()) { @@ -335,6 +356,7 @@ void SessionTcp::flushForwardLocked() { if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { this->wantWrite = true; this->stack->getReactor().mod(this->fd, ReactorEvent::READ | ReactorEvent::WRITE); + blocked = true; break; } lock.unlock(); @@ -345,7 +367,14 @@ void SessionTcp::flushForwardLocked() { this->wantWrite = false; this->stack->getReactor().mod(this->fd, ReactorEvent::READ); } + remain = this->forwardBuf.size(); } + // 背压埋点(lwIP->fd):记录本次写进落地 fd 的字节、剩余积压、是否因 fd 写满而背压。 + // blocked=true 表示落地端写满(EAGAIN),forwardBuf 残留并等待 onFdWritable 续写; + // 此时不向 lwIP 确认残留部分,源端接收窗口收紧 -> 自动减速,这正是背压生效的体现。 + spdlog::debug("[bp][fwd] flush {} -> {}:{} wrote={} remain={} blocked={} pendingRecved->{}", + this->origSrc.toString(), this->origDst.toString(), this->origDstPort, acked, remain, blocked, + this->pendingRecved.load() + acked); // 已落地的字节交由 NetStack 线程向 lwIP 确认(推进接收窗口)。 // 这才是真正的"收到",从而把源端发送速率约束在落地 fd 的消费能力之内。 if (acked > 0) { @@ -368,6 +397,10 @@ void SessionTcp::onFdReadable() { // 恢复由 NetStack 线程在 pumpToLwip(onSent 驱动)排空到低水位后触发。 if (this->backwardBytes.load() >= HIGH_WATER) { this->readPaused.store(true); + // 背压埋点(fd->lwIP):积压达到高水位,停止读取(水位上升沿,低频事件)。 + spdlog::debug("[bp][bwd] pause read {}:{} -> {} backwardBytes={} (>=HIGH_WATER={})", + this->origDst.toString(), this->origDstPort, this->origSrc.toString(), + this->backwardBytes.load(), HIGH_WATER); this->stack->getReactor().mod(this->fd, ReactorEvent::NONE); return; } From a7b5f7d8dd769b6f0ef2a54382689f2c56ff083e Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sun, 28 Jun 2026 01:14:19 +0800 Subject: [PATCH 03/29] lwip+socket+udp work --- candy/src/netstack/lwipopts.h | 5 +- candy/src/netstack/netstack.cc | 109 +++++++++++++- candy/src/netstack/netstack.h | 15 ++ candy/src/netstack/session_udp.cc | 234 ++++++++++++++++++++++++++++++ candy/src/netstack/session_udp.h | 83 +++++++++++ candy/src/tun/tun.cc | 5 +- cmake/lwip.cmake | 12 ++ 7 files changed, 459 insertions(+), 4 deletions(-) create mode 100644 candy/src/netstack/session_udp.cc create mode 100644 candy/src/netstack/session_udp.h diff --git a/candy/src/netstack/lwipopts.h b/candy/src/netstack/lwipopts.h index 9e9fbfcf..bb0a016d 100644 --- a/candy/src/netstack/lwipopts.h +++ b/candy/src/netstack/lwipopts.h @@ -11,7 +11,7 @@ #define IP_FORWARD 0 #define LWIP_TCP 1 -#define LWIP_UDP 0 +#define LWIP_UDP 1 #define LWIP_RAW 0 #define LWIP_ICMP 1 #define LWIP_DNS 0 @@ -34,6 +34,7 @@ #define MEMP_NUM_TCP_PCB 4096 #define MEMP_NUM_TCP_PCB_LISTEN 16 #define MEMP_NUM_TCP_SEG 4096 +#define MEMP_NUM_UDP_PCB 4096 #define MEMP_NUM_PBUF 4096 #define MEMP_NUM_REASSDATA 1 #define PBUF_POOL_SIZE 0 @@ -50,8 +51,10 @@ #define CHECKSUM_GEN_IP 1 #define CHECKSUM_GEN_TCP 1 +#define CHECKSUM_GEN_UDP 1 #define CHECKSUM_CHECK_IP 1 #define CHECKSUM_CHECK_TCP 1 +#define CHECKSUM_CHECK_UDP 1 #define LWIP_STATS 0 #define LWIP_STATS_DISPLAY 0 diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index ab484c8c..ed3fe212 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -3,6 +3,7 @@ #include "core/client.h" #include "core/message.h" #include "netstack/session_tcp.h" +#include "netstack/session_udp.h" #include #include #include @@ -13,11 +14,12 @@ #include "lwip/priv/tcp_priv.h" #include "lwip/tcp.h" #include "lwip/timeouts.h" +#include "lwip/udp.h" #include "netif/etharp.h" namespace candy { -NetStack::NetStack() : client(nullptr), running(false), listenPcb(nullptr) { +NetStack::NetStack() : client(nullptr), running(false), listenPcb(nullptr), udpListenPcb(nullptr) { std::memset(&this->lwipNetif, 0, sizeof(this->lwipNetif)); } @@ -134,6 +136,21 @@ int NetStack::initStack() { } tcp_arg(this->listenPcb, this); tcp_accept(this->listenPcb, acceptTrampoline); + + // UDP 捕获所有目的:建一个绑定本 netif 的 udp_pcb,bind(NULL,0) 接管任意目的。 + // 依赖 netif 的 NETIF_FLAG_PRETEND_TCP:收到首个数据报时 lwIP 会克隆出一个 + // 已 connect 源端的 npcb 并通过回调交给我们,由此建立四元组伪会话。 + this->udpListenPcb = udp_new_ip_type(IPADDR_TYPE_ANY); + if (this->udpListenPcb == nullptr) { + spdlog::critical("netstack udp_new failed"); + return -1; + } + udp_bind_netif(this->udpListenPcb, &this->lwipNetif); + if (udp_bind(this->udpListenPcb, nullptr, 0) != ERR_OK) { + spdlog::critical("netstack udp_bind failed"); + return -1; + } + udp_recv(this->udpListenPcb, udpRecvTrampoline, this); return 0; } @@ -149,10 +166,22 @@ void NetStack::teardownStack() { } this->sessions.clear(); } + // UDP 伪会话仅 NetStack 线程访问,此处即在 NetStack 线程,直接清理。 + for (auto &kv : this->udpSessions) { + if (kv.second) { + kv.second->shutdownFromStack(); + } + } + this->udpSessions.clear(); { std::unique_lock lock(this->peerMapMutex); this->vnetPeerMap.clear(); } + if (this->udpListenPcb != nullptr) { + udp_recv(this->udpListenPcb, nullptr, nullptr); + udp_remove(this->udpListenPcb); + this->udpListenPcb = nullptr; + } if (this->listenPcb != nullptr) { tcp_close(this->listenPcb); this->listenPcb = nullptr; @@ -162,6 +191,7 @@ void NetStack::teardownStack() { } void NetStack::loop() { + auto lastReap = std::chrono::steady_clock::now(); while (this->running.load()) { std::function task; { @@ -177,6 +207,34 @@ void NetStack::loop() { task(); } sys_check_timeouts(); + + // 节流:每 10s 扫描一次 UDP 伪会话,回收空闲超时项。 + auto now = std::chrono::steady_clock::now(); + if (now - lastReap >= std::chrono::seconds(10)) { + reapIdleUdpSessions(); + lastReap = now; + } + } +} + +void NetStack::reapIdleUdpSessions() { + // 仅 NetStack 线程访问 udpSessions,无需加锁。 + // UDP 无连接:60s 无收发则判定空闲,强制关闭并移除(shutdownFromStack 会 + // 移除 npcb 并交由 reactor 关闭 fd)。注意 shutdownFromStack 内不修改 udpSessions, + // 故可安全地在遍历后统一 erase。 + auto now = std::chrono::steady_clock::now(); + int reaped = 0; + for (auto it = this->udpSessions.begin(); it != this->udpSessions.end();) { + if (it->second && now - it->second->lastActive() > std::chrono::seconds(60)) { + it->second->shutdownFromStack(); + it = this->udpSessions.erase(it); + ++reaped; + } else { + ++it; + } + } + if (reaped > 0) { + spdlog::debug("netstack reap idle udp sessions: {} reaped, {} remain", reaped, this->udpSessions.size()); } } @@ -264,6 +322,15 @@ void NetStack::removeSession(struct tcp_pcb *pcb) { this->sessions.erase(pcb); } +void NetStack::removeUdpSession(const std::string &key) { + // 仅 NetStack 线程调用,udpSessions 无需加锁。 + this->udpSessions.erase(key); +} + +struct netif &NetStack::getNetif() { + return this->lwipNetif; +} + err_t NetStack::netifInitTrampoline(struct netif *netif) { NetStack *self = (NetStack *)netif->state; return self->onNetifInit(netif); @@ -279,6 +346,11 @@ err_t NetStack::acceptTrampoline(void *arg, struct tcp_pcb *newpcb, err_t err) { return self->onAccept(newpcb, err); } +void NetStack::udpRecvTrampoline(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) { + NetStack *self = (NetStack *)arg; + self->onUdpRecv(pcb, p, addr, port); +} + err_t NetStack::onNetifInit(struct netif *netif) { netif->name[0] = 'c'; netif->name[1] = 'd'; @@ -320,4 +392,39 @@ err_t NetStack::onAccept(struct tcp_pcb *newpcb, err_t err) { return ERR_OK; } +void NetStack::onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) { + // 首包路径:lwIP 已为该流克隆出已 connect 的 npcb(pcb 参数), + // addr/port = 目的(dev2 服务),pcb->remote_ip/remote_port = 源端(dev1)。 + // 我们建立伪会话并在 npcb 上注册每流 recv handler,然后**不释放 p**: + // lwIP 收尾会 goto again 把同一数据报按已连接 npcb 重投,交由每流 handler 处理。 + if (p == nullptr) { + return; + } + if (!this->running.load()) { + udp_remove(pcb); + pbuf_free(p); + return; + } + + IP4 origDst; + std::memcpy(&origDst, &ip_2_ip4(addr)->addr, sizeof(uint32_t)); + uint16_t origDstPort = port; + IP4 origSrc; + std::memcpy(&origSrc, &ip_2_ip4(&pcb->remote_ip)->addr, sizeof(uint32_t)); + uint16_t origSrcPort = pcb->remote_port; + + spdlog::debug("netstack onUdpRecv: {}:{} -> {}:{}", origSrc.toString(), origSrcPort, origDst.toString(), + origDstPort); + + auto session = std::make_shared(this, pcb, origSrc, origSrcPort, origDst, origDstPort); + if (session->start()) { + udp_recv(pcb, nullptr, nullptr); + udp_remove(pcb); + pbuf_free(p); + return; + } + this->udpSessions[session->key()] = session; + // 不释放 p:交还 lwIP(goto again 重投到已连接 npcb 的每流 handler)。 +} + } // namespace candy diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index b3154b13..07010feb 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -19,11 +19,13 @@ #include "lwip/ip_addr.h" #include "lwip/netif.h" #include "lwip/tcp.h" +#include "lwip/udp.h" namespace candy { class Client; class SessionTcp; +class SessionUdp; // NetStack:独占 lwIP(NO_SYS=1 单线程 raw API)的模块。 // 持有一个 NetStack 线程驱动 lwIP 收发与定时器,一个 Reactor 线程管理落地 fd。 @@ -43,12 +45,16 @@ class NetStack { // 供 Session 使用:在 NetStack 线程内执行任务(保证 lwIP API 线程安全)。 void postToStack(std::function task); Reactor &getReactor(); + // UDP 回包注入用:拿到本模块的 lwIP netif(仅 NetStack 线程使用)。 + struct netif &getNetif(); // 回包:lwIP netif->output 产生的 IP 包,按连接上下文重新 IPIP 封装送回源端。 void output(const std::string &innerPacket); // Session 结束时从会话表移除。 void removeSession(struct tcp_pcb *pcb); + // UDP 伪会话结束时从 UDP 会话表移除(按四元组 key)。仅 NetStack 线程调用。 + void removeUdpSession(const std::string &key); Client *getClient(); @@ -58,15 +64,20 @@ class NetStack { void loop(); void handleInput(std::string packet); void feedToLwip(const std::string &innerPacket, IP4 vnetPeer); + // UDP 伪会话空闲超时回收(仅 NetStack 线程,loop 中节流调用)。 + void reapIdleUdpSessions(); // lwIP 回调跳板 static err_t netifInitTrampoline(struct netif *netif); static err_t outputTrampoline(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr); static err_t acceptTrampoline(void *arg, struct tcp_pcb *newpcb, err_t err); + static void udpRecvTrampoline(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); err_t onNetifInit(struct netif *netif); err_t onOutput(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr); err_t onAccept(struct tcp_pcb *newpcb, err_t err); + // UDP 数据报到达:addr/port=源端(dev1),pcb->local=目的(落地服务)。仅 NetStack 线程。 + void onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); private: Client *client; @@ -76,6 +87,8 @@ class NetStack { struct netif lwipNetif; struct tcp_pcb *listenPcb; + // UDP 捕获所有目的的伪监听 pcb(绑定 netif、bind(NULL,0),收首包克隆出 connect 的 npcb)。 + struct udp_pcb *udpListenPcb; Reactor reactor; @@ -95,6 +108,8 @@ class NetStack { std::mutex sessionMutex; std::unordered_map> sessions; + // UDP 伪会话表:四元组 key -> SessionUdp。仅 NetStack 线程访问,无需加锁。 + std::unordered_map> udpSessions; }; } // namespace candy diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc new file mode 100644 index 00000000..138a5e14 --- /dev/null +++ b/candy/src/netstack/session_udp.cc @@ -0,0 +1,234 @@ +// SPDX-License-Identifier: MIT +#include "netstack/session_udp.h" +#include "netstack/netstack.h" +#include +#include + +#include "lwip/pbuf.h" +#include "lwip/udp.h" + +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#include +#include +#include +#endif + +namespace candy { + +SessionUdp::SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort, IP4 origDst, + uint16_t origDstPort) + : Session(stack), pcb(pcb), fd(-1), origSrc(origSrc), origSrcPort(origSrcPort), origDst(origDst), + origDstPort(origDstPort), lastActiveTs(std::chrono::steady_clock::now()) { + // 四元组 key:源IP:源端口 -> 目的IP:目的端口 + this->sessionKey.assign((const char *)&origSrc, sizeof(uint32_t)); + this->sessionKey.append((const char *)&origSrcPort, sizeof(origSrcPort)); + this->sessionKey.append((const char *)&origDst, sizeof(uint32_t)); + this->sessionKey.append((const char *)&origDstPort, sizeof(origDstPort)); +} + +SessionUdp::~SessionUdp() { + if (this->fd >= 0) { + ::close(this->fd); + this->fd = -1; + } +} + +int SessionUdp::start() { +#if defined(__linux__) || defined(__APPLE__) + this->self = shared_from_this(); + + this->fd = ::socket(AF_INET, SOCK_DGRAM, 0); + if (this->fd < 0) { + spdlog::warn("session udp socket failed: {}", strerror(errno)); + return -1; + } + int flags = ::fcntl(this->fd, F_GETFL, 0); + ::fcntl(this->fd, F_SETFL, flags | O_NONBLOCK); + + // connect 固定对端:内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE), + // 且后续 send/recv 只与该对端往来,形成 Fullcone NAT 的伪会话。 + struct sockaddr_in dst = {}; + dst.sin_family = AF_INET; + dst.sin_port = htons(this->origDstPort); + dst.sin_addr.s_addr = uint32_t(this->origDst); + if (::connect(this->fd, (struct sockaddr *)&dst, sizeof(dst)) != 0) { + spdlog::warn("session udp connect {}:{} failed: {}", this->origDst.toString(), this->origDstPort, + strerror(errno)); + ::close(this->fd); + this->fd = -1; + return -1; + } + + spdlog::debug("session udp: {}:{} -> {}:{}", this->origSrc.toString(), this->origSrcPort, this->origDst.toString(), + this->origDstPort); + + auto holder = shared_from_this(); + int fd = this->fd; + this->stack->getReactor().add(fd, ReactorEvent::READ, [holder](ReactorEvent ev) { holder->onFdEvent((uint32_t)ev); }); + + // 在 lwIP 克隆出的 npcb 上注册每流 recv:本流后续数据报都回调到 recvTrampoline。 + // recv_arg 用裸指针 this:会话存活期间被 udpSessions 持有 + self 自持,指针有效。 + udp_recv(this->pcb, recvTrampoline, this); + return 0; +#else + return -1; +#endif +} + +// ===================== NetStack 线程 ===================== + +void SessionUdp::recvTrampoline(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) { + SessionUdp *self = (SessionUdp *)arg; + self->onPcbRecv(p, addr, port); +} + +void SessionUdp::onPcbRecv(struct pbuf *p, const ip_addr_t *addr, u16_t port) { + // 本流后续数据报:addr/port=源端(dev1),数据负载转发到落地 fd。 + if (p == nullptr) { + return; + } + std::string data; + data.resize(p->tot_len); + pbuf_copy_partial(p, data.data(), p->tot_len, 0); + pbuf_free(p); + sendToLanding(std::move(data)); +} + +void SessionUdp::sendToLanding(std::string data) { + // 仅 NetStack 线程调用:刷新活跃时间,把数据报投递到 reactor 线程发送。 + this->lastActiveTs = std::chrono::steady_clock::now(); + auto holder = shared_from_this(); + this->stack->getReactor().post([holder, data = std::move(data)]() mutable { +#if defined(__linux__) || defined(__APPLE__) + if (holder->fd < 0 || holder->closing.load()) { + return; + } + ssize_t n = ::send(holder->fd, data.data(), data.size(), 0); + if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { + spdlog::debug("session udp send failed: {}", strerror(errno)); + } +#endif + }); +} + +void SessionUdp::replyToLwip(std::string data) { + // 仅 NetStack 线程调用:把落地回包以 (origDst:origDstPort -> origSrc:origSrcPort) + // 重新注入 lwIP,由 netif->output 产生内层 IP 包、再经 NetStack::output 封 IPIP 回源端。 + if (this->pcb == nullptr) { + return; + } + this->lastActiveTs = std::chrono::steady_clock::now(); + + struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)data.size(), PBUF_RAM); + if (p == nullptr) { + spdlog::warn("session udp reply pbuf_alloc failed"); + return; + } + pbuf_take(p, data.data(), (u16_t)data.size()); + + // 源地址固定为落地目的(origDst),源端口为 origDstPort;目的为源端(origSrc:origSrcPort)。 + ip_addr_t src; + ip_addr_t dst; + ip_addr_set_ip4_u32(&src, uint32_t(this->origDst)); + ip_addr_set_ip4_u32(&dst, uint32_t(this->origSrc)); + err_t e = udp_sendto_if_src(this->pcb, p, &dst, this->origSrcPort, &this->stack->getNetif(), &src); + if (e != ERR_OK) { + spdlog::debug("session udp udp_sendto_if_src failed: {}", (int)e); + } + pbuf_free(p); +} + +void SessionUdp::closeFromStack() { + if (this->pcb != nullptr) { + udp_recv(this->pcb, nullptr, nullptr); + udp_remove(this->pcb); + this->pcb = nullptr; + } + NetStack *stack = this->stack; + std::string k = this->sessionKey; + auto holder = shared_from_this(); + stack->getReactor().post([holder] { holder->closeFromReactor(); }); + stack->removeUdpSession(k); +} + +void SessionUdp::shutdownFromStack() { + // 栈拆除路径:NetStack 线程内直接移除 pcb;fd 交由 reactor 关闭。 + if (this->pcb != nullptr) { + udp_recv(this->pcb, nullptr, nullptr); + udp_remove(this->pcb); + this->pcb = nullptr; + } + if (!this->closing.exchange(true)) { + auto holder = shared_from_this(); + this->stack->getReactor().post([holder] { +#if defined(__linux__) || defined(__APPLE__) + if (holder->fd >= 0) { + holder->stack->getReactor().del(holder->fd); + ::close(holder->fd); + holder->fd = -1; + } +#endif + holder->self.reset(); + }); + } +} + +// ===================== Reactor 线程 ===================== + +void SessionUdp::onFdEvent(uint32_t events) { + ReactorEvent ev = (ReactorEvent)events; + if (ev & ReactorEvent::ERROR) { + closeFromReactor(); + return; + } + if (ev & ReactorEvent::READ) { + onFdReadable(); + } +} + +void SessionUdp::onFdReadable() { +#if defined(__linux__) || defined(__APPLE__) + char buf[65536]; + while (true) { + ssize_t n = ::recv(this->fd, buf, sizeof(buf), 0); + if (n > 0) { + std::string data(buf, n); + auto holder = shared_from_this(); + this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->replyToLwip(std::move(data)); }); + continue; + } + if (n == 0) { + return; + } + if (errno == EAGAIN || errno == EWOULDBLOCK) { + return; + } + // 落地 socket 错误(如 ICMP port unreachable 经 connect 返回 ECONNREFUSED):关闭会话。 + closeFromReactor(); + return; + } +#endif +} + +void SessionUdp::closeFromReactor() { + if (this->closing.exchange(true)) { + return; + } +#if defined(__linux__) || defined(__APPLE__) + if (this->fd >= 0) { + this->stack->getReactor().del(this->fd); + ::close(this->fd); + this->fd = -1; + } +#endif + if (this->pcb != nullptr) { + auto holder = shared_from_this(); + this->stack->postToStack([holder] { holder->closeFromStack(); }); + } + auto keep = this->self; + this->self.reset(); +} + +} // namespace candy diff --git a/candy/src/netstack/session_udp.h b/candy/src/netstack/session_udp.h new file mode 100644 index 00000000..20b19611 --- /dev/null +++ b/candy/src/netstack/session_udp.h @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_SESSION_UDP_H +#define CANDY_NETSTACK_SESSION_UDP_H + +#include "core/net.h" +#include "netstack/session.h" +#include +#include +#include +#include +#include + +#include "lwip/udp.h" + +namespace candy { + +// SessionUdp:一条 UDP "伪会话"——把 lwIP 终结的某个 (源,目的) 四元组数据流, +// 映射到一个 connect 到目的的内核 UDP socket 落地(Fullcone NAT 思路)。 +// +// 线程契约(与 SessionTcp 一致): +// - 所有 udp_*(pcb) 调用只在 NetStack 线程执行; +// - 所有 fd 的 recv/send 只在 Reactor 线程执行; +// - 两侧通过 NetStack::postToStack / Reactor::post 投递任务跨线程交互。 +// +// 数据流: +// 正向 lwIP->fd:udp_recv 回调(NetStack线程) 收数据报 -> 投递 reactor send(fd) +// 反向 fd->lwIP:fd 可读(reactor线程) recv -> 投递 NetStack udp_sendto_if_src 回送源端 +class SessionUdp : public Session, public std::enable_shared_from_this { +public: + // pcb:lwIP 为该四元组克隆出的已 connect 的 udp_pcb(NetStack 线程持有)。 + // origSrc/origSrcPort:源端(dev1);origDst/origDstPort:落地目的(dev2 服务)。 + SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort, IP4 origDst, + uint16_t origDstPort); + ~SessionUdp() override; + + int start() override; + + // 仅供 NetStack 线程在栈拆除(重连)时调用,强制关闭 pcb 与 fd。 + void shutdownFromStack(); + + // NetStack 线程:把一份来自 lwIP 的数据报投递到落地 fd 发送。 + void sendToLanding(std::string data); + + // 该会话的四元组 key(NetStack 线程的会话表用)。 + const std::string &key() const { return this->sessionKey; } + + // 最近活跃时间(用于空闲超时回收,仅 NetStack 线程访问)。 + std::chrono::steady_clock::time_point lastActive() const { return this->lastActiveTs; } + + // lwIP 每流 recv 跳板:克隆出的 npcb 后续同流数据报都走这里(仅 NetStack 线程)。 + static void recvTrampoline(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); + +private: + // ---- Reactor 线程 ---- + void onFdEvent(uint32_t events); + void onFdReadable(); + void closeFromReactor(); + + // ---- NetStack 线程 ---- + // 本流后续数据报到达:addr/port=源端(dev1),提取数据投递到落地 fd 发送。 + void onPcbRecv(struct pbuf *p, const ip_addr_t *addr, u16_t port); + // 落地 fd 收到回包后,用 pcb 把数据报回送源端。 + void replyToLwip(std::string data); + void closeFromStack(); + +private: + struct udp_pcb *pcb; + int fd; + + IP4 origSrc; + uint16_t origSrcPort; + IP4 origDst; + uint16_t origDstPort; + + std::string sessionKey; + std::chrono::steady_clock::time_point lastActiveTs; + + std::shared_ptr self; +}; + +} // namespace candy + +#endif diff --git a/candy/src/tun/tun.cc b/candy/src/tun/tun.cc index 65a6719d..584a660a 100644 --- a/candy/src/tun/tun.cc +++ b/candy/src/tun/tun.cc @@ -200,13 +200,14 @@ Client &Tun::getClient() { return *this->client; } -// 仅跟踪 TCP(阶段一)。返回是否成功解析出五元组。 +// 跟踪 TCP(0x06)/UDP(0x11) 发起流。返回是否成功解析出五元组。 +// TCP 与 UDP 的 L4 头前 4 字节都是 (源端口, 目的端口),端口提取逻辑一致。 static bool parseFlow(const std::string &inner, FlowKey &key) { if (inner.size() < sizeof(IP4Header)) { return false; } const IP4Header *ip = (const IP4Header *)inner.data(); - if (ip->protocol != 0x06) { // TCP + if (ip->protocol != 0x06 && ip->protocol != 0x11) { // TCP / UDP return false; } size_t ihl = (ip->version_ihl & 0x0f) * 4; diff --git a/cmake/lwip.cmake b/cmake/lwip.cmake index 13614286..e78ff5fc 100644 --- a/cmake/lwip.cmake +++ b/cmake/lwip.cmake @@ -13,6 +13,18 @@ if(NOT lwip_POPULATED) FetchContent_Populate(lwip) endif() +# 补丁:heiher lwIP 的 UDP PRETEND 克隆路径用了 pcb->local_ip.type, +# 该字段仅在双栈(LWIP_IPV6=1)的 ip_addr_t 存在;本项目是 IPv4-only, +# 编译会报 "ip_addr_t has no member named type"。改用版本无关的 +# IP_GET_TYPE() 宏(IPv4-only 下展开为 IPADDR_TYPE_V4),不引入 IPv6。 +set(LWIP_UDP_C "${lwip_SOURCE_DIR}/src/core/udp.c") +file(READ "${LWIP_UDP_C}" _lwip_udp_src) +string(REPLACE + "udp_new_ip_type(pcb->local_ip.type)" + "udp_new_ip_type(IP_GET_TYPE(&pcb->local_ip))" + _lwip_udp_src "${_lwip_udp_src}") +file(WRITE "${LWIP_UDP_C}" "${_lwip_udp_src}") + set(LWIP_SRC_DIR ${lwip_SOURCE_DIR}/src) set(LWIP_PORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/candy/src/netstack/lwip-port) set(LWIP_OPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/candy/src/netstack) From 600ca169e3bb8d4d1222426a4c06cbfa0d72c8fe Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sun, 28 Jun 2026 02:50:16 +0800 Subject: [PATCH 04/29] lwip+socket+icmp work --- candy/src/netstack/netstack.cc | 93 ++++++++++- candy/src/netstack/netstack.h | 10 ++ candy/src/netstack/session_icmp.cc | 248 +++++++++++++++++++++++++++++ candy/src/netstack/session_icmp.h | 75 +++++++++ candy/src/tun/tun.cc | 23 ++- 5 files changed, 446 insertions(+), 3 deletions(-) create mode 100644 candy/src/netstack/session_icmp.cc create mode 100644 candy/src/netstack/session_icmp.h diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index ed3fe212..7afd1d54 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -2,6 +2,7 @@ #include "netstack/netstack.h" #include "core/client.h" #include "core/message.h" +#include "netstack/session_icmp.h" #include "netstack/session_tcp.h" #include "netstack/session_udp.h" #include @@ -173,6 +174,13 @@ void NetStack::teardownStack() { } } this->udpSessions.clear(); + // ICMP 伪会话同理清理。 + for (auto &kv : this->icmpSessions) { + if (kv.second) { + kv.second->shutdownFromStack(); + } + } + this->icmpSessions.clear(); { std::unique_lock lock(this->peerMapMutex); this->vnetPeerMap.clear(); @@ -208,10 +216,11 @@ void NetStack::loop() { } sys_check_timeouts(); - // 节流:每 10s 扫描一次 UDP 伪会话,回收空闲超时项。 + // 节流:每 10s 扫描一次 UDP/ICMP 伪会话,回收空闲超时项。 auto now = std::chrono::steady_clock::now(); if (now - lastReap >= std::chrono::seconds(10)) { reapIdleUdpSessions(); + reapIdleIcmpSessions(); lastReap = now; } } @@ -238,6 +247,27 @@ void NetStack::reapIdleUdpSessions() { } } +void NetStack::reapIdleIcmpSessions() { + // 仅 NetStack 线程访问 icmpSessions,无需加锁。 + // ICMP echo 间隔通常 1s:30s 无活动判定空闲,强制关闭并移除。 + // shutdownFromStack 仅置 closing 并交 reactor 关 fd,不修改 icmpSessions, + // 故可安全地在遍历后统一 erase。 + auto now = std::chrono::steady_clock::now(); + int reaped = 0; + for (auto it = this->icmpSessions.begin(); it != this->icmpSessions.end();) { + if (it->second && now - it->second->lastActive() > std::chrono::seconds(30)) { + it->second->shutdownFromStack(); + it = this->icmpSessions.erase(it); + ++reaped; + } else { + ++it; + } + } + if (reaped > 0) { + spdlog::debug("netstack reap idle icmp sessions: {} reaped, {} remain", reaped, this->icmpSessions.size()); + } +} + void NetStack::handleInput(std::string packet) { if (packet.size() < sizeof(IP4Header)) { return; @@ -254,6 +284,12 @@ void NetStack::handleInput(std::string packet) { IP4Header *inner = (IP4Header *)packet.data(); spdlog::debug("netstack input: vnetPeer={} {} -> {} proto={}", vnetPeer.toString(), inner->saddr.toString(), inner->daddr.toString(), (int)inner->protocol); + // ICMP 不走 lwIP(PRETEND netif 仅接受 TCP/UDP),在入栈前拦截并独立落地。 + if (inner->protocol == 0x01) { + if (handleIcmp(packet, vnetPeer)) { + return; + } + } feedToLwip(packet, vnetPeer); } @@ -288,6 +324,56 @@ void NetStack::feedToLwip(const std::string &innerPacket, IP4 vnetPeer) { } } +bool NetStack::handleIcmp(const std::string &innerPacket, IP4 vnetPeer) { + // 仅处理 ICMP echo request(type 8):建立/复用伪会话,用内核 ICMP socket 落地。 + // 其余 ICMP(差错类等)暂不处理,返回 false 交回默认丢弃(不喂 lwIP)。 + IP4Header *ip = (IP4Header *)innerPacket.data(); + size_t ihl = (ip->version_ihl & 0x0f) * 4; + if (innerPacket.size() < ihl + 8) { + return false; + } + const uint8_t *icmp = (const uint8_t *)innerPacket.data() + ihl; + uint8_t type = icmp[0]; + if (type != 8) { // 仅 echo request + return false; + } + uint16_t icmpId = ((uint16_t)icmp[4] << 8) | icmp[5]; + + IP4 origSrc = ip->saddr; + IP4 origDst = ip->daddr; + + // 记录回包寻址:origSrc(dev1) -> vnetPeer(源网关虚拟IP),供 reply 经 output 回送。 + if (!vnetPeer.empty()) { + auto now = std::chrono::steady_clock::now(); + std::unique_lock lock(this->peerMapMutex); + this->vnetPeerMap[uint32_t(origSrc)] = PeerEntry{vnetPeer, now}; + } + + // key:(源,目的,icmpId)。复用已有会话或新建。 + std::string key; + key.assign((const char *)&origSrc, sizeof(uint32_t)); + key.append((const char *)&origDst, sizeof(uint32_t)); + key.append((const char *)&icmpId, sizeof(icmpId)); + + std::shared_ptr session; + auto sit = this->icmpSessions.find(key); + if (sit != this->icmpSessions.end()) { + session = sit->second; + } else { + session = std::make_shared(this, origSrc, origDst, icmpId); + if (session->start()) { + spdlog::debug("netstack icmp session start failed: {} -> {}", origSrc.toString(), origDst.toString()); + return true; // 已消费(落地失败也不喂 lwIP) + } + this->icmpSessions[key] = session; + } + + spdlog::debug("netstack handleIcmp: {} -> {} id={}", origSrc.toString(), origDst.toString(), icmpId); + // 把完整 ICMP 报文(含头与 payload)交给会话落地发送。 + session->sendEcho(innerPacket.substr(ihl)); + return true; +} + void NetStack::output(const std::string &innerPacket) { if (innerPacket.size() < sizeof(IP4Header)) { return; @@ -327,6 +413,11 @@ void NetStack::removeUdpSession(const std::string &key) { this->udpSessions.erase(key); } +void NetStack::removeIcmpSession(const std::string &key) { + // 仅 NetStack 线程调用,icmpSessions 无需加锁。 + this->icmpSessions.erase(key); +} + struct netif &NetStack::getNetif() { return this->lwipNetif; } diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index 07010feb..240c76ac 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -26,6 +26,7 @@ namespace candy { class Client; class SessionTcp; class SessionUdp; +class SessionIcmp; // NetStack:独占 lwIP(NO_SYS=1 单线程 raw API)的模块。 // 持有一个 NetStack 线程驱动 lwIP 收发与定时器,一个 Reactor 线程管理落地 fd。 @@ -55,6 +56,8 @@ class NetStack { void removeSession(struct tcp_pcb *pcb); // UDP 伪会话结束时从 UDP 会话表移除(按四元组 key)。仅 NetStack 线程调用。 void removeUdpSession(const std::string &key); + // ICMP 伪会话结束时从 ICMP 会话表移除(按 key)。仅 NetStack 线程调用。 + void removeIcmpSession(const std::string &key); Client *getClient(); @@ -64,8 +67,13 @@ class NetStack { void loop(); void handleInput(std::string packet); void feedToLwip(const std::string &innerPacket, IP4 vnetPeer); + // ICMP echo 在入栈前拦截:lwIP 的 PRETEND netif 只接受 TCP/UDP,ICMP 单独落地。 + // 返回 true 表示已被 ICMP 处理(不再喂 lwIP)。仅 NetStack 线程。 + bool handleIcmp(const std::string &innerPacket, IP4 vnetPeer); // UDP 伪会话空闲超时回收(仅 NetStack 线程,loop 中节流调用)。 void reapIdleUdpSessions(); + // ICMP 伪会话空闲超时回收(仅 NetStack 线程,loop 中节流调用)。 + void reapIdleIcmpSessions(); // lwIP 回调跳板 static err_t netifInitTrampoline(struct netif *netif); @@ -110,6 +118,8 @@ class NetStack { std::unordered_map> sessions; // UDP 伪会话表:四元组 key -> SessionUdp。仅 NetStack 线程访问,无需加锁。 std::unordered_map> udpSessions; + // ICMP 伪会话表:(源,目的,icmpId) key -> SessionIcmp。仅 NetStack 线程访问,无需加锁。 + std::unordered_map> icmpSessions; }; } // namespace candy diff --git a/candy/src/netstack/session_icmp.cc b/candy/src/netstack/session_icmp.cc new file mode 100644 index 00000000..72c0b65e --- /dev/null +++ b/candy/src/netstack/session_icmp.cc @@ -0,0 +1,248 @@ +// SPDX-License-Identifier: MIT +#include "netstack/session_icmp.h" +#include "netstack/netstack.h" +#include +#include + +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#include +#include +#include +#endif + +namespace candy { + +namespace { + +// 计算 16 位反码和校验(IP/ICMP 通用)。data 长度可奇可偶。 +uint16_t checksum16(const uint8_t *data, size_t len) { + uint32_t sum = 0; + while (len > 1) { + sum += (uint16_t)((data[0] << 8) | data[1]); + data += 2; + len -= 2; + } + if (len > 0) { + sum += (uint16_t)(data[0] << 8); + } + while (sum >> 16) { + sum = (sum & 0xffff) + (sum >> 16); + } + return (uint16_t)(~sum); +} + +constexpr size_t ICMP_HDR_LEN = 8; // type+code+checksum+id+seq + +} // namespace + +SessionIcmp::SessionIcmp(NetStack *stack, IP4 origSrc, IP4 origDst, uint16_t icmpId) + : Session(stack), fd(-1), rawMode(false), origSrc(origSrc), origDst(origDst), icmpId(icmpId), + lastActiveTs(std::chrono::steady_clock::now()) { + // key:源IP + 目的IP + icmp id(同一 ping 进程对同一目的复用一个落地 fd)。 + this->sessionKey.assign((const char *)&origSrc, sizeof(uint32_t)); + this->sessionKey.append((const char *)&origDst, sizeof(uint32_t)); + this->sessionKey.append((const char *)&icmpId, sizeof(icmpId)); +} + +SessionIcmp::~SessionIcmp() { + if (this->fd >= 0) { + ::close(this->fd); + this->fd = -1; + } +} + +int SessionIcmp::start() { +#if defined(__linux__) || defined(__APPLE__) + this->self = shared_from_this(); + + // 优先非特权 DGRAM ICMP socket(需 ping_group_range 覆盖运行用户); + // 失败退化为 RAW ICMP socket(需 root / CAP_NET_RAW)。 + this->fd = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); + if (this->fd < 0) { + this->fd = ::socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + this->rawMode = true; + } + if (this->fd < 0) { + spdlog::warn("session icmp socket failed: {}", strerror(errno)); + return -1; + } + int flags = ::fcntl(this->fd, F_GETFL, 0); + ::fcntl(this->fd, F_SETFL, flags | O_NONBLOCK); + + // connect 固定对端:内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE)。 + struct sockaddr_in dst = {}; + dst.sin_family = AF_INET; + dst.sin_addr.s_addr = uint32_t(this->origDst); + if (::connect(this->fd, (struct sockaddr *)&dst, sizeof(dst)) != 0) { + spdlog::warn("session icmp connect {} failed: {}", this->origDst.toString(), strerror(errno)); + ::close(this->fd); + this->fd = -1; + return -1; + } + + spdlog::debug("session icmp: {} -> {} id={} {}", this->origSrc.toString(), this->origDst.toString(), this->icmpId, + this->rawMode ? "raw" : "dgram"); + + auto holder = shared_from_this(); + int fd = this->fd; + this->stack->getReactor().add(fd, ReactorEvent::READ, [holder](ReactorEvent ev) { holder->onFdEvent((uint32_t)ev); }); + return 0; +#else + return -1; +#endif +} + +// ===================== NetStack 线程 ===================== + +void SessionIcmp::sendEcho(std::string icmpData) { + // 仅 NetStack 线程调用:刷新活跃时间,把 echo request 报文投递到 reactor 发送。 + // icmpData 是完整 ICMP 报文(type/code/checksum/id/seq/payload)。 + // DGRAM ICMP socket:内核会忽略并改写 id/checksum,按 fd 配对回包。 + this->lastActiveTs = std::chrono::steady_clock::now(); + auto holder = shared_from_this(); + this->stack->getReactor().post([holder, data = std::move(icmpData)]() mutable { +#if defined(__linux__) || defined(__APPLE__) + if (holder->fd < 0 || holder->closing.load()) { + return; + } + ssize_t n = ::send(holder->fd, data.data(), data.size(), 0); + if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { + spdlog::debug("session icmp send failed: {}", strerror(errno)); + } +#endif + }); +} + +void SessionIcmp::replyToPeer(std::string icmpReply) { + // 仅 NetStack 线程调用:icmpReply 是落地 fd 收到的 echo reply 的 ICMP 报文。 + // DGRAM ICMP socket 回包里 id 是内核分配值,需还原成源端 origIcmpId; + // 然后重算 ICMP 校验和,再手工封 IP 头(源=origDst, 目的=origSrc), + // 交 NetStack::output 封 IPIP 回源端。 + if (icmpReply.size() < ICMP_HDR_LEN) { + return; + } + this->lastActiveTs = std::chrono::steady_clock::now(); + + uint8_t *icmp = (uint8_t *)icmpReply.data(); + // 还原 icmp id(偏移 4..5),并把 type 规整为 echo reply(0)。 + icmp[0] = 0; // echo reply + icmp[1] = 0; // code + icmp[4] = (uint8_t)(this->icmpId >> 8); + icmp[5] = (uint8_t)(this->icmpId & 0xff); + // 重算 ICMP 校验和(偏移 2..3 先清零)。 + icmp[2] = 0; + icmp[3] = 0; + uint16_t csum = checksum16(icmp, icmpReply.size()); + icmp[2] = (uint8_t)(csum >> 8); + icmp[3] = (uint8_t)(csum & 0xff); + + // 手工封 IP 头:源=origDst(dev2),目的=origSrc(dev1),协议=ICMP(1)。 + std::string packet; + packet.resize(sizeof(IP4Header) + icmpReply.size()); + IP4Header *ip = (IP4Header *)packet.data(); + std::memset(ip, 0, sizeof(IP4Header)); + ip->version_ihl = 0x45; + ip->tos = 0; + ip->tot_len = hton((uint16_t)(sizeof(IP4Header) + icmpReply.size())); + ip->id = 0; + ip->frag_off = 0; + ip->ttl = 64; + ip->protocol = 0x01; // ICMP + ip->check = 0; + ip->saddr = this->origDst; + ip->daddr = this->origSrc; + uint16_t ipsum = checksum16((const uint8_t *)ip, sizeof(IP4Header)); + ip->check = hton(ipsum); + + std::memcpy(packet.data() + sizeof(IP4Header), icmpReply.data(), icmpReply.size()); + + // 经 NetStack::output 按 origSrc 查 vnetPeer、封 IPIP 回源端。 + this->stack->output(packet); +} + +void SessionIcmp::shutdownFromStack() { + // 栈拆除/空闲回收:fd 交由 reactor 关闭并释放自持。 + if (!this->closing.exchange(true)) { + auto holder = shared_from_this(); + this->stack->getReactor().post([holder] { +#if defined(__linux__) || defined(__APPLE__) + if (holder->fd >= 0) { + holder->stack->getReactor().del(holder->fd); + ::close(holder->fd); + holder->fd = -1; + } +#endif + holder->self.reset(); + }); + } +} + +// ===================== Reactor 线程 ===================== + +void SessionIcmp::onFdEvent(uint32_t events) { + ReactorEvent ev = (ReactorEvent)events; + if (ev & ReactorEvent::ERROR) { + closeFromReactor(); + return; + } + if (ev & ReactorEvent::READ) { + onFdReadable(); + } +} + +void SessionIcmp::onFdReadable() { +#if defined(__linux__) || defined(__APPLE__) + char buf[65536]; + while (true) { + ssize_t n = ::recv(this->fd, buf, sizeof(buf), 0); + if (n > 0) { + size_t off = 0; + // RAW ICMP socket 收到的是含 IP 头的整包,需剥掉 IP 头取 ICMP; + // DGRAM ICMP socket 收到的直接是 ICMP 报文。 + if (this->rawMode && n >= (ssize_t)sizeof(IP4Header)) { + uint8_t ihl = (((uint8_t *)buf)[0] & 0x0f) * 4; + if ((ssize_t)ihl <= n) { + off = ihl; + } + } + std::string data(buf + off, n - off); + auto holder = shared_from_this(); + this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->replyToPeer(std::move(data)); }); + continue; + } + if (n == 0) { + return; + } + if (errno == EAGAIN || errno == EWOULDBLOCK) { + return; + } + // 落地 socket 错误(如目的不可达):关闭会话。 + closeFromReactor(); + return; + } +#endif +} + +void SessionIcmp::closeFromReactor() { + if (this->closing.exchange(true)) { + return; + } +#if defined(__linux__) || defined(__APPLE__) + if (this->fd >= 0) { + this->stack->getReactor().del(this->fd); + ::close(this->fd); + this->fd = -1; + } +#endif + NetStack *stack = this->stack; + std::string k = this->sessionKey; + auto holder = shared_from_this(); + stack->postToStack([holder, stack, k] { + stack->removeIcmpSession(k); + holder->self.reset(); + }); +} + +} // namespace candy diff --git a/candy/src/netstack/session_icmp.h b/candy/src/netstack/session_icmp.h new file mode 100644 index 00000000..fd56a8b6 --- /dev/null +++ b/candy/src/netstack/session_icmp.h @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_SESSION_ICMP_H +#define CANDY_NETSTACK_SESSION_ICMP_H + +#include "core/net.h" +#include "netstack/session.h" +#include +#include +#include +#include +#include + +namespace candy { + +// SessionIcmp:一条 ICMP echo "伪会话"——把某个 (源,目的,icmp-id) 的 ping 流, +// 映射到一个 connect 到目的的内核 ICMP socket 落地(等价 iptables MASQUERADE)。 +// +// 与 TCP/UDP 不同,ICMP 不走 lwIP:lwIP 的 PRETEND netif 只接受 TCP/UDP, +// 故 ICMP echo 在入栈前(NetStack::handleInput)被直接拦截,由本会话落地+回包。 +// +// 线程契约(与 SessionUdp 一致): +// - 所有跨线程交互通过 NetStack::postToStack / Reactor::post; +// - fd 的 send/recv 只在 Reactor 线程;回包构造在 NetStack 线程。 +// +// 数据流: +// 正向:handleIcmp(NetStack线程) 提取 echo request -> 投递 reactor send(fd) +// 反向:fd 可读(reactor线程) recv echo reply -> 投递 NetStack 还原 id/校验和 +// -> 手工封 IP 包 -> NetStack::output 封 IPIP 回源端 +class SessionIcmp : public Session, public std::enable_shared_from_this { +public: + // origSrc/origDst:源端(dev1)/落地目的(dev2);icmpId:源端 ping 进程的 icmp id。 + SessionIcmp(NetStack *stack, IP4 origSrc, IP4 origDst, uint16_t icmpId); + ~SessionIcmp() override; + + int start() override; + + // 仅供 NetStack 线程在栈拆除/空闲回收时调用,强制关闭 fd。 + void shutdownFromStack(); + + // NetStack 线程:把一份 echo request 的 ICMP 报文(含 icmp 头)投递到落地 fd 发送。 + void sendEcho(std::string icmpData); + + // 该会话的 key(NetStack 线程的会话表用)。 + const std::string &key() const { return this->sessionKey; } + + // 最近活跃时间(空闲超时回收,仅 NetStack 线程访问)。 + std::chrono::steady_clock::time_point lastActive() const { return this->lastActiveTs; } + +private: + // ---- Reactor 线程 ---- + void onFdEvent(uint32_t events); + void onFdReadable(); + void closeFromReactor(); + + // ---- NetStack 线程 ---- + // 落地 fd 收到 echo reply 后:还原原始 icmp id、重算校验和、封 IP 头回送源端。 + void replyToPeer(std::string icmpReply); + +private: + int fd; + bool rawMode; + + IP4 origSrc; + IP4 origDst; + uint16_t icmpId; + + std::string sessionKey; + std::chrono::steady_clock::time_point lastActiveTs; + + std::shared_ptr self; +}; + +} // namespace candy + +#endif diff --git a/candy/src/tun/tun.cc b/candy/src/tun/tun.cc index 584a660a..7a2ca165 100644 --- a/candy/src/tun/tun.cc +++ b/candy/src/tun/tun.cc @@ -200,14 +200,16 @@ Client &Tun::getClient() { return *this->client; } -// 跟踪 TCP(0x06)/UDP(0x11) 发起流。返回是否成功解析出五元组。 +// 跟踪 TCP(0x06)/UDP(0x11)/ICMP(0x01) 发起流。返回是否成功解析出 key。 // TCP 与 UDP 的 L4 头前 4 字节都是 (源端口, 目的端口),端口提取逻辑一致。 +// ICMP 无端口:用 echo 的 id 同时充当 sport/dport,使"请求(type 8)"与 +// "应答(type 0)"在五元组反转后能配对(id 不变,src/dst 互换即对应)。 static bool parseFlow(const std::string &inner, FlowKey &key) { if (inner.size() < sizeof(IP4Header)) { return false; } const IP4Header *ip = (const IP4Header *)inner.data(); - if (ip->protocol != 0x06 && ip->protocol != 0x11) { // TCP / UDP + if (ip->protocol != 0x06 && ip->protocol != 0x11 && ip->protocol != 0x01) { // TCP / UDP / ICMP return false; } size_t ihl = (ip->version_ihl & 0x0f) * 4; @@ -215,6 +217,23 @@ static bool parseFlow(const std::string &inner, FlowKey &key) { return false; } const uint8_t *l4 = (const uint8_t *)inner.data() + ihl; + if (ip->protocol == 0x01) { + // ICMP:仅跟踪 echo request(8) / echo reply(0),其余不跟踪。 + uint8_t type = l4[0]; + if (type != 8 && type != 0) { + return false; + } + if (inner.size() < ihl + 6) { + return false; + } + uint16_t id = ((uint16_t)l4[4] << 8) | l4[5]; + key.src = uint32_t(ip->saddr); + key.dst = uint32_t(ip->daddr); + key.sport = id; + key.dport = id; + key.proto = ip->protocol; + return true; + } key.src = uint32_t(ip->saddr); key.dst = uint32_t(ip->daddr); key.sport = ((uint16_t)l4[0] << 8) | l4[1]; From 2eb46f46f7d749141e0810f0a0040421be3a0df4 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sun, 28 Jun 2026 10:12:27 +0800 Subject: [PATCH 05/29] support Mac/Windows, no test --- .github/workflows/ci.yaml | 78 ++++++ candy/src/netstack/reactor.cc | 450 ++++++++++++++++++++++++++++++ candy/src/netstack/reactor.h | 21 +- candy/src/netstack/session_tcp.cc | 55 ++-- candy/src/netstack/session_udp.cc | 43 ++- candy/src/netstack/sockcompat.h | 104 +++++++ 6 files changed, 701 insertions(+), 50 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 candy/src/netstack/sockcompat.h diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 00000000..a0573afd --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,78 @@ +name: ci + +# 编译级回归门禁:在 linux / macos / windows 三个 runner 上做编译验证。 +# 与 check.yaml(仅 PR 触发 + clang-format)互补,这里在每次 push 与手动触发时也跑, +# 任意一端编译失败即让流水线变红,挡住编译级回归。 +on: + push: + branches: [master] + pull_request: + branches: [master] + workflow_dispatch: + +# 同一分支/PR 的新提交会取消上一次仍在跑的任务,节省 runner 时间。 +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + linux: + name: build-linux + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v4 + # 与 dockerfile 保持一致:alpine + ninja + g++ 的权威编译环境。 + - name: build (docker) + run: docker build . + + macos: + name: build-macos + runs-on: macos-latest + steps: + - name: checkout + uses: actions/checkout@v4 + # macOS 走 kqueue 后端,依赖通过 homebrew 安装。 + - name: depends + run: brew update && brew install fmt poco spdlog openssl@3 + - name: build + run: | + if [ "$RUNNER_ARCH" == "ARM64" ]; then + export CPATH=/opt/homebrew/include + export LIBRARY_PATH=/opt/homebrew/lib + export OPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3 + else + export CPATH=/usr/local/include + export LIBRARY_PATH=/usr/local/lib + export OPENSSL_ROOT_DIR=/usr/local/opt/openssl@3 + fi + cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo + cmake --build build + + windows: + name: build-windows + runs-on: windows-latest + steps: + # Windows 当前为占位实现(reactor 为 IOCP/select stub),仅做编译验证。 + - name: depends + uses: msys2/setup-msys2@v2 + with: + msystem: MINGW64 + update: true + install: >- + mingw-w64-x86_64-cmake + mingw-w64-x86_64-ninja + mingw-w64-x86_64-gcc + mingw-w64-x86_64-spdlog + mingw-w64-x86_64-poco + mingw-w64-x86_64-openssl + - name: checkout + uses: actions/checkout@v4 + - name: cache + uses: actions/cache@v4 + with: + path: build + key: ${{ runner.os }}-${{ hashFiles('CMakeLists.txt') }} + - name: build + shell: msys2 {0} + run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo && cmake --build build diff --git a/candy/src/netstack/reactor.cc b/candy/src/netstack/reactor.cc index 5e7d0177..08b50c15 100644 --- a/candy/src/netstack/reactor.cc +++ b/candy/src/netstack/reactor.cc @@ -6,6 +6,17 @@ #include #include #include +#elif defined(__APPLE__) +#include +#include +#include +#elif defined(_WIN32) || defined(_WIN64) +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#endif +#include +#include +#include #endif namespace candy { @@ -200,6 +211,445 @@ void Reactor::loop() { spdlog::debug("stop thread: netstack reactor"); } +#elif defined(__APPLE__) + +// EVFILT_USER 的标识,用于跨线程唤醒 reactor 线程(替代 Linux 的 eventfd)。 +static constexpr uintptr_t WAKEUP_IDENT = 1; + +Reactor::Reactor() : kqueueFd(-1), running(false) {} + +Reactor::~Reactor() { + stop(); +} + +int Reactor::start() { + this->kqueueFd = kqueue(); + if (this->kqueueFd < 0) { + spdlog::error("reactor kqueue failed: {}", strerror(errno)); + return -1; + } + + // 注册 EVFILT_USER 作为唤醒通道。 + struct kevent kev = {}; + EV_SET(&kev, WAKEUP_IDENT, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, nullptr); + if (kevent(this->kqueueFd, &kev, 1, nullptr, 0, nullptr) != 0) { + spdlog::error("reactor register EVFILT_USER failed: {}", strerror(errno)); + ::close(this->kqueueFd); + this->kqueueFd = -1; + return -1; + } + + this->running.store(true); + this->thread = std::thread([this] { this->loop(); }); + return 0; +} + +void Reactor::stop() { + if (!this->running.exchange(false)) { + if (this->thread.joinable()) { + this->thread.join(); + } + return; + } + wakeup(); + if (this->thread.joinable()) { + this->thread.join(); + } + if (this->kqueueFd >= 0) { + ::close(this->kqueueFd); + this->kqueueFd = -1; + } +} + +int Reactor::add(int fd, ReactorEvent events, EventHandler handler) { + { + std::unique_lock lock(this->handlerMutex); + this->handlers[fd] = std::move(handler); + } + + // kqueue 的 READ/WRITE 为独立 filter,按兴趣集分别 EV_ADD。 + struct kevent kev[2]; + int n = 0; + if (events & ReactorEvent::READ) { + EV_SET(&kev[n++], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, nullptr); + } + if (events & ReactorEvent::WRITE) { + EV_SET(&kev[n++], fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, nullptr); + } + if (n > 0 && kevent(this->kqueueFd, kev, n, nullptr, 0, nullptr) != 0) { + spdlog::error("reactor add fd {} failed: {}", fd, strerror(errno)); + std::unique_lock lock(this->handlerMutex); + this->handlers.erase(fd); + return -1; + } + this->interests[fd] = events; + return 0; +} + +int Reactor::mod(int fd, ReactorEvent events) { + // 对比旧兴趣集,对增加的 filter EV_ADD、移除的 filter EV_DELETE,实现整体替换语义。 + ReactorEvent old = ReactorEvent::NONE; + auto it = this->interests.find(fd); + if (it != this->interests.end()) { + old = it->second; + } + + struct kevent kev[2]; + int n = 0; + bool oldRead = (old & ReactorEvent::READ); + bool newRead = (events & ReactorEvent::READ); + bool oldWrite = (old & ReactorEvent::WRITE); + bool newWrite = (events & ReactorEvent::WRITE); + if (newRead && !oldRead) { + EV_SET(&kev[n++], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, nullptr); + } else if (!newRead && oldRead) { + EV_SET(&kev[n++], fd, EVFILT_READ, EV_DELETE, 0, 0, nullptr); + } + if (newWrite && !oldWrite) { + EV_SET(&kev[n++], fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, nullptr); + } else if (!newWrite && oldWrite) { + EV_SET(&kev[n++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, nullptr); + } + if (n > 0 && kevent(this->kqueueFd, kev, n, nullptr, 0, nullptr) != 0) { + // EV_DELETE 对未注册 filter 返回 ENOENT 可忽略。 + if (errno != ENOENT) { + spdlog::error("reactor mod fd {} failed: {}", fd, strerror(errno)); + return -1; + } + } + this->interests[fd] = events; + return 0; +} + +int Reactor::del(int fd) { + ReactorEvent old = ReactorEvent::NONE; + auto it = this->interests.find(fd); + if (it != this->interests.end()) { + old = it->second; + } + struct kevent kev[2]; + int n = 0; + if (old & ReactorEvent::READ) { + EV_SET(&kev[n++], fd, EVFILT_READ, EV_DELETE, 0, 0, nullptr); + } + if (old & ReactorEvent::WRITE) { + EV_SET(&kev[n++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, nullptr); + } + if (n > 0) { + kevent(this->kqueueFd, kev, n, nullptr, 0, nullptr); + } + this->interests.erase(fd); + std::unique_lock lock(this->handlerMutex); + this->handlers.erase(fd); + return 0; +} + +void Reactor::post(Task task) { + { + std::unique_lock lock(this->taskMutex); + this->tasks.push(std::move(task)); + } + wakeup(); +} + +void Reactor::wakeup() { + if (this->kqueueFd < 0) { + return; + } + struct kevent kev = {}; + EV_SET(&kev, WAKEUP_IDENT, EVFILT_USER, 0, NOTE_TRIGGER, 0, nullptr); + kevent(this->kqueueFd, &kev, 1, nullptr, 0, nullptr); +} + +void Reactor::drainWakeup() { + // EVFILT_USER 配合 EV_CLEAR 为边沿触发,无需额外清空动作。 +} + +void Reactor::drainTasks() { + std::queue pending; + { + std::unique_lock lock(this->taskMutex); + std::swap(pending, this->tasks); + } + while (!pending.empty()) { + pending.front()(); + pending.pop(); + } +} + +void Reactor::loop() { + spdlog::debug("start thread: netstack reactor"); + constexpr int MAX_EVENTS = 256; + struct kevent events[MAX_EVENTS]; + while (this->running.load()) { + int n = kevent(this->kqueueFd, nullptr, 0, events, MAX_EVENTS, nullptr); + if (n < 0) { + if (errno == EINTR) { + continue; + } + spdlog::error("reactor kevent failed: {}", strerror(errno)); + break; + } + for (int i = 0; i < n; ++i) { + if (events[i].filter == EVFILT_USER && events[i].ident == WAKEUP_IDENT) { + drainWakeup(); + drainTasks(); + continue; + } + int fd = (int)events[i].ident; + EventHandler handler; + { + std::unique_lock lock(this->handlerMutex); + auto it = this->handlers.find(fd); + if (it == this->handlers.end()) { + continue; + } + handler = it->second; + } + ReactorEvent ev = ReactorEvent::NONE; + if (events[i].filter == EVFILT_READ) { + ev = ev | ReactorEvent::READ; + } + if (events[i].filter == EVFILT_WRITE) { + ev = ev | ReactorEvent::WRITE; + } + if (events[i].flags & EV_EOF) { + ev = ev | ReactorEvent::ERROR; + } + handler(ev); + } + } + spdlog::debug("stop thread: netstack reactor"); +} + +#elif defined(_WIN32) || defined(_WIN64) + +// Windows 后端:用 WSAPoll 做就绪通知(无内核兴趣集,每轮按 interests 重建 pollfd 数组), +// 用一对自连环回 UDP socket 做跨线程唤醒(替代 Linux eventfd / macOS EVFILT_USER)。 +// 注:与 Linux/macOS 一致,对外仍以 int fd 暴露;SOCKET 句柄在 Windows 上为小整数,转换安全。 + +static SHORT toWsaEvents(ReactorEvent events) { + SHORT ev = 0; + if (events & ReactorEvent::READ) { + ev |= POLLRDNORM; + } + if (events & ReactorEvent::WRITE) { + ev |= POLLWRNORM; + } + return ev; +} + +static ReactorEvent fromWsaEvents(SHORT ev) { + ReactorEvent events = ReactorEvent::NONE; + if (ev & POLLRDNORM) { + events = events | ReactorEvent::READ; + } + if (ev & POLLWRNORM) { + events = events | ReactorEvent::WRITE; + } + if (ev & (POLLERR | POLLHUP | POLLNVAL)) { + events = events | ReactorEvent::ERROR; + } + return events; +} + +Reactor::Reactor() : wakeupRecv(-1), wakeupSend(-1), running(false) {} + +Reactor::~Reactor() { + stop(); +} + +int Reactor::start() { + // WSAStartup 引用计数,tun/windows.cc 可能已初始化,这里再调一次安全。 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + spdlog::error("reactor WSAStartup failed: {}", WSAGetLastError()); + return -1; + } + + SOCKET recvSock = ::socket(AF_INET, SOCK_DGRAM, 0); + SOCKET sendSock = ::socket(AF_INET, SOCK_DGRAM, 0); + if (recvSock == INVALID_SOCKET || sendSock == INVALID_SOCKET) { + spdlog::error("reactor wakeup socket failed: {}", WSAGetLastError()); + if (recvSock != INVALID_SOCKET) { + ::closesocket(recvSock); + } + if (sendSock != INVALID_SOCKET) { + ::closesocket(sendSock); + } + WSACleanup(); + return -1; + } + + // recv 端绑定到 127.0.0.1 随机端口,send 端 connect 到该端口形成自连环回。 + struct sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = 0; + int addrLen = sizeof(addr); + if (::bind(recvSock, (struct sockaddr *)&addr, addrLen) != 0 || + ::getsockname(recvSock, (struct sockaddr *)&addr, &addrLen) != 0 || + ::connect(sendSock, (struct sockaddr *)&addr, sizeof(addr)) != 0) { + spdlog::error("reactor wakeup loopback setup failed: {}", WSAGetLastError()); + ::closesocket(recvSock); + ::closesocket(sendSock); + WSACleanup(); + return -1; + } + + u_long nonBlocking = 1; + ::ioctlsocket(recvSock, FIONBIO, &nonBlocking); + + this->wakeupRecv = (int)recvSock; + this->wakeupSend = (int)sendSock; + + this->running.store(true); + this->thread = std::thread([this] { this->loop(); }); + return 0; +} + +void Reactor::stop() { + if (!this->running.exchange(false)) { + if (this->thread.joinable()) { + this->thread.join(); + } + return; + } + wakeup(); + if (this->thread.joinable()) { + this->thread.join(); + } + if (this->wakeupSend >= 0) { + ::closesocket((SOCKET)this->wakeupSend); + this->wakeupSend = -1; + } + if (this->wakeupRecv >= 0) { + ::closesocket((SOCKET)this->wakeupRecv); + this->wakeupRecv = -1; + } + WSACleanup(); +} + +int Reactor::add(int fd, ReactorEvent events, EventHandler handler) { + std::unique_lock lock(this->handlerMutex); + this->handlers[fd] = std::move(handler); + this->interests[fd] = events; + return 0; +} + +int Reactor::mod(int fd, ReactorEvent events) { + std::unique_lock lock(this->handlerMutex); + this->interests[fd] = events; + return 0; +} + +int Reactor::del(int fd) { + std::unique_lock lock(this->handlerMutex); + this->interests.erase(fd); + this->handlers.erase(fd); + return 0; +} + +void Reactor::post(Task task) { + { + std::unique_lock lock(this->taskMutex); + this->tasks.push(std::move(task)); + } + wakeup(); +} + +void Reactor::wakeup() { + if (this->wakeupSend < 0) { + return; + } + char one = 1; + ::send((SOCKET)this->wakeupSend, &one, 1, 0); +} + +void Reactor::drainWakeup() { + char buf[256]; + while (::recv((SOCKET)this->wakeupRecv, buf, sizeof(buf), 0) > 0) { + // 持续读空唤醒 socket,避免残留触发空转。 + } +} + +void Reactor::drainTasks() { + std::queue pending; + { + std::unique_lock lock(this->taskMutex); + std::swap(pending, this->tasks); + } + while (!pending.empty()) { + pending.front()(); + pending.pop(); + } +} + +void Reactor::loop() { + spdlog::debug("start thread: netstack reactor"); + std::vector pfds; + std::vector fds; + while (this->running.load()) { + pfds.clear(); + fds.clear(); + + // 唤醒 socket 固定排第一位,只关心可读。 + WSAPOLLFD wake = {}; + wake.fd = (SOCKET)this->wakeupRecv; + wake.events = POLLRDNORM; + pfds.push_back(wake); + fds.push_back(this->wakeupRecv); + + // 快照当前兴趣集,构建 pollfd 数组。 + { + std::unique_lock lock(this->handlerMutex); + for (const auto &kv : this->interests) { + WSAPOLLFD pfd = {}; + pfd.fd = (SOCKET)kv.first; + pfd.events = toWsaEvents(kv.second); + pfds.push_back(pfd); + fds.push_back(kv.first); + } + } + + int n = WSAPoll(pfds.data(), (ULONG)pfds.size(), -1); + if (n < 0) { + int err = WSAGetLastError(); + if (err == WSAEINTR) { + continue; + } + spdlog::error("reactor WSAPoll failed: {}", err); + break; + } + if (n == 0) { + continue; + } + + // 先处理唤醒,再分发 fd 事件。 + if (pfds[0].revents != 0) { + drainWakeup(); + drainTasks(); + } + for (size_t i = 1; i < pfds.size(); ++i) { + if (pfds[i].revents == 0) { + continue; + } + int fd = fds[i]; + EventHandler handler; + { + std::unique_lock lock(this->handlerMutex); + auto it = this->handlers.find(fd); + if (it == this->handlers.end()) { + continue; + } + handler = it->second; + } + handler(fromWsaEvents(pfds[i].revents)); + } + } + spdlog::debug("stop thread: netstack reactor"); +} + #else Reactor::Reactor() : epollFd(-1), wakeupFd(-1), running(false) {} diff --git a/candy/src/netstack/reactor.h b/candy/src/netstack/reactor.h index b8aa9612..e8a7a1e1 100644 --- a/candy/src/netstack/reactor.h +++ b/candy/src/netstack/reactor.h @@ -27,9 +27,9 @@ inline bool operator&(ReactorEvent a, ReactorEvent b) { return (static_cast(a) & static_cast(b)) != 0; } -// Reactor:基于 epoll 的事件循环(阶段一 Linux 优先;macOS kqueue / Windows IOCP 后续补)。 -// 管理所有落地 fd(内核 socket)的可读/可写/错误事件,非阻塞、事件回调驱动。 -// 跨线程提交任务用 post(),由 reactor 线程在事件循环中执行(经 eventfd 唤醒)。 +// Reactor:落地 fd 的事件循环。Linux 用 epoll + eventfd,macOS 用 kqueue + EVFILT_USER, +// Windows 用 WSAPoll + 自连环回 UDP 唤醒 socket。管理所有落地 fd(内核 socket)的可读/可写/错误事件, +// 非阻塞、事件回调驱动。跨线程提交任务用 post(),由 reactor 线程在事件循环中执行(经唤醒机制)。 class Reactor { public: using EventHandler = std::function; @@ -53,8 +53,23 @@ class Reactor { void drainTasks(); void drainWakeup(); +#if defined(__linux__) int epollFd; int wakeupFd; +#elif defined(__APPLE__) + int kqueueFd; + // kqueue 的 READ/WRITE 为独立 filter,需记录每个 fd 当前兴趣集以做增量增删。 + std::unordered_map interests; +#elif defined(_WIN32) || defined(_WIN64) + // WSAPoll 无内核兴趣集,需在用户态维护 fd->兴趣集,每轮重建 pollfd 数组。 + std::unordered_map interests; + // 自连环回 UDP socket:wakeupRecv 注册进 poll,wakeup() 时往 wakeupSend 发一字节触发可读。 + int wakeupRecv; + int wakeupSend; +#else + int epollFd; + int wakeupFd; +#endif std::thread thread; std::atomic running; diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc index 2e371e3c..68373cec 100644 --- a/candy/src/netstack/session_tcp.cc +++ b/candy/src/netstack/session_tcp.cc @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT #include "netstack/session_tcp.h" #include "netstack/netstack.h" +#include "netstack/sockcompat.h" #include #include #include @@ -10,10 +11,9 @@ #if defined(__linux__) || defined(__APPLE__) #include -#include #include -#include -#include +#elif defined(_WIN32) || defined(_WIN64) +#include #endif namespace candy { @@ -32,7 +32,7 @@ SessionTcp::SessionTcp(NetStack *stack, struct tcp_pcb *pcb) SessionTcp::~SessionTcp() { if (this->fd >= 0) { - ::close(this->fd); + netClose(this->fd); this->fd = -1; } } @@ -45,14 +45,13 @@ int SessionTcp::start() { tcp_sent(this->pcb, sentTrampoline); tcp_err(this->pcb, errTrampoline); -#if defined(__linux__) || defined(__APPLE__) - this->fd = ::socket(AF_INET, SOCK_STREAM, 0); +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + this->fd = (int)::socket(AF_INET, SOCK_STREAM, 0); if (this->fd < 0) { - spdlog::warn("session tcp socket failed: {}", strerror(errno)); + spdlog::warn("session tcp socket failed: {}", netErrStr(netLastError())); return -1; } - int flags = ::fcntl(this->fd, F_GETFL, 0); - ::fcntl(this->fd, F_SETFL, flags | O_NONBLOCK); + netSetNonBlocking(this->fd); struct sockaddr_in dst = {}; dst.sin_family = AF_INET; @@ -60,10 +59,10 @@ int SessionTcp::start() { dst.sin_addr.s_addr = uint32_t(this->origDst); int ret = ::connect(this->fd, (struct sockaddr *)&dst, sizeof(dst)); - if (ret != 0 && errno != EINPROGRESS) { + if (ret != 0 && !netInProgress(netLastError())) { spdlog::warn("session tcp connect {}:{} failed: {}", this->origDst.toString(), this->origDstPort, - strerror(errno)); - ::close(this->fd); + netErrStr(netLastError())); + netClose(this->fd); this->fd = -1; return -1; } @@ -285,10 +284,10 @@ void SessionTcp::shutdownFromStack() { if (!this->closing.exchange(true)) { auto holder = shared_from_this(); this->stack->getReactor().post([holder] { -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) if (holder->fd >= 0) { holder->stack->getReactor().del(holder->fd); - ::close(holder->fd); + netClose(holder->fd); holder->fd = -1; } #endif @@ -322,11 +321,17 @@ void SessionTcp::onFdEvent(uint32_t events) { } void SessionTcp::onConnected() { -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) int err = 0; +#if defined(_WIN32) || defined(_WIN64) + int len = sizeof(err); + int ret = ::getsockopt((SOCKET)this->fd, SOL_SOCKET, SO_ERROR, (char *)&err, &len); +#else socklen_t len = sizeof(err); - if (::getsockopt(this->fd, SOL_SOCKET, SO_ERROR, &err, &len) != 0 || err != 0) { - spdlog::warn("session tcp connect result error: {}", strerror(err ? err : errno)); + int ret = ::getsockopt(this->fd, SOL_SOCKET, SO_ERROR, &err, &len); +#endif + if (ret != 0 || err != 0) { + spdlog::warn("session tcp connect result error: {}", netErrStr(err ? err : netLastError())); closeFromReactor(); return; } @@ -337,7 +342,7 @@ void SessionTcp::onConnected() { } void SessionTcp::flushForwardLocked() { -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) if (this->fd < 0 || !this->connected) { return; } @@ -347,13 +352,13 @@ void SessionTcp::flushForwardLocked() { { std::unique_lock lock(this->bufMutex); while (!this->forwardBuf.empty()) { - ssize_t n = ::write(this->fd, this->forwardBuf.data(), this->forwardBuf.size()); + long n = netSend(this->fd, this->forwardBuf.data(), this->forwardBuf.size()); if (n > 0) { this->forwardBuf.erase(0, n); acked += (size_t)n; continue; } - if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { + if (n < 0 && netWouldBlock(netLastError())) { this->wantWrite = true; this->stack->getReactor().mod(this->fd, ReactorEvent::READ | ReactorEvent::WRITE); blocked = true; @@ -390,7 +395,7 @@ void SessionTcp::onFdWritable() { } void SessionTcp::onFdReadable() { -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) char buf[65536]; while (true) { // 同步背压:积压超过高水位立即停读,避免单线程 NetStack 被 writeToLwip 任务淹没。 @@ -404,7 +409,7 @@ void SessionTcp::onFdReadable() { this->stack->getReactor().mod(this->fd, ReactorEvent::NONE); return; } - ssize_t n = ::read(this->fd, buf, sizeof(buf)); + long n = netRecv(this->fd, buf, sizeof(buf)); if (n > 0) { this->backwardBytes.fetch_add((size_t)n); std::string data(buf, n); @@ -419,7 +424,7 @@ void SessionTcp::onFdReadable() { this->stack->getReactor().del(this->fd); return; } - if (errno == EAGAIN || errno == EWOULDBLOCK) { + if (netWouldBlock(netLastError())) { return; } closeFromReactor(); @@ -432,10 +437,10 @@ void SessionTcp::closeFromReactor() { if (this->closing.exchange(true)) { return; } -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) if (this->fd >= 0) { this->stack->getReactor().del(this->fd); - ::close(this->fd); + netClose(this->fd); this->fd = -1; } #endif diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index 138a5e14..ce15db87 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT #include "netstack/session_udp.h" #include "netstack/netstack.h" +#include "netstack/sockcompat.h" #include #include @@ -9,10 +10,9 @@ #if defined(__linux__) || defined(__APPLE__) #include -#include #include -#include -#include +#elif defined(_WIN32) || defined(_WIN64) +#include #endif namespace candy { @@ -30,22 +30,21 @@ SessionUdp::SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16 SessionUdp::~SessionUdp() { if (this->fd >= 0) { - ::close(this->fd); + netClose(this->fd); this->fd = -1; } } int SessionUdp::start() { -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) this->self = shared_from_this(); - this->fd = ::socket(AF_INET, SOCK_DGRAM, 0); + this->fd = (int)::socket(AF_INET, SOCK_DGRAM, 0); if (this->fd < 0) { - spdlog::warn("session udp socket failed: {}", strerror(errno)); + spdlog::warn("session udp socket failed: {}", netErrStr(netLastError())); return -1; } - int flags = ::fcntl(this->fd, F_GETFL, 0); - ::fcntl(this->fd, F_SETFL, flags | O_NONBLOCK); + netSetNonBlocking(this->fd); // connect 固定对端:内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE), // 且后续 send/recv 只与该对端往来,形成 Fullcone NAT 的伪会话。 @@ -55,8 +54,8 @@ int SessionUdp::start() { dst.sin_addr.s_addr = uint32_t(this->origDst); if (::connect(this->fd, (struct sockaddr *)&dst, sizeof(dst)) != 0) { spdlog::warn("session udp connect {}:{} failed: {}", this->origDst.toString(), this->origDstPort, - strerror(errno)); - ::close(this->fd); + netErrStr(netLastError())); + netClose(this->fd); this->fd = -1; return -1; } @@ -101,13 +100,13 @@ void SessionUdp::sendToLanding(std::string data) { this->lastActiveTs = std::chrono::steady_clock::now(); auto holder = shared_from_this(); this->stack->getReactor().post([holder, data = std::move(data)]() mutable { -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) if (holder->fd < 0 || holder->closing.load()) { return; } - ssize_t n = ::send(holder->fd, data.data(), data.size(), 0); - if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { - spdlog::debug("session udp send failed: {}", strerror(errno)); + long n = netSend(holder->fd, data.data(), data.size()); + if (n < 0 && !netWouldBlock(netLastError())) { + spdlog::debug("session udp send failed: {}", netErrStr(netLastError())); } #endif }); @@ -163,10 +162,10 @@ void SessionUdp::shutdownFromStack() { if (!this->closing.exchange(true)) { auto holder = shared_from_this(); this->stack->getReactor().post([holder] { -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) if (holder->fd >= 0) { holder->stack->getReactor().del(holder->fd); - ::close(holder->fd); + netClose(holder->fd); holder->fd = -1; } #endif @@ -189,10 +188,10 @@ void SessionUdp::onFdEvent(uint32_t events) { } void SessionUdp::onFdReadable() { -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) char buf[65536]; while (true) { - ssize_t n = ::recv(this->fd, buf, sizeof(buf), 0); + long n = netRecv(this->fd, buf, sizeof(buf)); if (n > 0) { std::string data(buf, n); auto holder = shared_from_this(); @@ -202,7 +201,7 @@ void SessionUdp::onFdReadable() { if (n == 0) { return; } - if (errno == EAGAIN || errno == EWOULDBLOCK) { + if (netWouldBlock(netLastError())) { return; } // 落地 socket 错误(如 ICMP port unreachable 经 connect 返回 ECONNREFUSED):关闭会话。 @@ -216,10 +215,10 @@ void SessionUdp::closeFromReactor() { if (this->closing.exchange(true)) { return; } -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) if (this->fd >= 0) { this->stack->getReactor().del(this->fd); - ::close(this->fd); + netClose(this->fd); this->fd = -1; } #endif diff --git a/candy/src/netstack/sockcompat.h b/candy/src/netstack/sockcompat.h new file mode 100644 index 00000000..9acbc708 --- /dev/null +++ b/candy/src/netstack/sockcompat.h @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_SOCKCOMPAT_H +#define CANDY_NETSTACK_SOCKCOMPAT_H + +// 落地 socket 的跨平台薄封装:抹平 POSIX(Linux/macOS) 与 Winsock(Windows) 的差异, +// 使 session_tcp / session_udp 的落地收发逻辑三端共用同一份代码。 +// 约定:对外统一以 int 表示 socket(Windows SOCKET 为小整数句柄,转换安全)。 + +#include +#include + +#if defined(_WIN32) || defined(_WIN64) +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#endif +#include +#include +#else +#include +#include +#include +#include +#include +#endif + +namespace candy { + +// 关闭落地 socket。 +inline void netClose(int fd) { +#if defined(_WIN32) || defined(_WIN64) + ::closesocket((SOCKET)fd); +#else + ::close(fd); +#endif +} + +// 设为非阻塞。成功返回 0。 +inline int netSetNonBlocking(int fd) { +#if defined(_WIN32) || defined(_WIN64) + u_long on = 1; + return ::ioctlsocket((SOCKET)fd, FIONBIO, &on) == 0 ? 0 : -1; +#else + int flags = ::fcntl(fd, F_GETFL, 0); + return ::fcntl(fd, F_SETFL, flags | O_NONBLOCK); +#endif +} + +// 收:返回读到字节数;0 表示对端关闭;<0 表示错误(含 would-block)。 +inline long netRecv(int fd, void *buf, size_t len) { +#if defined(_WIN32) || defined(_WIN64) + return ::recv((SOCKET)fd, (char *)buf, (int)len, 0); +#else + return ::recv(fd, buf, len, 0); +#endif +} + +// 发:返回写出字节数;<0 表示错误(含 would-block)。 +inline long netSend(int fd, const void *buf, size_t len) { +#if defined(_WIN32) || defined(_WIN64) + return ::send((SOCKET)fd, (const char *)buf, (int)len, 0); +#else + return ::send(fd, buf, len, 0); +#endif +} + +// 取本线程最近一次 socket 错误码。 +inline int netLastError() { +#if defined(_WIN32) || defined(_WIN64) + return ::WSAGetLastError(); +#else + return errno; +#endif +} + +// 错误码是否为"暂时不可用"(非阻塞下应重试,而非关闭)。 +inline bool netWouldBlock(int err) { +#if defined(_WIN32) || defined(_WIN64) + return err == WSAEWOULDBLOCK; +#else + return err == EAGAIN || err == EWOULDBLOCK; +#endif +} + +// 错误码是否为"连接进行中"(非阻塞 connect 的正常返回)。 +inline bool netInProgress(int err) { +#if defined(_WIN32) || defined(_WIN64) + return err == WSAEWOULDBLOCK || err == WSAEINPROGRESS; +#else + return err == EINPROGRESS; +#endif +} + +// 错误码转可读字符串(仅用于日志)。 +inline std::string netErrStr(int err) { +#if defined(_WIN32) || defined(_WIN64) + return "wsa error " + std::to_string(err); +#else + return std::strerror(err); +#endif +} + +} // namespace candy + +#endif From 47c9daa080109caf390002cf166b501d8159f46e Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sun, 28 Jun 2026 12:08:49 +0800 Subject: [PATCH 06/29] tun2socks func --- candy/src/netstack/netstack.cc | 4 + candy/src/netstack/netstack.h | 7 + candy/src/netstack/outbound.cc | 69 ++++++++ candy/src/netstack/outbound.h | 71 ++++++++ candy/src/netstack/router.cc | 37 +++++ candy/src/netstack/router.h | 68 ++++++++ candy/src/netstack/session_tcp.cc | 20 +-- candy/src/netstack/session_udp.cc | 20 +-- candy/src/netstack/socks5_client.cc | 240 ++++++++++++++++++++++++++++ candy/src/netstack/socks5_client.h | 106 ++++++++++++ 10 files changed, 608 insertions(+), 34 deletions(-) create mode 100644 candy/src/netstack/outbound.cc create mode 100644 candy/src/netstack/outbound.h create mode 100644 candy/src/netstack/router.cc create mode 100644 candy/src/netstack/router.h create mode 100644 candy/src/netstack/socks5_client.cc create mode 100644 candy/src/netstack/socks5_client.h diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index 7afd1d54..8235a09b 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -37,6 +37,10 @@ Reactor &NetStack::getReactor() { return this->reactor; } +Outbound &NetStack::getOutbound() { + return this->directOutbound; +} + int NetStack::run(Client *client) { this->client = client; this->running.store(true); diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index 240c76ac..5d6b2a41 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -3,6 +3,7 @@ #define CANDY_NETSTACK_NETSTACK_H #include "core/net.h" +#include "netstack/outbound.h" #include "netstack/reactor.h" #include #include @@ -46,6 +47,8 @@ class NetStack { // 供 Session 使用:在 NetStack 线程内执行任务(保证 lwIP API 线程安全)。 void postToStack(std::function task); Reactor &getReactor(); + // 当前出站:默认 DirectOutbound(内核 socket 直连落地)。Session 经此发起落地拨号。 + Outbound &getOutbound(); // UDP 回包注入用:拿到本模块的 lwIP netif(仅 NetStack 线程使用)。 struct netif &getNetif(); @@ -100,6 +103,10 @@ class NetStack { Reactor reactor; + // 落地出站:阶段一/二恒为 DirectOutbound(内核 socket 直连)。阶段三引入 Router + // 后按流选择,此处先固定 direct,保证现有 kernel/userspace 行为不变。 + DirectOutbound directOutbound; + // NetStack 线程任务队列(用带超时读驱动定时器) std::mutex stackTaskMutex; std::condition_variable stackTaskCond; diff --git a/candy/src/netstack/outbound.cc b/candy/src/netstack/outbound.cc new file mode 100644 index 00000000..d35091c4 --- /dev/null +++ b/candy/src/netstack/outbound.cc @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: MIT +#include "netstack/outbound.h" +#include "netstack/sockcompat.h" +#include + +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#elif defined(_WIN32) || defined(_WIN64) +#include +#endif + +namespace candy { + +int DirectOutbound::dialTcp(const Endpoint &dst) { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + int fd = (int)::socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) { + spdlog::warn("direct outbound tcp socket failed: {}", netErrStr(netLastError())); + return -1; + } + netSetNonBlocking(fd); + + struct sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = htons(dst.port); + addr.sin_addr.s_addr = uint32_t(dst.host); + + int ret = ::connect(fd, (struct sockaddr *)&addr, sizeof(addr)); + if (ret != 0 && !netInProgress(netLastError())) { + spdlog::warn("direct outbound tcp connect {}:{} failed: {}", dst.host.toString(), dst.port, + netErrStr(netLastError())); + netClose(fd); + return -1; + } + return fd; +#else + return -1; +#endif +} + +int DirectOutbound::dialUdp(const Endpoint &dst) { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + int fd = (int)::socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + spdlog::warn("direct outbound udp socket failed: {}", netErrStr(netLastError())); + return -1; + } + netSetNonBlocking(fd); + + // connect 固定对端:内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE), + // 且后续 send/recv 只与该对端往来,形成 Fullcone NAT 的伪会话。 + struct sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = htons(dst.port); + addr.sin_addr.s_addr = uint32_t(dst.host); + if (::connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { + spdlog::warn("direct outbound udp connect {}:{} failed: {}", dst.host.toString(), dst.port, + netErrStr(netLastError())); + netClose(fd); + return -1; + } + return fd; +#else + return -1; +#endif +} + +} // namespace candy diff --git a/candy/src/netstack/outbound.h b/candy/src/netstack/outbound.h new file mode 100644 index 00000000..faa3ef16 --- /dev/null +++ b/candy/src/netstack/outbound.h @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_OUTBOUND_H +#define CANDY_NETSTACK_OUTBOUND_H + +#include "core/net.h" +#include +#include + +namespace candy { + +// 落地目的端点:一条被 lwIP 终结的流要连到的目标地址与端口。 +struct Endpoint { + IP4 host; + uint16_t port; +}; + +// Outbound:出站抽象。把"一条已被本机 lwIP 终结的流如何落地拨号"从 Session 中解耦, +// 使落地方式可替换(阶段一/二的内核 socket 直连收敛为 DirectOutbound,阶段三再加 +// Socks5Outbound / MeshOutbound)。 +// +// 设计约束(承接 03 文档 §3,并与现有 Session 实现保持一致): +// - dialTcp/dialUdp 只负责"建 socket + 设非阻塞 + 发起 connect",返回一个已发起 +// 连接的非阻塞 fd(失败返回 -1)。后续 reactor 注册、可读可写事件、双向 splice、 +// 背压等全部仍由 Session 自身处理 —— 因此本次抽取对运行时行为零影响。 +// - L4 终结型出站(direct/socks5)实现 dialTcp/dialUdp;needsTermination()==true。 +// - L3 转发型出站(mesh)不终结、走 forwardL3,dialTcp/dialUdp 返回 -1。 +class Outbound { +public: + virtual ~Outbound() = default; + + // 出站名称(direct / socks5 / mesh)。 + virtual std::string name() const = 0; + + // 是否需要发起端 lwIP 终结:direct/socks5 为 true,mesh 为 false。 + virtual bool needsTermination() const = 0; + + // L4 终结型出站:为一条已终结的 TCP 流建立落地通道,返回已发起 connect 的非阻塞 + // fd(EINPROGRESS 视为成功),失败返回 -1。默认不支持。 + virtual int dialTcp(const Endpoint &dst) { return -1; } + + // L4 终结型出站:为一条已终结的 UDP 流建立 connect 到目的的非阻塞 fd,失败返回 -1。 + // 默认不支持。 + virtual int dialUdp(const Endpoint &dst) { return -1; } +}; + +// DirectOutbound:内核 socket 直连落地(阶段一/二的拨号逻辑迁移至此)。 +// 源地址由内核在发起连接时自动填为本网关出口 IP,等价 MASQUERADE。 +class DirectOutbound : public Outbound { +public: + std::string name() const override { return "direct"; } + bool needsTermination() const override { return true; } + + int dialTcp(const Endpoint &dst) override; + int dialUdp(const Endpoint &dst) override; +}; + +// MeshOutbound:L3 转发型出口(自建节点 mesh)。发起端不终结,按 L3 原样封 IPIP 经 +// candy 虚拟网送到远端落地网关,由其在落地端用 lwIP 终结。保留 ICMP。 +// +// 当前为阶段三骨架:现有 tun.cc::handlePacket 的 IPIP 封装/直转路径已实现等价行为, +// 此处先建立类型占位,待 Router 接入热路径后再把 forwardL3 与现有路径收敛。 +class MeshOutbound : public Outbound { +public: + std::string name() const override { return "mesh"; } + bool needsTermination() const override { return false; } + // L4 拨号对 mesh 无意义:mesh 不在发起端终结,dialTcp/dialUdp 保持默认返回 -1。 +}; + +} // namespace candy + +#endif diff --git a/candy/src/netstack/router.cc b/candy/src/netstack/router.cc new file mode 100644 index 00000000..e7ba6aba --- /dev/null +++ b/candy/src/netstack/router.cc @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MIT +#include "netstack/router.h" + +namespace candy { + +Router::Router() : defaultOutbound("direct") {} + +void Router::addRule(const Rule &rule) { + this->rules.push_back(rule); +} + +void Router::setDefault(const std::string &outbound) { + this->defaultOutbound = outbound; +} + +std::string Router::match(const FlowKey &flow) const { + for (const auto &rule : this->rules) { + switch (rule.type) { + case MatchType::DstCidr: + // dst & mask == cidr & mask 即视为命中网段。 + if ((flow.dst & rule.mask) == (rule.cidr & rule.mask)) { + return rule.outbound; + } + break; + case MatchType::DstPort: + if (flow.dstPort == rule.port) { + return rule.outbound; + } + break; + case MatchType::Default: + return rule.outbound; + } + } + return this->defaultOutbound; +} + +} // namespace candy diff --git a/candy/src/netstack/router.h b/candy/src/netstack/router.h new file mode 100644 index 00000000..1fed4073 --- /dev/null +++ b/candy/src/netstack/router.h @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_ROUTER_H +#define CANDY_NETSTACK_ROUTER_H + +#include "core/net.h" +#include +#include +#include + +namespace candy { + +// Router:规则引擎(阶段三)。在 tun 入口、lwIP 终结之前,按目的网段/端口/兜底规则 +// 为一条流选择 outbound(mesh / direct / external-socks5)。 +// +// 匹配只依赖 L3 包头即可完成的字段(origSrc/origDst/proto/dstPort),与 03 文档 §5.2 +// 一致:分流发生在终结之前,只有命中需要本机终结的出口才进入 lwIP。 +// +// 当前为阶段三骨架:规则容器与 match 已就绪,但尚未接入 tun.cc 热路径,default 兜底 +// 返回 "direct",因此对现有 kernel/userspace 行为零影响。后续接入时由配置注入规则。 +class Router { +public: + // 匹配输入:从原始 IP 包头解析得到的 L3 信息(无需终结)。 + struct FlowKey { + IP4 src; + IP4 dst; + uint8_t proto; + uint16_t dstPort; + }; + + // 规则条件类型(按 03 文档 §5.1,自上而下优先匹配)。 + enum class MatchType { + DstCidr, // 目的网段:dst ∈ cidr + DstPort, // 目的端口 + Default, // 兜底 + }; + + struct Rule { + MatchType type; + // DstCidr 时生效:网段与掩码。 + IP4 cidr; + IP4 mask; + // DstPort 时生效。 + uint16_t port; + // 命中后选用的 outbound 名称(direct / mesh / socks5 / 组名)。 + std::string outbound; + }; + + Router(); + + // 追加一条规则(按追加顺序即为匹配优先级)。 + void addRule(const Rule &rule); + + // 设置兜底 outbound(无显式 Default 规则时使用)。 + void setDefault(const std::string &outbound); + + // 为一条流选择 outbound 名称;无任何规则命中时返回兜底(默认 "direct")。 + std::string match(const FlowKey &flow) const; + + bool empty() const { return this->rules.empty(); } + +private: + std::vector rules; + std::string defaultOutbound; +}; + +} // namespace candy + +#endif diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc index 68373cec..4d2813c1 100644 --- a/candy/src/netstack/session_tcp.cc +++ b/candy/src/netstack/session_tcp.cc @@ -46,24 +46,10 @@ int SessionTcp::start() { tcp_err(this->pcb, errTrampoline); #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - this->fd = (int)::socket(AF_INET, SOCK_STREAM, 0); + // 落地拨号交给 Outbound(阶段一/二为 DirectOutbound,内核 socket 直连)。 + // 返回已发起 connect(EINPROGRESS 视为成功)的非阻塞 fd,后续 splice/背压逻辑不变。 + this->fd = this->stack->getOutbound().dialTcp(Endpoint{this->origDst, this->origDstPort}); if (this->fd < 0) { - spdlog::warn("session tcp socket failed: {}", netErrStr(netLastError())); - return -1; - } - netSetNonBlocking(this->fd); - - struct sockaddr_in dst = {}; - dst.sin_family = AF_INET; - dst.sin_port = htons(this->origDstPort); - dst.sin_addr.s_addr = uint32_t(this->origDst); - - int ret = ::connect(this->fd, (struct sockaddr *)&dst, sizeof(dst)); - if (ret != 0 && !netInProgress(netLastError())) { - spdlog::warn("session tcp connect {}:{} failed: {}", this->origDst.toString(), this->origDstPort, - netErrStr(netLastError())); - netClose(this->fd); - this->fd = -1; return -1; } diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index ce15db87..733023e8 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -39,24 +39,10 @@ int SessionUdp::start() { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) this->self = shared_from_this(); - this->fd = (int)::socket(AF_INET, SOCK_DGRAM, 0); + // 落地拨号交给 Outbound(阶段一/二为 DirectOutbound)。connect 固定对端, + // 内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE),形成 Fullcone NAT 伪会话。 + this->fd = this->stack->getOutbound().dialUdp(Endpoint{this->origDst, this->origDstPort}); if (this->fd < 0) { - spdlog::warn("session udp socket failed: {}", netErrStr(netLastError())); - return -1; - } - netSetNonBlocking(this->fd); - - // connect 固定对端:内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE), - // 且后续 send/recv 只与该对端往来,形成 Fullcone NAT 的伪会话。 - struct sockaddr_in dst = {}; - dst.sin_family = AF_INET; - dst.sin_port = htons(this->origDstPort); - dst.sin_addr.s_addr = uint32_t(this->origDst); - if (::connect(this->fd, (struct sockaddr *)&dst, sizeof(dst)) != 0) { - spdlog::warn("session udp connect {}:{} failed: {}", this->origDst.toString(), this->origDstPort, - netErrStr(netLastError())); - netClose(this->fd); - this->fd = -1; return -1; } diff --git a/candy/src/netstack/socks5_client.cc b/candy/src/netstack/socks5_client.cc new file mode 100644 index 00000000..2feef2cb --- /dev/null +++ b/candy/src/netstack/socks5_client.cc @@ -0,0 +1,240 @@ +// SPDX-License-Identifier: MIT +#include "netstack/socks5_client.h" +#include + +namespace candy { + +void Socks5Client::startConnectIPv4(IP4 dstHost, uint16_t dstPort, const std::string &username, + const std::string &password) { + this->useDomain = false; + this->dstHost = dstHost; + this->dstPort = dstPort; + this->username = username; + this->password = password; + this->useUserPass = !username.empty(); + buildAuthMethods(); + this->st = State::WriteAuthMethods; +} + +void Socks5Client::startConnectDomain(const std::string &domain, uint16_t dstPort, const std::string &username, + const std::string &password) { + this->useDomain = true; + this->dstDomain = domain; + this->dstPort = dstPort; + this->username = username; + this->password = password; + this->useUserPass = !username.empty(); + buildAuthMethods(); + this->st = State::WriteAuthMethods; +} + +void Socks5Client::buildAuthMethods() { + // VER(1) NMETHODS(1) METHODS(nmethods)。无认证只声明 NONE; + // 配了用户口令则同时声明 NONE 与 USERPASS,让服务端择优。 + this->outbound.clear(); + this->outbound.push_back(Socks5::VERSION); + if (this->useUserPass) { + this->outbound.push_back(0x02); + this->outbound.push_back(Socks5::METHOD_NONE); + this->outbound.push_back(Socks5::METHOD_USERPASS); + } else { + this->outbound.push_back(0x01); + this->outbound.push_back(Socks5::METHOD_NONE); + } +} + +void Socks5Client::buildAuthCreds() { + // RFC 1929:VER(1=0x01) ULEN(1) UNAME(ulen) PLEN(1) PASSWD(plen)。 + this->outbound.clear(); + this->outbound.push_back(Socks5::AUTH_VERSION); + this->outbound.push_back((uint8_t)this->username.size()); + this->outbound.insert(this->outbound.end(), this->username.begin(), this->username.end()); + this->outbound.push_back((uint8_t)this->password.size()); + this->outbound.insert(this->outbound.end(), this->password.begin(), this->password.end()); +} + +void Socks5Client::buildRequest() { + // VER(1) CMD(1) RSV(1=0) ATYP(1) DST.ADDR DST.PORT。 + this->outbound.clear(); + this->outbound.push_back(Socks5::VERSION); + this->outbound.push_back(Socks5::CMD_CONNECT); + this->outbound.push_back(0x00); + if (this->useDomain) { + this->outbound.push_back(Socks5::ATYP_DOMAIN); + this->outbound.push_back((uint8_t)this->dstDomain.size()); + this->outbound.insert(this->outbound.end(), this->dstDomain.begin(), this->dstDomain.end()); + } else { + this->outbound.push_back(Socks5::ATYP_IPV4); + // IP4 的内存字节即网络序的 a.b.c.d,直接拷 4 字节即为 socks5 所需顺序。 + uint32_t v = uint32_t(this->dstHost); + const uint8_t *p = reinterpret_cast(&v); + this->outbound.insert(this->outbound.end(), p, p + 4); + } + // 端口为大端(网络序)。 + uint16_t portBE = hton(this->dstPort); + const uint8_t *pp = reinterpret_cast(&portBE); + this->outbound.insert(this->outbound.end(), pp, pp + 2); +} + +bool Socks5Client::feed(const uint8_t *data, size_t len) { + if (this->st == State::Failed) { + return false; + } + if (this->st == State::Done) { + // 握手已完成,多余字节归入 leftover。 + this->leftover.insert(this->leftover.end(), data, data + len); + return true; + } + this->inbound.insert(this->inbound.end(), data, data + len); + + // 循环推进:可能一次 feed 跨越多个状态(例如认证应答与请求应答粘包)。 + for (;;) { + switch (this->st) { + case State::ReadAuthMethod: { + // 应答固定 2 字节:VER, METHOD。 + if (this->inbound.size() < 2) { + return true; + } + uint8_t ver = this->inbound[0]; + uint8_t method = this->inbound[1]; + this->inbound.erase(this->inbound.begin(), this->inbound.begin() + 2); + if (ver != Socks5::VERSION) { + fail("bad socks version in auth method reply"); + return false; + } + if (method == Socks5::METHOD_NONE) { + buildRequest(); + this->st = State::WriteRequest; + } else if (method == Socks5::METHOD_USERPASS) { + if (this->username.empty()) { + fail("server requires userpass but no credentials configured"); + return false; + } + buildAuthCreds(); + this->st = State::WriteAuthCreds; + } else { + fail("server rejected all auth methods"); + return false; + } + // 进入 Write* 状态后没有更多可解析的入站数据,交回调用方发送。 + return true; + } + case State::ReadAuthCreds: { + // RFC 1929 应答固定 2 字节:VER, STATUS(0=成功)。 + if (this->inbound.size() < 2) { + return true; + } + uint8_t status = this->inbound[1]; + this->inbound.erase(this->inbound.begin(), this->inbound.begin() + 2); + if (status != 0x00) { + fail("socks5 userpass auth failed"); + return false; + } + buildRequest(); + this->st = State::WriteRequest; + return true; + } + case State::ReadResponse: { + // VER(1) REP(1) RSV(1) ATYP(1) BND.ADDR BND.PORT。先读前 4 字节定长头。 + if (this->inbound.size() < 4) { + return true; + } + uint8_t ver = this->inbound[0]; + uint8_t rep = this->inbound[1]; + uint8_t atyp = this->inbound[3]; + if (ver != Socks5::VERSION) { + fail("bad socks version in connect reply"); + return false; + } + if (rep != Socks5::REP_SUCCESS) { + fail("socks5 connect rejected, rep=" + std::to_string(rep)); + return false; + } + // 计算绑定地址长度,确认应答完整收齐。 + size_t addrLen = 0; + switch (atyp) { + case Socks5::ATYP_IPV4: + addrLen = 4; + break; + case Socks5::ATYP_IPV6: + addrLen = 16; + break; + case Socks5::ATYP_DOMAIN: + if (this->inbound.size() < 5) { + return true; // 还没收到域名长度字节 + } + addrLen = 1 + this->inbound[4]; + break; + default: + fail("unknown atyp in connect reply"); + return false; + } + size_t total = 4 + addrLen + 2; // 头 + 地址 + 端口 + if (this->inbound.size() < total) { + return true; // 等待剩余字节 + } + // 应答收齐,多余的即为业务数据。 + this->leftover.assign(this->inbound.begin() + total, this->inbound.end()); + this->inbound.clear(); + this->st = State::Done; + return true; + } + default: + // Write* 状态:等待调用方把 outbound 发出去后再调用 advanceAfterWrite()。 + return true; + } + } +} + +std::vector Socks5Client::takeOutbound() { + std::vector out; + out.swap(this->outbound); + // 发送完成意味着对应的 Write 状态结束,迁移到等待应答的 Read 状态。 + switch (this->st) { + case State::WriteAuthMethods: + this->st = State::ReadAuthMethod; + break; + case State::WriteAuthCreds: + this->st = State::ReadAuthCreds; + break; + case State::WriteRequest: + this->st = State::ReadResponse; + break; + default: + break; + } + return out; +} + +bool Socks5Client::hasOutbound() const { + return !this->outbound.empty(); +} + +std::vector Socks5Client::takeLeftover() { + std::vector out; + out.swap(this->leftover); + return out; +} + +Socks5Client::State Socks5Client::state() const { + return this->st; +} + +bool Socks5Client::done() const { + return this->st == State::Done; +} + +bool Socks5Client::failed() const { + return this->st == State::Failed; +} + +const std::string &Socks5Client::error() const { + return this->errMsg; +} + +void Socks5Client::fail(const std::string &reason) { + this->errMsg = reason; + this->st = State::Failed; +} + +} // namespace candy diff --git a/candy/src/netstack/socks5_client.h b/candy/src/netstack/socks5_client.h new file mode 100644 index 00000000..f4effdfb --- /dev/null +++ b/candy/src/netstack/socks5_client.h @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_SOCKS5_CLIENT_H +#define CANDY_NETSTACK_SOCKS5_CLIENT_H + +#include "core/net.h" +#include +#include +#include + +namespace candy { + +// socks5 协议常量(RFC 1928 / 1929,取值与 hev-socks5-proto.h 对齐)。 +namespace Socks5 { +constexpr uint8_t VERSION = 0x05; // socks 版本号 +constexpr uint8_t AUTH_VERSION = 0x01; // user/pass 认证子协商版本(RFC 1929) +constexpr uint8_t METHOD_NONE = 0x00; // 无认证 +constexpr uint8_t METHOD_USERPASS = 0x02; // 用户名口令认证 +constexpr uint8_t METHOD_NOACCEPT = 0xFF; // 服务端拒绝所有方法 +constexpr uint8_t CMD_CONNECT = 0x01; // CONNECT 请求 +constexpr uint8_t CMD_UDP_ASSOCIATE = 0x03;// UDP ASSOCIATE 请求 +constexpr uint8_t ATYP_IPV4 = 0x01; // 地址类型:IPv4 +constexpr uint8_t ATYP_DOMAIN = 0x03; // 地址类型:域名 +constexpr uint8_t ATYP_IPV6 = 0x04; // 地址类型:IPv6 +constexpr uint8_t REP_SUCCESS = 0x00; // 应答:成功 +} // namespace Socks5 + +// Socks5Client:非阻塞 reactor 模型下的 socks5 CONNECT 握手增量状态机。 +// +// 设计要点(适配 candy 的 reactor,区别于 hev 的协程阻塞 IO): +// - 本类不持有 fd、不直接收发;只做"协议字节"的生成与解析。 +// - 调用方在 socket 可写时取 takeOutbound() 的待发送字节并 send; +// 在 socket 可读时把收到的字节 feed() 进来推进状态机。 +// - 每次推进后通过 state() 查询:是否还要发/收、是否完成(Done)、是否失败(Failed)。 +// - 完成后,余下未消费的入站字节(握手应答之后紧跟的业务数据,极少见但协议允许) +// 可由 takeLeftover() 取走,交给后续 splice。 +// +// 握手时序(标准 CONNECT,参照 hev-socks5-client.c handshake_standard): +// WriteAuthMethods -> ReadAuthMethod +// -> [若选中 USERPASS] WriteAuthCreds -> ReadAuthCreds +// -> WriteRequest -> ReadResponse -> Done +class Socks5Client { +public: + enum class State { + Init, // 未开始 + WriteAuthMethods, // 待发送:版本+认证方法列表 + ReadAuthMethod, // 待接收:服务端选定的认证方法 + WriteAuthCreds, // 待发送:用户名/口令 + ReadAuthCreds, // 待接收:认证结果 + WriteRequest, // 待发送:CONNECT 请求 + ReadResponse, // 待接收:CONNECT 应答 + Done, // 握手成功 + Failed, // 握手失败 + }; + + // 目标地址用 IPv4 直发(ATYP=IPv4)。creds 为空表示仅声明无认证方法。 + void startConnectIPv4(IP4 dstHost, uint16_t dstPort, const std::string &username = "", + const std::string &password = ""); + + // 目标地址用域名直发(ATYP=DOMAIN),由 socks5 服务端解析。 + void startConnectDomain(const std::string &domain, uint16_t dstPort, const std::string &username = "", + const std::string &password = ""); + + // 喂入从 socket 收到的字节,推进状态机。返回 false 表示握手失败(state()==Failed)。 + bool feed(const uint8_t *data, size_t len); + + // 取走当前待发送字节(取走后内部清空)。调用方负责把它写到 socket; + // 若一次没写完,剩余部分应由调用方自行保留续发,本类不重复给出。 + std::vector takeOutbound(); + + // 是否还有待发送字节。 + bool hasOutbound() const; + + // 握手完成后,取走应答之后多余的入站业务数据(通常为空)。 + std::vector takeLeftover(); + + State state() const; + bool done() const; + bool failed() const; + const std::string &error() const; + +private: + void buildAuthMethods(); + void buildAuthCreds(); + void buildRequest(); + void fail(const std::string &reason); + + State st = State::Init; + bool useUserPass = false; + std::string username; + std::string password; + + // 目标地址(二选一:IPv4 或域名)。 + bool useDomain = false; + IP4 dstHost; + std::string dstDomain; + uint16_t dstPort = 0; + + std::vector outbound; // 待发送缓冲 + std::vector inbound; // 入站累积缓冲(用于跨次拼接定长/变长应答) + std::vector leftover; // 握手完成后多余的入站字节 + std::string errMsg; // 失败原因(仅在 Failed 时有效) +}; + +} // namespace candy + +#endif From da4dc57ea43df52f0348f1efe142bc591589b539 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sun, 28 Jun 2026 17:39:26 +0800 Subject: [PATCH 07/29] tun2socks work --- candy-cli/src/config.cc | 8 + candy-cli/src/config.h | 3 + candy/src/candy/client.cc | 2 + candy/src/core/client.cc | 10 + candy/src/core/client.h | 5 + candy/src/netstack/netstack.cc | 162 +++++++++++++++- candy/src/netstack/netstack.h | 20 +- candy/src/netstack/outbound.cc | 43 ++++- candy/src/netstack/outbound.h | 54 ++++++ candy/src/netstack/session_tcp.cc | 141 +++++++++++++- candy/src/netstack/session_tcp.h | 15 ++ candy/src/netstack/session_udp.cc | 289 +++++++++++++++++++++++++++- candy/src/netstack/session_udp.h | 24 +++ candy/src/netstack/socks5_client.cc | 110 ++++++++++- candy/src/netstack/socks5_client.h | 44 ++++- candy/src/netstack/tcp_handshake.h | 49 +++++ 16 files changed, 954 insertions(+), 25 deletions(-) create mode 100644 candy/src/netstack/tcp_handshake.h diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index a0899663..8db8db36 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -38,6 +38,8 @@ Poco::JSON::Object arguments::json() { config.set("vmac", virtualMac(this->name)); config.set("expt", loadTunAddress(this->name)); config.set("forward-mode", this->forwardMode); + config.set("socks5-upstream", this->socks5Upstream); + config.set("outbound-rules", this->outboundRules); } if (this->mode == "server") { @@ -127,6 +129,8 @@ int arguments::parse(int argc, char *argv[]) { .metavar(""); program.add_argument("--forward-mode").help("packet forward mode: kernel or userspace"); + program.add_argument("--socks5-upstream").help("external socks5 upstream: name=socks5://host:port;..."); + program.add_argument("--outbound-rules").help("outbound split rules: dst-cidr:.. => name;..."); program.add_group("Server options"); @@ -186,6 +190,8 @@ int arguments::parse(int argc, char *argv[]) { program.set_if_used("--discovery", this->discovery); program.set_if_used("--route", this->routeCost); program.set_if_used("--forward-mode", this->forwardMode); + program.set_if_used("--socks5-upstream", this->socks5Upstream); + program.set_if_used("--outbound-rules", this->outboundRules); bool needShowUsage = [&]() { if (this->mode != "client" && this->mode != "server") @@ -244,6 +250,8 @@ void arguments::parseFile(std::string cfgFile) { {"mtu", [&](const std::string &value) { this->mtu = std::stoi(value); }}, {"localhost", [&](const std::string &value) { this->localhost = value; }}, {"forward-mode", [&](const std::string &value) { this->forwardMode = value; }}, + {"socks5-upstream", [&](const std::string &value) { this->socks5Upstream = value; }}, + {"outbound-rules", [&](const std::string &value) { this->outboundRules = value; }}, }; auto trim = [](std::string str) { if (str.length() >= 2 && str.front() == '\"' && str.back() == '\"') { diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index f8f2772a..ae4be379 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -32,6 +32,9 @@ struct arguments { int routeCost = 0; int mtu = 1400; std::string forwardMode = "kernel"; + // 阶段三:外部 socks5 上游与分流规则(仅 userspace 模式有效,空表示全部 direct)。 + std::string socks5Upstream; + std::string outboundRules; }; int saveTunAddress(const std::string &name, const std::string &cidr); diff --git a/candy/src/candy/client.cc b/candy/src/candy/client.cc index 4143be2b..9f5c286e 100644 --- a/candy/src/candy/client.cc +++ b/candy/src/candy/client.cc @@ -100,6 +100,8 @@ bool run(const std::string &id, const Poco::JSON::Object &config) { client->setPort(config.getValue("port")); client->setLocalhost(config.getValue("localhost")); client->setForwardMode(config.optValue("forward-mode", "kernel")); + client->setSocks5Upstream(config.optValue("socks5-upstream", "")); + client->setOutboundRules(config.optValue("outbound-rules", "")); client->run(); } candy::logger().information(Poco::format("run exit: id=%s ", id)); diff --git a/candy/src/core/client.cc b/candy/src/core/client.cc index 8c41a781..22c55f68 100644 --- a/candy/src/core/client.cc +++ b/candy/src/core/client.cc @@ -124,10 +124,20 @@ void Client::setForwardMode(const std::string &mode) { this->forwardMode = mode; } +void Client::setSocks5Upstream(const std::string &upstream) { + this->socks5Upstream = upstream; +} + +void Client::setOutboundRules(const std::string &rules) { + this->outboundRules = rules; +} + void Client::run() { this->running.store(true); if (this->forwardMode == "userspace") { + // 阶段三:在 NetStack 启动前注入 socks5 上游与分流规则(空时全部走 direct,零回归)。 + netstack.configureOutbounds(this->socks5Upstream, this->outboundRules); if (netstack.run(this)) { return; } diff --git a/candy/src/core/client.h b/candy/src/core/client.h index 2d16326d..bfa1289b 100644 --- a/candy/src/core/client.h +++ b/candy/src/core/client.h @@ -44,6 +44,9 @@ class Client { void setVirtualMac(const std::string &vmac); void setForwardMode(const std::string &mode); + // 阶段三:外部 socks5 上游与分流规则(仅 userspace 模式生效,空表示全部 direct)。 + void setSocks5Upstream(const std::string &upstream); + void setOutboundRules(const std::string &rules); void run(); bool isRunning(); @@ -76,6 +79,8 @@ class Client { private: std::string tunName; std::string forwardMode = "kernel"; + std::string socks5Upstream; + std::string outboundRules; int mtu = 1400; }; diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index 8235a09b..3c0887c6 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "lwip/init.h" #include "lwip/pbuf.h" @@ -20,6 +22,153 @@ namespace candy { +namespace { + +// 去除字符串首尾空白(含 \r,兼容 CRLF 配置文件)。 +std::string trimWs(const std::string &s) { + size_t b = s.find_first_not_of(" \t\r\n"); + if (b == std::string::npos) { + return ""; + } + size_t e = s.find_last_not_of(" \t\r\n"); + return s.substr(b, e - b + 1); +} + +// 按分隔符切分并去空白,丢弃空片段。 +std::vector splitTrim(const std::string &s, char sep) { + std::vector out; + std::string cur; + std::istringstream iss(s); + while (std::getline(iss, cur, sep)) { + std::string t = trimWs(cur); + if (!t.empty()) { + out.push_back(t); + } + } + return out; +} + +// 解析单条 socks5 上游:"socks5://[user:pass@]host:port"。成功返回 true。 +bool parseSocks5Url(const std::string &url, Endpoint &server, std::string &user, std::string &pass) { + const std::string scheme = "socks5://"; + if (url.compare(0, scheme.size(), scheme) != 0) { + return false; + } + std::string rest = url.substr(scheme.size()); + // 可选 user:pass@ + auto at = rest.find('@'); + if (at != std::string::npos) { + std::string cred = rest.substr(0, at); + rest = rest.substr(at + 1); + auto colon = cred.find(':'); + if (colon == std::string::npos) { + return false; + } + user = cred.substr(0, colon); + pass = cred.substr(colon + 1); + } + // host:port + auto colon = rest.rfind(':'); + if (colon == std::string::npos) { + return false; + } + std::string host = rest.substr(0, colon); + std::string portStr = rest.substr(colon + 1); + int port = 0; + try { + port = std::stoi(portStr); + } catch (...) { + return false; + } + if (port <= 0 || port > 65535) { + return false; + } + server.host = IP4(host); + server.port = (uint16_t)port; + return true; +} + +} // namespace + +void NetStack::configureOutbounds(const std::string &socks5Upstream, const std::string &outboundRules) { + // 空配置:不建任何 socks5/规则,getOutbound 恒返回 direct,行为完全等价阶段一/二。 + if (trimWs(socks5Upstream).empty() && trimWs(outboundRules).empty()) { + return; + } + + // 1) 解析 socks5 上游:"name=socks5://[user:pass@]host:port;..." + for (const auto &item : splitTrim(socks5Upstream, ';')) { + auto eq = item.find('='); + if (eq == std::string::npos) { + spdlog::warn("netstack: bad socks5-upstream entry: {}", item); + continue; + } + std::string name = trimWs(item.substr(0, eq)); + std::string url = trimWs(item.substr(eq + 1)); + Endpoint server{}; + std::string user, pass; + if (name.empty() || !parseSocks5Url(url, server, user, pass)) { + spdlog::warn("netstack: bad socks5-upstream url: {}", item); + continue; + } + this->outbounds[name] = std::make_unique(server, user, pass); + spdlog::info("netstack: socks5 outbound '{}' -> {}:{}", name, server.host.toString(), server.port); + } + + // 2) 解析分流规则:"dst-cidr:CIDR => name; dst-port:PORT => name; default => name;" + // 自上而下即匹配优先级,与 03 文档 §5.1 一致。 + for (const auto &line : splitTrim(outboundRules, ';')) { + auto arrow = line.find("=>"); + if (arrow == std::string::npos) { + spdlog::warn("netstack: bad outbound-rule (no =>): {}", line); + continue; + } + std::string cond = trimWs(line.substr(0, arrow)); + std::string target = trimWs(line.substr(arrow + 2)); + if (target.empty()) { + spdlog::warn("netstack: bad outbound-rule (empty target): {}", line); + continue; + } + Router::Rule rule{}; + rule.outbound = target; + if (cond == "default") { + this->router.setDefault(target); + spdlog::info("netstack: rule default => {}", target); + continue; + } + const std::string cidrPfx = "dst-cidr:"; + const std::string portPfx = "dst-port:"; + if (cond.compare(0, cidrPfx.size(), cidrPfx) == 0) { + std::string cidr = trimWs(cond.substr(cidrPfx.size())); + Address addr; + if (addr.fromCidr(cidr)) { + spdlog::warn("netstack: bad dst-cidr: {}", cidr); + continue; + } + rule.type = Router::MatchType::DstCidr; + rule.cidr = addr.Host(); + rule.mask = addr.Mask(); + this->router.addRule(rule); + spdlog::info("netstack: rule dst-cidr:{} => {}", cidr, target); + } else if (cond.compare(0, portPfx.size(), portPfx) == 0) { + std::string portStr = trimWs(cond.substr(portPfx.size())); + int port = 0; + try { + port = std::stoi(portStr); + } catch (...) { + spdlog::warn("netstack: bad dst-port: {}", portStr); + continue; + } + rule.type = Router::MatchType::DstPort; + rule.port = (uint16_t)port; + this->router.addRule(rule); + spdlog::info("netstack: rule dst-port:{} => {}", port, target); + } else { + spdlog::warn("netstack: unsupported rule condition: {}", cond); + } + } +} + NetStack::NetStack() : client(nullptr), running(false), listenPcb(nullptr), udpListenPcb(nullptr) { std::memset(&this->lwipNetif, 0, sizeof(this->lwipNetif)); } @@ -37,7 +186,18 @@ Reactor &NetStack::getReactor() { return this->reactor; } -Outbound &NetStack::getOutbound() { +Outbound &NetStack::getOutbound(const Router::FlowKey &flow) { + // 无规则时 router.match 恒返回兜底 "direct",直接复用 directOutbound,零回归。 + const std::string name = this->router.match(flow); + if (name == "direct" || name.empty()) { + return this->directOutbound; + } + auto it = this->outbounds.find(name); + if (it != this->outbounds.end() && it->second) { + return *it->second; + } + // 命中了一个未注册的出站名(配置不一致),安全兜底回 direct,避免丢流。 + spdlog::warn("netstack: outbound '{}' not found, fallback to direct", name); return this->directOutbound; } diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index 5d6b2a41..85afc304 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -5,6 +5,7 @@ #include "core/net.h" #include "netstack/outbound.h" #include "netstack/reactor.h" +#include "netstack/router.h" #include #include #include @@ -41,14 +42,19 @@ class NetStack { int wait(); void shutdown(); + // 阶段三:在 run() 之前注入 socks5 上游与分流规则(解析配置串,构建 Router + 出站注册表)。 + // 入参为空时不建任何规则/socks5,getOutbound 恒返回 direct,行为与阶段一/二完全一致。 + void configureOutbounds(const std::string &socks5Upstream, const std::string &outboundRules); + // 把待终结的 IP 包投递给 NetStack(来自 tun 落地分支,含 IPIP 外层)。 void input(std::string packet); // 供 Session 使用:在 NetStack 线程内执行任务(保证 lwIP API 线程安全)。 void postToStack(std::function task); Reactor &getReactor(); - // 当前出站:默认 DirectOutbound(内核 socket 直连落地)。Session 经此发起落地拨号。 - Outbound &getOutbound(); + // 按流选择出站:Router.match(flow) 命中的 outbound(direct / socks5)。 + // 无规则或命中 direct 时返回 DirectOutbound(与阶段一/二行为完全一致)。 + Outbound &getOutbound(const Router::FlowKey &flow); // UDP 回包注入用:拿到本模块的 lwIP netif(仅 NetStack 线程使用)。 struct netif &getNetif(); @@ -103,9 +109,15 @@ class NetStack { Reactor reactor; - // 落地出站:阶段一/二恒为 DirectOutbound(内核 socket 直连)。阶段三引入 Router - // 后按流选择,此处先固定 direct,保证现有 kernel/userspace 行为不变。 + // 落地出站:DirectOutbound(内核 socket 直连)恒在;阶段三再按 Router 规则选 socks5。 + // 无规则时 getOutbound 恒返回 directOutbound,保证现有 kernel/userspace 行为不变。 DirectOutbound directOutbound; + // 阶段三:分流规则引擎(空规则时 match 恒返回 "direct")。 + Router router; + // 命名出站注册表:name -> Socks5Outbound(由 configureOutbounds 从配置构建)。 + // direct 不入表(直接用 directOutbound)。仅在 run() 前 configureOutbounds 中写入, + // 之后只读,故无需加锁。 + std::unordered_map> outbounds; // NetStack 线程任务队列(用带超时读驱动定时器) std::mutex stackTaskMutex; diff --git a/candy/src/netstack/outbound.cc b/candy/src/netstack/outbound.cc index d35091c4..7e2a7461 100644 --- a/candy/src/netstack/outbound.cc +++ b/candy/src/netstack/outbound.cc @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT #include "netstack/outbound.h" #include "netstack/sockcompat.h" +#include "netstack/socks5_client.h" #include #if defined(__linux__) || defined(__APPLE__) @@ -12,11 +13,15 @@ namespace candy { -int DirectOutbound::dialTcp(const Endpoint &dst) { +namespace { +// 共享的 TCP 非阻塞拨号:建 socket + 非阻塞 + 发起 connect 到 dst。 +// EINPROGRESS 视为成功,返回已发起 connect 的 fd;失败返回 -1。 +// direct 与 socks5 都用它(区别仅在 dst 是业务目的还是 socks5 server)。 +int dialTcpTo(const Endpoint &dst, const char *tag) { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) int fd = (int)::socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { - spdlog::warn("direct outbound tcp socket failed: {}", netErrStr(netLastError())); + spdlog::warn("{} tcp socket failed: {}", tag, netErrStr(netLastError())); return -1; } netSetNonBlocking(fd); @@ -28,8 +33,7 @@ int DirectOutbound::dialTcp(const Endpoint &dst) { int ret = ::connect(fd, (struct sockaddr *)&addr, sizeof(addr)); if (ret != 0 && !netInProgress(netLastError())) { - spdlog::warn("direct outbound tcp connect {}:{} failed: {}", dst.host.toString(), dst.port, - netErrStr(netLastError())); + spdlog::warn("{} tcp connect {}:{} failed: {}", tag, dst.host.toString(), dst.port, netErrStr(netLastError())); netClose(fd); return -1; } @@ -38,6 +42,11 @@ int DirectOutbound::dialTcp(const Endpoint &dst) { return -1; #endif } +} // namespace + +int DirectOutbound::dialTcp(const Endpoint &dst) { + return dialTcpTo(dst, "direct outbound"); +} int DirectOutbound::dialUdp(const Endpoint &dst) { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) @@ -66,4 +75,30 @@ int DirectOutbound::dialUdp(const Endpoint &dst) { #endif } +Socks5Outbound::Socks5Outbound(Endpoint server, std::string username, std::string password) + : server(server), username(std::move(username)), password(std::move(password)) {} + +int Socks5Outbound::dialTcp(const Endpoint &dst) { + // 注意:socks5 拨号连接的是 server,而非业务目的 dst(dst 留给握手阶段 CONNECT)。 + (void)dst; + return dialTcpTo(this->server, "socks5 outbound"); +} + +std::unique_ptr Socks5Outbound::makeTcpHandshake(const Endpoint &dst) { + auto hs = std::make_unique(); + hs->startConnectIPv4(dst.host, dst.port, this->username, this->password); + return hs; +} + +int Socks5Outbound::dialUdpControl() { + // UDP ASSOCIATE 的控制连接:仍是到 socks5 server 的 TCP 连接(与 dialTcp 同目标)。 + return dialTcpTo(this->server, "socks5 udp control"); +} + +std::unique_ptr Socks5Outbound::makeUdpAssociate() { + auto hs = std::make_unique(); + hs->startUdpAssociate(this->username, this->password); + return hs; +} + } // namespace candy diff --git a/candy/src/netstack/outbound.h b/candy/src/netstack/outbound.h index faa3ef16..13edd05d 100644 --- a/candy/src/netstack/outbound.h +++ b/candy/src/netstack/outbound.h @@ -3,7 +3,9 @@ #define CANDY_NETSTACK_OUTBOUND_H #include "core/net.h" +#include "netstack/tcp_handshake.h" #include +#include #include namespace candy { @@ -41,6 +43,28 @@ class Outbound { // L4 终结型出站:为一条已终结的 UDP 流建立 connect 到目的的非阻塞 fd,失败返回 -1。 // 默认不支持。 virtual int dialUdp(const Endpoint &dst) { return -1; } + + // 为本出站的一条 TCP 流创建"连接建立后、splice 之前"的应用层握手器。 + // direct/mesh 无需握手,返回 nullptr(Session 据此走原有直连 splice 逻辑,字节不变); + // socks5 返回一个已按目的地址初始化的 Socks5Client。 + // dst:业务真实目的(socks5 CONNECT 的目标,而非 socks5 server 地址)。 + virtual std::unique_ptr makeTcpHandshake(const Endpoint &dst) { return nullptr; } + + // UDP 是否需要先在 TCP 控制连接上做 UDP ASSOCIATE 关联:direct 为 false(内核 socket + // 直连即可);socks5 为 true(需先建控制连接握手、拿到中继端点,再经中继收发封装报文)。 + virtual bool udpNeedsAssociation() const { return false; } + + // socks5 UDP 关联用:拨号到 socks5 server 的 TCP 控制连接(与 dialTcp 同一目标 server, + // 但语义独立)。返回已发起 connect 的非阻塞 fd,失败返回 -1。默认不支持。 + virtual int dialUdpControl() { return -1; } + + // socks5 UDP 关联用:创建在控制连接上跑 UDP ASSOCIATE 命令的握手器(complete 后由 + // udpRelayHost()/udpRelayPort() 取中继端点)。默认返回 nullptr。 + virtual std::unique_ptr makeUdpAssociate() { return nullptr; } + + // socks5 UDP 关联用:代理服务端端点。当 UDP ASSOCIATE 应答的中继地址为 0.0.0.0 时, + // 按 RFC 1928 §7 回退到控制连接的服务端 IP。非 socks5 出站返回 0.0.0.0:0(不使用)。 + virtual Endpoint proxyServerEndpoint() const { return Endpoint{IP4("0.0.0.0"), 0}; } }; // DirectOutbound:内核 socket 直连落地(阶段一/二的拨号逻辑迁移至此)。 @@ -66,6 +90,36 @@ class MeshOutbound : public Outbound { // L4 拨号对 mesh 无意义:mesh 不在发起端终结,dialTcp/dialUdp 保持默认返回 -1。 }; +// Socks5Outbound:外部 socks5 代理落地。dialTcp 连接的是 socks5 server(而非业务目的), +// 连上后由 makeTcpHandshake 返回的 Socks5Client 在通道上完成 CONNECT 握手, +// 之后 Session 把 lwIP pcb <-> socks5 连接做双向 splice。needsTermination()==true。 +// 协议限制:不支持 ICMP;UDP(UDP ASSOCIATE)后续单独实现,此处先支持 TCP。 +class Socks5Outbound : public Outbound { +public: + // server:socks5 服务端地址;username/password 为空表示无认证。 + Socks5Outbound(Endpoint server, std::string username = "", std::string password = ""); + + std::string name() const override { return "socks5"; } + bool needsTermination() const override { return true; } + + // 连接 socks5 server(而非业务目的 dst)。 + int dialTcp(const Endpoint &dst) override; + + // 返回针对业务目的 dst 的 socks5 CONNECT 握手器。 + std::unique_ptr makeTcpHandshake(const Endpoint &dst) override; + + // UDP 走 UDP ASSOCIATE:需要先建 TCP 控制连接并握手拿到中继端点。 + bool udpNeedsAssociation() const override { return true; } + int dialUdpControl() override; + std::unique_ptr makeUdpAssociate() override; + Endpoint proxyServerEndpoint() const override { return this->server; } + +private: + Endpoint server; + std::string username; + std::string password; +}; + } // namespace candy #endif diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc index 4d2813c1..5f6e4f0c 100644 --- a/candy/src/netstack/session_tcp.cc +++ b/candy/src/netstack/session_tcp.cc @@ -46,13 +46,22 @@ int SessionTcp::start() { tcp_err(this->pcb, errTrampoline); #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - // 落地拨号交给 Outbound(阶段一/二为 DirectOutbound,内核 socket 直连)。 + // 按流分流:Router.match(origSrc/origDst/proto/dstPort) 选出 outbound(direct/socks5)。 + // 无规则时恒为 DirectOutbound,与阶段一/二行为完全一致(零回归)。 + Router::FlowKey flow{this->origSrc, this->origDst, IPPROTO_TCP, this->origDstPort}; + Outbound &outbound = this->stack->getOutbound(flow); + + // 落地拨号交给选中的 Outbound(direct: 连目的;socks5: 连 server)。 // 返回已发起 connect(EINPROGRESS 视为成功)的非阻塞 fd,后续 splice/背压逻辑不变。 - this->fd = this->stack->getOutbound().dialTcp(Endpoint{this->origDst, this->origDstPort}); + this->fd = outbound.dialTcp(Endpoint{this->origDst, this->origDstPort}); if (this->fd < 0) { return -1; } + // 询问 outbound 是否需要应用层握手(socks5 返回 Socks5Client;direct 返回 nullptr)。 + // 为空时后续走原有直连 splice 路径,字节完全不变(direct 零回归的关键)。 + this->handshake = outbound.makeTcpHandshake(Endpoint{this->origDst, this->origDstPort}); + spdlog::debug("session tcp: {} -> {}:{}", this->origSrc.toString(), this->origDst.toString(), this->origDstPort); auto holder = shared_from_this(); @@ -298,6 +307,21 @@ void SessionTcp::onFdEvent(uint32_t events) { } // 已连接:优先把可读数据/EOF 读干净(read 返回 0 走优雅排空,返回 -1 走关闭), // 避免 ERROR/HUP 与可读数据同时到达时抢先关闭而丢失尾部数据。 + // 握手阶段(handshake 非空):事件分流到握手处理,握手完成后置空,转入下方 splice。 + if (this->handshake) { + if (ev & ReactorEvent::ERROR) { + closeFromReactor(); + return; + } + if (ev & ReactorEvent::READ) { + onHandshakeReadable(); + } + // onHandshakeReadable 内可能完成握手并置空 handshake;若仍在握手则处理可写。 + if (this->handshake && (ev & ReactorEvent::WRITE)) { + onHandshakeWritable(); + } + return; + } if (ev & (ReactorEvent::READ | ReactorEvent::ERROR)) { onFdReadable(); } @@ -322,6 +346,113 @@ void SessionTcp::onConnected() { return; } this->connected = true; + // socks5 等需要握手:先进入握手阶段(在落地通道上完成 CONNECT),完成后再 splice。 + // direct 无握手(handshake==nullptr),走下方原有 splice 路径,行为字节不变。 + if (this->handshake) { + spdlog::debug("session tcp handshake begin {} -> {}:{}", this->origSrc.toString(), this->origDst.toString(), + this->origDstPort); + // 注册读写:先把握手首包(auth methods)写出,并等待服务端应答。 + this->stack->getReactor().mod(this->fd, ReactorEvent::READ | ReactorEvent::WRITE); + flushHandshakeOutbound(); + return; + } + this->stack->getReactor().mod(this->fd, ReactorEvent::READ); + flushForwardLocked(); +#endif +} + +// ===================== 握手阶段(Reactor 线程) ===================== + +void SessionTcp::flushHandshakeOutbound() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + if (!this->handshake || this->fd < 0) { + return; + } + // 先把握手器新产生的待发字节拼到残留 outbox 后面(保证发送顺序)。 + if (this->handshake->hasOutbound()) { + std::vector out = this->handshake->takeOutbound(); + this->handshakeOutbox.append(reinterpret_cast(out.data()), out.size()); + } + // 非阻塞写出 outbox;部分写则保留剩余,注册 WRITE 等待下次可写。 + while (!this->handshakeOutbox.empty()) { + long n = netSend(this->fd, this->handshakeOutbox.data(), this->handshakeOutbox.size()); + if (n > 0) { + this->handshakeOutbox.erase(0, n); + continue; + } + if (n < 0 && netWouldBlock(netLastError())) { + this->stack->getReactor().mod(this->fd, ReactorEvent::READ | ReactorEvent::WRITE); + return; + } + spdlog::warn("session tcp handshake send failed: {}", netErrStr(netLastError())); + closeFromReactor(); + return; + } + // outbox 已排空:只需等待服务端应答,去掉 WRITE 关注。 + this->stack->getReactor().mod(this->fd, ReactorEvent::READ); +#endif +} + +void SessionTcp::onHandshakeWritable() { + flushHandshakeOutbound(); +} + +void SessionTcp::onHandshakeReadable() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + if (!this->handshake) { + return; + } + char buf[4096]; + while (true) { + long n = netRecv(this->fd, buf, sizeof(buf)); + if (n > 0) { + if (!this->handshake->feed(reinterpret_cast(buf), (size_t)n)) { + spdlog::warn("session tcp socks5 handshake failed: {}", this->handshake->error()); + closeFromReactor(); + return; + } + if (this->handshake->done()) { + finishHandshake(); + return; + } + // 未完成:握手器可能产生了下一段待发字节(如收到认证应答后要发 CONNECT 请求)。 + if (this->handshake->hasOutbound()) { + flushHandshakeOutbound(); + } + continue; + } + if (n == 0) { + spdlog::warn("session tcp socks5 server closed during handshake"); + closeFromReactor(); + return; + } + if (netWouldBlock(netLastError())) { + return; + } + spdlog::warn("session tcp handshake recv failed: {}", netErrStr(netLastError())); + closeFromReactor(); + return; + } +#endif +} + +void SessionTcp::finishHandshake() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + // 握手成功:取走应答后粘连的业务数据(若有),当作 fd 先收到的反向数据灌给 lwIP, + // 避免丢失。然后销毁握手器,转入与 direct 完全一致的 splice 路径。 + std::vector leftover = this->handshake->takeLeftover(); + spdlog::debug("session tcp socks5 handshake done {} -> {}:{} leftover={}", this->origSrc.toString(), + this->origDst.toString(), this->origDstPort, leftover.size()); + this->handshake.reset(); + + if (!leftover.empty()) { + this->backwardBytes.fetch_add(leftover.size()); + std::string data(reinterpret_cast(leftover.data()), leftover.size()); + auto holder = shared_from_this(); + this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->writeToLwip(data); }); + } + // 进入正常 splice:只关注可读(与 onConnected 的 direct 分支一致),并把握手期间 + // lwIP 侧已缓冲的待发数据(forwardBuf)灌向落地 fd。 this->stack->getReactor().mod(this->fd, ReactorEvent::READ); flushForwardLocked(); #endif @@ -332,6 +463,12 @@ void SessionTcp::flushForwardLocked() { if (this->fd < 0 || !this->connected) { return; } + // 握手未完成时严禁向落地 fd 写业务数据:此时 fd 上跑的是 socks5 握手字节流, + // 若把 lwIP 侧已到达的业务数据(如 HTTP 请求)插进去会破坏协议。业务数据先在 + // forwardBuf 里缓冲,待 finishHandshake() 完成后再由其调用本函数统一灌出。 + if (this->handshake) { + return; + } size_t acked = 0; size_t remain = 0; bool blocked = false; diff --git a/candy/src/netstack/session_tcp.h b/candy/src/netstack/session_tcp.h index 50171960..4060fbd3 100644 --- a/candy/src/netstack/session_tcp.h +++ b/candy/src/netstack/session_tcp.h @@ -4,6 +4,7 @@ #include "core/net.h" #include "netstack/session.h" +#include "netstack/tcp_handshake.h" #include #include #include @@ -58,6 +59,13 @@ class SessionTcp : public Session, public std::enable_shared_from_thisdone() 后置空并进入 splice。 + // 仅 Reactor 线程访问。 + std::unique_ptr handshake; + // 握手待发字节中尚未写完的残留(非阻塞 write 部分写时保留续发)。仅 Reactor 线程访问。 + std::string handshakeOutbox; + IP4 origDst; uint16_t origDstPort; IP4 origSrc; diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index 733023e8..75c59862 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -2,6 +2,7 @@ #include "netstack/session_udp.h" #include "netstack/netstack.h" #include "netstack/sockcompat.h" +#include "netstack/socks5_client.h" #include #include @@ -33,15 +34,55 @@ SessionUdp::~SessionUdp() { netClose(this->fd); this->fd = -1; } + if (this->ctrlFd >= 0) { + netClose(this->ctrlFd); + this->ctrlFd = -1; + } } int SessionUdp::start() { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) this->self = shared_from_this(); - // 落地拨号交给 Outbound(阶段一/二为 DirectOutbound)。connect 固定对端, - // 内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE),形成 Fullcone NAT 伪会话。 - this->fd = this->stack->getOutbound().dialUdp(Endpoint{this->origDst, this->origDstPort}); + // 按流分流:Router.match 选出 outbound(direct/socks5)。 + // 无规则时恒为 DirectOutbound,与阶段一/二行为完全一致(零回归)。 + Router::FlowKey flow{this->origSrc, this->origDst, IPPROTO_UDP, this->origDstPort}; + Outbound &outbound = this->stack->getOutbound(flow); + + if (outbound.udpNeedsAssociation()) { + // ---- socks5 UDP ASSOCIATE 分支 ---- + // 先建到 socks5 server 的 TCP 控制连接(保活),connect 完成后在其上跑 UDP + // ASSOCIATE 握手;拿到中继端点再建中继 UDP socket。期间到达的数据报暂存。 + this->useSocks5 = true; + Endpoint ps = outbound.proxyServerEndpoint(); + this->proxyServerHost = ps.host; + + this->ctrlFd = outbound.dialUdpControl(); + if (this->ctrlFd < 0) { + return -1; + } + this->associate = outbound.makeUdpAssociate(); + if (!this->associate) { + return -1; + } + + spdlog::debug("session udp(socks5): {}:{} -> {}:{} via {}:{}", this->origSrc.toString(), this->origSrcPort, + this->origDst.toString(), this->origDstPort, ps.host.toString(), ps.port); + + auto holder = shared_from_this(); + int ctrlFd = this->ctrlFd; + // 控制连接非阻塞 connect:先等可写(connect 完成)。 + this->stack->getReactor().add(ctrlFd, ReactorEvent::WRITE, + [holder](ReactorEvent ev) { holder->onCtrlEvent((uint32_t)ev); }); + // 注册每流 recv:握手未完成时数据报会被暂存,完成后冲刷到中继 fd。 + udp_recv(this->pcb, recvTrampoline, this); + return 0; + } + + // ---- direct 分支(阶段一/二原逻辑,零回归)---- + // 落地拨号交给选中的 Outbound(DirectOutbound: connect 固定对端, + // 内核自动填源地址 = 网关 LAN IP,等价 MASQUERADE,形成 Fullcone NAT 伪会话)。 + this->fd = outbound.dialUdp(Endpoint{this->origDst, this->origDstPort}); if (this->fd < 0) { return -1; } @@ -87,7 +128,30 @@ void SessionUdp::sendToLanding(std::string data) { auto holder = shared_from_this(); this->stack->getReactor().post([holder, data = std::move(data)]() mutable { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - if (holder->fd < 0 || holder->closing.load()) { + if (holder->closing.load()) { + return; + } + if (holder->useSocks5) { + // socks5 中继:每个业务数据报封装 socks5 UDP 头(目的=origDst:origDstPort)。 + std::string pkt = socks5UdpEncap(holder->origDst, holder->origDstPort, + reinterpret_cast(data.data()), data.size()); + if (!holder->relayReady) { + // 握手尚未完成:暂存(带 2 字节长度前缀串联),完成后由 finishAssociate 冲刷。 + uint16_t n = (uint16_t)pkt.size(); + holder->pendingDatagrams.append(reinterpret_cast(&n), sizeof(n)); + holder->pendingDatagrams.append(pkt); + return; + } + if (holder->fd < 0) { + return; + } + long n = netSend(holder->fd, pkt.data(), pkt.size()); + if (n < 0 && !netWouldBlock(netLastError())) { + spdlog::debug("session udp(socks5) send failed: {}", netErrStr(netLastError())); + } + return; + } + if (holder->fd < 0) { return; } long n = netSend(holder->fd, data.data(), data.size()); @@ -154,6 +218,11 @@ void SessionUdp::shutdownFromStack() { netClose(holder->fd); holder->fd = -1; } + if (holder->ctrlFd >= 0) { + holder->stack->getReactor().del(holder->ctrlFd); + netClose(holder->ctrlFd); + holder->ctrlFd = -1; + } #endif holder->self.reset(); }); @@ -179,6 +248,22 @@ void SessionUdp::onFdReadable() { while (true) { long n = netRecv(this->fd, buf, sizeof(buf)); if (n > 0) { + if (this->useSocks5) { + // socks5 中继回包:去掉 socks5 UDP 头,仅把业务数据灌回 lwIP。 + IP4 srcHost; + uint16_t srcPort = 0; + const uint8_t *payload = nullptr; + size_t payloadLen = 0; + if (!socks5UdpDecap(reinterpret_cast(buf), (size_t)n, srcHost, srcPort, &payload, + payloadLen)) { + spdlog::debug("session udp(socks5) drop malformed relay datagram len={}", n); + continue; + } + std::string data(reinterpret_cast(payload), payloadLen); + auto holder = shared_from_this(); + this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->replyToLwip(std::move(data)); }); + continue; + } std::string data(buf, n); auto holder = shared_from_this(); this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->replyToLwip(std::move(data)); }); @@ -207,6 +292,11 @@ void SessionUdp::closeFromReactor() { netClose(this->fd); this->fd = -1; } + if (this->ctrlFd >= 0) { + this->stack->getReactor().del(this->ctrlFd); + netClose(this->ctrlFd); + this->ctrlFd = -1; + } #endif if (this->pcb != nullptr) { auto holder = shared_from_this(); @@ -216,4 +306,195 @@ void SessionUdp::closeFromReactor() { this->self.reset(); } +// ===================== socks5 UDP ASSOCIATE 控制连接(Reactor 线程) ===================== + +void SessionUdp::onCtrlEvent(uint32_t events) { + ReactorEvent ev = (ReactorEvent)events; + if (!this->ctrlConnected) { + if (ev & ReactorEvent::ERROR) { + closeFromReactor(); + return; + } + if (ev & ReactorEvent::WRITE) { + onCtrlConnected(); + } + return; + } + if (ev & ReactorEvent::ERROR) { + // 控制连接断开即关联失效,关闭整条会话。 + closeFromReactor(); + return; + } + // 握手阶段:分流可读/可写。握手完成后 associate 置空,控制连接仅保活(只关注 ERROR)。 + if (this->associate) { + if (ev & ReactorEvent::READ) { + onCtrlReadable(); + } + if (this->associate && (ev & ReactorEvent::WRITE)) { + onCtrlWritable(); + } + return; + } + // 关联已建立:控制连接上若有可读(通常不应有业务数据),读掉即可;对端关闭则关会话。 + if (ev & ReactorEvent::READ) { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + char buf[1024]; + long n = netRecv(this->ctrlFd, buf, sizeof(buf)); + if (n == 0) { + closeFromReactor(); + return; + } +#endif + } +} + +void SessionUdp::onCtrlConnected() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + int err = 0; +#if defined(_WIN32) || defined(_WIN64) + int len = sizeof(err); + int ret = ::getsockopt((SOCKET)this->ctrlFd, SOL_SOCKET, SO_ERROR, (char *)&err, &len); +#else + socklen_t len = sizeof(err); + int ret = ::getsockopt(this->ctrlFd, SOL_SOCKET, SO_ERROR, &err, &len); +#endif + if (ret != 0 || err != 0) { + spdlog::warn("session udp(socks5) ctrl connect error: {}", netErrStr(err ? err : netLastError())); + closeFromReactor(); + return; + } + this->ctrlConnected = true; + spdlog::debug("session udp(socks5) ctrl connected, begin UDP ASSOCIATE handshake"); + this->stack->getReactor().mod(this->ctrlFd, ReactorEvent::READ | ReactorEvent::WRITE); + flushCtrlOutbound(); +#endif +} + +void SessionUdp::flushCtrlOutbound() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + if (!this->associate || this->ctrlFd < 0) { + return; + } + if (this->associate->hasOutbound()) { + std::vector out = this->associate->takeOutbound(); + this->ctrlOutbox.append(reinterpret_cast(out.data()), out.size()); + } + while (!this->ctrlOutbox.empty()) { + long n = netSend(this->ctrlFd, this->ctrlOutbox.data(), this->ctrlOutbox.size()); + if (n > 0) { + this->ctrlOutbox.erase(0, n); + continue; + } + if (n < 0 && netWouldBlock(netLastError())) { + this->stack->getReactor().mod(this->ctrlFd, ReactorEvent::READ | ReactorEvent::WRITE); + return; + } + spdlog::warn("session udp(socks5) ctrl send failed: {}", netErrStr(netLastError())); + closeFromReactor(); + return; + } + this->stack->getReactor().mod(this->ctrlFd, ReactorEvent::READ); +#endif +} + +void SessionUdp::onCtrlWritable() { + flushCtrlOutbound(); +} + +void SessionUdp::onCtrlReadable() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + if (!this->associate) { + return; + } + char buf[4096]; + while (true) { + long n = netRecv(this->ctrlFd, buf, sizeof(buf)); + if (n > 0) { + if (!this->associate->feed(reinterpret_cast(buf), (size_t)n)) { + spdlog::warn("session udp(socks5) associate failed: {}", this->associate->error()); + closeFromReactor(); + return; + } + if (this->associate->done()) { + finishAssociate(); + return; + } + if (this->associate->hasOutbound()) { + flushCtrlOutbound(); + } + continue; + } + if (n == 0) { + spdlog::warn("session udp(socks5) server closed during associate"); + closeFromReactor(); + return; + } + if (netWouldBlock(netLastError())) { + return; + } + spdlog::warn("session udp(socks5) ctrl recv failed: {}", netErrStr(netLastError())); + closeFromReactor(); + return; + } +#endif +} + +void SessionUdp::finishAssociate() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + // 关联成功:取中继端点。若服务端返回 0.0.0.0(RFC 1928 §7 允许),回退到 server IP。 + this->relayHost = this->associate->udpRelayHost(); + this->relayPort = this->associate->udpRelayPort(); + if (uint32_t(this->relayHost) == 0) { + this->relayHost = this->proxyServerHost; + } + this->associate.reset(); + // 控制连接转为保活:仅关注 ERROR(其断开即关联失效)。 + this->stack->getReactor().mod(this->ctrlFd, ReactorEvent::NONE); + + spdlog::debug("session udp(socks5) associate done, relay {}:{}", this->relayHost.toString(), this->relayPort); + + // 建中继 UDP socket,connect 到中继端点(之后 send/recv 都是 socks5 UDP 封装报文)。 + int fd = (int)::socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + spdlog::warn("session udp(socks5) relay socket failed: {}", netErrStr(netLastError())); + closeFromReactor(); + return; + } + netSetNonBlocking(fd); + struct sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = htons(this->relayPort); + addr.sin_addr.s_addr = uint32_t(this->relayHost); + if (::connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { + spdlog::warn("session udp(socks5) relay connect {}:{} failed: {}", this->relayHost.toString(), this->relayPort, + netErrStr(netLastError())); + netClose(fd); + closeFromReactor(); + return; + } + this->fd = fd; + this->relayReady = true; + auto holder = shared_from_this(); + this->stack->getReactor().add(fd, ReactorEvent::READ, [holder](ReactorEvent ev) { holder->onFdEvent((uint32_t)ev); }); + + // 冲刷握手期间暂存的数据报(带 2 字节长度前缀串联)。 + size_t off = 0; + const std::string &buf = this->pendingDatagrams; + while (off + sizeof(uint16_t) <= buf.size()) { + uint16_t len = 0; + std::memcpy(&len, buf.data() + off, sizeof(len)); + off += sizeof(len); + if (off + len > buf.size()) { + break; + } + long n = netSend(this->fd, buf.data() + off, len); + if (n < 0 && !netWouldBlock(netLastError())) { + spdlog::debug("session udp(socks5) flush pending send failed: {}", netErrStr(netLastError())); + } + off += len; + } + this->pendingDatagrams.clear(); +#endif +} + } // namespace candy diff --git a/candy/src/netstack/session_udp.h b/candy/src/netstack/session_udp.h index 20b19611..b70f11eb 100644 --- a/candy/src/netstack/session_udp.h +++ b/candy/src/netstack/session_udp.h @@ -4,6 +4,7 @@ #include "core/net.h" #include "netstack/session.h" +#include "netstack/tcp_handshake.h" #include #include #include @@ -56,6 +57,17 @@ class SessionUdp : public Session, public std::enable_shared_from_this 跑 UDP ASSOCIATE 握手 -> 拿到中继端点 -> + // 建中继 UDP socket(fd) connect 到中继端点;之后 fd 上收发都加/去 socks5 UDP 头。 + // ctrlFd 保活,其关闭即关联失效,会话关闭。 + void onCtrlEvent(uint32_t events); + void onCtrlConnected(); + void onCtrlReadable(); + void onCtrlWritable(); + void flushCtrlOutbound(); + void finishAssociate(); + // ---- NetStack 线程 ---- // 本流后续数据报到达:addr/port=源端(dev1),提取数据投递到落地 fd 发送。 void onPcbRecv(struct pbuf *p, const ip_addr_t *addr, u16_t port); @@ -67,6 +79,18 @@ class SessionUdp : public Session, public std::enable_shared_from_this associate; // UDP ASSOCIATE 握手器(完成后置空) + std::string ctrlOutbox; // 控制连接待发字节残留(非阻塞部分写) + bool relayReady = false; // 中继 fd 是否已就绪(握手完成、connect 中继后置真) + IP4 relayHost; // socks5 UDP 中继端点 + uint16_t relayPort = 0; + IP4 proxyServerHost; // socks5 server IP(中继地址为 0.0.0.0 时回退用) + std::string pendingDatagrams; // 握手期间 lwIP 侧到达的数据报暂存(带长度前缀串联) + IP4 origSrc; uint16_t origSrcPort; IP4 origDst; diff --git a/candy/src/netstack/socks5_client.cc b/candy/src/netstack/socks5_client.cc index 2feef2cb..0b0f4e6c 100644 --- a/candy/src/netstack/socks5_client.cc +++ b/candy/src/netstack/socks5_client.cc @@ -6,6 +6,7 @@ namespace candy { void Socks5Client::startConnectIPv4(IP4 dstHost, uint16_t dstPort, const std::string &username, const std::string &password) { + this->command = Socks5::CMD_CONNECT; this->useDomain = false; this->dstHost = dstHost; this->dstPort = dstPort; @@ -18,6 +19,7 @@ void Socks5Client::startConnectIPv4(IP4 dstHost, uint16_t dstPort, const std::st void Socks5Client::startConnectDomain(const std::string &domain, uint16_t dstPort, const std::string &username, const std::string &password) { + this->command = Socks5::CMD_CONNECT; this->useDomain = true; this->dstDomain = domain; this->dstPort = dstPort; @@ -28,6 +30,28 @@ void Socks5Client::startConnectDomain(const std::string &domain, uint16_t dstPor this->st = State::WriteAuthMethods; } +void Socks5Client::startUdpAssociate(const std::string &username, const std::string &password) { + // UDP ASSOCIATE:CMD=0x03,请求里的 DST.ADDR/DST.PORT 填 0.0.0.0:0(RFC 1928 §7: + // 客户端尚不确定将从哪个源端口发 UDP,置 0 让服务端不做限制)。其余握手流程与 CONNECT 相同。 + this->command = Socks5::CMD_UDP_ASSOCIATE; + this->useDomain = false; + this->dstHost = IP4("0.0.0.0"); + this->dstPort = 0; + this->username = username; + this->password = password; + this->useUserPass = !username.empty(); + buildAuthMethods(); + this->st = State::WriteAuthMethods; +} + +IP4 Socks5Client::udpRelayHost() const { + return this->relayAddr; +} + +uint16_t Socks5Client::udpRelayPort() const { + return this->relayPortValue; +} + void Socks5Client::buildAuthMethods() { // VER(1) NMETHODS(1) METHODS(nmethods)。无认证只声明 NONE; // 配了用户口令则同时声明 NONE 与 USERPASS,让服务端择优。 @@ -54,10 +78,11 @@ void Socks5Client::buildAuthCreds() { } void Socks5Client::buildRequest() { - // VER(1) CMD(1) RSV(1=0) ATYP(1) DST.ADDR DST.PORT。 + // VER(1) CMD(1) RSV(1=0) ATYP(1) DST.ADDR DST.PORT。CMD 取 this->command + // (CONNECT 或 UDP ASSOCIATE)。 this->outbound.clear(); this->outbound.push_back(Socks5::VERSION); - this->outbound.push_back(Socks5::CMD_CONNECT); + this->outbound.push_back(this->command); this->outbound.push_back(0x00); if (this->useDomain) { this->outbound.push_back(Socks5::ATYP_DOMAIN); @@ -173,6 +198,21 @@ bool Socks5Client::feed(const uint8_t *data, size_t len) { if (this->inbound.size() < total) { return true; // 等待剩余字节 } + // UDP ASSOCIATE:BND.ADDR/BND.PORT 即服务端分配的 UDP 中继端点,需解析留用。 + // 仅支持 IPv4 中继地址(与本工程 IPv4-only 一致)。若服务端返回 0.0.0.0, + // 按 RFC 1928 约定回退到控制连接的对端地址(由调用方处理,这里置 0)。 + if (this->command == Socks5::CMD_UDP_ASSOCIATE) { + if (atyp != Socks5::ATYP_IPV4) { + fail("udp associate reply with non-IPv4 relay addr"); + return false; + } + uint32_t v = 0; + std::memcpy(&v, &this->inbound[4], 4); // 网络序字节直存即 a.b.c.d + std::memcpy(&this->relayAddr, &v, 4); // IP4 为 packed 4 字节,按内存序直存 + uint16_t portBE = 0; + std::memcpy(&portBE, &this->inbound[4 + addrLen], 2); + this->relayPortValue = ntoh(portBE); + } // 应答收齐,多余的即为业务数据。 this->leftover.assign(this->inbound.begin() + total, this->inbound.end()); this->inbound.clear(); @@ -237,4 +277,70 @@ void Socks5Client::fail(const std::string &reason) { this->st = State::Failed; } +std::string socks5UdpEncap(IP4 dstHost, uint16_t dstPort, const uint8_t *data, size_t len) { + // RSV(2=0) FRAG(1=0) ATYP(1=IPv4) DST.ADDR(4) DST.PORT(2) DATA。 + std::string out; + out.reserve(10 + len); + out.push_back(0x00); // RSV + out.push_back(0x00); // RSV + out.push_back(0x00); // FRAG + out.push_back((char)Socks5::ATYP_IPV4); + uint32_t v = uint32_t(dstHost); // 内存即网络序 a.b.c.d + out.append(reinterpret_cast(&v), 4); + uint16_t portBE = hton(dstPort); + out.append(reinterpret_cast(&portBE), 2); + if (data != nullptr && len > 0) { + out.append(reinterpret_cast(data), len); + } + return out; +} + +bool socks5UdpDecap(const uint8_t *pkt, size_t len, IP4 &srcHost, uint16_t &srcPort, const uint8_t **payload, + size_t &payloadLen) { + // 最小头长:RSV(2) FRAG(1) ATYP(1) = 4 字节,之后按 ATYP 取地址。 + if (pkt == nullptr || len < 4) { + return false; + } + uint8_t frag = pkt[2]; + if (frag != 0x00) { + return false; // 不支持分片,丢弃 + } + uint8_t atyp = pkt[3]; + size_t addrLen = 0; + switch (atyp) { + case Socks5::ATYP_IPV4: + addrLen = 4; + break; + case Socks5::ATYP_IPV6: + addrLen = 16; + break; + case Socks5::ATYP_DOMAIN: + if (len < 5) { + return false; + } + addrLen = 1 + pkt[4]; + break; + default: + return false; + } + size_t headLen = 4 + addrLen + 2; + if (len < headLen) { + return false; + } + // 仅 IPv4 源地址可还原为 IP4;其余类型也接受但 srcHost 置 0(回包寻址不依赖它)。 + if (atyp == Socks5::ATYP_IPV4) { + uint32_t v = 0; + std::memcpy(&v, &pkt[4], 4); + std::memcpy(&srcHost, &v, 4); + } else { + srcHost = IP4("0.0.0.0"); + } + uint16_t portBE = 0; + std::memcpy(&portBE, &pkt[4 + addrLen], 2); + srcPort = ntoh(portBE); + *payload = pkt + headLen; + payloadLen = len - headLen; + return true; +} + } // namespace candy diff --git a/candy/src/netstack/socks5_client.h b/candy/src/netstack/socks5_client.h index f4effdfb..9a39451c 100644 --- a/candy/src/netstack/socks5_client.h +++ b/candy/src/netstack/socks5_client.h @@ -3,6 +3,7 @@ #define CANDY_NETSTACK_SOCKS5_CLIENT_H #include "core/net.h" +#include "netstack/tcp_handshake.h" #include #include #include @@ -38,7 +39,7 @@ constexpr uint8_t REP_SUCCESS = 0x00; // 应答:成功 // WriteAuthMethods -> ReadAuthMethod // -> [若选中 USERPASS] WriteAuthCreds -> ReadAuthCreds // -> WriteRequest -> ReadResponse -> Done -class Socks5Client { +class Socks5Client : public TcpHandshake { public: enum class State { Init, // 未开始 @@ -60,23 +61,31 @@ class Socks5Client { void startConnectDomain(const std::string &domain, uint16_t dstPort, const std::string &username = "", const std::string &password = ""); + // UDP ASSOCIATE 握手(RFC 1928 §7):在 TCP 控制连接上请求 UDP 中继。 + // 请求里的 DST.ADDR/DST.PORT 填 0.0.0.0:0(表示客户端中继源端口未定,服务端不限制)。 + // 完成后由 udpRelayHost()/udpRelayPort() 取服务端返回的 UDP 中继端点(BND.ADDR:BND.PORT)。 + void startUdpAssociate(const std::string &username = "", const std::string &password = ""); + // 喂入从 socket 收到的字节,推进状态机。返回 false 表示握手失败(state()==Failed)。 - bool feed(const uint8_t *data, size_t len); + bool feed(const uint8_t *data, size_t len) override; // 取走当前待发送字节(取走后内部清空)。调用方负责把它写到 socket; // 若一次没写完,剩余部分应由调用方自行保留续发,本类不重复给出。 - std::vector takeOutbound(); + std::vector takeOutbound() override; // 是否还有待发送字节。 - bool hasOutbound() const; + bool hasOutbound() const override; // 握手完成后,取走应答之后多余的入站业务数据(通常为空)。 - std::vector takeLeftover(); + std::vector takeLeftover() override; State state() const; - bool done() const; - bool failed() const; - const std::string &error() const; + bool done() const override; + bool failed() const override; + const std::string &error() const override; + + IP4 udpRelayHost() const override; + uint16_t udpRelayPort() const override; private: void buildAuthMethods(); @@ -89,18 +98,37 @@ class Socks5Client { std::string username; std::string password; + // 握手命令:CONNECT(TCP) 或 UDP ASSOCIATE(UDP 中继),决定 buildRequest 的 CMD 与 + // 应答里 BND.ADDR/BND.PORT 的语义(CONNECT 时无用,UDP ASSOCIATE 时为中继端点)。 + uint8_t command = Socks5::CMD_CONNECT; + // 目标地址(二选一:IPv4 或域名)。 bool useDomain = false; IP4 dstHost; std::string dstDomain; uint16_t dstPort = 0; + // UDP ASSOCIATE 应答解析出的中继端点(仅 command==UDP ASSOCIATE 且 Done 后有效)。 + IP4 relayAddr; + uint16_t relayPortValue = 0; + std::vector outbound; // 待发送缓冲 std::vector inbound; // 入站累积缓冲(用于跨次拼接定长/变长应答) std::vector leftover; // 握手完成后多余的入站字节 std::string errMsg; // 失败原因(仅在 Failed 时有效) }; +// socks5 UDP 中继数据报封装(RFC 1928 §7)。每个发往中继的 UDP 数据报前缀一个头: +// RSV(2=0x0000) FRAG(1=0x00) ATYP(1) DST.ADDR DST.PORT DATA。 +// 本工程 IPv4-only,ATYP 固定 0x01。返回"头 + 业务数据"的完整数据报字节。 +std::string socks5UdpEncap(IP4 dstHost, uint16_t dstPort, const uint8_t *data, size_t len); + +// socks5 UDP 中继数据报解封装:解析中继回包的头,输出原始目的端点(srcHost/srcPort) +// 与业务数据。返回 false 表示包格式非法或 FRAG!=0(不支持分片,按 RFC 丢弃)。 +// 成功时 payload 指向 pkt 内部的业务数据起点(零拷贝),payloadLen 为其长度。 +bool socks5UdpDecap(const uint8_t *pkt, size_t len, IP4 &srcHost, uint16_t &srcPort, const uint8_t **payload, + size_t &payloadLen); + } // namespace candy #endif diff --git a/candy/src/netstack/tcp_handshake.h b/candy/src/netstack/tcp_handshake.h new file mode 100644 index 00000000..b695accf --- /dev/null +++ b/candy/src/netstack/tcp_handshake.h @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_TCP_HANDSHAKE_H +#define CANDY_NETSTACK_TCP_HANDSHAKE_H + +#include +#include +#include +#include + +#include "core/net.h" + +namespace candy { + +// TcpHandshake:在落地 TCP 连接建立之后、进入双向 splice 之前,需要先完成的 +// 应用层握手抽象(如 socks5 CONNECT)。direct 出站无需握手(makeTcpHandshake 返回 nullptr)。 +// +// 由 Reactor 线程单线程驱动,区别于 hev 的协程阻塞 IO: +// - 可写时:取 takeOutbound() 的待发字节写入 fd; +// - 可读时:把收到的字节 feed() 进来推进; +// - done() 表示握手成功,failed() 表示失败,takeLeftover() 取握手后粘连的业务数据。 +// Session 由此把"握手如何进行"与"具体协议(socks5)"解耦。 +class TcpHandshake { +public: + virtual ~TcpHandshake() = default; + + // 取走当前待发送字节(取走后内部清空),调用方负责写入 fd。 + virtual std::vector takeOutbound() = 0; + // 是否还有待发送字节。 + virtual bool hasOutbound() const = 0; + // 喂入从 fd 收到的字节推进握手;返回 false 表示握手失败。 + virtual bool feed(const uint8_t *data, size_t len) = 0; + // 握手是否成功完成。 + virtual bool done() const = 0; + // 握手是否失败。 + virtual bool failed() const = 0; + // 握手完成后取走应答之后多余的入站业务数据(通常为空)。 + virtual std::vector takeLeftover() = 0; + // 失败原因(仅在 failed() 时有效)。 + virtual const std::string &error() const = 0; + + // UDP ASSOCIATE 专用:握手完成后服务端返回的 UDP 中继端点。 + // 仅 socks5 UDP 关联握手有意义;其余握手返回默认值(调用方不会使用)。 + virtual IP4 udpRelayHost() const { return IP4("0.0.0.0"); } + virtual uint16_t udpRelayPort() const { return 0; } +}; + +} // namespace candy + +#endif From 48a7bc4225a5983543f4d9f5ec35f6883f3b1ac3 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Tue, 30 Jun 2026 17:47:17 +0800 Subject: [PATCH 08/29] use libve, Linux/Mac OK --- .gitignore | 1 + CMakeLists.txt | 4 + candy/CMakeLists.txt | 3 + candy/src/netstack/reactor.cc | 722 +--- candy/src/netstack/reactor.h | 56 +- candy/src/netstack/session_icmp.cc | 10 +- cmake/libev.cmake | 76 + third_party/libev/Changes | 517 +++ third_party/libev/LICENSE | 37 + third_party/libev/README | 58 + third_party/libev/ev++.h | 816 +++++ third_party/libev/ev.c | 5144 ++++++++++++++++++++++++++++ third_party/libev/ev.h | 854 +++++ third_party/libev/ev_epoll.c | 285 ++ third_party/libev/ev_kqueue.c | 218 ++ third_party/libev/ev_poll.c | 151 + third_party/libev/ev_port.c | 189 + third_party/libev/ev_select.c | 316 ++ third_party/libev/ev_vars.h | 204 ++ third_party/libev/ev_win32.c | 162 + third_party/libev/ev_wrap.h | 200 ++ 21 files changed, 9417 insertions(+), 606 deletions(-) create mode 100644 cmake/libev.cmake create mode 100644 third_party/libev/Changes create mode 100644 third_party/libev/LICENSE create mode 100644 third_party/libev/README create mode 100644 third_party/libev/ev++.h create mode 100644 third_party/libev/ev.c create mode 100644 third_party/libev/ev.h create mode 100644 third_party/libev/ev_epoll.c create mode 100644 third_party/libev/ev_kqueue.c create mode 100644 third_party/libev/ev_poll.c create mode 100644 third_party/libev/ev_port.c create mode 100644 third_party/libev/ev_select.c create mode 100644 third_party/libev/ev_vars.h create mode 100644 third_party/libev/ev_win32.c create mode 100644 third_party/libev/ev_wrap.h diff --git a/.gitignore b/.gitignore index b101ac94..ad582460 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ # CMake Build files .cache build +build-mac \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 204fd687..748013cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,10 @@ endif() set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) +# libev:事件循环库(reactor 后端,一份代码全平台)。 +# 采用 vendored 源码编译(third_party/libev),不依赖系统库,三平台开箱即用。 +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/libev.cmake) + include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/lwip.cmake) add_subdirectory(candy) diff --git a/candy/CMakeLists.txt b/candy/CMakeLists.txt index bdb07613..b6270896 100644 --- a/candy/CMakeLists.txt +++ b/candy/CMakeLists.txt @@ -19,6 +19,9 @@ target_link_libraries(candy-library PRIVATE Poco::Foundation Poco::JSON Poco::Ne target_link_libraries(candy-library PRIVATE Threads::Threads) target_link_libraries(candy-library PRIVATE lwip) +# libev:reactor 事件循环后端(vendored 源码编译,见 cmake/libev.cmake)。 +target_link_libraries(candy-library PRIVATE Candy::ev) + if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") target_link_libraries(candy-library PRIVATE ws2_32) endif() diff --git a/candy/src/netstack/reactor.cc b/candy/src/netstack/reactor.cc index 08b50c15..9e2ddf2e 100644 --- a/candy/src/netstack/reactor.cc +++ b/candy/src/netstack/reactor.cc @@ -1,88 +1,90 @@ // SPDX-License-Identifier: MIT #include "netstack/reactor.h" +#include #include -#if defined(__linux__) -#include -#include -#include -#elif defined(__APPLE__) -#include -#include -#include -#elif defined(_WIN32) || defined(_WIN64) -#ifndef _WIN32_WINNT -#define _WIN32_WINNT 0x0600 -#endif -#include -#include -#include -#endif - namespace candy { -#if defined(__linux__) - -static uint32_t toEpollEvents(ReactorEvent events) { - uint32_t ev = 0; +// 每个落地 fd 对应一个 IoWatcher:内嵌 libev 的 ev_io,并携带 fd、所属 Reactor 与事件回调。 +// 仅 reactor 线程创建/销毁/访问。io.data 指回本结构以便在静态回调里取回上下文。 +struct IoWatcher { + ev_io io; + int fd; + Reactor *reactor; + Reactor::EventHandler handler; + ReactorEvent events; +}; + +static int toEvEvents(ReactorEvent events) { + int ev = 0; if (events & ReactorEvent::READ) { - ev |= EPOLLIN; + ev |= EV_READ; } if (events & ReactorEvent::WRITE) { - ev |= EPOLLOUT; + ev |= EV_WRITE; } return ev; } -static ReactorEvent fromEpollEvents(uint32_t ev) { +static ReactorEvent fromEvEvents(int revents) { ReactorEvent events = ReactorEvent::NONE; - if (ev & EPOLLIN) { + if (revents & EV_READ) { events = events | ReactorEvent::READ; } - if (ev & EPOLLOUT) { + if (revents & EV_WRITE) { events = events | ReactorEvent::WRITE; } - if (ev & (EPOLLERR | EPOLLHUP)) { + if (revents & EV_ERROR) { events = events | ReactorEvent::ERROR; } return events; } -Reactor::Reactor() : epollFd(-1), wakeupFd(-1), running(false) {} +Reactor::Reactor() : evLoop(nullptr), asyncWatcher(nullptr), running(false) {} Reactor::~Reactor() { stop(); } -int Reactor::start() { - this->epollFd = epoll_create1(EPOLL_CLOEXEC); - if (this->epollFd < 0) { - spdlog::error("reactor epoll_create1 failed: {}", strerror(errno)); - return -1; - } +// 按平台显式指定 libev 后端,与 cmake/libev.cmake 编入的 EV_USE_* 严格对齐。 +// 不能用 EVFLAG_AUTO:AUTO 会走 ev_recommended_backends(),而该函数在 __APPLE__ +// 下硬编码剔除 KQUEUE 与 POLL(ev.c 注释 "horribly broken"),只留 SELECT; +// 但我们为缩小风险面把 EV_USE_SELECT 关了,导致 mac 上推荐后端集合为空、 +// ev_loop_new 返回 null(reactor ev_loop_new failed)。 +// reactor 只用后端监听 TCP/UDP socket(utun fd 由 tun 线程单独读写,不进 reactor), +// 普通 socket 上 kqueue 可靠,故这里显式指定 kqueue 绕过推荐过滤。 +static unsigned int reactorBackendFlags() { +#if defined(__APPLE__) + return EVBACKEND_KQUEUE; // 与 EV_USE_KQUEUE=1 对齐 +#elif defined(__linux__) || defined(__ANDROID__) + return EVBACKEND_EPOLL; // 与 EV_USE_EPOLL=1 对齐(musl 下也走 epoll) +#elif defined(_WIN32) + return EVBACKEND_SELECT; // 与 EV_USE_SELECT=1 / EV_SELECT_IS_WINSOCKET 对齐 +#else + return EVFLAG_AUTO; +#endif +} - this->wakeupFd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); - if (this->wakeupFd < 0) { - spdlog::error("reactor eventfd failed: {}", strerror(errno)); - ::close(this->epollFd); - this->epollFd = -1; +int Reactor::start() { + // 独立事件循环(不使用默认 loop,避免与进程内其他 libev 使用者冲突)。 + this->evLoop = ev_loop_new(reactorBackendFlags()); + if (this->evLoop == nullptr) { + spdlog::error("reactor ev_loop_new failed"); return -1; } - struct epoll_event ee = {}; - ee.events = EPOLLIN; - ee.data.fd = this->wakeupFd; - if (epoll_ctl(this->epollFd, EPOLL_CTL_ADD, this->wakeupFd, &ee) != 0) { - spdlog::error("reactor add wakeupFd failed: {}", strerror(errno)); - ::close(this->wakeupFd); - ::close(this->epollFd); - this->wakeupFd = -1; - this->epollFd = -1; - return -1; - } + // ev_async:唯一可跨线程安全触发的 watcher,用作 post() 的唤醒通道。 + ev_async *async = new ev_async(); + ev_async_init(async, &Reactor::asyncCallback); + async->data = this; + ev_async_start(this->evLoop, async); + this->asyncWatcher = async; this->running.store(true); this->thread = std::thread([this] { this->loop(); }); + // 在启动线程内确定性记录 reactor 线程 id(早于任何跨线程 add/mod/del 访问), + // 避免在 loop() 内写、其他线程读 loopThreadId 造成的数据竞争。 + this->loopThreadId = this->thread.get_id(); return 0; } @@ -93,487 +95,133 @@ void Reactor::stop() { } return; } - wakeup(); + // 唤醒 reactor 线程,async 回调里检测到 !running 会 ev_break 退出 ev_run。 + if (this->asyncWatcher != nullptr) { + ev_async_send(this->evLoop, (ev_async *)this->asyncWatcher); + } if (this->thread.joinable()) { this->thread.join(); } - if (this->wakeupFd >= 0) { - ::close(this->wakeupFd); - this->wakeupFd = -1; - } - if (this->epollFd >= 0) { - ::close(this->epollFd); - this->epollFd = -1; - } -} - -int Reactor::add(int fd, ReactorEvent events, EventHandler handler) { - { - std::unique_lock lock(this->handlerMutex); - this->handlers[fd] = std::move(handler); - } - struct epoll_event ee = {}; - ee.events = toEpollEvents(events); - ee.data.fd = fd; - if (epoll_ctl(this->epollFd, EPOLL_CTL_ADD, fd, &ee) != 0) { - spdlog::error("reactor add fd {} failed: {}", fd, strerror(errno)); - std::unique_lock lock(this->handlerMutex); - this->handlers.erase(fd); - return -1; - } - return 0; -} - -int Reactor::mod(int fd, ReactorEvent events) { - struct epoll_event ee = {}; - ee.events = toEpollEvents(events); - ee.data.fd = fd; - if (epoll_ctl(this->epollFd, EPOLL_CTL_MOD, fd, &ee) != 0) { - spdlog::error("reactor mod fd {} failed: {}", fd, strerror(errno)); - return -1; - } - return 0; -} - -int Reactor::del(int fd) { - epoll_ctl(this->epollFd, EPOLL_CTL_DEL, fd, nullptr); - std::unique_lock lock(this->handlerMutex); - this->handlers.erase(fd); - return 0; -} - -void Reactor::post(Task task) { - { - std::unique_lock lock(this->taskMutex); - this->tasks.push(std::move(task)); - } - wakeup(); -} - -void Reactor::wakeup() { - uint64_t one = 1; - if (this->wakeupFd >= 0) { - ssize_t n = ::write(this->wakeupFd, &one, sizeof(one)); - (void)n; - } -} -void Reactor::drainWakeup() { - uint64_t buf; - while (::read(this->wakeupFd, &buf, sizeof(buf)) > 0) { + // 线程已退出,安全清理剩余 watcher 与 loop(仅本线程访问)。 + for (auto &kv : this->ioWatchers) { + IoWatcher *iw = (IoWatcher *)kv.second; + ev_io_stop(this->evLoop, &iw->io); + delete iw; } -} + this->ioWatchers.clear(); -void Reactor::drainTasks() { - std::queue pending; - { - std::unique_lock lock(this->taskMutex); - std::swap(pending, this->tasks); + if (this->asyncWatcher != nullptr) { + ev_async_stop(this->evLoop, (ev_async *)this->asyncWatcher); + delete (ev_async *)this->asyncWatcher; + this->asyncWatcher = nullptr; } - while (!pending.empty()) { - pending.front()(); - pending.pop(); - } -} - -void Reactor::loop() { - spdlog::debug("start thread: netstack reactor"); - constexpr int MAX_EVENTS = 256; - struct epoll_event events[MAX_EVENTS]; - while (this->running.load()) { - int n = epoll_wait(this->epollFd, events, MAX_EVENTS, -1); - if (n < 0) { - if (errno == EINTR) { - continue; - } - spdlog::error("reactor epoll_wait failed: {}", strerror(errno)); - break; - } - for (int i = 0; i < n; ++i) { - int fd = events[i].data.fd; - if (fd == this->wakeupFd) { - drainWakeup(); - drainTasks(); - continue; - } - EventHandler handler; - { - std::unique_lock lock(this->handlerMutex); - auto it = this->handlers.find(fd); - if (it == this->handlers.end()) { - continue; - } - handler = it->second; - } - handler(fromEpollEvents(events[i].events)); - } + if (this->evLoop != nullptr) { + ev_loop_destroy(this->evLoop); + this->evLoop = nullptr; } - spdlog::debug("stop thread: netstack reactor"); } -#elif defined(__APPLE__) - -// EVFILT_USER 的标识,用于跨线程唤醒 reactor 线程(替代 Linux 的 eventfd)。 -static constexpr uintptr_t WAKEUP_IDENT = 1; - -Reactor::Reactor() : kqueueFd(-1), running(false) {} - -Reactor::~Reactor() { - stop(); -} - -int Reactor::start() { - this->kqueueFd = kqueue(); - if (this->kqueueFd < 0) { - spdlog::error("reactor kqueue failed: {}", strerror(errno)); - return -1; - } - - // 注册 EVFILT_USER 作为唤醒通道。 - struct kevent kev = {}; - EV_SET(&kev, WAKEUP_IDENT, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, nullptr); - if (kevent(this->kqueueFd, &kev, 1, nullptr, 0, nullptr) != 0) { - spdlog::error("reactor register EVFILT_USER failed: {}", strerror(errno)); - ::close(this->kqueueFd); - this->kqueueFd = -1; - return -1; - } - - this->running.store(true); - this->thread = std::thread([this] { this->loop(); }); - return 0; +bool Reactor::onLoopThread() const { + return std::this_thread::get_id() == this->loopThreadId; } -void Reactor::stop() { - if (!this->running.exchange(false)) { - if (this->thread.joinable()) { - this->thread.join(); - } - return; - } - wakeup(); - if (this->thread.joinable()) { - this->thread.join(); - } - if (this->kqueueFd >= 0) { - ::close(this->kqueueFd); - this->kqueueFd = -1; - } -} +// ===================== 对外接口:按线程归属直接应用或投递 ===================== int Reactor::add(int fd, ReactorEvent events, EventHandler handler) { - { - std::unique_lock lock(this->handlerMutex); - this->handlers[fd] = std::move(handler); - } - - // kqueue 的 READ/WRITE 为独立 filter,按兴趣集分别 EV_ADD。 - struct kevent kev[2]; - int n = 0; - if (events & ReactorEvent::READ) { - EV_SET(&kev[n++], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, nullptr); - } - if (events & ReactorEvent::WRITE) { - EV_SET(&kev[n++], fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, nullptr); + if (onLoopThread()) { + applyAdd(fd, events, std::move(handler)); + } else { + auto h = std::make_shared(std::move(handler)); + post([this, fd, events, h] { this->applyAdd(fd, events, std::move(*h)); }); } - if (n > 0 && kevent(this->kqueueFd, kev, n, nullptr, 0, nullptr) != 0) { - spdlog::error("reactor add fd {} failed: {}", fd, strerror(errno)); - std::unique_lock lock(this->handlerMutex); - this->handlers.erase(fd); - return -1; - } - this->interests[fd] = events; return 0; } int Reactor::mod(int fd, ReactorEvent events) { - // 对比旧兴趣集,对增加的 filter EV_ADD、移除的 filter EV_DELETE,实现整体替换语义。 - ReactorEvent old = ReactorEvent::NONE; - auto it = this->interests.find(fd); - if (it != this->interests.end()) { - old = it->second; - } - - struct kevent kev[2]; - int n = 0; - bool oldRead = (old & ReactorEvent::READ); - bool newRead = (events & ReactorEvent::READ); - bool oldWrite = (old & ReactorEvent::WRITE); - bool newWrite = (events & ReactorEvent::WRITE); - if (newRead && !oldRead) { - EV_SET(&kev[n++], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, nullptr); - } else if (!newRead && oldRead) { - EV_SET(&kev[n++], fd, EVFILT_READ, EV_DELETE, 0, 0, nullptr); - } - if (newWrite && !oldWrite) { - EV_SET(&kev[n++], fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, nullptr); - } else if (!newWrite && oldWrite) { - EV_SET(&kev[n++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, nullptr); + if (onLoopThread()) { + applyMod(fd, events); + } else { + post([this, fd, events] { this->applyMod(fd, events); }); } - if (n > 0 && kevent(this->kqueueFd, kev, n, nullptr, 0, nullptr) != 0) { - // EV_DELETE 对未注册 filter 返回 ENOENT 可忽略。 - if (errno != ENOENT) { - spdlog::error("reactor mod fd {} failed: {}", fd, strerror(errno)); - return -1; - } - } - this->interests[fd] = events; return 0; } int Reactor::del(int fd) { - ReactorEvent old = ReactorEvent::NONE; - auto it = this->interests.find(fd); - if (it != this->interests.end()) { - old = it->second; - } - struct kevent kev[2]; - int n = 0; - if (old & ReactorEvent::READ) { - EV_SET(&kev[n++], fd, EVFILT_READ, EV_DELETE, 0, 0, nullptr); + if (onLoopThread()) { + applyDel(fd); + } else { + post([this, fd] { this->applyDel(fd); }); } - if (old & ReactorEvent::WRITE) { - EV_SET(&kev[n++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, nullptr); - } - if (n > 0) { - kevent(this->kqueueFd, kev, n, nullptr, 0, nullptr); - } - this->interests.erase(fd); - std::unique_lock lock(this->handlerMutex); - this->handlers.erase(fd); return 0; } -void Reactor::post(Task task) { - { - std::unique_lock lock(this->taskMutex); - this->tasks.push(std::move(task)); - } - wakeup(); -} +// ===================== 仅 reactor 线程:实际操作 libev watcher ===================== -void Reactor::wakeup() { - if (this->kqueueFd < 0) { +void Reactor::applyAdd(int fd, ReactorEvent events, EventHandler handler) { + auto it = this->ioWatchers.find(fd); + if (it != this->ioWatchers.end()) { + // 已存在:等价于替换 handler 与兴趣集(防御性,正常不应发生)。 + IoWatcher *iw = (IoWatcher *)it->second; + ev_io_stop(this->evLoop, &iw->io); + iw->handler = std::move(handler); + iw->events = events; + ev_io_set(&iw->io, fd, toEvEvents(events)); + ev_io_start(this->evLoop, &iw->io); return; } - struct kevent kev = {}; - EV_SET(&kev, WAKEUP_IDENT, EVFILT_USER, 0, NOTE_TRIGGER, 0, nullptr); - kevent(this->kqueueFd, &kev, 1, nullptr, 0, nullptr); + IoWatcher *iw = new IoWatcher(); + iw->fd = fd; + iw->reactor = this; + iw->handler = std::move(handler); + iw->events = events; + iw->io.data = iw; + ev_io_init(&iw->io, &Reactor::ioCallback, fd, toEvEvents(events)); + ev_io_start(this->evLoop, &iw->io); + this->ioWatchers[fd] = iw; } -void Reactor::drainWakeup() { - // EVFILT_USER 配合 EV_CLEAR 为边沿触发,无需额外清空动作。 -} - -void Reactor::drainTasks() { - std::queue pending; - { - std::unique_lock lock(this->taskMutex); - std::swap(pending, this->tasks); - } - while (!pending.empty()) { - pending.front()(); - pending.pop(); - } -} - -void Reactor::loop() { - spdlog::debug("start thread: netstack reactor"); - constexpr int MAX_EVENTS = 256; - struct kevent events[MAX_EVENTS]; - while (this->running.load()) { - int n = kevent(this->kqueueFd, nullptr, 0, events, MAX_EVENTS, nullptr); - if (n < 0) { - if (errno == EINTR) { - continue; - } - spdlog::error("reactor kevent failed: {}", strerror(errno)); - break; - } - for (int i = 0; i < n; ++i) { - if (events[i].filter == EVFILT_USER && events[i].ident == WAKEUP_IDENT) { - drainWakeup(); - drainTasks(); - continue; - } - int fd = (int)events[i].ident; - EventHandler handler; - { - std::unique_lock lock(this->handlerMutex); - auto it = this->handlers.find(fd); - if (it == this->handlers.end()) { - continue; - } - handler = it->second; - } - ReactorEvent ev = ReactorEvent::NONE; - if (events[i].filter == EVFILT_READ) { - ev = ev | ReactorEvent::READ; - } - if (events[i].filter == EVFILT_WRITE) { - ev = ev | ReactorEvent::WRITE; - } - if (events[i].flags & EV_EOF) { - ev = ev | ReactorEvent::ERROR; - } - handler(ev); - } - } - spdlog::debug("stop thread: netstack reactor"); -} - -#elif defined(_WIN32) || defined(_WIN64) - -// Windows 后端:用 WSAPoll 做就绪通知(无内核兴趣集,每轮按 interests 重建 pollfd 数组), -// 用一对自连环回 UDP socket 做跨线程唤醒(替代 Linux eventfd / macOS EVFILT_USER)。 -// 注:与 Linux/macOS 一致,对外仍以 int fd 暴露;SOCKET 句柄在 Windows 上为小整数,转换安全。 - -static SHORT toWsaEvents(ReactorEvent events) { - SHORT ev = 0; - if (events & ReactorEvent::READ) { - ev |= POLLRDNORM; - } - if (events & ReactorEvent::WRITE) { - ev |= POLLWRNORM; - } - return ev; -} - -static ReactorEvent fromWsaEvents(SHORT ev) { - ReactorEvent events = ReactorEvent::NONE; - if (ev & POLLRDNORM) { - events = events | ReactorEvent::READ; - } - if (ev & POLLWRNORM) { - events = events | ReactorEvent::WRITE; - } - if (ev & (POLLERR | POLLHUP | POLLNVAL)) { - events = events | ReactorEvent::ERROR; - } - return events; -} - -Reactor::Reactor() : wakeupRecv(-1), wakeupSend(-1), running(false) {} - -Reactor::~Reactor() { - stop(); -} - -int Reactor::start() { - // WSAStartup 引用计数,tun/windows.cc 可能已初始化,这里再调一次安全。 - WSADATA wsaData; - if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { - spdlog::error("reactor WSAStartup failed: {}", WSAGetLastError()); - return -1; - } - - SOCKET recvSock = ::socket(AF_INET, SOCK_DGRAM, 0); - SOCKET sendSock = ::socket(AF_INET, SOCK_DGRAM, 0); - if (recvSock == INVALID_SOCKET || sendSock == INVALID_SOCKET) { - spdlog::error("reactor wakeup socket failed: {}", WSAGetLastError()); - if (recvSock != INVALID_SOCKET) { - ::closesocket(recvSock); - } - if (sendSock != INVALID_SOCKET) { - ::closesocket(sendSock); - } - WSACleanup(); - return -1; +void Reactor::applyMod(int fd, ReactorEvent events) { + auto it = this->ioWatchers.find(fd); + if (it == this->ioWatchers.end()) { + return; } - - // recv 端绑定到 127.0.0.1 随机端口,send 端 connect 到该端口形成自连环回。 - struct sockaddr_in addr = {}; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - addr.sin_port = 0; - int addrLen = sizeof(addr); - if (::bind(recvSock, (struct sockaddr *)&addr, addrLen) != 0 || - ::getsockname(recvSock, (struct sockaddr *)&addr, &addrLen) != 0 || - ::connect(sendSock, (struct sockaddr *)&addr, sizeof(addr)) != 0) { - spdlog::error("reactor wakeup loopback setup failed: {}", WSAGetLastError()); - ::closesocket(recvSock); - ::closesocket(sendSock); - WSACleanup(); - return -1; + IoWatcher *iw = (IoWatcher *)it->second; + if (iw->events == events) { + return; } - - u_long nonBlocking = 1; - ::ioctlsocket(recvSock, FIONBIO, &nonBlocking); - - this->wakeupRecv = (int)recvSock; - this->wakeupSend = (int)sendSock; - - this->running.store(true); - this->thread = std::thread([this] { this->loop(); }); - return 0; + // libev 不允许修改运行中的 watcher,需 stop -> set -> start。 + ev_io_stop(this->evLoop, &iw->io); + iw->events = events; + ev_io_set(&iw->io, fd, toEvEvents(events)); + ev_io_start(this->evLoop, &iw->io); } -void Reactor::stop() { - if (!this->running.exchange(false)) { - if (this->thread.joinable()) { - this->thread.join(); - } +void Reactor::applyDel(int fd) { + auto it = this->ioWatchers.find(fd); + if (it == this->ioWatchers.end()) { return; } - wakeup(); - if (this->thread.joinable()) { - this->thread.join(); - } - if (this->wakeupSend >= 0) { - ::closesocket((SOCKET)this->wakeupSend); - this->wakeupSend = -1; - } - if (this->wakeupRecv >= 0) { - ::closesocket((SOCKET)this->wakeupRecv); - this->wakeupRecv = -1; - } - WSACleanup(); + IoWatcher *iw = (IoWatcher *)it->second; + ev_io_stop(this->evLoop, &iw->io); + this->ioWatchers.erase(it); + delete iw; } -int Reactor::add(int fd, ReactorEvent events, EventHandler handler) { - std::unique_lock lock(this->handlerMutex); - this->handlers[fd] = std::move(handler); - this->interests[fd] = events; - return 0; -} - -int Reactor::mod(int fd, ReactorEvent events) { - std::unique_lock lock(this->handlerMutex); - this->interests[fd] = events; - return 0; -} - -int Reactor::del(int fd) { - std::unique_lock lock(this->handlerMutex); - this->interests.erase(fd); - this->handlers.erase(fd); - return 0; -} +// ===================== 跨线程任务投递 ===================== void Reactor::post(Task task) { { std::unique_lock lock(this->taskMutex); this->tasks.push(std::move(task)); } - wakeup(); -} - -void Reactor::wakeup() { - if (this->wakeupSend < 0) { - return; - } - char one = 1; - ::send((SOCKET)this->wakeupSend, &one, 1, 0); -} - -void Reactor::drainWakeup() { - char buf[256]; - while (::recv((SOCKET)this->wakeupRecv, buf, sizeof(buf), 0) > 0) { - // 持续读空唤醒 socket,避免残留触发空转。 + if (this->asyncWatcher != nullptr) { + ev_async_send(this->evLoop, (ev_async *)this->asyncWatcher); } } void Reactor::drainTasks() { + // swap 批量取出,持锁时间极短;锁外执行,允许 task 内再次 post(不会自死锁)。 std::queue pending; { std::unique_lock lock(this->taskMutex); @@ -585,108 +233,28 @@ void Reactor::drainTasks() { } } -void Reactor::loop() { - spdlog::debug("start thread: netstack reactor"); - std::vector pfds; - std::vector fds; - while (this->running.load()) { - pfds.clear(); - fds.clear(); +// ===================== libev 回调跳板 ===================== - // 唤醒 socket 固定排第一位,只关心可读。 - WSAPOLLFD wake = {}; - wake.fd = (SOCKET)this->wakeupRecv; - wake.events = POLLRDNORM; - pfds.push_back(wake); - fds.push_back(this->wakeupRecv); - - // 快照当前兴趣集,构建 pollfd 数组。 - { - std::unique_lock lock(this->handlerMutex); - for (const auto &kv : this->interests) { - WSAPOLLFD pfd = {}; - pfd.fd = (SOCKET)kv.first; - pfd.events = toWsaEvents(kv.second); - pfds.push_back(pfd); - fds.push_back(kv.first); - } - } - - int n = WSAPoll(pfds.data(), (ULONG)pfds.size(), -1); - if (n < 0) { - int err = WSAGetLastError(); - if (err == WSAEINTR) { - continue; - } - spdlog::error("reactor WSAPoll failed: {}", err); - break; - } - if (n == 0) { - continue; - } - - // 先处理唤醒,再分发 fd 事件。 - if (pfds[0].revents != 0) { - drainWakeup(); - drainTasks(); - } - for (size_t i = 1; i < pfds.size(); ++i) { - if (pfds[i].revents == 0) { - continue; - } - int fd = fds[i]; - EventHandler handler; - { - std::unique_lock lock(this->handlerMutex); - auto it = this->handlers.find(fd); - if (it == this->handlers.end()) { - continue; - } - handler = it->second; - } - handler(fromWsaEvents(pfds[i].revents)); - } - } - spdlog::debug("stop thread: netstack reactor"); -} - -#else - -Reactor::Reactor() : epollFd(-1), wakeupFd(-1), running(false) {} - -Reactor::~Reactor() { - stop(); -} - -int Reactor::start() { - spdlog::error("reactor not implemented on this platform yet"); - return -1; +void Reactor::ioCallback(struct ev_loop *, struct ev_io *w, int revents) { + IoWatcher *iw = (IoWatcher *)w->data; + // 关键:先把 handler 拷贝到局部,再调用。handler 内部可能 del(fd) 释放本 IoWatcher, + // 拷贝后调用可避免 use-after-free;回调返回前不再触碰 iw。 + Reactor::EventHandler handler = iw->handler; + handler(fromEvEvents(revents)); } -void Reactor::stop() {} - -int Reactor::add(int, ReactorEvent, EventHandler) { - return -1; -} - -int Reactor::mod(int, ReactorEvent) { - return -1; +void Reactor::asyncCallback(struct ev_loop *loop, struct ev_async *w, int) { + Reactor *self = (Reactor *)w->data; + self->drainTasks(); + if (!self->running.load()) { + ev_break(loop, EVBREAK_ALL); + } } -int Reactor::del(int) { - return -1; +void Reactor::loop() { + spdlog::debug("start thread: netstack reactor"); + ev_run(this->evLoop, 0); + spdlog::debug("stop thread: netstack reactor"); } -void Reactor::post(Task) {} - -void Reactor::loop() {} - -void Reactor::wakeup() {} - -void Reactor::drainTasks() {} - -void Reactor::drainWakeup() {} - -#endif - } // namespace candy diff --git a/candy/src/netstack/reactor.h b/candy/src/netstack/reactor.h index e8a7a1e1..d34d60fd 100644 --- a/candy/src/netstack/reactor.h +++ b/candy/src/netstack/reactor.h @@ -10,6 +10,11 @@ #include #include +// libev 的不透明类型前向声明,避免在头文件暴露 。 +struct ev_loop; +struct ev_io; +struct ev_async; + namespace candy { enum class ReactorEvent : uint32_t { @@ -27,9 +32,15 @@ inline bool operator&(ReactorEvent a, ReactorEvent b) { return (static_cast(a) & static_cast(b)) != 0; } -// Reactor:落地 fd 的事件循环。Linux 用 epoll + eventfd,macOS 用 kqueue + EVFILT_USER, -// Windows 用 WSAPoll + 自连环回 UDP 唤醒 socket。管理所有落地 fd(内核 socket)的可读/可写/错误事件, -// 非阻塞、事件回调驱动。跨线程提交任务用 post(),由 reactor 线程在事件循环中执行(经唤醒机制)。 +// Reactor:落地 fd 的事件循环。基于 libev(一份代码全平台:Linux=epoll、macOS/iOS=kqueue、 +// Android=epoll、其余=poll/select 兜底),不再手写各平台后端。管理所有落地 fd(内核 socket) +// 的可读/可写/错误事件,非阻塞、事件回调驱动。 +// +// 线程契约: +// - 事件循环跑在 reactor 自己的线程;libev 除 ev_async_send 外均非线程安全。 +// - 跨线程提交任务用 post():入队后 ev_async_send 唤醒 reactor 线程,由其在 async 回调里抽干执行。 +// - add/mod/del 对 libev watcher 的实际操作必须在 reactor 线程执行:若调用方已在 reactor 线程 +// (事件回调内)则直接应用;否则经 post 投递到 reactor 线程应用,保证线程安全。 class Reactor { public: using EventHandler = std::function; @@ -49,33 +60,26 @@ class Reactor { private: void loop(); - void wakeup(); void drainTasks(); - void drainWakeup(); - -#if defined(__linux__) - int epollFd; - int wakeupFd; -#elif defined(__APPLE__) - int kqueueFd; - // kqueue 的 READ/WRITE 为独立 filter,需记录每个 fd 当前兴趣集以做增量增删。 - std::unordered_map interests; -#elif defined(_WIN32) || defined(_WIN64) - // WSAPoll 无内核兴趣集,需在用户态维护 fd->兴趣集,每轮重建 pollfd 数组。 - std::unordered_map interests; - // 自连环回 UDP socket:wakeupRecv 注册进 poll,wakeup() 时往 wakeupSend 发一字节触发可读。 - int wakeupRecv; - int wakeupSend; -#else - int epollFd; - int wakeupFd; -#endif + + bool onLoopThread() const; + // 以下三个 apply* 只在 reactor 线程执行,直接操作 libev watcher 与 ioWatchers 表。 + void applyAdd(int fd, ReactorEvent events, EventHandler handler); + void applyMod(int fd, ReactorEvent events); + void applyDel(int fd); + + // libev 回调跳板(静态成员,C 兼容签名)。 + static void ioCallback(struct ev_loop *loop, struct ev_io *w, int revents); + static void asyncCallback(struct ev_loop *loop, struct ev_async *w, int revents); + + struct ev_loop *evLoop; + void *asyncWatcher; // ev_async*,堆分配 + // fd -> IoWatcher*(IoWatcher 定义于 .cc)。仅 reactor 线程访问,无需加锁。 + std::unordered_map ioWatchers; std::thread thread; std::atomic running; - - std::mutex handlerMutex; - std::unordered_map handlers; + std::thread::id loopThreadId; std::mutex taskMutex; std::queue tasks; diff --git a/candy/src/netstack/session_icmp.cc b/candy/src/netstack/session_icmp.cc index 72c0b65e..69c287ce 100644 --- a/candy/src/netstack/session_icmp.cc +++ b/candy/src/netstack/session_icmp.cc @@ -199,9 +199,13 @@ void SessionIcmp::onFdReadable() { ssize_t n = ::recv(this->fd, buf, sizeof(buf), 0); if (n > 0) { size_t off = 0; - // RAW ICMP socket 收到的是含 IP 头的整包,需剥掉 IP 头取 ICMP; - // DGRAM ICMP socket 收到的直接是 ICMP 报文。 - if (this->rawMode && n >= (ssize_t)sizeof(IP4Header)) { + // 按首字节探测是否带 IP 头,跨平台健壮: + // - RAW ICMP socket(Linux/mac):recv 始终返回含 IP 头的整包; + // - macOS 的 DGRAM ICMP socket:也返回含 IP 头的整包(与 Linux 不同); + // - Linux 的 DGRAM ICMP socket:返回纯 ICMP 报文(首字节=type,echo reply=0)。 + // IPv4 头首字节高 4 位为版本号 4(即 0x4X),而 ICMP echo reply 首字节(type)恒为 0, + // 二者不会混淆,故用 version==4 判定是否需要剥头,避免依赖 rawMode 漏判 mac dgram。 + if (n >= (ssize_t)sizeof(IP4Header) && (((uint8_t *)buf)[0] >> 4) == 4) { uint8_t ihl = (((uint8_t *)buf)[0] & 0x0f) * 4; if ((ssize_t)ihl <= n) { off = ihl; diff --git a/cmake/libev.cmake b/cmake/libev.cmake new file mode 100644 index 00000000..60fed21d --- /dev/null +++ b/cmake/libev.cmake @@ -0,0 +1,76 @@ +# libev:事件循环库(reactor 后端,一份代码全平台)。 +# 采用 vendored 源码编译(third_party/libev),不依赖系统库,保证三平台开箱即用。 +# +# libev 的源码组织很特殊:只需编译 ev.c 一个翻译单元,它在内部按宏条件 +# #include 具体后端(ev_epoll.c / ev_kqueue.c / ev_select.c 等)。因此这里 +# 只把 ev.c 加入编译,并通过 EV_STANDALONE=1 跳过 autotools 的 config.h 探测, +# 改由我们按平台显式指定后端宏。 +# +# 重要陷阱(务必保留这些显式宏): +# 1. EV_STANDALONE 下,epoll 的自动探测条件是 __linux && __GLIBC__, +# 而 Alpine/musl 下 __GLIBC__ 未定义,会退化到 select。故 Linux 必须显式 EV_USE_EPOLL=1。 +# 2. EV_STANDALONE 下 kqueue 默认关闭(EV_USE_KQUEUE 默认 0),故 macOS 必须显式 EV_USE_KQUEUE=1。 +# 3. Windows 用 select 后端,并需 EV_SELECT_IS_WINSOCKET=1 让 select 操作 SOCKET 句柄。 + +set(LIBEV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libev) + +add_library(ev STATIC ${LIBEV_DIR}/ev.c) + +# 仅暴露头文件目录;ev_*.c 由 ev.c 内部 include,不单独编译。 +target_include_directories(ev PUBLIC ${LIBEV_DIR}) + +# 公共:standalone(不读 config.h),关闭用不到的特性以缩小跨平台风险面。 +# reactor 仅用到 ev_io / ev_async / ev_run / ev_break,无需 timer/periodic/signal/child/stat/idle/fork/embed。 +target_compile_definitions(ev PUBLIC + EV_STANDALONE=1 + EV_USE_FLOOR=0 # 不依赖 libm 的 floor,使用 libev 内置近似实现 + EV_PERIODIC_ENABLE=0 + EV_STAT_ENABLE=0 + EV_IDLE_ENABLE=0 + EV_FORK_ENABLE=0 + EV_SIGNAL_ENABLE=0 + EV_CHILD_ENABLE=0 + EV_EMBED_ENABLE=0 + EV_ASYNC_ENABLE=1 # 跨线程唤醒(post)依赖 ev_async +) + +if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Android") + # Linux/Android:epoll 后端。musl 下 __GLIBC__ 未定义,必须显式开启 epoll, + # 否则退化到 select。eventfd 用于 ev_async 唤醒;clock 用 libc 的 clock_gettime + # (musl 自带),关闭 glibc 专属的 clock syscall 路径以兼容 musl。 + target_compile_definitions(ev PRIVATE + EV_USE_EPOLL=1 + EV_USE_SELECT=0 + EV_USE_POLL=0 + EV_USE_EVENTFD=1 + EV_USE_CLOCK_SYSCALL=0 + EV_USE_MONOTONIC=1 + EV_USE_REALTIME=1 + EV_USE_NANOSLEEP=1 + ) +elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR ${CMAKE_SYSTEM_NAME} STREQUAL "iOS") + # macOS/iOS:kqueue 后端(standalone 下默认关闭,必须显式开启)。 + target_compile_definitions(ev PRIVATE + EV_USE_KQUEUE=1 + EV_USE_EPOLL=0 + EV_USE_POLL=0 + EV_USE_SELECT=0 + EV_USE_MONOTONIC=1 + EV_USE_REALTIME=1 + EV_USE_NANOSLEEP=1 + ) +elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") + # Windows:select 后端,select 操作 winsock SOCKET 句柄。 + target_compile_definitions(ev PRIVATE + EV_USE_SELECT=1 + EV_SELECT_IS_WINSOCKET=1 + EV_USE_EPOLL=0 + EV_USE_KQUEUE=0 + EV_USE_POLL=0 + EV_USE_MONOTONIC=1 + EV_USE_REALTIME=1 + ) + target_link_libraries(ev PUBLIC ws2_32) +endif() + +add_library(Candy::ev ALIAS ev) diff --git a/third_party/libev/Changes b/third_party/libev/Changes new file mode 100644 index 00000000..9968527e --- /dev/null +++ b/third_party/libev/Changes @@ -0,0 +1,517 @@ +Revision history for libev, a high-performance and full-featured event loop. + + - ANDROID => __ANDROID__ (reported by enh@google.com). + - disable epoll_create1 on android because it has broken header files + and google is unwilling to fix them (reported by enh@google.com). + +4.24 Wed Dec 28 05:19:55 CET 2016 + - bump version to 4.24, as the release tarball inexplicably + didn't have the right version in ev.h, even though the cvs-tagged + version did have the right one (reported by Ales Teska). + +4.23 Wed Nov 16 18:23:41 CET 2016 + - move some declarations at the beginning to help certain retarded + microsoft compilers, even though their documentation claims + otherwise (reported by Ruslan Osmanov). + +4.22 Sun Dec 20 22:11:50 CET 2015 + - when epoll detects unremovable fds in the fd set, rebuild + only the epoll descriptor, not the signal pipe, to avoid + SIGPIPE in ev_async_send. This doesn't solve it on fork, + so document what needs to be done in ev_loop_fork + (analyzed by Benjamin Mahler). + - remove superfluous sys/timeb.h include on win32 + (analyzed by Jason Madden). + - updated libecb. + +4.20 Sat Jun 20 13:01:43 CEST 2015 + - prefer noexcept over throw () with C++ 11. + - update ecb.h due to incompatibilities with c11. + - fix a potential aliasing issue when reading and writing + watcher callbacks. + +4.19 Thu Sep 25 08:18:25 CEST 2014 + - ev.h wasn't valid C++ anymore, which tripped compilers other than + clang, msvc or gcc (analyzed by Raphael 'kena' Poss). Unfortunately, + C++ doesn't support typedefs for function pointers fully, so the affected + declarations have to spell out the types each time. + - when not using autoconf, tighten the check for clock_gettime and related + functionality. + +4.18 Fri Sep 5 17:55:26 CEST 2014 + - events on files were not always generated properly with the + epoll backend (testcase by Assaf Inbal). + - mark event pipe fd as cloexec after a fork (analyzed by Sami Farin). + - (ecb) support m68k, m88k and sh (patch by Miod Vallat). + - use a reasonable fallback for EV_NSIG instead of erroring out + when we can't detect the signal set size. + - in the absence of autoconf, do not use the clock syscall + on glibc >= 2.17 (avoids the syscall AND -lrt on systems + doing clock_gettime in userspace). + - ensure extern "C" function pointers are used for externally-visible + loop callbacks (not watcher callbacks yet). + - (ecb) work around memory barriers and volatile apparently both being + broken in visual studio 2008 and later (analysed and patch by Nicolas Noble). + +4.15 Fri Mar 1 12:04:50 CET 2013 + - destroying a non-default loop would stop the global waitpid + watcher (Denis Bilenko). + - queueing pending watchers of higher priority from a watcher now invokes + them in a timely fashion (reported by Denis Bilenko). + - add throw() to all libev functions that cannot throw exceptions, for + further code size decrease when compiling for C++. + - add throw () to callbacks that must not throw exceptions (allocator, + syserr, loop acquire/release, periodic reschedule cbs). + - fix event_base_loop return code, add event_get_callback, event_base_new, + event_base_get_method calls to improve libevent 1.x emulation and add + some libevent 2.x functionality (based on a patch by Jeff Davey). + - add more memory fences to fix a bug reported by Jeff Davey. Better + be overfenced than underprotected. + - ev_run now returns a boolean status (true meaning watchers are + still active). + - ev_once: undef EV_ERROR in ev_kqueue.c, to avoid clashing with + libev's EV_ERROR (reported by 191919). + - (ecb) add memory fence support for xlC (Darin McBride). + - (ecb) add memory fence support for gcc-mips (Anton Kirilov). + - (ecb) add memory fence support for gcc-alpha (Christian Weisgerber). + - work around some kernels losing file descriptors by leaking + the kqueue descriptor in the child. + - work around linux inotify not reporting IN_ATTRIB changes for directories + in many cases. + - include sys/syscall.h instead of plain syscall.h. + - check for io watcher loops in ev_verify, check for the most + common reported usage bug in ev_io_start. + - choose socket vs. WSASocket at compiletime using EV_USE_WSASOCKET. + - always use WSASend/WSARecv directly on windows, hoping that this + works in all cases (unlike read/write/send/recv...). + - try to detect signals around a fork faster (test program by + Denis Bilenko). + - work around recent glibc versions that leak memory in realloc. + - rename ev::embed::set to ev::embed::set_embed to avoid clashing + the watcher base set (loop) method. + - rewrite the async/signal pipe logic to always keep a valid fd, which + simplifies (and hopefully correctifies :) the race checking + on fork, at the cost of one extra fd. + - add fat, msdos, jffs2, ramfs, ntfs and btrfs to the list of + inotify-supporting filesystems. + - move orig_CFLAGS assignment to after AC_INIT, as newer autoconf + versions ignore it before + (https://bugzilla.redhat.com/show_bug.cgi?id=908096). + - add some untested android support. + - enum expressions must be of type int (reported by Juan Pablo L). + +4.11 Sat Feb 4 19:52:39 CET 2012 + - INCOMPATIBLE CHANGE: ev_timer_again now clears the pending status, as + was documented already, but not implemented in the repeating case. + - new compiletime symbols: EV_NO_SMP and EV_NO_THREADS. + - fix a race where the workaround against the epoll fork bugs + caused signals to not be handled anymore. + - correct backend_fudge for most backends, and implement a windows + specific workaround to avoid looping because we call both + select and Sleep, both with different time resolutions. + - document range and guarantees of ev_sleep. + - document reasonable ranges for periodics interval and offset. + - rename backend_fudge to backend_mintime to avoid future confusion :) + - change the default periodic reschedule function to hopefully be more + exact and correct even in corner cases or in the far future. + - do not rely on -lm anymore: use it when available but use our + own floor () if it is missing. This should make it easier to embed, + as no external libraries are required. + - strategically import macros from libecb and mark rarely-used functions + as cache-cold (saving almost 2k code size on typical amd64 setups). + - add Symbols.ev and Symbols.event files, that were missing. + - fix backend_mintime value for epoll (was 1/1024, is 1/1000 now). + - fix #3 "be smart about timeouts" to not "deadlock" when + timeout == now, also improve the section overall. + - avoid "AVOIDING FINISHING BEFORE RETURNING" idiom. + - support new EV_API_STATIC mode to make all libev symbols + static. + - supply default CFLAGS of -g -O3 with gcc when original CFLAGS + were empty. + +4.04 Wed Feb 16 09:01:51 CET 2011 + - fix two problems in the native win32 backend, where reuse of fd's + with different underlying handles caused handles not to be removed + or added to the select set (analyzed and tested by Bert Belder). + - do no rely on ceil() in ev_e?poll.c. + - backport libev to HP-UX versions before 11 v3. + - configure did not detect nanosleep and clock_gettime properly when + they are available in the libc (as opposed to -lrt). + +4.03 Tue Jan 11 14:37:25 CET 2011 + - officially support polling files with all backends. + - support files, /dev/zero etc. the same way as select in the epoll + backend, by generating events on our own. + - ports backend: work around solaris bug 6874410 and many related ones + (EINTR, maybe more), with no performance loss (note that the solaris + bug report is actually wrong, reality is far more bizarre and broken + than that). + - define EV_READ/EV_WRITE as macros in event.h, as some programs use + #ifdef to test for them. + - new (experimental) function: ev_feed_signal. + - new (to become default) EVFLAG_NOSIGMASK flag. + - new EVBACKEND_MASK symbol. + - updated COMMON IDIOMS SECTION. + +4.01 Fri Nov 5 21:51:29 CET 2010 + - automake fucked it up, apparently, --add-missing -f is not quite enough + to make it update its files, so 4.00 didn't install ev++.h and + event.h on make install. grrr. + - ev_loop(count|depth) didn't return anything (Robin Haberkorn). + - change EV_UNDEF to 0xffffffff to silence some overzealous compilers. + - use "(libev) " prefix for all libev error messages now. + +4.00 Mon Oct 25 12:32:12 CEST 2010 + - "PORTING FROM LIBEV 3.X TO 4.X" (in ev.pod) is recommended reading. + - ev_embed_stop did not correctly stop the watcher (very good + testcase by Vladimir Timofeev). + - ev_run will now always update the current loop time - it erroneously + didn't when idle watchers were active, causing timers not to fire. + - fix a bug where a timeout of zero caused the timer not to fire + in the libevent emulation (testcase by Péter Szabó). + - applied win32 fixes by Michael Lenaghan (also James Mansion). + - replace EV_MINIMAL by EV_FEATURES. + - prefer EPOLL_CTL_ADD over EPOLL_CTL_MOD in some more cases, as it + seems the former is *much* faster than the latter. + - linux kernel version detection (for inotify bug workarounds) + did not work properly. + - reduce the number of spurious wake-ups with the ports backend. + - remove dependency on sys/queue.h on freebsd (patch by Vanilla Hsu). + - do async init within ev_async_start, not ev_async_set, which avoids + an API quirk where the set function must be called in the C++ API + even when there is nothing to set. + - add (undocumented) EV_ENABLE when adding events with kqueue, + this might help with OS X, which seems to need it despite documenting + not to need it (helpfully pointed out by Tilghman Lesher). + - do not use poll by default on freebsd, it's broken (what isn't + on freebsd...). + - allow to embed epoll on kernels >= 2.6.32. + - configure now prepends -O3, not appends it, so one can still + override it. + - ev.pod: greatly expanded the portability section, added a porting + section, a description of watcher states and made lots of minor fixes. + - disable poll backend on AIX, the poll header spams the namespace + and it's not worth working around dead platforms (reported + and analyzed by Aivars Kalvans). + - improve header file compatibility of the standalone eventfd code + in an obscure case. + - implement EV_AVOID_STDIO option. + - do not use sscanf to parse linux version number (smaller, faster, + no sscanf dependency). + - new EV_CHILD_ENABLE and EV_SIGNAL_ENABLE configurable settings. + - update libev.m4 HAVE_CLOCK_SYSCALL test for newer glibcs. + - add section on accept() problems to the manpage. + - rename EV_TIMEOUT to EV_TIMER. + - rename ev_loop_count/depth/verify/loop/unloop. + - remove ev_default_destroy and ev_default_fork. + - switch to two-digit minor version. + - work around an apparent gentoo compiler bug. + - define _DARWIN_UNLIMITED_SELECT. just so. + - use enum instead of #define for most constants. + - improve compatibility to older C++ compilers. + - (experimental) ev_run/ev_default_loop/ev_break/ev_loop_new have now + default arguments when compiled as C++. + - enable automake dependency tracking. + - ev_loop_new no longer leaks memory when loop creation failed. + - new ev_cleanup watcher type. + +3.9 Thu Dec 31 07:59:59 CET 2009 + - signalfd is no longer used by default and has to be requested + explicitly - this means that easy to catch bugs become hard to + catch race conditions, but the users have spoken. + - point out the unspecified signal mask in the documentation, and + that this is a race condition regardless of EV_SIGNALFD. + - backport inotify code to C89. + - inotify file descriptors could leak into child processes. + - ev_stat watchers could keep an erroneous extra ref on the loop, + preventing exit when unregistering all watchers (testcases + provided by ry@tinyclouds.org). + - implement EV_WIN32_HANDLE_TO_FD and EV_WIN32_CLOSE_FD configuration + symbols to make it easier for apps to do their own fd management. + - support EV_IDLE_ENABLE being disabled in ev++.h + (patch by Didier Spezia). + - take advantage of inotify_init1, if available, to set cloexec/nonblock + on fd creation, to avoid races. + - the signal handling pipe wasn't always initialised under windows + (analysed by lekma). + - changed minimum glibc requirement from glibc 2.9 to 2.7, for + signalfd. + - add missing string.h include (Denis F. Latypoff). + - only replace ev_stat.prev when we detect an actual difference, + so prev is (almost) always different to attr. this might + have caused the problems with 04_stat.t. + - add ev::timer->remaining () method to C++ API. + +3.8 Sun Aug 9 14:30:45 CEST 2009 + - incompatible change: do not necessarily reset signal handler + to SIG_DFL when a sighandler is stopped. + - ev_default_destroy did not properly free or zero some members, + potentially causing crashes and memory corruption on repeated + ev_default_destroy/ev_default_loop calls. + - take advantage of signalfd on GNU/Linux systems. + - document that the signal mask might be in an unspecified + state when using libev's signal handling. + - take advantage of some GNU/Linux calls to set cloexec/nonblock + on fd creation, to avoid race conditions. + +3.7 Fri Jul 17 16:36:32 CEST 2009 + - ev_unloop and ev_loop wrongly used a global variable to exit loops, + instead of using a per-loop variable (bug caught by accident...). + - the ev_set_io_collect_interval interpretation has changed. + - add new functionality: ev_set_userdata, ev_userdata, + ev_set_invoke_pending_cb, ev_set_loop_release_cb, + ev_invoke_pending, ev_pending_count, together with a long example + about thread locking. + - add ev_timer_remaining (as requested by Denis F. Latypoff). + - add ev_loop_depth. + - calling ev_unloop in fork/prepare watchers will no longer poll + for new events. + - Denis F. Latypoff corrected many typos in example code snippets. + - honor autoconf detection of EV_USE_CLOCK_SYSCALL, also double- + check that the syscall number is available before trying to + use it (reported by ry@tinyclouds). + - use GetSystemTimeAsFileTime instead of _timeb on windows, for + slightly higher accuracy. + - properly declare ev_loop_verify and ev_now_update even when + !EV_MULTIPLICITY. + - do not compile in any priority code when EV_MAXPRI == EV_MINPRI. + - support EV_MINIMAL==2 for a reduced API. + - actually 0-initialise struct sigaction when installing signals. + - add section on hibernate and stopped processes to ev_timer docs. + +3.6 Tue Apr 28 02:49:30 CEST 2009 + - multiple timers becoming ready within an event loop iteration + will be invoked in the "correct" order now. + - do not leave the event loop early just because we have no active + watchers, fixing a problem when embedding a kqueue loop + that has active kernel events but no registered watchers + (reported by blacksand blacksand). + - correctly zero the idx values for arrays, so destroying and + reinitialising the default loop actually works (patch by + Malek Hadj-Ali). + - implement ev_suspend and ev_resume. + - new EV_CUSTOM revents flag for use by applications. + - add documentation section about priorities. + - add a glossary to the documentation. + - extend the ev_fork description slightly. + - optimize a jump out of call_pending. + +3.53 Sun Feb 15 02:38:20 CET 2009 + - fix a bug in event pipe creation on win32 that would cause a + failed assertion on event loop creation (patch by Malek Hadj-Ali). + - probe for CLOCK_REALTIME support at runtime as well and fall + back to gettimeofday if there is an error, to support older + operating systems with newer header files/libraries. + - prefer gettimeofday over clock_gettime with USE_CLOCK_SYSCALL + (default most everywhere), otherwise not. + +3.52 Wed Jan 7 21:43:02 CET 2009 + - fix compilation of select backend in fd_set mode when NFDBITS is + missing (to get it to compile on QNX, reported by Rodrigo Campos). + - better select-nfds handling when select backend is in fd_set mode. + - diagnose fd_set overruns when select backend is in fd_set mode. + - due to a thinko, instead of disabling everything but + select on the borked OS X platform, everything but select was + allowed (reported by Emanuele Giaquinta). + - actually verify that local and remote port are matching in + libev's socketpair emulation, which makes denial-of-service + attacks harder (but not impossible - it's windows). Make sure + it even works under vista, which thinks that getpeer/sockname + should return fantasy port numbers. + - include "libev" in all assertion messages for potentially + clearer diagnostics. + - event_get_version (libevent compatibility) returned + a useless string instead of the expected version string + (patch by W.C.A. Wijngaards). + +3.51 Wed Dec 24 23:00:11 CET 2008 + - fix a bug where an inotify watcher was added twice, causing + freezes on hash collisions (reported and analysed by Graham Leggett). + - new config symbol, EV_USE_CLOCK_SYSCALL, to make libev use + a direct syscall - slower, but no dependency on librt et al. + - assume negative return values != -1 signals success of port_getn + (http://cvs.epicsol.org/cgi/viewcvs.cgi/epic5/source/newio.c?rev=1.52) + (no known failure reports, but it doesn't hurt). + - fork detection in ev_embed now stops and restarts the watcher + automatically. + - EXPERIMENTAL: default the method to operator () in ev++.h, + to make it nicer to use functors (requested by Benedek László). + - fixed const object callbacks in ev++.h. + - replaced loop_ref argument of watcher.set (loop) by a direct + ev_loop * in ev++.h, to avoid clashes with functor patch. + - do not try to watch the empty string via inotify. + - inotify watchers could be leaked under certain circumstances. + - OS X 10.5 is actually even more broken than earlier versions, + so fall back to select on that piece of garbage. + - fixed some weirdness in the ev_embed documentation. + +3.49 Wed Nov 19 11:26:53 CET 2008 + - ev_stat watchers will now use inotify as a mere hint on + kernels <2.6.25, or if the filesystem is not in the + "known to be good" list. + - better mingw32 compatibility (it's not as borked as native win32) + (analysed by Roger Pack). + - include stdio.h in the example program, as too many people are + confused by the weird C language otherwise. I guess the next thing + I get told is that the "..." ellipses in the examples don't compile + with their C compiler. + +3.48 Thu Oct 30 09:02:37 CET 2008 + - further optimise away the EPOLL_CTL_ADD/MOD combo in the epoll + backend by assuming the kernel event mask hasn't changed if + ADD fails with EEXIST. + - work around spurious event notification bugs in epoll by using + a 32-bit generation counter. recreate kernel state if we receive + spurious notifications or unwanted events. this is very costly, + but I didn't come up with this horrible design. + - use memset to initialise most arrays now and do away with the + init functions. + - expand time-out strategies into a "Be smart about timeouts" section. + - drop the "struct" from all ev_watcher declarations in the + documentation and did other clarifications (yeah, it was a mistake + to have a struct AND a function called ev_loop). + - fix a bug where ev_default would not initialise the default + loop again after it was destroyed with ev_default_destroy. + - rename syserr to ev_syserr to avoid name clashes when embedding, + do similar changes for event.c. + +3.45 Tue Oct 21 21:59:26 CEST 2008 + - disable inotify usage on linux <2.6.25, as it is broken + (reported by Yoann Vandoorselaere). + - ev_stat erroneously would try to add inotify watchers + even when inotify wasn't available (this should only + have a performance impact). + - ev_once now passes both timeout and io to the callback if both + occur concurrently, instead of giving timeouts precedence. + - disable EV_USE_INOTIFY when sys/inotify.h is too old. + +3.44 Mon Sep 29 05:18:39 CEST 2008 + - embed watchers now automatically invoke ev_loop_fork on the + embedded loop when the parent loop forks. + - new function: ev_now_update (loop). + - verify_watcher was not marked static. + - improve the "associating..." manpage section. + - documentation tweaks here and there. + +3.43 Sun Jul 6 05:34:41 CEST 2008 + - include more include files on windows to get struct _stati64 + (reported by Chris Hulbert, but doesn't quite fix his issue). + - add missing #include in ev.c on windows (reported by + Matt Tolton). + +3.42 Tue Jun 17 12:12:07 CEST 2008 + - work around yet another windows bug: FD_SET actually adds fd's + multiple times to the fd_*SET*, despite official MSN docs claiming + otherwise. Reported and well-analysed by Matt Tolton. + - define NFDBITS to 0 when EV_SELECT_IS_WINSOCKET to make it compile + (reported any analysed by Chris Hulbert). + - fix a bug in ev_ebadf (this function is only used to catch + programming errors in the libev user). reported by Matt Tolton. + - fix a bug in fd_intern on win32 (could lead to compile errors + under some circumstances, but would work correctly if it compiles). + reported by Matt Tolton. + - (try to) work around missing lstat on windows. + - pass in the write fd set as except fd set under windows. windows + is so uncontrollably lame that it requires this. this means that + switching off oobinline is not supported (but tcp/ip doesn't + have oob, so that would be stupid anyways. + - use posix module symbol to auto-detect monotonic clock presence + and some other default values. + +3.41 Fri May 23 18:42:54 CEST 2008 + - work around an obscure bug in winsocket select: if you + provide only empty fd sets then select returns WSAEINVAL. how sucky. + - improve timer scheduling stability and reduce use of time_epsilon. + - use 1-based 2-heap for EV_MINIMAL, simplifies code, reduces + codesize and makes for better cache-efficiency. + - use 3-based 4-heap for !EV_MINIMAL. this makes better use + of cpu cache lines and gives better growth behaviour than + 2-based heaps. + - cache timestamp within heap for !EV_MINIMAL, to avoid random + memory accesses. + - document/add EV_USE_4HEAP and EV_HEAP_CACHE_AT. + - fix a potential aliasing issue in ev_timer_again. + - add/document ev_periodic_at, retract direct access to ->at. + - improve ev_stat docs. + - add portability requirements section. + - fix manpage headers etc. + - normalise WSA error codes to lower range on windows. + - add consistency check code that can be called automatically + or on demand to check for internal structures (ev_loop_verify). + +3.31 Wed Apr 16 20:45:04 CEST 2008 + - added last minute fix for ev_poll.c by Brandon Black. + +3.3 Wed Apr 16 19:04:10 CEST 2008 + - event_base_loopexit should return 0 on success + (W.C.A. Wijngaards). + - added linux eventfd support. + - try to autodetect epoll and inotify support + by libc header version if not using autoconf. + - new symbols: EV_DEFAULT_UC and EV_DEFAULT_UC_. + - declare functions defined in ev.h as inline if + C99 or gcc are available. + - enable inlining with gcc versions 2 and 3. + - work around broken poll implementations potentially + not clearing revents field in ev_poll (Brandon Black) + (no such systems are known at this time). + - work around a bug in realloc on openbsd and darwin, + also makes the erroneous valgrind complaints + go away (noted by various people). + - fix ev_async_pending, add c++ wrapper for ev_async + (based on patch sent by Johannes Deisenhofer). + - add sensible set method to ev::embed. + - made integer constants type int in ev.h. + +3.2 Wed Apr 2 17:11:19 CEST 2008 + - fix a 64 bit overflow issue in the select backend, + by using fd_mask instead of int for the mask. + - rename internal sighandler to avoid clash with very old perls. + - entering ev_loop will not clear the ONESHOT or NONBLOCKING + flags of any outer loops anymore. + - add ev_async_pending. + +3.1 Thu Mar 13 13:45:22 CET 2008 + - implement ev_async watchers. + - only initialise signal pipe on demand. + - make use of sig_atomic_t configurable. + - improved documentation. + +3.0 Mon Jan 28 13:14:47 CET 2008 + - API/ABI bump to version 3.0. + - ev++.h includes "ev.h" by default now, not . + - slightly improved documentation. + - speed up signal detection after a fork. + - only optionally return trace status changed in ev_child + watchers. + - experimental (and undocumented) loop wrappers for ev++.h. + +2.01 Tue Dec 25 08:04:41 CET 2007 + - separate Changes file. + - fix ev_path_set => ev_stat_set typo. + - remove event_compat.h from the libev tarball. + - change how include files are found. + - doc updates. + - update licenses, explicitly allow for GPL relicensing. + +2.0 Sat Dec 22 17:47:03 CET 2007 + - new ev_sleep, ev_set_(io|timeout)_collect_interval. + - removed epoll from embeddable fd set. + - fix embed watchers. + - renamed ev_embed.loop to other. + - added exported Symbol tables. + - undefine member wrapper macros at the end of ev.c. + - respect EV_H in ev++.h. + +1.86 Tue Dec 18 02:36:57 CET 2007 + - fix memleak on loop destroy (not relevant for perl). + +1.85 Fri Dec 14 20:32:40 CET 2007 + - fix some aliasing issues w.r.t. timers and periodics + (not relevant for perl). + +(for historic versions refer to EV/Changes, found in the Perl interface) + +0.1 Wed Oct 31 21:31:48 CET 2007 + - original version; hacked together in <24h. + diff --git a/third_party/libev/LICENSE b/third_party/libev/LICENSE new file mode 100644 index 00000000..2fdabd48 --- /dev/null +++ b/third_party/libev/LICENSE @@ -0,0 +1,37 @@ +All files in libev are +Copyright (c)2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Alternatively, the contents of this package may be used under the terms +of the GNU General Public License ("GPL") version 2 or any later version, +in which case the provisions of the GPL are applicable instead of the +above. If you wish to allow the use of your version of this package only +under the terms of the GPL and not to allow others to use your version of +this file under the BSD license, indicate your decision by deleting the +provisions above and replace them with the notice and other provisions +required by the GPL in this and the other files of this package. If you do +not delete the provisions above, a recipient may use your version of this +file under either the BSD or the GPL. diff --git a/third_party/libev/README b/third_party/libev/README new file mode 100644 index 00000000..31f61938 --- /dev/null +++ b/third_party/libev/README @@ -0,0 +1,58 @@ +libev is a high-performance event loop/event model with lots of features. +(see benchmark at http://libev.schmorp.de/bench.html) + + +ABOUT + + Homepage: http://software.schmorp.de/pkg/libev + Mailinglist: libev@lists.schmorp.de + http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev + Library Documentation: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod + + Libev is modelled (very losely) after libevent and the Event perl + module, but is faster, scales better and is more correct, and also more + featureful. And also smaller. Yay. + + Some of the specialties of libev not commonly found elsewhere are: + + - extensive and detailed, readable documentation (not doxygen garbage). + - fully supports fork, can detect fork in various ways and automatically + re-arms kernel mechanisms that do not support fork. + - highly optimised select, poll, epoll, kqueue and event ports backends. + - filesystem object (path) watching (with optional linux inotify support). + - wallclock-based times (using absolute time, cron-like). + - relative timers/timeouts (handle time jumps). + - fast intra-thread communication between multiple + event loops (with optional fast linux eventfd backend). + - extremely easy to embed (fully documented, no dependencies, + autoconf supported but optional). + - very small codebase, no bloated library, simple code. + - fully extensible by being able to plug into the event loop, + integrate other event loops, integrate other event loop users. + - very little memory use (small watchers, small event loop data). + - optional C++ interface allowing method and function callbacks + at no extra memory or runtime overhead. + - optional Perl interface with similar characteristics (capable + of running Glib/Gtk2 on libev). + - support for other languages (multiple C++ interfaces, D, Ruby, + Python) available from third-parties. + + Examples of programs that embed libev: the EV perl module, node.js, + auditd, rxvt-unicode, gvpe (GNU Virtual Private Ethernet), the + Deliantra MMORPG server (http://www.deliantra.net/), Rubinius (a + next-generation Ruby VM), the Ebb web server, the Rev event toolkit. + + +CONTRIBUTORS + + libev was written and designed by Marc Lehmann and Emanuele Giaquinta. + + The following people sent in patches or made other noteworthy + contributions to the design (for minor patches, see the Changes + file. If I forgot to include you, please shout at me, it was an + accident): + + W.C.A. Wijngaards + Christopher Layne + Chris Brody + diff --git a/third_party/libev/ev++.h b/third_party/libev/ev++.h new file mode 100644 index 00000000..c9b453bf --- /dev/null +++ b/third_party/libev/ev++.h @@ -0,0 +1,816 @@ +/* + * libev simple C++ wrapper classes + * + * Copyright (c) 2007,2008,2010 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#ifndef EVPP_H__ +#define EVPP_H__ + +#ifdef EV_H +# include EV_H +#else +# include "ev.h" +#endif + +#ifndef EV_USE_STDEXCEPT +# define EV_USE_STDEXCEPT 1 +#endif + +#if EV_USE_STDEXCEPT +# include +#endif + +namespace ev { + + typedef ev_tstamp tstamp; + + enum { + UNDEF = EV_UNDEF, + NONE = EV_NONE, + READ = EV_READ, + WRITE = EV_WRITE, +#if EV_COMPAT3 + TIMEOUT = EV_TIMEOUT, +#endif + TIMER = EV_TIMER, + PERIODIC = EV_PERIODIC, + SIGNAL = EV_SIGNAL, + CHILD = EV_CHILD, + STAT = EV_STAT, + IDLE = EV_IDLE, + CHECK = EV_CHECK, + PREPARE = EV_PREPARE, + FORK = EV_FORK, + ASYNC = EV_ASYNC, + EMBED = EV_EMBED, +# undef ERROR // some systems stupidly #define ERROR + ERROR = EV_ERROR + }; + + enum + { + AUTO = EVFLAG_AUTO, + NOENV = EVFLAG_NOENV, + FORKCHECK = EVFLAG_FORKCHECK, + + SELECT = EVBACKEND_SELECT, + POLL = EVBACKEND_POLL, + EPOLL = EVBACKEND_EPOLL, + KQUEUE = EVBACKEND_KQUEUE, + DEVPOLL = EVBACKEND_DEVPOLL, + PORT = EVBACKEND_PORT + }; + + enum + { +#if EV_COMPAT3 + NONBLOCK = EVLOOP_NONBLOCK, + ONESHOT = EVLOOP_ONESHOT, +#endif + NOWAIT = EVRUN_NOWAIT, + ONCE = EVRUN_ONCE + }; + + enum how_t + { + ONE = EVBREAK_ONE, + ALL = EVBREAK_ALL + }; + + struct bad_loop +#if EV_USE_STDEXCEPT + : std::runtime_error +#endif + { +#if EV_USE_STDEXCEPT + bad_loop () + : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?") + { + } +#endif + }; + +#ifdef EV_AX +# undef EV_AX +#endif + +#ifdef EV_AX_ +# undef EV_AX_ +#endif + +#if EV_MULTIPLICITY +# define EV_AX raw_loop +# define EV_AX_ raw_loop, +#else +# define EV_AX +# define EV_AX_ +#endif + + struct loop_ref + { + loop_ref (EV_P) throw () +#if EV_MULTIPLICITY + : EV_AX (EV_A) +#endif + { + } + + bool operator == (const loop_ref &other) const throw () + { +#if EV_MULTIPLICITY + return EV_AX == other.EV_AX; +#else + return true; +#endif + } + + bool operator != (const loop_ref &other) const throw () + { +#if EV_MULTIPLICITY + return ! (*this == other); +#else + return false; +#endif + } + +#if EV_MULTIPLICITY + bool operator == (const EV_P) const throw () + { + return this->EV_AX == EV_A; + } + + bool operator != (const EV_P) const throw () + { + return ! (*this == EV_A); + } + + operator struct ev_loop * () const throw () + { + return EV_AX; + } + + operator const struct ev_loop * () const throw () + { + return EV_AX; + } + + bool is_default () const throw () + { + return EV_AX == ev_default_loop (0); + } +#endif + +#if EV_COMPAT3 + void loop (int flags = 0) + { + ev_run (EV_AX_ flags); + } + + void unloop (how_t how = ONE) throw () + { + ev_break (EV_AX_ how); + } +#endif + + void run (int flags = 0) + { + ev_run (EV_AX_ flags); + } + + void break_loop (how_t how = ONE) throw () + { + ev_break (EV_AX_ how); + } + + void post_fork () throw () + { + ev_loop_fork (EV_AX); + } + + unsigned int backend () const throw () + { + return ev_backend (EV_AX); + } + + tstamp now () const throw () + { + return ev_now (EV_AX); + } + + void ref () throw () + { + ev_ref (EV_AX); + } + + void unref () throw () + { + ev_unref (EV_AX); + } + +#if EV_FEATURE_API + unsigned int iteration () const throw () + { + return ev_iteration (EV_AX); + } + + unsigned int depth () const throw () + { + return ev_depth (EV_AX); + } + + void set_io_collect_interval (tstamp interval) throw () + { + ev_set_io_collect_interval (EV_AX_ interval); + } + + void set_timeout_collect_interval (tstamp interval) throw () + { + ev_set_timeout_collect_interval (EV_AX_ interval); + } +#endif + + // function callback + void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) throw () + { + ev_once (EV_AX_ fd, events, timeout, cb, arg); + } + + // method callback + template + void once (int fd, int events, tstamp timeout, K *object) throw () + { + once (fd, events, timeout, method_thunk, object); + } + + // default method == operator () + template + void once (int fd, int events, tstamp timeout, K *object) throw () + { + once (fd, events, timeout, method_thunk, object); + } + + template + static void method_thunk (int revents, void *arg) + { + (static_cast(arg)->*method) + (revents); + } + + // no-argument method callback + template + void once (int fd, int events, tstamp timeout, K *object) throw () + { + once (fd, events, timeout, method_noargs_thunk, object); + } + + template + static void method_noargs_thunk (int revents, void *arg) + { + (static_cast(arg)->*method) + (); + } + + // simpler function callback + template + void once (int fd, int events, tstamp timeout) throw () + { + once (fd, events, timeout, simpler_func_thunk); + } + + template + static void simpler_func_thunk (int revents, void *arg) + { + (*cb) + (revents); + } + + // simplest function callback + template + void once (int fd, int events, tstamp timeout) throw () + { + once (fd, events, timeout, simplest_func_thunk); + } + + template + static void simplest_func_thunk (int revents, void *arg) + { + (*cb) + (); + } + + void feed_fd_event (int fd, int revents) throw () + { + ev_feed_fd_event (EV_AX_ fd, revents); + } + + void feed_signal_event (int signum) throw () + { + ev_feed_signal_event (EV_AX_ signum); + } + +#if EV_MULTIPLICITY + struct ev_loop* EV_AX; +#endif + + }; + +#if EV_MULTIPLICITY + struct dynamic_loop : loop_ref + { + + dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) + : loop_ref (ev_loop_new (flags)) + { + if (!EV_AX) + throw bad_loop (); + } + + ~dynamic_loop () throw () + { + ev_loop_destroy (EV_AX); + EV_AX = 0; + } + + private: + + dynamic_loop (const dynamic_loop &); + + dynamic_loop & operator= (const dynamic_loop &); + + }; +#endif + + struct default_loop : loop_ref + { + default_loop (unsigned int flags = AUTO) throw (bad_loop) +#if EV_MULTIPLICITY + : loop_ref (ev_default_loop (flags)) +#endif + { + if ( +#if EV_MULTIPLICITY + !EV_AX +#else + !ev_default_loop (flags) +#endif + ) + throw bad_loop (); + } + + private: + default_loop (const default_loop &); + default_loop &operator = (const default_loop &); + }; + + inline loop_ref get_default_loop () throw () + { +#if EV_MULTIPLICITY + return ev_default_loop (0); +#else + return loop_ref (); +#endif + } + +#undef EV_AX +#undef EV_AX_ + +#undef EV_PX +#undef EV_PX_ +#if EV_MULTIPLICITY +# define EV_PX loop_ref EV_A +# define EV_PX_ loop_ref EV_A_ +#else +# define EV_PX +# define EV_PX_ +#endif + + template + struct base : ev_watcher + { + #if EV_MULTIPLICITY + EV_PX; + + // loop set + void set (EV_P) throw () + { + this->EV_A = EV_A; + } + #endif + + base (EV_PX) throw () + #if EV_MULTIPLICITY + : EV_A (EV_A) + #endif + { + ev_init (this, 0); + } + + void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () + { + this->data = (void *)data; + ev_set_cb (static_cast(this), cb); + } + + // function callback + template + void set (void *data = 0) throw () + { + set_ (data, function_thunk); + } + + template + static void function_thunk (EV_P_ ev_watcher *w, int revents) + { + function + (*static_cast(w), revents); + } + + // method callback + template + void set (K *object) throw () + { + set_ (object, method_thunk); + } + + // default method == operator () + template + void set (K *object) throw () + { + set_ (object, method_thunk); + } + + template + static void method_thunk (EV_P_ ev_watcher *w, int revents) + { + (static_cast(w->data)->*method) + (*static_cast(w), revents); + } + + // no-argument callback + template + void set (K *object) throw () + { + set_ (object, method_noargs_thunk); + } + + template + static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) + { + (static_cast(w->data)->*method) + (); + } + + void operator ()(int events = EV_UNDEF) + { + return + ev_cb (static_cast(this)) + (static_cast(this), events); + } + + bool is_active () const throw () + { + return ev_is_active (static_cast(this)); + } + + bool is_pending () const throw () + { + return ev_is_pending (static_cast(this)); + } + + void feed_event (int revents) throw () + { + ev_feed_event (EV_A_ static_cast(this), revents); + } + }; + + inline tstamp now (EV_P) throw () + { + return ev_now (EV_A); + } + + inline void delay (tstamp interval) throw () + { + ev_sleep (interval); + } + + inline int version_major () throw () + { + return ev_version_major (); + } + + inline int version_minor () throw () + { + return ev_version_minor (); + } + + inline unsigned int supported_backends () throw () + { + return ev_supported_backends (); + } + + inline unsigned int recommended_backends () throw () + { + return ev_recommended_backends (); + } + + inline unsigned int embeddable_backends () throw () + { + return ev_embeddable_backends (); + } + + inline void set_allocator (void *(*cb)(void *ptr, long size) throw ()) throw () + { + ev_set_allocator (cb); + } + + inline void set_syserr_cb (void (*cb)(const char *msg) throw ()) throw () + { + ev_set_syserr_cb (cb); + } + + #if EV_MULTIPLICITY + #define EV_CONSTRUCT(cppstem,cstem) \ + (EV_PX = get_default_loop ()) throw () \ + : base (EV_A) \ + { \ + } + #else + #define EV_CONSTRUCT(cppstem,cstem) \ + () throw () \ + { \ + } + #endif + + /* using a template here would require quite a few more lines, + * so a macro solution was chosen */ + #define EV_BEGIN_WATCHER(cppstem,cstem) \ + \ + struct cppstem : base \ + { \ + void start () throw () \ + { \ + ev_ ## cstem ## _start (EV_A_ static_cast(this)); \ + } \ + \ + void stop () throw () \ + { \ + ev_ ## cstem ## _stop (EV_A_ static_cast(this)); \ + } \ + \ + cppstem EV_CONSTRUCT(cppstem,cstem) \ + \ + ~cppstem () throw () \ + { \ + stop (); \ + } \ + \ + using base::set; \ + \ + private: \ + \ + cppstem (const cppstem &o); \ + \ + cppstem &operator =(const cppstem &o); \ + \ + public: + + #define EV_END_WATCHER(cppstem,cstem) \ + }; + + EV_BEGIN_WATCHER (io, io) + void set (int fd, int events) throw () + { + int active = is_active (); + if (active) stop (); + ev_io_set (static_cast(this), fd, events); + if (active) start (); + } + + void set (int events) throw () + { + int active = is_active (); + if (active) stop (); + ev_io_set (static_cast(this), fd, events); + if (active) start (); + } + + void start (int fd, int events) throw () + { + set (fd, events); + start (); + } + EV_END_WATCHER (io, io) + + EV_BEGIN_WATCHER (timer, timer) + void set (ev_tstamp after, ev_tstamp repeat = 0.) throw () + { + int active = is_active (); + if (active) stop (); + ev_timer_set (static_cast(this), after, repeat); + if (active) start (); + } + + void start (ev_tstamp after, ev_tstamp repeat = 0.) throw () + { + set (after, repeat); + start (); + } + + void again () throw () + { + ev_timer_again (EV_A_ static_cast(this)); + } + + ev_tstamp remaining () + { + return ev_timer_remaining (EV_A_ static_cast(this)); + } + EV_END_WATCHER (timer, timer) + + #if EV_PERIODIC_ENABLE + EV_BEGIN_WATCHER (periodic, periodic) + void set (ev_tstamp at, ev_tstamp interval = 0.) throw () + { + int active = is_active (); + if (active) stop (); + ev_periodic_set (static_cast(this), at, interval, 0); + if (active) start (); + } + + void start (ev_tstamp at, ev_tstamp interval = 0.) throw () + { + set (at, interval); + start (); + } + + void again () throw () + { + ev_periodic_again (EV_A_ static_cast(this)); + } + EV_END_WATCHER (periodic, periodic) + #endif + + #if EV_SIGNAL_ENABLE + EV_BEGIN_WATCHER (sig, signal) + void set (int signum) throw () + { + int active = is_active (); + if (active) stop (); + ev_signal_set (static_cast(this), signum); + if (active) start (); + } + + void start (int signum) throw () + { + set (signum); + start (); + } + EV_END_WATCHER (sig, signal) + #endif + + #if EV_CHILD_ENABLE + EV_BEGIN_WATCHER (child, child) + void set (int pid, int trace = 0) throw () + { + int active = is_active (); + if (active) stop (); + ev_child_set (static_cast(this), pid, trace); + if (active) start (); + } + + void start (int pid, int trace = 0) throw () + { + set (pid, trace); + start (); + } + EV_END_WATCHER (child, child) + #endif + + #if EV_STAT_ENABLE + EV_BEGIN_WATCHER (stat, stat) + void set (const char *path, ev_tstamp interval = 0.) throw () + { + int active = is_active (); + if (active) stop (); + ev_stat_set (static_cast(this), path, interval); + if (active) start (); + } + + void start (const char *path, ev_tstamp interval = 0.) throw () + { + stop (); + set (path, interval); + start (); + } + + void update () throw () + { + ev_stat_stat (EV_A_ static_cast(this)); + } + EV_END_WATCHER (stat, stat) + #endif + + #if EV_IDLE_ENABLE + EV_BEGIN_WATCHER (idle, idle) + void set () throw () { } + EV_END_WATCHER (idle, idle) + #endif + + #if EV_PREPARE_ENABLE + EV_BEGIN_WATCHER (prepare, prepare) + void set () throw () { } + EV_END_WATCHER (prepare, prepare) + #endif + + #if EV_CHECK_ENABLE + EV_BEGIN_WATCHER (check, check) + void set () throw () { } + EV_END_WATCHER (check, check) + #endif + + #if EV_EMBED_ENABLE + EV_BEGIN_WATCHER (embed, embed) + void set_embed (struct ev_loop *embedded_loop) throw () + { + int active = is_active (); + if (active) stop (); + ev_embed_set (static_cast(this), embedded_loop); + if (active) start (); + } + + void start (struct ev_loop *embedded_loop) throw () + { + set (embedded_loop); + start (); + } + + void sweep () + { + ev_embed_sweep (EV_A_ static_cast(this)); + } + EV_END_WATCHER (embed, embed) + #endif + + #if EV_FORK_ENABLE + EV_BEGIN_WATCHER (fork, fork) + void set () throw () { } + EV_END_WATCHER (fork, fork) + #endif + + #if EV_ASYNC_ENABLE + EV_BEGIN_WATCHER (async, async) + void send () throw () + { + ev_async_send (EV_A_ static_cast(this)); + } + + bool async_pending () throw () + { + return ev_async_pending (static_cast(this)); + } + EV_END_WATCHER (async, async) + #endif + + #undef EV_PX + #undef EV_PX_ + #undef EV_CONSTRUCT + #undef EV_BEGIN_WATCHER + #undef EV_END_WATCHER +} + +#endif + diff --git a/third_party/libev/ev.c b/third_party/libev/ev.c new file mode 100644 index 00000000..fe03dac1 --- /dev/null +++ b/third_party/libev/ev.c @@ -0,0 +1,5144 @@ +/* + * libev event processing core, watcher management + * + * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +/* this big block deduces configuration from config.h */ +#ifndef EV_STANDALONE +# ifdef EV_CONFIG_H +# include EV_CONFIG_H +# else +# include "config.h" +# endif + +# if HAVE_FLOOR +# ifndef EV_USE_FLOOR +# define EV_USE_FLOOR 1 +# endif +# endif + +# if HAVE_CLOCK_SYSCALL +# ifndef EV_USE_CLOCK_SYSCALL +# define EV_USE_CLOCK_SYSCALL 1 +# ifndef EV_USE_REALTIME +# define EV_USE_REALTIME 0 +# endif +# ifndef EV_USE_MONOTONIC +# define EV_USE_MONOTONIC 1 +# endif +# endif +# elif !defined EV_USE_CLOCK_SYSCALL +# define EV_USE_CLOCK_SYSCALL 0 +# endif + +# if HAVE_CLOCK_GETTIME +# ifndef EV_USE_MONOTONIC +# define EV_USE_MONOTONIC 1 +# endif +# ifndef EV_USE_REALTIME +# define EV_USE_REALTIME 0 +# endif +# else +# ifndef EV_USE_MONOTONIC +# define EV_USE_MONOTONIC 0 +# endif +# ifndef EV_USE_REALTIME +# define EV_USE_REALTIME 0 +# endif +# endif + +# if HAVE_NANOSLEEP +# ifndef EV_USE_NANOSLEEP +# define EV_USE_NANOSLEEP EV_FEATURE_OS +# endif +# else +# undef EV_USE_NANOSLEEP +# define EV_USE_NANOSLEEP 0 +# endif + +# if HAVE_SELECT && HAVE_SYS_SELECT_H +# ifndef EV_USE_SELECT +# define EV_USE_SELECT EV_FEATURE_BACKENDS +# endif +# else +# undef EV_USE_SELECT +# define EV_USE_SELECT 0 +# endif + +# if HAVE_POLL && HAVE_POLL_H +# ifndef EV_USE_POLL +# define EV_USE_POLL EV_FEATURE_BACKENDS +# endif +# else +# undef EV_USE_POLL +# define EV_USE_POLL 0 +# endif + +# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H +# ifndef EV_USE_EPOLL +# define EV_USE_EPOLL EV_FEATURE_BACKENDS +# endif +# else +# undef EV_USE_EPOLL +# define EV_USE_EPOLL 0 +# endif + +# if HAVE_KQUEUE && HAVE_SYS_EVENT_H +# ifndef EV_USE_KQUEUE +# define EV_USE_KQUEUE EV_FEATURE_BACKENDS +# endif +# else +# undef EV_USE_KQUEUE +# define EV_USE_KQUEUE 0 +# endif + +# if HAVE_PORT_H && HAVE_PORT_CREATE +# ifndef EV_USE_PORT +# define EV_USE_PORT EV_FEATURE_BACKENDS +# endif +# else +# undef EV_USE_PORT +# define EV_USE_PORT 0 +# endif + +# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H +# ifndef EV_USE_INOTIFY +# define EV_USE_INOTIFY EV_FEATURE_OS +# endif +# else +# undef EV_USE_INOTIFY +# define EV_USE_INOTIFY 0 +# endif + +# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H +# ifndef EV_USE_SIGNALFD +# define EV_USE_SIGNALFD EV_FEATURE_OS +# endif +# else +# undef EV_USE_SIGNALFD +# define EV_USE_SIGNALFD 0 +# endif + +# if HAVE_EVENTFD +# ifndef EV_USE_EVENTFD +# define EV_USE_EVENTFD EV_FEATURE_OS +# endif +# else +# undef EV_USE_EVENTFD +# define EV_USE_EVENTFD 0 +# endif + +#endif + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include + +#ifdef EV_H +# include EV_H +#else +# include "ev.h" +#endif + +#if EV_NO_THREADS +# undef EV_NO_SMP +# define EV_NO_SMP 1 +# undef ECB_NO_THREADS +# define ECB_NO_THREADS 1 +#endif +#if EV_NO_SMP +# undef EV_NO_SMP +# define ECB_NO_SMP 1 +#endif + +#ifndef _WIN32 +# include +# include +# include +#else +# include +# define WIN32_LEAN_AND_MEAN +# include +# include +# ifndef EV_SELECT_IS_WINSOCKET +# define EV_SELECT_IS_WINSOCKET 1 +# endif +# undef EV_AVOID_STDIO +#endif + +/* OS X, in its infinite idiocy, actually HARDCODES + * a limit of 1024 into their select. Where people have brains, + * OS X engineers apparently have a vacuum. Or maybe they were + * ordered to have a vacuum, or they do anything for money. + * This might help. Or not. + */ +#define _DARWIN_UNLIMITED_SELECT 1 + +/* this block tries to deduce configuration from header-defined symbols and defaults */ + +/* try to deduce the maximum number of signals on this platform */ +#if defined EV_NSIG +/* use what's provided */ +#elif defined NSIG +# define EV_NSIG (NSIG) +#elif defined _NSIG +# define EV_NSIG (_NSIG) +#elif defined SIGMAX +# define EV_NSIG (SIGMAX+1) +#elif defined SIG_MAX +# define EV_NSIG (SIG_MAX+1) +#elif defined _SIG_MAX +# define EV_NSIG (_SIG_MAX+1) +#elif defined MAXSIG +# define EV_NSIG (MAXSIG+1) +#elif defined MAX_SIG +# define EV_NSIG (MAX_SIG+1) +#elif defined SIGARRAYSIZE +# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ +#elif defined _sys_nsig +# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ +#else +# define EV_NSIG (8 * sizeof (sigset_t) + 1) +#endif + +#ifndef EV_USE_FLOOR +# define EV_USE_FLOOR 0 +#endif + +#ifndef EV_USE_CLOCK_SYSCALL +# if __linux && __GLIBC__ == 2 && __GLIBC_MINOR__ < 17 +# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS +# else +# define EV_USE_CLOCK_SYSCALL 0 +# endif +#endif + +#if !(_POSIX_TIMERS > 0) +# ifndef EV_USE_MONOTONIC +# define EV_USE_MONOTONIC 0 +# endif +# ifndef EV_USE_REALTIME +# define EV_USE_REALTIME 0 +# endif +#endif + +#ifndef EV_USE_MONOTONIC +# if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 +# define EV_USE_MONOTONIC EV_FEATURE_OS +# else +# define EV_USE_MONOTONIC 0 +# endif +#endif + +#ifndef EV_USE_REALTIME +# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL +#endif + +#ifndef EV_USE_NANOSLEEP +# if _POSIX_C_SOURCE >= 199309L +# define EV_USE_NANOSLEEP EV_FEATURE_OS +# else +# define EV_USE_NANOSLEEP 0 +# endif +#endif + +#ifndef EV_USE_SELECT +# define EV_USE_SELECT EV_FEATURE_BACKENDS +#endif + +#ifndef EV_USE_POLL +# ifdef _WIN32 +# define EV_USE_POLL 0 +# else +# define EV_USE_POLL EV_FEATURE_BACKENDS +# endif +#endif + +#ifndef EV_USE_EPOLL +# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) +# define EV_USE_EPOLL EV_FEATURE_BACKENDS +# else +# define EV_USE_EPOLL 0 +# endif +#endif + +#ifndef EV_USE_KQUEUE +# define EV_USE_KQUEUE 0 +#endif + +#ifndef EV_USE_PORT +# define EV_USE_PORT 0 +#endif + +#ifndef EV_USE_INOTIFY +# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) +# define EV_USE_INOTIFY EV_FEATURE_OS +# else +# define EV_USE_INOTIFY 0 +# endif +#endif + +#ifndef EV_PID_HASHSIZE +# define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1 +#endif + +#ifndef EV_INOTIFY_HASHSIZE +# define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1 +#endif + +#ifndef EV_USE_EVENTFD +# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) +# define EV_USE_EVENTFD EV_FEATURE_OS +# else +# define EV_USE_EVENTFD 0 +# endif +#endif + +#ifndef EV_USE_SIGNALFD +# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) +# define EV_USE_SIGNALFD EV_FEATURE_OS +# else +# define EV_USE_SIGNALFD 0 +# endif +#endif + +#if 0 /* debugging */ +# define EV_VERIFY 3 +# define EV_USE_4HEAP 1 +# define EV_HEAP_CACHE_AT 1 +#endif + +#ifndef EV_VERIFY +# define EV_VERIFY (EV_FEATURE_API ? 1 : 0) +#endif + +#ifndef EV_USE_4HEAP +# define EV_USE_4HEAP EV_FEATURE_DATA +#endif + +#ifndef EV_HEAP_CACHE_AT +# define EV_HEAP_CACHE_AT EV_FEATURE_DATA +#endif + +#ifdef __ANDROID__ +/* supposedly, android doesn't typedef fd_mask */ +# undef EV_USE_SELECT +# define EV_USE_SELECT 0 +/* supposedly, we need to include syscall.h, not sys/syscall.h, so just disable */ +# undef EV_USE_CLOCK_SYSCALL +# define EV_USE_CLOCK_SYSCALL 0 +#endif + +/* aix's poll.h seems to cause lots of trouble */ +#ifdef _AIX +/* AIX has a completely broken poll.h header */ +# undef EV_USE_POLL +# define EV_USE_POLL 0 +#endif + +/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ +/* which makes programs even slower. might work on other unices, too. */ +#if EV_USE_CLOCK_SYSCALL +# include +# ifdef SYS_clock_gettime +# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) +# undef EV_USE_MONOTONIC +# define EV_USE_MONOTONIC 1 +# else +# undef EV_USE_CLOCK_SYSCALL +# define EV_USE_CLOCK_SYSCALL 0 +# endif +#endif + +/* this block fixes any misconfiguration where we know we run into trouble otherwise */ + +#ifndef CLOCK_MONOTONIC +# undef EV_USE_MONOTONIC +# define EV_USE_MONOTONIC 0 +#endif + +#ifndef CLOCK_REALTIME +# undef EV_USE_REALTIME +# define EV_USE_REALTIME 0 +#endif + +#if !EV_STAT_ENABLE +# undef EV_USE_INOTIFY +# define EV_USE_INOTIFY 0 +#endif + +#if !EV_USE_NANOSLEEP +/* hp-ux has it in sys/time.h, which we unconditionally include above */ +# if !defined _WIN32 && !defined __hpux +# include +# endif +#endif + +#if EV_USE_INOTIFY +# include +# include +/* some very old inotify.h headers don't have IN_DONT_FOLLOW */ +# ifndef IN_DONT_FOLLOW +# undef EV_USE_INOTIFY +# define EV_USE_INOTIFY 0 +# endif +#endif + +#if EV_USE_EVENTFD +/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ +# include +# ifndef EFD_NONBLOCK +# define EFD_NONBLOCK O_NONBLOCK +# endif +# ifndef EFD_CLOEXEC +# ifdef O_CLOEXEC +# define EFD_CLOEXEC O_CLOEXEC +# else +# define EFD_CLOEXEC 02000000 +# endif +# endif +EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags); +#endif + +#if EV_USE_SIGNALFD +/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ +# include +# ifndef SFD_NONBLOCK +# define SFD_NONBLOCK O_NONBLOCK +# endif +# ifndef SFD_CLOEXEC +# ifdef O_CLOEXEC +# define SFD_CLOEXEC O_CLOEXEC +# else +# define SFD_CLOEXEC 02000000 +# endif +# endif +EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags); + +struct signalfd_siginfo +{ + uint32_t ssi_signo; + char pad[128 - sizeof (uint32_t)]; +}; +#endif + +/**/ + +#if EV_VERIFY >= 3 +# define EV_FREQUENT_CHECK ev_verify (EV_A) +#else +# define EV_FREQUENT_CHECK do { } while (0) +#endif + +/* + * This is used to work around floating point rounding problems. + * This value is good at least till the year 4000. + */ +#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ +/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ + +#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ +#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ + +#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) +#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) + +/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ +/* ECB.H BEGIN */ +/* + * libecb - http://software.schmorp.de/pkg/libecb + * + * Copyright (©) 2009-2015 Marc Alexander Lehmann + * Copyright (©) 2011 Emanuele Giaquinta + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#ifndef ECB_H +#define ECB_H + +/* 16 bits major, 16 bits minor */ +#define ECB_VERSION 0x00010005 + +#ifdef _WIN32 + typedef signed char int8_t; + typedef unsigned char uint8_t; + typedef signed short int16_t; + typedef unsigned short uint16_t; + typedef signed int int32_t; + typedef unsigned int uint32_t; + #if __GNUC__ + typedef signed long long int64_t; + typedef unsigned long long uint64_t; + #else /* _MSC_VER || __BORLANDC__ */ + typedef signed __int64 int64_t; + typedef unsigned __int64 uint64_t; + #endif + #ifdef _WIN64 + #define ECB_PTRSIZE 8 + typedef uint64_t uintptr_t; + typedef int64_t intptr_t; + #else + #define ECB_PTRSIZE 4 + typedef uint32_t uintptr_t; + typedef int32_t intptr_t; + #endif +#else + #include + #if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU + #define ECB_PTRSIZE 8 + #else + #define ECB_PTRSIZE 4 + #endif +#endif + +#define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) +#define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) + +/* work around x32 idiocy by defining proper macros */ +#if ECB_GCC_AMD64 || ECB_MSVC_AMD64 + #if _ILP32 + #define ECB_AMD64_X32 1 + #else + #define ECB_AMD64 1 + #endif +#endif + +/* many compilers define _GNUC_ to some versions but then only implement + * what their idiot authors think are the "more important" extensions, + * causing enormous grief in return for some better fake benchmark numbers. + * or so. + * we try to detect these and simply assume they are not gcc - if they have + * an issue with that they should have done it right in the first place. + */ +#if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__ + #define ECB_GCC_VERSION(major,minor) 0 +#else + #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) +#endif + +#define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor))) + +#if __clang__ && defined __has_builtin + #define ECB_CLANG_BUILTIN(x) __has_builtin (x) +#else + #define ECB_CLANG_BUILTIN(x) 0 +#endif + +#if __clang__ && defined __has_extension + #define ECB_CLANG_EXTENSION(x) __has_extension (x) +#else + #define ECB_CLANG_EXTENSION(x) 0 +#endif + +#define ECB_CPP (__cplusplus+0) +#define ECB_CPP11 (__cplusplus >= 201103L) + +#if ECB_CPP + #define ECB_C 0 + #define ECB_STDC_VERSION 0 +#else + #define ECB_C 1 + #define ECB_STDC_VERSION __STDC_VERSION__ +#endif + +#define ECB_C99 (ECB_STDC_VERSION >= 199901L) +#define ECB_C11 (ECB_STDC_VERSION >= 201112L) + +#if ECB_CPP + #define ECB_EXTERN_C extern "C" + #define ECB_EXTERN_C_BEG ECB_EXTERN_C { + #define ECB_EXTERN_C_END } +#else + #define ECB_EXTERN_C extern + #define ECB_EXTERN_C_BEG + #define ECB_EXTERN_C_END +#endif + +/*****************************************************************************/ + +/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ +/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ + +#if ECB_NO_THREADS + #define ECB_NO_SMP 1 +#endif + +#if ECB_NO_SMP + #define ECB_MEMORY_FENCE do { } while (0) +#endif + +/* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */ +#if __xlC__ && ECB_CPP + #include +#endif + +#if 1400 <= _MSC_VER + #include /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ +#endif + +#ifndef ECB_MEMORY_FENCE + #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 + #if __i386 || __i386__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") + #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") + #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") + #elif ECB_GCC_AMD64 + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") + #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") + #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") + #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") + #elif defined __ARM_ARCH_2__ \ + || defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \ + || defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \ + || defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \ + || defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \ + || defined __ARM_ARCH_5TEJ__ + /* should not need any, unless running old code on newer cpu - arm doesn't support that */ + #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ + || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \ + || defined __ARM_ARCH_6T2__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") + #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ + || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") + #elif __aarch64__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory") + #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8) + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory") + #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") + #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") + #elif defined __s390__ || defined __s390x__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") + #elif defined __mips__ + /* GNU/Linux emulates sync on mips1 architectures, so we force its use */ + /* anybody else who still uses mips1 is supposed to send in their version, with detection code. */ + #define ECB_MEMORY_FENCE __asm__ __volatile__ (".set mips2; sync; .set mips0" : : : "memory") + #elif defined __alpha__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory") + #elif defined __hppa__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") + #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") + #elif defined __ia64__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory") + #elif defined __m68k__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") + #elif defined __m88k__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("tb1 0,%%r0,128" : : : "memory") + #elif defined __sh__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") + #endif + #endif +#endif + +#ifndef ECB_MEMORY_FENCE + #if ECB_GCC_VERSION(4,7) + /* see comment below (stdatomic.h) about the C11 memory model. */ + #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) + #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) + #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) + + #elif ECB_CLANG_EXTENSION(c_atomic) + /* see comment below (stdatomic.h) about the C11 memory model. */ + #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) + #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) + #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) + + #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ + #define ECB_MEMORY_FENCE __sync_synchronize () + #elif _MSC_VER >= 1500 /* VC++ 2008 */ + /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ + #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) + #define ECB_MEMORY_FENCE _ReadWriteBarrier (); MemoryBarrier() + #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier (); MemoryBarrier() /* according to msdn, _ReadBarrier is not a load fence */ + #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier (); MemoryBarrier() + #elif _MSC_VER >= 1400 /* VC++ 2005 */ + #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) + #define ECB_MEMORY_FENCE _ReadWriteBarrier () + #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ + #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () + #elif defined _WIN32 + #include + #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ + #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 + #include + #define ECB_MEMORY_FENCE __machine_rw_barrier () + #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () + #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () + #elif __xlC__ + #define ECB_MEMORY_FENCE __sync () + #endif +#endif + +#ifndef ECB_MEMORY_FENCE + #if ECB_C11 && !defined __STDC_NO_ATOMICS__ + /* we assume that these memory fences work on all variables/all memory accesses, */ + /* not just C11 atomics and atomic accesses */ + #include + /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ + /* any fence other than seq_cst, which isn't very efficient for us. */ + /* Why that is, we don't know - either the C11 memory model is quite useless */ + /* for most usages, or gcc and clang have a bug */ + /* I *currently* lean towards the latter, and inefficiently implement */ + /* all three of ecb's fences as a seq_cst fence */ + /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */ + /* for all __atomic_thread_fence's except seq_cst */ + #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) + #endif +#endif + +#ifndef ECB_MEMORY_FENCE + #if !ECB_AVOID_PTHREADS + /* + * if you get undefined symbol references to pthread_mutex_lock, + * or failure to find pthread.h, then you should implement + * the ECB_MEMORY_FENCE operations for your cpu/compiler + * OR provide pthread.h and link against the posix thread library + * of your system. + */ + #include + #define ECB_NEEDS_PTHREADS 1 + #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 + + static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; + #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) + #endif +#endif + +#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE + #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE +#endif + +#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE + #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE +#endif + +/*****************************************************************************/ + +#if ECB_CPP + #define ecb_inline static inline +#elif ECB_GCC_VERSION(2,5) + #define ecb_inline static __inline__ +#elif ECB_C99 + #define ecb_inline static inline +#else + #define ecb_inline static +#endif + +#if ECB_GCC_VERSION(3,3) + #define ecb_restrict __restrict__ +#elif ECB_C99 + #define ecb_restrict restrict +#else + #define ecb_restrict +#endif + +typedef int ecb_bool; + +#define ECB_CONCAT_(a, b) a ## b +#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) +#define ECB_STRINGIFY_(a) # a +#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) +#define ECB_STRINGIFY_EXPR(expr) ((expr), ECB_STRINGIFY_ (expr)) + +#define ecb_function_ ecb_inline + +#if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8) + #define ecb_attribute(attrlist) __attribute__ (attrlist) +#else + #define ecb_attribute(attrlist) +#endif + +#if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_constant_p) + #define ecb_is_constant(expr) __builtin_constant_p (expr) +#else + /* possible C11 impl for integral types + typedef struct ecb_is_constant_struct ecb_is_constant_struct; + #define ecb_is_constant(expr) _Generic ((1 ? (struct ecb_is_constant_struct *)0 : (void *)((expr) - (expr)), ecb_is_constant_struct *: 0, default: 1)) */ + + #define ecb_is_constant(expr) 0 +#endif + +#if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_expect) + #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) +#else + #define ecb_expect(expr,value) (expr) +#endif + +#if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_prefetch) + #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) +#else + #define ecb_prefetch(addr,rw,locality) +#endif + +/* no emulation for ecb_decltype */ +#if ECB_CPP11 + // older implementations might have problems with decltype(x)::type, work around it + template struct ecb_decltype_t { typedef T type; }; + #define ecb_decltype(x) ecb_decltype_t::type +#elif ECB_GCC_VERSION(3,0) || ECB_CLANG_VERSION(2,8) + #define ecb_decltype(x) __typeof__ (x) +#endif + +#if _MSC_VER >= 1300 + #define ecb_deprecated __declspec (deprecated) +#else + #define ecb_deprecated ecb_attribute ((__deprecated__)) +#endif + +#if _MSC_VER >= 1500 + #define ecb_deprecated_message(msg) __declspec (deprecated (msg)) +#elif ECB_GCC_VERSION(4,5) + #define ecb_deprecated_message(msg) ecb_attribute ((__deprecated__ (msg)) +#else + #define ecb_deprecated_message(msg) ecb_deprecated +#endif + +#if _MSC_VER >= 1400 + #define ecb_noinline __declspec (noinline) +#else + #define ecb_noinline ecb_attribute ((__noinline__)) +#endif + +#define ecb_unused ecb_attribute ((__unused__)) +#define ecb_const ecb_attribute ((__const__)) +#define ecb_pure ecb_attribute ((__pure__)) + +#if ECB_C11 || __IBMC_NORETURN + /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/language_ref/noreturn.html */ + #define ecb_noreturn _Noreturn +#elif ECB_CPP11 + #define ecb_noreturn [[noreturn]] +#elif _MSC_VER >= 1200 + /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx */ + #define ecb_noreturn __declspec (noreturn) +#else + #define ecb_noreturn ecb_attribute ((__noreturn__)) +#endif + +#if ECB_GCC_VERSION(4,3) + #define ecb_artificial ecb_attribute ((__artificial__)) + #define ecb_hot ecb_attribute ((__hot__)) + #define ecb_cold ecb_attribute ((__cold__)) +#else + #define ecb_artificial + #define ecb_hot + #define ecb_cold +#endif + +/* put around conditional expressions if you are very sure that the */ +/* expression is mostly true or mostly false. note that these return */ +/* booleans, not the expression. */ +#define ecb_expect_false(expr) ecb_expect (!!(expr), 0) +#define ecb_expect_true(expr) ecb_expect (!!(expr), 1) +/* for compatibility to the rest of the world */ +#define ecb_likely(expr) ecb_expect_true (expr) +#define ecb_unlikely(expr) ecb_expect_false (expr) + +/* count trailing zero bits and count # of one bits */ +#if ECB_GCC_VERSION(3,4) \ + || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \ + && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \ + && ECB_CLANG_BUILTIN(__builtin_popcount)) + /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ + #define ecb_ld32(x) (__builtin_clz (x) ^ 31) + #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) + #define ecb_ctz32(x) __builtin_ctz (x) + #define ecb_ctz64(x) __builtin_ctzll (x) + #define ecb_popcount32(x) __builtin_popcount (x) + /* no popcountll */ +#else + ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); + ecb_function_ ecb_const int + ecb_ctz32 (uint32_t x) + { +#if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) + unsigned long r; + _BitScanForward (&r, x); + return (int)r; +#else + int r = 0; + + x &= ~x + 1; /* this isolates the lowest bit */ + +#if ECB_branchless_on_i386 + r += !!(x & 0xaaaaaaaa) << 0; + r += !!(x & 0xcccccccc) << 1; + r += !!(x & 0xf0f0f0f0) << 2; + r += !!(x & 0xff00ff00) << 3; + r += !!(x & 0xffff0000) << 4; +#else + if (x & 0xaaaaaaaa) r += 1; + if (x & 0xcccccccc) r += 2; + if (x & 0xf0f0f0f0) r += 4; + if (x & 0xff00ff00) r += 8; + if (x & 0xffff0000) r += 16; +#endif + + return r; +#endif + } + + ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); + ecb_function_ ecb_const int + ecb_ctz64 (uint64_t x) + { +#if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) + unsigned long r; + _BitScanForward64 (&r, x); + return (int)r; +#else + int shift = x & 0xffffffff ? 0 : 32; + return ecb_ctz32 (x >> shift) + shift; +#endif + } + + ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); + ecb_function_ ecb_const int + ecb_popcount32 (uint32_t x) + { + x -= (x >> 1) & 0x55555555; + x = ((x >> 2) & 0x33333333) + (x & 0x33333333); + x = ((x >> 4) + x) & 0x0f0f0f0f; + x *= 0x01010101; + + return x >> 24; + } + + ecb_function_ ecb_const int ecb_ld32 (uint32_t x); + ecb_function_ ecb_const int ecb_ld32 (uint32_t x) + { +#if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) + unsigned long r; + _BitScanReverse (&r, x); + return (int)r; +#else + int r = 0; + + if (x >> 16) { x >>= 16; r += 16; } + if (x >> 8) { x >>= 8; r += 8; } + if (x >> 4) { x >>= 4; r += 4; } + if (x >> 2) { x >>= 2; r += 2; } + if (x >> 1) { r += 1; } + + return r; +#endif + } + + ecb_function_ ecb_const int ecb_ld64 (uint64_t x); + ecb_function_ ecb_const int ecb_ld64 (uint64_t x) + { +#if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) + unsigned long r; + _BitScanReverse64 (&r, x); + return (int)r; +#else + int r = 0; + + if (x >> 32) { x >>= 32; r += 32; } + + return r + ecb_ld32 (x); +#endif + } +#endif + +ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x); +ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } +ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x); +ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); } + +ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x); +ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x) +{ + return ( (x * 0x0802U & 0x22110U) + | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; +} + +ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x); +ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x) +{ + x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); + x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); + x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); + x = ( x >> 8 ) | ( x << 8); + + return x; +} + +ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x); +ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x) +{ + x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); + x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); + x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); + x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); + x = ( x >> 16 ) | ( x << 16); + + return x; +} + +/* popcount64 is only available on 64 bit cpus as gcc builtin */ +/* so for this version we are lazy */ +ecb_function_ ecb_const int ecb_popcount64 (uint64_t x); +ecb_function_ ecb_const int +ecb_popcount64 (uint64_t x) +{ + return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); +} + +ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count); +ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count); +ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count); +ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count); +ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); +ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count); +ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count); +ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count); + +ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } +ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } +ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } +ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } +ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } +ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } +ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } +ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } + +#if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) + #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16) + #define ecb_bswap16(x) __builtin_bswap16 (x) + #else + #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) + #endif + #define ecb_bswap32(x) __builtin_bswap32 (x) + #define ecb_bswap64(x) __builtin_bswap64 (x) +#elif _MSC_VER + #include + #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x))) + #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x))) + #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x))) +#else + ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x); + ecb_function_ ecb_const uint16_t + ecb_bswap16 (uint16_t x) + { + return ecb_rotl16 (x, 8); + } + + ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x); + ecb_function_ ecb_const uint32_t + ecb_bswap32 (uint32_t x) + { + return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); + } + + ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x); + ecb_function_ ecb_const uint64_t + ecb_bswap64 (uint64_t x) + { + return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); + } +#endif + +#if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable) + #define ecb_unreachable() __builtin_unreachable () +#else + /* this seems to work fine, but gcc always emits a warning for it :/ */ + ecb_inline ecb_noreturn void ecb_unreachable (void); + ecb_inline ecb_noreturn void ecb_unreachable (void) { } +#endif + +/* try to tell the compiler that some condition is definitely true */ +#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 + +ecb_inline ecb_const uint32_t ecb_byteorder_helper (void); +ecb_inline ecb_const uint32_t +ecb_byteorder_helper (void) +{ + /* the union code still generates code under pressure in gcc, */ + /* but less than using pointers, and always seems to */ + /* successfully return a constant. */ + /* the reason why we have this horrible preprocessor mess */ + /* is to avoid it in all cases, at least on common architectures */ + /* or when using a recent enough gcc version (>= 4.6) */ +#if (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \ + || ((__i386 || __i386__ || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64) && !__VOS__) + #define ECB_LITTLE_ENDIAN 1 + return 0x44332211; +#elif (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \ + || ((__AARCH64EB__ || __MIPSEB__ || __ARMEB__) && !__VOS__) + #define ECB_BIG_ENDIAN 1 + return 0x11223344; +#else + union + { + uint8_t c[4]; + uint32_t u; + } u = { 0x11, 0x22, 0x33, 0x44 }; + return u.u; +#endif +} + +ecb_inline ecb_const ecb_bool ecb_big_endian (void); +ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } +ecb_inline ecb_const ecb_bool ecb_little_endian (void); +ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } + +#if ECB_GCC_VERSION(3,0) || ECB_C99 + #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) +#else + #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) +#endif + +#if ECB_CPP + template + static inline T ecb_div_rd (T val, T div) + { + return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; + } + template + static inline T ecb_div_ru (T val, T div) + { + return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; + } +#else + #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) + #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) +#endif + +#if ecb_cplusplus_does_not_suck + /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ + template + static inline int ecb_array_length (const T (&arr)[N]) + { + return N; + } +#else + #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) +#endif + +ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); +ecb_function_ ecb_const uint32_t +ecb_binary16_to_binary32 (uint32_t x) +{ + unsigned int s = (x & 0x8000) << (31 - 15); + int e = (x >> 10) & 0x001f; + unsigned int m = x & 0x03ff; + + if (ecb_expect_false (e == 31)) + /* infinity or NaN */ + e = 255 - (127 - 15); + else if (ecb_expect_false (!e)) + { + if (ecb_expect_true (!m)) + /* zero, handled by code below by forcing e to 0 */ + e = 0 - (127 - 15); + else + { + /* subnormal, renormalise */ + unsigned int s = 10 - ecb_ld32 (m); + + m = (m << s) & 0x3ff; /* mask implicit bit */ + e -= s - 1; + } + } + + /* e and m now are normalised, or zero, (or inf or nan) */ + e += 127 - 15; + + return s | (e << 23) | (m << (23 - 10)); +} + +ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x); +ecb_function_ ecb_const uint16_t +ecb_binary32_to_binary16 (uint32_t x) +{ + unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */ + unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */ + unsigned int m = x & 0x007fffff; + + x &= 0x7fffffff; + + /* if it's within range of binary16 normals, use fast path */ + if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff)) + { + /* mantissa round-to-even */ + m += 0x00000fff + ((m >> (23 - 10)) & 1); + + /* handle overflow */ + if (ecb_expect_false (m >= 0x00800000)) + { + m >>= 1; + e += 1; + } + + return s | (e << 10) | (m >> (23 - 10)); + } + + /* handle large numbers and infinity */ + if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000)) + return s | 0x7c00; + + /* handle zero, subnormals and small numbers */ + if (ecb_expect_true (x < 0x38800000)) + { + /* zero */ + if (ecb_expect_true (!x)) + return s; + + /* handle subnormals */ + + /* too small, will be zero */ + if (e < (14 - 24)) /* might not be sharp, but is good enough */ + return s; + + m |= 0x00800000; /* make implicit bit explicit */ + + /* very tricky - we need to round to the nearest e (+10) bit value */ + { + unsigned int bits = 14 - e; + unsigned int half = (1 << (bits - 1)) - 1; + unsigned int even = (m >> bits) & 1; + + /* if this overflows, we will end up with a normalised number */ + m = (m + half + even) >> bits; + } + + return s | m; + } + + /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */ + m >>= 13; + + return s | 0x7c00 | m | !m; +} + +/*******************************************************************************/ +/* floating point stuff, can be disabled by defining ECB_NO_LIBM */ + +/* basically, everything uses "ieee pure-endian" floating point numbers */ +/* the only noteworthy exception is ancient armle, which uses order 43218765 */ +#if 0 \ + || __i386 || __i386__ \ + || ECB_GCC_AMD64 \ + || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \ + || defined __s390__ || defined __s390x__ \ + || defined __mips__ \ + || defined __alpha__ \ + || defined __hppa__ \ + || defined __ia64__ \ + || defined __m68k__ \ + || defined __m88k__ \ + || defined __sh__ \ + || defined _M_IX86 || defined ECB_MSVC_AMD64 || defined _M_IA64 \ + || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \ + || defined __aarch64__ + #define ECB_STDFP 1 + #include /* for memcpy */ +#else + #define ECB_STDFP 0 +#endif + +#ifndef ECB_NO_LIBM + + #include /* for frexp*, ldexp*, INFINITY, NAN */ + + /* only the oldest of old doesn't have this one. solaris. */ + #ifdef INFINITY + #define ECB_INFINITY INFINITY + #else + #define ECB_INFINITY HUGE_VAL + #endif + + #ifdef NAN + #define ECB_NAN NAN + #else + #define ECB_NAN ECB_INFINITY + #endif + + #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L + #define ecb_ldexpf(x,e) ldexpf ((x), (e)) + #define ecb_frexpf(x,e) frexpf ((x), (e)) + #else + #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e)) + #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e)) + #endif + + /* convert a float to ieee single/binary32 */ + ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); + ecb_function_ ecb_const uint32_t + ecb_float_to_binary32 (float x) + { + uint32_t r; + + #if ECB_STDFP + memcpy (&r, &x, 4); + #else + /* slow emulation, works for anything but -0 */ + uint32_t m; + int e; + + if (x == 0e0f ) return 0x00000000U; + if (x > +3.40282346638528860e+38f) return 0x7f800000U; + if (x < -3.40282346638528860e+38f) return 0xff800000U; + if (x != x ) return 0x7fbfffffU; + + m = ecb_frexpf (x, &e) * 0x1000000U; + + r = m & 0x80000000U; + + if (r) + m = -m; + + if (e <= -126) + { + m &= 0xffffffU; + m >>= (-125 - e); + e = -126; + } + + r |= (e + 126) << 23; + r |= m & 0x7fffffU; + #endif + + return r; + } + + /* converts an ieee single/binary32 to a float */ + ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x); + ecb_function_ ecb_const float + ecb_binary32_to_float (uint32_t x) + { + float r; + + #if ECB_STDFP + memcpy (&r, &x, 4); + #else + /* emulation, only works for normals and subnormals and +0 */ + int neg = x >> 31; + int e = (x >> 23) & 0xffU; + + x &= 0x7fffffU; + + if (e) + x |= 0x800000U; + else + e = 1; + + /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */ + r = ecb_ldexpf (x * (0.5f / 0x800000U), e - 126); + + r = neg ? -r : r; + #endif + + return r; + } + + /* convert a double to ieee double/binary64 */ + ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x); + ecb_function_ ecb_const uint64_t + ecb_double_to_binary64 (double x) + { + uint64_t r; + + #if ECB_STDFP + memcpy (&r, &x, 8); + #else + /* slow emulation, works for anything but -0 */ + uint64_t m; + int e; + + if (x == 0e0 ) return 0x0000000000000000U; + if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U; + if (x < -1.79769313486231470e+308) return 0xfff0000000000000U; + if (x != x ) return 0X7ff7ffffffffffffU; + + m = frexp (x, &e) * 0x20000000000000U; + + r = m & 0x8000000000000000;; + + if (r) + m = -m; + + if (e <= -1022) + { + m &= 0x1fffffffffffffU; + m >>= (-1021 - e); + e = -1022; + } + + r |= ((uint64_t)(e + 1022)) << 52; + r |= m & 0xfffffffffffffU; + #endif + + return r; + } + + /* converts an ieee double/binary64 to a double */ + ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x); + ecb_function_ ecb_const double + ecb_binary64_to_double (uint64_t x) + { + double r; + + #if ECB_STDFP + memcpy (&r, &x, 8); + #else + /* emulation, only works for normals and subnormals and +0 */ + int neg = x >> 63; + int e = (x >> 52) & 0x7ffU; + + x &= 0xfffffffffffffU; + + if (e) + x |= 0x10000000000000U; + else + e = 1; + + /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */ + r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022); + + r = neg ? -r : r; + #endif + + return r; + } + + /* convert a float to ieee half/binary16 */ + ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x); + ecb_function_ ecb_const uint16_t + ecb_float_to_binary16 (float x) + { + return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x)); + } + + /* convert an ieee half/binary16 to float */ + ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x); + ecb_function_ ecb_const float + ecb_binary16_to_float (uint16_t x) + { + return ecb_binary32_to_float (ecb_binary16_to_binary32 (x)); + } + +#endif + +#endif + +/* ECB.H END */ + +#if ECB_MEMORY_FENCE_NEEDS_PTHREADS +/* if your architecture doesn't need memory fences, e.g. because it is + * single-cpu/core, or if you use libev in a project that doesn't use libev + * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling + * libev, in which cases the memory fences become nops. + * alternatively, you can remove this #error and link against libpthread, + * which will then provide the memory fences. + */ +# error "memory fences not defined for your architecture, please report" +#endif + +#ifndef ECB_MEMORY_FENCE +# define ECB_MEMORY_FENCE do { } while (0) +# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE +# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE +#endif + +#define expect_false(cond) ecb_expect_false (cond) +#define expect_true(cond) ecb_expect_true (cond) +#define noinline ecb_noinline + +#define inline_size ecb_inline + +#if EV_FEATURE_CODE +# define inline_speed ecb_inline +#else +# define inline_speed noinline static +#endif + +#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) + +#if EV_MINPRI == EV_MAXPRI +# define ABSPRI(w) (((W)w), 0) +#else +# define ABSPRI(w) (((W)w)->priority - EV_MINPRI) +#endif + +#define EMPTY /* required for microsofts broken pseudo-c compiler */ +#define EMPTY2(a,b) /* used to suppress some warnings */ + +typedef ev_watcher *W; +typedef ev_watcher_list *WL; +typedef ev_watcher_time *WT; + +#define ev_active(w) ((W)(w))->active +#define ev_at(w) ((WT)(w))->at + +#if EV_USE_REALTIME +/* sig_atomic_t is used to avoid per-thread variables or locking but still */ +/* giving it a reasonably high chance of working on typical architectures */ +static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ +#endif + +#if EV_USE_MONOTONIC +static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ +#endif + +#ifndef EV_FD_TO_WIN32_HANDLE +# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) +#endif +#ifndef EV_WIN32_HANDLE_TO_FD +# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0) +#endif +#ifndef EV_WIN32_CLOSE_FD +# define EV_WIN32_CLOSE_FD(fd) close (fd) +#endif + +#ifdef _WIN32 +# include "ev_win32.c" +#endif + +/*****************************************************************************/ + +/* define a suitable floor function (only used by periodics atm) */ + +#if EV_USE_FLOOR +# include +# define ev_floor(v) floor (v) +#else + +#include + +/* a floor() replacement function, should be independent of ev_tstamp type */ +noinline +static ev_tstamp +ev_floor (ev_tstamp v) +{ + /* the choice of shift factor is not terribly important */ +#if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */ + const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; +#else + const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; +#endif + + /* argument too large for an unsigned long? */ + if (expect_false (v >= shift)) + { + ev_tstamp f; + + if (v == v - 1.) + return v; /* very large number */ + + f = shift * ev_floor (v * (1. / shift)); + return f + ev_floor (v - f); + } + + /* special treatment for negative args? */ + if (expect_false (v < 0.)) + { + ev_tstamp f = -ev_floor (-v); + + return f - (f == v ? 0 : 1); + } + + /* fits into an unsigned long */ + return (unsigned long)v; +} + +#endif + +/*****************************************************************************/ + +#ifdef __linux +# include +#endif + +noinline ecb_cold +static unsigned int +ev_linux_version (void) +{ +#ifdef __linux + unsigned int v = 0; + struct utsname buf; + int i; + char *p = buf.release; + + if (uname (&buf)) + return 0; + + for (i = 3+1; --i; ) + { + unsigned int c = 0; + + for (;;) + { + if (*p >= '0' && *p <= '9') + c = c * 10 + *p++ - '0'; + else + { + p += *p == '.'; + break; + } + } + + v = (v << 8) | c; + } + + return v; +#else + return 0; +#endif +} + +/*****************************************************************************/ + +#if EV_AVOID_STDIO +noinline ecb_cold +static void +ev_printerr (const char *msg) +{ + write (STDERR_FILENO, msg, strlen (msg)); +} +#endif + +static void (*syserr_cb)(const char *msg) EV_THROW; + +ecb_cold +void +ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW +{ + syserr_cb = cb; +} + +noinline ecb_cold +static void +ev_syserr (const char *msg) +{ + if (!msg) + msg = "(libev) system error"; + + if (syserr_cb) + syserr_cb (msg); + else + { +#if EV_AVOID_STDIO + ev_printerr (msg); + ev_printerr (": "); + ev_printerr (strerror (errno)); + ev_printerr ("\n"); +#else + perror (msg); +#endif + abort (); + } +} + +static void * +ev_realloc_emul (void *ptr, long size) EV_THROW +{ + /* some systems, notably openbsd and darwin, fail to properly + * implement realloc (x, 0) (as required by both ansi c-89 and + * the single unix specification, so work around them here. + * recently, also (at least) fedora and debian started breaking it, + * despite documenting it otherwise. + */ + + if (size) + return realloc (ptr, size); + + free (ptr); + return 0; +} + +static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; + +ecb_cold +void +ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW +{ + alloc = cb; +} + +inline_speed void * +ev_realloc (void *ptr, long size) +{ + ptr = alloc (ptr, size); + + if (!ptr && size) + { +#if EV_AVOID_STDIO + ev_printerr ("(libev) memory allocation failed, aborting.\n"); +#else + fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size); +#endif + abort (); + } + + return ptr; +} + +#define ev_malloc(size) ev_realloc (0, (size)) +#define ev_free(ptr) ev_realloc ((ptr), 0) + +/*****************************************************************************/ + +/* set in reify when reification needed */ +#define EV_ANFD_REIFY 1 + +/* file descriptor info structure */ +typedef struct +{ + WL head; + unsigned char events; /* the events watched for */ + unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ + unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ + unsigned char unused; +#if EV_USE_EPOLL + unsigned int egen; /* generation counter to counter epoll bugs */ +#endif +#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP + SOCKET handle; +#endif +#if EV_USE_IOCP + OVERLAPPED or, ow; +#endif +} ANFD; + +/* stores the pending event set for a given watcher */ +typedef struct +{ + W w; + int events; /* the pending event set for the given watcher */ +} ANPENDING; + +#if EV_USE_INOTIFY +/* hash table entry per inotify-id */ +typedef struct +{ + WL head; +} ANFS; +#endif + +/* Heap Entry */ +#if EV_HEAP_CACHE_AT + /* a heap element */ + typedef struct { + ev_tstamp at; + WT w; + } ANHE; + + #define ANHE_w(he) (he).w /* access watcher, read-write */ + #define ANHE_at(he) (he).at /* access cached at, read-only */ + #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ +#else + /* a heap element */ + typedef WT ANHE; + + #define ANHE_w(he) (he) + #define ANHE_at(he) (he)->at + #define ANHE_at_cache(he) +#endif + +#if EV_MULTIPLICITY + + struct ev_loop + { + ev_tstamp ev_rt_now; + #define ev_rt_now ((loop)->ev_rt_now) + #define VAR(name,decl) decl; + #include "ev_vars.h" + #undef VAR + }; + #include "ev_wrap.h" + + static struct ev_loop default_loop_struct; + EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ + +#else + + EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ + #define VAR(name,decl) static decl; + #include "ev_vars.h" + #undef VAR + + static int ev_default_loop_ptr; + +#endif + +#if EV_FEATURE_API +# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) +# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) +# define EV_INVOKE_PENDING invoke_cb (EV_A) +#else +# define EV_RELEASE_CB (void)0 +# define EV_ACQUIRE_CB (void)0 +# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) +#endif + +#define EVBREAK_RECURSE 0x80 + +/*****************************************************************************/ + +#ifndef EV_HAVE_EV_TIME +ev_tstamp +ev_time (void) EV_THROW +{ +#if EV_USE_REALTIME + if (expect_true (have_realtime)) + { + struct timespec ts; + clock_gettime (CLOCK_REALTIME, &ts); + return ts.tv_sec + ts.tv_nsec * 1e-9; + } +#endif + + struct timeval tv; + gettimeofday (&tv, 0); + return tv.tv_sec + tv.tv_usec * 1e-6; +} +#endif + +inline_size ev_tstamp +get_clock (void) +{ +#if EV_USE_MONOTONIC + if (expect_true (have_monotonic)) + { + struct timespec ts; + clock_gettime (CLOCK_MONOTONIC, &ts); + return ts.tv_sec + ts.tv_nsec * 1e-9; + } +#endif + + return ev_time (); +} + +#if EV_MULTIPLICITY +ev_tstamp +ev_now (EV_P) EV_THROW +{ + return ev_rt_now; +} +#endif + +void +ev_sleep (ev_tstamp delay) EV_THROW +{ + if (delay > 0.) + { +#if EV_USE_NANOSLEEP + struct timespec ts; + + EV_TS_SET (ts, delay); + nanosleep (&ts, 0); +#elif defined _WIN32 + Sleep ((unsigned long)(delay * 1e3)); +#else + struct timeval tv; + + /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ + /* something not guaranteed by newer posix versions, but guaranteed */ + /* by older ones */ + EV_TV_SET (tv, delay); + select (0, 0, 0, 0, &tv); +#endif + } +} + +/*****************************************************************************/ + +#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ + +/* find a suitable new size for the given array, */ +/* hopefully by rounding to a nice-to-malloc size */ +inline_size int +array_nextsize (int elem, int cur, int cnt) +{ + int ncur = cur + 1; + + do + ncur <<= 1; + while (cnt > ncur); + + /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */ + if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) + { + ncur *= elem; + ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); + ncur = ncur - sizeof (void *) * 4; + ncur /= elem; + } + + return ncur; +} + +noinline ecb_cold +static void * +array_realloc (int elem, void *base, int *cur, int cnt) +{ + *cur = array_nextsize (elem, *cur, cnt); + return ev_realloc (base, elem * *cur); +} + +#define array_init_zero(base,count) \ + memset ((void *)(base), 0, sizeof (*(base)) * (count)) + +#define array_needsize(type,base,cur,cnt,init) \ + if (expect_false ((cnt) > (cur))) \ + { \ + ecb_unused int ocur_ = (cur); \ + (base) = (type *)array_realloc \ + (sizeof (type), (base), &(cur), (cnt)); \ + init ((base) + (ocur_), (cur) - ocur_); \ + } + +#if 0 +#define array_slim(type,stem) \ + if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ + { \ + stem ## max = array_roundsize (stem ## cnt >> 1); \ + base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ + fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ + } +#endif + +#define array_free(stem, idx) \ + ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 + +/*****************************************************************************/ + +/* dummy callback for pending events */ +noinline +static void +pendingcb (EV_P_ ev_prepare *w, int revents) +{ +} + +noinline +void +ev_feed_event (EV_P_ void *w, int revents) EV_THROW +{ + W w_ = (W)w; + int pri = ABSPRI (w_); + + if (expect_false (w_->pending)) + pendings [pri][w_->pending - 1].events |= revents; + else + { + w_->pending = ++pendingcnt [pri]; + array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); + pendings [pri][w_->pending - 1].w = w_; + pendings [pri][w_->pending - 1].events = revents; + } + + pendingpri = NUMPRI - 1; +} + +inline_speed void +feed_reverse (EV_P_ W w) +{ + array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2); + rfeeds [rfeedcnt++] = w; +} + +inline_size void +feed_reverse_done (EV_P_ int revents) +{ + do + ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents); + while (rfeedcnt); +} + +inline_speed void +queue_events (EV_P_ W *events, int eventcnt, int type) +{ + int i; + + for (i = 0; i < eventcnt; ++i) + ev_feed_event (EV_A_ events [i], type); +} + +/*****************************************************************************/ + +inline_speed void +fd_event_nocheck (EV_P_ int fd, int revents) +{ + ANFD *anfd = anfds + fd; + ev_io *w; + + for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) + { + int ev = w->events & revents; + + if (ev) + ev_feed_event (EV_A_ (W)w, ev); + } +} + +/* do not submit kernel events for fds that have reify set */ +/* because that means they changed while we were polling for new events */ +inline_speed void +fd_event (EV_P_ int fd, int revents) +{ + ANFD *anfd = anfds + fd; + + if (expect_true (!anfd->reify)) + fd_event_nocheck (EV_A_ fd, revents); +} + +void +ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW +{ + if (fd >= 0 && fd < anfdmax) + fd_event_nocheck (EV_A_ fd, revents); +} + +/* make sure the external fd watch events are in-sync */ +/* with the kernel/libev internal state */ +inline_size void +fd_reify (EV_P) +{ + int i; + +#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP + for (i = 0; i < fdchangecnt; ++i) + { + int fd = fdchanges [i]; + ANFD *anfd = anfds + fd; + + if (anfd->reify & EV__IOFDSET && anfd->head) + { + SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd); + + if (handle != anfd->handle) + { + unsigned long arg; + + assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0)); + + /* handle changed, but fd didn't - we need to do it in two steps */ + backend_modify (EV_A_ fd, anfd->events, 0); + anfd->events = 0; + anfd->handle = handle; + } + } + } +#endif + + for (i = 0; i < fdchangecnt; ++i) + { + int fd = fdchanges [i]; + ANFD *anfd = anfds + fd; + ev_io *w; + + unsigned char o_events = anfd->events; + unsigned char o_reify = anfd->reify; + + anfd->reify = 0; + + /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ + { + anfd->events = 0; + + for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) + anfd->events |= (unsigned char)w->events; + + if (o_events != anfd->events) + o_reify = EV__IOFDSET; /* actually |= */ + } + + if (o_reify & EV__IOFDSET) + backend_modify (EV_A_ fd, o_events, anfd->events); + } + + fdchangecnt = 0; +} + +/* something about the given fd changed */ +inline_size +void +fd_change (EV_P_ int fd, int flags) +{ + unsigned char reify = anfds [fd].reify; + anfds [fd].reify |= flags; + + if (expect_true (!reify)) + { + ++fdchangecnt; + array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); + fdchanges [fdchangecnt - 1] = fd; + } +} + +/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ +inline_speed ecb_cold void +fd_kill (EV_P_ int fd) +{ + ev_io *w; + + while ((w = (ev_io *)anfds [fd].head)) + { + ev_io_stop (EV_A_ w); + ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); + } +} + +/* check whether the given fd is actually valid, for error recovery */ +inline_size ecb_cold int +fd_valid (int fd) +{ +#ifdef _WIN32 + return EV_FD_TO_WIN32_HANDLE (fd) != -1; +#else + return fcntl (fd, F_GETFD) != -1; +#endif +} + +/* called on EBADF to verify fds */ +noinline ecb_cold +static void +fd_ebadf (EV_P) +{ + int fd; + + for (fd = 0; fd < anfdmax; ++fd) + if (anfds [fd].events) + if (!fd_valid (fd) && errno == EBADF) + fd_kill (EV_A_ fd); +} + +/* called on ENOMEM in select/poll to kill some fds and retry */ +noinline ecb_cold +static void +fd_enomem (EV_P) +{ + int fd; + + for (fd = anfdmax; fd--; ) + if (anfds [fd].events) + { + fd_kill (EV_A_ fd); + break; + } +} + +/* usually called after fork if backend needs to re-arm all fds from scratch */ +noinline +static void +fd_rearm_all (EV_P) +{ + int fd; + + for (fd = 0; fd < anfdmax; ++fd) + if (anfds [fd].events) + { + anfds [fd].events = 0; + anfds [fd].emask = 0; + fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); + } +} + +/* used to prepare libev internal fd's */ +/* this is not fork-safe */ +inline_speed void +fd_intern (int fd) +{ +#ifdef _WIN32 + unsigned long arg = 1; + ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg); +#else + fcntl (fd, F_SETFD, FD_CLOEXEC); + fcntl (fd, F_SETFL, O_NONBLOCK); +#endif +} + +/*****************************************************************************/ + +/* + * the heap functions want a real array index. array index 0 is guaranteed to not + * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives + * the branching factor of the d-tree. + */ + +/* + * at the moment we allow libev the luxury of two heaps, + * a small-code-size 2-heap one and a ~1.5kb larger 4-heap + * which is more cache-efficient. + * the difference is about 5% with 50000+ watchers. + */ +#if EV_USE_4HEAP + +#define DHEAP 4 +#define HEAP0 (DHEAP - 1) /* index of first element in heap */ +#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0) +#define UPHEAP_DONE(p,k) ((p) == (k)) + +/* away from the root */ +inline_speed void +downheap (ANHE *heap, int N, int k) +{ + ANHE he = heap [k]; + ANHE *E = heap + N + HEAP0; + + for (;;) + { + ev_tstamp minat; + ANHE *minpos; + ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1; + + /* find minimum child */ + if (expect_true (pos + DHEAP - 1 < E)) + { + /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); + if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); + if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); + if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); + } + else if (pos < E) + { + /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); + if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); + if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); + if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); + } + else + break; + + if (ANHE_at (he) <= minat) + break; + + heap [k] = *minpos; + ev_active (ANHE_w (*minpos)) = k; + + k = minpos - heap; + } + + heap [k] = he; + ev_active (ANHE_w (he)) = k; +} + +#else /* 4HEAP */ + +#define HEAP0 1 +#define HPARENT(k) ((k) >> 1) +#define UPHEAP_DONE(p,k) (!(p)) + +/* away from the root */ +inline_speed void +downheap (ANHE *heap, int N, int k) +{ + ANHE he = heap [k]; + + for (;;) + { + int c = k << 1; + + if (c >= N + HEAP0) + break; + + c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) + ? 1 : 0; + + if (ANHE_at (he) <= ANHE_at (heap [c])) + break; + + heap [k] = heap [c]; + ev_active (ANHE_w (heap [k])) = k; + + k = c; + } + + heap [k] = he; + ev_active (ANHE_w (he)) = k; +} +#endif + +/* towards the root */ +inline_speed void +upheap (ANHE *heap, int k) +{ + ANHE he = heap [k]; + + for (;;) + { + int p = HPARENT (k); + + if (UPHEAP_DONE (p, k) || ANHE_at (heap [p]) <= ANHE_at (he)) + break; + + heap [k] = heap [p]; + ev_active (ANHE_w (heap [k])) = k; + k = p; + } + + heap [k] = he; + ev_active (ANHE_w (he)) = k; +} + +/* move an element suitably so it is in a correct place */ +inline_size void +adjustheap (ANHE *heap, int N, int k) +{ + if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)])) + upheap (heap, k); + else + downheap (heap, N, k); +} + +/* rebuild the heap: this function is used only once and executed rarely */ +inline_size void +reheap (ANHE *heap, int N) +{ + int i; + + /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ + /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */ + for (i = 0; i < N; ++i) + upheap (heap, i + HEAP0); +} + +/*****************************************************************************/ + +/* associate signal watchers to a signal signal */ +typedef struct +{ + EV_ATOMIC_T pending; +#if EV_MULTIPLICITY + EV_P; +#endif + WL head; +} ANSIG; + +static ANSIG signals [EV_NSIG - 1]; + +/*****************************************************************************/ + +#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE + +noinline ecb_cold +static void +evpipe_init (EV_P) +{ + if (!ev_is_active (&pipe_w)) + { + int fds [2]; + +# if EV_USE_EVENTFD + fds [0] = -1; + fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); + if (fds [1] < 0 && errno == EINVAL) + fds [1] = eventfd (0, 0); + + if (fds [1] < 0) +# endif + { + while (pipe (fds)) + ev_syserr ("(libev) error creating signal/async pipe"); + + fd_intern (fds [0]); + } + + evpipe [0] = fds [0]; + + if (evpipe [1] < 0) + evpipe [1] = fds [1]; /* first call, set write fd */ + else + { + /* on subsequent calls, do not change evpipe [1] */ + /* so that evpipe_write can always rely on its value. */ + /* this branch does not do anything sensible on windows, */ + /* so must not be executed on windows */ + + dup2 (fds [1], evpipe [1]); + close (fds [1]); + } + + fd_intern (evpipe [1]); + + ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ); + ev_io_start (EV_A_ &pipe_w); + ev_unref (EV_A); /* watcher should not keep loop alive */ + } +} + +inline_speed void +evpipe_write (EV_P_ EV_ATOMIC_T *flag) +{ + ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */ + + if (expect_true (*flag)) + return; + + *flag = 1; + ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ + + pipe_write_skipped = 1; + + ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ + + if (pipe_write_wanted) + { + int old_errno; + + pipe_write_skipped = 0; + ECB_MEMORY_FENCE_RELEASE; + + old_errno = errno; /* save errno because write will clobber it */ + +#if EV_USE_EVENTFD + if (evpipe [0] < 0) + { + uint64_t counter = 1; + write (evpipe [1], &counter, sizeof (uint64_t)); + } + else +#endif + { +#ifdef _WIN32 + WSABUF buf; + DWORD sent; +// buf.buf=&buf; + buf.buf = (char *)&buf; + buf.len = 1; + WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0); +#else + write (evpipe [1], &(evpipe [1]), 1); +#endif + } + + errno = old_errno; + } +} + +/* called whenever the libev signal pipe */ +/* got some events (signal, async) */ +static void +pipecb (EV_P_ ev_io *iow, int revents) +{ + int i; + + if (revents & EV_READ) + { +#if EV_USE_EVENTFD + if (evpipe [0] < 0) + { + uint64_t counter; + read (evpipe [1], &counter, sizeof (uint64_t)); + } + else +#endif + { + char dummy[4]; +#ifdef _WIN32 + WSABUF buf; + DWORD recvd; + DWORD flags = 0; + buf.buf = dummy; + buf.len = sizeof (dummy); + WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0); +#else + read (evpipe [0], &dummy, sizeof (dummy)); +#endif + } + } + + pipe_write_skipped = 0; + + ECB_MEMORY_FENCE; /* push out skipped, acquire flags */ + +#if EV_SIGNAL_ENABLE + if (sig_pending) + { + sig_pending = 0; + + ECB_MEMORY_FENCE; + + for (i = EV_NSIG - 1; i--; ) + if (expect_false (signals [i].pending)) + ev_feed_signal_event (EV_A_ i + 1); + } +#endif + +#if EV_ASYNC_ENABLE + if (async_pending) + { + async_pending = 0; + + ECB_MEMORY_FENCE; + + for (i = asynccnt; i--; ) + if (asyncs [i]->sent) + { + asyncs [i]->sent = 0; + ECB_MEMORY_FENCE_RELEASE; + ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); + } + } +#endif +} + +/*****************************************************************************/ + +void +ev_feed_signal (int signum) EV_THROW +{ +#if EV_MULTIPLICITY + EV_P; + ECB_MEMORY_FENCE_ACQUIRE; + EV_A = signals [signum - 1].loop; + + if (!EV_A) + return; +#endif + + signals [signum - 1].pending = 1; + evpipe_write (EV_A_ &sig_pending); +} + +static void +ev_sighandler (int signum) +{ +#ifdef _WIN32 + signal (signum, ev_sighandler); +#endif + + ev_feed_signal (signum); +} + +noinline +void +ev_feed_signal_event (EV_P_ int signum) EV_THROW +{ + WL w; + + if (expect_false (signum <= 0 || signum >= EV_NSIG)) + return; + + --signum; + +#if EV_MULTIPLICITY + /* it is permissible to try to feed a signal to the wrong loop */ + /* or, likely more useful, feeding a signal nobody is waiting for */ + + if (expect_false (signals [signum].loop != EV_A)) + return; +#endif + + signals [signum].pending = 0; + ECB_MEMORY_FENCE_RELEASE; + + for (w = signals [signum].head; w; w = w->next) + ev_feed_event (EV_A_ (W)w, EV_SIGNAL); +} + +#if EV_USE_SIGNALFD +static void +sigfdcb (EV_P_ ev_io *iow, int revents) +{ + struct signalfd_siginfo si[2], *sip; /* these structs are big */ + + for (;;) + { + ssize_t res = read (sigfd, si, sizeof (si)); + + /* not ISO-C, as res might be -1, but works with SuS */ + for (sip = si; (char *)sip < (char *)si + res; ++sip) + ev_feed_signal_event (EV_A_ sip->ssi_signo); + + if (res < (ssize_t)sizeof (si)) + break; + } +} +#endif + +#endif + +/*****************************************************************************/ + +#if EV_CHILD_ENABLE +static WL childs [EV_PID_HASHSIZE]; + +static ev_signal childev; + +#ifndef WIFCONTINUED +# define WIFCONTINUED(status) 0 +#endif + +/* handle a single child status event */ +inline_speed void +child_reap (EV_P_ int chain, int pid, int status) +{ + ev_child *w; + int traced = WIFSTOPPED (status) || WIFCONTINUED (status); + + for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next) + { + if ((w->pid == pid || !w->pid) + && (!traced || (w->flags & 1))) + { + ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */ + w->rpid = pid; + w->rstatus = status; + ev_feed_event (EV_A_ (W)w, EV_CHILD); + } + } +} + +#ifndef WCONTINUED +# define WCONTINUED 0 +#endif + +/* called on sigchld etc., calls waitpid */ +static void +childcb (EV_P_ ev_signal *sw, int revents) +{ + int pid, status; + + /* some systems define WCONTINUED but then fail to support it (linux 2.4) */ + if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) + if (!WCONTINUED + || errno != EINVAL + || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) + return; + + /* make sure we are called again until all children have been reaped */ + /* we need to do it this way so that the callback gets called before we continue */ + ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); + + child_reap (EV_A_ pid, pid, status); + if ((EV_PID_HASHSIZE) > 1) + child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ +} + +#endif + +/*****************************************************************************/ + +#if EV_USE_IOCP +# include "ev_iocp.c" +#endif +#if EV_USE_PORT +# include "ev_port.c" +#endif +#if EV_USE_KQUEUE +# include "ev_kqueue.c" +#endif +#if EV_USE_EPOLL +# include "ev_epoll.c" +#endif +#if EV_USE_POLL +# include "ev_poll.c" +#endif +#if EV_USE_SELECT +# include "ev_select.c" +#endif + +ecb_cold int +ev_version_major (void) EV_THROW +{ + return EV_VERSION_MAJOR; +} + +ecb_cold int +ev_version_minor (void) EV_THROW +{ + return EV_VERSION_MINOR; +} + +/* return true if we are running with elevated privileges and should ignore env variables */ +inline_size ecb_cold int +enable_secure (void) +{ +#ifdef _WIN32 + return 0; +#else + return getuid () != geteuid () + || getgid () != getegid (); +#endif +} + +ecb_cold +unsigned int +ev_supported_backends (void) EV_THROW +{ + unsigned int flags = 0; + + if (EV_USE_PORT ) flags |= EVBACKEND_PORT; + if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; + if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; + if (EV_USE_POLL ) flags |= EVBACKEND_POLL; + if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; + + return flags; +} + +ecb_cold +unsigned int +ev_recommended_backends (void) EV_THROW +{ + unsigned int flags = ev_supported_backends (); + +#ifndef __NetBSD__ + /* kqueue is borked on everything but netbsd apparently */ + /* it usually doesn't work correctly on anything but sockets and pipes */ + flags &= ~EVBACKEND_KQUEUE; +#endif +#ifdef __APPLE__ + /* only select works correctly on that "unix-certified" platform */ + flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */ + flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */ +#endif +#ifdef __FreeBSD__ + flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */ +#endif + + return flags; +} + +ecb_cold +unsigned int +ev_embeddable_backends (void) EV_THROW +{ + int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; + + /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ + if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ + flags &= ~EVBACKEND_EPOLL; + + return flags; +} + +unsigned int +ev_backend (EV_P) EV_THROW +{ + return backend; +} + +#if EV_FEATURE_API +unsigned int +ev_iteration (EV_P) EV_THROW +{ + return loop_count; +} + +unsigned int +ev_depth (EV_P) EV_THROW +{ + return loop_depth; +} + +void +ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW +{ + io_blocktime = interval; +} + +void +ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW +{ + timeout_blocktime = interval; +} + +void +ev_set_userdata (EV_P_ void *data) EV_THROW +{ + userdata = data; +} + +void * +ev_userdata (EV_P) EV_THROW +{ + return userdata; +} + +void +ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_THROW +{ + invoke_cb = invoke_pending_cb; +} + +void +ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW +{ + release_cb = release; + acquire_cb = acquire; +} +#endif + +/* initialise a loop structure, must be zero-initialised */ +noinline ecb_cold +static void +loop_init (EV_P_ unsigned int flags) EV_THROW +{ + if (!backend) + { + origflags = flags; + +#if EV_USE_REALTIME + if (!have_realtime) + { + struct timespec ts; + + if (!clock_gettime (CLOCK_REALTIME, &ts)) + have_realtime = 1; + } +#endif + +#if EV_USE_MONOTONIC + if (!have_monotonic) + { + struct timespec ts; + + if (!clock_gettime (CLOCK_MONOTONIC, &ts)) + have_monotonic = 1; + } +#endif + + /* pid check not overridable via env */ +#ifndef _WIN32 + if (flags & EVFLAG_FORKCHECK) + curpid = getpid (); +#endif + + if (!(flags & EVFLAG_NOENV) + && !enable_secure () + && getenv ("LIBEV_FLAGS")) + flags = atoi (getenv ("LIBEV_FLAGS")); + + ev_rt_now = ev_time (); + mn_now = get_clock (); + now_floor = mn_now; + rtmn_diff = ev_rt_now - mn_now; +#if EV_FEATURE_API + invoke_cb = ev_invoke_pending; +#endif + + io_blocktime = 0.; + timeout_blocktime = 0.; + backend = 0; + backend_fd = -1; + sig_pending = 0; +#if EV_ASYNC_ENABLE + async_pending = 0; +#endif + pipe_write_skipped = 0; + pipe_write_wanted = 0; + evpipe [0] = -1; + evpipe [1] = -1; +#if EV_USE_INOTIFY + fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; +#endif +#if EV_USE_SIGNALFD + sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; +#endif + + if (!(flags & EVBACKEND_MASK)) + flags |= ev_recommended_backends (); + +#if EV_USE_IOCP + if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); +#endif +#if EV_USE_PORT + if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); +#endif +#if EV_USE_KQUEUE + if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); +#endif +#if EV_USE_EPOLL + if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); +#endif +#if EV_USE_POLL + if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); +#endif +#if EV_USE_SELECT + if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); +#endif + + ev_prepare_init (&pending_w, pendingcb); + +#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE + ev_init (&pipe_w, pipecb); + ev_set_priority (&pipe_w, EV_MAXPRI); +#endif + } +} + +/* free up a loop structure */ +ecb_cold +void +ev_loop_destroy (EV_P) +{ + int i; + +#if EV_MULTIPLICITY + /* mimic free (0) */ + if (!EV_A) + return; +#endif + +#if EV_CLEANUP_ENABLE + /* queue cleanup watchers (and execute them) */ + if (expect_false (cleanupcnt)) + { + queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP); + EV_INVOKE_PENDING; + } +#endif + +#if EV_CHILD_ENABLE + if (ev_is_default_loop (EV_A) && ev_is_active (&childev)) + { + ev_ref (EV_A); /* child watcher */ + ev_signal_stop (EV_A_ &childev); + } +#endif + + if (ev_is_active (&pipe_w)) + { + /*ev_ref (EV_A);*/ + /*ev_io_stop (EV_A_ &pipe_w);*/ + + if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]); + if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]); + } + +#if EV_USE_SIGNALFD + if (ev_is_active (&sigfd_w)) + close (sigfd); +#endif + +#if EV_USE_INOTIFY + if (fs_fd >= 0) + close (fs_fd); +#endif + + if (backend_fd >= 0) + close (backend_fd); + +#if EV_USE_IOCP + if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A); +#endif +#if EV_USE_PORT + if (backend == EVBACKEND_PORT ) port_destroy (EV_A); +#endif +#if EV_USE_KQUEUE + if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); +#endif +#if EV_USE_EPOLL + if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); +#endif +#if EV_USE_POLL + if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); +#endif +#if EV_USE_SELECT + if (backend == EVBACKEND_SELECT) select_destroy (EV_A); +#endif + + for (i = NUMPRI; i--; ) + { + array_free (pending, [i]); +#if EV_IDLE_ENABLE + array_free (idle, [i]); +#endif + } + + ev_free (anfds); anfds = 0; anfdmax = 0; + + /* have to use the microsoft-never-gets-it-right macro */ + array_free (rfeed, EMPTY); + array_free (fdchange, EMPTY); + array_free (timer, EMPTY); +#if EV_PERIODIC_ENABLE + array_free (periodic, EMPTY); +#endif +#if EV_FORK_ENABLE + array_free (fork, EMPTY); +#endif +#if EV_CLEANUP_ENABLE + array_free (cleanup, EMPTY); +#endif + array_free (prepare, EMPTY); + array_free (check, EMPTY); +#if EV_ASYNC_ENABLE + array_free (async, EMPTY); +#endif + + backend = 0; + +#if EV_MULTIPLICITY + if (ev_is_default_loop (EV_A)) +#endif + ev_default_loop_ptr = 0; +#if EV_MULTIPLICITY + else + ev_free (EV_A); +#endif +} + +#if EV_USE_INOTIFY +inline_size void infy_fork (EV_P); +#endif + +inline_size void +loop_fork (EV_P) +{ +#if EV_USE_PORT + if (backend == EVBACKEND_PORT ) port_fork (EV_A); +#endif +#if EV_USE_KQUEUE + if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); +#endif +#if EV_USE_EPOLL + if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); +#endif +#if EV_USE_INOTIFY + infy_fork (EV_A); +#endif + +#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE + if (ev_is_active (&pipe_w) && postfork != 2) + { + /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ + + ev_ref (EV_A); + ev_io_stop (EV_A_ &pipe_w); + + if (evpipe [0] >= 0) + EV_WIN32_CLOSE_FD (evpipe [0]); + + evpipe_init (EV_A); + /* iterate over everything, in case we missed something before */ + ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); + } +#endif + + postfork = 0; +} + +#if EV_MULTIPLICITY + +ecb_cold +struct ev_loop * +ev_loop_new (unsigned int flags) EV_THROW +{ + EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); + + memset (EV_A, 0, sizeof (struct ev_loop)); + loop_init (EV_A_ flags); + + if (ev_backend (EV_A)) + return EV_A; + + ev_free (EV_A); + return 0; +} + +#endif /* multiplicity */ + +#if EV_VERIFY +noinline ecb_cold +static void +verify_watcher (EV_P_ W w) +{ + assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); + + if (w->pending) + assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); +} + +noinline ecb_cold +static void +verify_heap (EV_P_ ANHE *heap, int N) +{ + int i; + + for (i = HEAP0; i < N + HEAP0; ++i) + { + assert (("libev: active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i)); + assert (("libev: heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i]))); + assert (("libev: heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i])))); + + verify_watcher (EV_A_ (W)ANHE_w (heap [i])); + } +} + +noinline ecb_cold +static void +array_verify (EV_P_ W *ws, int cnt) +{ + while (cnt--) + { + assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); + verify_watcher (EV_A_ ws [cnt]); + } +} +#endif + +#if EV_FEATURE_API +void ecb_cold +ev_verify (EV_P) EV_THROW +{ +#if EV_VERIFY + int i; + WL w, w2; + + assert (activecnt >= -1); + + assert (fdchangemax >= fdchangecnt); + for (i = 0; i < fdchangecnt; ++i) + assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); + + assert (anfdmax >= 0); + for (i = 0; i < anfdmax; ++i) + { + int j = 0; + + for (w = w2 = anfds [i].head; w; w = w->next) + { + verify_watcher (EV_A_ (W)w); + + if (j++ & 1) + { + assert (("libev: io watcher list contains a loop", w != w2)); + w2 = w2->next; + } + + assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); + assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); + } + } + + assert (timermax >= timercnt); + verify_heap (EV_A_ timers, timercnt); + +#if EV_PERIODIC_ENABLE + assert (periodicmax >= periodiccnt); + verify_heap (EV_A_ periodics, periodiccnt); +#endif + + for (i = NUMPRI; i--; ) + { + assert (pendingmax [i] >= pendingcnt [i]); +#if EV_IDLE_ENABLE + assert (idleall >= 0); + assert (idlemax [i] >= idlecnt [i]); + array_verify (EV_A_ (W *)idles [i], idlecnt [i]); +#endif + } + +#if EV_FORK_ENABLE + assert (forkmax >= forkcnt); + array_verify (EV_A_ (W *)forks, forkcnt); +#endif + +#if EV_CLEANUP_ENABLE + assert (cleanupmax >= cleanupcnt); + array_verify (EV_A_ (W *)cleanups, cleanupcnt); +#endif + +#if EV_ASYNC_ENABLE + assert (asyncmax >= asynccnt); + array_verify (EV_A_ (W *)asyncs, asynccnt); +#endif + +#if EV_PREPARE_ENABLE + assert (preparemax >= preparecnt); + array_verify (EV_A_ (W *)prepares, preparecnt); +#endif + +#if EV_CHECK_ENABLE + assert (checkmax >= checkcnt); + array_verify (EV_A_ (W *)checks, checkcnt); +#endif + +# if 0 +#if EV_CHILD_ENABLE + for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next) + for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) +#endif +# endif +#endif +} +#endif + +#if EV_MULTIPLICITY +ecb_cold +struct ev_loop * +#else +int +#endif +ev_default_loop (unsigned int flags) EV_THROW +{ + if (!ev_default_loop_ptr) + { +#if EV_MULTIPLICITY + EV_P = ev_default_loop_ptr = &default_loop_struct; +#else + ev_default_loop_ptr = 1; +#endif + + loop_init (EV_A_ flags); + + if (ev_backend (EV_A)) + { +#if EV_CHILD_ENABLE + ev_signal_init (&childev, childcb, SIGCHLD); + ev_set_priority (&childev, EV_MAXPRI); + ev_signal_start (EV_A_ &childev); + ev_unref (EV_A); /* child watcher should not keep loop alive */ +#endif + } + else + ev_default_loop_ptr = 0; + } + + return ev_default_loop_ptr; +} + +void +ev_loop_fork (EV_P) EV_THROW +{ + postfork = 1; +} + +/*****************************************************************************/ + +void +ev_invoke (EV_P_ void *w, int revents) +{ + EV_CB_INVOKE ((W)w, revents); +} + +unsigned int +ev_pending_count (EV_P) EV_THROW +{ + int pri; + unsigned int count = 0; + + for (pri = NUMPRI; pri--; ) + count += pendingcnt [pri]; + + return count; +} + +noinline +void +ev_invoke_pending (EV_P) +{ + pendingpri = NUMPRI; + + while (pendingpri) /* pendingpri possibly gets modified in the inner loop */ + { + --pendingpri; + + while (pendingcnt [pendingpri]) + { + ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; + + p->w->pending = 0; + EV_CB_INVOKE (p->w, p->events); + EV_FREQUENT_CHECK; + } + } +} + +#if EV_IDLE_ENABLE +/* make idle watchers pending. this handles the "call-idle */ +/* only when higher priorities are idle" logic */ +inline_size void +idle_reify (EV_P) +{ + if (expect_false (idleall)) + { + int pri; + + for (pri = NUMPRI; pri--; ) + { + if (pendingcnt [pri]) + break; + + if (idlecnt [pri]) + { + queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); + break; + } + } + } +} +#endif + +/* make timers pending */ +inline_size void +timers_reify (EV_P) +{ + EV_FREQUENT_CHECK; + + if (timercnt && ANHE_at (timers [HEAP0]) < mn_now) + { + do + { + ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); + + /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/ + + /* first reschedule or stop timer */ + if (w->repeat) + { + ev_at (w) += w->repeat; + if (ev_at (w) < mn_now) + ev_at (w) = mn_now; + + assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); + + ANHE_at_cache (timers [HEAP0]); + downheap (timers, timercnt, HEAP0); + } + else + ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ + + EV_FREQUENT_CHECK; + feed_reverse (EV_A_ (W)w); + } + while (timercnt && ANHE_at (timers [HEAP0]) < mn_now); + + feed_reverse_done (EV_A_ EV_TIMER); + } +} + +#if EV_PERIODIC_ENABLE + +noinline +static void +periodic_recalc (EV_P_ ev_periodic *w) +{ + ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL; + ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval); + + /* the above almost always errs on the low side */ + while (at <= ev_rt_now) + { + ev_tstamp nat = at + w->interval; + + /* when resolution fails us, we use ev_rt_now */ + if (expect_false (nat == at)) + { + at = ev_rt_now; + break; + } + + at = nat; + } + + ev_at (w) = at; +} + +/* make periodics pending */ +inline_size void +periodics_reify (EV_P) +{ + EV_FREQUENT_CHECK; + + while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) + { + do + { + ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); + + /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ + + /* first reschedule or stop timer */ + if (w->reschedule_cb) + { + ev_at (w) = w->reschedule_cb (w, ev_rt_now); + + assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); + + ANHE_at_cache (periodics [HEAP0]); + downheap (periodics, periodiccnt, HEAP0); + } + else if (w->interval) + { + periodic_recalc (EV_A_ w); + ANHE_at_cache (periodics [HEAP0]); + downheap (periodics, periodiccnt, HEAP0); + } + else + ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ + + EV_FREQUENT_CHECK; + feed_reverse (EV_A_ (W)w); + } + while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now); + + feed_reverse_done (EV_A_ EV_PERIODIC); + } +} + +/* simply recalculate all periodics */ +/* TODO: maybe ensure that at least one event happens when jumping forward? */ +noinline ecb_cold +static void +periodics_reschedule (EV_P) +{ + int i; + + /* adjust periodics after time jump */ + for (i = HEAP0; i < periodiccnt + HEAP0; ++i) + { + ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); + + if (w->reschedule_cb) + ev_at (w) = w->reschedule_cb (w, ev_rt_now); + else if (w->interval) + periodic_recalc (EV_A_ w); + + ANHE_at_cache (periodics [i]); + } + + reheap (periodics, periodiccnt); +} +#endif + +/* adjust all timers by a given offset */ +noinline ecb_cold +static void +timers_reschedule (EV_P_ ev_tstamp adjust) +{ + int i; + + for (i = 0; i < timercnt; ++i) + { + ANHE *he = timers + i + HEAP0; + ANHE_w (*he)->at += adjust; + ANHE_at_cache (*he); + } +} + +/* fetch new monotonic and realtime times from the kernel */ +/* also detect if there was a timejump, and act accordingly */ +inline_speed void +time_update (EV_P_ ev_tstamp max_block) +{ +#if EV_USE_MONOTONIC + if (expect_true (have_monotonic)) + { + int i; + ev_tstamp odiff = rtmn_diff; + + mn_now = get_clock (); + + /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ + /* interpolate in the meantime */ + if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) + { + ev_rt_now = rtmn_diff + mn_now; + return; + } + + now_floor = mn_now; + ev_rt_now = ev_time (); + + /* loop a few times, before making important decisions. + * on the choice of "4": one iteration isn't enough, + * in case we get preempted during the calls to + * ev_time and get_clock. a second call is almost guaranteed + * to succeed in that case, though. and looping a few more times + * doesn't hurt either as we only do this on time-jumps or + * in the unlikely event of having been preempted here. + */ + for (i = 4; --i; ) + { + ev_tstamp diff; + rtmn_diff = ev_rt_now - mn_now; + + diff = odiff - rtmn_diff; + + if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) + return; /* all is well */ + + ev_rt_now = ev_time (); + mn_now = get_clock (); + now_floor = mn_now; + } + + /* no timer adjustment, as the monotonic clock doesn't jump */ + /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ +# if EV_PERIODIC_ENABLE + periodics_reschedule (EV_A); +# endif + } + else +#endif + { + ev_rt_now = ev_time (); + + if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) + { + /* adjust timers. this is easy, as the offset is the same for all of them */ + timers_reschedule (EV_A_ ev_rt_now - mn_now); +#if EV_PERIODIC_ENABLE + periodics_reschedule (EV_A); +#endif + } + + mn_now = ev_rt_now; + } +} + +int +ev_run (EV_P_ int flags) +{ +#if EV_FEATURE_API + ++loop_depth; +#endif + + assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE)); + + loop_done = EVBREAK_CANCEL; + + EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ + + do + { +#if EV_VERIFY >= 2 + ev_verify (EV_A); +#endif + +#ifndef _WIN32 + if (expect_false (curpid)) /* penalise the forking check even more */ + if (expect_false (getpid () != curpid)) + { + curpid = getpid (); + postfork = 1; + } +#endif + +#if EV_FORK_ENABLE + /* we might have forked, so queue fork handlers */ + if (expect_false (postfork)) + if (forkcnt) + { + queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); + EV_INVOKE_PENDING; + } +#endif + +#if EV_PREPARE_ENABLE + /* queue prepare watchers (and execute them) */ + if (expect_false (preparecnt)) + { + queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); + EV_INVOKE_PENDING; + } +#endif + + if (expect_false (loop_done)) + break; + + /* we might have forked, so reify kernel state if necessary */ + if (expect_false (postfork)) + loop_fork (EV_A); + + /* update fd-related kernel structures */ + fd_reify (EV_A); + + /* calculate blocking time */ + { + ev_tstamp waittime = 0.; + ev_tstamp sleeptime = 0.; + + /* remember old timestamp for io_blocktime calculation */ + ev_tstamp prev_mn_now = mn_now; + + /* update time to cancel out callback processing overhead */ + time_update (EV_A_ 1e100); + + /* from now on, we want a pipe-wake-up */ + pipe_write_wanted = 1; + + ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ + + if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) + { + waittime = MAX_BLOCKTIME; + + if (timercnt) + { + ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; + if (waittime > to) waittime = to; + } + +#if EV_PERIODIC_ENABLE + if (periodiccnt) + { + ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; + if (waittime > to) waittime = to; + } +#endif + + /* don't let timeouts decrease the waittime below timeout_blocktime */ + if (expect_false (waittime < timeout_blocktime)) + waittime = timeout_blocktime; + + /* at this point, we NEED to wait, so we have to ensure */ + /* to pass a minimum nonzero value to the backend */ + if (expect_false (waittime < backend_mintime)) + waittime = backend_mintime; + + /* extra check because io_blocktime is commonly 0 */ + if (expect_false (io_blocktime)) + { + sleeptime = io_blocktime - (mn_now - prev_mn_now); + + if (sleeptime > waittime - backend_mintime) + sleeptime = waittime - backend_mintime; + + if (expect_true (sleeptime > 0.)) + { + ev_sleep (sleeptime); + waittime -= sleeptime; + } + } + } + +#if EV_FEATURE_API + ++loop_count; +#endif + assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ + backend_poll (EV_A_ waittime); + assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ + + pipe_write_wanted = 0; /* just an optimisation, no fence needed */ + + ECB_MEMORY_FENCE_ACQUIRE; + if (pipe_write_skipped) + { + assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); + ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); + } + + + /* update ev_rt_now, do magic */ + time_update (EV_A_ waittime + sleeptime); + } + + /* queue pending timers and reschedule them */ + timers_reify (EV_A); /* relative timers called last */ +#if EV_PERIODIC_ENABLE + periodics_reify (EV_A); /* absolute timers called first */ +#endif + +#if EV_IDLE_ENABLE + /* queue idle watchers unless other events are pending */ + idle_reify (EV_A); +#endif + +#if EV_CHECK_ENABLE + /* queue check watchers, to be executed first */ + if (expect_false (checkcnt)) + queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); +#endif + + EV_INVOKE_PENDING; + } + while (expect_true ( + activecnt + && !loop_done + && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT)) + )); + + if (loop_done == EVBREAK_ONE) + loop_done = EVBREAK_CANCEL; + +#if EV_FEATURE_API + --loop_depth; +#endif + + return activecnt; +} + +void +ev_break (EV_P_ int how) EV_THROW +{ + loop_done = how; +} + +void +ev_ref (EV_P) EV_THROW +{ + ++activecnt; +} + +void +ev_unref (EV_P) EV_THROW +{ + --activecnt; +} + +void +ev_now_update (EV_P) EV_THROW +{ + time_update (EV_A_ 1e100); +} + +void +ev_suspend (EV_P) EV_THROW +{ + ev_now_update (EV_A); +} + +void +ev_resume (EV_P) EV_THROW +{ + ev_tstamp mn_prev = mn_now; + + ev_now_update (EV_A); + timers_reschedule (EV_A_ mn_now - mn_prev); +#if EV_PERIODIC_ENABLE + /* TODO: really do this? */ + periodics_reschedule (EV_A); +#endif +} + +/*****************************************************************************/ +/* singly-linked list management, used when the expected list length is short */ + +inline_size void +wlist_add (WL *head, WL elem) +{ + elem->next = *head; + *head = elem; +} + +inline_size void +wlist_del (WL *head, WL elem) +{ + while (*head) + { + if (expect_true (*head == elem)) + { + *head = elem->next; + break; + } + + head = &(*head)->next; + } +} + +/* internal, faster, version of ev_clear_pending */ +inline_speed void +clear_pending (EV_P_ W w) +{ + if (w->pending) + { + pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w; + w->pending = 0; + } +} + +int +ev_clear_pending (EV_P_ void *w) EV_THROW +{ + W w_ = (W)w; + int pending = w_->pending; + + if (expect_true (pending)) + { + ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; + p->w = (W)&pending_w; + w_->pending = 0; + return p->events; + } + else + return 0; +} + +inline_size void +pri_adjust (EV_P_ W w) +{ + int pri = ev_priority (w); + pri = pri < EV_MINPRI ? EV_MINPRI : pri; + pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; + ev_set_priority (w, pri); +} + +inline_speed void +ev_start (EV_P_ W w, int active) +{ + pri_adjust (EV_A_ w); + w->active = active; + ev_ref (EV_A); +} + +inline_size void +ev_stop (EV_P_ W w) +{ + ev_unref (EV_A); + w->active = 0; +} + +/*****************************************************************************/ + +noinline +void +ev_io_start (EV_P_ ev_io *w) EV_THROW +{ + int fd = w->fd; + + if (expect_false (ev_is_active (w))) + return; + + assert (("libev: ev_io_start called with negative fd", fd >= 0)); + assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); + + EV_FREQUENT_CHECK; + + ev_start (EV_A_ (W)w, 1); + array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); + wlist_add (&anfds[fd].head, (WL)w); + + /* common bug, apparently */ + assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w)); + + fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); + w->events &= ~EV__IOFDSET; + + EV_FREQUENT_CHECK; +} + +noinline +void +ev_io_stop (EV_P_ ev_io *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); + + EV_FREQUENT_CHECK; + + wlist_del (&anfds[w->fd].head, (WL)w); + ev_stop (EV_A_ (W)w); + + fd_change (EV_A_ w->fd, EV_ANFD_REIFY); + + EV_FREQUENT_CHECK; +} + +noinline +void +ev_timer_start (EV_P_ ev_timer *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + ev_at (w) += mn_now; + + assert (("libev: ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); + + EV_FREQUENT_CHECK; + + ++timercnt; + ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1); + array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2); + ANHE_w (timers [ev_active (w)]) = (WT)w; + ANHE_at_cache (timers [ev_active (w)]); + upheap (timers, ev_active (w)); + + EV_FREQUENT_CHECK; + + /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ +} + +noinline +void +ev_timer_stop (EV_P_ ev_timer *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + { + int active = ev_active (w); + + assert (("libev: internal timer heap corruption", ANHE_w (timers [active]) == (WT)w)); + + --timercnt; + + if (expect_true (active < timercnt + HEAP0)) + { + timers [active] = timers [timercnt + HEAP0]; + adjustheap (timers, timercnt, active); + } + } + + ev_at (w) -= mn_now; + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} + +noinline +void +ev_timer_again (EV_P_ ev_timer *w) EV_THROW +{ + EV_FREQUENT_CHECK; + + clear_pending (EV_A_ (W)w); + + if (ev_is_active (w)) + { + if (w->repeat) + { + ev_at (w) = mn_now + w->repeat; + ANHE_at_cache (timers [ev_active (w)]); + adjustheap (timers, timercnt, ev_active (w)); + } + else + ev_timer_stop (EV_A_ w); + } + else if (w->repeat) + { + ev_at (w) = w->repeat; + ev_timer_start (EV_A_ w); + } + + EV_FREQUENT_CHECK; +} + +ev_tstamp +ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW +{ + return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); +} + +#if EV_PERIODIC_ENABLE +noinline +void +ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + if (w->reschedule_cb) + ev_at (w) = w->reschedule_cb (w, ev_rt_now); + else if (w->interval) + { + assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); + periodic_recalc (EV_A_ w); + } + else + ev_at (w) = w->offset; + + EV_FREQUENT_CHECK; + + ++periodiccnt; + ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1); + array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2); + ANHE_w (periodics [ev_active (w)]) = (WT)w; + ANHE_at_cache (periodics [ev_active (w)]); + upheap (periodics, ev_active (w)); + + EV_FREQUENT_CHECK; + + /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ +} + +noinline +void +ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + { + int active = ev_active (w); + + assert (("libev: internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w)); + + --periodiccnt; + + if (expect_true (active < periodiccnt + HEAP0)) + { + periodics [active] = periodics [periodiccnt + HEAP0]; + adjustheap (periodics, periodiccnt, active); + } + } + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} + +noinline +void +ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW +{ + /* TODO: use adjustheap and recalculation */ + ev_periodic_stop (EV_A_ w); + ev_periodic_start (EV_A_ w); +} +#endif + +#ifndef SA_RESTART +# define SA_RESTART 0 +#endif + +#if EV_SIGNAL_ENABLE + +noinline +void +ev_signal_start (EV_P_ ev_signal *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); + +#if EV_MULTIPLICITY + assert (("libev: a signal must not be attached to two different loops", + !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); + + signals [w->signum - 1].loop = EV_A; + ECB_MEMORY_FENCE_RELEASE; +#endif + + EV_FREQUENT_CHECK; + +#if EV_USE_SIGNALFD + if (sigfd == -2) + { + sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC); + if (sigfd < 0 && errno == EINVAL) + sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */ + + if (sigfd >= 0) + { + fd_intern (sigfd); /* doing it twice will not hurt */ + + sigemptyset (&sigfd_set); + + ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ); + ev_set_priority (&sigfd_w, EV_MAXPRI); + ev_io_start (EV_A_ &sigfd_w); + ev_unref (EV_A); /* signalfd watcher should not keep loop alive */ + } + } + + if (sigfd >= 0) + { + /* TODO: check .head */ + sigaddset (&sigfd_set, w->signum); + sigprocmask (SIG_BLOCK, &sigfd_set, 0); + + signalfd (sigfd, &sigfd_set, 0); + } +#endif + + ev_start (EV_A_ (W)w, 1); + wlist_add (&signals [w->signum - 1].head, (WL)w); + + if (!((WL)w)->next) +# if EV_USE_SIGNALFD + if (sigfd < 0) /*TODO*/ +# endif + { +# ifdef _WIN32 + evpipe_init (EV_A); + + signal (w->signum, ev_sighandler); +# else + struct sigaction sa; + + evpipe_init (EV_A); + + sa.sa_handler = ev_sighandler; + sigfillset (&sa.sa_mask); + sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ + sigaction (w->signum, &sa, 0); + + if (origflags & EVFLAG_NOSIGMASK) + { + sigemptyset (&sa.sa_mask); + sigaddset (&sa.sa_mask, w->signum); + sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); + } +#endif + } + + EV_FREQUENT_CHECK; +} + +noinline +void +ev_signal_stop (EV_P_ ev_signal *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + wlist_del (&signals [w->signum - 1].head, (WL)w); + ev_stop (EV_A_ (W)w); + + if (!signals [w->signum - 1].head) + { +#if EV_MULTIPLICITY + signals [w->signum - 1].loop = 0; /* unattach from signal */ +#endif +#if EV_USE_SIGNALFD + if (sigfd >= 0) + { + sigset_t ss; + + sigemptyset (&ss); + sigaddset (&ss, w->signum); + sigdelset (&sigfd_set, w->signum); + + signalfd (sigfd, &sigfd_set, 0); + sigprocmask (SIG_UNBLOCK, &ss, 0); + } + else +#endif + signal (w->signum, SIG_DFL); + } + + EV_FREQUENT_CHECK; +} + +#endif + +#if EV_CHILD_ENABLE + +void +ev_child_start (EV_P_ ev_child *w) EV_THROW +{ +#if EV_MULTIPLICITY + assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); +#endif + if (expect_false (ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + ev_start (EV_A_ (W)w, 1); + wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w); + + EV_FREQUENT_CHECK; +} + +void +ev_child_stop (EV_P_ ev_child *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w); + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} + +#endif + +#if EV_STAT_ENABLE + +# ifdef _WIN32 +# undef lstat +# define lstat(a,b) _stati64 (a,b) +# endif + +#define DEF_STAT_INTERVAL 5.0074891 +#define NFS_STAT_INTERVAL 30.1074891 /* for filesystems potentially failing inotify */ +#define MIN_STAT_INTERVAL 0.1074891 + +noinline static void stat_timer_cb (EV_P_ ev_timer *w_, int revents); + +#if EV_USE_INOTIFY + +/* the * 2 is to allow for alignment padding, which for some reason is >> 8 */ +# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) + +noinline +static void +infy_add (EV_P_ ev_stat *w) +{ + w->wd = inotify_add_watch (fs_fd, w->path, + IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY + | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO + | IN_DONT_FOLLOW | IN_MASK_ADD); + + if (w->wd >= 0) + { + struct statfs sfs; + + /* now local changes will be tracked by inotify, but remote changes won't */ + /* unless the filesystem is known to be local, we therefore still poll */ + /* also do poll on <2.6.25, but with normal frequency */ + + if (!fs_2625) + w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; + else if (!statfs (w->path, &sfs) + && (sfs.f_type == 0x1373 /* devfs */ + || sfs.f_type == 0x4006 /* fat */ + || sfs.f_type == 0x4d44 /* msdos */ + || sfs.f_type == 0xEF53 /* ext2/3 */ + || sfs.f_type == 0x72b6 /* jffs2 */ + || sfs.f_type == 0x858458f6 /* ramfs */ + || sfs.f_type == 0x5346544e /* ntfs */ + || sfs.f_type == 0x3153464a /* jfs */ + || sfs.f_type == 0x9123683e /* btrfs */ + || sfs.f_type == 0x52654973 /* reiser3 */ + || sfs.f_type == 0x01021994 /* tmpfs */ + || sfs.f_type == 0x58465342 /* xfs */)) + w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ + else + w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ + } + else + { + /* can't use inotify, continue to stat */ + w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; + + /* if path is not there, monitor some parent directory for speedup hints */ + /* note that exceeding the hardcoded path limit is not a correctness issue, */ + /* but an efficiency issue only */ + if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) + { + char path [4096]; + strcpy (path, w->path); + + do + { + int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF + | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO); + + char *pend = strrchr (path, '/'); + + if (!pend || pend == path) + break; + + *pend = 0; + w->wd = inotify_add_watch (fs_fd, path, mask); + } + while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); + } + } + + if (w->wd >= 0) + wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w); + + /* now re-arm timer, if required */ + if (ev_is_active (&w->timer)) ev_ref (EV_A); + ev_timer_again (EV_A_ &w->timer); + if (ev_is_active (&w->timer)) ev_unref (EV_A); +} + +noinline +static void +infy_del (EV_P_ ev_stat *w) +{ + int slot; + int wd = w->wd; + + if (wd < 0) + return; + + w->wd = -2; + slot = wd & ((EV_INOTIFY_HASHSIZE) - 1); + wlist_del (&fs_hash [slot].head, (WL)w); + + /* remove this watcher, if others are watching it, they will rearm */ + inotify_rm_watch (fs_fd, wd); +} + +noinline +static void +infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) +{ + if (slot < 0) + /* overflow, need to check for all hash slots */ + for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot) + infy_wd (EV_A_ slot, wd, ev); + else + { + WL w_; + + for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; ) + { + ev_stat *w = (ev_stat *)w_; + w_ = w_->next; /* lets us remove this watcher and all before it */ + + if (w->wd == wd || wd == -1) + { + if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF)) + { + wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w); + w->wd = -1; + infy_add (EV_A_ w); /* re-add, no matter what */ + } + + stat_timer_cb (EV_A_ &w->timer, 0); + } + } + } +} + +static void +infy_cb (EV_P_ ev_io *w, int revents) +{ + char buf [EV_INOTIFY_BUFSIZE]; + int ofs; + int len = read (fs_fd, buf, sizeof (buf)); + + for (ofs = 0; ofs < len; ) + { + struct inotify_event *ev = (struct inotify_event *)(buf + ofs); + infy_wd (EV_A_ ev->wd, ev->wd, ev); + ofs += sizeof (struct inotify_event) + ev->len; + } +} + +inline_size ecb_cold +void +ev_check_2625 (EV_P) +{ + /* kernels < 2.6.25 are borked + * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html + */ + if (ev_linux_version () < 0x020619) + return; + + fs_2625 = 1; +} + +inline_size int +infy_newfd (void) +{ +#if defined IN_CLOEXEC && defined IN_NONBLOCK + int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); + if (fd >= 0) + return fd; +#endif + return inotify_init (); +} + +inline_size void +infy_init (EV_P) +{ + if (fs_fd != -2) + return; + + fs_fd = -1; + + ev_check_2625 (EV_A); + + fs_fd = infy_newfd (); + + if (fs_fd >= 0) + { + fd_intern (fs_fd); + ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); + ev_set_priority (&fs_w, EV_MAXPRI); + ev_io_start (EV_A_ &fs_w); + ev_unref (EV_A); + } +} + +inline_size void +infy_fork (EV_P) +{ + int slot; + + if (fs_fd < 0) + return; + + ev_ref (EV_A); + ev_io_stop (EV_A_ &fs_w); + close (fs_fd); + fs_fd = infy_newfd (); + + if (fs_fd >= 0) + { + fd_intern (fs_fd); + ev_io_set (&fs_w, fs_fd, EV_READ); + ev_io_start (EV_A_ &fs_w); + ev_unref (EV_A); + } + + for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot) + { + WL w_ = fs_hash [slot].head; + fs_hash [slot].head = 0; + + while (w_) + { + ev_stat *w = (ev_stat *)w_; + w_ = w_->next; /* lets us add this watcher */ + + w->wd = -1; + + if (fs_fd >= 0) + infy_add (EV_A_ w); /* re-add, no matter what */ + else + { + w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; + if (ev_is_active (&w->timer)) ev_ref (EV_A); + ev_timer_again (EV_A_ &w->timer); + if (ev_is_active (&w->timer)) ev_unref (EV_A); + } + } + } +} + +#endif + +#ifdef _WIN32 +# define EV_LSTAT(p,b) _stati64 (p, b) +#else +# define EV_LSTAT(p,b) lstat (p, b) +#endif + +void +ev_stat_stat (EV_P_ ev_stat *w) EV_THROW +{ + if (lstat (w->path, &w->attr) < 0) + w->attr.st_nlink = 0; + else if (!w->attr.st_nlink) + w->attr.st_nlink = 1; +} + +noinline +static void +stat_timer_cb (EV_P_ ev_timer *w_, int revents) +{ + ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); + + ev_statdata prev = w->attr; + ev_stat_stat (EV_A_ w); + + /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ + if ( + prev.st_dev != w->attr.st_dev + || prev.st_ino != w->attr.st_ino + || prev.st_mode != w->attr.st_mode + || prev.st_nlink != w->attr.st_nlink + || prev.st_uid != w->attr.st_uid + || prev.st_gid != w->attr.st_gid + || prev.st_rdev != w->attr.st_rdev + || prev.st_size != w->attr.st_size + || prev.st_atime != w->attr.st_atime + || prev.st_mtime != w->attr.st_mtime + || prev.st_ctime != w->attr.st_ctime + ) { + /* we only update w->prev on actual differences */ + /* in case we test more often than invoke the callback, */ + /* to ensure that prev is always different to attr */ + w->prev = prev; + + #if EV_USE_INOTIFY + if (fs_fd >= 0) + { + infy_del (EV_A_ w); + infy_add (EV_A_ w); + ev_stat_stat (EV_A_ w); /* avoid race... */ + } + #endif + + ev_feed_event (EV_A_ w, EV_STAT); + } +} + +void +ev_stat_start (EV_P_ ev_stat *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + ev_stat_stat (EV_A_ w); + + if (w->interval < MIN_STAT_INTERVAL && w->interval) + w->interval = MIN_STAT_INTERVAL; + + ev_timer_init (&w->timer, stat_timer_cb, 0., w->interval ? w->interval : DEF_STAT_INTERVAL); + ev_set_priority (&w->timer, ev_priority (w)); + +#if EV_USE_INOTIFY + infy_init (EV_A); + + if (fs_fd >= 0) + infy_add (EV_A_ w); + else +#endif + { + ev_timer_again (EV_A_ &w->timer); + ev_unref (EV_A); + } + + ev_start (EV_A_ (W)w, 1); + + EV_FREQUENT_CHECK; +} + +void +ev_stat_stop (EV_P_ ev_stat *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + +#if EV_USE_INOTIFY + infy_del (EV_A_ w); +#endif + + if (ev_is_active (&w->timer)) + { + ev_ref (EV_A); + ev_timer_stop (EV_A_ &w->timer); + } + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} +#endif + +#if EV_IDLE_ENABLE +void +ev_idle_start (EV_P_ ev_idle *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + pri_adjust (EV_A_ (W)w); + + EV_FREQUENT_CHECK; + + { + int active = ++idlecnt [ABSPRI (w)]; + + ++idleall; + ev_start (EV_A_ (W)w, active); + + array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2); + idles [ABSPRI (w)][active - 1] = w; + } + + EV_FREQUENT_CHECK; +} + +void +ev_idle_stop (EV_P_ ev_idle *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + { + int active = ev_active (w); + + idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; + ev_active (idles [ABSPRI (w)][active - 1]) = active; + + ev_stop (EV_A_ (W)w); + --idleall; + } + + EV_FREQUENT_CHECK; +} +#endif + +#if EV_PREPARE_ENABLE +void +ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + ev_start (EV_A_ (W)w, ++preparecnt); + array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); + prepares [preparecnt - 1] = w; + + EV_FREQUENT_CHECK; +} + +void +ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + { + int active = ev_active (w); + + prepares [active - 1] = prepares [--preparecnt]; + ev_active (prepares [active - 1]) = active; + } + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} +#endif + +#if EV_CHECK_ENABLE +void +ev_check_start (EV_P_ ev_check *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + ev_start (EV_A_ (W)w, ++checkcnt); + array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); + checks [checkcnt - 1] = w; + + EV_FREQUENT_CHECK; +} + +void +ev_check_stop (EV_P_ ev_check *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + { + int active = ev_active (w); + + checks [active - 1] = checks [--checkcnt]; + ev_active (checks [active - 1]) = active; + } + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} +#endif + +#if EV_EMBED_ENABLE +noinline +void +ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW +{ + ev_run (w->other, EVRUN_NOWAIT); +} + +static void +embed_io_cb (EV_P_ ev_io *io, int revents) +{ + ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); + + if (ev_cb (w)) + ev_feed_event (EV_A_ (W)w, EV_EMBED); + else + ev_run (w->other, EVRUN_NOWAIT); +} + +static void +embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) +{ + ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); + + { + EV_P = w->other; + + while (fdchangecnt) + { + fd_reify (EV_A); + ev_run (EV_A_ EVRUN_NOWAIT); + } + } +} + +static void +embed_fork_cb (EV_P_ ev_fork *fork_w, int revents) +{ + ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); + + ev_embed_stop (EV_A_ w); + + { + EV_P = w->other; + + ev_loop_fork (EV_A); + ev_run (EV_A_ EVRUN_NOWAIT); + } + + ev_embed_start (EV_A_ w); +} + +#if 0 +static void +embed_idle_cb (EV_P_ ev_idle *idle, int revents) +{ + ev_idle_stop (EV_A_ idle); +} +#endif + +void +ev_embed_start (EV_P_ ev_embed *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + { + EV_P = w->other; + assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); + ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); + } + + EV_FREQUENT_CHECK; + + ev_set_priority (&w->io, ev_priority (w)); + ev_io_start (EV_A_ &w->io); + + ev_prepare_init (&w->prepare, embed_prepare_cb); + ev_set_priority (&w->prepare, EV_MINPRI); + ev_prepare_start (EV_A_ &w->prepare); + + ev_fork_init (&w->fork, embed_fork_cb); + ev_fork_start (EV_A_ &w->fork); + + /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ + + ev_start (EV_A_ (W)w, 1); + + EV_FREQUENT_CHECK; +} + +void +ev_embed_stop (EV_P_ ev_embed *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + ev_io_stop (EV_A_ &w->io); + ev_prepare_stop (EV_A_ &w->prepare); + ev_fork_stop (EV_A_ &w->fork); + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} +#endif + +#if EV_FORK_ENABLE +void +ev_fork_start (EV_P_ ev_fork *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + ev_start (EV_A_ (W)w, ++forkcnt); + array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2); + forks [forkcnt - 1] = w; + + EV_FREQUENT_CHECK; +} + +void +ev_fork_stop (EV_P_ ev_fork *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + { + int active = ev_active (w); + + forks [active - 1] = forks [--forkcnt]; + ev_active (forks [active - 1]) = active; + } + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} +#endif + +#if EV_CLEANUP_ENABLE +void +ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + ev_start (EV_A_ (W)w, ++cleanupcnt); + array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2); + cleanups [cleanupcnt - 1] = w; + + /* cleanup watchers should never keep a refcount on the loop */ + ev_unref (EV_A); + EV_FREQUENT_CHECK; +} + +void +ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + ev_ref (EV_A); + + { + int active = ev_active (w); + + cleanups [active - 1] = cleanups [--cleanupcnt]; + ev_active (cleanups [active - 1]) = active; + } + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} +#endif + +#if EV_ASYNC_ENABLE +void +ev_async_start (EV_P_ ev_async *w) EV_THROW +{ + if (expect_false (ev_is_active (w))) + return; + + w->sent = 0; + + evpipe_init (EV_A); + + EV_FREQUENT_CHECK; + + ev_start (EV_A_ (W)w, ++asynccnt); + array_needsize (ev_async *, asyncs, asyncmax, asynccnt, EMPTY2); + asyncs [asynccnt - 1] = w; + + EV_FREQUENT_CHECK; +} + +void +ev_async_stop (EV_P_ ev_async *w) EV_THROW +{ + clear_pending (EV_A_ (W)w); + if (expect_false (!ev_is_active (w))) + return; + + EV_FREQUENT_CHECK; + + { + int active = ev_active (w); + + asyncs [active - 1] = asyncs [--asynccnt]; + ev_active (asyncs [active - 1]) = active; + } + + ev_stop (EV_A_ (W)w); + + EV_FREQUENT_CHECK; +} + +void +ev_async_send (EV_P_ ev_async *w) EV_THROW +{ + w->sent = 1; + evpipe_write (EV_A_ &async_pending); +} +#endif + +/*****************************************************************************/ + +struct ev_once +{ + ev_io io; + ev_timer to; + void (*cb)(int revents, void *arg); + void *arg; +}; + +static void +once_cb (EV_P_ struct ev_once *once, int revents) +{ + void (*cb)(int revents, void *arg) = once->cb; + void *arg = once->arg; + + ev_io_stop (EV_A_ &once->io); + ev_timer_stop (EV_A_ &once->to); + ev_free (once); + + cb (revents, arg); +} + +static void +once_cb_io (EV_P_ ev_io *w, int revents) +{ + struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)); + + once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->to)); +} + +static void +once_cb_to (EV_P_ ev_timer *w, int revents) +{ + struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)); + + once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); +} + +void +ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW +{ + struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); + + if (expect_false (!once)) + { + cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg); + return; + } + + once->cb = cb; + once->arg = arg; + + ev_init (&once->io, once_cb_io); + if (fd >= 0) + { + ev_io_set (&once->io, fd, events); + ev_io_start (EV_A_ &once->io); + } + + ev_init (&once->to, once_cb_to); + if (timeout >= 0.) + { + ev_timer_set (&once->to, timeout, 0.); + ev_timer_start (EV_A_ &once->to); + } +} + +/*****************************************************************************/ + +#if EV_WALK_ENABLE +ecb_cold +void +ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW +{ + int i, j; + ev_watcher_list *wl, *wn; + + if (types & (EV_IO | EV_EMBED)) + for (i = 0; i < anfdmax; ++i) + for (wl = anfds [i].head; wl; ) + { + wn = wl->next; + +#if EV_EMBED_ENABLE + if (ev_cb ((ev_io *)wl) == embed_io_cb) + { + if (types & EV_EMBED) + cb (EV_A_ EV_EMBED, ((char *)wl) - offsetof (struct ev_embed, io)); + } + else +#endif +#if EV_USE_INOTIFY + if (ev_cb ((ev_io *)wl) == infy_cb) + ; + else +#endif + if ((ev_io *)wl != &pipe_w) + if (types & EV_IO) + cb (EV_A_ EV_IO, wl); + + wl = wn; + } + + if (types & (EV_TIMER | EV_STAT)) + for (i = timercnt + HEAP0; i-- > HEAP0; ) +#if EV_STAT_ENABLE + /*TODO: timer is not always active*/ + if (ev_cb ((ev_timer *)ANHE_w (timers [i])) == stat_timer_cb) + { + if (types & EV_STAT) + cb (EV_A_ EV_STAT, ((char *)ANHE_w (timers [i])) - offsetof (struct ev_stat, timer)); + } + else +#endif + if (types & EV_TIMER) + cb (EV_A_ EV_TIMER, ANHE_w (timers [i])); + +#if EV_PERIODIC_ENABLE + if (types & EV_PERIODIC) + for (i = periodiccnt + HEAP0; i-- > HEAP0; ) + cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); +#endif + +#if EV_IDLE_ENABLE + if (types & EV_IDLE) + for (j = NUMPRI; j--; ) + for (i = idlecnt [j]; i--; ) + cb (EV_A_ EV_IDLE, idles [j][i]); +#endif + +#if EV_FORK_ENABLE + if (types & EV_FORK) + for (i = forkcnt; i--; ) + if (ev_cb (forks [i]) != embed_fork_cb) + cb (EV_A_ EV_FORK, forks [i]); +#endif + +#if EV_ASYNC_ENABLE + if (types & EV_ASYNC) + for (i = asynccnt; i--; ) + cb (EV_A_ EV_ASYNC, asyncs [i]); +#endif + +#if EV_PREPARE_ENABLE + if (types & EV_PREPARE) + for (i = preparecnt; i--; ) +# if EV_EMBED_ENABLE + if (ev_cb (prepares [i]) != embed_prepare_cb) +# endif + cb (EV_A_ EV_PREPARE, prepares [i]); +#endif + +#if EV_CHECK_ENABLE + if (types & EV_CHECK) + for (i = checkcnt; i--; ) + cb (EV_A_ EV_CHECK, checks [i]); +#endif + +#if EV_SIGNAL_ENABLE + if (types & EV_SIGNAL) + for (i = 0; i < EV_NSIG - 1; ++i) + for (wl = signals [i].head; wl; ) + { + wn = wl->next; + cb (EV_A_ EV_SIGNAL, wl); + wl = wn; + } +#endif + +#if EV_CHILD_ENABLE + if (types & EV_CHILD) + for (i = (EV_PID_HASHSIZE); i--; ) + for (wl = childs [i]; wl; ) + { + wn = wl->next; + cb (EV_A_ EV_CHILD, wl); + wl = wn; + } +#endif +/* EV_STAT 0x00001000 /* stat data changed */ +/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ +} +#endif + +#if EV_MULTIPLICITY + #include "ev_wrap.h" +#endif + diff --git a/third_party/libev/ev.h b/third_party/libev/ev.h new file mode 100644 index 00000000..db937776 --- /dev/null +++ b/third_party/libev/ev.h @@ -0,0 +1,854 @@ +/* + * libev native API header + * + * Copyright (c) 2007,2008,2009,2010,2011,2012,2015 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#ifndef EV_H_ +#define EV_H_ + +#ifdef __cplusplus +# define EV_CPP(x) x +# if __cplusplus >= 201103L +# define EV_THROW noexcept +# else +# define EV_THROW throw () +# endif +#else +# define EV_CPP(x) +# define EV_THROW +#endif + +EV_CPP(extern "C" {) + +/*****************************************************************************/ + +/* pre-4.0 compatibility */ +#ifndef EV_COMPAT3 +# define EV_COMPAT3 1 +#endif + +#ifndef EV_FEATURES +# if defined __OPTIMIZE_SIZE__ +# define EV_FEATURES 0x7c +# else +# define EV_FEATURES 0x7f +# endif +#endif + +#define EV_FEATURE_CODE ((EV_FEATURES) & 1) +#define EV_FEATURE_DATA ((EV_FEATURES) & 2) +#define EV_FEATURE_CONFIG ((EV_FEATURES) & 4) +#define EV_FEATURE_API ((EV_FEATURES) & 8) +#define EV_FEATURE_WATCHERS ((EV_FEATURES) & 16) +#define EV_FEATURE_BACKENDS ((EV_FEATURES) & 32) +#define EV_FEATURE_OS ((EV_FEATURES) & 64) + +/* these priorities are inclusive, higher priorities will be invoked earlier */ +#ifndef EV_MINPRI +# define EV_MINPRI (EV_FEATURE_CONFIG ? -2 : 0) +#endif +#ifndef EV_MAXPRI +# define EV_MAXPRI (EV_FEATURE_CONFIG ? +2 : 0) +#endif + +#ifndef EV_MULTIPLICITY +# define EV_MULTIPLICITY EV_FEATURE_CONFIG +#endif + +#ifndef EV_PERIODIC_ENABLE +# define EV_PERIODIC_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_STAT_ENABLE +# define EV_STAT_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_PREPARE_ENABLE +# define EV_PREPARE_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_CHECK_ENABLE +# define EV_CHECK_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_IDLE_ENABLE +# define EV_IDLE_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_FORK_ENABLE +# define EV_FORK_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_CLEANUP_ENABLE +# define EV_CLEANUP_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_SIGNAL_ENABLE +# define EV_SIGNAL_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_CHILD_ENABLE +# ifdef _WIN32 +# define EV_CHILD_ENABLE 0 +# else +# define EV_CHILD_ENABLE EV_FEATURE_WATCHERS +#endif +#endif + +#ifndef EV_ASYNC_ENABLE +# define EV_ASYNC_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_EMBED_ENABLE +# define EV_EMBED_ENABLE EV_FEATURE_WATCHERS +#endif + +#ifndef EV_WALK_ENABLE +# define EV_WALK_ENABLE 0 /* not yet */ +#endif + +/*****************************************************************************/ + +#if EV_CHILD_ENABLE && !EV_SIGNAL_ENABLE +# undef EV_SIGNAL_ENABLE +# define EV_SIGNAL_ENABLE 1 +#endif + +/*****************************************************************************/ + +typedef double ev_tstamp; + +#include /* for memmove */ + +#ifndef EV_ATOMIC_T +# include +# define EV_ATOMIC_T sig_atomic_t volatile +#endif + +#if EV_STAT_ENABLE +# ifdef _WIN32 +# include +# include +# endif +# include +#endif + +/* support multiple event loops? */ +#if EV_MULTIPLICITY +struct ev_loop; +# define EV_P struct ev_loop *loop /* a loop as sole parameter in a declaration */ +# define EV_P_ EV_P, /* a loop as first of multiple parameters */ +# define EV_A loop /* a loop as sole argument to a function call */ +# define EV_A_ EV_A, /* a loop as first of multiple arguments */ +# define EV_DEFAULT_UC ev_default_loop_uc_ () /* the default loop, if initialised, as sole arg */ +# define EV_DEFAULT_UC_ EV_DEFAULT_UC, /* the default loop as first of multiple arguments */ +# define EV_DEFAULT ev_default_loop (0) /* the default loop as sole arg */ +# define EV_DEFAULT_ EV_DEFAULT, /* the default loop as first of multiple arguments */ +#else +# define EV_P void +# define EV_P_ +# define EV_A +# define EV_A_ +# define EV_DEFAULT +# define EV_DEFAULT_ +# define EV_DEFAULT_UC +# define EV_DEFAULT_UC_ +# undef EV_EMBED_ENABLE +#endif + +/* EV_INLINE is used for functions in header files */ +#if __STDC_VERSION__ >= 199901L || __GNUC__ >= 3 +# define EV_INLINE static inline +#else +# define EV_INLINE static +#endif + +#ifdef EV_API_STATIC +# define EV_API_DECL static +#else +# define EV_API_DECL extern +#endif + +/* EV_PROTOTYPES can be used to switch of prototype declarations */ +#ifndef EV_PROTOTYPES +# define EV_PROTOTYPES 1 +#endif + +/*****************************************************************************/ + +#define EV_VERSION_MAJOR 4 +#define EV_VERSION_MINOR 24 + +/* eventmask, revents, events... */ +enum { + EV_UNDEF = (int)0xFFFFFFFF, /* guaranteed to be invalid */ + EV_NONE = 0x00, /* no events */ + EV_READ = 0x01, /* ev_io detected read will not block */ + EV_WRITE = 0x02, /* ev_io detected write will not block */ + EV__IOFDSET = 0x80, /* internal use only */ + EV_IO = EV_READ, /* alias for type-detection */ + EV_TIMER = 0x00000100, /* timer timed out */ +#if EV_COMPAT3 + EV_TIMEOUT = EV_TIMER, /* pre 4.0 API compatibility */ +#endif + EV_PERIODIC = 0x00000200, /* periodic timer timed out */ + EV_SIGNAL = 0x00000400, /* signal was received */ + EV_CHILD = 0x00000800, /* child/pid had status change */ + EV_STAT = 0x00001000, /* stat data changed */ + EV_IDLE = 0x00002000, /* event loop is idling */ + EV_PREPARE = 0x00004000, /* event loop about to poll */ + EV_CHECK = 0x00008000, /* event loop finished poll */ + EV_EMBED = 0x00010000, /* embedded event loop needs sweep */ + EV_FORK = 0x00020000, /* event loop resumed in child */ + EV_CLEANUP = 0x00040000, /* event loop resumed in child */ + EV_ASYNC = 0x00080000, /* async intra-loop signal */ + EV_CUSTOM = 0x01000000, /* for use by user code */ + EV_ERROR = (int)0x80000000 /* sent when an error occurs */ +}; + +/* can be used to add custom fields to all watchers, while losing binary compatibility */ +#ifndef EV_COMMON +# define EV_COMMON void *data; +#endif + +#ifndef EV_CB_DECLARE +# define EV_CB_DECLARE(type) void (*cb)(EV_P_ struct type *w, int revents); +#endif +#ifndef EV_CB_INVOKE +# define EV_CB_INVOKE(watcher,revents) (watcher)->cb (EV_A_ (watcher), (revents)) +#endif + +/* not official, do not use */ +#define EV_CB(type,name) void name (EV_P_ struct ev_ ## type *w, int revents) + +/* + * struct member types: + * private: you may look at them, but not change them, + * and they might not mean anything to you. + * ro: can be read anytime, but only changed when the watcher isn't active. + * rw: can be read and modified anytime, even when the watcher is active. + * + * some internal details that might be helpful for debugging: + * + * active is either 0, which means the watcher is not active, + * or the array index of the watcher (periodics, timers) + * or the array index + 1 (most other watchers) + * or simply 1 for watchers that aren't in some array. + * pending is either 0, in which case the watcher isn't, + * or the array index + 1 in the pendings array. + */ + +#if EV_MINPRI == EV_MAXPRI +# define EV_DECL_PRIORITY +#elif !defined (EV_DECL_PRIORITY) +# define EV_DECL_PRIORITY int priority; +#endif + +/* shared by all watchers */ +#define EV_WATCHER(type) \ + int active; /* private */ \ + int pending; /* private */ \ + EV_DECL_PRIORITY /* private */ \ + EV_COMMON /* rw */ \ + EV_CB_DECLARE (type) /* private */ + +#define EV_WATCHER_LIST(type) \ + EV_WATCHER (type) \ + struct ev_watcher_list *next; /* private */ + +#define EV_WATCHER_TIME(type) \ + EV_WATCHER (type) \ + ev_tstamp at; /* private */ + +/* base class, nothing to see here unless you subclass */ +typedef struct ev_watcher +{ + EV_WATCHER (ev_watcher) +} ev_watcher; + +/* base class, nothing to see here unless you subclass */ +typedef struct ev_watcher_list +{ + EV_WATCHER_LIST (ev_watcher_list) +} ev_watcher_list; + +/* base class, nothing to see here unless you subclass */ +typedef struct ev_watcher_time +{ + EV_WATCHER_TIME (ev_watcher_time) +} ev_watcher_time; + +/* invoked when fd is either EV_READable or EV_WRITEable */ +/* revent EV_READ, EV_WRITE */ +typedef struct ev_io +{ + EV_WATCHER_LIST (ev_io) + + int fd; /* ro */ + int events; /* ro */ +} ev_io; + +/* invoked after a specific time, repeatable (based on monotonic clock) */ +/* revent EV_TIMEOUT */ +typedef struct ev_timer +{ + EV_WATCHER_TIME (ev_timer) + + ev_tstamp repeat; /* rw */ +} ev_timer; + +/* invoked at some specific time, possibly repeating at regular intervals (based on UTC) */ +/* revent EV_PERIODIC */ +typedef struct ev_periodic +{ + EV_WATCHER_TIME (ev_periodic) + + ev_tstamp offset; /* rw */ + ev_tstamp interval; /* rw */ + ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) EV_THROW; /* rw */ +} ev_periodic; + +/* invoked when the given signal has been received */ +/* revent EV_SIGNAL */ +typedef struct ev_signal +{ + EV_WATCHER_LIST (ev_signal) + + int signum; /* ro */ +} ev_signal; + +/* invoked when sigchld is received and waitpid indicates the given pid */ +/* revent EV_CHILD */ +/* does not support priorities */ +typedef struct ev_child +{ + EV_WATCHER_LIST (ev_child) + + int flags; /* private */ + int pid; /* ro */ + int rpid; /* rw, holds the received pid */ + int rstatus; /* rw, holds the exit status, use the macros from sys/wait.h */ +} ev_child; + +#if EV_STAT_ENABLE +/* st_nlink = 0 means missing file or other error */ +# ifdef _WIN32 +typedef struct _stati64 ev_statdata; +# else +typedef struct stat ev_statdata; +# endif + +/* invoked each time the stat data changes for a given path */ +/* revent EV_STAT */ +typedef struct ev_stat +{ + EV_WATCHER_LIST (ev_stat) + + ev_timer timer; /* private */ + ev_tstamp interval; /* ro */ + const char *path; /* ro */ + ev_statdata prev; /* ro */ + ev_statdata attr; /* ro */ + + int wd; /* wd for inotify, fd for kqueue */ +} ev_stat; +#endif + +#if EV_IDLE_ENABLE +/* invoked when the nothing else needs to be done, keeps the process from blocking */ +/* revent EV_IDLE */ +typedef struct ev_idle +{ + EV_WATCHER (ev_idle) +} ev_idle; +#endif + +/* invoked for each run of the mainloop, just before the blocking call */ +/* you can still change events in any way you like */ +/* revent EV_PREPARE */ +typedef struct ev_prepare +{ + EV_WATCHER (ev_prepare) +} ev_prepare; + +/* invoked for each run of the mainloop, just after the blocking call */ +/* revent EV_CHECK */ +typedef struct ev_check +{ + EV_WATCHER (ev_check) +} ev_check; + +#if EV_FORK_ENABLE +/* the callback gets invoked before check in the child process when a fork was detected */ +/* revent EV_FORK */ +typedef struct ev_fork +{ + EV_WATCHER (ev_fork) +} ev_fork; +#endif + +#if EV_CLEANUP_ENABLE +/* is invoked just before the loop gets destroyed */ +/* revent EV_CLEANUP */ +typedef struct ev_cleanup +{ + EV_WATCHER (ev_cleanup) +} ev_cleanup; +#endif + +#if EV_EMBED_ENABLE +/* used to embed an event loop inside another */ +/* the callback gets invoked when the event loop has handled events, and can be 0 */ +typedef struct ev_embed +{ + EV_WATCHER (ev_embed) + + struct ev_loop *other; /* ro */ + ev_io io; /* private */ + ev_prepare prepare; /* private */ + ev_check check; /* unused */ + ev_timer timer; /* unused */ + ev_periodic periodic; /* unused */ + ev_idle idle; /* unused */ + ev_fork fork; /* private */ +#if EV_CLEANUP_ENABLE + ev_cleanup cleanup; /* unused */ +#endif +} ev_embed; +#endif + +#if EV_ASYNC_ENABLE +/* invoked when somebody calls ev_async_send on the watcher */ +/* revent EV_ASYNC */ +typedef struct ev_async +{ + EV_WATCHER (ev_async) + + EV_ATOMIC_T sent; /* private */ +} ev_async; + +# define ev_async_pending(w) (+(w)->sent) +#endif + +/* the presence of this union forces similar struct layout */ +union ev_any_watcher +{ + struct ev_watcher w; + struct ev_watcher_list wl; + + struct ev_io io; + struct ev_timer timer; + struct ev_periodic periodic; + struct ev_signal signal; + struct ev_child child; +#if EV_STAT_ENABLE + struct ev_stat stat; +#endif +#if EV_IDLE_ENABLE + struct ev_idle idle; +#endif + struct ev_prepare prepare; + struct ev_check check; +#if EV_FORK_ENABLE + struct ev_fork fork; +#endif +#if EV_CLEANUP_ENABLE + struct ev_cleanup cleanup; +#endif +#if EV_EMBED_ENABLE + struct ev_embed embed; +#endif +#if EV_ASYNC_ENABLE + struct ev_async async; +#endif +}; + +/* flag bits for ev_default_loop and ev_loop_new */ +enum { + /* the default */ + EVFLAG_AUTO = 0x00000000U, /* not quite a mask */ + /* flag bits */ + EVFLAG_NOENV = 0x01000000U, /* do NOT consult environment */ + EVFLAG_FORKCHECK = 0x02000000U, /* check for a fork in each iteration */ + /* debugging/feature disable */ + EVFLAG_NOINOTIFY = 0x00100000U, /* do not attempt to use inotify */ +#if EV_COMPAT3 + EVFLAG_NOSIGFD = 0, /* compatibility to pre-3.9 */ +#endif + EVFLAG_SIGNALFD = 0x00200000U, /* attempt to use signalfd */ + EVFLAG_NOSIGMASK = 0x00400000U /* avoid modifying the signal mask */ +}; + +/* method bits to be ored together */ +enum { + EVBACKEND_SELECT = 0x00000001U, /* available just about anywhere */ + EVBACKEND_POLL = 0x00000002U, /* !win, !aix, broken on osx */ + EVBACKEND_EPOLL = 0x00000004U, /* linux */ + EVBACKEND_KQUEUE = 0x00000008U, /* bsd, broken on osx */ + EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */ + EVBACKEND_PORT = 0x00000020U, /* solaris 10 */ + EVBACKEND_ALL = 0x0000003FU, /* all known backends */ + EVBACKEND_MASK = 0x0000FFFFU /* all future backends */ +}; + +#if EV_PROTOTYPES +EV_API_DECL int ev_version_major (void) EV_THROW; +EV_API_DECL int ev_version_minor (void) EV_THROW; + +EV_API_DECL unsigned int ev_supported_backends (void) EV_THROW; +EV_API_DECL unsigned int ev_recommended_backends (void) EV_THROW; +EV_API_DECL unsigned int ev_embeddable_backends (void) EV_THROW; + +EV_API_DECL ev_tstamp ev_time (void) EV_THROW; +EV_API_DECL void ev_sleep (ev_tstamp delay) EV_THROW; /* sleep for a while */ + +/* Sets the allocation function to use, works like realloc. + * It is used to allocate and free memory. + * If it returns zero when memory needs to be allocated, the library might abort + * or take some potentially destructive action. + * The default is your system realloc function. + */ +EV_API_DECL void ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW; + +/* set the callback function to call on a + * retryable syscall error + * (such as failed select, poll, epoll_wait) + */ +EV_API_DECL void ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW; + +#if EV_MULTIPLICITY + +/* the default loop is the only one that handles signals and child watchers */ +/* you can call this as often as you like */ +EV_API_DECL struct ev_loop *ev_default_loop (unsigned int flags EV_CPP (= 0)) EV_THROW; + +#ifdef EV_API_STATIC +EV_API_DECL struct ev_loop *ev_default_loop_ptr; +#endif + +EV_INLINE struct ev_loop * +ev_default_loop_uc_ (void) EV_THROW +{ + extern struct ev_loop *ev_default_loop_ptr; + + return ev_default_loop_ptr; +} + +EV_INLINE int +ev_is_default_loop (EV_P) EV_THROW +{ + return EV_A == EV_DEFAULT_UC; +} + +/* create and destroy alternative loops that don't handle signals */ +EV_API_DECL struct ev_loop *ev_loop_new (unsigned int flags EV_CPP (= 0)) EV_THROW; + +EV_API_DECL ev_tstamp ev_now (EV_P) EV_THROW; /* time w.r.t. timers and the eventloop, updated after each poll */ + +#else + +EV_API_DECL int ev_default_loop (unsigned int flags EV_CPP (= 0)) EV_THROW; /* returns true when successful */ + +EV_API_DECL ev_tstamp ev_rt_now; + +EV_INLINE ev_tstamp +ev_now (void) EV_THROW +{ + return ev_rt_now; +} + +/* looks weird, but ev_is_default_loop (EV_A) still works if this exists */ +EV_INLINE int +ev_is_default_loop (void) EV_THROW +{ + return 1; +} + +#endif /* multiplicity */ + +/* destroy event loops, also works for the default loop */ +EV_API_DECL void ev_loop_destroy (EV_P); + +/* this needs to be called after fork, to duplicate the loop */ +/* when you want to re-use it in the child */ +/* you can call it in either the parent or the child */ +/* you can actually call it at any time, anywhere :) */ +EV_API_DECL void ev_loop_fork (EV_P) EV_THROW; + +EV_API_DECL unsigned int ev_backend (EV_P) EV_THROW; /* backend in use by loop */ + +EV_API_DECL void ev_now_update (EV_P) EV_THROW; /* update event loop time */ + +#if EV_WALK_ENABLE +/* walk (almost) all watchers in the loop of a given type, invoking the */ +/* callback on every such watcher. The callback might stop the watcher, */ +/* but do nothing else with the loop */ +EV_API_DECL void ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW; +#endif + +#endif /* prototypes */ + +/* ev_run flags values */ +enum { + EVRUN_NOWAIT = 1, /* do not block/wait */ + EVRUN_ONCE = 2 /* block *once* only */ +}; + +/* ev_break how values */ +enum { + EVBREAK_CANCEL = 0, /* undo unloop */ + EVBREAK_ONE = 1, /* unloop once */ + EVBREAK_ALL = 2 /* unloop all loops */ +}; + +#if EV_PROTOTYPES +EV_API_DECL int ev_run (EV_P_ int flags EV_CPP (= 0)); +EV_API_DECL void ev_break (EV_P_ int how EV_CPP (= EVBREAK_ONE)) EV_THROW; /* break out of the loop */ + +/* + * ref/unref can be used to add or remove a refcount on the mainloop. every watcher + * keeps one reference. if you have a long-running watcher you never unregister that + * should not keep ev_loop from running, unref() after starting, and ref() before stopping. + */ +EV_API_DECL void ev_ref (EV_P) EV_THROW; +EV_API_DECL void ev_unref (EV_P) EV_THROW; + +/* + * convenience function, wait for a single event, without registering an event watcher + * if timeout is < 0, do wait indefinitely + */ +EV_API_DECL void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW; + +# if EV_FEATURE_API +EV_API_DECL unsigned int ev_iteration (EV_P) EV_THROW; /* number of loop iterations */ +EV_API_DECL unsigned int ev_depth (EV_P) EV_THROW; /* #ev_loop enters - #ev_loop leaves */ +EV_API_DECL void ev_verify (EV_P) EV_THROW; /* abort if loop data corrupted */ + +EV_API_DECL void ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW; /* sleep at least this time, default 0 */ +EV_API_DECL void ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW; /* sleep at least this time, default 0 */ + +/* advanced stuff for threading etc. support, see docs */ +EV_API_DECL void ev_set_userdata (EV_P_ void *data) EV_THROW; +EV_API_DECL void *ev_userdata (EV_P) EV_THROW; +typedef void (*ev_loop_callback)(EV_P); +EV_API_DECL void ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_THROW; +/* C++ doesn't allow the use of the ev_loop_callback typedef here, so we need to spell it out */ +EV_API_DECL void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW; + +EV_API_DECL unsigned int ev_pending_count (EV_P) EV_THROW; /* number of pending events, if any */ +EV_API_DECL void ev_invoke_pending (EV_P); /* invoke all pending watchers */ + +/* + * stop/start the timer handling. + */ +EV_API_DECL void ev_suspend (EV_P) EV_THROW; +EV_API_DECL void ev_resume (EV_P) EV_THROW; +#endif + +#endif + +/* these may evaluate ev multiple times, and the other arguments at most once */ +/* either use ev_init + ev_TYPE_set, or the ev_TYPE_init macro, below, to first initialise a watcher */ +#define ev_init(ev,cb_) do { \ + ((ev_watcher *)(void *)(ev))->active = \ + ((ev_watcher *)(void *)(ev))->pending = 0; \ + ev_set_priority ((ev), 0); \ + ev_set_cb ((ev), cb_); \ +} while (0) + +#define ev_io_set(ev,fd_,events_) do { (ev)->fd = (fd_); (ev)->events = (events_) | EV__IOFDSET; } while (0) +#define ev_timer_set(ev,after_,repeat_) do { ((ev_watcher_time *)(ev))->at = (after_); (ev)->repeat = (repeat_); } while (0) +#define ev_periodic_set(ev,ofs_,ival_,rcb_) do { (ev)->offset = (ofs_); (ev)->interval = (ival_); (ev)->reschedule_cb = (rcb_); } while (0) +#define ev_signal_set(ev,signum_) do { (ev)->signum = (signum_); } while (0) +#define ev_child_set(ev,pid_,trace_) do { (ev)->pid = (pid_); (ev)->flags = !!(trace_); } while (0) +#define ev_stat_set(ev,path_,interval_) do { (ev)->path = (path_); (ev)->interval = (interval_); (ev)->wd = -2; } while (0) +#define ev_idle_set(ev) /* nop, yes, this is a serious in-joke */ +#define ev_prepare_set(ev) /* nop, yes, this is a serious in-joke */ +#define ev_check_set(ev) /* nop, yes, this is a serious in-joke */ +#define ev_embed_set(ev,other_) do { (ev)->other = (other_); } while (0) +#define ev_fork_set(ev) /* nop, yes, this is a serious in-joke */ +#define ev_cleanup_set(ev) /* nop, yes, this is a serious in-joke */ +#define ev_async_set(ev) /* nop, yes, this is a serious in-joke */ + +#define ev_io_init(ev,cb,fd,events) do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0) +#define ev_timer_init(ev,cb,after,repeat) do { ev_init ((ev), (cb)); ev_timer_set ((ev),(after),(repeat)); } while (0) +#define ev_periodic_init(ev,cb,ofs,ival,rcb) do { ev_init ((ev), (cb)); ev_periodic_set ((ev),(ofs),(ival),(rcb)); } while (0) +#define ev_signal_init(ev,cb,signum) do { ev_init ((ev), (cb)); ev_signal_set ((ev), (signum)); } while (0) +#define ev_child_init(ev,cb,pid,trace) do { ev_init ((ev), (cb)); ev_child_set ((ev),(pid),(trace)); } while (0) +#define ev_stat_init(ev,cb,path,interval) do { ev_init ((ev), (cb)); ev_stat_set ((ev),(path),(interval)); } while (0) +#define ev_idle_init(ev,cb) do { ev_init ((ev), (cb)); ev_idle_set ((ev)); } while (0) +#define ev_prepare_init(ev,cb) do { ev_init ((ev), (cb)); ev_prepare_set ((ev)); } while (0) +#define ev_check_init(ev,cb) do { ev_init ((ev), (cb)); ev_check_set ((ev)); } while (0) +#define ev_embed_init(ev,cb,other) do { ev_init ((ev), (cb)); ev_embed_set ((ev),(other)); } while (0) +#define ev_fork_init(ev,cb) do { ev_init ((ev), (cb)); ev_fork_set ((ev)); } while (0) +#define ev_cleanup_init(ev,cb) do { ev_init ((ev), (cb)); ev_cleanup_set ((ev)); } while (0) +#define ev_async_init(ev,cb) do { ev_init ((ev), (cb)); ev_async_set ((ev)); } while (0) + +#define ev_is_pending(ev) (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */ +#define ev_is_active(ev) (0 + ((ev_watcher *)(void *)(ev))->active) /* ro, true when the watcher has been started */ + +#define ev_cb_(ev) (ev)->cb /* rw */ +#define ev_cb(ev) (memmove (&ev_cb_ (ev), &((ev_watcher *)(ev))->cb, sizeof (ev_cb_ (ev))), (ev)->cb) + +#if EV_MINPRI == EV_MAXPRI +# define ev_priority(ev) ((ev), EV_MINPRI) +# define ev_set_priority(ev,pri) ((ev), (pri)) +#else +# define ev_priority(ev) (+(((ev_watcher *)(void *)(ev))->priority)) +# define ev_set_priority(ev,pri) ( (ev_watcher *)(void *)(ev))->priority = (pri) +#endif + +#define ev_periodic_at(ev) (+((ev_watcher_time *)(ev))->at) + +#ifndef ev_set_cb +# define ev_set_cb(ev,cb_) (ev_cb_ (ev) = (cb_), memmove (&((ev_watcher *)(ev))->cb, &ev_cb_ (ev), sizeof (ev_cb_ (ev)))) +#endif + +/* stopping (enabling, adding) a watcher does nothing if it is already running */ +/* stopping (disabling, deleting) a watcher does nothing unless it's already running */ +#if EV_PROTOTYPES + +/* feeds an event into a watcher as if the event actually occurred */ +/* accepts any ev_watcher type */ +EV_API_DECL void ev_feed_event (EV_P_ void *w, int revents) EV_THROW; +EV_API_DECL void ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW; +#if EV_SIGNAL_ENABLE +EV_API_DECL void ev_feed_signal (int signum) EV_THROW; +EV_API_DECL void ev_feed_signal_event (EV_P_ int signum) EV_THROW; +#endif +EV_API_DECL void ev_invoke (EV_P_ void *w, int revents); +EV_API_DECL int ev_clear_pending (EV_P_ void *w) EV_THROW; + +EV_API_DECL void ev_io_start (EV_P_ ev_io *w) EV_THROW; +EV_API_DECL void ev_io_stop (EV_P_ ev_io *w) EV_THROW; + +EV_API_DECL void ev_timer_start (EV_P_ ev_timer *w) EV_THROW; +EV_API_DECL void ev_timer_stop (EV_P_ ev_timer *w) EV_THROW; +/* stops if active and no repeat, restarts if active and repeating, starts if inactive and repeating */ +EV_API_DECL void ev_timer_again (EV_P_ ev_timer *w) EV_THROW; +/* return remaining time */ +EV_API_DECL ev_tstamp ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW; + +#if EV_PERIODIC_ENABLE +EV_API_DECL void ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW; +EV_API_DECL void ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW; +EV_API_DECL void ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW; +#endif + +/* only supported in the default loop */ +#if EV_SIGNAL_ENABLE +EV_API_DECL void ev_signal_start (EV_P_ ev_signal *w) EV_THROW; +EV_API_DECL void ev_signal_stop (EV_P_ ev_signal *w) EV_THROW; +#endif + +/* only supported in the default loop */ +# if EV_CHILD_ENABLE +EV_API_DECL void ev_child_start (EV_P_ ev_child *w) EV_THROW; +EV_API_DECL void ev_child_stop (EV_P_ ev_child *w) EV_THROW; +# endif + +# if EV_STAT_ENABLE +EV_API_DECL void ev_stat_start (EV_P_ ev_stat *w) EV_THROW; +EV_API_DECL void ev_stat_stop (EV_P_ ev_stat *w) EV_THROW; +EV_API_DECL void ev_stat_stat (EV_P_ ev_stat *w) EV_THROW; +# endif + +# if EV_IDLE_ENABLE +EV_API_DECL void ev_idle_start (EV_P_ ev_idle *w) EV_THROW; +EV_API_DECL void ev_idle_stop (EV_P_ ev_idle *w) EV_THROW; +# endif + +#if EV_PREPARE_ENABLE +EV_API_DECL void ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW; +EV_API_DECL void ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW; +#endif + +#if EV_CHECK_ENABLE +EV_API_DECL void ev_check_start (EV_P_ ev_check *w) EV_THROW; +EV_API_DECL void ev_check_stop (EV_P_ ev_check *w) EV_THROW; +#endif + +# if EV_FORK_ENABLE +EV_API_DECL void ev_fork_start (EV_P_ ev_fork *w) EV_THROW; +EV_API_DECL void ev_fork_stop (EV_P_ ev_fork *w) EV_THROW; +# endif + +# if EV_CLEANUP_ENABLE +EV_API_DECL void ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW; +EV_API_DECL void ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW; +# endif + +# if EV_EMBED_ENABLE +/* only supported when loop to be embedded is in fact embeddable */ +EV_API_DECL void ev_embed_start (EV_P_ ev_embed *w) EV_THROW; +EV_API_DECL void ev_embed_stop (EV_P_ ev_embed *w) EV_THROW; +EV_API_DECL void ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW; +# endif + +# if EV_ASYNC_ENABLE +EV_API_DECL void ev_async_start (EV_P_ ev_async *w) EV_THROW; +EV_API_DECL void ev_async_stop (EV_P_ ev_async *w) EV_THROW; +EV_API_DECL void ev_async_send (EV_P_ ev_async *w) EV_THROW; +# endif + +#if EV_COMPAT3 + #define EVLOOP_NONBLOCK EVRUN_NOWAIT + #define EVLOOP_ONESHOT EVRUN_ONCE + #define EVUNLOOP_CANCEL EVBREAK_CANCEL + #define EVUNLOOP_ONE EVBREAK_ONE + #define EVUNLOOP_ALL EVBREAK_ALL + #if EV_PROTOTYPES + EV_INLINE void ev_loop (EV_P_ int flags) { ev_run (EV_A_ flags); } + EV_INLINE void ev_unloop (EV_P_ int how ) { ev_break (EV_A_ how ); } + EV_INLINE void ev_default_destroy (void) { ev_loop_destroy (EV_DEFAULT); } + EV_INLINE void ev_default_fork (void) { ev_loop_fork (EV_DEFAULT); } + #if EV_FEATURE_API + EV_INLINE unsigned int ev_loop_count (EV_P) { return ev_iteration (EV_A); } + EV_INLINE unsigned int ev_loop_depth (EV_P) { return ev_depth (EV_A); } + EV_INLINE void ev_loop_verify (EV_P) { ev_verify (EV_A); } + #endif + #endif +#else + typedef struct ev_loop ev_loop; +#endif + +#endif + +EV_CPP(}) + +#endif + diff --git a/third_party/libev/ev_epoll.c b/third_party/libev/ev_epoll.c new file mode 100644 index 00000000..6aef8622 --- /dev/null +++ b/third_party/libev/ev_epoll.c @@ -0,0 +1,285 @@ +/* + * libev epoll fd activity backend + * + * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +/* + * general notes about epoll: + * + * a) epoll silently removes fds from the fd set. as nothing tells us + * that an fd has been removed otherwise, we have to continually + * "rearm" fds that we suspect *might* have changed (same + * problem with kqueue, but much less costly there). + * b) the fact that ADD != MOD creates a lot of extra syscalls due to a) + * and seems not to have any advantage. + * c) the inability to handle fork or file descriptors (think dup) + * limits the applicability over poll, so this is not a generic + * poll replacement. + * d) epoll doesn't work the same as select with many file descriptors + * (such as files). while not critical, no other advanced interface + * seems to share this (rather non-unixy) limitation. + * e) epoll claims to be embeddable, but in practise you never get + * a ready event for the epoll fd (broken: <=2.6.26, working: >=2.6.32). + * f) epoll_ctl returning EPERM means the fd is always ready. + * + * lots of "weird code" and complication handling in this file is due + * to these design problems with epoll, as we try very hard to avoid + * epoll_ctl syscalls for common usage patterns and handle the breakage + * ensuing from receiving events for closed and otherwise long gone + * file descriptors. + */ + +#include + +#define EV_EMASK_EPERM 0x80 + +static void +epoll_modify (EV_P_ int fd, int oev, int nev) +{ + struct epoll_event ev; + unsigned char oldmask; + + /* + * we handle EPOLL_CTL_DEL by ignoring it here + * on the assumption that the fd is gone anyways + * if that is wrong, we have to handle the spurious + * event in epoll_poll. + * if the fd is added again, we try to ADD it, and, if that + * fails, we assume it still has the same eventmask. + */ + if (!nev) + return; + + oldmask = anfds [fd].emask; + anfds [fd].emask = nev; + + /* store the generation counter in the upper 32 bits, the fd in the lower 32 bits */ + ev.data.u64 = (uint64_t)(uint32_t)fd + | ((uint64_t)(uint32_t)++anfds [fd].egen << 32); + ev.events = (nev & EV_READ ? EPOLLIN : 0) + | (nev & EV_WRITE ? EPOLLOUT : 0); + + if (expect_true (!epoll_ctl (backend_fd, oev && oldmask != nev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) + return; + + if (expect_true (errno == ENOENT)) + { + /* if ENOENT then the fd went away, so try to do the right thing */ + if (!nev) + goto dec_egen; + + if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev)) + return; + } + else if (expect_true (errno == EEXIST)) + { + /* EEXIST means we ignored a previous DEL, but the fd is still active */ + /* if the kernel mask is the same as the new mask, we assume it hasn't changed */ + if (oldmask == nev) + goto dec_egen; + + if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev)) + return; + } + else if (expect_true (errno == EPERM)) + { + /* EPERM means the fd is always ready, but epoll is too snobbish */ + /* to handle it, unlike select or poll. */ + anfds [fd].emask = EV_EMASK_EPERM; + + /* add fd to epoll_eperms, if not already inside */ + if (!(oldmask & EV_EMASK_EPERM)) + { + array_needsize (int, epoll_eperms, epoll_epermmax, epoll_epermcnt + 1, EMPTY2); + epoll_eperms [epoll_epermcnt++] = fd; + } + + return; + } + + fd_kill (EV_A_ fd); + +dec_egen: + /* we didn't successfully call epoll_ctl, so decrement the generation counter again */ + --anfds [fd].egen; +} + +static void +epoll_poll (EV_P_ ev_tstamp timeout) +{ + int i; + int eventcnt; + + if (expect_false (epoll_epermcnt)) + timeout = 0.; + + /* epoll wait times cannot be larger than (LONG_MAX - 999UL) / HZ msecs, which is below */ + /* the default libev max wait time, however. */ + EV_RELEASE_CB; + eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, timeout * 1e3); + EV_ACQUIRE_CB; + + if (expect_false (eventcnt < 0)) + { + if (errno != EINTR) + ev_syserr ("(libev) epoll_wait"); + + return; + } + + for (i = 0; i < eventcnt; ++i) + { + struct epoll_event *ev = epoll_events + i; + + int fd = (uint32_t)ev->data.u64; /* mask out the lower 32 bits */ + int want = anfds [fd].events; + int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) + | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); + + /* + * check for spurious notification. + * this only finds spurious notifications on egen updates + * other spurious notifications will be found by epoll_ctl, below + * we assume that fd is always in range, as we never shrink the anfds array + */ + if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32))) + { + /* recreate kernel state */ + postfork |= 2; + continue; + } + + if (expect_false (got & ~want)) + { + anfds [fd].emask = want; + + /* + * we received an event but are not interested in it, try mod or del + * this often happens because we optimistically do not unregister fds + * when we are no longer interested in them, but also when we get spurious + * notifications for fds from another process. this is partially handled + * above with the gencounter check (== our fd is not the event fd), and + * partially here, when epoll_ctl returns an error (== a child has the fd + * but we closed it). + */ + ev->events = (want & EV_READ ? EPOLLIN : 0) + | (want & EV_WRITE ? EPOLLOUT : 0); + + /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */ + /* which is fortunately easy to do for us. */ + if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev)) + { + postfork |= 2; /* an error occurred, recreate kernel state */ + continue; + } + } + + fd_event (EV_A_ fd, got); + } + + /* if the receive array was full, increase its size */ + if (expect_false (eventcnt == epoll_eventmax)) + { + ev_free (epoll_events); + epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1); + epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); + } + + /* now synthesize events for all fds where epoll fails, while select works... */ + for (i = epoll_epermcnt; i--; ) + { + int fd = epoll_eperms [i]; + unsigned char events = anfds [fd].events & (EV_READ | EV_WRITE); + + if (anfds [fd].emask & EV_EMASK_EPERM && events) + fd_event (EV_A_ fd, events); + else + { + epoll_eperms [i] = epoll_eperms [--epoll_epermcnt]; + anfds [fd].emask = 0; + } + } +} + +inline_size +int +epoll_init (EV_P_ int flags) +{ +#if defined EPOLL_CLOEXEC && !defined __ANDROID__ + backend_fd = epoll_create1 (EPOLL_CLOEXEC); + + if (backend_fd < 0 && (errno == EINVAL || errno == ENOSYS)) +#endif + backend_fd = epoll_create (256); + + if (backend_fd < 0) + return 0; + + fcntl (backend_fd, F_SETFD, FD_CLOEXEC); + + backend_mintime = 1e-3; /* epoll does sometimes return early, this is just to avoid the worst */ + backend_modify = epoll_modify; + backend_poll = epoll_poll; + + epoll_eventmax = 64; /* initial number of events receivable per poll */ + epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); + + return EVBACKEND_EPOLL; +} + +inline_size +void +epoll_destroy (EV_P) +{ + ev_free (epoll_events); + array_free (epoll_eperm, EMPTY); +} + +inline_size +void +epoll_fork (EV_P) +{ + close (backend_fd); + + while ((backend_fd = epoll_create (256)) < 0) + ev_syserr ("(libev) epoll_create"); + + fcntl (backend_fd, F_SETFD, FD_CLOEXEC); + + fd_rearm_all (EV_A); +} + diff --git a/third_party/libev/ev_kqueue.c b/third_party/libev/ev_kqueue.c new file mode 100644 index 00000000..0c05ab9e --- /dev/null +++ b/third_party/libev/ev_kqueue.c @@ -0,0 +1,218 @@ +/* + * libev kqueue backend + * + * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#include +#include +#include +#include +#include + +inline_speed +void +kqueue_change (EV_P_ int fd, int filter, int flags, int fflags) +{ + ++kqueue_changecnt; + array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2); + + EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0); +} + +/* OS X at least needs this */ +#ifndef EV_ENABLE +# define EV_ENABLE 0 +#endif +#ifndef NOTE_EOF +# define NOTE_EOF 0 +#endif + +static void +kqueue_modify (EV_P_ int fd, int oev, int nev) +{ + if (oev != nev) + { + if (oev & EV_READ) + kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0); + + if (oev & EV_WRITE) + kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0); + } + + /* to detect close/reopen reliably, we have to re-add */ + /* event requests even when oev == nev */ + + if (nev & EV_READ) + kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD | EV_ENABLE, NOTE_EOF); + + if (nev & EV_WRITE) + kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, NOTE_EOF); +} + +static void +kqueue_poll (EV_P_ ev_tstamp timeout) +{ + int res, i; + struct timespec ts; + + /* need to resize so there is enough space for errors */ + if (kqueue_changecnt > kqueue_eventmax) + { + ev_free (kqueue_events); + kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt); + kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); + } + + EV_RELEASE_CB; + EV_TS_SET (ts, timeout); + res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts); + EV_ACQUIRE_CB; + kqueue_changecnt = 0; + + if (expect_false (res < 0)) + { + if (errno != EINTR) + ev_syserr ("(libev) kevent"); + + return; + } + + for (i = 0; i < res; ++i) + { + int fd = kqueue_events [i].ident; + + if (expect_false (kqueue_events [i].flags & EV_ERROR)) + { + int err = kqueue_events [i].data; + + /* we are only interested in errors for fds that we are interested in :) */ + if (anfds [fd].events) + { + if (err == ENOENT) /* resubmit changes on ENOENT */ + kqueue_modify (EV_A_ fd, 0, anfds [fd].events); + else if (err == EBADF) /* on EBADF, we re-check the fd */ + { + if (fd_valid (fd)) + kqueue_modify (EV_A_ fd, 0, anfds [fd].events); + else + fd_kill (EV_A_ fd); + } + else /* on all other errors, we error out on the fd */ + fd_kill (EV_A_ fd); + } + } + else + fd_event ( + EV_A_ + fd, + kqueue_events [i].filter == EVFILT_READ ? EV_READ + : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE + : 0 + ); + } + + if (expect_false (res == kqueue_eventmax)) + { + ev_free (kqueue_events); + kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1); + kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); + } +} + +inline_size +int +kqueue_init (EV_P_ int flags) +{ + /* initialize the kernel queue */ + kqueue_fd_pid = getpid (); + if ((backend_fd = kqueue ()) < 0) + return 0; + + fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ + + backend_mintime = 1e-9; /* apparently, they did the right thing in freebsd */ + backend_modify = kqueue_modify; + backend_poll = kqueue_poll; + + kqueue_eventmax = 64; /* initial number of events receivable per poll */ + kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); + + kqueue_changes = 0; + kqueue_changemax = 0; + kqueue_changecnt = 0; + + return EVBACKEND_KQUEUE; +} + +inline_size +void +kqueue_destroy (EV_P) +{ + ev_free (kqueue_events); + ev_free (kqueue_changes); +} + +inline_size +void +kqueue_fork (EV_P) +{ + /* some BSD kernels don't just destroy the kqueue itself, + * but also close the fd, which isn't documented, and + * impossible to support properly. + * we remember the pid of the kqueue call and only close + * the fd if the pid is still the same. + * this leaks fds on sane kernels, but BSD interfaces are + * notoriously buggy and rarely get fixed. + */ + pid_t newpid = getpid (); + + if (newpid == kqueue_fd_pid) + close (backend_fd); + + kqueue_fd_pid = newpid; + while ((backend_fd = kqueue ()) < 0) + ev_syserr ("(libev) kqueue"); + + fcntl (backend_fd, F_SETFD, FD_CLOEXEC); + + /* re-register interest in fds */ + fd_rearm_all (EV_A); +} + +/* sys/event.h defines EV_ERROR */ +#undef EV_ERROR + diff --git a/third_party/libev/ev_poll.c b/third_party/libev/ev_poll.c new file mode 100644 index 00000000..bd742b07 --- /dev/null +++ b/third_party/libev/ev_poll.c @@ -0,0 +1,151 @@ +/* + * libev poll fd activity backend + * + * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#include + +inline_size +void +pollidx_init (int *base, int count) +{ + /* consider using memset (.., -1, ...), which is practically guaranteed + * to work on all systems implementing poll */ + while (count--) + *base++ = -1; +} + +static void +poll_modify (EV_P_ int fd, int oev, int nev) +{ + int idx; + + if (oev == nev) + return; + + array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); + + idx = pollidxs [fd]; + + if (idx < 0) /* need to allocate a new pollfd */ + { + pollidxs [fd] = idx = pollcnt++; + array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2); + polls [idx].fd = fd; + } + + assert (polls [idx].fd == fd); + + if (nev) + polls [idx].events = + (nev & EV_READ ? POLLIN : 0) + | (nev & EV_WRITE ? POLLOUT : 0); + else /* remove pollfd */ + { + pollidxs [fd] = -1; + + if (expect_true (idx < --pollcnt)) + { + polls [idx] = polls [pollcnt]; + pollidxs [polls [idx].fd] = idx; + } + } +} + +static void +poll_poll (EV_P_ ev_tstamp timeout) +{ + struct pollfd *p; + int res; + + EV_RELEASE_CB; + res = poll (polls, pollcnt, timeout * 1e3); + EV_ACQUIRE_CB; + + if (expect_false (res < 0)) + { + if (errno == EBADF) + fd_ebadf (EV_A); + else if (errno == ENOMEM && !syserr_cb) + fd_enomem (EV_A); + else if (errno != EINTR) + ev_syserr ("(libev) poll"); + } + else + for (p = polls; res; ++p) + { + assert (("libev: poll() returned illegal result, broken BSD kernel?", p < polls + pollcnt)); + + if (expect_false (p->revents)) /* this expect is debatable */ + { + --res; + + if (expect_false (p->revents & POLLNVAL)) + fd_kill (EV_A_ p->fd); + else + fd_event ( + EV_A_ + p->fd, + (p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) + | (p->revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) + ); + } + } +} + +inline_size +int +poll_init (EV_P_ int flags) +{ + backend_mintime = 1e-3; + backend_modify = poll_modify; + backend_poll = poll_poll; + + pollidxs = 0; pollidxmax = 0; + polls = 0; pollmax = 0; pollcnt = 0; + + return EVBACKEND_POLL; +} + +inline_size +void +poll_destroy (EV_P) +{ + ev_free (pollidxs); + ev_free (polls); +} + diff --git a/third_party/libev/ev_port.c b/third_party/libev/ev_port.c new file mode 100644 index 00000000..c7b0b70c --- /dev/null +++ b/third_party/libev/ev_port.c @@ -0,0 +1,189 @@ +/* + * libev solaris event port backend + * + * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +/* useful reading: + * + * http://bugs.opensolaris.org/view_bug.do?bug_id=6268715 (random results) + * http://bugs.opensolaris.org/view_bug.do?bug_id=6455223 (just totally broken) + * http://bugs.opensolaris.org/view_bug.do?bug_id=6873782 (manpage ETIME) + * http://bugs.opensolaris.org/view_bug.do?bug_id=6874410 (implementation ETIME) + * http://www.mail-archive.com/networking-discuss@opensolaris.org/msg11898.html ETIME vs. nget + * http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/event_port.c (libc) + * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/fs/portfs/port.c#1325 (kernel) + */ + +#include +#include +#include +#include +#include +#include + +inline_speed +void +port_associate_and_check (EV_P_ int fd, int ev) +{ + if (0 > + port_associate ( + backend_fd, PORT_SOURCE_FD, fd, + (ev & EV_READ ? POLLIN : 0) + | (ev & EV_WRITE ? POLLOUT : 0), + 0 + ) + ) + { + if (errno == EBADFD) + fd_kill (EV_A_ fd); + else + ev_syserr ("(libev) port_associate"); + } +} + +static void +port_modify (EV_P_ int fd, int oev, int nev) +{ + /* we need to reassociate no matter what, as closes are + * once more silently being discarded. + */ + if (!nev) + { + if (oev) + port_dissociate (backend_fd, PORT_SOURCE_FD, fd); + } + else + port_associate_and_check (EV_A_ fd, nev); +} + +static void +port_poll (EV_P_ ev_tstamp timeout) +{ + int res, i; + struct timespec ts; + uint_t nget = 1; + + /* we initialise this to something we will skip in the loop, as */ + /* port_getn can return with nget unchanged, but no indication */ + /* whether it was the original value or has been updated :/ */ + port_events [0].portev_source = 0; + + EV_RELEASE_CB; + EV_TS_SET (ts, timeout); + res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts); + EV_ACQUIRE_CB; + + /* port_getn may or may not set nget on error */ + /* so we rely on port_events [0].portev_source not being updated */ + if (res == -1 && errno != ETIME && errno != EINTR) + ev_syserr ("(libev) port_getn (see http://bugs.opensolaris.org/view_bug.do?bug_id=6268715, try LIBEV_FLAGS=3 env variable)"); + + for (i = 0; i < nget; ++i) + { + if (port_events [i].portev_source == PORT_SOURCE_FD) + { + int fd = port_events [i].portev_object; + + fd_event ( + EV_A_ + fd, + (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) + | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) + ); + + fd_change (EV_A_ fd, EV__IOFDSET); + } + } + + if (expect_false (nget == port_eventmax)) + { + ev_free (port_events); + port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1); + port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); + } +} + +inline_size +int +port_init (EV_P_ int flags) +{ + /* Initialize the kernel queue */ + if ((backend_fd = port_create ()) < 0) + return 0; + + assert (("libev: PORT_SOURCE_FD must not be zero", PORT_SOURCE_FD)); + + fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ + + /* if my reading of the opensolaris kernel sources are correct, then + * opensolaris does something very stupid: it checks if the time has already + * elapsed and doesn't round up if that is the case,m otherwise it DOES round + * up. Since we can't know what the case is, we need to guess by using a + * "large enough" timeout. Normally, 1e-9 would be correct. + */ + backend_mintime = 1e-3; /* needed to compensate for port_getn returning early */ + backend_modify = port_modify; + backend_poll = port_poll; + + port_eventmax = 64; /* initial number of events receivable per poll */ + port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); + + return EVBACKEND_PORT; +} + +inline_size +void +port_destroy (EV_P) +{ + ev_free (port_events); +} + +inline_size +void +port_fork (EV_P) +{ + close (backend_fd); + + while ((backend_fd = port_create ()) < 0) + ev_syserr ("(libev) port"); + + fcntl (backend_fd, F_SETFD, FD_CLOEXEC); + + /* re-register interest in fds */ + fd_rearm_all (EV_A); +} + diff --git a/third_party/libev/ev_select.c b/third_party/libev/ev_select.c new file mode 100644 index 00000000..ed1fc7ad --- /dev/null +++ b/third_party/libev/ev_select.c @@ -0,0 +1,316 @@ +/* + * libev select fd activity backend + * + * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#ifndef _WIN32 +/* for unix systems */ +# include +# ifndef __hpux +/* for REAL unix systems */ +# include +# endif +#endif + +#ifndef EV_SELECT_USE_FD_SET +# ifdef NFDBITS +# define EV_SELECT_USE_FD_SET 0 +# else +# define EV_SELECT_USE_FD_SET 1 +# endif +#endif + +#if EV_SELECT_IS_WINSOCKET +# undef EV_SELECT_USE_FD_SET +# define EV_SELECT_USE_FD_SET 1 +# undef NFDBITS +# define NFDBITS 0 +#endif + +#if !EV_SELECT_USE_FD_SET +# define NFDBYTES (NFDBITS / 8) +#endif + +#include + +static void +select_modify (EV_P_ int fd, int oev, int nev) +{ + if (oev == nev) + return; + + { +#if EV_SELECT_USE_FD_SET + + #if EV_SELECT_IS_WINSOCKET + SOCKET handle = anfds [fd].handle; + #else + int handle = fd; + #endif + + assert (("libev: fd >= FD_SETSIZE passed to fd_set-based select backend", fd < FD_SETSIZE)); + + /* FD_SET is broken on windows (it adds the fd to a set twice or more, + * which eventually leads to overflows). Need to call it only on changes. + */ + #if EV_SELECT_IS_WINSOCKET + if ((oev ^ nev) & EV_READ) + #endif + if (nev & EV_READ) + FD_SET (handle, (fd_set *)vec_ri); + else + FD_CLR (handle, (fd_set *)vec_ri); + + #if EV_SELECT_IS_WINSOCKET + if ((oev ^ nev) & EV_WRITE) + #endif + if (nev & EV_WRITE) + FD_SET (handle, (fd_set *)vec_wi); + else + FD_CLR (handle, (fd_set *)vec_wi); + +#else + + int word = fd / NFDBITS; + fd_mask mask = 1UL << (fd % NFDBITS); + + if (expect_false (vec_max <= word)) + { + int new_max = word + 1; + + vec_ri = ev_realloc (vec_ri, new_max * NFDBYTES); + vec_ro = ev_realloc (vec_ro, new_max * NFDBYTES); /* could free/malloc */ + vec_wi = ev_realloc (vec_wi, new_max * NFDBYTES); + vec_wo = ev_realloc (vec_wo, new_max * NFDBYTES); /* could free/malloc */ + #ifdef _WIN32 + vec_eo = ev_realloc (vec_eo, new_max * NFDBYTES); /* could free/malloc */ + #endif + + for (; vec_max < new_max; ++vec_max) + ((fd_mask *)vec_ri) [vec_max] = + ((fd_mask *)vec_wi) [vec_max] = 0; + } + + ((fd_mask *)vec_ri) [word] |= mask; + if (!(nev & EV_READ)) + ((fd_mask *)vec_ri) [word] &= ~mask; + + ((fd_mask *)vec_wi) [word] |= mask; + if (!(nev & EV_WRITE)) + ((fd_mask *)vec_wi) [word] &= ~mask; +#endif + } +} + +static void +select_poll (EV_P_ ev_tstamp timeout) +{ + struct timeval tv; + int res; + int fd_setsize; + + EV_RELEASE_CB; + EV_TV_SET (tv, timeout); + +#if EV_SELECT_USE_FD_SET + fd_setsize = sizeof (fd_set); +#else + fd_setsize = vec_max * NFDBYTES; +#endif + + memcpy (vec_ro, vec_ri, fd_setsize); + memcpy (vec_wo, vec_wi, fd_setsize); + +#ifdef _WIN32 + /* pass in the write set as except set. + * the idea behind this is to work around a windows bug that causes + * errors to be reported as an exception and not by setting + * the writable bit. this is so uncontrollably lame. + */ + memcpy (vec_eo, vec_wi, fd_setsize); + res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, (fd_set *)vec_eo, &tv); +#elif EV_SELECT_USE_FD_SET + fd_setsize = anfdmax < FD_SETSIZE ? anfdmax : FD_SETSIZE; + res = select (fd_setsize, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); +#else + res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); +#endif + EV_ACQUIRE_CB; + + if (expect_false (res < 0)) + { + #if EV_SELECT_IS_WINSOCKET + errno = WSAGetLastError (); + #endif + #ifdef WSABASEERR + /* on windows, select returns incompatible error codes, fix this */ + if (errno >= WSABASEERR && errno < WSABASEERR + 1000) + if (errno == WSAENOTSOCK) + errno = EBADF; + else + errno -= WSABASEERR; + #endif + + #ifdef _WIN32 + /* select on windows erroneously returns EINVAL when no fd sets have been + * provided (this is documented). what microsoft doesn't tell you that this bug + * exists even when the fd sets _are_ provided, so we have to check for this bug + * here and emulate by sleeping manually. + * we also get EINVAL when the timeout is invalid, but we ignore this case here + * and assume that EINVAL always means: you have to wait manually. + */ + if (errno == EINVAL) + { + if (timeout) + { + unsigned long ms = timeout * 1e3; + Sleep (ms ? ms : 1); + } + + return; + } + #endif + + if (errno == EBADF) + fd_ebadf (EV_A); + else if (errno == ENOMEM && !syserr_cb) + fd_enomem (EV_A); + else if (errno != EINTR) + ev_syserr ("(libev) select"); + + return; + } + +#if EV_SELECT_USE_FD_SET + + { + int fd; + + for (fd = 0; fd < anfdmax; ++fd) + if (anfds [fd].events) + { + int events = 0; + #if EV_SELECT_IS_WINSOCKET + SOCKET handle = anfds [fd].handle; + #else + int handle = fd; + #endif + + if (FD_ISSET (handle, (fd_set *)vec_ro)) events |= EV_READ; + if (FD_ISSET (handle, (fd_set *)vec_wo)) events |= EV_WRITE; + #ifdef _WIN32 + if (FD_ISSET (handle, (fd_set *)vec_eo)) events |= EV_WRITE; + #endif + + if (expect_true (events)) + fd_event (EV_A_ fd, events); + } + } + +#else + + { + int word, bit; + for (word = vec_max; word--; ) + { + fd_mask word_r = ((fd_mask *)vec_ro) [word]; + fd_mask word_w = ((fd_mask *)vec_wo) [word]; + #ifdef _WIN32 + word_w |= ((fd_mask *)vec_eo) [word]; + #endif + + if (word_r || word_w) + for (bit = NFDBITS; bit--; ) + { + fd_mask mask = 1UL << bit; + int events = 0; + + events |= word_r & mask ? EV_READ : 0; + events |= word_w & mask ? EV_WRITE : 0; + + if (expect_true (events)) + fd_event (EV_A_ word * NFDBITS + bit, events); + } + } + } + +#endif +} + +inline_size +int +select_init (EV_P_ int flags) +{ + backend_mintime = 1e-6; + backend_modify = select_modify; + backend_poll = select_poll; + +#if EV_SELECT_USE_FD_SET + vec_ri = ev_malloc (sizeof (fd_set)); FD_ZERO ((fd_set *)vec_ri); + vec_ro = ev_malloc (sizeof (fd_set)); + vec_wi = ev_malloc (sizeof (fd_set)); FD_ZERO ((fd_set *)vec_wi); + vec_wo = ev_malloc (sizeof (fd_set)); + #ifdef _WIN32 + vec_eo = ev_malloc (sizeof (fd_set)); + #endif +#else + vec_max = 0; + vec_ri = 0; + vec_ro = 0; + vec_wi = 0; + vec_wo = 0; + #ifdef _WIN32 + vec_eo = 0; + #endif +#endif + + return EVBACKEND_SELECT; +} + +inline_size +void +select_destroy (EV_P) +{ + ev_free (vec_ri); + ev_free (vec_ro); + ev_free (vec_wi); + ev_free (vec_wo); + #ifdef _WIN32 + ev_free (vec_eo); + #endif +} + diff --git a/third_party/libev/ev_vars.h b/third_party/libev/ev_vars.h new file mode 100644 index 00000000..04d4db16 --- /dev/null +++ b/third_party/libev/ev_vars.h @@ -0,0 +1,204 @@ +/* + * loop member variable declarations + * + * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#define VARx(type,name) VAR(name, type name) + +VARx(ev_tstamp, now_floor) /* last time we refreshed rt_time */ +VARx(ev_tstamp, mn_now) /* monotonic clock "now" */ +VARx(ev_tstamp, rtmn_diff) /* difference realtime - monotonic time */ + +/* for reverse feeding of events */ +VARx(W *, rfeeds) +VARx(int, rfeedmax) +VARx(int, rfeedcnt) + +VAR (pendings, ANPENDING *pendings [NUMPRI]) +VAR (pendingmax, int pendingmax [NUMPRI]) +VAR (pendingcnt, int pendingcnt [NUMPRI]) +VARx(int, pendingpri) /* highest priority currently pending */ +VARx(ev_prepare, pending_w) /* dummy pending watcher */ + +VARx(ev_tstamp, io_blocktime) +VARx(ev_tstamp, timeout_blocktime) + +VARx(int, backend) +VARx(int, activecnt) /* total number of active events ("refcount") */ +VARx(EV_ATOMIC_T, loop_done) /* signal by ev_break */ + +VARx(int, backend_fd) +VARx(ev_tstamp, backend_mintime) /* assumed typical timer resolution */ +VAR (backend_modify, void (*backend_modify)(EV_P_ int fd, int oev, int nev)) +VAR (backend_poll , void (*backend_poll)(EV_P_ ev_tstamp timeout)) + +VARx(ANFD *, anfds) +VARx(int, anfdmax) + +VAR (evpipe, int evpipe [2]) +VARx(ev_io, pipe_w) +VARx(EV_ATOMIC_T, pipe_write_wanted) +VARx(EV_ATOMIC_T, pipe_write_skipped) + +#if !defined(_WIN32) || EV_GENWRAP +VARx(pid_t, curpid) +#endif + +VARx(char, postfork) /* true if we need to recreate kernel state after fork */ + +#if EV_USE_SELECT || EV_GENWRAP +VARx(void *, vec_ri) +VARx(void *, vec_ro) +VARx(void *, vec_wi) +VARx(void *, vec_wo) +#if defined(_WIN32) || EV_GENWRAP +VARx(void *, vec_eo) +#endif +VARx(int, vec_max) +#endif + +#if EV_USE_POLL || EV_GENWRAP +VARx(struct pollfd *, polls) +VARx(int, pollmax) +VARx(int, pollcnt) +VARx(int *, pollidxs) /* maps fds into structure indices */ +VARx(int, pollidxmax) +#endif + +#if EV_USE_EPOLL || EV_GENWRAP +VARx(struct epoll_event *, epoll_events) +VARx(int, epoll_eventmax) +VARx(int *, epoll_eperms) +VARx(int, epoll_epermcnt) +VARx(int, epoll_epermmax) +#endif + +#if EV_USE_KQUEUE || EV_GENWRAP +VARx(pid_t, kqueue_fd_pid) +VARx(struct kevent *, kqueue_changes) +VARx(int, kqueue_changemax) +VARx(int, kqueue_changecnt) +VARx(struct kevent *, kqueue_events) +VARx(int, kqueue_eventmax) +#endif + +#if EV_USE_PORT || EV_GENWRAP +VARx(struct port_event *, port_events) +VARx(int, port_eventmax) +#endif + +#if EV_USE_IOCP || EV_GENWRAP +VARx(HANDLE, iocp) +#endif + +VARx(int *, fdchanges) +VARx(int, fdchangemax) +VARx(int, fdchangecnt) + +VARx(ANHE *, timers) +VARx(int, timermax) +VARx(int, timercnt) + +#if EV_PERIODIC_ENABLE || EV_GENWRAP +VARx(ANHE *, periodics) +VARx(int, periodicmax) +VARx(int, periodiccnt) +#endif + +#if EV_IDLE_ENABLE || EV_GENWRAP +VAR (idles, ev_idle **idles [NUMPRI]) +VAR (idlemax, int idlemax [NUMPRI]) +VAR (idlecnt, int idlecnt [NUMPRI]) +#endif +VARx(int, idleall) /* total number */ + +VARx(struct ev_prepare **, prepares) +VARx(int, preparemax) +VARx(int, preparecnt) + +VARx(struct ev_check **, checks) +VARx(int, checkmax) +VARx(int, checkcnt) + +#if EV_FORK_ENABLE || EV_GENWRAP +VARx(struct ev_fork **, forks) +VARx(int, forkmax) +VARx(int, forkcnt) +#endif + +#if EV_CLEANUP_ENABLE || EV_GENWRAP +VARx(struct ev_cleanup **, cleanups) +VARx(int, cleanupmax) +VARx(int, cleanupcnt) +#endif + +#if EV_ASYNC_ENABLE || EV_GENWRAP +VARx(EV_ATOMIC_T, async_pending) +VARx(struct ev_async **, asyncs) +VARx(int, asyncmax) +VARx(int, asynccnt) +#endif + +#if EV_USE_INOTIFY || EV_GENWRAP +VARx(int, fs_fd) +VARx(ev_io, fs_w) +VARx(char, fs_2625) /* whether we are running in linux 2.6.25 or newer */ +VAR (fs_hash, ANFS fs_hash [EV_INOTIFY_HASHSIZE]) +#endif + +VARx(EV_ATOMIC_T, sig_pending) +#if EV_USE_SIGNALFD || EV_GENWRAP +VARx(int, sigfd) +VARx(ev_io, sigfd_w) +VARx(sigset_t, sigfd_set) +#endif + +VARx(unsigned int, origflags) /* original loop flags */ + +#if EV_FEATURE_API || EV_GENWRAP +VARx(unsigned int, loop_count) /* total number of loop iterations/blocks */ +VARx(unsigned int, loop_depth) /* #ev_run enters - #ev_run leaves */ + +VARx(void *, userdata) +/* C++ doesn't support the ev_loop_callback typedef here. stinks. */ +VAR (release_cb, void (*release_cb)(EV_P) EV_THROW) +VAR (acquire_cb, void (*acquire_cb)(EV_P) EV_THROW) +VAR (invoke_cb , ev_loop_callback invoke_cb) +#endif + +#undef VARx + diff --git a/third_party/libev/ev_win32.c b/third_party/libev/ev_win32.c new file mode 100644 index 00000000..fd671356 --- /dev/null +++ b/third_party/libev/ev_win32.c @@ -0,0 +1,162 @@ +/* + * libev win32 compatibility cruft (_not_ a backend) + * + * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#ifdef _WIN32 + +/* note: the comment below could not be substantiated, but what would I care */ +/* MSDN says this is required to handle SIGFPE */ +/* my wild guess would be that using something floating-pointy is required */ +/* for the crt to do something about it */ +volatile double SIGFPE_REQ = 0.0f; + +static SOCKET +ev_tcp_socket (void) +{ +#if EV_USE_WSASOCKET + return WSASocket (AF_INET, SOCK_STREAM, 0, 0, 0, 0); +#else + return socket (AF_INET, SOCK_STREAM, 0); +#endif +} + +/* oh, the humanity! */ +static int +ev_pipe (int filedes [2]) +{ + struct sockaddr_in addr = { 0 }; + int addr_size = sizeof (addr); + struct sockaddr_in adr2; + int adr2_size = sizeof (adr2); + SOCKET listener; + SOCKET sock [2] = { -1, -1 }; + + if ((listener = ev_tcp_socket ()) == INVALID_SOCKET) + return -1; + + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); + addr.sin_port = 0; + + if (bind (listener, (struct sockaddr *)&addr, addr_size)) + goto fail; + + if (getsockname (listener, (struct sockaddr *)&addr, &addr_size)) + goto fail; + + if (listen (listener, 1)) + goto fail; + + if ((sock [0] = ev_tcp_socket ()) == INVALID_SOCKET) + goto fail; + + if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) + goto fail; + + /* TODO: returns INVALID_SOCKET on winsock accept, not < 0. fix it */ + /* when convenient, probably by just removing error checking altogether? */ + if ((sock [1] = accept (listener, 0, 0)) < 0) + goto fail; + + /* windows vista returns fantasy port numbers for sockets: + * example for two interconnected tcp sockets: + * + * (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364 + * (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363 + * (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363 + * (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365 + * + * wow! tridirectional sockets! + * + * this way of checking ports seems to work: + */ + if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size)) + goto fail; + + if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size)) + goto fail; + + errno = WSAEINVAL; + if (addr_size != adr2_size + || addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */ + || addr.sin_port != adr2.sin_port) + goto fail; + + closesocket (listener); + +#if EV_SELECT_IS_WINSOCKET + filedes [0] = EV_WIN32_HANDLE_TO_FD (sock [0]); + filedes [1] = EV_WIN32_HANDLE_TO_FD (sock [1]); +#else + /* when select isn't winsocket, we also expect socket, connect, accept etc. + * to work on fds */ + filedes [0] = sock [0]; + filedes [1] = sock [1]; +#endif + + return 0; + +fail: + closesocket (listener); + + if (sock [0] != INVALID_SOCKET) closesocket (sock [0]); + if (sock [1] != INVALID_SOCKET) closesocket (sock [1]); + + return -1; +} + +#undef pipe +#define pipe(filedes) ev_pipe (filedes) + +#define EV_HAVE_EV_TIME 1 +ev_tstamp +ev_time (void) +{ + FILETIME ft; + ULARGE_INTEGER ui; + + GetSystemTimeAsFileTime (&ft); + ui.u.LowPart = ft.dwLowDateTime; + ui.u.HighPart = ft.dwHighDateTime; + + /* msvc cannot convert ulonglong to double... yes, it is that sucky */ + return (LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-7; +} + +#endif + diff --git a/third_party/libev/ev_wrap.h b/third_party/libev/ev_wrap.h new file mode 100644 index 00000000..ad989ea7 --- /dev/null +++ b/third_party/libev/ev_wrap.h @@ -0,0 +1,200 @@ +/* DO NOT EDIT, automatically generated by update_ev_wrap */ +#ifndef EV_WRAP_H +#define EV_WRAP_H +#define acquire_cb ((loop)->acquire_cb) +#define activecnt ((loop)->activecnt) +#define anfdmax ((loop)->anfdmax) +#define anfds ((loop)->anfds) +#define async_pending ((loop)->async_pending) +#define asynccnt ((loop)->asynccnt) +#define asyncmax ((loop)->asyncmax) +#define asyncs ((loop)->asyncs) +#define backend ((loop)->backend) +#define backend_fd ((loop)->backend_fd) +#define backend_mintime ((loop)->backend_mintime) +#define backend_modify ((loop)->backend_modify) +#define backend_poll ((loop)->backend_poll) +#define checkcnt ((loop)->checkcnt) +#define checkmax ((loop)->checkmax) +#define checks ((loop)->checks) +#define cleanupcnt ((loop)->cleanupcnt) +#define cleanupmax ((loop)->cleanupmax) +#define cleanups ((loop)->cleanups) +#define curpid ((loop)->curpid) +#define epoll_epermcnt ((loop)->epoll_epermcnt) +#define epoll_epermmax ((loop)->epoll_epermmax) +#define epoll_eperms ((loop)->epoll_eperms) +#define epoll_eventmax ((loop)->epoll_eventmax) +#define epoll_events ((loop)->epoll_events) +#define evpipe ((loop)->evpipe) +#define fdchangecnt ((loop)->fdchangecnt) +#define fdchangemax ((loop)->fdchangemax) +#define fdchanges ((loop)->fdchanges) +#define forkcnt ((loop)->forkcnt) +#define forkmax ((loop)->forkmax) +#define forks ((loop)->forks) +#define fs_2625 ((loop)->fs_2625) +#define fs_fd ((loop)->fs_fd) +#define fs_hash ((loop)->fs_hash) +#define fs_w ((loop)->fs_w) +#define idleall ((loop)->idleall) +#define idlecnt ((loop)->idlecnt) +#define idlemax ((loop)->idlemax) +#define idles ((loop)->idles) +#define invoke_cb ((loop)->invoke_cb) +#define io_blocktime ((loop)->io_blocktime) +#define iocp ((loop)->iocp) +#define kqueue_changecnt ((loop)->kqueue_changecnt) +#define kqueue_changemax ((loop)->kqueue_changemax) +#define kqueue_changes ((loop)->kqueue_changes) +#define kqueue_eventmax ((loop)->kqueue_eventmax) +#define kqueue_events ((loop)->kqueue_events) +#define kqueue_fd_pid ((loop)->kqueue_fd_pid) +#define loop_count ((loop)->loop_count) +#define loop_depth ((loop)->loop_depth) +#define loop_done ((loop)->loop_done) +#define mn_now ((loop)->mn_now) +#define now_floor ((loop)->now_floor) +#define origflags ((loop)->origflags) +#define pending_w ((loop)->pending_w) +#define pendingcnt ((loop)->pendingcnt) +#define pendingmax ((loop)->pendingmax) +#define pendingpri ((loop)->pendingpri) +#define pendings ((loop)->pendings) +#define periodiccnt ((loop)->periodiccnt) +#define periodicmax ((loop)->periodicmax) +#define periodics ((loop)->periodics) +#define pipe_w ((loop)->pipe_w) +#define pipe_write_skipped ((loop)->pipe_write_skipped) +#define pipe_write_wanted ((loop)->pipe_write_wanted) +#define pollcnt ((loop)->pollcnt) +#define pollidxmax ((loop)->pollidxmax) +#define pollidxs ((loop)->pollidxs) +#define pollmax ((loop)->pollmax) +#define polls ((loop)->polls) +#define port_eventmax ((loop)->port_eventmax) +#define port_events ((loop)->port_events) +#define postfork ((loop)->postfork) +#define preparecnt ((loop)->preparecnt) +#define preparemax ((loop)->preparemax) +#define prepares ((loop)->prepares) +#define release_cb ((loop)->release_cb) +#define rfeedcnt ((loop)->rfeedcnt) +#define rfeedmax ((loop)->rfeedmax) +#define rfeeds ((loop)->rfeeds) +#define rtmn_diff ((loop)->rtmn_diff) +#define sig_pending ((loop)->sig_pending) +#define sigfd ((loop)->sigfd) +#define sigfd_set ((loop)->sigfd_set) +#define sigfd_w ((loop)->sigfd_w) +#define timeout_blocktime ((loop)->timeout_blocktime) +#define timercnt ((loop)->timercnt) +#define timermax ((loop)->timermax) +#define timers ((loop)->timers) +#define userdata ((loop)->userdata) +#define vec_eo ((loop)->vec_eo) +#define vec_max ((loop)->vec_max) +#define vec_ri ((loop)->vec_ri) +#define vec_ro ((loop)->vec_ro) +#define vec_wi ((loop)->vec_wi) +#define vec_wo ((loop)->vec_wo) +#else +#undef EV_WRAP_H +#undef acquire_cb +#undef activecnt +#undef anfdmax +#undef anfds +#undef async_pending +#undef asynccnt +#undef asyncmax +#undef asyncs +#undef backend +#undef backend_fd +#undef backend_mintime +#undef backend_modify +#undef backend_poll +#undef checkcnt +#undef checkmax +#undef checks +#undef cleanupcnt +#undef cleanupmax +#undef cleanups +#undef curpid +#undef epoll_epermcnt +#undef epoll_epermmax +#undef epoll_eperms +#undef epoll_eventmax +#undef epoll_events +#undef evpipe +#undef fdchangecnt +#undef fdchangemax +#undef fdchanges +#undef forkcnt +#undef forkmax +#undef forks +#undef fs_2625 +#undef fs_fd +#undef fs_hash +#undef fs_w +#undef idleall +#undef idlecnt +#undef idlemax +#undef idles +#undef invoke_cb +#undef io_blocktime +#undef iocp +#undef kqueue_changecnt +#undef kqueue_changemax +#undef kqueue_changes +#undef kqueue_eventmax +#undef kqueue_events +#undef kqueue_fd_pid +#undef loop_count +#undef loop_depth +#undef loop_done +#undef mn_now +#undef now_floor +#undef origflags +#undef pending_w +#undef pendingcnt +#undef pendingmax +#undef pendingpri +#undef pendings +#undef periodiccnt +#undef periodicmax +#undef periodics +#undef pipe_w +#undef pipe_write_skipped +#undef pipe_write_wanted +#undef pollcnt +#undef pollidxmax +#undef pollidxs +#undef pollmax +#undef polls +#undef port_eventmax +#undef port_events +#undef postfork +#undef preparecnt +#undef preparemax +#undef prepares +#undef release_cb +#undef rfeedcnt +#undef rfeedmax +#undef rfeeds +#undef rtmn_diff +#undef sig_pending +#undef sigfd +#undef sigfd_set +#undef sigfd_w +#undef timeout_blocktime +#undef timercnt +#undef timermax +#undef timers +#undef userdata +#undef vec_eo +#undef vec_max +#undef vec_ri +#undef vec_ro +#undef vec_wi +#undef vec_wo +#endif From 73ff0ebf33e05d2b981d6c629d7e2b116eed166e Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Wed, 1 Jul 2026 16:15:55 +0800 Subject: [PATCH 09/29] use libev lwip thirdparty --- CMakeLists.txt | 2 +- candy/src/netstack/lwip-port/arch/cc.h | 34 - candy/src/netstack/lwip-port/arch/perf.h | 7 - candy/src/netstack/lwip-port/arch/sys_arch.h | 7 - candy/src/netstack/lwip-port/sys_arch.c | 40 - candy/src/netstack/lwipopts.h | 64 - cmake/libev.cmake | 20 +- cmake/lwip.cmake | 36 +- third_party/libev/Changes | 517 -- third_party/libev/LICENSE | 37 - third_party/libev/README | 58 - third_party/libev/ev++.h | 816 --- third_party/libev/ev.c | 5144 ------------------ third_party/libev/ev.h | 854 --- third_party/libev/ev_epoll.c | 285 - third_party/libev/ev_kqueue.c | 218 - third_party/libev/ev_poll.c | 151 - third_party/libev/ev_port.c | 189 - third_party/libev/ev_select.c | 316 -- third_party/libev/ev_vars.h | 204 - third_party/libev/ev_win32.c | 162 - third_party/libev/ev_wrap.h | 200 - 22 files changed, 36 insertions(+), 9325 deletions(-) delete mode 100644 candy/src/netstack/lwip-port/arch/cc.h delete mode 100644 candy/src/netstack/lwip-port/arch/perf.h delete mode 100644 candy/src/netstack/lwip-port/arch/sys_arch.h delete mode 100644 candy/src/netstack/lwip-port/sys_arch.c delete mode 100644 candy/src/netstack/lwipopts.h delete mode 100644 third_party/libev/Changes delete mode 100644 third_party/libev/LICENSE delete mode 100644 third_party/libev/README delete mode 100644 third_party/libev/ev++.h delete mode 100644 third_party/libev/ev.c delete mode 100644 third_party/libev/ev.h delete mode 100644 third_party/libev/ev_epoll.c delete mode 100644 third_party/libev/ev_kqueue.c delete mode 100644 third_party/libev/ev_poll.c delete mode 100644 third_party/libev/ev_port.c delete mode 100644 third_party/libev/ev_select.c delete mode 100644 third_party/libev/ev_vars.h delete mode 100644 third_party/libev/ev_win32.c delete mode 100644 third_party/libev/ev_wrap.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 748013cf..34c197c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,7 +87,7 @@ set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) # libev:事件循环库(reactor 后端,一份代码全平台)。 -# 采用 vendored 源码编译(third_party/libev),不依赖系统库,三平台开箱即用。 +# 依赖自定义 fork(Bepartofyou/libev),FetchContent 拉取,不再 vendored,三平台开箱即用。 include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/libev.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/lwip.cmake) diff --git a/candy/src/netstack/lwip-port/arch/cc.h b/candy/src/netstack/lwip-port/arch/cc.h deleted file mode 100644 index 78b6a6c6..00000000 --- a/candy/src/netstack/lwip-port/arch/cc.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef CANDY_NETSTACK_LWIP_PORT_ARCH_CC_H -#define CANDY_NETSTACK_LWIP_PORT_ARCH_CC_H - -#if defined(_WIN32) || defined(_WIN64) -#else -#if defined __ANDROID__ -#define LWIP_UNIX_ANDROID -#elif defined __linux__ -#define LWIP_UNIX_LINUX -#elif defined __APPLE__ -#define LWIP_UNIX_MACH -#endif -#endif - -#define LWIP_TIMEVAL_PRIVATE 0 -#include - -#define LWIP_ERRNO_INCLUDE - -#if defined(LWIP_UNIX_LINUX) || defined(LWIP_UNIX_ANDROID) -#define LWIP_ERRNO_STDINCLUDE 1 -#endif - -extern unsigned int lwip_port_rand(void); -#define LWIP_RAND() (lwip_port_rand()) - -#if defined(LWIP_UNIX_MACH) -#include -#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS -#endif - -typedef unsigned int sys_prot_t; - -#endif diff --git a/candy/src/netstack/lwip-port/arch/perf.h b/candy/src/netstack/lwip-port/arch/perf.h deleted file mode 100644 index 6961de23..00000000 --- a/candy/src/netstack/lwip-port/arch/perf.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef CANDY_NETSTACK_LWIP_PORT_ARCH_PERF_H -#define CANDY_NETSTACK_LWIP_PORT_ARCH_PERF_H - -#define PERF_START -#define PERF_STOP(x) - -#endif diff --git a/candy/src/netstack/lwip-port/arch/sys_arch.h b/candy/src/netstack/lwip-port/arch/sys_arch.h deleted file mode 100644 index 55418daa..00000000 --- a/candy/src/netstack/lwip-port/arch/sys_arch.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef CANDY_NETSTACK_LWIP_PORT_ARCH_SYS_ARCH_H -#define CANDY_NETSTACK_LWIP_PORT_ARCH_SYS_ARCH_H - -#define SYS_MBOX_NULL NULL -#define SYS_SEM_NULL NULL - -#endif diff --git a/candy/src/netstack/lwip-port/sys_arch.c b/candy/src/netstack/lwip-port/sys_arch.c deleted file mode 100644 index 9cd35b45..00000000 --- a/candy/src/netstack/lwip-port/sys_arch.c +++ /dev/null @@ -1,40 +0,0 @@ -#include "lwip/opt.h" -#include "lwip/sys.h" - -#include -#include - -#if defined(__APPLE__) -#include -#endif - -u32_t lwip_port_rand(void) { - return (u32_t)rand(); -} - -static void get_monotonic_time(struct timespec *ts) { -#if defined(__APPLE__) - u64_t t = mach_absolute_time(); - mach_timebase_info_data_t timebase_info = {0, 0}; - mach_timebase_info(&timebase_info); - u64_t nano = (t * timebase_info.numer) / (timebase_info.denom); - u64_t sec = nano / 1000000000L; - nano -= sec * 1000000000L; - ts->tv_sec = sec; - ts->tv_nsec = nano; -#else - clock_gettime(CLOCK_MONOTONIC, ts); -#endif -} - -u32_t sys_now(void) { - struct timespec ts; - get_monotonic_time(&ts); - return (u32_t)(ts.tv_sec * 1000L + ts.tv_nsec / 1000000L); -} - -u32_t sys_jiffies(void) { - struct timespec ts; - get_monotonic_time(&ts); - return (u32_t)(ts.tv_sec * 1000000000L + ts.tv_nsec); -} diff --git a/candy/src/netstack/lwipopts.h b/candy/src/netstack/lwipopts.h deleted file mode 100644 index bb0a016d..00000000 --- a/candy/src/netstack/lwipopts.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef CANDY_NETSTACK_LWIPOPTS_H -#define CANDY_NETSTACK_LWIPOPTS_H - -#define NO_SYS 1 -#define LWIP_NETCONN 0 -#define LWIP_SOCKET 0 -#define SYS_LIGHTWEIGHT_PROT 0 - -#define LWIP_TIMERS 1 - -#define IP_FORWARD 0 - -#define LWIP_TCP 1 -#define LWIP_UDP 1 -#define LWIP_RAW 0 -#define LWIP_ICMP 1 -#define LWIP_DNS 0 -#define LWIP_DHCP 0 -#define LWIP_AUTOIP 0 -#define LWIP_IGMP 0 -#define LWIP_ARP 0 -#define LWIP_ETHERNET 0 -#define LWIP_NETIF_API 0 -#define LWIP_NETIF_LOOPBACK 0 -#define LWIP_HAVE_LOOPIF 0 - -#define LWIP_IPV4 1 -#define LWIP_IPV6 0 - -#define MEM_ALIGNMENT 4U -#define MEM_LIBC_MALLOC 1 -#define MEMP_MEM_MALLOC 1 - -#define MEMP_NUM_TCP_PCB 4096 -#define MEMP_NUM_TCP_PCB_LISTEN 16 -#define MEMP_NUM_TCP_SEG 4096 -#define MEMP_NUM_UDP_PCB 4096 -#define MEMP_NUM_PBUF 4096 -#define MEMP_NUM_REASSDATA 1 -#define PBUF_POOL_SIZE 0 - -#define TCP_MSS 1460 -#define TCP_WND (32 * TCP_MSS) -#define TCP_SND_BUF (32 * TCP_MSS) -#define TCP_SND_QUEUELEN ((4 * TCP_SND_BUF) / TCP_MSS) -#define TCP_QUEUE_OOSEQ 1 - -#define LWIP_NETIF_STATUS_CALLBACK 0 -#define LWIP_NETIF_LINK_CALLBACK 0 -#define LWIP_NETIF_HOSTNAME 0 - -#define CHECKSUM_GEN_IP 1 -#define CHECKSUM_GEN_TCP 1 -#define CHECKSUM_GEN_UDP 1 -#define CHECKSUM_CHECK_IP 1 -#define CHECKSUM_CHECK_TCP 1 -#define CHECKSUM_CHECK_UDP 1 - -#define LWIP_STATS 0 -#define LWIP_STATS_DISPLAY 0 - -#define LWIP_DEBUG 0 - -#endif diff --git a/cmake/libev.cmake b/cmake/libev.cmake index 60fed21d..4dc16806 100644 --- a/cmake/libev.cmake +++ b/cmake/libev.cmake @@ -1,5 +1,6 @@ # libev:事件循环库(reactor 后端,一份代码全平台)。 -# 采用 vendored 源码编译(third_party/libev),不依赖系统库,保证三平台开箱即用。 +# 依赖自定义 fork(Bepartofyou/libev),通过 FetchContent 拉取,不再在仓库内 vendored 源码, +# 也不依赖系统库,保证三平台开箱即用。 # # libev 的源码组织很特殊:只需编译 ev.c 一个翻译单元,它在内部按宏条件 # #include 具体后端(ev_epoll.c / ev_kqueue.c / ev_select.c 等)。因此这里 @@ -12,7 +13,22 @@ # 2. EV_STANDALONE 下 kqueue 默认关闭(EV_USE_KQUEUE 默认 0),故 macOS 必须显式 EV_USE_KQUEUE=1。 # 3. Windows 用 select 后端,并需 EV_SELECT_IS_WINSOCKET=1 让 select 操作 SOCKET 句柄。 -set(LIBEV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libev) +include(FetchContent) + +set(LIBEV_GIT_REPOSITORY "https://github.com/Bepartofyou/libev.git") +set(LIBEV_GIT_TAG "a56570ae6d076d7471b945116171c88724ba14a9") + +FetchContent_Declare( + libev + GIT_REPOSITORY ${LIBEV_GIT_REPOSITORY} + GIT_TAG ${LIBEV_GIT_TAG} +) +FetchContent_GetProperties(libev) +if(NOT libev_POPULATED) + FetchContent_Populate(libev) +endif() + +set(LIBEV_DIR ${libev_SOURCE_DIR}) add_library(ev STATIC ${LIBEV_DIR}/ev.c) diff --git a/cmake/lwip.cmake b/cmake/lwip.cmake index e78ff5fc..8e255de4 100644 --- a/cmake/lwip.cmake +++ b/cmake/lwip.cmake @@ -1,7 +1,7 @@ include(FetchContent) -set(LWIP_GIT_REPOSITORY "https://github.com/heiher/lwip.git") -set(LWIP_GIT_TAG "8c69dfbe537835d5f2a5fd8c08c859f667b108ea") +set(LWIP_GIT_REPOSITORY "https://github.com/Bepartofyou/lwip.git") +set(LWIP_GIT_TAG "2ce295cc41ecbfcb7c71eae35ae3e018b408fc5b") FetchContent_Declare( lwip @@ -13,21 +13,19 @@ if(NOT lwip_POPULATED) FetchContent_Populate(lwip) endif() -# 补丁:heiher lwIP 的 UDP PRETEND 克隆路径用了 pcb->local_ip.type, -# 该字段仅在双栈(LWIP_IPV6=1)的 ip_addr_t 存在;本项目是 IPv4-only, -# 编译会报 "ip_addr_t has no member named type"。改用版本无关的 -# IP_GET_TYPE() 宏(IPv4-only 下展开为 IPADDR_TYPE_V4),不引入 IPv6。 -set(LWIP_UDP_C "${lwip_SOURCE_DIR}/src/core/udp.c") -file(READ "${LWIP_UDP_C}" _lwip_udp_src) -string(REPLACE - "udp_new_ip_type(pcb->local_ip.type)" - "udp_new_ip_type(IP_GET_TYPE(&pcb->local_ip))" - _lwip_udp_src "${_lwip_udp_src}") -file(WRITE "${LWIP_UDP_C}" "${_lwip_udp_src}") - set(LWIP_SRC_DIR ${lwip_SOURCE_DIR}/src) -set(LWIP_PORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/candy/src/netstack/lwip-port) -set(LWIP_OPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/candy/src/netstack) +# 直接复用 fork(Bepartofyou/lwip)自带的移植层与配置: +# - src/ports/unix|win32/lib/sys_arch.c:平台适配,不再手写。 +# - src/ports/include/arch/*.h:转发头,按平台自动转发到 unix/win32 实现头。 +# - src/ports/include/lwipopts.h:已在 fork 内改成 candy 定制配置, +# candy 不再单独维护 lwipopts.h。 +# 另:udp.c 的 IPv4-only 兼容修复(IP_GET_TYPE)也已合入 fork,无需再打补丁。 +set(LWIP_PORT_INCLUDE_DIR ${LWIP_SRC_DIR}/ports/include) +if(WIN32) + set(LWIP_PORT_DIR ${LWIP_SRC_DIR}/ports/win32) +else() + set(LWIP_PORT_DIR ${LWIP_SRC_DIR}/ports/unix) +endif() set(LWIP_CORE_SOURCES ${LWIP_SRC_DIR}/core/init.c @@ -56,7 +54,7 @@ set(LWIP_CORE_IPV4_SOURCES ) set(LWIP_PORT_SOURCES - ${LWIP_PORT_DIR}/sys_arch.c + ${LWIP_PORT_DIR}/lib/sys_arch.c ) add_library(lwip STATIC @@ -66,7 +64,7 @@ add_library(lwip STATIC ) target_include_directories(lwip PUBLIC - ${LWIP_OPTS_DIR} - ${LWIP_PORT_DIR} + ${LWIP_PORT_INCLUDE_DIR} + ${LWIP_PORT_DIR}/include ${LWIP_SRC_DIR}/include ) diff --git a/third_party/libev/Changes b/third_party/libev/Changes deleted file mode 100644 index 9968527e..00000000 --- a/third_party/libev/Changes +++ /dev/null @@ -1,517 +0,0 @@ -Revision history for libev, a high-performance and full-featured event loop. - - - ANDROID => __ANDROID__ (reported by enh@google.com). - - disable epoll_create1 on android because it has broken header files - and google is unwilling to fix them (reported by enh@google.com). - -4.24 Wed Dec 28 05:19:55 CET 2016 - - bump version to 4.24, as the release tarball inexplicably - didn't have the right version in ev.h, even though the cvs-tagged - version did have the right one (reported by Ales Teska). - -4.23 Wed Nov 16 18:23:41 CET 2016 - - move some declarations at the beginning to help certain retarded - microsoft compilers, even though their documentation claims - otherwise (reported by Ruslan Osmanov). - -4.22 Sun Dec 20 22:11:50 CET 2015 - - when epoll detects unremovable fds in the fd set, rebuild - only the epoll descriptor, not the signal pipe, to avoid - SIGPIPE in ev_async_send. This doesn't solve it on fork, - so document what needs to be done in ev_loop_fork - (analyzed by Benjamin Mahler). - - remove superfluous sys/timeb.h include on win32 - (analyzed by Jason Madden). - - updated libecb. - -4.20 Sat Jun 20 13:01:43 CEST 2015 - - prefer noexcept over throw () with C++ 11. - - update ecb.h due to incompatibilities with c11. - - fix a potential aliasing issue when reading and writing - watcher callbacks. - -4.19 Thu Sep 25 08:18:25 CEST 2014 - - ev.h wasn't valid C++ anymore, which tripped compilers other than - clang, msvc or gcc (analyzed by Raphael 'kena' Poss). Unfortunately, - C++ doesn't support typedefs for function pointers fully, so the affected - declarations have to spell out the types each time. - - when not using autoconf, tighten the check for clock_gettime and related - functionality. - -4.18 Fri Sep 5 17:55:26 CEST 2014 - - events on files were not always generated properly with the - epoll backend (testcase by Assaf Inbal). - - mark event pipe fd as cloexec after a fork (analyzed by Sami Farin). - - (ecb) support m68k, m88k and sh (patch by Miod Vallat). - - use a reasonable fallback for EV_NSIG instead of erroring out - when we can't detect the signal set size. - - in the absence of autoconf, do not use the clock syscall - on glibc >= 2.17 (avoids the syscall AND -lrt on systems - doing clock_gettime in userspace). - - ensure extern "C" function pointers are used for externally-visible - loop callbacks (not watcher callbacks yet). - - (ecb) work around memory barriers and volatile apparently both being - broken in visual studio 2008 and later (analysed and patch by Nicolas Noble). - -4.15 Fri Mar 1 12:04:50 CET 2013 - - destroying a non-default loop would stop the global waitpid - watcher (Denis Bilenko). - - queueing pending watchers of higher priority from a watcher now invokes - them in a timely fashion (reported by Denis Bilenko). - - add throw() to all libev functions that cannot throw exceptions, for - further code size decrease when compiling for C++. - - add throw () to callbacks that must not throw exceptions (allocator, - syserr, loop acquire/release, periodic reschedule cbs). - - fix event_base_loop return code, add event_get_callback, event_base_new, - event_base_get_method calls to improve libevent 1.x emulation and add - some libevent 2.x functionality (based on a patch by Jeff Davey). - - add more memory fences to fix a bug reported by Jeff Davey. Better - be overfenced than underprotected. - - ev_run now returns a boolean status (true meaning watchers are - still active). - - ev_once: undef EV_ERROR in ev_kqueue.c, to avoid clashing with - libev's EV_ERROR (reported by 191919). - - (ecb) add memory fence support for xlC (Darin McBride). - - (ecb) add memory fence support for gcc-mips (Anton Kirilov). - - (ecb) add memory fence support for gcc-alpha (Christian Weisgerber). - - work around some kernels losing file descriptors by leaking - the kqueue descriptor in the child. - - work around linux inotify not reporting IN_ATTRIB changes for directories - in many cases. - - include sys/syscall.h instead of plain syscall.h. - - check for io watcher loops in ev_verify, check for the most - common reported usage bug in ev_io_start. - - choose socket vs. WSASocket at compiletime using EV_USE_WSASOCKET. - - always use WSASend/WSARecv directly on windows, hoping that this - works in all cases (unlike read/write/send/recv...). - - try to detect signals around a fork faster (test program by - Denis Bilenko). - - work around recent glibc versions that leak memory in realloc. - - rename ev::embed::set to ev::embed::set_embed to avoid clashing - the watcher base set (loop) method. - - rewrite the async/signal pipe logic to always keep a valid fd, which - simplifies (and hopefully correctifies :) the race checking - on fork, at the cost of one extra fd. - - add fat, msdos, jffs2, ramfs, ntfs and btrfs to the list of - inotify-supporting filesystems. - - move orig_CFLAGS assignment to after AC_INIT, as newer autoconf - versions ignore it before - (https://bugzilla.redhat.com/show_bug.cgi?id=908096). - - add some untested android support. - - enum expressions must be of type int (reported by Juan Pablo L). - -4.11 Sat Feb 4 19:52:39 CET 2012 - - INCOMPATIBLE CHANGE: ev_timer_again now clears the pending status, as - was documented already, but not implemented in the repeating case. - - new compiletime symbols: EV_NO_SMP and EV_NO_THREADS. - - fix a race where the workaround against the epoll fork bugs - caused signals to not be handled anymore. - - correct backend_fudge for most backends, and implement a windows - specific workaround to avoid looping because we call both - select and Sleep, both with different time resolutions. - - document range and guarantees of ev_sleep. - - document reasonable ranges for periodics interval and offset. - - rename backend_fudge to backend_mintime to avoid future confusion :) - - change the default periodic reschedule function to hopefully be more - exact and correct even in corner cases or in the far future. - - do not rely on -lm anymore: use it when available but use our - own floor () if it is missing. This should make it easier to embed, - as no external libraries are required. - - strategically import macros from libecb and mark rarely-used functions - as cache-cold (saving almost 2k code size on typical amd64 setups). - - add Symbols.ev and Symbols.event files, that were missing. - - fix backend_mintime value for epoll (was 1/1024, is 1/1000 now). - - fix #3 "be smart about timeouts" to not "deadlock" when - timeout == now, also improve the section overall. - - avoid "AVOIDING FINISHING BEFORE RETURNING" idiom. - - support new EV_API_STATIC mode to make all libev symbols - static. - - supply default CFLAGS of -g -O3 with gcc when original CFLAGS - were empty. - -4.04 Wed Feb 16 09:01:51 CET 2011 - - fix two problems in the native win32 backend, where reuse of fd's - with different underlying handles caused handles not to be removed - or added to the select set (analyzed and tested by Bert Belder). - - do no rely on ceil() in ev_e?poll.c. - - backport libev to HP-UX versions before 11 v3. - - configure did not detect nanosleep and clock_gettime properly when - they are available in the libc (as opposed to -lrt). - -4.03 Tue Jan 11 14:37:25 CET 2011 - - officially support polling files with all backends. - - support files, /dev/zero etc. the same way as select in the epoll - backend, by generating events on our own. - - ports backend: work around solaris bug 6874410 and many related ones - (EINTR, maybe more), with no performance loss (note that the solaris - bug report is actually wrong, reality is far more bizarre and broken - than that). - - define EV_READ/EV_WRITE as macros in event.h, as some programs use - #ifdef to test for them. - - new (experimental) function: ev_feed_signal. - - new (to become default) EVFLAG_NOSIGMASK flag. - - new EVBACKEND_MASK symbol. - - updated COMMON IDIOMS SECTION. - -4.01 Fri Nov 5 21:51:29 CET 2010 - - automake fucked it up, apparently, --add-missing -f is not quite enough - to make it update its files, so 4.00 didn't install ev++.h and - event.h on make install. grrr. - - ev_loop(count|depth) didn't return anything (Robin Haberkorn). - - change EV_UNDEF to 0xffffffff to silence some overzealous compilers. - - use "(libev) " prefix for all libev error messages now. - -4.00 Mon Oct 25 12:32:12 CEST 2010 - - "PORTING FROM LIBEV 3.X TO 4.X" (in ev.pod) is recommended reading. - - ev_embed_stop did not correctly stop the watcher (very good - testcase by Vladimir Timofeev). - - ev_run will now always update the current loop time - it erroneously - didn't when idle watchers were active, causing timers not to fire. - - fix a bug where a timeout of zero caused the timer not to fire - in the libevent emulation (testcase by Péter Szabó). - - applied win32 fixes by Michael Lenaghan (also James Mansion). - - replace EV_MINIMAL by EV_FEATURES. - - prefer EPOLL_CTL_ADD over EPOLL_CTL_MOD in some more cases, as it - seems the former is *much* faster than the latter. - - linux kernel version detection (for inotify bug workarounds) - did not work properly. - - reduce the number of spurious wake-ups with the ports backend. - - remove dependency on sys/queue.h on freebsd (patch by Vanilla Hsu). - - do async init within ev_async_start, not ev_async_set, which avoids - an API quirk where the set function must be called in the C++ API - even when there is nothing to set. - - add (undocumented) EV_ENABLE when adding events with kqueue, - this might help with OS X, which seems to need it despite documenting - not to need it (helpfully pointed out by Tilghman Lesher). - - do not use poll by default on freebsd, it's broken (what isn't - on freebsd...). - - allow to embed epoll on kernels >= 2.6.32. - - configure now prepends -O3, not appends it, so one can still - override it. - - ev.pod: greatly expanded the portability section, added a porting - section, a description of watcher states and made lots of minor fixes. - - disable poll backend on AIX, the poll header spams the namespace - and it's not worth working around dead platforms (reported - and analyzed by Aivars Kalvans). - - improve header file compatibility of the standalone eventfd code - in an obscure case. - - implement EV_AVOID_STDIO option. - - do not use sscanf to parse linux version number (smaller, faster, - no sscanf dependency). - - new EV_CHILD_ENABLE and EV_SIGNAL_ENABLE configurable settings. - - update libev.m4 HAVE_CLOCK_SYSCALL test for newer glibcs. - - add section on accept() problems to the manpage. - - rename EV_TIMEOUT to EV_TIMER. - - rename ev_loop_count/depth/verify/loop/unloop. - - remove ev_default_destroy and ev_default_fork. - - switch to two-digit minor version. - - work around an apparent gentoo compiler bug. - - define _DARWIN_UNLIMITED_SELECT. just so. - - use enum instead of #define for most constants. - - improve compatibility to older C++ compilers. - - (experimental) ev_run/ev_default_loop/ev_break/ev_loop_new have now - default arguments when compiled as C++. - - enable automake dependency tracking. - - ev_loop_new no longer leaks memory when loop creation failed. - - new ev_cleanup watcher type. - -3.9 Thu Dec 31 07:59:59 CET 2009 - - signalfd is no longer used by default and has to be requested - explicitly - this means that easy to catch bugs become hard to - catch race conditions, but the users have spoken. - - point out the unspecified signal mask in the documentation, and - that this is a race condition regardless of EV_SIGNALFD. - - backport inotify code to C89. - - inotify file descriptors could leak into child processes. - - ev_stat watchers could keep an erroneous extra ref on the loop, - preventing exit when unregistering all watchers (testcases - provided by ry@tinyclouds.org). - - implement EV_WIN32_HANDLE_TO_FD and EV_WIN32_CLOSE_FD configuration - symbols to make it easier for apps to do their own fd management. - - support EV_IDLE_ENABLE being disabled in ev++.h - (patch by Didier Spezia). - - take advantage of inotify_init1, if available, to set cloexec/nonblock - on fd creation, to avoid races. - - the signal handling pipe wasn't always initialised under windows - (analysed by lekma). - - changed minimum glibc requirement from glibc 2.9 to 2.7, for - signalfd. - - add missing string.h include (Denis F. Latypoff). - - only replace ev_stat.prev when we detect an actual difference, - so prev is (almost) always different to attr. this might - have caused the problems with 04_stat.t. - - add ev::timer->remaining () method to C++ API. - -3.8 Sun Aug 9 14:30:45 CEST 2009 - - incompatible change: do not necessarily reset signal handler - to SIG_DFL when a sighandler is stopped. - - ev_default_destroy did not properly free or zero some members, - potentially causing crashes and memory corruption on repeated - ev_default_destroy/ev_default_loop calls. - - take advantage of signalfd on GNU/Linux systems. - - document that the signal mask might be in an unspecified - state when using libev's signal handling. - - take advantage of some GNU/Linux calls to set cloexec/nonblock - on fd creation, to avoid race conditions. - -3.7 Fri Jul 17 16:36:32 CEST 2009 - - ev_unloop and ev_loop wrongly used a global variable to exit loops, - instead of using a per-loop variable (bug caught by accident...). - - the ev_set_io_collect_interval interpretation has changed. - - add new functionality: ev_set_userdata, ev_userdata, - ev_set_invoke_pending_cb, ev_set_loop_release_cb, - ev_invoke_pending, ev_pending_count, together with a long example - about thread locking. - - add ev_timer_remaining (as requested by Denis F. Latypoff). - - add ev_loop_depth. - - calling ev_unloop in fork/prepare watchers will no longer poll - for new events. - - Denis F. Latypoff corrected many typos in example code snippets. - - honor autoconf detection of EV_USE_CLOCK_SYSCALL, also double- - check that the syscall number is available before trying to - use it (reported by ry@tinyclouds). - - use GetSystemTimeAsFileTime instead of _timeb on windows, for - slightly higher accuracy. - - properly declare ev_loop_verify and ev_now_update even when - !EV_MULTIPLICITY. - - do not compile in any priority code when EV_MAXPRI == EV_MINPRI. - - support EV_MINIMAL==2 for a reduced API. - - actually 0-initialise struct sigaction when installing signals. - - add section on hibernate and stopped processes to ev_timer docs. - -3.6 Tue Apr 28 02:49:30 CEST 2009 - - multiple timers becoming ready within an event loop iteration - will be invoked in the "correct" order now. - - do not leave the event loop early just because we have no active - watchers, fixing a problem when embedding a kqueue loop - that has active kernel events but no registered watchers - (reported by blacksand blacksand). - - correctly zero the idx values for arrays, so destroying and - reinitialising the default loop actually works (patch by - Malek Hadj-Ali). - - implement ev_suspend and ev_resume. - - new EV_CUSTOM revents flag for use by applications. - - add documentation section about priorities. - - add a glossary to the documentation. - - extend the ev_fork description slightly. - - optimize a jump out of call_pending. - -3.53 Sun Feb 15 02:38:20 CET 2009 - - fix a bug in event pipe creation on win32 that would cause a - failed assertion on event loop creation (patch by Malek Hadj-Ali). - - probe for CLOCK_REALTIME support at runtime as well and fall - back to gettimeofday if there is an error, to support older - operating systems with newer header files/libraries. - - prefer gettimeofday over clock_gettime with USE_CLOCK_SYSCALL - (default most everywhere), otherwise not. - -3.52 Wed Jan 7 21:43:02 CET 2009 - - fix compilation of select backend in fd_set mode when NFDBITS is - missing (to get it to compile on QNX, reported by Rodrigo Campos). - - better select-nfds handling when select backend is in fd_set mode. - - diagnose fd_set overruns when select backend is in fd_set mode. - - due to a thinko, instead of disabling everything but - select on the borked OS X platform, everything but select was - allowed (reported by Emanuele Giaquinta). - - actually verify that local and remote port are matching in - libev's socketpair emulation, which makes denial-of-service - attacks harder (but not impossible - it's windows). Make sure - it even works under vista, which thinks that getpeer/sockname - should return fantasy port numbers. - - include "libev" in all assertion messages for potentially - clearer diagnostics. - - event_get_version (libevent compatibility) returned - a useless string instead of the expected version string - (patch by W.C.A. Wijngaards). - -3.51 Wed Dec 24 23:00:11 CET 2008 - - fix a bug where an inotify watcher was added twice, causing - freezes on hash collisions (reported and analysed by Graham Leggett). - - new config symbol, EV_USE_CLOCK_SYSCALL, to make libev use - a direct syscall - slower, but no dependency on librt et al. - - assume negative return values != -1 signals success of port_getn - (http://cvs.epicsol.org/cgi/viewcvs.cgi/epic5/source/newio.c?rev=1.52) - (no known failure reports, but it doesn't hurt). - - fork detection in ev_embed now stops and restarts the watcher - automatically. - - EXPERIMENTAL: default the method to operator () in ev++.h, - to make it nicer to use functors (requested by Benedek László). - - fixed const object callbacks in ev++.h. - - replaced loop_ref argument of watcher.set (loop) by a direct - ev_loop * in ev++.h, to avoid clashes with functor patch. - - do not try to watch the empty string via inotify. - - inotify watchers could be leaked under certain circumstances. - - OS X 10.5 is actually even more broken than earlier versions, - so fall back to select on that piece of garbage. - - fixed some weirdness in the ev_embed documentation. - -3.49 Wed Nov 19 11:26:53 CET 2008 - - ev_stat watchers will now use inotify as a mere hint on - kernels <2.6.25, or if the filesystem is not in the - "known to be good" list. - - better mingw32 compatibility (it's not as borked as native win32) - (analysed by Roger Pack). - - include stdio.h in the example program, as too many people are - confused by the weird C language otherwise. I guess the next thing - I get told is that the "..." ellipses in the examples don't compile - with their C compiler. - -3.48 Thu Oct 30 09:02:37 CET 2008 - - further optimise away the EPOLL_CTL_ADD/MOD combo in the epoll - backend by assuming the kernel event mask hasn't changed if - ADD fails with EEXIST. - - work around spurious event notification bugs in epoll by using - a 32-bit generation counter. recreate kernel state if we receive - spurious notifications or unwanted events. this is very costly, - but I didn't come up with this horrible design. - - use memset to initialise most arrays now and do away with the - init functions. - - expand time-out strategies into a "Be smart about timeouts" section. - - drop the "struct" from all ev_watcher declarations in the - documentation and did other clarifications (yeah, it was a mistake - to have a struct AND a function called ev_loop). - - fix a bug where ev_default would not initialise the default - loop again after it was destroyed with ev_default_destroy. - - rename syserr to ev_syserr to avoid name clashes when embedding, - do similar changes for event.c. - -3.45 Tue Oct 21 21:59:26 CEST 2008 - - disable inotify usage on linux <2.6.25, as it is broken - (reported by Yoann Vandoorselaere). - - ev_stat erroneously would try to add inotify watchers - even when inotify wasn't available (this should only - have a performance impact). - - ev_once now passes both timeout and io to the callback if both - occur concurrently, instead of giving timeouts precedence. - - disable EV_USE_INOTIFY when sys/inotify.h is too old. - -3.44 Mon Sep 29 05:18:39 CEST 2008 - - embed watchers now automatically invoke ev_loop_fork on the - embedded loop when the parent loop forks. - - new function: ev_now_update (loop). - - verify_watcher was not marked static. - - improve the "associating..." manpage section. - - documentation tweaks here and there. - -3.43 Sun Jul 6 05:34:41 CEST 2008 - - include more include files on windows to get struct _stati64 - (reported by Chris Hulbert, but doesn't quite fix his issue). - - add missing #include in ev.c on windows (reported by - Matt Tolton). - -3.42 Tue Jun 17 12:12:07 CEST 2008 - - work around yet another windows bug: FD_SET actually adds fd's - multiple times to the fd_*SET*, despite official MSN docs claiming - otherwise. Reported and well-analysed by Matt Tolton. - - define NFDBITS to 0 when EV_SELECT_IS_WINSOCKET to make it compile - (reported any analysed by Chris Hulbert). - - fix a bug in ev_ebadf (this function is only used to catch - programming errors in the libev user). reported by Matt Tolton. - - fix a bug in fd_intern on win32 (could lead to compile errors - under some circumstances, but would work correctly if it compiles). - reported by Matt Tolton. - - (try to) work around missing lstat on windows. - - pass in the write fd set as except fd set under windows. windows - is so uncontrollably lame that it requires this. this means that - switching off oobinline is not supported (but tcp/ip doesn't - have oob, so that would be stupid anyways. - - use posix module symbol to auto-detect monotonic clock presence - and some other default values. - -3.41 Fri May 23 18:42:54 CEST 2008 - - work around an obscure bug in winsocket select: if you - provide only empty fd sets then select returns WSAEINVAL. how sucky. - - improve timer scheduling stability and reduce use of time_epsilon. - - use 1-based 2-heap for EV_MINIMAL, simplifies code, reduces - codesize and makes for better cache-efficiency. - - use 3-based 4-heap for !EV_MINIMAL. this makes better use - of cpu cache lines and gives better growth behaviour than - 2-based heaps. - - cache timestamp within heap for !EV_MINIMAL, to avoid random - memory accesses. - - document/add EV_USE_4HEAP and EV_HEAP_CACHE_AT. - - fix a potential aliasing issue in ev_timer_again. - - add/document ev_periodic_at, retract direct access to ->at. - - improve ev_stat docs. - - add portability requirements section. - - fix manpage headers etc. - - normalise WSA error codes to lower range on windows. - - add consistency check code that can be called automatically - or on demand to check for internal structures (ev_loop_verify). - -3.31 Wed Apr 16 20:45:04 CEST 2008 - - added last minute fix for ev_poll.c by Brandon Black. - -3.3 Wed Apr 16 19:04:10 CEST 2008 - - event_base_loopexit should return 0 on success - (W.C.A. Wijngaards). - - added linux eventfd support. - - try to autodetect epoll and inotify support - by libc header version if not using autoconf. - - new symbols: EV_DEFAULT_UC and EV_DEFAULT_UC_. - - declare functions defined in ev.h as inline if - C99 or gcc are available. - - enable inlining with gcc versions 2 and 3. - - work around broken poll implementations potentially - not clearing revents field in ev_poll (Brandon Black) - (no such systems are known at this time). - - work around a bug in realloc on openbsd and darwin, - also makes the erroneous valgrind complaints - go away (noted by various people). - - fix ev_async_pending, add c++ wrapper for ev_async - (based on patch sent by Johannes Deisenhofer). - - add sensible set method to ev::embed. - - made integer constants type int in ev.h. - -3.2 Wed Apr 2 17:11:19 CEST 2008 - - fix a 64 bit overflow issue in the select backend, - by using fd_mask instead of int for the mask. - - rename internal sighandler to avoid clash with very old perls. - - entering ev_loop will not clear the ONESHOT or NONBLOCKING - flags of any outer loops anymore. - - add ev_async_pending. - -3.1 Thu Mar 13 13:45:22 CET 2008 - - implement ev_async watchers. - - only initialise signal pipe on demand. - - make use of sig_atomic_t configurable. - - improved documentation. - -3.0 Mon Jan 28 13:14:47 CET 2008 - - API/ABI bump to version 3.0. - - ev++.h includes "ev.h" by default now, not . - - slightly improved documentation. - - speed up signal detection after a fork. - - only optionally return trace status changed in ev_child - watchers. - - experimental (and undocumented) loop wrappers for ev++.h. - -2.01 Tue Dec 25 08:04:41 CET 2007 - - separate Changes file. - - fix ev_path_set => ev_stat_set typo. - - remove event_compat.h from the libev tarball. - - change how include files are found. - - doc updates. - - update licenses, explicitly allow for GPL relicensing. - -2.0 Sat Dec 22 17:47:03 CET 2007 - - new ev_sleep, ev_set_(io|timeout)_collect_interval. - - removed epoll from embeddable fd set. - - fix embed watchers. - - renamed ev_embed.loop to other. - - added exported Symbol tables. - - undefine member wrapper macros at the end of ev.c. - - respect EV_H in ev++.h. - -1.86 Tue Dec 18 02:36:57 CET 2007 - - fix memleak on loop destroy (not relevant for perl). - -1.85 Fri Dec 14 20:32:40 CET 2007 - - fix some aliasing issues w.r.t. timers and periodics - (not relevant for perl). - -(for historic versions refer to EV/Changes, found in the Perl interface) - -0.1 Wed Oct 31 21:31:48 CET 2007 - - original version; hacked together in <24h. - diff --git a/third_party/libev/LICENSE b/third_party/libev/LICENSE deleted file mode 100644 index 2fdabd48..00000000 --- a/third_party/libev/LICENSE +++ /dev/null @@ -1,37 +0,0 @@ -All files in libev are -Copyright (c)2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -Alternatively, the contents of this package may be used under the terms -of the GNU General Public License ("GPL") version 2 or any later version, -in which case the provisions of the GPL are applicable instead of the -above. If you wish to allow the use of your version of this package only -under the terms of the GPL and not to allow others to use your version of -this file under the BSD license, indicate your decision by deleting the -provisions above and replace them with the notice and other provisions -required by the GPL in this and the other files of this package. If you do -not delete the provisions above, a recipient may use your version of this -file under either the BSD or the GPL. diff --git a/third_party/libev/README b/third_party/libev/README deleted file mode 100644 index 31f61938..00000000 --- a/third_party/libev/README +++ /dev/null @@ -1,58 +0,0 @@ -libev is a high-performance event loop/event model with lots of features. -(see benchmark at http://libev.schmorp.de/bench.html) - - -ABOUT - - Homepage: http://software.schmorp.de/pkg/libev - Mailinglist: libev@lists.schmorp.de - http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev - Library Documentation: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod - - Libev is modelled (very losely) after libevent and the Event perl - module, but is faster, scales better and is more correct, and also more - featureful. And also smaller. Yay. - - Some of the specialties of libev not commonly found elsewhere are: - - - extensive and detailed, readable documentation (not doxygen garbage). - - fully supports fork, can detect fork in various ways and automatically - re-arms kernel mechanisms that do not support fork. - - highly optimised select, poll, epoll, kqueue and event ports backends. - - filesystem object (path) watching (with optional linux inotify support). - - wallclock-based times (using absolute time, cron-like). - - relative timers/timeouts (handle time jumps). - - fast intra-thread communication between multiple - event loops (with optional fast linux eventfd backend). - - extremely easy to embed (fully documented, no dependencies, - autoconf supported but optional). - - very small codebase, no bloated library, simple code. - - fully extensible by being able to plug into the event loop, - integrate other event loops, integrate other event loop users. - - very little memory use (small watchers, small event loop data). - - optional C++ interface allowing method and function callbacks - at no extra memory or runtime overhead. - - optional Perl interface with similar characteristics (capable - of running Glib/Gtk2 on libev). - - support for other languages (multiple C++ interfaces, D, Ruby, - Python) available from third-parties. - - Examples of programs that embed libev: the EV perl module, node.js, - auditd, rxvt-unicode, gvpe (GNU Virtual Private Ethernet), the - Deliantra MMORPG server (http://www.deliantra.net/), Rubinius (a - next-generation Ruby VM), the Ebb web server, the Rev event toolkit. - - -CONTRIBUTORS - - libev was written and designed by Marc Lehmann and Emanuele Giaquinta. - - The following people sent in patches or made other noteworthy - contributions to the design (for minor patches, see the Changes - file. If I forgot to include you, please shout at me, it was an - accident): - - W.C.A. Wijngaards - Christopher Layne - Chris Brody - diff --git a/third_party/libev/ev++.h b/third_party/libev/ev++.h deleted file mode 100644 index c9b453bf..00000000 --- a/third_party/libev/ev++.h +++ /dev/null @@ -1,816 +0,0 @@ -/* - * libev simple C++ wrapper classes - * - * Copyright (c) 2007,2008,2010 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -#ifndef EVPP_H__ -#define EVPP_H__ - -#ifdef EV_H -# include EV_H -#else -# include "ev.h" -#endif - -#ifndef EV_USE_STDEXCEPT -# define EV_USE_STDEXCEPT 1 -#endif - -#if EV_USE_STDEXCEPT -# include -#endif - -namespace ev { - - typedef ev_tstamp tstamp; - - enum { - UNDEF = EV_UNDEF, - NONE = EV_NONE, - READ = EV_READ, - WRITE = EV_WRITE, -#if EV_COMPAT3 - TIMEOUT = EV_TIMEOUT, -#endif - TIMER = EV_TIMER, - PERIODIC = EV_PERIODIC, - SIGNAL = EV_SIGNAL, - CHILD = EV_CHILD, - STAT = EV_STAT, - IDLE = EV_IDLE, - CHECK = EV_CHECK, - PREPARE = EV_PREPARE, - FORK = EV_FORK, - ASYNC = EV_ASYNC, - EMBED = EV_EMBED, -# undef ERROR // some systems stupidly #define ERROR - ERROR = EV_ERROR - }; - - enum - { - AUTO = EVFLAG_AUTO, - NOENV = EVFLAG_NOENV, - FORKCHECK = EVFLAG_FORKCHECK, - - SELECT = EVBACKEND_SELECT, - POLL = EVBACKEND_POLL, - EPOLL = EVBACKEND_EPOLL, - KQUEUE = EVBACKEND_KQUEUE, - DEVPOLL = EVBACKEND_DEVPOLL, - PORT = EVBACKEND_PORT - }; - - enum - { -#if EV_COMPAT3 - NONBLOCK = EVLOOP_NONBLOCK, - ONESHOT = EVLOOP_ONESHOT, -#endif - NOWAIT = EVRUN_NOWAIT, - ONCE = EVRUN_ONCE - }; - - enum how_t - { - ONE = EVBREAK_ONE, - ALL = EVBREAK_ALL - }; - - struct bad_loop -#if EV_USE_STDEXCEPT - : std::runtime_error -#endif - { -#if EV_USE_STDEXCEPT - bad_loop () - : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?") - { - } -#endif - }; - -#ifdef EV_AX -# undef EV_AX -#endif - -#ifdef EV_AX_ -# undef EV_AX_ -#endif - -#if EV_MULTIPLICITY -# define EV_AX raw_loop -# define EV_AX_ raw_loop, -#else -# define EV_AX -# define EV_AX_ -#endif - - struct loop_ref - { - loop_ref (EV_P) throw () -#if EV_MULTIPLICITY - : EV_AX (EV_A) -#endif - { - } - - bool operator == (const loop_ref &other) const throw () - { -#if EV_MULTIPLICITY - return EV_AX == other.EV_AX; -#else - return true; -#endif - } - - bool operator != (const loop_ref &other) const throw () - { -#if EV_MULTIPLICITY - return ! (*this == other); -#else - return false; -#endif - } - -#if EV_MULTIPLICITY - bool operator == (const EV_P) const throw () - { - return this->EV_AX == EV_A; - } - - bool operator != (const EV_P) const throw () - { - return ! (*this == EV_A); - } - - operator struct ev_loop * () const throw () - { - return EV_AX; - } - - operator const struct ev_loop * () const throw () - { - return EV_AX; - } - - bool is_default () const throw () - { - return EV_AX == ev_default_loop (0); - } -#endif - -#if EV_COMPAT3 - void loop (int flags = 0) - { - ev_run (EV_AX_ flags); - } - - void unloop (how_t how = ONE) throw () - { - ev_break (EV_AX_ how); - } -#endif - - void run (int flags = 0) - { - ev_run (EV_AX_ flags); - } - - void break_loop (how_t how = ONE) throw () - { - ev_break (EV_AX_ how); - } - - void post_fork () throw () - { - ev_loop_fork (EV_AX); - } - - unsigned int backend () const throw () - { - return ev_backend (EV_AX); - } - - tstamp now () const throw () - { - return ev_now (EV_AX); - } - - void ref () throw () - { - ev_ref (EV_AX); - } - - void unref () throw () - { - ev_unref (EV_AX); - } - -#if EV_FEATURE_API - unsigned int iteration () const throw () - { - return ev_iteration (EV_AX); - } - - unsigned int depth () const throw () - { - return ev_depth (EV_AX); - } - - void set_io_collect_interval (tstamp interval) throw () - { - ev_set_io_collect_interval (EV_AX_ interval); - } - - void set_timeout_collect_interval (tstamp interval) throw () - { - ev_set_timeout_collect_interval (EV_AX_ interval); - } -#endif - - // function callback - void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) throw () - { - ev_once (EV_AX_ fd, events, timeout, cb, arg); - } - - // method callback - template - void once (int fd, int events, tstamp timeout, K *object) throw () - { - once (fd, events, timeout, method_thunk, object); - } - - // default method == operator () - template - void once (int fd, int events, tstamp timeout, K *object) throw () - { - once (fd, events, timeout, method_thunk, object); - } - - template - static void method_thunk (int revents, void *arg) - { - (static_cast(arg)->*method) - (revents); - } - - // no-argument method callback - template - void once (int fd, int events, tstamp timeout, K *object) throw () - { - once (fd, events, timeout, method_noargs_thunk, object); - } - - template - static void method_noargs_thunk (int revents, void *arg) - { - (static_cast(arg)->*method) - (); - } - - // simpler function callback - template - void once (int fd, int events, tstamp timeout) throw () - { - once (fd, events, timeout, simpler_func_thunk); - } - - template - static void simpler_func_thunk (int revents, void *arg) - { - (*cb) - (revents); - } - - // simplest function callback - template - void once (int fd, int events, tstamp timeout) throw () - { - once (fd, events, timeout, simplest_func_thunk); - } - - template - static void simplest_func_thunk (int revents, void *arg) - { - (*cb) - (); - } - - void feed_fd_event (int fd, int revents) throw () - { - ev_feed_fd_event (EV_AX_ fd, revents); - } - - void feed_signal_event (int signum) throw () - { - ev_feed_signal_event (EV_AX_ signum); - } - -#if EV_MULTIPLICITY - struct ev_loop* EV_AX; -#endif - - }; - -#if EV_MULTIPLICITY - struct dynamic_loop : loop_ref - { - - dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) - : loop_ref (ev_loop_new (flags)) - { - if (!EV_AX) - throw bad_loop (); - } - - ~dynamic_loop () throw () - { - ev_loop_destroy (EV_AX); - EV_AX = 0; - } - - private: - - dynamic_loop (const dynamic_loop &); - - dynamic_loop & operator= (const dynamic_loop &); - - }; -#endif - - struct default_loop : loop_ref - { - default_loop (unsigned int flags = AUTO) throw (bad_loop) -#if EV_MULTIPLICITY - : loop_ref (ev_default_loop (flags)) -#endif - { - if ( -#if EV_MULTIPLICITY - !EV_AX -#else - !ev_default_loop (flags) -#endif - ) - throw bad_loop (); - } - - private: - default_loop (const default_loop &); - default_loop &operator = (const default_loop &); - }; - - inline loop_ref get_default_loop () throw () - { -#if EV_MULTIPLICITY - return ev_default_loop (0); -#else - return loop_ref (); -#endif - } - -#undef EV_AX -#undef EV_AX_ - -#undef EV_PX -#undef EV_PX_ -#if EV_MULTIPLICITY -# define EV_PX loop_ref EV_A -# define EV_PX_ loop_ref EV_A_ -#else -# define EV_PX -# define EV_PX_ -#endif - - template - struct base : ev_watcher - { - #if EV_MULTIPLICITY - EV_PX; - - // loop set - void set (EV_P) throw () - { - this->EV_A = EV_A; - } - #endif - - base (EV_PX) throw () - #if EV_MULTIPLICITY - : EV_A (EV_A) - #endif - { - ev_init (this, 0); - } - - void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () - { - this->data = (void *)data; - ev_set_cb (static_cast(this), cb); - } - - // function callback - template - void set (void *data = 0) throw () - { - set_ (data, function_thunk); - } - - template - static void function_thunk (EV_P_ ev_watcher *w, int revents) - { - function - (*static_cast(w), revents); - } - - // method callback - template - void set (K *object) throw () - { - set_ (object, method_thunk); - } - - // default method == operator () - template - void set (K *object) throw () - { - set_ (object, method_thunk); - } - - template - static void method_thunk (EV_P_ ev_watcher *w, int revents) - { - (static_cast(w->data)->*method) - (*static_cast(w), revents); - } - - // no-argument callback - template - void set (K *object) throw () - { - set_ (object, method_noargs_thunk); - } - - template - static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) - { - (static_cast(w->data)->*method) - (); - } - - void operator ()(int events = EV_UNDEF) - { - return - ev_cb (static_cast(this)) - (static_cast(this), events); - } - - bool is_active () const throw () - { - return ev_is_active (static_cast(this)); - } - - bool is_pending () const throw () - { - return ev_is_pending (static_cast(this)); - } - - void feed_event (int revents) throw () - { - ev_feed_event (EV_A_ static_cast(this), revents); - } - }; - - inline tstamp now (EV_P) throw () - { - return ev_now (EV_A); - } - - inline void delay (tstamp interval) throw () - { - ev_sleep (interval); - } - - inline int version_major () throw () - { - return ev_version_major (); - } - - inline int version_minor () throw () - { - return ev_version_minor (); - } - - inline unsigned int supported_backends () throw () - { - return ev_supported_backends (); - } - - inline unsigned int recommended_backends () throw () - { - return ev_recommended_backends (); - } - - inline unsigned int embeddable_backends () throw () - { - return ev_embeddable_backends (); - } - - inline void set_allocator (void *(*cb)(void *ptr, long size) throw ()) throw () - { - ev_set_allocator (cb); - } - - inline void set_syserr_cb (void (*cb)(const char *msg) throw ()) throw () - { - ev_set_syserr_cb (cb); - } - - #if EV_MULTIPLICITY - #define EV_CONSTRUCT(cppstem,cstem) \ - (EV_PX = get_default_loop ()) throw () \ - : base (EV_A) \ - { \ - } - #else - #define EV_CONSTRUCT(cppstem,cstem) \ - () throw () \ - { \ - } - #endif - - /* using a template here would require quite a few more lines, - * so a macro solution was chosen */ - #define EV_BEGIN_WATCHER(cppstem,cstem) \ - \ - struct cppstem : base \ - { \ - void start () throw () \ - { \ - ev_ ## cstem ## _start (EV_A_ static_cast(this)); \ - } \ - \ - void stop () throw () \ - { \ - ev_ ## cstem ## _stop (EV_A_ static_cast(this)); \ - } \ - \ - cppstem EV_CONSTRUCT(cppstem,cstem) \ - \ - ~cppstem () throw () \ - { \ - stop (); \ - } \ - \ - using base::set; \ - \ - private: \ - \ - cppstem (const cppstem &o); \ - \ - cppstem &operator =(const cppstem &o); \ - \ - public: - - #define EV_END_WATCHER(cppstem,cstem) \ - }; - - EV_BEGIN_WATCHER (io, io) - void set (int fd, int events) throw () - { - int active = is_active (); - if (active) stop (); - ev_io_set (static_cast(this), fd, events); - if (active) start (); - } - - void set (int events) throw () - { - int active = is_active (); - if (active) stop (); - ev_io_set (static_cast(this), fd, events); - if (active) start (); - } - - void start (int fd, int events) throw () - { - set (fd, events); - start (); - } - EV_END_WATCHER (io, io) - - EV_BEGIN_WATCHER (timer, timer) - void set (ev_tstamp after, ev_tstamp repeat = 0.) throw () - { - int active = is_active (); - if (active) stop (); - ev_timer_set (static_cast(this), after, repeat); - if (active) start (); - } - - void start (ev_tstamp after, ev_tstamp repeat = 0.) throw () - { - set (after, repeat); - start (); - } - - void again () throw () - { - ev_timer_again (EV_A_ static_cast(this)); - } - - ev_tstamp remaining () - { - return ev_timer_remaining (EV_A_ static_cast(this)); - } - EV_END_WATCHER (timer, timer) - - #if EV_PERIODIC_ENABLE - EV_BEGIN_WATCHER (periodic, periodic) - void set (ev_tstamp at, ev_tstamp interval = 0.) throw () - { - int active = is_active (); - if (active) stop (); - ev_periodic_set (static_cast(this), at, interval, 0); - if (active) start (); - } - - void start (ev_tstamp at, ev_tstamp interval = 0.) throw () - { - set (at, interval); - start (); - } - - void again () throw () - { - ev_periodic_again (EV_A_ static_cast(this)); - } - EV_END_WATCHER (periodic, periodic) - #endif - - #if EV_SIGNAL_ENABLE - EV_BEGIN_WATCHER (sig, signal) - void set (int signum) throw () - { - int active = is_active (); - if (active) stop (); - ev_signal_set (static_cast(this), signum); - if (active) start (); - } - - void start (int signum) throw () - { - set (signum); - start (); - } - EV_END_WATCHER (sig, signal) - #endif - - #if EV_CHILD_ENABLE - EV_BEGIN_WATCHER (child, child) - void set (int pid, int trace = 0) throw () - { - int active = is_active (); - if (active) stop (); - ev_child_set (static_cast(this), pid, trace); - if (active) start (); - } - - void start (int pid, int trace = 0) throw () - { - set (pid, trace); - start (); - } - EV_END_WATCHER (child, child) - #endif - - #if EV_STAT_ENABLE - EV_BEGIN_WATCHER (stat, stat) - void set (const char *path, ev_tstamp interval = 0.) throw () - { - int active = is_active (); - if (active) stop (); - ev_stat_set (static_cast(this), path, interval); - if (active) start (); - } - - void start (const char *path, ev_tstamp interval = 0.) throw () - { - stop (); - set (path, interval); - start (); - } - - void update () throw () - { - ev_stat_stat (EV_A_ static_cast(this)); - } - EV_END_WATCHER (stat, stat) - #endif - - #if EV_IDLE_ENABLE - EV_BEGIN_WATCHER (idle, idle) - void set () throw () { } - EV_END_WATCHER (idle, idle) - #endif - - #if EV_PREPARE_ENABLE - EV_BEGIN_WATCHER (prepare, prepare) - void set () throw () { } - EV_END_WATCHER (prepare, prepare) - #endif - - #if EV_CHECK_ENABLE - EV_BEGIN_WATCHER (check, check) - void set () throw () { } - EV_END_WATCHER (check, check) - #endif - - #if EV_EMBED_ENABLE - EV_BEGIN_WATCHER (embed, embed) - void set_embed (struct ev_loop *embedded_loop) throw () - { - int active = is_active (); - if (active) stop (); - ev_embed_set (static_cast(this), embedded_loop); - if (active) start (); - } - - void start (struct ev_loop *embedded_loop) throw () - { - set (embedded_loop); - start (); - } - - void sweep () - { - ev_embed_sweep (EV_A_ static_cast(this)); - } - EV_END_WATCHER (embed, embed) - #endif - - #if EV_FORK_ENABLE - EV_BEGIN_WATCHER (fork, fork) - void set () throw () { } - EV_END_WATCHER (fork, fork) - #endif - - #if EV_ASYNC_ENABLE - EV_BEGIN_WATCHER (async, async) - void send () throw () - { - ev_async_send (EV_A_ static_cast(this)); - } - - bool async_pending () throw () - { - return ev_async_pending (static_cast(this)); - } - EV_END_WATCHER (async, async) - #endif - - #undef EV_PX - #undef EV_PX_ - #undef EV_CONSTRUCT - #undef EV_BEGIN_WATCHER - #undef EV_END_WATCHER -} - -#endif - diff --git a/third_party/libev/ev.c b/third_party/libev/ev.c deleted file mode 100644 index fe03dac1..00000000 --- a/third_party/libev/ev.c +++ /dev/null @@ -1,5144 +0,0 @@ -/* - * libev event processing core, watcher management - * - * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -/* this big block deduces configuration from config.h */ -#ifndef EV_STANDALONE -# ifdef EV_CONFIG_H -# include EV_CONFIG_H -# else -# include "config.h" -# endif - -# if HAVE_FLOOR -# ifndef EV_USE_FLOOR -# define EV_USE_FLOOR 1 -# endif -# endif - -# if HAVE_CLOCK_SYSCALL -# ifndef EV_USE_CLOCK_SYSCALL -# define EV_USE_CLOCK_SYSCALL 1 -# ifndef EV_USE_REALTIME -# define EV_USE_REALTIME 0 -# endif -# ifndef EV_USE_MONOTONIC -# define EV_USE_MONOTONIC 1 -# endif -# endif -# elif !defined EV_USE_CLOCK_SYSCALL -# define EV_USE_CLOCK_SYSCALL 0 -# endif - -# if HAVE_CLOCK_GETTIME -# ifndef EV_USE_MONOTONIC -# define EV_USE_MONOTONIC 1 -# endif -# ifndef EV_USE_REALTIME -# define EV_USE_REALTIME 0 -# endif -# else -# ifndef EV_USE_MONOTONIC -# define EV_USE_MONOTONIC 0 -# endif -# ifndef EV_USE_REALTIME -# define EV_USE_REALTIME 0 -# endif -# endif - -# if HAVE_NANOSLEEP -# ifndef EV_USE_NANOSLEEP -# define EV_USE_NANOSLEEP EV_FEATURE_OS -# endif -# else -# undef EV_USE_NANOSLEEP -# define EV_USE_NANOSLEEP 0 -# endif - -# if HAVE_SELECT && HAVE_SYS_SELECT_H -# ifndef EV_USE_SELECT -# define EV_USE_SELECT EV_FEATURE_BACKENDS -# endif -# else -# undef EV_USE_SELECT -# define EV_USE_SELECT 0 -# endif - -# if HAVE_POLL && HAVE_POLL_H -# ifndef EV_USE_POLL -# define EV_USE_POLL EV_FEATURE_BACKENDS -# endif -# else -# undef EV_USE_POLL -# define EV_USE_POLL 0 -# endif - -# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H -# ifndef EV_USE_EPOLL -# define EV_USE_EPOLL EV_FEATURE_BACKENDS -# endif -# else -# undef EV_USE_EPOLL -# define EV_USE_EPOLL 0 -# endif - -# if HAVE_KQUEUE && HAVE_SYS_EVENT_H -# ifndef EV_USE_KQUEUE -# define EV_USE_KQUEUE EV_FEATURE_BACKENDS -# endif -# else -# undef EV_USE_KQUEUE -# define EV_USE_KQUEUE 0 -# endif - -# if HAVE_PORT_H && HAVE_PORT_CREATE -# ifndef EV_USE_PORT -# define EV_USE_PORT EV_FEATURE_BACKENDS -# endif -# else -# undef EV_USE_PORT -# define EV_USE_PORT 0 -# endif - -# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H -# ifndef EV_USE_INOTIFY -# define EV_USE_INOTIFY EV_FEATURE_OS -# endif -# else -# undef EV_USE_INOTIFY -# define EV_USE_INOTIFY 0 -# endif - -# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H -# ifndef EV_USE_SIGNALFD -# define EV_USE_SIGNALFD EV_FEATURE_OS -# endif -# else -# undef EV_USE_SIGNALFD -# define EV_USE_SIGNALFD 0 -# endif - -# if HAVE_EVENTFD -# ifndef EV_USE_EVENTFD -# define EV_USE_EVENTFD EV_FEATURE_OS -# endif -# else -# undef EV_USE_EVENTFD -# define EV_USE_EVENTFD 0 -# endif - -#endif - -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include - -#include - -#ifdef EV_H -# include EV_H -#else -# include "ev.h" -#endif - -#if EV_NO_THREADS -# undef EV_NO_SMP -# define EV_NO_SMP 1 -# undef ECB_NO_THREADS -# define ECB_NO_THREADS 1 -#endif -#if EV_NO_SMP -# undef EV_NO_SMP -# define ECB_NO_SMP 1 -#endif - -#ifndef _WIN32 -# include -# include -# include -#else -# include -# define WIN32_LEAN_AND_MEAN -# include -# include -# ifndef EV_SELECT_IS_WINSOCKET -# define EV_SELECT_IS_WINSOCKET 1 -# endif -# undef EV_AVOID_STDIO -#endif - -/* OS X, in its infinite idiocy, actually HARDCODES - * a limit of 1024 into their select. Where people have brains, - * OS X engineers apparently have a vacuum. Or maybe they were - * ordered to have a vacuum, or they do anything for money. - * This might help. Or not. - */ -#define _DARWIN_UNLIMITED_SELECT 1 - -/* this block tries to deduce configuration from header-defined symbols and defaults */ - -/* try to deduce the maximum number of signals on this platform */ -#if defined EV_NSIG -/* use what's provided */ -#elif defined NSIG -# define EV_NSIG (NSIG) -#elif defined _NSIG -# define EV_NSIG (_NSIG) -#elif defined SIGMAX -# define EV_NSIG (SIGMAX+1) -#elif defined SIG_MAX -# define EV_NSIG (SIG_MAX+1) -#elif defined _SIG_MAX -# define EV_NSIG (_SIG_MAX+1) -#elif defined MAXSIG -# define EV_NSIG (MAXSIG+1) -#elif defined MAX_SIG -# define EV_NSIG (MAX_SIG+1) -#elif defined SIGARRAYSIZE -# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ -#elif defined _sys_nsig -# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ -#else -# define EV_NSIG (8 * sizeof (sigset_t) + 1) -#endif - -#ifndef EV_USE_FLOOR -# define EV_USE_FLOOR 0 -#endif - -#ifndef EV_USE_CLOCK_SYSCALL -# if __linux && __GLIBC__ == 2 && __GLIBC_MINOR__ < 17 -# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS -# else -# define EV_USE_CLOCK_SYSCALL 0 -# endif -#endif - -#if !(_POSIX_TIMERS > 0) -# ifndef EV_USE_MONOTONIC -# define EV_USE_MONOTONIC 0 -# endif -# ifndef EV_USE_REALTIME -# define EV_USE_REALTIME 0 -# endif -#endif - -#ifndef EV_USE_MONOTONIC -# if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 -# define EV_USE_MONOTONIC EV_FEATURE_OS -# else -# define EV_USE_MONOTONIC 0 -# endif -#endif - -#ifndef EV_USE_REALTIME -# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL -#endif - -#ifndef EV_USE_NANOSLEEP -# if _POSIX_C_SOURCE >= 199309L -# define EV_USE_NANOSLEEP EV_FEATURE_OS -# else -# define EV_USE_NANOSLEEP 0 -# endif -#endif - -#ifndef EV_USE_SELECT -# define EV_USE_SELECT EV_FEATURE_BACKENDS -#endif - -#ifndef EV_USE_POLL -# ifdef _WIN32 -# define EV_USE_POLL 0 -# else -# define EV_USE_POLL EV_FEATURE_BACKENDS -# endif -#endif - -#ifndef EV_USE_EPOLL -# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) -# define EV_USE_EPOLL EV_FEATURE_BACKENDS -# else -# define EV_USE_EPOLL 0 -# endif -#endif - -#ifndef EV_USE_KQUEUE -# define EV_USE_KQUEUE 0 -#endif - -#ifndef EV_USE_PORT -# define EV_USE_PORT 0 -#endif - -#ifndef EV_USE_INOTIFY -# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) -# define EV_USE_INOTIFY EV_FEATURE_OS -# else -# define EV_USE_INOTIFY 0 -# endif -#endif - -#ifndef EV_PID_HASHSIZE -# define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1 -#endif - -#ifndef EV_INOTIFY_HASHSIZE -# define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1 -#endif - -#ifndef EV_USE_EVENTFD -# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) -# define EV_USE_EVENTFD EV_FEATURE_OS -# else -# define EV_USE_EVENTFD 0 -# endif -#endif - -#ifndef EV_USE_SIGNALFD -# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) -# define EV_USE_SIGNALFD EV_FEATURE_OS -# else -# define EV_USE_SIGNALFD 0 -# endif -#endif - -#if 0 /* debugging */ -# define EV_VERIFY 3 -# define EV_USE_4HEAP 1 -# define EV_HEAP_CACHE_AT 1 -#endif - -#ifndef EV_VERIFY -# define EV_VERIFY (EV_FEATURE_API ? 1 : 0) -#endif - -#ifndef EV_USE_4HEAP -# define EV_USE_4HEAP EV_FEATURE_DATA -#endif - -#ifndef EV_HEAP_CACHE_AT -# define EV_HEAP_CACHE_AT EV_FEATURE_DATA -#endif - -#ifdef __ANDROID__ -/* supposedly, android doesn't typedef fd_mask */ -# undef EV_USE_SELECT -# define EV_USE_SELECT 0 -/* supposedly, we need to include syscall.h, not sys/syscall.h, so just disable */ -# undef EV_USE_CLOCK_SYSCALL -# define EV_USE_CLOCK_SYSCALL 0 -#endif - -/* aix's poll.h seems to cause lots of trouble */ -#ifdef _AIX -/* AIX has a completely broken poll.h header */ -# undef EV_USE_POLL -# define EV_USE_POLL 0 -#endif - -/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ -/* which makes programs even slower. might work on other unices, too. */ -#if EV_USE_CLOCK_SYSCALL -# include -# ifdef SYS_clock_gettime -# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) -# undef EV_USE_MONOTONIC -# define EV_USE_MONOTONIC 1 -# else -# undef EV_USE_CLOCK_SYSCALL -# define EV_USE_CLOCK_SYSCALL 0 -# endif -#endif - -/* this block fixes any misconfiguration where we know we run into trouble otherwise */ - -#ifndef CLOCK_MONOTONIC -# undef EV_USE_MONOTONIC -# define EV_USE_MONOTONIC 0 -#endif - -#ifndef CLOCK_REALTIME -# undef EV_USE_REALTIME -# define EV_USE_REALTIME 0 -#endif - -#if !EV_STAT_ENABLE -# undef EV_USE_INOTIFY -# define EV_USE_INOTIFY 0 -#endif - -#if !EV_USE_NANOSLEEP -/* hp-ux has it in sys/time.h, which we unconditionally include above */ -# if !defined _WIN32 && !defined __hpux -# include -# endif -#endif - -#if EV_USE_INOTIFY -# include -# include -/* some very old inotify.h headers don't have IN_DONT_FOLLOW */ -# ifndef IN_DONT_FOLLOW -# undef EV_USE_INOTIFY -# define EV_USE_INOTIFY 0 -# endif -#endif - -#if EV_USE_EVENTFD -/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ -# include -# ifndef EFD_NONBLOCK -# define EFD_NONBLOCK O_NONBLOCK -# endif -# ifndef EFD_CLOEXEC -# ifdef O_CLOEXEC -# define EFD_CLOEXEC O_CLOEXEC -# else -# define EFD_CLOEXEC 02000000 -# endif -# endif -EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags); -#endif - -#if EV_USE_SIGNALFD -/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ -# include -# ifndef SFD_NONBLOCK -# define SFD_NONBLOCK O_NONBLOCK -# endif -# ifndef SFD_CLOEXEC -# ifdef O_CLOEXEC -# define SFD_CLOEXEC O_CLOEXEC -# else -# define SFD_CLOEXEC 02000000 -# endif -# endif -EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags); - -struct signalfd_siginfo -{ - uint32_t ssi_signo; - char pad[128 - sizeof (uint32_t)]; -}; -#endif - -/**/ - -#if EV_VERIFY >= 3 -# define EV_FREQUENT_CHECK ev_verify (EV_A) -#else -# define EV_FREQUENT_CHECK do { } while (0) -#endif - -/* - * This is used to work around floating point rounding problems. - * This value is good at least till the year 4000. - */ -#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ -/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ - -#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ -#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ - -#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) -#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) - -/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ -/* ECB.H BEGIN */ -/* - * libecb - http://software.schmorp.de/pkg/libecb - * - * Copyright (©) 2009-2015 Marc Alexander Lehmann - * Copyright (©) 2011 Emanuele Giaquinta - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -#ifndef ECB_H -#define ECB_H - -/* 16 bits major, 16 bits minor */ -#define ECB_VERSION 0x00010005 - -#ifdef _WIN32 - typedef signed char int8_t; - typedef unsigned char uint8_t; - typedef signed short int16_t; - typedef unsigned short uint16_t; - typedef signed int int32_t; - typedef unsigned int uint32_t; - #if __GNUC__ - typedef signed long long int64_t; - typedef unsigned long long uint64_t; - #else /* _MSC_VER || __BORLANDC__ */ - typedef signed __int64 int64_t; - typedef unsigned __int64 uint64_t; - #endif - #ifdef _WIN64 - #define ECB_PTRSIZE 8 - typedef uint64_t uintptr_t; - typedef int64_t intptr_t; - #else - #define ECB_PTRSIZE 4 - typedef uint32_t uintptr_t; - typedef int32_t intptr_t; - #endif -#else - #include - #if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU - #define ECB_PTRSIZE 8 - #else - #define ECB_PTRSIZE 4 - #endif -#endif - -#define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) -#define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) - -/* work around x32 idiocy by defining proper macros */ -#if ECB_GCC_AMD64 || ECB_MSVC_AMD64 - #if _ILP32 - #define ECB_AMD64_X32 1 - #else - #define ECB_AMD64 1 - #endif -#endif - -/* many compilers define _GNUC_ to some versions but then only implement - * what their idiot authors think are the "more important" extensions, - * causing enormous grief in return for some better fake benchmark numbers. - * or so. - * we try to detect these and simply assume they are not gcc - if they have - * an issue with that they should have done it right in the first place. - */ -#if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__ - #define ECB_GCC_VERSION(major,minor) 0 -#else - #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) -#endif - -#define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor))) - -#if __clang__ && defined __has_builtin - #define ECB_CLANG_BUILTIN(x) __has_builtin (x) -#else - #define ECB_CLANG_BUILTIN(x) 0 -#endif - -#if __clang__ && defined __has_extension - #define ECB_CLANG_EXTENSION(x) __has_extension (x) -#else - #define ECB_CLANG_EXTENSION(x) 0 -#endif - -#define ECB_CPP (__cplusplus+0) -#define ECB_CPP11 (__cplusplus >= 201103L) - -#if ECB_CPP - #define ECB_C 0 - #define ECB_STDC_VERSION 0 -#else - #define ECB_C 1 - #define ECB_STDC_VERSION __STDC_VERSION__ -#endif - -#define ECB_C99 (ECB_STDC_VERSION >= 199901L) -#define ECB_C11 (ECB_STDC_VERSION >= 201112L) - -#if ECB_CPP - #define ECB_EXTERN_C extern "C" - #define ECB_EXTERN_C_BEG ECB_EXTERN_C { - #define ECB_EXTERN_C_END } -#else - #define ECB_EXTERN_C extern - #define ECB_EXTERN_C_BEG - #define ECB_EXTERN_C_END -#endif - -/*****************************************************************************/ - -/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ -/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ - -#if ECB_NO_THREADS - #define ECB_NO_SMP 1 -#endif - -#if ECB_NO_SMP - #define ECB_MEMORY_FENCE do { } while (0) -#endif - -/* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */ -#if __xlC__ && ECB_CPP - #include -#endif - -#if 1400 <= _MSC_VER - #include /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ -#endif - -#ifndef ECB_MEMORY_FENCE - #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 - #if __i386 || __i386__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") - #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") - #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") - #elif ECB_GCC_AMD64 - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") - #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") - #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") - #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") - #elif defined __ARM_ARCH_2__ \ - || defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \ - || defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \ - || defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \ - || defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \ - || defined __ARM_ARCH_5TEJ__ - /* should not need any, unless running old code on newer cpu - arm doesn't support that */ - #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ - || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \ - || defined __ARM_ARCH_6T2__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") - #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ - || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") - #elif __aarch64__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory") - #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8) - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory") - #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") - #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") - #elif defined __s390__ || defined __s390x__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") - #elif defined __mips__ - /* GNU/Linux emulates sync on mips1 architectures, so we force its use */ - /* anybody else who still uses mips1 is supposed to send in their version, with detection code. */ - #define ECB_MEMORY_FENCE __asm__ __volatile__ (".set mips2; sync; .set mips0" : : : "memory") - #elif defined __alpha__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory") - #elif defined __hppa__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") - #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") - #elif defined __ia64__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory") - #elif defined __m68k__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") - #elif defined __m88k__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("tb1 0,%%r0,128" : : : "memory") - #elif defined __sh__ - #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") - #endif - #endif -#endif - -#ifndef ECB_MEMORY_FENCE - #if ECB_GCC_VERSION(4,7) - /* see comment below (stdatomic.h) about the C11 memory model. */ - #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) - #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) - #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) - - #elif ECB_CLANG_EXTENSION(c_atomic) - /* see comment below (stdatomic.h) about the C11 memory model. */ - #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) - #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) - #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) - - #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ - #define ECB_MEMORY_FENCE __sync_synchronize () - #elif _MSC_VER >= 1500 /* VC++ 2008 */ - /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ - #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) - #define ECB_MEMORY_FENCE _ReadWriteBarrier (); MemoryBarrier() - #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier (); MemoryBarrier() /* according to msdn, _ReadBarrier is not a load fence */ - #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier (); MemoryBarrier() - #elif _MSC_VER >= 1400 /* VC++ 2005 */ - #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) - #define ECB_MEMORY_FENCE _ReadWriteBarrier () - #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ - #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () - #elif defined _WIN32 - #include - #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ - #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 - #include - #define ECB_MEMORY_FENCE __machine_rw_barrier () - #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () - #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () - #elif __xlC__ - #define ECB_MEMORY_FENCE __sync () - #endif -#endif - -#ifndef ECB_MEMORY_FENCE - #if ECB_C11 && !defined __STDC_NO_ATOMICS__ - /* we assume that these memory fences work on all variables/all memory accesses, */ - /* not just C11 atomics and atomic accesses */ - #include - /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ - /* any fence other than seq_cst, which isn't very efficient for us. */ - /* Why that is, we don't know - either the C11 memory model is quite useless */ - /* for most usages, or gcc and clang have a bug */ - /* I *currently* lean towards the latter, and inefficiently implement */ - /* all three of ecb's fences as a seq_cst fence */ - /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */ - /* for all __atomic_thread_fence's except seq_cst */ - #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) - #endif -#endif - -#ifndef ECB_MEMORY_FENCE - #if !ECB_AVOID_PTHREADS - /* - * if you get undefined symbol references to pthread_mutex_lock, - * or failure to find pthread.h, then you should implement - * the ECB_MEMORY_FENCE operations for your cpu/compiler - * OR provide pthread.h and link against the posix thread library - * of your system. - */ - #include - #define ECB_NEEDS_PTHREADS 1 - #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 - - static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; - #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) - #endif -#endif - -#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE - #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE -#endif - -#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE - #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE -#endif - -/*****************************************************************************/ - -#if ECB_CPP - #define ecb_inline static inline -#elif ECB_GCC_VERSION(2,5) - #define ecb_inline static __inline__ -#elif ECB_C99 - #define ecb_inline static inline -#else - #define ecb_inline static -#endif - -#if ECB_GCC_VERSION(3,3) - #define ecb_restrict __restrict__ -#elif ECB_C99 - #define ecb_restrict restrict -#else - #define ecb_restrict -#endif - -typedef int ecb_bool; - -#define ECB_CONCAT_(a, b) a ## b -#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) -#define ECB_STRINGIFY_(a) # a -#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) -#define ECB_STRINGIFY_EXPR(expr) ((expr), ECB_STRINGIFY_ (expr)) - -#define ecb_function_ ecb_inline - -#if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8) - #define ecb_attribute(attrlist) __attribute__ (attrlist) -#else - #define ecb_attribute(attrlist) -#endif - -#if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_constant_p) - #define ecb_is_constant(expr) __builtin_constant_p (expr) -#else - /* possible C11 impl for integral types - typedef struct ecb_is_constant_struct ecb_is_constant_struct; - #define ecb_is_constant(expr) _Generic ((1 ? (struct ecb_is_constant_struct *)0 : (void *)((expr) - (expr)), ecb_is_constant_struct *: 0, default: 1)) */ - - #define ecb_is_constant(expr) 0 -#endif - -#if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_expect) - #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) -#else - #define ecb_expect(expr,value) (expr) -#endif - -#if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_prefetch) - #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) -#else - #define ecb_prefetch(addr,rw,locality) -#endif - -/* no emulation for ecb_decltype */ -#if ECB_CPP11 - // older implementations might have problems with decltype(x)::type, work around it - template struct ecb_decltype_t { typedef T type; }; - #define ecb_decltype(x) ecb_decltype_t::type -#elif ECB_GCC_VERSION(3,0) || ECB_CLANG_VERSION(2,8) - #define ecb_decltype(x) __typeof__ (x) -#endif - -#if _MSC_VER >= 1300 - #define ecb_deprecated __declspec (deprecated) -#else - #define ecb_deprecated ecb_attribute ((__deprecated__)) -#endif - -#if _MSC_VER >= 1500 - #define ecb_deprecated_message(msg) __declspec (deprecated (msg)) -#elif ECB_GCC_VERSION(4,5) - #define ecb_deprecated_message(msg) ecb_attribute ((__deprecated__ (msg)) -#else - #define ecb_deprecated_message(msg) ecb_deprecated -#endif - -#if _MSC_VER >= 1400 - #define ecb_noinline __declspec (noinline) -#else - #define ecb_noinline ecb_attribute ((__noinline__)) -#endif - -#define ecb_unused ecb_attribute ((__unused__)) -#define ecb_const ecb_attribute ((__const__)) -#define ecb_pure ecb_attribute ((__pure__)) - -#if ECB_C11 || __IBMC_NORETURN - /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/language_ref/noreturn.html */ - #define ecb_noreturn _Noreturn -#elif ECB_CPP11 - #define ecb_noreturn [[noreturn]] -#elif _MSC_VER >= 1200 - /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx */ - #define ecb_noreturn __declspec (noreturn) -#else - #define ecb_noreturn ecb_attribute ((__noreturn__)) -#endif - -#if ECB_GCC_VERSION(4,3) - #define ecb_artificial ecb_attribute ((__artificial__)) - #define ecb_hot ecb_attribute ((__hot__)) - #define ecb_cold ecb_attribute ((__cold__)) -#else - #define ecb_artificial - #define ecb_hot - #define ecb_cold -#endif - -/* put around conditional expressions if you are very sure that the */ -/* expression is mostly true or mostly false. note that these return */ -/* booleans, not the expression. */ -#define ecb_expect_false(expr) ecb_expect (!!(expr), 0) -#define ecb_expect_true(expr) ecb_expect (!!(expr), 1) -/* for compatibility to the rest of the world */ -#define ecb_likely(expr) ecb_expect_true (expr) -#define ecb_unlikely(expr) ecb_expect_false (expr) - -/* count trailing zero bits and count # of one bits */ -#if ECB_GCC_VERSION(3,4) \ - || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \ - && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \ - && ECB_CLANG_BUILTIN(__builtin_popcount)) - /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ - #define ecb_ld32(x) (__builtin_clz (x) ^ 31) - #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) - #define ecb_ctz32(x) __builtin_ctz (x) - #define ecb_ctz64(x) __builtin_ctzll (x) - #define ecb_popcount32(x) __builtin_popcount (x) - /* no popcountll */ -#else - ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); - ecb_function_ ecb_const int - ecb_ctz32 (uint32_t x) - { -#if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) - unsigned long r; - _BitScanForward (&r, x); - return (int)r; -#else - int r = 0; - - x &= ~x + 1; /* this isolates the lowest bit */ - -#if ECB_branchless_on_i386 - r += !!(x & 0xaaaaaaaa) << 0; - r += !!(x & 0xcccccccc) << 1; - r += !!(x & 0xf0f0f0f0) << 2; - r += !!(x & 0xff00ff00) << 3; - r += !!(x & 0xffff0000) << 4; -#else - if (x & 0xaaaaaaaa) r += 1; - if (x & 0xcccccccc) r += 2; - if (x & 0xf0f0f0f0) r += 4; - if (x & 0xff00ff00) r += 8; - if (x & 0xffff0000) r += 16; -#endif - - return r; -#endif - } - - ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); - ecb_function_ ecb_const int - ecb_ctz64 (uint64_t x) - { -#if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) - unsigned long r; - _BitScanForward64 (&r, x); - return (int)r; -#else - int shift = x & 0xffffffff ? 0 : 32; - return ecb_ctz32 (x >> shift) + shift; -#endif - } - - ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); - ecb_function_ ecb_const int - ecb_popcount32 (uint32_t x) - { - x -= (x >> 1) & 0x55555555; - x = ((x >> 2) & 0x33333333) + (x & 0x33333333); - x = ((x >> 4) + x) & 0x0f0f0f0f; - x *= 0x01010101; - - return x >> 24; - } - - ecb_function_ ecb_const int ecb_ld32 (uint32_t x); - ecb_function_ ecb_const int ecb_ld32 (uint32_t x) - { -#if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM) - unsigned long r; - _BitScanReverse (&r, x); - return (int)r; -#else - int r = 0; - - if (x >> 16) { x >>= 16; r += 16; } - if (x >> 8) { x >>= 8; r += 8; } - if (x >> 4) { x >>= 4; r += 4; } - if (x >> 2) { x >>= 2; r += 2; } - if (x >> 1) { r += 1; } - - return r; -#endif - } - - ecb_function_ ecb_const int ecb_ld64 (uint64_t x); - ecb_function_ ecb_const int ecb_ld64 (uint64_t x) - { -#if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM) - unsigned long r; - _BitScanReverse64 (&r, x); - return (int)r; -#else - int r = 0; - - if (x >> 32) { x >>= 32; r += 32; } - - return r + ecb_ld32 (x); -#endif - } -#endif - -ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x); -ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } -ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x); -ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); } - -ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x); -ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x) -{ - return ( (x * 0x0802U & 0x22110U) - | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; -} - -ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x); -ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x) -{ - x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); - x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); - x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); - x = ( x >> 8 ) | ( x << 8); - - return x; -} - -ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x); -ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x) -{ - x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); - x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); - x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); - x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); - x = ( x >> 16 ) | ( x << 16); - - return x; -} - -/* popcount64 is only available on 64 bit cpus as gcc builtin */ -/* so for this version we are lazy */ -ecb_function_ ecb_const int ecb_popcount64 (uint64_t x); -ecb_function_ ecb_const int -ecb_popcount64 (uint64_t x) -{ - return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); -} - -ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count); -ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count); -ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count); -ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count); -ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); -ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count); -ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count); -ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count); - -ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } -ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } -ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } -ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } -ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } -ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } -ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } -ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } - -#if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) - #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16) - #define ecb_bswap16(x) __builtin_bswap16 (x) - #else - #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) - #endif - #define ecb_bswap32(x) __builtin_bswap32 (x) - #define ecb_bswap64(x) __builtin_bswap64 (x) -#elif _MSC_VER - #include - #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x))) - #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x))) - #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x))) -#else - ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x); - ecb_function_ ecb_const uint16_t - ecb_bswap16 (uint16_t x) - { - return ecb_rotl16 (x, 8); - } - - ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x); - ecb_function_ ecb_const uint32_t - ecb_bswap32 (uint32_t x) - { - return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); - } - - ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x); - ecb_function_ ecb_const uint64_t - ecb_bswap64 (uint64_t x) - { - return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); - } -#endif - -#if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable) - #define ecb_unreachable() __builtin_unreachable () -#else - /* this seems to work fine, but gcc always emits a warning for it :/ */ - ecb_inline ecb_noreturn void ecb_unreachable (void); - ecb_inline ecb_noreturn void ecb_unreachable (void) { } -#endif - -/* try to tell the compiler that some condition is definitely true */ -#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 - -ecb_inline ecb_const uint32_t ecb_byteorder_helper (void); -ecb_inline ecb_const uint32_t -ecb_byteorder_helper (void) -{ - /* the union code still generates code under pressure in gcc, */ - /* but less than using pointers, and always seems to */ - /* successfully return a constant. */ - /* the reason why we have this horrible preprocessor mess */ - /* is to avoid it in all cases, at least on common architectures */ - /* or when using a recent enough gcc version (>= 4.6) */ -#if (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \ - || ((__i386 || __i386__ || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64) && !__VOS__) - #define ECB_LITTLE_ENDIAN 1 - return 0x44332211; -#elif (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \ - || ((__AARCH64EB__ || __MIPSEB__ || __ARMEB__) && !__VOS__) - #define ECB_BIG_ENDIAN 1 - return 0x11223344; -#else - union - { - uint8_t c[4]; - uint32_t u; - } u = { 0x11, 0x22, 0x33, 0x44 }; - return u.u; -#endif -} - -ecb_inline ecb_const ecb_bool ecb_big_endian (void); -ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; } -ecb_inline ecb_const ecb_bool ecb_little_endian (void); -ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; } - -#if ECB_GCC_VERSION(3,0) || ECB_C99 - #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) -#else - #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) -#endif - -#if ECB_CPP - template - static inline T ecb_div_rd (T val, T div) - { - return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; - } - template - static inline T ecb_div_ru (T val, T div) - { - return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; - } -#else - #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) - #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) -#endif - -#if ecb_cplusplus_does_not_suck - /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ - template - static inline int ecb_array_length (const T (&arr)[N]) - { - return N; - } -#else - #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) -#endif - -ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); -ecb_function_ ecb_const uint32_t -ecb_binary16_to_binary32 (uint32_t x) -{ - unsigned int s = (x & 0x8000) << (31 - 15); - int e = (x >> 10) & 0x001f; - unsigned int m = x & 0x03ff; - - if (ecb_expect_false (e == 31)) - /* infinity or NaN */ - e = 255 - (127 - 15); - else if (ecb_expect_false (!e)) - { - if (ecb_expect_true (!m)) - /* zero, handled by code below by forcing e to 0 */ - e = 0 - (127 - 15); - else - { - /* subnormal, renormalise */ - unsigned int s = 10 - ecb_ld32 (m); - - m = (m << s) & 0x3ff; /* mask implicit bit */ - e -= s - 1; - } - } - - /* e and m now are normalised, or zero, (or inf or nan) */ - e += 127 - 15; - - return s | (e << 23) | (m << (23 - 10)); -} - -ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x); -ecb_function_ ecb_const uint16_t -ecb_binary32_to_binary16 (uint32_t x) -{ - unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */ - unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */ - unsigned int m = x & 0x007fffff; - - x &= 0x7fffffff; - - /* if it's within range of binary16 normals, use fast path */ - if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff)) - { - /* mantissa round-to-even */ - m += 0x00000fff + ((m >> (23 - 10)) & 1); - - /* handle overflow */ - if (ecb_expect_false (m >= 0x00800000)) - { - m >>= 1; - e += 1; - } - - return s | (e << 10) | (m >> (23 - 10)); - } - - /* handle large numbers and infinity */ - if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000)) - return s | 0x7c00; - - /* handle zero, subnormals and small numbers */ - if (ecb_expect_true (x < 0x38800000)) - { - /* zero */ - if (ecb_expect_true (!x)) - return s; - - /* handle subnormals */ - - /* too small, will be zero */ - if (e < (14 - 24)) /* might not be sharp, but is good enough */ - return s; - - m |= 0x00800000; /* make implicit bit explicit */ - - /* very tricky - we need to round to the nearest e (+10) bit value */ - { - unsigned int bits = 14 - e; - unsigned int half = (1 << (bits - 1)) - 1; - unsigned int even = (m >> bits) & 1; - - /* if this overflows, we will end up with a normalised number */ - m = (m + half + even) >> bits; - } - - return s | m; - } - - /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */ - m >>= 13; - - return s | 0x7c00 | m | !m; -} - -/*******************************************************************************/ -/* floating point stuff, can be disabled by defining ECB_NO_LIBM */ - -/* basically, everything uses "ieee pure-endian" floating point numbers */ -/* the only noteworthy exception is ancient armle, which uses order 43218765 */ -#if 0 \ - || __i386 || __i386__ \ - || ECB_GCC_AMD64 \ - || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \ - || defined __s390__ || defined __s390x__ \ - || defined __mips__ \ - || defined __alpha__ \ - || defined __hppa__ \ - || defined __ia64__ \ - || defined __m68k__ \ - || defined __m88k__ \ - || defined __sh__ \ - || defined _M_IX86 || defined ECB_MSVC_AMD64 || defined _M_IA64 \ - || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \ - || defined __aarch64__ - #define ECB_STDFP 1 - #include /* for memcpy */ -#else - #define ECB_STDFP 0 -#endif - -#ifndef ECB_NO_LIBM - - #include /* for frexp*, ldexp*, INFINITY, NAN */ - - /* only the oldest of old doesn't have this one. solaris. */ - #ifdef INFINITY - #define ECB_INFINITY INFINITY - #else - #define ECB_INFINITY HUGE_VAL - #endif - - #ifdef NAN - #define ECB_NAN NAN - #else - #define ECB_NAN ECB_INFINITY - #endif - - #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L - #define ecb_ldexpf(x,e) ldexpf ((x), (e)) - #define ecb_frexpf(x,e) frexpf ((x), (e)) - #else - #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e)) - #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e)) - #endif - - /* convert a float to ieee single/binary32 */ - ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); - ecb_function_ ecb_const uint32_t - ecb_float_to_binary32 (float x) - { - uint32_t r; - - #if ECB_STDFP - memcpy (&r, &x, 4); - #else - /* slow emulation, works for anything but -0 */ - uint32_t m; - int e; - - if (x == 0e0f ) return 0x00000000U; - if (x > +3.40282346638528860e+38f) return 0x7f800000U; - if (x < -3.40282346638528860e+38f) return 0xff800000U; - if (x != x ) return 0x7fbfffffU; - - m = ecb_frexpf (x, &e) * 0x1000000U; - - r = m & 0x80000000U; - - if (r) - m = -m; - - if (e <= -126) - { - m &= 0xffffffU; - m >>= (-125 - e); - e = -126; - } - - r |= (e + 126) << 23; - r |= m & 0x7fffffU; - #endif - - return r; - } - - /* converts an ieee single/binary32 to a float */ - ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x); - ecb_function_ ecb_const float - ecb_binary32_to_float (uint32_t x) - { - float r; - - #if ECB_STDFP - memcpy (&r, &x, 4); - #else - /* emulation, only works for normals and subnormals and +0 */ - int neg = x >> 31; - int e = (x >> 23) & 0xffU; - - x &= 0x7fffffU; - - if (e) - x |= 0x800000U; - else - e = 1; - - /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */ - r = ecb_ldexpf (x * (0.5f / 0x800000U), e - 126); - - r = neg ? -r : r; - #endif - - return r; - } - - /* convert a double to ieee double/binary64 */ - ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x); - ecb_function_ ecb_const uint64_t - ecb_double_to_binary64 (double x) - { - uint64_t r; - - #if ECB_STDFP - memcpy (&r, &x, 8); - #else - /* slow emulation, works for anything but -0 */ - uint64_t m; - int e; - - if (x == 0e0 ) return 0x0000000000000000U; - if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U; - if (x < -1.79769313486231470e+308) return 0xfff0000000000000U; - if (x != x ) return 0X7ff7ffffffffffffU; - - m = frexp (x, &e) * 0x20000000000000U; - - r = m & 0x8000000000000000;; - - if (r) - m = -m; - - if (e <= -1022) - { - m &= 0x1fffffffffffffU; - m >>= (-1021 - e); - e = -1022; - } - - r |= ((uint64_t)(e + 1022)) << 52; - r |= m & 0xfffffffffffffU; - #endif - - return r; - } - - /* converts an ieee double/binary64 to a double */ - ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x); - ecb_function_ ecb_const double - ecb_binary64_to_double (uint64_t x) - { - double r; - - #if ECB_STDFP - memcpy (&r, &x, 8); - #else - /* emulation, only works for normals and subnormals and +0 */ - int neg = x >> 63; - int e = (x >> 52) & 0x7ffU; - - x &= 0xfffffffffffffU; - - if (e) - x |= 0x10000000000000U; - else - e = 1; - - /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */ - r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022); - - r = neg ? -r : r; - #endif - - return r; - } - - /* convert a float to ieee half/binary16 */ - ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x); - ecb_function_ ecb_const uint16_t - ecb_float_to_binary16 (float x) - { - return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x)); - } - - /* convert an ieee half/binary16 to float */ - ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x); - ecb_function_ ecb_const float - ecb_binary16_to_float (uint16_t x) - { - return ecb_binary32_to_float (ecb_binary16_to_binary32 (x)); - } - -#endif - -#endif - -/* ECB.H END */ - -#if ECB_MEMORY_FENCE_NEEDS_PTHREADS -/* if your architecture doesn't need memory fences, e.g. because it is - * single-cpu/core, or if you use libev in a project that doesn't use libev - * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling - * libev, in which cases the memory fences become nops. - * alternatively, you can remove this #error and link against libpthread, - * which will then provide the memory fences. - */ -# error "memory fences not defined for your architecture, please report" -#endif - -#ifndef ECB_MEMORY_FENCE -# define ECB_MEMORY_FENCE do { } while (0) -# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE -# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE -#endif - -#define expect_false(cond) ecb_expect_false (cond) -#define expect_true(cond) ecb_expect_true (cond) -#define noinline ecb_noinline - -#define inline_size ecb_inline - -#if EV_FEATURE_CODE -# define inline_speed ecb_inline -#else -# define inline_speed noinline static -#endif - -#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) - -#if EV_MINPRI == EV_MAXPRI -# define ABSPRI(w) (((W)w), 0) -#else -# define ABSPRI(w) (((W)w)->priority - EV_MINPRI) -#endif - -#define EMPTY /* required for microsofts broken pseudo-c compiler */ -#define EMPTY2(a,b) /* used to suppress some warnings */ - -typedef ev_watcher *W; -typedef ev_watcher_list *WL; -typedef ev_watcher_time *WT; - -#define ev_active(w) ((W)(w))->active -#define ev_at(w) ((WT)(w))->at - -#if EV_USE_REALTIME -/* sig_atomic_t is used to avoid per-thread variables or locking but still */ -/* giving it a reasonably high chance of working on typical architectures */ -static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ -#endif - -#if EV_USE_MONOTONIC -static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ -#endif - -#ifndef EV_FD_TO_WIN32_HANDLE -# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) -#endif -#ifndef EV_WIN32_HANDLE_TO_FD -# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0) -#endif -#ifndef EV_WIN32_CLOSE_FD -# define EV_WIN32_CLOSE_FD(fd) close (fd) -#endif - -#ifdef _WIN32 -# include "ev_win32.c" -#endif - -/*****************************************************************************/ - -/* define a suitable floor function (only used by periodics atm) */ - -#if EV_USE_FLOOR -# include -# define ev_floor(v) floor (v) -#else - -#include - -/* a floor() replacement function, should be independent of ev_tstamp type */ -noinline -static ev_tstamp -ev_floor (ev_tstamp v) -{ - /* the choice of shift factor is not terribly important */ -#if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */ - const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; -#else - const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; -#endif - - /* argument too large for an unsigned long? */ - if (expect_false (v >= shift)) - { - ev_tstamp f; - - if (v == v - 1.) - return v; /* very large number */ - - f = shift * ev_floor (v * (1. / shift)); - return f + ev_floor (v - f); - } - - /* special treatment for negative args? */ - if (expect_false (v < 0.)) - { - ev_tstamp f = -ev_floor (-v); - - return f - (f == v ? 0 : 1); - } - - /* fits into an unsigned long */ - return (unsigned long)v; -} - -#endif - -/*****************************************************************************/ - -#ifdef __linux -# include -#endif - -noinline ecb_cold -static unsigned int -ev_linux_version (void) -{ -#ifdef __linux - unsigned int v = 0; - struct utsname buf; - int i; - char *p = buf.release; - - if (uname (&buf)) - return 0; - - for (i = 3+1; --i; ) - { - unsigned int c = 0; - - for (;;) - { - if (*p >= '0' && *p <= '9') - c = c * 10 + *p++ - '0'; - else - { - p += *p == '.'; - break; - } - } - - v = (v << 8) | c; - } - - return v; -#else - return 0; -#endif -} - -/*****************************************************************************/ - -#if EV_AVOID_STDIO -noinline ecb_cold -static void -ev_printerr (const char *msg) -{ - write (STDERR_FILENO, msg, strlen (msg)); -} -#endif - -static void (*syserr_cb)(const char *msg) EV_THROW; - -ecb_cold -void -ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW -{ - syserr_cb = cb; -} - -noinline ecb_cold -static void -ev_syserr (const char *msg) -{ - if (!msg) - msg = "(libev) system error"; - - if (syserr_cb) - syserr_cb (msg); - else - { -#if EV_AVOID_STDIO - ev_printerr (msg); - ev_printerr (": "); - ev_printerr (strerror (errno)); - ev_printerr ("\n"); -#else - perror (msg); -#endif - abort (); - } -} - -static void * -ev_realloc_emul (void *ptr, long size) EV_THROW -{ - /* some systems, notably openbsd and darwin, fail to properly - * implement realloc (x, 0) (as required by both ansi c-89 and - * the single unix specification, so work around them here. - * recently, also (at least) fedora and debian started breaking it, - * despite documenting it otherwise. - */ - - if (size) - return realloc (ptr, size); - - free (ptr); - return 0; -} - -static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; - -ecb_cold -void -ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW -{ - alloc = cb; -} - -inline_speed void * -ev_realloc (void *ptr, long size) -{ - ptr = alloc (ptr, size); - - if (!ptr && size) - { -#if EV_AVOID_STDIO - ev_printerr ("(libev) memory allocation failed, aborting.\n"); -#else - fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size); -#endif - abort (); - } - - return ptr; -} - -#define ev_malloc(size) ev_realloc (0, (size)) -#define ev_free(ptr) ev_realloc ((ptr), 0) - -/*****************************************************************************/ - -/* set in reify when reification needed */ -#define EV_ANFD_REIFY 1 - -/* file descriptor info structure */ -typedef struct -{ - WL head; - unsigned char events; /* the events watched for */ - unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ - unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ - unsigned char unused; -#if EV_USE_EPOLL - unsigned int egen; /* generation counter to counter epoll bugs */ -#endif -#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP - SOCKET handle; -#endif -#if EV_USE_IOCP - OVERLAPPED or, ow; -#endif -} ANFD; - -/* stores the pending event set for a given watcher */ -typedef struct -{ - W w; - int events; /* the pending event set for the given watcher */ -} ANPENDING; - -#if EV_USE_INOTIFY -/* hash table entry per inotify-id */ -typedef struct -{ - WL head; -} ANFS; -#endif - -/* Heap Entry */ -#if EV_HEAP_CACHE_AT - /* a heap element */ - typedef struct { - ev_tstamp at; - WT w; - } ANHE; - - #define ANHE_w(he) (he).w /* access watcher, read-write */ - #define ANHE_at(he) (he).at /* access cached at, read-only */ - #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ -#else - /* a heap element */ - typedef WT ANHE; - - #define ANHE_w(he) (he) - #define ANHE_at(he) (he)->at - #define ANHE_at_cache(he) -#endif - -#if EV_MULTIPLICITY - - struct ev_loop - { - ev_tstamp ev_rt_now; - #define ev_rt_now ((loop)->ev_rt_now) - #define VAR(name,decl) decl; - #include "ev_vars.h" - #undef VAR - }; - #include "ev_wrap.h" - - static struct ev_loop default_loop_struct; - EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ - -#else - - EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ - #define VAR(name,decl) static decl; - #include "ev_vars.h" - #undef VAR - - static int ev_default_loop_ptr; - -#endif - -#if EV_FEATURE_API -# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) -# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) -# define EV_INVOKE_PENDING invoke_cb (EV_A) -#else -# define EV_RELEASE_CB (void)0 -# define EV_ACQUIRE_CB (void)0 -# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) -#endif - -#define EVBREAK_RECURSE 0x80 - -/*****************************************************************************/ - -#ifndef EV_HAVE_EV_TIME -ev_tstamp -ev_time (void) EV_THROW -{ -#if EV_USE_REALTIME - if (expect_true (have_realtime)) - { - struct timespec ts; - clock_gettime (CLOCK_REALTIME, &ts); - return ts.tv_sec + ts.tv_nsec * 1e-9; - } -#endif - - struct timeval tv; - gettimeofday (&tv, 0); - return tv.tv_sec + tv.tv_usec * 1e-6; -} -#endif - -inline_size ev_tstamp -get_clock (void) -{ -#if EV_USE_MONOTONIC - if (expect_true (have_monotonic)) - { - struct timespec ts; - clock_gettime (CLOCK_MONOTONIC, &ts); - return ts.tv_sec + ts.tv_nsec * 1e-9; - } -#endif - - return ev_time (); -} - -#if EV_MULTIPLICITY -ev_tstamp -ev_now (EV_P) EV_THROW -{ - return ev_rt_now; -} -#endif - -void -ev_sleep (ev_tstamp delay) EV_THROW -{ - if (delay > 0.) - { -#if EV_USE_NANOSLEEP - struct timespec ts; - - EV_TS_SET (ts, delay); - nanosleep (&ts, 0); -#elif defined _WIN32 - Sleep ((unsigned long)(delay * 1e3)); -#else - struct timeval tv; - - /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ - /* something not guaranteed by newer posix versions, but guaranteed */ - /* by older ones */ - EV_TV_SET (tv, delay); - select (0, 0, 0, 0, &tv); -#endif - } -} - -/*****************************************************************************/ - -#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ - -/* find a suitable new size for the given array, */ -/* hopefully by rounding to a nice-to-malloc size */ -inline_size int -array_nextsize (int elem, int cur, int cnt) -{ - int ncur = cur + 1; - - do - ncur <<= 1; - while (cnt > ncur); - - /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */ - if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) - { - ncur *= elem; - ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); - ncur = ncur - sizeof (void *) * 4; - ncur /= elem; - } - - return ncur; -} - -noinline ecb_cold -static void * -array_realloc (int elem, void *base, int *cur, int cnt) -{ - *cur = array_nextsize (elem, *cur, cnt); - return ev_realloc (base, elem * *cur); -} - -#define array_init_zero(base,count) \ - memset ((void *)(base), 0, sizeof (*(base)) * (count)) - -#define array_needsize(type,base,cur,cnt,init) \ - if (expect_false ((cnt) > (cur))) \ - { \ - ecb_unused int ocur_ = (cur); \ - (base) = (type *)array_realloc \ - (sizeof (type), (base), &(cur), (cnt)); \ - init ((base) + (ocur_), (cur) - ocur_); \ - } - -#if 0 -#define array_slim(type,stem) \ - if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ - { \ - stem ## max = array_roundsize (stem ## cnt >> 1); \ - base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ - fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ - } -#endif - -#define array_free(stem, idx) \ - ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 - -/*****************************************************************************/ - -/* dummy callback for pending events */ -noinline -static void -pendingcb (EV_P_ ev_prepare *w, int revents) -{ -} - -noinline -void -ev_feed_event (EV_P_ void *w, int revents) EV_THROW -{ - W w_ = (W)w; - int pri = ABSPRI (w_); - - if (expect_false (w_->pending)) - pendings [pri][w_->pending - 1].events |= revents; - else - { - w_->pending = ++pendingcnt [pri]; - array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); - pendings [pri][w_->pending - 1].w = w_; - pendings [pri][w_->pending - 1].events = revents; - } - - pendingpri = NUMPRI - 1; -} - -inline_speed void -feed_reverse (EV_P_ W w) -{ - array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2); - rfeeds [rfeedcnt++] = w; -} - -inline_size void -feed_reverse_done (EV_P_ int revents) -{ - do - ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents); - while (rfeedcnt); -} - -inline_speed void -queue_events (EV_P_ W *events, int eventcnt, int type) -{ - int i; - - for (i = 0; i < eventcnt; ++i) - ev_feed_event (EV_A_ events [i], type); -} - -/*****************************************************************************/ - -inline_speed void -fd_event_nocheck (EV_P_ int fd, int revents) -{ - ANFD *anfd = anfds + fd; - ev_io *w; - - for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) - { - int ev = w->events & revents; - - if (ev) - ev_feed_event (EV_A_ (W)w, ev); - } -} - -/* do not submit kernel events for fds that have reify set */ -/* because that means they changed while we were polling for new events */ -inline_speed void -fd_event (EV_P_ int fd, int revents) -{ - ANFD *anfd = anfds + fd; - - if (expect_true (!anfd->reify)) - fd_event_nocheck (EV_A_ fd, revents); -} - -void -ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW -{ - if (fd >= 0 && fd < anfdmax) - fd_event_nocheck (EV_A_ fd, revents); -} - -/* make sure the external fd watch events are in-sync */ -/* with the kernel/libev internal state */ -inline_size void -fd_reify (EV_P) -{ - int i; - -#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP - for (i = 0; i < fdchangecnt; ++i) - { - int fd = fdchanges [i]; - ANFD *anfd = anfds + fd; - - if (anfd->reify & EV__IOFDSET && anfd->head) - { - SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd); - - if (handle != anfd->handle) - { - unsigned long arg; - - assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0)); - - /* handle changed, but fd didn't - we need to do it in two steps */ - backend_modify (EV_A_ fd, anfd->events, 0); - anfd->events = 0; - anfd->handle = handle; - } - } - } -#endif - - for (i = 0; i < fdchangecnt; ++i) - { - int fd = fdchanges [i]; - ANFD *anfd = anfds + fd; - ev_io *w; - - unsigned char o_events = anfd->events; - unsigned char o_reify = anfd->reify; - - anfd->reify = 0; - - /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ - { - anfd->events = 0; - - for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) - anfd->events |= (unsigned char)w->events; - - if (o_events != anfd->events) - o_reify = EV__IOFDSET; /* actually |= */ - } - - if (o_reify & EV__IOFDSET) - backend_modify (EV_A_ fd, o_events, anfd->events); - } - - fdchangecnt = 0; -} - -/* something about the given fd changed */ -inline_size -void -fd_change (EV_P_ int fd, int flags) -{ - unsigned char reify = anfds [fd].reify; - anfds [fd].reify |= flags; - - if (expect_true (!reify)) - { - ++fdchangecnt; - array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); - fdchanges [fdchangecnt - 1] = fd; - } -} - -/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ -inline_speed ecb_cold void -fd_kill (EV_P_ int fd) -{ - ev_io *w; - - while ((w = (ev_io *)anfds [fd].head)) - { - ev_io_stop (EV_A_ w); - ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); - } -} - -/* check whether the given fd is actually valid, for error recovery */ -inline_size ecb_cold int -fd_valid (int fd) -{ -#ifdef _WIN32 - return EV_FD_TO_WIN32_HANDLE (fd) != -1; -#else - return fcntl (fd, F_GETFD) != -1; -#endif -} - -/* called on EBADF to verify fds */ -noinline ecb_cold -static void -fd_ebadf (EV_P) -{ - int fd; - - for (fd = 0; fd < anfdmax; ++fd) - if (anfds [fd].events) - if (!fd_valid (fd) && errno == EBADF) - fd_kill (EV_A_ fd); -} - -/* called on ENOMEM in select/poll to kill some fds and retry */ -noinline ecb_cold -static void -fd_enomem (EV_P) -{ - int fd; - - for (fd = anfdmax; fd--; ) - if (anfds [fd].events) - { - fd_kill (EV_A_ fd); - break; - } -} - -/* usually called after fork if backend needs to re-arm all fds from scratch */ -noinline -static void -fd_rearm_all (EV_P) -{ - int fd; - - for (fd = 0; fd < anfdmax; ++fd) - if (anfds [fd].events) - { - anfds [fd].events = 0; - anfds [fd].emask = 0; - fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); - } -} - -/* used to prepare libev internal fd's */ -/* this is not fork-safe */ -inline_speed void -fd_intern (int fd) -{ -#ifdef _WIN32 - unsigned long arg = 1; - ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg); -#else - fcntl (fd, F_SETFD, FD_CLOEXEC); - fcntl (fd, F_SETFL, O_NONBLOCK); -#endif -} - -/*****************************************************************************/ - -/* - * the heap functions want a real array index. array index 0 is guaranteed to not - * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives - * the branching factor of the d-tree. - */ - -/* - * at the moment we allow libev the luxury of two heaps, - * a small-code-size 2-heap one and a ~1.5kb larger 4-heap - * which is more cache-efficient. - * the difference is about 5% with 50000+ watchers. - */ -#if EV_USE_4HEAP - -#define DHEAP 4 -#define HEAP0 (DHEAP - 1) /* index of first element in heap */ -#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0) -#define UPHEAP_DONE(p,k) ((p) == (k)) - -/* away from the root */ -inline_speed void -downheap (ANHE *heap, int N, int k) -{ - ANHE he = heap [k]; - ANHE *E = heap + N + HEAP0; - - for (;;) - { - ev_tstamp minat; - ANHE *minpos; - ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1; - - /* find minimum child */ - if (expect_true (pos + DHEAP - 1 < E)) - { - /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); - if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); - if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); - if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); - } - else if (pos < E) - { - /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); - if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); - if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); - if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); - } - else - break; - - if (ANHE_at (he) <= minat) - break; - - heap [k] = *minpos; - ev_active (ANHE_w (*minpos)) = k; - - k = minpos - heap; - } - - heap [k] = he; - ev_active (ANHE_w (he)) = k; -} - -#else /* 4HEAP */ - -#define HEAP0 1 -#define HPARENT(k) ((k) >> 1) -#define UPHEAP_DONE(p,k) (!(p)) - -/* away from the root */ -inline_speed void -downheap (ANHE *heap, int N, int k) -{ - ANHE he = heap [k]; - - for (;;) - { - int c = k << 1; - - if (c >= N + HEAP0) - break; - - c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) - ? 1 : 0; - - if (ANHE_at (he) <= ANHE_at (heap [c])) - break; - - heap [k] = heap [c]; - ev_active (ANHE_w (heap [k])) = k; - - k = c; - } - - heap [k] = he; - ev_active (ANHE_w (he)) = k; -} -#endif - -/* towards the root */ -inline_speed void -upheap (ANHE *heap, int k) -{ - ANHE he = heap [k]; - - for (;;) - { - int p = HPARENT (k); - - if (UPHEAP_DONE (p, k) || ANHE_at (heap [p]) <= ANHE_at (he)) - break; - - heap [k] = heap [p]; - ev_active (ANHE_w (heap [k])) = k; - k = p; - } - - heap [k] = he; - ev_active (ANHE_w (he)) = k; -} - -/* move an element suitably so it is in a correct place */ -inline_size void -adjustheap (ANHE *heap, int N, int k) -{ - if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)])) - upheap (heap, k); - else - downheap (heap, N, k); -} - -/* rebuild the heap: this function is used only once and executed rarely */ -inline_size void -reheap (ANHE *heap, int N) -{ - int i; - - /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ - /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */ - for (i = 0; i < N; ++i) - upheap (heap, i + HEAP0); -} - -/*****************************************************************************/ - -/* associate signal watchers to a signal signal */ -typedef struct -{ - EV_ATOMIC_T pending; -#if EV_MULTIPLICITY - EV_P; -#endif - WL head; -} ANSIG; - -static ANSIG signals [EV_NSIG - 1]; - -/*****************************************************************************/ - -#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE - -noinline ecb_cold -static void -evpipe_init (EV_P) -{ - if (!ev_is_active (&pipe_w)) - { - int fds [2]; - -# if EV_USE_EVENTFD - fds [0] = -1; - fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); - if (fds [1] < 0 && errno == EINVAL) - fds [1] = eventfd (0, 0); - - if (fds [1] < 0) -# endif - { - while (pipe (fds)) - ev_syserr ("(libev) error creating signal/async pipe"); - - fd_intern (fds [0]); - } - - evpipe [0] = fds [0]; - - if (evpipe [1] < 0) - evpipe [1] = fds [1]; /* first call, set write fd */ - else - { - /* on subsequent calls, do not change evpipe [1] */ - /* so that evpipe_write can always rely on its value. */ - /* this branch does not do anything sensible on windows, */ - /* so must not be executed on windows */ - - dup2 (fds [1], evpipe [1]); - close (fds [1]); - } - - fd_intern (evpipe [1]); - - ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ); - ev_io_start (EV_A_ &pipe_w); - ev_unref (EV_A); /* watcher should not keep loop alive */ - } -} - -inline_speed void -evpipe_write (EV_P_ EV_ATOMIC_T *flag) -{ - ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */ - - if (expect_true (*flag)) - return; - - *flag = 1; - ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ - - pipe_write_skipped = 1; - - ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ - - if (pipe_write_wanted) - { - int old_errno; - - pipe_write_skipped = 0; - ECB_MEMORY_FENCE_RELEASE; - - old_errno = errno; /* save errno because write will clobber it */ - -#if EV_USE_EVENTFD - if (evpipe [0] < 0) - { - uint64_t counter = 1; - write (evpipe [1], &counter, sizeof (uint64_t)); - } - else -#endif - { -#ifdef _WIN32 - WSABUF buf; - DWORD sent; -// buf.buf=&buf; - buf.buf = (char *)&buf; - buf.len = 1; - WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0); -#else - write (evpipe [1], &(evpipe [1]), 1); -#endif - } - - errno = old_errno; - } -} - -/* called whenever the libev signal pipe */ -/* got some events (signal, async) */ -static void -pipecb (EV_P_ ev_io *iow, int revents) -{ - int i; - - if (revents & EV_READ) - { -#if EV_USE_EVENTFD - if (evpipe [0] < 0) - { - uint64_t counter; - read (evpipe [1], &counter, sizeof (uint64_t)); - } - else -#endif - { - char dummy[4]; -#ifdef _WIN32 - WSABUF buf; - DWORD recvd; - DWORD flags = 0; - buf.buf = dummy; - buf.len = sizeof (dummy); - WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0); -#else - read (evpipe [0], &dummy, sizeof (dummy)); -#endif - } - } - - pipe_write_skipped = 0; - - ECB_MEMORY_FENCE; /* push out skipped, acquire flags */ - -#if EV_SIGNAL_ENABLE - if (sig_pending) - { - sig_pending = 0; - - ECB_MEMORY_FENCE; - - for (i = EV_NSIG - 1; i--; ) - if (expect_false (signals [i].pending)) - ev_feed_signal_event (EV_A_ i + 1); - } -#endif - -#if EV_ASYNC_ENABLE - if (async_pending) - { - async_pending = 0; - - ECB_MEMORY_FENCE; - - for (i = asynccnt; i--; ) - if (asyncs [i]->sent) - { - asyncs [i]->sent = 0; - ECB_MEMORY_FENCE_RELEASE; - ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); - } - } -#endif -} - -/*****************************************************************************/ - -void -ev_feed_signal (int signum) EV_THROW -{ -#if EV_MULTIPLICITY - EV_P; - ECB_MEMORY_FENCE_ACQUIRE; - EV_A = signals [signum - 1].loop; - - if (!EV_A) - return; -#endif - - signals [signum - 1].pending = 1; - evpipe_write (EV_A_ &sig_pending); -} - -static void -ev_sighandler (int signum) -{ -#ifdef _WIN32 - signal (signum, ev_sighandler); -#endif - - ev_feed_signal (signum); -} - -noinline -void -ev_feed_signal_event (EV_P_ int signum) EV_THROW -{ - WL w; - - if (expect_false (signum <= 0 || signum >= EV_NSIG)) - return; - - --signum; - -#if EV_MULTIPLICITY - /* it is permissible to try to feed a signal to the wrong loop */ - /* or, likely more useful, feeding a signal nobody is waiting for */ - - if (expect_false (signals [signum].loop != EV_A)) - return; -#endif - - signals [signum].pending = 0; - ECB_MEMORY_FENCE_RELEASE; - - for (w = signals [signum].head; w; w = w->next) - ev_feed_event (EV_A_ (W)w, EV_SIGNAL); -} - -#if EV_USE_SIGNALFD -static void -sigfdcb (EV_P_ ev_io *iow, int revents) -{ - struct signalfd_siginfo si[2], *sip; /* these structs are big */ - - for (;;) - { - ssize_t res = read (sigfd, si, sizeof (si)); - - /* not ISO-C, as res might be -1, but works with SuS */ - for (sip = si; (char *)sip < (char *)si + res; ++sip) - ev_feed_signal_event (EV_A_ sip->ssi_signo); - - if (res < (ssize_t)sizeof (si)) - break; - } -} -#endif - -#endif - -/*****************************************************************************/ - -#if EV_CHILD_ENABLE -static WL childs [EV_PID_HASHSIZE]; - -static ev_signal childev; - -#ifndef WIFCONTINUED -# define WIFCONTINUED(status) 0 -#endif - -/* handle a single child status event */ -inline_speed void -child_reap (EV_P_ int chain, int pid, int status) -{ - ev_child *w; - int traced = WIFSTOPPED (status) || WIFCONTINUED (status); - - for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next) - { - if ((w->pid == pid || !w->pid) - && (!traced || (w->flags & 1))) - { - ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */ - w->rpid = pid; - w->rstatus = status; - ev_feed_event (EV_A_ (W)w, EV_CHILD); - } - } -} - -#ifndef WCONTINUED -# define WCONTINUED 0 -#endif - -/* called on sigchld etc., calls waitpid */ -static void -childcb (EV_P_ ev_signal *sw, int revents) -{ - int pid, status; - - /* some systems define WCONTINUED but then fail to support it (linux 2.4) */ - if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) - if (!WCONTINUED - || errno != EINVAL - || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) - return; - - /* make sure we are called again until all children have been reaped */ - /* we need to do it this way so that the callback gets called before we continue */ - ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); - - child_reap (EV_A_ pid, pid, status); - if ((EV_PID_HASHSIZE) > 1) - child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ -} - -#endif - -/*****************************************************************************/ - -#if EV_USE_IOCP -# include "ev_iocp.c" -#endif -#if EV_USE_PORT -# include "ev_port.c" -#endif -#if EV_USE_KQUEUE -# include "ev_kqueue.c" -#endif -#if EV_USE_EPOLL -# include "ev_epoll.c" -#endif -#if EV_USE_POLL -# include "ev_poll.c" -#endif -#if EV_USE_SELECT -# include "ev_select.c" -#endif - -ecb_cold int -ev_version_major (void) EV_THROW -{ - return EV_VERSION_MAJOR; -} - -ecb_cold int -ev_version_minor (void) EV_THROW -{ - return EV_VERSION_MINOR; -} - -/* return true if we are running with elevated privileges and should ignore env variables */ -inline_size ecb_cold int -enable_secure (void) -{ -#ifdef _WIN32 - return 0; -#else - return getuid () != geteuid () - || getgid () != getegid (); -#endif -} - -ecb_cold -unsigned int -ev_supported_backends (void) EV_THROW -{ - unsigned int flags = 0; - - if (EV_USE_PORT ) flags |= EVBACKEND_PORT; - if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; - if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; - if (EV_USE_POLL ) flags |= EVBACKEND_POLL; - if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; - - return flags; -} - -ecb_cold -unsigned int -ev_recommended_backends (void) EV_THROW -{ - unsigned int flags = ev_supported_backends (); - -#ifndef __NetBSD__ - /* kqueue is borked on everything but netbsd apparently */ - /* it usually doesn't work correctly on anything but sockets and pipes */ - flags &= ~EVBACKEND_KQUEUE; -#endif -#ifdef __APPLE__ - /* only select works correctly on that "unix-certified" platform */ - flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */ - flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */ -#endif -#ifdef __FreeBSD__ - flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */ -#endif - - return flags; -} - -ecb_cold -unsigned int -ev_embeddable_backends (void) EV_THROW -{ - int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; - - /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ - if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ - flags &= ~EVBACKEND_EPOLL; - - return flags; -} - -unsigned int -ev_backend (EV_P) EV_THROW -{ - return backend; -} - -#if EV_FEATURE_API -unsigned int -ev_iteration (EV_P) EV_THROW -{ - return loop_count; -} - -unsigned int -ev_depth (EV_P) EV_THROW -{ - return loop_depth; -} - -void -ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW -{ - io_blocktime = interval; -} - -void -ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW -{ - timeout_blocktime = interval; -} - -void -ev_set_userdata (EV_P_ void *data) EV_THROW -{ - userdata = data; -} - -void * -ev_userdata (EV_P) EV_THROW -{ - return userdata; -} - -void -ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_THROW -{ - invoke_cb = invoke_pending_cb; -} - -void -ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW -{ - release_cb = release; - acquire_cb = acquire; -} -#endif - -/* initialise a loop structure, must be zero-initialised */ -noinline ecb_cold -static void -loop_init (EV_P_ unsigned int flags) EV_THROW -{ - if (!backend) - { - origflags = flags; - -#if EV_USE_REALTIME - if (!have_realtime) - { - struct timespec ts; - - if (!clock_gettime (CLOCK_REALTIME, &ts)) - have_realtime = 1; - } -#endif - -#if EV_USE_MONOTONIC - if (!have_monotonic) - { - struct timespec ts; - - if (!clock_gettime (CLOCK_MONOTONIC, &ts)) - have_monotonic = 1; - } -#endif - - /* pid check not overridable via env */ -#ifndef _WIN32 - if (flags & EVFLAG_FORKCHECK) - curpid = getpid (); -#endif - - if (!(flags & EVFLAG_NOENV) - && !enable_secure () - && getenv ("LIBEV_FLAGS")) - flags = atoi (getenv ("LIBEV_FLAGS")); - - ev_rt_now = ev_time (); - mn_now = get_clock (); - now_floor = mn_now; - rtmn_diff = ev_rt_now - mn_now; -#if EV_FEATURE_API - invoke_cb = ev_invoke_pending; -#endif - - io_blocktime = 0.; - timeout_blocktime = 0.; - backend = 0; - backend_fd = -1; - sig_pending = 0; -#if EV_ASYNC_ENABLE - async_pending = 0; -#endif - pipe_write_skipped = 0; - pipe_write_wanted = 0; - evpipe [0] = -1; - evpipe [1] = -1; -#if EV_USE_INOTIFY - fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; -#endif -#if EV_USE_SIGNALFD - sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; -#endif - - if (!(flags & EVBACKEND_MASK)) - flags |= ev_recommended_backends (); - -#if EV_USE_IOCP - if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); -#endif -#if EV_USE_PORT - if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); -#endif -#if EV_USE_KQUEUE - if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); -#endif -#if EV_USE_EPOLL - if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); -#endif -#if EV_USE_POLL - if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); -#endif -#if EV_USE_SELECT - if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); -#endif - - ev_prepare_init (&pending_w, pendingcb); - -#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE - ev_init (&pipe_w, pipecb); - ev_set_priority (&pipe_w, EV_MAXPRI); -#endif - } -} - -/* free up a loop structure */ -ecb_cold -void -ev_loop_destroy (EV_P) -{ - int i; - -#if EV_MULTIPLICITY - /* mimic free (0) */ - if (!EV_A) - return; -#endif - -#if EV_CLEANUP_ENABLE - /* queue cleanup watchers (and execute them) */ - if (expect_false (cleanupcnt)) - { - queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP); - EV_INVOKE_PENDING; - } -#endif - -#if EV_CHILD_ENABLE - if (ev_is_default_loop (EV_A) && ev_is_active (&childev)) - { - ev_ref (EV_A); /* child watcher */ - ev_signal_stop (EV_A_ &childev); - } -#endif - - if (ev_is_active (&pipe_w)) - { - /*ev_ref (EV_A);*/ - /*ev_io_stop (EV_A_ &pipe_w);*/ - - if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]); - if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]); - } - -#if EV_USE_SIGNALFD - if (ev_is_active (&sigfd_w)) - close (sigfd); -#endif - -#if EV_USE_INOTIFY - if (fs_fd >= 0) - close (fs_fd); -#endif - - if (backend_fd >= 0) - close (backend_fd); - -#if EV_USE_IOCP - if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A); -#endif -#if EV_USE_PORT - if (backend == EVBACKEND_PORT ) port_destroy (EV_A); -#endif -#if EV_USE_KQUEUE - if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); -#endif -#if EV_USE_EPOLL - if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); -#endif -#if EV_USE_POLL - if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); -#endif -#if EV_USE_SELECT - if (backend == EVBACKEND_SELECT) select_destroy (EV_A); -#endif - - for (i = NUMPRI; i--; ) - { - array_free (pending, [i]); -#if EV_IDLE_ENABLE - array_free (idle, [i]); -#endif - } - - ev_free (anfds); anfds = 0; anfdmax = 0; - - /* have to use the microsoft-never-gets-it-right macro */ - array_free (rfeed, EMPTY); - array_free (fdchange, EMPTY); - array_free (timer, EMPTY); -#if EV_PERIODIC_ENABLE - array_free (periodic, EMPTY); -#endif -#if EV_FORK_ENABLE - array_free (fork, EMPTY); -#endif -#if EV_CLEANUP_ENABLE - array_free (cleanup, EMPTY); -#endif - array_free (prepare, EMPTY); - array_free (check, EMPTY); -#if EV_ASYNC_ENABLE - array_free (async, EMPTY); -#endif - - backend = 0; - -#if EV_MULTIPLICITY - if (ev_is_default_loop (EV_A)) -#endif - ev_default_loop_ptr = 0; -#if EV_MULTIPLICITY - else - ev_free (EV_A); -#endif -} - -#if EV_USE_INOTIFY -inline_size void infy_fork (EV_P); -#endif - -inline_size void -loop_fork (EV_P) -{ -#if EV_USE_PORT - if (backend == EVBACKEND_PORT ) port_fork (EV_A); -#endif -#if EV_USE_KQUEUE - if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); -#endif -#if EV_USE_EPOLL - if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); -#endif -#if EV_USE_INOTIFY - infy_fork (EV_A); -#endif - -#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE - if (ev_is_active (&pipe_w) && postfork != 2) - { - /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ - - ev_ref (EV_A); - ev_io_stop (EV_A_ &pipe_w); - - if (evpipe [0] >= 0) - EV_WIN32_CLOSE_FD (evpipe [0]); - - evpipe_init (EV_A); - /* iterate over everything, in case we missed something before */ - ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); - } -#endif - - postfork = 0; -} - -#if EV_MULTIPLICITY - -ecb_cold -struct ev_loop * -ev_loop_new (unsigned int flags) EV_THROW -{ - EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); - - memset (EV_A, 0, sizeof (struct ev_loop)); - loop_init (EV_A_ flags); - - if (ev_backend (EV_A)) - return EV_A; - - ev_free (EV_A); - return 0; -} - -#endif /* multiplicity */ - -#if EV_VERIFY -noinline ecb_cold -static void -verify_watcher (EV_P_ W w) -{ - assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); - - if (w->pending) - assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); -} - -noinline ecb_cold -static void -verify_heap (EV_P_ ANHE *heap, int N) -{ - int i; - - for (i = HEAP0; i < N + HEAP0; ++i) - { - assert (("libev: active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i)); - assert (("libev: heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i]))); - assert (("libev: heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i])))); - - verify_watcher (EV_A_ (W)ANHE_w (heap [i])); - } -} - -noinline ecb_cold -static void -array_verify (EV_P_ W *ws, int cnt) -{ - while (cnt--) - { - assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); - verify_watcher (EV_A_ ws [cnt]); - } -} -#endif - -#if EV_FEATURE_API -void ecb_cold -ev_verify (EV_P) EV_THROW -{ -#if EV_VERIFY - int i; - WL w, w2; - - assert (activecnt >= -1); - - assert (fdchangemax >= fdchangecnt); - for (i = 0; i < fdchangecnt; ++i) - assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); - - assert (anfdmax >= 0); - for (i = 0; i < anfdmax; ++i) - { - int j = 0; - - for (w = w2 = anfds [i].head; w; w = w->next) - { - verify_watcher (EV_A_ (W)w); - - if (j++ & 1) - { - assert (("libev: io watcher list contains a loop", w != w2)); - w2 = w2->next; - } - - assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); - assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); - } - } - - assert (timermax >= timercnt); - verify_heap (EV_A_ timers, timercnt); - -#if EV_PERIODIC_ENABLE - assert (periodicmax >= periodiccnt); - verify_heap (EV_A_ periodics, periodiccnt); -#endif - - for (i = NUMPRI; i--; ) - { - assert (pendingmax [i] >= pendingcnt [i]); -#if EV_IDLE_ENABLE - assert (idleall >= 0); - assert (idlemax [i] >= idlecnt [i]); - array_verify (EV_A_ (W *)idles [i], idlecnt [i]); -#endif - } - -#if EV_FORK_ENABLE - assert (forkmax >= forkcnt); - array_verify (EV_A_ (W *)forks, forkcnt); -#endif - -#if EV_CLEANUP_ENABLE - assert (cleanupmax >= cleanupcnt); - array_verify (EV_A_ (W *)cleanups, cleanupcnt); -#endif - -#if EV_ASYNC_ENABLE - assert (asyncmax >= asynccnt); - array_verify (EV_A_ (W *)asyncs, asynccnt); -#endif - -#if EV_PREPARE_ENABLE - assert (preparemax >= preparecnt); - array_verify (EV_A_ (W *)prepares, preparecnt); -#endif - -#if EV_CHECK_ENABLE - assert (checkmax >= checkcnt); - array_verify (EV_A_ (W *)checks, checkcnt); -#endif - -# if 0 -#if EV_CHILD_ENABLE - for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next) - for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) -#endif -# endif -#endif -} -#endif - -#if EV_MULTIPLICITY -ecb_cold -struct ev_loop * -#else -int -#endif -ev_default_loop (unsigned int flags) EV_THROW -{ - if (!ev_default_loop_ptr) - { -#if EV_MULTIPLICITY - EV_P = ev_default_loop_ptr = &default_loop_struct; -#else - ev_default_loop_ptr = 1; -#endif - - loop_init (EV_A_ flags); - - if (ev_backend (EV_A)) - { -#if EV_CHILD_ENABLE - ev_signal_init (&childev, childcb, SIGCHLD); - ev_set_priority (&childev, EV_MAXPRI); - ev_signal_start (EV_A_ &childev); - ev_unref (EV_A); /* child watcher should not keep loop alive */ -#endif - } - else - ev_default_loop_ptr = 0; - } - - return ev_default_loop_ptr; -} - -void -ev_loop_fork (EV_P) EV_THROW -{ - postfork = 1; -} - -/*****************************************************************************/ - -void -ev_invoke (EV_P_ void *w, int revents) -{ - EV_CB_INVOKE ((W)w, revents); -} - -unsigned int -ev_pending_count (EV_P) EV_THROW -{ - int pri; - unsigned int count = 0; - - for (pri = NUMPRI; pri--; ) - count += pendingcnt [pri]; - - return count; -} - -noinline -void -ev_invoke_pending (EV_P) -{ - pendingpri = NUMPRI; - - while (pendingpri) /* pendingpri possibly gets modified in the inner loop */ - { - --pendingpri; - - while (pendingcnt [pendingpri]) - { - ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; - - p->w->pending = 0; - EV_CB_INVOKE (p->w, p->events); - EV_FREQUENT_CHECK; - } - } -} - -#if EV_IDLE_ENABLE -/* make idle watchers pending. this handles the "call-idle */ -/* only when higher priorities are idle" logic */ -inline_size void -idle_reify (EV_P) -{ - if (expect_false (idleall)) - { - int pri; - - for (pri = NUMPRI; pri--; ) - { - if (pendingcnt [pri]) - break; - - if (idlecnt [pri]) - { - queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); - break; - } - } - } -} -#endif - -/* make timers pending */ -inline_size void -timers_reify (EV_P) -{ - EV_FREQUENT_CHECK; - - if (timercnt && ANHE_at (timers [HEAP0]) < mn_now) - { - do - { - ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); - - /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/ - - /* first reschedule or stop timer */ - if (w->repeat) - { - ev_at (w) += w->repeat; - if (ev_at (w) < mn_now) - ev_at (w) = mn_now; - - assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); - - ANHE_at_cache (timers [HEAP0]); - downheap (timers, timercnt, HEAP0); - } - else - ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ - - EV_FREQUENT_CHECK; - feed_reverse (EV_A_ (W)w); - } - while (timercnt && ANHE_at (timers [HEAP0]) < mn_now); - - feed_reverse_done (EV_A_ EV_TIMER); - } -} - -#if EV_PERIODIC_ENABLE - -noinline -static void -periodic_recalc (EV_P_ ev_periodic *w) -{ - ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL; - ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval); - - /* the above almost always errs on the low side */ - while (at <= ev_rt_now) - { - ev_tstamp nat = at + w->interval; - - /* when resolution fails us, we use ev_rt_now */ - if (expect_false (nat == at)) - { - at = ev_rt_now; - break; - } - - at = nat; - } - - ev_at (w) = at; -} - -/* make periodics pending */ -inline_size void -periodics_reify (EV_P) -{ - EV_FREQUENT_CHECK; - - while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) - { - do - { - ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); - - /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ - - /* first reschedule or stop timer */ - if (w->reschedule_cb) - { - ev_at (w) = w->reschedule_cb (w, ev_rt_now); - - assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); - - ANHE_at_cache (periodics [HEAP0]); - downheap (periodics, periodiccnt, HEAP0); - } - else if (w->interval) - { - periodic_recalc (EV_A_ w); - ANHE_at_cache (periodics [HEAP0]); - downheap (periodics, periodiccnt, HEAP0); - } - else - ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ - - EV_FREQUENT_CHECK; - feed_reverse (EV_A_ (W)w); - } - while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now); - - feed_reverse_done (EV_A_ EV_PERIODIC); - } -} - -/* simply recalculate all periodics */ -/* TODO: maybe ensure that at least one event happens when jumping forward? */ -noinline ecb_cold -static void -periodics_reschedule (EV_P) -{ - int i; - - /* adjust periodics after time jump */ - for (i = HEAP0; i < periodiccnt + HEAP0; ++i) - { - ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); - - if (w->reschedule_cb) - ev_at (w) = w->reschedule_cb (w, ev_rt_now); - else if (w->interval) - periodic_recalc (EV_A_ w); - - ANHE_at_cache (periodics [i]); - } - - reheap (periodics, periodiccnt); -} -#endif - -/* adjust all timers by a given offset */ -noinline ecb_cold -static void -timers_reschedule (EV_P_ ev_tstamp adjust) -{ - int i; - - for (i = 0; i < timercnt; ++i) - { - ANHE *he = timers + i + HEAP0; - ANHE_w (*he)->at += adjust; - ANHE_at_cache (*he); - } -} - -/* fetch new monotonic and realtime times from the kernel */ -/* also detect if there was a timejump, and act accordingly */ -inline_speed void -time_update (EV_P_ ev_tstamp max_block) -{ -#if EV_USE_MONOTONIC - if (expect_true (have_monotonic)) - { - int i; - ev_tstamp odiff = rtmn_diff; - - mn_now = get_clock (); - - /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ - /* interpolate in the meantime */ - if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) - { - ev_rt_now = rtmn_diff + mn_now; - return; - } - - now_floor = mn_now; - ev_rt_now = ev_time (); - - /* loop a few times, before making important decisions. - * on the choice of "4": one iteration isn't enough, - * in case we get preempted during the calls to - * ev_time and get_clock. a second call is almost guaranteed - * to succeed in that case, though. and looping a few more times - * doesn't hurt either as we only do this on time-jumps or - * in the unlikely event of having been preempted here. - */ - for (i = 4; --i; ) - { - ev_tstamp diff; - rtmn_diff = ev_rt_now - mn_now; - - diff = odiff - rtmn_diff; - - if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) - return; /* all is well */ - - ev_rt_now = ev_time (); - mn_now = get_clock (); - now_floor = mn_now; - } - - /* no timer adjustment, as the monotonic clock doesn't jump */ - /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ -# if EV_PERIODIC_ENABLE - periodics_reschedule (EV_A); -# endif - } - else -#endif - { - ev_rt_now = ev_time (); - - if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) - { - /* adjust timers. this is easy, as the offset is the same for all of them */ - timers_reschedule (EV_A_ ev_rt_now - mn_now); -#if EV_PERIODIC_ENABLE - periodics_reschedule (EV_A); -#endif - } - - mn_now = ev_rt_now; - } -} - -int -ev_run (EV_P_ int flags) -{ -#if EV_FEATURE_API - ++loop_depth; -#endif - - assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE)); - - loop_done = EVBREAK_CANCEL; - - EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ - - do - { -#if EV_VERIFY >= 2 - ev_verify (EV_A); -#endif - -#ifndef _WIN32 - if (expect_false (curpid)) /* penalise the forking check even more */ - if (expect_false (getpid () != curpid)) - { - curpid = getpid (); - postfork = 1; - } -#endif - -#if EV_FORK_ENABLE - /* we might have forked, so queue fork handlers */ - if (expect_false (postfork)) - if (forkcnt) - { - queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); - EV_INVOKE_PENDING; - } -#endif - -#if EV_PREPARE_ENABLE - /* queue prepare watchers (and execute them) */ - if (expect_false (preparecnt)) - { - queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); - EV_INVOKE_PENDING; - } -#endif - - if (expect_false (loop_done)) - break; - - /* we might have forked, so reify kernel state if necessary */ - if (expect_false (postfork)) - loop_fork (EV_A); - - /* update fd-related kernel structures */ - fd_reify (EV_A); - - /* calculate blocking time */ - { - ev_tstamp waittime = 0.; - ev_tstamp sleeptime = 0.; - - /* remember old timestamp for io_blocktime calculation */ - ev_tstamp prev_mn_now = mn_now; - - /* update time to cancel out callback processing overhead */ - time_update (EV_A_ 1e100); - - /* from now on, we want a pipe-wake-up */ - pipe_write_wanted = 1; - - ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ - - if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) - { - waittime = MAX_BLOCKTIME; - - if (timercnt) - { - ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; - if (waittime > to) waittime = to; - } - -#if EV_PERIODIC_ENABLE - if (periodiccnt) - { - ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; - if (waittime > to) waittime = to; - } -#endif - - /* don't let timeouts decrease the waittime below timeout_blocktime */ - if (expect_false (waittime < timeout_blocktime)) - waittime = timeout_blocktime; - - /* at this point, we NEED to wait, so we have to ensure */ - /* to pass a minimum nonzero value to the backend */ - if (expect_false (waittime < backend_mintime)) - waittime = backend_mintime; - - /* extra check because io_blocktime is commonly 0 */ - if (expect_false (io_blocktime)) - { - sleeptime = io_blocktime - (mn_now - prev_mn_now); - - if (sleeptime > waittime - backend_mintime) - sleeptime = waittime - backend_mintime; - - if (expect_true (sleeptime > 0.)) - { - ev_sleep (sleeptime); - waittime -= sleeptime; - } - } - } - -#if EV_FEATURE_API - ++loop_count; -#endif - assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ - backend_poll (EV_A_ waittime); - assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ - - pipe_write_wanted = 0; /* just an optimisation, no fence needed */ - - ECB_MEMORY_FENCE_ACQUIRE; - if (pipe_write_skipped) - { - assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); - ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); - } - - - /* update ev_rt_now, do magic */ - time_update (EV_A_ waittime + sleeptime); - } - - /* queue pending timers and reschedule them */ - timers_reify (EV_A); /* relative timers called last */ -#if EV_PERIODIC_ENABLE - periodics_reify (EV_A); /* absolute timers called first */ -#endif - -#if EV_IDLE_ENABLE - /* queue idle watchers unless other events are pending */ - idle_reify (EV_A); -#endif - -#if EV_CHECK_ENABLE - /* queue check watchers, to be executed first */ - if (expect_false (checkcnt)) - queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); -#endif - - EV_INVOKE_PENDING; - } - while (expect_true ( - activecnt - && !loop_done - && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT)) - )); - - if (loop_done == EVBREAK_ONE) - loop_done = EVBREAK_CANCEL; - -#if EV_FEATURE_API - --loop_depth; -#endif - - return activecnt; -} - -void -ev_break (EV_P_ int how) EV_THROW -{ - loop_done = how; -} - -void -ev_ref (EV_P) EV_THROW -{ - ++activecnt; -} - -void -ev_unref (EV_P) EV_THROW -{ - --activecnt; -} - -void -ev_now_update (EV_P) EV_THROW -{ - time_update (EV_A_ 1e100); -} - -void -ev_suspend (EV_P) EV_THROW -{ - ev_now_update (EV_A); -} - -void -ev_resume (EV_P) EV_THROW -{ - ev_tstamp mn_prev = mn_now; - - ev_now_update (EV_A); - timers_reschedule (EV_A_ mn_now - mn_prev); -#if EV_PERIODIC_ENABLE - /* TODO: really do this? */ - periodics_reschedule (EV_A); -#endif -} - -/*****************************************************************************/ -/* singly-linked list management, used when the expected list length is short */ - -inline_size void -wlist_add (WL *head, WL elem) -{ - elem->next = *head; - *head = elem; -} - -inline_size void -wlist_del (WL *head, WL elem) -{ - while (*head) - { - if (expect_true (*head == elem)) - { - *head = elem->next; - break; - } - - head = &(*head)->next; - } -} - -/* internal, faster, version of ev_clear_pending */ -inline_speed void -clear_pending (EV_P_ W w) -{ - if (w->pending) - { - pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w; - w->pending = 0; - } -} - -int -ev_clear_pending (EV_P_ void *w) EV_THROW -{ - W w_ = (W)w; - int pending = w_->pending; - - if (expect_true (pending)) - { - ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; - p->w = (W)&pending_w; - w_->pending = 0; - return p->events; - } - else - return 0; -} - -inline_size void -pri_adjust (EV_P_ W w) -{ - int pri = ev_priority (w); - pri = pri < EV_MINPRI ? EV_MINPRI : pri; - pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; - ev_set_priority (w, pri); -} - -inline_speed void -ev_start (EV_P_ W w, int active) -{ - pri_adjust (EV_A_ w); - w->active = active; - ev_ref (EV_A); -} - -inline_size void -ev_stop (EV_P_ W w) -{ - ev_unref (EV_A); - w->active = 0; -} - -/*****************************************************************************/ - -noinline -void -ev_io_start (EV_P_ ev_io *w) EV_THROW -{ - int fd = w->fd; - - if (expect_false (ev_is_active (w))) - return; - - assert (("libev: ev_io_start called with negative fd", fd >= 0)); - assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); - - EV_FREQUENT_CHECK; - - ev_start (EV_A_ (W)w, 1); - array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); - wlist_add (&anfds[fd].head, (WL)w); - - /* common bug, apparently */ - assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w)); - - fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); - w->events &= ~EV__IOFDSET; - - EV_FREQUENT_CHECK; -} - -noinline -void -ev_io_stop (EV_P_ ev_io *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); - - EV_FREQUENT_CHECK; - - wlist_del (&anfds[w->fd].head, (WL)w); - ev_stop (EV_A_ (W)w); - - fd_change (EV_A_ w->fd, EV_ANFD_REIFY); - - EV_FREQUENT_CHECK; -} - -noinline -void -ev_timer_start (EV_P_ ev_timer *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - ev_at (w) += mn_now; - - assert (("libev: ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); - - EV_FREQUENT_CHECK; - - ++timercnt; - ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1); - array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2); - ANHE_w (timers [ev_active (w)]) = (WT)w; - ANHE_at_cache (timers [ev_active (w)]); - upheap (timers, ev_active (w)); - - EV_FREQUENT_CHECK; - - /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ -} - -noinline -void -ev_timer_stop (EV_P_ ev_timer *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - { - int active = ev_active (w); - - assert (("libev: internal timer heap corruption", ANHE_w (timers [active]) == (WT)w)); - - --timercnt; - - if (expect_true (active < timercnt + HEAP0)) - { - timers [active] = timers [timercnt + HEAP0]; - adjustheap (timers, timercnt, active); - } - } - - ev_at (w) -= mn_now; - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} - -noinline -void -ev_timer_again (EV_P_ ev_timer *w) EV_THROW -{ - EV_FREQUENT_CHECK; - - clear_pending (EV_A_ (W)w); - - if (ev_is_active (w)) - { - if (w->repeat) - { - ev_at (w) = mn_now + w->repeat; - ANHE_at_cache (timers [ev_active (w)]); - adjustheap (timers, timercnt, ev_active (w)); - } - else - ev_timer_stop (EV_A_ w); - } - else if (w->repeat) - { - ev_at (w) = w->repeat; - ev_timer_start (EV_A_ w); - } - - EV_FREQUENT_CHECK; -} - -ev_tstamp -ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW -{ - return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); -} - -#if EV_PERIODIC_ENABLE -noinline -void -ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - if (w->reschedule_cb) - ev_at (w) = w->reschedule_cb (w, ev_rt_now); - else if (w->interval) - { - assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); - periodic_recalc (EV_A_ w); - } - else - ev_at (w) = w->offset; - - EV_FREQUENT_CHECK; - - ++periodiccnt; - ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1); - array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2); - ANHE_w (periodics [ev_active (w)]) = (WT)w; - ANHE_at_cache (periodics [ev_active (w)]); - upheap (periodics, ev_active (w)); - - EV_FREQUENT_CHECK; - - /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ -} - -noinline -void -ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - { - int active = ev_active (w); - - assert (("libev: internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w)); - - --periodiccnt; - - if (expect_true (active < periodiccnt + HEAP0)) - { - periodics [active] = periodics [periodiccnt + HEAP0]; - adjustheap (periodics, periodiccnt, active); - } - } - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} - -noinline -void -ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW -{ - /* TODO: use adjustheap and recalculation */ - ev_periodic_stop (EV_A_ w); - ev_periodic_start (EV_A_ w); -} -#endif - -#ifndef SA_RESTART -# define SA_RESTART 0 -#endif - -#if EV_SIGNAL_ENABLE - -noinline -void -ev_signal_start (EV_P_ ev_signal *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); - -#if EV_MULTIPLICITY - assert (("libev: a signal must not be attached to two different loops", - !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); - - signals [w->signum - 1].loop = EV_A; - ECB_MEMORY_FENCE_RELEASE; -#endif - - EV_FREQUENT_CHECK; - -#if EV_USE_SIGNALFD - if (sigfd == -2) - { - sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC); - if (sigfd < 0 && errno == EINVAL) - sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */ - - if (sigfd >= 0) - { - fd_intern (sigfd); /* doing it twice will not hurt */ - - sigemptyset (&sigfd_set); - - ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ); - ev_set_priority (&sigfd_w, EV_MAXPRI); - ev_io_start (EV_A_ &sigfd_w); - ev_unref (EV_A); /* signalfd watcher should not keep loop alive */ - } - } - - if (sigfd >= 0) - { - /* TODO: check .head */ - sigaddset (&sigfd_set, w->signum); - sigprocmask (SIG_BLOCK, &sigfd_set, 0); - - signalfd (sigfd, &sigfd_set, 0); - } -#endif - - ev_start (EV_A_ (W)w, 1); - wlist_add (&signals [w->signum - 1].head, (WL)w); - - if (!((WL)w)->next) -# if EV_USE_SIGNALFD - if (sigfd < 0) /*TODO*/ -# endif - { -# ifdef _WIN32 - evpipe_init (EV_A); - - signal (w->signum, ev_sighandler); -# else - struct sigaction sa; - - evpipe_init (EV_A); - - sa.sa_handler = ev_sighandler; - sigfillset (&sa.sa_mask); - sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ - sigaction (w->signum, &sa, 0); - - if (origflags & EVFLAG_NOSIGMASK) - { - sigemptyset (&sa.sa_mask); - sigaddset (&sa.sa_mask, w->signum); - sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); - } -#endif - } - - EV_FREQUENT_CHECK; -} - -noinline -void -ev_signal_stop (EV_P_ ev_signal *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - wlist_del (&signals [w->signum - 1].head, (WL)w); - ev_stop (EV_A_ (W)w); - - if (!signals [w->signum - 1].head) - { -#if EV_MULTIPLICITY - signals [w->signum - 1].loop = 0; /* unattach from signal */ -#endif -#if EV_USE_SIGNALFD - if (sigfd >= 0) - { - sigset_t ss; - - sigemptyset (&ss); - sigaddset (&ss, w->signum); - sigdelset (&sigfd_set, w->signum); - - signalfd (sigfd, &sigfd_set, 0); - sigprocmask (SIG_UNBLOCK, &ss, 0); - } - else -#endif - signal (w->signum, SIG_DFL); - } - - EV_FREQUENT_CHECK; -} - -#endif - -#if EV_CHILD_ENABLE - -void -ev_child_start (EV_P_ ev_child *w) EV_THROW -{ -#if EV_MULTIPLICITY - assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); -#endif - if (expect_false (ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - ev_start (EV_A_ (W)w, 1); - wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w); - - EV_FREQUENT_CHECK; -} - -void -ev_child_stop (EV_P_ ev_child *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w); - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} - -#endif - -#if EV_STAT_ENABLE - -# ifdef _WIN32 -# undef lstat -# define lstat(a,b) _stati64 (a,b) -# endif - -#define DEF_STAT_INTERVAL 5.0074891 -#define NFS_STAT_INTERVAL 30.1074891 /* for filesystems potentially failing inotify */ -#define MIN_STAT_INTERVAL 0.1074891 - -noinline static void stat_timer_cb (EV_P_ ev_timer *w_, int revents); - -#if EV_USE_INOTIFY - -/* the * 2 is to allow for alignment padding, which for some reason is >> 8 */ -# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) - -noinline -static void -infy_add (EV_P_ ev_stat *w) -{ - w->wd = inotify_add_watch (fs_fd, w->path, - IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY - | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO - | IN_DONT_FOLLOW | IN_MASK_ADD); - - if (w->wd >= 0) - { - struct statfs sfs; - - /* now local changes will be tracked by inotify, but remote changes won't */ - /* unless the filesystem is known to be local, we therefore still poll */ - /* also do poll on <2.6.25, but with normal frequency */ - - if (!fs_2625) - w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; - else if (!statfs (w->path, &sfs) - && (sfs.f_type == 0x1373 /* devfs */ - || sfs.f_type == 0x4006 /* fat */ - || sfs.f_type == 0x4d44 /* msdos */ - || sfs.f_type == 0xEF53 /* ext2/3 */ - || sfs.f_type == 0x72b6 /* jffs2 */ - || sfs.f_type == 0x858458f6 /* ramfs */ - || sfs.f_type == 0x5346544e /* ntfs */ - || sfs.f_type == 0x3153464a /* jfs */ - || sfs.f_type == 0x9123683e /* btrfs */ - || sfs.f_type == 0x52654973 /* reiser3 */ - || sfs.f_type == 0x01021994 /* tmpfs */ - || sfs.f_type == 0x58465342 /* xfs */)) - w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ - else - w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ - } - else - { - /* can't use inotify, continue to stat */ - w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; - - /* if path is not there, monitor some parent directory for speedup hints */ - /* note that exceeding the hardcoded path limit is not a correctness issue, */ - /* but an efficiency issue only */ - if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) - { - char path [4096]; - strcpy (path, w->path); - - do - { - int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF - | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO); - - char *pend = strrchr (path, '/'); - - if (!pend || pend == path) - break; - - *pend = 0; - w->wd = inotify_add_watch (fs_fd, path, mask); - } - while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); - } - } - - if (w->wd >= 0) - wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w); - - /* now re-arm timer, if required */ - if (ev_is_active (&w->timer)) ev_ref (EV_A); - ev_timer_again (EV_A_ &w->timer); - if (ev_is_active (&w->timer)) ev_unref (EV_A); -} - -noinline -static void -infy_del (EV_P_ ev_stat *w) -{ - int slot; - int wd = w->wd; - - if (wd < 0) - return; - - w->wd = -2; - slot = wd & ((EV_INOTIFY_HASHSIZE) - 1); - wlist_del (&fs_hash [slot].head, (WL)w); - - /* remove this watcher, if others are watching it, they will rearm */ - inotify_rm_watch (fs_fd, wd); -} - -noinline -static void -infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) -{ - if (slot < 0) - /* overflow, need to check for all hash slots */ - for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot) - infy_wd (EV_A_ slot, wd, ev); - else - { - WL w_; - - for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; ) - { - ev_stat *w = (ev_stat *)w_; - w_ = w_->next; /* lets us remove this watcher and all before it */ - - if (w->wd == wd || wd == -1) - { - if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF)) - { - wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w); - w->wd = -1; - infy_add (EV_A_ w); /* re-add, no matter what */ - } - - stat_timer_cb (EV_A_ &w->timer, 0); - } - } - } -} - -static void -infy_cb (EV_P_ ev_io *w, int revents) -{ - char buf [EV_INOTIFY_BUFSIZE]; - int ofs; - int len = read (fs_fd, buf, sizeof (buf)); - - for (ofs = 0; ofs < len; ) - { - struct inotify_event *ev = (struct inotify_event *)(buf + ofs); - infy_wd (EV_A_ ev->wd, ev->wd, ev); - ofs += sizeof (struct inotify_event) + ev->len; - } -} - -inline_size ecb_cold -void -ev_check_2625 (EV_P) -{ - /* kernels < 2.6.25 are borked - * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html - */ - if (ev_linux_version () < 0x020619) - return; - - fs_2625 = 1; -} - -inline_size int -infy_newfd (void) -{ -#if defined IN_CLOEXEC && defined IN_NONBLOCK - int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); - if (fd >= 0) - return fd; -#endif - return inotify_init (); -} - -inline_size void -infy_init (EV_P) -{ - if (fs_fd != -2) - return; - - fs_fd = -1; - - ev_check_2625 (EV_A); - - fs_fd = infy_newfd (); - - if (fs_fd >= 0) - { - fd_intern (fs_fd); - ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); - ev_set_priority (&fs_w, EV_MAXPRI); - ev_io_start (EV_A_ &fs_w); - ev_unref (EV_A); - } -} - -inline_size void -infy_fork (EV_P) -{ - int slot; - - if (fs_fd < 0) - return; - - ev_ref (EV_A); - ev_io_stop (EV_A_ &fs_w); - close (fs_fd); - fs_fd = infy_newfd (); - - if (fs_fd >= 0) - { - fd_intern (fs_fd); - ev_io_set (&fs_w, fs_fd, EV_READ); - ev_io_start (EV_A_ &fs_w); - ev_unref (EV_A); - } - - for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot) - { - WL w_ = fs_hash [slot].head; - fs_hash [slot].head = 0; - - while (w_) - { - ev_stat *w = (ev_stat *)w_; - w_ = w_->next; /* lets us add this watcher */ - - w->wd = -1; - - if (fs_fd >= 0) - infy_add (EV_A_ w); /* re-add, no matter what */ - else - { - w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; - if (ev_is_active (&w->timer)) ev_ref (EV_A); - ev_timer_again (EV_A_ &w->timer); - if (ev_is_active (&w->timer)) ev_unref (EV_A); - } - } - } -} - -#endif - -#ifdef _WIN32 -# define EV_LSTAT(p,b) _stati64 (p, b) -#else -# define EV_LSTAT(p,b) lstat (p, b) -#endif - -void -ev_stat_stat (EV_P_ ev_stat *w) EV_THROW -{ - if (lstat (w->path, &w->attr) < 0) - w->attr.st_nlink = 0; - else if (!w->attr.st_nlink) - w->attr.st_nlink = 1; -} - -noinline -static void -stat_timer_cb (EV_P_ ev_timer *w_, int revents) -{ - ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); - - ev_statdata prev = w->attr; - ev_stat_stat (EV_A_ w); - - /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ - if ( - prev.st_dev != w->attr.st_dev - || prev.st_ino != w->attr.st_ino - || prev.st_mode != w->attr.st_mode - || prev.st_nlink != w->attr.st_nlink - || prev.st_uid != w->attr.st_uid - || prev.st_gid != w->attr.st_gid - || prev.st_rdev != w->attr.st_rdev - || prev.st_size != w->attr.st_size - || prev.st_atime != w->attr.st_atime - || prev.st_mtime != w->attr.st_mtime - || prev.st_ctime != w->attr.st_ctime - ) { - /* we only update w->prev on actual differences */ - /* in case we test more often than invoke the callback, */ - /* to ensure that prev is always different to attr */ - w->prev = prev; - - #if EV_USE_INOTIFY - if (fs_fd >= 0) - { - infy_del (EV_A_ w); - infy_add (EV_A_ w); - ev_stat_stat (EV_A_ w); /* avoid race... */ - } - #endif - - ev_feed_event (EV_A_ w, EV_STAT); - } -} - -void -ev_stat_start (EV_P_ ev_stat *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - ev_stat_stat (EV_A_ w); - - if (w->interval < MIN_STAT_INTERVAL && w->interval) - w->interval = MIN_STAT_INTERVAL; - - ev_timer_init (&w->timer, stat_timer_cb, 0., w->interval ? w->interval : DEF_STAT_INTERVAL); - ev_set_priority (&w->timer, ev_priority (w)); - -#if EV_USE_INOTIFY - infy_init (EV_A); - - if (fs_fd >= 0) - infy_add (EV_A_ w); - else -#endif - { - ev_timer_again (EV_A_ &w->timer); - ev_unref (EV_A); - } - - ev_start (EV_A_ (W)w, 1); - - EV_FREQUENT_CHECK; -} - -void -ev_stat_stop (EV_P_ ev_stat *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - -#if EV_USE_INOTIFY - infy_del (EV_A_ w); -#endif - - if (ev_is_active (&w->timer)) - { - ev_ref (EV_A); - ev_timer_stop (EV_A_ &w->timer); - } - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} -#endif - -#if EV_IDLE_ENABLE -void -ev_idle_start (EV_P_ ev_idle *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - pri_adjust (EV_A_ (W)w); - - EV_FREQUENT_CHECK; - - { - int active = ++idlecnt [ABSPRI (w)]; - - ++idleall; - ev_start (EV_A_ (W)w, active); - - array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2); - idles [ABSPRI (w)][active - 1] = w; - } - - EV_FREQUENT_CHECK; -} - -void -ev_idle_stop (EV_P_ ev_idle *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - { - int active = ev_active (w); - - idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; - ev_active (idles [ABSPRI (w)][active - 1]) = active; - - ev_stop (EV_A_ (W)w); - --idleall; - } - - EV_FREQUENT_CHECK; -} -#endif - -#if EV_PREPARE_ENABLE -void -ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - ev_start (EV_A_ (W)w, ++preparecnt); - array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); - prepares [preparecnt - 1] = w; - - EV_FREQUENT_CHECK; -} - -void -ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - { - int active = ev_active (w); - - prepares [active - 1] = prepares [--preparecnt]; - ev_active (prepares [active - 1]) = active; - } - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} -#endif - -#if EV_CHECK_ENABLE -void -ev_check_start (EV_P_ ev_check *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - ev_start (EV_A_ (W)w, ++checkcnt); - array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); - checks [checkcnt - 1] = w; - - EV_FREQUENT_CHECK; -} - -void -ev_check_stop (EV_P_ ev_check *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - { - int active = ev_active (w); - - checks [active - 1] = checks [--checkcnt]; - ev_active (checks [active - 1]) = active; - } - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} -#endif - -#if EV_EMBED_ENABLE -noinline -void -ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW -{ - ev_run (w->other, EVRUN_NOWAIT); -} - -static void -embed_io_cb (EV_P_ ev_io *io, int revents) -{ - ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); - - if (ev_cb (w)) - ev_feed_event (EV_A_ (W)w, EV_EMBED); - else - ev_run (w->other, EVRUN_NOWAIT); -} - -static void -embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) -{ - ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); - - { - EV_P = w->other; - - while (fdchangecnt) - { - fd_reify (EV_A); - ev_run (EV_A_ EVRUN_NOWAIT); - } - } -} - -static void -embed_fork_cb (EV_P_ ev_fork *fork_w, int revents) -{ - ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); - - ev_embed_stop (EV_A_ w); - - { - EV_P = w->other; - - ev_loop_fork (EV_A); - ev_run (EV_A_ EVRUN_NOWAIT); - } - - ev_embed_start (EV_A_ w); -} - -#if 0 -static void -embed_idle_cb (EV_P_ ev_idle *idle, int revents) -{ - ev_idle_stop (EV_A_ idle); -} -#endif - -void -ev_embed_start (EV_P_ ev_embed *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - { - EV_P = w->other; - assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); - ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); - } - - EV_FREQUENT_CHECK; - - ev_set_priority (&w->io, ev_priority (w)); - ev_io_start (EV_A_ &w->io); - - ev_prepare_init (&w->prepare, embed_prepare_cb); - ev_set_priority (&w->prepare, EV_MINPRI); - ev_prepare_start (EV_A_ &w->prepare); - - ev_fork_init (&w->fork, embed_fork_cb); - ev_fork_start (EV_A_ &w->fork); - - /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ - - ev_start (EV_A_ (W)w, 1); - - EV_FREQUENT_CHECK; -} - -void -ev_embed_stop (EV_P_ ev_embed *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - ev_io_stop (EV_A_ &w->io); - ev_prepare_stop (EV_A_ &w->prepare); - ev_fork_stop (EV_A_ &w->fork); - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} -#endif - -#if EV_FORK_ENABLE -void -ev_fork_start (EV_P_ ev_fork *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - ev_start (EV_A_ (W)w, ++forkcnt); - array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2); - forks [forkcnt - 1] = w; - - EV_FREQUENT_CHECK; -} - -void -ev_fork_stop (EV_P_ ev_fork *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - { - int active = ev_active (w); - - forks [active - 1] = forks [--forkcnt]; - ev_active (forks [active - 1]) = active; - } - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} -#endif - -#if EV_CLEANUP_ENABLE -void -ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - ev_start (EV_A_ (W)w, ++cleanupcnt); - array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2); - cleanups [cleanupcnt - 1] = w; - - /* cleanup watchers should never keep a refcount on the loop */ - ev_unref (EV_A); - EV_FREQUENT_CHECK; -} - -void -ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - ev_ref (EV_A); - - { - int active = ev_active (w); - - cleanups [active - 1] = cleanups [--cleanupcnt]; - ev_active (cleanups [active - 1]) = active; - } - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} -#endif - -#if EV_ASYNC_ENABLE -void -ev_async_start (EV_P_ ev_async *w) EV_THROW -{ - if (expect_false (ev_is_active (w))) - return; - - w->sent = 0; - - evpipe_init (EV_A); - - EV_FREQUENT_CHECK; - - ev_start (EV_A_ (W)w, ++asynccnt); - array_needsize (ev_async *, asyncs, asyncmax, asynccnt, EMPTY2); - asyncs [asynccnt - 1] = w; - - EV_FREQUENT_CHECK; -} - -void -ev_async_stop (EV_P_ ev_async *w) EV_THROW -{ - clear_pending (EV_A_ (W)w); - if (expect_false (!ev_is_active (w))) - return; - - EV_FREQUENT_CHECK; - - { - int active = ev_active (w); - - asyncs [active - 1] = asyncs [--asynccnt]; - ev_active (asyncs [active - 1]) = active; - } - - ev_stop (EV_A_ (W)w); - - EV_FREQUENT_CHECK; -} - -void -ev_async_send (EV_P_ ev_async *w) EV_THROW -{ - w->sent = 1; - evpipe_write (EV_A_ &async_pending); -} -#endif - -/*****************************************************************************/ - -struct ev_once -{ - ev_io io; - ev_timer to; - void (*cb)(int revents, void *arg); - void *arg; -}; - -static void -once_cb (EV_P_ struct ev_once *once, int revents) -{ - void (*cb)(int revents, void *arg) = once->cb; - void *arg = once->arg; - - ev_io_stop (EV_A_ &once->io); - ev_timer_stop (EV_A_ &once->to); - ev_free (once); - - cb (revents, arg); -} - -static void -once_cb_io (EV_P_ ev_io *w, int revents) -{ - struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)); - - once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->to)); -} - -static void -once_cb_to (EV_P_ ev_timer *w, int revents) -{ - struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)); - - once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); -} - -void -ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW -{ - struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); - - if (expect_false (!once)) - { - cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg); - return; - } - - once->cb = cb; - once->arg = arg; - - ev_init (&once->io, once_cb_io); - if (fd >= 0) - { - ev_io_set (&once->io, fd, events); - ev_io_start (EV_A_ &once->io); - } - - ev_init (&once->to, once_cb_to); - if (timeout >= 0.) - { - ev_timer_set (&once->to, timeout, 0.); - ev_timer_start (EV_A_ &once->to); - } -} - -/*****************************************************************************/ - -#if EV_WALK_ENABLE -ecb_cold -void -ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW -{ - int i, j; - ev_watcher_list *wl, *wn; - - if (types & (EV_IO | EV_EMBED)) - for (i = 0; i < anfdmax; ++i) - for (wl = anfds [i].head; wl; ) - { - wn = wl->next; - -#if EV_EMBED_ENABLE - if (ev_cb ((ev_io *)wl) == embed_io_cb) - { - if (types & EV_EMBED) - cb (EV_A_ EV_EMBED, ((char *)wl) - offsetof (struct ev_embed, io)); - } - else -#endif -#if EV_USE_INOTIFY - if (ev_cb ((ev_io *)wl) == infy_cb) - ; - else -#endif - if ((ev_io *)wl != &pipe_w) - if (types & EV_IO) - cb (EV_A_ EV_IO, wl); - - wl = wn; - } - - if (types & (EV_TIMER | EV_STAT)) - for (i = timercnt + HEAP0; i-- > HEAP0; ) -#if EV_STAT_ENABLE - /*TODO: timer is not always active*/ - if (ev_cb ((ev_timer *)ANHE_w (timers [i])) == stat_timer_cb) - { - if (types & EV_STAT) - cb (EV_A_ EV_STAT, ((char *)ANHE_w (timers [i])) - offsetof (struct ev_stat, timer)); - } - else -#endif - if (types & EV_TIMER) - cb (EV_A_ EV_TIMER, ANHE_w (timers [i])); - -#if EV_PERIODIC_ENABLE - if (types & EV_PERIODIC) - for (i = periodiccnt + HEAP0; i-- > HEAP0; ) - cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); -#endif - -#if EV_IDLE_ENABLE - if (types & EV_IDLE) - for (j = NUMPRI; j--; ) - for (i = idlecnt [j]; i--; ) - cb (EV_A_ EV_IDLE, idles [j][i]); -#endif - -#if EV_FORK_ENABLE - if (types & EV_FORK) - for (i = forkcnt; i--; ) - if (ev_cb (forks [i]) != embed_fork_cb) - cb (EV_A_ EV_FORK, forks [i]); -#endif - -#if EV_ASYNC_ENABLE - if (types & EV_ASYNC) - for (i = asynccnt; i--; ) - cb (EV_A_ EV_ASYNC, asyncs [i]); -#endif - -#if EV_PREPARE_ENABLE - if (types & EV_PREPARE) - for (i = preparecnt; i--; ) -# if EV_EMBED_ENABLE - if (ev_cb (prepares [i]) != embed_prepare_cb) -# endif - cb (EV_A_ EV_PREPARE, prepares [i]); -#endif - -#if EV_CHECK_ENABLE - if (types & EV_CHECK) - for (i = checkcnt; i--; ) - cb (EV_A_ EV_CHECK, checks [i]); -#endif - -#if EV_SIGNAL_ENABLE - if (types & EV_SIGNAL) - for (i = 0; i < EV_NSIG - 1; ++i) - for (wl = signals [i].head; wl; ) - { - wn = wl->next; - cb (EV_A_ EV_SIGNAL, wl); - wl = wn; - } -#endif - -#if EV_CHILD_ENABLE - if (types & EV_CHILD) - for (i = (EV_PID_HASHSIZE); i--; ) - for (wl = childs [i]; wl; ) - { - wn = wl->next; - cb (EV_A_ EV_CHILD, wl); - wl = wn; - } -#endif -/* EV_STAT 0x00001000 /* stat data changed */ -/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ -} -#endif - -#if EV_MULTIPLICITY - #include "ev_wrap.h" -#endif - diff --git a/third_party/libev/ev.h b/third_party/libev/ev.h deleted file mode 100644 index db937776..00000000 --- a/third_party/libev/ev.h +++ /dev/null @@ -1,854 +0,0 @@ -/* - * libev native API header - * - * Copyright (c) 2007,2008,2009,2010,2011,2012,2015 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -#ifndef EV_H_ -#define EV_H_ - -#ifdef __cplusplus -# define EV_CPP(x) x -# if __cplusplus >= 201103L -# define EV_THROW noexcept -# else -# define EV_THROW throw () -# endif -#else -# define EV_CPP(x) -# define EV_THROW -#endif - -EV_CPP(extern "C" {) - -/*****************************************************************************/ - -/* pre-4.0 compatibility */ -#ifndef EV_COMPAT3 -# define EV_COMPAT3 1 -#endif - -#ifndef EV_FEATURES -# if defined __OPTIMIZE_SIZE__ -# define EV_FEATURES 0x7c -# else -# define EV_FEATURES 0x7f -# endif -#endif - -#define EV_FEATURE_CODE ((EV_FEATURES) & 1) -#define EV_FEATURE_DATA ((EV_FEATURES) & 2) -#define EV_FEATURE_CONFIG ((EV_FEATURES) & 4) -#define EV_FEATURE_API ((EV_FEATURES) & 8) -#define EV_FEATURE_WATCHERS ((EV_FEATURES) & 16) -#define EV_FEATURE_BACKENDS ((EV_FEATURES) & 32) -#define EV_FEATURE_OS ((EV_FEATURES) & 64) - -/* these priorities are inclusive, higher priorities will be invoked earlier */ -#ifndef EV_MINPRI -# define EV_MINPRI (EV_FEATURE_CONFIG ? -2 : 0) -#endif -#ifndef EV_MAXPRI -# define EV_MAXPRI (EV_FEATURE_CONFIG ? +2 : 0) -#endif - -#ifndef EV_MULTIPLICITY -# define EV_MULTIPLICITY EV_FEATURE_CONFIG -#endif - -#ifndef EV_PERIODIC_ENABLE -# define EV_PERIODIC_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_STAT_ENABLE -# define EV_STAT_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_PREPARE_ENABLE -# define EV_PREPARE_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_CHECK_ENABLE -# define EV_CHECK_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_IDLE_ENABLE -# define EV_IDLE_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_FORK_ENABLE -# define EV_FORK_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_CLEANUP_ENABLE -# define EV_CLEANUP_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_SIGNAL_ENABLE -# define EV_SIGNAL_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_CHILD_ENABLE -# ifdef _WIN32 -# define EV_CHILD_ENABLE 0 -# else -# define EV_CHILD_ENABLE EV_FEATURE_WATCHERS -#endif -#endif - -#ifndef EV_ASYNC_ENABLE -# define EV_ASYNC_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_EMBED_ENABLE -# define EV_EMBED_ENABLE EV_FEATURE_WATCHERS -#endif - -#ifndef EV_WALK_ENABLE -# define EV_WALK_ENABLE 0 /* not yet */ -#endif - -/*****************************************************************************/ - -#if EV_CHILD_ENABLE && !EV_SIGNAL_ENABLE -# undef EV_SIGNAL_ENABLE -# define EV_SIGNAL_ENABLE 1 -#endif - -/*****************************************************************************/ - -typedef double ev_tstamp; - -#include /* for memmove */ - -#ifndef EV_ATOMIC_T -# include -# define EV_ATOMIC_T sig_atomic_t volatile -#endif - -#if EV_STAT_ENABLE -# ifdef _WIN32 -# include -# include -# endif -# include -#endif - -/* support multiple event loops? */ -#if EV_MULTIPLICITY -struct ev_loop; -# define EV_P struct ev_loop *loop /* a loop as sole parameter in a declaration */ -# define EV_P_ EV_P, /* a loop as first of multiple parameters */ -# define EV_A loop /* a loop as sole argument to a function call */ -# define EV_A_ EV_A, /* a loop as first of multiple arguments */ -# define EV_DEFAULT_UC ev_default_loop_uc_ () /* the default loop, if initialised, as sole arg */ -# define EV_DEFAULT_UC_ EV_DEFAULT_UC, /* the default loop as first of multiple arguments */ -# define EV_DEFAULT ev_default_loop (0) /* the default loop as sole arg */ -# define EV_DEFAULT_ EV_DEFAULT, /* the default loop as first of multiple arguments */ -#else -# define EV_P void -# define EV_P_ -# define EV_A -# define EV_A_ -# define EV_DEFAULT -# define EV_DEFAULT_ -# define EV_DEFAULT_UC -# define EV_DEFAULT_UC_ -# undef EV_EMBED_ENABLE -#endif - -/* EV_INLINE is used for functions in header files */ -#if __STDC_VERSION__ >= 199901L || __GNUC__ >= 3 -# define EV_INLINE static inline -#else -# define EV_INLINE static -#endif - -#ifdef EV_API_STATIC -# define EV_API_DECL static -#else -# define EV_API_DECL extern -#endif - -/* EV_PROTOTYPES can be used to switch of prototype declarations */ -#ifndef EV_PROTOTYPES -# define EV_PROTOTYPES 1 -#endif - -/*****************************************************************************/ - -#define EV_VERSION_MAJOR 4 -#define EV_VERSION_MINOR 24 - -/* eventmask, revents, events... */ -enum { - EV_UNDEF = (int)0xFFFFFFFF, /* guaranteed to be invalid */ - EV_NONE = 0x00, /* no events */ - EV_READ = 0x01, /* ev_io detected read will not block */ - EV_WRITE = 0x02, /* ev_io detected write will not block */ - EV__IOFDSET = 0x80, /* internal use only */ - EV_IO = EV_READ, /* alias for type-detection */ - EV_TIMER = 0x00000100, /* timer timed out */ -#if EV_COMPAT3 - EV_TIMEOUT = EV_TIMER, /* pre 4.0 API compatibility */ -#endif - EV_PERIODIC = 0x00000200, /* periodic timer timed out */ - EV_SIGNAL = 0x00000400, /* signal was received */ - EV_CHILD = 0x00000800, /* child/pid had status change */ - EV_STAT = 0x00001000, /* stat data changed */ - EV_IDLE = 0x00002000, /* event loop is idling */ - EV_PREPARE = 0x00004000, /* event loop about to poll */ - EV_CHECK = 0x00008000, /* event loop finished poll */ - EV_EMBED = 0x00010000, /* embedded event loop needs sweep */ - EV_FORK = 0x00020000, /* event loop resumed in child */ - EV_CLEANUP = 0x00040000, /* event loop resumed in child */ - EV_ASYNC = 0x00080000, /* async intra-loop signal */ - EV_CUSTOM = 0x01000000, /* for use by user code */ - EV_ERROR = (int)0x80000000 /* sent when an error occurs */ -}; - -/* can be used to add custom fields to all watchers, while losing binary compatibility */ -#ifndef EV_COMMON -# define EV_COMMON void *data; -#endif - -#ifndef EV_CB_DECLARE -# define EV_CB_DECLARE(type) void (*cb)(EV_P_ struct type *w, int revents); -#endif -#ifndef EV_CB_INVOKE -# define EV_CB_INVOKE(watcher,revents) (watcher)->cb (EV_A_ (watcher), (revents)) -#endif - -/* not official, do not use */ -#define EV_CB(type,name) void name (EV_P_ struct ev_ ## type *w, int revents) - -/* - * struct member types: - * private: you may look at them, but not change them, - * and they might not mean anything to you. - * ro: can be read anytime, but only changed when the watcher isn't active. - * rw: can be read and modified anytime, even when the watcher is active. - * - * some internal details that might be helpful for debugging: - * - * active is either 0, which means the watcher is not active, - * or the array index of the watcher (periodics, timers) - * or the array index + 1 (most other watchers) - * or simply 1 for watchers that aren't in some array. - * pending is either 0, in which case the watcher isn't, - * or the array index + 1 in the pendings array. - */ - -#if EV_MINPRI == EV_MAXPRI -# define EV_DECL_PRIORITY -#elif !defined (EV_DECL_PRIORITY) -# define EV_DECL_PRIORITY int priority; -#endif - -/* shared by all watchers */ -#define EV_WATCHER(type) \ - int active; /* private */ \ - int pending; /* private */ \ - EV_DECL_PRIORITY /* private */ \ - EV_COMMON /* rw */ \ - EV_CB_DECLARE (type) /* private */ - -#define EV_WATCHER_LIST(type) \ - EV_WATCHER (type) \ - struct ev_watcher_list *next; /* private */ - -#define EV_WATCHER_TIME(type) \ - EV_WATCHER (type) \ - ev_tstamp at; /* private */ - -/* base class, nothing to see here unless you subclass */ -typedef struct ev_watcher -{ - EV_WATCHER (ev_watcher) -} ev_watcher; - -/* base class, nothing to see here unless you subclass */ -typedef struct ev_watcher_list -{ - EV_WATCHER_LIST (ev_watcher_list) -} ev_watcher_list; - -/* base class, nothing to see here unless you subclass */ -typedef struct ev_watcher_time -{ - EV_WATCHER_TIME (ev_watcher_time) -} ev_watcher_time; - -/* invoked when fd is either EV_READable or EV_WRITEable */ -/* revent EV_READ, EV_WRITE */ -typedef struct ev_io -{ - EV_WATCHER_LIST (ev_io) - - int fd; /* ro */ - int events; /* ro */ -} ev_io; - -/* invoked after a specific time, repeatable (based on monotonic clock) */ -/* revent EV_TIMEOUT */ -typedef struct ev_timer -{ - EV_WATCHER_TIME (ev_timer) - - ev_tstamp repeat; /* rw */ -} ev_timer; - -/* invoked at some specific time, possibly repeating at regular intervals (based on UTC) */ -/* revent EV_PERIODIC */ -typedef struct ev_periodic -{ - EV_WATCHER_TIME (ev_periodic) - - ev_tstamp offset; /* rw */ - ev_tstamp interval; /* rw */ - ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) EV_THROW; /* rw */ -} ev_periodic; - -/* invoked when the given signal has been received */ -/* revent EV_SIGNAL */ -typedef struct ev_signal -{ - EV_WATCHER_LIST (ev_signal) - - int signum; /* ro */ -} ev_signal; - -/* invoked when sigchld is received and waitpid indicates the given pid */ -/* revent EV_CHILD */ -/* does not support priorities */ -typedef struct ev_child -{ - EV_WATCHER_LIST (ev_child) - - int flags; /* private */ - int pid; /* ro */ - int rpid; /* rw, holds the received pid */ - int rstatus; /* rw, holds the exit status, use the macros from sys/wait.h */ -} ev_child; - -#if EV_STAT_ENABLE -/* st_nlink = 0 means missing file or other error */ -# ifdef _WIN32 -typedef struct _stati64 ev_statdata; -# else -typedef struct stat ev_statdata; -# endif - -/* invoked each time the stat data changes for a given path */ -/* revent EV_STAT */ -typedef struct ev_stat -{ - EV_WATCHER_LIST (ev_stat) - - ev_timer timer; /* private */ - ev_tstamp interval; /* ro */ - const char *path; /* ro */ - ev_statdata prev; /* ro */ - ev_statdata attr; /* ro */ - - int wd; /* wd for inotify, fd for kqueue */ -} ev_stat; -#endif - -#if EV_IDLE_ENABLE -/* invoked when the nothing else needs to be done, keeps the process from blocking */ -/* revent EV_IDLE */ -typedef struct ev_idle -{ - EV_WATCHER (ev_idle) -} ev_idle; -#endif - -/* invoked for each run of the mainloop, just before the blocking call */ -/* you can still change events in any way you like */ -/* revent EV_PREPARE */ -typedef struct ev_prepare -{ - EV_WATCHER (ev_prepare) -} ev_prepare; - -/* invoked for each run of the mainloop, just after the blocking call */ -/* revent EV_CHECK */ -typedef struct ev_check -{ - EV_WATCHER (ev_check) -} ev_check; - -#if EV_FORK_ENABLE -/* the callback gets invoked before check in the child process when a fork was detected */ -/* revent EV_FORK */ -typedef struct ev_fork -{ - EV_WATCHER (ev_fork) -} ev_fork; -#endif - -#if EV_CLEANUP_ENABLE -/* is invoked just before the loop gets destroyed */ -/* revent EV_CLEANUP */ -typedef struct ev_cleanup -{ - EV_WATCHER (ev_cleanup) -} ev_cleanup; -#endif - -#if EV_EMBED_ENABLE -/* used to embed an event loop inside another */ -/* the callback gets invoked when the event loop has handled events, and can be 0 */ -typedef struct ev_embed -{ - EV_WATCHER (ev_embed) - - struct ev_loop *other; /* ro */ - ev_io io; /* private */ - ev_prepare prepare; /* private */ - ev_check check; /* unused */ - ev_timer timer; /* unused */ - ev_periodic periodic; /* unused */ - ev_idle idle; /* unused */ - ev_fork fork; /* private */ -#if EV_CLEANUP_ENABLE - ev_cleanup cleanup; /* unused */ -#endif -} ev_embed; -#endif - -#if EV_ASYNC_ENABLE -/* invoked when somebody calls ev_async_send on the watcher */ -/* revent EV_ASYNC */ -typedef struct ev_async -{ - EV_WATCHER (ev_async) - - EV_ATOMIC_T sent; /* private */ -} ev_async; - -# define ev_async_pending(w) (+(w)->sent) -#endif - -/* the presence of this union forces similar struct layout */ -union ev_any_watcher -{ - struct ev_watcher w; - struct ev_watcher_list wl; - - struct ev_io io; - struct ev_timer timer; - struct ev_periodic periodic; - struct ev_signal signal; - struct ev_child child; -#if EV_STAT_ENABLE - struct ev_stat stat; -#endif -#if EV_IDLE_ENABLE - struct ev_idle idle; -#endif - struct ev_prepare prepare; - struct ev_check check; -#if EV_FORK_ENABLE - struct ev_fork fork; -#endif -#if EV_CLEANUP_ENABLE - struct ev_cleanup cleanup; -#endif -#if EV_EMBED_ENABLE - struct ev_embed embed; -#endif -#if EV_ASYNC_ENABLE - struct ev_async async; -#endif -}; - -/* flag bits for ev_default_loop and ev_loop_new */ -enum { - /* the default */ - EVFLAG_AUTO = 0x00000000U, /* not quite a mask */ - /* flag bits */ - EVFLAG_NOENV = 0x01000000U, /* do NOT consult environment */ - EVFLAG_FORKCHECK = 0x02000000U, /* check for a fork in each iteration */ - /* debugging/feature disable */ - EVFLAG_NOINOTIFY = 0x00100000U, /* do not attempt to use inotify */ -#if EV_COMPAT3 - EVFLAG_NOSIGFD = 0, /* compatibility to pre-3.9 */ -#endif - EVFLAG_SIGNALFD = 0x00200000U, /* attempt to use signalfd */ - EVFLAG_NOSIGMASK = 0x00400000U /* avoid modifying the signal mask */ -}; - -/* method bits to be ored together */ -enum { - EVBACKEND_SELECT = 0x00000001U, /* available just about anywhere */ - EVBACKEND_POLL = 0x00000002U, /* !win, !aix, broken on osx */ - EVBACKEND_EPOLL = 0x00000004U, /* linux */ - EVBACKEND_KQUEUE = 0x00000008U, /* bsd, broken on osx */ - EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */ - EVBACKEND_PORT = 0x00000020U, /* solaris 10 */ - EVBACKEND_ALL = 0x0000003FU, /* all known backends */ - EVBACKEND_MASK = 0x0000FFFFU /* all future backends */ -}; - -#if EV_PROTOTYPES -EV_API_DECL int ev_version_major (void) EV_THROW; -EV_API_DECL int ev_version_minor (void) EV_THROW; - -EV_API_DECL unsigned int ev_supported_backends (void) EV_THROW; -EV_API_DECL unsigned int ev_recommended_backends (void) EV_THROW; -EV_API_DECL unsigned int ev_embeddable_backends (void) EV_THROW; - -EV_API_DECL ev_tstamp ev_time (void) EV_THROW; -EV_API_DECL void ev_sleep (ev_tstamp delay) EV_THROW; /* sleep for a while */ - -/* Sets the allocation function to use, works like realloc. - * It is used to allocate and free memory. - * If it returns zero when memory needs to be allocated, the library might abort - * or take some potentially destructive action. - * The default is your system realloc function. - */ -EV_API_DECL void ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW; - -/* set the callback function to call on a - * retryable syscall error - * (such as failed select, poll, epoll_wait) - */ -EV_API_DECL void ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW; - -#if EV_MULTIPLICITY - -/* the default loop is the only one that handles signals and child watchers */ -/* you can call this as often as you like */ -EV_API_DECL struct ev_loop *ev_default_loop (unsigned int flags EV_CPP (= 0)) EV_THROW; - -#ifdef EV_API_STATIC -EV_API_DECL struct ev_loop *ev_default_loop_ptr; -#endif - -EV_INLINE struct ev_loop * -ev_default_loop_uc_ (void) EV_THROW -{ - extern struct ev_loop *ev_default_loop_ptr; - - return ev_default_loop_ptr; -} - -EV_INLINE int -ev_is_default_loop (EV_P) EV_THROW -{ - return EV_A == EV_DEFAULT_UC; -} - -/* create and destroy alternative loops that don't handle signals */ -EV_API_DECL struct ev_loop *ev_loop_new (unsigned int flags EV_CPP (= 0)) EV_THROW; - -EV_API_DECL ev_tstamp ev_now (EV_P) EV_THROW; /* time w.r.t. timers and the eventloop, updated after each poll */ - -#else - -EV_API_DECL int ev_default_loop (unsigned int flags EV_CPP (= 0)) EV_THROW; /* returns true when successful */ - -EV_API_DECL ev_tstamp ev_rt_now; - -EV_INLINE ev_tstamp -ev_now (void) EV_THROW -{ - return ev_rt_now; -} - -/* looks weird, but ev_is_default_loop (EV_A) still works if this exists */ -EV_INLINE int -ev_is_default_loop (void) EV_THROW -{ - return 1; -} - -#endif /* multiplicity */ - -/* destroy event loops, also works for the default loop */ -EV_API_DECL void ev_loop_destroy (EV_P); - -/* this needs to be called after fork, to duplicate the loop */ -/* when you want to re-use it in the child */ -/* you can call it in either the parent or the child */ -/* you can actually call it at any time, anywhere :) */ -EV_API_DECL void ev_loop_fork (EV_P) EV_THROW; - -EV_API_DECL unsigned int ev_backend (EV_P) EV_THROW; /* backend in use by loop */ - -EV_API_DECL void ev_now_update (EV_P) EV_THROW; /* update event loop time */ - -#if EV_WALK_ENABLE -/* walk (almost) all watchers in the loop of a given type, invoking the */ -/* callback on every such watcher. The callback might stop the watcher, */ -/* but do nothing else with the loop */ -EV_API_DECL void ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW; -#endif - -#endif /* prototypes */ - -/* ev_run flags values */ -enum { - EVRUN_NOWAIT = 1, /* do not block/wait */ - EVRUN_ONCE = 2 /* block *once* only */ -}; - -/* ev_break how values */ -enum { - EVBREAK_CANCEL = 0, /* undo unloop */ - EVBREAK_ONE = 1, /* unloop once */ - EVBREAK_ALL = 2 /* unloop all loops */ -}; - -#if EV_PROTOTYPES -EV_API_DECL int ev_run (EV_P_ int flags EV_CPP (= 0)); -EV_API_DECL void ev_break (EV_P_ int how EV_CPP (= EVBREAK_ONE)) EV_THROW; /* break out of the loop */ - -/* - * ref/unref can be used to add or remove a refcount on the mainloop. every watcher - * keeps one reference. if you have a long-running watcher you never unregister that - * should not keep ev_loop from running, unref() after starting, and ref() before stopping. - */ -EV_API_DECL void ev_ref (EV_P) EV_THROW; -EV_API_DECL void ev_unref (EV_P) EV_THROW; - -/* - * convenience function, wait for a single event, without registering an event watcher - * if timeout is < 0, do wait indefinitely - */ -EV_API_DECL void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW; - -# if EV_FEATURE_API -EV_API_DECL unsigned int ev_iteration (EV_P) EV_THROW; /* number of loop iterations */ -EV_API_DECL unsigned int ev_depth (EV_P) EV_THROW; /* #ev_loop enters - #ev_loop leaves */ -EV_API_DECL void ev_verify (EV_P) EV_THROW; /* abort if loop data corrupted */ - -EV_API_DECL void ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW; /* sleep at least this time, default 0 */ -EV_API_DECL void ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW; /* sleep at least this time, default 0 */ - -/* advanced stuff for threading etc. support, see docs */ -EV_API_DECL void ev_set_userdata (EV_P_ void *data) EV_THROW; -EV_API_DECL void *ev_userdata (EV_P) EV_THROW; -typedef void (*ev_loop_callback)(EV_P); -EV_API_DECL void ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_THROW; -/* C++ doesn't allow the use of the ev_loop_callback typedef here, so we need to spell it out */ -EV_API_DECL void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW; - -EV_API_DECL unsigned int ev_pending_count (EV_P) EV_THROW; /* number of pending events, if any */ -EV_API_DECL void ev_invoke_pending (EV_P); /* invoke all pending watchers */ - -/* - * stop/start the timer handling. - */ -EV_API_DECL void ev_suspend (EV_P) EV_THROW; -EV_API_DECL void ev_resume (EV_P) EV_THROW; -#endif - -#endif - -/* these may evaluate ev multiple times, and the other arguments at most once */ -/* either use ev_init + ev_TYPE_set, or the ev_TYPE_init macro, below, to first initialise a watcher */ -#define ev_init(ev,cb_) do { \ - ((ev_watcher *)(void *)(ev))->active = \ - ((ev_watcher *)(void *)(ev))->pending = 0; \ - ev_set_priority ((ev), 0); \ - ev_set_cb ((ev), cb_); \ -} while (0) - -#define ev_io_set(ev,fd_,events_) do { (ev)->fd = (fd_); (ev)->events = (events_) | EV__IOFDSET; } while (0) -#define ev_timer_set(ev,after_,repeat_) do { ((ev_watcher_time *)(ev))->at = (after_); (ev)->repeat = (repeat_); } while (0) -#define ev_periodic_set(ev,ofs_,ival_,rcb_) do { (ev)->offset = (ofs_); (ev)->interval = (ival_); (ev)->reschedule_cb = (rcb_); } while (0) -#define ev_signal_set(ev,signum_) do { (ev)->signum = (signum_); } while (0) -#define ev_child_set(ev,pid_,trace_) do { (ev)->pid = (pid_); (ev)->flags = !!(trace_); } while (0) -#define ev_stat_set(ev,path_,interval_) do { (ev)->path = (path_); (ev)->interval = (interval_); (ev)->wd = -2; } while (0) -#define ev_idle_set(ev) /* nop, yes, this is a serious in-joke */ -#define ev_prepare_set(ev) /* nop, yes, this is a serious in-joke */ -#define ev_check_set(ev) /* nop, yes, this is a serious in-joke */ -#define ev_embed_set(ev,other_) do { (ev)->other = (other_); } while (0) -#define ev_fork_set(ev) /* nop, yes, this is a serious in-joke */ -#define ev_cleanup_set(ev) /* nop, yes, this is a serious in-joke */ -#define ev_async_set(ev) /* nop, yes, this is a serious in-joke */ - -#define ev_io_init(ev,cb,fd,events) do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0) -#define ev_timer_init(ev,cb,after,repeat) do { ev_init ((ev), (cb)); ev_timer_set ((ev),(after),(repeat)); } while (0) -#define ev_periodic_init(ev,cb,ofs,ival,rcb) do { ev_init ((ev), (cb)); ev_periodic_set ((ev),(ofs),(ival),(rcb)); } while (0) -#define ev_signal_init(ev,cb,signum) do { ev_init ((ev), (cb)); ev_signal_set ((ev), (signum)); } while (0) -#define ev_child_init(ev,cb,pid,trace) do { ev_init ((ev), (cb)); ev_child_set ((ev),(pid),(trace)); } while (0) -#define ev_stat_init(ev,cb,path,interval) do { ev_init ((ev), (cb)); ev_stat_set ((ev),(path),(interval)); } while (0) -#define ev_idle_init(ev,cb) do { ev_init ((ev), (cb)); ev_idle_set ((ev)); } while (0) -#define ev_prepare_init(ev,cb) do { ev_init ((ev), (cb)); ev_prepare_set ((ev)); } while (0) -#define ev_check_init(ev,cb) do { ev_init ((ev), (cb)); ev_check_set ((ev)); } while (0) -#define ev_embed_init(ev,cb,other) do { ev_init ((ev), (cb)); ev_embed_set ((ev),(other)); } while (0) -#define ev_fork_init(ev,cb) do { ev_init ((ev), (cb)); ev_fork_set ((ev)); } while (0) -#define ev_cleanup_init(ev,cb) do { ev_init ((ev), (cb)); ev_cleanup_set ((ev)); } while (0) -#define ev_async_init(ev,cb) do { ev_init ((ev), (cb)); ev_async_set ((ev)); } while (0) - -#define ev_is_pending(ev) (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */ -#define ev_is_active(ev) (0 + ((ev_watcher *)(void *)(ev))->active) /* ro, true when the watcher has been started */ - -#define ev_cb_(ev) (ev)->cb /* rw */ -#define ev_cb(ev) (memmove (&ev_cb_ (ev), &((ev_watcher *)(ev))->cb, sizeof (ev_cb_ (ev))), (ev)->cb) - -#if EV_MINPRI == EV_MAXPRI -# define ev_priority(ev) ((ev), EV_MINPRI) -# define ev_set_priority(ev,pri) ((ev), (pri)) -#else -# define ev_priority(ev) (+(((ev_watcher *)(void *)(ev))->priority)) -# define ev_set_priority(ev,pri) ( (ev_watcher *)(void *)(ev))->priority = (pri) -#endif - -#define ev_periodic_at(ev) (+((ev_watcher_time *)(ev))->at) - -#ifndef ev_set_cb -# define ev_set_cb(ev,cb_) (ev_cb_ (ev) = (cb_), memmove (&((ev_watcher *)(ev))->cb, &ev_cb_ (ev), sizeof (ev_cb_ (ev)))) -#endif - -/* stopping (enabling, adding) a watcher does nothing if it is already running */ -/* stopping (disabling, deleting) a watcher does nothing unless it's already running */ -#if EV_PROTOTYPES - -/* feeds an event into a watcher as if the event actually occurred */ -/* accepts any ev_watcher type */ -EV_API_DECL void ev_feed_event (EV_P_ void *w, int revents) EV_THROW; -EV_API_DECL void ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW; -#if EV_SIGNAL_ENABLE -EV_API_DECL void ev_feed_signal (int signum) EV_THROW; -EV_API_DECL void ev_feed_signal_event (EV_P_ int signum) EV_THROW; -#endif -EV_API_DECL void ev_invoke (EV_P_ void *w, int revents); -EV_API_DECL int ev_clear_pending (EV_P_ void *w) EV_THROW; - -EV_API_DECL void ev_io_start (EV_P_ ev_io *w) EV_THROW; -EV_API_DECL void ev_io_stop (EV_P_ ev_io *w) EV_THROW; - -EV_API_DECL void ev_timer_start (EV_P_ ev_timer *w) EV_THROW; -EV_API_DECL void ev_timer_stop (EV_P_ ev_timer *w) EV_THROW; -/* stops if active and no repeat, restarts if active and repeating, starts if inactive and repeating */ -EV_API_DECL void ev_timer_again (EV_P_ ev_timer *w) EV_THROW; -/* return remaining time */ -EV_API_DECL ev_tstamp ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW; - -#if EV_PERIODIC_ENABLE -EV_API_DECL void ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW; -EV_API_DECL void ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW; -EV_API_DECL void ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW; -#endif - -/* only supported in the default loop */ -#if EV_SIGNAL_ENABLE -EV_API_DECL void ev_signal_start (EV_P_ ev_signal *w) EV_THROW; -EV_API_DECL void ev_signal_stop (EV_P_ ev_signal *w) EV_THROW; -#endif - -/* only supported in the default loop */ -# if EV_CHILD_ENABLE -EV_API_DECL void ev_child_start (EV_P_ ev_child *w) EV_THROW; -EV_API_DECL void ev_child_stop (EV_P_ ev_child *w) EV_THROW; -# endif - -# if EV_STAT_ENABLE -EV_API_DECL void ev_stat_start (EV_P_ ev_stat *w) EV_THROW; -EV_API_DECL void ev_stat_stop (EV_P_ ev_stat *w) EV_THROW; -EV_API_DECL void ev_stat_stat (EV_P_ ev_stat *w) EV_THROW; -# endif - -# if EV_IDLE_ENABLE -EV_API_DECL void ev_idle_start (EV_P_ ev_idle *w) EV_THROW; -EV_API_DECL void ev_idle_stop (EV_P_ ev_idle *w) EV_THROW; -# endif - -#if EV_PREPARE_ENABLE -EV_API_DECL void ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW; -EV_API_DECL void ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW; -#endif - -#if EV_CHECK_ENABLE -EV_API_DECL void ev_check_start (EV_P_ ev_check *w) EV_THROW; -EV_API_DECL void ev_check_stop (EV_P_ ev_check *w) EV_THROW; -#endif - -# if EV_FORK_ENABLE -EV_API_DECL void ev_fork_start (EV_P_ ev_fork *w) EV_THROW; -EV_API_DECL void ev_fork_stop (EV_P_ ev_fork *w) EV_THROW; -# endif - -# if EV_CLEANUP_ENABLE -EV_API_DECL void ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW; -EV_API_DECL void ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW; -# endif - -# if EV_EMBED_ENABLE -/* only supported when loop to be embedded is in fact embeddable */ -EV_API_DECL void ev_embed_start (EV_P_ ev_embed *w) EV_THROW; -EV_API_DECL void ev_embed_stop (EV_P_ ev_embed *w) EV_THROW; -EV_API_DECL void ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW; -# endif - -# if EV_ASYNC_ENABLE -EV_API_DECL void ev_async_start (EV_P_ ev_async *w) EV_THROW; -EV_API_DECL void ev_async_stop (EV_P_ ev_async *w) EV_THROW; -EV_API_DECL void ev_async_send (EV_P_ ev_async *w) EV_THROW; -# endif - -#if EV_COMPAT3 - #define EVLOOP_NONBLOCK EVRUN_NOWAIT - #define EVLOOP_ONESHOT EVRUN_ONCE - #define EVUNLOOP_CANCEL EVBREAK_CANCEL - #define EVUNLOOP_ONE EVBREAK_ONE - #define EVUNLOOP_ALL EVBREAK_ALL - #if EV_PROTOTYPES - EV_INLINE void ev_loop (EV_P_ int flags) { ev_run (EV_A_ flags); } - EV_INLINE void ev_unloop (EV_P_ int how ) { ev_break (EV_A_ how ); } - EV_INLINE void ev_default_destroy (void) { ev_loop_destroy (EV_DEFAULT); } - EV_INLINE void ev_default_fork (void) { ev_loop_fork (EV_DEFAULT); } - #if EV_FEATURE_API - EV_INLINE unsigned int ev_loop_count (EV_P) { return ev_iteration (EV_A); } - EV_INLINE unsigned int ev_loop_depth (EV_P) { return ev_depth (EV_A); } - EV_INLINE void ev_loop_verify (EV_P) { ev_verify (EV_A); } - #endif - #endif -#else - typedef struct ev_loop ev_loop; -#endif - -#endif - -EV_CPP(}) - -#endif - diff --git a/third_party/libev/ev_epoll.c b/third_party/libev/ev_epoll.c deleted file mode 100644 index 6aef8622..00000000 --- a/third_party/libev/ev_epoll.c +++ /dev/null @@ -1,285 +0,0 @@ -/* - * libev epoll fd activity backend - * - * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -/* - * general notes about epoll: - * - * a) epoll silently removes fds from the fd set. as nothing tells us - * that an fd has been removed otherwise, we have to continually - * "rearm" fds that we suspect *might* have changed (same - * problem with kqueue, but much less costly there). - * b) the fact that ADD != MOD creates a lot of extra syscalls due to a) - * and seems not to have any advantage. - * c) the inability to handle fork or file descriptors (think dup) - * limits the applicability over poll, so this is not a generic - * poll replacement. - * d) epoll doesn't work the same as select with many file descriptors - * (such as files). while not critical, no other advanced interface - * seems to share this (rather non-unixy) limitation. - * e) epoll claims to be embeddable, but in practise you never get - * a ready event for the epoll fd (broken: <=2.6.26, working: >=2.6.32). - * f) epoll_ctl returning EPERM means the fd is always ready. - * - * lots of "weird code" and complication handling in this file is due - * to these design problems with epoll, as we try very hard to avoid - * epoll_ctl syscalls for common usage patterns and handle the breakage - * ensuing from receiving events for closed and otherwise long gone - * file descriptors. - */ - -#include - -#define EV_EMASK_EPERM 0x80 - -static void -epoll_modify (EV_P_ int fd, int oev, int nev) -{ - struct epoll_event ev; - unsigned char oldmask; - - /* - * we handle EPOLL_CTL_DEL by ignoring it here - * on the assumption that the fd is gone anyways - * if that is wrong, we have to handle the spurious - * event in epoll_poll. - * if the fd is added again, we try to ADD it, and, if that - * fails, we assume it still has the same eventmask. - */ - if (!nev) - return; - - oldmask = anfds [fd].emask; - anfds [fd].emask = nev; - - /* store the generation counter in the upper 32 bits, the fd in the lower 32 bits */ - ev.data.u64 = (uint64_t)(uint32_t)fd - | ((uint64_t)(uint32_t)++anfds [fd].egen << 32); - ev.events = (nev & EV_READ ? EPOLLIN : 0) - | (nev & EV_WRITE ? EPOLLOUT : 0); - - if (expect_true (!epoll_ctl (backend_fd, oev && oldmask != nev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) - return; - - if (expect_true (errno == ENOENT)) - { - /* if ENOENT then the fd went away, so try to do the right thing */ - if (!nev) - goto dec_egen; - - if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev)) - return; - } - else if (expect_true (errno == EEXIST)) - { - /* EEXIST means we ignored a previous DEL, but the fd is still active */ - /* if the kernel mask is the same as the new mask, we assume it hasn't changed */ - if (oldmask == nev) - goto dec_egen; - - if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev)) - return; - } - else if (expect_true (errno == EPERM)) - { - /* EPERM means the fd is always ready, but epoll is too snobbish */ - /* to handle it, unlike select or poll. */ - anfds [fd].emask = EV_EMASK_EPERM; - - /* add fd to epoll_eperms, if not already inside */ - if (!(oldmask & EV_EMASK_EPERM)) - { - array_needsize (int, epoll_eperms, epoll_epermmax, epoll_epermcnt + 1, EMPTY2); - epoll_eperms [epoll_epermcnt++] = fd; - } - - return; - } - - fd_kill (EV_A_ fd); - -dec_egen: - /* we didn't successfully call epoll_ctl, so decrement the generation counter again */ - --anfds [fd].egen; -} - -static void -epoll_poll (EV_P_ ev_tstamp timeout) -{ - int i; - int eventcnt; - - if (expect_false (epoll_epermcnt)) - timeout = 0.; - - /* epoll wait times cannot be larger than (LONG_MAX - 999UL) / HZ msecs, which is below */ - /* the default libev max wait time, however. */ - EV_RELEASE_CB; - eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, timeout * 1e3); - EV_ACQUIRE_CB; - - if (expect_false (eventcnt < 0)) - { - if (errno != EINTR) - ev_syserr ("(libev) epoll_wait"); - - return; - } - - for (i = 0; i < eventcnt; ++i) - { - struct epoll_event *ev = epoll_events + i; - - int fd = (uint32_t)ev->data.u64; /* mask out the lower 32 bits */ - int want = anfds [fd].events; - int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) - | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); - - /* - * check for spurious notification. - * this only finds spurious notifications on egen updates - * other spurious notifications will be found by epoll_ctl, below - * we assume that fd is always in range, as we never shrink the anfds array - */ - if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32))) - { - /* recreate kernel state */ - postfork |= 2; - continue; - } - - if (expect_false (got & ~want)) - { - anfds [fd].emask = want; - - /* - * we received an event but are not interested in it, try mod or del - * this often happens because we optimistically do not unregister fds - * when we are no longer interested in them, but also when we get spurious - * notifications for fds from another process. this is partially handled - * above with the gencounter check (== our fd is not the event fd), and - * partially here, when epoll_ctl returns an error (== a child has the fd - * but we closed it). - */ - ev->events = (want & EV_READ ? EPOLLIN : 0) - | (want & EV_WRITE ? EPOLLOUT : 0); - - /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */ - /* which is fortunately easy to do for us. */ - if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev)) - { - postfork |= 2; /* an error occurred, recreate kernel state */ - continue; - } - } - - fd_event (EV_A_ fd, got); - } - - /* if the receive array was full, increase its size */ - if (expect_false (eventcnt == epoll_eventmax)) - { - ev_free (epoll_events); - epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1); - epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); - } - - /* now synthesize events for all fds where epoll fails, while select works... */ - for (i = epoll_epermcnt; i--; ) - { - int fd = epoll_eperms [i]; - unsigned char events = anfds [fd].events & (EV_READ | EV_WRITE); - - if (anfds [fd].emask & EV_EMASK_EPERM && events) - fd_event (EV_A_ fd, events); - else - { - epoll_eperms [i] = epoll_eperms [--epoll_epermcnt]; - anfds [fd].emask = 0; - } - } -} - -inline_size -int -epoll_init (EV_P_ int flags) -{ -#if defined EPOLL_CLOEXEC && !defined __ANDROID__ - backend_fd = epoll_create1 (EPOLL_CLOEXEC); - - if (backend_fd < 0 && (errno == EINVAL || errno == ENOSYS)) -#endif - backend_fd = epoll_create (256); - - if (backend_fd < 0) - return 0; - - fcntl (backend_fd, F_SETFD, FD_CLOEXEC); - - backend_mintime = 1e-3; /* epoll does sometimes return early, this is just to avoid the worst */ - backend_modify = epoll_modify; - backend_poll = epoll_poll; - - epoll_eventmax = 64; /* initial number of events receivable per poll */ - epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); - - return EVBACKEND_EPOLL; -} - -inline_size -void -epoll_destroy (EV_P) -{ - ev_free (epoll_events); - array_free (epoll_eperm, EMPTY); -} - -inline_size -void -epoll_fork (EV_P) -{ - close (backend_fd); - - while ((backend_fd = epoll_create (256)) < 0) - ev_syserr ("(libev) epoll_create"); - - fcntl (backend_fd, F_SETFD, FD_CLOEXEC); - - fd_rearm_all (EV_A); -} - diff --git a/third_party/libev/ev_kqueue.c b/third_party/libev/ev_kqueue.c deleted file mode 100644 index 0c05ab9e..00000000 --- a/third_party/libev/ev_kqueue.c +++ /dev/null @@ -1,218 +0,0 @@ -/* - * libev kqueue backend - * - * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -#include -#include -#include -#include -#include - -inline_speed -void -kqueue_change (EV_P_ int fd, int filter, int flags, int fflags) -{ - ++kqueue_changecnt; - array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2); - - EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0); -} - -/* OS X at least needs this */ -#ifndef EV_ENABLE -# define EV_ENABLE 0 -#endif -#ifndef NOTE_EOF -# define NOTE_EOF 0 -#endif - -static void -kqueue_modify (EV_P_ int fd, int oev, int nev) -{ - if (oev != nev) - { - if (oev & EV_READ) - kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0); - - if (oev & EV_WRITE) - kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0); - } - - /* to detect close/reopen reliably, we have to re-add */ - /* event requests even when oev == nev */ - - if (nev & EV_READ) - kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD | EV_ENABLE, NOTE_EOF); - - if (nev & EV_WRITE) - kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, NOTE_EOF); -} - -static void -kqueue_poll (EV_P_ ev_tstamp timeout) -{ - int res, i; - struct timespec ts; - - /* need to resize so there is enough space for errors */ - if (kqueue_changecnt > kqueue_eventmax) - { - ev_free (kqueue_events); - kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt); - kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); - } - - EV_RELEASE_CB; - EV_TS_SET (ts, timeout); - res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts); - EV_ACQUIRE_CB; - kqueue_changecnt = 0; - - if (expect_false (res < 0)) - { - if (errno != EINTR) - ev_syserr ("(libev) kevent"); - - return; - } - - for (i = 0; i < res; ++i) - { - int fd = kqueue_events [i].ident; - - if (expect_false (kqueue_events [i].flags & EV_ERROR)) - { - int err = kqueue_events [i].data; - - /* we are only interested in errors for fds that we are interested in :) */ - if (anfds [fd].events) - { - if (err == ENOENT) /* resubmit changes on ENOENT */ - kqueue_modify (EV_A_ fd, 0, anfds [fd].events); - else if (err == EBADF) /* on EBADF, we re-check the fd */ - { - if (fd_valid (fd)) - kqueue_modify (EV_A_ fd, 0, anfds [fd].events); - else - fd_kill (EV_A_ fd); - } - else /* on all other errors, we error out on the fd */ - fd_kill (EV_A_ fd); - } - } - else - fd_event ( - EV_A_ - fd, - kqueue_events [i].filter == EVFILT_READ ? EV_READ - : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE - : 0 - ); - } - - if (expect_false (res == kqueue_eventmax)) - { - ev_free (kqueue_events); - kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1); - kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); - } -} - -inline_size -int -kqueue_init (EV_P_ int flags) -{ - /* initialize the kernel queue */ - kqueue_fd_pid = getpid (); - if ((backend_fd = kqueue ()) < 0) - return 0; - - fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ - - backend_mintime = 1e-9; /* apparently, they did the right thing in freebsd */ - backend_modify = kqueue_modify; - backend_poll = kqueue_poll; - - kqueue_eventmax = 64; /* initial number of events receivable per poll */ - kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); - - kqueue_changes = 0; - kqueue_changemax = 0; - kqueue_changecnt = 0; - - return EVBACKEND_KQUEUE; -} - -inline_size -void -kqueue_destroy (EV_P) -{ - ev_free (kqueue_events); - ev_free (kqueue_changes); -} - -inline_size -void -kqueue_fork (EV_P) -{ - /* some BSD kernels don't just destroy the kqueue itself, - * but also close the fd, which isn't documented, and - * impossible to support properly. - * we remember the pid of the kqueue call and only close - * the fd if the pid is still the same. - * this leaks fds on sane kernels, but BSD interfaces are - * notoriously buggy and rarely get fixed. - */ - pid_t newpid = getpid (); - - if (newpid == kqueue_fd_pid) - close (backend_fd); - - kqueue_fd_pid = newpid; - while ((backend_fd = kqueue ()) < 0) - ev_syserr ("(libev) kqueue"); - - fcntl (backend_fd, F_SETFD, FD_CLOEXEC); - - /* re-register interest in fds */ - fd_rearm_all (EV_A); -} - -/* sys/event.h defines EV_ERROR */ -#undef EV_ERROR - diff --git a/third_party/libev/ev_poll.c b/third_party/libev/ev_poll.c deleted file mode 100644 index bd742b07..00000000 --- a/third_party/libev/ev_poll.c +++ /dev/null @@ -1,151 +0,0 @@ -/* - * libev poll fd activity backend - * - * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -#include - -inline_size -void -pollidx_init (int *base, int count) -{ - /* consider using memset (.., -1, ...), which is practically guaranteed - * to work on all systems implementing poll */ - while (count--) - *base++ = -1; -} - -static void -poll_modify (EV_P_ int fd, int oev, int nev) -{ - int idx; - - if (oev == nev) - return; - - array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); - - idx = pollidxs [fd]; - - if (idx < 0) /* need to allocate a new pollfd */ - { - pollidxs [fd] = idx = pollcnt++; - array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2); - polls [idx].fd = fd; - } - - assert (polls [idx].fd == fd); - - if (nev) - polls [idx].events = - (nev & EV_READ ? POLLIN : 0) - | (nev & EV_WRITE ? POLLOUT : 0); - else /* remove pollfd */ - { - pollidxs [fd] = -1; - - if (expect_true (idx < --pollcnt)) - { - polls [idx] = polls [pollcnt]; - pollidxs [polls [idx].fd] = idx; - } - } -} - -static void -poll_poll (EV_P_ ev_tstamp timeout) -{ - struct pollfd *p; - int res; - - EV_RELEASE_CB; - res = poll (polls, pollcnt, timeout * 1e3); - EV_ACQUIRE_CB; - - if (expect_false (res < 0)) - { - if (errno == EBADF) - fd_ebadf (EV_A); - else if (errno == ENOMEM && !syserr_cb) - fd_enomem (EV_A); - else if (errno != EINTR) - ev_syserr ("(libev) poll"); - } - else - for (p = polls; res; ++p) - { - assert (("libev: poll() returned illegal result, broken BSD kernel?", p < polls + pollcnt)); - - if (expect_false (p->revents)) /* this expect is debatable */ - { - --res; - - if (expect_false (p->revents & POLLNVAL)) - fd_kill (EV_A_ p->fd); - else - fd_event ( - EV_A_ - p->fd, - (p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) - | (p->revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) - ); - } - } -} - -inline_size -int -poll_init (EV_P_ int flags) -{ - backend_mintime = 1e-3; - backend_modify = poll_modify; - backend_poll = poll_poll; - - pollidxs = 0; pollidxmax = 0; - polls = 0; pollmax = 0; pollcnt = 0; - - return EVBACKEND_POLL; -} - -inline_size -void -poll_destroy (EV_P) -{ - ev_free (pollidxs); - ev_free (polls); -} - diff --git a/third_party/libev/ev_port.c b/third_party/libev/ev_port.c deleted file mode 100644 index c7b0b70c..00000000 --- a/third_party/libev/ev_port.c +++ /dev/null @@ -1,189 +0,0 @@ -/* - * libev solaris event port backend - * - * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -/* useful reading: - * - * http://bugs.opensolaris.org/view_bug.do?bug_id=6268715 (random results) - * http://bugs.opensolaris.org/view_bug.do?bug_id=6455223 (just totally broken) - * http://bugs.opensolaris.org/view_bug.do?bug_id=6873782 (manpage ETIME) - * http://bugs.opensolaris.org/view_bug.do?bug_id=6874410 (implementation ETIME) - * http://www.mail-archive.com/networking-discuss@opensolaris.org/msg11898.html ETIME vs. nget - * http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/event_port.c (libc) - * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/fs/portfs/port.c#1325 (kernel) - */ - -#include -#include -#include -#include -#include -#include - -inline_speed -void -port_associate_and_check (EV_P_ int fd, int ev) -{ - if (0 > - port_associate ( - backend_fd, PORT_SOURCE_FD, fd, - (ev & EV_READ ? POLLIN : 0) - | (ev & EV_WRITE ? POLLOUT : 0), - 0 - ) - ) - { - if (errno == EBADFD) - fd_kill (EV_A_ fd); - else - ev_syserr ("(libev) port_associate"); - } -} - -static void -port_modify (EV_P_ int fd, int oev, int nev) -{ - /* we need to reassociate no matter what, as closes are - * once more silently being discarded. - */ - if (!nev) - { - if (oev) - port_dissociate (backend_fd, PORT_SOURCE_FD, fd); - } - else - port_associate_and_check (EV_A_ fd, nev); -} - -static void -port_poll (EV_P_ ev_tstamp timeout) -{ - int res, i; - struct timespec ts; - uint_t nget = 1; - - /* we initialise this to something we will skip in the loop, as */ - /* port_getn can return with nget unchanged, but no indication */ - /* whether it was the original value or has been updated :/ */ - port_events [0].portev_source = 0; - - EV_RELEASE_CB; - EV_TS_SET (ts, timeout); - res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts); - EV_ACQUIRE_CB; - - /* port_getn may or may not set nget on error */ - /* so we rely on port_events [0].portev_source not being updated */ - if (res == -1 && errno != ETIME && errno != EINTR) - ev_syserr ("(libev) port_getn (see http://bugs.opensolaris.org/view_bug.do?bug_id=6268715, try LIBEV_FLAGS=3 env variable)"); - - for (i = 0; i < nget; ++i) - { - if (port_events [i].portev_source == PORT_SOURCE_FD) - { - int fd = port_events [i].portev_object; - - fd_event ( - EV_A_ - fd, - (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) - | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) - ); - - fd_change (EV_A_ fd, EV__IOFDSET); - } - } - - if (expect_false (nget == port_eventmax)) - { - ev_free (port_events); - port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1); - port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); - } -} - -inline_size -int -port_init (EV_P_ int flags) -{ - /* Initialize the kernel queue */ - if ((backend_fd = port_create ()) < 0) - return 0; - - assert (("libev: PORT_SOURCE_FD must not be zero", PORT_SOURCE_FD)); - - fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ - - /* if my reading of the opensolaris kernel sources are correct, then - * opensolaris does something very stupid: it checks if the time has already - * elapsed and doesn't round up if that is the case,m otherwise it DOES round - * up. Since we can't know what the case is, we need to guess by using a - * "large enough" timeout. Normally, 1e-9 would be correct. - */ - backend_mintime = 1e-3; /* needed to compensate for port_getn returning early */ - backend_modify = port_modify; - backend_poll = port_poll; - - port_eventmax = 64; /* initial number of events receivable per poll */ - port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); - - return EVBACKEND_PORT; -} - -inline_size -void -port_destroy (EV_P) -{ - ev_free (port_events); -} - -inline_size -void -port_fork (EV_P) -{ - close (backend_fd); - - while ((backend_fd = port_create ()) < 0) - ev_syserr ("(libev) port"); - - fcntl (backend_fd, F_SETFD, FD_CLOEXEC); - - /* re-register interest in fds */ - fd_rearm_all (EV_A); -} - diff --git a/third_party/libev/ev_select.c b/third_party/libev/ev_select.c deleted file mode 100644 index ed1fc7ad..00000000 --- a/third_party/libev/ev_select.c +++ /dev/null @@ -1,316 +0,0 @@ -/* - * libev select fd activity backend - * - * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -#ifndef _WIN32 -/* for unix systems */ -# include -# ifndef __hpux -/* for REAL unix systems */ -# include -# endif -#endif - -#ifndef EV_SELECT_USE_FD_SET -# ifdef NFDBITS -# define EV_SELECT_USE_FD_SET 0 -# else -# define EV_SELECT_USE_FD_SET 1 -# endif -#endif - -#if EV_SELECT_IS_WINSOCKET -# undef EV_SELECT_USE_FD_SET -# define EV_SELECT_USE_FD_SET 1 -# undef NFDBITS -# define NFDBITS 0 -#endif - -#if !EV_SELECT_USE_FD_SET -# define NFDBYTES (NFDBITS / 8) -#endif - -#include - -static void -select_modify (EV_P_ int fd, int oev, int nev) -{ - if (oev == nev) - return; - - { -#if EV_SELECT_USE_FD_SET - - #if EV_SELECT_IS_WINSOCKET - SOCKET handle = anfds [fd].handle; - #else - int handle = fd; - #endif - - assert (("libev: fd >= FD_SETSIZE passed to fd_set-based select backend", fd < FD_SETSIZE)); - - /* FD_SET is broken on windows (it adds the fd to a set twice or more, - * which eventually leads to overflows). Need to call it only on changes. - */ - #if EV_SELECT_IS_WINSOCKET - if ((oev ^ nev) & EV_READ) - #endif - if (nev & EV_READ) - FD_SET (handle, (fd_set *)vec_ri); - else - FD_CLR (handle, (fd_set *)vec_ri); - - #if EV_SELECT_IS_WINSOCKET - if ((oev ^ nev) & EV_WRITE) - #endif - if (nev & EV_WRITE) - FD_SET (handle, (fd_set *)vec_wi); - else - FD_CLR (handle, (fd_set *)vec_wi); - -#else - - int word = fd / NFDBITS; - fd_mask mask = 1UL << (fd % NFDBITS); - - if (expect_false (vec_max <= word)) - { - int new_max = word + 1; - - vec_ri = ev_realloc (vec_ri, new_max * NFDBYTES); - vec_ro = ev_realloc (vec_ro, new_max * NFDBYTES); /* could free/malloc */ - vec_wi = ev_realloc (vec_wi, new_max * NFDBYTES); - vec_wo = ev_realloc (vec_wo, new_max * NFDBYTES); /* could free/malloc */ - #ifdef _WIN32 - vec_eo = ev_realloc (vec_eo, new_max * NFDBYTES); /* could free/malloc */ - #endif - - for (; vec_max < new_max; ++vec_max) - ((fd_mask *)vec_ri) [vec_max] = - ((fd_mask *)vec_wi) [vec_max] = 0; - } - - ((fd_mask *)vec_ri) [word] |= mask; - if (!(nev & EV_READ)) - ((fd_mask *)vec_ri) [word] &= ~mask; - - ((fd_mask *)vec_wi) [word] |= mask; - if (!(nev & EV_WRITE)) - ((fd_mask *)vec_wi) [word] &= ~mask; -#endif - } -} - -static void -select_poll (EV_P_ ev_tstamp timeout) -{ - struct timeval tv; - int res; - int fd_setsize; - - EV_RELEASE_CB; - EV_TV_SET (tv, timeout); - -#if EV_SELECT_USE_FD_SET - fd_setsize = sizeof (fd_set); -#else - fd_setsize = vec_max * NFDBYTES; -#endif - - memcpy (vec_ro, vec_ri, fd_setsize); - memcpy (vec_wo, vec_wi, fd_setsize); - -#ifdef _WIN32 - /* pass in the write set as except set. - * the idea behind this is to work around a windows bug that causes - * errors to be reported as an exception and not by setting - * the writable bit. this is so uncontrollably lame. - */ - memcpy (vec_eo, vec_wi, fd_setsize); - res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, (fd_set *)vec_eo, &tv); -#elif EV_SELECT_USE_FD_SET - fd_setsize = anfdmax < FD_SETSIZE ? anfdmax : FD_SETSIZE; - res = select (fd_setsize, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); -#else - res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); -#endif - EV_ACQUIRE_CB; - - if (expect_false (res < 0)) - { - #if EV_SELECT_IS_WINSOCKET - errno = WSAGetLastError (); - #endif - #ifdef WSABASEERR - /* on windows, select returns incompatible error codes, fix this */ - if (errno >= WSABASEERR && errno < WSABASEERR + 1000) - if (errno == WSAENOTSOCK) - errno = EBADF; - else - errno -= WSABASEERR; - #endif - - #ifdef _WIN32 - /* select on windows erroneously returns EINVAL when no fd sets have been - * provided (this is documented). what microsoft doesn't tell you that this bug - * exists even when the fd sets _are_ provided, so we have to check for this bug - * here and emulate by sleeping manually. - * we also get EINVAL when the timeout is invalid, but we ignore this case here - * and assume that EINVAL always means: you have to wait manually. - */ - if (errno == EINVAL) - { - if (timeout) - { - unsigned long ms = timeout * 1e3; - Sleep (ms ? ms : 1); - } - - return; - } - #endif - - if (errno == EBADF) - fd_ebadf (EV_A); - else if (errno == ENOMEM && !syserr_cb) - fd_enomem (EV_A); - else if (errno != EINTR) - ev_syserr ("(libev) select"); - - return; - } - -#if EV_SELECT_USE_FD_SET - - { - int fd; - - for (fd = 0; fd < anfdmax; ++fd) - if (anfds [fd].events) - { - int events = 0; - #if EV_SELECT_IS_WINSOCKET - SOCKET handle = anfds [fd].handle; - #else - int handle = fd; - #endif - - if (FD_ISSET (handle, (fd_set *)vec_ro)) events |= EV_READ; - if (FD_ISSET (handle, (fd_set *)vec_wo)) events |= EV_WRITE; - #ifdef _WIN32 - if (FD_ISSET (handle, (fd_set *)vec_eo)) events |= EV_WRITE; - #endif - - if (expect_true (events)) - fd_event (EV_A_ fd, events); - } - } - -#else - - { - int word, bit; - for (word = vec_max; word--; ) - { - fd_mask word_r = ((fd_mask *)vec_ro) [word]; - fd_mask word_w = ((fd_mask *)vec_wo) [word]; - #ifdef _WIN32 - word_w |= ((fd_mask *)vec_eo) [word]; - #endif - - if (word_r || word_w) - for (bit = NFDBITS; bit--; ) - { - fd_mask mask = 1UL << bit; - int events = 0; - - events |= word_r & mask ? EV_READ : 0; - events |= word_w & mask ? EV_WRITE : 0; - - if (expect_true (events)) - fd_event (EV_A_ word * NFDBITS + bit, events); - } - } - } - -#endif -} - -inline_size -int -select_init (EV_P_ int flags) -{ - backend_mintime = 1e-6; - backend_modify = select_modify; - backend_poll = select_poll; - -#if EV_SELECT_USE_FD_SET - vec_ri = ev_malloc (sizeof (fd_set)); FD_ZERO ((fd_set *)vec_ri); - vec_ro = ev_malloc (sizeof (fd_set)); - vec_wi = ev_malloc (sizeof (fd_set)); FD_ZERO ((fd_set *)vec_wi); - vec_wo = ev_malloc (sizeof (fd_set)); - #ifdef _WIN32 - vec_eo = ev_malloc (sizeof (fd_set)); - #endif -#else - vec_max = 0; - vec_ri = 0; - vec_ro = 0; - vec_wi = 0; - vec_wo = 0; - #ifdef _WIN32 - vec_eo = 0; - #endif -#endif - - return EVBACKEND_SELECT; -} - -inline_size -void -select_destroy (EV_P) -{ - ev_free (vec_ri); - ev_free (vec_ro); - ev_free (vec_wi); - ev_free (vec_wo); - #ifdef _WIN32 - ev_free (vec_eo); - #endif -} - diff --git a/third_party/libev/ev_vars.h b/third_party/libev/ev_vars.h deleted file mode 100644 index 04d4db16..00000000 --- a/third_party/libev/ev_vars.h +++ /dev/null @@ -1,204 +0,0 @@ -/* - * loop member variable declarations - * - * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -#define VARx(type,name) VAR(name, type name) - -VARx(ev_tstamp, now_floor) /* last time we refreshed rt_time */ -VARx(ev_tstamp, mn_now) /* monotonic clock "now" */ -VARx(ev_tstamp, rtmn_diff) /* difference realtime - monotonic time */ - -/* for reverse feeding of events */ -VARx(W *, rfeeds) -VARx(int, rfeedmax) -VARx(int, rfeedcnt) - -VAR (pendings, ANPENDING *pendings [NUMPRI]) -VAR (pendingmax, int pendingmax [NUMPRI]) -VAR (pendingcnt, int pendingcnt [NUMPRI]) -VARx(int, pendingpri) /* highest priority currently pending */ -VARx(ev_prepare, pending_w) /* dummy pending watcher */ - -VARx(ev_tstamp, io_blocktime) -VARx(ev_tstamp, timeout_blocktime) - -VARx(int, backend) -VARx(int, activecnt) /* total number of active events ("refcount") */ -VARx(EV_ATOMIC_T, loop_done) /* signal by ev_break */ - -VARx(int, backend_fd) -VARx(ev_tstamp, backend_mintime) /* assumed typical timer resolution */ -VAR (backend_modify, void (*backend_modify)(EV_P_ int fd, int oev, int nev)) -VAR (backend_poll , void (*backend_poll)(EV_P_ ev_tstamp timeout)) - -VARx(ANFD *, anfds) -VARx(int, anfdmax) - -VAR (evpipe, int evpipe [2]) -VARx(ev_io, pipe_w) -VARx(EV_ATOMIC_T, pipe_write_wanted) -VARx(EV_ATOMIC_T, pipe_write_skipped) - -#if !defined(_WIN32) || EV_GENWRAP -VARx(pid_t, curpid) -#endif - -VARx(char, postfork) /* true if we need to recreate kernel state after fork */ - -#if EV_USE_SELECT || EV_GENWRAP -VARx(void *, vec_ri) -VARx(void *, vec_ro) -VARx(void *, vec_wi) -VARx(void *, vec_wo) -#if defined(_WIN32) || EV_GENWRAP -VARx(void *, vec_eo) -#endif -VARx(int, vec_max) -#endif - -#if EV_USE_POLL || EV_GENWRAP -VARx(struct pollfd *, polls) -VARx(int, pollmax) -VARx(int, pollcnt) -VARx(int *, pollidxs) /* maps fds into structure indices */ -VARx(int, pollidxmax) -#endif - -#if EV_USE_EPOLL || EV_GENWRAP -VARx(struct epoll_event *, epoll_events) -VARx(int, epoll_eventmax) -VARx(int *, epoll_eperms) -VARx(int, epoll_epermcnt) -VARx(int, epoll_epermmax) -#endif - -#if EV_USE_KQUEUE || EV_GENWRAP -VARx(pid_t, kqueue_fd_pid) -VARx(struct kevent *, kqueue_changes) -VARx(int, kqueue_changemax) -VARx(int, kqueue_changecnt) -VARx(struct kevent *, kqueue_events) -VARx(int, kqueue_eventmax) -#endif - -#if EV_USE_PORT || EV_GENWRAP -VARx(struct port_event *, port_events) -VARx(int, port_eventmax) -#endif - -#if EV_USE_IOCP || EV_GENWRAP -VARx(HANDLE, iocp) -#endif - -VARx(int *, fdchanges) -VARx(int, fdchangemax) -VARx(int, fdchangecnt) - -VARx(ANHE *, timers) -VARx(int, timermax) -VARx(int, timercnt) - -#if EV_PERIODIC_ENABLE || EV_GENWRAP -VARx(ANHE *, periodics) -VARx(int, periodicmax) -VARx(int, periodiccnt) -#endif - -#if EV_IDLE_ENABLE || EV_GENWRAP -VAR (idles, ev_idle **idles [NUMPRI]) -VAR (idlemax, int idlemax [NUMPRI]) -VAR (idlecnt, int idlecnt [NUMPRI]) -#endif -VARx(int, idleall) /* total number */ - -VARx(struct ev_prepare **, prepares) -VARx(int, preparemax) -VARx(int, preparecnt) - -VARx(struct ev_check **, checks) -VARx(int, checkmax) -VARx(int, checkcnt) - -#if EV_FORK_ENABLE || EV_GENWRAP -VARx(struct ev_fork **, forks) -VARx(int, forkmax) -VARx(int, forkcnt) -#endif - -#if EV_CLEANUP_ENABLE || EV_GENWRAP -VARx(struct ev_cleanup **, cleanups) -VARx(int, cleanupmax) -VARx(int, cleanupcnt) -#endif - -#if EV_ASYNC_ENABLE || EV_GENWRAP -VARx(EV_ATOMIC_T, async_pending) -VARx(struct ev_async **, asyncs) -VARx(int, asyncmax) -VARx(int, asynccnt) -#endif - -#if EV_USE_INOTIFY || EV_GENWRAP -VARx(int, fs_fd) -VARx(ev_io, fs_w) -VARx(char, fs_2625) /* whether we are running in linux 2.6.25 or newer */ -VAR (fs_hash, ANFS fs_hash [EV_INOTIFY_HASHSIZE]) -#endif - -VARx(EV_ATOMIC_T, sig_pending) -#if EV_USE_SIGNALFD || EV_GENWRAP -VARx(int, sigfd) -VARx(ev_io, sigfd_w) -VARx(sigset_t, sigfd_set) -#endif - -VARx(unsigned int, origflags) /* original loop flags */ - -#if EV_FEATURE_API || EV_GENWRAP -VARx(unsigned int, loop_count) /* total number of loop iterations/blocks */ -VARx(unsigned int, loop_depth) /* #ev_run enters - #ev_run leaves */ - -VARx(void *, userdata) -/* C++ doesn't support the ev_loop_callback typedef here. stinks. */ -VAR (release_cb, void (*release_cb)(EV_P) EV_THROW) -VAR (acquire_cb, void (*acquire_cb)(EV_P) EV_THROW) -VAR (invoke_cb , ev_loop_callback invoke_cb) -#endif - -#undef VARx - diff --git a/third_party/libev/ev_win32.c b/third_party/libev/ev_win32.c deleted file mode 100644 index fd671356..00000000 --- a/third_party/libev/ev_win32.c +++ /dev/null @@ -1,162 +0,0 @@ -/* - * libev win32 compatibility cruft (_not_ a backend) - * - * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- - * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- - * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- - * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU General Public License ("GPL") version 2 or any later version, - * in which case the provisions of the GPL are applicable instead of - * the above. If you wish to allow the use of your version of this file - * only under the terms of the GPL and not to allow others to use your - * version of this file under the BSD license, indicate your decision - * by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL. If you do not delete the - * provisions above, a recipient may use your version of this file under - * either the BSD or the GPL. - */ - -#ifdef _WIN32 - -/* note: the comment below could not be substantiated, but what would I care */ -/* MSDN says this is required to handle SIGFPE */ -/* my wild guess would be that using something floating-pointy is required */ -/* for the crt to do something about it */ -volatile double SIGFPE_REQ = 0.0f; - -static SOCKET -ev_tcp_socket (void) -{ -#if EV_USE_WSASOCKET - return WSASocket (AF_INET, SOCK_STREAM, 0, 0, 0, 0); -#else - return socket (AF_INET, SOCK_STREAM, 0); -#endif -} - -/* oh, the humanity! */ -static int -ev_pipe (int filedes [2]) -{ - struct sockaddr_in addr = { 0 }; - int addr_size = sizeof (addr); - struct sockaddr_in adr2; - int adr2_size = sizeof (adr2); - SOCKET listener; - SOCKET sock [2] = { -1, -1 }; - - if ((listener = ev_tcp_socket ()) == INVALID_SOCKET) - return -1; - - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); - addr.sin_port = 0; - - if (bind (listener, (struct sockaddr *)&addr, addr_size)) - goto fail; - - if (getsockname (listener, (struct sockaddr *)&addr, &addr_size)) - goto fail; - - if (listen (listener, 1)) - goto fail; - - if ((sock [0] = ev_tcp_socket ()) == INVALID_SOCKET) - goto fail; - - if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) - goto fail; - - /* TODO: returns INVALID_SOCKET on winsock accept, not < 0. fix it */ - /* when convenient, probably by just removing error checking altogether? */ - if ((sock [1] = accept (listener, 0, 0)) < 0) - goto fail; - - /* windows vista returns fantasy port numbers for sockets: - * example for two interconnected tcp sockets: - * - * (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364 - * (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363 - * (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363 - * (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365 - * - * wow! tridirectional sockets! - * - * this way of checking ports seems to work: - */ - if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size)) - goto fail; - - if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size)) - goto fail; - - errno = WSAEINVAL; - if (addr_size != adr2_size - || addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */ - || addr.sin_port != adr2.sin_port) - goto fail; - - closesocket (listener); - -#if EV_SELECT_IS_WINSOCKET - filedes [0] = EV_WIN32_HANDLE_TO_FD (sock [0]); - filedes [1] = EV_WIN32_HANDLE_TO_FD (sock [1]); -#else - /* when select isn't winsocket, we also expect socket, connect, accept etc. - * to work on fds */ - filedes [0] = sock [0]; - filedes [1] = sock [1]; -#endif - - return 0; - -fail: - closesocket (listener); - - if (sock [0] != INVALID_SOCKET) closesocket (sock [0]); - if (sock [1] != INVALID_SOCKET) closesocket (sock [1]); - - return -1; -} - -#undef pipe -#define pipe(filedes) ev_pipe (filedes) - -#define EV_HAVE_EV_TIME 1 -ev_tstamp -ev_time (void) -{ - FILETIME ft; - ULARGE_INTEGER ui; - - GetSystemTimeAsFileTime (&ft); - ui.u.LowPart = ft.dwLowDateTime; - ui.u.HighPart = ft.dwHighDateTime; - - /* msvc cannot convert ulonglong to double... yes, it is that sucky */ - return (LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-7; -} - -#endif - diff --git a/third_party/libev/ev_wrap.h b/third_party/libev/ev_wrap.h deleted file mode 100644 index ad989ea7..00000000 --- a/third_party/libev/ev_wrap.h +++ /dev/null @@ -1,200 +0,0 @@ -/* DO NOT EDIT, automatically generated by update_ev_wrap */ -#ifndef EV_WRAP_H -#define EV_WRAP_H -#define acquire_cb ((loop)->acquire_cb) -#define activecnt ((loop)->activecnt) -#define anfdmax ((loop)->anfdmax) -#define anfds ((loop)->anfds) -#define async_pending ((loop)->async_pending) -#define asynccnt ((loop)->asynccnt) -#define asyncmax ((loop)->asyncmax) -#define asyncs ((loop)->asyncs) -#define backend ((loop)->backend) -#define backend_fd ((loop)->backend_fd) -#define backend_mintime ((loop)->backend_mintime) -#define backend_modify ((loop)->backend_modify) -#define backend_poll ((loop)->backend_poll) -#define checkcnt ((loop)->checkcnt) -#define checkmax ((loop)->checkmax) -#define checks ((loop)->checks) -#define cleanupcnt ((loop)->cleanupcnt) -#define cleanupmax ((loop)->cleanupmax) -#define cleanups ((loop)->cleanups) -#define curpid ((loop)->curpid) -#define epoll_epermcnt ((loop)->epoll_epermcnt) -#define epoll_epermmax ((loop)->epoll_epermmax) -#define epoll_eperms ((loop)->epoll_eperms) -#define epoll_eventmax ((loop)->epoll_eventmax) -#define epoll_events ((loop)->epoll_events) -#define evpipe ((loop)->evpipe) -#define fdchangecnt ((loop)->fdchangecnt) -#define fdchangemax ((loop)->fdchangemax) -#define fdchanges ((loop)->fdchanges) -#define forkcnt ((loop)->forkcnt) -#define forkmax ((loop)->forkmax) -#define forks ((loop)->forks) -#define fs_2625 ((loop)->fs_2625) -#define fs_fd ((loop)->fs_fd) -#define fs_hash ((loop)->fs_hash) -#define fs_w ((loop)->fs_w) -#define idleall ((loop)->idleall) -#define idlecnt ((loop)->idlecnt) -#define idlemax ((loop)->idlemax) -#define idles ((loop)->idles) -#define invoke_cb ((loop)->invoke_cb) -#define io_blocktime ((loop)->io_blocktime) -#define iocp ((loop)->iocp) -#define kqueue_changecnt ((loop)->kqueue_changecnt) -#define kqueue_changemax ((loop)->kqueue_changemax) -#define kqueue_changes ((loop)->kqueue_changes) -#define kqueue_eventmax ((loop)->kqueue_eventmax) -#define kqueue_events ((loop)->kqueue_events) -#define kqueue_fd_pid ((loop)->kqueue_fd_pid) -#define loop_count ((loop)->loop_count) -#define loop_depth ((loop)->loop_depth) -#define loop_done ((loop)->loop_done) -#define mn_now ((loop)->mn_now) -#define now_floor ((loop)->now_floor) -#define origflags ((loop)->origflags) -#define pending_w ((loop)->pending_w) -#define pendingcnt ((loop)->pendingcnt) -#define pendingmax ((loop)->pendingmax) -#define pendingpri ((loop)->pendingpri) -#define pendings ((loop)->pendings) -#define periodiccnt ((loop)->periodiccnt) -#define periodicmax ((loop)->periodicmax) -#define periodics ((loop)->periodics) -#define pipe_w ((loop)->pipe_w) -#define pipe_write_skipped ((loop)->pipe_write_skipped) -#define pipe_write_wanted ((loop)->pipe_write_wanted) -#define pollcnt ((loop)->pollcnt) -#define pollidxmax ((loop)->pollidxmax) -#define pollidxs ((loop)->pollidxs) -#define pollmax ((loop)->pollmax) -#define polls ((loop)->polls) -#define port_eventmax ((loop)->port_eventmax) -#define port_events ((loop)->port_events) -#define postfork ((loop)->postfork) -#define preparecnt ((loop)->preparecnt) -#define preparemax ((loop)->preparemax) -#define prepares ((loop)->prepares) -#define release_cb ((loop)->release_cb) -#define rfeedcnt ((loop)->rfeedcnt) -#define rfeedmax ((loop)->rfeedmax) -#define rfeeds ((loop)->rfeeds) -#define rtmn_diff ((loop)->rtmn_diff) -#define sig_pending ((loop)->sig_pending) -#define sigfd ((loop)->sigfd) -#define sigfd_set ((loop)->sigfd_set) -#define sigfd_w ((loop)->sigfd_w) -#define timeout_blocktime ((loop)->timeout_blocktime) -#define timercnt ((loop)->timercnt) -#define timermax ((loop)->timermax) -#define timers ((loop)->timers) -#define userdata ((loop)->userdata) -#define vec_eo ((loop)->vec_eo) -#define vec_max ((loop)->vec_max) -#define vec_ri ((loop)->vec_ri) -#define vec_ro ((loop)->vec_ro) -#define vec_wi ((loop)->vec_wi) -#define vec_wo ((loop)->vec_wo) -#else -#undef EV_WRAP_H -#undef acquire_cb -#undef activecnt -#undef anfdmax -#undef anfds -#undef async_pending -#undef asynccnt -#undef asyncmax -#undef asyncs -#undef backend -#undef backend_fd -#undef backend_mintime -#undef backend_modify -#undef backend_poll -#undef checkcnt -#undef checkmax -#undef checks -#undef cleanupcnt -#undef cleanupmax -#undef cleanups -#undef curpid -#undef epoll_epermcnt -#undef epoll_epermmax -#undef epoll_eperms -#undef epoll_eventmax -#undef epoll_events -#undef evpipe -#undef fdchangecnt -#undef fdchangemax -#undef fdchanges -#undef forkcnt -#undef forkmax -#undef forks -#undef fs_2625 -#undef fs_fd -#undef fs_hash -#undef fs_w -#undef idleall -#undef idlecnt -#undef idlemax -#undef idles -#undef invoke_cb -#undef io_blocktime -#undef iocp -#undef kqueue_changecnt -#undef kqueue_changemax -#undef kqueue_changes -#undef kqueue_eventmax -#undef kqueue_events -#undef kqueue_fd_pid -#undef loop_count -#undef loop_depth -#undef loop_done -#undef mn_now -#undef now_floor -#undef origflags -#undef pending_w -#undef pendingcnt -#undef pendingmax -#undef pendingpri -#undef pendings -#undef periodiccnt -#undef periodicmax -#undef periodics -#undef pipe_w -#undef pipe_write_skipped -#undef pipe_write_wanted -#undef pollcnt -#undef pollidxmax -#undef pollidxs -#undef pollmax -#undef polls -#undef port_eventmax -#undef port_events -#undef postfork -#undef preparecnt -#undef preparemax -#undef prepares -#undef release_cb -#undef rfeedcnt -#undef rfeedmax -#undef rfeeds -#undef rtmn_diff -#undef sig_pending -#undef sigfd -#undef sigfd_set -#undef sigfd_w -#undef timeout_blocktime -#undef timercnt -#undef timermax -#undef timers -#undef userdata -#undef vec_eo -#undef vec_max -#undef vec_ri -#undef vec_ro -#undef vec_wi -#undef vec_wo -#endif From 1ca304324b1f07c1cbb198c258cfcf71a94b57dd Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Wed, 1 Jul 2026 16:58:51 +0800 Subject: [PATCH 10/29] remove ci.yaml --- .github/workflows/ci.yaml | 78 --------------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml deleted file mode 100644 index a0573afd..00000000 --- a/.github/workflows/ci.yaml +++ /dev/null @@ -1,78 +0,0 @@ -name: ci - -# 编译级回归门禁:在 linux / macos / windows 三个 runner 上做编译验证。 -# 与 check.yaml(仅 PR 触发 + clang-format)互补,这里在每次 push 与手动触发时也跑, -# 任意一端编译失败即让流水线变红,挡住编译级回归。 -on: - push: - branches: [master] - pull_request: - branches: [master] - workflow_dispatch: - -# 同一分支/PR 的新提交会取消上一次仍在跑的任务,节省 runner 时间。 -concurrency: - group: ci-${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - linux: - name: build-linux - runs-on: ubuntu-latest - steps: - - name: checkout - uses: actions/checkout@v4 - # 与 dockerfile 保持一致:alpine + ninja + g++ 的权威编译环境。 - - name: build (docker) - run: docker build . - - macos: - name: build-macos - runs-on: macos-latest - steps: - - name: checkout - uses: actions/checkout@v4 - # macOS 走 kqueue 后端,依赖通过 homebrew 安装。 - - name: depends - run: brew update && brew install fmt poco spdlog openssl@3 - - name: build - run: | - if [ "$RUNNER_ARCH" == "ARM64" ]; then - export CPATH=/opt/homebrew/include - export LIBRARY_PATH=/opt/homebrew/lib - export OPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3 - else - export CPATH=/usr/local/include - export LIBRARY_PATH=/usr/local/lib - export OPENSSL_ROOT_DIR=/usr/local/opt/openssl@3 - fi - cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo - cmake --build build - - windows: - name: build-windows - runs-on: windows-latest - steps: - # Windows 当前为占位实现(reactor 为 IOCP/select stub),仅做编译验证。 - - name: depends - uses: msys2/setup-msys2@v2 - with: - msystem: MINGW64 - update: true - install: >- - mingw-w64-x86_64-cmake - mingw-w64-x86_64-ninja - mingw-w64-x86_64-gcc - mingw-w64-x86_64-spdlog - mingw-w64-x86_64-poco - mingw-w64-x86_64-openssl - - name: checkout - uses: actions/checkout@v4 - - name: cache - uses: actions/cache@v4 - with: - path: build - key: ${{ runner.os }}-${{ hashFiles('CMakeLists.txt') }} - - name: build - shell: msys2 {0} - run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo && cmake --build build From 09634c84f950f7af8d2f861f681dbc71f44ac5b5 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Wed, 1 Jul 2026 18:24:25 +0800 Subject: [PATCH 11/29] only userspace udp tcp --- candy/src/candy/client.cc | 2 - candy/src/core/client.cc | 10 - candy/src/core/client.h | 5 - candy/src/netstack/netstack.cc | 258 +-------------------- candy/src/netstack/netstack.h | 29 +-- candy/src/netstack/outbound.cc | 44 +--- candy/src/netstack/outbound.h | 87 +------ candy/src/netstack/router.cc | 37 --- candy/src/netstack/router.h | 68 ------ candy/src/netstack/session_icmp.cc | 252 -------------------- candy/src/netstack/session_icmp.h | 75 ------ candy/src/netstack/session_tcp.cc | 142 +----------- candy/src/netstack/session_tcp.h | 15 -- candy/src/netstack/session_udp.cc | 283 +---------------------- candy/src/netstack/session_udp.h | 24 -- candy/src/netstack/socks5_client.cc | 346 ---------------------------- candy/src/netstack/socks5_client.h | 134 ----------- candy/src/netstack/tcp_handshake.h | 49 ---- 18 files changed, 29 insertions(+), 1831 deletions(-) delete mode 100644 candy/src/netstack/router.cc delete mode 100644 candy/src/netstack/router.h delete mode 100644 candy/src/netstack/session_icmp.cc delete mode 100644 candy/src/netstack/session_icmp.h delete mode 100644 candy/src/netstack/socks5_client.cc delete mode 100644 candy/src/netstack/socks5_client.h delete mode 100644 candy/src/netstack/tcp_handshake.h diff --git a/candy/src/candy/client.cc b/candy/src/candy/client.cc index 9f5c286e..4143be2b 100644 --- a/candy/src/candy/client.cc +++ b/candy/src/candy/client.cc @@ -100,8 +100,6 @@ bool run(const std::string &id, const Poco::JSON::Object &config) { client->setPort(config.getValue("port")); client->setLocalhost(config.getValue("localhost")); client->setForwardMode(config.optValue("forward-mode", "kernel")); - client->setSocks5Upstream(config.optValue("socks5-upstream", "")); - client->setOutboundRules(config.optValue("outbound-rules", "")); client->run(); } candy::logger().information(Poco::format("run exit: id=%s ", id)); diff --git a/candy/src/core/client.cc b/candy/src/core/client.cc index 22c55f68..8c41a781 100644 --- a/candy/src/core/client.cc +++ b/candy/src/core/client.cc @@ -124,20 +124,10 @@ void Client::setForwardMode(const std::string &mode) { this->forwardMode = mode; } -void Client::setSocks5Upstream(const std::string &upstream) { - this->socks5Upstream = upstream; -} - -void Client::setOutboundRules(const std::string &rules) { - this->outboundRules = rules; -} - void Client::run() { this->running.store(true); if (this->forwardMode == "userspace") { - // 阶段三:在 NetStack 启动前注入 socks5 上游与分流规则(空时全部走 direct,零回归)。 - netstack.configureOutbounds(this->socks5Upstream, this->outboundRules); if (netstack.run(this)) { return; } diff --git a/candy/src/core/client.h b/candy/src/core/client.h index bfa1289b..2d16326d 100644 --- a/candy/src/core/client.h +++ b/candy/src/core/client.h @@ -44,9 +44,6 @@ class Client { void setVirtualMac(const std::string &vmac); void setForwardMode(const std::string &mode); - // 阶段三:外部 socks5 上游与分流规则(仅 userspace 模式生效,空表示全部 direct)。 - void setSocks5Upstream(const std::string &upstream); - void setOutboundRules(const std::string &rules); void run(); bool isRunning(); @@ -79,8 +76,6 @@ class Client { private: std::string tunName; std::string forwardMode = "kernel"; - std::string socks5Upstream; - std::string outboundRules; int mtu = 1400; }; diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index 3c0887c6..1a7d8f5e 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -2,14 +2,12 @@ #include "netstack/netstack.h" #include "core/client.h" #include "core/message.h" -#include "netstack/session_icmp.h" #include "netstack/session_tcp.h" #include "netstack/session_udp.h" #include #include #include #include -#include #include #include "lwip/init.h" @@ -22,153 +20,6 @@ namespace candy { -namespace { - -// 去除字符串首尾空白(含 \r,兼容 CRLF 配置文件)。 -std::string trimWs(const std::string &s) { - size_t b = s.find_first_not_of(" \t\r\n"); - if (b == std::string::npos) { - return ""; - } - size_t e = s.find_last_not_of(" \t\r\n"); - return s.substr(b, e - b + 1); -} - -// 按分隔符切分并去空白,丢弃空片段。 -std::vector splitTrim(const std::string &s, char sep) { - std::vector out; - std::string cur; - std::istringstream iss(s); - while (std::getline(iss, cur, sep)) { - std::string t = trimWs(cur); - if (!t.empty()) { - out.push_back(t); - } - } - return out; -} - -// 解析单条 socks5 上游:"socks5://[user:pass@]host:port"。成功返回 true。 -bool parseSocks5Url(const std::string &url, Endpoint &server, std::string &user, std::string &pass) { - const std::string scheme = "socks5://"; - if (url.compare(0, scheme.size(), scheme) != 0) { - return false; - } - std::string rest = url.substr(scheme.size()); - // 可选 user:pass@ - auto at = rest.find('@'); - if (at != std::string::npos) { - std::string cred = rest.substr(0, at); - rest = rest.substr(at + 1); - auto colon = cred.find(':'); - if (colon == std::string::npos) { - return false; - } - user = cred.substr(0, colon); - pass = cred.substr(colon + 1); - } - // host:port - auto colon = rest.rfind(':'); - if (colon == std::string::npos) { - return false; - } - std::string host = rest.substr(0, colon); - std::string portStr = rest.substr(colon + 1); - int port = 0; - try { - port = std::stoi(portStr); - } catch (...) { - return false; - } - if (port <= 0 || port > 65535) { - return false; - } - server.host = IP4(host); - server.port = (uint16_t)port; - return true; -} - -} // namespace - -void NetStack::configureOutbounds(const std::string &socks5Upstream, const std::string &outboundRules) { - // 空配置:不建任何 socks5/规则,getOutbound 恒返回 direct,行为完全等价阶段一/二。 - if (trimWs(socks5Upstream).empty() && trimWs(outboundRules).empty()) { - return; - } - - // 1) 解析 socks5 上游:"name=socks5://[user:pass@]host:port;..." - for (const auto &item : splitTrim(socks5Upstream, ';')) { - auto eq = item.find('='); - if (eq == std::string::npos) { - spdlog::warn("netstack: bad socks5-upstream entry: {}", item); - continue; - } - std::string name = trimWs(item.substr(0, eq)); - std::string url = trimWs(item.substr(eq + 1)); - Endpoint server{}; - std::string user, pass; - if (name.empty() || !parseSocks5Url(url, server, user, pass)) { - spdlog::warn("netstack: bad socks5-upstream url: {}", item); - continue; - } - this->outbounds[name] = std::make_unique(server, user, pass); - spdlog::info("netstack: socks5 outbound '{}' -> {}:{}", name, server.host.toString(), server.port); - } - - // 2) 解析分流规则:"dst-cidr:CIDR => name; dst-port:PORT => name; default => name;" - // 自上而下即匹配优先级,与 03 文档 §5.1 一致。 - for (const auto &line : splitTrim(outboundRules, ';')) { - auto arrow = line.find("=>"); - if (arrow == std::string::npos) { - spdlog::warn("netstack: bad outbound-rule (no =>): {}", line); - continue; - } - std::string cond = trimWs(line.substr(0, arrow)); - std::string target = trimWs(line.substr(arrow + 2)); - if (target.empty()) { - spdlog::warn("netstack: bad outbound-rule (empty target): {}", line); - continue; - } - Router::Rule rule{}; - rule.outbound = target; - if (cond == "default") { - this->router.setDefault(target); - spdlog::info("netstack: rule default => {}", target); - continue; - } - const std::string cidrPfx = "dst-cidr:"; - const std::string portPfx = "dst-port:"; - if (cond.compare(0, cidrPfx.size(), cidrPfx) == 0) { - std::string cidr = trimWs(cond.substr(cidrPfx.size())); - Address addr; - if (addr.fromCidr(cidr)) { - spdlog::warn("netstack: bad dst-cidr: {}", cidr); - continue; - } - rule.type = Router::MatchType::DstCidr; - rule.cidr = addr.Host(); - rule.mask = addr.Mask(); - this->router.addRule(rule); - spdlog::info("netstack: rule dst-cidr:{} => {}", cidr, target); - } else if (cond.compare(0, portPfx.size(), portPfx) == 0) { - std::string portStr = trimWs(cond.substr(portPfx.size())); - int port = 0; - try { - port = std::stoi(portStr); - } catch (...) { - spdlog::warn("netstack: bad dst-port: {}", portStr); - continue; - } - rule.type = Router::MatchType::DstPort; - rule.port = (uint16_t)port; - this->router.addRule(rule); - spdlog::info("netstack: rule dst-port:{} => {}", port, target); - } else { - spdlog::warn("netstack: unsupported rule condition: {}", cond); - } - } -} - NetStack::NetStack() : client(nullptr), running(false), listenPcb(nullptr), udpListenPcb(nullptr) { std::memset(&this->lwipNetif, 0, sizeof(this->lwipNetif)); } @@ -186,18 +37,8 @@ Reactor &NetStack::getReactor() { return this->reactor; } -Outbound &NetStack::getOutbound(const Router::FlowKey &flow) { - // 无规则时 router.match 恒返回兜底 "direct",直接复用 directOutbound,零回归。 - const std::string name = this->router.match(flow); - if (name == "direct" || name.empty()) { - return this->directOutbound; - } - auto it = this->outbounds.find(name); - if (it != this->outbounds.end() && it->second) { - return *it->second; - } - // 命中了一个未注册的出站名(配置不一致),安全兜底回 direct,避免丢流。 - spdlog::warn("netstack: outbound '{}' not found, fallback to direct", name); +Outbound &NetStack::getOutbound() { + // 当前仅支持内核 socket 直连落地。 return this->directOutbound; } @@ -338,13 +179,6 @@ void NetStack::teardownStack() { } } this->udpSessions.clear(); - // ICMP 伪会话同理清理。 - for (auto &kv : this->icmpSessions) { - if (kv.second) { - kv.second->shutdownFromStack(); - } - } - this->icmpSessions.clear(); { std::unique_lock lock(this->peerMapMutex); this->vnetPeerMap.clear(); @@ -380,11 +214,10 @@ void NetStack::loop() { } sys_check_timeouts(); - // 节流:每 10s 扫描一次 UDP/ICMP 伪会话,回收空闲超时项。 + // 节流:每 10s 扫描一次 UDP 伪会话,回收空闲超时项。 auto now = std::chrono::steady_clock::now(); if (now - lastReap >= std::chrono::seconds(10)) { reapIdleUdpSessions(); - reapIdleIcmpSessions(); lastReap = now; } } @@ -411,27 +244,6 @@ void NetStack::reapIdleUdpSessions() { } } -void NetStack::reapIdleIcmpSessions() { - // 仅 NetStack 线程访问 icmpSessions,无需加锁。 - // ICMP echo 间隔通常 1s:30s 无活动判定空闲,强制关闭并移除。 - // shutdownFromStack 仅置 closing 并交 reactor 关 fd,不修改 icmpSessions, - // 故可安全地在遍历后统一 erase。 - auto now = std::chrono::steady_clock::now(); - int reaped = 0; - for (auto it = this->icmpSessions.begin(); it != this->icmpSessions.end();) { - if (it->second && now - it->second->lastActive() > std::chrono::seconds(30)) { - it->second->shutdownFromStack(); - it = this->icmpSessions.erase(it); - ++reaped; - } else { - ++it; - } - } - if (reaped > 0) { - spdlog::debug("netstack reap idle icmp sessions: {} reaped, {} remain", reaped, this->icmpSessions.size()); - } -} - void NetStack::handleInput(std::string packet) { if (packet.size() < sizeof(IP4Header)) { return; @@ -448,11 +260,10 @@ void NetStack::handleInput(std::string packet) { IP4Header *inner = (IP4Header *)packet.data(); spdlog::debug("netstack input: vnetPeer={} {} -> {} proto={}", vnetPeer.toString(), inner->saddr.toString(), inner->daddr.toString(), (int)inner->protocol); - // ICMP 不走 lwIP(PRETEND netif 仅接受 TCP/UDP),在入栈前拦截并独立落地。 - if (inner->protocol == 0x01) { - if (handleIcmp(packet, vnetPeer)) { - return; - } + // 本次增量发版仅支持 userspace TCP/UDP 组网:非 TCP(0x06)/UDP(0x11) 一律丢弃 + // (ICMP 等待后续迭代再支持;PRETEND netif 本也只接受 TCP/UDP)。 + if (inner->protocol != 0x06 && inner->protocol != 0x11) { + return; } feedToLwip(packet, vnetPeer); } @@ -488,56 +299,6 @@ void NetStack::feedToLwip(const std::string &innerPacket, IP4 vnetPeer) { } } -bool NetStack::handleIcmp(const std::string &innerPacket, IP4 vnetPeer) { - // 仅处理 ICMP echo request(type 8):建立/复用伪会话,用内核 ICMP socket 落地。 - // 其余 ICMP(差错类等)暂不处理,返回 false 交回默认丢弃(不喂 lwIP)。 - IP4Header *ip = (IP4Header *)innerPacket.data(); - size_t ihl = (ip->version_ihl & 0x0f) * 4; - if (innerPacket.size() < ihl + 8) { - return false; - } - const uint8_t *icmp = (const uint8_t *)innerPacket.data() + ihl; - uint8_t type = icmp[0]; - if (type != 8) { // 仅 echo request - return false; - } - uint16_t icmpId = ((uint16_t)icmp[4] << 8) | icmp[5]; - - IP4 origSrc = ip->saddr; - IP4 origDst = ip->daddr; - - // 记录回包寻址:origSrc(dev1) -> vnetPeer(源网关虚拟IP),供 reply 经 output 回送。 - if (!vnetPeer.empty()) { - auto now = std::chrono::steady_clock::now(); - std::unique_lock lock(this->peerMapMutex); - this->vnetPeerMap[uint32_t(origSrc)] = PeerEntry{vnetPeer, now}; - } - - // key:(源,目的,icmpId)。复用已有会话或新建。 - std::string key; - key.assign((const char *)&origSrc, sizeof(uint32_t)); - key.append((const char *)&origDst, sizeof(uint32_t)); - key.append((const char *)&icmpId, sizeof(icmpId)); - - std::shared_ptr session; - auto sit = this->icmpSessions.find(key); - if (sit != this->icmpSessions.end()) { - session = sit->second; - } else { - session = std::make_shared(this, origSrc, origDst, icmpId); - if (session->start()) { - spdlog::debug("netstack icmp session start failed: {} -> {}", origSrc.toString(), origDst.toString()); - return true; // 已消费(落地失败也不喂 lwIP) - } - this->icmpSessions[key] = session; - } - - spdlog::debug("netstack handleIcmp: {} -> {} id={}", origSrc.toString(), origDst.toString(), icmpId); - // 把完整 ICMP 报文(含头与 payload)交给会话落地发送。 - session->sendEcho(innerPacket.substr(ihl)); - return true; -} - void NetStack::output(const std::string &innerPacket) { if (innerPacket.size() < sizeof(IP4Header)) { return; @@ -577,11 +338,6 @@ void NetStack::removeUdpSession(const std::string &key) { this->udpSessions.erase(key); } -void NetStack::removeIcmpSession(const std::string &key) { - // 仅 NetStack 线程调用,icmpSessions 无需加锁。 - this->icmpSessions.erase(key); -} - struct netif &NetStack::getNetif() { return this->lwipNetif; } diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index 85afc304..c1311c65 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -5,7 +5,6 @@ #include "core/net.h" #include "netstack/outbound.h" #include "netstack/reactor.h" -#include "netstack/router.h" #include #include #include @@ -28,7 +27,6 @@ namespace candy { class Client; class SessionTcp; class SessionUdp; -class SessionIcmp; // NetStack:独占 lwIP(NO_SYS=1 单线程 raw API)的模块。 // 持有一个 NetStack 线程驱动 lwIP 收发与定时器,一个 Reactor 线程管理落地 fd。 @@ -42,19 +40,14 @@ class NetStack { int wait(); void shutdown(); - // 阶段三:在 run() 之前注入 socks5 上游与分流规则(解析配置串,构建 Router + 出站注册表)。 - // 入参为空时不建任何规则/socks5,getOutbound 恒返回 direct,行为与阶段一/二完全一致。 - void configureOutbounds(const std::string &socks5Upstream, const std::string &outboundRules); - // 把待终结的 IP 包投递给 NetStack(来自 tun 落地分支,含 IPIP 外层)。 void input(std::string packet); // 供 Session 使用:在 NetStack 线程内执行任务(保证 lwIP API 线程安全)。 void postToStack(std::function task); Reactor &getReactor(); - // 按流选择出站:Router.match(flow) 命中的 outbound(direct / socks5)。 - // 无规则或命中 direct 时返回 DirectOutbound(与阶段一/二行为完全一致)。 - Outbound &getOutbound(const Router::FlowKey &flow); + // 落地拨号出站:当前仅支持内核 socket 直连(DirectOutbound)。 + Outbound &getOutbound(); // UDP 回包注入用:拿到本模块的 lwIP netif(仅 NetStack 线程使用)。 struct netif &getNetif(); @@ -65,8 +58,6 @@ class NetStack { void removeSession(struct tcp_pcb *pcb); // UDP 伪会话结束时从 UDP 会话表移除(按四元组 key)。仅 NetStack 线程调用。 void removeUdpSession(const std::string &key); - // ICMP 伪会话结束时从 ICMP 会话表移除(按 key)。仅 NetStack 线程调用。 - void removeIcmpSession(const std::string &key); Client *getClient(); @@ -76,13 +67,8 @@ class NetStack { void loop(); void handleInput(std::string packet); void feedToLwip(const std::string &innerPacket, IP4 vnetPeer); - // ICMP echo 在入栈前拦截:lwIP 的 PRETEND netif 只接受 TCP/UDP,ICMP 单独落地。 - // 返回 true 表示已被 ICMP 处理(不再喂 lwIP)。仅 NetStack 线程。 - bool handleIcmp(const std::string &innerPacket, IP4 vnetPeer); // UDP 伪会话空闲超时回收(仅 NetStack 线程,loop 中节流调用)。 void reapIdleUdpSessions(); - // ICMP 伪会话空闲超时回收(仅 NetStack 线程,loop 中节流调用)。 - void reapIdleIcmpSessions(); // lwIP 回调跳板 static err_t netifInitTrampoline(struct netif *netif); @@ -109,15 +95,8 @@ class NetStack { Reactor reactor; - // 落地出站:DirectOutbound(内核 socket 直连)恒在;阶段三再按 Router 规则选 socks5。 - // 无规则时 getOutbound 恒返回 directOutbound,保证现有 kernel/userspace 行为不变。 + // 落地出站:DirectOutbound(内核 socket 直连)。userspace 局域网组网的落地方式。 DirectOutbound directOutbound; - // 阶段三:分流规则引擎(空规则时 match 恒返回 "direct")。 - Router router; - // 命名出站注册表:name -> Socks5Outbound(由 configureOutbounds 从配置构建)。 - // direct 不入表(直接用 directOutbound)。仅在 run() 前 configureOutbounds 中写入, - // 之后只读,故无需加锁。 - std::unordered_map> outbounds; // NetStack 线程任务队列(用带超时读驱动定时器) std::mutex stackTaskMutex; @@ -137,8 +116,6 @@ class NetStack { std::unordered_map> sessions; // UDP 伪会话表:四元组 key -> SessionUdp。仅 NetStack 线程访问,无需加锁。 std::unordered_map> udpSessions; - // ICMP 伪会话表:(源,目的,icmpId) key -> SessionIcmp。仅 NetStack 线程访问,无需加锁。 - std::unordered_map> icmpSessions; }; } // namespace candy diff --git a/candy/src/netstack/outbound.cc b/candy/src/netstack/outbound.cc index 7e2a7461..a3c0ac4f 100644 --- a/candy/src/netstack/outbound.cc +++ b/candy/src/netstack/outbound.cc @@ -1,7 +1,6 @@ // SPDX-License-Identifier: MIT #include "netstack/outbound.h" #include "netstack/sockcompat.h" -#include "netstack/socks5_client.h" #include #if defined(__linux__) || defined(__APPLE__) @@ -13,15 +12,12 @@ namespace candy { -namespace { -// 共享的 TCP 非阻塞拨号:建 socket + 非阻塞 + 发起 connect 到 dst。 -// EINPROGRESS 视为成功,返回已发起 connect 的 fd;失败返回 -1。 -// direct 与 socks5 都用它(区别仅在 dst 是业务目的还是 socks5 server)。 -int dialTcpTo(const Endpoint &dst, const char *tag) { +int DirectOutbound::dialTcp(const Endpoint &dst) { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + // 建 socket + 非阻塞 + 发起 connect 到 dst。EINPROGRESS 视为成功。 int fd = (int)::socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { - spdlog::warn("{} tcp socket failed: {}", tag, netErrStr(netLastError())); + spdlog::warn("direct outbound tcp socket failed: {}", netErrStr(netLastError())); return -1; } netSetNonBlocking(fd); @@ -33,7 +29,8 @@ int dialTcpTo(const Endpoint &dst, const char *tag) { int ret = ::connect(fd, (struct sockaddr *)&addr, sizeof(addr)); if (ret != 0 && !netInProgress(netLastError())) { - spdlog::warn("{} tcp connect {}:{} failed: {}", tag, dst.host.toString(), dst.port, netErrStr(netLastError())); + spdlog::warn("direct outbound tcp connect {}:{} failed: {}", dst.host.toString(), dst.port, + netErrStr(netLastError())); netClose(fd); return -1; } @@ -42,11 +39,6 @@ int dialTcpTo(const Endpoint &dst, const char *tag) { return -1; #endif } -} // namespace - -int DirectOutbound::dialTcp(const Endpoint &dst) { - return dialTcpTo(dst, "direct outbound"); -} int DirectOutbound::dialUdp(const Endpoint &dst) { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) @@ -75,30 +67,4 @@ int DirectOutbound::dialUdp(const Endpoint &dst) { #endif } -Socks5Outbound::Socks5Outbound(Endpoint server, std::string username, std::string password) - : server(server), username(std::move(username)), password(std::move(password)) {} - -int Socks5Outbound::dialTcp(const Endpoint &dst) { - // 注意:socks5 拨号连接的是 server,而非业务目的 dst(dst 留给握手阶段 CONNECT)。 - (void)dst; - return dialTcpTo(this->server, "socks5 outbound"); -} - -std::unique_ptr Socks5Outbound::makeTcpHandshake(const Endpoint &dst) { - auto hs = std::make_unique(); - hs->startConnectIPv4(dst.host, dst.port, this->username, this->password); - return hs; -} - -int Socks5Outbound::dialUdpControl() { - // UDP ASSOCIATE 的控制连接:仍是到 socks5 server 的 TCP 连接(与 dialTcp 同目标)。 - return dialTcpTo(this->server, "socks5 udp control"); -} - -std::unique_ptr Socks5Outbound::makeUdpAssociate() { - auto hs = std::make_unique(); - hs->startUdpAssociate(this->username, this->password); - return hs; -} - } // namespace candy diff --git a/candy/src/netstack/outbound.h b/candy/src/netstack/outbound.h index 13edd05d..4c228c1c 100644 --- a/candy/src/netstack/outbound.h +++ b/candy/src/netstack/outbound.h @@ -3,9 +3,7 @@ #define CANDY_NETSTACK_OUTBOUND_H #include "core/net.h" -#include "netstack/tcp_handshake.h" #include -#include #include namespace candy { @@ -16,26 +14,22 @@ struct Endpoint { uint16_t port; }; -// Outbound:出站抽象。把"一条已被本机 lwIP 终结的流如何落地拨号"从 Session 中解耦, -// 使落地方式可替换(阶段一/二的内核 socket 直连收敛为 DirectOutbound,阶段三再加 -// Socks5Outbound / MeshOutbound)。 +// Outbound:出站抽象。把"一条已被本机 lwIP 终结的流如何落地拨号"从 Session 中解耦。 // -// 设计约束(承接 03 文档 §3,并与现有 Session 实现保持一致): +// 当前增量发版仅保留 userspace 局域网组网所需的内核 socket 直连落地(DirectOutbound), +// socks5/mesh 等出站方式待后续迭代再引入。 +// +// 设计约束(与现有 Session 实现保持一致): // - dialTcp/dialUdp 只负责"建 socket + 设非阻塞 + 发起 connect",返回一个已发起 // 连接的非阻塞 fd(失败返回 -1)。后续 reactor 注册、可读可写事件、双向 splice、 -// 背压等全部仍由 Session 自身处理 —— 因此本次抽取对运行时行为零影响。 -// - L4 终结型出站(direct/socks5)实现 dialTcp/dialUdp;needsTermination()==true。 -// - L3 转发型出站(mesh)不终结、走 forwardL3,dialTcp/dialUdp 返回 -1。 +// 背压等全部仍由 Session 自身处理。 class Outbound { public: virtual ~Outbound() = default; - // 出站名称(direct / socks5 / mesh)。 + // 出站名称(direct)。 virtual std::string name() const = 0; - // 是否需要发起端 lwIP 终结:direct/socks5 为 true,mesh 为 false。 - virtual bool needsTermination() const = 0; - // L4 终结型出站:为一条已终结的 TCP 流建立落地通道,返回已发起 connect 的非阻塞 // fd(EINPROGRESS 视为成功),失败返回 -1。默认不支持。 virtual int dialTcp(const Endpoint &dst) { return -1; } @@ -43,83 +37,18 @@ class Outbound { // L4 终结型出站:为一条已终结的 UDP 流建立 connect 到目的的非阻塞 fd,失败返回 -1。 // 默认不支持。 virtual int dialUdp(const Endpoint &dst) { return -1; } - - // 为本出站的一条 TCP 流创建"连接建立后、splice 之前"的应用层握手器。 - // direct/mesh 无需握手,返回 nullptr(Session 据此走原有直连 splice 逻辑,字节不变); - // socks5 返回一个已按目的地址初始化的 Socks5Client。 - // dst:业务真实目的(socks5 CONNECT 的目标,而非 socks5 server 地址)。 - virtual std::unique_ptr makeTcpHandshake(const Endpoint &dst) { return nullptr; } - - // UDP 是否需要先在 TCP 控制连接上做 UDP ASSOCIATE 关联:direct 为 false(内核 socket - // 直连即可);socks5 为 true(需先建控制连接握手、拿到中继端点,再经中继收发封装报文)。 - virtual bool udpNeedsAssociation() const { return false; } - - // socks5 UDP 关联用:拨号到 socks5 server 的 TCP 控制连接(与 dialTcp 同一目标 server, - // 但语义独立)。返回已发起 connect 的非阻塞 fd,失败返回 -1。默认不支持。 - virtual int dialUdpControl() { return -1; } - - // socks5 UDP 关联用:创建在控制连接上跑 UDP ASSOCIATE 命令的握手器(complete 后由 - // udpRelayHost()/udpRelayPort() 取中继端点)。默认返回 nullptr。 - virtual std::unique_ptr makeUdpAssociate() { return nullptr; } - - // socks5 UDP 关联用:代理服务端端点。当 UDP ASSOCIATE 应答的中继地址为 0.0.0.0 时, - // 按 RFC 1928 §7 回退到控制连接的服务端 IP。非 socks5 出站返回 0.0.0.0:0(不使用)。 - virtual Endpoint proxyServerEndpoint() const { return Endpoint{IP4("0.0.0.0"), 0}; } }; -// DirectOutbound:内核 socket 直连落地(阶段一/二的拨号逻辑迁移至此)。 +// DirectOutbound:内核 socket 直连落地。 // 源地址由内核在发起连接时自动填为本网关出口 IP,等价 MASQUERADE。 class DirectOutbound : public Outbound { public: std::string name() const override { return "direct"; } - bool needsTermination() const override { return true; } int dialTcp(const Endpoint &dst) override; int dialUdp(const Endpoint &dst) override; }; -// MeshOutbound:L3 转发型出口(自建节点 mesh)。发起端不终结,按 L3 原样封 IPIP 经 -// candy 虚拟网送到远端落地网关,由其在落地端用 lwIP 终结。保留 ICMP。 -// -// 当前为阶段三骨架:现有 tun.cc::handlePacket 的 IPIP 封装/直转路径已实现等价行为, -// 此处先建立类型占位,待 Router 接入热路径后再把 forwardL3 与现有路径收敛。 -class MeshOutbound : public Outbound { -public: - std::string name() const override { return "mesh"; } - bool needsTermination() const override { return false; } - // L4 拨号对 mesh 无意义:mesh 不在发起端终结,dialTcp/dialUdp 保持默认返回 -1。 -}; - -// Socks5Outbound:外部 socks5 代理落地。dialTcp 连接的是 socks5 server(而非业务目的), -// 连上后由 makeTcpHandshake 返回的 Socks5Client 在通道上完成 CONNECT 握手, -// 之后 Session 把 lwIP pcb <-> socks5 连接做双向 splice。needsTermination()==true。 -// 协议限制:不支持 ICMP;UDP(UDP ASSOCIATE)后续单独实现,此处先支持 TCP。 -class Socks5Outbound : public Outbound { -public: - // server:socks5 服务端地址;username/password 为空表示无认证。 - Socks5Outbound(Endpoint server, std::string username = "", std::string password = ""); - - std::string name() const override { return "socks5"; } - bool needsTermination() const override { return true; } - - // 连接 socks5 server(而非业务目的 dst)。 - int dialTcp(const Endpoint &dst) override; - - // 返回针对业务目的 dst 的 socks5 CONNECT 握手器。 - std::unique_ptr makeTcpHandshake(const Endpoint &dst) override; - - // UDP 走 UDP ASSOCIATE:需要先建 TCP 控制连接并握手拿到中继端点。 - bool udpNeedsAssociation() const override { return true; } - int dialUdpControl() override; - std::unique_ptr makeUdpAssociate() override; - Endpoint proxyServerEndpoint() const override { return this->server; } - -private: - Endpoint server; - std::string username; - std::string password; -}; - } // namespace candy #endif diff --git a/candy/src/netstack/router.cc b/candy/src/netstack/router.cc deleted file mode 100644 index e7ba6aba..00000000 --- a/candy/src/netstack/router.cc +++ /dev/null @@ -1,37 +0,0 @@ -// SPDX-License-Identifier: MIT -#include "netstack/router.h" - -namespace candy { - -Router::Router() : defaultOutbound("direct") {} - -void Router::addRule(const Rule &rule) { - this->rules.push_back(rule); -} - -void Router::setDefault(const std::string &outbound) { - this->defaultOutbound = outbound; -} - -std::string Router::match(const FlowKey &flow) const { - for (const auto &rule : this->rules) { - switch (rule.type) { - case MatchType::DstCidr: - // dst & mask == cidr & mask 即视为命中网段。 - if ((flow.dst & rule.mask) == (rule.cidr & rule.mask)) { - return rule.outbound; - } - break; - case MatchType::DstPort: - if (flow.dstPort == rule.port) { - return rule.outbound; - } - break; - case MatchType::Default: - return rule.outbound; - } - } - return this->defaultOutbound; -} - -} // namespace candy diff --git a/candy/src/netstack/router.h b/candy/src/netstack/router.h deleted file mode 100644 index 1fed4073..00000000 --- a/candy/src/netstack/router.h +++ /dev/null @@ -1,68 +0,0 @@ -// SPDX-License-Identifier: MIT -#ifndef CANDY_NETSTACK_ROUTER_H -#define CANDY_NETSTACK_ROUTER_H - -#include "core/net.h" -#include -#include -#include - -namespace candy { - -// Router:规则引擎(阶段三)。在 tun 入口、lwIP 终结之前,按目的网段/端口/兜底规则 -// 为一条流选择 outbound(mesh / direct / external-socks5)。 -// -// 匹配只依赖 L3 包头即可完成的字段(origSrc/origDst/proto/dstPort),与 03 文档 §5.2 -// 一致:分流发生在终结之前,只有命中需要本机终结的出口才进入 lwIP。 -// -// 当前为阶段三骨架:规则容器与 match 已就绪,但尚未接入 tun.cc 热路径,default 兜底 -// 返回 "direct",因此对现有 kernel/userspace 行为零影响。后续接入时由配置注入规则。 -class Router { -public: - // 匹配输入:从原始 IP 包头解析得到的 L3 信息(无需终结)。 - struct FlowKey { - IP4 src; - IP4 dst; - uint8_t proto; - uint16_t dstPort; - }; - - // 规则条件类型(按 03 文档 §5.1,自上而下优先匹配)。 - enum class MatchType { - DstCidr, // 目的网段:dst ∈ cidr - DstPort, // 目的端口 - Default, // 兜底 - }; - - struct Rule { - MatchType type; - // DstCidr 时生效:网段与掩码。 - IP4 cidr; - IP4 mask; - // DstPort 时生效。 - uint16_t port; - // 命中后选用的 outbound 名称(direct / mesh / socks5 / 组名)。 - std::string outbound; - }; - - Router(); - - // 追加一条规则(按追加顺序即为匹配优先级)。 - void addRule(const Rule &rule); - - // 设置兜底 outbound(无显式 Default 规则时使用)。 - void setDefault(const std::string &outbound); - - // 为一条流选择 outbound 名称;无任何规则命中时返回兜底(默认 "direct")。 - std::string match(const FlowKey &flow) const; - - bool empty() const { return this->rules.empty(); } - -private: - std::vector rules; - std::string defaultOutbound; -}; - -} // namespace candy - -#endif diff --git a/candy/src/netstack/session_icmp.cc b/candy/src/netstack/session_icmp.cc deleted file mode 100644 index 69c287ce..00000000 --- a/candy/src/netstack/session_icmp.cc +++ /dev/null @@ -1,252 +0,0 @@ -// SPDX-License-Identifier: MIT -#include "netstack/session_icmp.h" -#include "netstack/netstack.h" -#include -#include - -#if defined(__linux__) || defined(__APPLE__) -#include -#include -#include -#include -#include -#endif - -namespace candy { - -namespace { - -// 计算 16 位反码和校验(IP/ICMP 通用)。data 长度可奇可偶。 -uint16_t checksum16(const uint8_t *data, size_t len) { - uint32_t sum = 0; - while (len > 1) { - sum += (uint16_t)((data[0] << 8) | data[1]); - data += 2; - len -= 2; - } - if (len > 0) { - sum += (uint16_t)(data[0] << 8); - } - while (sum >> 16) { - sum = (sum & 0xffff) + (sum >> 16); - } - return (uint16_t)(~sum); -} - -constexpr size_t ICMP_HDR_LEN = 8; // type+code+checksum+id+seq - -} // namespace - -SessionIcmp::SessionIcmp(NetStack *stack, IP4 origSrc, IP4 origDst, uint16_t icmpId) - : Session(stack), fd(-1), rawMode(false), origSrc(origSrc), origDst(origDst), icmpId(icmpId), - lastActiveTs(std::chrono::steady_clock::now()) { - // key:源IP + 目的IP + icmp id(同一 ping 进程对同一目的复用一个落地 fd)。 - this->sessionKey.assign((const char *)&origSrc, sizeof(uint32_t)); - this->sessionKey.append((const char *)&origDst, sizeof(uint32_t)); - this->sessionKey.append((const char *)&icmpId, sizeof(icmpId)); -} - -SessionIcmp::~SessionIcmp() { - if (this->fd >= 0) { - ::close(this->fd); - this->fd = -1; - } -} - -int SessionIcmp::start() { -#if defined(__linux__) || defined(__APPLE__) - this->self = shared_from_this(); - - // 优先非特权 DGRAM ICMP socket(需 ping_group_range 覆盖运行用户); - // 失败退化为 RAW ICMP socket(需 root / CAP_NET_RAW)。 - this->fd = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); - if (this->fd < 0) { - this->fd = ::socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); - this->rawMode = true; - } - if (this->fd < 0) { - spdlog::warn("session icmp socket failed: {}", strerror(errno)); - return -1; - } - int flags = ::fcntl(this->fd, F_GETFL, 0); - ::fcntl(this->fd, F_SETFL, flags | O_NONBLOCK); - - // connect 固定对端:内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE)。 - struct sockaddr_in dst = {}; - dst.sin_family = AF_INET; - dst.sin_addr.s_addr = uint32_t(this->origDst); - if (::connect(this->fd, (struct sockaddr *)&dst, sizeof(dst)) != 0) { - spdlog::warn("session icmp connect {} failed: {}", this->origDst.toString(), strerror(errno)); - ::close(this->fd); - this->fd = -1; - return -1; - } - - spdlog::debug("session icmp: {} -> {} id={} {}", this->origSrc.toString(), this->origDst.toString(), this->icmpId, - this->rawMode ? "raw" : "dgram"); - - auto holder = shared_from_this(); - int fd = this->fd; - this->stack->getReactor().add(fd, ReactorEvent::READ, [holder](ReactorEvent ev) { holder->onFdEvent((uint32_t)ev); }); - return 0; -#else - return -1; -#endif -} - -// ===================== NetStack 线程 ===================== - -void SessionIcmp::sendEcho(std::string icmpData) { - // 仅 NetStack 线程调用:刷新活跃时间,把 echo request 报文投递到 reactor 发送。 - // icmpData 是完整 ICMP 报文(type/code/checksum/id/seq/payload)。 - // DGRAM ICMP socket:内核会忽略并改写 id/checksum,按 fd 配对回包。 - this->lastActiveTs = std::chrono::steady_clock::now(); - auto holder = shared_from_this(); - this->stack->getReactor().post([holder, data = std::move(icmpData)]() mutable { -#if defined(__linux__) || defined(__APPLE__) - if (holder->fd < 0 || holder->closing.load()) { - return; - } - ssize_t n = ::send(holder->fd, data.data(), data.size(), 0); - if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { - spdlog::debug("session icmp send failed: {}", strerror(errno)); - } -#endif - }); -} - -void SessionIcmp::replyToPeer(std::string icmpReply) { - // 仅 NetStack 线程调用:icmpReply 是落地 fd 收到的 echo reply 的 ICMP 报文。 - // DGRAM ICMP socket 回包里 id 是内核分配值,需还原成源端 origIcmpId; - // 然后重算 ICMP 校验和,再手工封 IP 头(源=origDst, 目的=origSrc), - // 交 NetStack::output 封 IPIP 回源端。 - if (icmpReply.size() < ICMP_HDR_LEN) { - return; - } - this->lastActiveTs = std::chrono::steady_clock::now(); - - uint8_t *icmp = (uint8_t *)icmpReply.data(); - // 还原 icmp id(偏移 4..5),并把 type 规整为 echo reply(0)。 - icmp[0] = 0; // echo reply - icmp[1] = 0; // code - icmp[4] = (uint8_t)(this->icmpId >> 8); - icmp[5] = (uint8_t)(this->icmpId & 0xff); - // 重算 ICMP 校验和(偏移 2..3 先清零)。 - icmp[2] = 0; - icmp[3] = 0; - uint16_t csum = checksum16(icmp, icmpReply.size()); - icmp[2] = (uint8_t)(csum >> 8); - icmp[3] = (uint8_t)(csum & 0xff); - - // 手工封 IP 头:源=origDst(dev2),目的=origSrc(dev1),协议=ICMP(1)。 - std::string packet; - packet.resize(sizeof(IP4Header) + icmpReply.size()); - IP4Header *ip = (IP4Header *)packet.data(); - std::memset(ip, 0, sizeof(IP4Header)); - ip->version_ihl = 0x45; - ip->tos = 0; - ip->tot_len = hton((uint16_t)(sizeof(IP4Header) + icmpReply.size())); - ip->id = 0; - ip->frag_off = 0; - ip->ttl = 64; - ip->protocol = 0x01; // ICMP - ip->check = 0; - ip->saddr = this->origDst; - ip->daddr = this->origSrc; - uint16_t ipsum = checksum16((const uint8_t *)ip, sizeof(IP4Header)); - ip->check = hton(ipsum); - - std::memcpy(packet.data() + sizeof(IP4Header), icmpReply.data(), icmpReply.size()); - - // 经 NetStack::output 按 origSrc 查 vnetPeer、封 IPIP 回源端。 - this->stack->output(packet); -} - -void SessionIcmp::shutdownFromStack() { - // 栈拆除/空闲回收:fd 交由 reactor 关闭并释放自持。 - if (!this->closing.exchange(true)) { - auto holder = shared_from_this(); - this->stack->getReactor().post([holder] { -#if defined(__linux__) || defined(__APPLE__) - if (holder->fd >= 0) { - holder->stack->getReactor().del(holder->fd); - ::close(holder->fd); - holder->fd = -1; - } -#endif - holder->self.reset(); - }); - } -} - -// ===================== Reactor 线程 ===================== - -void SessionIcmp::onFdEvent(uint32_t events) { - ReactorEvent ev = (ReactorEvent)events; - if (ev & ReactorEvent::ERROR) { - closeFromReactor(); - return; - } - if (ev & ReactorEvent::READ) { - onFdReadable(); - } -} - -void SessionIcmp::onFdReadable() { -#if defined(__linux__) || defined(__APPLE__) - char buf[65536]; - while (true) { - ssize_t n = ::recv(this->fd, buf, sizeof(buf), 0); - if (n > 0) { - size_t off = 0; - // 按首字节探测是否带 IP 头,跨平台健壮: - // - RAW ICMP socket(Linux/mac):recv 始终返回含 IP 头的整包; - // - macOS 的 DGRAM ICMP socket:也返回含 IP 头的整包(与 Linux 不同); - // - Linux 的 DGRAM ICMP socket:返回纯 ICMP 报文(首字节=type,echo reply=0)。 - // IPv4 头首字节高 4 位为版本号 4(即 0x4X),而 ICMP echo reply 首字节(type)恒为 0, - // 二者不会混淆,故用 version==4 判定是否需要剥头,避免依赖 rawMode 漏判 mac dgram。 - if (n >= (ssize_t)sizeof(IP4Header) && (((uint8_t *)buf)[0] >> 4) == 4) { - uint8_t ihl = (((uint8_t *)buf)[0] & 0x0f) * 4; - if ((ssize_t)ihl <= n) { - off = ihl; - } - } - std::string data(buf + off, n - off); - auto holder = shared_from_this(); - this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->replyToPeer(std::move(data)); }); - continue; - } - if (n == 0) { - return; - } - if (errno == EAGAIN || errno == EWOULDBLOCK) { - return; - } - // 落地 socket 错误(如目的不可达):关闭会话。 - closeFromReactor(); - return; - } -#endif -} - -void SessionIcmp::closeFromReactor() { - if (this->closing.exchange(true)) { - return; - } -#if defined(__linux__) || defined(__APPLE__) - if (this->fd >= 0) { - this->stack->getReactor().del(this->fd); - ::close(this->fd); - this->fd = -1; - } -#endif - NetStack *stack = this->stack; - std::string k = this->sessionKey; - auto holder = shared_from_this(); - stack->postToStack([holder, stack, k] { - stack->removeIcmpSession(k); - holder->self.reset(); - }); -} - -} // namespace candy diff --git a/candy/src/netstack/session_icmp.h b/candy/src/netstack/session_icmp.h deleted file mode 100644 index fd56a8b6..00000000 --- a/candy/src/netstack/session_icmp.h +++ /dev/null @@ -1,75 +0,0 @@ -// SPDX-License-Identifier: MIT -#ifndef CANDY_NETSTACK_SESSION_ICMP_H -#define CANDY_NETSTACK_SESSION_ICMP_H - -#include "core/net.h" -#include "netstack/session.h" -#include -#include -#include -#include -#include - -namespace candy { - -// SessionIcmp:一条 ICMP echo "伪会话"——把某个 (源,目的,icmp-id) 的 ping 流, -// 映射到一个 connect 到目的的内核 ICMP socket 落地(等价 iptables MASQUERADE)。 -// -// 与 TCP/UDP 不同,ICMP 不走 lwIP:lwIP 的 PRETEND netif 只接受 TCP/UDP, -// 故 ICMP echo 在入栈前(NetStack::handleInput)被直接拦截,由本会话落地+回包。 -// -// 线程契约(与 SessionUdp 一致): -// - 所有跨线程交互通过 NetStack::postToStack / Reactor::post; -// - fd 的 send/recv 只在 Reactor 线程;回包构造在 NetStack 线程。 -// -// 数据流: -// 正向:handleIcmp(NetStack线程) 提取 echo request -> 投递 reactor send(fd) -// 反向:fd 可读(reactor线程) recv echo reply -> 投递 NetStack 还原 id/校验和 -// -> 手工封 IP 包 -> NetStack::output 封 IPIP 回源端 -class SessionIcmp : public Session, public std::enable_shared_from_this { -public: - // origSrc/origDst:源端(dev1)/落地目的(dev2);icmpId:源端 ping 进程的 icmp id。 - SessionIcmp(NetStack *stack, IP4 origSrc, IP4 origDst, uint16_t icmpId); - ~SessionIcmp() override; - - int start() override; - - // 仅供 NetStack 线程在栈拆除/空闲回收时调用,强制关闭 fd。 - void shutdownFromStack(); - - // NetStack 线程:把一份 echo request 的 ICMP 报文(含 icmp 头)投递到落地 fd 发送。 - void sendEcho(std::string icmpData); - - // 该会话的 key(NetStack 线程的会话表用)。 - const std::string &key() const { return this->sessionKey; } - - // 最近活跃时间(空闲超时回收,仅 NetStack 线程访问)。 - std::chrono::steady_clock::time_point lastActive() const { return this->lastActiveTs; } - -private: - // ---- Reactor 线程 ---- - void onFdEvent(uint32_t events); - void onFdReadable(); - void closeFromReactor(); - - // ---- NetStack 线程 ---- - // 落地 fd 收到 echo reply 后:还原原始 icmp id、重算校验和、封 IP 头回送源端。 - void replyToPeer(std::string icmpReply); - -private: - int fd; - bool rawMode; - - IP4 origSrc; - IP4 origDst; - uint16_t icmpId; - - std::string sessionKey; - std::chrono::steady_clock::time_point lastActiveTs; - - std::shared_ptr self; -}; - -} // namespace candy - -#endif diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc index 5f6e4f0c..619425fe 100644 --- a/candy/src/netstack/session_tcp.cc +++ b/candy/src/netstack/session_tcp.cc @@ -46,22 +46,14 @@ int SessionTcp::start() { tcp_err(this->pcb, errTrampoline); #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - // 按流分流:Router.match(origSrc/origDst/proto/dstPort) 选出 outbound(direct/socks5)。 - // 无规则时恒为 DirectOutbound,与阶段一/二行为完全一致(零回归)。 - Router::FlowKey flow{this->origSrc, this->origDst, IPPROTO_TCP, this->origDstPort}; - Outbound &outbound = this->stack->getOutbound(flow); - - // 落地拨号交给选中的 Outbound(direct: 连目的;socks5: 连 server)。 - // 返回已发起 connect(EINPROGRESS 视为成功)的非阻塞 fd,后续 splice/背压逻辑不变。 + // 落地拨号:内核 socket 直连业务目的(DirectOutbound)。 + // 返回已发起 connect(EINPROGRESS 视为成功)的非阻塞 fd。 + Outbound &outbound = this->stack->getOutbound(); this->fd = outbound.dialTcp(Endpoint{this->origDst, this->origDstPort}); if (this->fd < 0) { return -1; } - // 询问 outbound 是否需要应用层握手(socks5 返回 Socks5Client;direct 返回 nullptr)。 - // 为空时后续走原有直连 splice 路径,字节完全不变(direct 零回归的关键)。 - this->handshake = outbound.makeTcpHandshake(Endpoint{this->origDst, this->origDstPort}); - spdlog::debug("session tcp: {} -> {}:{}", this->origSrc.toString(), this->origDst.toString(), this->origDstPort); auto holder = shared_from_this(); @@ -307,21 +299,6 @@ void SessionTcp::onFdEvent(uint32_t events) { } // 已连接:优先把可读数据/EOF 读干净(read 返回 0 走优雅排空,返回 -1 走关闭), // 避免 ERROR/HUP 与可读数据同时到达时抢先关闭而丢失尾部数据。 - // 握手阶段(handshake 非空):事件分流到握手处理,握手完成后置空,转入下方 splice。 - if (this->handshake) { - if (ev & ReactorEvent::ERROR) { - closeFromReactor(); - return; - } - if (ev & ReactorEvent::READ) { - onHandshakeReadable(); - } - // onHandshakeReadable 内可能完成握手并置空 handshake;若仍在握手则处理可写。 - if (this->handshake && (ev & ReactorEvent::WRITE)) { - onHandshakeWritable(); - } - return; - } if (ev & (ReactorEvent::READ | ReactorEvent::ERROR)) { onFdReadable(); } @@ -346,113 +323,6 @@ void SessionTcp::onConnected() { return; } this->connected = true; - // socks5 等需要握手:先进入握手阶段(在落地通道上完成 CONNECT),完成后再 splice。 - // direct 无握手(handshake==nullptr),走下方原有 splice 路径,行为字节不变。 - if (this->handshake) { - spdlog::debug("session tcp handshake begin {} -> {}:{}", this->origSrc.toString(), this->origDst.toString(), - this->origDstPort); - // 注册读写:先把握手首包(auth methods)写出,并等待服务端应答。 - this->stack->getReactor().mod(this->fd, ReactorEvent::READ | ReactorEvent::WRITE); - flushHandshakeOutbound(); - return; - } - this->stack->getReactor().mod(this->fd, ReactorEvent::READ); - flushForwardLocked(); -#endif -} - -// ===================== 握手阶段(Reactor 线程) ===================== - -void SessionTcp::flushHandshakeOutbound() { -#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - if (!this->handshake || this->fd < 0) { - return; - } - // 先把握手器新产生的待发字节拼到残留 outbox 后面(保证发送顺序)。 - if (this->handshake->hasOutbound()) { - std::vector out = this->handshake->takeOutbound(); - this->handshakeOutbox.append(reinterpret_cast(out.data()), out.size()); - } - // 非阻塞写出 outbox;部分写则保留剩余,注册 WRITE 等待下次可写。 - while (!this->handshakeOutbox.empty()) { - long n = netSend(this->fd, this->handshakeOutbox.data(), this->handshakeOutbox.size()); - if (n > 0) { - this->handshakeOutbox.erase(0, n); - continue; - } - if (n < 0 && netWouldBlock(netLastError())) { - this->stack->getReactor().mod(this->fd, ReactorEvent::READ | ReactorEvent::WRITE); - return; - } - spdlog::warn("session tcp handshake send failed: {}", netErrStr(netLastError())); - closeFromReactor(); - return; - } - // outbox 已排空:只需等待服务端应答,去掉 WRITE 关注。 - this->stack->getReactor().mod(this->fd, ReactorEvent::READ); -#endif -} - -void SessionTcp::onHandshakeWritable() { - flushHandshakeOutbound(); -} - -void SessionTcp::onHandshakeReadable() { -#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - if (!this->handshake) { - return; - } - char buf[4096]; - while (true) { - long n = netRecv(this->fd, buf, sizeof(buf)); - if (n > 0) { - if (!this->handshake->feed(reinterpret_cast(buf), (size_t)n)) { - spdlog::warn("session tcp socks5 handshake failed: {}", this->handshake->error()); - closeFromReactor(); - return; - } - if (this->handshake->done()) { - finishHandshake(); - return; - } - // 未完成:握手器可能产生了下一段待发字节(如收到认证应答后要发 CONNECT 请求)。 - if (this->handshake->hasOutbound()) { - flushHandshakeOutbound(); - } - continue; - } - if (n == 0) { - spdlog::warn("session tcp socks5 server closed during handshake"); - closeFromReactor(); - return; - } - if (netWouldBlock(netLastError())) { - return; - } - spdlog::warn("session tcp handshake recv failed: {}", netErrStr(netLastError())); - closeFromReactor(); - return; - } -#endif -} - -void SessionTcp::finishHandshake() { -#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - // 握手成功:取走应答后粘连的业务数据(若有),当作 fd 先收到的反向数据灌给 lwIP, - // 避免丢失。然后销毁握手器,转入与 direct 完全一致的 splice 路径。 - std::vector leftover = this->handshake->takeLeftover(); - spdlog::debug("session tcp socks5 handshake done {} -> {}:{} leftover={}", this->origSrc.toString(), - this->origDst.toString(), this->origDstPort, leftover.size()); - this->handshake.reset(); - - if (!leftover.empty()) { - this->backwardBytes.fetch_add(leftover.size()); - std::string data(reinterpret_cast(leftover.data()), leftover.size()); - auto holder = shared_from_this(); - this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->writeToLwip(data); }); - } - // 进入正常 splice:只关注可读(与 onConnected 的 direct 分支一致),并把握手期间 - // lwIP 侧已缓冲的待发数据(forwardBuf)灌向落地 fd。 this->stack->getReactor().mod(this->fd, ReactorEvent::READ); flushForwardLocked(); #endif @@ -463,12 +333,6 @@ void SessionTcp::flushForwardLocked() { if (this->fd < 0 || !this->connected) { return; } - // 握手未完成时严禁向落地 fd 写业务数据:此时 fd 上跑的是 socks5 握手字节流, - // 若把 lwIP 侧已到达的业务数据(如 HTTP 请求)插进去会破坏协议。业务数据先在 - // forwardBuf 里缓冲,待 finishHandshake() 完成后再由其调用本函数统一灌出。 - if (this->handshake) { - return; - } size_t acked = 0; size_t remain = 0; bool blocked = false; diff --git a/candy/src/netstack/session_tcp.h b/candy/src/netstack/session_tcp.h index 4060fbd3..50171960 100644 --- a/candy/src/netstack/session_tcp.h +++ b/candy/src/netstack/session_tcp.h @@ -4,7 +4,6 @@ #include "core/net.h" #include "netstack/session.h" -#include "netstack/tcp_handshake.h" #include #include #include @@ -59,13 +58,6 @@ class SessionTcp : public Session, public std::enable_shared_from_thisdone() 后置空并进入 splice。 - // 仅 Reactor 线程访问。 - std::unique_ptr handshake; - // 握手待发字节中尚未写完的残留(非阻塞 write 部分写时保留续发)。仅 Reactor 线程访问。 - std::string handshakeOutbox; - IP4 origDst; uint16_t origDstPort; IP4 origSrc; diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index 75c59862..4031af09 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -2,7 +2,6 @@ #include "netstack/session_udp.h" #include "netstack/netstack.h" #include "netstack/sockcompat.h" -#include "netstack/socks5_client.h" #include #include @@ -34,54 +33,15 @@ SessionUdp::~SessionUdp() { netClose(this->fd); this->fd = -1; } - if (this->ctrlFd >= 0) { - netClose(this->ctrlFd); - this->ctrlFd = -1; - } } int SessionUdp::start() { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) this->self = shared_from_this(); - // 按流分流:Router.match 选出 outbound(direct/socks5)。 - // 无规则时恒为 DirectOutbound,与阶段一/二行为完全一致(零回归)。 - Router::FlowKey flow{this->origSrc, this->origDst, IPPROTO_UDP, this->origDstPort}; - Outbound &outbound = this->stack->getOutbound(flow); - - if (outbound.udpNeedsAssociation()) { - // ---- socks5 UDP ASSOCIATE 分支 ---- - // 先建到 socks5 server 的 TCP 控制连接(保活),connect 完成后在其上跑 UDP - // ASSOCIATE 握手;拿到中继端点再建中继 UDP socket。期间到达的数据报暂存。 - this->useSocks5 = true; - Endpoint ps = outbound.proxyServerEndpoint(); - this->proxyServerHost = ps.host; - - this->ctrlFd = outbound.dialUdpControl(); - if (this->ctrlFd < 0) { - return -1; - } - this->associate = outbound.makeUdpAssociate(); - if (!this->associate) { - return -1; - } - - spdlog::debug("session udp(socks5): {}:{} -> {}:{} via {}:{}", this->origSrc.toString(), this->origSrcPort, - this->origDst.toString(), this->origDstPort, ps.host.toString(), ps.port); - - auto holder = shared_from_this(); - int ctrlFd = this->ctrlFd; - // 控制连接非阻塞 connect:先等可写(connect 完成)。 - this->stack->getReactor().add(ctrlFd, ReactorEvent::WRITE, - [holder](ReactorEvent ev) { holder->onCtrlEvent((uint32_t)ev); }); - // 注册每流 recv:握手未完成时数据报会被暂存,完成后冲刷到中继 fd。 - udp_recv(this->pcb, recvTrampoline, this); - return 0; - } - - // ---- direct 分支(阶段一/二原逻辑,零回归)---- - // 落地拨号交给选中的 Outbound(DirectOutbound: connect 固定对端, - // 内核自动填源地址 = 网关 LAN IP,等价 MASQUERADE,形成 Fullcone NAT 伪会话)。 + // 落地拨号:内核 socket 直连(DirectOutbound)。connect 固定对端,内核自动填源 + // 地址 = 网关 LAN IP(等价 MASQUERADE),形成 Fullcone NAT 伪会话。 + Outbound &outbound = this->stack->getOutbound(); this->fd = outbound.dialUdp(Endpoint{this->origDst, this->origDstPort}); if (this->fd < 0) { return -1; @@ -131,26 +91,6 @@ void SessionUdp::sendToLanding(std::string data) { if (holder->closing.load()) { return; } - if (holder->useSocks5) { - // socks5 中继:每个业务数据报封装 socks5 UDP 头(目的=origDst:origDstPort)。 - std::string pkt = socks5UdpEncap(holder->origDst, holder->origDstPort, - reinterpret_cast(data.data()), data.size()); - if (!holder->relayReady) { - // 握手尚未完成:暂存(带 2 字节长度前缀串联),完成后由 finishAssociate 冲刷。 - uint16_t n = (uint16_t)pkt.size(); - holder->pendingDatagrams.append(reinterpret_cast(&n), sizeof(n)); - holder->pendingDatagrams.append(pkt); - return; - } - if (holder->fd < 0) { - return; - } - long n = netSend(holder->fd, pkt.data(), pkt.size()); - if (n < 0 && !netWouldBlock(netLastError())) { - spdlog::debug("session udp(socks5) send failed: {}", netErrStr(netLastError())); - } - return; - } if (holder->fd < 0) { return; } @@ -218,11 +158,6 @@ void SessionUdp::shutdownFromStack() { netClose(holder->fd); holder->fd = -1; } - if (holder->ctrlFd >= 0) { - holder->stack->getReactor().del(holder->ctrlFd); - netClose(holder->ctrlFd); - holder->ctrlFd = -1; - } #endif holder->self.reset(); }); @@ -248,22 +183,6 @@ void SessionUdp::onFdReadable() { while (true) { long n = netRecv(this->fd, buf, sizeof(buf)); if (n > 0) { - if (this->useSocks5) { - // socks5 中继回包:去掉 socks5 UDP 头,仅把业务数据灌回 lwIP。 - IP4 srcHost; - uint16_t srcPort = 0; - const uint8_t *payload = nullptr; - size_t payloadLen = 0; - if (!socks5UdpDecap(reinterpret_cast(buf), (size_t)n, srcHost, srcPort, &payload, - payloadLen)) { - spdlog::debug("session udp(socks5) drop malformed relay datagram len={}", n); - continue; - } - std::string data(reinterpret_cast(payload), payloadLen); - auto holder = shared_from_this(); - this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->replyToLwip(std::move(data)); }); - continue; - } std::string data(buf, n); auto holder = shared_from_this(); this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->replyToLwip(std::move(data)); }); @@ -292,11 +211,6 @@ void SessionUdp::closeFromReactor() { netClose(this->fd); this->fd = -1; } - if (this->ctrlFd >= 0) { - this->stack->getReactor().del(this->ctrlFd); - netClose(this->ctrlFd); - this->ctrlFd = -1; - } #endif if (this->pcb != nullptr) { auto holder = shared_from_this(); @@ -306,195 +220,4 @@ void SessionUdp::closeFromReactor() { this->self.reset(); } -// ===================== socks5 UDP ASSOCIATE 控制连接(Reactor 线程) ===================== - -void SessionUdp::onCtrlEvent(uint32_t events) { - ReactorEvent ev = (ReactorEvent)events; - if (!this->ctrlConnected) { - if (ev & ReactorEvent::ERROR) { - closeFromReactor(); - return; - } - if (ev & ReactorEvent::WRITE) { - onCtrlConnected(); - } - return; - } - if (ev & ReactorEvent::ERROR) { - // 控制连接断开即关联失效,关闭整条会话。 - closeFromReactor(); - return; - } - // 握手阶段:分流可读/可写。握手完成后 associate 置空,控制连接仅保活(只关注 ERROR)。 - if (this->associate) { - if (ev & ReactorEvent::READ) { - onCtrlReadable(); - } - if (this->associate && (ev & ReactorEvent::WRITE)) { - onCtrlWritable(); - } - return; - } - // 关联已建立:控制连接上若有可读(通常不应有业务数据),读掉即可;对端关闭则关会话。 - if (ev & ReactorEvent::READ) { -#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - char buf[1024]; - long n = netRecv(this->ctrlFd, buf, sizeof(buf)); - if (n == 0) { - closeFromReactor(); - return; - } -#endif - } -} - -void SessionUdp::onCtrlConnected() { -#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - int err = 0; -#if defined(_WIN32) || defined(_WIN64) - int len = sizeof(err); - int ret = ::getsockopt((SOCKET)this->ctrlFd, SOL_SOCKET, SO_ERROR, (char *)&err, &len); -#else - socklen_t len = sizeof(err); - int ret = ::getsockopt(this->ctrlFd, SOL_SOCKET, SO_ERROR, &err, &len); -#endif - if (ret != 0 || err != 0) { - spdlog::warn("session udp(socks5) ctrl connect error: {}", netErrStr(err ? err : netLastError())); - closeFromReactor(); - return; - } - this->ctrlConnected = true; - spdlog::debug("session udp(socks5) ctrl connected, begin UDP ASSOCIATE handshake"); - this->stack->getReactor().mod(this->ctrlFd, ReactorEvent::READ | ReactorEvent::WRITE); - flushCtrlOutbound(); -#endif -} - -void SessionUdp::flushCtrlOutbound() { -#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - if (!this->associate || this->ctrlFd < 0) { - return; - } - if (this->associate->hasOutbound()) { - std::vector out = this->associate->takeOutbound(); - this->ctrlOutbox.append(reinterpret_cast(out.data()), out.size()); - } - while (!this->ctrlOutbox.empty()) { - long n = netSend(this->ctrlFd, this->ctrlOutbox.data(), this->ctrlOutbox.size()); - if (n > 0) { - this->ctrlOutbox.erase(0, n); - continue; - } - if (n < 0 && netWouldBlock(netLastError())) { - this->stack->getReactor().mod(this->ctrlFd, ReactorEvent::READ | ReactorEvent::WRITE); - return; - } - spdlog::warn("session udp(socks5) ctrl send failed: {}", netErrStr(netLastError())); - closeFromReactor(); - return; - } - this->stack->getReactor().mod(this->ctrlFd, ReactorEvent::READ); -#endif -} - -void SessionUdp::onCtrlWritable() { - flushCtrlOutbound(); -} - -void SessionUdp::onCtrlReadable() { -#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - if (!this->associate) { - return; - } - char buf[4096]; - while (true) { - long n = netRecv(this->ctrlFd, buf, sizeof(buf)); - if (n > 0) { - if (!this->associate->feed(reinterpret_cast(buf), (size_t)n)) { - spdlog::warn("session udp(socks5) associate failed: {}", this->associate->error()); - closeFromReactor(); - return; - } - if (this->associate->done()) { - finishAssociate(); - return; - } - if (this->associate->hasOutbound()) { - flushCtrlOutbound(); - } - continue; - } - if (n == 0) { - spdlog::warn("session udp(socks5) server closed during associate"); - closeFromReactor(); - return; - } - if (netWouldBlock(netLastError())) { - return; - } - spdlog::warn("session udp(socks5) ctrl recv failed: {}", netErrStr(netLastError())); - closeFromReactor(); - return; - } -#endif -} - -void SessionUdp::finishAssociate() { -#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) - // 关联成功:取中继端点。若服务端返回 0.0.0.0(RFC 1928 §7 允许),回退到 server IP。 - this->relayHost = this->associate->udpRelayHost(); - this->relayPort = this->associate->udpRelayPort(); - if (uint32_t(this->relayHost) == 0) { - this->relayHost = this->proxyServerHost; - } - this->associate.reset(); - // 控制连接转为保活:仅关注 ERROR(其断开即关联失效)。 - this->stack->getReactor().mod(this->ctrlFd, ReactorEvent::NONE); - - spdlog::debug("session udp(socks5) associate done, relay {}:{}", this->relayHost.toString(), this->relayPort); - - // 建中继 UDP socket,connect 到中继端点(之后 send/recv 都是 socks5 UDP 封装报文)。 - int fd = (int)::socket(AF_INET, SOCK_DGRAM, 0); - if (fd < 0) { - spdlog::warn("session udp(socks5) relay socket failed: {}", netErrStr(netLastError())); - closeFromReactor(); - return; - } - netSetNonBlocking(fd); - struct sockaddr_in addr = {}; - addr.sin_family = AF_INET; - addr.sin_port = htons(this->relayPort); - addr.sin_addr.s_addr = uint32_t(this->relayHost); - if (::connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { - spdlog::warn("session udp(socks5) relay connect {}:{} failed: {}", this->relayHost.toString(), this->relayPort, - netErrStr(netLastError())); - netClose(fd); - closeFromReactor(); - return; - } - this->fd = fd; - this->relayReady = true; - auto holder = shared_from_this(); - this->stack->getReactor().add(fd, ReactorEvent::READ, [holder](ReactorEvent ev) { holder->onFdEvent((uint32_t)ev); }); - - // 冲刷握手期间暂存的数据报(带 2 字节长度前缀串联)。 - size_t off = 0; - const std::string &buf = this->pendingDatagrams; - while (off + sizeof(uint16_t) <= buf.size()) { - uint16_t len = 0; - std::memcpy(&len, buf.data() + off, sizeof(len)); - off += sizeof(len); - if (off + len > buf.size()) { - break; - } - long n = netSend(this->fd, buf.data() + off, len); - if (n < 0 && !netWouldBlock(netLastError())) { - spdlog::debug("session udp(socks5) flush pending send failed: {}", netErrStr(netLastError())); - } - off += len; - } - this->pendingDatagrams.clear(); -#endif -} - } // namespace candy diff --git a/candy/src/netstack/session_udp.h b/candy/src/netstack/session_udp.h index b70f11eb..20b19611 100644 --- a/candy/src/netstack/session_udp.h +++ b/candy/src/netstack/session_udp.h @@ -4,7 +4,6 @@ #include "core/net.h" #include "netstack/session.h" -#include "netstack/tcp_handshake.h" #include #include #include @@ -57,17 +56,6 @@ class SessionUdp : public Session, public std::enable_shared_from_this 跑 UDP ASSOCIATE 握手 -> 拿到中继端点 -> - // 建中继 UDP socket(fd) connect 到中继端点;之后 fd 上收发都加/去 socks5 UDP 头。 - // ctrlFd 保活,其关闭即关联失效,会话关闭。 - void onCtrlEvent(uint32_t events); - void onCtrlConnected(); - void onCtrlReadable(); - void onCtrlWritable(); - void flushCtrlOutbound(); - void finishAssociate(); - // ---- NetStack 线程 ---- // 本流后续数据报到达:addr/port=源端(dev1),提取数据投递到落地 fd 发送。 void onPcbRecv(struct pbuf *p, const ip_addr_t *addr, u16_t port); @@ -79,18 +67,6 @@ class SessionUdp : public Session, public std::enable_shared_from_this associate; // UDP ASSOCIATE 握手器(完成后置空) - std::string ctrlOutbox; // 控制连接待发字节残留(非阻塞部分写) - bool relayReady = false; // 中继 fd 是否已就绪(握手完成、connect 中继后置真) - IP4 relayHost; // socks5 UDP 中继端点 - uint16_t relayPort = 0; - IP4 proxyServerHost; // socks5 server IP(中继地址为 0.0.0.0 时回退用) - std::string pendingDatagrams; // 握手期间 lwIP 侧到达的数据报暂存(带长度前缀串联) - IP4 origSrc; uint16_t origSrcPort; IP4 origDst; diff --git a/candy/src/netstack/socks5_client.cc b/candy/src/netstack/socks5_client.cc deleted file mode 100644 index 0b0f4e6c..00000000 --- a/candy/src/netstack/socks5_client.cc +++ /dev/null @@ -1,346 +0,0 @@ -// SPDX-License-Identifier: MIT -#include "netstack/socks5_client.h" -#include - -namespace candy { - -void Socks5Client::startConnectIPv4(IP4 dstHost, uint16_t dstPort, const std::string &username, - const std::string &password) { - this->command = Socks5::CMD_CONNECT; - this->useDomain = false; - this->dstHost = dstHost; - this->dstPort = dstPort; - this->username = username; - this->password = password; - this->useUserPass = !username.empty(); - buildAuthMethods(); - this->st = State::WriteAuthMethods; -} - -void Socks5Client::startConnectDomain(const std::string &domain, uint16_t dstPort, const std::string &username, - const std::string &password) { - this->command = Socks5::CMD_CONNECT; - this->useDomain = true; - this->dstDomain = domain; - this->dstPort = dstPort; - this->username = username; - this->password = password; - this->useUserPass = !username.empty(); - buildAuthMethods(); - this->st = State::WriteAuthMethods; -} - -void Socks5Client::startUdpAssociate(const std::string &username, const std::string &password) { - // UDP ASSOCIATE:CMD=0x03,请求里的 DST.ADDR/DST.PORT 填 0.0.0.0:0(RFC 1928 §7: - // 客户端尚不确定将从哪个源端口发 UDP,置 0 让服务端不做限制)。其余握手流程与 CONNECT 相同。 - this->command = Socks5::CMD_UDP_ASSOCIATE; - this->useDomain = false; - this->dstHost = IP4("0.0.0.0"); - this->dstPort = 0; - this->username = username; - this->password = password; - this->useUserPass = !username.empty(); - buildAuthMethods(); - this->st = State::WriteAuthMethods; -} - -IP4 Socks5Client::udpRelayHost() const { - return this->relayAddr; -} - -uint16_t Socks5Client::udpRelayPort() const { - return this->relayPortValue; -} - -void Socks5Client::buildAuthMethods() { - // VER(1) NMETHODS(1) METHODS(nmethods)。无认证只声明 NONE; - // 配了用户口令则同时声明 NONE 与 USERPASS,让服务端择优。 - this->outbound.clear(); - this->outbound.push_back(Socks5::VERSION); - if (this->useUserPass) { - this->outbound.push_back(0x02); - this->outbound.push_back(Socks5::METHOD_NONE); - this->outbound.push_back(Socks5::METHOD_USERPASS); - } else { - this->outbound.push_back(0x01); - this->outbound.push_back(Socks5::METHOD_NONE); - } -} - -void Socks5Client::buildAuthCreds() { - // RFC 1929:VER(1=0x01) ULEN(1) UNAME(ulen) PLEN(1) PASSWD(plen)。 - this->outbound.clear(); - this->outbound.push_back(Socks5::AUTH_VERSION); - this->outbound.push_back((uint8_t)this->username.size()); - this->outbound.insert(this->outbound.end(), this->username.begin(), this->username.end()); - this->outbound.push_back((uint8_t)this->password.size()); - this->outbound.insert(this->outbound.end(), this->password.begin(), this->password.end()); -} - -void Socks5Client::buildRequest() { - // VER(1) CMD(1) RSV(1=0) ATYP(1) DST.ADDR DST.PORT。CMD 取 this->command - // (CONNECT 或 UDP ASSOCIATE)。 - this->outbound.clear(); - this->outbound.push_back(Socks5::VERSION); - this->outbound.push_back(this->command); - this->outbound.push_back(0x00); - if (this->useDomain) { - this->outbound.push_back(Socks5::ATYP_DOMAIN); - this->outbound.push_back((uint8_t)this->dstDomain.size()); - this->outbound.insert(this->outbound.end(), this->dstDomain.begin(), this->dstDomain.end()); - } else { - this->outbound.push_back(Socks5::ATYP_IPV4); - // IP4 的内存字节即网络序的 a.b.c.d,直接拷 4 字节即为 socks5 所需顺序。 - uint32_t v = uint32_t(this->dstHost); - const uint8_t *p = reinterpret_cast(&v); - this->outbound.insert(this->outbound.end(), p, p + 4); - } - // 端口为大端(网络序)。 - uint16_t portBE = hton(this->dstPort); - const uint8_t *pp = reinterpret_cast(&portBE); - this->outbound.insert(this->outbound.end(), pp, pp + 2); -} - -bool Socks5Client::feed(const uint8_t *data, size_t len) { - if (this->st == State::Failed) { - return false; - } - if (this->st == State::Done) { - // 握手已完成,多余字节归入 leftover。 - this->leftover.insert(this->leftover.end(), data, data + len); - return true; - } - this->inbound.insert(this->inbound.end(), data, data + len); - - // 循环推进:可能一次 feed 跨越多个状态(例如认证应答与请求应答粘包)。 - for (;;) { - switch (this->st) { - case State::ReadAuthMethod: { - // 应答固定 2 字节:VER, METHOD。 - if (this->inbound.size() < 2) { - return true; - } - uint8_t ver = this->inbound[0]; - uint8_t method = this->inbound[1]; - this->inbound.erase(this->inbound.begin(), this->inbound.begin() + 2); - if (ver != Socks5::VERSION) { - fail("bad socks version in auth method reply"); - return false; - } - if (method == Socks5::METHOD_NONE) { - buildRequest(); - this->st = State::WriteRequest; - } else if (method == Socks5::METHOD_USERPASS) { - if (this->username.empty()) { - fail("server requires userpass but no credentials configured"); - return false; - } - buildAuthCreds(); - this->st = State::WriteAuthCreds; - } else { - fail("server rejected all auth methods"); - return false; - } - // 进入 Write* 状态后没有更多可解析的入站数据,交回调用方发送。 - return true; - } - case State::ReadAuthCreds: { - // RFC 1929 应答固定 2 字节:VER, STATUS(0=成功)。 - if (this->inbound.size() < 2) { - return true; - } - uint8_t status = this->inbound[1]; - this->inbound.erase(this->inbound.begin(), this->inbound.begin() + 2); - if (status != 0x00) { - fail("socks5 userpass auth failed"); - return false; - } - buildRequest(); - this->st = State::WriteRequest; - return true; - } - case State::ReadResponse: { - // VER(1) REP(1) RSV(1) ATYP(1) BND.ADDR BND.PORT。先读前 4 字节定长头。 - if (this->inbound.size() < 4) { - return true; - } - uint8_t ver = this->inbound[0]; - uint8_t rep = this->inbound[1]; - uint8_t atyp = this->inbound[3]; - if (ver != Socks5::VERSION) { - fail("bad socks version in connect reply"); - return false; - } - if (rep != Socks5::REP_SUCCESS) { - fail("socks5 connect rejected, rep=" + std::to_string(rep)); - return false; - } - // 计算绑定地址长度,确认应答完整收齐。 - size_t addrLen = 0; - switch (atyp) { - case Socks5::ATYP_IPV4: - addrLen = 4; - break; - case Socks5::ATYP_IPV6: - addrLen = 16; - break; - case Socks5::ATYP_DOMAIN: - if (this->inbound.size() < 5) { - return true; // 还没收到域名长度字节 - } - addrLen = 1 + this->inbound[4]; - break; - default: - fail("unknown atyp in connect reply"); - return false; - } - size_t total = 4 + addrLen + 2; // 头 + 地址 + 端口 - if (this->inbound.size() < total) { - return true; // 等待剩余字节 - } - // UDP ASSOCIATE:BND.ADDR/BND.PORT 即服务端分配的 UDP 中继端点,需解析留用。 - // 仅支持 IPv4 中继地址(与本工程 IPv4-only 一致)。若服务端返回 0.0.0.0, - // 按 RFC 1928 约定回退到控制连接的对端地址(由调用方处理,这里置 0)。 - if (this->command == Socks5::CMD_UDP_ASSOCIATE) { - if (atyp != Socks5::ATYP_IPV4) { - fail("udp associate reply with non-IPv4 relay addr"); - return false; - } - uint32_t v = 0; - std::memcpy(&v, &this->inbound[4], 4); // 网络序字节直存即 a.b.c.d - std::memcpy(&this->relayAddr, &v, 4); // IP4 为 packed 4 字节,按内存序直存 - uint16_t portBE = 0; - std::memcpy(&portBE, &this->inbound[4 + addrLen], 2); - this->relayPortValue = ntoh(portBE); - } - // 应答收齐,多余的即为业务数据。 - this->leftover.assign(this->inbound.begin() + total, this->inbound.end()); - this->inbound.clear(); - this->st = State::Done; - return true; - } - default: - // Write* 状态:等待调用方把 outbound 发出去后再调用 advanceAfterWrite()。 - return true; - } - } -} - -std::vector Socks5Client::takeOutbound() { - std::vector out; - out.swap(this->outbound); - // 发送完成意味着对应的 Write 状态结束,迁移到等待应答的 Read 状态。 - switch (this->st) { - case State::WriteAuthMethods: - this->st = State::ReadAuthMethod; - break; - case State::WriteAuthCreds: - this->st = State::ReadAuthCreds; - break; - case State::WriteRequest: - this->st = State::ReadResponse; - break; - default: - break; - } - return out; -} - -bool Socks5Client::hasOutbound() const { - return !this->outbound.empty(); -} - -std::vector Socks5Client::takeLeftover() { - std::vector out; - out.swap(this->leftover); - return out; -} - -Socks5Client::State Socks5Client::state() const { - return this->st; -} - -bool Socks5Client::done() const { - return this->st == State::Done; -} - -bool Socks5Client::failed() const { - return this->st == State::Failed; -} - -const std::string &Socks5Client::error() const { - return this->errMsg; -} - -void Socks5Client::fail(const std::string &reason) { - this->errMsg = reason; - this->st = State::Failed; -} - -std::string socks5UdpEncap(IP4 dstHost, uint16_t dstPort, const uint8_t *data, size_t len) { - // RSV(2=0) FRAG(1=0) ATYP(1=IPv4) DST.ADDR(4) DST.PORT(2) DATA。 - std::string out; - out.reserve(10 + len); - out.push_back(0x00); // RSV - out.push_back(0x00); // RSV - out.push_back(0x00); // FRAG - out.push_back((char)Socks5::ATYP_IPV4); - uint32_t v = uint32_t(dstHost); // 内存即网络序 a.b.c.d - out.append(reinterpret_cast(&v), 4); - uint16_t portBE = hton(dstPort); - out.append(reinterpret_cast(&portBE), 2); - if (data != nullptr && len > 0) { - out.append(reinterpret_cast(data), len); - } - return out; -} - -bool socks5UdpDecap(const uint8_t *pkt, size_t len, IP4 &srcHost, uint16_t &srcPort, const uint8_t **payload, - size_t &payloadLen) { - // 最小头长:RSV(2) FRAG(1) ATYP(1) = 4 字节,之后按 ATYP 取地址。 - if (pkt == nullptr || len < 4) { - return false; - } - uint8_t frag = pkt[2]; - if (frag != 0x00) { - return false; // 不支持分片,丢弃 - } - uint8_t atyp = pkt[3]; - size_t addrLen = 0; - switch (atyp) { - case Socks5::ATYP_IPV4: - addrLen = 4; - break; - case Socks5::ATYP_IPV6: - addrLen = 16; - break; - case Socks5::ATYP_DOMAIN: - if (len < 5) { - return false; - } - addrLen = 1 + pkt[4]; - break; - default: - return false; - } - size_t headLen = 4 + addrLen + 2; - if (len < headLen) { - return false; - } - // 仅 IPv4 源地址可还原为 IP4;其余类型也接受但 srcHost 置 0(回包寻址不依赖它)。 - if (atyp == Socks5::ATYP_IPV4) { - uint32_t v = 0; - std::memcpy(&v, &pkt[4], 4); - std::memcpy(&srcHost, &v, 4); - } else { - srcHost = IP4("0.0.0.0"); - } - uint16_t portBE = 0; - std::memcpy(&portBE, &pkt[4 + addrLen], 2); - srcPort = ntoh(portBE); - *payload = pkt + headLen; - payloadLen = len - headLen; - return true; -} - -} // namespace candy diff --git a/candy/src/netstack/socks5_client.h b/candy/src/netstack/socks5_client.h deleted file mode 100644 index 9a39451c..00000000 --- a/candy/src/netstack/socks5_client.h +++ /dev/null @@ -1,134 +0,0 @@ -// SPDX-License-Identifier: MIT -#ifndef CANDY_NETSTACK_SOCKS5_CLIENT_H -#define CANDY_NETSTACK_SOCKS5_CLIENT_H - -#include "core/net.h" -#include "netstack/tcp_handshake.h" -#include -#include -#include - -namespace candy { - -// socks5 协议常量(RFC 1928 / 1929,取值与 hev-socks5-proto.h 对齐)。 -namespace Socks5 { -constexpr uint8_t VERSION = 0x05; // socks 版本号 -constexpr uint8_t AUTH_VERSION = 0x01; // user/pass 认证子协商版本(RFC 1929) -constexpr uint8_t METHOD_NONE = 0x00; // 无认证 -constexpr uint8_t METHOD_USERPASS = 0x02; // 用户名口令认证 -constexpr uint8_t METHOD_NOACCEPT = 0xFF; // 服务端拒绝所有方法 -constexpr uint8_t CMD_CONNECT = 0x01; // CONNECT 请求 -constexpr uint8_t CMD_UDP_ASSOCIATE = 0x03;// UDP ASSOCIATE 请求 -constexpr uint8_t ATYP_IPV4 = 0x01; // 地址类型:IPv4 -constexpr uint8_t ATYP_DOMAIN = 0x03; // 地址类型:域名 -constexpr uint8_t ATYP_IPV6 = 0x04; // 地址类型:IPv6 -constexpr uint8_t REP_SUCCESS = 0x00; // 应答:成功 -} // namespace Socks5 - -// Socks5Client:非阻塞 reactor 模型下的 socks5 CONNECT 握手增量状态机。 -// -// 设计要点(适配 candy 的 reactor,区别于 hev 的协程阻塞 IO): -// - 本类不持有 fd、不直接收发;只做"协议字节"的生成与解析。 -// - 调用方在 socket 可写时取 takeOutbound() 的待发送字节并 send; -// 在 socket 可读时把收到的字节 feed() 进来推进状态机。 -// - 每次推进后通过 state() 查询:是否还要发/收、是否完成(Done)、是否失败(Failed)。 -// - 完成后,余下未消费的入站字节(握手应答之后紧跟的业务数据,极少见但协议允许) -// 可由 takeLeftover() 取走,交给后续 splice。 -// -// 握手时序(标准 CONNECT,参照 hev-socks5-client.c handshake_standard): -// WriteAuthMethods -> ReadAuthMethod -// -> [若选中 USERPASS] WriteAuthCreds -> ReadAuthCreds -// -> WriteRequest -> ReadResponse -> Done -class Socks5Client : public TcpHandshake { -public: - enum class State { - Init, // 未开始 - WriteAuthMethods, // 待发送:版本+认证方法列表 - ReadAuthMethod, // 待接收:服务端选定的认证方法 - WriteAuthCreds, // 待发送:用户名/口令 - ReadAuthCreds, // 待接收:认证结果 - WriteRequest, // 待发送:CONNECT 请求 - ReadResponse, // 待接收:CONNECT 应答 - Done, // 握手成功 - Failed, // 握手失败 - }; - - // 目标地址用 IPv4 直发(ATYP=IPv4)。creds 为空表示仅声明无认证方法。 - void startConnectIPv4(IP4 dstHost, uint16_t dstPort, const std::string &username = "", - const std::string &password = ""); - - // 目标地址用域名直发(ATYP=DOMAIN),由 socks5 服务端解析。 - void startConnectDomain(const std::string &domain, uint16_t dstPort, const std::string &username = "", - const std::string &password = ""); - - // UDP ASSOCIATE 握手(RFC 1928 §7):在 TCP 控制连接上请求 UDP 中继。 - // 请求里的 DST.ADDR/DST.PORT 填 0.0.0.0:0(表示客户端中继源端口未定,服务端不限制)。 - // 完成后由 udpRelayHost()/udpRelayPort() 取服务端返回的 UDP 中继端点(BND.ADDR:BND.PORT)。 - void startUdpAssociate(const std::string &username = "", const std::string &password = ""); - - // 喂入从 socket 收到的字节,推进状态机。返回 false 表示握手失败(state()==Failed)。 - bool feed(const uint8_t *data, size_t len) override; - - // 取走当前待发送字节(取走后内部清空)。调用方负责把它写到 socket; - // 若一次没写完,剩余部分应由调用方自行保留续发,本类不重复给出。 - std::vector takeOutbound() override; - - // 是否还有待发送字节。 - bool hasOutbound() const override; - - // 握手完成后,取走应答之后多余的入站业务数据(通常为空)。 - std::vector takeLeftover() override; - - State state() const; - bool done() const override; - bool failed() const override; - const std::string &error() const override; - - IP4 udpRelayHost() const override; - uint16_t udpRelayPort() const override; - -private: - void buildAuthMethods(); - void buildAuthCreds(); - void buildRequest(); - void fail(const std::string &reason); - - State st = State::Init; - bool useUserPass = false; - std::string username; - std::string password; - - // 握手命令:CONNECT(TCP) 或 UDP ASSOCIATE(UDP 中继),决定 buildRequest 的 CMD 与 - // 应答里 BND.ADDR/BND.PORT 的语义(CONNECT 时无用,UDP ASSOCIATE 时为中继端点)。 - uint8_t command = Socks5::CMD_CONNECT; - - // 目标地址(二选一:IPv4 或域名)。 - bool useDomain = false; - IP4 dstHost; - std::string dstDomain; - uint16_t dstPort = 0; - - // UDP ASSOCIATE 应答解析出的中继端点(仅 command==UDP ASSOCIATE 且 Done 后有效)。 - IP4 relayAddr; - uint16_t relayPortValue = 0; - - std::vector outbound; // 待发送缓冲 - std::vector inbound; // 入站累积缓冲(用于跨次拼接定长/变长应答) - std::vector leftover; // 握手完成后多余的入站字节 - std::string errMsg; // 失败原因(仅在 Failed 时有效) -}; - -// socks5 UDP 中继数据报封装(RFC 1928 §7)。每个发往中继的 UDP 数据报前缀一个头: -// RSV(2=0x0000) FRAG(1=0x00) ATYP(1) DST.ADDR DST.PORT DATA。 -// 本工程 IPv4-only,ATYP 固定 0x01。返回"头 + 业务数据"的完整数据报字节。 -std::string socks5UdpEncap(IP4 dstHost, uint16_t dstPort, const uint8_t *data, size_t len); - -// socks5 UDP 中继数据报解封装:解析中继回包的头,输出原始目的端点(srcHost/srcPort) -// 与业务数据。返回 false 表示包格式非法或 FRAG!=0(不支持分片,按 RFC 丢弃)。 -// 成功时 payload 指向 pkt 内部的业务数据起点(零拷贝),payloadLen 为其长度。 -bool socks5UdpDecap(const uint8_t *pkt, size_t len, IP4 &srcHost, uint16_t &srcPort, const uint8_t **payload, - size_t &payloadLen); - -} // namespace candy - -#endif diff --git a/candy/src/netstack/tcp_handshake.h b/candy/src/netstack/tcp_handshake.h deleted file mode 100644 index b695accf..00000000 --- a/candy/src/netstack/tcp_handshake.h +++ /dev/null @@ -1,49 +0,0 @@ -// SPDX-License-Identifier: MIT -#ifndef CANDY_NETSTACK_TCP_HANDSHAKE_H -#define CANDY_NETSTACK_TCP_HANDSHAKE_H - -#include -#include -#include -#include - -#include "core/net.h" - -namespace candy { - -// TcpHandshake:在落地 TCP 连接建立之后、进入双向 splice 之前,需要先完成的 -// 应用层握手抽象(如 socks5 CONNECT)。direct 出站无需握手(makeTcpHandshake 返回 nullptr)。 -// -// 由 Reactor 线程单线程驱动,区别于 hev 的协程阻塞 IO: -// - 可写时:取 takeOutbound() 的待发字节写入 fd; -// - 可读时:把收到的字节 feed() 进来推进; -// - done() 表示握手成功,failed() 表示失败,takeLeftover() 取握手后粘连的业务数据。 -// Session 由此把"握手如何进行"与"具体协议(socks5)"解耦。 -class TcpHandshake { -public: - virtual ~TcpHandshake() = default; - - // 取走当前待发送字节(取走后内部清空),调用方负责写入 fd。 - virtual std::vector takeOutbound() = 0; - // 是否还有待发送字节。 - virtual bool hasOutbound() const = 0; - // 喂入从 fd 收到的字节推进握手;返回 false 表示握手失败。 - virtual bool feed(const uint8_t *data, size_t len) = 0; - // 握手是否成功完成。 - virtual bool done() const = 0; - // 握手是否失败。 - virtual bool failed() const = 0; - // 握手完成后取走应答之后多余的入站业务数据(通常为空)。 - virtual std::vector takeLeftover() = 0; - // 失败原因(仅在 failed() 时有效)。 - virtual const std::string &error() const = 0; - - // UDP ASSOCIATE 专用:握手完成后服务端返回的 UDP 中继端点。 - // 仅 socks5 UDP 关联握手有意义;其余握手返回默认值(调用方不会使用)。 - virtual IP4 udpRelayHost() const { return IP4("0.0.0.0"); } - virtual uint16_t udpRelayPort() const { return 0; } -}; - -} // namespace candy - -#endif From cab8e375deb39ff9a15fc10bd0e3962be659bfcb Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Wed, 1 Jul 2026 18:43:58 +0800 Subject: [PATCH 12/29] remove unused --- candy-cli/src/config.cc | 8 -------- candy-cli/src/config.h | 3 --- candy/src/netstack/session.h | 3 +-- 3 files changed, 1 insertion(+), 13 deletions(-) diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index 8db8db36..a0899663 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -38,8 +38,6 @@ Poco::JSON::Object arguments::json() { config.set("vmac", virtualMac(this->name)); config.set("expt", loadTunAddress(this->name)); config.set("forward-mode", this->forwardMode); - config.set("socks5-upstream", this->socks5Upstream); - config.set("outbound-rules", this->outboundRules); } if (this->mode == "server") { @@ -129,8 +127,6 @@ int arguments::parse(int argc, char *argv[]) { .metavar(""); program.add_argument("--forward-mode").help("packet forward mode: kernel or userspace"); - program.add_argument("--socks5-upstream").help("external socks5 upstream: name=socks5://host:port;..."); - program.add_argument("--outbound-rules").help("outbound split rules: dst-cidr:.. => name;..."); program.add_group("Server options"); @@ -190,8 +186,6 @@ int arguments::parse(int argc, char *argv[]) { program.set_if_used("--discovery", this->discovery); program.set_if_used("--route", this->routeCost); program.set_if_used("--forward-mode", this->forwardMode); - program.set_if_used("--socks5-upstream", this->socks5Upstream); - program.set_if_used("--outbound-rules", this->outboundRules); bool needShowUsage = [&]() { if (this->mode != "client" && this->mode != "server") @@ -250,8 +244,6 @@ void arguments::parseFile(std::string cfgFile) { {"mtu", [&](const std::string &value) { this->mtu = std::stoi(value); }}, {"localhost", [&](const std::string &value) { this->localhost = value; }}, {"forward-mode", [&](const std::string &value) { this->forwardMode = value; }}, - {"socks5-upstream", [&](const std::string &value) { this->socks5Upstream = value; }}, - {"outbound-rules", [&](const std::string &value) { this->outboundRules = value; }}, }; auto trim = [](std::string str) { if (str.length() >= 2 && str.front() == '\"' && str.back() == '\"') { diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index ae4be379..f8f2772a 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -32,9 +32,6 @@ struct arguments { int routeCost = 0; int mtu = 1400; std::string forwardMode = "kernel"; - // 阶段三:外部 socks5 上游与分流规则(仅 userspace 模式有效,空表示全部 direct)。 - std::string socks5Upstream; - std::string outboundRules; }; int saveTunAddress(const std::string &name, const std::string &cidr); diff --git a/candy/src/netstack/session.h b/candy/src/netstack/session.h index 7fe5e362..3730a095 100644 --- a/candy/src/netstack/session.h +++ b/candy/src/netstack/session.h @@ -9,8 +9,7 @@ namespace candy { class NetStack; -// Session 基类:会话生命周期的公共抽象。 -// 阶段一仅有 TCP 会话;UDP/ICMP 会话在阶段二加入。 +// Session 基类:会话生命周期的公共抽象,派生出 TCP/UDP 两类会话。 class Session { public: explicit Session(NetStack *stack); From e6b22c9c1d058e6677b9b07c9b3073dc6cf352de Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Wed, 1 Jul 2026 18:55:32 +0800 Subject: [PATCH 13/29] format --- .gitignore | 3 ++- candy/src/netstack/netstack.cc | 7 +++---- candy/src/netstack/outbound.cc | 6 ++---- candy/src/netstack/outbound.h | 12 +++++++++--- candy/src/netstack/reactor.cc | 2 +- candy/src/netstack/session_tcp.cc | 16 +++++++--------- candy/src/netstack/session_udp.cc | 7 +++---- candy/src/netstack/session_udp.h | 11 +++++++---- 8 files changed, 34 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index ad582460..545e5e74 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,5 @@ # CMake Build files .cache build -build-mac \ No newline at end of file +build-mac +.trae \ No newline at end of file diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index 1a7d8f5e..2d3c0669 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -376,8 +376,8 @@ err_t NetStack::onOutput(struct netif *netif, struct pbuf *p, const ip4_addr_t * pbuf_copy_partial(p, buffer.data(), p->tot_len, 0); if (buffer.size() >= sizeof(IP4Header)) { IP4Header *h = (IP4Header *)buffer.data(); - spdlog::debug("netstack onOutput: {} -> {} proto={} len={}", h->saddr.toString(), h->daddr.toString(), - (int)h->protocol, buffer.size()); + spdlog::debug("netstack onOutput: {} -> {} proto={} len={}", h->saddr.toString(), h->daddr.toString(), (int)h->protocol, + buffer.size()); } output(buffer); return ERR_OK; @@ -424,8 +424,7 @@ void NetStack::onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *a std::memcpy(&origSrc, &ip_2_ip4(&pcb->remote_ip)->addr, sizeof(uint32_t)); uint16_t origSrcPort = pcb->remote_port; - spdlog::debug("netstack onUdpRecv: {}:{} -> {}:{}", origSrc.toString(), origSrcPort, origDst.toString(), - origDstPort); + spdlog::debug("netstack onUdpRecv: {}:{} -> {}:{}", origSrc.toString(), origSrcPort, origDst.toString(), origDstPort); auto session = std::make_shared(this, pcb, origSrc, origSrcPort, origDst, origDstPort); if (session->start()) { diff --git a/candy/src/netstack/outbound.cc b/candy/src/netstack/outbound.cc index a3c0ac4f..5b9a1ecf 100644 --- a/candy/src/netstack/outbound.cc +++ b/candy/src/netstack/outbound.cc @@ -29,8 +29,7 @@ int DirectOutbound::dialTcp(const Endpoint &dst) { int ret = ::connect(fd, (struct sockaddr *)&addr, sizeof(addr)); if (ret != 0 && !netInProgress(netLastError())) { - spdlog::warn("direct outbound tcp connect {}:{} failed: {}", dst.host.toString(), dst.port, - netErrStr(netLastError())); + spdlog::warn("direct outbound tcp connect {}:{} failed: {}", dst.host.toString(), dst.port, netErrStr(netLastError())); netClose(fd); return -1; } @@ -56,8 +55,7 @@ int DirectOutbound::dialUdp(const Endpoint &dst) { addr.sin_port = htons(dst.port); addr.sin_addr.s_addr = uint32_t(dst.host); if (::connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { - spdlog::warn("direct outbound udp connect {}:{} failed: {}", dst.host.toString(), dst.port, - netErrStr(netLastError())); + spdlog::warn("direct outbound udp connect {}:{} failed: {}", dst.host.toString(), dst.port, netErrStr(netLastError())); netClose(fd); return -1; } diff --git a/candy/src/netstack/outbound.h b/candy/src/netstack/outbound.h index 4c228c1c..c422ea5f 100644 --- a/candy/src/netstack/outbound.h +++ b/candy/src/netstack/outbound.h @@ -32,18 +32,24 @@ class Outbound { // L4 终结型出站:为一条已终结的 TCP 流建立落地通道,返回已发起 connect 的非阻塞 // fd(EINPROGRESS 视为成功),失败返回 -1。默认不支持。 - virtual int dialTcp(const Endpoint &dst) { return -1; } + virtual int dialTcp(const Endpoint &dst) { + return -1; + } // L4 终结型出站:为一条已终结的 UDP 流建立 connect 到目的的非阻塞 fd,失败返回 -1。 // 默认不支持。 - virtual int dialUdp(const Endpoint &dst) { return -1; } + virtual int dialUdp(const Endpoint &dst) { + return -1; + } }; // DirectOutbound:内核 socket 直连落地。 // 源地址由内核在发起连接时自动填为本网关出口 IP,等价 MASQUERADE。 class DirectOutbound : public Outbound { public: - std::string name() const override { return "direct"; } + std::string name() const override { + return "direct"; + } int dialTcp(const Endpoint &dst) override; int dialUdp(const Endpoint &dst) override; diff --git a/candy/src/netstack/reactor.cc b/candy/src/netstack/reactor.cc index 9e2ddf2e..4107df5b 100644 --- a/candy/src/netstack/reactor.cc +++ b/candy/src/netstack/reactor.cc @@ -57,7 +57,7 @@ static unsigned int reactorBackendFlags() { #if defined(__APPLE__) return EVBACKEND_KQUEUE; // 与 EV_USE_KQUEUE=1 对齐 #elif defined(__linux__) || defined(__ANDROID__) - return EVBACKEND_EPOLL; // 与 EV_USE_EPOLL=1 对齐(musl 下也走 epoll) + return EVBACKEND_EPOLL; // 与 EV_USE_EPOLL=1 对齐(musl 下也走 epoll) #elif defined(_WIN32) return EVBACKEND_SELECT; // 与 EV_USE_SELECT=1 / EV_SELECT_IS_WINSOCKET 对齐 #else diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc index 619425fe..0ac57007 100644 --- a/candy/src/netstack/session_tcp.cc +++ b/candy/src/netstack/session_tcp.cc @@ -110,9 +110,9 @@ err_t SessionTcp::onRecv(struct tcp_pcb *pcb, struct pbuf *p, err_t err) { // 背压埋点(lwIP->fd):记录本次收量、forwardBuf 积压、当前未确认窗口(pendingRecved)。 // forwardBuf 应稳定在一个 TCP_WND 内;若持续增长说明落地端写不动、背压失效。 - spdlog::debug("[bp][fwd] onRecv {} -> {}:{} recv={} forwardBuf={} pendingRecved={} sndwnd={}", - this->origSrc.toString(), this->origDst.toString(), this->origDstPort, recvLen, bufSize, - this->pendingRecved.load(), (this->pcb ? tcp_sndbuf(this->pcb) : 0)); + spdlog::debug("[bp][fwd] onRecv {} -> {}:{} recv={} forwardBuf={} pendingRecved={} sndwnd={}", this->origSrc.toString(), + this->origDst.toString(), this->origDstPort, recvLen, bufSize, this->pendingRecved.load(), + (this->pcb ? tcp_sndbuf(this->pcb) : 0)); auto holder = shared_from_this(); this->stack->getReactor().post([holder] { holder->flushForwardLocked(); }); @@ -364,9 +364,8 @@ void SessionTcp::flushForwardLocked() { // 背压埋点(lwIP->fd):记录本次写进落地 fd 的字节、剩余积压、是否因 fd 写满而背压。 // blocked=true 表示落地端写满(EAGAIN),forwardBuf 残留并等待 onFdWritable 续写; // 此时不向 lwIP 确认残留部分,源端接收窗口收紧 -> 自动减速,这正是背压生效的体现。 - spdlog::debug("[bp][fwd] flush {} -> {}:{} wrote={} remain={} blocked={} pendingRecved->{}", - this->origSrc.toString(), this->origDst.toString(), this->origDstPort, acked, remain, blocked, - this->pendingRecved.load() + acked); + spdlog::debug("[bp][fwd] flush {} -> {}:{} wrote={} remain={} blocked={} pendingRecved->{}", this->origSrc.toString(), + this->origDst.toString(), this->origDstPort, acked, remain, blocked, this->pendingRecved.load() + acked); // 已落地的字节交由 NetStack 线程向 lwIP 确认(推进接收窗口)。 // 这才是真正的"收到",从而把源端发送速率约束在落地 fd 的消费能力之内。 if (acked > 0) { @@ -390,9 +389,8 @@ void SessionTcp::onFdReadable() { if (this->backwardBytes.load() >= HIGH_WATER) { this->readPaused.store(true); // 背压埋点(fd->lwIP):积压达到高水位,停止读取(水位上升沿,低频事件)。 - spdlog::debug("[bp][bwd] pause read {}:{} -> {} backwardBytes={} (>=HIGH_WATER={})", - this->origDst.toString(), this->origDstPort, this->origSrc.toString(), - this->backwardBytes.load(), HIGH_WATER); + spdlog::debug("[bp][bwd] pause read {}:{} -> {} backwardBytes={} (>=HIGH_WATER={})", this->origDst.toString(), + this->origDstPort, this->origSrc.toString(), this->backwardBytes.load(), HIGH_WATER); this->stack->getReactor().mod(this->fd, ReactorEvent::NONE); return; } diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index 4031af09..149769a4 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -17,10 +17,9 @@ namespace candy { -SessionUdp::SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort, IP4 origDst, - uint16_t origDstPort) - : Session(stack), pcb(pcb), fd(-1), origSrc(origSrc), origSrcPort(origSrcPort), origDst(origDst), - origDstPort(origDstPort), lastActiveTs(std::chrono::steady_clock::now()) { +SessionUdp::SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort, IP4 origDst, uint16_t origDstPort) + : Session(stack), pcb(pcb), fd(-1), origSrc(origSrc), origSrcPort(origSrcPort), origDst(origDst), origDstPort(origDstPort), + lastActiveTs(std::chrono::steady_clock::now()) { // 四元组 key:源IP:源端口 -> 目的IP:目的端口 this->sessionKey.assign((const char *)&origSrc, sizeof(uint32_t)); this->sessionKey.append((const char *)&origSrcPort, sizeof(origSrcPort)); diff --git a/candy/src/netstack/session_udp.h b/candy/src/netstack/session_udp.h index 20b19611..5f4a661e 100644 --- a/candy/src/netstack/session_udp.h +++ b/candy/src/netstack/session_udp.h @@ -29,8 +29,7 @@ class SessionUdp : public Session, public std::enable_shared_from_thissessionKey; } + const std::string &key() const { + return this->sessionKey; + } // 最近活跃时间(用于空闲超时回收,仅 NetStack 线程访问)。 - std::chrono::steady_clock::time_point lastActive() const { return this->lastActiveTs; } + std::chrono::steady_clock::time_point lastActive() const { + return this->lastActiveTs; + } // lwIP 每流 recv 跳板:克隆出的 npcb 后续同流数据报都走这里(仅 NetStack 线程)。 static void recvTrampoline(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); From 0601c76ce22e4f56a776a2366887fb524f8024d7 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Thu, 2 Jul 2026 11:44:09 +0800 Subject: [PATCH 14/29] windows build fix --- candy/src/netstack/netstack.h | 4 ++++ candy/src/netstack/reactor.cc | 2 +- candy/src/netstack/reactor.h | 2 +- candy/src/netstack/session_tcp.cc | 4 ++-- candy/src/netstack/session_tcp.h | 3 +++ candy/src/netstack/session_udp.cc | 2 +- candy/src/netstack/session_udp.h | 3 +++ 7 files changed, 15 insertions(+), 5 deletions(-) diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index c1311c65..028d54ca 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -17,6 +17,10 @@ #include #include +// Windows:必须在 lwIP 头之前引入 winsock(sockcompat.h 内已按平台处理),否则 +// lwip/def.h 的 htonl 宏会改写 winsock 中 htonl 的原型,导致 lwip_htonl 声明冲突。 +#include "netstack/sockcompat.h" + #include "lwip/ip_addr.h" #include "lwip/netif.h" #include "lwip/tcp.h" diff --git a/candy/src/netstack/reactor.cc b/candy/src/netstack/reactor.cc index 4107df5b..f3f04259 100644 --- a/candy/src/netstack/reactor.cc +++ b/candy/src/netstack/reactor.cc @@ -35,7 +35,7 @@ static ReactorEvent fromEvEvents(int revents) { events = events | ReactorEvent::WRITE; } if (revents & EV_ERROR) { - events = events | ReactorEvent::ERROR; + events = events | ReactorEvent::FAILURE; } return events; } diff --git a/candy/src/netstack/reactor.h b/candy/src/netstack/reactor.h index d34d60fd..fa0d58a7 100644 --- a/candy/src/netstack/reactor.h +++ b/candy/src/netstack/reactor.h @@ -21,7 +21,7 @@ enum class ReactorEvent : uint32_t { NONE = 0, READ = 1 << 0, WRITE = 1 << 1, - ERROR = 1 << 2, + FAILURE = 1 << 2, }; inline ReactorEvent operator|(ReactorEvent a, ReactorEvent b) { diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc index 0ac57007..a3ee7309 100644 --- a/candy/src/netstack/session_tcp.cc +++ b/candy/src/netstack/session_tcp.cc @@ -288,7 +288,7 @@ void SessionTcp::shutdownFromStack() { void SessionTcp::onFdEvent(uint32_t events) { ReactorEvent ev = (ReactorEvent)events; if (!this->connected) { - if (ev & ReactorEvent::ERROR) { + if (ev & ReactorEvent::FAILURE) { closeFromReactor(); return; } @@ -299,7 +299,7 @@ void SessionTcp::onFdEvent(uint32_t events) { } // 已连接:优先把可读数据/EOF 读干净(read 返回 0 走优雅排空,返回 -1 走关闭), // 避免 ERROR/HUP 与可读数据同时到达时抢先关闭而丢失尾部数据。 - if (ev & (ReactorEvent::READ | ReactorEvent::ERROR)) { + if (ev & (ReactorEvent::READ | ReactorEvent::FAILURE)) { onFdReadable(); } if (ev & ReactorEvent::WRITE) { diff --git a/candy/src/netstack/session_tcp.h b/candy/src/netstack/session_tcp.h index 50171960..128f2217 100644 --- a/candy/src/netstack/session_tcp.h +++ b/candy/src/netstack/session_tcp.h @@ -10,6 +10,9 @@ #include #include +// Windows:在 lwIP 头之前引入 winsock(见 netstack.h 说明),避免 htonl 宏改写导致声明冲突。 +#include "netstack/sockcompat.h" + #include "lwip/tcp.h" namespace candy { diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index 149769a4..4f73d77b 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -167,7 +167,7 @@ void SessionUdp::shutdownFromStack() { void SessionUdp::onFdEvent(uint32_t events) { ReactorEvent ev = (ReactorEvent)events; - if (ev & ReactorEvent::ERROR) { + if (ev & ReactorEvent::FAILURE) { closeFromReactor(); return; } diff --git a/candy/src/netstack/session_udp.h b/candy/src/netstack/session_udp.h index 5f4a661e..bb63d103 100644 --- a/candy/src/netstack/session_udp.h +++ b/candy/src/netstack/session_udp.h @@ -10,6 +10,9 @@ #include #include +// Windows:在 lwIP 头之前引入 winsock(见 netstack.h 说明),避免 htonl 宏改写导致声明冲突。 +#include "netstack/sockcompat.h" + #include "lwip/udp.h" namespace candy { From ca9f9cfd194899b495ef65670997beb5ed15d528 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Thu, 2 Jul 2026 12:11:32 +0800 Subject: [PATCH 15/29] windows build fix --- candy/src/websocket/server.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/candy/src/websocket/server.cc b/candy/src/websocket/server.cc index 6bfb7325..c6d131ef 100644 --- a/candy/src/websocket/server.cc +++ b/candy/src/websocket/server.cc @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include From bd60826510152dad951fccc7b459ea31d09161e8 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Thu, 2 Jul 2026 12:33:47 +0800 Subject: [PATCH 16/29] refactor tun userspace --- candy/src/tun/tun.cc | 118 ++++++++----------------------------------- candy/src/tun/tun.h | 36 ------------- 2 files changed, 21 insertions(+), 133 deletions(-) diff --git a/candy/src/tun/tun.cc b/candy/src/tun/tun.cc index 7a2ca165..eacfa483 100644 --- a/candy/src/tun/tun.cc +++ b/candy/src/tun/tun.cc @@ -81,12 +81,6 @@ int Tun::handleTunDevice() { return 0; } - // userspace 模式:记录本网关发起(本地 LAN -> 虚拟网)的 TCP 流, - // 以便收到 IPIP 返程包时识别并 L3 转发回本地 LAN,而非误终结。 - if (getClient().getForwardMode() == "userspace") { - trackOutboundFlow(buffer.substr(sizeof(IP4Header))); - } - this->client->getPeerMsgQueue().write(Msg(MsgKind::PACKET, std::move(buffer))); return 0; } @@ -121,25 +115,33 @@ int Tun::handlePacket(Msg msg) { } IP4Header *header = (IP4Header *)msg.data.data(); - // userspace 模式:落地端把"新入站连接"的 IPIP 整包交给 NetStack 终结(参照1), - // 由 NetStack 记录 vnetPeer 并剥 IPIP、喂 lwIP;不写内核 tun。 - // 但若该 IPIP 内层是本网关"发起流"的返程(如 SYN-ACK),本网关是发起端, - // 必须剥 IPIP 后写内核 tun,L3 转发回本地 LAN,而非误喂自己的 lwIP。 - // kernel 模式:保持现状,剥 IPIP 外层后写内核 tun。 - if (getClient().getForwardMode() == "userspace" && header->isIPIP()) { - std::string inner = msg.data.substr(sizeof(IP4Header)); - if (!isReturnFlow(inner)) { + if (header->isIPIP()) { + // 内层源地址落在本机已知路由子网内 => 本机(经本网关)发起流量的返程/中转, + // 剥掉 IPIP 外层后写内核 tun,L3 转发回本地 LAN。 + // 该判定复用本就为路由维护的 sysRtTable,无需额外的发起流跟踪表: + // 「本机能发起到的子网」== 「sysRtTable 里能命中的子网」,O(1) 空间。 + if (msg.data.size() >= sizeof(IP4Header) * 2) { + const IP4Header *inner = header + 1; + std::shared_lock lock(this->sysRtMutex); + for (auto const &rt : sysRtTable) { + if ((inner->saddr & rt.mask) == rt.dst) { + msg.data.erase(0, sizeof(IP4Header)); + write(msg.data); + return 0; + } + } + } + + // 内层源不可路由 => 新入站连接:userspace 模式把 IPIP 整包交给 NetStack 用 lwIP 终结。 + if (getClient().getForwardMode() == "userspace") { getClient().getNetStack().input(std::move(msg.data)); return 0; } - write(inner); - return 0; - } - if (header->isIPIP()) { + // kernel 模式:剥掉 IPIP 外层后写内核 tun。 msg.data.erase(0, sizeof(IP4Header)); - header = (IP4Header *)msg.data.data(); } + write(msg.data); return 0; } @@ -200,82 +202,4 @@ Client &Tun::getClient() { return *this->client; } -// 跟踪 TCP(0x06)/UDP(0x11)/ICMP(0x01) 发起流。返回是否成功解析出 key。 -// TCP 与 UDP 的 L4 头前 4 字节都是 (源端口, 目的端口),端口提取逻辑一致。 -// ICMP 无端口:用 echo 的 id 同时充当 sport/dport,使"请求(type 8)"与 -// "应答(type 0)"在五元组反转后能配对(id 不变,src/dst 互换即对应)。 -static bool parseFlow(const std::string &inner, FlowKey &key) { - if (inner.size() < sizeof(IP4Header)) { - return false; - } - const IP4Header *ip = (const IP4Header *)inner.data(); - if (ip->protocol != 0x06 && ip->protocol != 0x11 && ip->protocol != 0x01) { // TCP / UDP / ICMP - return false; - } - size_t ihl = (ip->version_ihl & 0x0f) * 4; - if (inner.size() < ihl + 4) { - return false; - } - const uint8_t *l4 = (const uint8_t *)inner.data() + ihl; - if (ip->protocol == 0x01) { - // ICMP:仅跟踪 echo request(8) / echo reply(0),其余不跟踪。 - uint8_t type = l4[0]; - if (type != 8 && type != 0) { - return false; - } - if (inner.size() < ihl + 6) { - return false; - } - uint16_t id = ((uint16_t)l4[4] << 8) | l4[5]; - key.src = uint32_t(ip->saddr); - key.dst = uint32_t(ip->daddr); - key.sport = id; - key.dport = id; - key.proto = ip->protocol; - return true; - } - key.src = uint32_t(ip->saddr); - key.dst = uint32_t(ip->daddr); - key.sport = ((uint16_t)l4[0] << 8) | l4[1]; - key.dport = ((uint16_t)l4[2] << 8) | l4[3]; - key.proto = ip->protocol; - return true; -} - -void Tun::trackOutboundFlow(const std::string &innerPacket) { - FlowKey key; - if (!parseFlow(innerPacket, key)) { - return; - } - auto now = std::chrono::steady_clock::now(); - std::unique_lock lock(this->flowMutex); - this->flowTable[key] = now; - // 顺带做惰性老化清理,避免表无限增长(180s 空闲过期)。 - if (this->flowTable.size() > 4096) { - for (auto it = this->flowTable.begin(); it != this->flowTable.end();) { - if (now - it->second > std::chrono::seconds(180)) { - it = this->flowTable.erase(it); - } else { - ++it; - } - } - } -} - -bool Tun::isReturnFlow(const std::string &innerPacket) { - FlowKey in; - if (!parseFlow(innerPacket, in)) { - return false; - } - // 返程包的五元组是发起流的反向:src/dst、sport/dport 互换。 - FlowKey rev{in.dst, in.src, in.dport, in.sport, in.proto}; - std::unique_lock lock(this->flowMutex); - auto it = this->flowTable.find(rev); - if (it == this->flowTable.end()) { - return false; - } - it->second = std::chrono::steady_clock::now(); - return true; -} - } // namespace candy diff --git a/candy/src/tun/tun.h b/candy/src/tun/tun.h index 7a3a2401..81738c16 100644 --- a/candy/src/tun/tun.h +++ b/candy/src/tun/tun.h @@ -5,42 +5,15 @@ #include "core/message.h" #include "core/net.h" #include -#include -#include #include #include #include #include -#include namespace candy { class Client; -// 发起流五元组:用于区分"本网关发起的流"(其返程应 L3 转发回本地 LAN) -// 与"新入站连接"(应在本落地端用 lwIP 终结)。仅 userspace 模式下使用。 -struct FlowKey { - uint32_t src; - uint32_t dst; - uint16_t sport; - uint16_t dport; - uint8_t proto; - - bool operator==(const FlowKey &o) const { - return src == o.src && dst == o.dst && sport == o.sport && dport == o.dport && proto == o.proto; - } -}; - -struct FlowKeyHash { - std::size_t operator()(const FlowKey &k) const noexcept { - std::size_t h = std::hash{}(k.src); - h ^= std::hash{}(k.dst) + 0x9e3779b9 + (h << 6) + (h >> 2); - h ^= std::hash{}((uint32_t(k.sport) << 16) | k.dport) + 0x9e3779b9 + (h << 6) + (h >> 2); - h ^= std::hash{}(k.proto) + 0x9e3779b9 + (h << 6) + (h >> 2); - return h; - } -}; - class Tun { public: Tun(); @@ -83,15 +56,6 @@ class Tun { std::shared_mutex sysRtMutex; std::list sysRtTable; - // userspace 模式发起流跟踪:记录本网关发起(本地 LAN -> 虚拟网)的流, - // 用于判定收到的 IPIP 内层包是"返程"(L3 转发回本地 LAN)还是 - // "新入站连接"(落地端 lwIP 终结)。 - void trackOutboundFlow(const std::string &innerPacket); - bool isReturnFlow(const std::string &innerPacket); - - std::mutex flowMutex; - std::unordered_map flowTable; - private: std::any impl; From e20c380e82fb9ba04b16da95a2236928eb8dd78f Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Thu, 2 Jul 2026 13:25:23 +0800 Subject: [PATCH 17/29] remove libev, use poco --- CMakeLists.txt | 4 - candy/CMakeLists.txt | 3 - candy/src/netstack/reactor.cc | 171 ++++++++++++++-------------------- candy/src/netstack/reactor.h | 38 ++++---- cmake/libev.cmake | 92 ------------------ 5 files changed, 89 insertions(+), 219 deletions(-) delete mode 100644 cmake/libev.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 34c197c8..204fd687 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,10 +86,6 @@ endif() set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) -# libev:事件循环库(reactor 后端,一份代码全平台)。 -# 依赖自定义 fork(Bepartofyou/libev),FetchContent 拉取,不再 vendored,三平台开箱即用。 -include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/libev.cmake) - include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/lwip.cmake) add_subdirectory(candy) diff --git a/candy/CMakeLists.txt b/candy/CMakeLists.txt index b6270896..bdb07613 100644 --- a/candy/CMakeLists.txt +++ b/candy/CMakeLists.txt @@ -19,9 +19,6 @@ target_link_libraries(candy-library PRIVATE Poco::Foundation Poco::JSON Poco::Ne target_link_libraries(candy-library PRIVATE Threads::Threads) target_link_libraries(candy-library PRIVATE lwip) -# libev:reactor 事件循环后端(vendored 源码编译,见 cmake/libev.cmake)。 -target_link_libraries(candy-library PRIVATE Candy::ev) - if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") target_link_libraries(candy-library PRIVATE ws2_32) endif() diff --git a/candy/src/netstack/reactor.cc b/candy/src/netstack/reactor.cc index f3f04259..2920765a 100644 --- a/candy/src/netstack/reactor.cc +++ b/candy/src/netstack/reactor.cc @@ -1,85 +1,72 @@ // SPDX-License-Identifier: MIT #include "netstack/reactor.h" -#include +#include +#include +#include +#include #include namespace candy { -// 每个落地 fd 对应一个 IoWatcher:内嵌 libev 的 ev_io,并携带 fd、所属 Reactor 与事件回调。 -// 仅 reactor 线程创建/销毁/访问。io.data 指回本结构以便在静态回调里取回上下文。 +// 非拥有的 SocketImpl:包裹一个由外部(session)创建并负责关闭的落地 fd。 +// 关键:析构前把 _sockfd 复位为无效句柄(reset() 不做 close),使基类 ~SocketImpl 的 +// close() 成为空操作,避免与 session 的 netClose(fd) 造成双重关闭(fd 可能已被复用)。 +class NonOwningSocketImpl : public Poco::Net::StreamSocketImpl { +public: + explicit NonOwningSocketImpl(int fd) : Poco::Net::StreamSocketImpl(fd) {} + ~NonOwningSocketImpl() override { + reset(); + } +}; + +// 非拥有的 Socket:仅用于把落地 fd 交给 PollSet 监听,不持有 fd 生命周期。 +class NonOwningSocket : public Poco::Net::Socket { +public: + explicit NonOwningSocket(int fd) : Poco::Net::Socket(new NonOwningSocketImpl(fd)) {} +}; + +// 每个落地 fd 对应一个 IoWatcher:持有非拥有的 Poco::Net::Socket、fd 与事件回调。 +// 仅 reactor 线程创建/销毁/访问。 struct IoWatcher { - ev_io io; int fd; - Reactor *reactor; + Poco::Net::Socket socket; Reactor::EventHandler handler; ReactorEvent events; }; -static int toEvEvents(ReactorEvent events) { - int ev = 0; +static int toPocoMode(ReactorEvent events) { + int mode = 0; if (events & ReactorEvent::READ) { - ev |= EV_READ; + mode |= Poco::Net::PollSet::POLL_READ; } if (events & ReactorEvent::WRITE) { - ev |= EV_WRITE; + mode |= Poco::Net::PollSet::POLL_WRITE; } - return ev; + return mode; } -static ReactorEvent fromEvEvents(int revents) { +static ReactorEvent fromPocoMode(int mode) { ReactorEvent events = ReactorEvent::NONE; - if (revents & EV_READ) { + if (mode & Poco::Net::PollSet::POLL_READ) { events = events | ReactorEvent::READ; } - if (revents & EV_WRITE) { + if (mode & Poco::Net::PollSet::POLL_WRITE) { events = events | ReactorEvent::WRITE; } - if (revents & EV_ERROR) { + if (mode & Poco::Net::PollSet::POLL_ERROR) { events = events | ReactorEvent::FAILURE; } return events; } -Reactor::Reactor() : evLoop(nullptr), asyncWatcher(nullptr), running(false) {} +Reactor::Reactor() : pollSet(nullptr), running(false) {} Reactor::~Reactor() { stop(); } -// 按平台显式指定 libev 后端,与 cmake/libev.cmake 编入的 EV_USE_* 严格对齐。 -// 不能用 EVFLAG_AUTO:AUTO 会走 ev_recommended_backends(),而该函数在 __APPLE__ -// 下硬编码剔除 KQUEUE 与 POLL(ev.c 注释 "horribly broken"),只留 SELECT; -// 但我们为缩小风险面把 EV_USE_SELECT 关了,导致 mac 上推荐后端集合为空、 -// ev_loop_new 返回 null(reactor ev_loop_new failed)。 -// reactor 只用后端监听 TCP/UDP socket(utun fd 由 tun 线程单独读写,不进 reactor), -// 普通 socket 上 kqueue 可靠,故这里显式指定 kqueue 绕过推荐过滤。 -static unsigned int reactorBackendFlags() { -#if defined(__APPLE__) - return EVBACKEND_KQUEUE; // 与 EV_USE_KQUEUE=1 对齐 -#elif defined(__linux__) || defined(__ANDROID__) - return EVBACKEND_EPOLL; // 与 EV_USE_EPOLL=1 对齐(musl 下也走 epoll) -#elif defined(_WIN32) - return EVBACKEND_SELECT; // 与 EV_USE_SELECT=1 / EV_SELECT_IS_WINSOCKET 对齐 -#else - return EVFLAG_AUTO; -#endif -} - int Reactor::start() { - // 独立事件循环(不使用默认 loop,避免与进程内其他 libev 使用者冲突)。 - this->evLoop = ev_loop_new(reactorBackendFlags()); - if (this->evLoop == nullptr) { - spdlog::error("reactor ev_loop_new failed"); - return -1; - } - - // ev_async:唯一可跨线程安全触发的 watcher,用作 post() 的唤醒通道。 - ev_async *async = new ev_async(); - ev_async_init(async, &Reactor::asyncCallback); - async->data = this; - ev_async_start(this->evLoop, async); - this->asyncWatcher = async; - + this->pollSet = new Poco::Net::PollSet(); this->running.store(true); this->thread = std::thread([this] { this->loop(); }); // 在启动线程内确定性记录 reactor 线程 id(早于任何跨线程 add/mod/del 访问), @@ -95,30 +82,23 @@ void Reactor::stop() { } return; } - // 唤醒 reactor 线程,async 回调里检测到 !running 会 ev_break 退出 ev_run。 - if (this->asyncWatcher != nullptr) { - ev_async_send(this->evLoop, (ev_async *)this->asyncWatcher); + // 唤醒 reactor 线程:poll 立即返回,循环检测到 !running 后退出。 + if (this->pollSet != nullptr) { + this->pollSet->wakeUp(); } if (this->thread.joinable()) { this->thread.join(); } - // 线程已退出,安全清理剩余 watcher 与 loop(仅本线程访问)。 + // 线程已退出,安全清理剩余 watcher 与 pollSet(仅本线程访问)。 for (auto &kv : this->ioWatchers) { - IoWatcher *iw = (IoWatcher *)kv.second; - ev_io_stop(this->evLoop, &iw->io); - delete iw; + delete (IoWatcher *)kv.second; } this->ioWatchers.clear(); - if (this->asyncWatcher != nullptr) { - ev_async_stop(this->evLoop, (ev_async *)this->asyncWatcher); - delete (ev_async *)this->asyncWatcher; - this->asyncWatcher = nullptr; - } - if (this->evLoop != nullptr) { - ev_loop_destroy(this->evLoop); - this->evLoop = nullptr; + if (this->pollSet != nullptr) { + delete this->pollSet; + this->pollSet = nullptr; } } @@ -156,28 +136,20 @@ int Reactor::del(int fd) { return 0; } -// ===================== 仅 reactor 线程:实际操作 libev watcher ===================== +// ===================== 仅 reactor 线程:实际操作 PollSet ===================== void Reactor::applyAdd(int fd, ReactorEvent events, EventHandler handler) { auto it = this->ioWatchers.find(fd); if (it != this->ioWatchers.end()) { // 已存在:等价于替换 handler 与兴趣集(防御性,正常不应发生)。 IoWatcher *iw = (IoWatcher *)it->second; - ev_io_stop(this->evLoop, &iw->io); iw->handler = std::move(handler); iw->events = events; - ev_io_set(&iw->io, fd, toEvEvents(events)); - ev_io_start(this->evLoop, &iw->io); + this->pollSet->update(iw->socket, toPocoMode(events)); return; } - IoWatcher *iw = new IoWatcher(); - iw->fd = fd; - iw->reactor = this; - iw->handler = std::move(handler); - iw->events = events; - iw->io.data = iw; - ev_io_init(&iw->io, &Reactor::ioCallback, fd, toEvEvents(events)); - ev_io_start(this->evLoop, &iw->io); + IoWatcher *iw = new IoWatcher{fd, NonOwningSocket(fd), std::move(handler), events}; + this->pollSet->add(iw->socket, toPocoMode(events)); this->ioWatchers[fd] = iw; } @@ -190,11 +162,9 @@ void Reactor::applyMod(int fd, ReactorEvent events) { if (iw->events == events) { return; } - // libev 不允许修改运行中的 watcher,需 stop -> set -> start。 - ev_io_stop(this->evLoop, &iw->io); iw->events = events; - ev_io_set(&iw->io, fd, toEvEvents(events)); - ev_io_start(this->evLoop, &iw->io); + // update 为非累积语义:直接覆盖为新的兴趣集(与原 mod 语义一致)。 + this->pollSet->update(iw->socket, toPocoMode(events)); } void Reactor::applyDel(int fd) { @@ -203,7 +173,7 @@ void Reactor::applyDel(int fd) { return; } IoWatcher *iw = (IoWatcher *)it->second; - ev_io_stop(this->evLoop, &iw->io); + this->pollSet->remove(iw->socket); this->ioWatchers.erase(it); delete iw; } @@ -215,8 +185,8 @@ void Reactor::post(Task task) { std::unique_lock lock(this->taskMutex); this->tasks.push(std::move(task)); } - if (this->asyncWatcher != nullptr) { - ev_async_send(this->evLoop, (ev_async *)this->asyncWatcher); + if (this->pollSet != nullptr) { + this->pollSet->wakeUp(); } } @@ -233,27 +203,26 @@ void Reactor::drainTasks() { } } -// ===================== libev 回调跳板 ===================== - -void Reactor::ioCallback(struct ev_loop *, struct ev_io *w, int revents) { - IoWatcher *iw = (IoWatcher *)w->data; - // 关键:先把 handler 拷贝到局部,再调用。handler 内部可能 del(fd) 释放本 IoWatcher, - // 拷贝后调用可避免 use-after-free;回调返回前不再触碰 iw。 - Reactor::EventHandler handler = iw->handler; - handler(fromEvEvents(revents)); -} - -void Reactor::asyncCallback(struct ev_loop *loop, struct ev_async *w, int) { - Reactor *self = (Reactor *)w->data; - self->drainTasks(); - if (!self->running.load()) { - ev_break(loop, EVBREAK_ALL); - } -} - void Reactor::loop() { spdlog::debug("start thread: netstack reactor"); - ev_run(this->evLoop, 0); + // poll 超时仅作兜底;跨线程 post/stop 均通过 wakeUp 立即唤醒,不依赖该超时。 + const Poco::Timespan timeout(1, 0); // 1s + while (this->running.load()) { + Poco::Net::PollSet::SocketModeMap ready = this->pollSet->poll(timeout); + for (auto &kv : ready) { + int fd = (int)kv.first.impl()->sockfd(); + auto it = this->ioWatchers.find(fd); + if (it == this->ioWatchers.end()) { + continue; + } + IoWatcher *iw = (IoWatcher *)it->second; + // 关键:先把 handler 拷贝到局部,再调用。handler 内部可能 del(fd) 释放本 IoWatcher, + // 拷贝后调用可避免 use-after-free;回调返回前不再触碰 iw。 + Reactor::EventHandler handler = iw->handler; + handler(fromPocoMode(kv.second)); + } + drainTasks(); + } spdlog::debug("stop thread: netstack reactor"); } diff --git a/candy/src/netstack/reactor.h b/candy/src/netstack/reactor.h index fa0d58a7..a4cd1456 100644 --- a/candy/src/netstack/reactor.h +++ b/candy/src/netstack/reactor.h @@ -10,10 +10,12 @@ #include #include -// libev 的不透明类型前向声明,避免在头文件暴露 。 -struct ev_loop; -struct ev_io; -struct ev_async; +// Poco::Net::PollSet 的前向声明,避免在头文件暴露 (含 winsock)。 +namespace Poco { +namespace Net { +class PollSet; +} +} // namespace Poco namespace candy { @@ -32,15 +34,17 @@ inline bool operator&(ReactorEvent a, ReactorEvent b) { return (static_cast(a) & static_cast(b)) != 0; } -// Reactor:落地 fd 的事件循环。基于 libev(一份代码全平台:Linux=epoll、macOS/iOS=kqueue、 -// Android=epoll、其余=poll/select 兜底),不再手写各平台后端。管理所有落地 fd(内核 socket) -// 的可读/可写/错误事件,非阻塞、事件回调驱动。 +// Reactor:落地 fd 的事件循环。基于 Poco::Net::PollSet(一份代码全平台:Linux=epoll、 +// macOS/BSD=poll、Windows=wepoll,select 兜底),不再手写各平台后端。管理所有落地 fd(内核 +// socket)的可读/可写/错误事件,非阻塞、事件回调驱动。 // // 线程契约: -// - 事件循环跑在 reactor 自己的线程;libev 除 ev_async_send 外均非线程安全。 -// - 跨线程提交任务用 post():入队后 ev_async_send 唤醒 reactor 线程,由其在 async 回调里抽干执行。 -// - add/mod/del 对 libev watcher 的实际操作必须在 reactor 线程执行:若调用方已在 reactor 线程 -// (事件回调内)则直接应用;否则经 post 投递到 reactor 线程应用,保证线程安全。 +// - 事件循环跑在 reactor 自己的线程;PollSet 的 add/update/remove 自带内部锁,可跨线程调用, +// 但为与原有语义一致并保证回调时序,仍统一在 reactor 线程内实际应用。 +// - 跨线程提交任务用 post():入队后 PollSet::wakeUp 唤醒 reactor 线程,由其在下一轮 poll 后 +// 抽干执行。wakeUp 通过 eventfd(Linux)/pipe(BSD) 实现,线程安全且不丢唤醒。 +// - add/mod/del 对 PollSet 的实际操作必须在 reactor 线程执行:若调用方已在 reactor 线程 +// (事件回调内)则直接应用;否则经 post 投递到 reactor 线程应用,保证 fd->handler 表一致。 class Reactor { public: using EventHandler = std::function; @@ -63,18 +67,14 @@ class Reactor { void drainTasks(); bool onLoopThread() const; - // 以下三个 apply* 只在 reactor 线程执行,直接操作 libev watcher 与 ioWatchers 表。 + // 以下三个 apply* 只在 reactor 线程执行,直接操作 PollSet 与 ioWatchers 表。 void applyAdd(int fd, ReactorEvent events, EventHandler handler); void applyMod(int fd, ReactorEvent events); void applyDel(int fd); - // libev 回调跳板(静态成员,C 兼容签名)。 - static void ioCallback(struct ev_loop *loop, struct ev_io *w, int revents); - static void asyncCallback(struct ev_loop *loop, struct ev_async *w, int revents); - - struct ev_loop *evLoop; - void *asyncWatcher; // ev_async*,堆分配 - // fd -> IoWatcher*(IoWatcher 定义于 .cc)。仅 reactor 线程访问,无需加锁。 + Poco::Net::PollSet *pollSet; + // fd -> IoWatcher*(IoWatcher 定义于 .cc,内含非拥有的 Poco::Net::Socket 与事件回调)。 + // 仅 reactor 线程访问,无需加锁。 std::unordered_map ioWatchers; std::thread thread; diff --git a/cmake/libev.cmake b/cmake/libev.cmake deleted file mode 100644 index 4dc16806..00000000 --- a/cmake/libev.cmake +++ /dev/null @@ -1,92 +0,0 @@ -# libev:事件循环库(reactor 后端,一份代码全平台)。 -# 依赖自定义 fork(Bepartofyou/libev),通过 FetchContent 拉取,不再在仓库内 vendored 源码, -# 也不依赖系统库,保证三平台开箱即用。 -# -# libev 的源码组织很特殊:只需编译 ev.c 一个翻译单元,它在内部按宏条件 -# #include 具体后端(ev_epoll.c / ev_kqueue.c / ev_select.c 等)。因此这里 -# 只把 ev.c 加入编译,并通过 EV_STANDALONE=1 跳过 autotools 的 config.h 探测, -# 改由我们按平台显式指定后端宏。 -# -# 重要陷阱(务必保留这些显式宏): -# 1. EV_STANDALONE 下,epoll 的自动探测条件是 __linux && __GLIBC__, -# 而 Alpine/musl 下 __GLIBC__ 未定义,会退化到 select。故 Linux 必须显式 EV_USE_EPOLL=1。 -# 2. EV_STANDALONE 下 kqueue 默认关闭(EV_USE_KQUEUE 默认 0),故 macOS 必须显式 EV_USE_KQUEUE=1。 -# 3. Windows 用 select 后端,并需 EV_SELECT_IS_WINSOCKET=1 让 select 操作 SOCKET 句柄。 - -include(FetchContent) - -set(LIBEV_GIT_REPOSITORY "https://github.com/Bepartofyou/libev.git") -set(LIBEV_GIT_TAG "a56570ae6d076d7471b945116171c88724ba14a9") - -FetchContent_Declare( - libev - GIT_REPOSITORY ${LIBEV_GIT_REPOSITORY} - GIT_TAG ${LIBEV_GIT_TAG} -) -FetchContent_GetProperties(libev) -if(NOT libev_POPULATED) - FetchContent_Populate(libev) -endif() - -set(LIBEV_DIR ${libev_SOURCE_DIR}) - -add_library(ev STATIC ${LIBEV_DIR}/ev.c) - -# 仅暴露头文件目录;ev_*.c 由 ev.c 内部 include,不单独编译。 -target_include_directories(ev PUBLIC ${LIBEV_DIR}) - -# 公共:standalone(不读 config.h),关闭用不到的特性以缩小跨平台风险面。 -# reactor 仅用到 ev_io / ev_async / ev_run / ev_break,无需 timer/periodic/signal/child/stat/idle/fork/embed。 -target_compile_definitions(ev PUBLIC - EV_STANDALONE=1 - EV_USE_FLOOR=0 # 不依赖 libm 的 floor,使用 libev 内置近似实现 - EV_PERIODIC_ENABLE=0 - EV_STAT_ENABLE=0 - EV_IDLE_ENABLE=0 - EV_FORK_ENABLE=0 - EV_SIGNAL_ENABLE=0 - EV_CHILD_ENABLE=0 - EV_EMBED_ENABLE=0 - EV_ASYNC_ENABLE=1 # 跨线程唤醒(post)依赖 ev_async -) - -if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Android") - # Linux/Android:epoll 后端。musl 下 __GLIBC__ 未定义,必须显式开启 epoll, - # 否则退化到 select。eventfd 用于 ev_async 唤醒;clock 用 libc 的 clock_gettime - # (musl 自带),关闭 glibc 专属的 clock syscall 路径以兼容 musl。 - target_compile_definitions(ev PRIVATE - EV_USE_EPOLL=1 - EV_USE_SELECT=0 - EV_USE_POLL=0 - EV_USE_EVENTFD=1 - EV_USE_CLOCK_SYSCALL=0 - EV_USE_MONOTONIC=1 - EV_USE_REALTIME=1 - EV_USE_NANOSLEEP=1 - ) -elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR ${CMAKE_SYSTEM_NAME} STREQUAL "iOS") - # macOS/iOS:kqueue 后端(standalone 下默认关闭,必须显式开启)。 - target_compile_definitions(ev PRIVATE - EV_USE_KQUEUE=1 - EV_USE_EPOLL=0 - EV_USE_POLL=0 - EV_USE_SELECT=0 - EV_USE_MONOTONIC=1 - EV_USE_REALTIME=1 - EV_USE_NANOSLEEP=1 - ) -elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") - # Windows:select 后端,select 操作 winsock SOCKET 句柄。 - target_compile_definitions(ev PRIVATE - EV_USE_SELECT=1 - EV_SELECT_IS_WINSOCKET=1 - EV_USE_EPOLL=0 - EV_USE_KQUEUE=0 - EV_USE_POLL=0 - EV_USE_MONOTONIC=1 - EV_USE_REALTIME=1 - ) - target_link_libraries(ev PUBLIC ws2_32) -endif() - -add_library(Candy::ev ALIAS ev) From 2f0b70de99dd8cb378f66baefbd123c762bdd3ea Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Thu, 2 Jul 2026 23:09:26 +0800 Subject: [PATCH 18/29] optization, poco version --- CMakeLists.txt | 2 +- candy/src/netstack/reactor.cc | 62 ++++++++++++++++------------------- candy/src/netstack/reactor.h | 12 ++++--- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 204fd687..c7110d52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,7 +78,7 @@ if (${CANDY_STATIC_POCO}) set(ENABLE_ACTIVERECORD_COMPILER OFF CACHE BOOL "" FORCE) set(ENABLE_ZIP OFF CACHE BOOL "" FORCE) set(ENABLE_JWT OFF CACHE BOOL "" FORCE) - Fetch(poco "https://github.com/pocoproject/poco.git" "poco-1.13.3-release") + Fetch(poco "https://github.com/pocoproject/poco.git" "poco-1.14.2-release") else() find_package(Poco REQUIRED COMPONENTS Foundation XML JSON Net NetSSL Util) endif() diff --git a/candy/src/netstack/reactor.cc b/candy/src/netstack/reactor.cc index 2920765a..230de43b 100644 --- a/candy/src/netstack/reactor.cc +++ b/candy/src/netstack/reactor.cc @@ -22,6 +22,9 @@ class NonOwningSocketImpl : public Poco::Net::StreamSocketImpl { // 非拥有的 Socket:仅用于把落地 fd 交给 PollSet 监听,不持有 fd 生命周期。 class NonOwningSocket : public Poco::Net::Socket { public: + // 说明:Poco::Net::Socket(SocketImpl*) 是框架约定的唯一构造入口——SocketImpl 派生自 + // RefCountedObject,Socket 会接管其引用计数并在析构时 release(),故此处 new 出来的 impl + // 生命周期由 Poco 内部托管,并非需要手动 delete 的裸指针。 explicit NonOwningSocket(int fd) : Poco::Net::Socket(new NonOwningSocketImpl(fd)) {} }; @@ -59,14 +62,14 @@ static ReactorEvent fromPocoMode(int mode) { return events; } -Reactor::Reactor() : pollSet(nullptr), running(false) {} +Reactor::Reactor() : running(false) {} Reactor::~Reactor() { stop(); } int Reactor::start() { - this->pollSet = new Poco::Net::PollSet(); + this->pollSet = std::make_unique(); this->running.store(true); this->thread = std::thread([this] { this->loop(); }); // 在启动线程内确定性记录 reactor 线程 id(早于任何跨线程 add/mod/del 访问), @@ -83,23 +86,16 @@ void Reactor::stop() { return; } // 唤醒 reactor 线程:poll 立即返回,循环检测到 !running 后退出。 - if (this->pollSet != nullptr) { + if (this->pollSet) { this->pollSet->wakeUp(); } if (this->thread.joinable()) { this->thread.join(); } - // 线程已退出,安全清理剩余 watcher 与 pollSet(仅本线程访问)。 - for (auto &kv : this->ioWatchers) { - delete (IoWatcher *)kv.second; - } + // 线程已退出,安全清理剩余 watcher 与 pollSet(仅本线程访问)。unique_ptr 自动释放。 this->ioWatchers.clear(); - - if (this->pollSet != nullptr) { - delete this->pollSet; - this->pollSet = nullptr; - } + this->pollSet.reset(); } bool Reactor::onLoopThread() const { @@ -112,8 +108,8 @@ int Reactor::add(int fd, ReactorEvent events, EventHandler handler) { if (onLoopThread()) { applyAdd(fd, events, std::move(handler)); } else { - auto h = std::make_shared(std::move(handler)); - post([this, fd, events, h] { this->applyAdd(fd, events, std::move(*h)); }); + // C++17 init-capture:把 handler 移动进 lambda,无需 shared_ptr 包裹。 + post([this, fd, events, handler = std::move(handler)]() mutable { this->applyAdd(fd, events, std::move(handler)); }); } return 0; } @@ -142,15 +138,15 @@ void Reactor::applyAdd(int fd, ReactorEvent events, EventHandler handler) { auto it = this->ioWatchers.find(fd); if (it != this->ioWatchers.end()) { // 已存在:等价于替换 handler 与兴趣集(防御性,正常不应发生)。 - IoWatcher *iw = (IoWatcher *)it->second; - iw->handler = std::move(handler); - iw->events = events; - this->pollSet->update(iw->socket, toPocoMode(events)); + IoWatcher &iw = *it->second; + iw.handler = std::move(handler); + iw.events = events; + this->pollSet->update(iw.socket, toPocoMode(events)); return; } - IoWatcher *iw = new IoWatcher{fd, NonOwningSocket(fd), std::move(handler), events}; + auto iw = std::make_unique(IoWatcher{fd, NonOwningSocket(fd), std::move(handler), events}); this->pollSet->add(iw->socket, toPocoMode(events)); - this->ioWatchers[fd] = iw; + this->ioWatchers.emplace(fd, std::move(iw)); } void Reactor::applyMod(int fd, ReactorEvent events) { @@ -158,13 +154,13 @@ void Reactor::applyMod(int fd, ReactorEvent events) { if (it == this->ioWatchers.end()) { return; } - IoWatcher *iw = (IoWatcher *)it->second; - if (iw->events == events) { + IoWatcher &iw = *it->second; + if (iw.events == events) { return; } - iw->events = events; + iw.events = events; // update 为非累积语义:直接覆盖为新的兴趣集(与原 mod 语义一致)。 - this->pollSet->update(iw->socket, toPocoMode(events)); + this->pollSet->update(iw.socket, toPocoMode(events)); } void Reactor::applyDel(int fd) { @@ -172,10 +168,9 @@ void Reactor::applyDel(int fd) { if (it == this->ioWatchers.end()) { return; } - IoWatcher *iw = (IoWatcher *)it->second; - this->pollSet->remove(iw->socket); + this->pollSet->remove(it->second->socket); + // erase 会析构 unique_ptr,自动释放,无需手动 delete。 this->ioWatchers.erase(it); - delete iw; } // ===================== 跨线程任务投递 ===================== @@ -185,7 +180,7 @@ void Reactor::post(Task task) { std::unique_lock lock(this->taskMutex); this->tasks.push(std::move(task)); } - if (this->pollSet != nullptr) { + if (this->pollSet) { this->pollSet->wakeUp(); } } @@ -209,17 +204,16 @@ void Reactor::loop() { const Poco::Timespan timeout(1, 0); // 1s while (this->running.load()) { Poco::Net::PollSet::SocketModeMap ready = this->pollSet->poll(timeout); - for (auto &kv : ready) { - int fd = (int)kv.first.impl()->sockfd(); + for (const auto &[socket, mode] : ready) { + int fd = static_cast(socket.impl()->sockfd()); auto it = this->ioWatchers.find(fd); if (it == this->ioWatchers.end()) { continue; } - IoWatcher *iw = (IoWatcher *)it->second; // 关键:先把 handler 拷贝到局部,再调用。handler 内部可能 del(fd) 释放本 IoWatcher, - // 拷贝后调用可避免 use-after-free;回调返回前不再触碰 iw。 - Reactor::EventHandler handler = iw->handler; - handler(fromPocoMode(kv.second)); + // 拷贝后调用可避免 use-after-free;回调返回前不再触碰 it/IoWatcher。 + Reactor::EventHandler handler = it->second->handler; + handler(fromPocoMode(mode)); } drainTasks(); } diff --git a/candy/src/netstack/reactor.h b/candy/src/netstack/reactor.h index a4cd1456..daed66e8 100644 --- a/candy/src/netstack/reactor.h +++ b/candy/src/netstack/reactor.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,10 @@ class PollSet; namespace candy { +// IoWatcher 定义于 .cc:内含非拥有的 Poco::Net::Socket、fd 与事件回调。 +// 此处仅前向声明,头文件不暴露 Poco 类型。 +struct IoWatcher; + enum class ReactorEvent : uint32_t { NONE = 0, READ = 1 << 0, @@ -72,10 +77,9 @@ class Reactor { void applyMod(int fd, ReactorEvent events); void applyDel(int fd); - Poco::Net::PollSet *pollSet; - // fd -> IoWatcher*(IoWatcher 定义于 .cc,内含非拥有的 Poco::Net::Socket 与事件回调)。 - // 仅 reactor 线程访问,无需加锁。 - std::unordered_map ioWatchers; + std::unique_ptr pollSet; + // fd -> IoWatcher(内含非拥有的 Poco::Net::Socket 与事件回调)。仅 reactor 线程访问,无需加锁。 + std::unordered_map> ioWatchers; std::thread thread; std::atomic running; From 798da5353506b492cded519bd2e9ec4144c24114 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Fri, 3 Jul 2026 17:55:20 +0800 Subject: [PATCH 19/29] optimization --- CMakeLists.txt | 9 +++++- candy-cli/src/config.cc | 11 ++++--- candy-cli/src/config.h | 2 +- candy.cfg | 11 ++++--- candy/CMakeLists.txt | 8 ++++- candy/src/candy/client.cc | 2 +- candy/src/core/client.cc | 32 +++++++++---------- candy/src/core/client.h | 17 +++++----- candy/src/core/message.h | 2 +- candy/src/netstack/netstack.cc | 48 ++++++++++++++++++++++++++++ candy/src/netstack/netstack.h | 4 ++- candy/src/netstack/outbound.cc | 6 ++++ candy/src/netstack/sockcompat.h | 7 ++--- candy/src/tun/linux.cc | 10 ++++++ candy/src/tun/macos.cc | 10 ++++++ candy/src/tun/tun.cc | 55 +++++++++++++++++++-------------- candy/src/tun/tun.h | 3 ++ candy/src/tun/unknown.cc | 4 +++ candy/src/tun/windows.cc | 10 ++++++ dockerfile | 5 ++- 20 files changed, 188 insertions(+), 68 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7110d52..2bd68e5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,7 +86,14 @@ endif() set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) -include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/lwip.cmake) +# 用户态协议栈(netstack + lwIP)编译开关。默认关闭:不编译任何 netstack 代码、 +# 不拉取 lwIP。线上发行版打包(部分要求构建时无网络)保持关闭即可零影响。 +# GitHub Release 的纯静态二进制可用 -DCANDY_NETSTACK=ON 开启此功能。 +option(CANDY_NETSTACK "Build the embedded userspace network stack (lwIP)" OFF) +if (${CANDY_NETSTACK}) + add_compile_definitions(CANDY_NETSTACK) + include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/lwip.cmake) +endif() add_subdirectory(candy) add_subdirectory(candy-cli) diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index a0899663..d984090b 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -37,7 +37,7 @@ Poco::JSON::Object arguments::json() { config.set("port", this->port); config.set("vmac", virtualMac(this->name)); config.set("expt", loadTunAddress(this->name)); - config.set("forward-mode", this->forwardMode); + config.set("userspace-stack", this->userspaceStack); } if (this->mode == "server") { @@ -126,7 +126,10 @@ int arguments::parse(int argc, char *argv[]) { "If omitted, auto-detected from the first physical network interface. (client only)") .metavar(""); - program.add_argument("--forward-mode").help("packet forward mode: kernel or userspace"); + program.add_argument("--userspace-stack") + .help("enable the embedded userspace network stack (lwIP) for traffic landing.\n" + "Default is off (kernel stack). Requires a build with CANDY_NETSTACK=ON. (client only)") + .implicit_value(true); program.add_group("Server options"); @@ -185,7 +188,7 @@ int arguments::parse(int argc, char *argv[]) { program.set_if_used("--mtu", this->mtu); program.set_if_used("--discovery", this->discovery); program.set_if_used("--route", this->routeCost); - program.set_if_used("--forward-mode", this->forwardMode); + program.set_if_used("--userspace-stack", this->userspaceStack); bool needShowUsage = [&]() { if (this->mode != "client" && this->mode != "server") @@ -243,7 +246,7 @@ void arguments::parseFile(std::string cfgFile) { {"port", [&](const std::string &value) { this->port = std::stoi(value); }}, {"mtu", [&](const std::string &value) { this->mtu = std::stoi(value); }}, {"localhost", [&](const std::string &value) { this->localhost = value; }}, - {"forward-mode", [&](const std::string &value) { this->forwardMode = value; }}, + {"userspace-stack", [&](const std::string &value) { this->userspaceStack = (value == "true"); }}, }; auto trim = [](std::string str) { if (str.length() >= 2 && str.front() == '\"' && str.back() == '\"') { diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index f8f2772a..e5f27cad 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -31,7 +31,7 @@ struct arguments { int discovery = 0; int routeCost = 0; int mtu = 1400; - std::string forwardMode = "kernel"; + bool userspaceStack = false; }; int saveTunAddress(const std::string &name, const std::string &cidr); diff --git a/candy.cfg b/candy.cfg index 2a7ad572..7577ab62 100644 --- a/candy.cfg +++ b/candy.cfg @@ -66,11 +66,12 @@ route = 5 # [Optional] Maximum Transmission Unit #mtu=1400 -# [Optional] Packet forward mode for landing gateway: "kernel" or "userspace" -# kernel (default): write decapsulated packets to the kernel tun device, relying +# [Optional] Enable the embedded userspace network stack for landing gateway. +# false (default): write decapsulated packets to the kernel tun device, relying # on external iptables MASQUERADE / ip_forward for cross-LAN access. -# userspace: terminate the inner packet with an embedded userspace network stack +# true: terminate the inner packet with an embedded userspace network stack # (lwIP) and dial the target with a local kernel socket, replacing iptables -# MASQUERADE. Phase 1 supports TCP only. -#forward-mode = "kernel" +# MASQUERADE. Requires a build with CANDY_NETSTACK enabled. Phase 1 supports +# TCP/UDP only. +#userspace-stack = false diff --git a/candy/CMakeLists.txt b/candy/CMakeLists.txt index bdb07613..84866245 100644 --- a/candy/CMakeLists.txt +++ b/candy/CMakeLists.txt @@ -1,6 +1,10 @@ add_library(candy-library) file(GLOB_RECURSE SOURCES "src/*.cc") +# 未开启 CANDY_NETSTACK 时,netstack 目录整体不参与编译(默认关闭,零代码编译)。 +if (NOT CANDY_NETSTACK) + list(FILTER SOURCES EXCLUDE REGEX "/src/netstack/") +endif() target_sources(candy-library PRIVATE ${SOURCES}) target_include_directories(candy-library PUBLIC @@ -17,7 +21,9 @@ endif() target_link_libraries(candy-library PRIVATE Poco::Foundation Poco::JSON Poco::Net Poco::NetSSL) target_link_libraries(candy-library PRIVATE Threads::Threads) -target_link_libraries(candy-library PRIVATE lwip) +if (${CANDY_NETSTACK}) + target_link_libraries(candy-library PRIVATE lwip) +endif() if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") target_link_libraries(candy-library PRIVATE ws2_32) diff --git a/candy/src/candy/client.cc b/candy/src/candy/client.cc index 4143be2b..b0e66cfb 100644 --- a/candy/src/candy/client.cc +++ b/candy/src/candy/client.cc @@ -99,7 +99,7 @@ bool run(const std::string &id, const Poco::JSON::Object &config) { client->setRouteCost(config.getValue("route")), client->setMtu(config.getValue("mtu")); client->setPort(config.getValue("port")); client->setLocalhost(config.getValue("localhost")); - client->setForwardMode(config.optValue("forward-mode", "kernel")); + client->setUserspaceStack(config.optValue("userspace-stack", false)); client->run(); } candy::logger().information(Poco::format("run exit: id=%s ", id)); diff --git a/candy/src/core/client.cc b/candy/src/core/client.cc index 8c41a781..0e8f49eb 100644 --- a/candy/src/core/client.cc +++ b/candy/src/core/client.cc @@ -62,16 +62,16 @@ MsgQueue &Client::getWsMsgQueue() { return this->wsMsgQueue; } -NetStack &Client::getNetStack() { - return this->netstack; +MsgQueue &Client::getNetStackMsgQueue() { + return this->netstackMsgQueue; } -std::string Client::getForwardMode() const { - return this->forwardMode; +bool Client::getUserspaceStack() const { + return this->userspaceStack; } int Client::getMtu() const { - return this->mtu; + return this->tun.getMTU(); } void Client::setPassword(const std::string &password) { @@ -116,23 +116,21 @@ void Client::setLocalhost(std::string ip) { } void Client::setMtu(int mtu) { - this->mtu = mtu; tun.setMTU(mtu); } -void Client::setForwardMode(const std::string &mode) { - this->forwardMode = mode; +void Client::setUserspaceStack(bool enable) { + this->userspaceStack = enable; } void Client::run() { this->running.store(true); - if (this->forwardMode == "userspace") { - if (netstack.run(this)) { - return; - } +#ifdef CANDY_NETSTACK + if (netstack.run(this)) { + return; } - +#endif if (ws.run(this)) { return; } @@ -146,14 +144,14 @@ void Client::run() { ws.wait(); tun.wait(); peerManager.wait(); - if (this->forwardMode == "userspace") { - netstack.shutdown(); - netstack.wait(); - } +#ifdef CANDY_NETSTACK + netstack.wait(); +#endif wsMsgQueue.clear(); tunMsgQueue.clear(); peerMsgQueue.clear(); + netstackMsgQueue.clear(); } bool Client::isRunning() { diff --git a/candy/src/core/client.h b/candy/src/core/client.h index 2d16326d..ebd7d8d6 100644 --- a/candy/src/core/client.h +++ b/candy/src/core/client.h @@ -3,11 +3,13 @@ #define CANDY_CORE_CLIENT_H #include "core/message.h" -#include "netstack/netstack.h" #include "peer/manager.h" #include "tun/tun.h" #include "utils/atomic.h" #include "websocket/client.h" +#ifdef CANDY_NETSTACK +#include "netstack/netstack.h" +#endif #include #include #include @@ -43,7 +45,7 @@ class Client { void setExptTunAddress(const std::string &cidr); void setVirtualMac(const std::string &vmac); - void setForwardMode(const std::string &mode); + void setUserspaceStack(bool enable); void run(); bool isRunning(); @@ -60,23 +62,24 @@ class Client { MsgQueue &getTunMsgQueue(); MsgQueue &getPeerMsgQueue(); MsgQueue &getWsMsgQueue(); + MsgQueue &getNetStackMsgQueue(); - NetStack &getNetStack(); - std::string getForwardMode() const; + bool getUserspaceStack() const; int getMtu() const; private: - MsgQueue tunMsgQueue, peerMsgQueue, wsMsgQueue; + MsgQueue tunMsgQueue, peerMsgQueue, wsMsgQueue, netstackMsgQueue; Tun tun; PeerManager peerManager; WebSocketClient ws; +#ifdef CANDY_NETSTACK NetStack netstack; +#endif private: std::string tunName; - std::string forwardMode = "kernel"; - int mtu = 1400; + bool userspaceStack = false; }; } // namespace candy diff --git a/candy/src/core/message.h b/candy/src/core/message.h index fb263ef2..86f2e6c7 100644 --- a/candy/src/core/message.h +++ b/candy/src/core/message.h @@ -16,7 +16,7 @@ enum class MsgKind { TRYP2P, PUBINFO, DISCOVERY, - NETSTACK_RX, + NETSTACK, }; struct Msg { diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index 2d3c0669..abb421c4 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -44,6 +44,13 @@ Outbound &NetStack::getOutbound() { int NetStack::run(Client *client) { this->client = client; + + // userspace 用户态协议栈的启用判定收敛在本模块内部,使 Client::run 里各模块调用 + // 视觉平行;未启用时本模块不启动任何线程、不占用资源。 + if (!client->getUserspaceStack()) { + return 0; + } + this->running.store(true); if (this->reactor.start()) { @@ -67,17 +74,54 @@ int NetStack::run(Client *client) { } spdlog::debug("stop thread: netstack"); }); + + this->msgThread = std::thread([this] { + spdlog::debug("start thread: netstack msg"); + try { + while (getClient()->isRunning()) { + if (handleQueue()) { + break; + } + } + getClient()->shutdown(); + } catch (const std::exception &e) { + spdlog::error("netstack msg thread exception: {}", e.what()); + getClient()->shutdown(); + } + spdlog::debug("stop thread: netstack msg"); + }); return 0; } int NetStack::wait() { + // 内部驱动 stack loop 退出,无需 Client 显式先调 shutdown(),保持与其他模块 + // 「run + wait」两段式调用对称。未启用 userspace 时线程均未创建,join 为空操作。 + shutdown(); if (this->stackThread.joinable()) { this->stackThread.join(); } + if (this->msgThread.joinable()) { + this->msgThread.join(); + } this->reactor.stop(); return 0; } +int NetStack::handleQueue() { + Msg msg = this->client->getNetStackMsgQueue().read(); + switch (msg.kind) { + case MsgKind::TIMEOUT: + break; + case MsgKind::NETSTACK: + input(std::move(msg.data)); + break; + default: + spdlog::warn("unexcepted netstack message type: {}", static_cast(msg.kind)); + break; + } + return 0; +} + void NetStack::shutdown() { this->running.store(false); this->stackTaskCond.notify_all(); @@ -102,6 +146,10 @@ int NetStack::initStack() { static std::once_flag lwipInitFlag; std::call_once(lwipInitFlag, [] { lwip_init(); }); + // 这里的 addr/mask 是 lwIP 内部 netif 的占位地址,不是 candy 的虚拟网卡地址, + // 二者无需匹配:本 netif 配合下方 NETIF_FLAG_PRETEND_TCP 作为「捕获全部」网卡, + // 把喂进来的 IPIP 内层包无条件交给 lwIP 协议栈终结(lwIP 不会用这个地址回 ARP, + // 也不会据此做路由判定)。选 10.255.255.254/32 只是取一个不与业务网段冲突的占位值。 ip4_addr_t addr, mask, gw; IP4_ADDR(&addr, 10, 255, 255, 254); IP4_ADDR(&mask, 255, 255, 255, 255); diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index 028d54ca..dbc6da42 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -46,7 +46,6 @@ class NetStack { // 把待终结的 IP 包投递给 NetStack(来自 tun 落地分支,含 IPIP 外层)。 void input(std::string packet); - // 供 Session 使用:在 NetStack 线程内执行任务(保证 lwIP API 线程安全)。 void postToStack(std::function task); Reactor &getReactor(); @@ -69,6 +68,8 @@ class NetStack { int initStack(); void teardownStack(); void loop(); + // 读取本模块 MsgQueue(MsgKind::NETSTACK 落地入站包),驱动 input()。 + int handleQueue(); void handleInput(std::string packet); void feedToLwip(const std::string &innerPacket, IP4 vnetPeer); // UDP 伪会话空闲超时回收(仅 NetStack 线程,loop 中节流调用)。 @@ -90,6 +91,7 @@ class NetStack { Client *client; std::thread stackThread; + std::thread msgThread; std::atomic running; struct netif lwipNetif; diff --git a/candy/src/netstack/outbound.cc b/candy/src/netstack/outbound.cc index 5b9a1ecf..a9c6884c 100644 --- a/candy/src/netstack/outbound.cc +++ b/candy/src/netstack/outbound.cc @@ -3,6 +3,12 @@ #include "netstack/sockcompat.h" #include +// 说明(回应 review:优先用 Poco 封装):此处属「不得已」而使用平台相关裸 socket。 +// 原因:落地 fd 的生命周期由 Reactor 按裸 int fd 统一管理(注册/注销/关闭), +// 而 Poco::Net::StreamSocket/DatagramSocket 会自行持有并在析构时关闭 fd, +// 与 Reactor 的 fd 所有权模型冲突,若混用会导致 double-close。故这里只借用 +// 系统 socket() 拿到裸 fd 交给 Reactor,平台差异已收敛在 sockcompat.h 内, +// 本文件仅剩 sockaddr_in 组装这一处必须用平台头。 #if defined(__linux__) || defined(__APPLE__) #include #include diff --git a/candy/src/netstack/sockcompat.h b/candy/src/netstack/sockcompat.h index 9acbc708..7cea01c0 100644 --- a/candy/src/netstack/sockcompat.h +++ b/candy/src/netstack/sockcompat.h @@ -6,8 +6,8 @@ // 使 session_tcp / session_udp 的落地收发逻辑三端共用同一份代码。 // 约定:对外统一以 int 表示 socket(Windows SOCKET 为小整数句柄,转换安全)。 -#include #include +#include #if defined(_WIN32) || defined(_WIN64) #ifndef _WIN32_WINNT @@ -16,8 +16,7 @@ #include #include #else -#include -#include +#include // errno 是 C 标准库唯一入口,无 C++ 等价,属不得已保留。 #include #include #include @@ -95,7 +94,7 @@ inline std::string netErrStr(int err) { #if defined(_WIN32) || defined(_WIN64) return "wsa error " + std::to_string(err); #else - return std::strerror(err); + return std::generic_category().message(err); #endif } diff --git a/candy/src/tun/linux.cc b/candy/src/tun/linux.cc index f41857f4..3a28a77d 100644 --- a/candy/src/tun/linux.cc +++ b/candy/src/tun/linux.cc @@ -44,6 +44,10 @@ class LinuxTun { return 0; } + int getMTU() { + return this->mtu; + } + // 配置网卡,设置路由 int up() { this->tunFd = open("/dev/net/tun", O_RDWR); @@ -262,6 +266,12 @@ int Tun::setMTU(int mtu) { return 0; } +int Tun::getMTU() const { + std::shared_ptr tun; + tun = std::any_cast>(this->impl); + return tun->getMTU(); +} + int Tun::up() { std::shared_ptr tun; tun = std::any_cast>(this->impl); diff --git a/candy/src/tun/macos.cc b/candy/src/tun/macos.cc index 895c1189..a0a30ed1 100644 --- a/candy/src/tun/macos.cc +++ b/candy/src/tun/macos.cc @@ -53,6 +53,10 @@ class MacTun { return 0; } + int getMTU() { + return this->mtu; + } + int up() { // 创建设备,操作系统不允许自定义设备名,只能由内核分配 this->tunFd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL); @@ -318,6 +322,12 @@ int Tun::setMTU(int mtu) { return 0; } +int Tun::getMTU() const { + std::shared_ptr tun; + tun = std::any_cast>(this->impl); + return tun->getMTU(); +} + int Tun::up() { std::shared_ptr tun; tun = std::any_cast>(this->impl); diff --git a/candy/src/tun/tun.cc b/candy/src/tun/tun.cc index eacfa483..3e51f742 100644 --- a/candy/src/tun/tun.cc +++ b/candy/src/tun/tun.cc @@ -115,37 +115,44 @@ int Tun::handlePacket(Msg msg) { } IP4Header *header = (IP4Header *)msg.data.data(); - if (header->isIPIP()) { - // 内层源地址落在本机已知路由子网内 => 本机(经本网关)发起流量的返程/中转, - // 剥掉 IPIP 外层后写内核 tun,L3 转发回本地 LAN。 - // 该判定复用本就为路由维护的 sysRtTable,无需额外的发起流跟踪表: - // 「本机能发起到的子网」== 「sysRtTable 里能命中的子网」,O(1) 空间。 - if (msg.data.size() >= sizeof(IP4Header) * 2) { - const IP4Header *inner = header + 1; - std::shared_lock lock(this->sysRtMutex); - for (auto const &rt : sysRtTable) { - if ((inner->saddr & rt.mask) == rt.dst) { - msg.data.erase(0, sizeof(IP4Header)); - write(msg.data); - return 0; - } - } - } - - // 内层源不可路由 => 新入站连接:userspace 模式把 IPIP 整包交给 NetStack 用 lwIP 终结。 - if (getClient().getForwardMode() == "userspace") { - getClient().getNetStack().input(std::move(msg.data)); - return 0; - } +#ifdef CANDY_NETSTACK + // userspace 模式下的新入站连接:内层源不可路由的 IPIP 包整包交给 NetStack 用 lwIP + // 终结。这一分支不剥壳、不写内核 tun,直接经 MsgQueue 转交 netstack 模块(跨模块 + // 数据一律走 MsgQueue,不直接持有 netstack)。判定完成后提前返回,下方只保留唯一 + // 的一处 erase + write。未编译 CANDY_NETSTACK 时本分支不存在,一律走内核转发。 + if (header->isIPIP() && !isReturnTraffic(*header, msg.data.size()) && getClient().getUserspaceStack()) { + this->client->getNetStackMsgQueue().write(Msg(MsgKind::NETSTACK, std::move(msg.data))); + return 0; + } +#endif - // kernel 模式:剥掉 IPIP 外层后写内核 tun。 + // 其余情况都要写内核 tun: + // - IPIP 返程/中转,或 kernel 模式的 IPIP 入站:剥掉 IPIP 外层(20B)后写。 + // - 非 IPIP 的普通包:原样写。 + if (header->isIPIP()) { msg.data.erase(0, sizeof(IP4Header)); } - write(msg.data); return 0; } +bool Tun::isReturnTraffic(const IP4Header &header, size_t size) { + // 内层源地址落在本机已知路由子网内 => 本机(经本网关)发起流量的返程/中转。 + // 复用本就为路由维护的 sysRtTable:「本机能发起到的子网」== 「sysRtTable 命中的 + // 子网」,O(1) 空间,无需额外发起流跟踪表。 + if (size < sizeof(IP4Header) * 2) { + return false; + } + const IP4Header *inner = &header + 1; + std::shared_lock lock(this->sysRtMutex); + for (auto const &rt : sysRtTable) { + if ((inner->saddr & rt.mask) == rt.dst) { + return true; + } + } + return false; +} + int Tun::handleTunAddr(Msg msg) { if (setAddress(msg.data)) { return -1; diff --git a/candy/src/tun/tun.h b/candy/src/tun/tun.h index 81738c16..826e5c05 100644 --- a/candy/src/tun/tun.h +++ b/candy/src/tun/tun.h @@ -21,6 +21,7 @@ class Tun { int setName(const std::string &name); int setMTU(int mtu); + int getMTU() const; int run(Client *client); int wait(); @@ -36,6 +37,8 @@ class Tun { // 处理来自消息队列的数据 int handleTunQueue(); int handlePacket(Msg msg); + // IPIP 内层源是否落在本机路由子网内(返程/中转流量判定)。 + bool isReturnTraffic(const IP4Header &header, std::size_t size); int handleTunAddr(Msg msg); int handleSysRt(Msg msg); diff --git a/candy/src/tun/unknown.cc b/candy/src/tun/unknown.cc index c04ea6a7..4b4cc678 100644 --- a/candy/src/tun/unknown.cc +++ b/candy/src/tun/unknown.cc @@ -23,6 +23,10 @@ int Tun::setMTU(int mtu) { return -1; } +int Tun::getMTU() { + return -1; +} + int Tun::up() { return -1; } diff --git a/candy/src/tun/windows.cc b/candy/src/tun/windows.cc index 703659bf..1d2d71d0 100644 --- a/candy/src/tun/windows.cc +++ b/candy/src/tun/windows.cc @@ -115,6 +115,10 @@ class WindowsTun { return 0; } + int getMTU() { + return this->mtu; + } + int up() { if (!Holder::Ok()) { candy::logger().fatal("init wintun failed"); @@ -315,6 +319,12 @@ int Tun::setMTU(int mtu) { return 0; } +int Tun::getMTU() const { + std::shared_ptr tun; + tun = std::any_cast>(this->impl); + return tun->getMTU(); +} + int Tun::up() { std::shared_ptr tun; tun = std::any_cast>(this->impl); diff --git a/dockerfile b/dockerfile index b70f7d75..f886b40a 100644 --- a/dockerfile +++ b/dockerfile @@ -4,8 +4,11 @@ RUN apk add openssl poco FROM base AS build RUN apk add git cmake ninja pkgconf g++ openssl-dev poco-dev linux-headers +# 用户态协议栈编译开关,默认 OFF:官方发行镜像零影响。 +# 回归/需要 userspace 落地时用 --build-arg CANDY_NETSTACK=ON 构建。 +ARG CANDY_NETSTACK=OFF COPY . candy -RUN cd candy && cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo && cmake --build build && cmake --install build +RUN cd candy && cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCANDY_NETSTACK=${CANDY_NETSTACK} && cmake --build build && cmake --install build FROM base AS product RUN install -D /dev/null /var/lib/candy/lost From 587e6f78301c2ea4fb6b7813b8e00a0d01c6127e Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Fri, 3 Jul 2026 21:23:59 +0800 Subject: [PATCH 20/29] fix config --- candy-cli/src/config.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index d984090b..0a0c1598 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -127,8 +127,8 @@ int arguments::parse(int argc, char *argv[]) { .metavar(""); program.add_argument("--userspace-stack") - .help("enable the embedded userspace network stack (lwIP) for traffic landing.\n" - "Default is off (kernel stack). Requires a build with CANDY_NETSTACK=ON. (client only)") + .help("terminate landing traffic with the built-in userspace network stack (lwIP)\n" + "instead of the kernel stack. Default is false (kernel). (client only)") .implicit_value(true); program.add_group("Server options"); From 32daa3a3dea2213c7275f72c6e3995bc07ec7977 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Mon, 6 Jul 2026 14:50:24 +0800 Subject: [PATCH 21/29] remove --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 545e5e74..b101ac94 100644 --- a/.gitignore +++ b/.gitignore @@ -34,5 +34,3 @@ # CMake Build files .cache build -build-mac -.trae \ No newline at end of file From e365ebe6b77101b09211afcdb0a7af2e0fe8b24b Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Thu, 9 Jul 2026 18:55:01 +0800 Subject: [PATCH 22/29] update lwip --- cmake/lwip.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/lwip.cmake b/cmake/lwip.cmake index 8e255de4..b93476a6 100644 --- a/cmake/lwip.cmake +++ b/cmake/lwip.cmake @@ -1,7 +1,7 @@ include(FetchContent) set(LWIP_GIT_REPOSITORY "https://github.com/Bepartofyou/lwip.git") -set(LWIP_GIT_TAG "2ce295cc41ecbfcb7c71eae35ae3e018b408fc5b") +set(LWIP_GIT_TAG "9b6fe14f0a44c477e10bf0e68da56b3498a91352") FetchContent_Declare( lwip From 6fb4267e9a835f7ad29085e65f8399268fcdc3ae Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Fri, 10 Jul 2026 12:03:35 +0800 Subject: [PATCH 23/29] fix us->us use 5-tuple --- candy/src/tun/tun.cc | 121 ++++++++++++++++++++++++++++++++++++++++++- candy/src/tun/tun.h | 47 ++++++++++++++++- 2 files changed, 166 insertions(+), 2 deletions(-) diff --git a/candy/src/tun/tun.cc b/candy/src/tun/tun.cc index 3e51f742..c6431d9e 100644 --- a/candy/src/tun/tun.cc +++ b/candy/src/tun/tun.cc @@ -69,6 +69,14 @@ int Tun::handleTunDevice() { return IP4(); }(); if (!nextHop.empty()) { +#ifdef CANDY_NETSTACK + // 发起端仅在 userspace 模式记录正向流:内核模式做 SNAT 且不喂 lwIP,无需跟踪, + // 门控可省纯 kernel 网关的内存/CPU。落地端据此表用反向五元组区分新入站/返程。 + // 必须在 insert 封 IPIP 前记录:此时 buffer 仍是原始内层单层包。 + if (getClient().getUserspaceStack()) { + recordInitiatedFlow(buffer); + } +#endif buffer.insert(0, sizeof(IP4Header), 0); header = (IP4Header *)buffer.data(); header->protocol = 0x04; @@ -120,7 +128,7 @@ int Tun::handlePacket(Msg msg) { // 终结。这一分支不剥壳、不写内核 tun,直接经 MsgQueue 转交 netstack 模块(跨模块 // 数据一律走 MsgQueue,不直接持有 netstack)。判定完成后提前返回,下方只保留唯一 // 的一处 erase + write。未编译 CANDY_NETSTACK 时本分支不存在,一律走内核转发。 - if (header->isIPIP() && !isReturnTraffic(*header, msg.data.size()) && getClient().getUserspaceStack()) { + if (header->isIPIP() && !isReturnByFlow(*header, msg.data.size()) && getClient().getUserspaceStack()) { this->client->getNetStackMsgQueue().write(Msg(MsgKind::NETSTACK, std::move(msg.data))); return 0; } @@ -153,6 +161,117 @@ bool Tun::isReturnTraffic(const IP4Header &header, size_t size) { return false; } +bool Tun::parseInnerL4(const uint8_t *ipPkt, std::size_t len, FlowKey &out) { + // 从一个裸 IPv4 包解析 TCP/UDP 五元组。所有偏移访问前都做长度校验防越界。 + if (len < sizeof(IP4Header)) { + return false; + } + const IP4Header *ip = (const IP4Header *)ipPkt; + if ((ip->version_ihl >> 4) != 4) { + return false; + } + // 仅跟踪 TCP(6)/UDP(17):其余(如 ICMP)交回退启发式处理。 + if (ip->protocol != 0x06 && ip->protocol != 0x11) { + return false; + } + // 分片:非首片(offset!=0)没有 L4 头,按 IHL 偏移读到的是 payload,会构造出垃圾键, + // 导致同一数据报首片/余片判定不一致、重组崩坏。一律跳过(返回 false 回退)。 + uint16_t fragOff = ntoh(ip->frag_off); + if ((fragOff & 0x1FFF) != 0) { + return false; + } + std::size_t ihl = (ip->version_ihl & 0x0F) * 4; + if (ihl < sizeof(IP4Header)) { + return false; + } + // 源/目的端口都在 L4 头前 4 字节,需保证 IP 头 + 4 字节可读。 + if (len < ihl + 4) { + return false; + } + const uint8_t *l4 = ipPkt + ihl; + uint16_t srcPort, dstPort; + std::memcpy(&srcPort, l4, sizeof(uint16_t)); + std::memcpy(&dstPort, l4 + 2, sizeof(uint16_t)); + // 端口按网络字节序原样存(记录与查询表示一致即可,无需 ntoh)。 + out.proto = ip->protocol; + out.srcIP = ip->saddr; + out.srcPort = srcPort; + out.dstIP = ip->daddr; + out.dstPort = dstPort; + return true; +} + +void Tun::recordInitiatedFlow(const std::string &origPacket) { + // 发起端:本机 LAN 主动发起、经本网关封 IPIP 转发的正向流。按内层五元组记录, + // 返程回来时落地端用反向五元组匹配到本条 => 判返程走内核。 + FlowKey key; + if (!parseInnerL4((const uint8_t *)origPacket.data(), origPacket.size(), key)) { + return; + } + auto now = std::chrono::steady_clock::now(); + std::unique_lock lock(this->flowMutex); + this->initiatedFlows[key] = now; + reapIdleFlows(); +} + +bool Tun::isReturnByFlow(const IP4Header &header, size_t size) { + // 落地端:区分「别人主动发起的新入站」(喂 lwIP) 与「本机发起流的返程」(走内核)。 + if (size < sizeof(IP4Header) * 2) { + return false; + } + const uint8_t *inner = (const uint8_t *)(&header + 1); + std::size_t innerLen = size - sizeof(IP4Header); + FlowKey fwd; + if (!parseInnerL4(inner, innerLen, fwd)) { + // 非 TCP/UDP(如 ICMP)或分片:无法做五元组精确匹配,回退旧的子网启发式, + // 保持 ICMP 等现状零回归。 + return isReturnTraffic(header, size); + } + // 用入站包的反向五元组还原发起方向:入站是 peer->self,发起过则表里有 self->peer。 + FlowKey rev; + rev.proto = fwd.proto; + rev.srcIP = fwd.dstIP; + rev.srcPort = fwd.dstPort; + rev.dstIP = fwd.srcIP; + rev.dstPort = fwd.srcPort; + std::unique_lock lock(this->flowMutex); + auto it = this->initiatedFlows.find(rev); + if (it != this->initiatedFlows.end()) { + // 命中:本机之前发起过该流,此入站包是其返程;刷新时间戳(长连接保活)。 + it->second = std::chrono::steady_clock::now(); + return true; + } + return false; +} + +void Tun::reapIdleFlows() { + // 调用方已持有 flowMutex。节流:每 ~10s 才真正扫描一次,避免每包遍历。 + auto now = std::chrono::steady_clock::now(); + bool overCapacity = this->initiatedFlows.size() > 4096; + if (!overCapacity && now - this->lastFlowReap < std::chrono::seconds(10)) { + return; + } + this->lastFlowReap = now; + // 空闲 > 300s 的流剔除:返程通常秒级到达,长连接的正向包会持续刷新时间戳。 + for (auto it = this->initiatedFlows.begin(); it != this->initiatedFlows.end();) { + if (now - it->second > std::chrono::seconds(300)) { + it = this->initiatedFlows.erase(it); + } else { + ++it; + } + } + // 容量兜底:清理过期项后仍超限(如 LAN 设备扫描/伪造多目的),淘汰最旧项防内存爆炸。 + while (this->initiatedFlows.size() > 4096) { + auto oldest = this->initiatedFlows.begin(); + for (auto it = this->initiatedFlows.begin(); it != this->initiatedFlows.end(); ++it) { + if (it->second < oldest->second) { + oldest = it; + } + } + this->initiatedFlows.erase(oldest); + } +} + int Tun::handleTunAddr(Msg msg) { if (setAddress(msg.data)) { return -1; diff --git a/candy/src/tun/tun.h b/candy/src/tun/tun.h index 826e5c05..8fca6faf 100644 --- a/candy/src/tun/tun.h +++ b/candy/src/tun/tun.h @@ -5,15 +5,43 @@ #include "core/message.h" #include "core/net.h" #include +#include +#include #include +#include #include #include #include +#include namespace candy { class Client; +// 发起流五元组:唯一标识一条「本机 LAN 主动发起、经本网关封 IPIP 转发」的流。 +// 各字段按网络字节序原样存储(记录与查询表示一致即可,无需 ntoh 转换)。 +struct FlowKey { + uint8_t proto; // 6=TCP, 17=UDP + uint32_t srcIP; + uint16_t srcPort; + uint32_t dstIP; + uint16_t dstPort; + bool operator==(const FlowKey &o) const { + return proto == o.proto && srcIP == o.srcIP && srcPort == o.srcPort && dstIP == o.dstIP && dstPort == o.dstPort; + } +}; + +// net.h 只提供了 hash,FlowKey 需自定义 hash。 +struct FlowKeyHash { + std::size_t operator()(const FlowKey &k) const { + std::size_t h = std::hash{}(k.srcIP); + h ^= std::hash{}(k.dstIP) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash{}((uint32_t(k.srcPort) << 16) | k.dstPort) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash{}(k.proto) + 0x9e3779b9 + (h << 6) + (h >> 2); + return h; + } +}; + class Tun { public: Tun(); @@ -37,8 +65,17 @@ class Tun { // 处理来自消息队列的数据 int handleTunQueue(); int handlePacket(Msg msg); - // IPIP 内层源是否落在本机路由子网内(返程/中转流量判定)。 + // IPIP 内层源是否落在本机路由子网内(返程/中转流量判定)。非 TCP/UDP(如 ICMP) + // 场景下作为 isReturnByFlow 的回退启发式使用。 bool isReturnTraffic(const IP4Header &header, std::size_t size); + // 基于发起流跟踪表的返程判定:TCP/UDP 用反向五元组精确匹配,其余回退 isReturnTraffic。 + bool isReturnByFlow(const IP4Header &header, std::size_t size); + // 从一个 IPv4 包解析 TCP/UDP 五元组(含分片/越界校验);成功填 out 返回 true。 + bool parseInnerL4(const uint8_t *ipPkt, std::size_t len, FlowKey &out); + // 发起端封 IPIP 前记录本机 LAN 主动发起的正向流(仅 TCP/UDP)。 + void recordInitiatedFlow(const std::string &origPacket); + // 老化清理:剔除空闲超时的发起流,并对表容量兜底。 + void reapIdleFlows(); int handleTunAddr(Msg msg); int handleSysRt(Msg msg); @@ -59,6 +96,14 @@ class Tun { std::shared_mutex sysRtMutex; std::list sysRtTable; + // 发起流跟踪表:记录本机 LAN 主动发起、经本网关封 IPIP 转发的正向流。 + // 落地端据此用反向五元组区分「别人发起的新入站」(喂 lwIP) 与「本机发起流的返程」 + // (走内核)。tunThread 写、msgThread 读,用独立 mutex 保护(临界区仅 hash 操作, + // 不得与 sysRtMutex 嵌套持有以防死锁)。 + std::mutex flowMutex; + std::unordered_map initiatedFlows; + std::chrono::steady_clock::time_point lastFlowReap; + private: std::any impl; From afba6c493fabe0ac585f58fdaf684e34fc6d7cc1 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sat, 11 Jul 2026 18:09:01 +0800 Subject: [PATCH 24/29] per udp socket with src ip/port --- candy/src/netstack/netstack.cc | 19 ++++++---- candy/src/netstack/netstack.h | 9 +++-- candy/src/netstack/outbound.cc | 16 ++++---- candy/src/netstack/outbound.h | 10 +++-- candy/src/netstack/session_udp.cc | 63 +++++++++++++++++++------------ candy/src/netstack/session_udp.h | 38 +++++++++++-------- candy/src/netstack/sockcompat.h | 54 +++++++++++++++++++++++++- 7 files changed, 143 insertions(+), 66 deletions(-) diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index abb421c4..160c7a20 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -452,10 +452,11 @@ err_t NetStack::onAccept(struct tcp_pcb *newpcb, err_t err) { } void NetStack::onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) { - // 首包路径:lwIP 已为该流克隆出已 connect 的 npcb(pcb 参数), - // addr/port = 目的(dev2 服务),pcb->remote_ip/remote_port = 源端(dev1)。 - // 我们建立伪会话并在 npcb 上注册每流 recv handler,然后**不释放 p**: - // lwIP 收尾会 goto again 把同一数据报按已连接 npcb 重投,交由每流 handler 处理。 + // 首包路径:lwIP 已为该「源二元组」克隆出 npcb(pcb 参数), + // addr/port = 首包目的,pcb->remote_ip/remote_port = 源端(dev1)。 + // 全锥形:clone 天然「一源一 npcb」(只按源聚合),故 onUdpRecv 对每个源仅触发一次, + // 无需 find-or-create。会话只按源二元组建立;后续各目的由每流 handler 从 pcb->local_* + // per-packet 读取。建会话后**不释放 p**:lwIP 收尾 goto again 把该数据报重投到每流 handler。 if (p == nullptr) { return; } @@ -472,17 +473,19 @@ void NetStack::onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *a std::memcpy(&origSrc, &ip_2_ip4(&pcb->remote_ip)->addr, sizeof(uint32_t)); uint16_t origSrcPort = pcb->remote_port; - spdlog::debug("netstack onUdpRecv: {}:{} -> {}:{}", origSrc.toString(), origSrcPort, origDst.toString(), origDstPort); + // 目的仅用于首包日志(真实目的为 per-packet),会话本体只认源二元组。 + spdlog::debug("netstack onUdpRecv src {}:{} (first dst {}:{})", origSrc.toString(), origSrcPort, origDst.toString(), + origDstPort); - auto session = std::make_shared(this, pcb, origSrc, origSrcPort, origDst, origDstPort); + auto session = std::make_shared(this, pcb, origSrc, origSrcPort); if (session->start()) { udp_recv(pcb, nullptr, nullptr); udp_remove(pcb); pbuf_free(p); return; } - this->udpSessions[session->key()] = session; - // 不释放 p:交还 lwIP(goto again 重投到已连接 npcb 的每流 handler)。 + this->udpSessions[session->key()] = session; // key = 源二元组 + // 不释放 p:交还 lwIP(goto again 重投到该 npcb 的每流 handler)。 } } // namespace candy diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index dbc6da42..42bcffa8 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -59,7 +59,7 @@ class NetStack { // Session 结束时从会话表移除。 void removeSession(struct tcp_pcb *pcb); - // UDP 伪会话结束时从 UDP 会话表移除(按四元组 key)。仅 NetStack 线程调用。 + // UDP 会话结束时从 UDP 会话表移除(按源二元组 key)。仅 NetStack 线程调用。 void removeUdpSession(const std::string &key); Client *getClient(); @@ -84,7 +84,8 @@ class NetStack { err_t onNetifInit(struct netif *netif); err_t onOutput(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr); err_t onAccept(struct tcp_pcb *newpcb, err_t err); - // UDP 数据报到达:addr/port=源端(dev1),pcb->local=目的(落地服务)。仅 NetStack 线程。 + // UDP 数据报到达:addr/port=源端(dev1),pcb->local=首包目的。全锥形下 clone 一源一 npcb, + // 故对每个源仅触发一次,会话按源二元组建立。仅 NetStack 线程。 void onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); private: @@ -96,7 +97,7 @@ class NetStack { struct netif lwipNetif; struct tcp_pcb *listenPcb; - // UDP 捕获所有目的的伪监听 pcb(绑定 netif、bind(NULL,0),收首包克隆出 connect 的 npcb)。 + // UDP 捕获所有目的的伪监听 pcb(绑定 netif、bind(NULL,0),收首包克隆出按源聚合的 npcb)。 struct udp_pcb *udpListenPcb; Reactor reactor; @@ -120,7 +121,7 @@ class NetStack { std::mutex sessionMutex; std::unordered_map> sessions; - // UDP 伪会话表:四元组 key -> SessionUdp。仅 NetStack 线程访问,无需加锁。 + // UDP 会话表:源二元组 key -> SessionUdp。仅 NetStack 线程访问,无需加锁。 std::unordered_map> udpSessions; }; diff --git a/candy/src/netstack/outbound.cc b/candy/src/netstack/outbound.cc index a9c6884c..966a4445 100644 --- a/candy/src/netstack/outbound.cc +++ b/candy/src/netstack/outbound.cc @@ -45,7 +45,7 @@ int DirectOutbound::dialTcp(const Endpoint &dst) { #endif } -int DirectOutbound::dialUdp(const Endpoint &dst) { +int DirectOutbound::dialUdp() { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) int fd = (int)::socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { @@ -54,14 +54,12 @@ int DirectOutbound::dialUdp(const Endpoint &dst) { } netSetNonBlocking(fd); - // connect 固定对端:内核自动填源地址 = 网关 LAN IP(等价 MASQUERADE), - // 且后续 send/recv 只与该对端往来,形成 Fullcone NAT 的伪会话。 - struct sockaddr_in addr = {}; - addr.sin_family = AF_INET; - addr.sin_port = htons(dst.port); - addr.sin_addr.s_addr = uint32_t(dst.host); - if (::connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { - spdlog::warn("direct outbound udp connect {}:{} failed: {}", dst.host.toString(), dst.port, netErrStr(netLastError())); + // 全锥形(Full Cone):不 connect 固定对端。bind(INADDR_ANY,0) 让内核立即分配一个 + // 固定出口端口,全生命周期不变;对所有目的复用同一 (出口IP,端口)(endpoint-independent + // mapping),后续 sendto 发往任意目的、recvfrom 收任意对端回包。出口源 IP 仍由内核按 + // 默认路由自动填为本网关出口 IP(等价 MASQUERADE)。 + if (netBindAny(fd) != 0) { + spdlog::warn("direct outbound udp bind failed: {}", netErrStr(netLastError())); netClose(fd); return -1; } diff --git a/candy/src/netstack/outbound.h b/candy/src/netstack/outbound.h index c422ea5f..2eadcbb7 100644 --- a/candy/src/netstack/outbound.h +++ b/candy/src/netstack/outbound.h @@ -36,9 +36,11 @@ class Outbound { return -1; } - // L4 终结型出站:为一条已终结的 UDP 流建立 connect 到目的的非阻塞 fd,失败返回 -1。 - // 默认不支持。 - virtual int dialUdp(const Endpoint &dst) { + // L4 终结型出站:为一条已终结的 UDP 流建立落地出口 fd。 + // 全锥形(Full Cone):不 connect 固定目的,而是 bind 一个固定出口端口的 unconnected + // socket,后续用 sendto 发往任意多目的、用 recvfrom 收任意对端回包,返回非阻塞 fd, + // 失败返回 -1。默认不支持。 + virtual int dialUdp() { return -1; } }; @@ -52,7 +54,7 @@ class DirectOutbound : public Outbound { } int dialTcp(const Endpoint &dst) override; - int dialUdp(const Endpoint &dst) override; + int dialUdp() override; }; } // namespace candy diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index 4f73d77b..f031728f 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -17,14 +17,12 @@ namespace candy { -SessionUdp::SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort, IP4 origDst, uint16_t origDstPort) - : Session(stack), pcb(pcb), fd(-1), origSrc(origSrc), origSrcPort(origSrcPort), origDst(origDst), origDstPort(origDstPort), +SessionUdp::SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort) + : Session(stack), pcb(pcb), fd(-1), origSrc(origSrc), origSrcPort(origSrcPort), lastActiveTs(std::chrono::steady_clock::now()) { - // 四元组 key:源IP:源端口 -> 目的IP:目的端口 + // 源二元组 key:源IP:源端口(全锥形下一源一会话,目的 per-packet 不入 key) this->sessionKey.assign((const char *)&origSrc, sizeof(uint32_t)); this->sessionKey.append((const char *)&origSrcPort, sizeof(origSrcPort)); - this->sessionKey.append((const char *)&origDst, sizeof(uint32_t)); - this->sessionKey.append((const char *)&origDstPort, sizeof(origDstPort)); } SessionUdp::~SessionUdp() { @@ -38,16 +36,15 @@ int SessionUdp::start() { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) this->self = shared_from_this(); - // 落地拨号:内核 socket 直连(DirectOutbound)。connect 固定对端,内核自动填源 - // 地址 = 网关 LAN IP(等价 MASQUERADE),形成 Fullcone NAT 伪会话。 + // 落地拨号:内核 socket 直连(DirectOutbound)。全锥形——unconnected + 固定出口端口, + // 后续用 sendto 发往任意目的、recvfrom 收任意对端回包。出口源 IP 由内核填为网关出口 IP。 Outbound &outbound = this->stack->getOutbound(); - this->fd = outbound.dialUdp(Endpoint{this->origDst, this->origDstPort}); + this->fd = outbound.dialUdp(); if (this->fd < 0) { return -1; } - spdlog::debug("session udp: {}:{} -> {}:{}", this->origSrc.toString(), this->origSrcPort, this->origDst.toString(), - this->origDstPort); + spdlog::debug("session udp src: {}:{}", this->origSrc.toString(), this->origSrcPort); auto holder = shared_from_this(); int fd = this->fd; @@ -70,22 +67,26 @@ void SessionUdp::recvTrampoline(void *arg, struct udp_pcb *pcb, struct pbuf *p, } void SessionUdp::onPcbRecv(struct pbuf *p, const ip_addr_t *addr, u16_t port) { - // 本流后续数据报:addr/port=源端(dev1),数据负载转发到落地 fd。 + // 本流后续数据报:addr/port=源端(dev1)。目的由 lwIP 在回调前覆写进 pcb->local_*, + // 必须此刻同步读取(下一入站包会再次覆写),随数据一并按值投递到 reactor。 if (p == nullptr) { return; } + uint32_t dstIpBe; + std::memcpy(&dstIpBe, &ip_2_ip4(&this->pcb->local_ip)->addr, sizeof(uint32_t)); + uint16_t dstPortHost = this->pcb->local_port; // lwIP 端口为主机序 std::string data; data.resize(p->tot_len); pbuf_copy_partial(p, data.data(), p->tot_len, 0); pbuf_free(p); - sendToLanding(std::move(data)); + sendToLanding(dstIpBe, dstPortHost, std::move(data)); } -void SessionUdp::sendToLanding(std::string data) { - // 仅 NetStack 线程调用:刷新活跃时间,把数据报投递到 reactor 线程发送。 +void SessionUdp::sendToLanding(uint32_t dstIpBe, uint16_t dstPortHost, std::string data) { + // 仅 NetStack 线程调用:刷新活跃时间,把数据报连同目的投递到 reactor 线程用 sendto 发送。 this->lastActiveTs = std::chrono::steady_clock::now(); auto holder = shared_from_this(); - this->stack->getReactor().post([holder, data = std::move(data)]() mutable { + this->stack->getReactor().post([holder, dstIpBe, dstPortHost, data = std::move(data)]() mutable { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) if (holder->closing.load()) { return; @@ -93,17 +94,18 @@ void SessionUdp::sendToLanding(std::string data) { if (holder->fd < 0) { return; } - long n = netSend(holder->fd, data.data(), data.size()); + long n = netSendTo(holder->fd, data.data(), data.size(), dstIpBe, dstPortHost); if (n < 0 && !netWouldBlock(netLastError())) { - spdlog::debug("session udp send failed: {}", netErrStr(netLastError())); + spdlog::debug("session udp sendto failed: {}", netErrStr(netLastError())); } #endif }); } -void SessionUdp::replyToLwip(std::string data) { - // 仅 NetStack 线程调用:把落地回包以 (origDst:origDstPort -> origSrc:origSrcPort) - // 重新注入 lwIP,由 netif->output 产生内层 IP 包、再经 NetStack::output 封 IPIP 回源端。 +void SessionUdp::replyToLwip(uint32_t peerIpBe, uint16_t peerPortHost, std::string data) { + // 仅 NetStack 线程调用:把落地回包以 (实际对端 -> origSrc:origSrcPort) 重新注入 lwIP, + // 由 netif->output 产生内层 IP 包、再经 NetStack::output 封 IPIP 回源端。 + // 全锥形:回注源用「实际对端」(可为 P2P 对端/STUN),而非固定落地目的,支撑打洞回包。 if (this->pcb == nullptr) { return; } @@ -116,10 +118,14 @@ void SessionUdp::replyToLwip(std::string data) { } pbuf_take(p, data.data(), (u16_t)data.size()); - // 源地址固定为落地目的(origDst),源端口为 origDstPort;目的为源端(origSrc:origSrcPort)。 + // udp_sendto_if_src 的 UDP 源端口取自 pcb->local_port(非参数),故注入前先写为对端端口。 + // pretend pcb 的 local_* 不参与入站匹配(只认 remote_*),且下一正向包会被 lwIP 再次覆写,安全。 + this->pcb->local_port = peerPortHost; + + // 源地址=实际对端(peerIp:peerPort);目的=源端(origSrc:origSrcPort)。 ip_addr_t src; ip_addr_t dst; - ip_addr_set_ip4_u32(&src, uint32_t(this->origDst)); + ip_addr_set_ip4_u32(&src, peerIpBe); ip_addr_set_ip4_u32(&dst, uint32_t(this->origSrc)); err_t e = udp_sendto_if_src(this->pcb, p, &dst, this->origSrcPort, &this->stack->getNetif(), &src); if (e != ERR_OK) { @@ -180,11 +186,17 @@ void SessionUdp::onFdReadable() { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) char buf[65536]; while (true) { - long n = netRecv(this->fd, buf, sizeof(buf)); + // 全锥形:unconnected socket 用 recvfrom 收任意对端回包,同时取回真实对端地址/端口, + // 连同数据投递到 NetStack 线程,由 replyToLwip 以该对端为源注入回 dev1(支撑打洞回包)。 + uint32_t peerIpBe; + uint16_t peerPortHost; + long n = netRecvFrom(this->fd, buf, sizeof(buf), &peerIpBe, &peerPortHost); if (n > 0) { std::string data(buf, n); auto holder = shared_from_this(); - this->stack->postToStack([holder, data = std::move(data)]() mutable { holder->replyToLwip(std::move(data)); }); + this->stack->postToStack([holder, peerIpBe, peerPortHost, data = std::move(data)]() mutable { + holder->replyToLwip(peerIpBe, peerPortHost, std::move(data)); + }); continue; } if (n == 0) { @@ -193,7 +205,8 @@ void SessionUdp::onFdReadable() { if (netWouldBlock(netLastError())) { return; } - // 落地 socket 错误(如 ICMP port unreachable 经 connect 返回 ECONNREFUSED):关闭会话。 + // 落地 socket 错误:unconnected 下罕见(对称型 connect socket 才会经 ICMP 收到 + // ECONNREFUSED),保留关闭分支兜底,空闲回收主要交由 reapIdleUdpSessions。 closeFromReactor(); return; } diff --git a/candy/src/netstack/session_udp.h b/candy/src/netstack/session_udp.h index bb63d103..331791c6 100644 --- a/candy/src/netstack/session_udp.h +++ b/candy/src/netstack/session_udp.h @@ -17,8 +17,17 @@ namespace candy { -// SessionUdp:一条 UDP "伪会话"——把 lwIP 终结的某个 (源,目的) 四元组数据流, -// 映射到一个 connect 到目的的内核 UDP socket 落地(Fullcone NAT 思路)。 +// SessionUdp:一条 UDP 源端点会话——把 lwIP 终结的某个源二元组 (源IP,源端口) 的全部 +// 出站数据流,映射到一个 unconnected 的内核 UDP socket 落地,实现全锥形(Full Cone)NAT。 +// +// 全锥形要点: +// - 落地 fd 不 connect 固定目的,bind 一个固定出口端口后用 sendto 发往任意多目的、 +// 用 recvfrom 收任意对端回包 → endpoint-independent mapping + filtering。 +// - lwIP 的 PRETEND netif 天然「一源一 npcb」:同一源二元组无论发往多少不同目的都命中 +// 同一 npcb,每个数据报的目的由 lwIP 覆写进 pcb->local_ip/local_port,故目的是 per-packet +// 从回调现场读取,不属于会话本体。 +// - 回包注入时用「实际对端」作源地址、并临时写 npcb->local_port 作 UDP 源端口,从而把来自 +// 陌生对端(P2P 对端 / STUN)的包反向注入回源端,支撑 UDP 打洞。 // // 线程契约(与 SessionTcp 一致): // - 所有 udp_*(pcb) 调用只在 NetStack 线程执行; @@ -26,13 +35,13 @@ namespace candy { // - 两侧通过 NetStack::postToStack / Reactor::post 投递任务跨线程交互。 // // 数据流: -// 正向 lwIP->fd:udp_recv 回调(NetStack线程) 收数据报 -> 投递 reactor send(fd) -// 反向 fd->lwIP:fd 可读(reactor线程) recv -> 投递 NetStack udp_sendto_if_src 回送源端 +// 正向 lwIP->fd:udp_recv 回调(NetStack线程) 收数据报 + 读 pcb->local_* 得目的 -> 投递 reactor sendto(fd) +// 反向 fd->lwIP:fd 可读(reactor线程) recvfrom 得真实对端 -> 投递 NetStack udp_sendto_if_src 回送源端 class SessionUdp : public Session, public std::enable_shared_from_this { public: - // pcb:lwIP 为该四元组克隆出的已 connect 的 udp_pcb(NetStack 线程持有)。 - // origSrc/origSrcPort:源端(dev1);origDst/origDstPort:落地目的(dev2 服务)。 - SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort, IP4 origDst, uint16_t origDstPort); + // pcb:lwIP 为该源二元组克隆出的 npcb(NetStack 线程持有,其 local_* 随每个入站包被 lwIP + // 覆写为当前目的)。origSrc/origSrcPort:源端(dev1)。 + SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort); ~SessionUdp() override; int start() override; @@ -40,10 +49,11 @@ class SessionUdp : public Session, public std::enable_shared_from_thislocal_* 读得) + // 投递到落地 fd,用 sendto 发往该目的。 + void sendToLanding(uint32_t dstIpBe, uint16_t dstPortHost, std::string data); - // 该会话的四元组 key(NetStack 线程的会话表用)。 + // 该会话的源二元组 key(NetStack 线程的会话表用)。 const std::string &key() const { return this->sessionKey; } @@ -63,10 +73,10 @@ class SessionUdp : public Session, public std::enable_shared_from_thislocal_* 读取,投递到落地 fd 发送。 void onPcbRecv(struct pbuf *p, const ip_addr_t *addr, u16_t port); - // 落地 fd 收到回包后,用 pcb 把数据报回送源端。 - void replyToLwip(std::string data); + // 落地 fd 收到某对端(peerIp:peerPort)回包后,用 pcb 以该对端为源把数据报注入回源端。 + void replyToLwip(uint32_t peerIpBe, uint16_t peerPortHost, std::string data); void closeFromStack(); private: @@ -75,8 +85,6 @@ class SessionUdp : public Session, public std::enable_shared_from_this #include #else -#include // errno 是 C 标准库唯一入口,无 C++ 等价,属不得已保留。 +#include // htons/ntohs(无连接收发的端口字节序转换) +#include // errno 是 C 标准库唯一入口,无 C++ 等价,属不得已保留。 #include +#include // struct sockaddr_in / INADDR_ANY(无连接收发地址组装) #include #include #endif @@ -62,6 +64,56 @@ inline long netSend(int fd, const void *buf, size_t len) { #endif } +// 无连接发(全锥形出口用):目的地址(网络序 s_addr) + 目的端口(主机序)。 +// 返回写出字节数;<0 表示错误(含 would-block)。 +inline long netSendTo(int fd, const void *buf, size_t len, uint32_t dstIpBe, uint16_t dstPortHost) { + struct sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = htons(dstPortHost); + addr.sin_addr.s_addr = dstIpBe; +#if defined(_WIN32) || defined(_WIN64) + return ::sendto((SOCKET)fd, (const char *)buf, (int)len, 0, (struct sockaddr *)&addr, sizeof(addr)); +#else + return ::sendto(fd, buf, len, 0, (struct sockaddr *)&addr, sizeof(addr)); +#endif +} + +// 无连接收(全锥形出口用):返回读到字节数;0 表示无数据;<0 表示错误(含 would-block)。 +// 同时回填对端地址(网络序 s_addr)与端口(主机序)。 +inline long netRecvFrom(int fd, void *buf, size_t len, uint32_t *outIpBe, uint16_t *outPortHost) { + struct sockaddr_in addr = {}; +#if defined(_WIN32) || defined(_WIN64) + int alen = (int)sizeof(addr); + long n = ::recvfrom((SOCKET)fd, (char *)buf, (int)len, 0, (struct sockaddr *)&addr, &alen); +#else + socklen_t alen = sizeof(addr); + long n = ::recvfrom(fd, buf, len, 0, (struct sockaddr *)&addr, &alen); +#endif + if (n >= 0) { + if (outIpBe != nullptr) { + *outIpBe = addr.sin_addr.s_addr; + } + if (outPortHost != nullptr) { + *outPortHost = ntohs(addr.sin_port); + } + } + return n; +} + +// 绑定到 (INADDR_ANY, 0):让内核立即分配一个固定出口端口,全生命周期内不变, +// 对所有目的复用同一 (出口IP,端口),实现全锥形 endpoint-independent mapping。成功返回 0。 +inline int netBindAny(int fd) { + struct sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = 0; + addr.sin_addr.s_addr = INADDR_ANY; +#if defined(_WIN32) || defined(_WIN64) + return ::bind((SOCKET)fd, (struct sockaddr *)&addr, sizeof(addr)) == 0 ? 0 : -1; +#else + return ::bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0 ? 0 : -1; +#endif +} + // 取本线程最近一次 socket 错误码。 inline int netLastError() { #if defined(_WIN32) || defined(_WIN64) From 998ebec718ce14a43c1f7334cf924e6b8ae4d477 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Sat, 11 Jul 2026 18:59:40 +0800 Subject: [PATCH 25/29] netstack: migrate logging from spdlog to Poco MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rebase 到已完成 spdlog→Poco 迁移的上游后,netstack 模块(本次 UDP 全锥形改造新增)仍停留在 spdlog API,导致 CANDY_NETSTACK=ON 编译失败 (spdlog/spdlog.h 头缺失)。 将 6 个 netstack 文件的日志统一迁移到项目 Poco 封装: - spdlog::{debug,warn,error,critical,info} -> candy::logger().{debug,warning,error,fatal,information} - 格式化从 spdlog 的 "{}" 改为 Poco::format 的 printf 风格占位符 - 按参数类型精确匹配格式符(%s/%hu/%zu/%d),bool 与 tcp_sndbuf 返回值用 (int) 显式转换,指针用 (void*)+%p,避免 Poco::format 运行时类型不匹配([ERRFMT]) --- candy/src/netstack/netstack.cc | 56 ++++++++++++++++--------------- candy/src/netstack/outbound.cc | 12 ++++--- candy/src/netstack/reactor.cc | 6 ++-- candy/src/netstack/session_tcp.cc | 29 +++++++++------- candy/src/netstack/session_udp.cc | 11 +++--- candy/src/netstack/sockcompat.h | 4 +-- 6 files changed, 64 insertions(+), 54 deletions(-) diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index 160c7a20..ab01e165 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -4,10 +4,11 @@ #include "core/message.h" #include "netstack/session_tcp.h" #include "netstack/session_udp.h" +#include "utils/log.h" +#include #include #include #include -#include #include #include "lwip/init.h" @@ -54,29 +55,29 @@ int NetStack::run(Client *client) { this->running.store(true); if (this->reactor.start()) { - spdlog::critical("netstack reactor start failed"); + candy::logger().fatal("netstack reactor start failed"); return -1; } this->stackThread = std::thread([this] { - spdlog::debug("start thread: netstack"); + candy::logger().debug("start thread: netstack"); try { if (initStack()) { - spdlog::critical("netstack init failed"); + candy::logger().fatal("netstack init failed"); getClient()->shutdown(); } else { loop(); teardownStack(); } } catch (const std::exception &e) { - spdlog::error("netstack thread exception: {}", e.what()); + candy::logger().error(Poco::format("netstack thread exception: %s", std::string(e.what()))); getClient()->shutdown(); } - spdlog::debug("stop thread: netstack"); + candy::logger().debug("stop thread: netstack"); }); this->msgThread = std::thread([this] { - spdlog::debug("start thread: netstack msg"); + candy::logger().debug("start thread: netstack msg"); try { while (getClient()->isRunning()) { if (handleQueue()) { @@ -85,10 +86,10 @@ int NetStack::run(Client *client) { } getClient()->shutdown(); } catch (const std::exception &e) { - spdlog::error("netstack msg thread exception: {}", e.what()); + candy::logger().error(Poco::format("netstack msg thread exception: %s", std::string(e.what()))); getClient()->shutdown(); } - spdlog::debug("stop thread: netstack msg"); + candy::logger().debug("stop thread: netstack msg"); }); return 0; } @@ -116,7 +117,7 @@ int NetStack::handleQueue() { input(std::move(msg.data)); break; default: - spdlog::warn("unexcepted netstack message type: {}", static_cast(msg.kind)); + candy::logger().warning(Poco::format("unexcepted netstack message type: %d", static_cast(msg.kind))); break; } return 0; @@ -156,7 +157,7 @@ int NetStack::initStack() { ip4_addr_set_any(&gw); if (netif_add(&this->lwipNetif, &addr, &mask, &gw, this, netifInitTrampoline, ip_input) == nullptr) { - spdlog::critical("netstack netif_add failed"); + candy::logger().fatal("netstack netif_add failed"); return -1; } netif_set_up(&this->lwipNetif); @@ -175,17 +176,17 @@ int NetStack::initStack() { struct tcp_pcb *pcb = tcp_new_ip_type(IPADDR_TYPE_ANY); if (pcb == nullptr) { - spdlog::critical("netstack tcp_new failed"); + candy::logger().fatal("netstack tcp_new failed"); return -1; } tcp_bind_netif(pcb, &this->lwipNetif); if (tcp_bind(pcb, nullptr, 0) != ERR_OK) { - spdlog::critical("netstack tcp_bind failed"); + candy::logger().fatal("netstack tcp_bind failed"); return -1; } this->listenPcb = tcp_listen(pcb); if (this->listenPcb == nullptr) { - spdlog::critical("netstack tcp_listen failed"); + candy::logger().fatal("netstack tcp_listen failed"); return -1; } tcp_arg(this->listenPcb, this); @@ -196,12 +197,12 @@ int NetStack::initStack() { // 已 connect 源端的 npcb 并通过回调交给我们,由此建立四元组伪会话。 this->udpListenPcb = udp_new_ip_type(IPADDR_TYPE_ANY); if (this->udpListenPcb == nullptr) { - spdlog::critical("netstack udp_new failed"); + candy::logger().fatal("netstack udp_new failed"); return -1; } udp_bind_netif(this->udpListenPcb, &this->lwipNetif); if (udp_bind(this->udpListenPcb, nullptr, 0) != ERR_OK) { - spdlog::critical("netstack udp_bind failed"); + candy::logger().fatal("netstack udp_bind failed"); return -1; } udp_recv(this->udpListenPcb, udpRecvTrampoline, this); @@ -288,7 +289,8 @@ void NetStack::reapIdleUdpSessions() { } } if (reaped > 0) { - spdlog::debug("netstack reap idle udp sessions: {} reaped, {} remain", reaped, this->udpSessions.size()); + candy::logger().debug( + Poco::format("netstack reap idle udp sessions: %d reaped, %zu remain", reaped, this->udpSessions.size())); } } @@ -306,8 +308,8 @@ void NetStack::handleInput(std::string packet) { return; } IP4Header *inner = (IP4Header *)packet.data(); - spdlog::debug("netstack input: vnetPeer={} {} -> {} proto={}", vnetPeer.toString(), inner->saddr.toString(), - inner->daddr.toString(), (int)inner->protocol); + candy::logger().debug(Poco::format("netstack input: vnetPeer=%s %s -> %s proto=%d", vnetPeer.toString(), + inner->saddr.toString(), inner->daddr.toString(), (int)inner->protocol)); // 本次增量发版仅支持 userspace TCP/UDP 组网:非 TCP(0x06)/UDP(0x11) 一律丢弃 // (ICMP 等待后续迭代再支持;PRETEND netif 本也只接受 TCP/UDP)。 if (inner->protocol != 0x06 && inner->protocol != 0x11) { @@ -336,13 +338,13 @@ void NetStack::feedToLwip(const std::string &innerPacket, IP4 vnetPeer) { struct pbuf *p = pbuf_alloc(PBUF_RAW, innerPacket.size(), PBUF_RAM); if (p == nullptr) { - spdlog::warn("netstack pbuf_alloc failed"); + candy::logger().warning("netstack pbuf_alloc failed"); return; } pbuf_take(p, innerPacket.data(), innerPacket.size()); err_t e = this->lwipNetif.input(p, &this->lwipNetif); if (e != ERR_OK) { - spdlog::warn("netstack netif.input failed: {}", (int)e); + candy::logger().warning(Poco::format("netstack netif.input failed: %d", (int)e)); pbuf_free(p); } } @@ -362,7 +364,7 @@ void NetStack::output(const std::string &innerPacket) { } } if (vnetPeer.empty()) { - spdlog::warn("netstack output drop: no vnetPeer for {}", inner->daddr.toString()); + candy::logger().warning(Poco::format("netstack output drop: no vnetPeer for %s", inner->daddr.toString())); return; } @@ -424,15 +426,15 @@ err_t NetStack::onOutput(struct netif *netif, struct pbuf *p, const ip4_addr_t * pbuf_copy_partial(p, buffer.data(), p->tot_len, 0); if (buffer.size() >= sizeof(IP4Header)) { IP4Header *h = (IP4Header *)buffer.data(); - spdlog::debug("netstack onOutput: {} -> {} proto={} len={}", h->saddr.toString(), h->daddr.toString(), (int)h->protocol, - buffer.size()); + candy::logger().debug(Poco::format("netstack onOutput: %s -> %s proto=%d len=%zu", h->saddr.toString(), + h->daddr.toString(), (int)h->protocol, buffer.size())); } output(buffer); return ERR_OK; } err_t NetStack::onAccept(struct tcp_pcb *newpcb, err_t err) { - spdlog::debug("netstack onAccept: err={} pcb={}", (int)err, (void *)newpcb); + candy::logger().debug(Poco::format("netstack onAccept: err=%d pcb=%p", (int)err, (void *)newpcb)); if (err != ERR_OK || newpcb == nullptr) { return ERR_VAL; } @@ -474,8 +476,8 @@ void NetStack::onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *a uint16_t origSrcPort = pcb->remote_port; // 目的仅用于首包日志(真实目的为 per-packet),会话本体只认源二元组。 - spdlog::debug("netstack onUdpRecv src {}:{} (first dst {}:{})", origSrc.toString(), origSrcPort, origDst.toString(), - origDstPort); + candy::logger().debug(Poco::format("netstack onUdpRecv src %s:%hu (first dst %s:%hu)", origSrc.toString(), origSrcPort, + origDst.toString(), origDstPort)); auto session = std::make_shared(this, pcb, origSrc, origSrcPort); if (session->start()) { diff --git a/candy/src/netstack/outbound.cc b/candy/src/netstack/outbound.cc index 966a4445..e31603b1 100644 --- a/candy/src/netstack/outbound.cc +++ b/candy/src/netstack/outbound.cc @@ -1,7 +1,8 @@ // SPDX-License-Identifier: MIT #include "netstack/outbound.h" #include "netstack/sockcompat.h" -#include +#include "utils/log.h" +#include // 说明(回应 review:优先用 Poco 封装):此处属「不得已」而使用平台相关裸 socket。 // 原因:落地 fd 的生命周期由 Reactor 按裸 int fd 统一管理(注册/注销/关闭), @@ -23,7 +24,7 @@ int DirectOutbound::dialTcp(const Endpoint &dst) { // 建 socket + 非阻塞 + 发起 connect 到 dst。EINPROGRESS 视为成功。 int fd = (int)::socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { - spdlog::warn("direct outbound tcp socket failed: {}", netErrStr(netLastError())); + candy::logger().warning(Poco::format("direct outbound tcp socket failed: %s", netErrStr(netLastError()))); return -1; } netSetNonBlocking(fd); @@ -35,7 +36,8 @@ int DirectOutbound::dialTcp(const Endpoint &dst) { int ret = ::connect(fd, (struct sockaddr *)&addr, sizeof(addr)); if (ret != 0 && !netInProgress(netLastError())) { - spdlog::warn("direct outbound tcp connect {}:{} failed: {}", dst.host.toString(), dst.port, netErrStr(netLastError())); + candy::logger().warning(Poco::format("direct outbound tcp connect %s:%hu failed: %s", dst.host.toString(), dst.port, + netErrStr(netLastError()))); netClose(fd); return -1; } @@ -49,7 +51,7 @@ int DirectOutbound::dialUdp() { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) int fd = (int)::socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { - spdlog::warn("direct outbound udp socket failed: {}", netErrStr(netLastError())); + candy::logger().warning(Poco::format("direct outbound udp socket failed: %s", netErrStr(netLastError()))); return -1; } netSetNonBlocking(fd); @@ -59,7 +61,7 @@ int DirectOutbound::dialUdp() { // mapping),后续 sendto 发往任意目的、recvfrom 收任意对端回包。出口源 IP 仍由内核按 // 默认路由自动填为本网关出口 IP(等价 MASQUERADE)。 if (netBindAny(fd) != 0) { - spdlog::warn("direct outbound udp bind failed: {}", netErrStr(netLastError())); + candy::logger().warning(Poco::format("direct outbound udp bind failed: %s", netErrStr(netLastError()))); netClose(fd); return -1; } diff --git a/candy/src/netstack/reactor.cc b/candy/src/netstack/reactor.cc index 230de43b..7e6cb675 100644 --- a/candy/src/netstack/reactor.cc +++ b/candy/src/netstack/reactor.cc @@ -1,10 +1,10 @@ // SPDX-License-Identifier: MIT #include "netstack/reactor.h" +#include "utils/log.h" #include #include #include #include -#include namespace candy { @@ -199,7 +199,7 @@ void Reactor::drainTasks() { } void Reactor::loop() { - spdlog::debug("start thread: netstack reactor"); + candy::logger().debug("start thread: netstack reactor"); // poll 超时仅作兜底;跨线程 post/stop 均通过 wakeUp 立即唤醒,不依赖该超时。 const Poco::Timespan timeout(1, 0); // 1s while (this->running.load()) { @@ -217,7 +217,7 @@ void Reactor::loop() { } drainTasks(); } - spdlog::debug("stop thread: netstack reactor"); + candy::logger().debug("stop thread: netstack reactor"); } } // namespace candy diff --git a/candy/src/netstack/session_tcp.cc b/candy/src/netstack/session_tcp.cc index a3ee7309..4c50d16f 100644 --- a/candy/src/netstack/session_tcp.cc +++ b/candy/src/netstack/session_tcp.cc @@ -2,9 +2,10 @@ #include "netstack/session_tcp.h" #include "netstack/netstack.h" #include "netstack/sockcompat.h" +#include "utils/log.h" +#include #include #include -#include #include "lwip/pbuf.h" #include "lwip/tcp.h" @@ -54,7 +55,8 @@ int SessionTcp::start() { return -1; } - spdlog::debug("session tcp: {} -> {}:{}", this->origSrc.toString(), this->origDst.toString(), this->origDstPort); + candy::logger().debug( + Poco::format("session tcp: %s -> %s:%hu", this->origSrc.toString(), this->origDst.toString(), this->origDstPort)); auto holder = shared_from_this(); int fd = this->fd; @@ -110,9 +112,9 @@ err_t SessionTcp::onRecv(struct tcp_pcb *pcb, struct pbuf *p, err_t err) { // 背压埋点(lwIP->fd):记录本次收量、forwardBuf 积压、当前未确认窗口(pendingRecved)。 // forwardBuf 应稳定在一个 TCP_WND 内;若持续增长说明落地端写不动、背压失效。 - spdlog::debug("[bp][fwd] onRecv {} -> {}:{} recv={} forwardBuf={} pendingRecved={} sndwnd={}", this->origSrc.toString(), - this->origDst.toString(), this->origDstPort, recvLen, bufSize, this->pendingRecved.load(), - (this->pcb ? tcp_sndbuf(this->pcb) : 0)); + candy::logger().debug(Poco::format("[bp][fwd] onRecv %s -> %s:%hu recv=%hu forwardBuf=%zu pendingRecved=%zu sndwnd=%d", + this->origSrc.toString(), this->origDst.toString(), this->origDstPort, recvLen, bufSize, + this->pendingRecved.load(), (int)(this->pcb ? tcp_sndbuf(this->pcb) : 0))); auto holder = shared_from_this(); this->stack->getReactor().post([holder] { holder->flushForwardLocked(); }); @@ -224,8 +226,9 @@ void SessionTcp::updateReadInterest() { if (this->readPaused.load() && this->backwardBytes.load() <= LOW_WATER) { this->readPaused.store(false); // 背压埋点(fd->lwIP):积压回落到低水位,恢复读取(水位下降沿,低频事件)。 - spdlog::debug("[bp][bwd] resume read {}:{} -> {} backwardBytes={} (<=LOW_WATER={})", this->origDst.toString(), - this->origDstPort, this->origSrc.toString(), this->backwardBytes.load(), LOW_WATER); + candy::logger().debug(Poco::format("[bp][bwd] resume read %s:%hu -> %s backwardBytes=%zu (<=LOW_WATER=%zu)", + this->origDst.toString(), this->origDstPort, this->origSrc.toString(), + this->backwardBytes.load(), LOW_WATER)); int fd = this->fd; auto holder = shared_from_this(); this->stack->getReactor().post([holder, fd] { @@ -318,7 +321,7 @@ void SessionTcp::onConnected() { int ret = ::getsockopt(this->fd, SOL_SOCKET, SO_ERROR, &err, &len); #endif if (ret != 0 || err != 0) { - spdlog::warn("session tcp connect result error: {}", netErrStr(err ? err : netLastError())); + candy::logger().warning(Poco::format("session tcp connect result error: %s", netErrStr(err ? err : netLastError()))); closeFromReactor(); return; } @@ -364,8 +367,9 @@ void SessionTcp::flushForwardLocked() { // 背压埋点(lwIP->fd):记录本次写进落地 fd 的字节、剩余积压、是否因 fd 写满而背压。 // blocked=true 表示落地端写满(EAGAIN),forwardBuf 残留并等待 onFdWritable 续写; // 此时不向 lwIP 确认残留部分,源端接收窗口收紧 -> 自动减速,这正是背压生效的体现。 - spdlog::debug("[bp][fwd] flush {} -> {}:{} wrote={} remain={} blocked={} pendingRecved->{}", this->origSrc.toString(), - this->origDst.toString(), this->origDstPort, acked, remain, blocked, this->pendingRecved.load() + acked); + candy::logger().debug(Poco::format("[bp][fwd] flush %s -> %s:%hu wrote=%zu remain=%zu blocked=%d pendingRecved->%zu", + this->origSrc.toString(), this->origDst.toString(), this->origDstPort, acked, remain, + (int)blocked, this->pendingRecved.load() + acked)); // 已落地的字节交由 NetStack 线程向 lwIP 确认(推进接收窗口)。 // 这才是真正的"收到",从而把源端发送速率约束在落地 fd 的消费能力之内。 if (acked > 0) { @@ -389,8 +393,9 @@ void SessionTcp::onFdReadable() { if (this->backwardBytes.load() >= HIGH_WATER) { this->readPaused.store(true); // 背压埋点(fd->lwIP):积压达到高水位,停止读取(水位上升沿,低频事件)。 - spdlog::debug("[bp][bwd] pause read {}:{} -> {} backwardBytes={} (>=HIGH_WATER={})", this->origDst.toString(), - this->origDstPort, this->origSrc.toString(), this->backwardBytes.load(), HIGH_WATER); + candy::logger().debug(Poco::format("[bp][bwd] pause read %s:%hu -> %s backwardBytes=%zu (>=HIGH_WATER=%zu)", + this->origDst.toString(), this->origDstPort, this->origSrc.toString(), + this->backwardBytes.load(), HIGH_WATER)); this->stack->getReactor().mod(this->fd, ReactorEvent::NONE); return; } diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index f031728f..f36983c9 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -2,8 +2,9 @@ #include "netstack/session_udp.h" #include "netstack/netstack.h" #include "netstack/sockcompat.h" +#include "utils/log.h" +#include #include -#include #include "lwip/pbuf.h" #include "lwip/udp.h" @@ -44,7 +45,7 @@ int SessionUdp::start() { return -1; } - spdlog::debug("session udp src: {}:{}", this->origSrc.toString(), this->origSrcPort); + candy::logger().debug(Poco::format("session udp src: %s:%hu", this->origSrc.toString(), this->origSrcPort)); auto holder = shared_from_this(); int fd = this->fd; @@ -96,7 +97,7 @@ void SessionUdp::sendToLanding(uint32_t dstIpBe, uint16_t dstPortHost, std::stri } long n = netSendTo(holder->fd, data.data(), data.size(), dstIpBe, dstPortHost); if (n < 0 && !netWouldBlock(netLastError())) { - spdlog::debug("session udp sendto failed: {}", netErrStr(netLastError())); + candy::logger().debug(Poco::format("session udp sendto failed: %s", netErrStr(netLastError()))); } #endif }); @@ -113,7 +114,7 @@ void SessionUdp::replyToLwip(uint32_t peerIpBe, uint16_t peerPortHost, std::stri struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)data.size(), PBUF_RAM); if (p == nullptr) { - spdlog::warn("session udp reply pbuf_alloc failed"); + candy::logger().warning("session udp reply pbuf_alloc failed"); return; } pbuf_take(p, data.data(), (u16_t)data.size()); @@ -129,7 +130,7 @@ void SessionUdp::replyToLwip(uint32_t peerIpBe, uint16_t peerPortHost, std::stri ip_addr_set_ip4_u32(&dst, uint32_t(this->origSrc)); err_t e = udp_sendto_if_src(this->pcb, p, &dst, this->origSrcPort, &this->stack->getNetif(), &src); if (e != ERR_OK) { - spdlog::debug("session udp udp_sendto_if_src failed: {}", (int)e); + candy::logger().debug(Poco::format("session udp udp_sendto_if_src failed: %d", (int)e)); } pbuf_free(p); } diff --git a/candy/src/netstack/sockcompat.h b/candy/src/netstack/sockcompat.h index 44eb9779..b6948a8c 100644 --- a/candy/src/netstack/sockcompat.h +++ b/candy/src/netstack/sockcompat.h @@ -16,8 +16,8 @@ #include #include #else -#include // htons/ntohs(无连接收发的端口字节序转换) -#include // errno 是 C 标准库唯一入口,无 C++ 等价,属不得已保留。 +#include // htons/ntohs(无连接收发的端口字节序转换) +#include // errno 是 C 标准库唯一入口,无 C++ 等价,属不得已保留。 #include #include // struct sockaddr_in / INADDR_ANY(无连接收发地址组装) #include From a988c20217ec83b11466694cebd51b92c029fea2 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Fri, 17 Jul 2026 14:09:28 +0800 Subject: [PATCH 26/29] use only one socket for udp, switch config --- candy-cli/src/config.cc | 9 + candy-cli/src/config.h | 2 + candy/src/candy/client.cc | 1 + candy/src/core/client.cc | 8 + candy/src/core/client.h | 4 + candy/src/netstack/netstack.cc | 54 +++++- candy/src/netstack/netstack.h | 13 ++ candy/src/netstack/session_udp.cc | 30 ++- candy/src/netstack/session_udp.h | 15 +- candy/src/netstack/sockcompat.h | 10 + candy/src/netstack/stun.h | 86 +++++++++ candy/src/netstack/udpmux.cc | 296 ++++++++++++++++++++++++++++++ candy/src/netstack/udpmux.h | 110 +++++++++++ 13 files changed, 629 insertions(+), 9 deletions(-) create mode 100644 candy/src/netstack/stun.h create mode 100644 candy/src/netstack/udpmux.cc create mode 100644 candy/src/netstack/udpmux.h diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index 0a0c1598..9e128c5a 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -38,6 +38,7 @@ Poco::JSON::Object arguments::json() { config.set("vmac", virtualMac(this->name)); config.set("expt", loadTunAddress(this->name)); config.set("userspace-stack", this->userspaceStack); + config.set("udp-port-convergence", this->udpPortConvergence); } if (this->mode == "server") { @@ -131,6 +132,12 @@ int arguments::parse(int argc, char *argv[]) { "instead of the kernel stack. Default is false (kernel). (client only)") .implicit_value(true); + program.add_argument("--udp-port-convergence") + .help("converge all inner UDP sources onto a single landing socket/port (single-port mode)\n" + "instead of one socket per source. Requires --userspace-stack.\n" + "Default is false (per-source full-cone). (client only)") + .implicit_value(true); + program.add_group("Server options"); program.add_argument("-d", "--dhcp") @@ -189,6 +196,7 @@ int arguments::parse(int argc, char *argv[]) { program.set_if_used("--discovery", this->discovery); program.set_if_used("--route", this->routeCost); program.set_if_used("--userspace-stack", this->userspaceStack); + program.set_if_used("--udp-port-convergence", this->udpPortConvergence); bool needShowUsage = [&]() { if (this->mode != "client" && this->mode != "server") @@ -247,6 +255,7 @@ void arguments::parseFile(std::string cfgFile) { {"mtu", [&](const std::string &value) { this->mtu = std::stoi(value); }}, {"localhost", [&](const std::string &value) { this->localhost = value; }}, {"userspace-stack", [&](const std::string &value) { this->userspaceStack = (value == "true"); }}, + {"udp-port-convergence", [&](const std::string &value) { this->udpPortConvergence = (value == "true"); }}, }; auto trim = [](std::string str) { if (str.length() >= 2 && str.front() == '\"' && str.back() == '\"') { diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index e5f27cad..27f4bf43 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -32,6 +32,8 @@ struct arguments { int routeCost = 0; int mtu = 1400; bool userspaceStack = false; + // UDP 单端口收敛:多个内部源共用一个出口 fd/端口(默认关闭=保留每源全锥形)。 + bool udpPortConvergence = false; }; int saveTunAddress(const std::string &name, const std::string &cidr); diff --git a/candy/src/candy/client.cc b/candy/src/candy/client.cc index b0e66cfb..b1813e75 100644 --- a/candy/src/candy/client.cc +++ b/candy/src/candy/client.cc @@ -100,6 +100,7 @@ bool run(const std::string &id, const Poco::JSON::Object &config) { client->setPort(config.getValue("port")); client->setLocalhost(config.getValue("localhost")); client->setUserspaceStack(config.optValue("userspace-stack", false)); + client->setUdpPortConvergence(config.optValue("udp-port-convergence", false)); client->run(); } candy::logger().information(Poco::format("run exit: id=%s ", id)); diff --git a/candy/src/core/client.cc b/candy/src/core/client.cc index 0e8f49eb..71b62a45 100644 --- a/candy/src/core/client.cc +++ b/candy/src/core/client.cc @@ -70,6 +70,10 @@ bool Client::getUserspaceStack() const { return this->userspaceStack; } +bool Client::getUdpPortConvergence() const { + return this->udpPortConvergence; +} + int Client::getMtu() const { return this->tun.getMTU(); } @@ -123,6 +127,10 @@ void Client::setUserspaceStack(bool enable) { this->userspaceStack = enable; } +void Client::setUdpPortConvergence(bool enable) { + this->udpPortConvergence = enable; +} + void Client::run() { this->running.store(true); diff --git a/candy/src/core/client.h b/candy/src/core/client.h index ebd7d8d6..feb94e70 100644 --- a/candy/src/core/client.h +++ b/candy/src/core/client.h @@ -46,6 +46,7 @@ class Client { void setVirtualMac(const std::string &vmac); void setUserspaceStack(bool enable); + void setUdpPortConvergence(bool enable); void run(); bool isRunning(); @@ -65,6 +66,7 @@ class Client { MsgQueue &getNetStackMsgQueue(); bool getUserspaceStack() const; + bool getUdpPortConvergence() const; int getMtu() const; private: @@ -80,6 +82,8 @@ class Client { private: std::string tunName; bool userspaceStack = false; + // UDP 单端口收敛开关(默认关闭=每源全锥形)。仅在 userspaceStack=true 时生效。 + bool udpPortConvergence = false; }; } // namespace candy diff --git a/candy/src/netstack/netstack.cc b/candy/src/netstack/netstack.cc index ab01e165..df1ed585 100644 --- a/candy/src/netstack/netstack.cc +++ b/candy/src/netstack/netstack.cc @@ -4,6 +4,7 @@ #include "core/message.h" #include "netstack/session_tcp.h" #include "netstack/session_udp.h" +#include "netstack/udpmux.h" #include "utils/log.h" #include #include @@ -228,6 +229,11 @@ void NetStack::teardownStack() { } } this->udpSessions.clear(); + // 单端口收敛:整栈拆除时统一 shutdown UdpMux(清四表 + 交 reactor 关全局 fd)。 + if (this->udpMux) { + this->udpMux->shutdown(); + this->udpMux.reset(); + } { std::unique_lock lock(this->peerMapMutex); this->vnetPeerMap.clear(); @@ -267,6 +273,10 @@ void NetStack::loop() { auto now = std::chrono::steady_clock::now(); if (now - lastReap >= std::chrono::seconds(10)) { reapIdleUdpSessions(); + // 收敛模式:同频老化 UdpMux 的 endpointOwner / stunTxn。 + if (this->udpMux) { + this->udpMux->reap(); + } lastReap = now; } } @@ -479,7 +489,23 @@ void NetStack::onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *a candy::logger().debug(Poco::format("netstack onUdpRecv src %s:%hu (first dst %s:%hu)", origSrc.toString(), origSrcPort, origDst.toString(), origDstPort)); - auto session = std::make_shared(this, pcb, origSrc, origSrcPort); + // 模式选择:读客户端开关。收敛模式下所有源共用全局 UdpMux(惰性首用即建)。 + bool converged = this->client && this->client->getUdpPortConvergence(); + if (converged && !this->udpMux) { + this->udpMux = std::make_shared(this); + if (this->udpMux->start()) { + // 配置明确选择收敛模式时,不得静默回退到每源模式:否则出口语义与用户选择不一致。 + candy::logger().error("netstack udpmux start failed while UDP convergence is enabled"); + this->udpMux.reset(); + udp_recv(pcb, nullptr, nullptr); + udp_remove(pcb); + pbuf_free(p); + this->client->shutdown(); + return; + } + } + + auto session = std::make_shared(this, pcb, origSrc, origSrcPort, converged); if (session->start()) { udp_recv(pcb, nullptr, nullptr); udp_remove(pcb); @@ -490,4 +516,30 @@ void NetStack::onUdpRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *a // 不释放 p:交还 lwIP(goto again 重投到该 npcb 的每流 handler)。 } +// ===================== UDP 单端口收敛协作接口(仅 NetStack 线程) ===================== + +void NetStack::sendUdpConverged(const std::string &innerSrc, uint32_t dstIpBe, uint16_t dstPortHost, std::string data) { + if (!this->udpMux) { + return; + } + this->udpMux->sendFromSource(innerSrc, dstIpBe, dstPortHost, std::move(data)); +} + +void NetStack::onUdpSourceGone(const std::string &innerSrc) { + if (!this->udpMux) { + return; + } + this->udpMux->onSourceGone(innerSrc); +} + +void NetStack::injectUdpReply(const std::string &innerSrc, uint32_t rIpBe, uint16_t rPortHost, std::string data) { + // innerNpcb 复用 udpSessions:按内部源定位对应 SessionUdp,以实际对端为源注入回 lwIP。 + // 源可能已老化消亡(会话被回收),此时静默丢弃即可。 + auto it = this->udpSessions.find(innerSrc); + if (it == this->udpSessions.end() || !it->second) { + return; + } + it->second->replyToLwip(rIpBe, rPortHost, std::move(data)); +} + } // namespace candy diff --git a/candy/src/netstack/netstack.h b/candy/src/netstack/netstack.h index 42bcffa8..d8969843 100644 --- a/candy/src/netstack/netstack.h +++ b/candy/src/netstack/netstack.h @@ -31,6 +31,7 @@ namespace candy { class Client; class SessionTcp; class SessionUdp; +class UdpMux; // NetStack:独占 lwIP(NO_SYS=1 单线程 raw API)的模块。 // 持有一个 NetStack 线程驱动 lwIP 收发与定时器,一个 Reactor 线程管理落地 fd。 @@ -62,6 +63,14 @@ class NetStack { // UDP 会话结束时从 UDP 会话表移除(按源二元组 key)。仅 NetStack 线程调用。 void removeUdpSession(const std::string &key); + // ---- UDP 单端口收敛(converged 模式)协作接口,均仅 NetStack 线程调用 ---- + // 某内部源 innerSrc 发往 (dstIpBe:dstPortHost) 的一份报文,转交全局 UdpMux 发送。 + void sendUdpConverged(const std::string &innerSrc, uint32_t dstIpBe, uint16_t dstPortHost, std::string data); + // 某内部源整体消亡,通知 UdpMux 级联清掉该源名下全部 endpointOwner 条目。 + void onUdpSourceGone(const std::string &innerSrc); + // UdpMux demux 出内部源后,把回包定位到对应 SessionUdp 注入回 lwIP。 + void injectUdpReply(const std::string &innerSrc, uint32_t rIpBe, uint16_t rPortHost, std::string data); + Client *getClient(); private: @@ -123,6 +132,10 @@ class NetStack { std::unordered_map> sessions; // UDP 会话表:源二元组 key -> SessionUdp。仅 NetStack 线程访问,无需加锁。 std::unordered_map> udpSessions; + + // UDP 单端口收敛多路复用器:仅在 udpPortConvergence 开启时创建(惰性首用即建), + // 收敛模式下所有内部源共用它的单一落地 fd。仅 NetStack 线程访问。 + std::shared_ptr udpMux; }; } // namespace candy diff --git a/candy/src/netstack/session_udp.cc b/candy/src/netstack/session_udp.cc index f36983c9..d7a068dc 100644 --- a/candy/src/netstack/session_udp.cc +++ b/candy/src/netstack/session_udp.cc @@ -18,10 +18,10 @@ namespace candy { -SessionUdp::SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort) - : Session(stack), pcb(pcb), fd(-1), origSrc(origSrc), origSrcPort(origSrcPort), +SessionUdp::SessionUdp(NetStack *stack, struct udp_pcb *pcb, IP4 origSrc, uint16_t origSrcPort, bool converged) + : Session(stack), pcb(pcb), fd(-1), converged(converged), origSrc(origSrc), origSrcPort(origSrcPort), lastActiveTs(std::chrono::steady_clock::now()) { - // 源二元组 key:源IP:源端口(全锥形下一源一会话,目的 per-packet 不入 key) + // 源二元组 key:源IP:源端口(一源一会话,目的 per-packet 不入 key) this->sessionKey.assign((const char *)&origSrc, sizeof(uint32_t)); this->sessionKey.append((const char *)&origSrcPort, sizeof(origSrcPort)); } @@ -37,8 +37,16 @@ int SessionUdp::start() { #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) this->self = shared_from_this(); - // 落地拨号:内核 socket 直连(DirectOutbound)。全锥形——unconnected + 固定出口端口, - // 后续用 sendto 发往任意目的、recvfrom 收任意对端回包。出口源 IP 由内核填为网关出口 IP。 + if (this->converged) { + // 单端口收敛模式:不自建 fd。发送经全局 UdpMux 的单 fd、回程由 UdpMux demux 后回调 + // replyToLwip。本会话只保留 npcb(作 innerNpcb 定位表项),注册每流 recv 即可。 + candy::logger().debug( + Poco::format("session udp (converged) src: %s:%hu", this->origSrc.toString(), this->origSrcPort)); + udp_recv(this->pcb, recvTrampoline, this); + return 0; + } + + // 每源全锥形模式:本会话自建 unconnected + 固定出口端口的落地 fd,自 recvfrom 收回包。 Outbound &outbound = this->stack->getOutbound(); this->fd = outbound.dialUdp(); if (this->fd < 0) { @@ -80,6 +88,13 @@ void SessionUdp::onPcbRecv(struct pbuf *p, const ip_addr_t *addr, u16_t port) { data.resize(p->tot_len); pbuf_copy_partial(p, data.data(), p->tot_len, 0); pbuf_free(p); + this->lastActiveTs = std::chrono::steady_clock::now(); + if (this->converged) { + // 收敛模式:发送经全局 UdpMux(内部做 STUN 识别/txid 登记/数据面 FCFS 锁,通过后 post + // 到 Reactor 用全局 fd 发送)。表操作全在 NetStack 线程完成,符合 §11.3a 线程归属。 + this->stack->sendUdpConverged(this->sessionKey, dstIpBe, dstPortHost, std::move(data)); + return; + } sendToLanding(dstIpBe, dstPortHost, std::move(data)); } @@ -143,6 +158,10 @@ void SessionUdp::closeFromStack() { } NetStack *stack = this->stack; std::string k = this->sessionKey; + // 收敛模式:本源整体消亡,通知 UdpMux 级联清掉该源名下全部 endpointOwner 条目(§11.3 兜底)。 + if (this->converged) { + stack->onUdpSourceGone(k); + } auto holder = shared_from_this(); stack->getReactor().post([holder] { holder->closeFromReactor(); }); stack->removeUdpSession(k); @@ -155,6 +174,7 @@ void SessionUdp::shutdownFromStack() { udp_remove(this->pcb); this->pcb = nullptr; } + // 收敛模式:整栈拆除时 UdpMux 会被 NetStack 统一 shutdown 清表,故此处无需逐源级联。 if (!this->closing.exchange(true)) { auto holder = shared_from_this(); this->stack->getReactor().post([holder] { diff --git a/candy/src/netstack/session_udp.h b/candy/src/netstack/session_udp.h index 331791c6..4c00458c 100644 --- a/candy/src/netstack/session_udp.h +++ b/candy/src/netstack/session_udp.h @@ -41,7 +41,10 @@ class SessionUdp : public Session, public std::enable_shared_from_thissessionKey; @@ -75,14 +83,15 @@ class SessionUdp : public Session, public std::enable_shared_from_thislocal_* 读取,投递到落地 fd 发送。 void onPcbRecv(struct pbuf *p, const ip_addr_t *addr, u16_t port); - // 落地 fd 收到某对端(peerIp:peerPort)回包后,用 pcb 以该对端为源把数据报注入回源端。 - void replyToLwip(uint32_t peerIpBe, uint16_t peerPortHost, std::string data); void closeFromStack(); private: struct udp_pcb *pcb; int fd; + // 单端口收敛模式:true=不自建 fd,发送经全局 UdpMux、回程由 UdpMux 回调 replyToLwip。 + bool converged; + IP4 origSrc; uint16_t origSrcPort; diff --git a/candy/src/netstack/sockcompat.h b/candy/src/netstack/sockcompat.h index b6948a8c..2a58a3a1 100644 --- a/candy/src/netstack/sockcompat.h +++ b/candy/src/netstack/sockcompat.h @@ -114,6 +114,16 @@ inline int netBindAny(int fd) { #endif } +// 调大接收缓冲区(SO_RCVBUF)。单端口收敛时单个 fd 要收全部对端回包, +// 突发海量流可能溢出默认缓冲导致丢包(原多 socket 天然分摊,无此问题)。成功返回 0。 +inline int netSetRecvBuf(int fd, int bytes) { +#if defined(_WIN32) || defined(_WIN64) + return ::setsockopt((SOCKET)fd, SOL_SOCKET, SO_RCVBUF, (const char *)&bytes, sizeof(bytes)) == 0 ? 0 : -1; +#else + return ::setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bytes, sizeof(bytes)) == 0 ? 0 : -1; +#endif +} + // 取本线程最近一次 socket 错误码。 inline int netLastError() { #if defined(_WIN32) || defined(_WIN64) diff --git a/candy/src/netstack/stun.h b/candy/src/netstack/stun.h new file mode 100644 index 00000000..1a1907b1 --- /dev/null +++ b/candy/src/netstack/stun.h @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_STUN_H +#define CANDY_NETSTACK_STUN_H + +// 标准 STUN(RFC5389) 识别与分流工具(仅供 UDP 单端口收敛的 demux 使用)。 +// +// 背景(见方案 §11.2/§11.4):单端口收敛后,多个内部源共用一个出口 fd,回程 demux 依赖 +// 「该包是否带有 E 可解析且对每源唯一的标识」。STUN 的 96-bit transaction id 正是这种标识: +// - 出站 Binding Request 打洞并建 srflx 候选,本机按 txid 登记归属; +// - 回程 Binding Response 靠 txid 精确还原到源(即便多源共享同一公共 STUN 服务器); +// - 故 STUN 一律豁免数据面 FCFS(否则第二源发往共享 STUN 会被误丢,导致 gather 失败)。 +// +// 支持范围:**仅标准 STUN(RFC5389,带 magic cookie 0x2112A442)**;RFC3489 老式 STUN 无 +// magic cookie,不在支持范围(不识别,按普通数据面处理)。 + +#include +#include +#include + +namespace candy { + +// STUN 消息 class(RFC5389 §6:method 与 class 交织编码在 14-bit message type 内, +// class 由 bit8(C1)、bit4(C0) 两位构成,与具体 method 无关)。按 class 分流即可, +// 无需枚举各 method(Binding/Allocate/… 处理方式一致)。 +enum class StunClass : uint8_t { + Request = 0, // 0b00:请求(出站含 txid 需登记;入站为对端 ICE 连通性检查) + Indication = 1, // 0b01:指示(如 keepalive Binding Indication 0x0011,无需回程配对) + SuccessResp = 2, // 0b10:成功响应(回程靠 txid 还原) + ErrorResp = 3, // 0b11:错误响应(回程靠 txid 还原) +}; + +struct StunInfo { + StunClass klass; // 消息 class + uint8_t txid[12]; // 96-bit transaction id +}; + +// STUN magic cookie(RFC5389,位于 byte4-7,网络序)。 +inline constexpr uint32_t kStunMagicCookie = 0x2112A442u; + +// 识别一段载荷是否为标准 STUN 消息。命中则回填 out 并返回 true。 +// +// 三重校验(见 §11.4「STUN 识别」,避免把恰好前 4 字节等于 magic cookie 的数据包误判): +// ① byte4-7 == 0x2112A442(magic cookie) +// ② 消息类型最高 2 bit == 0(byte0 高 2 位为 0,STUN 头部固定前导) +// ③ length 字段(byte2-3) == 总长-20 且 4 字节对齐 +inline bool stunParse(const void *data, size_t len, StunInfo *out) { + // STUN 头部固定 20 字节:2(type)+2(length)+4(cookie)+12(txid)。 + if (data == nullptr || len < 20) { + return false; + } + const uint8_t *b = static_cast(data); + + // ② 消息类型最高 2 bit 必须为 0。 + if ((b[0] & 0xC0) != 0) { + return false; + } + + // ① magic cookie(byte4-7,网络序)。 + uint32_t cookie = (uint32_t(b[4]) << 24) | (uint32_t(b[5]) << 16) | (uint32_t(b[6]) << 8) | uint32_t(b[7]); + if (cookie != kStunMagicCookie) { + return false; + } + + // ③ length 字段(byte2-3,网络序) 必须等于「总长-20」且 4 字节对齐。 + uint16_t msgLen = (uint16_t(b[2]) << 8) | uint16_t(b[3]); + if ((msgLen & 0x3) != 0) { + return false; + } + if (size_t(msgLen) + 20 != len) { + return false; + } + + if (out != nullptr) { + // class 由 message type 的 bit8(C1)、bit4(C0) 两位构成。 + uint16_t type = (uint16_t(b[0]) << 8) | uint16_t(b[1]); + uint8_t c1 = (type >> 8) & 0x1; + uint8_t c0 = (type >> 4) & 0x1; + out->klass = static_cast((c1 << 1) | c0); + std::memcpy(out->txid, b + 8, 12); + } + return true; +} + +} // namespace candy + +#endif diff --git a/candy/src/netstack/udpmux.cc b/candy/src/netstack/udpmux.cc new file mode 100644 index 00000000..aa198ad8 --- /dev/null +++ b/candy/src/netstack/udpmux.cc @@ -0,0 +1,296 @@ +// SPDX-License-Identifier: MIT +#include "netstack/udpmux.h" +#include "netstack/netstack.h" +#include "netstack/sockcompat.h" +#include "netstack/stun.h" +#include "utils/log.h" +#include +#include + +namespace candy { + +// endpointOwner 按端点 LRU 老化阈值:必须 ≤ 物理出口 NAT3 的 UDP 映射老化(运营商常 30–60s)。 +// 无 TURN 兜底,故不能比物理层活得久(否则 lwIP 以为映射还在、物理层已老化,回包被物理 NAT 丢)。 +static constexpr std::chrono::seconds kEndpointIdle{30}; +// STUN 事务表超时:覆盖 RFC5389 重传窗口(Rc*RTO≈39.5s),略给余量,防 Response 丢失致表膨胀。 +static constexpr std::chrono::seconds kStunTxnIdle{40}; +// 单出口 fd 接收缓冲:单 socket 收全部对端回包,突发海量流需大缓冲防溢出丢包。 +static constexpr int kRecvBufBytes = 4 * 1024 * 1024; +// 未命中可能由异常对端高频触发;保留 WARN 可观测性,同时限频保护日志系统。 +static constexpr std::chrono::seconds kDropWarnInterval{1}; + +UdpMux::UdpMux(NetStack *stack) + : stack(stack), fd(-1), closing(false), dropCollision(0), dropTxidMiss(0), dropEndpointMiss(0), lastLoggedDrops(0) {} + +UdpMux::~UdpMux() { + if (this->fd >= 0) { + netClose(this->fd); + this->fd = -1; + } +} + +int UdpMux::start() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + // 全局唯一落地 fd:unconnected + bindAny(固定出口端口,全生命周期不变),所有内部源复用。 + this->fd = this->stack->getOutbound().dialUdp(); + if (this->fd < 0) { + candy::logger().error("udpmux dialUdp failed"); + return -1; + } + // 收敛后单 fd 承载全部回包,显式调大接收缓冲防溢出(原多 socket 天然分摊,无此需求)。 + if (netSetRecvBuf(this->fd, kRecvBufBytes) != 0) { + candy::logger().warning(Poco::format("udpmux set SO_RCVBUF failed: %s", netErrStr(netLastError()))); + } + + candy::logger().information("udpmux started: single-port UDP convergence enabled"); + + // fd 已成功创建后才建立自持引用;失败路径不产生 shared_ptr 环,确保调用方 reset 后可析构。 + this->self = shared_from_this(); + auto holder = this->self; + int f = this->fd; + this->stack->getReactor().add(f, ReactorEvent::READ, [holder](ReactorEvent ev) { holder->onFdEvent((uint32_t)ev); }); + return 0; +#else + return -1; +#endif +} + +void UdpMux::shutdown() { + // NetStack 线程调用:清空四表(本线程独占,无锁),fd 交由 reactor 关闭。 + this->endpointOwner.clear(); + this->stunTxn.clear(); + this->ownerEndpoints.clear(); + + if (!this->closing.exchange(true)) { + auto holder = shared_from_this(); + this->stack->getReactor().post([holder] { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + if (holder->fd >= 0) { + holder->stack->getReactor().del(holder->fd); + netClose(holder->fd); + holder->fd = -1; + } +#endif + holder->self.reset(); + }); + } +} + +// ===================== NetStack 线程:发送侧 ===================== + +void UdpMux::sendFromSource(const std::string &innerSrc, uint32_t dstIpBe, uint16_t dstPortHost, std::string data) { + auto now = std::chrono::steady_clock::now(); + uint64_t k = endpointKey(dstIpBe, dstPortHost); + + // step 1:STUN 一律豁免数据面 FCFS(不看具体 method/class)。见 §11.4 发送 step 1。 + StunInfo info; + if (stunParse(data.data(), data.size(), &info)) { + if (info.klass == StunClass::Request) { + // 带事务的 Request:登记 txid → 内部源,供回程 Response 靠 txid 分流。 + // keepalive(Indication) 等无需回程配对的消息:豁免但不记 txid。 + std::string txid((const char *)info.txid, sizeof(info.txid)); + this->stunTxn[txid] = TxnEntry{innerSrc, now}; + } + // 对 endpointOwner:仅「存在且同源则保活」,不含则不创建(避免把共享 STUN 端点锁给某源), + // 异源则跳过、不丢(否则多源共享同一公共 STUN 会被误判碰撞丢弃)。 + auto it = this->endpointOwner.find(k); + if (it != this->endpointOwner.end() && it->second.owner == innerSrc) { + it->second.lastActive = now; + } + // step 3:直接发送(必须跳过 step 2 的 FCFS)。 + } else { + // step 2:数据包端点归属(数据面 FCFS 锁定)。 + auto it = this->endpointOwner.find(k); + if (it == this->endpointOwner.end()) { + // 首占,锁定。 + this->endpointOwner[k] = OwnerEntry{innerSrc, now}; + this->ownerEndpoints[innerSrc].insert(k); + } else if (it->second.owner != innerSrc) { + // 碰撞:D 已被别的源占用(禁区 A)——只丢后者,绝不影响首占者。 + ++this->dropCollision; + candy::logger().warning(Poco::format( + "udpmux drop(collision): dst=%08x:%hu owned by other source, dropping later source's packet", dstIpBe, + dstPortHost)); + return; + } else { + // 同源续发:保活(对端 NAT3,keepalive 维持物理映射,命脉)。 + it->second.lastActive = now; + } + } + + // step 3:post 到 Reactor 线程用全局 fd 发送(表操作已在 NetStack 线程完成)。 + auto holder = shared_from_this(); + this->stack->getReactor().post([holder, dstIpBe, dstPortHost, data = std::move(data)]() mutable { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + if (holder->closing.load() || holder->fd < 0) { + return; + } + long n = netSendTo(holder->fd, data.data(), data.size(), dstIpBe, dstPortHost); + if (n < 0 && !netWouldBlock(netLastError())) { + candy::logger().debug(Poco::format("udpmux sendto failed: %s", netErrStr(netLastError()))); + } +#endif + }); +} + +void UdpMux::onSourceGone(const std::string &innerSrc) { + // 源整体消亡(npcb 回收):经反向索引级联清掉该源名下全部 endpointOwner 条目(§11.3 兜底)。 + auto it = this->ownerEndpoints.find(innerSrc); + if (it == this->ownerEndpoints.end()) { + return; + } + for (uint64_t k : it->second) { + auto eit = this->endpointOwner.find(k); + // 仅删仍属本源的条目(老化后可能已被新首占者重锁,不能误删)。 + if (eit != this->endpointOwner.end() && eit->second.owner == innerSrc) { + this->endpointOwner.erase(eit); + } + } + this->ownerEndpoints.erase(it); +} + +void UdpMux::warnDrop(const char *kind, uint32_t peerIpBe, uint16_t peerPortHost) { + auto now = std::chrono::steady_clock::now(); + if (now - this->lastDropWarn < kDropWarnInterval) { + return; + } + this->lastDropWarn = now; + candy::logger().warning(Poco::format( + "udpmux drop(%s): peer=%08x:%hu totals(collision=%s txidMiss=%s endpointMiss=%s)", kind, peerIpBe, + peerPortHost, std::to_string(this->dropCollision), std::to_string(this->dropTxidMiss), + std::to_string(this->dropEndpointMiss))); +} + +void UdpMux::reap() { + auto now = std::chrono::steady_clock::now(); + + // endpointOwner:按端点各自 lastActive 做 LRU 老化(对齐真实 NAT 逐映射独立超时)。 + for (auto it = this->endpointOwner.begin(); it != this->endpointOwner.end();) { + if (now - it->second.lastActive > kEndpointIdle) { + // 同步从反向索引摘除该端点。 + auto oit = this->ownerEndpoints.find(it->second.owner); + if (oit != this->ownerEndpoints.end()) { + oit->second.erase(it->first); + if (oit->second.empty()) { + this->ownerEndpoints.erase(oit); + } + } + it = this->endpointOwner.erase(it); + } else { + ++it; + } + } + + // stunTxn:超时清理(防 Response 丢失导致表膨胀)。 + for (auto it = this->stunTxn.begin(); it != this->stunTxn.end();) { + if (now - it->second.createdAt > kStunTxnIdle) { + it = this->stunTxn.erase(it); + } else { + ++it; + } + } + + // 丢弃计数仅在增长时打印(可观测:判断是否频繁踩禁区)。 + uint64_t total = this->dropCollision + this->dropTxidMiss + this->dropEndpointMiss; + if (total != this->lastLoggedDrops) { + candy::logger().information(Poco::format( + "udpmux stats: endpoints=%s txns=%s drops(collision=%s txidMiss=%s endpointMiss=%s)", + std::to_string(this->endpointOwner.size()), std::to_string(this->stunTxn.size()), + std::to_string(this->dropCollision), std::to_string(this->dropTxidMiss), + std::to_string(this->dropEndpointMiss))); + this->lastLoggedDrops = total; + } +} + +// ===================== Reactor 线程:单读者 ===================== + +void UdpMux::onFdEvent(uint32_t events) { + ReactorEvent ev = (ReactorEvent)events; + if (ev & ReactorEvent::FAILURE) { + // 全局 fd 是收敛命脉,不能因偶发错误关闭;记录后继续(unconnected 下罕见)。 + candy::logger().warning("udpmux fd failure event"); + return; + } + if (ev & ReactorEvent::READ) { + onFdReadable(); + } +} + +void UdpMux::onFdReadable() { +#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) + char buf[65536]; + while (true) { + // 单 socket 收任意对端回包,取回真实对端地址/端口,连同数据投递到 NetStack 线程做 demux。 + uint32_t peerIpBe; + uint16_t peerPortHost; + long n = netRecvFrom(this->fd, buf, sizeof(buf), &peerIpBe, &peerPortHost); + if (n > 0) { + std::string data(buf, n); + auto holder = shared_from_this(); + this->stack->postToStack([holder, peerIpBe, peerPortHost, data = std::move(data)]() mutable { + holder->demux(peerIpBe, peerPortHost, std::move(data)); + }); + continue; + } + if (n == 0) { + return; + } + if (netWouldBlock(netLastError())) { + return; + } + // 罕见错误:记录但保留 fd(不像 per-session 那样关闭)。 + candy::logger().debug(Poco::format("udpmux recvfrom failed: %s", netErrStr(netLastError()))); + return; + } +#endif +} + +// ===================== NetStack 线程:接收侧 demux ===================== + +void UdpMux::demux(uint32_t rIpBe, uint16_t rPortHost, std::string data) { + auto now = std::chrono::steady_clock::now(); + uint64_t k = endpointKey(rIpBe, rPortHost); + + // step 1:STUN 按类型分流。 + StunInfo info; + if (stunParse(data.data(), data.size(), &info)) { + if (info.klass == StunClass::SuccessResp || info.klass == StunClass::ErrorResp) { + // 1a:Response/Error 走 txid 反查。 + std::string txid((const char *)info.txid, sizeof(info.txid)); + auto it = this->stunTxn.find(txid); + if (it != this->stunTxn.end()) { + std::string innerSrc = it->second.innerSrc; + this->stunTxn.erase(it); + this->stack->injectUdpReply(innerSrc, rIpBe, rPortHost, std::move(data)); + return; + } + ++this->dropTxidMiss; // txid 对不上:非本机发起或已超时。 + this->warnDrop("stun-txid-miss", rIpBe, rPortHost); + return; + } + // 1b:入站 Request/Indication(对端 ICE 连通性检查)——txid 由对端生成,不在 stunTxn, + // 必须走 endpointOwner(前提:我方已先向 R 发过包并 FCFS 锁定)。 + auto it = this->endpointOwner.find(k); + if (it != this->endpointOwner.end()) { + it->second.lastActive = now; + this->stack->injectUdpReply(it->second.owner, rIpBe, rPortHost, std::move(data)); + return; + } + ++this->dropEndpointMiss; // 我方未先发起,无归属。 + this->warnDrop("stun-endpoint-miss", rIpBe, rPortHost); + return; + } + + // step 2:数据包走 endpointOwner。 + auto it = this->endpointOwner.find(k); + if (it != this->endpointOwner.end()) { + it->second.lastActive = now; // 接收侧保活:对端单向来包也算活跃,防误回收。 + this->stack->injectUdpReply(it->second.owner, rIpBe, rPortHost, std::move(data)); + return; + } + // 未命中:对端冷启动主动连 / 已老化 / 禁区碰撞受害包(对端为 NAT3 时 R==D 不会误丢)。 + ++this->dropEndpointMiss; + this->warnDrop("data-endpoint-miss", rIpBe, rPortHost); +} + +} // namespace candy diff --git a/candy/src/netstack/udpmux.h b/candy/src/netstack/udpmux.h new file mode 100644 index 00000000..706ed5a2 --- /dev/null +++ b/candy/src/netstack/udpmux.h @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_NETSTACK_UDPMUX_H +#define CANDY_NETSTACK_UDPMUX_H + +#include +#include +#include +#include +#include +#include +#include + +namespace candy { + +class NetStack; + +// UdpMux:UDP 单端口收敛的全局多路复用器(见方案 §11)。 +// +// 与「每源全锥形」的区别:每源模式下每个内部源(origSrc,origSrcPort)各持一个 unconnected 落地 +// fd、各自 recvfrom/sendto;UdpMux 模式下**所有内部源共用一个** unconnected fd、一个出口端口, +// 回程靠下面四张表做 demux 还原到各自内部源。收敛的是「落地 fd」,不收敛「npcb」——lwIP 仍 +// 一源一 npcb(保留在 NetStack::udpSessions 作 innerNpcb 定位表),回程注入仍逐源进行。 +// +// 性质(前提:双端 NAT3 + 无 TURN):单端口后过滤维度为「地址且端口依赖过滤(APDF)=端口受限锥」, +// 恰与物理出口 NAT3 对齐,对端为 NAT3(cone) 时回包端点 R==D 必命中、无损。 +// +// 线程契约(§11.3a,必须遵守):四张表**全部只在 NetStack 线程读写、无锁**。 +// - 发送侧判定在 NetStack 线程(SessionUdp::onPcbRecv → sendFromSource),通过后才 post 到 +// Reactor 线程做 netSendTo; +// - 接收侧 Reactor 线程只 recvfrom 收字节+对端地址,随即 postToStack 到 NetStack 线程做 demux。 +// Reactor 线程绝不触碰四表。 +class UdpMux : public std::enable_shared_from_this { +public: + explicit UdpMux(NetStack *stack); + ~UdpMux(); + + // NetStack 线程:建立全局落地 fd(unconnected + bindAny + 调大 SO_RCVBUF),注册单读者到 + // Reactor。成功返回 0,失败返回 -1。 + int start(); + + // NetStack 线程:栈拆除(重连)时关闭全局 fd、清空四表。 + void shutdown(); + + // NetStack 线程(SessionUdp::onPcbRecv 内):某内部源 innerSrc 发往远端 D 的一份报文。 + // 内部做 STUN 识别/txid 登记/数据面 FCFS 锁,判定通过后 post 到 Reactor 用全局 fd 发送。 + void sendFromSource(const std::string &innerSrc, uint32_t dstIpBe, uint16_t dstPortHost, std::string data); + + // NetStack 线程:某内部源整体消亡(npcb 回收)时,经反向索引级联清掉该源名下全部 + // endpointOwner 条目,防端点泄漏 + 新源被 FCFS 误挡(§11.3 兜底级联)。 + void onSourceGone(const std::string &innerSrc); + + // NetStack 线程:按端点各自 lastActive 做 LRU 老化 endpointOwner;清理超时 stunTxn。 + // loop 节流调用。 + void reap(); + +private: + // ---- Reactor 线程 ---- + void onFdEvent(uint32_t events); + void onFdReadable(); + + // ---- NetStack 线程 ---- + // 收到远端 R=(rIpBe,rPortHost) 的一份回包,按 STUN txid / endpointOwner 反查还原内部源并注入。 + void demux(uint32_t rIpBe, uint16_t rPortHost, std::string data); + // 丢弃告警限频:保留端点和分类信息,同时避免异常流量刷爆日志。 + void warnDrop(const char *kind, uint32_t peerIpBe, uint16_t peerPortHost); + + // 远端端点 key:(R_ip 网络序) 高 32 位 | (R_port 主机序) 低 16 位。 + static uint64_t endpointKey(uint32_t ipBe, uint16_t portHost) { + return (uint64_t(ipBe) << 16) | uint64_t(portHost); + } + +private: + NetStack *stack; + int fd; + std::atomic closing; + std::shared_ptr self; // 自持,保证跨线程投递期间存活(对齐 SessionUdp) + + // ---- 四张表(全部仅 NetStack 线程访问,无锁)---- + + // 反向 demux 主表:远端数据端点 -> {首占内部源 owner, 该端点自身活跃时间}。FCFS 首占锁定。 + struct OwnerEntry { + std::string owner; // InnerSrc(= SessionUdp::key,源二元组字节串) + std::chrono::steady_clock::time_point lastActive; + }; + std::unordered_map endpointOwner; + + // STUN 事务表:txid(12B) -> {内部源, 登记时间}。Request 登记 / Response 消费 / 超时清理。 + struct TxnEntry { + std::string innerSrc; + std::chrono::steady_clock::time_point createdAt; + }; + std::unordered_map stunTxn; + + // 反向索引:内部源 -> 它占用的全部远端端点集合(供源整体消亡时兜底级联清 endpointOwner)。 + std::unordered_map> ownerEndpoints; + + // 注:innerNpcb(内部源 -> npcb)复用 NetStack::udpSessions,回程注入经 + // NetStack::injectUdpReply 定位,故此处不再单独维护。 + + // ---- 丢弃计数(可观测性,§11.7 点5)---- + uint64_t dropCollision; // 数据面 FCFS 碰撞(≥2 源发往同一数据端点) + uint64_t dropTxidMiss; // STUN Response txid 未命中 + uint64_t dropEndpointMiss; // 数据 / 入站 Request 的 endpoint 未命中 + uint64_t lastLoggedDrops; // 上次日志时的丢弃总数(仅在增长时打印) + std::chrono::steady_clock::time_point lastDropWarn; +}; + +} // namespace candy + +#endif From 9f2ee2fd18665395411c3055009f9e9d4b0a3260 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Wed, 22 Jul 2026 12:21:14 +0800 Subject: [PATCH 27/29] update config --- candy-cli/src/config.h | 1 + candy.cfg | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index 27f4bf43..b9d197ff 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -31,6 +31,7 @@ struct arguments { int discovery = 0; int routeCost = 0; int mtu = 1400; + // userspace-stack:内嵌 lwIP 落地(默认关闭=继续走内核 tun)。 bool userspaceStack = false; // UDP 单端口收敛:多个内部源共用一个出口 fd/端口(默认关闭=保留每源全锥形)。 bool udpPortConvergence = false; diff --git a/candy.cfg b/candy.cfg index 7577ab62..0648d302 100644 --- a/candy.cfg +++ b/candy.cfg @@ -66,12 +66,18 @@ route = 5 # [Optional] Maximum Transmission Unit #mtu=1400 -# [Optional] Enable the embedded userspace network stack for landing gateway. +# [Optional] Enable the embedded userspace network stack (lwIP) for landing +# gateway. # false (default): write decapsulated packets to the kernel tun device, relying # on external iptables MASQUERADE / ip_forward for cross-LAN access. # true: terminate the inner packet with an embedded userspace network stack # (lwIP) and dial the target with a local kernel socket, replacing iptables # MASQUERADE. Requires a build with CANDY_NETSTACK enabled. Phase 1 supports # TCP/UDP only. -#userspace-stack = false +userspace-stack = false +# [Optional] UDP single-port convergence for netstack landing mode. +# false (default): per-source UDP sockets/full-cone behavior. +# true: all inner UDP sources share one landing UDP socket/port. Requires +# userspace-stack = true. +udp-port-convergence = false From d4bfcbe7ae7aabe30470921a427c0a195402c78b Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Thu, 23 Jul 2026 12:19:32 +0800 Subject: [PATCH 28/29] p2p only --- candy-cli/src/config.cc | 9 +++++++++ candy-cli/src/config.h | 2 ++ candy.cfg | 9 +++++++++ candy/src/candy/client.cc | 1 + candy/src/core/client.cc | 8 ++++++++ candy/src/core/client.h | 3 +++ candy/src/peer/manager.cc | 27 ++++++++++++++++++++++++++- candy/src/peer/manager.h | 5 +++++ candy/src/peer/peer.cc | 4 ++-- candy/src/peer/peer.h | 2 +- candy/src/websocket/client.cc | 4 ++++ 11 files changed, 70 insertions(+), 4 deletions(-) diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index 9e128c5a..40466c39 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -37,6 +37,7 @@ Poco::JSON::Object arguments::json() { config.set("port", this->port); config.set("vmac", virtualMac(this->name)); config.set("expt", loadTunAddress(this->name)); + config.set("p2p-only", this->p2pOnly); config.set("userspace-stack", this->userspaceStack); config.set("udp-port-convergence", this->udpPortConvergence); } @@ -127,6 +128,12 @@ int arguments::parse(int argc, char *argv[]) { "If omitted, auto-detected from the first physical network interface. (client only)") .metavar(""); + program.add_argument("--p2p-only") + .help("allow business packets only over direct P2P links.\n" + "WebSocket remains available for signaling, but relay forwarding is disabled.\n" + "Default is false (fall back to WebSocket relay when P2P is unavailable). (client only)") + .implicit_value(true); + program.add_argument("--userspace-stack") .help("terminate landing traffic with the built-in userspace network stack (lwIP)\n" "instead of the kernel stack. Default is false (kernel). (client only)") @@ -195,6 +202,7 @@ int arguments::parse(int argc, char *argv[]) { program.set_if_used("--mtu", this->mtu); program.set_if_used("--discovery", this->discovery); program.set_if_used("--route", this->routeCost); + program.set_if_used("--p2p-only", this->p2pOnly); program.set_if_used("--userspace-stack", this->userspaceStack); program.set_if_used("--udp-port-convergence", this->udpPortConvergence); @@ -254,6 +262,7 @@ void arguments::parseFile(std::string cfgFile) { {"port", [&](const std::string &value) { this->port = std::stoi(value); }}, {"mtu", [&](const std::string &value) { this->mtu = std::stoi(value); }}, {"localhost", [&](const std::string &value) { this->localhost = value; }}, + {"p2p-only", [&](const std::string &value) { this->p2pOnly = (value == "true"); }}, {"userspace-stack", [&](const std::string &value) { this->userspaceStack = (value == "true"); }}, {"udp-port-convergence", [&](const std::string &value) { this->udpPortConvergence = (value == "true"); }}, }; diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index b9d197ff..e96c02ed 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -31,6 +31,8 @@ struct arguments { int discovery = 0; int routeCost = 0; int mtu = 1400; + // 仅允许 P2P 承载业务数据;WebSocket 只保留信令(默认关闭=允许服务端中转)。 + bool p2pOnly = false; // userspace-stack:内嵌 lwIP 落地(默认关闭=继续走内核 tun)。 bool userspaceStack = false; // UDP 单端口收敛:多个内部源共用一个出口 fd/端口(默认关闭=保留每源全锥形)。 diff --git a/candy.cfg b/candy.cfg index 0648d302..7b958905 100644 --- a/candy.cfg +++ b/candy.cfg @@ -63,6 +63,15 @@ route = 5 # results may not be the best. You can specify it manually. #localhost = "127.0.0.1" +# [Optional] Allow business traffic only over direct P2P links. +# false (default): prefer P2P and fall back to WebSocket relay through the +# server when no P2P link is available. +# true: WebSocket is used only for authentication, routing and P2P signaling; +# business packets are dropped until a direct P2P link is established. +# Public-network use requires a reachable STUN server; discovery > 0 is +# recommended for periodic P2P discovery. +p2p-only = false + # [Optional] Maximum Transmission Unit #mtu=1400 diff --git a/candy/src/candy/client.cc b/candy/src/candy/client.cc index b1813e75..735b321b 100644 --- a/candy/src/candy/client.cc +++ b/candy/src/candy/client.cc @@ -99,6 +99,7 @@ bool run(const std::string &id, const Poco::JSON::Object &config) { client->setRouteCost(config.getValue("route")), client->setMtu(config.getValue("mtu")); client->setPort(config.getValue("port")); client->setLocalhost(config.getValue("localhost")); + client->setP2POnly(config.optValue("p2p-only", false)); client->setUserspaceStack(config.optValue("userspace-stack", false)); client->setUdpPortConvergence(config.optValue("udp-port-convergence", false)); client->run(); diff --git a/candy/src/core/client.cc b/candy/src/core/client.cc index 71b62a45..f3c223dc 100644 --- a/candy/src/core/client.cc +++ b/candy/src/core/client.cc @@ -70,6 +70,10 @@ bool Client::getUserspaceStack() const { return this->userspaceStack; } +bool Client::getP2POnly() const { + return this->p2pOnly; +} + bool Client::getUdpPortConvergence() const { return this->udpPortConvergence; } @@ -123,6 +127,10 @@ void Client::setMtu(int mtu) { tun.setMTU(mtu); } +void Client::setP2POnly(bool enable) { + this->p2pOnly = enable; +} + void Client::setUserspaceStack(bool enable) { this->userspaceStack = enable; } diff --git a/candy/src/core/client.h b/candy/src/core/client.h index feb94e70..c8c39371 100644 --- a/candy/src/core/client.h +++ b/candy/src/core/client.h @@ -45,6 +45,7 @@ class Client { void setExptTunAddress(const std::string &cidr); void setVirtualMac(const std::string &vmac); + void setP2POnly(bool enable); void setUserspaceStack(bool enable); void setUdpPortConvergence(bool enable); @@ -65,6 +66,7 @@ class Client { MsgQueue &getWsMsgQueue(); MsgQueue &getNetStackMsgQueue(); + bool getP2POnly() const; bool getUserspaceStack() const; bool getUdpPortConvergence() const; int getMtu() const; @@ -81,6 +83,7 @@ class Client { private: std::string tunName; + bool p2pOnly = false; bool userspaceStack = false; // UDP 单端口收敛开关(默认关闭=每源全锥形)。仅在 userspaceStack=true 时生效。 bool udpPortConvergence = false; diff --git a/candy/src/peer/manager.cc b/candy/src/peer/manager.cc index 1841c221..255b14c6 100644 --- a/candy/src/peer/manager.cc +++ b/candy/src/peer/manager.cc @@ -57,6 +57,8 @@ int PeerManager::setLocalhost(const std::string &ip) { int PeerManager::run(Client *client) { this->client = client; this->localP2PDisabled = false; + this->p2pOnlyDropCount = 0; + this->lastP2POnlyDropWarn = {}; if (this->stun.update()) { candy::logger().fatal("update stun failed"); @@ -183,6 +185,23 @@ int PeerManager::sendPacketRelay(IP4 dst, const Msg &msg) { return sendPacketDirect(dst, msg); } +IP4 PeerManager::resolvePeerTarget(IP4 dst) { + std::shared_lock rtTableLock(this->rtTableMutex); + auto it = this->rtTableMap.find(dst); + return it == this->rtTableMap.end() ? dst : it->second.next; +} + +void PeerManager::warnP2POnlyDrop(IP4 peer) { + ++this->p2pOnlyDropCount; + auto now = std::chrono::steady_clock::now(); + if (now - this->lastP2POnlyDropWarn < std::chrono::seconds(5)) { + return; + } + this->lastP2POnlyDropWarn = now; + candy::logger().warning(Poco::format("p2p-only drop: peer=%s p2p link unavailable total=%s", peer.toString(), + std::to_string(this->p2pOnlyDropCount))); +} + int PeerManager::sendPubInfo(CoreMsg::PubInfo info) { info.src = getClient().address(); if (info.local) { @@ -205,6 +224,12 @@ int PeerManager::handlePacket(Msg msg) { if (!sendPacket(header->daddr, msg)) { return 0; } + if (getClient().getP2POnly()) { + IP4 peer = resolvePeerTarget(header->daddr); + handleTryP2P(Msg(MsgKind::TRYP2P, peer.toString())); + warnP2POnlyDrop(peer); + return 0; + } getClient().getWsMsgQueue().write(std::move(msg)); return 0; } @@ -232,7 +257,7 @@ int PeerManager::handleTryP2P(Msg msg) { std::shared_lock lock(this->ipPeerMutex); auto it = this->ipPeerMap.find(src); if (it != this->ipPeerMap.end()) { - it->second.tryConnecct(); + it->second.tryConnecct(getClient().getP2POnly()); return 0; } } diff --git a/candy/src/peer/manager.h b/candy/src/peer/manager.h index ecc7ad51..986893b7 100644 --- a/candy/src/peer/manager.h +++ b/candy/src/peer/manager.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -95,6 +96,8 @@ class PeerManager { int sendPacket(IP4 dst, const Msg &msg); int sendPacketDirect(IP4 dst, const Msg &msg); int sendPacketRelay(IP4 dst, const Msg &msg); + IP4 resolvePeerTarget(IP4 dst); + void warnP2POnlyDrop(IP4 peer); Address tunAddr; @@ -146,6 +149,8 @@ class PeerManager { int discoveryInterval = 0; int routeCost = 0; + uint64_t p2pOnlyDropCount = 0; + std::chrono::steady_clock::time_point lastP2POnlyDropWarn; Client &getClient(); Client *client; diff --git a/candy/src/peer/peer.cc b/candy/src/peer/peer.cc index b52f9491..5d7cec27 100644 --- a/candy/src/peer/peer.cc +++ b/candy/src/peer/peer.cc @@ -48,8 +48,8 @@ Peer::Peer(const IP4 &addr, PeerManager *peerManager) : peerManager(peerManager) Peer::~Peer() {} -void Peer::tryConnecct() { - if (this->state == PeerState::INIT) { +void Peer::tryConnecct(bool retryFailed) { + if (this->state == PeerState::INIT || (retryFailed && this->state == PeerState::FAILED)) { updateState(PeerState::PREPARING); } } diff --git a/candy/src/peer/peer.h b/candy/src/peer/peer.h index 81431d35..2f92584e 100644 --- a/candy/src/peer/peer.h +++ b/candy/src/peer/peer.h @@ -40,7 +40,7 @@ class Peer { ~Peer(); void tick(); - void tryConnecct(); + void tryConnecct(bool retryFailed = false); void handleStunResponse(); void handlePubInfo(IP4 ip, uint16_t port, bool local = false); diff --git a/candy/src/websocket/client.cc b/candy/src/websocket/client.cc index 4bd1bea8..7c058c80 100644 --- a/candy/src/websocket/client.cc +++ b/candy/src/websocket/client.cc @@ -248,6 +248,10 @@ void WebSocketClient::handleForwardMsg(std::string buffer) { IP4Header *header = (IP4Header *)buffer.data(); // 每次通过服务端转发收到报文都触发一次尝试 P2P 连接, 用于暗示通过服务端转发是个非常耗时的操作 this->client->getPeerMsgQueue().write(Msg(MsgKind::TRYP2P, header->saddr.toString())); + if (this->client->getP2POnly()) { + candy::logger().debug(Poco::format("p2p-only drop websocket forward: peer=%s", header->saddr.toString())); + return; + } // 最后把报文移动到 TUN 模块, 因为有移动操作所以必须在最后执行 this->client->getTunMsgQueue().write(Msg(MsgKind::PACKET, std::move(buffer))); } From b519e4e84f6a8d566935a29dd1ef90dd29446842 Mon Sep 17 00:00:00 2001 From: bepartofyou Date: Thu, 23 Jul 2026 14:26:47 +0800 Subject: [PATCH 29/29] fix compile --- candy/src/candy/server.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/candy/src/candy/server.cc b/candy/src/candy/server.cc index bdd83476..ae4f2995 100644 --- a/candy/src/candy/server.cc +++ b/candy/src/candy/server.cc @@ -2,6 +2,8 @@ #include "candy/server.h" #include "core/server.h" #include "utils/atomic.h" +#include +#include namespace candy { namespace server {