Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ uint32_t Mesh::getCADFailRetryDelay() const {
return _rng->nextInt(1, 4)*120;
}

static bool isPeerDatagramType(uint8_t type) {
return type == PAYLOAD_TYPE_PATH || type == PAYLOAD_TYPE_REQ || type == PAYLOAD_TYPE_RESPONSE || type == PAYLOAD_TYPE_TXT_MSG;
}

void Mesh::rememberSelfAddressed(const Packet* packet) {
if (!isPeerDatagramType(packet->getPayloadType()) || packet->payload_len < 2) return;
if (!self_id.isHashMatch(&packet->payload[0])) return; // dest_hash differs from ours, seen table is enough

packet->calculatePacketHash(_self_addressed_sent[_self_addressed_next]);
_self_addressed_next = (_self_addressed_next + 1) % MAX_SELF_ADDRESSED_SENT;
}

bool Mesh::isOwnSelfAddressedEcho(const Packet* packet) {
uint8_t hash[MAX_HASH_SIZE];
packet->calculatePacketHash(hash);
for (int i = 0; i < MAX_SELF_ADDRESSED_SENT; i++) {
if (memcmp(hash, _self_addressed_sent[i], MAX_HASH_SIZE) == 0) return true;
}
return false;
}

int Mesh::searchPeersByHash(const uint8_t* hash) {
return 0; // not found
}
Expand Down Expand Up @@ -140,6 +161,12 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) {
MESH_DEBUG_PRINTLN("%s Mesh::onRecvPacket(): incomplete data packet", getLogDateTime());
} else if (!_tables->wasSeen(pkt)) {
_tables->markSeen(pkt);
if (dest_hash == src_hash && self_id.isHashMatch(&dest_hash) && isOwnSelfAddressedEcho(pkt)) {
// our own datagram to a peer that shares our hash, echoed back after its seen-table entry was
// evicted. It would decrypt as sent by that peer, so handle it like a seen packet: drop it.
MESH_DEBUG_PRINTLN("%s Mesh::onRecvPacket(): dropping echo of own datagram, hash=%02X", getLogDateTime(), (uint32_t)dest_hash);
break;
}
// NOTE: this is a 'first packet wins' impl. When receiving from multiple paths, the first to arrive wins.
// For flood mode, the path may not be the 'best' in terms of hops.
// FUTURE: could send back multiple paths, using createPathReturn(), and let sender choose which to use(?)
Expand Down Expand Up @@ -649,6 +676,7 @@ void Mesh::sendFlood(Packet* packet, uint32_t delay_millis, uint8_t path_hash_si
packet->setPathHashSizeAndCount(path_hash_size, 0);

_tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us
rememberSelfAddressed(packet);

uint8_t pri;
if (packet->getPayloadType() == PAYLOAD_TYPE_PATH) {
Expand Down Expand Up @@ -678,6 +706,7 @@ void Mesh::sendFlood(Packet* packet, uint16_t* transport_codes, uint32_t delay_m
packet->setPathHashSizeAndCount(path_hash_size, 0);

_tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us
rememberSelfAddressed(packet);

uint8_t pri;
if (packet->getPayloadType() == PAYLOAD_TYPE_PATH) {
Expand Down Expand Up @@ -711,6 +740,7 @@ void Mesh::sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uin
}
}
_tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us
rememberSelfAddressed(packet);
sendPacket(packet, pri, delay_millis);
}

Expand All @@ -721,6 +751,7 @@ void Mesh::sendZeroHop(Packet* packet, uint32_t delay_millis) {
packet->path_len = 0; // path_len of zero means Zero Hop

_tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us
rememberSelfAddressed(packet);

sendPacket(packet, 0, delay_millis);
}
Expand All @@ -734,6 +765,7 @@ void Mesh::sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay
packet->path_len = 0; // path_len of zero means Zero Hop

_tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us
rememberSelfAddressed(packet);

sendPacket(packet, 0, delay_millis);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ class MeshTables {
* \brief The next layer in the basic Dispatcher task, Mesh recognises the particular Payload TYPES,
* and provides virtual methods for sub-classes on handling incoming, and also preparing outbound Packets.
*/
#ifndef MAX_SELF_ADDRESSED_SENT
#define MAX_SELF_ADDRESSED_SENT 8
#endif

class Mesh : public Dispatcher {
RTCClock* _rtc;
RNG* _rng;
MeshTables* _tables;

// Hashes of recently sent datagrams whose dest_hash equals our own hash (a peer shares our first
// pub_key byte). The shared secret is symmetric, so an echo of such a packet decrypts as if that peer
// sent it. Kept apart from _tables, so normal traffic cannot evict these entries.
uint8_t _self_addressed_sent[MAX_SELF_ADDRESSED_SENT][MAX_HASH_SIZE];
uint8_t _self_addressed_next;

void rememberSelfAddressed(const Packet* packet);
bool isOwnSelfAddressedEcho(const Packet* packet);
void removeSelfFromPath(Packet* packet);
void routeDirectRecvAcks(Packet* packet, uint32_t delay_millis);
//void routeRecvAcks(Packet* packet, uint32_t delay_millis);
Expand Down Expand Up @@ -169,6 +181,8 @@ class Mesh : public Dispatcher {
Mesh(Radio& radio, MillisecondClock& ms, RNG& rng, RTCClock& rtc, PacketManager& mgr, MeshTables& tables)
: Dispatcher(radio, ms, mgr), _rng(&rng), _rtc(&rtc), _tables(&tables)
{
memset(_self_addressed_sent, 0, sizeof(_self_addressed_sent));
_self_addressed_next = 0;
}

MeshTables* getTables() const { return _tables; }
Expand Down