Add IGMP, prepend_buffer/parse_buffer, split UDP header, discovery tests, test all
This commit is contained in:
@@ -7,8 +7,7 @@
|
||||
|
||||
namespace arp {
|
||||
|
||||
struct __attribute__((packed)) packet {
|
||||
eth::header eth;
|
||||
struct __attribute__((packed)) header {
|
||||
uint16_t htype;
|
||||
uint16_t ptype;
|
||||
uint8_t hlen;
|
||||
@@ -19,7 +18,7 @@ struct __attribute__((packed)) packet {
|
||||
eth::mac_addr tha;
|
||||
ipv4::ip4_addr tpa;
|
||||
};
|
||||
static_assert(sizeof(packet) == 42);
|
||||
static_assert(sizeof(header) == 28);
|
||||
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip,
|
||||
|
||||
@@ -17,4 +17,12 @@ struct __attribute__((packed)) header {
|
||||
};
|
||||
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
|
||||
|
||||
@@ -21,10 +21,20 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip,
|
||||
std::function<void(std::span<const uint8_t>)> send_raw);
|
||||
|
||||
size_t build_echo_request(std::span<uint8_t> buf,
|
||||
template <typename Buf>
|
||||
void prepend_echo_request(Buf& buf,
|
||||
eth::mac_addr src_mac, ipv4::ip4_addr src_ip,
|
||||
eth::mac_addr dst_mac, ipv4::ip4_addr dst_ip,
|
||||
uint16_t id, uint16_t seq);
|
||||
uint16_t id, uint16_t seq) {
|
||||
auto* e = buf.template prepend<echo>();
|
||||
e->type = 8;
|
||||
e->code = 0;
|
||||
e->checksum = 0;
|
||||
e->id = id;
|
||||
e->seq = seq;
|
||||
e->checksum = ipv4::checksum(e, sizeof(echo));
|
||||
ipv4::prepend(buf, dst_mac, src_mac, src_ip, dst_ip, 1, sizeof(echo));
|
||||
}
|
||||
|
||||
bool parse_echo_reply(std::span<const uint8_t> frame, ipv4::ip4_addr& src_ip, uint16_t expected_id);
|
||||
|
||||
|
||||
64
firmware/include/igmp.h
Normal file
64
firmware/include/igmp.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "span_writer.h"
|
||||
|
||||
namespace igmp {
|
||||
|
||||
static constexpr ipv4::ip4_addr PICOMAP_DISCOVERY_GROUP = {239, 112, 77, 1};
|
||||
static constexpr ipv4::ip4_addr ALL_HOSTS = {224, 0, 0, 1};
|
||||
|
||||
struct __attribute__((packed)) message {
|
||||
uint8_t type;
|
||||
uint8_t max_resp_time;
|
||||
uint16_t checksum;
|
||||
ipv4::ip4_addr group;
|
||||
};
|
||||
static_assert(sizeof(message) == 8);
|
||||
|
||||
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);
|
||||
|
||||
void join(const ipv4::ip4_addr& group,
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip,
|
||||
std::function<void(std::span<const uint8_t>)> send_raw);
|
||||
|
||||
void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip,
|
||||
std::function<void(std::span<const uint8_t>)> send_raw);
|
||||
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip,
|
||||
std::function<void(std::span<const uint8_t>)> send_raw);
|
||||
|
||||
template <typename Buf>
|
||||
void prepend_report(Buf& buf, const eth::mac_addr& src_mac, ipv4::ip4_addr src_ip,
|
||||
const ipv4::ip4_addr& group) {
|
||||
auto* m = buf.template prepend<message>();
|
||||
m->type = 0x16;
|
||||
m->max_resp_time = 0;
|
||||
m->checksum = 0;
|
||||
m->group = group;
|
||||
m->checksum = ipv4::checksum(m, sizeof(message));
|
||||
ipv4::prepend(buf, mac_for_ip(group), src_mac, src_ip, group, 2, sizeof(message), 1);
|
||||
}
|
||||
|
||||
template <typename Buf>
|
||||
void prepend_query(Buf& buf, const eth::mac_addr& src_mac, ipv4::ip4_addr src_ip,
|
||||
const ipv4::ip4_addr& group) {
|
||||
ipv4::ip4_addr dst_ip = (group == ipv4::ip4_addr{0, 0, 0, 0}) ? ALL_HOSTS : group;
|
||||
auto* m = buf.template prepend<message>();
|
||||
m->type = 0x11;
|
||||
m->max_resp_time = 100;
|
||||
m->checksum = 0;
|
||||
m->group = group;
|
||||
m->checksum = ipv4::checksum(m, sizeof(message));
|
||||
ipv4::prepend(buf, mac_for_ip(dst_ip), src_mac, src_ip, dst_ip, 2, sizeof(message), 1);
|
||||
}
|
||||
|
||||
bool parse_report(std::span<const uint8_t> frame, ipv4::ip4_addr& group);
|
||||
|
||||
} // namespace igmp
|
||||
@@ -19,7 +19,6 @@ inline std::string to_string(const ip4_addr& ip) {
|
||||
}
|
||||
|
||||
struct __attribute__((packed)) header {
|
||||
eth::header eth;
|
||||
uint8_t ver_ihl;
|
||||
uint8_t dscp_ecn;
|
||||
uint16_t total_len;
|
||||
@@ -31,24 +30,32 @@ struct __attribute__((packed)) header {
|
||||
ip4_addr src;
|
||||
ip4_addr dst;
|
||||
|
||||
size_t ip_header_len() const { return (ver_ihl & 0x0F) * 4; }
|
||||
size_t ip_total_len() const { return __builtin_bswap16(total_len); }
|
||||
const uint8_t* ip_start() const { return reinterpret_cast<const uint8_t*>(&ver_ihl); }
|
||||
uint8_t* ip_start() { return reinterpret_cast<uint8_t*>(&ver_ihl); }
|
||||
size_t header_len() const { return (ver_ihl & 0x0F) * 4; }
|
||||
size_t total() const { return __builtin_bswap16(total_len); }
|
||||
};
|
||||
static_assert(sizeof(header) == 34);
|
||||
|
||||
struct __attribute__((packed)) udp_header {
|
||||
header ip;
|
||||
uint16_t src_port;
|
||||
uint16_t dst_port;
|
||||
uint16_t length;
|
||||
uint16_t checksum;
|
||||
};
|
||||
static_assert(sizeof(udp_header) == 42);
|
||||
static_assert(sizeof(header) == 20);
|
||||
|
||||
uint16_t checksum(const void* data, size_t len);
|
||||
|
||||
template <typename Buf>
|
||||
void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_mac,
|
||||
ip4_addr src_ip, ip4_addr dst_ip, uint8_t protocol,
|
||||
size_t payload_len, uint8_t ttl = 64) {
|
||||
auto* h = buf.template prepend<header>();
|
||||
h->ver_ihl = 0x45;
|
||||
h->dscp_ecn = 0;
|
||||
h->total_len = __builtin_bswap16(sizeof(header) + payload_len);
|
||||
h->identification = 0;
|
||||
h->flags_frag = 0;
|
||||
h->ttl = ttl;
|
||||
h->protocol = protocol;
|
||||
h->checksum = 0;
|
||||
h->src = src_ip;
|
||||
h->dst = dst_ip;
|
||||
h->checksum = checksum(h, sizeof(header));
|
||||
eth::prepend(buf, dst_mac, src_mac, eth::ETH_IPV4);
|
||||
}
|
||||
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
eth::mac_addr our_mac, ip4_addr our_ip, ip4_addr subnet_broadcast,
|
||||
std::function<void(std::span<const uint8_t>)> send_raw,
|
||||
|
||||
32
firmware/include/parse_buffer.h
Normal file
32
firmware/include/parse_buffer.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
|
||||
class parse_buffer {
|
||||
const uint8_t* m_data;
|
||||
size_t m_remaining;
|
||||
|
||||
public:
|
||||
parse_buffer(std::span<const uint8_t> data)
|
||||
: m_data(data.data()), m_remaining(data.size()) {}
|
||||
|
||||
template <typename T>
|
||||
const T* consume() {
|
||||
if (m_remaining < sizeof(T)) return nullptr;
|
||||
auto* p = reinterpret_cast<const T*>(m_data);
|
||||
m_data += sizeof(T);
|
||||
m_remaining -= sizeof(T);
|
||||
return p;
|
||||
}
|
||||
|
||||
bool skip(size_t len) {
|
||||
if (m_remaining < len) return false;
|
||||
m_data += len;
|
||||
m_remaining -= len;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::span<const uint8_t> remaining() const { return {m_data, m_remaining}; }
|
||||
size_t remaining_size() const { return m_remaining; }
|
||||
};
|
||||
37
firmware/include/prepend_buffer.h
Normal file
37
firmware/include/prepend_buffer.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <span>
|
||||
|
||||
template <size_t N>
|
||||
class prepend_buffer {
|
||||
uint8_t m_buf[N];
|
||||
size_t m_start = N / 2;
|
||||
size_t m_end = N / 2;
|
||||
|
||||
public:
|
||||
template <typename T>
|
||||
T* prepend() {
|
||||
m_start -= sizeof(T);
|
||||
return reinterpret_cast<T*>(m_buf + m_start);
|
||||
}
|
||||
|
||||
uint8_t* append(size_t len) {
|
||||
uint8_t* p = m_buf + m_end;
|
||||
m_end += len;
|
||||
return p;
|
||||
}
|
||||
|
||||
void append_copy(std::span<const uint8_t> data) {
|
||||
memcpy(append(data.size()), data.data(), data.size());
|
||||
}
|
||||
|
||||
uint8_t* payload_ptr() { return m_buf + m_end; }
|
||||
|
||||
uint8_t* data() { return m_buf + m_start; }
|
||||
const uint8_t* data() const { return m_buf + m_start; }
|
||||
size_t size() const { return m_end - m_start; }
|
||||
|
||||
std::span<const uint8_t> span() const { return {data(), size()}; }
|
||||
};
|
||||
29
firmware/include/udp.h
Normal file
29
firmware/include/udp.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
|
||||
namespace udp {
|
||||
|
||||
struct __attribute__((packed)) header {
|
||||
uint16_t src_port;
|
||||
uint16_t dst_port;
|
||||
uint16_t length;
|
||||
uint16_t checksum;
|
||||
};
|
||||
static_assert(sizeof(header) == 8);
|
||||
|
||||
template <typename Buf>
|
||||
void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_mac,
|
||||
ipv4::ip4_addr src_ip, ipv4::ip4_addr dst_ip,
|
||||
uint16_t src_port, uint16_t dst_port,
|
||||
size_t payload_len, uint8_t ttl = 64) {
|
||||
auto* u = buf.template prepend<header>();
|
||||
u->src_port = src_port;
|
||||
u->dst_port = dst_port;
|
||||
u->length = __builtin_bswap16(sizeof(header) + payload_len);
|
||||
u->checksum = 0;
|
||||
ipv4::prepend(buf, dst_mac, src_mac, src_ip, dst_ip, 17, sizeof(header) + payload_len, ttl);
|
||||
}
|
||||
|
||||
} // namespace udp
|
||||
Reference in New Issue
Block a user