blob: 33046d0830a36e1530005a7b0d11d8e3add8150c [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
Brian Silverman833dfb62021-11-03 10:47:55 -070023SctpServer::SctpServer(int streams, std::string_view local_host, int local_port)
Austin Schuh387b7de2020-03-15 14:28:07 -070024 : sockaddr_local_(ResolveSocket(local_host, local_port)) {
25 while (true) {
Austin Schuh507f7582021-07-31 20:39:55 -070026 sctp_.OpenSocket(sockaddr_local_);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080027
Austin Schuh387b7de2020-03-15 14:28:07 -070028 {
Brian Silverman833dfb62021-11-03 10:47:55 -070029 struct sctp_initmsg initmsg;
30 memset(&initmsg, 0, sizeof(struct sctp_initmsg));
31 initmsg.sinit_num_ostreams = streams;
32 initmsg.sinit_max_instreams = streams;
33 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
34 sizeof(struct sctp_initmsg)) == 0);
35 }
36
37 {
Austin Schuh387b7de2020-03-15 14:28:07 -070038 // Turn off the NAGLE algorithm.
39 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070040 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) ==
Austin Schuh387b7de2020-03-15 14:28:07 -070041 0);
42 }
43
Austin Schuhf6ed4522020-12-13 16:40:38 -080044 {
45 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070046 LOG(INFO) << "setsockopt(" << fd()
47 << ", SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)";
48 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) == 0);
Austin Schuhf6ed4522020-12-13 16:40:38 -080049 }
50
Austin Schuh387b7de2020-03-15 14:28:07 -070051 // And go!
Austin Schuh507f7582021-07-31 20:39:55 -070052 if (bind(fd(), (struct sockaddr *)&sockaddr_local_,
Austin Schuh387b7de2020-03-15 14:28:07 -070053 sockaddr_local_.ss_family == AF_INET6
54 ? sizeof(struct sockaddr_in6)
55 : sizeof(struct sockaddr_in)) != 0) {
56 PLOG(ERROR) << "Failed to bind, retrying";
Austin Schuh507f7582021-07-31 20:39:55 -070057 close(fd());
Austin Schuh387b7de2020-03-15 14:28:07 -070058 std::this_thread::sleep_for(std::chrono::seconds(5));
59 continue;
60 }
Austin Schuh507f7582021-07-31 20:39:55 -070061 LOG(INFO) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")";
Austin Schuh387b7de2020-03-15 14:28:07 -070062
Austin Schuh507f7582021-07-31 20:39:55 -070063 PCHECK(listen(fd(), 100) == 0);
Austin Schuh387b7de2020-03-15 14:28:07 -070064
65 SetMaxSize(1000);
66 break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080067 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080068}
69
Austin Schuh4889b182020-11-18 19:11:56 -080070bool SctpServer::Abort(sctp_assoc_t snd_assoc_id) {
71 // Use the assoc_id for the destination instead of the msg_name.
72 struct msghdr outmsg;
73 outmsg.msg_namelen = 0;
74
75 outmsg.msg_iovlen = 0;
76
77 // Build up the sndinfo message.
78 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
79 outmsg.msg_control = outcmsg;
80 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
81 outmsg.msg_flags = 0;
82
83 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
84 cmsg->cmsg_level = IPPROTO_SCTP;
85 cmsg->cmsg_type = SCTP_SNDRCV;
86 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
87
88 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
89 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
Austin Schuh4889b182020-11-18 19:11:56 -080090 sinfo->sinfo_stream = 0;
91 sinfo->sinfo_flags = SCTP_ABORT;
92 sinfo->sinfo_assoc_id = snd_assoc_id;
93
94 // And send.
Austin Schuh507f7582021-07-31 20:39:55 -070095 const ssize_t size = sendmsg(fd(), &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
Austin Schuh4889b182020-11-18 19:11:56 -080096 if (size == -1) {
97 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
98 return false;
99 }
100 return false;
101 } else {
102 CHECK_EQ(0, size);
103 return true;
104 }
105}
106
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800107void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) {
108 struct sctp_assoc_value scheduler;
109 memset(&scheduler, 0, sizeof(scheduler));
110 scheduler.assoc_id = assoc_id;
111 scheduler.assoc_value = SCTP_SS_PRIO;
112 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
113 sizeof(scheduler)) != 0) {
114 PLOG(WARNING) << "Failed to set scheduler";
115 }
116}
117
118void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
119 uint16_t priority) {
120 struct sctp_stream_value sctp_priority;
121 memset(&sctp_priority, 0, sizeof(sctp_priority));
122 sctp_priority.assoc_id = assoc_id;
123 sctp_priority.stream_id = stream_id;
124 sctp_priority.stream_value = priority;
125 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE,
126 &sctp_priority, sizeof(sctp_priority)) != 0) {
127 PLOG(WARNING) << "Failed to set scheduler";
128 }
129}
130
131} // namespace message_bridge
132} // namespace aos