From 26060616e3a9eeeba009e89b8e07819129cba2b0 Mon Sep 17 00:00:00 2001
From: Stefano Colli <stefano2.colli@mail.polimi.it>
Date: Tue, 10 Dec 2024 22:30:25 +0100
Subject: [PATCH] Add option for UDP clients to always accept messages ignoring
 sysid

---
 src/endpoint.cpp |  8 ++++++++
 src/endpoint.h   |  2 ++
 src/mainloop.cpp | 10 +++++++---
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/endpoint.cpp b/src/endpoint.cpp
index 9a03256c..ee78951e 100644
--- a/src/endpoint.cpp
+++ b/src/endpoint.cpp
@@ -99,6 +99,7 @@ const ConfFile::OptionsTable UdpEndpoint::option_table[] = {
     {"CoalesceBytes",   false,  ConfFile::parse_ul,             OPTIONS_TABLE_STRUCT_FIELD(UdpEndpointConfig, coalesce_bytes)},
     {"CoalesceMs",      false,  ConfFile::parse_ul,             OPTIONS_TABLE_STRUCT_FIELD(UdpEndpointConfig, coalesce_ms)},
     {"CoalesceNoDelay", false,  ConfFile::parse_uint32_vector,  OPTIONS_TABLE_STRUCT_FIELD(UdpEndpointConfig, coalesce_nodelay)},
+    {"AlwaysAccept",    false,  ConfFile::parse_bool,           OPTIONS_TABLE_STRUCT_FIELD(UdpEndpointConfig, always_accept)},
     {}
 };
 
@@ -1161,6 +1162,7 @@ bool UdpEndpoint::setup(UdpEndpointConfig conf)
         this->add_no_coalesce_msg_id(msg_id);
     }
 
+    this->_always_accept = conf.always_accept;
     return true;
 }
 
@@ -1535,6 +1537,12 @@ Endpoint::AcceptState UdpEndpoint::accept_msg(const struct buffer *pbuf) const
         return Endpoint::AcceptState::Filtered;
     }
 
+    // Endpoint has the always accept flag set
+    if (this->_always_accept) {
+        log_trace("Endpoint [%d]%s: message accepted because the always_accept flag is set", fd, _name.c_str());
+        return Endpoint::AcceptState::Accepted;
+    }
+     
     // otherwise: refer to standard accept rules
     return Endpoint::accept_msg(pbuf);
 }
diff --git a/src/endpoint.h b/src/endpoint.h
index 794b43a0..02c6c9c7 100644
--- a/src/endpoint.h
+++ b/src/endpoint.h
@@ -80,6 +80,7 @@ struct UdpEndpointConfig {
     unsigned long coalesce_bytes;
     unsigned long coalesce_ms;
     std::vector<uint32_t> coalesce_nodelay;
+    bool always_accept;
 };
 
 struct TcpEndpointConfig {
@@ -378,6 +379,7 @@ class UdpEndpoint : public Endpoint {
     unsigned long _coalesce_ms = 0UL;                  // max time to hold data to try to coalesce packets together
 
     UdpEndpointConfig::Mode _mode = UdpEndpointConfig::Mode::Undefined;
+    bool _always_accept = false;      // always accept messages disregarding sys and com IDs
 private:
     bool is_ipv6;
     struct sockaddr_in sockaddr;
diff --git a/src/mainloop.cpp b/src/mainloop.cpp
index d4abecf6..41385966 100644
--- a/src/mainloop.cpp
+++ b/src/mainloop.cpp
@@ -279,10 +279,10 @@ void Mainloop::handle_command_pipe()
                 // a0  a1   a2  a3  a4   a5   a6        a7          a8           a9 
                 //  allow_msg_id_out block_msg_id_out allow_src_comp_out block_src_comp_out allow_src_sys_out block_src_sys_out allow_msg_id_in 
                 //        a10               a11              a12                  a13             a14                a15              a16 
-                //  block_msg_id_in allow_src_comp_in block_src_comp_in allow_src_sys_in block_src_sys_in
-                //           a17           a18                a19             a20              a21 
+                //  block_msg_id_in allow_src_comp_in block_src_comp_in allow_src_sys_in block_src_sys_in always_accept
+                //           a17           a18                a19             a20              a21            a22
 
-                std::set<unsigned> argc_options = {6, 7, 10, 22};
+                std::set<unsigned> argc_options = {6, 7, 10, 22, 23};
 
                 // Sanity checks
                 if (!argc_options.count(a.size()) || a[1] != "udp") {
@@ -354,6 +354,10 @@ void Mainloop::handle_command_pipe()
                     parse_into_vector(a[21], conf.block_src_sys_in);
                 } 
 
+                if (a.size() > 22) { // always accept flag provided
+                    conf.always_accept = a[4] == "true";
+                }
+                
                 // UDP endpoint configuration to instance
                 auto dynamic_udp = std::make_shared<UdpEndpoint>(conf.name);
                 if (!dynamic_udp->setup(conf)) {