multichannel code is done! (only basic testing completed though)

pull/706/head
Kevin Hester 2021-02-23 10:45:03 +08:00
rodzic 2761c85564
commit ae6b7e7259
4 zmienionych plików z 51 dodań i 40 usunięć

Wyświetl plik

@ -5,8 +5,8 @@ You probably don't care about this section - skip to the next one.
1.2 cleanup & multichannel support: 1.2 cleanup & multichannel support:
* DONE call RouterPlugin for *all* packets - not just Router packets * DONE call RouterPlugin for *all* packets - not just Router packets
* generate channel hash from the name of the channel+the psk (not just one or the other) * DONE generate channel hash from the name of the channel+the psk (not just one or the other)
* send a hint that can be used to select which channel to try and hash against with each message * DONE send a hint that can be used to select which channel to try and hash against with each message
* DONE remove deprecated * DONE remove deprecated
* DONE fix setchannel in phoneapi.cpp * DONE fix setchannel in phoneapi.cpp
* DONE set mynodeinfo.max_channels * DONE set mynodeinfo.max_channels
@ -15,7 +15,9 @@ You probably don't care about this section - skip to the next one.
* DONE enable remote setttings access by moving settings operations into a regular plugin (move settings ops out of PhoneAPI) * DONE enable remote setttings access by moving settings operations into a regular plugin (move settings ops out of PhoneAPI)
* DONE move portnum up? * DONE move portnum up?
* DONE remove region specific builds from the firmware * DONE remove region specific builds from the firmware
* restrict settings operations to the admin channel * test single channel
* test multi channel
* restrict gpio & serial & settings operations to the admin channel (unless local to the current node)
* add gui in android app for setting region * add gui in android app for setting region
* make an alpha channel for the python API * make an alpha channel for the python API
* "FIXME - move the radioconfig/user/channel READ operations into SettingsMessage as well" * "FIXME - move the radioconfig/user/channel READ operations into SettingsMessage as well"

Wyświetl plik

@ -192,7 +192,7 @@ void Channels::setChannel(const Channel &c)
// if this is the new primary, demote any existing roles // if this is the new primary, demote any existing roles
if (c.role == Channel_Role_PRIMARY) if (c.role == Channel_Role_PRIMARY)
for (int i = 0; i < devicestate.channels_count; i++) for (int i = 0; i < getNumChannels(); i++)
if (devicestate.channels[i].role == Channel_Role_PRIMARY) if (devicestate.channels[i].role == Channel_Role_PRIMARY)
devicestate.channels[i].role = Channel_Role_SECONDARY; devicestate.channels[i].role = Channel_Role_SECONDARY;
@ -271,11 +271,18 @@ const char *Channels::getPrimaryName()
* *
* This method is called before decoding inbound packets * This method is called before decoding inbound packets
* *
* @return -1 if no suitable channel could be found, otherwise returns the channel index * @return false if the channel hash or channel is invalid
*/ */
int16_t Channels::setActiveByHash(ChannelHash channelHash) bool Channels::decryptForHash(ChannelIndex chIndex, ChannelHash channelHash)
{ {
// fixme cant work; if(chIndex > getNumChannels() || getHash(chIndex) != channelHash) {
DEBUG_MSG("Skipping channel %d due to invalid hash/index\n", chIndex);
return false;
}
else {
setCrypto(chIndex);
return true;
}
} }
/** Given a channel index setup crypto for encoding that channel (or the primary channel if that channel is unsecured) /** Given a channel index setup crypto for encoding that channel (or the primary channel if that channel is unsecured)

Wyświetl plik

@ -1,8 +1,9 @@
#pragma once #pragma once
#include "CryptoEngine.h"
#include "NodeDB.h"
#include "mesh-pb-constants.h" #include "mesh-pb-constants.h"
#include <Arduino.h> #include <Arduino.h>
#include "CryptoEngine.h"
/** A channel number (index into the channel table) /** A channel number (index into the channel table)
*/ */
@ -10,7 +11,7 @@ typedef uint8_t ChannelIndex;
/** A low quality hash of the channel PSK and the channel name. created by generateHash(chIndex) /** A low quality hash of the channel PSK and the channel name. created by generateHash(chIndex)
* Used as a hint to limit which PSKs are considered for packet decoding. * Used as a hint to limit which PSKs are considered for packet decoding.
*/ */
typedef uint8_t ChannelHash; typedef uint8_t ChannelHash;
/** The container/on device API for working with channels */ /** The container/on device API for working with channels */
@ -43,6 +44,8 @@ class Channels
/** The index of the primary channel */ /** The index of the primary channel */
ChannelIndex getPrimaryIndex() const { return primaryIndex; } ChannelIndex getPrimaryIndex() const { return primaryIndex; }
ChannelIndex getNumChannels() { return devicestate.channels_count; }
/** /**
* Generate a short suffix used to disambiguate channels that might have the same "name" entered by the human but different * Generate a short suffix used to disambiguate channels that might have the same "name" entered by the human but different
PSKs. PSKs.
@ -72,9 +75,9 @@ class Channels
* *
* This method is called before decoding inbound packets * This method is called before decoding inbound packets
* *
* @return -1 if no suitable channel could be found, otherwise returns the channel index * @return false if the channel hash or channel is invalid
*/ */
int16_t setActiveByHash(ChannelHash channelHash); bool decryptForHash(ChannelIndex chIndex, ChannelHash channelHash);
/** Given a channel index setup crypto for encoding that channel (or the primary channel if that channel is unsecured) /** Given a channel index setup crypto for encoding that channel (or the primary channel if that channel is unsecured)
* *
@ -114,11 +117,10 @@ class Channels
void initDefaultChannel(ChannelIndex chIndex); void initDefaultChannel(ChannelIndex chIndex);
/** /**
* Return the key used for encrypting this channel (if channel is secondary and no key provided, use the primary channel's PSK) * Return the key used for encrypting this channel (if channel is secondary and no key provided, use the primary channel's
* PSK)
*/ */
CryptoKey getKey(ChannelIndex chIndex); CryptoKey getKey(ChannelIndex chIndex);
}; };
/// Singleton channel table /// Singleton channel table

Wyświetl plik

@ -215,31 +215,31 @@ bool Router::perhapsDecode(MeshPacket *p)
assert(p->which_payloadVariant == MeshPacket_encrypted_tag); assert(p->which_payloadVariant == MeshPacket_encrypted_tag);
ChannelHash chHash = p->channel; // Try to find a channel that works with this hash
int16_t chIndex = channels.setActiveByHash(chHash); for (ChannelIndex chIndex = 0; chIndex < channels.getNumChannels(); chIndex++) {
if (chIndex < 0) { // Try to use this hash/channel pair
DEBUG_MSG("No suitable channel found for decoding, hash was 0x%x!\n", chHash); if (channels.decryptForHash(chIndex, p->channel)) {
return false; // Try to decrypt the packet if we can
} else { static uint8_t bytes[MAX_RHPACKETLEN];
p->channel = chIndex; memcpy(bytes, p->encrypted.bytes,
p->encrypted
.size); // we have to copy into a scratch buffer, because these bytes are a union with the decoded protobuf
crypto->decrypt(p->from, p->id, p->encrypted.size, bytes);
// Try to decrypt the packet if we can // Take those raw bytes and convert them back into a well structured protobuf we can understand
static uint8_t bytes[MAX_RHPACKETLEN]; if (!pb_decode_from_bytes(bytes, p->encrypted.size, Data_fields, &p->decoded)) {
memcpy(bytes, p->encrypted.bytes, DEBUG_MSG("Invalid protobufs in received mesh packet (bad psk?!\n");
p->encrypted } else {
.size); // we have to copy into a scratch buffer, because these bytes are a union with the decoded protobuf // parsing was successful
crypto->decrypt(p->from, p->id, p->encrypted.size, bytes); p->channel = chIndex; // change to store the index instead of the hash
p->which_payloadVariant = MeshPacket_decoded_tag;
// Take those raw bytes and convert them back into a well structured protobuf we can understand return true;
if (!pb_decode_from_bytes(bytes, p->encrypted.size, Data_fields, &p->decoded)) { }
DEBUG_MSG("Invalid protobufs in received mesh packet!\n");
return false;
} else {
// parsing was successful
p->which_payloadVariant = MeshPacket_decoded_tag;
return true;
} }
} }
DEBUG_MSG("No suitable channel found for decoding, hash was 0x%x!\n", p->channel);
return false;
} }
NodeNum Router::getNodeNum() NodeNum Router::getNodeNum()