2022-02-27 00:18:35 -08:00
|
|
|
#include "RoutingModule.h"
|
2024-07-29 20:16:47 -05:00
|
|
|
#include "Default.h"
|
2021-02-17 13:06:23 +08:00
|
|
|
#include "MeshService.h"
|
|
|
|
|
#include "NodeDB.h"
|
|
|
|
|
#include "Router.h"
|
2023-01-21 14:34:29 +01:00
|
|
|
#include "configuration.h"
|
2021-02-17 13:06:23 +08:00
|
|
|
#include "main.h"
|
|
|
|
|
|
2022-02-27 02:21:02 -08:00
|
|
|
RoutingModule *routingModule;
|
2021-02-17 13:06:23 +08:00
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
bool RoutingModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_Routing *r)
|
|
|
|
|
{
|
|
|
|
|
bool maybePKI = mp.which_payload_variant == meshtastic_MeshPacket_encrypted_tag && mp.channel == 0 && !isBroadcast(mp.to);
|
|
|
|
|
// Beginning of logic whether to drop the packet based on Rebroadcast mode
|
|
|
|
|
if (mp.which_payload_variant == meshtastic_MeshPacket_encrypted_tag &&
|
|
|
|
|
(config.device.rebroadcast_mode == meshtastic_Config_DeviceConfig_RebroadcastMode_LOCAL_ONLY ||
|
|
|
|
|
config.device.rebroadcast_mode == meshtastic_Config_DeviceConfig_RebroadcastMode_KNOWN_ONLY)) {
|
|
|
|
|
if (!maybePKI)
|
|
|
|
|
return false;
|
|
|
|
|
if ((nodeDB->getMeshNode(mp.from) == NULL || !nodeDB->getMeshNode(mp.from)->has_user) &&
|
|
|
|
|
(nodeDB->getMeshNode(mp.to) == NULL || !nodeDB->getMeshNode(mp.to)->has_user))
|
|
|
|
|
return false;
|
|
|
|
|
} else if (owner.is_licensed && nodeDB->getLicenseStatus(mp.from) == UserLicenseStatus::NotLicensed) {
|
|
|
|
|
// Don't let licensed users to rebroadcast packets from unlicensed users
|
|
|
|
|
// If we know they are in-fact unlicensed
|
|
|
|
|
LOG_DEBUG("Packet from unlicensed user, ignoring packet");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-08-16 19:37:28 -05:00
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
printPacket("Routing sniffing", &mp);
|
|
|
|
|
router->sniffReceived(&mp, r);
|
2024-10-21 00:04:45 +02:00
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
// FIXME - move this to a non promsicious PhoneAPI module?
|
|
|
|
|
// Note: we are careful not to send back packets that started with the phone back to the phone
|
|
|
|
|
if ((isBroadcast(mp.to) || isToUs(&mp)) && (mp.from != 0)) {
|
|
|
|
|
printPacket("Delivering rx packet", &mp);
|
|
|
|
|
service->handleFromRadio(&mp);
|
|
|
|
|
}
|
2021-03-23 11:44:51 +08:00
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
return false; // Let others look at this message also if they want
|
2021-02-17 13:06:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
meshtastic_MeshPacket *RoutingModule::allocReply()
|
|
|
|
|
{
|
|
|
|
|
assert(currentRequest);
|
2021-02-23 14:35:34 +08:00
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
return NULL;
|
2021-02-17 13:06:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
void RoutingModule::sendAckNak(meshtastic_Routing_Error err, NodeNum to, PacketId idFrom, ChannelIndex chIndex, uint8_t hopLimit,
|
|
|
|
|
bool ackWantsAck)
|
|
|
|
|
{
|
|
|
|
|
auto p = allocAckNak(err, to, idFrom, chIndex, hopLimit);
|
2021-02-17 13:06:23 +08:00
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
// Allow the caller to set want_ack on this ACK packet if it's important that the ACK be delivered reliably
|
|
|
|
|
p->want_ack = ackWantsAck;
|
2025-10-01 16:31:53 -04:00
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
router->sendLocal(p); // we sometimes send directly to the local node
|
2021-02-17 13:06:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
uint8_t RoutingModule::getHopLimitForResponse(const meshtastic_MeshPacket &mp)
|
|
|
|
|
{
|
|
|
|
|
const int8_t hopsUsed = getHopsAway(mp);
|
|
|
|
|
if (hopsUsed >= 0) {
|
|
|
|
|
if (hopsUsed > (int32_t)(config.lora.hop_limit)) {
|
2024-07-29 20:16:47 -05:00
|
|
|
// In event mode, we never want to send packets with more than our default 3 hops.
|
2026-01-04 12:15:53 +01:00
|
|
|
#if !(EVENTMODE) // This falls through to the default.
|
|
|
|
|
return hopsUsed; // If the request used more hops than the limit, use the same amount of hops
|
2024-07-29 20:16:47 -05:00
|
|
|
#endif
|
2026-01-04 12:15:53 +01:00
|
|
|
} else if ((uint8_t)(hopsUsed + 2) < config.lora.hop_limit) {
|
|
|
|
|
return hopsUsed + 2; // Use only the amount of hops needed with some margin as the way back may be different
|
|
|
|
|
}
|
2024-03-08 14:13:57 +01:00
|
|
|
}
|
2026-01-04 12:15:53 +01:00
|
|
|
return Default::getConfiguredOrDefaultHopLimit(config.lora.hop_limit); // Use the default hop limit
|
2024-03-08 14:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
meshtastic_MeshPacket *RoutingModule::allocAckNak(meshtastic_Routing_Error err, NodeNum to, PacketId idFrom, ChannelIndex chIndex,
|
|
|
|
|
uint8_t hopLimit)
|
|
|
|
|
{
|
|
|
|
|
return MeshModule::allocAckNak(err, to, idFrom, chIndex, hopLimit);
|
2025-12-12 12:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
RoutingModule::RoutingModule() : ProtobufModule("routing", meshtastic_PortNum_ROUTING_APP, &meshtastic_Routing_msg)
|
|
|
|
|
{
|
|
|
|
|
isPromiscuous = true;
|
2024-08-16 19:37:28 -05:00
|
|
|
|
2026-01-04 12:15:53 +01:00
|
|
|
// moved the RebroadcastMode logic into handleReceivedProtobuf
|
|
|
|
|
// LocalOnly requires either the from or to to be a known node
|
|
|
|
|
// knownOnly specifically requires the from to be a known node.
|
|
|
|
|
encryptedOk = true;
|
2024-03-21 09:06:37 -05:00
|
|
|
}
|