blob: 0d021a95f26989484ca898f8b05f5c5ffe06e8a6 [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#ifndef AOS_NETWORK_SCTP_LIB_H_
2#define AOS_NETWORK_SCTP_LIB_H_
3
4#include <arpa/inet.h>
Adam Snaiderbe263512023-05-18 20:40:23 -07005#include <linux/sctp.h>
Adam Snaider96a0f4b2023-05-18 20:41:19 -07006#include <linux/version.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08007
8#include <memory>
Austin Schuh507f7582021-07-31 20:39:55 -07009#include <optional>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010#include <string>
11#include <string_view>
Austin Schuha705d782021-07-31 20:40:00 -070012#include <vector>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080013
Adam Snaider9bb33442023-06-26 16:31:37 -070014#include "absl/types/span.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080015#include "gflags/gflags.h"
16#include "glog/logging.h"
17
Philipp Schrader790cb542023-07-05 21:06:52 -070018#include "aos/unique_malloc_ptr.h"
19
Adam Snaider96a0f4b2023-05-18 20:41:19 -070020#define HAS_SCTP_AUTH LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
21
Austin Schuhe84c3ed2019-12-14 15:29:48 -080022namespace aos {
23namespace message_bridge {
24
Adam Snaider9bb33442023-06-26 16:31:37 -070025constexpr bool HasSctpAuth() { return HAS_SCTP_AUTH; }
26
Austin Schuh0a0a8272021-12-08 13:19:32 -080027// 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.
32bool Ipv6Enabled();
33
Austin Schuhe84c3ed2019-12-14 15:29:48 -080034// Resolves a socket and returns the address. This can be either an ipv4 or
35// ipv6 address.
Austin Schuh0a0a8272021-12-08 13:19:32 -080036struct sockaddr_storage ResolveSocket(std::string_view host, int port,
37 bool use_ipv6);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080038
39// Returns a formatted version of the address.
40std::string Address(const struct sockaddr_storage &sockaddr);
41// Returns a formatted version of the address family.
42std::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.
47struct 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 Schuh89f23e32023-05-15 17:06:43 -070062 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 Schuhe84c3ed2019-12-14 15:29:48 -080070
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 Schuhc4202572021-03-31 21:06:55 -070079 uint32_t partial_deliveries = 0;
80
Austin Schuhe84c3ed2019-12-14 15:29:48 -080081 // 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
91void PrintNotification(const Message *msg);
92
93std::string GetHostname();
94
95// Gets and logs the contents of the sctp_status message.
96void LogSctpStatus(int fd, sctp_assoc_t assoc_id);
97
Adam Snaider9bb33442023-06-26 16:31:37 -070098// Authentication method used for the SCTP socket.
99enum class SctpAuthMethod {
100 // Use unauthenticated sockets.
101 kNoAuth,
102 // Use RFC4895 authentication for SCTP.
103 kAuth,
104};
105
Austin Schuh507f7582021-07-31 20:39:55 -0700106// Manages reading and writing SCTP messages.
107class SctpReadWrite {
108 public:
Adam Snaider9bb33442023-06-26 16:31:37 -0700109 // 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 Schuh507f7582021-07-31 20:39:55 -0700123 ~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 Newman80e955e2022-04-13 11:19:36 -0700138 // Send an abort message for the given association.
139 bool Abort(sctp_assoc_t snd_assoc_id);
140
Austin Schuh507f7582021-07-31 20:39:55 -0700141 int fd() const { return fd_; }
142
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700143 void SetMaxReadSize(size_t max_size) {
Austin Schuha705d782021-07-31 20:40:00 -0700144 CHECK(partial_messages_.empty())
145 << ": May not update size with queued fragments because we do not "
146 "track individual message sizes";
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700147 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 Schuh507f7582021-07-31 20:39:55 -0700158 if (fd_ != -1) {
159 DoSetMaxSize();
160 }
161 }
162
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700163 // 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 Snaider9bb33442023-06-26 16:31:37 -0700169 // Set the active authentication key to `auth_key`.
170 void SetAuthKey(absl::Span<const uint8_t> auth_key);
171
Austin Schuh507f7582021-07-31 20:39:55 -0700172 private:
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700173 aos::unique_c_ptr<Message> AcquireMessage();
174
Austin Schuh507f7582021-07-31 20:39:55 -0700175 void CloseSocket();
176 void DoSetMaxSize();
177
Austin Schuha705d782021-07-31 20:40:00 -0700178 // 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 Schuh507f7582021-07-31 20:39:55 -0700182 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 Schuh89e1e9c2023-05-15 14:38:44 -0700187 size_t max_read_size_ = 1000;
188 size_t max_write_size_ = 1000;
Austin Schuha705d782021-07-31 20:40:00 -0700189
190 std::vector<aos::unique_c_ptr<Message>> partial_messages_;
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700191
192 bool use_pool_ = false;
193 std::vector<aos::unique_c_ptr<Message>> free_messages_;
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700194
Adam Snaider9bb33442023-06-26 16:31:37 -0700195 // Use SCTP authentication (RFC4895).
196 bool sctp_authentication_;
197 std::vector<uint8_t> current_key_;
Austin Schuh507f7582021-07-31 20:39:55 -0700198};
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800199
Austin Schuh2fe4b712020-03-15 14:21:45 -0700200// Returns the max network buffer available for reading for a socket.
201size_t ReadRMemMax();
202// Returns the max network buffer available for writing for a socket.
203size_t ReadWMemMax();
204
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800205} // namespace message_bridge
206} // namespace aos
207
208#endif // AOS_NETWORK_SCTP_LIB_H_