blob: a2c0439f38b5d44e4de76dab9a9bb1ca04a9702a [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#include "aos/network/sctp_client.h"
2
3#include <arpa/inet.h>
4#include <net/if.h>
5#include <netinet/sctp.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08006#include <sys/socket.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007
8#include <cstdlib>
9#include <cstring>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010#include <string_view>
11
12#include "aos/network/sctp_lib.h"
13#include "aos/unique_malloc_ptr.h"
14#include "glog/logging.h"
15
16namespace aos {
17namespace message_bridge {
18
19SctpClient::SctpClient(std::string_view remote_host, int remote_port,
Austin Schuh0a0a8272021-12-08 13:19:32 -080020 int streams, std::string_view local_host,
21 int local_port) {
22 bool use_ipv6 = Ipv6Enabled();
23 sockaddr_local_ = ResolveSocket(local_host, local_port, use_ipv6);
24 sockaddr_remote_ = ResolveSocket(remote_host, remote_port, use_ipv6);
Austin Schuh507f7582021-07-31 20:39:55 -070025 sctp_.OpenSocket(sockaddr_local_);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080026
27 {
28 struct sctp_initmsg initmsg;
29 memset(&initmsg, 0, sizeof(struct sctp_initmsg));
30 initmsg.sinit_num_ostreams = streams;
31 initmsg.sinit_max_instreams = streams;
Austin Schuh507f7582021-07-31 20:39:55 -070032 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
Austin Schuhe84c3ed2019-12-14 15:29:48 -080033 sizeof(struct sctp_initmsg)) == 0);
34 }
35
36 {
Austin Schuh8e7034c2021-11-02 20:47:47 -070037 // Turn off the NAGLE algorithm so the timestamps heading back across the
38 // network arrive promptly.
39 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070040 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080041 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080042}
43
44void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) {
45 message_bridge::LogSctpStatus(fd(), assoc_id);
46}
47
48void SctpClient::SetPriorityScheduler(sctp_assoc_t assoc_id) {
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -070049 struct sctp_assoc_value scheduler;
50 memset(&scheduler, 0, sizeof(scheduler));
51 scheduler.assoc_id = assoc_id;
52 scheduler.assoc_value = SCTP_SS_PRIO;
53 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
54 sizeof(scheduler)) != 0) {
Sarah Newman29da5ed2022-04-28 18:51:18 -070055 LOG_FIRST_N(WARNING, 1) << "Failed to set scheduler: " << strerror(errno)
56 << " [" << errno << "]";
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -070057 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080058}
59
60} // namespace message_bridge
61} // namespace aos