2021-06-27 10:56:28 -07:00
|
|
|
#include "FloodingRouter.h"
|
2024-07-29 20:16:47 -05:00
|
|
|
#include "../userPrefs.h"
|
2022-03-02 18:55:11 -08:00
|
|
|
#include "configuration.h"
|
2020-04-17 11:52:20 -07:00
|
|
|
#include "mesh-pb-constants.h"
|
|
|
|
|
|
2020-05-19 10:27:28 -07:00
|
|
|
FloodingRouter::FloodingRouter() {}
|
2020-04-17 11:52:20 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a packet on a suitable interface. This routine will
|
|
|
|
|
* later free() the packet to pool. This routine is not allowed to stall.
|
|
|
|
|
* If the txmit queue is full it might return an error
|
|
|
|
|
*/
|
2023-01-21 18:22:19 +01:00
|
|
|
ErrorCode FloodingRouter::send(meshtastic_MeshPacket *p)
|
2020-04-17 11:52:20 -07:00
|
|
|
{
|
2020-05-21 12:47:41 -07:00
|
|
|
// Add any messages _we_ send to the seen message list (so we will ignore all retransmissions we see)
|
2020-05-19 10:27:28 -07:00
|
|
|
wasSeenRecently(p); // FIXME, move this to a sniffSent method
|
2020-04-17 11:52:20 -07:00
|
|
|
|
|
|
|
|
return Router::send(p);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
bool FloodingRouter::shouldFilterReceived(const meshtastic_MeshPacket *p)
|
2020-04-17 11:52:20 -07:00
|
|
|
{
|
2020-05-25 11:55:42 -07:00
|
|
|
if (wasSeenRecently(p)) { // Note: this will also add a recent packet record
|
2024-11-04 12:16:25 -06:00
|
|
|
printPacket("Ignore dupe incoming msg", p);
|
2024-10-04 13:28:51 +02:00
|
|
|
rxDupe++;
|
2024-02-21 20:00:14 +01:00
|
|
|
if (config.device.role != meshtastic_Config_DeviceConfig_Role_ROUTER &&
|
2023-07-06 04:43:21 -07:00
|
|
|
config.device.role != meshtastic_Config_DeviceConfig_Role_REPEATER) {
|
|
|
|
|
// cancel rebroadcast of this message *if* there was already one, unless we're a router/repeater!
|
2024-10-04 13:28:51 +02:00
|
|
|
if (Router::cancelSending(p->from, p->id))
|
|
|
|
|
txRelayCanceled++;
|
2023-07-06 04:43:21 -07:00
|
|
|
}
|
2020-05-23 09:24:22 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
2020-05-18 17:35:23 -07:00
|
|
|
|
2020-05-23 09:24:22 -07:00
|
|
|
return Router::shouldFilterReceived(p);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 06:17:44 -05:00
|
|
|
bool FloodingRouter::isRebroadcaster()
|
|
|
|
|
{
|
|
|
|
|
return config.device.role != meshtastic_Config_DeviceConfig_Role_CLIENT_MUTE &&
|
|
|
|
|
config.device.rebroadcast_mode != meshtastic_Config_DeviceConfig_RebroadcastMode_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
void FloodingRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtastic_Routing *c)
|
2020-05-23 09:24:22 -07:00
|
|
|
{
|
2024-05-01 03:07:15 +02:00
|
|
|
bool isAckorReply = (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) && (p->decoded.request_id != 0);
|
2024-10-19 12:48:00 -05:00
|
|
|
if (isAckorReply && !isToUs(p) && !isBroadcast(p->to)) {
|
2024-05-01 03:07:15 +02:00
|
|
|
// do not flood direct message that is ACKed or replied to
|
2024-11-04 20:09:23 +08:00
|
|
|
LOG_DEBUG("Rxd an ACK/reply not for me, cancel rebroadcast");
|
2023-01-21 14:34:29 +01:00
|
|
|
Router::cancelSending(p->to, p->decoded.request_id); // cancel rebroadcast for this DM
|
|
|
|
|
}
|
2024-10-04 13:28:51 +02:00
|
|
|
if (!isToUs(p) && (p->hop_limit > 0) && !isFromUs(p)) {
|
2020-05-23 09:24:22 -07:00
|
|
|
if (p->id != 0) {
|
2024-10-12 06:17:44 -05:00
|
|
|
if (isRebroadcaster()) {
|
2023-01-21 18:22:19 +01:00
|
|
|
meshtastic_MeshPacket *tosend = packetPool.allocCopy(*p); // keep a copy because we will be sending it
|
2020-05-23 09:24:22 -07:00
|
|
|
|
2022-03-29 22:02:21 -07:00
|
|
|
tosend->hop_limit--; // bump down the hop count
|
2024-10-02 06:14:24 -05:00
|
|
|
#if USERPREFS_EVENT_MODE
|
2024-07-29 20:16:47 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2020-05-23 09:24:22 -07:00
|
|
|
|
2024-11-04 19:15:59 -06:00
|
|
|
LOG_INFO("Rebroadcast received floodmsg");
|
2022-03-29 22:02:21 -07:00
|
|
|
// Note: we are careful to resend using the original senders node id
|
|
|
|
|
// We are careful not to call our hooked version of send() - because we don't want to check this again
|
|
|
|
|
Router::send(tosend);
|
|
|
|
|
} else {
|
2024-11-04 19:15:59 -06:00
|
|
|
LOG_DEBUG("No rebroadcast: Role = CLIENT_MUTE or Rebroadcast Mode = NONE");
|
2022-03-29 22:02:21 -07:00
|
|
|
}
|
2020-05-23 09:24:22 -07:00
|
|
|
} else {
|
2024-11-04 12:16:25 -06:00
|
|
|
LOG_DEBUG("Ignore 0 id broadcast");
|
2020-05-23 09:24:22 -07:00
|
|
|
}
|
2020-05-18 17:35:23 -07:00
|
|
|
}
|
2020-05-23 09:24:22 -07:00
|
|
|
// handle the packet as normal
|
2021-02-17 13:06:23 +08:00
|
|
|
Router::sniffReceived(p, c);
|
2024-11-04 20:09:23 +08:00
|
|
|
}
|