diff --git a/.agents/docs/2026-12-01-add-hev-lwip-plan.md b/.agents/docs/2026-12-01-add-hev-lwip-plan.md new file mode 100644 index 00000000..530b5844 --- /dev/null +++ b/.agents/docs/2026-12-01-add-hev-lwip-plan.md @@ -0,0 +1,30 @@ +# Package Hev's lwIP 2.2.1.12 fork + +## Source and build + +`compat.hev-lwip` builds the BSD-3-Clause C core from upstream tag +`2.2.1.12` (commit `8115cd6150ab32500db3d4d8bd8ad7ec6648534c`). +The inline descriptor exports the lwIP headers and the matching fork-specific +port headers, including `lwipopts.h`. The upstream configuration defines the +IPv4, IPv6, TCP and UDP options. The package adds no feature switches. +Linux and macOS select the Unix system port. Windows selects the Win32 port +and links Advapi32 and Winsock. The package requires no capture device. + +## Archive and updates + +All three entries use the upstream GitHub tag archive directly. Two downloads +produced SHA-256 `6b6f44cc2a0d01832352bfcd66b007a220d7e5440eb2f18ba86db532a74d3a01`. +The pinned updater recognized the numeric tag declaration and reported +`up-to-date` for `compat:hev-lwip`. A CN mirror has not been verified. + +## Verification + +The consumer injects IPv4 UDP datagrams for two remote endpoints from the +same client port and checks the two replies in reverse order, including +both IP addresses, both ports and the payload. It uses an in-memory netif. + +On Linux x86_64, the CI-pinned mcpp 2026.11.23.1 parsed the descriptor. +A fresh test home with GCC 16.2.0 and the package cache disabled passed +`bash tests/run_members.sh --cache off hev-lwip` (one test). +Descriptor lint and cross-package reference checks passed. macOS and Windows +execution remain for native CI. diff --git a/mcpp.toml b/mcpp.toml index 72c498b3..4098f90e 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -95,6 +95,7 @@ members = [ "tests/examples/catch2-v2-main", "tests/examples/capi-lua", "tests/examples/cjson", + "tests/examples/hev-lwip", "tests/examples/cmdline", "tests/examples/cmp-module", "tests/examples/cli11", diff --git a/pkgs/c/compat.hev-lwip.lua b/pkgs/c/compat.hev-lwip.lua new file mode 100644 index 00000000..6cb85d73 --- /dev/null +++ b/pkgs/c/compat.hev-lwip.lua @@ -0,0 +1,58 @@ +package = { + spec = "1", + namespace = "compat", + name = "hev-lwip", + description = "Hev lwIP fork with transparent TCP and UDP support", + licenses = { "BSD-3-Clause" }, + repo = "https://github.com/heiher/lwip", + type = "package", + + extensions = { + autoupdate = { + tag = "${version}", + url = "https://github.com/heiher/lwip/archive/refs/tags/${tag}.tar.gz", + }, + }, + + xpm = { + linux = { + ["2.2.1.12"] = { + url = "https://github.com/heiher/lwip/archive/refs/tags/2.2.1.12.tar.gz", + sha256 = "6b6f44cc2a0d01832352bfcd66b007a220d7e5440eb2f18ba86db532a74d3a01", + }, + }, + macosx = { + ["2.2.1.12"] = { + url = "https://github.com/heiher/lwip/archive/refs/tags/2.2.1.12.tar.gz", + sha256 = "6b6f44cc2a0d01832352bfcd66b007a220d7e5440eb2f18ba86db532a74d3a01", + }, + }, + windows = { + ["2.2.1.12"] = { + url = "https://github.com/heiher/lwip/archive/refs/tags/2.2.1.12.tar.gz", + sha256 = "6b6f44cc2a0d01832352bfcd66b007a220d7e5440eb2f18ba86db532a74d3a01", + }, + }, + }, + + mcpp = { + language = "c++23", + import_std = false, + c_standard = "c11", + include_dirs = { "*/src/include", "*/src/ports/include" }, + sources = { + "*/src/core/*.c", "*/src/core/ipv4/*.c", "*/src/core/ipv6/*.c", + "*/src/netif/ethernet.c", "*/src/ports/lib/mem.c", + }, + linux = { + cflags = { "-D_DEFAULT_SOURCE" }, + sources = { "*/src/ports/unix/lib/sys_arch.c" }, + }, + macosx = { sources = { "*/src/ports/unix/lib/sys_arch.c" } }, + windows = { + sources = { "*/src/ports/win32/lib/sys_arch.c" }, + ldflags = { "-ladvapi32", "-lws2_32" }, + }, + targets = { ["hev-lwip"] = { kind = "lib" } }, + }, +} diff --git a/tests/examples/hev-lwip/mcpp.toml b/tests/examples/hev-lwip/mcpp.toml new file mode 100644 index 00000000..345c834a --- /dev/null +++ b/tests/examples/hev-lwip/mcpp.toml @@ -0,0 +1,9 @@ +[package] +name = "hev-lwip-tests" +version = "0.1.0" + +[dependencies.compat] +hev-lwip = "2.2.1.12" + +[indices] +compat = { path = "../../.." } diff --git a/tests/examples/hev-lwip/tests/udp_reply.cpp b/tests/examples/hev-lwip/tests/udp_reply.cpp new file mode 100644 index 00000000..68c0e3ff --- /dev/null +++ b/tests/examples/hev-lwip/tests/udp_reply.cpp @@ -0,0 +1,161 @@ +// 在内存网卡上验证透明接收与回包端点 +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +using Address = std::array; +using Payload = std::array; +constexpr Address client{192, 0, 2, 10}; +constexpr std::uint16_t client_port = 42000; + +struct Request { + Address target; + std::uint16_t port; + Payload data; +}; + +struct PendingReply { + udp_pcb* flow; + ip_addr_t target; + std::uint16_t port; + Payload data; +}; + +struct Capture { + std::vector pending; + std::vector> packets; + udp_pcb* flow{}; +}; + +// 使断言在发布构建中也能报错退出 +void require(bool condition, const char* message) { + if (!condition) { + std::fprintf(stderr, "%s\n", message); + std::exit(EXIT_FAILURE); + } +} + +// 读取报文中的网络字节序端口或长度 +std::uint16_t read_u16(const std::uint8_t* bytes) { + return static_cast((bytes[0] << 8) | bytes[1]); +} + +// 借用输出缓冲区,在回调返回前复制完整的 IPv4 包 +err_t collect_packet(netif* device, pbuf* packet, const ip4_addr_t*) { + auto& capture = *static_cast(device->state); + auto& bytes = capture.packets.emplace_back(packet->tot_len); + require(pbuf_copy_partial(packet, bytes.data(), packet->tot_len, 0) == packet->tot_len, + "short output packet"); + return ERR_OK; +} + +// 配置仅在本进程中收发的数据链路 +err_t init_netif(netif* device) { + device->name[0] = 'u'; + device->name[1] = 't'; + device->mtu = 1500; + device->output = collect_packet; + return ERR_OK; +} + +// 保存当前目标和负载,允许在后续数据报更改控制块后回包 +void receive(void* arg, udp_pcb* flow, pbuf* packet, const ip_addr_t*, u16_t) { + auto& capture = *static_cast(arg); + PendingReply reply{flow, flow->local_ip, flow->local_port, {}}; + require(packet->tot_len == reply.data.size(), "unexpected request size"); + pbuf_copy_partial(packet, reply.data.data(), packet->tot_len, 0); + pbuf_free(packet); + capture.pending.push_back(reply); +} + +// 首包由协议栈再次投递,此处只安装数据处理回调 +void accept_flow(void* arg, udp_pcb* flow, pbuf*, const ip_addr_t*, u16_t) { + auto& capture = *static_cast(arg); + require(!capture.flow, "expected one client association"); + capture.flow = flow; + udp_recv(flow, receive, arg); +} + +// 构造合法 IPv4 数据报,UDP 校验和为 IPv4 允许的零值 +void inject(netif& device, const Request& request) { + constexpr std::size_t header = 28; + std::array bytes{ + 0x45, 0, 0, header + Payload{}.size(), 0, 0, 0, 0, 64, 17 + }; + std::copy(client.begin(), client.end(), bytes.begin() + 12); + std::copy(request.target.begin(), request.target.end(), bytes.begin() + 16); + const auto checksum = inet_chksum(bytes.data(), 20); + std::memcpy(bytes.data() + 10, &checksum, sizeof(checksum)); + bytes[20] = client_port >> 8; + bytes[21] = client_port & 0xff; + bytes[22] = request.port >> 8; + bytes[23] = request.port & 0xff; + bytes[25] = 8 + request.data.size(); + std::copy(request.data.begin(), request.data.end(), bytes.begin() + header); + auto* packet = pbuf_alloc(PBUF_RAW, bytes.size(), PBUF_RAM); + require(packet && pbuf_take(packet, bytes.data(), bytes.size()) == ERR_OK, "input allocation failed"); + require(device.input(packet, &device) == ERR_OK, "IP input failed"); +} + +// 延迟发送,显式还原原始目标端点作为回复的源端点 +void send_reply(const PendingReply& reply) { + auto* packet = pbuf_alloc(PBUF_TRANSPORT, reply.data.size(), PBUF_RAM); + require(packet && pbuf_take(packet, reply.data.data(), reply.data.size()) == ERR_OK, "reply allocation failed"); + require(udp_sendfrom(reply.flow, packet, &reply.target, reply.port) == ERR_OK, "UDP send failed"); + pbuf_free(packet); +} + +// 从设备输出的线上格式独立核对协议、两个端点、长度和负载 +void verify(const std::vector& bytes, const Request& request) { + require(bytes.size() == 28 + request.data.size(), "wrong packet length"); + require(bytes[0] == 0x45 && bytes[9] == 17 && read_u16(&bytes[2]) == bytes.size(), "wrong IP header"); + require(std::equal(request.target.begin(), request.target.end(), bytes.begin() + 12), "wrong reply source IP"); + require(std::equal(client.begin(), client.end(), bytes.begin() + 16), "wrong reply destination IP"); + require(read_u16(&bytes[20]) == request.port && read_u16(&bytes[22]) == client_port, "wrong UDP ports"); + require(read_u16(&bytes[24]) == 8 + request.data.size(), "wrong UDP length"); + require(std::equal(request.data.begin(), request.data.end(), bytes.begin() + 28), "wrong UDP payload"); +} + +// 从同一端口访问两个非本地目标,再按相反顺序发送回复 +int main() { + lwip_init(); + Capture capture; + netif device{}; + ip4_addr_t address, mask, gateway{}; + IP4_ADDR(&address, 192, 0, 2, 1); + IP4_ADDR(&mask, 255, 255, 255, 0); + require(netif_add(&device, &address, &mask, &gateway, &capture, init_netif, ip4_input), "netif setup failed"); + netif_set_default(&device); + netif_set_up(&device); + netif_set_link_up(&device); + netif_set_flags(&device, NETIF_FLAG_PRETEND_UDP); + auto* listener = udp_new_ip_type(IPADDR_TYPE_ANY); + require(listener, "UDP listener allocation failed"); + udp_bind_netif(listener, &device); + require(udp_bind(listener, nullptr, 0) == ERR_OK, "transparent bind failed"); + udp_recv(listener, accept_flow, &capture); + + const Request first{{198, 51, 100, 17}, 5300, {'o', 'n', 'e'}}; + const Request second{{203, 0, 113, 29}, 4400, {'t', 'w', 'o'}}; + inject(device, first); + inject(device, second); + require(capture.pending.size() == 2, "expected two UDP receives"); + send_reply(capture.pending[1]); + send_reply(capture.pending[0]); + require(capture.packets.size() == 2, "expected two UDP replies"); + verify(capture.packets[0], second); + verify(capture.packets[1], first); + + udp_remove(capture.flow); + udp_remove(listener); + netif_remove(&device); +}