Compare commits
10 Commits
7000c2e825
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 30f3eae111 | |||
| 71c07957f8 | |||
| a56e034bfb | |||
| 2d7ff98b82 | |||
| f64cdcaacf | |||
| fceae27f10 | |||
| cc1448d6a2 | |||
| bcddb14acf | |||
| b565a5de4c | |||
| 1e97058b9b |
+16
-7
@@ -1,19 +1,21 @@
|
|||||||
cmake_minimum_required(VERSION 3.13)
|
cmake_minimum_required(VERSION 3.13)
|
||||||
|
|
||||||
|
add_subdirectory(util)
|
||||||
add_subdirectory(w6300)
|
add_subdirectory(w6300)
|
||||||
add_subdirectory(debug_log)
|
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
|
add_library(limen STATIC
|
||||||
src/arp.cpp
|
|
||||||
src/dispatch.cpp
|
src/dispatch.cpp
|
||||||
src/flash.cpp
|
src/flash.cpp
|
||||||
src/handlers.cpp
|
src/handlers.cpp
|
||||||
src/icmp.cpp
|
|
||||||
src/igmp.cpp
|
|
||||||
src/ipv4.cpp
|
|
||||||
src/net.cpp
|
|
||||||
src/test_handlers.cpp
|
src/test_handlers.cpp
|
||||||
src/udp.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(limen PUBLIC
|
target_include_directories(limen PUBLIC
|
||||||
@@ -23,11 +25,18 @@ target_include_directories(limen PUBLIC
|
|||||||
target_compile_options(limen PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
target_compile_options(limen PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||||
|
|
||||||
target_link_libraries(limen PUBLIC
|
target_link_libraries(limen PUBLIC
|
||||||
|
util
|
||||||
w6300
|
w6300
|
||||||
debug_log
|
debug_log
|
||||||
|
msgpack
|
||||||
|
eth
|
||||||
|
ipv4
|
||||||
|
arp
|
||||||
|
icmp
|
||||||
|
igmp
|
||||||
|
udp
|
||||||
pico_stdlib
|
pico_stdlib
|
||||||
pico_sha256
|
pico_sha256
|
||||||
pico_unique_id
|
|
||||||
)
|
)
|
||||||
|
|
||||||
set(LIMEN_PARTITION_TABLE ${CMAKE_CURRENT_SOURCE_DIR}/partition_table.json CACHE INTERNAL "")
|
set(LIMEN_PARTITION_TABLE ${CMAKE_CURRENT_SOURCE_DIR}/partition_table.json CACHE INTERNAL "")
|
||||||
|
|||||||
@@ -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
|
||||||
|
)
|
||||||
+11
-10
@@ -1,5 +1,6 @@
|
|||||||
#include "arp.h"
|
#include "arp.h"
|
||||||
#include "net.h"
|
#include "eth.h"
|
||||||
|
#include "ipv4.h"
|
||||||
#include "parse_buffer.h"
|
#include "parse_buffer.h"
|
||||||
#include "prepend_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);
|
static constexpr uint16_t ARP_OP_REPLY = __builtin_bswap16(2);
|
||||||
|
|
||||||
void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
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);
|
parse_buffer pb(frame);
|
||||||
pb.consume<eth::header>();
|
pb.consume<eth::header>();
|
||||||
auto* arp_hdr = pb.consume<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->ptype != ARP_PTYPE_IPV4) return;
|
||||||
if (arp_hdr->hlen != 6 || arp_hdr->plen != 4) return;
|
if (arp_hdr->hlen != 6 || arp_hdr->plen != 4) return;
|
||||||
if (arp_hdr->oper != ARP_OP_REQUEST) 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;
|
prepend_buffer<4096> buf;
|
||||||
auto* reply = buf.template prepend<header>();
|
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->hlen = 6;
|
||||||
reply->plen = 4;
|
reply->plen = 4;
|
||||||
reply->oper = ARP_OP_REPLY;
|
reply->oper = ARP_OP_REPLY;
|
||||||
reply->sha = ns.mac;
|
reply->sha = mac;
|
||||||
reply->spa = ns.ip;
|
reply->spa = ip;
|
||||||
reply->tha = arp_hdr->sha;
|
reply->tha = arp_hdr->sha;
|
||||||
reply->tpa = arp_hdr->spa;
|
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))
|
void init() {
|
||||||
static void register_ethertype() {
|
eth::register_ethertype(eth::ETH_ARP, handle);
|
||||||
net::register_ethertype(eth::ETH_ARP, handle);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace arp
|
} // namespace arp
|
||||||
@@ -19,6 +19,7 @@ struct __attribute__((packed)) header {
|
|||||||
};
|
};
|
||||||
static_assert(sizeof(header) == 28);
|
static_assert(sizeof(header) == 28);
|
||||||
|
|
||||||
|
void init();
|
||||||
void handle(std::span<const uint8_t> frame, span_writer& tx);
|
void handle(std::span<const uint8_t> frame, span_writer& tx);
|
||||||
|
|
||||||
} // namespace arp
|
} // namespace arp
|
||||||
@@ -4,4 +4,4 @@ target_include_directories(debug_log PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
|||||||
|
|
||||||
target_compile_options(debug_log PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
target_compile_options(debug_log PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||||
|
|
||||||
target_link_libraries(debug_log PUBLIC pico_stdlib)
|
target_link_libraries(debug_log PUBLIC util pico_stdlib)
|
||||||
|
|||||||
@@ -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
|
||||||
|
)
|
||||||
+23
-33
@@ -1,18 +1,16 @@
|
|||||||
#include "net.h"
|
#include "eth.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include "pico/unique_id.h"
|
#include "pico/unique_id.h"
|
||||||
#include "pico/time.h"
|
|
||||||
#include "eth.h"
|
|
||||||
#include "parse_buffer.h"
|
#include "parse_buffer.h"
|
||||||
#include "prepend_buffer.h"
|
#include "prepend_buffer.h"
|
||||||
#include "w6300.h"
|
#include "w6300.h"
|
||||||
#include "debug_log.h"
|
#include "debug_log.h"
|
||||||
|
|
||||||
namespace net {
|
namespace eth {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
state g_state;
|
mac_addr g_mac;
|
||||||
w6300::socket_id raw_socket{0};
|
w6300::socket_id raw_socket{0};
|
||||||
frame_cb_list frame_callbacks;
|
frame_cb_list frame_callbacks;
|
||||||
|
|
||||||
@@ -26,11 +24,11 @@ size_t eth_handler_count = 0;
|
|||||||
std::array<mac_filter, 4> mac_filters;
|
std::array<mac_filter, 4> mac_filters;
|
||||||
size_t mac_filter_count = 0;
|
size_t mac_filter_count = 0;
|
||||||
|
|
||||||
bool default_mac_filter(const eth::mac_addr& dst) {
|
bool default_mac_filter(const mac_addr& dst) {
|
||||||
return dst == g_state.mac || dst == eth::MAC_BROADCAST;
|
return dst == g_mac || dst == MAC_BROADCAST;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mac_match(const eth::mac_addr& dst) {
|
bool mac_match(const mac_addr& dst) {
|
||||||
for (size_t i = 0; i < mac_filter_count; i++) {
|
for (size_t i = 0; i < mac_filter_count; i++) {
|
||||||
if (mac_filters[i](dst)) {
|
if (mac_filters[i](dst)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -40,10 +38,10 @@ bool mac_match(const eth::mac_addr& dst) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
|
void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
|
||||||
if (frame.size() < sizeof(eth::header)) {
|
if (frame.size() < sizeof(header)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto& eth_hdr = *reinterpret_cast<const eth::header*>(frame.data());
|
auto& eth_hdr = *reinterpret_cast<const header*>(frame.data());
|
||||||
|
|
||||||
if (!mac_match(eth_hdr.dst)) {
|
if (!mac_match(eth_hdr.dst)) {
|
||||||
return;
|
return;
|
||||||
@@ -63,16 +61,11 @@ void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((constructor))
|
|
||||||
void register_default_mac_filter() {
|
|
||||||
register_mac_filter(default_mac_filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
|
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
|
||||||
if (eth_handler_count >= eth_handlers.size()) {
|
if (eth_handler_count >= eth_handlers.size()) {
|
||||||
dlogf("net::register_ethertype overflow: ethertype=0x%04x dropped", __builtin_bswap16(ethertype_be));
|
dlogf("eth::register_ethertype overflow: ethertype=0x%04x dropped", __builtin_bswap16(ethertype_be));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
|
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
|
||||||
@@ -80,14 +73,14 @@ void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
|
|||||||
|
|
||||||
void register_mac_filter(mac_filter fn) {
|
void register_mac_filter(mac_filter fn) {
|
||||||
if (mac_filter_count >= mac_filters.size()) {
|
if (mac_filter_count >= mac_filters.size()) {
|
||||||
dlog("net::register_mac_filter overflow: filter dropped");
|
dlog("eth::register_mac_filter overflow: filter dropped");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mac_filters[mac_filter_count++] = fn;
|
mac_filters[mac_filter_count++] = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_raw(std::span<const uint8_t> data) {
|
void send_raw(std::span<const uint8_t> data) {
|
||||||
dlog_if_slow("net::send_raw", 1000, [&]{
|
dlog_if_slow("eth::send_raw", 1000, [&]{
|
||||||
auto result = w6300::send(raw_socket, data);
|
auto result = w6300::send(raw_socket, data);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
dlogf("w6300 send failed: %zu bytes, err %d",
|
dlogf("w6300 send failed: %zu bytes, err %d",
|
||||||
@@ -106,32 +99,29 @@ bool init() {
|
|||||||
|
|
||||||
pico_unique_board_id_t uid;
|
pico_unique_board_id_t uid;
|
||||||
pico_get_unique_board_id(&uid);
|
pico_get_unique_board_id(&uid);
|
||||||
g_state.mac[0] = (uid.id[0] & 0xFC) | 0x02;
|
g_mac[0] = (uid.id[0] & 0xFC) | 0x02;
|
||||||
g_state.mac[1] = uid.id[1];
|
g_mac[1] = uid.id[1];
|
||||||
g_state.mac[2] = uid.id[2];
|
g_mac[2] = uid.id[2];
|
||||||
g_state.mac[3] = uid.id[3];
|
g_mac[3] = uid.id[3];
|
||||||
g_state.mac[4] = uid.id[4];
|
g_mac[4] = uid.id[4];
|
||||||
g_state.mac[5] = uid.id[5];
|
g_mac[5] = uid.id[5];
|
||||||
|
|
||||||
g_state.ip[0] = 169;
|
|
||||||
g_state.ip[1] = 254;
|
|
||||||
g_state.ip[2] = g_state.mac[4];
|
|
||||||
g_state.ip[3] = g_state.mac[5];
|
|
||||||
|
|
||||||
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
|
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
|
||||||
w6300::set_interrupt_mask(w6300::ik_sock_0);
|
w6300::set_interrupt_mask(w6300::ik_sock_0);
|
||||||
|
|
||||||
|
register_mac_filter(default_mac_filter);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const state& get_state() {
|
const mac_addr& get_mac() {
|
||||||
return g_state;
|
return g_mac;
|
||||||
}
|
}
|
||||||
|
|
||||||
frame_cb_handle add_frame_callback(frame_callback cb) {
|
frame_cb_handle add_frame_callback(frame_callback cb) {
|
||||||
auto h = frame_callbacks.insert(cb);
|
auto h = frame_callbacks.insert(cb);
|
||||||
if (!h) {
|
if (!h) {
|
||||||
dlog("net::add_frame_callback overflow: callback dropped");
|
dlog("eth::add_frame_callback overflow: callback dropped");
|
||||||
}
|
}
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
@@ -158,4 +148,4 @@ void poll(std::span<uint8_t> tx) {
|
|||||||
w6300::rearm_gpio_irq();
|
w6300::rearm_gpio_irq();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace net
|
} // namespace eth
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
|
)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "icmp.h"
|
#include "icmp.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include "eth.h"
|
||||||
#include "ipv4.h"
|
#include "ipv4.h"
|
||||||
#include "net.h"
|
|
||||||
#include "parse_buffer.h"
|
#include "parse_buffer.h"
|
||||||
#include "prepend_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) return;
|
||||||
if (icmp_pkt->type != 8) return;
|
if (icmp_pkt->type != 8) return;
|
||||||
|
|
||||||
const auto& ns = net::get_state();
|
|
||||||
prepend_buffer<4096> buf;
|
prepend_buffer<4096> buf;
|
||||||
memcpy(buf.append(icmp_len), pb.remaining().data() - sizeof(echo), icmp_len);
|
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 = 0;
|
||||||
reply->checksum = ipv4::checksum(reply, icmp_len);
|
reply->checksum = ipv4::checksum(reply, icmp_len);
|
||||||
|
|
||||||
ipv4::prepend(buf, eth_hdr->src, ns.mac, ns.ip, ip->src, 1, icmp_len);
|
ipv4::prepend(buf, eth_hdr->src, eth::get_mac(), ipv4::get_ip(), ip->src, 1, icmp_len);
|
||||||
net::send_raw(buf.span());
|
eth::send_raw(buf.span());
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((constructor))
|
void init() {
|
||||||
static void register_protocol() {
|
|
||||||
ipv4::register_protocol(1, handle);
|
ipv4::register_protocol(1, handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,6 +16,7 @@ struct __attribute__((packed)) echo {
|
|||||||
};
|
};
|
||||||
static_assert(sizeof(echo) == 8);
|
static_assert(sizeof(echo) == 8);
|
||||||
|
|
||||||
|
void init();
|
||||||
void handle(std::span<const uint8_t> frame, span_writer& tx);
|
void handle(std::span<const uint8_t> frame, span_writer& tx);
|
||||||
|
|
||||||
template <typename Buf>
|
template <typename Buf>
|
||||||
@@ -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
|
||||||
|
)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "igmp.h"
|
#include "igmp.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "eth.h"
|
||||||
#include "ipv4.h"
|
#include "ipv4.h"
|
||||||
#include "net.h"
|
|
||||||
#include "parse_buffer.h"
|
#include "parse_buffer.h"
|
||||||
#include "prepend_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) {
|
static void send_report(const ipv4::ip4_addr& group) {
|
||||||
const auto& ns = net::get_state();
|
|
||||||
prepend_buffer<4096> buf;
|
prepend_buffer<4096> buf;
|
||||||
prepend_report(buf, ns.mac, ns.ip, group);
|
prepend_report(buf, eth::get_mac(), ipv4::get_ip(), group);
|
||||||
net::send_raw(buf.span());
|
eth::send_raw(buf.span());
|
||||||
}
|
}
|
||||||
|
|
||||||
void join(const ipv4::ip4_addr& group) {
|
void join(const ipv4::ip4_addr& group) {
|
||||||
@@ -79,10 +78,9 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((constructor))
|
void init() {
|
||||||
static void register_protocol() {
|
|
||||||
ipv4::register_protocol(2, handle);
|
ipv4::register_protocol(2, handle);
|
||||||
net::register_mac_filter(is_member_mac);
|
eth::register_mac_filter(is_member_mac);
|
||||||
ipv4::register_addr_filter(is_member);
|
ipv4::register_addr_filter(is_member);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,6 +17,8 @@ struct __attribute__((packed)) message {
|
|||||||
};
|
};
|
||||||
static_assert(sizeof(message) == 8);
|
static_assert(sizeof(message) == 8);
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
eth::mac_addr mac_for_ip(const ipv4::ip4_addr& group);
|
eth::mac_addr mac_for_ip(const ipv4::ip4_addr& group);
|
||||||
bool is_member(const ipv4::ip4_addr& ip);
|
bool is_member(const ipv4::ip4_addr& ip);
|
||||||
bool is_member_mac(const eth::mac_addr& mac);
|
bool is_member_mac(const eth::mac_addr& mac);
|
||||||
+4
-4
@@ -5,7 +5,8 @@
|
|||||||
#include <span>
|
#include <span>
|
||||||
#include "wire.h"
|
#include "wire.h"
|
||||||
#include "timer_queue.h"
|
#include "timer_queue.h"
|
||||||
#include "net.h"
|
#include "eth.h"
|
||||||
|
#include "ipv4.h"
|
||||||
#include "prepend_buffer.h"
|
#include "prepend_buffer.h"
|
||||||
#include "udp.h"
|
#include "udp.h"
|
||||||
|
|
||||||
@@ -17,7 +18,6 @@ struct responder {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void respond(const T& msg) const {
|
void respond(const T& msg) const {
|
||||||
const auto& ns = net::get_state();
|
|
||||||
prepend_buffer<4096> buf;
|
prepend_buffer<4096> buf;
|
||||||
span_writer out(buf.payload_ptr(), 2048);
|
span_writer out(buf.payload_ptr(), 2048);
|
||||||
auto r = encode_response_into(out, message_id, msg);
|
auto r = encode_response_into(out, message_id, msg);
|
||||||
@@ -25,9 +25,9 @@ struct responder {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
buf.append(*r);
|
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);
|
dispatch_listen_port_be(), reply_to.port, *r);
|
||||||
net::send_raw(buf.span());
|
eth::send_raw(buf.span());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -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
|
|
||||||
@@ -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
|
||||||
|
)
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "ipv4.h"
|
#include "ipv4.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include "net.h"
|
#include "eth.h"
|
||||||
#include "parse_buffer.h"
|
#include "parse_buffer.h"
|
||||||
#include "debug_log.h"
|
#include "debug_log.h"
|
||||||
|
|
||||||
@@ -10,6 +10,8 @@ namespace {
|
|||||||
|
|
||||||
constexpr ip4_addr IP_BROADCAST_ALL = {255, 255, 255, 255};
|
constexpr ip4_addr IP_BROADCAST_ALL = {255, 255, 255, 255};
|
||||||
|
|
||||||
|
ip4_addr g_ip;
|
||||||
|
|
||||||
std::array<addr_filter, 4> addr_filters;
|
std::array<addr_filter, 4> addr_filters;
|
||||||
size_t addr_filter_count = 0;
|
size_t addr_filter_count = 0;
|
||||||
|
|
||||||
@@ -21,12 +23,22 @@ std::array<protocol_entry, 8> protocol_handlers;
|
|||||||
size_t protocol_handler_count = 0;
|
size_t protocol_handler_count = 0;
|
||||||
|
|
||||||
bool default_addr_filter(const ip4_addr& dst) {
|
bool default_addr_filter(const ip4_addr& dst) {
|
||||||
const auto& ns = net::get_state();
|
return dst == g_ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST;
|
||||||
return dst == ns.ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // 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) {
|
uint16_t checksum(const void* data, size_t len) {
|
||||||
auto p = static_cast<const uint8_t*>(data);
|
auto p = static_cast<const uint8_t*>(data);
|
||||||
uint32_t sum = 0;
|
uint32_t sum = 0;
|
||||||
@@ -91,10 +103,4 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((constructor))
|
|
||||||
static void register_self() {
|
|
||||||
net::register_ethertype(eth::ETH_IPV4, handle);
|
|
||||||
register_addr_filter(default_addr_filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ipv4
|
} // namespace ipv4
|
||||||
@@ -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 handle(std::span<const uint8_t> frame, span_writer& tx);
|
||||||
|
|
||||||
|
void init();
|
||||||
|
const ip4_addr& get_ip();
|
||||||
|
|
||||||
bool addressed_to_us(ip4_addr dst);
|
bool addressed_to_us(ip4_addr dst);
|
||||||
|
|
||||||
using protocol_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
|
using protocol_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
|
||||||
@@ -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)
|
||||||
@@ -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
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cassert>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <span>
|
||||||
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "span_writer.h"
|
#include "span_writer.h"
|
||||||
|
|
||||||
@@ -102,65 +104,11 @@ result<T> body_number(const uint8_t *p, int size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct body_info {
|
struct body_info {
|
||||||
int header; // bytes before the body (includes format byte + length fields + ext type byte)
|
int header;
|
||||||
uint32_t body; // body size in bytes (0 for containers, computed for variable-length)
|
uint32_t body;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline result<body_info> get_body_info(const uint8_t *p, int size) {
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class packer {
|
class packer {
|
||||||
private:
|
private:
|
||||||
@@ -185,15 +133,8 @@ public:
|
|||||||
|
|
||||||
using pack_result = result<std::reference_wrapper<packer>>;
|
using pack_result = result<std::reference_wrapper<packer>>;
|
||||||
|
|
||||||
pack_result pack_nil() {
|
pack_result pack_nil();
|
||||||
m_buf.push_back(format::NIL);
|
pack_result pack_bool(bool v);
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
pack_result pack_bool(bool v) {
|
|
||||||
m_buf.push_back(v ? format::TRUE : format::FALSE);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
pack_result pack_integer(T n) {
|
pack_result pack_integer(T n) {
|
||||||
@@ -201,7 +142,7 @@ public:
|
|||||||
if (n >= 0 && n <= 0x7F) {
|
if (n >= 0 && n <= 0x7F) {
|
||||||
m_buf.push_back(static_cast<uint8_t>(n));
|
m_buf.push_back(static_cast<uint8_t>(n));
|
||||||
} else if (n >= -32 && n < 0) {
|
} 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()) {
|
} 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(format::INT8);
|
||||||
m_buf.push_back(static_cast<uint8_t>(n));
|
m_buf.push_back(static_cast<uint8_t>(n));
|
||||||
@@ -235,23 +176,9 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
pack_result pack_uint32_fixed(uint32_t n) {
|
pack_result pack_uint32_fixed(uint32_t n);
|
||||||
m_buf.push_back(format::UINT32);
|
pack_result pack_float(float n);
|
||||||
push_big_endian(n);
|
pack_result pack_double(double 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class Range>
|
template <class Range>
|
||||||
pack_result pack_str(const Range &r) {
|
pack_result pack_str(const Range &r) {
|
||||||
@@ -274,9 +201,7 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
pack_result pack_str(const char *s) {
|
pack_result pack_str(const char *s);
|
||||||
return pack_str(std::string_view(s));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class Range>
|
template <class Range>
|
||||||
pack_result pack_bin(const Range &r) {
|
pack_result pack_bin(const Range &r) {
|
||||||
@@ -297,48 +222,10 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
pack_result pack_array(size_t n) {
|
pack_result pack_array(size_t n);
|
||||||
if (n <= 15) {
|
pack_result pack_map(size_t n);
|
||||||
m_buf.push_back(format::FIXARRAY_MIN | static_cast<uint8_t>(n));
|
pack_result pack_ext16_header(char type, uint16_t len);
|
||||||
} else if (n <= std::numeric_limits<uint16_t>::max()) {
|
pack_result pack_bin16_header(uint16_t len);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class Range>
|
template <class Range>
|
||||||
pack_result pack_ext(char type, const Range &r) {
|
pack_result pack_ext(char type, const Range &r) {
|
||||||
@@ -377,14 +264,13 @@ public:
|
|||||||
requires std::is_enum_v<T>
|
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(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(bool v);
|
||||||
pack_result pack(float v) { return pack_float(v); }
|
pack_result pack(float v);
|
||||||
pack_result pack(double v) { return pack_double(v); }
|
pack_result pack(double v);
|
||||||
pack_result pack(const char *v) { return pack_str(v); }
|
pack_result pack(const char *v);
|
||||||
pack_result pack(std::string_view v) { return pack_str(v); }
|
pack_result pack(std::string_view v);
|
||||||
pack_result pack(const std::string &v) { return pack_str(v); }
|
pack_result pack(const std::string &v);
|
||||||
|
pack_result pack(const std::vector<uint8_t> &v);
|
||||||
pack_result pack(const std::vector<uint8_t> &v) { return pack_bin(v); }
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
requires (!std::is_same_v<T, uint8_t>)
|
requires (!std::is_same_v<T, uint8_t>)
|
||||||
@@ -464,210 +350,21 @@ public:
|
|||||||
return parser(m_p + n, m_size - n);
|
return parser(m_p + n, m_size - n);
|
||||||
}
|
}
|
||||||
|
|
||||||
result<parser> next() const {
|
result<parser> next() const;
|
||||||
auto hdr = header_byte();
|
|
||||||
if (!hdr) return std::unexpected(hdr.error());
|
|
||||||
|
|
||||||
if (is_array()) {
|
bool is_nil() const;
|
||||||
auto info = get_body_info(m_p, m_size);
|
bool is_bool() const;
|
||||||
if (!info) return std::unexpected(info.error());
|
bool is_number() const;
|
||||||
auto cnt = count();
|
bool is_string() const;
|
||||||
if (!cnt) return std::unexpected(cnt.error());
|
bool is_binary() const;
|
||||||
auto cur = advance(info->header);
|
bool is_ext() const;
|
||||||
if (!cur) return std::unexpected(cur.error());
|
bool is_array() const;
|
||||||
for (uint32_t i = 0; i < *cnt; ++i) {
|
bool is_map() const;
|
||||||
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 {
|
result<bool> get_bool() const;
|
||||||
auto h = header_byte();
|
result<std::string_view> get_string() const;
|
||||||
return h && *h == format::NIL;
|
result<std::string_view> get_binary_view() const;
|
||||||
}
|
result<std::tuple<int8_t, std::string_view>> get_ext() const;
|
||||||
|
|
||||||
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)};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
result<T> get_number() const {
|
result<T> get_number() const {
|
||||||
@@ -694,41 +391,9 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result<uint32_t> count() const {
|
result<uint32_t> count() const;
|
||||||
auto h = header_byte();
|
result<parser> first_item() const;
|
||||||
if (!h) return std::unexpected(h.error());
|
parser operator[](int index) const;
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -750,26 +415,11 @@ result<parser> unpack(const parser &p, T &out) {
|
|||||||
return p.next();
|
return p.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline result<parser> unpack(const parser &p, bool &out) {
|
result<parser> unpack(const parser &p, bool &out);
|
||||||
auto v = p.get_bool();
|
result<parser> unpack(const parser &p, std::string_view &out);
|
||||||
if (!v) return std::unexpected(v.error());
|
result<parser> unpack(const parser &p, std::string &out);
|
||||||
out = *v;
|
result<parser> unpack(const parser &p, std::vector<uint8_t> &out);
|
||||||
return p.next();
|
result<parser> unpack(const parser &p, std::span<const uint8_t> &out);
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
result<parser> unpack(const parser &p, std::array<uint8_t, N> &out) {
|
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();
|
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>
|
template <typename T>
|
||||||
requires (!std::is_same_v<T, uint8_t>)
|
requires (!std::is_same_v<T, uint8_t>)
|
||||||
result<parser> unpack(const parser &p, std::vector<T> &out) {
|
result<parser> unpack(const parser &p, std::vector<T> &out) {
|
||||||
+11
-3
@@ -3,7 +3,10 @@
|
|||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include "wire.h"
|
#include "wire.h"
|
||||||
#include "timer_queue.h"
|
#include "timer_queue.h"
|
||||||
#include "net.h"
|
#include "eth.h"
|
||||||
|
#include "ipv4.h"
|
||||||
|
#include "arp.h"
|
||||||
|
#include "icmp.h"
|
||||||
#include "igmp.h"
|
#include "igmp.h"
|
||||||
#include "udp.h"
|
#include "udp.h"
|
||||||
#include "debug_log.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) {
|
void dispatch_init(uint16_t port_be) {
|
||||||
listen_port_be = 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);
|
udp::register_port(port_be, on_udp_message);
|
||||||
net::init();
|
|
||||||
dispatch_schedule_ms(60000, igmp_reannounce);
|
dispatch_schedule_ms(60000, igmp_reannounce);
|
||||||
dlog("dispatch_init complete");
|
dlog("dispatch_init complete");
|
||||||
}
|
}
|
||||||
@@ -59,7 +67,7 @@ bool dispatch_cancel_timer(timer_handle h) {
|
|||||||
uint32_t save = save_and_disable_interrupts();
|
uint32_t save = save_and_disable_interrupts();
|
||||||
|
|
||||||
dlog_if_slow("timers", 1000, [&]{ timers.run(); });
|
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();
|
__wfi();
|
||||||
restore_interrupts(save);
|
restore_interrupts(save);
|
||||||
|
|||||||
+4
-4
@@ -5,7 +5,8 @@
|
|||||||
#include "hardware/watchdog.h"
|
#include "hardware/watchdog.h"
|
||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "dispatch.h"
|
#include "dispatch.h"
|
||||||
#include "net.h"
|
#include "eth.h"
|
||||||
|
#include "ipv4.h"
|
||||||
#include "debug_log.h"
|
#include "debug_log.h"
|
||||||
|
|
||||||
static boot_reason detected_boot_reason;
|
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_unique_board_id_t uid;
|
||||||
pico_get_unique_board_id(&uid);
|
pico_get_unique_board_id(&uid);
|
||||||
std::copy(uid.id, uid.id + 8, resp.board_id.begin());
|
std::copy(uid.id, uid.id + 8, resp.board_id.begin());
|
||||||
auto& ns = net::get_state();
|
resp.mac = eth::get_mac();
|
||||||
resp.mac = ns.mac;
|
resp.ip = ipv4::get_ip();
|
||||||
resp.ip = ns.ip;
|
|
||||||
resp.firmware_name = firmware_name;
|
resp.firmware_name = firmware_name;
|
||||||
resp.boot = detected_boot_reason;
|
resp.boot = detected_boot_reason;
|
||||||
resp.build_epoch = firmware_build_epoch;
|
resp.build_epoch = firmware_build_epoch;
|
||||||
|
|||||||
+20
-23
@@ -4,7 +4,8 @@
|
|||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include "pico/time.h"
|
#include "pico/time.h"
|
||||||
#include "handlers.h"
|
#include "handlers.h"
|
||||||
#include "net.h"
|
#include "eth.h"
|
||||||
|
#include "ipv4.h"
|
||||||
#include "icmp.h"
|
#include "icmp.h"
|
||||||
#include "igmp.h"
|
#include "igmp.h"
|
||||||
#include "udp.h"
|
#include "udp.h"
|
||||||
@@ -55,7 +56,7 @@ struct test_state {
|
|||||||
bool in_flight = false;
|
bool in_flight = false;
|
||||||
responder resp;
|
responder resp;
|
||||||
timer_handle timer = nullptr;
|
timer_handle timer = nullptr;
|
||||||
net::frame_cb_handle frame_cb = nullptr;
|
eth::frame_cb_handle frame_cb = nullptr;
|
||||||
|
|
||||||
discovery_data* active_discovery = nullptr;
|
discovery_data* active_discovery = nullptr;
|
||||||
ping_rate_data* active_rate = nullptr;
|
ping_rate_data* active_rate = nullptr;
|
||||||
@@ -72,7 +73,7 @@ static test_state ts;
|
|||||||
|
|
||||||
static void test_end(const ResponseTest& result) {
|
static void test_end(const ResponseTest& result) {
|
||||||
if (ts.timer) { dispatch_cancel_timer(ts.timer); ts.timer = nullptr; }
|
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_discovery = nullptr;
|
||||||
ts.active_rate = nullptr;
|
ts.active_rate = nullptr;
|
||||||
ts.resp.respond(result);
|
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;
|
if (options_len > 0 && !pb.skip(options_len)) return false;
|
||||||
auto* uhdr = pb.consume<udp::header>();
|
auto* uhdr = pb.consume<udp::header>();
|
||||||
if (!uhdr || uhdr->src_port != PICOMAP_PORT_BE) return false;
|
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);
|
dispatch_cancel_timer(ts.timer);
|
||||||
ts.timer = nullptr;
|
ts.timer = nullptr;
|
||||||
ts.frame_cb = 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() {
|
static void discover_timeout_cb() {
|
||||||
net::remove_frame_callback(ts.frame_cb);
|
eth::remove_frame_callback(ts.frame_cb);
|
||||||
ts.frame_cb = nullptr;
|
ts.frame_cb = nullptr;
|
||||||
ts.timer = nullptr;
|
ts.timer = nullptr;
|
||||||
auto cont = ts.active_discovery ? ts.active_discovery->on_timeout : 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;
|
d.on_timeout = timeout;
|
||||||
ts.active_discovery = &d;
|
ts.active_discovery = &d;
|
||||||
|
|
||||||
const auto& ns = net::get_state();
|
|
||||||
eth::mac_addr mcast_mac = igmp::mac_for_ip(PICOMAP_DISCOVERY_GROUP);
|
eth::mac_addr mcast_mac = igmp::mac_for_ip(PICOMAP_DISCOVERY_GROUP);
|
||||||
|
|
||||||
prepend_buffer<4096> buf;
|
prepend_buffer<4096> buf;
|
||||||
@@ -134,13 +134,13 @@ static void discover_peer(discovery_data& d,
|
|||||||
}
|
}
|
||||||
buf.append(*encoded);
|
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);
|
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);
|
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) {
|
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() {
|
static void test_discovery_igmp() {
|
||||||
const auto& ns = net::get_state();
|
|
||||||
prepend_buffer<4096> buf;
|
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);
|
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) {
|
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;
|
ipv4::ip4_addr src_ip;
|
||||||
if (!icmp::parse_echo_reply(frame, src_ip, PING_ECHO_ID)) return false;
|
if (!icmp::parse_echo_reply(frame, src_ip, PING_ECHO_ID)) return false;
|
||||||
ts.frame_cb = nullptr;
|
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)}});
|
test_end({false, {"got reply from self: " + ipv4::to_string(src_ip)}});
|
||||||
else
|
else
|
||||||
test_end({true, {"reply from " + ipv4::to_string(src_ip)}});
|
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) {
|
static void start_ping(ipv4::ip4_addr dst_ip) {
|
||||||
const auto& ns = net::get_state();
|
|
||||||
prepend_buffer<4096> buf;
|
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);
|
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);
|
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}); }
|
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() {
|
static void ping_rate_send_one() {
|
||||||
const auto& ns = net::get_state();
|
|
||||||
auto& r = *ts.active_rate;
|
auto& r = *ts.active_rate;
|
||||||
prepend_buffer<4096> buf;
|
prepend_buffer<4096> buf;
|
||||||
if (r.payload_len > 0)
|
if (r.payload_len > 0)
|
||||||
memset(buf.append(r.payload_len), 0xAA, r.payload_len);
|
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.peer.mac, r.peer.ip, PING_RATE_ECHO_ID,
|
||||||
r.sent + 1, r.payload_len);
|
r.sent + 1, r.payload_len);
|
||||||
net::send_raw(buf.span());
|
eth::send_raw(buf.span());
|
||||||
r.sent++;
|
r.sent++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ping_rate_reply_cb(std::span<const uint8_t> frame) {
|
static bool ping_rate_reply_cb(std::span<const uint8_t> frame) {
|
||||||
ipv4::ip4_addr src_ip;
|
ipv4::ip4_addr src_ip;
|
||||||
if (!icmp::parse_echo_reply(frame, src_ip, PING_RATE_ECHO_ID)) return false;
|
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;
|
auto& r = *ts.active_rate;
|
||||||
r.received++;
|
r.received++;
|
||||||
@@ -276,7 +273,7 @@ static void ping_rate_found(const peer_info& peer) {
|
|||||||
r.received = 0;
|
r.received = 0;
|
||||||
r.start_us = time_us_32();
|
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);
|
ts.timer = dispatch_schedule_ms(10000, ping_rate_timeout_cb);
|
||||||
|
|
||||||
for (uint16_t i = 0; i < r.pipeline && r.sent < r.target; i++)
|
for (uint16_t i = 0; i < r.pipeline && r.sent < r.target; i++)
|
||||||
|
|||||||
@@ -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
|
||||||
|
)
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include "eth.h"
|
#include "eth.h"
|
||||||
#include "ipv4.h"
|
#include "ipv4.h"
|
||||||
#include "net.h"
|
|
||||||
#include "parse_buffer.h"
|
#include "parse_buffer.h"
|
||||||
#include "debug_log.h"
|
#include "debug_log.h"
|
||||||
|
|
||||||
@@ -50,8 +49,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((constructor))
|
void init() {
|
||||||
static void register_protocol() {
|
|
||||||
ipv4::register_protocol(17, handle);
|
ipv4::register_protocol(17, handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3,7 +3,6 @@
|
|||||||
#include <span>
|
#include <span>
|
||||||
#include "eth.h"
|
#include "eth.h"
|
||||||
#include "ipv4.h"
|
#include "ipv4.h"
|
||||||
#include "net.h"
|
|
||||||
#include "span_writer.h"
|
#include "span_writer.h"
|
||||||
|
|
||||||
namespace udp {
|
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);
|
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);
|
void handle(std::span<const uint8_t> frame, span_writer& tx);
|
||||||
|
|
||||||
using port_handler = void (*)(std::span<const uint8_t> payload, const address& from);
|
using port_handler = void (*)(std::span<const uint8_t> payload, const address& from);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
add_library(util INTERFACE)
|
||||||
|
target_include_directories(util INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
target_link_libraries(util INTERFACE pico_stdlib)
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <utility>
|
#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>
|
template <typename T, int N>
|
||||||
struct callback_list {
|
struct callback_list {
|
||||||
struct node {
|
struct node {
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <span>
|
#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 {
|
class parse_buffer {
|
||||||
const uint8_t* m_data;
|
const uint8_t* m_data;
|
||||||
size_t m_remaining;
|
size_t m_remaining;
|
||||||
@@ -4,6 +4,10 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <span>
|
#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>
|
template <size_t N>
|
||||||
class prepend_buffer {
|
class prepend_buffer {
|
||||||
uint8_t m_buf[N];
|
uint8_t m_buf[N];
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <span>
|
#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>
|
template <typename T, uint16_t N>
|
||||||
struct ring_buffer {
|
struct ring_buffer {
|
||||||
std::array<T, N> data = {};
|
std::array<T, N> data = {};
|
||||||
@@ -4,6 +4,9 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <span>
|
#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 {
|
class span_writer {
|
||||||
uint8_t *m_data;
|
uint8_t *m_data;
|
||||||
size_t m_capacity;
|
size_t m_capacity;
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#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>
|
template <typename T, size_t Capacity>
|
||||||
class static_vector {
|
class static_vector {
|
||||||
T m_data[Capacity];
|
T m_data[Capacity];
|
||||||
@@ -9,6 +9,9 @@ struct timer_entry {
|
|||||||
|
|
||||||
using timer_handle = callback_list<timer_entry, 16>::node*;
|
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 {
|
struct timer_queue {
|
||||||
callback_list<timer_entry, 16> list;
|
callback_list<timer_entry, 16> list;
|
||||||
alarm_id_t alarm = -1;
|
alarm_id_t alarm = -1;
|
||||||
Reference in New Issue
Block a user