Compare commits

...

12 Commits

Author SHA1 Message Date
Ian Gulliver
30f3eae111 extract udp into its own static library 2026-05-01 13:52:28 -07:00
Ian Gulliver
71c07957f8 extract igmp into its own static library 2026-05-01 13:47:22 -07:00
Ian Gulliver
a56e034bfb extract icmp into its own static library; replace all __attribute__((constructor)) self-registrations with explicit init() calls in dispatch_init 2026-05-01 13:44:53 -07:00
Ian Gulliver
2d7ff98b82 extract arp into its own static library; add empty arp::init() called from dispatch_init to anchor the .o so the self-registration constructor links in 2026-05-01 13:35:40 -07:00
Ian Gulliver
f64cdcaacf extract ipv4 into its own static library 2026-05-01 13:23:10 -07:00
Ian Gulliver
fceae27f10 merge net into eth lib; ipv4 owns its IP via ipv4::init/get_ip; drop state struct in favor of eth::get_mac/ipv4::get_ip 2026-05-01 11:03:16 -07:00
Ian Gulliver
cc1448d6a2 move callback_list, parse_buffer, prepend_buffer, static_vector, timer_queue into util; util links pico_stdlib for timer_queue 2026-05-01 10:50:15 -07:00
Ian Gulliver
bcddb14acf move ring_buffer.h from debug_log into util; debug_log links util PUBLIC 2026-05-01 10:48:16 -07:00
Ian Gulliver
b565a5de4c add header doc comments to non-obvious util data structures 2026-05-01 10:47:08 -07:00
Ian Gulliver
1e97058b9b extract msgpack into its own static library; move non-templated bodies to msgpack.cpp; introduce util INTERFACE lib for span_writer 2026-05-01 10:43:29 -07:00
Ian Gulliver
7000c2e825 extract debug_log into its own static library; expose log_view iterator instead of raw ring_buffer 2026-05-01 10:33:39 -07:00
Ian Gulliver
b82c038091 net::/ipv4:: namespace, mac/addr filter framework with default filters, dlog on registry overflow 2026-05-01 10:15:54 -07:00
42 changed files with 1056 additions and 749 deletions

View File

@@ -1,18 +1,21 @@
cmake_minimum_required(VERSION 3.13)
add_subdirectory(util)
add_subdirectory(w6300)
add_subdirectory(debug_log)
add_subdirectory(msgpack)
add_subdirectory(eth)
add_subdirectory(ipv4)
add_subdirectory(arp)
add_subdirectory(icmp)
add_subdirectory(igmp)
add_subdirectory(udp)
add_library(limen STATIC
src/arp.cpp
src/dispatch.cpp
src/flash.cpp
src/handlers.cpp
src/icmp.cpp
src/igmp.cpp
src/ipv4.cpp
src/net.cpp
src/test_handlers.cpp
src/udp.cpp
)
target_include_directories(limen PUBLIC
@@ -22,10 +25,18 @@ target_include_directories(limen PUBLIC
target_compile_options(limen PRIVATE -Wall -Wextra -Wno-unused-parameter)
target_link_libraries(limen PUBLIC
util
w6300
debug_log
msgpack
eth
ipv4
arp
icmp
igmp
udp
pico_stdlib
pico_sha256
pico_unique_id
)
set(LIMEN_PARTITION_TABLE ${CMAKE_CURRENT_SOURCE_DIR}/partition_table.json CACHE INTERNAL "")

11
arp/CMakeLists.txt Normal file
View File

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

View File

@@ -1,5 +1,6 @@
#include "arp.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
@@ -11,7 +12,8 @@ static constexpr uint16_t ARP_OP_REQUEST = __builtin_bswap16(1);
static constexpr uint16_t ARP_OP_REPLY = __builtin_bswap16(2);
void handle(std::span<const uint8_t> frame, span_writer& tx) {
const auto& ns = net_get_state();
const auto& mac = eth::get_mac();
const auto& ip = ipv4::get_ip();
parse_buffer pb(frame);
pb.consume<eth::header>();
auto* arp_hdr = pb.consume<header>();
@@ -21,7 +23,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
if (arp_hdr->ptype != ARP_PTYPE_IPV4) return;
if (arp_hdr->hlen != 6 || arp_hdr->plen != 4) return;
if (arp_hdr->oper != ARP_OP_REQUEST) return;
if (arp_hdr->tpa != ns.ip) return;
if (arp_hdr->tpa != ip) return;
prepend_buffer<4096> buf;
auto* reply = buf.template prepend<header>();
@@ -30,18 +32,17 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
reply->hlen = 6;
reply->plen = 4;
reply->oper = ARP_OP_REPLY;
reply->sha = ns.mac;
reply->spa = ns.ip;
reply->sha = mac;
reply->spa = ip;
reply->tha = arp_hdr->sha;
reply->tpa = arp_hdr->spa;
eth::prepend(buf, arp_hdr->sha, ns.mac, eth::ETH_ARP);
eth::prepend(buf, arp_hdr->sha, mac, eth::ETH_ARP);
net_send_raw(buf.span());
eth::send_raw(buf.span());
}
__attribute__((constructor))
static void register_ethertype() {
net_register_ethertype(eth::ETH_ARP, handle);
void init() {
eth::register_ethertype(eth::ETH_ARP, handle);
}
} // namespace arp

View File

@@ -19,6 +19,7 @@ struct __attribute__((packed)) header {
};
static_assert(sizeof(header) == 28);
void init();
void handle(std::span<const uint8_t> frame, span_writer& tx);
} // namespace arp

7
debug_log/CMakeLists.txt Normal file
View File

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

28
debug_log/debug_log.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include "debug_log.h"
#include <cstdarg>
#include <cstdio>
#include "ring_buffer.h"
namespace {
constexpr uint16_t LOG_CAP = 32;
ring_buffer<log_entry, LOG_CAP> buf;
} // namespace
void dlog(std::string_view msg) {
buf.push_overwrite(log_entry{static_cast<uint32_t>(time_us_32()), std::string(msg)});
}
void dlogf(const char* fmt, ...) {
char b[128];
va_list args;
va_start(args, fmt);
vsnprintf(b, sizeof(b), fmt, args);
va_end(args);
dlog(b);
}
log_view log_entries() {
return log_view{buf.data.data(), buf.head, LOG_CAP, buf.used()};
}

52
debug_log/debug_log.h Normal file
View File

@@ -0,0 +1,52 @@
#pragma once
#include <cstdint>
#include <string>
#include <string_view>
#include "pico/time.h"
struct log_entry {
uint32_t timestamp_us;
std::string message;
};
class log_iterator {
const log_entry* data;
uint16_t base;
uint16_t cap;
uint16_t pos;
public:
log_iterator(const log_entry* d, uint16_t b, uint16_t c, uint16_t p)
: data(d), base(b), cap(c), pos(p) {}
const log_entry& operator*() const { return data[(base + pos) % cap]; }
log_iterator& operator++() { ++pos; return *this; }
bool operator!=(const log_iterator& o) const { return pos != o.pos; }
};
class log_view {
const log_entry* data;
uint16_t base;
uint16_t cap;
uint16_t count;
public:
log_view(const log_entry* d, uint16_t b, uint16_t c, uint16_t n)
: data(d), base(b), cap(c), count(n) {}
log_iterator begin() const { return {data, base, cap, 0}; }
log_iterator end() const { return {data, base, cap, count}; }
};
log_view log_entries();
void dlog(std::string_view msg);
__attribute__((format(printf, 1, 2)))
void dlogf(const char* fmt, ...);
template <typename F>
inline void dlog_if_slow(std::string_view label, uint32_t threshold_us, F&& fn) {
uint32_t t0 = time_us_32();
fn();
uint32_t elapsed = time_us_32() - t0;
if (elapsed > threshold_us) {
dlogf("%.*s %luus", static_cast<int>(label.size()), label.data(), static_cast<unsigned long>(elapsed));
}
}

12
eth/CMakeLists.txt Normal file
View File

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

151
eth/eth.cpp Normal file
View File

@@ -0,0 +1,151 @@
#include "eth.h"
#include <array>
#include "pico/unique_id.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
#include "w6300.h"
#include "debug_log.h"
namespace eth {
namespace {
mac_addr g_mac;
w6300::socket_id raw_socket{0};
frame_cb_list frame_callbacks;
struct ethertype_entry {
uint16_t ethertype_be;
ethertype_handler fn;
};
std::array<ethertype_entry, 8> eth_handlers;
size_t eth_handler_count = 0;
std::array<mac_filter, 4> mac_filters;
size_t mac_filter_count = 0;
bool default_mac_filter(const mac_addr& dst) {
return dst == g_mac || dst == MAC_BROADCAST;
}
bool mac_match(const mac_addr& dst) {
for (size_t i = 0; i < mac_filter_count; i++) {
if (mac_filters[i](dst)) {
return true;
}
}
return false;
}
void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
if (frame.size() < sizeof(header)) {
return;
}
auto& eth_hdr = *reinterpret_cast<const header*>(frame.data());
if (!mac_match(eth_hdr.dst)) {
return;
}
frame_callbacks.for_each([&](frame_cb_list::node* n) {
if (n->value(frame)) {
frame_callbacks.remove(n);
}
});
for (size_t i = 0; i < eth_handler_count; i++) {
if (eth_handlers[i].ethertype_be == eth_hdr.ethertype) {
eth_handlers[i].fn(frame, tx);
break;
}
}
}
} // namespace
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
if (eth_handler_count >= eth_handlers.size()) {
dlogf("eth::register_ethertype overflow: ethertype=0x%04x dropped", __builtin_bswap16(ethertype_be));
return;
}
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
}
void register_mac_filter(mac_filter fn) {
if (mac_filter_count >= mac_filters.size()) {
dlog("eth::register_mac_filter overflow: filter dropped");
return;
}
mac_filters[mac_filter_count++] = fn;
}
void send_raw(std::span<const uint8_t> data) {
dlog_if_slow("eth::send_raw", 1000, [&]{
auto result = w6300::send(raw_socket, data);
if (!result) {
dlogf("w6300 send failed: %zu bytes, err %d",
data.size(), static_cast<int>(result.error()));
}
});
}
bool init() {
w6300::init_spi();
w6300::reset();
w6300::init();
if (!w6300::check()) {
return false;
}
pico_unique_board_id_t uid;
pico_get_unique_board_id(&uid);
g_mac[0] = (uid.id[0] & 0xFC) | 0x02;
g_mac[1] = uid.id[1];
g_mac[2] = uid.id[2];
g_mac[3] = uid.id[3];
g_mac[4] = uid.id[4];
g_mac[5] = uid.id[5];
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
w6300::set_interrupt_mask(w6300::ik_sock_0);
register_mac_filter(default_mac_filter);
return true;
}
const mac_addr& get_mac() {
return g_mac;
}
frame_cb_handle add_frame_callback(frame_callback cb) {
auto h = frame_callbacks.insert(cb);
if (!h) {
dlog("eth::add_frame_callback overflow: callback dropped");
}
return h;
}
void remove_frame_callback(frame_cb_handle h) {
frame_callbacks.remove(h);
}
void poll(std::span<uint8_t> tx) {
if (!w6300::irq_pending) {
return;
}
w6300::irq_pending = false;
w6300::clear_interrupt(w6300::ik_int_all);
static uint8_t rx_buf[1518];
for (int i = 0; i < 16 && w6300::get_socket_recv_buf(raw_socket) > 0; i++) {
auto result = w6300::recv(raw_socket, std::span{rx_buf});
if (!result) {
break;
}
span_writer tx_writer(tx);
process_frame({rx_buf, *result}, tx_writer);
}
w6300::rearm_gpio_irq();
}
} // namespace eth

47
eth/eth.h Normal file
View File

@@ -0,0 +1,47 @@
#pragma once
#include <array>
#include <cstdint>
#include <span>
#include "callback_list.h"
#include "span_writer.h"
namespace eth {
using mac_addr = std::array<uint8_t, 6>;
static constexpr mac_addr MAC_BROADCAST = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
static constexpr uint16_t ETH_ARP = __builtin_bswap16(0x0806);
static constexpr uint16_t ETH_IPV4 = __builtin_bswap16(0x0800);
struct __attribute__((packed)) header {
mac_addr dst;
mac_addr src;
uint16_t ethertype;
};
static_assert(sizeof(header) == 14);
template <typename Buf>
void prepend(Buf& buf, const mac_addr& dst, const mac_addr& src, uint16_t ethertype) {
auto* h = buf.template prepend<header>();
h->dst = dst;
h->src = src;
h->ethertype = ethertype;
}
using frame_callback = bool (*)(std::span<const uint8_t> frame);
using frame_cb_list = callback_list<frame_callback, 16>;
using frame_cb_handle = frame_cb_list::node*;
using ethertype_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
using mac_filter = bool (*)(const mac_addr& dst);
bool init();
const mac_addr& get_mac();
frame_cb_handle add_frame_callback(frame_callback cb);
void remove_frame_callback(frame_cb_handle h);
void poll(std::span<uint8_t> tx);
void send_raw(std::span<const uint8_t> data);
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn);
void register_mac_filter(mac_filter fn);
} // namespace eth

11
icmp/CMakeLists.txt Normal file
View File

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

View File

@@ -1,7 +1,7 @@
#include "icmp.h"
#include <cstring>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
@@ -24,7 +24,6 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
if (!icmp_pkt) return;
if (icmp_pkt->type != 8) return;
const auto& ns = net_get_state();
prepend_buffer<4096> buf;
memcpy(buf.append(icmp_len), pb.remaining().data() - sizeof(echo), icmp_len);
@@ -33,12 +32,11 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
reply->checksum = 0;
reply->checksum = ipv4::checksum(reply, icmp_len);
ipv4::prepend(buf, eth_hdr->src, ns.mac, ns.ip, ip->src, 1, icmp_len);
net_send_raw(buf.span());
ipv4::prepend(buf, eth_hdr->src, eth::get_mac(), ipv4::get_ip(), ip->src, 1, icmp_len);
eth::send_raw(buf.span());
}
__attribute__((constructor))
static void register_protocol() {
void init() {
ipv4::register_protocol(1, handle);
}

View File

@@ -16,6 +16,7 @@ struct __attribute__((packed)) echo {
};
static_assert(sizeof(echo) == 8);
void init();
void handle(std::span<const uint8_t> frame, span_writer& tx);
template <typename Buf>

11
igmp/CMakeLists.txt Normal file
View File

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

View File

@@ -1,7 +1,7 @@
#include "igmp.h"
#include <vector>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
@@ -35,10 +35,9 @@ bool is_member_mac(const eth::mac_addr& mac) {
}
static void send_report(const ipv4::ip4_addr& group) {
const auto& ns = net_get_state();
prepend_buffer<4096> buf;
prepend_report(buf, ns.mac, ns.ip, group);
net_send_raw(buf.span());
prepend_report(buf, eth::get_mac(), ipv4::get_ip(), group);
eth::send_raw(buf.span());
}
void join(const ipv4::ip4_addr& group) {
@@ -79,9 +78,10 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
}
}
__attribute__((constructor))
static void register_protocol() {
void init() {
ipv4::register_protocol(2, handle);
eth::register_mac_filter(is_member_mac);
ipv4::register_addr_filter(is_member);
}
bool parse_report(std::span<const uint8_t> frame, ipv4::ip4_addr& group) {

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

@@ -1,39 +0,0 @@
#pragma once
#include <cstdarg>
#include <cstdio>
#include <string>
#include <string_view>
#include <vector>
#include "pico/time.h"
#include "ring_buffer.h"
struct log_entry {
uint32_t timestamp_us;
std::string message;
};
inline ring_buffer<log_entry, 32> g_debug_log;
inline void dlog(std::string_view msg) {
g_debug_log.push_overwrite(log_entry{static_cast<uint32_t>(time_us_32()), std::string(msg)});
}
__attribute__((format(printf, 1, 2)))
inline void dlogf(const char* fmt, ...) {
char buf[128];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
dlog(buf);
}
template <typename F>
inline void dlog_if_slow(std::string_view label, uint32_t threshold_us, F&& fn) {
uint32_t t0 = time_us_32();
fn();
uint32_t elapsed = time_us_32() - t0;
if (elapsed > threshold_us)
dlogf("%.*s %luus", static_cast<int>(label.size()), label.data(), static_cast<unsigned long>(elapsed));
}

View File

@@ -5,7 +5,8 @@
#include <span>
#include "wire.h"
#include "timer_queue.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "prepend_buffer.h"
#include "udp.h"
@@ -17,15 +18,16 @@ struct responder {
template <typename T>
void respond(const T& msg) const {
const auto& ns = net_get_state();
prepend_buffer<4096> buf;
span_writer out(buf.payload_ptr(), 2048);
auto r = encode_response_into(out, message_id, msg);
if (!r) return;
if (!r) {
return;
}
buf.append(*r);
udp::prepend(buf, reply_to.mac, ns.mac, ns.ip, reply_to.ip,
udp::prepend(buf, reply_to.mac, eth::get_mac(), ipv4::get_ip(), reply_to.ip,
dispatch_listen_port_be(), reply_to.port, *r);
net_send_raw(buf.span());
eth::send_raw(buf.span());
}
};

View File

@@ -1,28 +0,0 @@
#pragma once
#include <array>
#include <cstdint>
namespace eth {
using mac_addr = std::array<uint8_t, 6>;
static constexpr mac_addr MAC_BROADCAST = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
static constexpr uint16_t ETH_ARP = __builtin_bswap16(0x0806);
static constexpr uint16_t ETH_IPV4 = __builtin_bswap16(0x0800);
struct __attribute__((packed)) header {
mac_addr dst;
mac_addr src;
uint16_t ethertype;
};
static_assert(sizeof(header) == 14);
template <typename Buf>
void prepend(Buf& buf, const mac_addr& dst, const mac_addr& src, uint16_t ethertype) {
auto* h = buf.template prepend<header>();
h->dst = dst;
h->src = src;
h->ethertype = ethertype;
}
} // namespace eth

View File

@@ -1,27 +0,0 @@
#pragma once
#include <cstdint>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "span_writer.h"
#include "callback_list.h"
struct net_state {
eth::mac_addr mac;
ipv4::ip4_addr ip;
};
using net_frame_callback = bool (*)(std::span<const uint8_t> frame);
using frame_cb_list = callback_list<net_frame_callback, 16>;
using frame_cb_handle = frame_cb_list::node*;
using ethertype_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
bool net_init();
const net_state& net_get_state();
frame_cb_handle net_add_frame_callback(net_frame_callback cb);
void net_remove_frame_callback(frame_cb_handle h);
void net_poll(std::span<uint8_t> tx);
void net_send_raw(std::span<const uint8_t> data);
void net_register_ethertype(uint16_t ethertype_be, ethertype_handler fn);

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
)

106
ipv4/ipv4.cpp Normal file
View File

@@ -0,0 +1,106 @@
#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]};
eth::register_ethertype(eth::ETH_IPV4, handle);
register_addr_filter(default_addr_filter);
}
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;
}
}
}
} // namespace ipv4

View File

@@ -59,9 +59,15 @@ void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_ma
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

7
msgpack/CMakeLists.txt Normal file
View File

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

419
msgpack/msgpack.cpp Normal file
View File

@@ -0,0 +1,419 @@
#include "msgpack.h"
namespace msgpack {
result<body_info> get_body_info(const uint8_t *p, int size) {
if (size < 1) return std::unexpected(error_code::empty);
uint8_t b = p[0];
using namespace format;
if (is_positive_fixint(b)) return body_info{1, 0};
if (is_negative_fixint(b)) return body_info{1, 0};
if (is_fixmap(b)) return body_info{1, 0};
if (is_fixarray(b)) return body_info{1, 0};
if (is_fixstr(b)) return body_info{1, static_cast<uint32_t>(b & 0x1F)};
switch (b) {
case NIL: case FALSE: case TRUE:
return body_info{1, 0};
case NEVER_USED:
return std::unexpected(error_code::invalid);
case BIN8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1, *n}; }
case BIN16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2, *n}; }
case BIN32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4, *n}; }
case EXT8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1+1, *n}; }
case EXT16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2+1, *n}; }
case EXT32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4+1, *n}; }
case FLOAT32: return body_info{1, 4};
case FLOAT64: return body_info{1, 8};
case UINT8: return body_info{1, 1};
case UINT16: return body_info{1, 2};
case UINT32: return body_info{1, 4};
case UINT64: return body_info{1, 8};
case INT8: return body_info{1, 1};
case INT16: return body_info{1, 2};
case INT32: return body_info{1, 4};
case INT64: return body_info{1, 8};
case FIXEXT1: return body_info{1+1, 1};
case FIXEXT2: return body_info{1+1, 2};
case FIXEXT4: return body_info{1+1, 4};
case FIXEXT8: return body_info{1+1, 8};
case FIXEXT16: return body_info{1+1, 16};
case STR8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1, *n}; }
case STR16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2, *n}; }
case STR32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4, *n}; }
case ARRAY16: case ARRAY32:
case MAP16: case MAP32:
return body_info{1 + (b == ARRAY16 || b == MAP16 ? 2 : 4), 0};
default:
return std::unexpected(error_code::invalid);
}
}
packer::pack_result packer::pack_nil() {
m_buf.push_back(format::NIL);
return *this;
}
packer::pack_result packer::pack_bool(bool v) {
m_buf.push_back(v ? format::TRUE : format::FALSE);
return *this;
}
packer::pack_result packer::pack_uint32_fixed(uint32_t n) {
m_buf.push_back(format::UINT32);
push_big_endian(n);
return *this;
}
packer::pack_result packer::pack_float(float n) {
m_buf.push_back(format::FLOAT32);
push_big_endian(n);
return *this;
}
packer::pack_result packer::pack_double(double n) {
m_buf.push_back(format::FLOAT64);
push_big_endian(n);
return *this;
}
packer::pack_result packer::pack_str(const char *s) {
return pack_str(std::string_view(s));
}
packer::pack_result packer::pack_array(size_t n) {
if (n <= 15) {
m_buf.push_back(format::FIXARRAY_MIN | static_cast<uint8_t>(n));
} else if (n <= std::numeric_limits<uint16_t>::max()) {
m_buf.push_back(format::ARRAY16);
push_big_endian(static_cast<uint16_t>(n));
} else if (n <= std::numeric_limits<uint32_t>::max()) {
m_buf.push_back(format::ARRAY32);
push_big_endian(static_cast<uint32_t>(n));
} else {
return std::unexpected(error_code::overflow);
}
return *this;
}
packer::pack_result packer::pack_map(size_t n) {
if (n <= 15) {
m_buf.push_back(format::FIXMAP_MIN | static_cast<uint8_t>(n));
} else if (n <= std::numeric_limits<uint16_t>::max()) {
m_buf.push_back(format::MAP16);
push_big_endian(static_cast<uint16_t>(n));
} else if (n <= std::numeric_limits<uint32_t>::max()) {
m_buf.push_back(format::MAP32);
push_big_endian(static_cast<uint32_t>(n));
} else {
return std::unexpected(error_code::overflow);
}
return *this;
}
packer::pack_result packer::pack_ext16_header(char type, uint16_t len) {
m_buf.push_back(format::EXT16);
push_big_endian(len);
m_buf.push_back(static_cast<uint8_t>(type));
return *this;
}
packer::pack_result packer::pack_bin16_header(uint16_t len) {
m_buf.push_back(format::BIN16);
push_big_endian(len);
return *this;
}
packer::pack_result packer::pack(bool v) { return pack_bool(v); }
packer::pack_result packer::pack(float v) { return pack_float(v); }
packer::pack_result packer::pack(double v) { return pack_double(v); }
packer::pack_result packer::pack(const char *v) { return pack_str(v); }
packer::pack_result packer::pack(std::string_view v) { return pack_str(v); }
packer::pack_result packer::pack(const std::string &v) { return pack_str(v); }
packer::pack_result packer::pack(const std::vector<uint8_t> &v) { return pack_bin(v); }
result<parser> parser::next() const {
auto hdr = header_byte();
if (!hdr) return std::unexpected(hdr.error());
if (is_array()) {
auto info = get_body_info(m_p, m_size);
if (!info) return std::unexpected(info.error());
auto cnt = count();
if (!cnt) return std::unexpected(cnt.error());
auto cur = advance(info->header);
if (!cur) return std::unexpected(cur.error());
for (uint32_t i = 0; i < *cnt; ++i) {
auto n = cur->next();
if (!n) return std::unexpected(n.error());
cur = *n;
}
return *cur;
} else if (is_map()) {
auto info = get_body_info(m_p, m_size);
if (!info) return std::unexpected(info.error());
auto cnt = count();
if (!cnt) return std::unexpected(cnt.error());
auto cur = advance(info->header);
if (!cur) return std::unexpected(cur.error());
for (uint32_t i = 0; i < *cnt; ++i) {
auto k = cur->next();
if (!k) return std::unexpected(k.error());
cur = *k;
auto v = cur->next();
if (!v) return std::unexpected(v.error());
cur = *v;
}
return *cur;
} else {
auto info = get_body_info(m_p, m_size);
if (!info) return std::unexpected(info.error());
auto total = info->header + static_cast<int>(info->body);
return advance(total);
}
}
bool parser::is_nil() const {
auto h = header_byte();
return h && *h == format::NIL;
}
bool parser::is_bool() const {
auto h = header_byte();
return h && (*h == format::TRUE || *h == format::FALSE);
}
bool parser::is_number() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
if (format::is_positive_fixint(b)) return true;
if (format::is_negative_fixint(b)) return true;
return b >= format::FLOAT32 && b <= format::INT64;
}
bool parser::is_string() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
if (format::is_fixstr(b)) return true;
return b == format::STR8 || b == format::STR16 || b == format::STR32;
}
bool parser::is_binary() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
return b == format::BIN8 || b == format::BIN16 || b == format::BIN32;
}
bool parser::is_ext() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
return (b >= format::FIXEXT1 && b <= format::FIXEXT16) ||
b == format::EXT8 || b == format::EXT16 || b == format::EXT32;
}
bool parser::is_array() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
if (format::is_fixarray(b)) return true;
return b == format::ARRAY16 || b == format::ARRAY32;
}
bool parser::is_map() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
if (format::is_fixmap(b)) return true;
return b == format::MAP16 || b == format::MAP32;
}
result<bool> parser::get_bool() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
if (*h == format::TRUE) return true;
if (*h == format::FALSE) return false;
return std::unexpected(error_code::type_error);
}
result<std::string_view> parser::get_string() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
uint8_t b = *h;
size_t offset, len;
if (format::is_fixstr(b)) {
len = b & 0x1F;
offset = 1;
} else if (b == format::STR8) {
auto n = body_number<uint8_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 1;
} else if (b == format::STR16) {
auto n = body_number<uint16_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 2;
} else if (b == format::STR32) {
auto n = body_number<uint32_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 4;
} else {
return std::unexpected(error_code::type_error);
}
if (static_cast<int>(offset + len) > m_size) {
return std::unexpected(error_code::lack);
}
return std::string_view(reinterpret_cast<const char *>(m_p + offset), len);
}
result<std::string_view> parser::get_binary_view() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
uint8_t b = *h;
size_t offset, len;
if (b == format::BIN8) {
auto n = body_number<uint8_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 1;
} else if (b == format::BIN16) {
auto n = body_number<uint16_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 2;
} else if (b == format::BIN32) {
auto n = body_number<uint32_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 4;
} else {
return std::unexpected(error_code::type_error);
}
if (static_cast<int>(offset + len) > m_size) {
return std::unexpected(error_code::lack);
}
return std::string_view(reinterpret_cast<const char *>(m_p + offset), len);
}
result<std::tuple<int8_t, std::string_view>> parser::get_ext() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
uint8_t b = *h;
int8_t ext_type;
size_t data_offset, data_len;
switch (b) {
case format::FIXEXT1: ext_type = m_p[1]; data_offset = 2; data_len = 1; break;
case format::FIXEXT2: ext_type = m_p[1]; data_offset = 2; data_len = 2; break;
case format::FIXEXT4: ext_type = m_p[1]; data_offset = 2; data_len = 4; break;
case format::FIXEXT8: ext_type = m_p[1]; data_offset = 2; data_len = 8; break;
case format::FIXEXT16: ext_type = m_p[1]; data_offset = 2; data_len = 16; break;
case format::EXT8: {
auto n = body_number<uint8_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
ext_type = m_p[2]; data_offset = 3; data_len = *n;
break;
}
case format::EXT16: {
auto n = body_number<uint16_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
ext_type = m_p[3]; data_offset = 4; data_len = *n;
break;
}
case format::EXT32: {
auto n = body_number<uint32_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
ext_type = m_p[5]; data_offset = 6; data_len = *n;
break;
}
default:
return std::unexpected(error_code::type_error);
}
if (static_cast<int>(data_offset + data_len) > m_size) {
return std::unexpected(error_code::lack);
}
return std::tuple{ext_type,
std::string_view(reinterpret_cast<const char *>(m_p + data_offset), data_len)};
}
result<uint32_t> parser::count() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
uint8_t b = *h;
if (format::is_fixarray(b)) return static_cast<uint32_t>(b & 0x0F);
if (format::is_fixmap(b)) return static_cast<uint32_t>(b & 0x0F);
switch (b) {
case format::ARRAY16: { auto n = body_number<uint16_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<uint32_t>(*n); }
case format::ARRAY32: { auto n = body_number<uint32_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return *n; }
case format::MAP16: { auto n = body_number<uint16_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<uint32_t>(*n); }
case format::MAP32: { auto n = body_number<uint32_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return *n; }
default:
return std::unexpected(error_code::type_error);
}
}
result<parser> parser::first_item() const {
if (!is_array() && !is_map()) return std::unexpected(error_code::type_error);
auto info = get_body_info(m_p, m_size);
if (!info) return std::unexpected(info.error());
return advance(info->header);
}
parser parser::operator[](int index) const {
auto cur = first_item();
if (!cur) return {};
for (int i = 0; i < index; ++i) {
auto n = cur->next();
if (!n) return {};
cur = *n;
}
return *cur;
}
result<parser> unpack(const parser &p, bool &out) {
auto v = p.get_bool();
if (!v) return std::unexpected(v.error());
out = *v;
return p.next();
}
result<parser> unpack(const parser &p, std::string_view &out) {
auto v = p.get_string();
if (!v) return std::unexpected(v.error());
out = *v;
return p.next();
}
result<parser> unpack(const parser &p, std::string &out) {
auto v = p.get_string();
if (!v) return std::unexpected(v.error());
out = std::string(v->data(), v->size());
return p.next();
}
result<parser> unpack(const parser &p, std::vector<uint8_t> &out) {
auto v = p.get_binary_view();
if (!v) return std::unexpected(v.error());
out.assign(v->begin(), v->end());
return p.next();
}
result<parser> unpack(const parser &p, std::span<const uint8_t> &out) {
auto v = p.get_binary_view();
if (!v) return std::unexpected(v.error());
out = std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(v->data()), v->size());
return p.next();
}
} // namespace msgpack

View File

@@ -1,14 +1,16 @@
#pragma once
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <expected>
#include <iterator>
#include <limits>
#include <span>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "span_writer.h"
@@ -102,65 +104,11 @@ result<T> body_number(const uint8_t *p, int size) {
}
struct body_info {
int header; // bytes before the body (includes format byte + length fields + ext type byte)
uint32_t body; // body size in bytes (0 for containers, computed for variable-length)
int header;
uint32_t body;
};
inline result<body_info> get_body_info(const uint8_t *p, int size) {
if (size < 1) return std::unexpected(error_code::empty);
uint8_t b = p[0];
using namespace format;
if (is_positive_fixint(b)) return body_info{1, 0};
if (is_negative_fixint(b)) return body_info{1, 0};
if (is_fixmap(b)) return body_info{1, 0}; // container
if (is_fixarray(b)) return body_info{1, 0}; // container
if (is_fixstr(b)) return body_info{1, static_cast<uint32_t>(b & 0x1F)};
switch (b) {
case NIL: case FALSE: case TRUE:
return body_info{1, 0};
case NEVER_USED:
return std::unexpected(error_code::invalid);
case BIN8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1, *n}; }
case BIN16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2, *n}; }
case BIN32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4, *n}; }
case EXT8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1+1, *n}; }
case EXT16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2+1, *n}; }
case EXT32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4+1, *n}; }
case FLOAT32: return body_info{1, 4};
case FLOAT64: return body_info{1, 8};
case UINT8: return body_info{1, 1};
case UINT16: return body_info{1, 2};
case UINT32: return body_info{1, 4};
case UINT64: return body_info{1, 8};
case INT8: return body_info{1, 1};
case INT16: return body_info{1, 2};
case INT32: return body_info{1, 4};
case INT64: return body_info{1, 8};
case FIXEXT1: return body_info{1+1, 1};
case FIXEXT2: return body_info{1+1, 2};
case FIXEXT4: return body_info{1+1, 4};
case FIXEXT8: return body_info{1+1, 8};
case FIXEXT16: return body_info{1+1, 16};
case STR8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1, *n}; }
case STR16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2, *n}; }
case STR32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4, *n}; }
case ARRAY16: case ARRAY32:
case MAP16: case MAP32:
return body_info{1 + (b == ARRAY16 || b == MAP16 ? 2 : 4), 0}; // container
default:
return std::unexpected(error_code::invalid);
}
}
result<body_info> get_body_info(const uint8_t *p, int size);
class packer {
private:
@@ -185,15 +133,8 @@ public:
using pack_result = result<std::reference_wrapper<packer>>;
pack_result pack_nil() {
m_buf.push_back(format::NIL);
return *this;
}
pack_result pack_bool(bool v) {
m_buf.push_back(v ? format::TRUE : format::FALSE);
return *this;
}
pack_result pack_nil();
pack_result pack_bool(bool v);
template <typename T>
pack_result pack_integer(T n) {
@@ -201,7 +142,7 @@ public:
if (n >= 0 && n <= 0x7F) {
m_buf.push_back(static_cast<uint8_t>(n));
} else if (n >= -32 && n < 0) {
m_buf.push_back(static_cast<uint8_t>(n)); // negative fixint
m_buf.push_back(static_cast<uint8_t>(n));
} else if (n >= std::numeric_limits<int8_t>::min() && n <= std::numeric_limits<int8_t>::max()) {
m_buf.push_back(format::INT8);
m_buf.push_back(static_cast<uint8_t>(n));
@@ -235,23 +176,9 @@ public:
return *this;
}
pack_result pack_uint32_fixed(uint32_t n) {
m_buf.push_back(format::UINT32);
push_big_endian(n);
return *this;
}
pack_result pack_float(float n) {
m_buf.push_back(format::FLOAT32);
push_big_endian(n);
return *this;
}
pack_result pack_double(double n) {
m_buf.push_back(format::FLOAT64);
push_big_endian(n);
return *this;
}
pack_result pack_uint32_fixed(uint32_t n);
pack_result pack_float(float n);
pack_result pack_double(double n);
template <class Range>
pack_result pack_str(const Range &r) {
@@ -274,9 +201,7 @@ public:
return *this;
}
pack_result pack_str(const char *s) {
return pack_str(std::string_view(s));
}
pack_result pack_str(const char *s);
template <class Range>
pack_result pack_bin(const Range &r) {
@@ -297,48 +222,10 @@ public:
return *this;
}
pack_result pack_array(size_t n) {
if (n <= 15) {
m_buf.push_back(format::FIXARRAY_MIN | static_cast<uint8_t>(n));
} else if (n <= std::numeric_limits<uint16_t>::max()) {
m_buf.push_back(format::ARRAY16);
push_big_endian(static_cast<uint16_t>(n));
} else if (n <= std::numeric_limits<uint32_t>::max()) {
m_buf.push_back(format::ARRAY32);
push_big_endian(static_cast<uint32_t>(n));
} else {
return std::unexpected(error_code::overflow);
}
return *this;
}
pack_result pack_map(size_t n) {
if (n <= 15) {
m_buf.push_back(format::FIXMAP_MIN | static_cast<uint8_t>(n));
} else if (n <= std::numeric_limits<uint16_t>::max()) {
m_buf.push_back(format::MAP16);
push_big_endian(static_cast<uint16_t>(n));
} else if (n <= std::numeric_limits<uint32_t>::max()) {
m_buf.push_back(format::MAP32);
push_big_endian(static_cast<uint32_t>(n));
} else {
return std::unexpected(error_code::overflow);
}
return *this;
}
pack_result pack_ext16_header(char type, uint16_t len) {
m_buf.push_back(format::EXT16);
push_big_endian(len);
m_buf.push_back(static_cast<uint8_t>(type));
return *this;
}
pack_result pack_bin16_header(uint16_t len) {
m_buf.push_back(format::BIN16);
push_big_endian(len);
return *this;
}
pack_result pack_array(size_t n);
pack_result pack_map(size_t n);
pack_result pack_ext16_header(char type, uint16_t len);
pack_result pack_bin16_header(uint16_t len);
template <class Range>
pack_result pack_ext(char type, const Range &r) {
@@ -377,14 +264,13 @@ public:
requires std::is_enum_v<T>
pack_result pack(T v) { return pack_integer(static_cast<std::underlying_type_t<T>>(v)); }
pack_result pack(bool v) { return pack_bool(v); }
pack_result pack(float v) { return pack_float(v); }
pack_result pack(double v) { return pack_double(v); }
pack_result pack(const char *v) { return pack_str(v); }
pack_result pack(std::string_view v) { return pack_str(v); }
pack_result pack(const std::string &v) { return pack_str(v); }
pack_result pack(const std::vector<uint8_t> &v) { return pack_bin(v); }
pack_result pack(bool v);
pack_result pack(float v);
pack_result pack(double v);
pack_result pack(const char *v);
pack_result pack(std::string_view v);
pack_result pack(const std::string &v);
pack_result pack(const std::vector<uint8_t> &v);
template <typename T>
requires (!std::is_same_v<T, uint8_t>)
@@ -464,210 +350,21 @@ public:
return parser(m_p + n, m_size - n);
}
result<parser> next() const {
auto hdr = header_byte();
if (!hdr) return std::unexpected(hdr.error());
result<parser> next() const;
if (is_array()) {
auto info = get_body_info(m_p, m_size);
if (!info) return std::unexpected(info.error());
auto cnt = count();
if (!cnt) return std::unexpected(cnt.error());
auto cur = advance(info->header);
if (!cur) return std::unexpected(cur.error());
for (uint32_t i = 0; i < *cnt; ++i) {
auto n = cur->next();
if (!n) return std::unexpected(n.error());
cur = *n;
}
return *cur;
} else if (is_map()) {
auto info = get_body_info(m_p, m_size);
if (!info) return std::unexpected(info.error());
auto cnt = count();
if (!cnt) return std::unexpected(cnt.error());
auto cur = advance(info->header);
if (!cur) return std::unexpected(cur.error());
for (uint32_t i = 0; i < *cnt; ++i) {
auto k = cur->next();
if (!k) return std::unexpected(k.error());
cur = *k;
auto v = cur->next();
if (!v) return std::unexpected(v.error());
cur = *v;
}
return *cur;
} else {
auto info = get_body_info(m_p, m_size);
if (!info) return std::unexpected(info.error());
auto total = info->header + static_cast<int>(info->body);
return advance(total);
}
}
bool is_nil() const;
bool is_bool() const;
bool is_number() const;
bool is_string() const;
bool is_binary() const;
bool is_ext() const;
bool is_array() const;
bool is_map() const;
bool is_nil() const {
auto h = header_byte();
return h && *h == format::NIL;
}
bool is_bool() const {
auto h = header_byte();
return h && (*h == format::TRUE || *h == format::FALSE);
}
bool is_number() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
if (format::is_positive_fixint(b)) return true;
if (format::is_negative_fixint(b)) return true;
return b >= format::FLOAT32 && b <= format::INT64;
}
bool is_string() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
if (format::is_fixstr(b)) return true;
return b == format::STR8 || b == format::STR16 || b == format::STR32;
}
bool is_binary() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
return b == format::BIN8 || b == format::BIN16 || b == format::BIN32;
}
bool is_ext() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
return (b >= format::FIXEXT1 && b <= format::FIXEXT16) ||
b == format::EXT8 || b == format::EXT16 || b == format::EXT32;
}
bool is_array() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
if (format::is_fixarray(b)) return true;
return b == format::ARRAY16 || b == format::ARRAY32;
}
bool is_map() const {
auto h = header_byte();
if (!h) return false;
uint8_t b = *h;
if (format::is_fixmap(b)) return true;
return b == format::MAP16 || b == format::MAP32;
}
result<bool> get_bool() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
if (*h == format::TRUE) return true;
if (*h == format::FALSE) return false;
return std::unexpected(error_code::type_error);
}
result<std::string_view> get_string() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
uint8_t b = *h;
size_t offset, len;
if (format::is_fixstr(b)) {
len = b & 0x1F;
offset = 1;
} else if (b == format::STR8) {
auto n = body_number<uint8_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 1;
} else if (b == format::STR16) {
auto n = body_number<uint16_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 2;
} else if (b == format::STR32) {
auto n = body_number<uint32_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 4;
} else {
return std::unexpected(error_code::type_error);
}
if (static_cast<int>(offset + len) > m_size) {
return std::unexpected(error_code::lack);
}
return std::string_view(reinterpret_cast<const char *>(m_p + offset), len);
}
result<std::string_view> get_binary_view() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
uint8_t b = *h;
size_t offset, len;
if (b == format::BIN8) {
auto n = body_number<uint8_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 1;
} else if (b == format::BIN16) {
auto n = body_number<uint16_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 2;
} else if (b == format::BIN32) {
auto n = body_number<uint32_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
len = *n; offset = 1 + 4;
} else {
return std::unexpected(error_code::type_error);
}
if (static_cast<int>(offset + len) > m_size) {
return std::unexpected(error_code::lack);
}
return std::string_view(reinterpret_cast<const char *>(m_p + offset), len);
}
result<std::tuple<int8_t, std::string_view>> get_ext() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
uint8_t b = *h;
int8_t ext_type;
size_t data_offset, data_len;
switch (b) {
case format::FIXEXT1: ext_type = m_p[1]; data_offset = 2; data_len = 1; break;
case format::FIXEXT2: ext_type = m_p[1]; data_offset = 2; data_len = 2; break;
case format::FIXEXT4: ext_type = m_p[1]; data_offset = 2; data_len = 4; break;
case format::FIXEXT8: ext_type = m_p[1]; data_offset = 2; data_len = 8; break;
case format::FIXEXT16: ext_type = m_p[1]; data_offset = 2; data_len = 16; break;
case format::EXT8: {
auto n = body_number<uint8_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
ext_type = m_p[2]; data_offset = 3; data_len = *n;
break;
}
case format::EXT16: {
auto n = body_number<uint16_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
ext_type = m_p[3]; data_offset = 4; data_len = *n;
break;
}
case format::EXT32: {
auto n = body_number<uint32_t>(m_p, m_size);
if (!n) return std::unexpected(n.error());
ext_type = m_p[5]; data_offset = 6; data_len = *n;
break;
}
default:
return std::unexpected(error_code::type_error);
}
if (static_cast<int>(data_offset + data_len) > m_size) {
return std::unexpected(error_code::lack);
}
return std::tuple{ext_type,
std::string_view(reinterpret_cast<const char *>(m_p + data_offset), data_len)};
}
result<bool> get_bool() const;
result<std::string_view> get_string() const;
result<std::string_view> get_binary_view() const;
result<std::tuple<int8_t, std::string_view>> get_ext() const;
template <typename T>
result<T> get_number() const {
@@ -694,41 +391,9 @@ public:
}
}
result<uint32_t> count() const {
auto h = header_byte();
if (!h) return std::unexpected(h.error());
uint8_t b = *h;
if (format::is_fixarray(b)) return static_cast<uint32_t>(b & 0x0F);
if (format::is_fixmap(b)) return static_cast<uint32_t>(b & 0x0F);
switch (b) {
case format::ARRAY16: { auto n = body_number<uint16_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<uint32_t>(*n); }
case format::ARRAY32: { auto n = body_number<uint32_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return *n; }
case format::MAP16: { auto n = body_number<uint16_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<uint32_t>(*n); }
case format::MAP32: { auto n = body_number<uint32_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return *n; }
default:
return std::unexpected(error_code::type_error);
}
}
result<parser> first_item() const {
if (!is_array() && !is_map()) return std::unexpected(error_code::type_error);
auto info = get_body_info(m_p, m_size);
if (!info) return std::unexpected(info.error());
return advance(info->header);
}
parser operator[](int index) const {
auto cur = first_item();
if (!cur) return {};
for (int i = 0; i < index; ++i) {
auto n = cur->next();
if (!n) return {};
cur = *n;
}
return *cur;
}
result<uint32_t> count() const;
result<parser> first_item() const;
parser operator[](int index) const;
};
template <typename T>
@@ -750,26 +415,11 @@ result<parser> unpack(const parser &p, T &out) {
return p.next();
}
inline result<parser> unpack(const parser &p, bool &out) {
auto v = p.get_bool();
if (!v) return std::unexpected(v.error());
out = *v;
return p.next();
}
inline result<parser> unpack(const parser &p, std::string_view &out) {
auto v = p.get_string();
if (!v) return std::unexpected(v.error());
out = *v;
return p.next();
}
inline result<parser> unpack(const parser &p, std::string &out) {
auto v = p.get_string();
if (!v) return std::unexpected(v.error());
out = std::string(v->data(), v->size());
return p.next();
}
result<parser> unpack(const parser &p, bool &out);
result<parser> unpack(const parser &p, std::string_view &out);
result<parser> unpack(const parser &p, std::string &out);
result<parser> unpack(const parser &p, std::vector<uint8_t> &out);
result<parser> unpack(const parser &p, std::span<const uint8_t> &out);
template <size_t N>
result<parser> unpack(const parser &p, std::array<uint8_t, N> &out) {
@@ -780,20 +430,6 @@ result<parser> unpack(const parser &p, std::array<uint8_t, N> &out) {
return p.next();
}
inline result<parser> unpack(const parser &p, std::vector<uint8_t> &out) {
auto v = p.get_binary_view();
if (!v) return std::unexpected(v.error());
out.assign(v->begin(), v->end());
return p.next();
}
inline result<parser> unpack(const parser &p, std::span<const uint8_t> &out) {
auto v = p.get_binary_view();
if (!v) return std::unexpected(v.error());
out = std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(v->data()), v->size());
return p.next();
}
template <typename T>
requires (!std::is_same_v<T, uint8_t>)
result<parser> unpack(const parser &p, std::vector<T> &out) {

View File

@@ -3,7 +3,10 @@
#include "pico/stdlib.h"
#include "wire.h"
#include "timer_queue.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "arp.h"
#include "icmp.h"
#include "igmp.h"
#include "udp.h"
#include "debug_log.h"
@@ -33,8 +36,13 @@ static void on_udp_message(std::span<const uint8_t> payload, const udp::address&
void dispatch_init(uint16_t port_be) {
listen_port_be = port_be;
eth::init();
ipv4::init();
arp::init();
icmp::init();
igmp::init();
udp::init();
udp::register_port(port_be, on_udp_message);
net_init();
dispatch_schedule_ms(60000, igmp_reannounce);
dlog("dispatch_init complete");
}
@@ -59,7 +67,7 @@ bool dispatch_cancel_timer(timer_handle h) {
uint32_t save = save_and_disable_interrupts();
dlog_if_slow("timers", 1000, [&]{ timers.run(); });
dlog_if_slow("net_poll", 1000, [&]{ net_poll(std::span{tx_buf}); });
dlog_if_slow("eth::poll", 1000, [&]{ eth::poll(std::span{tx_buf}); });
__wfi();
restore_interrupts(save);

View File

@@ -5,7 +5,8 @@
#include "hardware/watchdog.h"
#include "flash.h"
#include "dispatch.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "debug_log.h"
static boot_reason detected_boot_reason;
@@ -34,9 +35,8 @@ std::optional<ResponseInfo> handle_info(const responder&, const RequestInfo&) {
pico_unique_board_id_t uid;
pico_get_unique_board_id(&uid);
std::copy(uid.id, uid.id + 8, resp.board_id.begin());
auto& ns = net_get_state();
resp.mac = ns.mac;
resp.ip = ns.ip;
resp.mac = eth::get_mac();
resp.ip = ipv4::get_ip();
resp.firmware_name = firmware_name;
resp.boot = detected_boot_reason;
resp.build_epoch = firmware_build_epoch;
@@ -45,8 +45,9 @@ std::optional<ResponseInfo> handle_info(const responder&, const RequestInfo&) {
std::optional<ResponseLog> handle_log(const responder&, const RequestLog&) {
ResponseLog resp;
for (auto& e : g_debug_log)
for (auto& e : log_entries()) {
resp.entries.push_back(LogEntry{e.timestamp_us, e.message});
}
return resp;
}

View File

@@ -1,63 +0,0 @@
#include "ipv4.h"
#include <array>
#include "igmp.h"
#include "net.h"
#include "parse_buffer.h"
namespace ipv4 {
static constexpr ip4_addr IP_BROADCAST_ALL = {255, 255, 255, 255};
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);
}
bool addressed_to_us(ip4_addr dst) {
const auto& ns = net_get_state();
return dst == ns.ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST || igmp::is_member(dst);
}
struct protocol_entry {
uint8_t protocol;
protocol_handler fn;
};
static std::array<protocol_entry, 8> protocol_handlers;
static size_t protocol_handler_count = 0;
void register_protocol(uint8_t protocol, protocol_handler fn) {
if (protocol_handler_count < protocol_handlers.size())
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_ethertype() {
net_register_ethertype(eth::ETH_IPV4, handle);
}
} // namespace ipv4

View File

@@ -1,112 +0,0 @@
#include "net.h"
#include <array>
#include "pico/unique_id.h"
#include "pico/time.h"
#include "eth.h"
#include "igmp.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
#include "w6300.h"
#include "debug_log.h"
static net_state state;
static w6300::socket_id raw_socket{0};
static frame_cb_list frame_callbacks;
struct ethertype_entry {
uint16_t ethertype_be;
ethertype_handler fn;
};
static std::array<ethertype_entry, 8> eth_handlers;
static size_t eth_handler_count = 0;
void net_register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
if (eth_handler_count < eth_handlers.size())
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
}
void net_send_raw(std::span<const uint8_t> data) {
dlog_if_slow("net_send_raw", 1000, [&]{
auto result = w6300::send(raw_socket, data);
if (!result)
dlogf("w6300 send failed: %zu bytes, err %d",
data.size(), static_cast<int>(result.error()));
});
}
static bool mac_match(const eth::mac_addr& dst) {
return dst == state.mac || dst == eth::MAC_BROADCAST || igmp::is_member_mac(dst);
}
static void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
if (frame.size() < sizeof(eth::header)) return;
auto& eth_hdr = *reinterpret_cast<const eth::header*>(frame.data());
if (!mac_match(eth_hdr.dst)) return;
frame_callbacks.for_each([&](frame_cb_list::node* n) {
if (n->value(frame))
frame_callbacks.remove(n);
});
for (size_t i = 0; i < eth_handler_count; i++) {
if (eth_handlers[i].ethertype_be == eth_hdr.ethertype) {
eth_handlers[i].fn(frame, tx);
break;
}
}
}
bool net_init() {
w6300::init_spi();
w6300::reset();
w6300::init();
if (!w6300::check()) return false;
pico_unique_board_id_t uid;
pico_get_unique_board_id(&uid);
state.mac[0] = (uid.id[0] & 0xFC) | 0x02;
state.mac[1] = uid.id[1];
state.mac[2] = uid.id[2];
state.mac[3] = uid.id[3];
state.mac[4] = uid.id[4];
state.mac[5] = uid.id[5];
state.ip[0] = 169;
state.ip[1] = 254;
state.ip[2] = state.mac[4];
state.ip[3] = state.mac[5];
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
w6300::set_interrupt_mask(w6300::ik_sock_0);
return true;
}
const net_state& net_get_state() {
return state;
}
frame_cb_handle net_add_frame_callback(net_frame_callback cb) {
auto h = frame_callbacks.insert(cb);
if (!h) dlog("frame callback alloc failed");
return h;
}
void net_remove_frame_callback(frame_cb_handle h) {
frame_callbacks.remove(h);
}
void net_poll(std::span<uint8_t> tx) {
if (!w6300::irq_pending) return;
w6300::irq_pending = false;
w6300::clear_interrupt(w6300::ik_int_all);
static uint8_t rx_buf[1518];
for (int i = 0; i < 16 && w6300::get_socket_recv_buf(raw_socket) > 0; i++) {
auto result = w6300::recv(raw_socket, std::span{rx_buf});
if (!result) break;
span_writer tx_writer(tx);
process_frame({rx_buf, *result}, tx_writer);
}
w6300::rearm_gpio_irq();
}

View File

@@ -4,7 +4,8 @@
#include "pico/stdlib.h"
#include "pico/time.h"
#include "handlers.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "icmp.h"
#include "igmp.h"
#include "udp.h"
@@ -55,7 +56,7 @@ struct test_state {
bool in_flight = false;
responder resp;
timer_handle timer = nullptr;
frame_cb_handle frame_cb = nullptr;
eth::frame_cb_handle frame_cb = nullptr;
discovery_data* active_discovery = nullptr;
ping_rate_data* active_rate = nullptr;
@@ -72,7 +73,7 @@ static test_state ts;
static void test_end(const ResponseTest& result) {
if (ts.timer) { dispatch_cancel_timer(ts.timer); ts.timer = nullptr; }
if (ts.frame_cb) { net_remove_frame_callback(ts.frame_cb); ts.frame_cb = nullptr; }
if (ts.frame_cb) { eth::remove_frame_callback(ts.frame_cb); ts.frame_cb = nullptr; }
ts.active_discovery = nullptr;
ts.active_rate = nullptr;
ts.resp.respond(result);
@@ -93,7 +94,7 @@ static bool discover_reply_cb(std::span<const uint8_t> frame) {
if (options_len > 0 && !pb.skip(options_len)) return false;
auto* uhdr = pb.consume<udp::header>();
if (!uhdr || uhdr->src_port != PICOMAP_PORT_BE) return false;
if (ip->src == net_get_state().ip) return false;
if (ip->src == ipv4::get_ip()) return false;
dispatch_cancel_timer(ts.timer);
ts.timer = nullptr;
ts.frame_cb = nullptr;
@@ -105,7 +106,7 @@ static bool discover_reply_cb(std::span<const uint8_t> frame) {
}
static void discover_timeout_cb() {
net_remove_frame_callback(ts.frame_cb);
eth::remove_frame_callback(ts.frame_cb);
ts.frame_cb = nullptr;
ts.timer = nullptr;
auto cont = ts.active_discovery ? ts.active_discovery->on_timeout : nullptr;
@@ -119,7 +120,6 @@ static void discover_peer(discovery_data& d,
d.on_timeout = timeout;
ts.active_discovery = &d;
const auto& ns = net_get_state();
eth::mac_addr mcast_mac = igmp::mac_for_ip(PICOMAP_DISCOVERY_GROUP);
prepend_buffer<4096> buf;
@@ -134,13 +134,13 @@ static void discover_peer(discovery_data& d,
}
buf.append(*encoded);
udp::prepend(buf, mcast_mac, ns.mac, ns.ip, PICOMAP_DISCOVERY_GROUP,
udp::prepend(buf, mcast_mac, eth::get_mac(), ipv4::get_ip(), PICOMAP_DISCOVERY_GROUP,
PICOMAP_PORT_BE, PICOMAP_PORT_BE, *encoded, 1);
ts.frame_cb = net_add_frame_callback(discover_reply_cb);
ts.frame_cb = eth::add_frame_callback(discover_reply_cb);
ts.timer = dispatch_schedule_ms(5000, discover_timeout_cb);
net_send_raw(buf.span());
eth::send_raw(buf.span());
}
static bool igmp_report_cb(std::span<const uint8_t> frame) {
@@ -158,14 +158,13 @@ static void igmp_timeout_cb() {
}
static void test_discovery_igmp() {
const auto& ns = net_get_state();
prepend_buffer<4096> buf;
igmp::prepend_query(buf, ns.mac, ns.ip, PICOMAP_DISCOVERY_GROUP);
igmp::prepend_query(buf, eth::get_mac(), ipv4::get_ip(), PICOMAP_DISCOVERY_GROUP);
ts.frame_cb = net_add_frame_callback(igmp_report_cb);
ts.frame_cb = eth::add_frame_callback(igmp_report_cb);
ts.timer = dispatch_schedule_ms(5000, igmp_timeout_cb);
net_send_raw(buf.span());
eth::send_raw(buf.span());
}
static void info_found(const peer_info& peer) {
@@ -184,7 +183,7 @@ static bool ping_reply_cb(std::span<const uint8_t> frame) {
ipv4::ip4_addr src_ip;
if (!icmp::parse_echo_reply(frame, src_ip, PING_ECHO_ID)) return false;
ts.frame_cb = nullptr;
if (src_ip == net_get_state().ip)
if (src_ip == ipv4::get_ip())
test_end({false, {"got reply from self: " + ipv4::to_string(src_ip)}});
else
test_end({true, {"reply from " + ipv4::to_string(src_ip)}});
@@ -197,13 +196,12 @@ static void ping_timeout_cb() {
}
static void start_ping(ipv4::ip4_addr dst_ip) {
const auto& ns = net_get_state();
prepend_buffer<4096> buf;
icmp::prepend_echo_request(buf, ns.mac, ns.ip,
icmp::prepend_echo_request(buf, eth::get_mac(), ipv4::get_ip(),
eth::MAC_BROADCAST, dst_ip, PING_ECHO_ID, 1);
ts.frame_cb = net_add_frame_callback(ping_reply_cb);
ts.frame_cb = eth::add_frame_callback(ping_reply_cb);
ts.timer = dispatch_schedule_ms(5000, ping_timeout_cb);
net_send_raw(buf.span());
eth::send_raw(buf.span());
}
static void test_ping_subnet() { start_ping({169, 254, 255, 255}); }
@@ -215,22 +213,21 @@ static size_t ping_rate_frame_size() {
}
static void ping_rate_send_one() {
const auto& ns = net_get_state();
auto& r = *ts.active_rate;
prepend_buffer<4096> buf;
if (r.payload_len > 0)
memset(buf.append(r.payload_len), 0xAA, r.payload_len);
icmp::prepend_echo_request(buf, ns.mac, ns.ip,
icmp::prepend_echo_request(buf, eth::get_mac(), ipv4::get_ip(),
r.peer.mac, r.peer.ip, PING_RATE_ECHO_ID,
r.sent + 1, r.payload_len);
net_send_raw(buf.span());
eth::send_raw(buf.span());
r.sent++;
}
static bool ping_rate_reply_cb(std::span<const uint8_t> frame) {
ipv4::ip4_addr src_ip;
if (!icmp::parse_echo_reply(frame, src_ip, PING_RATE_ECHO_ID)) return false;
if (src_ip == net_get_state().ip) return false;
if (src_ip == ipv4::get_ip()) return false;
auto& r = *ts.active_rate;
r.received++;
@@ -276,7 +273,7 @@ static void ping_rate_found(const peer_info& peer) {
r.received = 0;
r.start_us = time_us_32();
ts.frame_cb = net_add_frame_callback(ping_rate_reply_cb);
ts.frame_cb = eth::add_frame_callback(ping_rate_reply_cb);
ts.timer = dispatch_schedule_ms(10000, ping_rate_timeout_cb);
for (uint16_t i = 0; i < r.pipeline && r.sent < r.target; i++)

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
)

View File

@@ -2,8 +2,8 @@
#include <array>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "parse_buffer.h"
#include "debug_log.h"
namespace udp {
@@ -15,8 +15,11 @@ 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())
port_handlers[port_handler_count++] = {port_be, 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) {
@@ -46,8 +49,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
}
}
__attribute__((constructor))
static void register_protocol() {
void init() {
ipv4::register_protocol(17, handle);
}

View File

@@ -3,7 +3,6 @@
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "span_writer.h"
namespace udp {
@@ -37,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);

3
util/CMakeLists.txt Normal file
View File

@@ -0,0 +1,3 @@
add_library(util INTERFACE)
target_include_directories(util INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(util INTERFACE pico_stdlib)

View File

@@ -1,6 +1,9 @@
#pragma once
#include <utility>
// Fixed-capacity doubly-linked list with an embedded node array and intrusive
// free list. Used for callback registries (frame callbacks, timers) needing
// O(1) insert/remove with stable handles, without heap in IRQ-adjacent paths.
template <typename T, int N>
struct callback_list {
struct node {

View File

@@ -3,6 +3,9 @@
#include <cstdint>
#include <span>
// Bounds-checked sequential reader for byte streams. consume<T>() returns a
// typed pointer into the underlying buffer or nullptr on underflow. Used to
// walk packet headers in order without UB and without copies.
class parse_buffer {
const uint8_t* m_data;
size_t m_remaining;

View File

@@ -4,6 +4,10 @@
#include <cstring>
#include <span>
// Buffer that grows in both directions from a midpoint. Payload is appended at
// the back; each protocol layer then prepends its header in reverse order
// (UDP, then IPv4, then Ethernet) without moving payload bytes. Lets the
// network stack build outbound frames bottom-up with no copies or scratch.
template <size_t N>
class prepend_buffer {
uint8_t m_buf[N];

View File

@@ -3,6 +3,9 @@
#include <cstdint>
#include <span>
// Fixed-capacity FIFO with wrap-around. push_overwrite drops the oldest entry
// when full -- used by the debug log so dlog never blocks or fails. Iteration
// walks logical positions head..tail, hiding the modular indexing.
template <typename T, uint16_t N>
struct ring_buffer {
std::array<T, N> data = {};

View File

@@ -4,6 +4,9 @@
#include <cstring>
#include <span>
// Bounded writer over a caller-owned byte buffer. Tracks fill level and a
// sticky overflow flag so packing code can write blindly and check once at the
// end. Unlike std::span, it has a size cursor that grows toward capacity.
class span_writer {
uint8_t *m_data;
size_t m_capacity;

View File

@@ -3,6 +3,9 @@
#include <cstdint>
#include <cstring>
// Fixed-capacity contiguous container: std::vector's interface with std::array's
// storage. Used for response payloads sized at parse time but bounded at compile
// time, where heap allocation is unavailable or undesirable.
template <typename T, size_t Capacity>
class static_vector {
T m_data[Capacity];

View File

@@ -9,6 +9,9 @@ struct timer_entry {
using timer_handle = callback_list<timer_entry, 16>::node*;
// Deadline-sorted callback list with a single hardware alarm armed at the
// head. The alarm IRQ sets a flag; run() drains expired entries on the main
// loop, so callbacks execute in normal context rather than interrupt context.
struct timer_queue {
callback_list<timer_entry, 16> list;
alarm_id_t alarm = -1;