From 9da37b29ac58db565065d16c5da8a06cb2e56de8 Mon Sep 17 00:00:00 2001 From: Chad Attermann Date: Sun, 1 Mar 2026 18:22:09 -0700 Subject: [PATCH] Added RNS 1.x Link MTU handling to Transport --- src/Bytes.h | 13 +++++++------ src/Transport.cpp | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/Bytes.h b/src/Bytes.h index 29af261..ec3d90f 100644 --- a/src/Bytes.h +++ b/src/Bytes.h @@ -262,7 +262,7 @@ MEM("Creating from data-move..."); //inline void assign(const std::string& string) { assign(string.c_str()); } inline void assign(const std::string& string) { assign((uint8_t*)string.c_str(), string.length()); } void assignHex(const uint8_t* hex, size_t hex_size); - inline void assignHex(const char* hex) { assignHex((uint8_t*)hex, strlen(hex)); } + inline void assignHex(const char* hex) { if (!hex) return; assignHex((uint8_t*)hex, strlen(hex)); } inline void append(const Bytes& bytes) { // if append is empty then do nothing @@ -307,7 +307,7 @@ MEM("Creating from data-move..."); //inline void append(const std::string& string) { append(string.c_str()); } inline void append(const std::string& string) { append((uint8_t*)string.c_str(), string.length()); } void appendHex(const uint8_t* hex, size_t hex_size); - inline void appendHex(const char* hex) { appendHex((uint8_t*)hex, strlen(hex)); } + inline void appendHex(const char* hex) { if (!hex) return; appendHex((uint8_t*)hex, strlen(hex)); } inline uint8_t* writable(size_t size) { if (size > 0) { @@ -343,7 +343,7 @@ MEM("Creating from data-move..."); public: int compare(const Bytes& bytes) const; int compare(const uint8_t* buf, size_t size) const; - inline int compare(const char* str) const { return compare((const uint8_t*)str, strlen(str)); } + inline int compare(const char* str) const { if (!str) return empty() ? 0 : 1; return compare((const uint8_t*)str, strlen(str)); } inline size_t size() const { if (!_data) return 0; return _data->size(); } inline bool empty() const { if (!_data) return true; return _data->empty(); } inline size_t capacity() const { if (!_data) return 0; return _data->capacity(); } @@ -358,7 +358,7 @@ MEM("Creating from data-move..."); inline Bytes left(size_t len) const { if (!_data) return NONE; if (len > size()) len = size(); return {data(), len}; } inline Bytes right(size_t len) const { if (!_data) return NONE; if (len > size()) len = size(); return {data() + (size() - len), len}; } inline int find(int pos, const char* str) { - if (!_data || _data->data() == nullptr || (size_t)pos >= _data->size()) { + if (!str || !_data || _data->data() == nullptr || (size_t)pos >= _data->size()) { return -1; } //const char* ptr = strnstr((const char*)(_data->data() + pos), str, (_data->size() - pos)); @@ -403,7 +403,7 @@ MEM("Creating from data-move..."); //inline Bytes bytesFromChunk(const uint8_t* ptr, size_t len) { return Bytes(ptr, len); } inline Bytes bytesFromChunk(const uint8_t* ptr, size_t len) { return {ptr, len}; } //inline Bytes bytesFromString(const char* str) { return Bytes((uint8_t*)str, strlen(str)); } - inline Bytes bytesFromString(const char* str) { return {(uint8_t*)str, strlen(str)}; } + inline Bytes bytesFromString(const char* str) { if (!str) return {}; return {(uint8_t*)str, strlen(str)}; } //z inline Bytes bytesFromInt(const int) { return {(uint8_t*)str, strlen(str)}; } inline std::string stringFromBytes(const Bytes& bytes) { return bytes.toString(); } @@ -440,7 +440,8 @@ namespace ArduinoJson { } // Deserialize inline void convertFromJson(JsonVariantConst src, RNS::Bytes& dst) { - dst.assignHex(src.as()); + const char* hex = src.as(); + if (hex) dst.assignHex(hex); } inline bool canConvertFromJson(JsonVariantConst src, const RNS::Bytes&) { return src.is(); diff --git a/src/Transport.cpp b/src/Transport.cpp index 6657b63..7075ee2 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -1547,7 +1547,41 @@ using namespace RNS::Utilities; if (packet.packet_type() == Type::Packet::LINKREQUEST) { TRACE("Transport::inbound: Packet is next-hop LINKREQUEST"); double now = OS::time(); - double proof_timeout = now + Type::Link::ESTABLISHMENT_TIMEOUT_PER_HOP * std::max((uint8_t)1, remaining_hops); + double proof_timeout = extra_link_proof_timeout(packet.receiving_interface()); + proof_timeout += now + Type::Link::ESTABLISHMENT_TIMEOUT_PER_HOP * std::max((uint8_t)1, remaining_hops); + uint16_t path_mtu = Link::mtu_from_lr_packet(packet); + Type::Link::link_mode mode = Link::mode_from_lr_packet(packet); + uint16_t ph_mtu = 0; + if (interface) { + ph_mtu = interface.HW_MTU(); + } + uint16_t nh_mtu = outbound_interface.HW_MTU(); + if (path_mtu != 0) { + if (outbound_interface.HW_MTU() == 0) { + DEBUG("No next-hop HW MTU, disabling link MTU upgrade"); + path_mtu = 0; + new_raw = new_raw.left(new_raw.size()-Type::Link::LINK_MTU_SIZE); + } + else if (outbound_interface.AUTOCONFIGURE_MTU() == 0 && outbound_interface.FIXED_MTU() == 0) { + DEBUG("Outbound interface doesn't support MTU autoconfiguration, disabling link MTU upgrade"); + path_mtu = 0; + new_raw = new_raw.left(new_raw.size()-Type::Link::LINK_MTU_SIZE); + } + else { + if (nh_mtu < path_mtu || (ph_mtu != 0 && ph_mtu < path_mtu)) { + try { + path_mtu = std::min(nh_mtu, ph_mtu); + Bytes clamped_mtu = Link::signalling_bytes(path_mtu, mode); + DEBUGF("Clamping link MTU to %u", path_mtu); + new_raw = new_raw.left(new_raw.size()-Type::Link::LINK_MTU_SIZE)+clamped_mtu; + } + catch (const std::exception& e) { + WARNINGF("Dropping link request packet. The contained exception was: %s", e.what()); + return; + } + } + } + } LinkEntry link_entry( now, next_hop,