Files
firmware/src/modules/RoutingModule.cpp

86 lines
3.6 KiB
C++
Raw Normal View History

#include "RoutingModule.h"
#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
bool RoutingModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_Routing *r)
2021-02-17 13:06:23 +08:00
{
2021-03-05 11:44:45 +08:00
printPacket("Routing sniffing", &mp);
2021-02-21 12:59:47 +08:00
router->sniffReceived(&mp, r);
bool maybePKI =
mp.which_payload_variant == meshtastic_MeshPacket_encrypted_tag && mp.channel == 0 && mp.to != NODENUM_BROADCAST;
// 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;
}
// FIXME - move this to a non promsicious PhoneAPI module?
2021-03-05 11:44:45 +08:00
// Note: we are careful not to send back packets that started with the phone back to the phone
if ((mp.to == NODENUM_BROADCAST || isToUs(&mp)) && (mp.from != 0)) {
2021-02-21 12:59:47 +08:00
printPacket("Delivering rx packet", &mp);
service->handleFromRadio(&mp);
2021-02-21 12:59:47 +08:00
}
2021-02-17 13:06:23 +08:00
return false; // Let others look at this message also if they want
}
meshtastic_MeshPacket *RoutingModule::allocReply()
2021-02-17 13:06:23 +08:00
{
if (config.device.role == meshtastic_Config_DeviceConfig_Role_REPEATER)
return NULL;
2021-02-23 14:35:34 +08:00
assert(currentRequest);
// We only consider making replies if the request was a legit routing packet (not just something we were sniffing)
2023-01-30 14:50:57 -06:00
if (currentRequest->decoded.portnum == meshtastic_PortNum_ROUTING_APP) {
2021-02-23 14:35:34 +08:00
assert(0); // 1.2 refactoring fixme, Not sure if anything needs this yet?
// return allocDataProtobuf(u);
}
2021-02-17 13:06:23 +08:00
return NULL;
}
void RoutingModule::sendAckNak(meshtastic_Routing_Error err, NodeNum to, PacketId idFrom, ChannelIndex chIndex, uint8_t hopStart,
uint8_t hopLimit)
2021-02-17 13:06:23 +08:00
{
auto p = allocAckNak(err, to, idFrom, chIndex, hopStart, hopLimit);
2021-02-17 13:06:23 +08:00
router->sendLocal(p); // we sometimes send directly to the local node
}
uint8_t RoutingModule::getHopLimitForResponse(uint8_t hopStart, uint8_t hopLimit)
{
if (hopStart != 0) {
// Hops used by the request. If somebody in between running modified firmware modified it, ignore it
uint8_t hopsUsed = hopStart < hopLimit ? config.lora.hop_limit : hopStart - hopLimit;
if (hopsUsed > config.lora.hop_limit) {
// In event mode, we never want to send packets with more than our default 3 hops.
#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
#endif
} 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
}
}
return Default::getConfiguredOrDefaultHopLimit(config.lora.hop_limit); // Use the default hop limit
}
RoutingModule::RoutingModule() : ProtobufModule("routing", meshtastic_PortNum_ROUTING_APP, &meshtastic_Routing_msg)
2021-02-17 13:06:23 +08:00
{
isPromiscuous = true;
// moved the ReboradcastMode 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;
}