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 | { |
Austin Schuh | 80b055b | 2021-03-31 21:06:24 -0700 | [diff] [blame] | 28 | // Per https://tools.ietf.org/html/rfc6458 |
| 29 | // Setting this to !0 allows event notifications to be interleaved |
| 30 | // with data if enabled, and would have to be handled in the code. |
| 31 | // Enabling interleaving would only matter during congestion, which |
| 32 | // typically only happens during application startup. |
| 33 | int interleaving = 0; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 34 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE, |
Austin Schuh | 80b055b | 2021-03-31 21:06:24 -0700 | [diff] [blame] | 35 | &interleaving, sizeof(interleaving)) == 0); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | { |
| 39 | struct sctp_initmsg initmsg; |
| 40 | memset(&initmsg, 0, sizeof(struct sctp_initmsg)); |
| 41 | initmsg.sinit_num_ostreams = streams; |
| 42 | initmsg.sinit_max_instreams = streams; |
| 43 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, |
| 44 | sizeof(struct sctp_initmsg)) == 0); |
| 45 | } |
| 46 | |
| 47 | { |
| 48 | int on = 1; |
| 49 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) == |
| 50 | 0); |
| 51 | } |
| 52 | { |
| 53 | // Servers send promptly. Clients don't. |
| 54 | // TODO(austin): Revisit this assumption when we have time sync. |
| 55 | int on = 0; |
| 56 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0); |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | // TODO(austin): This is the old style registration... But, the sctp |
| 61 | // stack out in the wild for linux is old and primitive. |
| 62 | struct sctp_event_subscribe subscribe; |
| 63 | memset(&subscribe, 0, sizeof(subscribe)); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 64 | subscribe.sctp_association_event = 1; |
Austin Schuh | f777700 | 2020-09-01 18:41:28 -0700 | [diff] [blame] | 65 | subscribe.sctp_stream_change_event = 1; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 66 | PCHECK(setsockopt(fd_, SOL_SCTP, SCTP_EVENTS, (char *)&subscribe, |
| 67 | sizeof(subscribe)) == 0); |
| 68 | } |
| 69 | |
| 70 | PCHECK(bind(fd_, (struct sockaddr *)&sockaddr_local_, |
| 71 | sockaddr_local_.ss_family == AF_INET6 |
| 72 | ? sizeof(struct sockaddr_in6) |
| 73 | : sizeof(struct sockaddr_in)) == 0); |
| 74 | VLOG(1) << "bind(" << fd_ << ", " << Address(sockaddr_local_) << ")"; |
| 75 | } |
| 76 | |
| 77 | aos::unique_c_ptr<Message> SctpClient::Read() { |
| 78 | return ReadSctpMessage(fd_, max_size_); |
| 79 | } |
| 80 | |
| 81 | bool SctpClient::Send(int stream, std::string_view data, int time_to_live) { |
| 82 | struct iovec iov; |
| 83 | iov.iov_base = const_cast<char *>(data.data()); |
| 84 | iov.iov_len = data.size(); |
| 85 | |
| 86 | struct msghdr outmsg; |
| 87 | // Target to send to. |
| 88 | outmsg.msg_name = &sockaddr_remote_; |
| 89 | outmsg.msg_namelen = sizeof(struct sockaddr_storage); |
| 90 | VLOG(1) << "Sending to " << Address(sockaddr_remote_); |
| 91 | |
| 92 | // Data to send. |
| 93 | outmsg.msg_iov = &iov; |
| 94 | outmsg.msg_iovlen = 1; |
| 95 | |
| 96 | // Build up the sndinfo message. |
| 97 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 98 | outmsg.msg_control = outcmsg; |
| 99 | outmsg.msg_controllen = sizeof(outcmsg); |
| 100 | outmsg.msg_flags = 0; |
| 101 | |
| 102 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg); |
| 103 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 104 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 105 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 106 | |
| 107 | outmsg.msg_controllen = cmsg->cmsg_len; |
| 108 | struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 109 | memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 110 | sinfo->sinfo_ppid = rand(); |
| 111 | sinfo->sinfo_stream = stream; |
| 112 | sinfo->sinfo_context = 19; |
| 113 | sinfo->sinfo_flags = 0; |
| 114 | sinfo->sinfo_timetolive = time_to_live; |
| 115 | |
| 116 | // And send. |
| 117 | const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT); |
| 118 | if (size == -1) { |
Austin Schuh | 871da99 | 2020-03-28 23:03:02 -0700 | [diff] [blame] | 119 | if (errno != EPIPE && errno != EAGAIN && errno != ESHUTDOWN) { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 120 | PCHECK(size == static_cast<ssize_t>(data.size())); |
| 121 | } else { |
| 122 | return false; |
| 123 | } |
| 124 | } else { |
| 125 | CHECK_EQ(static_cast<ssize_t>(data.size()), size); |
| 126 | } |
| 127 | |
| 128 | VLOG(1) << "Sent " << data.size(); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) { |
| 133 | message_bridge::LogSctpStatus(fd(), assoc_id); |
| 134 | } |
| 135 | |
| 136 | void SctpClient::SetPriorityScheduler(sctp_assoc_t assoc_id) { |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 137 | struct sctp_assoc_value scheduler; |
| 138 | memset(&scheduler, 0, sizeof(scheduler)); |
| 139 | scheduler.assoc_id = assoc_id; |
| 140 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 141 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 142 | sizeof(scheduler)) != 0) { |
| 143 | PLOG(WARNING) << "Failed to set scheduler"; |
| 144 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace message_bridge |
| 148 | } // namespace aos |