Add IGMP, prepend_buffer/parse_buffer, split UDP header, discovery tests, test all
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "ipv4.h"
|
||||
#include "icmp.h"
|
||||
#include "igmp.h"
|
||||
#include "parse_buffer.h"
|
||||
|
||||
namespace ipv4 {
|
||||
|
||||
@@ -17,26 +19,34 @@ uint16_t checksum(const void* data, size_t len) {
|
||||
return __builtin_bswap16(~sum);
|
||||
}
|
||||
|
||||
static bool ip_match_or_broadcast(const ip4_addr& dst, const ip4_addr& our_ip, const ip4_addr& subnet_broadcast) {
|
||||
return dst == our_ip || dst == IP_BROADCAST_ALL || dst == subnet_broadcast;
|
||||
static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip, const ip4_addr& subnet_broadcast) {
|
||||
return dst == our_ip || dst == IP_BROADCAST_ALL || dst == subnet_broadcast || igmp::is_member(dst);
|
||||
}
|
||||
|
||||
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) {
|
||||
if (frame.size() < sizeof(header)) return;
|
||||
auto& ip = *reinterpret_cast<const header*>(frame.data());
|
||||
if ((ip.ver_ihl >> 4) != 4) return;
|
||||
parse_buffer pb(frame);
|
||||
pb.consume<eth::header>();
|
||||
auto* ip = pb.consume<header>();
|
||||
if (!ip) return;
|
||||
if ((ip->ver_ihl >> 4) != 4) return;
|
||||
|
||||
switch (ip.protocol) {
|
||||
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_or_broadcast(ip.dst, our_ip, subnet_broadcast))
|
||||
if (!ip_match(ip->dst, our_ip, subnet_broadcast))
|
||||
return;
|
||||
icmp::handle(frame, tx, our_mac, our_ip, send_raw);
|
||||
break;
|
||||
case 2:
|
||||
igmp::handle(frame, tx, our_mac, our_ip, send_raw);
|
||||
break;
|
||||
case 17:
|
||||
if (!ip_match_or_broadcast(ip.dst, our_ip, subnet_broadcast))
|
||||
if (!ip_match(ip->dst, our_ip, subnet_broadcast))
|
||||
return;
|
||||
handle_udp(frame, tx);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user