Ethertype handler registry: arp/ipv4 self-register via __attribute__((constructor)), drop threaded mac/ip/subnet params

This commit is contained in:
Ian Gulliver
2026-04-19 08:26:57 -07:00
parent 59829b569e
commit 7e493b7d70
6 changed files with 50 additions and 30 deletions

View File

@@ -1,6 +1,7 @@
#include "ipv4.h"
#include "icmp.h"
#include "igmp.h"
#include "net.h"
#include "udp.h"
#include "parse_buffer.h"
@@ -20,12 +21,12 @@ uint16_t checksum(const void* data, size_t len) {
return __builtin_bswap16(~sum);
}
static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip, const ip4_addr& subnet_broadcast) {
return dst == our_ip || dst == IP_BROADCAST_ALL || dst == subnet_broadcast || igmp::is_member(dst);
static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip) {
return dst == our_ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST || igmp::is_member(dst);
}
void handle(std::span<const uint8_t> frame, span_writer& tx,
eth::mac_addr our_mac, ip4_addr our_ip, ip4_addr subnet_broadcast) {
void handle(std::span<const uint8_t> frame, span_writer& tx) {
const auto& ns = net_get_state();
parse_buffer pb(frame);
pb.consume<eth::header>();
auto* ip = pb.consume<header>();
@@ -37,19 +38,24 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
switch (ip->protocol) {
case 1:
if (!ip_match(ip->dst, our_ip, subnet_broadcast))
if (!ip_match(ip->dst, ns.ip))
return;
icmp::handle(frame, tx, our_mac, our_ip);
icmp::handle(frame, tx, ns.mac, ns.ip);
break;
case 2:
igmp::handle(frame, tx, our_mac, our_ip);
igmp::handle(frame, tx, ns.mac, ns.ip);
break;
case 17:
if (!ip_match(ip->dst, our_ip, subnet_broadcast))
if (!ip_match(ip->dst, ns.ip))
return;
udp::handle(frame, tx);
break;
}
}
__attribute__((constructor))
static void register_ethertype() {
net_register_ethertype(eth::ETH_IPV4, handle);
}
} // namespace ipv4