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:
11
icmp/CMakeLists.txt
Normal file
11
icmp/CMakeLists.txt
Normal file
@@ -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
|
||||
)
|
||||
66
icmp/icmp.cpp
Normal file
66
icmp/icmp.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#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());
|
||||
}
|
||||
|
||||
void init() {
|
||||
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
|
||||
41
icmp/icmp.h
Normal file
41
icmp/icmp.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "span_writer.h"
|
||||
|
||||
namespace icmp {
|
||||
|
||||
struct __attribute__((packed)) echo {
|
||||
uint8_t type;
|
||||
uint8_t code;
|
||||
uint16_t checksum;
|
||||
uint16_t id;
|
||||
uint16_t seq;
|
||||
};
|
||||
static_assert(sizeof(echo) == 8);
|
||||
|
||||
void init();
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx);
|
||||
|
||||
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,
|
||||
size_t payload_len = 0) {
|
||||
auto* e = buf.template prepend<echo>();
|
||||
e->type = 8;
|
||||
e->code = 0;
|
||||
e->checksum = 0;
|
||||
e->id = id;
|
||||
e->seq = seq;
|
||||
size_t icmp_len = sizeof(echo) + payload_len;
|
||||
e->checksum = ipv4::checksum(e, icmp_len);
|
||||
ipv4::prepend(buf, dst_mac, src_mac, src_ip, dst_ip, 1, icmp_len);
|
||||
}
|
||||
|
||||
bool parse_echo_reply(std::span<const uint8_t> frame, ipv4::ip4_addr& src_ip, uint16_t expected_id);
|
||||
|
||||
} // namespace icmp
|
||||
Reference in New Issue
Block a user