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

This commit is contained in:
Ian Gulliver
2026-05-01 11:03:16 -07:00
parent cc1448d6a2
commit fceae27f10
17 changed files with 149 additions and 148 deletions

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,7 +18,6 @@ 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);
@@ -25,9 +25,9 @@ struct responder {
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

@@ -59,6 +59,9 @@ 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);

View File

@@ -1,33 +0,0 @@
#pragma once
#include <cstdint>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "span_writer.h"
#include "callback_list.h"
namespace net {
struct state {
eth::mac_addr mac;
ipv4::ip4_addr ip;
};
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 eth::mac_addr& dst);
bool init();
const state& get_state();
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 net

View File

@@ -3,7 +3,6 @@
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "span_writer.h"
namespace udp {