blob: d77392e83bc01857da925be81bb6acc5cd4e3850 [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 {
28 // Allow the kernel to deliver messages from different streams in any order.
29 int full_interleaving = 2;
30 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE,
31 &full_interleaving, sizeof(full_interleaving)) == 0);
32 }
33
34 {
35 struct sctp_initmsg initmsg;
36 memset(&initmsg, 0, sizeof(struct sctp_initmsg));
37 initmsg.sinit_num_ostreams = streams;
38 initmsg.sinit_max_instreams = streams;
39 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
40 sizeof(struct sctp_initmsg)) == 0);
41 }
42
43 {
44 int on = 1;
45 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) ==
46 0);
47 }
48 {
49 // Servers send promptly. Clients don't.
50 // TODO(austin): Revisit this assumption when we have time sync.
51 int on = 0;
52 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0);
53 }
54
55 {
56 // TODO(austin): This is the old style registration... But, the sctp
57 // stack out in the wild for linux is old and primitive.
58 struct sctp_event_subscribe subscribe;
59 memset(&subscribe, 0, sizeof(subscribe));
60 subscribe.sctp_data_io_event = 1;
61 subscribe.sctp_association_event = 1;
Austin Schuhf7777002020-09-01 18:41:28 -070062 subscribe.sctp_stream_change_event = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063 PCHECK(setsockopt(fd_, SOL_SCTP, SCTP_EVENTS, (char *)&subscribe,
64 sizeof(subscribe)) == 0);
65 }
66
67 PCHECK(bind(fd_, (struct sockaddr *)&sockaddr_local_,
68 sockaddr_local_.ss_family == AF_INET6
69 ? sizeof(struct sockaddr_in6)
70 : sizeof(struct sockaddr_in)) == 0);
71 VLOG(1) << "bind(" << fd_ << ", " << Address(sockaddr_local_) << ")";
72}
73
74aos::unique_c_ptr<Message> SctpClient::Read() {
75 return ReadSctpMessage(fd_, max_size_);
76}
77
78bool SctpClient::Send(int stream, std::string_view data, int time_to_live) {
79 struct iovec iov;
80 iov.iov_base = const_cast<char *>(data.data());
81 iov.iov_len = data.size();
82
83 struct msghdr outmsg;
84 // Target to send to.
85 outmsg.msg_name = &sockaddr_remote_;
86 outmsg.msg_namelen = sizeof(struct sockaddr_storage);
87 VLOG(1) << "Sending to " << Address(sockaddr_remote_);
88
89 // Data to send.
90 outmsg.msg_iov = &iov;
91 outmsg.msg_iovlen = 1;
92
93 // Build up the sndinfo message.
94 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
95 outmsg.msg_control = outcmsg;
96 outmsg.msg_controllen = sizeof(outcmsg);
97 outmsg.msg_flags = 0;
98
99 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
100 cmsg->cmsg_level = IPPROTO_SCTP;
101 cmsg->cmsg_type = SCTP_SNDRCV;
102 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
103
104 outmsg.msg_controllen = cmsg->cmsg_len;
105 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
106 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
107 sinfo->sinfo_ppid = rand();
108 sinfo->sinfo_stream = stream;
109 sinfo->sinfo_context = 19;
110 sinfo->sinfo_flags = 0;
111 sinfo->sinfo_timetolive = time_to_live;
112
113 // And send.
114 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
115 if (size == -1) {
Austin Schuh871da992020-03-28 23:03:02 -0700116 if (errno != EPIPE && errno != EAGAIN && errno != ESHUTDOWN) {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800117 PCHECK(size == static_cast<ssize_t>(data.size()));
118 } else {
119 return false;
120 }
121 } else {
122 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
123 }
124
125 VLOG(1) << "Sent " << data.size();
126 return true;
127}
128
129void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) {
130 message_bridge::LogSctpStatus(fd(), assoc_id);
131}
132
133void SctpClient::SetPriorityScheduler(sctp_assoc_t assoc_id) {
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -0700134 struct sctp_assoc_value scheduler;
135 memset(&scheduler, 0, sizeof(scheduler));
136 scheduler.assoc_id = assoc_id;
137 scheduler.assoc_value = SCTP_SS_PRIO;
138 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
139 sizeof(scheduler)) != 0) {
140 PLOG(WARNING) << "Failed to set scheduler";
141 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800142}
143
144} // namespace message_bridge
145} // namespace aos