Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #include "aos/network/sctp_client.h" |
| 2 | |
| 3 | #include <arpa/inet.h> |
| 4 | #include <net/if.h> |
| 5 | #include <netinet/sctp.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <sys/socket.h> |
| 9 | #include <string_view> |
| 10 | |
| 11 | #include "aos/network/sctp_lib.h" |
| 12 | #include "aos/unique_malloc_ptr.h" |
| 13 | #include "glog/logging.h" |
| 14 | |
| 15 | namespace aos { |
| 16 | namespace message_bridge { |
| 17 | |
| 18 | SctpClient::SctpClient(std::string_view remote_host, int remote_port, |
| 19 | int streams, std::string_view local_host, int local_port) |
| 20 | : sockaddr_remote_(ResolveSocket(remote_host, remote_port)), |
| 21 | sockaddr_local_(ResolveSocket(local_host, local_port)), |
| 22 | fd_(socket(sockaddr_local_.ss_family, SOCK_SEQPACKET, IPPROTO_SCTP)) { |
| 23 | LOG(INFO) << "socket(" << Family(sockaddr_local_) |
| 24 | << ", SOCK_SEQPACKET, IPPROTOSCTP) = " << fd_; |
| 25 | PCHECK(fd_ != -1); |
| 26 | |
| 27 | { |
| 28 | // Allow the kernel to deliver messages from different streams in any order. |
| 29 | int full_interleaving = 2; |
| 30 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE, |
| 31 | &full_interleaving, sizeof(full_interleaving)) == 0); |
| 32 | } |
| 33 | |
| 34 | { |
| 35 | struct sctp_initmsg initmsg; |
| 36 | memset(&initmsg, 0, sizeof(struct sctp_initmsg)); |
| 37 | initmsg.sinit_num_ostreams = streams; |
| 38 | initmsg.sinit_max_instreams = streams; |
| 39 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, |
| 40 | sizeof(struct sctp_initmsg)) == 0); |
| 41 | } |
| 42 | |
| 43 | { |
| 44 | int on = 1; |
| 45 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) == |
| 46 | 0); |
| 47 | } |
| 48 | { |
| 49 | // Servers send promptly. Clients don't. |
| 50 | // TODO(austin): Revisit this assumption when we have time sync. |
| 51 | int on = 0; |
| 52 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0); |
| 53 | } |
| 54 | |
| 55 | { |
| 56 | // TODO(austin): This is the old style registration... But, the sctp |
| 57 | // stack out in the wild for linux is old and primitive. |
| 58 | struct sctp_event_subscribe subscribe; |
| 59 | memset(&subscribe, 0, sizeof(subscribe)); |
| 60 | subscribe.sctp_data_io_event = 1; |
| 61 | subscribe.sctp_association_event = 1; |
| 62 | PCHECK(setsockopt(fd_, SOL_SCTP, SCTP_EVENTS, (char *)&subscribe, |
| 63 | sizeof(subscribe)) == 0); |
| 64 | } |
| 65 | |
| 66 | PCHECK(bind(fd_, (struct sockaddr *)&sockaddr_local_, |
| 67 | sockaddr_local_.ss_family == AF_INET6 |
| 68 | ? sizeof(struct sockaddr_in6) |
| 69 | : sizeof(struct sockaddr_in)) == 0); |
| 70 | VLOG(1) << "bind(" << fd_ << ", " << Address(sockaddr_local_) << ")"; |
| 71 | } |
| 72 | |
| 73 | aos::unique_c_ptr<Message> SctpClient::Read() { |
| 74 | return ReadSctpMessage(fd_, max_size_); |
| 75 | } |
| 76 | |
| 77 | bool SctpClient::Send(int stream, std::string_view data, int time_to_live) { |
| 78 | struct iovec iov; |
| 79 | iov.iov_base = const_cast<char *>(data.data()); |
| 80 | iov.iov_len = data.size(); |
| 81 | |
| 82 | struct msghdr outmsg; |
| 83 | // Target to send to. |
| 84 | outmsg.msg_name = &sockaddr_remote_; |
| 85 | outmsg.msg_namelen = sizeof(struct sockaddr_storage); |
| 86 | VLOG(1) << "Sending to " << Address(sockaddr_remote_); |
| 87 | |
| 88 | // Data to send. |
| 89 | outmsg.msg_iov = &iov; |
| 90 | outmsg.msg_iovlen = 1; |
| 91 | |
| 92 | // Build up the sndinfo message. |
| 93 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 94 | outmsg.msg_control = outcmsg; |
| 95 | outmsg.msg_controllen = sizeof(outcmsg); |
| 96 | outmsg.msg_flags = 0; |
| 97 | |
| 98 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg); |
| 99 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 100 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 101 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 102 | |
| 103 | outmsg.msg_controllen = cmsg->cmsg_len; |
| 104 | struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 105 | memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 106 | sinfo->sinfo_ppid = rand(); |
| 107 | sinfo->sinfo_stream = stream; |
| 108 | sinfo->sinfo_context = 19; |
| 109 | sinfo->sinfo_flags = 0; |
| 110 | sinfo->sinfo_timetolive = time_to_live; |
| 111 | |
| 112 | // And send. |
| 113 | const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT); |
| 114 | if (size == -1) { |
| 115 | if (errno != EPIPE && errno != EAGAIN) { |
| 116 | PCHECK(size == static_cast<ssize_t>(data.size())); |
| 117 | } else { |
| 118 | return false; |
| 119 | } |
| 120 | } else { |
| 121 | CHECK_EQ(static_cast<ssize_t>(data.size()), size); |
| 122 | } |
| 123 | |
| 124 | VLOG(1) << "Sent " << data.size(); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) { |
| 129 | message_bridge::LogSctpStatus(fd(), assoc_id); |
| 130 | } |
| 131 | |
| 132 | void SctpClient::SetPriorityScheduler(sctp_assoc_t assoc_id) { |
| 133 | struct sctp_assoc_value scheduler; |
| 134 | memset(&scheduler, 0, sizeof(scheduler)); |
| 135 | scheduler.assoc_id = assoc_id; |
| 136 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 137 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 138 | sizeof(scheduler)) != 0) { |
| 139 | PLOG(WARNING) << "Failed to set scheduler"; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | } // namespace message_bridge |
| 144 | } // namespace aos |