extract icmp into its own static library; replace all __attribute__((constructor)) self-registrations with explicit init() calls in dispatch_init
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "arp.h"
|
||||
#include "icmp.h"
|
||||
#include "igmp.h"
|
||||
#include "udp.h"
|
||||
#include "debug_log.h"
|
||||
@@ -35,10 +36,13 @@ static void on_udp_message(std::span<const uint8_t> payload, const udp::address&
|
||||
|
||||
void dispatch_init(uint16_t port_be) {
|
||||
listen_port_be = port_be;
|
||||
udp::register_port(port_be, on_udp_message);
|
||||
eth::init();
|
||||
ipv4::init();
|
||||
arp::init();
|
||||
icmp::init();
|
||||
igmp::init();
|
||||
udp::init();
|
||||
udp::register_port(port_be, on_udp_message);
|
||||
dispatch_schedule_ms(60000, igmp_reannounce);
|
||||
dlog("dispatch_init complete");
|
||||
}
|
||||
|
||||
67
src/icmp.cpp
67
src/icmp.cpp
@@ -1,67 +0,0 @@
|
||||
#include "icmp.h"
|
||||
#include <cstring>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "parse_buffer.h"
|
||||
#include "prepend_buffer.h"
|
||||
|
||||
namespace icmp {
|
||||
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
parse_buffer pb(frame);
|
||||
auto* eth_hdr = pb.consume<eth::header>();
|
||||
auto* ip = pb.consume<ipv4::header>();
|
||||
if (!ip) return;
|
||||
if (!ipv4::addressed_to_us(ip->dst)) return;
|
||||
|
||||
size_t options_len = ip->header_len() - sizeof(ipv4::header);
|
||||
if (options_len > 0 && !pb.skip(options_len)) return;
|
||||
|
||||
size_t icmp_len = ip->total() - ip->header_len();
|
||||
if (pb.remaining_size() < icmp_len) return;
|
||||
|
||||
auto* icmp_pkt = pb.consume<echo>();
|
||||
if (!icmp_pkt) return;
|
||||
if (icmp_pkt->type != 8) return;
|
||||
|
||||
prepend_buffer<4096> buf;
|
||||
memcpy(buf.append(icmp_len), pb.remaining().data() - sizeof(echo), icmp_len);
|
||||
|
||||
auto* reply = reinterpret_cast<echo*>(buf.data());
|
||||
reply->type = 0;
|
||||
reply->checksum = 0;
|
||||
reply->checksum = ipv4::checksum(reply, icmp_len);
|
||||
|
||||
ipv4::prepend(buf, eth_hdr->src, eth::get_mac(), ipv4::get_ip(), ip->src, 1, icmp_len);
|
||||
eth::send_raw(buf.span());
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void register_protocol() {
|
||||
ipv4::register_protocol(1, handle);
|
||||
}
|
||||
|
||||
bool parse_echo_reply(std::span<const uint8_t> frame, ipv4::ip4_addr& src_ip, uint16_t expected_id) {
|
||||
parse_buffer pb(frame);
|
||||
auto* eth_hdr = pb.consume<eth::header>();
|
||||
if (!eth_hdr) return false;
|
||||
if (eth_hdr->ethertype != eth::ETH_IPV4) return false;
|
||||
|
||||
auto* ip = pb.consume<ipv4::header>();
|
||||
if (!ip) return false;
|
||||
if ((ip->ver_ihl >> 4) != 4) return false;
|
||||
if (ip->protocol != 1) return false;
|
||||
|
||||
size_t options_len = ip->header_len() - sizeof(ipv4::header);
|
||||
if (options_len > 0 && !pb.skip(options_len)) return false;
|
||||
|
||||
auto* icmp_pkt = pb.consume<echo>();
|
||||
if (!icmp_pkt) return false;
|
||||
if (icmp_pkt->type != 0) return false;
|
||||
if (icmp_pkt->id != expected_id) return false;
|
||||
|
||||
src_ip = ip->src;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace icmp
|
||||
@@ -78,8 +78,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void register_protocol() {
|
||||
void init() {
|
||||
ipv4::register_protocol(2, handle);
|
||||
eth::register_mac_filter(is_member_mac);
|
||||
ipv4::register_addr_filter(is_member);
|
||||
|
||||
@@ -49,8 +49,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void register_protocol() {
|
||||
void init() {
|
||||
ipv4::register_protocol(17, handle);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user