extract udp into its own static library

This commit is contained in:
Ian Gulliver
2026-05-01 13:52:28 -07:00
parent 71c07957f8
commit 30f3eae111
4 changed files with 14 additions and 1 deletions

12
udp/CMakeLists.txt Normal file
View File

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

56
udp/udp.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include "udp.h"
#include <array>
#include "eth.h"
#include "ipv4.h"
#include "parse_buffer.h"
#include "debug_log.h"
namespace udp {
struct port_entry {
uint16_t port_be;
port_handler fn;
};
static std::array<port_entry, 8> port_handlers;
static size_t port_handler_count = 0;
void register_port(uint16_t port_be, port_handler fn) {
if (port_handler_count >= port_handlers.size()) {
dlogf("udp::register_port overflow: port=%u dropped", __builtin_bswap16(port_be));
return;
}
port_handlers[port_handler_count++] = {port_be, fn};
}
void handle(std::span<const uint8_t> frame, span_writer& tx) {
parse_buffer pb(frame);
auto* eth_hdr = pb.consume<eth::header>();
auto* ip = pb.consume<ipv4::header>();
if (!ip) return;
if (!ipv4::addressed_to_us(ip->dst)) return;
size_t options_len = ip->header_len() - sizeof(ipv4::header);
if (options_len > 0 && !pb.skip(options_len)) return;
auto* uhdr = pb.consume<header>();
if (!uhdr) return;
size_t udp_len = __builtin_bswap16(uhdr->length);
if (udp_len < sizeof(header)) return;
size_t payload_len = udp_len - sizeof(header);
if (pb.remaining_size() < payload_len) return;
for (size_t i = 0; i < port_handler_count; i++) {
if (port_handlers[i].port_be == uhdr->dst_port) {
address from{eth_hdr->src, ip->src, uhdr->src_port};
port_handlers[i].fn(pb.remaining().subspan(0, payload_len), from);
break;
}
}
}
void init() {
ipv4::register_protocol(17, handle);
}
} // namespace udp

45
udp/udp.h Normal file
View File

@@ -0,0 +1,45 @@
#pragma once
#include <cstdint>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "span_writer.h"
namespace udp {
struct __attribute__((packed)) header {
uint16_t src_port;
uint16_t dst_port;
uint16_t length;
uint16_t checksum;
};
static_assert(sizeof(header) == 8);
struct address {
// mac is carried here until we grow an ARP cache; once we do, a
// destination mac comes from resolving ip and this field goes away.
eth::mac_addr mac;
ipv4::ip4_addr ip;
uint16_t port;
};
template <typename Buf>
void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_mac,
ipv4::ip4_addr src_ip, ipv4::ip4_addr dst_ip,
uint16_t src_port, uint16_t dst_port,
size_t payload_len, uint8_t ttl = 64) {
auto* u = buf.template prepend<header>();
u->src_port = src_port;
u->dst_port = dst_port;
u->length = __builtin_bswap16(sizeof(header) + payload_len);
u->checksum = 0;
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);
void register_port(uint16_t port_be, port_handler fn);
} // namespace udp