blob: 093e3f057f031d87836d21c319b4e97290514eed [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
83void SctpServer::Send(std::string_view data, sctp_assoc_t snd_assoc_id,
84 int stream, int timetolive) {
85 struct iovec iov;
86 iov.iov_base = const_cast<char *>(data.data());
87 iov.iov_len = data.size();
88
89 // Use the assoc_id for the destination instead of the msg_name.
90 struct msghdr outmsg;
91 outmsg.msg_namelen = 0;
92
93 // Data to send.
94 outmsg.msg_iov = &iov;
95 outmsg.msg_iovlen = 1;
96
97 // Build up the sndinfo message.
98 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
99 outmsg.msg_control = outcmsg;
100 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
101 outmsg.msg_flags = 0;
102
103 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
104 cmsg->cmsg_level = IPPROTO_SCTP;
105 cmsg->cmsg_type = SCTP_SNDRCV;
106 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
107
108 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
109 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
110 sinfo->sinfo_ppid = ++ppid_;
111 sinfo->sinfo_stream = stream;
112 sinfo->sinfo_flags = 0;
113 sinfo->sinfo_assoc_id = snd_assoc_id;
114 sinfo->sinfo_timetolive = timetolive;
115
116 // And send.
117 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
118 if (size == -1) {
119 if (errno != EPIPE) {
120 PCHECK(size == static_cast<ssize_t>(data.size()));
121 }
122 } else {
123 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
124 }
125}
126
127void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) {
128 struct sctp_assoc_value scheduler;
129 memset(&scheduler, 0, sizeof(scheduler));
130 scheduler.assoc_id = assoc_id;
131 scheduler.assoc_value = SCTP_SS_PRIO;
132 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
133 sizeof(scheduler)) != 0) {
134 PLOG(WARNING) << "Failed to set scheduler";
135 }
136}
137
138void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
139 uint16_t priority) {
140 struct sctp_stream_value sctp_priority;
141 memset(&sctp_priority, 0, sizeof(sctp_priority));
142 sctp_priority.assoc_id = assoc_id;
143 sctp_priority.stream_id = stream_id;
144 sctp_priority.stream_value = priority;
145 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE,
146 &sctp_priority, sizeof(sctp_priority)) != 0) {
147 PLOG(WARNING) << "Failed to set scheduler";
148 }
149}
150
151} // namespace message_bridge
152} // namespace aos