blob: 996645eef588968a9cd35559a4e49fc5a0ba65bf [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#ifndef AOS_NETWORK_SCTP_SERVER_H_
2#define AOS_NETWORK_SCTP_SERVER_H_
3
4#include <arpa/inet.h>
Adam Snaiderbe263512023-05-18 20:40:23 -07005#include <linux/sctp.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08006#include <net/if.h>
7#include <netdb.h>
8#include <netinet/in.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08009#include <sys/socket.h>
10
Tyler Chatowbf0609c2021-07-31 16:13:27 -070011#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <memory>
15
Adam Snaider9bb33442023-06-26 16:31:37 -070016#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070017#include "glog/logging.h"
18
Austin Schuhe84c3ed2019-12-14 15:29:48 -080019#include "aos/network/sctp_lib.h"
20#include "aos/unique_malloc_ptr.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080021
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080022namespace aos::message_bridge {
Austin Schuhe84c3ed2019-12-14 15:29:48 -080023
24class SctpServer {
25 public:
Brian Silverman833dfb62021-11-03 10:47:55 -070026 SctpServer(int streams, std::string_view local_host = "0.0.0.0",
Adam Snaider9bb33442023-06-26 16:31:37 -070027 int local_port = 9971,
28 SctpAuthMethod requested_authentication = SctpAuthMethod::kNoAuth);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080029
Austin Schuh507f7582021-07-31 20:39:55 -070030 ~SctpServer() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080031
32 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070033 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080034
Austin Schuhf95a6ab2023-05-15 14:34:57 -070035 // Frees the message returned by Read();
36 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
37 sctp_.FreeMessage(std::move(message));
38 }
39
Austin Schuh83afb7a2020-03-15 23:09:22 -070040 // 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 Schuh507f7582021-07-31 20:39:55 -070043 int time_to_live) {
44 return sctp_.SendMessage(stream, data, time_to_live, std::nullopt,
45 snd_assoc_id);
46 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080047
Austin Schuh4889b182020-11-18 19:11:56 -080048 // Aborts a connection. Returns true on success.
Sarah Newman80e955e2022-04-13 11:19:36 -070049 bool Abort(sctp_assoc_t snd_assoc_id) { return sctp_.Abort(snd_assoc_id); }
Austin Schuh4889b182020-11-18 19:11:56 -080050
Austin Schuh507f7582021-07-31 20:39:55 -070051 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080052
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 Schuh89e1e9c2023-05-15 14:38:44 -070062 void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); }
63 void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080064
Austin Schuhf95a6ab2023-05-15 14:34:57 -070065 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
66
Adam Snaider9bb33442023-06-26 16:31:37 -070067 void SetAuthKey(absl::Span<const uint8_t> auth_key) {
68 sctp_.SetAuthKey(auth_key);
69 }
70
Austin Schuhe84c3ed2019-12-14 15:29:48 -080071 private:
72 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070073 SctpReadWrite sctp_;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080074};
75
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080076} // namespace aos::message_bridge
Austin Schuhe84c3ed2019-12-14 15:29:48 -080077
78#endif // AOS_NETWORK_SCTP_SERVER_H_