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> |
| 13 | |
| 14 | #include "aos/network/sctp_lib.h" |
| 15 | #include "aos/unique_malloc_ptr.h" |
| 16 | #include "glog/logging.h" |
| 17 | |
| 18 | namespace aos { |
| 19 | namespace message_bridge { |
| 20 | |
| 21 | SctpServer::SctpServer(std::string_view local_host, int local_port) |
| 22 | : sockaddr_local_(ResolveSocket(local_host, local_port)), |
| 23 | fd_(socket(sockaddr_local_.ss_family, SOCK_SEQPACKET, IPPROTO_SCTP)) { |
| 24 | LOG(INFO) << "socket(" << Family(sockaddr_local_) |
| 25 | << ", SOCK_SEQPACKET, IPPROTOSCTP) = " << fd_; |
| 26 | PCHECK(fd_ != -1); |
| 27 | |
| 28 | { |
| 29 | struct sctp_event_subscribe subscribe; |
| 30 | memset(&subscribe, 0, sizeof(subscribe)); |
| 31 | subscribe.sctp_data_io_event = 1; |
| 32 | subscribe.sctp_association_event = 1; |
| 33 | subscribe.sctp_send_failure_event = 1; |
| 34 | subscribe.sctp_partial_delivery_event = 1; |
| 35 | |
| 36 | PCHECK(setsockopt(fd_, SOL_SCTP, SCTP_EVENTS, (char *)&subscribe, |
| 37 | sizeof(subscribe)) == 0); |
| 38 | } |
| 39 | { |
| 40 | // Enable recvinfo when a packet arrives. |
| 41 | int on = 1; |
| 42 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) == |
| 43 | 0); |
| 44 | } |
| 45 | { |
| 46 | // Allow one packet on the wire to have multiple source packets. |
| 47 | int full_interleaving = 2; |
| 48 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE, |
| 49 | &full_interleaving, sizeof(full_interleaving)) == 0); |
| 50 | } |
| 51 | { |
| 52 | // Turn off the NAGLE algorithm. |
| 53 | int on = 1; |
| 54 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0); |
| 55 | } |
| 56 | |
| 57 | // And go! |
| 58 | PCHECK(bind(fd_, (struct sockaddr *)&sockaddr_local_, |
| 59 | sockaddr_local_.ss_family == AF_INET6 |
| 60 | ? sizeof(struct sockaddr_in6) |
| 61 | : sizeof(struct sockaddr_in)) == 0); |
| 62 | LOG(INFO) << "bind(" << fd_ << ", " << Address(sockaddr_local_) << ")"; |
| 63 | |
| 64 | PCHECK(listen(fd_, 100) == 0); |
| 65 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 66 | SetMaxSize(1000); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | aos::unique_c_ptr<Message> SctpServer::Read() { |
| 70 | return ReadSctpMessage(fd_, max_size_); |
| 71 | } |
| 72 | |
| 73 | void SctpServer::Send(std::string_view data, sctp_assoc_t snd_assoc_id, |
| 74 | int stream, int timetolive) { |
| 75 | struct iovec iov; |
| 76 | iov.iov_base = const_cast<char *>(data.data()); |
| 77 | iov.iov_len = data.size(); |
| 78 | |
| 79 | // Use the assoc_id for the destination instead of the msg_name. |
| 80 | struct msghdr outmsg; |
| 81 | outmsg.msg_namelen = 0; |
| 82 | |
| 83 | // Data to send. |
| 84 | outmsg.msg_iov = &iov; |
| 85 | outmsg.msg_iovlen = 1; |
| 86 | |
| 87 | // Build up the sndinfo message. |
| 88 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 89 | outmsg.msg_control = outcmsg; |
| 90 | outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); |
| 91 | outmsg.msg_flags = 0; |
| 92 | |
| 93 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg); |
| 94 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 95 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 96 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 97 | |
| 98 | struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 99 | memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 100 | sinfo->sinfo_ppid = ++ppid_; |
| 101 | sinfo->sinfo_stream = stream; |
| 102 | sinfo->sinfo_flags = 0; |
| 103 | sinfo->sinfo_assoc_id = snd_assoc_id; |
| 104 | sinfo->sinfo_timetolive = timetolive; |
| 105 | |
| 106 | // And send. |
| 107 | const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT); |
| 108 | if (size == -1) { |
| 109 | if (errno != EPIPE) { |
| 110 | PCHECK(size == static_cast<ssize_t>(data.size())); |
| 111 | } |
| 112 | } else { |
| 113 | CHECK_EQ(static_cast<ssize_t>(data.size()), size); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void SctpServer::SetPriorityScheduler(sctp_assoc_t assoc_id) { |
| 118 | struct sctp_assoc_value scheduler; |
| 119 | memset(&scheduler, 0, sizeof(scheduler)); |
| 120 | scheduler.assoc_id = assoc_id; |
| 121 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 122 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 123 | sizeof(scheduler)) != 0) { |
| 124 | PLOG(WARNING) << "Failed to set scheduler"; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void SctpServer::SetStreamPriority(sctp_assoc_t assoc_id, int stream_id, |
| 129 | uint16_t priority) { |
| 130 | struct sctp_stream_value sctp_priority; |
| 131 | memset(&sctp_priority, 0, sizeof(sctp_priority)); |
| 132 | sctp_priority.assoc_id = assoc_id; |
| 133 | sctp_priority.stream_id = stream_id; |
| 134 | sctp_priority.stream_value = priority; |
| 135 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE, |
| 136 | &sctp_priority, sizeof(sctp_priority)) != 0) { |
| 137 | PLOG(WARNING) << "Failed to set scheduler"; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } // namespace message_bridge |
| 142 | } // namespace aos |