From 3cd64bb8b5971e99f1f7f5a686016c83858421af Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 5 Apr 2021 08:44:47 +0800 Subject: [PATCH] allow passing even encrypted packets through the plugins --- src/mesh/MeshPlugin.cpp | 15 ++++++++------- src/mesh/MeshPlugin.h | 6 +++++- src/mesh/ProtobufPlugin.h | 10 +++++----- src/mesh/Router.cpp | 9 ++++----- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/mesh/MeshPlugin.cpp b/src/mesh/MeshPlugin.cpp index e97836c09..b3a12fc83 100644 --- a/src/mesh/MeshPlugin.cpp +++ b/src/mesh/MeshPlugin.cpp @@ -70,7 +70,8 @@ void MeshPlugin::callPlugins(const MeshPacket &mp) // DEBUG_MSG("In call plugins\n"); bool pluginFound = false; - assert(mp.which_payloadVariant == MeshPacket_decoded_tag); // I think we are guarnteed the packet is decoded by this point? + // We now allow **encrypted** packets to pass through the plugins + bool isDecoded = mp.which_payloadVariant == MeshPacket_decoded_tag; currentReply = NULL; // No reply yet @@ -82,19 +83,19 @@ void MeshPlugin::callPlugins(const MeshPacket &mp) pi.currentRequest = ∓ - /// received channel - auto ch = channels.getByIndex(mp.channel); - assert(ch.has_settings); - /// We only call plugins that are interested in the packet (and the message is destined to us or we are promiscious) - bool wantsPacket = (pi.isPromiscuous || toUs) && pi.wantPacket(&mp); + bool wantsPacket = (isDecoded || pi.encryptedOk) && (pi.isPromiscuous || toUs) && pi.wantPacket(&mp); if (wantsPacket) { // DEBUG_MSG("Plugin %s wantsPacket=%d\n", pi.name, wantsPacket); pluginFound = true; + /// received channel (or NULL if not decoded) + Channel *ch = isDecoded ? &channels.getByIndex(mp.channel) : NULL; + /// Is the channel this packet arrived on acceptable? (security check) - bool rxChannelOk = !pi.boundChannel || (mp.from == 0) || (strcmp(ch.settings.name, pi.boundChannel) == 0); + /// Note: we can't know channel names for encrypted packets, so those are NEVER sent to boundChannel plugins + bool rxChannelOk = !pi.boundChannel || (ch && ((mp.from == 0) || (strcmp(ch->settings.name, pi.boundChannel) == 0))); if (!rxChannelOk) { // no one should have already replied! diff --git a/src/mesh/MeshPlugin.h b/src/mesh/MeshPlugin.h index 23a9cf754..011e680ab 100644 --- a/src/mesh/MeshPlugin.h +++ b/src/mesh/MeshPlugin.h @@ -42,12 +42,16 @@ class MeshPlugin protected: const char *name; - /* Most plugins only care about packets that are destined for their node (i.e. broadcasts or has their node as the specific + /** Most plugins only care about packets that are destined for their node (i.e. broadcasts or has their node as the specific recipient) But some plugs might want to 'sniff' packets that are merely being routed (passing through the current node). Those plugins can set this to true and their handleReceived() will be called for every packet. */ bool isPromiscuous = false; + /** Most plugins only understand decrypted packets. For plugins that also want to see encrypted packets, they should set this + * flag */ + bool encryptedOk = false; + /** If a bound channel name is set, we will only accept received packets that come in on that channel. * A special exception (FIXME, not sure if this is a good idea) - packets that arrive on the local interface * are allowed on any channel (this lets the local user do anything). diff --git a/src/mesh/ProtobufPlugin.h b/src/mesh/ProtobufPlugin.h index 30874b24b..6984cf626 100644 --- a/src/mesh/ProtobufPlugin.h +++ b/src/mesh/ProtobufPlugin.h @@ -22,10 +22,9 @@ template class ProtobufPlugin : protected SinglePortPlugin } protected: - /** * Handle a received message, the data field in the message is already decoded and is provided - * + * * In general decoded will always be !NULL. But in some special applications (where you have handling packets * for multiple port numbers, decoding will ONLY be attempted for packets where the portnum matches our expected ourPortNum. */ @@ -58,11 +57,12 @@ template class ProtobufPlugin : protected SinglePortPlugin // it would be better to update even if the message was destined to others. auto &p = mp.decoded; - DEBUG_MSG("Received %s from=0x%0x, id=0x%x, portnum=%d, payloadlen=%d\n", name, mp.from, mp.id, p.portnum, p.payload.size); + DEBUG_MSG("Received %s from=0x%0x, id=0x%x, portnum=%d, payloadlen=%d\n", name, mp.from, mp.id, p.portnum, + p.payload.size); T scratch; - T *decoded = NULL; - if(mp.decoded.portnum == ourPortNum) { + T *decoded = NULL; + if (mp.which_payloadVariant == MeshPacket_decoded_tag && mp.decoded.portnum == ourPortNum) { memset(&scratch, 0, sizeof(scratch)); if (pb_decode_from_bytes(p.payload.bytes, p.payload.size, fields, &scratch)) decoded = &scratch; diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index e1aa89586..e006aa98d 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -304,13 +304,12 @@ void Router::handleReceived(MeshPacket *p) if (decoded) { // parsing was successful, queue for our recipient printPacket("handleReceived", p); - - // call any promiscious plugins here, make a (non promisiocous) plugin for forwarding messages to phone api - // sniffReceived(p); - MeshPlugin::callPlugins(*p); } else { - DEBUG_MSG("packet decoding failed\n"); + printPacket("packet decoding failed (no PSK?)", p); } + + // call plugins here + MeshPlugin::callPlugins(*p); } void Router::perhapsHandleReceived(MeshPacket *p)