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