Flatbuffer validation

This commit is contained in:
2025-10-15 21:55:20 -04:00
parent c05df920fd
commit 534e9e9ee4
4 changed files with 16 additions and 11 deletions

View File

@@ -30,8 +30,4 @@ namespace Flatbuffers {
return {builder_.GetBufferPointer(), builder_.GetSize()}; return {builder_.GetBufferPointer(), builder_.GetSize()};
} }
const Messaging::TopologyMessage* TopologyMessageBuilder::parse_topology_message(const uint8_t* buffer) {
return flatbuffers::GetRoot<Messaging::TopologyMessage>(buffer);
}
} }

View File

@@ -23,8 +23,6 @@ namespace Flatbuffers {
const std::vector<uint8_t>& channel_to_module, const std::vector<uint8_t>& channel_to_module,
const std::vector<int8_t>& orientation_to_module); const std::vector<int8_t>& orientation_to_module);
static const Messaging::TopologyMessage* parse_topology_message(const uint8_t* buffer);
private: private:
flatbuffers::FlatBufferBuilder builder_; flatbuffers::FlatBufferBuilder builder_;
}; };

View File

@@ -100,6 +100,13 @@ void CommunicationRouter::update_leader() {
} }
void CommunicationRouter::route(uint8_t* buffer, const size_t length) const { void CommunicationRouter::route(uint8_t* buffer, const size_t length) const {
flatbuffers::Verifier verifier(buffer, length);
bool ok = Messaging::VerifyMPIMessageBuffer(verifier);
if (!ok) { // This could be moved to just be called on wireline data to save cpu cycles.
ESP_LOGW(TAG, "route: got an invalid MPI message, disregarding");
return;
}
const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer); const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer);
if (mpi_message->destination() == m_module_id) { if (mpi_message->destination() == m_module_id) {

View File

@@ -124,7 +124,8 @@ TCPServer::~TCPServer() {
const auto that = static_cast<TCPServer *>(args); const auto that = static_cast<TCPServer *>(args);
while (true) { while (true) {
vTaskDelay(1 / portTICK_PERIOD_MS); // Avoid starving other threads vTaskDelay(0); // Avoid starving other threads
fd_set readfds; fd_set readfds;
FD_ZERO(&readfds); FD_ZERO(&readfds);
int max_fd = -1; int max_fd = -1;
@@ -136,13 +137,18 @@ TCPServer::~TCPServer() {
} }
xSemaphoreGive(that->m_mutex); xSemaphoreGive(that->m_mutex);
timeval timeout = {0, 100000}; // 100 ms timeout // todo: Select seems to be timing out the watchdog, even though we have a 50ms timeout.
// Potentially select is not respecting our timeout?
timeval timeout = {0, 50000}; // 50 ms timeout
int ret = select(max_fd + 1, &readfds, nullptr, nullptr, &timeout); int ret = select(max_fd + 1, &readfds, nullptr, nullptr, &timeout);
vTaskDelay(0); // Avoid starving other threads
if (ret > 0) { if (ret > 0) {
xSemaphoreTake(that->m_mutex, portMAX_DELAY); xSemaphoreTake(that->m_mutex, portMAX_DELAY);
std::vector<int> to_remove; std::vector<int> to_remove;
for (int sock : that->m_clients) { for (int sock : that->m_clients) {
vTaskDelay(0); // Avoid starving other threads
if (FD_ISSET(sock, &readfds)) { if (FD_ISSET(sock, &readfds)) {
// Handle socket // Handle socket
auto buffer = std::make_unique<std::vector<uint8_t>>(); auto buffer = std::make_unique<std::vector<uint8_t>>();
@@ -160,7 +166,7 @@ TCPServer::~TCPServer() {
ESP_LOGE(TAG, "Error occurred during receiving: errno %d\n", errno); ESP_LOGE(TAG, "Error occurred during receiving: errno %d\n", errno);
to_remove.emplace_back(sock); to_remove.emplace_back(sock);
} else if (0 == len) { } else if (0 == len) {
ESP_LOGW(TAG, "Connection closed\n"); ESP_LOGI(TAG, "TCP Connection closed\n");
close(sock); close(sock);
to_remove.emplace_back(sock); to_remove.emplace_back(sock);
} else { } else {
@@ -211,8 +217,6 @@ bool TCPServer::authenticate_client(int sock) {
} }
int TCPServer::send_msg(char *buffer, uint32_t length) const { int TCPServer::send_msg(char *buffer, uint32_t length) const {
// todo: should we assign a unique rank to each pc?
if (!is_network_connected()) { if (!is_network_connected()) {
return -1; return -1;
} }