Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #ifndef AOS_NETWORK_SCTP_LIB_H_ |
| 2 | #define AOS_NETWORK_SCTP_LIB_H_ |
| 3 | |
| 4 | #include <arpa/inet.h> |
Adam Snaider | be26351 | 2023-05-18 20:40:23 -0700 | [diff] [blame] | 5 | #include <linux/sctp.h> |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 6 | #include <linux/version.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 9 | #include <optional> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <string_view> |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 12 | #include <vector> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 13 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 14 | #include "absl/types/span.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 15 | #include "gflags/gflags.h" |
| 16 | #include "glog/logging.h" |
| 17 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 18 | #include "aos/unique_malloc_ptr.h" |
| 19 | |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 20 | #define HAS_SCTP_AUTH LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0) |
| 21 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 22 | namespace aos { |
| 23 | namespace message_bridge { |
| 24 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 25 | constexpr bool HasSctpAuth() { return HAS_SCTP_AUTH; } |
| 26 | |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 27 | // Check if ipv6 is enabled. |
| 28 | // If we don't try IPv6, and omit AI_ADDRCONFIG when resolving addresses, the |
| 29 | // library will happily resolve nodes to IPv6 IPs that can't be used. If we add |
| 30 | // AI_ADDRCONFIG, the unit tests no longer work because they only have loopback |
| 31 | // addresses available. |
| 32 | bool Ipv6Enabled(); |
| 33 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 34 | // Resolves a socket and returns the address. This can be either an ipv4 or |
| 35 | // ipv6 address. |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 36 | struct sockaddr_storage ResolveSocket(std::string_view host, int port, |
| 37 | bool use_ipv6); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 38 | |
| 39 | // Returns a formatted version of the address. |
| 40 | std::string Address(const struct sockaddr_storage &sockaddr); |
| 41 | // Returns a formatted version of the address family. |
| 42 | std::string_view Family(const struct sockaddr_storage &sockaddr); |
| 43 | |
| 44 | // Message received. |
| 45 | // This message is malloced bigger than needed and the extra space after it is |
| 46 | // the data. |
| 47 | struct Message { |
| 48 | // Struct to let us force data to be well aligned. |
| 49 | struct OveralignedChar { |
| 50 | uint8_t data alignas(32); |
| 51 | }; |
| 52 | |
| 53 | // Headers. |
| 54 | struct { |
| 55 | struct sctp_rcvinfo rcvinfo; |
| 56 | } header; |
| 57 | |
| 58 | // Address of the sender. |
| 59 | struct sockaddr_storage sin; |
| 60 | |
| 61 | // Data type. Is it a block of data, or is it a struct sctp_notification? |
Austin Schuh | 89f23e3 | 2023-05-15 17:06:43 -0700 | [diff] [blame] | 62 | enum MessageType { |
| 63 | // Block of data? |
| 64 | kMessage, |
| 65 | // struct sctp_notification? |
| 66 | kNotification, |
| 67 | // Client sent too large a message and was disconnected. |
| 68 | kOverflow, |
| 69 | } message_type; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 70 | |
| 71 | size_t size = 0u; |
| 72 | uint8_t *mutable_data() { |
| 73 | return reinterpret_cast<uint8_t *>(&actual_data[0].data); |
| 74 | } |
| 75 | const uint8_t *data() const { |
| 76 | return reinterpret_cast<const uint8_t *>(&actual_data[0].data); |
| 77 | } |
| 78 | |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 79 | uint32_t partial_deliveries = 0; |
| 80 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 81 | // Returns a human readable peer IP address. |
| 82 | std::string PeerAddress() const; |
| 83 | |
| 84 | // Prints out the RcvInfo structure. |
| 85 | void LogRcvInfo() const; |
| 86 | |
| 87 | // The start of the data. |
| 88 | OveralignedChar actual_data[]; |
| 89 | }; |
| 90 | |
| 91 | void PrintNotification(const Message *msg); |
| 92 | |
| 93 | std::string GetHostname(); |
| 94 | |
| 95 | // Gets and logs the contents of the sctp_status message. |
| 96 | void LogSctpStatus(int fd, sctp_assoc_t assoc_id); |
| 97 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 98 | // Authentication method used for the SCTP socket. |
| 99 | enum class SctpAuthMethod { |
| 100 | // Use unauthenticated sockets. |
| 101 | kNoAuth, |
| 102 | // Use RFC4895 authentication for SCTP. |
| 103 | kAuth, |
| 104 | }; |
| 105 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 106 | // Manages reading and writing SCTP messages. |
| 107 | class SctpReadWrite { |
| 108 | public: |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 109 | // When `requested_authentication` is kAuth, it will use SCTP authentication |
| 110 | // if it's provided by the kernel. Note that this will ignore the value of |
| 111 | // `requested_authentication` if the kernel is too old and will fall back to |
| 112 | // an unauthenticated channel. |
| 113 | SctpReadWrite( |
| 114 | SctpAuthMethod requested_authentication = SctpAuthMethod::kNoAuth) |
| 115 | : sctp_authentication_(HasSctpAuth() ? requested_authentication == |
| 116 | SctpAuthMethod::kAuth |
| 117 | : false) { |
| 118 | LOG_IF(WARNING, |
| 119 | requested_authentication == SctpAuthMethod::kAuth && !HasSctpAuth()) |
| 120 | << "SCTP authentication requested but not provided by the kernel... " |
| 121 | "You may need a newer kernel"; |
| 122 | } |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 123 | ~SctpReadWrite() { CloseSocket(); } |
| 124 | |
| 125 | // Opens a new socket. |
| 126 | void OpenSocket(const struct sockaddr_storage &sockaddr_local); |
| 127 | |
| 128 | // Sends a message to the kernel. |
| 129 | // Returns true for success. Will not send a partial message on failure. |
| 130 | bool SendMessage(int stream, std::string_view data, int time_to_live, |
| 131 | std::optional<struct sockaddr_storage> sockaddr_remote, |
| 132 | sctp_assoc_t snd_assoc_id); |
| 133 | |
| 134 | // Reads from the kernel until a complete message is received or it blocks. |
| 135 | // Returns nullptr if the kernel blocks before returning a complete message. |
| 136 | aos::unique_c_ptr<Message> ReadMessage(); |
| 137 | |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 138 | // Send an abort message for the given association. |
| 139 | bool Abort(sctp_assoc_t snd_assoc_id); |
| 140 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 141 | int fd() const { return fd_; } |
| 142 | |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 143 | void SetMaxReadSize(size_t max_size) { |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 144 | CHECK(partial_messages_.empty()) |
| 145 | << ": May not update size with queued fragments because we do not " |
| 146 | "track individual message sizes"; |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 147 | max_read_size_ = max_size; |
| 148 | if (fd_ != -1) { |
| 149 | DoSetMaxSize(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void SetMaxWriteSize(size_t max_size) { |
| 154 | CHECK(partial_messages_.empty()) |
| 155 | << ": May not update size with queued fragments because we do not " |
| 156 | "track individual message sizes"; |
| 157 | max_write_size_ = max_size; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 158 | if (fd_ != -1) { |
| 159 | DoSetMaxSize(); |
| 160 | } |
| 161 | } |
| 162 | |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 163 | // Returns a message returned from ReadMessage back to the pool. |
| 164 | void FreeMessage(aos::unique_c_ptr<Message> &&message); |
| 165 | |
| 166 | // Allocates messages for the pool. SetMaxSize must be set first. |
| 167 | void SetPoolSize(size_t pool_size); |
| 168 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 169 | // Set the active authentication key to `auth_key`. |
| 170 | void SetAuthKey(absl::Span<const uint8_t> auth_key); |
| 171 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 172 | private: |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 173 | aos::unique_c_ptr<Message> AcquireMessage(); |
| 174 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 175 | void CloseSocket(); |
| 176 | void DoSetMaxSize(); |
| 177 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 178 | // Examines a notification message for ones we handle here. |
| 179 | // Returns true if the notification was handled by this class. |
| 180 | bool ProcessNotification(const Message *message); |
| 181 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 182 | int fd_ = -1; |
| 183 | |
| 184 | // We use this as a unique identifier that just increments for each message. |
| 185 | uint32_t send_ppid_ = 0; |
| 186 | |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 187 | size_t max_read_size_ = 1000; |
| 188 | size_t max_write_size_ = 1000; |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 189 | |
| 190 | std::vector<aos::unique_c_ptr<Message>> partial_messages_; |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 191 | |
| 192 | bool use_pool_ = false; |
| 193 | std::vector<aos::unique_c_ptr<Message>> free_messages_; |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 194 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 195 | // Use SCTP authentication (RFC4895). |
| 196 | bool sctp_authentication_; |
| 197 | std::vector<uint8_t> current_key_; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 198 | }; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 199 | |
Austin Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 200 | // Returns the max network buffer available for reading for a socket. |
| 201 | size_t ReadRMemMax(); |
| 202 | // Returns the max network buffer available for writing for a socket. |
| 203 | size_t ReadWMemMax(); |
| 204 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 205 | } // namespace message_bridge |
| 206 | } // namespace aos |
| 207 | |
| 208 | #endif // AOS_NETWORK_SCTP_LIB_H_ |