blob: 06f6b15cf27e1abfa3aa8fd4b7ead4b1035b7e7c [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
14namespace aos {
15namespace message_bridge {
16
17// Class to encapsulate everything needed to be a SCTP client.
18class SctpClient {
19 public:
20 SctpClient(std::string_view remote_host, int remote_port, int streams,
Adam Snaider96a0f4b2023-05-18 20:41:19 -070021 std::string_view local_host = "0.0.0.0", int local_port = 9971,
Adam Snaider9bb33442023-06-26 16:31:37 -070022 SctpAuthMethod requested_authentication = SctpAuthMethod::kNoAuth);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080023
Austin Schuh507f7582021-07-31 20:39:55 -070024 ~SctpClient() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080025
26 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070027 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080028
29 // Sends a block of data on a stream with a TTL.
Austin Schuh507f7582021-07-31 20:39:55 -070030 // TODO(austin): time_to_live should be a chrono::duration
31 bool Send(int stream, std::string_view data, int time_to_live) {
Sarah Newman80e955e2022-04-13 11:19:36 -070032 return sctp_.SendMessage(stream, data, time_to_live, sockaddr_remote_,
33 sac_assoc_id_);
Austin Schuh507f7582021-07-31 20:39:55 -070034 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080035
Sarah Newman80e955e2022-04-13 11:19:36 -070036 // Aborts a connection. Returns true on success.
37 bool Abort() { return sctp_.Abort(sac_assoc_id_); }
38
Austin Schuh507f7582021-07-31 20:39:55 -070039 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080040
41 // Enables the priority scheduler. This is a SCTP feature which lets us
42 // configure the priority per stream so that higher priority packets don't get
43 // backed up behind lower priority packets in the networking queues.
44 void SetPriorityScheduler(sctp_assoc_t assoc_id);
45
46 // Remote to send to.
47 struct sockaddr_storage sockaddr_remote() const {
48 return sockaddr_remote_;
49 }
50
51 void LogSctpStatus(sctp_assoc_t assoc_id);
52
Austin Schuh89e1e9c2023-05-15 14:38:44 -070053 void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); }
54 void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); }
Austin Schuh4f558e52023-05-08 21:51:31 -070055 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080056
Sarah Newman80e955e2022-04-13 11:19:36 -070057 void SetAssociationId(sctp_assoc_t sac_assoc_id) {
58 sac_assoc_id_ = sac_assoc_id;
59 }
60
Austin Schuh4f558e52023-05-08 21:51:31 -070061 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
62 sctp_.FreeMessage(std::move(message));
63 }
64
Adam Snaider9bb33442023-06-26 16:31:37 -070065 void SetAuthKey(absl::Span<const uint8_t> auth_key) {
66 sctp_.SetAuthKey(auth_key);
67 }
68
Austin Schuhe84c3ed2019-12-14 15:29:48 -080069 private:
70 struct sockaddr_storage sockaddr_remote_;
71 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070072 SctpReadWrite sctp_;
Sarah Newman80e955e2022-04-13 11:19:36 -070073
74 // Valid if != 0.
75 sctp_assoc_t sac_assoc_id_ = 0;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080076};
77
78} // namespace message_bridge
79} // namespace aos
80
81#endif // AOS_NETWORK_SCTP_CLIENT_H_