extract icmp into its own static library; replace all __attribute__((constructor)) self-registrations with explicit init() calls in dispatch_init

This commit is contained in:
Ian Gulliver
2026-05-01 13:44:53 -07:00
parent 2d7ff98b82
commit a56e034bfb
12 changed files with 30 additions and 23 deletions

View File

@@ -1,40 +0,0 @@
#pragma once
#include <cstdint>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "span_writer.h"
namespace icmp {
struct __attribute__((packed)) echo {
uint8_t type;
uint8_t code;
uint16_t checksum;
uint16_t id;
uint16_t seq;
};
static_assert(sizeof(echo) == 8);
void handle(std::span<const uint8_t> frame, span_writer& tx);
template <typename Buf>
void prepend_echo_request(Buf& buf,
eth::mac_addr src_mac, ipv4::ip4_addr src_ip,
eth::mac_addr dst_mac, ipv4::ip4_addr dst_ip,
uint16_t id, uint16_t seq,
size_t payload_len = 0) {
auto* e = buf.template prepend<echo>();
e->type = 8;
e->code = 0;
e->checksum = 0;
e->id = id;
e->seq = seq;
size_t icmp_len = sizeof(echo) + payload_len;
e->checksum = ipv4::checksum(e, icmp_len);
ipv4::prepend(buf, dst_mac, src_mac, src_ip, dst_ip, 1, icmp_len);
}
bool parse_echo_reply(std::span<const uint8_t> frame, ipv4::ip4_addr& src_ip, uint16_t expected_id);
} // namespace icmp

View File

@@ -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);

View File

@@ -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<const uint8_t> frame, span_writer& tx);
using port_handler = void (*)(std::span<const uint8_t> payload, const address& from);