Add sctp authentication to sctp_lib

This only works for linux >=5.4. When enabled, it will use
a shared key to authenticate messages. The functionality is
controlled by a flag and behind a linux version check.

Performance degradation is minimal, even for smaller messages
and unnoticeable when measuring overall system performance.

Change-Id: I836e61ec38a0c116fd7244b771437738ccca9828
Signed-off-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
diff --git a/aos/network/sctp_perf.cc b/aos/network/sctp_perf.cc
index cce4bed..3bafed1 100644
--- a/aos/network/sctp_perf.cc
+++ b/aos/network/sctp_perf.cc
@@ -6,6 +6,7 @@
 #include "aos/events/shm_event_loop.h"
 #include "aos/init.h"
 #include "aos/network/sctp_client.h"
+#include "aos/network/sctp_lib.h"
 #include "aos/network/sctp_server.h"
 
 DEFINE_string(config, "aos_config.json", "Path to the config.");
@@ -21,16 +22,38 @@
 DEFINE_uint32(skip_first_n, 10,
               "Skip the first 'n' messages when computing statistics.");
 
+#if HAS_SCTP_AUTH
+DEFINE_string(sctp_auth_key_file, "",
+              "When set, use the provided key for SCTP authentication as "
+              "defined in RFC 4895");
+#endif
+
 DECLARE_bool(die_on_malloc);
 
 namespace aos::message_bridge::perf {
 
+namespace {
+
+using util::ReadFileToVecOrDie;
+
+std::vector<uint8_t> GetSctpAuthKey() {
+#if HAS_SCTP_AUTH
+  if (!FLAGS_sctp_auth_key_file.empty()) {
+    return ReadFileToVecOrDie(FLAGS_sctp_auth_key_file);
+  }
+#endif
+  return {};
+}
+
+}  // namespace
+
 namespace chrono = std::chrono;
 
 class Server {
  public:
   Server(aos::ShmEventLoop *event_loop)
-      : event_loop_(event_loop), server_(2, "0.0.0.0", FLAGS_port) {
+      : event_loop_(event_loop),
+        server_(2, "0.0.0.0", FLAGS_port, GetSctpAuthKey()) {
     event_loop_->epoll()->OnReadable(server_.fd(),
                                      [this]() { MessageReceived(); });
     server_.SetMaxReadSize(FLAGS_rx_size + 100);
@@ -109,7 +132,9 @@
 class Client {
  public:
   Client(aos::ShmEventLoop *event_loop)
-      : event_loop_(event_loop), client_(FLAGS_host, FLAGS_port, 2) {
+      : event_loop_(event_loop),
+        client_(FLAGS_host, FLAGS_port, 2, "0.0.0.0", FLAGS_port,
+                GetSctpAuthKey()) {
     client_.SetMaxReadSize(FLAGS_rx_size + 100);
     client_.SetMaxWriteSize(FLAGS_rx_size + 100);
 
@@ -196,8 +221,8 @@
     double throughput = FLAGS_payload_size * 2.0 / elapsed_secs;
     double avg_throughput = FLAGS_payload_size * 2.0 / avg_latency_;
     printf(
-        "Round trip: %.2fms | %.2f KB/s | Avg RTL: %.2fms | %.2f KB/s | Count: "
-        "%d\n",
+        "Round trip: %.2fms | %.2f KB/s | Avg RTL: %.2fms | %.2f KB/s | "
+        "Count: %d\n",
         elapsed_secs * 1000, throughput / 1024, avg_latency_ * 1000,
         avg_throughput / 1024, count_);
   }