blob: 952d0e9902f1f0b3a0eecc4b5d166b840c8e9fb8 [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#ifndef AOS_NETWORK_SCTP_CLIENT_H_
2#define AOS_NETWORK_SCTP_CLIENT_H_
3
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdio>
5#include <cstdlib>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08006#include <string_view>
7
Adam Snaider9bb33442023-06-26 16:31:37 -07008#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07009#include "glog/logging.h"
10
Austin Schuhe84c3ed2019-12-14 15:29:48 -080011#include "aos/network/sctp_lib.h"
12#include "aos/unique_malloc_ptr.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080013
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080014namespace aos::message_bridge {
Austin Schuhe84c3ed2019-12-14 15:29:48 -080015
16// Class to encapsulate everything needed to be a SCTP client.
17class SctpClient {
18 public:
19 SctpClient(std::string_view remote_host, int remote_port, int streams,
Adam Snaider96a0f4b2023-05-18 20:41:19 -070020 std::string_view local_host = "0.0.0.0", int local_port = 9971,
Adam Snaider9bb33442023-06-26 16:31:37 -070021 SctpAuthMethod requested_authentication = SctpAuthMethod::kNoAuth);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080022
Austin Schuh507f7582021-07-31 20:39:55 -070023 ~SctpClient() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080024
25 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070026 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080027
28 // Sends a block of data on a stream with a TTL.
Austin Schuh507f7582021-07-31 20:39:55 -070029 // TODO(austin): time_to_live should be a chrono::duration
30 bool Send(int stream, std::string_view data, int time_to_live) {
Sarah Newman80e955e2022-04-13 11:19:36 -070031 return sctp_.SendMessage(stream, data, time_to_live, sockaddr_remote_,
32 sac_assoc_id_);
Austin Schuh507f7582021-07-31 20:39:55 -070033 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080034
Sarah Newman80e955e2022-04-13 11:19:36 -070035 // Aborts a connection. Returns true on success.
36 bool Abort() { return sctp_.Abort(sac_assoc_id_); }
37
Austin Schuh507f7582021-07-31 20:39:55 -070038 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080039
40 // Enables the priority scheduler. This is a SCTP feature which lets us
41 // configure the priority per stream so that higher priority packets don't get
42 // backed up behind lower priority packets in the networking queues.
43 void SetPriorityScheduler(sctp_assoc_t assoc_id);
44
45 // Remote to send to.
Austin Schuh50e3dca2023-07-23 14:34:27 -070046 struct sockaddr_storage sockaddr_remote() const { return sockaddr_remote_; }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080047
48 void LogSctpStatus(sctp_assoc_t assoc_id);
49
Austin Schuh89e1e9c2023-05-15 14:38:44 -070050 void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); }
51 void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); }
Austin Schuh4f558e52023-05-08 21:51:31 -070052 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080053
Sarah Newman80e955e2022-04-13 11:19:36 -070054 void SetAssociationId(sctp_assoc_t sac_assoc_id) {
55 sac_assoc_id_ = sac_assoc_id;
56 }
57
Austin Schuh4f558e52023-05-08 21:51:31 -070058 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
59 sctp_.FreeMessage(std::move(message));
60 }
61
Adam Snaider9bb33442023-06-26 16:31:37 -070062 void SetAuthKey(absl::Span<const uint8_t> auth_key) {
63 sctp_.SetAuthKey(auth_key);
64 }
65
Austin Schuhe84c3ed2019-12-14 15:29:48 -080066 private:
67 struct sockaddr_storage sockaddr_remote_;
68 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070069 SctpReadWrite sctp_;
Sarah Newman80e955e2022-04-13 11:19:36 -070070
71 // Valid if != 0.
72 sctp_assoc_t sac_assoc_id_ = 0;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080073};
74
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080075} // namespace aos::message_bridge
Austin Schuhe84c3ed2019-12-14 15:29:48 -080076
77#endif // AOS_NETWORK_SCTP_CLIENT_H_