Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #ifndef AOS_NETWORK_SCTP_CLIENT_H_ |
| 2 | #define AOS_NETWORK_SCTP_CLIENT_H_ |
| 3 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cstdio> |
| 5 | #include <cstdlib> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 6 | #include <string_view> |
| 7 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 8 | #include "absl/types/span.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "glog/logging.h" |
| 10 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 11 | #include "aos/network/sctp_lib.h" |
| 12 | #include "aos/unique_malloc_ptr.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 13 | |
| 14 | namespace aos { |
| 15 | namespace message_bridge { |
| 16 | |
| 17 | // Class to encapsulate everything needed to be a SCTP client. |
| 18 | class SctpClient { |
| 19 | public: |
| 20 | SctpClient(std::string_view remote_host, int remote_port, int streams, |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 21 | std::string_view local_host = "0.0.0.0", int local_port = 9971, |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 22 | SctpAuthMethod requested_authentication = SctpAuthMethod::kNoAuth); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 24 | ~SctpClient() {} |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 25 | |
| 26 | // Receives the next packet from the remote. |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 27 | aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 28 | |
| 29 | // Sends a block of data on a stream with a TTL. |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 30 | // TODO(austin): time_to_live should be a chrono::duration |
| 31 | bool Send(int stream, std::string_view data, int time_to_live) { |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 32 | return sctp_.SendMessage(stream, data, time_to_live, sockaddr_remote_, |
| 33 | sac_assoc_id_); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 34 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 35 | |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 36 | // Aborts a connection. Returns true on success. |
| 37 | bool Abort() { return sctp_.Abort(sac_assoc_id_); } |
| 38 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 39 | int fd() { return sctp_.fd(); } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 40 | |
| 41 | // Enables the priority scheduler. This is a SCTP feature which lets us |
| 42 | // configure the priority per stream so that higher priority packets don't get |
| 43 | // backed up behind lower priority packets in the networking queues. |
| 44 | void SetPriorityScheduler(sctp_assoc_t assoc_id); |
| 45 | |
| 46 | // Remote to send to. |
| 47 | struct sockaddr_storage sockaddr_remote() const { |
| 48 | return sockaddr_remote_; |
| 49 | } |
| 50 | |
| 51 | void LogSctpStatus(sctp_assoc_t assoc_id); |
| 52 | |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 53 | void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); } |
| 54 | void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); } |
Austin Schuh | 4f558e5 | 2023-05-08 21:51:31 -0700 | [diff] [blame] | 55 | void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); } |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 56 | |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 57 | void SetAssociationId(sctp_assoc_t sac_assoc_id) { |
| 58 | sac_assoc_id_ = sac_assoc_id; |
| 59 | } |
| 60 | |
Austin Schuh | 4f558e5 | 2023-05-08 21:51:31 -0700 | [diff] [blame] | 61 | void FreeMessage(aos::unique_c_ptr<Message> &&message) { |
| 62 | sctp_.FreeMessage(std::move(message)); |
| 63 | } |
| 64 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame^] | 65 | void SetAuthKey(absl::Span<const uint8_t> auth_key) { |
| 66 | sctp_.SetAuthKey(auth_key); |
| 67 | } |
| 68 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 69 | private: |
| 70 | struct sockaddr_storage sockaddr_remote_; |
| 71 | struct sockaddr_storage sockaddr_local_; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 72 | SctpReadWrite sctp_; |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 73 | |
| 74 | // Valid if != 0. |
| 75 | sctp_assoc_t sac_assoc_id_ = 0; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace message_bridge |
| 79 | } // namespace aos |
| 80 | |
| 81 | #endif // AOS_NETWORK_SCTP_CLIENT_H_ |