mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-22 23:10:38 +00:00
AutoInterface: carrier-change handling, configurable hops, port-collision guard
This commit is contained in:
1 parent
670820a41e
commit
858010cee0
4 files changed
+494
-17
No files matched your search
@@ -47,13 +47,17 @@ namespace RNS {
|
||||
char scope,
|
||||
uint16_t discovery_port,
|
||||
uint16_t data_port,
|
||||
uint8_t max_peers)
|
||||
uint8_t max_peers,
|
||||
uint8_t multicast_hops)
|
||||
: _addr_type(addr_type),
|
||||
_scope(scope),
|
||||
_discovery_port(discovery_port),
|
||||
_unicast_disco_port(discovery_port + 1),
|
||||
_data_port(data_port),
|
||||
_max_peers(max_peers)
|
||||
_max_peers(max_peers),
|
||||
_multicast_hops(multicast_hops != 0
|
||||
? multicast_hops
|
||||
: (scope == SCOPE_LINK ? 1 : 32))
|
||||
{
|
||||
_name = name ? name : "AutoInterface";
|
||||
_group_id_bytes = Bytes(group_id);
|
||||
@@ -89,19 +93,72 @@ namespace RNS {
|
||||
}
|
||||
|
||||
void AutoInterface::set_link_local(const std::string& addr_str, uint32_t scope_id) {
|
||||
_scope_id = scope_id;
|
||||
notify_link_change(addr_str, scope_id);
|
||||
}
|
||||
|
||||
void AutoInterface::notify_link_change(const std::string& addr_str,
|
||||
uint32_t scope_id) {
|
||||
// Re-canonicalize so the stored form is the lowercase RFC 5952 form
|
||||
// regardless of whether the caller passed an expanded
|
||||
// "fe80:0000:..." form (Arduino IPv6Address::toString) or
|
||||
// uppercase from lwIP. Peers compute their expected self-token by
|
||||
// hashing what they read off recvfrom + inet_ntop, which on every
|
||||
// other implementation is lowercase canonical.
|
||||
struct in6_addr tmp;
|
||||
if (inet_pton(AF_INET6, addr_str.c_str(), &tmp) == 1) {
|
||||
std::memcpy(_self_link_local_bin.data(), &tmp, 16);
|
||||
const std::string canon = canonicalize_ipv6(addr_str);
|
||||
if (canon == _link_local_addr && scope_id == _scope_id) {
|
||||
return; // idempotent no-op
|
||||
}
|
||||
_link_local_addr = canonicalize_ipv6(addr_str);
|
||||
|
||||
struct in6_addr new_bin{};
|
||||
if (inet_pton(AF_INET6, canon.c_str(), &new_bin) != 1) {
|
||||
WARNINGF("AutoInterface::notify_link_change: invalid address '%s'",
|
||||
addr_str.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
// Remember the old link-local for self-echo suppression. Late
|
||||
// multicast loopbacks of our previous announces would otherwise
|
||||
// pass token validation (their token was hashed from our prior
|
||||
// address) and get added as a "peer" pointing back at us.
|
||||
if (!_link_local_addr.empty()) {
|
||||
_self_link_local_history.push_front(_self_link_local_bin);
|
||||
while (_self_link_local_history.size() > SELF_LL_HISTORY_MAX) {
|
||||
_self_link_local_history.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
std::memcpy(_self_link_local_bin.data(), &new_bin, 16);
|
||||
_link_local_addr = canon;
|
||||
_self_disco_token = compute_discovery_token(_group_id_bytes, _link_local_addr);
|
||||
|
||||
const uint32_t prev_scope_id = _scope_id;
|
||||
_scope_id = scope_id;
|
||||
|
||||
if (_online && _disco_sock >= 0 && prev_scope_id != scope_id) {
|
||||
// Leave the old multicast membership on the previous ifindex
|
||||
// and rejoin on the new one. Both calls are best-effort:
|
||||
// failure here is no worse than the start-time best-effort
|
||||
// join (see open_sockets()), and we don't want a transient
|
||||
// network blip to take the whole interface offline.
|
||||
struct ipv6_mreq leave{};
|
||||
std::memcpy(&leave.ipv6mr_multiaddr, _mcast_addr_bin.data(), 16);
|
||||
leave.ipv6mr_interface = prev_scope_id;
|
||||
::setsockopt(_disco_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
|
||||
&leave, sizeof(leave));
|
||||
|
||||
const unsigned int idx = static_cast<unsigned int>(scope_id);
|
||||
::setsockopt(_disco_sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
|
||||
&idx, sizeof(idx));
|
||||
|
||||
struct ipv6_mreq join{};
|
||||
std::memcpy(&join.ipv6mr_multiaddr, _mcast_addr_bin.data(), 16);
|
||||
join.ipv6mr_interface = idx;
|
||||
if (::setsockopt(_disco_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
|
||||
&join, sizeof(join)) != 0) {
|
||||
WARNINGF("AutoInterface::notify_link_change: rejoin on "
|
||||
"scope=%u failed (errno=%d)", (unsigned)scope_id, errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool AutoInterface::start() {
|
||||
@@ -110,6 +167,14 @@ namespace RNS {
|
||||
WARNING("AutoInterface::start: no link-local address configured");
|
||||
return false;
|
||||
}
|
||||
if (_data_port == _unicast_disco_port || _data_port == _discovery_port) {
|
||||
WARNINGF("AutoInterface::start: data_port (%u) collides with "
|
||||
"discovery_port (%u) or unicast_disco_port (%u); "
|
||||
"pick data_port at least 2 away from discovery_port",
|
||||
(unsigned)_data_port, (unsigned)_discovery_port,
|
||||
(unsigned)_unicast_disco_port);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compute group hash + multicast address now (cheap; idempotent).
|
||||
_group_hash = compute_group_hash(_group_id_bytes);
|
||||
@@ -360,7 +425,7 @@ namespace RNS {
|
||||
|
||||
bool AutoInterface::open_sockets() {
|
||||
const int yes = 1;
|
||||
const int hops = 1;
|
||||
const int hops = static_cast<int>(_multicast_hops);
|
||||
const unsigned int ifindex = static_cast<unsigned int>(_scope_id);
|
||||
|
||||
// --- Multicast discovery socket (RX + TX announces) ---
|
||||
@@ -381,7 +446,8 @@ namespace RNS {
|
||||
// IPV6_MULTICAST_LOOP: receive our own multicast frames so we can detect
|
||||
// the multicast echo (matches Python AutoInterface lines 451-464).
|
||||
::setsockopt(_disco_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes));
|
||||
// IPV6_MULTICAST_HOPS = 1 — link-local only, never leak.
|
||||
// IPV6_MULTICAST_HOPS — defaults to 1 for SCOPE_LINK (never leaks)
|
||||
// and 32 for wider scopes; user can override via the constructor.
|
||||
::setsockopt(_disco_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops));
|
||||
if (!bind_any(_disco_sock, _discovery_port)) {
|
||||
WARNING("AutoInterface: bind(disco) failed");
|
||||
@@ -496,8 +562,21 @@ namespace RNS {
|
||||
if (n <= 0) break;
|
||||
if (!_final_init_done) continue;
|
||||
|
||||
// Self-echo: confirms multicast loopback works (carrier).
|
||||
if (std::memcmp(&src.sin6_addr, _self_link_local_bin.data(), 16) == 0) {
|
||||
// Self-echo: confirms multicast loopback works (carrier). Also
|
||||
// suppress echoes of our previously-rotated link-locals so a
|
||||
// late loopback after notify_link_change() doesn't accidentally
|
||||
// peer us with our former self.
|
||||
bool self_echo = std::memcmp(&src.sin6_addr,
|
||||
_self_link_local_bin.data(), 16) == 0;
|
||||
if (!self_echo) {
|
||||
for (const auto& prev : _self_link_local_history) {
|
||||
if (std::memcmp(&src.sin6_addr, prev.data(), 16) == 0) {
|
||||
self_echo = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (self_echo) {
|
||||
_last_mcast_echo_ms = static_cast<uint64_t>(Utilities::OS::ltime());
|
||||
_has_mcast_echo = true;
|
||||
continue;
|
||||
|
||||
@@ -39,7 +39,33 @@ namespace RNS {
|
||||
* - One parent AutoInterface holds the multicast/unicast/data sockets.
|
||||
* - One AutoInterfacePeer (registered with Transport) per peer.
|
||||
* - Single-threaded, cooperative: loop() polls non-blocking sockets.
|
||||
* - IFAC: not implemented in v1 (see plan).
|
||||
*
|
||||
* Limitations vs the Python reference:
|
||||
* - Single-NIC only. v1 does not enumerate system interfaces or take
|
||||
* `devices` / `ignored_devices`; the host injects exactly one
|
||||
* link-local address via set_link_local() / notify_link_change().
|
||||
* - IFAC keying not implemented. A Python AutoInterface configured
|
||||
* with `ifac_netname` / `ifac_netkey` is not interoperable with this
|
||||
* implementation.
|
||||
* - The host must call notify_link_change() on Wi-Fi reassociation
|
||||
* (or any event that rotates the link-local address or interface
|
||||
* index). Python's peer_jobs() polls for this every 4 s; the C++
|
||||
* port is event-driven to keep the MCU loop hot path empty.
|
||||
* - multicast_hops defaults to 1 for SCOPE_LINK and 32 for wider
|
||||
* scopes. Crossing routers also requires MLD/router config beyond
|
||||
* setting IPV6_MULTICAST_HOPS — same caveat as Python.
|
||||
*
|
||||
* Host integration recipe:
|
||||
* 1. Resolve the link-local: WiFi.linkLocalIPv6().toString() (Arduino)
|
||||
* or getifaddrs() (POSIX).
|
||||
* 2. Resolve the interface index: esp_netif_get_netif_impl_index(...)
|
||||
* (Arduino) or if_nametoindex(...) (POSIX).
|
||||
* 3. Construct AutoInterface, set_link_local(addr, scope_id), start().
|
||||
* 4. Register the parent with Transport::register_interface() — the
|
||||
* library only registers spawned per-peer wrappers, not the parent.
|
||||
* 5. Pump loop() from your main event loop (≤100 ms cadence).
|
||||
* 6. On Wi-Fi reassociation, call notify_link_change(new_addr,
|
||||
* new_scope_id) — no need to stop()/start().
|
||||
*/
|
||||
class AutoInterface : public InterfaceImpl {
|
||||
public:
|
||||
@@ -70,13 +96,17 @@ namespace RNS {
|
||||
|
||||
using IPv6Addr = std::array<uint8_t, 16>;
|
||||
|
||||
// multicast_hops = 0 → auto: 1 for SCOPE_LINK, 32 otherwise. Larger
|
||||
// scopes (admin/site/org/global) also need router/MLD config to
|
||||
// actually forward; this only sets the IPV6_MULTICAST_HOPS sockopt.
|
||||
AutoInterface(const char* name = "AutoInterface",
|
||||
const char* group_id = DEFAULT_GROUP_ID,
|
||||
char addr_type = MCAST_ADDR_TYPE_TEMPORARY,
|
||||
char scope = SCOPE_LINK,
|
||||
uint16_t discovery_port = DEFAULT_DISCOVERY_PORT,
|
||||
uint16_t data_port = DEFAULT_DATA_PORT,
|
||||
uint8_t max_peers = RNS_AUTOIFACE_MAX_PEERS_DEFAULT);
|
||||
uint8_t max_peers = RNS_AUTOIFACE_MAX_PEERS_DEFAULT,
|
||||
uint8_t multicast_hops = 0);
|
||||
virtual ~AutoInterface();
|
||||
|
||||
// InterfaceImpl overrides
|
||||
@@ -88,17 +118,30 @@ namespace RNS {
|
||||
// Diagnostics / UI surface
|
||||
size_t peer_count() const { return _peers.size(); }
|
||||
bool has_carrier() const { return _has_mcast_echo; }
|
||||
uint8_t multicast_hops() const { return _multicast_hops; }
|
||||
const std::string& multicast_address() const { return _mcast_addr_str; }
|
||||
const std::string& link_local_address() const { return _link_local_addr; }
|
||||
bool has_peer(const IPv6Addr& addr) const;
|
||||
uint64_t peer_last_heard_ms(const IPv6Addr& addr) const;
|
||||
|
||||
// Inject the local link-local address + interface scope id.
|
||||
// Must be called before start(). Production code resolves these from
|
||||
// WiFi.linkLocalIPv6() + esp_netif_get_netif_impl_index() (Arduino) or
|
||||
// getifaddrs() + if_nametoindex() (native).
|
||||
// Production code resolves these from WiFi.linkLocalIPv6() +
|
||||
// esp_netif_get_netif_impl_index() (Arduino) or getifaddrs() +
|
||||
// if_nametoindex() (native). May be called any time — internally
|
||||
// dispatches to notify_link_change().
|
||||
void set_link_local(const std::string& addr_str, uint32_t scope_id);
|
||||
|
||||
// Notify the library that the host's link-local address and/or
|
||||
// interface scope id has changed (e.g. Wi-Fi reassociation, DHCPv6
|
||||
// PD reissue). Recomputes the announce token so peers continue to
|
||||
// validate us, rotates the previous link-local into the self-echo
|
||||
// filter so late multicast loopback echoes don't accidentally peer
|
||||
// us with our former self, and (if scope_id changed while online)
|
||||
// leaves the old IPV6 multicast group and rejoins on the new
|
||||
// interface index. Idempotent: a no-op if (addr, scope) match
|
||||
// the current values. Safe to call before start().
|
||||
void notify_link_change(const std::string& addr_str, uint32_t scope_id);
|
||||
|
||||
// Diagnostic FD accessors — returns -1 when closed.
|
||||
int discovery_socket_fd() const { return _disco_sock; }
|
||||
int unicast_socket_fd() const { return _unicast_sock; }
|
||||
@@ -109,6 +152,10 @@ namespace RNS {
|
||||
void test_inject_peer(const IPv6Addr& addr, uint32_t scope_id, uint64_t now_ms);
|
||||
void test_run_peer_jobs(uint64_t now_ms);
|
||||
void test_loop_at(uint64_t now_ms);
|
||||
size_t test_self_link_local_history_size() const {
|
||||
return _self_link_local_history.size();
|
||||
}
|
||||
const Bytes& test_self_disco_token() const { return _self_disco_token; }
|
||||
|
||||
// Drive a single iteration with an injected clock. Production code
|
||||
// uses the no-arg loop() which calls this with OS::ltime().
|
||||
@@ -157,6 +204,7 @@ namespace RNS {
|
||||
uint16_t _unicast_disco_port;
|
||||
uint16_t _data_port;
|
||||
uint8_t _max_peers;
|
||||
uint8_t _multicast_hops;
|
||||
std::string _mcast_addr_str;
|
||||
IPv6Addr _mcast_addr_bin{};
|
||||
|
||||
@@ -170,6 +218,12 @@ namespace RNS {
|
||||
IPv6Addr _self_link_local_bin{};
|
||||
uint32_t _scope_id = 0;
|
||||
Bytes _self_disco_token;
|
||||
// Recently rotated link-local addresses kept for self-echo
|
||||
// suppression after notify_link_change(). Bounded at 4 — entries
|
||||
// age out implicitly via the 22 s peer timeout if anything slips
|
||||
// through.
|
||||
std::deque<IPv6Addr> _self_link_local_history;
|
||||
static constexpr size_t SELF_LL_HISTORY_MAX = 4;
|
||||
|
||||
// Peer table — keyed by 16-byte address (as std::string for cheap lookup)
|
||||
struct PeerEntry {
|
||||
|
||||
@@ -258,8 +258,10 @@ void test_peer_jobs_keeps_recent_peers() {
|
||||
// --- Socket lifecycle (native impl). Uses ephemeral high ports to avoid
|
||||
// colliding with any system AutoInterface listener on the host.
|
||||
|
||||
// Discovery and data must be ≥ 2 apart so the auto-derived
|
||||
// unicast_disco_port (= discovery + 1) doesn't collide with data.
|
||||
static const uint16_t kTestDiscoPort = 49716;
|
||||
static const uint16_t kTestDataPort = 49717;
|
||||
static const uint16_t kTestDataPort = 49720;
|
||||
|
||||
void test_start_without_link_local_fails() {
|
||||
AutoInterface ai("test_lifecycle", "reticulum",
|
||||
@@ -274,6 +276,127 @@ void test_start_without_link_local_fails() {
|
||||
TEST_ASSERT_EQUAL_INT(-1, ai.data_socket_fd());
|
||||
}
|
||||
|
||||
void test_notify_link_change_recomputes_token() {
|
||||
AutoInterface ai("nlc_token", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
29716, 42671, 4);
|
||||
|
||||
ai.set_link_local("fe80::abcd:1234", 1);
|
||||
Bytes t1 = ai.test_self_disco_token();
|
||||
TEST_ASSERT_EQUAL_size_t(32, t1.size());
|
||||
TEST_ASSERT_EQUAL_STRING(kTokenReticulumFe80AbcdHex, t1.toHex().c_str());
|
||||
|
||||
ai.notify_link_change("fe80::1", 1);
|
||||
Bytes t2 = ai.test_self_disco_token();
|
||||
TEST_ASSERT_EQUAL_size_t(32, t2.size());
|
||||
TEST_ASSERT_EQUAL_STRING(kTokenReticulumFe80OneHex, t2.toHex().c_str());
|
||||
TEST_ASSERT_TRUE(t1 != t2);
|
||||
}
|
||||
|
||||
void test_notify_link_change_remembers_old_for_self_echo() {
|
||||
AutoInterface ai("nlc_history", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
29716, 42671, 4);
|
||||
|
||||
// First call: no prior address, history stays empty.
|
||||
ai.set_link_local("fe80::abcd:1234", 1);
|
||||
TEST_ASSERT_EQUAL_size_t(0, ai.test_self_link_local_history_size());
|
||||
|
||||
// Second call: prior address is rotated into history.
|
||||
ai.notify_link_change("fe80::1", 1);
|
||||
TEST_ASSERT_EQUAL_size_t(1, ai.test_self_link_local_history_size());
|
||||
|
||||
// Three more rotations — history caps at SELF_LL_HISTORY_MAX (4).
|
||||
ai.notify_link_change("fe80::2", 1);
|
||||
ai.notify_link_change("fe80::3", 1);
|
||||
ai.notify_link_change("fe80::4", 1);
|
||||
ai.notify_link_change("fe80::5", 1);
|
||||
TEST_ASSERT_EQUAL_size_t(4, ai.test_self_link_local_history_size());
|
||||
}
|
||||
|
||||
void test_notify_link_change_idempotent() {
|
||||
AutoInterface ai("nlc_idem", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
29716, 42671, 4);
|
||||
|
||||
ai.set_link_local("fe80::abcd:1234", 1);
|
||||
TEST_ASSERT_EQUAL_size_t(0, ai.test_self_link_local_history_size());
|
||||
|
||||
// Same args → no-op, history must not grow.
|
||||
ai.notify_link_change("fe80::abcd:1234", 1);
|
||||
TEST_ASSERT_EQUAL_size_t(0, ai.test_self_link_local_history_size());
|
||||
|
||||
// Different canonical form of the same address (uppercase) → still no-op.
|
||||
ai.notify_link_change("FE80::ABCD:1234", 1);
|
||||
TEST_ASSERT_EQUAL_size_t(0, ai.test_self_link_local_history_size());
|
||||
}
|
||||
|
||||
void test_notify_link_change_rejects_invalid_address() {
|
||||
AutoInterface ai("nlc_bad", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
29716, 42671, 4);
|
||||
|
||||
ai.set_link_local("fe80::1", 1);
|
||||
Bytes before = ai.test_self_disco_token();
|
||||
|
||||
// Garbage input — must not mutate token or history.
|
||||
ai.notify_link_change("not-an-address", 2);
|
||||
TEST_ASSERT_TRUE(before == ai.test_self_disco_token());
|
||||
TEST_ASSERT_EQUAL_size_t(0, ai.test_self_link_local_history_size());
|
||||
}
|
||||
|
||||
void test_constructor_default_hops_link_scope() {
|
||||
// SCOPE_LINK with hops=0 → auto-default 1 (link-local only).
|
||||
AutoInterface ai("hops_link", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
29716, 42671, 4);
|
||||
TEST_ASSERT_EQUAL_UINT8(1, ai.multicast_hops());
|
||||
}
|
||||
|
||||
void test_constructor_default_hops_admin_scope() {
|
||||
// SCOPE_ADMIN with hops=0 → auto-default 32 (cross-router-ready).
|
||||
AutoInterface ai("hops_admin", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_ADMIN,
|
||||
29716, 42671, 4);
|
||||
TEST_ASSERT_EQUAL_UINT8(32, ai.multicast_hops());
|
||||
}
|
||||
|
||||
void test_constructor_explicit_hops_overrides_default() {
|
||||
AutoInterface ai("hops_explicit", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
29716, 42671, 4, /*multicast_hops=*/16);
|
||||
TEST_ASSERT_EQUAL_UINT8(16, ai.multicast_hops());
|
||||
}
|
||||
|
||||
void test_start_rejects_colliding_data_port() {
|
||||
// data_port == unicast_disco_port (= discovery_port + 1). Without the
|
||||
// guard, both sockets bind via SO_REUSEPORT and silently steal each
|
||||
// other's packets.
|
||||
AutoInterface ai_collide_unicast("test_collide_unicast", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
29716, 29717, 4);
|
||||
ai_collide_unicast.set_link_local("fe80::1", 1);
|
||||
TEST_ASSERT_FALSE(ai_collide_unicast.start());
|
||||
TEST_ASSERT_EQUAL_INT(-1, ai_collide_unicast.discovery_socket_fd());
|
||||
|
||||
// data_port == discovery_port (the multicast socket itself).
|
||||
AutoInterface ai_collide_disco("test_collide_disco", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
29716, 29716, 4);
|
||||
ai_collide_disco.set_link_local("fe80::1", 1);
|
||||
TEST_ASSERT_FALSE(ai_collide_disco.start());
|
||||
TEST_ASSERT_EQUAL_INT(-1, ai_collide_disco.discovery_socket_fd());
|
||||
}
|
||||
|
||||
void test_start_opens_three_sockets() {
|
||||
AutoInterface ai("test_lifecycle", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
@@ -438,7 +561,15 @@ int runUnityTests(void) {
|
||||
RUN_TEST(test_add_peer_respects_max_peers_cap);
|
||||
RUN_TEST(test_peer_jobs_evicts_timed_out_peers);
|
||||
RUN_TEST(test_peer_jobs_keeps_recent_peers);
|
||||
RUN_TEST(test_notify_link_change_recomputes_token);
|
||||
RUN_TEST(test_notify_link_change_remembers_old_for_self_echo);
|
||||
RUN_TEST(test_notify_link_change_idempotent);
|
||||
RUN_TEST(test_notify_link_change_rejects_invalid_address);
|
||||
RUN_TEST(test_constructor_default_hops_link_scope);
|
||||
RUN_TEST(test_constructor_default_hops_admin_scope);
|
||||
RUN_TEST(test_constructor_explicit_hops_overrides_default);
|
||||
RUN_TEST(test_start_without_link_local_fails);
|
||||
RUN_TEST(test_start_rejects_colliding_data_port);
|
||||
RUN_TEST(test_start_opens_three_sockets);
|
||||
RUN_TEST(test_stop_closes_sockets_and_clears_peers);
|
||||
RUN_TEST(test_start_stop_start_is_idempotent);
|
||||
|
||||
@@ -0,0 +1,213 @@
|
||||
// AutoInterface loopback integration tests.
|
||||
//
|
||||
// These exercise the real socket/poll/setsockopt paths against the host's
|
||||
// loopback IPv6 multicast — the closest we can get to live LAN behavior
|
||||
// without two physical machines or root privileges.
|
||||
//
|
||||
// Limitations of single-host loopback testing:
|
||||
// * Two AutoInterface instances bound to the same fe80:: address can't
|
||||
// genuinely peer with each other: each treats the other's announce as
|
||||
// a self-echo (their src.sin6_addr matches each instance's own
|
||||
// _self_link_local_bin). True peering requires distinct link-local
|
||||
// identities, which on a single host requires root + `ip -6 addr add`.
|
||||
// * IPV6_JOIN_GROUP on lo0 may be filtered by some hosts (notably
|
||||
// macOS / older lwIP); those configurations TEST_IGNORE rather than
|
||||
// fail so CI on developer laptops doesn't flake.
|
||||
//
|
||||
// What IS verified end-to-end here:
|
||||
// 1. Two AutoInterface instances can coexist on the same host without
|
||||
// crashing, leaking sockets, or interfering with each other's
|
||||
// multicast traffic.
|
||||
// 2. has_carrier() becomes true after start() drives the multicast
|
||||
// loopback round-trip.
|
||||
// 3. Self-echo suppression keeps peer_count() at 0 after notify_link_
|
||||
// change(), even when the kernel delays a loopback of the announce
|
||||
// sent under the previous link-local.
|
||||
// 4. notify_link_change() while online survives the IPV6_LEAVE_GROUP /
|
||||
// IPV6_JOIN_GROUP rotation without breaking carrier.
|
||||
|
||||
#include <unity.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "Interfaces/AutoInterface.h"
|
||||
#include "Bytes.h"
|
||||
|
||||
using RNS::AutoInterface;
|
||||
using RNS::Bytes;
|
||||
|
||||
// Ports chosen well above the production defaults and the unit-test range
|
||||
// (49716+) so there is no overlap.
|
||||
static constexpr uint16_t kAlfaDisc = 49810;
|
||||
static constexpr uint16_t kAlfaData = 49814;
|
||||
static constexpr uint16_t kBravoDisc = 49820;
|
||||
static constexpr uint16_t kBravoData = 49824;
|
||||
|
||||
namespace {
|
||||
|
||||
// Drive loop_at() in 100ms simulated steps, sleeping a real 5ms between
|
||||
// each so the kernel actually delivers any pending multicast frames.
|
||||
void drive(AutoInterface& ai, uint64_t start_ms, uint64_t end_ms) {
|
||||
for (uint64_t t = start_ms; t <= end_ms; t += 100) {
|
||||
ai.test_loop_at(t);
|
||||
struct timespec ts { 0, 5'000'000 }; // 5 ms
|
||||
nanosleep(&ts, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void drive_pair(AutoInterface& a, AutoInterface& b,
|
||||
uint64_t start_ms, uint64_t end_ms) {
|
||||
for (uint64_t t = start_ms; t <= end_ms; t += 100) {
|
||||
a.test_loop_at(t);
|
||||
b.test_loop_at(t);
|
||||
struct timespec ts { 0, 5'000'000 };
|
||||
nanosleep(&ts, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void test_two_instances_coexist_on_loopback() {
|
||||
AutoInterface alfa("alfa", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
kAlfaDisc, kAlfaData, 4);
|
||||
AutoInterface bravo("bravo", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
kBravoDisc, kBravoData, 4);
|
||||
|
||||
alfa.set_link_local("fe80::1", 1);
|
||||
bravo.set_link_local("fe80::1", 1);
|
||||
|
||||
if (!alfa.start() || !bravo.start()) {
|
||||
alfa.stop(); bravo.stop();
|
||||
TEST_IGNORE_MESSAGE("could not start two AutoInterface instances on loopback");
|
||||
return;
|
||||
}
|
||||
|
||||
drive_pair(alfa, bravo, 0, 3000);
|
||||
|
||||
if (!alfa.has_carrier() || !bravo.has_carrier()) {
|
||||
alfa.stop(); bravo.stop();
|
||||
TEST_IGNORE_MESSAGE("multicast loopback filtered on this host (lo0 IPV6 mcast)");
|
||||
return;
|
||||
}
|
||||
|
||||
// Both saw multicast carrier — kernel is routing between them.
|
||||
TEST_ASSERT_TRUE(alfa.has_carrier());
|
||||
TEST_ASSERT_TRUE(bravo.has_carrier());
|
||||
|
||||
// Both bound to fe80::1, so each correctly treats the other's
|
||||
// announce as self-echo. Documented limitation — round-trip peering
|
||||
// across a single loopback host without root is not achievable.
|
||||
TEST_ASSERT_EQUAL_size_t(0, alfa.peer_count());
|
||||
TEST_ASSERT_EQUAL_size_t(0, bravo.peer_count());
|
||||
|
||||
alfa.stop();
|
||||
bravo.stop();
|
||||
}
|
||||
|
||||
void test_notify_link_change_while_online_keeps_carrier() {
|
||||
AutoInterface ai("nlc_online", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
kAlfaDisc + 100, kAlfaData + 100, 4);
|
||||
|
||||
ai.set_link_local("fe80::1", 1);
|
||||
if (!ai.start()) {
|
||||
TEST_IGNORE_MESSAGE("start() failed on this host's loopback");
|
||||
return;
|
||||
}
|
||||
|
||||
// Phase 1: acquire carrier under the original address.
|
||||
drive(ai, 0, 2000);
|
||||
if (!ai.has_carrier()) {
|
||||
ai.stop();
|
||||
TEST_IGNORE_MESSAGE("no multicast echo on this host's loopback");
|
||||
return;
|
||||
}
|
||||
|
||||
// Phase 2: rotate the link-local while online. Same scope_id so no
|
||||
// IPV6_LEAVE/JOIN cycle fires — only the token rotates and the old
|
||||
// address goes into the self-echo history.
|
||||
ai.notify_link_change("fe80::abcd:1234", 1);
|
||||
TEST_ASSERT_EQUAL_size_t(1, ai.test_self_link_local_history_size());
|
||||
|
||||
// Phase 3: keep driving. Any late loopback of the *previous*
|
||||
// announce's source address must be suppressed by the history-
|
||||
// backed self-echo filter — peer_count() must stay 0.
|
||||
drive(ai, 2000, 5000);
|
||||
TEST_ASSERT_EQUAL_size_t(0, ai.peer_count());
|
||||
|
||||
// Carrier should still be true (we kept getting echoes of our new
|
||||
// announces during phase 3). If multicast suddenly broke this would
|
||||
// surface here as a regression.
|
||||
TEST_ASSERT_TRUE(ai.has_carrier());
|
||||
|
||||
ai.stop();
|
||||
}
|
||||
|
||||
void test_notify_link_change_with_new_scope_rejoins_group() {
|
||||
// Same scope_id is the common case (Wi-Fi reassoc, new link-local
|
||||
// prefix on the same NIC). This test exercises the rarer scope_id
|
||||
// change (e.g. Wi-Fi disconnect, fall back to a different netif) so
|
||||
// the IPV6_LEAVE_GROUP / IPV6_JOIN_GROUP code path runs in a real
|
||||
// socket context. We can't observe the rejoin directly without
|
||||
// platform-specific introspection, so the assertion is "didn't
|
||||
// crash, sockets still open, carrier eventually reacquires."
|
||||
AutoInterface ai("nlc_scope", "reticulum",
|
||||
AutoInterface::MCAST_ADDR_TYPE_TEMPORARY,
|
||||
AutoInterface::SCOPE_LINK,
|
||||
kAlfaDisc + 200, kAlfaData + 200, 4);
|
||||
|
||||
ai.set_link_local("fe80::1", 1);
|
||||
if (!ai.start()) {
|
||||
TEST_IGNORE_MESSAGE("start() failed on this host's loopback");
|
||||
return;
|
||||
}
|
||||
drive(ai, 0, 2000);
|
||||
if (!ai.has_carrier()) {
|
||||
ai.stop();
|
||||
TEST_IGNORE_MESSAGE("no multicast echo on this host's loopback");
|
||||
return;
|
||||
}
|
||||
|
||||
// Rotate scope while online. scope_id=2 on most hosts won't match
|
||||
// any real interface — the rejoin will warn but must not break the
|
||||
// already-open sockets.
|
||||
ai.notify_link_change("fe80::abcd:1234", 2);
|
||||
|
||||
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, ai.discovery_socket_fd());
|
||||
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, ai.unicast_socket_fd());
|
||||
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, ai.data_socket_fd());
|
||||
|
||||
// Drive a few more cycles — this must not crash and must continue
|
||||
// processing the loop without leaking FDs.
|
||||
drive(ai, 2000, 3500);
|
||||
|
||||
ai.stop();
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(-1, ai.discovery_socket_fd());
|
||||
TEST_ASSERT_EQUAL_INT(-1, ai.unicast_socket_fd());
|
||||
TEST_ASSERT_EQUAL_INT(-1, ai.data_socket_fd());
|
||||
}
|
||||
|
||||
void setUp(void) {}
|
||||
void tearDown(void) {}
|
||||
|
||||
int runUnityTests(void) {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_two_instances_coexist_on_loopback);
|
||||
RUN_TEST(test_notify_link_change_while_online_keeps_carrier);
|
||||
RUN_TEST(test_notify_link_change_with_new_scope_rejoins_group);
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
int main(void) { return runUnityTests(); }
|
||||
|
||||
#ifdef ARDUINO
|
||||
void setup() { delay(2000); runUnityTests(); }
|
||||
void loop() {}
|
||||
#endif
|
||||
|
||||
void app_main() { runUnityTests(); }
|
||||
Reference in new issue
Block a user