blob: e18d0d885e39fd1cac9c5e90713033fae8f7a8dd [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#include "aos/network/sctp_server.h"
2
3#include <arpa/inet.h>
4#include <net/if.h>
5#include <netdb.h>
6#include <netinet/in.h>
7#include <netinet/sctp.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/socket.h>
12#include <memory>
Austin Schuh387b7de2020-03-15 14:28:07 -070013#include <thread>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080014
15#include "aos/network/sctp_lib.h"
16#include "aos/unique_malloc_ptr.h"
17#include "glog/logging.h"
18
19namespace aos {
20namespace message_bridge {
21
22SctpServer::SctpServer(std::string_view local_host, int local_port)
Austin Schuh387b7de2020-03-15 14:28:07 -070023 : sockaddr_local_(ResolveSocket(local_host, local_port)) {
24 while (true) {
25 fd_ = socket(sockaddr_local_.ss_family, SOCK_SEQPACKET, IPPROTO_SCTP);
26 LOG(INFO) << "socket(" << Family(sockaddr_local_)
27 << ", SOCK_SEQPACKET, IPPROTOSCTP) = " << fd_;
28 PCHECK(fd_ != -1);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080029
Austin Schuh387b7de2020-03-15 14:28:07 -070030 {
31 struct sctp_event_subscribe subscribe;
32 memset(&subscribe, 0, sizeof(subscribe));
33 subscribe.sctp_data_io_event = 1;
34 subscribe.sctp_association_event = 1;
35 subscribe.sctp_send_failure_event = 1;
36 subscribe.sctp_partial_delivery_event = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080037
Austin Schuh387b7de2020-03-15 14:28:07 -070038 PCHECK(setsockopt(fd_, SOL_SCTP, SCTP_EVENTS, (char *)&subscribe,
39 sizeof(subscribe)) == 0);
40 }
41 {
42 // Enable recvinfo when a packet arrives.
43 int on = 1;
44 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on,
45 sizeof(int)) == 0);
46 }
47 {
48 // Allow one packet on the wire to have multiple source packets.
49 int full_interleaving = 2;
50 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE,
51 &full_interleaving, sizeof(full_interleaving)) == 0);
52 }
53 {
54 // Turn off the NAGLE algorithm.
55 int on = 1;
56 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) ==
57 0);
58 }
59
60 // And go!
61 if (bind(fd_, (struct sockaddr *)&sockaddr_local_,
62 sockaddr_local_.ss_family == AF_INET6
63 ? sizeof(struct sockaddr_in6)
64 : sizeof(struct sockaddr_in)) != 0) {
65 PLOG(ERROR) << "Failed to bind, retrying";
66 close(fd_);
67 std::this_thread::sleep_for(std::chrono::seconds(5));
68 continue;
69 }
70 LOG(INFO) << "bind(" << fd_ << ", " << Address(sockaddr_local_) << ")";
71
72 PCHECK(listen(fd_, 100) == 0);
73
74 SetMaxSize(1000);
75 break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080076 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080077}
78
79aos::unique_c_ptr<Message> SctpServer::Read() {
80 return ReadSctpMessage(fd_, max_size_);
81}
82
Austin Schuh4889b182020-11-18 19:11:56 -080083bool SctpServer::Abort(sctp_assoc_t snd_assoc_id) {
84 // Use the assoc_id for the destination instead of the msg_name.
85 struct msghdr outmsg;
86 outmsg.msg_namelen = 0;
87
88 outmsg.msg_iovlen = 0;
89
90 // Build up the sndinfo message.
91 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
92 outmsg.msg_control = outcmsg;
93 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
94 outmsg.msg_flags = 0;
95
96 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
97 cmsg->cmsg_level = IPPROTO_SCTP;
98 cmsg->cmsg_type = SCTP_SNDRCV;
99 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
100
101 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
102 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
103 sinfo->sinfo_ppid = ++ppid_;
104 sinfo->sinfo_stream = 0;
105 sinfo->sinfo_flags = SCTP_ABORT;
106 sinfo->sinfo_assoc_id = snd_assoc_id;
107
108 // And send.
109 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
110 if (size == -1) {
111 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
112 return false;
113 }
114 return false;
115 } else {
116 CHECK_EQ(0, size);
117 return true;
118 }
119}
120
Austin Schuh83afb7a2020-03-15 23:09:22 -0700121bool SctpServer::Send(std::string_view data, sctp_assoc_t snd_assoc_id,
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800122 int stream, int timetolive) {
123 struct iovec iov;
124 iov.iov_base = const_cast<char *>(data.data());
125 iov.iov_len = data.size();
126
127 // Use the assoc_id for the destination instead of the msg_name.
128 struct msghdr outmsg;
129 outmsg.msg_namelen = 0;
130
131 // Data to send.
132 outmsg.msg_iov = &iov;
133 outmsg.msg_iovlen = 1;
134
135 // Build up the sndinfo message.
136 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
137 outmsg.msg_control = outcmsg;
138 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
139 outmsg.msg_flags = 0;
140
141 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
142 cmsg->cmsg_level = IPPROTO_SCTP;
143 cmsg->cmsg_type = SCTP_SNDRCV;
144 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
145
146 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
147 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
148 sinfo->sinfo_ppid = ++ppid_;
149 sinfo->sinfo_stream = stream;
150 sinfo->sinfo_flags = 0;
151 sinfo->sinfo_assoc_id = snd_assoc_id;
152 sinfo->sinfo_timetolive = timetolive;
153
154 // And send.
155 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
156 if (size == -1) {
Austin Schuh40629422020-03-29 23:12:12 -0700157 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
Austin Schuh83afb7a2020-03-15 23:09:22 -0700158 return false;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800159 }
Austin Schuh83afb7a2020-03-15 23:09:22 -0700160 PCHECK(size == static_cast<ssize_t>(data.size()));
161 return false;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800162 } else {
163 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
Austin Schuh83afb7a2020-03-15 23:09:22 -0700164 return true;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800165 }
166}
167
168void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) {
169 struct sctp_assoc_value scheduler;
170 memset(&scheduler, 0, sizeof(scheduler));
171 scheduler.assoc_id = assoc_id;
172 scheduler.assoc_value = SCTP_SS_PRIO;
173 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
174 sizeof(scheduler)) != 0) {
175 PLOG(WARNING) << "Failed to set scheduler";
176 }
177}
178
179void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
180 uint16_t priority) {
181 struct sctp_stream_value sctp_priority;
182 memset(&sctp_priority, 0, sizeof(sctp_priority));
183 sctp_priority.assoc_id = assoc_id;
184 sctp_priority.stream_id = stream_id;
185 sctp_priority.stream_value = priority;
186 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE,
187 &sctp_priority, sizeof(sctp_priority)) != 0) {
188 PLOG(WARNING) << "Failed to set scheduler";
189 }
190}
191
192} // namespace message_bridge
193} // namespace aos