Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #ifndef AOS_NETWORK_SCTP_SERVER_H_ |
| 2 | #define AOS_NETWORK_SCTP_SERVER_H_ |
| 3 | |
| 4 | #include <arpa/inet.h> |
Adam Snaider | be26351 | 2023-05-18 20:40:23 -0700 | [diff] [blame] | 5 | #include <linux/sctp.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 6 | #include <net/if.h> |
| 7 | #include <netdb.h> |
| 8 | #include <netinet/in.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 11 | #include <cstdio> |
| 12 | #include <cstdlib> |
| 13 | #include <cstring> |
| 14 | #include <memory> |
| 15 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 16 | #include "absl/log/check.h" |
| 17 | #include "absl/log/log.h" |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 18 | #include "absl/types/span.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 19 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 20 | #include "aos/network/sctp_lib.h" |
| 21 | #include "aos/unique_malloc_ptr.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 22 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 23 | namespace aos::message_bridge { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 24 | |
| 25 | class SctpServer { |
| 26 | public: |
Brian Silverman | 833dfb6 | 2021-11-03 10:47:55 -0700 | [diff] [blame] | 27 | SctpServer(int streams, std::string_view local_host = "0.0.0.0", |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 28 | int local_port = 9971, |
| 29 | SctpAuthMethod requested_authentication = SctpAuthMethod::kNoAuth); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 30 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 31 | ~SctpServer() {} |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 32 | |
| 33 | // Receives the next packet from the remote. |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 34 | aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 35 | |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 36 | // Frees the message returned by Read(); |
| 37 | void FreeMessage(aos::unique_c_ptr<Message> &&message) { |
| 38 | sctp_.FreeMessage(std::move(message)); |
| 39 | } |
| 40 | |
Austin Schuh | 83afb7a | 2020-03-15 23:09:22 -0700 | [diff] [blame] | 41 | // Sends a block of data to a client on a stream with a TTL. Returns true on |
| 42 | // success. |
| 43 | bool Send(std::string_view data, sctp_assoc_t snd_assoc_id, int stream, |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 44 | int time_to_live) { |
| 45 | return sctp_.SendMessage(stream, data, time_to_live, std::nullopt, |
| 46 | snd_assoc_id); |
| 47 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 48 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 49 | // Aborts a connection. Returns true on success. |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 50 | bool Abort(sctp_assoc_t snd_assoc_id) { return sctp_.Abort(snd_assoc_id); } |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 52 | int fd() { return sctp_.fd(); } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 53 | |
| 54 | // Enables the priority scheduler. This is a SCTP feature which lets us |
| 55 | // configure the priority per stream so that higher priority packets don't get |
| 56 | // backed up behind lower priority packets in the networking queues. |
| 57 | void SetPriorityScheduler(sctp_assoc_t assoc_id); |
| 58 | |
| 59 | // Sets the priority of a specific stream. |
| 60 | void SetStreamPriority(sctp_assoc_t assoc_id, int stream_id, |
| 61 | uint16_t priority); |
| 62 | |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 63 | void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); } |
| 64 | void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); } |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 65 | |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 66 | void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); } |
| 67 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 68 | void SetAuthKey(absl::Span<const uint8_t> auth_key) { |
| 69 | sctp_.SetAuthKey(auth_key); |
| 70 | } |
| 71 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 72 | private: |
| 73 | struct sockaddr_storage sockaddr_local_; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 74 | SctpReadWrite sctp_; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 77 | } // namespace aos::message_bridge |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 78 | |
| 79 | #endif // AOS_NETWORK_SCTP_SERVER_H_ |