Self-registering IP-protocol and UDP-port dispatch; move picomap-specific constants out of generic net/dispatch layers
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
#include "ipv4.h"
|
||||
#include "icmp.h"
|
||||
#include <array>
|
||||
#include "igmp.h"
|
||||
#include "net.h"
|
||||
#include "udp.h"
|
||||
#include "parse_buffer.h"
|
||||
|
||||
namespace ipv4 {
|
||||
@@ -21,12 +20,24 @@ uint16_t checksum(const void* data, size_t len) {
|
||||
return __builtin_bswap16(~sum);
|
||||
}
|
||||
|
||||
static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip) {
|
||||
return dst == our_ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST || igmp::is_member(dst);
|
||||
bool addressed_to_us(ip4_addr dst) {
|
||||
const auto& ns = net_get_state();
|
||||
return dst == ns.ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST || igmp::is_member(dst);
|
||||
}
|
||||
|
||||
struct protocol_entry {
|
||||
uint8_t protocol;
|
||||
protocol_handler fn;
|
||||
};
|
||||
static std::array<protocol_entry, 8> protocol_handlers;
|
||||
static size_t protocol_handler_count = 0;
|
||||
|
||||
void register_protocol(uint8_t protocol, protocol_handler fn) {
|
||||
if (protocol_handler_count < protocol_handlers.size())
|
||||
protocol_handlers[protocol_handler_count++] = {protocol, fn};
|
||||
}
|
||||
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
const auto& ns = net_get_state();
|
||||
parse_buffer pb(frame);
|
||||
pb.consume<eth::header>();
|
||||
auto* ip = pb.consume<header>();
|
||||
@@ -36,20 +47,11 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
size_t options_len = ip->header_len() - sizeof(header);
|
||||
if (options_len > 0 && !pb.skip(options_len)) return;
|
||||
|
||||
switch (ip->protocol) {
|
||||
case 1:
|
||||
if (!ip_match(ip->dst, ns.ip))
|
||||
return;
|
||||
icmp::handle(frame, tx, ns.mac, ns.ip);
|
||||
break;
|
||||
case 2:
|
||||
igmp::handle(frame, tx, ns.mac, ns.ip);
|
||||
break;
|
||||
case 17:
|
||||
if (!ip_match(ip->dst, ns.ip))
|
||||
return;
|
||||
udp::handle(frame, tx);
|
||||
break;
|
||||
for (size_t i = 0; i < protocol_handler_count; i++) {
|
||||
if (protocol_handlers[i].protocol == ip->protocol) {
|
||||
protocol_handlers[i].fn(frame, tx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user