blob: 7d863168e497e2fff0e53d7bae024dc11996bf34 [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>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/socket.h>
9#include <string_view>
10
11#include "aos/network/sctp_lib.h"
12#include "aos/unique_malloc_ptr.h"
13#include "glog/logging.h"
14
15namespace aos {
16namespace message_bridge {
17
18SctpClient::SctpClient(std::string_view remote_host, int remote_port,
19 int streams, std::string_view local_host, int local_port)
20 : sockaddr_remote_(ResolveSocket(remote_host, remote_port)),
21 sockaddr_local_(ResolveSocket(local_host, local_port)),
22 fd_(socket(sockaddr_local_.ss_family, SOCK_SEQPACKET, IPPROTO_SCTP)) {
23 LOG(INFO) << "socket(" << Family(sockaddr_local_)
24 << ", SOCK_SEQPACKET, IPPROTOSCTP) = " << fd_;
25 PCHECK(fd_ != -1);
26
27 {
Austin Schuh80b055b2021-03-31 21:06:24 -070028 // Per https://tools.ietf.org/html/rfc6458
29 // Setting this to !0 allows event notifications to be interleaved
30 // with data if enabled, and would have to be handled in the code.
31 // Enabling interleaving would only matter during congestion, which
32 // typically only happens during application startup.
33 int interleaving = 0;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080034 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE,
Austin Schuh80b055b2021-03-31 21:06:24 -070035 &interleaving, sizeof(interleaving)) == 0);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080036 }
37
38 {
39 struct sctp_initmsg initmsg;
40 memset(&initmsg, 0, sizeof(struct sctp_initmsg));
41 initmsg.sinit_num_ostreams = streams;
42 initmsg.sinit_max_instreams = streams;
43 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
44 sizeof(struct sctp_initmsg)) == 0);
45 }
46
47 {
48 int on = 1;
49 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) ==
50 0);
51 }
52 {
53 // Servers send promptly. Clients don't.
54 // TODO(austin): Revisit this assumption when we have time sync.
55 int on = 0;
56 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0);
57 }
58
59 {
60 // TODO(austin): This is the old style registration... But, the sctp
61 // stack out in the wild for linux is old and primitive.
62 struct sctp_event_subscribe subscribe;
63 memset(&subscribe, 0, sizeof(subscribe));
Austin Schuhe84c3ed2019-12-14 15:29:48 -080064 subscribe.sctp_association_event = 1;
Austin Schuhf7777002020-09-01 18:41:28 -070065 subscribe.sctp_stream_change_event = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080066 PCHECK(setsockopt(fd_, SOL_SCTP, SCTP_EVENTS, (char *)&subscribe,
67 sizeof(subscribe)) == 0);
68 }
69
70 PCHECK(bind(fd_, (struct sockaddr *)&sockaddr_local_,
71 sockaddr_local_.ss_family == AF_INET6
72 ? sizeof(struct sockaddr_in6)
73 : sizeof(struct sockaddr_in)) == 0);
74 VLOG(1) << "bind(" << fd_ << ", " << Address(sockaddr_local_) << ")";
75}
76
77aos::unique_c_ptr<Message> SctpClient::Read() {
78 return ReadSctpMessage(fd_, max_size_);
79}
80
81bool SctpClient::Send(int stream, std::string_view data, int time_to_live) {
82 struct iovec iov;
83 iov.iov_base = const_cast<char *>(data.data());
84 iov.iov_len = data.size();
85
86 struct msghdr outmsg;
87 // Target to send to.
88 outmsg.msg_name = &sockaddr_remote_;
89 outmsg.msg_namelen = sizeof(struct sockaddr_storage);
90 VLOG(1) << "Sending to " << Address(sockaddr_remote_);
91
92 // Data to send.
93 outmsg.msg_iov = &iov;
94 outmsg.msg_iovlen = 1;
95
96 // Build up the sndinfo message.
97 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
98 outmsg.msg_control = outcmsg;
99 outmsg.msg_controllen = sizeof(outcmsg);
100 outmsg.msg_flags = 0;
101
102 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
103 cmsg->cmsg_level = IPPROTO_SCTP;
104 cmsg->cmsg_type = SCTP_SNDRCV;
105 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
106
107 outmsg.msg_controllen = cmsg->cmsg_len;
108 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
109 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
110 sinfo->sinfo_ppid = rand();
111 sinfo->sinfo_stream = stream;
112 sinfo->sinfo_context = 19;
113 sinfo->sinfo_flags = 0;
114 sinfo->sinfo_timetolive = time_to_live;
115
116 // And send.
117 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
118 if (size == -1) {
Austin Schuh871da992020-03-28 23:03:02 -0700119 if (errno != EPIPE && errno != EAGAIN && errno != ESHUTDOWN) {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800120 PCHECK(size == static_cast<ssize_t>(data.size()));
121 } else {
122 return false;
123 }
124 } else {
125 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
126 }
127
128 VLOG(1) << "Sent " << data.size();
129 return true;
130}
131
132void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) {
133 message_bridge::LogSctpStatus(fd(), assoc_id);
134}
135
136void SctpClient::SetPriorityScheduler(sctp_assoc_t assoc_id) {
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -0700137 struct sctp_assoc_value scheduler;
138 memset(&scheduler, 0, sizeof(scheduler));
139 scheduler.assoc_id = assoc_id;
140 scheduler.assoc_value = SCTP_SS_PRIO;
141 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
142 sizeof(scheduler)) != 0) {
143 PLOG(WARNING) << "Failed to set scheduler";
144 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800145}
146
147} // namespace message_bridge
148} // namespace aos