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> |
Adam Snaider | be26351 | 2023-05-18 20:40:23 -0700 | [diff] [blame] | 4 | #include <linux/sctp.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 5 | #include <net/if.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 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 12 | #include "absl/flags/flag.h" |
| 13 | #include "absl/log/check.h" |
| 14 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 15 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 16 | #include "aos/network/sctp_lib.h" |
| 17 | #include "aos/unique_malloc_ptr.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 18 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 19 | ABSL_FLAG(int32_t, sinit_max_init_timeout, 0, |
| 20 | "Timeout in milliseconds for retrying the INIT packet when " |
| 21 | "connecting to the message bridge server"); |
Austin Schuh | 3c4af57 | 2021-11-11 18:47:33 -0800 | [diff] [blame] | 22 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 23 | namespace aos::message_bridge { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 24 | |
| 25 | SctpClient::SctpClient(std::string_view remote_host, int remote_port, |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 26 | int streams, std::string_view local_host, int local_port, |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 27 | SctpAuthMethod requested_authentication) |
| 28 | : sctp_(requested_authentication) { |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 29 | bool use_ipv6 = Ipv6Enabled(); |
| 30 | sockaddr_local_ = ResolveSocket(local_host, local_port, use_ipv6); |
| 31 | sockaddr_remote_ = ResolveSocket(remote_host, remote_port, use_ipv6); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 32 | sctp_.OpenSocket(sockaddr_local_); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 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; |
Austin Schuh | 3c4af57 | 2021-11-11 18:47:33 -0800 | [diff] [blame] | 39 | // Max timeout in milliseconds for the INIT packet. |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 40 | initmsg.sinit_max_init_timeo = absl::GetFlag(FLAGS_sinit_max_init_timeout); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 41 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg, |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 42 | sizeof(struct sctp_initmsg)) == 0); |
| 43 | } |
| 44 | |
| 45 | { |
Austin Schuh | 8e7034c | 2021-11-02 20:47:47 -0700 | [diff] [blame] | 46 | // Turn off the NAGLE algorithm so the timestamps heading back across the |
| 47 | // network arrive promptly. |
| 48 | int on = 1; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 49 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == 0); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 50 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void SctpClient::LogSctpStatus(sctp_assoc_t assoc_id) { |
| 54 | message_bridge::LogSctpStatus(fd(), assoc_id); |
| 55 | } |
| 56 | |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame] | 57 | void SctpClient::SetPriorityScheduler([[maybe_unused]] sctp_assoc_t assoc_id) { |
| 58 | // Kernel 4.9 does not have SCTP_SS_PRIO |
| 59 | #ifdef SCTP_SS_PRIO |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 60 | struct sctp_assoc_value scheduler; |
| 61 | memset(&scheduler, 0, sizeof(scheduler)); |
| 62 | scheduler.assoc_id = assoc_id; |
| 63 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 64 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 65 | sizeof(scheduler)) != 0) { |
James Kuszmaul | 18e7118 | 2023-09-04 15:32:49 -0700 | [diff] [blame] | 66 | PLOG(FATAL) << "Failed to set scheduler."; |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 67 | } |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame] | 68 | #endif |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 71 | } // namespace aos::message_bridge |