blob: 537f3ab8547ef0aa9380e6d66a29f51fafc07d4d [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));
Austin Schuh387b7de2020-03-15 14:28:07 -070033 subscribe.sctp_association_event = 1;
34 subscribe.sctp_send_failure_event = 1;
35 subscribe.sctp_partial_delivery_event = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080036
Austin Schuh387b7de2020-03-15 14:28:07 -070037 PCHECK(setsockopt(fd_, SOL_SCTP, SCTP_EVENTS, (char *)&subscribe,
38 sizeof(subscribe)) == 0);
39 }
40 {
41 // Enable recvinfo when a packet arrives.
42 int on = 1;
43 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on,
44 sizeof(int)) == 0);
45 }
46 {
Austin Schuh80b055b2021-03-31 21:06:24 -070047 // Per https://tools.ietf.org/html/rfc6458
48 // Setting this to !0 allows event notifications to be interleaved
49 // with data if enabled, and would have to be handled in the code.
50 // Enabling interleaving would only matter during congestion, which
51 // typically only happens during application startup.
52 int interleaving = 0;
Austin Schuh387b7de2020-03-15 14:28:07 -070053 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE,
Austin Schuh80b055b2021-03-31 21:06:24 -070054 &interleaving, sizeof(interleaving)) == 0);
Austin Schuh387b7de2020-03-15 14:28:07 -070055 }
56 {
57 // Turn off the NAGLE algorithm.
58 int on = 1;
59 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) ==
60 0);
61 }
62
Austin Schuhf6ed4522020-12-13 16:40:38 -080063 {
64 int on = 1;
65 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) == 0);
66 }
67
Austin Schuh387b7de2020-03-15 14:28:07 -070068 // And go!
69 if (bind(fd_, (struct sockaddr *)&sockaddr_local_,
70 sockaddr_local_.ss_family == AF_INET6
71 ? sizeof(struct sockaddr_in6)
72 : sizeof(struct sockaddr_in)) != 0) {
73 PLOG(ERROR) << "Failed to bind, retrying";
74 close(fd_);
75 std::this_thread::sleep_for(std::chrono::seconds(5));
76 continue;
77 }
78 LOG(INFO) << "bind(" << fd_ << ", " << Address(sockaddr_local_) << ")";
79
80 PCHECK(listen(fd_, 100) == 0);
81
82 SetMaxSize(1000);
83 break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080084 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080085}
86
87aos::unique_c_ptr<Message> SctpServer::Read() {
88 return ReadSctpMessage(fd_, max_size_);
89}
90
Austin Schuh4889b182020-11-18 19:11:56 -080091bool SctpServer::Abort(sctp_assoc_t snd_assoc_id) {
92 // Use the assoc_id for the destination instead of the msg_name.
93 struct msghdr outmsg;
94 outmsg.msg_namelen = 0;
95
96 outmsg.msg_iovlen = 0;
97
98 // Build up the sndinfo message.
99 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
100 outmsg.msg_control = outcmsg;
101 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
102 outmsg.msg_flags = 0;
103
104 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
105 cmsg->cmsg_level = IPPROTO_SCTP;
106 cmsg->cmsg_type = SCTP_SNDRCV;
107 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
108
109 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
110 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
111 sinfo->sinfo_ppid = ++ppid_;
112 sinfo->sinfo_stream = 0;
113 sinfo->sinfo_flags = SCTP_ABORT;
114 sinfo->sinfo_assoc_id = snd_assoc_id;
115
116 // And send.
117 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
118 if (size == -1) {
119 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
120 return false;
121 }
122 return false;
123 } else {
124 CHECK_EQ(0, size);
125 return true;
126 }
127}
128
Austin Schuh83afb7a2020-03-15 23:09:22 -0700129bool SctpServer::Send(std::string_view data, sctp_assoc_t snd_assoc_id,
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800130 int stream, int timetolive) {
131 struct iovec iov;
132 iov.iov_base = const_cast<char *>(data.data());
133 iov.iov_len = data.size();
134
135 // Use the assoc_id for the destination instead of the msg_name.
136 struct msghdr outmsg;
137 outmsg.msg_namelen = 0;
138
139 // Data to send.
140 outmsg.msg_iov = &iov;
141 outmsg.msg_iovlen = 1;
142
143 // Build up the sndinfo message.
144 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
145 outmsg.msg_control = outcmsg;
146 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
147 outmsg.msg_flags = 0;
148
149 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
150 cmsg->cmsg_level = IPPROTO_SCTP;
151 cmsg->cmsg_type = SCTP_SNDRCV;
152 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
153
154 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
155 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
156 sinfo->sinfo_ppid = ++ppid_;
157 sinfo->sinfo_stream = stream;
158 sinfo->sinfo_flags = 0;
159 sinfo->sinfo_assoc_id = snd_assoc_id;
160 sinfo->sinfo_timetolive = timetolive;
161
162 // And send.
163 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
164 if (size == -1) {
Austin Schuh40629422020-03-29 23:12:12 -0700165 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
Austin Schuh83afb7a2020-03-15 23:09:22 -0700166 return false;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800167 }
Austin Schuh83afb7a2020-03-15 23:09:22 -0700168 PCHECK(size == static_cast<ssize_t>(data.size()));
169 return false;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800170 } else {
171 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
Austin Schuh83afb7a2020-03-15 23:09:22 -0700172 return true;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800173 }
174}
175
176void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) {
177 struct sctp_assoc_value scheduler;
178 memset(&scheduler, 0, sizeof(scheduler));
179 scheduler.assoc_id = assoc_id;
180 scheduler.assoc_value = SCTP_SS_PRIO;
181 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
182 sizeof(scheduler)) != 0) {
183 PLOG(WARNING) << "Failed to set scheduler";
184 }
185}
186
187void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
188 uint16_t priority) {
189 struct sctp_stream_value sctp_priority;
190 memset(&sctp_priority, 0, sizeof(sctp_priority));
191 sctp_priority.assoc_id = assoc_id;
192 sctp_priority.stream_id = stream_id;
193 sctp_priority.stream_value = priority;
194 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE,
195 &sctp_priority, sizeof(sctp_priority)) != 0) {
196 PLOG(WARNING) << "Failed to set scheduler";
197 }
198}
199
200} // namespace message_bridge
201} // namespace aos