Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event mode: Enforce reliable hop limit and disallow default public MQTT #4343

Merged
merged 10 commits into from
Jul 30, 2024
7 changes: 7 additions & 0 deletions src/mesh/Channels.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Channels.h"
#include "../userPrefs.h"
#include "CryptoEngine.h"
#include "Default.h"
#include "DisplayFormatters.h"
#include "NodeDB.h"
#include "RadioInterface.h"
Expand Down Expand Up @@ -276,6 +277,12 @@ void Channels::setChannel(const meshtastic_Channel &c)

bool Channels::anyMqttEnabled()
{
#if EVENT_MODE
// Don't publish messages on the public MQTT broker if we are in event mode
if (strcmp(moduleConfig.mqtt.address, default_mqtt_address) == 0) {
return false;
}
#endif
for (int i = 0; i < getNumChannels(); i++)
if (channelFile.channels[i].role != meshtastic_Channel_Role_DISABLED && channelFile.channels[i].has_settings &&
(channelFile.channels[i].settings.downlink_enabled || channelFile.channels[i].settings.uplink_enabled))
Expand Down
10 changes: 10 additions & 0 deletions src/mesh/Default.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Default.h"
#include "../userPrefs.h"

uint32_t Default::getConfiguredOrDefaultMs(uint32_t configuredInterval, uint32_t defaultInterval)
{
Expand Down Expand Up @@ -40,4 +41,13 @@ uint32_t Default::getConfiguredOrDefaultMsScaled(uint32_t configured, uint32_t d
return getConfiguredOrDefaultMs(configured, defaultValue);

return getConfiguredOrDefaultMs(configured, defaultValue) * congestionScalingCoefficient(numOnlineNodes);
}

uint8_t Default::getConfiguredOrDefaultHopLimit(uint8_t configured)
{
#if EVENT_MODE
return (configured > HOP_RELIABLE) ? HOP_RELIABLE : config.lora.hop_limit;
#else
return (configured >= HOP_MAX) ? HOP_MAX : config.lora.hop_limit;
#endif
}
1 change: 1 addition & 0 deletions src/mesh/Default.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Default
static uint32_t getConfiguredOrDefaultMs(uint32_t configuredInterval, uint32_t defaultInterval);
static uint32_t getConfiguredOrDefault(uint32_t configured, uint32_t defaultValue);
static uint32_t getConfiguredOrDefaultMsScaled(uint32_t configured, uint32_t defaultValue, uint32_t numOnlineNodes);
static uint8_t getConfiguredOrDefaultHopLimit(uint8_t configured);

private:
static float congestionScalingCoefficient(int numOnlineNodes)
Expand Down
8 changes: 8 additions & 0 deletions src/mesh/FloodingRouter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "FloodingRouter.h"
#include "../userPrefs.h"
#include "configuration.h"
#include "mesh-pb-constants.h"

Expand Down Expand Up @@ -46,6 +47,13 @@ void FloodingRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtas
meshtastic_MeshPacket *tosend = packetPool.allocCopy(*p); // keep a copy because we will be sending it

tosend->hop_limit--; // bump down the hop count
#if EVENT_MODE
if (tosend->hop_limit > 2) {
// if we are "correcting" the hop_limit, "correct" the hop_start by the same amount to preserve hops away.
tosend->hop_start -= (tosend->hop_limit - 2);
tosend->hop_limit = 2;
jp-bennett marked this conversation as resolved.
Show resolved Hide resolved
}
#endif

LOG_INFO("Rebroadcasting received floodmsg to neighbors\n");
// Note: we are careful to resend using the original senders node id
Expand Down
3 changes: 2 additions & 1 deletion src/mesh/ReliableRouter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ReliableRouter.h"
#include "Default.h"
#include "MeshModule.h"
#include "MeshTypes.h"
#include "configuration.h"
Expand All @@ -17,7 +18,7 @@ ErrorCode ReliableRouter::send(meshtastic_MeshPacket *p)
// message will rebroadcast. But asking for hop_limit 0 in that context means the client app has no preference on hop
// counts and we want this message to get through the whole mesh, so use the default.
if (p->hop_limit == 0) {
p->hop_limit = (config.lora.hop_limit >= HOP_MAX) ? HOP_MAX : config.lora.hop_limit;
p->hop_limit = Default::getConfiguredOrDefaultHopLimit(config.lora.hop_limit);
}

auto copy = packetPool.allocCopy(*p);
Expand Down
3 changes: 2 additions & 1 deletion src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#if !MESHTASTIC_EXCLUDE_MQTT
#include "mqtt/MQTT.h"
#endif
#include "Default.h"
#if ARCH_PORTDUINO
#include "platform/portduino/PortduinoGlue.h"
#endif
Expand Down Expand Up @@ -125,7 +126,7 @@ meshtastic_MeshPacket *Router::allocForSending()
p->which_payload_variant = meshtastic_MeshPacket_decoded_tag; // Assume payload is decoded at start.
p->from = nodeDB->getNodeNum();
p->to = NODENUM_BROADCAST;
p->hop_limit = (config.lora.hop_limit >= HOP_MAX) ? HOP_MAX : config.lora.hop_limit;
p->hop_limit = Default::getConfiguredOrDefaultHopLimit(config.lora.hop_limit);
p->id = generatePacketId();
p->rx_time =
getValidTime(RTCQualityFromNet); // Just in case we process the packet locally - make sure it has a valid timestamp
Expand Down
7 changes: 7 additions & 0 deletions src/modules/AdminModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c)
requiresReboot = true;
}
}
#if EVENT_MODE
// If we're in event mode, nobody is a Router or Repeater
if (config.device.role == meshtastic_Config_DeviceConfig_Role_ROUTER ||
config.device.role == meshtastic_Config_DeviceConfig_Role_REPEATER) {
config.device.role = meshtastic_Config_DeviceConfig_Role_CLIENT;
}
#endif
break;
case meshtastic_Config_position_tag:
LOG_INFO("Setting config: Position\n");
Expand Down
6 changes: 5 additions & 1 deletion src/modules/RoutingModule.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "RoutingModule.h"
#include "Default.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "Router.h"
Expand Down Expand Up @@ -50,12 +51,15 @@ uint8_t RoutingModule::getHopLimitForResponse(uint8_t hopStart, uint8_t hopLimit
// 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 config.lora.hop_limit; // Use the default hop limit
return Default::getConfiguredOrDefaultHopLimit(config.lora.hop_limit); // Use the default hop limit
}

RoutingModule::RoutingModule() : ProtobufModule("routing", meshtastic_PortNum_ROUTING_APP, &meshtastic_Routing_msg)
Expand Down
3 changes: 3 additions & 0 deletions userPrefs.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef _USERPREFS_
#define _USERPREFS_

// Uncomment and modify to set device defaults

// #define EVENT_MODE 1

// #define CONFIG_LORA_REGION_USERPREFS meshtastic_Config_LoRaConfig_RegionCode_US
// #define LORACONFIG_MODEM_PRESET_USERPREFS meshtastic_Config_LoRaConfig_ModemPreset_SHORT_FAST
// #define LORACONFIG_CHANNEL_NUM_USERPREFS 31
Expand Down
Loading