Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,21 @@ ErrorCode Router::send(meshtastic_MeshPacket *p)
if (moduleConfig.mqtt.enabled && isFromUs(p) && mqtt) {
mqtt->onSend(*p, *p_decoded, chIndex);
}
#endif
#if HAS_UDP_MULTICAST
// Only publish to UDP if we're the original transmitter of the packet
if (udpHandler && config.network.enabled_protocols & meshtastic_Config_NetworkConfig_ProtocolFlags_UDP_BROADCAST &&
isFromUs(p)) {
Comment thread
GUVWAF marked this conversation as resolved.
Resolved
udpHandler->onSend(const_cast<meshtastic_MeshPacket *>(p), chIndex);
}
#endif
packetPool.release(p_decoded);
}

#if HAS_UDP_MULTICAST
if (udpHandler && config.network.enabled_protocols & meshtastic_Config_NetworkConfig_ProtocolFlags_UDP_BROADCAST) {
udpHandler->onSend(const_cast<meshtastic_MeshPacket *>(p));
// For packets that are already encrypted, we need to determine the channel from packet info
else if (udpHandler && config.network.enabled_protocols & meshtastic_Config_NetworkConfig_ProtocolFlags_UDP_BROADCAST &&
isFromUs(p)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isFromUs(p) is here still.

udpHandler->onSend(const_cast<meshtastic_MeshPacket *>(p), p->channel);
Comment thread
thebentern marked this conversation as resolved.
Resolved
Comment thread
GUVWAF marked this conversation as resolved.
Resolved
}
#endif

Expand Down Expand Up @@ -483,6 +491,35 @@ DecodeState perhapsDecode(meshtastic_MeshPacket *p)
}
}

#if HAS_UDP_MULTICAST
// Fallback: for UDP multicast, try default preset names with default PSK if normal channel match failed
if (!decrypted && p->transport_mechanism == meshtastic_MeshPacket_TransportMechanism_TRANSPORT_MULTICAST_UDP) {
if (channels.setDefaultPresetCryptoForHash(p->channel)) {
memcpy(bytes, p->encrypted.bytes, rawSize);
crypto->decrypt(p->from, p->id, rawSize, bytes);

meshtastic_Data decodedtmp;
memset(&decodedtmp, 0, sizeof(decodedtmp));
if (pb_decode_from_bytes(bytes, rawSize, &meshtastic_Data_msg, &decodedtmp) &&
decodedtmp.portnum != meshtastic_PortNum_UNKNOWN_APP) {
p->decoded = decodedtmp;
p->which_payload_variant = meshtastic_MeshPacket_decoded_tag;
// Map to our local default channel index (name+PSK default), not necessarily primary
ChannelIndex defaultIndex = channels.getPrimaryIndex();
for (ChannelIndex i = 0; i < channels.getNumChannels(); ++i) {
if (channels.isDefaultChannel(i)) {
defaultIndex = i;
break;
}
}
chIndex = defaultIndex;
decrypted = true;
} else {
LOG_WARN("UDP fallback decode attempted but failed for hash 0x%x", p->channel);
}
}
}
#endif
if (decrypted) {
// parsing was successful
p->channel = chIndex; // change to store the index instead of the hash
Expand Down
16 changes: 15 additions & 1 deletion src/mesh/udp/UdpMulticastHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#if HAS_UDP_MULTICAST
#include "configuration.h"
#include "main.h"
#include "mesh/Channels.h"
#include "mesh/Router.h"

#if HAS_ETHERNET && defined(ARCH_NRF52)
Expand Down Expand Up @@ -53,6 +54,13 @@ class UdpMulticastHandler final
LOG_DEBUG("Decoding MeshPacket from UDP len=%u", packetLength);
bool isPacketDecoded = pb_decode_from_bytes(packet.data(), packetLength, &meshtastic_MeshPacket_msg, &mp);
if (isPacketDecoded && router && mp.which_payload_variant == meshtastic_MeshPacket_encrypted_tag) {
Comment thread
thebentern marked this conversation as resolved.
Resolved
// Check if downlink is enabled for this channel
auto &ch = channels.getByIndex(mp.channel);
Comment thread
GUVWAF marked this conversation as resolved.
Resolved
if (!ch.settings.downlink_enabled) {
LOG_DEBUG("UDP downlink disabled for channel %d", mp.channel);
return;
}

mp.transport_mechanism = meshtastic_MeshPacket_TransportMechanism_TRANSPORT_MULTICAST_UDP;
mp.pki_encrypted = false;
mp.public_key.size = 0;
Expand All @@ -65,11 +73,17 @@ class UdpMulticastHandler final
}
}

bool onSend(const meshtastic_MeshPacket *mp)
bool onSend(const meshtastic_MeshPacket *mp, ChannelIndex chIndex)
{
if (!mp || !udp) {
return false;
}
Comment thread
thebentern marked this conversation as resolved.
Resolved
// Check if uplink is enabled for this specific channel
auto &ch = channels.getByIndex(chIndex);
if (!ch.settings.uplink_enabled) {
LOG_DEBUG("UDP uplink disabled for channel %d", chIndex);
return false;
}
#if defined(ARCH_NRF52)
if (!isEthernetAvailable()) {
return false;
Expand Down