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 | |
Austin Schuh | 3c4af57 | 2021-11-11 18:47:33 -0800 | [diff] [blame] | 16 | DEFINE_int32(sinit_max_init_timeout, 0, |
| 17 | "Timeout in milliseconds for retrying the INIT packet when " |
| 18 | "connecting to the message bridge server"); |
| 19 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 20 | namespace aos { |
| 21 | namespace message_bridge { |
| 22 | |
| 23 | SctpClient::SctpClient(std::string_view remote_host, int remote_port, |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 24 | int streams, std::string_view local_host, |
| 25 | int local_port) { |
| 26 | bool use_ipv6 = Ipv6Enabled(); |
| 27 | sockaddr_local_ = ResolveSocket(local_host, local_port, use_ipv6); |
| 28 | sockaddr_remote_ = ResolveSocket(remote_host, remote_port, use_ipv6); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 29 | sctp_.OpenSocket(sockaddr_local_); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 30 | |
| 31 | { |
| 32 | struct sctp_initmsg initmsg; |
| 33 | memset(&initmsg, 0, sizeof(struct sctp_initmsg)); |
| 34 | initmsg.sinit_num_ostreams = streams; |
| 35 | initmsg.sinit_max_instreams = streams; |
Austin Schuh | 3c4af57 | 2021-11-11 18:47:33 -0800 | [diff] [blame] | 36 | // Max timeout in milliseconds for the INIT packet. |
| 37 | initmsg.sinit_max_init_timeo = FLAGS_sinit_max_init_timeout; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 38 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg, |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 39 | sizeof(struct sctp_initmsg)) == 0); |
| 40 | } |
| 41 | |
| 42 | { |
Austin Schuh | 8e7034c | 2021-11-02 20:47:47 -0700 | [diff] [blame] | 43 | // Turn off the NAGLE algorithm so the timestamps heading back across the |
| 44 | // network arrive promptly. |
| 45 | int on = 1; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 46 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 47 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) { |
| 51 | message_bridge::LogSctpStatus(fd(), assoc_id); |
| 52 | } |
| 53 | |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame^] | 54 | void SctpClient::SetPriorityScheduler([[maybe_unused]] sctp_assoc_t assoc_id) { |
| 55 | // Kernel 4.9 does not have SCTP_SS_PRIO |
| 56 | #ifdef SCTP_SS_PRIO |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 57 | struct sctp_assoc_value scheduler; |
| 58 | memset(&scheduler, 0, sizeof(scheduler)); |
| 59 | scheduler.assoc_id = assoc_id; |
| 60 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 61 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 62 | sizeof(scheduler)) != 0) { |
Sarah Newman | 29da5ed | 2022-04-28 18:51:18 -0700 | [diff] [blame] | 63 | LOG_FIRST_N(WARNING, 1) << "Failed to set scheduler: " << strerror(errno) |
| 64 | << " [" << errno << "]"; |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 65 | } |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame^] | 66 | #endif |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace message_bridge |
| 70 | } // namespace aos |