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