* squashed

This commit is contained in:
Scott Powell committed 2026-10-19 10:41:51 +11:00
1 parent 428a95c643
commit fe298318be
19 files changed
+755 -204

No files matched your search

+35
View File
@@ -188,6 +188,12 @@ void MyMesh::writeContactRespFrame(uint8_t code, const ContactInfo &contact) {
i += 4;
memcpy(&out_frame[i], &contact.lastmod, 4);
i += 4;
// new fields
out_frame[i++] = contact.direct_mode;
out_frame[i++] = contact.next_seq_no;
memcpy(&out_frame[i], &contact.curr_session_id, 2);
i += 2;
_serial->writeFrame(out_frame, i);
}
@@ -212,6 +218,12 @@ void MyMesh::updateContactFromFrame(ContactInfo &contact, uint32_t& last_mod, co
i += 4;
if (len >= i + 4) {
memcpy(&last_mod, &frame[i], 4);
if (len >= i + 4) {
contact.direct_mode = frame[i++];
contact.next_seq_no = frame[i++];
memcpy(&contact.curr_session_id, &frame[i], 2);
i += 2;
}
}
}
}
@@ -488,6 +500,29 @@ bool MyMesh::allowPacketForward(const mesh::Packet* packet) {
return _prefs.isRepeatEn();
}
mesh::Packet* MyMesh::obtainPacketFor(ContactInfo& contact) {
auto pkt = obtainNewPacket();
if (pkt) {
if (contact.out_path_len == OUT_PATH_UNKNOWN) {
pkt->header |= ROUTE_TYPE_FLOOD;
// NOTE: final route type, transport codes, path_len is set later in sendFloodScoped()
} else {
uint8_t crypto_mode = contact.direct_mode & 0x0F;
if (crypto_mode == CRYPTO_NEW_DIRECT) {
uint8_t dm_mode = contact.direct_mode >> 4; // upper 4 bits is which DM_MODE_* to use
pkt->header |= ROUTE_TYPE_TRANSPORT_DIRECT;
pkt->transport_codes[0] = (((uint16_t)dm_mode) << 14) | contact.next_seq_no++; // implicit, cursor = 0
pkt->transport_codes[1] = (contact.curr_session_id & ~(3u << 14));
pkt->transport_codes[1] |= (CRYPTO_NEW_DIRECT << 14);
} else {
pkt->header |= ROUTE_TYPE_DIRECT;
}
pkt->path_len = mesh::Packet::copyPath(pkt->path, contact.out_path, contact.out_path_len);
}
}
return pkt;
}
void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis) {
if (scope.isNull()) {
sendFlood(pkt, delay_millis, _prefs.path_hash_mode + 1);
+3 -1
View File
@@ -4,7 +4,7 @@
#include <Mesh.h>
/*------------ Frame Protocol --------------*/
#define FIRMWARE_VER_CODE 14
#define FIRMWARE_VER_CODE 15
#ifndef FIRMWARE_BUILD_DATE
#define FIRMWARE_BUILD_DATE "15 Oct 2026"
@@ -140,6 +140,8 @@ protected:
bool filterRecvFloodPacket(mesh::Packet* packet) override;
bool allowPacketForward(const mesh::Packet* packet) override;
mesh::Packet* obtainPacketFor(ContactInfo& contact) override;
void sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis);
void sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0) override;
void sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis=0) override;
+86 -21
View File
@@ -60,6 +60,29 @@
#define LAZY_CONTACTS_WRITE_DELAY 5000
mesh::Packet* MyMesh::obtainPacketFor(ClientInfo& client, const uint8_t* out_path, uint8_t out_path_len) {
auto pkt = obtainNewPacket();
if (pkt) {
if (out_path_len == OUT_PATH_UNKNOWN) {
pkt->header |= ROUTE_TYPE_FLOOD;
// NOTE: final route type, transport codes, path_len is set later in sendFloodScoped()
} else {
uint8_t crypto_mode = client.direct_mode & 0x0F;
if (crypto_mode == CRYPTO_NEW_DIRECT) {
uint8_t dm_mode = client.direct_mode >> 4; // upper 4 bits is which DM_MODE_* to use
pkt->header |= ROUTE_TYPE_TRANSPORT_DIRECT;
pkt->transport_codes[0] = (((uint16_t)dm_mode) << 14) | client.next_seq_no++; // implicit, cursor = 0
pkt->transport_codes[1] = (client.curr_session_id & ~(3u << 14));
pkt->transport_codes[1] |= (CRYPTO_NEW_DIRECT << 14);
} else {
pkt->header |= ROUTE_TYPE_DIRECT;
}
pkt->path_len = mesh::Packet::copyPath(pkt->path, out_path, out_path_len);
}
}
return pkt;
}
void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr) {
#if MAX_NEIGHBOURS // check if neighbours enabled
// find existing neighbour, else use least recently updated
@@ -123,6 +146,11 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr
client->permissions |= perms;
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
// start new session
getRNG()->random((uint8_t *) &client->curr_session_id, sizeof(client->curr_session_id));
client->next_seq_no = 0;
client->direct_mode = CRYPTO_LEGACY; // TODO: can Client send a new request param, indicating new-direct support?
if (perms != PERM_ACL_GUEST) { // keep number of FS writes to a minimum
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
}
@@ -140,8 +168,9 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr
reply_data[7] = client->permissions;
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
reply_data[12] = FIRMWARE_VER_LEVEL; // New field
reply_data[13] = (1 << CRYPTO_NEW_DIRECT) | (1 << CRYPTO_LEGACY); // New bitfield: which crypto-types supported
return 13; // reply length
return 14; // reply length
}
uint8_t MyMesh::handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data) {
@@ -431,6 +460,16 @@ void MyMesh::sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, ui
}
}
bool MyMesh::isHashNeighbor(const uint8_t* hash, uint8_t hash_size) {
#if MAX_NEIGHBOURS
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
auto n = &neighbours[i];
if (n->heard_timestamp > 0 && n->id.isHashMatch(hash, hash_size)) return true;
}
#endif
return false;
}
bool MyMesh::allowPacketForward(const mesh::Packet *packet) {
if (_prefs.disable_fwd) return false;
if (packet->isRouteFlood()
@@ -569,7 +608,7 @@ mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) {
}
void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const mesh::Identity &sender,
uint8_t *data, size_t len) {
uint8_t *data, size_t len, uint8_t crypto_type) {
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin
// client (unknown at this stage)
uint32_t timestamp;
@@ -595,9 +634,18 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
// a DIRECT login can reply via the stored out_path, as onPeerDataRecv() does for REQ
ClientInfo* client = acl.getClient(sender.pub_key, PUB_KEY_SIZE);
bool have_out_path = client != NULL && client->out_path_len != OUT_PATH_UNKNOWN;
if (client == NULL) {
MESH_DEBUG_PRINTLN("onAnonDataRecv: client ACL cannot find slot!"); // This shouldn't happen!
return;
}
auto route = mesh::chooseReplyRoute(packet->isRouteFlood(), reply_path_len != 0xFF, have_out_path);
if (crypto_type == CRYPTO_NEW_DIRECT) {
if (client->direct_mode == CRYPTO_LEGACY) {
client->direct_mode = (DM_MODE_SEQ_NORMAL << 4) | CRYPTO_NEW_DIRECT; // have received from Client in new scheme, so they must support it
}
}
auto route = mesh::chooseReplyRoute(packet->isRouteFlood(), reply_path_len != 0xFF, client->out_path_len != OUT_PATH_UNKNOWN);
if (route == mesh::REPLY_ROUTE_PATH_RETURN) {
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
@@ -607,13 +655,17 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
return;
}
mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, secret, reply_data, reply_len);
if (reply == NULL) return;
mesh::Packet* reply;
if (route == mesh::REPLY_ROUTE_DIRECT_SUPPLIED) {
sendDirect(reply, reply_path, reply_path_len, SERVER_RESPONSE_DELAY);
} else if (route == mesh::REPLY_ROUTE_DIRECT_OUT_PATH) {
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
reply = obtainPacketFor(*client, reply_path, reply_path_len);
} else {
reply = obtainPacketFor(*client, client->out_path, client->out_path_len);
}
if (reply == NULL) return; // packet pool exhausted
encodeDatagram(reply, PAYLOAD_TYPE_RESPONSE, sender, secret, reply_data, reply_len);
if (reply->isRouteDirect()) {
send(reply, SERVER_RESPONSE_DELAY);
} else {
sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
}
@@ -661,7 +713,7 @@ void MyMesh::onAdvertRecv(mesh::Packet *packet, const mesh::Identity &id, uint32
}
void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, const uint8_t *secret,
uint8_t *data, size_t len) {
uint8_t *data, size_t len, uint8_t crypto_type) {
int i = matching_peer_indexes[sender_idx];
if (i < 0 || i >= acl.getNumClients()) { // get from our known_clients table (sender SHOULD already be known in this context)
MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid peer idx: %d", i);
@@ -669,6 +721,12 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
}
ClientInfo* client = acl.getClientByIdx(i);
if (crypto_type == CRYPTO_NEW_DIRECT) {
if (client->direct_mode == CRYPTO_LEGACY) {
client->direct_mode = (DM_MODE_SEQ_NORMAL << 4) | CRYPTO_NEW_DIRECT; // have received from Client in new scheme, so they must support it
}
}
if (type == PAYLOAD_TYPE_REQ) { // request (from a Known admin client!)
uint32_t timestamp;
memcpy(&timestamp, data, 4);
@@ -686,11 +744,11 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len);
if (path) sendFloodReply(path, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
} else {
mesh::Packet *reply =
createDatagram(PAYLOAD_TYPE_RESPONSE, client->id, secret, reply_data, reply_len);
auto reply = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (reply) {
if (client->out_path_len != OUT_PATH_UNKNOWN) { // we have an out_path, so send DIRECT
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
encodeDatagram(reply, PAYLOAD_TYPE_RESPONSE, client->id, secret, reply_data, reply_len);
if (reply->isRouteDirect()) {
send(reply, SERVER_RESPONSE_DELAY);
} else {
sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
}
@@ -714,7 +772,7 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
// len can be > original length, but 'text' will be padded with zeroes
data[len] = 0; // need to make a C string again, with null terminator
if (flags == TXT_TYPE_PLAIN) { // for legacy CLI, send Acks
if (flags == TXT_TYPE_PLAIN) { // for legacy CLI, send Acks <==== TODO, DEPRECATE?
uint32_t ack_hash; // calc truncated hash of the message timestamp + text + sender pub_key, to prove
// to sender that we got it
mesh::Utils::sha256((uint8_t *)&ack_hash, 4, data, 5 + strlen((char *)&data[5]), client->id.pub_key,
@@ -748,12 +806,13 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
memcpy(temp, &timestamp, 4); // mostly an extra blob to help make packet_hash unique
temp[4] = (TXT_TYPE_CLI_DATA << 2); // NOTE: legacy was: TXT_TYPE_PLAIN
auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len);
auto reply = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (reply) {
if (client->out_path_len == OUT_PATH_UNKNOWN) {
sendFloodReply(reply, CLI_REPLY_DELAY_MILLIS, packet->getPathHashSize());
encodeDatagram(reply, PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len);
if (reply->isRouteDirect()) {
send(reply, CLI_REPLY_DELAY_MILLIS);
} else {
sendDirect(reply, client->out_path, client->out_path_len, CLI_REPLY_DELAY_MILLIS);
sendFloodReply(reply, CLI_REPLY_DELAY_MILLIS, packet->getPathHashSize());
}
}
}
@@ -764,7 +823,7 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
}
bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t *secret, uint8_t *path,
uint8_t path_len, uint8_t extra_type, uint8_t *extra, uint8_t extra_len) {
uint8_t path_len, uint8_t extra_type, uint8_t *extra, uint8_t extra_len, uint8_t crypto_type) {
// TODO: prevent replay attacks
int i = matching_peer_indexes[sender_idx];
@@ -772,6 +831,12 @@ bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t
MESH_DEBUG_PRINTLN("PATH to client, path_len=%d", (uint32_t)path_len);
auto client = acl.getClientByIdx(i);
if (crypto_type == CRYPTO_NEW_DIRECT) {
if (client->direct_mode == CRYPTO_LEGACY) {
client->direct_mode = (DM_MODE_SEQ_NORMAL << 4) | CRYPTO_NEW_DIRECT; // have received from Client in new scheme, so they must support it
}
}
// store a copy of path, for sendDirect()
client->out_path_len = mesh::Packet::copyPath(client->out_path, path, path_len);
client->last_activity = getRTCClock()->getCurrentTime();
+7 -3
View File
@@ -132,6 +132,9 @@ protected:
return _prefs.airtime_factor;
}
// sets up an outgoing Packet with appropriate route_type, transport codes and path
mesh::Packet* obtainPacketFor(ClientInfo& client, const uint8_t* out_path, uint8_t out_path_len);
bool allowPacketForward(const mesh::Packet* packet) override;
const char* getLogDateTime() override;
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
@@ -156,6 +159,7 @@ protected:
uint8_t getExtraAckTransmitCount() const override {
return _prefs.multi_acks;
}
bool isHashNeighbor(const uint8_t* hash, uint8_t hash_size) override;
#if ENV_INCLUDE_GPS == 1
void applyGpsPrefs() {
@@ -165,12 +169,12 @@ protected:
mesh::DispatcherAction onRecvPacket(mesh::Packet* pkt) override;
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len, uint8_t crypto_type) override;
int searchPeersByHash(const uint8_t* hash) override;
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len);
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len, uint8_t crypto_type) override;
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len, uint8_t crypto_type) override;
void onControlDataRecv(mesh::Packet* packet) override;
void sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, uint8_t path_hash_size);
+82 -25
View File
@@ -38,6 +38,29 @@ struct ServerStats {
uint16_t n_posted, n_post_push;
};
mesh::Packet* MyMesh::obtainPacketFor(ClientInfo& client, const uint8_t* out_path, uint8_t out_path_len) {
auto pkt = obtainNewPacket();
if (pkt) {
if (out_path_len == OUT_PATH_UNKNOWN) {
pkt->header |= ROUTE_TYPE_FLOOD;
// NOTE: final route type, transport codes, path_len is set later in sendFloodScoped()
} else {
uint8_t crypto_mode = client.direct_mode & 0x0F;
if (crypto_mode == CRYPTO_NEW_DIRECT) {
uint8_t dm_mode = client.direct_mode >> 4; // upper 4 bits is which DM_MODE_* to use
pkt->header |= ROUTE_TYPE_TRANSPORT_DIRECT;
pkt->transport_codes[0] = (((uint16_t)dm_mode) << 14) | client.next_seq_no++; // implicit, cursor = 0
pkt->transport_codes[1] = (client.curr_session_id & ~(3u << 14));
pkt->transport_codes[1] |= (CRYPTO_NEW_DIRECT << 14);
} else {
pkt->header |= ROUTE_TYPE_DIRECT;
}
pkt->path_len = mesh::Packet::copyPath(pkt->path, out_path, out_path_len);
}
}
return pkt;
}
void MyMesh::addPost(ClientInfo *client, const char *postData) {
storePost(client->id, postData);
}
@@ -88,17 +111,17 @@ void MyMesh::pushPostToClient(ClientInfo *client, PostInfo &post) {
mesh::Utils::sha256((uint8_t *)&client->extra.room.pending_ack, 4, reply_data, len, client->id.pub_key, PUB_KEY_SIZE);
client->extra.room.push_post_timestamp = post.post_timestamp;
auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, client->shared_secret, reply_data, len);
auto reply = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (reply) {
if (client->out_path_len == OUT_PATH_UNKNOWN) {
encodeDatagram(reply, PAYLOAD_TYPE_TXT_MSG, client->id, client->shared_secret, reply_data, len);
if (reply->isRouteDirect()) {
send(reply);
uint8_t path_hash_count = client->out_path_len & 63;
client->extra.room.ack_timeout = futureMillis(PUSH_TIMEOUT_BASE + PUSH_ACK_TIMEOUT_FACTOR * (path_hash_count + 1));
} else {
unsigned long delay_millis = 0;
sendFloodScoped(default_scope, reply, delay_millis, _prefs.path_hash_mode + 1); // REVISIT
client->extra.room.ack_timeout = futureMillis(PUSH_ACK_TIMEOUT_FLOOD);
} else {
sendDirect(reply, client->out_path, client->out_path_len);
uint8_t path_hash_count = client->out_path_len & 63;
client->extra.room.ack_timeout = futureMillis(PUSH_TIMEOUT_BASE + PUSH_ACK_TIMEOUT_FACTOR * (path_hash_count + 1));
}
_num_post_pushes++; // stats
} else {
@@ -322,7 +345,7 @@ mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) {
}
void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const mesh::Identity &sender,
uint8_t *data, size_t len) {
uint8_t *data, size_t len, uint8_t crypto_type) {
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin
// client (unknown at this stage)
uint32_t sender_timestamp, sender_sync_since;
@@ -372,6 +395,11 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
client->permissions |= perm;
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
// start new session
getRNG()->random((uint8_t *) &client->curr_session_id, sizeof(client->curr_session_id));
client->next_seq_no = 0;
client->direct_mode = CRYPTO_LEGACY; // TODO: can Client send a new request param, indicating new-direct support?
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
}
@@ -379,6 +407,12 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
client->out_path_len = OUT_PATH_UNKNOWN; // need to rediscover out_path
}
if (crypto_type == CRYPTO_NEW_DIRECT) {
if (client->direct_mode == CRYPTO_LEGACY) {
client->direct_mode = (DM_MODE_SEQ_NORMAL << 4) | CRYPTO_NEW_DIRECT; // have received from Client in new scheme, so they must support it
}
}
uint32_t now = getRTCClock()->getCurrentTimeUnique();
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
// TODO: maybe reply with count of messages waiting to be synced for THIS client?
@@ -389,19 +423,21 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
reply_data[7] = client->permissions; // NEW
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
reply_data[12] = FIRMWARE_VER_LEVEL; // New field
reply_data[13] = (1 << CRYPTO_NEW_DIRECT) | (1 << CRYPTO_LEGACY); // New bitfield: which crypto-types supported
next_push = futureMillis(PUSH_NOTIFY_DELAY_MILLIS); // delay next push, give RESPONSE packet time to arrive first
if (packet->isRouteFlood()) {
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
mesh::Packet *path = createPathReturn(sender, client->shared_secret, packet->path, packet->path_len,
PAYLOAD_TYPE_RESPONSE, reply_data, 13);
PAYLOAD_TYPE_RESPONSE, reply_data, 14);
if (path) sendFloodReply(path, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
} else {
mesh::Packet *reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, client->shared_secret, reply_data, 13);
auto reply = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (reply) {
if (client->out_path_len != OUT_PATH_UNKNOWN) { // we have an out_path, so send DIRECT
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
encodeDatagram(reply, PAYLOAD_TYPE_RESPONSE, sender, client->shared_secret, reply_data, 14);
if (reply->isRouteDirect()) {
send(reply, SERVER_RESPONSE_DELAY);
} else {
sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
}
@@ -431,13 +467,19 @@ void MyMesh::getPeerSharedSecret(uint8_t *dest_secret, int peer_idx) {
}
void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, const uint8_t *secret,
uint8_t *data, size_t len) {
uint8_t *data, size_t len, uint8_t crypto_type) {
int i = matching_peer_indexes[sender_idx];
if (i < 0 || i >= acl.getNumClients()) { // get from our known_clients table (sender SHOULD already be known in this context)
MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid peer idx: %d", i);
return;
}
auto client = acl.getClientByIdx(i);
if (crypto_type == CRYPTO_NEW_DIRECT) {
if (client->direct_mode == CRYPTO_LEGACY) {
client->direct_mode = (DM_MODE_SEQ_NORMAL << 4) | CRYPTO_NEW_DIRECT; // have received from Client in new scheme, so they must support it
}
}
if (type == PAYLOAD_TYPE_TXT_MSG && len > 5) { // a CLI command or new Post
uint32_t sender_timestamp;
memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
@@ -498,13 +540,19 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
} else {
uint32_t d = TXT_ACK_DELAY;
if (getExtraAckTransmitCount() > 0) {
mesh::Packet *a1 = createMultiAck(ack_hash, 1);
if (a1) sendDirect(a1, client->out_path, client->out_path_len, d);
mesh::Packet *a1 = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (a1) {
encodeMultiAck(a1, (uint8_t *) &ack_hash, 4, 1);
send(a1, d);
}
d += 300;
}
mesh::Packet *a2 = createAck(ack_hash);
if (a2) sendDirect(a2, client->out_path, client->out_path_len, d);
mesh::Packet *a2 = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (a2) {
encodeAck(a2, (uint8_t *)&ack_hash, 4);
send(a2, d);
}
delay_millis = d + REPLY_DELAY_MILLIS;
}
} else {
@@ -523,12 +571,13 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
// mesh::Utils::sha256((uint8_t *)&expected_ack_crc, 4, temp, 5 + text_len, self_id.pub_key,
// PUB_KEY_SIZE);
auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len);
auto reply = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (reply) {
if (client->out_path_len == OUT_PATH_UNKNOWN) {
sendFloodReply(reply, delay_millis + SERVER_RESPONSE_DELAY, packet->getPathHashSize());
encodeDatagram(reply, PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len);
if (reply->isRouteDirect()) {
send(reply, delay_millis + SERVER_RESPONSE_DELAY);
} else {
sendDirect(reply, client->out_path, client->out_path_len, delay_millis + SERVER_RESPONSE_DELAY);
sendFloodReply(reply, delay_millis + SERVER_RESPONSE_DELAY, packet->getPathHashSize());
}
}
}
@@ -583,10 +632,11 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len);
if (path) sendFloodReply(path, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
} else {
mesh::Packet *reply = createDatagram(PAYLOAD_TYPE_RESPONSE, client->id, secret, reply_data, reply_len);
auto reply = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (reply) {
if (client->out_path_len != OUT_PATH_UNKNOWN) { // we have an out_path, so send DIRECT
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
encodeDatagram(reply, PAYLOAD_TYPE_RESPONSE, client->id, secret, reply_data, reply_len);
if (reply->isRouteDirect()) {
send(reply, SERVER_RESPONSE_DELAY);
} else {
sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
}
@@ -599,13 +649,20 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
}
bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t *secret, uint8_t *path,
uint8_t path_len, uint8_t extra_type, uint8_t *extra, uint8_t extra_len) {
uint8_t path_len, uint8_t extra_type, uint8_t *extra, uint8_t extra_len, uint8_t crypto_type) {
// TODO: prevent replay attacks
int i = matching_peer_indexes[sender_idx];
if (i >= 0 && i < acl.getNumClients()) { // get from our known_clients table (sender SHOULD already be known in this context)
MESH_DEBUG_PRINTLN("PATH to client, path_len=%d", (uint32_t)path_len);
auto client = acl.getClientByIdx(i);
if (crypto_type == CRYPTO_NEW_DIRECT) {
if (client->direct_mode == CRYPTO_LEGACY) {
client->direct_mode = (DM_MODE_SEQ_NORMAL << 4) | CRYPTO_NEW_DIRECT; // have received from Client in new scheme, so they must support it
}
}
client->out_path_len = mesh::Packet::copyPath(client->out_path, path, path_len); // store a copy of path, for sendDirect()
client->last_activity = getRTCClock()->getCurrentTime();
} else {
+6 -3
View File
@@ -158,12 +158,15 @@ protected:
mesh::DispatcherAction onRecvPacket(mesh::Packet* pkt) override;
// sets up an outgoing Packet with appropriate route_type, transport codes and path
mesh::Packet* obtainPacketFor(ClientInfo& client, const uint8_t* out_path, uint8_t out_path_len);
bool allowPacketForward(const mesh::Packet* packet) override;
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len, uint8_t crypto_type) override;
int searchPeersByHash(const uint8_t* hash) override ;
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len, uint8_t crypto_type) override;
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len, uint8_t crypto_type) override;
void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
#if ENV_INCLUDE_GPS == 1
+77 -24
View File
@@ -101,6 +101,29 @@ static float findTelemValue(const uint8_t* buf, uint8_t size, uint8_t channel, u
/* ------------------ end Cayenne LPP helpers ----------------------*/
mesh::Packet* SensorMesh::obtainPacketFor(ClientInfo& client, const uint8_t* out_path, uint8_t out_path_len) {
auto pkt = obtainNewPacket();
if (pkt) {
if (out_path_len == OUT_PATH_UNKNOWN) {
pkt->header |= ROUTE_TYPE_FLOOD;
// NOTE: final route type, transport codes, path_len is set later in sendFloodScoped()
} else {
uint8_t crypto_mode = client.direct_mode & 0x0F;
if (crypto_mode == CRYPTO_NEW_DIRECT) {
uint8_t dm_mode = client.direct_mode >> 4; // upper 4 bits is which DM_MODE_* to use
pkt->header |= ROUTE_TYPE_TRANSPORT_DIRECT;
pkt->transport_codes[0] = (((uint16_t)dm_mode) << 14) | client.next_seq_no++; // implicit, cursor = 0
pkt->transport_codes[1] = (client.curr_session_id & ~(3u << 14));
pkt->transport_codes[1] |= (CRYPTO_NEW_DIRECT << 14);
} else {
pkt->header |= ROUTE_TYPE_DIRECT;
}
pkt->path_len = mesh::Packet::copyPath(pkt->path, out_path, out_path_len);
}
}
return pkt;
}
bool SensorMesh::telemHasChanged(ClientInfo* c) {
auto buf = telemetry.getBuffer();
uint8_t size = telemetry.getSize();
@@ -259,7 +282,7 @@ mesh::Packet* SensorMesh::createSelfAdvert() {
return createAdvert(self_id, app_data, app_data_len);
}
void SensorMesh::sendAlert(const ClientInfo* c, Trigger* t) {
void SensorMesh::sendAlert(ClientInfo* c, Trigger* t) {
int text_len = strlen(t->text);
uint8_t data[MAX_PACKET_PAYLOAD];
@@ -271,10 +294,11 @@ void SensorMesh::sendAlert(const ClientInfo* c, Trigger* t) {
mesh::Utils::sha256((uint8_t *)&t->expected_acks[t->attempt], 4, data, 5 + text_len, self_id.pub_key, PUB_KEY_SIZE);
t->attempt++;
auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, c->id, c->shared_secret, data, 5 + text_len);
auto pkt = obtainPacketFor(*c, c->out_path, c->out_path_len);
if (pkt) {
if (c->out_path_len != OUT_PATH_UNKNOWN) { // we have an out_path, so send DIRECT
sendDirect(pkt, c->out_path, c->out_path_len);
encodeDatagram(pkt, PAYLOAD_TYPE_TXT_MSG, c->id, c->shared_secret, data, 5 + text_len);
if (pkt->isRouteDirect()) {
send(pkt);
} else {
unsigned long delay_millis = 0;
sendFloodScoped(default_scope, pkt, delay_millis, _prefs.path_hash_mode + 1);
@@ -409,8 +433,9 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
reply_data[7] = client->permissions;
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
reply_data[12] = FIRMWARE_VER_LEVEL;
reply_data[13] = (1 << CRYPTO_NEW_DIRECT) | (1 << CRYPTO_LEGACY); // New bitfield: which crypto-types supported
return 13; // reply length
return 14; // reply length
}
void SensorMesh::handleCommand(ClientInfo* from, uint32_t sender_timestamp, char* command, char* reply) {
@@ -535,7 +560,7 @@ mesh::DispatcherAction SensorMesh::onRecvPacket(mesh::Packet* pkt) {
return Mesh::onRecvPacket(pkt);
}
void SensorMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) {
void SensorMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len, uint8_t crypto_type) {
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
uint32_t timestamp;
memcpy(&timestamp, data, 4);
@@ -558,8 +583,24 @@ void SensorMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, con
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len);
if (path) sendFloodReply(path, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
} else {
mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, secret, reply_data, reply_len);
if (reply) sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
auto client = acl.getClient(sender.pub_key, PUB_KEY_SIZE);
if (client == NULL) {
MESH_DEBUG_PRINTLN("onAnonDataRecv: client ACL cannot find slot!"); // This shouldn't happen!
return;
}
if (crypto_type == CRYPTO_NEW_DIRECT) {
client->direct_mode = (DM_MODE_SEQ_NORMAL << 4) | CRYPTO_NEW_DIRECT; // have received from Client in new scheme, so they must support it
}
auto reply = obtainPacketFor(*client, client->out_path, client->out_path_len);
if (reply) {
encodeDatagram(reply, PAYLOAD_TYPE_RESPONSE, sender, secret, reply_data, reply_len);
if (reply->isRouteDirect()) {
send(reply, SERVER_RESPONSE_DELAY);
} else {
sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
}
}
}
}
}
@@ -584,24 +625,30 @@ void SensorMesh::getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) {
}
}
void SensorMesh::sendAckTo(const ClientInfo& dest, uint32_t ack_hash, uint8_t path_hash_size) {
void SensorMesh::sendAckTo(ClientInfo& dest, uint32_t ack_hash, uint8_t path_hash_size) {
if (dest.out_path_len == OUT_PATH_UNKNOWN) {
mesh::Packet* ack = createAck(ack_hash);
if (ack) sendFloodScoped(default_scope, ack, TXT_ACK_DELAY, path_hash_size);
} else {
uint32_t d = TXT_ACK_DELAY;
if (getExtraAckTransmitCount() > 0) {
mesh::Packet* a1 = createMultiAck(ack_hash, 1);
if (a1) sendDirect(a1, dest.out_path, dest.out_path_len, d);
mesh::Packet* a1 = obtainPacketFor(dest, dest.out_path, dest.out_path_len);
if (a1) {
encodeMultiAck(a1, (uint8_t *) &ack_hash, 4, 1);
send(a1, d);
}
d += 300;
}
mesh::Packet* a2 = createAck(ack_hash);
if (a2) sendDirect(a2, dest.out_path, dest.out_path_len, d);
mesh::Packet* a2 = obtainPacketFor(dest, dest.out_path, dest.out_path_len);
if (a2) {
encodeAck(a2, (uint8_t *) &ack_hash, 4);
send(a2, d);
}
}
}
void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) {
void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len, uint8_t crypto_type) {
int i = matching_peer_indexes[sender_idx];
if (i < 0 || i >= acl.getNumClients()) {
MESH_DEBUG_PRINTLN("onPeerDataRecv: Invalid sender idx: %d", i);
@@ -609,6 +656,9 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
}
ClientInfo* from = acl.getClientByIdx(i);
if (crypto_type == CRYPTO_NEW_DIRECT) {
from->direct_mode = (DM_MODE_SEQ_NORMAL << 4) | CRYPTO_NEW_DIRECT; // have received from Client in new scheme, so they must support it
}
if (type == PAYLOAD_TYPE_REQ) { // request (from a known contact)
uint32_t timestamp;
@@ -627,10 +677,11 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len);
if (path) sendFloodReply(path, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
} else {
mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, from->id, secret, reply_data, reply_len);
auto reply = obtainPacketFor(*from, from->out_path, from->out_path_len);
if (reply) {
if (from->out_path_len != OUT_PATH_UNKNOWN) { // we have an out_path, so send DIRECT
sendDirect(reply, from->out_path, from->out_path_len, SERVER_RESPONSE_DELAY);
encodeDatagram(reply, PAYLOAD_TYPE_RESPONSE, from->id, secret, reply_data, reply_len);
if (reply->isRouteDirect()) {
send(reply, SERVER_RESPONSE_DELAY);
} else {
sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
}
@@ -682,12 +733,13 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
memcpy(temp, &timestamp, 4); // mostly an extra blob to help make packet_hash unique
temp[4] = (TXT_TYPE_CLI_DATA << 2);
auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, from->id, secret, temp, 5 + text_len);
auto reply = obtainPacketFor(*from, from->out_path, from->out_path_len);
if (reply) {
if (from->out_path_len == OUT_PATH_UNKNOWN) {
sendFloodReply(reply, CLI_REPLY_DELAY_MILLIS, packet->getPathHashSize());
encodeDatagram(reply, PAYLOAD_TYPE_TXT_MSG, from->id, secret, temp, 5 + text_len);
if (reply->isRouteDirect()) {
send(reply, CLI_REPLY_DELAY_MILLIS);
} else {
sendDirect(reply, from->out_path, from->out_path_len, CLI_REPLY_DELAY_MILLIS);
sendFloodReply(reply, CLI_REPLY_DELAY_MILLIS, packet->getPathHashSize());
}
}
}
@@ -1055,10 +1107,11 @@ void SensorMesh::loop() {
memcpy(&reply_data[4], &timestamp, 4);
memcpy(&reply_data[8], telemetry.getBuffer(), tlen);
mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, c->id, c->shared_secret, reply_data, 8 + tlen);
auto reply = obtainPacketFor(*c, c->out_path, c->out_path_len);
if (reply) {
if (c->out_path_len != OUT_PATH_UNKNOWN) { // we have an out_path, so send DIRECT
sendDirect(reply, c->out_path, c->out_path_len, 0);
encodeDatagram(reply, PAYLOAD_TYPE_RESPONSE, c->id, c->shared_secret, reply_data, 8 + tlen);
if (reply->isRouteDirect()) {
send(reply);
} else {
sendFloodScoped(scope, reply, 0, _prefs.path_hash_mode + 1);
}
+7 -4
View File
@@ -119,6 +119,9 @@ protected:
virtual int querySeriesData(uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg dest[], int max_num) = 0; // for app to implement
virtual bool handleCustomCommand(uint32_t sender_timestamp, char* command, char* reply) { return false; }
// sets up an outgoing Packet with appropriate route_type, transport codes and path
mesh::Packet* obtainPacketFor(ClientInfo& client, const uint8_t* out_path, uint8_t out_path_len);
// Mesh overrides
float getAirtimeBudgetFactor() const override;
bool allowPacketForward(const mesh::Packet* packet) override;
@@ -129,16 +132,16 @@ protected:
bool getCADEnabled() const override;
int getAGCResetInterval() const override;
mesh::DispatcherAction onRecvPacket(mesh::Packet* pkt) override;
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len, uint8_t crypto_type) override;
int searchPeersByHash(const uint8_t* hash) override;
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len, uint8_t crypto_type) override;
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
void onControlDataRecv(mesh::Packet* packet) override;
void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
virtual bool handleIncomingMsg(ClientInfo& from, uint32_t timestamp, uint8_t* data, uint8_t flags, size_t len);
void sendAckTo(const ClientInfo& dest, uint32_t ack_hash, uint8_t path_hash_size=1);
void sendAckTo(ClientInfo& dest, uint32_t ack_hash, uint8_t path_hash_size=1);
private:
FILESYSTEM* _fs;
unsigned long next_local_advert, next_flood_advert;
@@ -169,7 +172,7 @@ private:
uint8_t handleRequest(ClientInfo* from, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len);
mesh::Packet* createSelfAdvert();
void sendAlert(const ClientInfo* c, Trigger* t);
void sendAlert(ClientInfo* c, Trigger* t);
#if ENV_INCLUDE_GPS == 1
void applyGpsPrefs() {