Add IGMP, prepend_buffer/parse_buffer, split UDP header, discovery tests, test all

This commit is contained in:
Ian Gulliver
2026-04-11 09:04:55 +09:00
parent e486f6501a
commit a6225faa2b
17 changed files with 582 additions and 183 deletions

View File

@@ -19,7 +19,6 @@ inline std::string to_string(const ip4_addr& ip) {
}
struct __attribute__((packed)) header {
eth::header eth;
uint8_t ver_ihl;
uint8_t dscp_ecn;
uint16_t total_len;
@@ -31,24 +30,32 @@ struct __attribute__((packed)) header {
ip4_addr src;
ip4_addr dst;
size_t ip_header_len() const { return (ver_ihl & 0x0F) * 4; }
size_t ip_total_len() const { return __builtin_bswap16(total_len); }
const uint8_t* ip_start() const { return reinterpret_cast<const uint8_t*>(&ver_ihl); }
uint8_t* ip_start() { return reinterpret_cast<uint8_t*>(&ver_ihl); }
size_t header_len() const { return (ver_ihl & 0x0F) * 4; }
size_t total() const { return __builtin_bswap16(total_len); }
};
static_assert(sizeof(header) == 34);
struct __attribute__((packed)) udp_header {
header ip;
uint16_t src_port;
uint16_t dst_port;
uint16_t length;
uint16_t checksum;
};
static_assert(sizeof(udp_header) == 42);
static_assert(sizeof(header) == 20);
uint16_t checksum(const void* data, size_t len);
template <typename Buf>
void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_mac,
ip4_addr src_ip, ip4_addr dst_ip, uint8_t protocol,
size_t payload_len, uint8_t ttl = 64) {
auto* h = buf.template prepend<header>();
h->ver_ihl = 0x45;
h->dscp_ecn = 0;
h->total_len = __builtin_bswap16(sizeof(header) + payload_len);
h->identification = 0;
h->flags_frag = 0;
h->ttl = ttl;
h->protocol = protocol;
h->checksum = 0;
h->src = src_ip;
h->dst = dst_ip;
h->checksum = checksum(h, sizeof(header));
eth::prepend(buf, dst_mac, src_mac, eth::ETH_IPV4);
}
void handle(std::span<const uint8_t> frame, span_writer& tx,
eth::mac_addr our_mac, ip4_addr our_ip, ip4_addr subnet_broadcast,
std::function<void(std::span<const uint8_t>)> send_raw,