From fceae27f10481330981ef92d42b986ea990f7f30 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 1 May 2026 11:03:16 -0700 Subject: [PATCH] merge net into eth lib; ipv4 owns its IP via ipv4::init/get_ip; drop state struct in favor of eth::get_mac/ipv4::get_ip --- CMakeLists.txt | 4 ++-- eth/CMakeLists.txt | 12 ++++++++++ src/net.cpp => eth/eth.cpp | 49 ++++++++++++++++---------------------- eth/eth.h | 47 ++++++++++++++++++++++++++++++++++++ include/dispatch.h | 8 +++---- include/eth.h | 28 ---------------------- include/ipv4.h | 3 +++ include/net.h | 33 ------------------------- include/udp.h | 1 - src/arp.cpp | 18 +++++++------- src/dispatch.cpp | 8 ++++--- src/handlers.cpp | 8 +++---- src/icmp.cpp | 7 +++--- src/igmp.cpp | 9 ++++--- src/ipv4.cpp | 18 ++++++++++---- src/test_handlers.cpp | 43 ++++++++++++++++----------------- src/udp.cpp | 1 - 17 files changed, 149 insertions(+), 148 deletions(-) create mode 100644 eth/CMakeLists.txt rename src/net.cpp => eth/eth.cpp (75%) create mode 100644 eth/eth.h delete mode 100644 include/eth.h delete mode 100644 include/net.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ee01c6..f88c959 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ add_subdirectory(util) add_subdirectory(w6300) add_subdirectory(debug_log) add_subdirectory(msgpack) +add_subdirectory(eth) add_library(limen STATIC src/arp.cpp @@ -13,7 +14,6 @@ add_library(limen STATIC src/icmp.cpp src/igmp.cpp src/ipv4.cpp - src/net.cpp src/test_handlers.cpp src/udp.cpp ) @@ -29,9 +29,9 @@ target_link_libraries(limen PUBLIC w6300 debug_log msgpack + eth pico_stdlib pico_sha256 - pico_unique_id ) set(LIMEN_PARTITION_TABLE ${CMAKE_CURRENT_SOURCE_DIR}/partition_table.json CACHE INTERNAL "") diff --git a/eth/CMakeLists.txt b/eth/CMakeLists.txt new file mode 100644 index 0000000..33775da --- /dev/null +++ b/eth/CMakeLists.txt @@ -0,0 +1,12 @@ +add_library(eth STATIC eth.cpp) + +target_include_directories(eth PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +target_compile_options(eth PRIVATE -Wall -Wextra -Wno-unused-parameter) + +target_link_libraries(eth PUBLIC + util + debug_log + w6300 + pico_unique_id +) diff --git a/src/net.cpp b/eth/eth.cpp similarity index 75% rename from src/net.cpp rename to eth/eth.cpp index d47a8cd..d81980b 100644 --- a/src/net.cpp +++ b/eth/eth.cpp @@ -1,18 +1,16 @@ -#include "net.h" +#include "eth.h" #include #include "pico/unique_id.h" -#include "pico/time.h" -#include "eth.h" #include "parse_buffer.h" #include "prepend_buffer.h" #include "w6300.h" #include "debug_log.h" -namespace net { +namespace eth { namespace { -state g_state; +mac_addr g_mac; w6300::socket_id raw_socket{0}; frame_cb_list frame_callbacks; @@ -26,11 +24,11 @@ size_t eth_handler_count = 0; std::array mac_filters; size_t mac_filter_count = 0; -bool default_mac_filter(const eth::mac_addr& dst) { - return dst == g_state.mac || dst == eth::MAC_BROADCAST; +bool default_mac_filter(const mac_addr& dst) { + return dst == g_mac || dst == MAC_BROADCAST; } -bool mac_match(const eth::mac_addr& dst) { +bool mac_match(const mac_addr& dst) { for (size_t i = 0; i < mac_filter_count; i++) { if (mac_filters[i](dst)) { return true; @@ -40,10 +38,10 @@ bool mac_match(const eth::mac_addr& dst) { } void process_frame(std::span frame, span_writer& tx) { - if (frame.size() < sizeof(eth::header)) { + if (frame.size() < sizeof(header)) { return; } - auto& eth_hdr = *reinterpret_cast(frame.data()); + auto& eth_hdr = *reinterpret_cast(frame.data()); if (!mac_match(eth_hdr.dst)) { return; @@ -72,7 +70,7 @@ void register_default_mac_filter() { void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) { if (eth_handler_count >= eth_handlers.size()) { - dlogf("net::register_ethertype overflow: ethertype=0x%04x dropped", __builtin_bswap16(ethertype_be)); + dlogf("eth::register_ethertype overflow: ethertype=0x%04x dropped", __builtin_bswap16(ethertype_be)); return; } eth_handlers[eth_handler_count++] = {ethertype_be, fn}; @@ -80,14 +78,14 @@ void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) { void register_mac_filter(mac_filter fn) { if (mac_filter_count >= mac_filters.size()) { - dlog("net::register_mac_filter overflow: filter dropped"); + dlog("eth::register_mac_filter overflow: filter dropped"); return; } mac_filters[mac_filter_count++] = fn; } void send_raw(std::span data) { - dlog_if_slow("net::send_raw", 1000, [&]{ + dlog_if_slow("eth::send_raw", 1000, [&]{ auto result = w6300::send(raw_socket, data); if (!result) { dlogf("w6300 send failed: %zu bytes, err %d", @@ -106,17 +104,12 @@ bool init() { pico_unique_board_id_t uid; pico_get_unique_board_id(&uid); - g_state.mac[0] = (uid.id[0] & 0xFC) | 0x02; - g_state.mac[1] = uid.id[1]; - g_state.mac[2] = uid.id[2]; - g_state.mac[3] = uid.id[3]; - g_state.mac[4] = uid.id[4]; - g_state.mac[5] = uid.id[5]; - - g_state.ip[0] = 169; - g_state.ip[1] = 254; - g_state.ip[2] = g_state.mac[4]; - g_state.ip[3] = g_state.mac[5]; + g_mac[0] = (uid.id[0] & 0xFC) | 0x02; + g_mac[1] = uid.id[1]; + g_mac[2] = uid.id[2]; + g_mac[3] = uid.id[3]; + g_mac[4] = uid.id[4]; + g_mac[5] = uid.id[5]; w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none); w6300::set_interrupt_mask(w6300::ik_sock_0); @@ -124,14 +117,14 @@ bool init() { return true; } -const state& get_state() { - return g_state; +const mac_addr& get_mac() { + return g_mac; } frame_cb_handle add_frame_callback(frame_callback cb) { auto h = frame_callbacks.insert(cb); if (!h) { - dlog("net::add_frame_callback overflow: callback dropped"); + dlog("eth::add_frame_callback overflow: callback dropped"); } return h; } @@ -158,4 +151,4 @@ void poll(std::span tx) { w6300::rearm_gpio_irq(); } -} // namespace net +} // namespace eth diff --git a/eth/eth.h b/eth/eth.h new file mode 100644 index 0000000..fa9879a --- /dev/null +++ b/eth/eth.h @@ -0,0 +1,47 @@ +#pragma once +#include +#include +#include +#include "callback_list.h" +#include "span_writer.h" + +namespace eth { + +using mac_addr = std::array; + +static constexpr mac_addr MAC_BROADCAST = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; +static constexpr uint16_t ETH_ARP = __builtin_bswap16(0x0806); +static constexpr uint16_t ETH_IPV4 = __builtin_bswap16(0x0800); + +struct __attribute__((packed)) header { + mac_addr dst; + mac_addr src; + uint16_t ethertype; +}; +static_assert(sizeof(header) == 14); + +template +void prepend(Buf& buf, const mac_addr& dst, const mac_addr& src, uint16_t ethertype) { + auto* h = buf.template prepend
(); + h->dst = dst; + h->src = src; + h->ethertype = ethertype; +} + +using frame_callback = bool (*)(std::span frame); +using frame_cb_list = callback_list; +using frame_cb_handle = frame_cb_list::node*; + +using ethertype_handler = void (*)(std::span frame, span_writer& tx); +using mac_filter = bool (*)(const mac_addr& dst); + +bool init(); +const mac_addr& get_mac(); +frame_cb_handle add_frame_callback(frame_callback cb); +void remove_frame_callback(frame_cb_handle h); +void poll(std::span tx); +void send_raw(std::span data); +void register_ethertype(uint16_t ethertype_be, ethertype_handler fn); +void register_mac_filter(mac_filter fn); + +} // namespace eth diff --git a/include/dispatch.h b/include/dispatch.h index 7b3a9a7..1f5714e 100644 --- a/include/dispatch.h +++ b/include/dispatch.h @@ -5,7 +5,8 @@ #include #include "wire.h" #include "timer_queue.h" -#include "net.h" +#include "eth.h" +#include "ipv4.h" #include "prepend_buffer.h" #include "udp.h" @@ -17,7 +18,6 @@ struct responder { template void respond(const T& msg) const { - const auto& ns = net::get_state(); prepend_buffer<4096> buf; span_writer out(buf.payload_ptr(), 2048); auto r = encode_response_into(out, message_id, msg); @@ -25,9 +25,9 @@ struct responder { return; } buf.append(*r); - udp::prepend(buf, reply_to.mac, ns.mac, ns.ip, reply_to.ip, + udp::prepend(buf, reply_to.mac, eth::get_mac(), ipv4::get_ip(), reply_to.ip, dispatch_listen_port_be(), reply_to.port, *r); - net::send_raw(buf.span()); + eth::send_raw(buf.span()); } }; diff --git a/include/eth.h b/include/eth.h deleted file mode 100644 index df02e3f..0000000 --- a/include/eth.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include -#include - -namespace eth { - -using mac_addr = std::array; - -static constexpr mac_addr MAC_BROADCAST = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; -static constexpr uint16_t ETH_ARP = __builtin_bswap16(0x0806); -static constexpr uint16_t ETH_IPV4 = __builtin_bswap16(0x0800); - -struct __attribute__((packed)) header { - mac_addr dst; - mac_addr src; - uint16_t ethertype; -}; -static_assert(sizeof(header) == 14); - -template -void prepend(Buf& buf, const mac_addr& dst, const mac_addr& src, uint16_t ethertype) { - auto* h = buf.template prepend
(); - h->dst = dst; - h->src = src; - h->ethertype = ethertype; -} - -} // namespace eth diff --git a/include/ipv4.h b/include/ipv4.h index 8cae60d..e8d4e0b 100644 --- a/include/ipv4.h +++ b/include/ipv4.h @@ -59,6 +59,9 @@ void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_ma void handle(std::span frame, span_writer& tx); +void init(); +const ip4_addr& get_ip(); + bool addressed_to_us(ip4_addr dst); using protocol_handler = void (*)(std::span frame, span_writer& tx); diff --git a/include/net.h b/include/net.h deleted file mode 100644 index 44110a1..0000000 --- a/include/net.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once -#include -#include -#include "eth.h" -#include "ipv4.h" -#include "span_writer.h" -#include "callback_list.h" - -namespace net { - -struct state { - eth::mac_addr mac; - ipv4::ip4_addr ip; -}; - -using frame_callback = bool (*)(std::span frame); - -using frame_cb_list = callback_list; -using frame_cb_handle = frame_cb_list::node*; - -using ethertype_handler = void (*)(std::span frame, span_writer& tx); -using mac_filter = bool (*)(const eth::mac_addr& dst); - -bool init(); -const state& get_state(); -frame_cb_handle add_frame_callback(frame_callback cb); -void remove_frame_callback(frame_cb_handle h); -void poll(std::span tx); -void send_raw(std::span data); -void register_ethertype(uint16_t ethertype_be, ethertype_handler fn); -void register_mac_filter(mac_filter fn); - -} // namespace net diff --git a/include/udp.h b/include/udp.h index 71c6e9e..e88a1e9 100644 --- a/include/udp.h +++ b/include/udp.h @@ -3,7 +3,6 @@ #include #include "eth.h" #include "ipv4.h" -#include "net.h" #include "span_writer.h" namespace udp { diff --git a/src/arp.cpp b/src/arp.cpp index 30d20b8..b64733c 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -1,5 +1,6 @@ #include "arp.h" -#include "net.h" +#include "eth.h" +#include "ipv4.h" #include "parse_buffer.h" #include "prepend_buffer.h" @@ -11,7 +12,8 @@ static constexpr uint16_t ARP_OP_REQUEST = __builtin_bswap16(1); static constexpr uint16_t ARP_OP_REPLY = __builtin_bswap16(2); void handle(std::span frame, span_writer& tx) { - const auto& ns = net::get_state(); + const auto& mac = eth::get_mac(); + const auto& ip = ipv4::get_ip(); parse_buffer pb(frame); pb.consume(); auto* arp_hdr = pb.consume
(); @@ -21,7 +23,7 @@ void handle(std::span frame, span_writer& tx) { if (arp_hdr->ptype != ARP_PTYPE_IPV4) return; if (arp_hdr->hlen != 6 || arp_hdr->plen != 4) return; if (arp_hdr->oper != ARP_OP_REQUEST) return; - if (arp_hdr->tpa != ns.ip) return; + if (arp_hdr->tpa != ip) return; prepend_buffer<4096> buf; auto* reply = buf.template prepend
(); @@ -30,18 +32,18 @@ void handle(std::span frame, span_writer& tx) { reply->hlen = 6; reply->plen = 4; reply->oper = ARP_OP_REPLY; - reply->sha = ns.mac; - reply->spa = ns.ip; + reply->sha = mac; + reply->spa = ip; reply->tha = arp_hdr->sha; reply->tpa = arp_hdr->spa; - eth::prepend(buf, arp_hdr->sha, ns.mac, eth::ETH_ARP); + eth::prepend(buf, arp_hdr->sha, mac, eth::ETH_ARP); - net::send_raw(buf.span()); + eth::send_raw(buf.span()); } __attribute__((constructor)) static void register_ethertype() { - net::register_ethertype(eth::ETH_ARP, handle); + eth::register_ethertype(eth::ETH_ARP, handle); } } // namespace arp diff --git a/src/dispatch.cpp b/src/dispatch.cpp index 2470a11..0947e91 100644 --- a/src/dispatch.cpp +++ b/src/dispatch.cpp @@ -3,7 +3,8 @@ #include "pico/stdlib.h" #include "wire.h" #include "timer_queue.h" -#include "net.h" +#include "eth.h" +#include "ipv4.h" #include "igmp.h" #include "udp.h" #include "debug_log.h" @@ -34,7 +35,8 @@ 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); - net::init(); + eth::init(); + ipv4::init(); dispatch_schedule_ms(60000, igmp_reannounce); dlog("dispatch_init complete"); } @@ -59,7 +61,7 @@ bool dispatch_cancel_timer(timer_handle h) { uint32_t save = save_and_disable_interrupts(); dlog_if_slow("timers", 1000, [&]{ timers.run(); }); - dlog_if_slow("net::poll", 1000, [&]{ net::poll(std::span{tx_buf}); }); + dlog_if_slow("eth::poll", 1000, [&]{ eth::poll(std::span{tx_buf}); }); __wfi(); restore_interrupts(save); diff --git a/src/handlers.cpp b/src/handlers.cpp index cb7e6d5..62edafc 100644 --- a/src/handlers.cpp +++ b/src/handlers.cpp @@ -5,7 +5,8 @@ #include "hardware/watchdog.h" #include "flash.h" #include "dispatch.h" -#include "net.h" +#include "eth.h" +#include "ipv4.h" #include "debug_log.h" static boot_reason detected_boot_reason; @@ -34,9 +35,8 @@ std::optional handle_info(const responder&, const RequestInfo&) { pico_unique_board_id_t uid; pico_get_unique_board_id(&uid); std::copy(uid.id, uid.id + 8, resp.board_id.begin()); - auto& ns = net::get_state(); - resp.mac = ns.mac; - resp.ip = ns.ip; + resp.mac = eth::get_mac(); + resp.ip = ipv4::get_ip(); resp.firmware_name = firmware_name; resp.boot = detected_boot_reason; resp.build_epoch = firmware_build_epoch; diff --git a/src/icmp.cpp b/src/icmp.cpp index 686d424..18de291 100644 --- a/src/icmp.cpp +++ b/src/icmp.cpp @@ -1,7 +1,7 @@ #include "icmp.h" #include +#include "eth.h" #include "ipv4.h" -#include "net.h" #include "parse_buffer.h" #include "prepend_buffer.h" @@ -24,7 +24,6 @@ void handle(std::span frame, span_writer& tx) { if (!icmp_pkt) return; if (icmp_pkt->type != 8) return; - const auto& ns = net::get_state(); prepend_buffer<4096> buf; memcpy(buf.append(icmp_len), pb.remaining().data() - sizeof(echo), icmp_len); @@ -33,8 +32,8 @@ void handle(std::span frame, span_writer& tx) { reply->checksum = 0; reply->checksum = ipv4::checksum(reply, icmp_len); - ipv4::prepend(buf, eth_hdr->src, ns.mac, ns.ip, ip->src, 1, icmp_len); - net::send_raw(buf.span()); + ipv4::prepend(buf, eth_hdr->src, eth::get_mac(), ipv4::get_ip(), ip->src, 1, icmp_len); + eth::send_raw(buf.span()); } __attribute__((constructor)) diff --git a/src/igmp.cpp b/src/igmp.cpp index 0849e41..c07ee50 100644 --- a/src/igmp.cpp +++ b/src/igmp.cpp @@ -1,7 +1,7 @@ #include "igmp.h" #include +#include "eth.h" #include "ipv4.h" -#include "net.h" #include "parse_buffer.h" #include "prepend_buffer.h" @@ -35,10 +35,9 @@ bool is_member_mac(const eth::mac_addr& mac) { } static void send_report(const ipv4::ip4_addr& group) { - const auto& ns = net::get_state(); prepend_buffer<4096> buf; - prepend_report(buf, ns.mac, ns.ip, group); - net::send_raw(buf.span()); + prepend_report(buf, eth::get_mac(), ipv4::get_ip(), group); + eth::send_raw(buf.span()); } void join(const ipv4::ip4_addr& group) { @@ -82,7 +81,7 @@ void handle(std::span frame, span_writer& tx) { __attribute__((constructor)) static void register_protocol() { ipv4::register_protocol(2, handle); - net::register_mac_filter(is_member_mac); + eth::register_mac_filter(is_member_mac); ipv4::register_addr_filter(is_member); } diff --git a/src/ipv4.cpp b/src/ipv4.cpp index 2e24223..6c547a0 100644 --- a/src/ipv4.cpp +++ b/src/ipv4.cpp @@ -1,6 +1,6 @@ #include "ipv4.h" #include -#include "net.h" +#include "eth.h" #include "parse_buffer.h" #include "debug_log.h" @@ -10,6 +10,8 @@ namespace { constexpr ip4_addr IP_BROADCAST_ALL = {255, 255, 255, 255}; +ip4_addr g_ip; + std::array addr_filters; size_t addr_filter_count = 0; @@ -21,12 +23,20 @@ std::array protocol_handlers; size_t protocol_handler_count = 0; bool default_addr_filter(const ip4_addr& dst) { - const auto& ns = net::get_state(); - return dst == ns.ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST; + return dst == g_ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST; } } // namespace +void init() { + const auto& mac = eth::get_mac(); + g_ip = {169, 254, mac[4], mac[5]}; +} + +const ip4_addr& get_ip() { + return g_ip; +} + uint16_t checksum(const void* data, size_t len) { auto p = static_cast(data); uint32_t sum = 0; @@ -93,7 +103,7 @@ void handle(std::span frame, span_writer& tx) { __attribute__((constructor)) static void register_self() { - net::register_ethertype(eth::ETH_IPV4, handle); + eth::register_ethertype(eth::ETH_IPV4, handle); register_addr_filter(default_addr_filter); } diff --git a/src/test_handlers.cpp b/src/test_handlers.cpp index 9c23cb2..bde0cad 100644 --- a/src/test_handlers.cpp +++ b/src/test_handlers.cpp @@ -4,7 +4,8 @@ #include "pico/stdlib.h" #include "pico/time.h" #include "handlers.h" -#include "net.h" +#include "eth.h" +#include "ipv4.h" #include "icmp.h" #include "igmp.h" #include "udp.h" @@ -55,7 +56,7 @@ struct test_state { bool in_flight = false; responder resp; timer_handle timer = nullptr; - net::frame_cb_handle frame_cb = nullptr; + eth::frame_cb_handle frame_cb = nullptr; discovery_data* active_discovery = nullptr; ping_rate_data* active_rate = nullptr; @@ -72,7 +73,7 @@ static test_state ts; static void test_end(const ResponseTest& result) { if (ts.timer) { dispatch_cancel_timer(ts.timer); ts.timer = nullptr; } - if (ts.frame_cb) { net::remove_frame_callback(ts.frame_cb); ts.frame_cb = nullptr; } + if (ts.frame_cb) { eth::remove_frame_callback(ts.frame_cb); ts.frame_cb = nullptr; } ts.active_discovery = nullptr; ts.active_rate = nullptr; ts.resp.respond(result); @@ -93,7 +94,7 @@ static bool discover_reply_cb(std::span frame) { if (options_len > 0 && !pb.skip(options_len)) return false; auto* uhdr = pb.consume(); if (!uhdr || uhdr->src_port != PICOMAP_PORT_BE) return false; - if (ip->src == net::get_state().ip) return false; + if (ip->src == ipv4::get_ip()) return false; dispatch_cancel_timer(ts.timer); ts.timer = nullptr; ts.frame_cb = nullptr; @@ -105,7 +106,7 @@ static bool discover_reply_cb(std::span frame) { } static void discover_timeout_cb() { - net::remove_frame_callback(ts.frame_cb); + eth::remove_frame_callback(ts.frame_cb); ts.frame_cb = nullptr; ts.timer = nullptr; auto cont = ts.active_discovery ? ts.active_discovery->on_timeout : nullptr; @@ -119,7 +120,6 @@ static void discover_peer(discovery_data& d, d.on_timeout = timeout; ts.active_discovery = &d; - const auto& ns = net::get_state(); eth::mac_addr mcast_mac = igmp::mac_for_ip(PICOMAP_DISCOVERY_GROUP); prepend_buffer<4096> buf; @@ -134,13 +134,13 @@ static void discover_peer(discovery_data& d, } buf.append(*encoded); - udp::prepend(buf, mcast_mac, ns.mac, ns.ip, PICOMAP_DISCOVERY_GROUP, + udp::prepend(buf, mcast_mac, eth::get_mac(), ipv4::get_ip(), PICOMAP_DISCOVERY_GROUP, PICOMAP_PORT_BE, PICOMAP_PORT_BE, *encoded, 1); - ts.frame_cb = net::add_frame_callback(discover_reply_cb); + ts.frame_cb = eth::add_frame_callback(discover_reply_cb); ts.timer = dispatch_schedule_ms(5000, discover_timeout_cb); - net::send_raw(buf.span()); + eth::send_raw(buf.span()); } static bool igmp_report_cb(std::span frame) { @@ -158,14 +158,13 @@ static void igmp_timeout_cb() { } static void test_discovery_igmp() { - const auto& ns = net::get_state(); prepend_buffer<4096> buf; - igmp::prepend_query(buf, ns.mac, ns.ip, PICOMAP_DISCOVERY_GROUP); + igmp::prepend_query(buf, eth::get_mac(), ipv4::get_ip(), PICOMAP_DISCOVERY_GROUP); - ts.frame_cb = net::add_frame_callback(igmp_report_cb); + ts.frame_cb = eth::add_frame_callback(igmp_report_cb); ts.timer = dispatch_schedule_ms(5000, igmp_timeout_cb); - net::send_raw(buf.span()); + eth::send_raw(buf.span()); } static void info_found(const peer_info& peer) { @@ -184,7 +183,7 @@ static bool ping_reply_cb(std::span frame) { ipv4::ip4_addr src_ip; if (!icmp::parse_echo_reply(frame, src_ip, PING_ECHO_ID)) return false; ts.frame_cb = nullptr; - if (src_ip == net::get_state().ip) + if (src_ip == ipv4::get_ip()) test_end({false, {"got reply from self: " + ipv4::to_string(src_ip)}}); else test_end({true, {"reply from " + ipv4::to_string(src_ip)}}); @@ -197,13 +196,12 @@ static void ping_timeout_cb() { } static void start_ping(ipv4::ip4_addr dst_ip) { - const auto& ns = net::get_state(); prepend_buffer<4096> buf; - icmp::prepend_echo_request(buf, ns.mac, ns.ip, + icmp::prepend_echo_request(buf, eth::get_mac(), ipv4::get_ip(), eth::MAC_BROADCAST, dst_ip, PING_ECHO_ID, 1); - ts.frame_cb = net::add_frame_callback(ping_reply_cb); + ts.frame_cb = eth::add_frame_callback(ping_reply_cb); ts.timer = dispatch_schedule_ms(5000, ping_timeout_cb); - net::send_raw(buf.span()); + eth::send_raw(buf.span()); } static void test_ping_subnet() { start_ping({169, 254, 255, 255}); } @@ -215,22 +213,21 @@ static size_t ping_rate_frame_size() { } static void ping_rate_send_one() { - const auto& ns = net::get_state(); auto& r = *ts.active_rate; prepend_buffer<4096> buf; if (r.payload_len > 0) memset(buf.append(r.payload_len), 0xAA, r.payload_len); - icmp::prepend_echo_request(buf, ns.mac, ns.ip, + icmp::prepend_echo_request(buf, eth::get_mac(), ipv4::get_ip(), r.peer.mac, r.peer.ip, PING_RATE_ECHO_ID, r.sent + 1, r.payload_len); - net::send_raw(buf.span()); + eth::send_raw(buf.span()); r.sent++; } static bool ping_rate_reply_cb(std::span frame) { ipv4::ip4_addr src_ip; if (!icmp::parse_echo_reply(frame, src_ip, PING_RATE_ECHO_ID)) return false; - if (src_ip == net::get_state().ip) return false; + if (src_ip == ipv4::get_ip()) return false; auto& r = *ts.active_rate; r.received++; @@ -276,7 +273,7 @@ static void ping_rate_found(const peer_info& peer) { r.received = 0; r.start_us = time_us_32(); - ts.frame_cb = net::add_frame_callback(ping_rate_reply_cb); + ts.frame_cb = eth::add_frame_callback(ping_rate_reply_cb); ts.timer = dispatch_schedule_ms(10000, ping_rate_timeout_cb); for (uint16_t i = 0; i < r.pipeline && r.sent < r.target; i++) diff --git a/src/udp.cpp b/src/udp.cpp index d5c7542..9e67652 100644 --- a/src/udp.cpp +++ b/src/udp.cpp @@ -2,7 +2,6 @@ #include #include "eth.h" #include "ipv4.h" -#include "net.h" #include "parse_buffer.h" #include "debug_log.h"