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