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> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 6 | #include <sys/socket.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | |
| 8 | #include <cstdlib> |
| 9 | #include <cstring> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 10 | #include <string_view> |
| 11 | |
| 12 | #include "aos/network/sctp_lib.h" |
| 13 | #include "aos/unique_malloc_ptr.h" |
| 14 | #include "glog/logging.h" |
| 15 | |
| 16 | namespace aos { |
| 17 | namespace message_bridge { |
| 18 | |
| 19 | SctpClient::SctpClient(std::string_view remote_host, int remote_port, |
| 20 | int streams, std::string_view local_host, int local_port) |
| 21 | : sockaddr_remote_(ResolveSocket(remote_host, remote_port)), |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame^] | 22 | sockaddr_local_(ResolveSocket(local_host, local_port)) { |
| 23 | sctp_.OpenSocket(sockaddr_local_); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 24 | |
| 25 | { |
| 26 | struct sctp_initmsg initmsg; |
| 27 | memset(&initmsg, 0, sizeof(struct sctp_initmsg)); |
| 28 | initmsg.sinit_num_ostreams = streams; |
| 29 | initmsg.sinit_max_instreams = streams; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame^] | 30 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg, |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 31 | sizeof(struct sctp_initmsg)) == 0); |
| 32 | } |
| 33 | |
| 34 | { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 35 | // Servers send promptly. Clients don't. |
| 36 | // TODO(austin): Revisit this assumption when we have time sync. |
| 37 | int on = 0; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame^] | 38 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | { |
| 42 | // TODO(austin): This is the old style registration... But, the sctp |
| 43 | // stack out in the wild for linux is old and primitive. |
| 44 | struct sctp_event_subscribe subscribe; |
| 45 | memset(&subscribe, 0, sizeof(subscribe)); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 46 | subscribe.sctp_association_event = 1; |
Austin Schuh | f777700 | 2020-09-01 18:41:28 -0700 | [diff] [blame] | 47 | subscribe.sctp_stream_change_event = 1; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame^] | 48 | PCHECK(setsockopt(fd(), SOL_SCTP, SCTP_EVENTS, (char *)&subscribe, |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 49 | sizeof(subscribe)) == 0); |
| 50 | } |
| 51 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame^] | 52 | PCHECK(bind(fd(), (struct sockaddr *)&sockaddr_local_, |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 53 | sockaddr_local_.ss_family == AF_INET6 |
| 54 | ? sizeof(struct sockaddr_in6) |
| 55 | : sizeof(struct sockaddr_in)) == 0); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame^] | 56 | VLOG(1) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")"; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) { |
| 60 | message_bridge::LogSctpStatus(fd(), assoc_id); |
| 61 | } |
| 62 | |
| 63 | void SctpClient::SetPriorityScheduler(sctp_assoc_t assoc_id) { |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 64 | struct sctp_assoc_value scheduler; |
| 65 | memset(&scheduler, 0, sizeof(scheduler)); |
| 66 | scheduler.assoc_id = assoc_id; |
| 67 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 68 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 69 | sizeof(scheduler)) != 0) { |
| 70 | PLOG(WARNING) << "Failed to set scheduler"; |
| 71 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } // namespace message_bridge |
| 75 | } // namespace aos |