blob: 1033e3144a652f96b983a3637a9d827aa3aee816 [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 }
42
Austin Schuh507f7582021-07-31 20:39:55 -070043 PCHECK(bind(fd(), (struct sockaddr *)&sockaddr_local_,
Austin Schuhe84c3ed2019-12-14 15:29:48 -080044 sockaddr_local_.ss_family == AF_INET6
45 ? sizeof(struct sockaddr_in6)
46 : sizeof(struct sockaddr_in)) == 0);
Austin Schuh507f7582021-07-31 20:39:55 -070047 VLOG(1) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")";
Austin Schuhe84c3ed2019-12-14 15:29:48 -080048}
49
50void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) {
51 message_bridge::LogSctpStatus(fd(), assoc_id);
52}
53
54void SctpClient::SetPriorityScheduler(sctp_assoc_t assoc_id) {
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -070055 struct sctp_assoc_value scheduler;
56 memset(&scheduler, 0, sizeof(scheduler));
57 scheduler.assoc_id = assoc_id;
58 scheduler.assoc_value = SCTP_SS_PRIO;
59 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
60 sizeof(scheduler)) != 0) {
61 PLOG(WARNING) << "Failed to set scheduler";
62 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063}
64
65} // namespace message_bridge
66} // namespace aos