From 96724cd26aa84f849721fd40f26de1890dcd7e37 Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Tue, 21 Jan 2025 18:39:55 +1100 Subject: [PATCH] * onPeerPathRecv() refactored: 'reciprocal path' now handled in Mesh class, not in application layer --- examples/ping_client/main.cpp | 11 +++-------- examples/ping_server/main.cpp | 3 ++- examples/simple_repeater/main.cpp | 3 ++- examples/simple_secure_chat/main.cpp | 11 +++-------- examples/test_admin/main.cpp | 9 ++------- src/Mesh.cpp | 16 ++++++++++++++-- src/Mesh.h | 4 +++- src/helpers/RAK4631Board.h | 1 + 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/examples/ping_client/main.cpp b/examples/ping_client/main.cpp index f48bd475..60b68533 100644 --- a/examples/ping_client/main.cpp +++ b/examples/ping_client/main.cpp @@ -81,21 +81,16 @@ protected: } } - void onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { + bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { // must be from server_id Serial.printf("PATH to server, path_len=%d\n", (uint32_t) path_len); memcpy(server_path, path, server_path_len = path_len); // store a copy of path, for sendDirect() - if (packet->isRouteFlood()) { - // send a reciprocal return path to sender, but send DIRECTLY! - mesh::Packet* rpath = createPathReturn(server_id, secret, packet->path, packet->path_len, 0, NULL, 0); - if (rpath) sendDirect(rpath, path, path_len); - } - - if (extra_type == PAYLOAD_TYPE_RESPONSE) { + if (extra_type == PAYLOAD_TYPE_RESPONSE && extra_len > 0) { Serial.println("Received PING Reply!"); } + return true; // send reciprocal path if necessary } public: diff --git a/examples/ping_server/main.cpp b/examples/ping_server/main.cpp index f2111a5e..d5c92a1a 100644 --- a/examples/ping_server/main.cpp +++ b/examples/ping_server/main.cpp @@ -118,7 +118,7 @@ protected: } } - void onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { + bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { if (sender_idx >= 0 && sender_idx < MAX_CLIENTS) { Serial.printf("PATH to client, path_len=%d\n", (uint32_t) path_len); @@ -134,6 +134,7 @@ protected: } // NOTE: no reciprocal path send!! + return false; } public: diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 3d1ecddd..83d738ca 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -299,7 +299,7 @@ protected: } } - void onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { + bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { // TODO: prevent replay attacks int i = matching_peer_indexes[sender_idx]; @@ -312,6 +312,7 @@ protected: } // NOTE: no reciprocal path send!! + return false; } public: diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index a950a15d..8316da1a 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/examples/simple_secure_chat/main.cpp @@ -191,11 +191,11 @@ protected: } } - void onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { + bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { int i = matching_peer_indexes[sender_idx]; if (i < 0 || i >= num_contacts) { MESH_DEBUG_PRINTLN("onPeerPathRecv: Invalid sender idx: %d", i); - return; + return false; } ContactInfo& from = contacts[i]; @@ -205,16 +205,11 @@ protected: // FUTURE: could store multiple out_paths per contact, and try to find which is the 'best'(?) memcpy(from.out_path, path, from.out_path_len = path_len); // store a copy of path, for sendDirect() - if (packet->isRouteFlood()) { - // send a reciprocal return path to sender, but send DIRECTLY! - mesh::Packet* rpath = createPathReturn(from.id, secret, packet->path, packet->path_len, 0, NULL, 0); - if (rpath) sendDirect(rpath, path, path_len); - } - if (extra_type == PAYLOAD_TYPE_ACK && extra_len >= 4) { // also got an encoded ACK! processAck(extra); } + return true; // send reciprocal path if necessary } void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override { diff --git a/examples/test_admin/main.cpp b/examples/test_admin/main.cpp index 33395026..2b8805de 100644 --- a/examples/test_admin/main.cpp +++ b/examples/test_admin/main.cpp @@ -135,21 +135,16 @@ protected: } } - void onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { + bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override { // must be from server_id Serial.printf("PATH to repeater, path_len=%d\n", (uint32_t) path_len); memcpy(server_path, path, server_path_len = path_len); // store a copy of path, for sendDirect() - if (packet->isRouteFlood()) { - // send a reciprocal return path to sender, but send DIRECTLY! - mesh::Packet* rpath = createPathReturn(server_id, secret, packet->path, packet->path_len, 0, NULL, 0); - if (rpath) sendDirect(rpath, path, path_len); - } - if (extra_type == PAYLOAD_TYPE_RESPONSE) { handleResponse(extra, extra_len); } + return true; // send reciprocal path if necessary } public: diff --git a/src/Mesh.cpp b/src/Mesh.cpp index bbc42b2b..3d59796f 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -97,7 +97,13 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { uint8_t extra_type = data[k++]; uint8_t* extra = &data[k]; uint8_t extra_len = len - k; // remainder of packet (may be padded with zeroes!) - onPeerPathRecv(pkt, j, secret, path, path_len, extra_type, extra, extra_len); + if (onPeerPathRecv(pkt, j, secret, path, path_len, extra_type, extra, extra_len)) { + if (pkt->isRouteFlood()) { + // send a reciprocal return path to sender, but send DIRECTLY! + mesh::Packet* rpath = createPathReturn(&src_hash, secret, pkt->path, pkt->path_len, 0, NULL, 0); + if (rpath) sendDirect(rpath, path, path_len); + } + } } else { onPeerDataRecv(pkt, pkt->getPayloadType(), j, secret, data, len); } @@ -261,6 +267,12 @@ Packet* Mesh::createAdvert(const LocalIdentity& id, const uint8_t* app_data, siz #define MAX_COMBINED_PATH (MAX_PACKET_PAYLOAD - 2 - CIPHER_BLOCK_SIZE) Packet* Mesh::createPathReturn(const Identity& dest, const uint8_t* secret, const uint8_t* path, uint8_t path_len, uint8_t extra_type, const uint8_t*extra, size_t extra_len) { + uint8_t dest_hash[PATH_HASH_SIZE]; + dest.copyHashTo(dest_hash); + return createPathReturn(dest_hash, secret, path, path_len, extra_type, extra, extra_len); +} + +Packet* Mesh::createPathReturn(const uint8_t* dest_hash, const uint8_t* secret, const uint8_t* path, uint8_t path_len, uint8_t extra_type, const uint8_t*extra, size_t extra_len) { if (path_len + extra_len + 5 > MAX_COMBINED_PATH) return NULL; // too long!! Packet* packet = obtainNewPacket(); @@ -271,7 +283,7 @@ Packet* Mesh::createPathReturn(const Identity& dest, const uint8_t* secret, cons packet->header = (PAYLOAD_TYPE_PATH << PH_TYPE_SHIFT); // ROUTE_TYPE_* set later int len = 0; - len += dest.copyHashTo(&packet->payload[len]); // dest hash + memcpy(&packet->payload[len], dest_hash, PATH_HASH_SIZE); len += PATH_HASH_SIZE; // dest hash len += self_id.copyHashTo(&packet->payload[len]); // src hash { diff --git a/src/Mesh.h b/src/Mesh.h index c7d689c1..e5da9406 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -89,8 +89,9 @@ protected: * NOTE: these can be received multiple times (per sender), via differen routes * \param sender_idx index of peer, [0..n) where n is what searchPeersByHash() returned * \param secret the pre-calculated shared-secret (handy for sending response packet) + * \returns true, if path was accepted and that reciprocal path should be sent */ - virtual void onPeerPathRecv(Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) { } + virtual bool onPeerPathRecv(Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) { return false; } /** * \brief A new incoming Advertisement has been received. @@ -152,6 +153,7 @@ public: Packet* createAnonDatagram(uint8_t type, const LocalIdentity& sender, const Identity& dest, const uint8_t* secret, const uint8_t* data, size_t data_len); Packet* createGroupDatagram(uint8_t type, const GroupChannel& channel, const uint8_t* data, size_t data_len); Packet* createAck(uint32_t ack_crc); + Packet* createPathReturn(const uint8_t* dest_hash, const uint8_t* secret, const uint8_t* path, uint8_t path_len, uint8_t extra_type, const uint8_t*extra, size_t extra_len); Packet* createPathReturn(const Identity& dest, const uint8_t* secret, const uint8_t* path, uint8_t path_len, uint8_t extra_type, const uint8_t*extra, size_t extra_len); /** diff --git a/src/helpers/RAK4631Board.h b/src/helpers/RAK4631Board.h index 904fd8ea..e194d05a 100644 --- a/src/helpers/RAK4631Board.h +++ b/src/helpers/RAK4631Board.h @@ -33,6 +33,7 @@ public: pinMode(SX126X_POWER_EN, OUTPUT); digitalWrite(SX126X_POWER_EN, HIGH); + delay(10); // give sx1262 some time to power up } uint8_t getStartupReason() const override { return startup_reason; }