Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #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 Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 13 | #include <thread> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 14 | |
| 15 | #include "aos/network/sctp_lib.h" |
| 16 | #include "aos/unique_malloc_ptr.h" |
| 17 | #include "glog/logging.h" |
| 18 | |
| 19 | namespace aos { |
| 20 | namespace message_bridge { |
| 21 | |
| 22 | SctpServer::SctpServer(std::string_view local_host, int local_port) |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 23 | : 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 30 | { |
| 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 37 | |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 38 | 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 76 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | aos::unique_c_ptr<Message> SctpServer::Read() { |
| 80 | return ReadSctpMessage(fd_, max_size_); |
| 81 | } |
| 82 | |
Austin Schuh | 83afb7a | 2020-03-15 23:09:22 -0700 | [diff] [blame^] | 83 | bool SctpServer::Send(std::string_view data, sctp_assoc_t snd_assoc_id, |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 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) { |
Austin Schuh | 83afb7a | 2020-03-15 23:09:22 -0700 | [diff] [blame^] | 119 | if (errno == EPIPE || errno == EAGAIN) { |
| 120 | return false; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 121 | } |
Austin Schuh | 83afb7a | 2020-03-15 23:09:22 -0700 | [diff] [blame^] | 122 | PCHECK(size == static_cast<ssize_t>(data.size())); |
| 123 | return false; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 124 | } else { |
| 125 | CHECK_EQ(static_cast<ssize_t>(data.size()), size); |
Austin Schuh | 83afb7a | 2020-03-15 23:09:22 -0700 | [diff] [blame^] | 126 | return true; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) { |
| 131 | struct sctp_assoc_value scheduler; |
| 132 | memset(&scheduler, 0, sizeof(scheduler)); |
| 133 | scheduler.assoc_id = assoc_id; |
| 134 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 135 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 136 | sizeof(scheduler)) != 0) { |
| 137 | PLOG(WARNING) << "Failed to set scheduler"; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id, |
| 142 | uint16_t priority) { |
| 143 | struct sctp_stream_value sctp_priority; |
| 144 | memset(&sctp_priority, 0, sizeof(sctp_priority)); |
| 145 | sctp_priority.assoc_id = assoc_id; |
| 146 | sctp_priority.stream_id = stream_id; |
| 147 | sctp_priority.stream_value = priority; |
| 148 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE, |
| 149 | &sctp_priority, sizeof(sctp_priority)) != 0) { |
| 150 | PLOG(WARNING) << "Failed to set scheduler"; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } // namespace message_bridge |
| 155 | } // namespace aos |