extract ipv4 into its own static library

This commit is contained in:
Ian Gulliver
2026-05-01 13:23:10 -07:00
parent fceae27f10
commit f64cdcaacf
4 changed files with 13 additions and 1 deletions

11
ipv4/CMakeLists.txt Normal file
View File

@@ -0,0 +1,11 @@
add_library(ipv4 STATIC ipv4.cpp)
target_include_directories(ipv4 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(ipv4 PRIVATE -Wall -Wextra -Wno-unused-parameter)
target_link_libraries(ipv4 PUBLIC
util
eth
debug_log
)

110
ipv4/ipv4.cpp Normal file
View File

@@ -0,0 +1,110 @@
#include "ipv4.h"
#include <array>
#include "eth.h"
#include "parse_buffer.h"
#include "debug_log.h"
namespace ipv4 {
namespace {
constexpr ip4_addr IP_BROADCAST_ALL = {255, 255, 255, 255};
ip4_addr g_ip;
std::array<addr_filter, 4> addr_filters;
size_t addr_filter_count = 0;
struct protocol_entry {
uint8_t protocol;
protocol_handler fn;
};
std::array<protocol_entry, 8> protocol_handlers;
size_t protocol_handler_count = 0;
bool default_addr_filter(const ip4_addr& dst) {
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<const uint8_t*>(data);
uint32_t sum = 0;
for (size_t i = 0; i < len - 1; i += 2) {
sum += (p[i] << 8) | p[i + 1];
}
if (len & 1) {
sum += p[len - 1] << 8;
}
while (sum >> 16) {
sum = (sum & 0xFFFF) + (sum >> 16);
}
return __builtin_bswap16(~sum);
}
void register_addr_filter(addr_filter fn) {
if (addr_filter_count >= addr_filters.size()) {
dlog("ipv4::register_addr_filter overflow: filter dropped");
return;
}
addr_filters[addr_filter_count++] = fn;
}
bool addressed_to_us(ip4_addr dst) {
for (size_t i = 0; i < addr_filter_count; i++) {
if (addr_filters[i](dst)) {
return true;
}
}
return false;
}
void register_protocol(uint8_t protocol, protocol_handler fn) {
if (protocol_handler_count >= protocol_handlers.size()) {
dlogf("ipv4::register_protocol overflow: protocol=%u dropped", protocol);
return;
}
protocol_handlers[protocol_handler_count++] = {protocol, fn};
}
void handle(std::span<const uint8_t> frame, span_writer& tx) {
parse_buffer pb(frame);
pb.consume<eth::header>();
auto* ip = pb.consume<header>();
if (!ip) {
return;
}
if ((ip->ver_ihl >> 4) != 4) {
return;
}
size_t options_len = ip->header_len() - sizeof(header);
if (options_len > 0 && !pb.skip(options_len)) {
return;
}
for (size_t i = 0; i < protocol_handler_count; i++) {
if (protocol_handlers[i].protocol == ip->protocol) {
protocol_handlers[i].fn(frame, tx);
break;
}
}
}
__attribute__((constructor))
static void register_self() {
eth::register_ethertype(eth::ETH_IPV4, handle);
register_addr_filter(default_addr_filter);
}
} // namespace ipv4

73
ipv4/ipv4.h Normal file
View File

@@ -0,0 +1,73 @@
#pragma once
#include <array>
#include <cstdint>
#include <cstdio>
#include <span>
#include <string>
#include "eth.h"
#include "span_writer.h"
namespace ipv4 {
using ip4_addr = std::array<uint8_t, 4>;
inline std::string to_string(const ip4_addr& ip) {
char buf[16];
snprintf(buf, sizeof(buf), "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
return buf;
}
struct __attribute__((packed)) header {
uint8_t ver_ihl;
uint8_t dscp_ecn;
uint16_t total_len;
uint16_t identification;
uint16_t flags_frag;
uint8_t ttl;
uint8_t protocol;
uint16_t checksum;
ip4_addr src;
ip4_addr dst;
size_t header_len() const { return (ver_ihl & 0x0F) * 4; }
size_t total() const { return __builtin_bswap16(total_len); }
};
static_assert(sizeof(header) == 20);
uint16_t checksum(const void* data, size_t len);
static constexpr ip4_addr SUBNET_BROADCAST = {169, 254, 255, 255};
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);
void init();
const ip4_addr& get_ip();
bool addressed_to_us(ip4_addr dst);
using protocol_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
void register_protocol(uint8_t protocol, protocol_handler fn);
using addr_filter = bool (*)(const ip4_addr& dst);
void register_addr_filter(addr_filter fn);
} // namespace ipv4