blob: 894a1f186b81d1b493ee148e7f6aad21ecbae156 [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
23SctpServer::SctpServer(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 {
Austin Schuh387b7de2020-03-15 14:28:07 -070029 // Turn off the NAGLE algorithm.
30 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070031 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) ==
Austin Schuh387b7de2020-03-15 14:28:07 -070032 0);
33 }
34
Austin Schuhf6ed4522020-12-13 16:40:38 -080035 {
36 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070037 LOG(INFO) << "setsockopt(" << fd()
38 << ", SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)";
39 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) == 0);
Austin Schuhf6ed4522020-12-13 16:40:38 -080040 }
41
Austin Schuh387b7de2020-03-15 14:28:07 -070042 // And go!
Austin Schuh507f7582021-07-31 20:39:55 -070043 if (bind(fd(), (struct sockaddr *)&sockaddr_local_,
Austin Schuh387b7de2020-03-15 14:28:07 -070044 sockaddr_local_.ss_family == AF_INET6
45 ? sizeof(struct sockaddr_in6)
46 : sizeof(struct sockaddr_in)) != 0) {
47 PLOG(ERROR) << "Failed to bind, retrying";
Austin Schuh507f7582021-07-31 20:39:55 -070048 close(fd());
Austin Schuh387b7de2020-03-15 14:28:07 -070049 std::this_thread::sleep_for(std::chrono::seconds(5));
50 continue;
51 }
Austin Schuh507f7582021-07-31 20:39:55 -070052 LOG(INFO) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")";
Austin Schuh387b7de2020-03-15 14:28:07 -070053
Austin Schuh507f7582021-07-31 20:39:55 -070054 PCHECK(listen(fd(), 100) == 0);
Austin Schuh387b7de2020-03-15 14:28:07 -070055
56 SetMaxSize(1000);
57 break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080058 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080059}
60
Austin Schuh4889b182020-11-18 19:11:56 -080061bool SctpServer::Abort(sctp_assoc_t snd_assoc_id) {
62 // Use the assoc_id for the destination instead of the msg_name.
63 struct msghdr outmsg;
64 outmsg.msg_namelen = 0;
65
66 outmsg.msg_iovlen = 0;
67
68 // Build up the sndinfo message.
69 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
70 outmsg.msg_control = outcmsg;
71 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
72 outmsg.msg_flags = 0;
73
74 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
75 cmsg->cmsg_level = IPPROTO_SCTP;
76 cmsg->cmsg_type = SCTP_SNDRCV;
77 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
78
79 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
80 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
Austin Schuh4889b182020-11-18 19:11:56 -080081 sinfo->sinfo_stream = 0;
82 sinfo->sinfo_flags = SCTP_ABORT;
83 sinfo->sinfo_assoc_id = snd_assoc_id;
84
85 // And send.
Austin Schuh507f7582021-07-31 20:39:55 -070086 const ssize_t size = sendmsg(fd(), &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
Austin Schuh4889b182020-11-18 19:11:56 -080087 if (size == -1) {
88 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
89 return false;
90 }
91 return false;
92 } else {
93 CHECK_EQ(0, size);
94 return true;
95 }
96}
97
Austin Schuhe84c3ed2019-12-14 15:29:48 -080098void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) {
99 struct sctp_assoc_value scheduler;
100 memset(&scheduler, 0, sizeof(scheduler));
101 scheduler.assoc_id = assoc_id;
102 scheduler.assoc_value = SCTP_SS_PRIO;
103 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
104 sizeof(scheduler)) != 0) {
105 PLOG(WARNING) << "Failed to set scheduler";
106 }
107}
108
109void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
110 uint16_t priority) {
111 struct sctp_stream_value sctp_priority;
112 memset(&sctp_priority, 0, sizeof(sctp_priority));
113 sctp_priority.assoc_id = assoc_id;
114 sctp_priority.stream_id = stream_id;
115 sctp_priority.stream_value = priority;
116 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE,
117 &sctp_priority, sizeof(sctp_priority)) != 0) {
118 PLOG(WARNING) << "Failed to set scheduler";
119 }
120}
121
122} // namespace message_bridge
123} // namespace aos