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/util/file.cc b/aos/util/file.cc
index 591539c..52657a9 100644
--- a/aos/util/file.cc
+++ b/aos/util/file.cc
@@ -47,6 +47,22 @@
   return r;
 }
 
+std::vector<uint8_t> ReadFileToVecOrDie(const std::string_view filename) {
+  std::vector<uint8_t> r;
+  ScopedFD fd(open(::std::string(filename).c_str(), O_RDONLY));
+  PCHECK(fd.get() != -1) << ": opening " << filename;
+  while (true) {
+    uint8_t buffer[1024];
+    const ssize_t result = read(fd.get(), buffer, sizeof(buffer));
+    PCHECK(result >= 0) << ": reading from " << filename;
+    if (result == 0) {
+      break;
+    }
+    std::copy(buffer, buffer + result, std::back_inserter(r));
+  }
+  return r;
+}
+
 void WriteStringToFileOrDie(const std::string_view filename,
                             const std::string_view contents,
                             mode_t permissions) {
diff --git a/aos/util/file.h b/aos/util/file.h
index 0cba867..da5b29f 100644
--- a/aos/util/file.h
+++ b/aos/util/file.h
@@ -29,6 +29,10 @@
 std::optional<std::string> MaybeReadFileToString(
     const std::string_view filename);
 
+// Returns the complete contents of filename as a byte vector. LOG(FATAL)s if
+// any errors are encountered.
+std::vector<uint8_t> ReadFileToVecOrDie(const std::string_view filename);
+
 // Creates filename if it doesn't exist and sets the contents to contents.
 void WriteStringToFileOrDie(const std::string_view filename,
                             const std::string_view contents,
diff --git a/aos/util/file_test.cc b/aos/util/file_test.cc
index df16d58..ec4bfe4 100644
--- a/aos/util/file_test.cc
+++ b/aos/util/file_test.cc
@@ -4,6 +4,7 @@
 #include <optional>
 #include <string>
 
+#include "gmock/gmock-matchers.h"
 #include "gtest/gtest.h"
 
 #include "aos/realtime.h"
@@ -13,6 +14,8 @@
 namespace util {
 namespace testing {
 
+using ::testing::ElementsAre;
+
 // Basic test of reading a normal file.
 TEST(FileTest, ReadNormalFile) {
   const std::string tmpdir(aos::testing::TestTmpDir());
@@ -21,6 +24,15 @@
   EXPECT_EQ("contents\n", ReadFileToStringOrDie(test_file));
 }
 
+// Basic test of reading a normal file.
+TEST(FileTest, ReadNormalFileToBytes) {
+  const std::string tmpdir(aos::testing::TestTmpDir());
+  const std::string test_file = tmpdir + "/test_file";
+  ASSERT_EQ(0, system(("echo contents > " + test_file).c_str()));
+  EXPECT_THAT(ReadFileToVecOrDie(test_file),
+              ElementsAre('c', 'o', 'n', 't', 'e', 'n', 't', 's', '\n'));
+}
+
 // Tests reading a file with 0 size, among other weird things.
 TEST(FileTest, ReadSpecialFile) {
   const std::string stat = ReadFileToStringOrDie("/proc/self/stat");