extract arp into its own static library; add empty arp::init() called from dispatch_init to anchor the .o so the self-registration constructor links in
This commit is contained in:
11
arp/CMakeLists.txt
Normal file
11
arp/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
add_library(arp STATIC arp.cpp)
|
||||
|
||||
target_include_directories(arp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_compile_options(arp PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||
|
||||
target_link_libraries(arp PUBLIC
|
||||
util
|
||||
eth
|
||||
ipv4
|
||||
)
|
||||
51
arp/arp.cpp
Normal file
51
arp/arp.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "arp.h"
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "parse_buffer.h"
|
||||
#include "prepend_buffer.h"
|
||||
|
||||
namespace arp {
|
||||
|
||||
static constexpr uint16_t ARP_HTYPE_ETH = __builtin_bswap16(1);
|
||||
static constexpr uint16_t ARP_PTYPE_IPV4 = __builtin_bswap16(0x0800);
|
||||
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) {
|
||||
const auto& mac = eth::get_mac();
|
||||
const auto& ip = ipv4::get_ip();
|
||||
parse_buffer pb(frame);
|
||||
pb.consume<eth::header>();
|
||||
auto* arp_hdr = pb.consume<header>();
|
||||
if (!arp_hdr) return;
|
||||
|
||||
if (arp_hdr->htype != ARP_HTYPE_ETH) return;
|
||||
if (arp_hdr->ptype != ARP_PTYPE_IPV4) return;
|
||||
if (arp_hdr->hlen != 6 || arp_hdr->plen != 4) return;
|
||||
if (arp_hdr->oper != ARP_OP_REQUEST) return;
|
||||
if (arp_hdr->tpa != ip) return;
|
||||
|
||||
prepend_buffer<4096> buf;
|
||||
auto* reply = buf.template prepend<header>();
|
||||
reply->htype = ARP_HTYPE_ETH;
|
||||
reply->ptype = ARP_PTYPE_IPV4;
|
||||
reply->hlen = 6;
|
||||
reply->plen = 4;
|
||||
reply->oper = ARP_OP_REPLY;
|
||||
reply->sha = mac;
|
||||
reply->spa = ip;
|
||||
reply->tha = arp_hdr->sha;
|
||||
reply->tpa = arp_hdr->spa;
|
||||
eth::prepend(buf, arp_hdr->sha, mac, eth::ETH_ARP);
|
||||
|
||||
eth::send_raw(buf.span());
|
||||
}
|
||||
|
||||
void init() {}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void register_ethertype() {
|
||||
eth::register_ethertype(eth::ETH_ARP, handle);
|
||||
}
|
||||
|
||||
} // namespace arp
|
||||
25
arp/arp.h
Normal file
25
arp/arp.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include <span>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "span_writer.h"
|
||||
|
||||
namespace arp {
|
||||
|
||||
struct __attribute__((packed)) header {
|
||||
uint16_t htype;
|
||||
uint16_t ptype;
|
||||
uint8_t hlen;
|
||||
uint8_t plen;
|
||||
uint16_t oper;
|
||||
eth::mac_addr sha;
|
||||
ipv4::ip4_addr spa;
|
||||
eth::mac_addr tha;
|
||||
ipv4::ip4_addr tpa;
|
||||
};
|
||||
static_assert(sizeof(header) == 28);
|
||||
|
||||
void init();
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx);
|
||||
|
||||
} // namespace arp
|
||||
Reference in New Issue
Block a user