From a56e034bfbc21351f32b57d52814a4b0ab1b87dd Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 1 May 2026 13:44:53 -0700 Subject: [PATCH] extract icmp into its own static library; replace all __attribute__((constructor)) self-registrations with explicit init() calls in dispatch_init --- CMakeLists.txt | 3 ++- arp/arp.cpp | 5 +---- eth/eth.cpp | 7 ++----- icmp/CMakeLists.txt | 11 +++++++++++ {src => icmp}/icmp.cpp | 3 +-- {include => icmp}/icmp.h | 1 + include/igmp.h | 2 ++ include/udp.h | 1 + ipv4/ipv4.cpp | 8 ++------ src/dispatch.cpp | 6 +++++- src/igmp.cpp | 3 +-- src/udp.cpp | 3 +-- 12 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 icmp/CMakeLists.txt rename {src => icmp}/icmp.cpp (96%) rename {include => icmp}/icmp.h (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fa239a..5a27f1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,12 +7,12 @@ add_subdirectory(msgpack) add_subdirectory(eth) add_subdirectory(ipv4) add_subdirectory(arp) +add_subdirectory(icmp) add_library(limen STATIC src/dispatch.cpp src/flash.cpp src/handlers.cpp - src/icmp.cpp src/igmp.cpp src/test_handlers.cpp src/udp.cpp @@ -32,6 +32,7 @@ target_link_libraries(limen PUBLIC eth ipv4 arp + icmp pico_stdlib pico_sha256 ) diff --git a/arp/arp.cpp b/arp/arp.cpp index 5ceb482..9814ef4 100644 --- a/arp/arp.cpp +++ b/arp/arp.cpp @@ -41,10 +41,7 @@ void handle(std::span frame, span_writer& tx) { eth::send_raw(buf.span()); } -void init() {} - -__attribute__((constructor)) -static void register_ethertype() { +void init() { eth::register_ethertype(eth::ETH_ARP, handle); } diff --git a/eth/eth.cpp b/eth/eth.cpp index d81980b..f18310c 100644 --- a/eth/eth.cpp +++ b/eth/eth.cpp @@ -61,11 +61,6 @@ void process_frame(std::span frame, span_writer& tx) { } } -__attribute__((constructor)) -void register_default_mac_filter() { - register_mac_filter(default_mac_filter); -} - } // namespace void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) { @@ -114,6 +109,8 @@ bool init() { w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none); w6300::set_interrupt_mask(w6300::ik_sock_0); + register_mac_filter(default_mac_filter); + return true; } diff --git a/icmp/CMakeLists.txt b/icmp/CMakeLists.txt new file mode 100644 index 0000000..8a9ab25 --- /dev/null +++ b/icmp/CMakeLists.txt @@ -0,0 +1,11 @@ +add_library(icmp STATIC icmp.cpp) + +target_include_directories(icmp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +target_compile_options(icmp PRIVATE -Wall -Wextra -Wno-unused-parameter) + +target_link_libraries(icmp PUBLIC + util + eth + ipv4 +) diff --git a/src/icmp.cpp b/icmp/icmp.cpp similarity index 96% rename from src/icmp.cpp rename to icmp/icmp.cpp index 18de291..5c7a3b6 100644 --- a/src/icmp.cpp +++ b/icmp/icmp.cpp @@ -36,8 +36,7 @@ void handle(std::span frame, span_writer& tx) { eth::send_raw(buf.span()); } -__attribute__((constructor)) -static void register_protocol() { +void init() { ipv4::register_protocol(1, handle); } diff --git a/include/icmp.h b/icmp/icmp.h similarity index 98% rename from include/icmp.h rename to icmp/icmp.h index b4f67b9..737823e 100644 --- a/include/icmp.h +++ b/icmp/icmp.h @@ -16,6 +16,7 @@ struct __attribute__((packed)) echo { }; static_assert(sizeof(echo) == 8); +void init(); void handle(std::span frame, span_writer& tx); template diff --git a/include/igmp.h b/include/igmp.h index fc6bf10..7c964e9 100644 --- a/include/igmp.h +++ b/include/igmp.h @@ -17,6 +17,8 @@ struct __attribute__((packed)) message { }; static_assert(sizeof(message) == 8); +void init(); + eth::mac_addr mac_for_ip(const ipv4::ip4_addr& group); bool is_member(const ipv4::ip4_addr& ip); bool is_member_mac(const eth::mac_addr& mac); diff --git a/include/udp.h b/include/udp.h index e88a1e9..025dadb 100644 --- a/include/udp.h +++ b/include/udp.h @@ -36,6 +36,7 @@ void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_ma ipv4::prepend(buf, dst_mac, src_mac, src_ip, dst_ip, 17, sizeof(header) + payload_len, ttl); } +void init(); void handle(std::span frame, span_writer& tx); using port_handler = void (*)(std::span payload, const address& from); diff --git a/ipv4/ipv4.cpp b/ipv4/ipv4.cpp index 6c547a0..2df00f7 100644 --- a/ipv4/ipv4.cpp +++ b/ipv4/ipv4.cpp @@ -31,6 +31,8 @@ bool default_addr_filter(const ip4_addr& dst) { void init() { const auto& mac = eth::get_mac(); g_ip = {169, 254, mac[4], mac[5]}; + eth::register_ethertype(eth::ETH_IPV4, handle); + register_addr_filter(default_addr_filter); } const ip4_addr& get_ip() { @@ -101,10 +103,4 @@ void handle(std::span frame, span_writer& tx) { } } -__attribute__((constructor)) -static void register_self() { - eth::register_ethertype(eth::ETH_IPV4, handle); - register_addr_filter(default_addr_filter); -} - } // namespace ipv4 diff --git a/src/dispatch.cpp b/src/dispatch.cpp index 4b17976..829a850 100644 --- a/src/dispatch.cpp +++ b/src/dispatch.cpp @@ -6,6 +6,7 @@ #include "eth.h" #include "ipv4.h" #include "arp.h" +#include "icmp.h" #include "igmp.h" #include "udp.h" #include "debug_log.h" @@ -35,10 +36,13 @@ static void on_udp_message(std::span payload, const udp::address& void dispatch_init(uint16_t port_be) { listen_port_be = port_be; - udp::register_port(port_be, on_udp_message); eth::init(); ipv4::init(); arp::init(); + icmp::init(); + igmp::init(); + udp::init(); + udp::register_port(port_be, on_udp_message); dispatch_schedule_ms(60000, igmp_reannounce); dlog("dispatch_init complete"); } diff --git a/src/igmp.cpp b/src/igmp.cpp index c07ee50..74ad520 100644 --- a/src/igmp.cpp +++ b/src/igmp.cpp @@ -78,8 +78,7 @@ void handle(std::span frame, span_writer& tx) { } } -__attribute__((constructor)) -static void register_protocol() { +void init() { ipv4::register_protocol(2, handle); eth::register_mac_filter(is_member_mac); ipv4::register_addr_filter(is_member); diff --git a/src/udp.cpp b/src/udp.cpp index 9e67652..239fcfc 100644 --- a/src/udp.cpp +++ b/src/udp.cpp @@ -49,8 +49,7 @@ void handle(std::span frame, span_writer& tx) { } } -__attribute__((constructor)) -static void register_protocol() { +void init() { ipv4::register_protocol(17, handle); }