blob: 8bf14f637f3af6aca8dd4237192b681865c4c1fe [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
Austin Schuhf6ed4522020-12-13 16:40:38 -080060 {
61 int on = 1;
62 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) == 0);
63 }
64
Austin Schuh387b7de2020-03-15 14:28:07 -070065 // And go!
66 if (bind(fd_, (struct sockaddr *)&sockaddr_local_,
67 sockaddr_local_.ss_family == AF_INET6
68 ? sizeof(struct sockaddr_in6)
69 : sizeof(struct sockaddr_in)) != 0) {
70 PLOG(ERROR) << "Failed to bind, retrying";
71 close(fd_);
72 std::this_thread::sleep_for(std::chrono::seconds(5));
73 continue;
74 }
75 LOG(INFO) << "bind(" << fd_ << ", " << Address(sockaddr_local_) << ")";
76
77 PCHECK(listen(fd_, 100) == 0);
78
79 SetMaxSize(1000);
80 break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080081 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080082}
83
84aos::unique_c_ptr<Message> SctpServer::Read() {
85 return ReadSctpMessage(fd_, max_size_);
86}
87
Austin Schuh4889b182020-11-18 19:11:56 -080088bool SctpServer::Abort(sctp_assoc_t snd_assoc_id) {
89 // Use the assoc_id for the destination instead of the msg_name.
90 struct msghdr outmsg;
91 outmsg.msg_namelen = 0;
92
93 outmsg.msg_iovlen = 0;
94
95 // Build up the sndinfo message.
96 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
97 outmsg.msg_control = outcmsg;
98 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
99 outmsg.msg_flags = 0;
100
101 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
102 cmsg->cmsg_level = IPPROTO_SCTP;
103 cmsg->cmsg_type = SCTP_SNDRCV;
104 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
105
106 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
107 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
108 sinfo->sinfo_ppid = ++ppid_;
109 sinfo->sinfo_stream = 0;
110 sinfo->sinfo_flags = SCTP_ABORT;
111 sinfo->sinfo_assoc_id = snd_assoc_id;
112
113 // And send.
114 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
115 if (size == -1) {
116 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
117 return false;
118 }
119 return false;
120 } else {
121 CHECK_EQ(0, size);
122 return true;
123 }
124}
125
Austin Schuh83afb7a2020-03-15 23:09:22 -0700126bool SctpServer::Send(std::string_view data, sctp_assoc_t snd_assoc_id,
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800127 int stream, int timetolive) {
128 struct iovec iov;
129 iov.iov_base = const_cast<char *>(data.data());
130 iov.iov_len = data.size();
131
132 // Use the assoc_id for the destination instead of the msg_name.
133 struct msghdr outmsg;
134 outmsg.msg_namelen = 0;
135
136 // Data to send.
137 outmsg.msg_iov = &iov;
138 outmsg.msg_iovlen = 1;
139
140 // Build up the sndinfo message.
141 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
142 outmsg.msg_control = outcmsg;
143 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
144 outmsg.msg_flags = 0;
145
146 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
147 cmsg->cmsg_level = IPPROTO_SCTP;
148 cmsg->cmsg_type = SCTP_SNDRCV;
149 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
150
151 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
152 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
153 sinfo->sinfo_ppid = ++ppid_;
154 sinfo->sinfo_stream = stream;
155 sinfo->sinfo_flags = 0;
156 sinfo->sinfo_assoc_id = snd_assoc_id;
157 sinfo->sinfo_timetolive = timetolive;
158
159 // And send.
160 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
161 if (size == -1) {
Austin Schuh40629422020-03-29 23:12:12 -0700162 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
Austin Schuh83afb7a2020-03-15 23:09:22 -0700163 return false;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800164 }
Austin Schuh83afb7a2020-03-15 23:09:22 -0700165 PCHECK(size == static_cast<ssize_t>(data.size()));
166 return false;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800167 } else {
168 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
Austin Schuh83afb7a2020-03-15 23:09:22 -0700169 return true;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800170 }
171}
172
173void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) {
174 struct sctp_assoc_value scheduler;
175 memset(&scheduler, 0, sizeof(scheduler));
176 scheduler.assoc_id = assoc_id;
177 scheduler.assoc_value = SCTP_SS_PRIO;
178 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
179 sizeof(scheduler)) != 0) {
180 PLOG(WARNING) << "Failed to set scheduler";
181 }
182}
183
184void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
185 uint16_t priority) {
186 struct sctp_stream_value sctp_priority;
187 memset(&sctp_priority, 0, sizeof(sctp_priority));
188 sctp_priority.assoc_id = assoc_id;
189 sctp_priority.stream_id = stream_id;
190 sctp_priority.stream_value = priority;
191 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE,
192 &sctp_priority, sizeof(sctp_priority)) != 0) {
193 PLOG(WARNING) << "Failed to set scheduler";
194 }
195}
196
197} // namespace message_bridge
198} // namespace aos