blob: 4dd295d7ca39568488dcb80f19d4ec5cfc82816e [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -070016#include "absl/log/check.h"
17#include "absl/log/log.h"
Adam Snaider9bb33442023-06-26 16:31:37 -070018#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070019
Austin Schuhe84c3ed2019-12-14 15:29:48 -080020#include "aos/network/sctp_lib.h"
21#include "aos/unique_malloc_ptr.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080022
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080023namespace aos::message_bridge {
Austin Schuhe84c3ed2019-12-14 15:29:48 -080024
25class SctpServer {
26 public:
Brian Silverman833dfb62021-11-03 10:47:55 -070027 SctpServer(int streams, std::string_view local_host = "0.0.0.0",
Adam Snaider9bb33442023-06-26 16:31:37 -070028 int local_port = 9971,
29 SctpAuthMethod requested_authentication = SctpAuthMethod::kNoAuth);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080030
Austin Schuh507f7582021-07-31 20:39:55 -070031 ~SctpServer() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080032
33 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070034 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080035
Austin Schuhf95a6ab2023-05-15 14:34:57 -070036 // Frees the message returned by Read();
37 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
38 sctp_.FreeMessage(std::move(message));
39 }
40
Austin Schuh83afb7a2020-03-15 23:09:22 -070041 // 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 Schuh507f7582021-07-31 20:39:55 -070044 int time_to_live) {
45 return sctp_.SendMessage(stream, data, time_to_live, std::nullopt,
46 snd_assoc_id);
47 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080048
Austin Schuh4889b182020-11-18 19:11:56 -080049 // Aborts a connection. Returns true on success.
Sarah Newman80e955e2022-04-13 11:19:36 -070050 bool Abort(sctp_assoc_t snd_assoc_id) { return sctp_.Abort(snd_assoc_id); }
Austin Schuh4889b182020-11-18 19:11:56 -080051
Austin Schuh507f7582021-07-31 20:39:55 -070052 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080053
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 Schuh89e1e9c2023-05-15 14:38:44 -070063 void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); }
64 void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080065
Austin Schuhf95a6ab2023-05-15 14:34:57 -070066 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
67
Adam Snaider9bb33442023-06-26 16:31:37 -070068 void SetAuthKey(absl::Span<const uint8_t> auth_key) {
69 sctp_.SetAuthKey(auth_key);
70 }
71
Austin Schuhe84c3ed2019-12-14 15:29:48 -080072 private:
73 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070074 SctpReadWrite sctp_;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080075};
76
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080077} // namespace aos::message_bridge
Austin Schuhe84c3ed2019-12-14 15:29:48 -080078
79#endif // AOS_NETWORK_SCTP_SERVER_H_