Drop send_raw parameter threading: arp/icmp/igmp/ipv4 call net_send_raw directly
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "arp.h"
|
||||
#include "net.h"
|
||||
#include "parse_buffer.h"
|
||||
#include "prepend_buffer.h"
|
||||
|
||||
@@ -10,8 +11,7 @@ static constexpr uint16_t ARP_OP_REQUEST = __builtin_bswap16(1);
|
||||
static constexpr uint16_t ARP_OP_REPLY = __builtin_bswap16(2);
|
||||
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip,
|
||||
std::function<void(std::span<const uint8_t>)> send_raw) {
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
|
||||
parse_buffer pb(frame);
|
||||
pb.consume<eth::header>();
|
||||
auto* arp_hdr = pb.consume<header>();
|
||||
@@ -36,7 +36,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
reply->tpa = arp_hdr->spa;
|
||||
eth::prepend(buf, arp_hdr->sha, our_mac, eth::ETH_ARP);
|
||||
|
||||
send_raw(buf.span());
|
||||
net_send_raw(buf.span());
|
||||
}
|
||||
|
||||
} // namespace arp
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "dispatch.h"
|
||||
#include <unordered_map>
|
||||
#include "pico/stdlib.h"
|
||||
#include "wire.h"
|
||||
#include "timer_queue.h"
|
||||
@@ -12,7 +11,7 @@ static timer_queue timers;
|
||||
|
||||
static void igmp_reannounce() {
|
||||
auto& ns = net_get_state();
|
||||
igmp::send_all_reports(ns.mac, ns.ip, net_send_raw);
|
||||
igmp::send_all_reports(ns.mac, ns.ip);
|
||||
dispatch_schedule_ms(60000, igmp_reannounce);
|
||||
}
|
||||
|
||||
@@ -33,7 +32,7 @@ bool dispatch_cancel_timer(timer_handle h) {
|
||||
}
|
||||
|
||||
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers) {
|
||||
std::unordered_map<int8_t, handler_fn> handler_map;
|
||||
std::array<handler_fn, 128> handler_map{};
|
||||
for (auto& entry : handlers) {
|
||||
handler_map[entry.type_id] = entry.handle;
|
||||
}
|
||||
@@ -41,13 +40,12 @@ bool dispatch_cancel_timer(timer_handle h) {
|
||||
static std::array<uint8_t, 4096> tx_buf;
|
||||
|
||||
auto dispatch_msg = [&](const DecodedMessage& msg, send_fn send) {
|
||||
auto it = handler_map.find(msg.type_id);
|
||||
if (it == handler_map.end()) {
|
||||
if (msg.type_id < 0 || !handler_map[msg.type_id]) {
|
||||
dlogf("dispatch: unknown type_id %d", msg.type_id);
|
||||
return;
|
||||
}
|
||||
responder resp{msg.message_id, std::move(send)};
|
||||
it->second(resp, msg.payload);
|
||||
handler_map[msg.type_id](resp, msg.payload);
|
||||
};
|
||||
|
||||
net_set_handler([&](std::span<const uint8_t> payload,
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#include "icmp.h"
|
||||
#include <cstring>
|
||||
#include "ipv4.h"
|
||||
#include "net.h"
|
||||
#include "parse_buffer.h"
|
||||
#include "prepend_buffer.h"
|
||||
|
||||
namespace icmp {
|
||||
|
||||
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) {
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
|
||||
parse_buffer pb(frame);
|
||||
auto* eth_hdr = pb.consume<eth::header>();
|
||||
auto* ip = pb.consume<ipv4::header>();
|
||||
@@ -34,7 +34,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
reply->checksum = ipv4::checksum(reply, icmp_len);
|
||||
|
||||
ipv4::prepend(buf, eth_hdr->src, our_mac, our_ip, ip->src, 1, icmp_len);
|
||||
send_raw(buf.span());
|
||||
net_send_raw(buf.span());
|
||||
}
|
||||
|
||||
bool parse_echo_reply(std::span<const uint8_t> frame, ipv4::ip4_addr& src_ip, uint16_t expected_id) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "igmp.h"
|
||||
#include <vector>
|
||||
#include "ipv4.h"
|
||||
#include "net.h"
|
||||
#include "parse_buffer.h"
|
||||
#include "prepend_buffer.h"
|
||||
|
||||
@@ -34,31 +35,27 @@ bool is_member_mac(const eth::mac_addr& mac) {
|
||||
}
|
||||
|
||||
static void send_report(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) {
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
|
||||
prepend_buffer<4096> buf;
|
||||
prepend_report(buf, our_mac, our_ip, group);
|
||||
send_raw(buf.span());
|
||||
net_send_raw(buf.span());
|
||||
}
|
||||
|
||||
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) {
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
|
||||
for (auto& g : groups)
|
||||
if (g.ip == group) return;
|
||||
groups.push_back({group, mac_for_ip(group)});
|
||||
send_report(group, our_mac, our_ip, send_raw);
|
||||
send_report(group, our_mac, our_ip);
|
||||
}
|
||||
|
||||
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 send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
|
||||
for (auto& g : groups)
|
||||
send_report(g.ip, our_mac, our_ip, send_raw);
|
||||
send_report(g.ip, our_mac, our_ip);
|
||||
}
|
||||
|
||||
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) {
|
||||
eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
|
||||
parse_buffer pb(frame);
|
||||
pb.consume<eth::header>();
|
||||
auto* ip = pb.consume<ipv4::header>();
|
||||
@@ -73,11 +70,11 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
|
||||
if (msg->group == ipv4::ip4_addr{0, 0, 0, 0}) {
|
||||
for (auto& g : groups)
|
||||
send_report(g.ip, our_mac, our_ip, send_raw);
|
||||
send_report(g.ip, our_mac, our_ip);
|
||||
} else {
|
||||
for (auto& g : groups) {
|
||||
if (g.ip == msg->group) {
|
||||
send_report(g.ip, our_mac, our_ip, send_raw);
|
||||
send_report(g.ip, our_mac, our_ip);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip, const ip4_addr
|
||||
|
||||
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,
|
||||
std::function<void(std::span<const uint8_t>, span_writer&)> handle_udp) {
|
||||
parse_buffer pb(frame);
|
||||
pb.consume<eth::header>();
|
||||
@@ -40,10 +39,10 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
|
||||
case 1:
|
||||
if (!ip_match(ip->dst, our_ip, subnet_broadcast))
|
||||
return;
|
||||
icmp::handle(frame, tx, our_mac, our_ip, send_raw);
|
||||
icmp::handle(frame, tx, our_mac, our_ip);
|
||||
break;
|
||||
case 2:
|
||||
igmp::handle(frame, tx, our_mac, our_ip, send_raw);
|
||||
igmp::handle(frame, tx, our_mac, our_ip);
|
||||
break;
|
||||
case 17:
|
||||
if (!ip_match(ip->dst, our_ip, subnet_broadcast))
|
||||
|
||||
@@ -81,10 +81,10 @@ static void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
|
||||
switch (eth_hdr.ethertype) {
|
||||
case eth::ETH_ARP:
|
||||
arp::handle(frame, tx, state.mac, state.ip, net_send_raw);
|
||||
arp::handle(frame, tx, state.mac, state.ip);
|
||||
break;
|
||||
case eth::ETH_IPV4:
|
||||
ipv4::handle(frame, tx, state.mac, state.ip, IP_BROADCAST_SUBNET, net_send_raw, handle_udp);
|
||||
ipv4::handle(frame, tx, state.mac, state.ip, IP_BROADCAST_SUBNET, handle_udp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -112,7 +112,7 @@ bool net_init() {
|
||||
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
|
||||
w6300::set_interrupt_mask(w6300::ik_sock_0);
|
||||
|
||||
igmp::join(igmp::PICOMAP_DISCOVERY_GROUP, state.mac, state.ip, net_send_raw);
|
||||
igmp::join(igmp::PICOMAP_DISCOVERY_GROUP, state.mac, state.ip);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user