blob: 93d1e88c87e1619852303b1c6a4da298bf56765a [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>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08008#include <sys/socket.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07009
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080013#include <memory>
Austin Schuh387b7de2020-03-15 14:28:07 -070014#include <thread>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080015
16#include "aos/network/sctp_lib.h"
17#include "aos/unique_malloc_ptr.h"
18#include "glog/logging.h"
19
20namespace aos {
21namespace message_bridge {
22
Austin Schuh0a0a8272021-12-08 13:19:32 -080023SctpServer::SctpServer(int streams, std::string_view local_host,
24 int local_port) {
25 bool use_ipv6 = Ipv6Enabled();
26 sockaddr_local_ = ResolveSocket(local_host, local_port, use_ipv6);
Austin Schuh387b7de2020-03-15 14:28:07 -070027 while (true) {
Austin Schuh507f7582021-07-31 20:39:55 -070028 sctp_.OpenSocket(sockaddr_local_);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080029
Austin Schuh387b7de2020-03-15 14:28:07 -070030 {
Brian Silverman833dfb62021-11-03 10:47:55 -070031 struct sctp_initmsg initmsg;
32 memset(&initmsg, 0, sizeof(struct sctp_initmsg));
33 initmsg.sinit_num_ostreams = streams;
34 initmsg.sinit_max_instreams = streams;
35 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
36 sizeof(struct sctp_initmsg)) == 0);
37 }
38
39 {
Austin Schuh387b7de2020-03-15 14:28:07 -070040 // Turn off the NAGLE algorithm.
41 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070042 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) ==
Austin Schuh387b7de2020-03-15 14:28:07 -070043 0);
44 }
45
Austin Schuhf6ed4522020-12-13 16:40:38 -080046 {
47 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070048 LOG(INFO) << "setsockopt(" << fd()
49 << ", SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)";
50 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) == 0);
Austin Schuhf6ed4522020-12-13 16:40:38 -080051 }
52
Austin Schuh387b7de2020-03-15 14:28:07 -070053 // And go!
Austin Schuh507f7582021-07-31 20:39:55 -070054 if (bind(fd(), (struct sockaddr *)&sockaddr_local_,
Austin Schuh387b7de2020-03-15 14:28:07 -070055 sockaddr_local_.ss_family == AF_INET6
56 ? sizeof(struct sockaddr_in6)
57 : sizeof(struct sockaddr_in)) != 0) {
58 PLOG(ERROR) << "Failed to bind, retrying";
Austin Schuh507f7582021-07-31 20:39:55 -070059 close(fd());
Austin Schuh387b7de2020-03-15 14:28:07 -070060 std::this_thread::sleep_for(std::chrono::seconds(5));
61 continue;
62 }
Austin Schuh507f7582021-07-31 20:39:55 -070063 LOG(INFO) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")";
Austin Schuh387b7de2020-03-15 14:28:07 -070064
Austin Schuh507f7582021-07-31 20:39:55 -070065 PCHECK(listen(fd(), 100) == 0);
Austin Schuh387b7de2020-03-15 14:28:07 -070066
67 SetMaxSize(1000);
68 break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080069 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080070}
71
Austin Schuh4889b182020-11-18 19:11:56 -080072bool SctpServer::Abort(sctp_assoc_t snd_assoc_id) {
73 // Use the assoc_id for the destination instead of the msg_name.
74 struct msghdr outmsg;
75 outmsg.msg_namelen = 0;
76
77 outmsg.msg_iovlen = 0;
78
79 // Build up the sndinfo message.
80 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
81 outmsg.msg_control = outcmsg;
82 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
83 outmsg.msg_flags = 0;
84
85 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
86 cmsg->cmsg_level = IPPROTO_SCTP;
87 cmsg->cmsg_type = SCTP_SNDRCV;
88 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
89
90 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
91 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
Austin Schuh4889b182020-11-18 19:11:56 -080092 sinfo->sinfo_stream = 0;
93 sinfo->sinfo_flags = SCTP_ABORT;
94 sinfo->sinfo_assoc_id = snd_assoc_id;
95
96 // And send.
Austin Schuh507f7582021-07-31 20:39:55 -070097 const ssize_t size = sendmsg(fd(), &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
Austin Schuh4889b182020-11-18 19:11:56 -080098 if (size == -1) {
99 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
100 return false;
101 }
102 return false;
103 } else {
104 CHECK_EQ(0, size);
105 return true;
106 }
107}
108
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800109void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) {
110 struct sctp_assoc_value scheduler;
111 memset(&scheduler, 0, sizeof(scheduler));
112 scheduler.assoc_id = assoc_id;
113 scheduler.assoc_value = SCTP_SS_PRIO;
114 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
115 sizeof(scheduler)) != 0) {
116 PLOG(WARNING) << "Failed to set scheduler";
117 }
118}
119
120void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
121 uint16_t priority) {
122 struct sctp_stream_value sctp_priority;
123 memset(&sctp_priority, 0, sizeof(sctp_priority));
124 sctp_priority.assoc_id = assoc_id;
125 sctp_priority.stream_id = stream_id;
126 sctp_priority.stream_value = priority;
127 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE,
128 &sctp_priority, sizeof(sctp_priority)) != 0) {
129 PLOG(WARNING) << "Failed to set scheduler";
130 }
131}
132
133} // namespace message_bridge
134} // namespace aos