extract igmp into its own static library
This commit is contained in:
11
igmp/CMakeLists.txt
Normal file
11
igmp/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
add_library(igmp STATIC igmp.cpp)
|
||||
|
||||
target_include_directories(igmp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_compile_options(igmp PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||
|
||||
target_link_libraries(igmp PUBLIC
|
||||
util
|
||||
eth
|
||||
ipv4
|
||||
)
|
||||
109
igmp/igmp.cpp
Normal file
109
igmp/igmp.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "igmp.h"
|
||||
#include <vector>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "parse_buffer.h"
|
||||
#include "prepend_buffer.h"
|
||||
|
||||
namespace igmp {
|
||||
|
||||
struct group_entry {
|
||||
ipv4::ip4_addr ip;
|
||||
eth::mac_addr mac;
|
||||
};
|
||||
|
||||
static std::vector<group_entry> groups;
|
||||
|
||||
eth::mac_addr mac_for_ip(const ipv4::ip4_addr& group) {
|
||||
return {0x01, 0x00, 0x5E,
|
||||
static_cast<uint8_t>(group[1] & 0x7F), group[2], group[3]};
|
||||
}
|
||||
|
||||
bool is_member(const ipv4::ip4_addr& ip) {
|
||||
if (ip == ALL_HOSTS) return true;
|
||||
for (auto& g : groups)
|
||||
if (g.ip == ip) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_member_mac(const eth::mac_addr& mac) {
|
||||
static constexpr eth::mac_addr ALL_HOSTS_MAC = {0x01, 0x00, 0x5E, 0x00, 0x00, 0x01};
|
||||
if (mac == ALL_HOSTS_MAC) return true;
|
||||
for (auto& g : groups)
|
||||
if (g.mac == mac) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static void send_report(const ipv4::ip4_addr& group) {
|
||||
prepend_buffer<4096> buf;
|
||||
prepend_report(buf, eth::get_mac(), ipv4::get_ip(), group);
|
||||
eth::send_raw(buf.span());
|
||||
}
|
||||
|
||||
void join(const ipv4::ip4_addr& group) {
|
||||
for (auto& g : groups)
|
||||
if (g.ip == group) return;
|
||||
groups.push_back({group, mac_for_ip(group)});
|
||||
send_report(group);
|
||||
}
|
||||
|
||||
void send_all_reports() {
|
||||
for (auto& g : groups)
|
||||
send_report(g.ip);
|
||||
}
|
||||
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
parse_buffer pb(frame);
|
||||
pb.consume<eth::header>();
|
||||
auto* ip = pb.consume<ipv4::header>();
|
||||
if (!ip) return;
|
||||
|
||||
size_t options_len = ip->header_len() - sizeof(ipv4::header);
|
||||
if (options_len > 0 && !pb.skip(options_len)) return;
|
||||
|
||||
auto* msg = pb.consume<message>();
|
||||
if (!msg) return;
|
||||
if (msg->type != 0x11) return;
|
||||
|
||||
if (msg->group == ipv4::ip4_addr{0, 0, 0, 0}) {
|
||||
for (auto& g : groups)
|
||||
send_report(g.ip);
|
||||
} else {
|
||||
for (auto& g : groups) {
|
||||
if (g.ip == msg->group) {
|
||||
send_report(g.ip);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init() {
|
||||
ipv4::register_protocol(2, handle);
|
||||
eth::register_mac_filter(is_member_mac);
|
||||
ipv4::register_addr_filter(is_member);
|
||||
}
|
||||
|
||||
bool parse_report(std::span<const uint8_t> frame, ipv4::ip4_addr& group) {
|
||||
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 != 2) return false;
|
||||
|
||||
size_t options_len = ip->header_len() - sizeof(ipv4::header);
|
||||
if (options_len > 0 && !pb.skip(options_len)) return false;
|
||||
|
||||
auto* msg = pb.consume<message>();
|
||||
if (!msg) return false;
|
||||
if (msg->type != 0x16) return false;
|
||||
|
||||
group = msg->group;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace igmp
|
||||
59
igmp/igmp.h
Normal file
59
igmp/igmp.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "span_writer.h"
|
||||
|
||||
namespace igmp {
|
||||
|
||||
static constexpr ipv4::ip4_addr ALL_HOSTS = {224, 0, 0, 1};
|
||||
|
||||
struct __attribute__((packed)) message {
|
||||
uint8_t type;
|
||||
uint8_t max_resp_time;
|
||||
uint16_t checksum;
|
||||
ipv4::ip4_addr group;
|
||||
};
|
||||
static_assert(sizeof(message) == 8);
|
||||
|
||||
void init();
|
||||
|
||||
eth::mac_addr mac_for_ip(const ipv4::ip4_addr& group);
|
||||
bool is_member(const ipv4::ip4_addr& ip);
|
||||
bool is_member_mac(const eth::mac_addr& mac);
|
||||
|
||||
void join(const ipv4::ip4_addr& group);
|
||||
|
||||
void send_all_reports();
|
||||
|
||||
void handle(std::span<const uint8_t> frame, span_writer& tx);
|
||||
|
||||
template <typename Buf>
|
||||
void prepend_report(Buf& buf, const eth::mac_addr& src_mac, ipv4::ip4_addr src_ip,
|
||||
const ipv4::ip4_addr& group) {
|
||||
auto* m = buf.template prepend<message>();
|
||||
m->type = 0x16;
|
||||
m->max_resp_time = 0;
|
||||
m->checksum = 0;
|
||||
m->group = group;
|
||||
m->checksum = ipv4::checksum(m, sizeof(message));
|
||||
ipv4::prepend(buf, mac_for_ip(group), src_mac, src_ip, group, 2, sizeof(message), 1);
|
||||
}
|
||||
|
||||
template <typename Buf>
|
||||
void prepend_query(Buf& buf, const eth::mac_addr& src_mac, ipv4::ip4_addr src_ip,
|
||||
const ipv4::ip4_addr& group) {
|
||||
ipv4::ip4_addr dst_ip = (group == ipv4::ip4_addr{0, 0, 0, 0}) ? ALL_HOSTS : group;
|
||||
auto* m = buf.template prepend<message>();
|
||||
m->type = 0x11;
|
||||
m->max_resp_time = 100;
|
||||
m->checksum = 0;
|
||||
m->group = group;
|
||||
m->checksum = ipv4::checksum(m, sizeof(message));
|
||||
ipv4::prepend(buf, mac_for_ip(dst_ip), src_mac, src_ip, dst_ip, 2, sizeof(message), 1);
|
||||
}
|
||||
|
||||
bool parse_report(std::span<const uint8_t> frame, ipv4::ip4_addr& group);
|
||||
|
||||
} // namespace igmp
|
||||
Reference in New Issue
Block a user