Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #include "aos/network/sctp_server.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> |
| 6 | #include <netdb.h> |
| 7 | #include <netinet/in.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 8 | #include <sys/socket.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 9 | |
| 10 | #include <cstdio> |
| 11 | #include <cstdlib> |
| 12 | #include <cstring> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 13 | #include <memory> |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 14 | #include <thread> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 15 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 16 | #include "absl/log/check.h" |
| 17 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 18 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 19 | #include "aos/network/sctp_lib.h" |
| 20 | #include "aos/unique_malloc_ptr.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 21 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 22 | namespace aos::message_bridge { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 23 | |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 24 | SctpServer::SctpServer(int streams, std::string_view local_host, int local_port, |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 25 | SctpAuthMethod requested_authentication) |
| 26 | : sctp_(requested_authentication) { |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 27 | bool use_ipv6 = Ipv6Enabled(); |
| 28 | sockaddr_local_ = ResolveSocket(local_host, local_port, use_ipv6); |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 29 | while (true) { |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 30 | sctp_.OpenSocket(sockaddr_local_); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 32 | { |
Brian Silverman | 833dfb6 | 2021-11-03 10:47:55 -0700 | [diff] [blame] | 33 | struct sctp_initmsg initmsg; |
| 34 | memset(&initmsg, 0, sizeof(struct sctp_initmsg)); |
| 35 | initmsg.sinit_num_ostreams = streams; |
| 36 | initmsg.sinit_max_instreams = streams; |
| 37 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg, |
| 38 | sizeof(struct sctp_initmsg)) == 0); |
| 39 | } |
| 40 | |
| 41 | { |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 42 | // Turn off the NAGLE algorithm. |
| 43 | int on = 1; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 44 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 45 | 0); |
| 46 | } |
| 47 | |
Austin Schuh | f6ed452 | 2020-12-13 16:40:38 -0800 | [diff] [blame] | 48 | { |
| 49 | int on = 1; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 50 | LOG(INFO) << "setsockopt(" << fd() |
| 51 | << ", SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)"; |
| 52 | PCHECK(setsockopt(fd(), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) == 0); |
Austin Schuh | f6ed452 | 2020-12-13 16:40:38 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 55 | // And go! |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 56 | if (bind(fd(), (struct sockaddr *)&sockaddr_local_, |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 57 | sockaddr_local_.ss_family == AF_INET6 |
| 58 | ? sizeof(struct sockaddr_in6) |
| 59 | : sizeof(struct sockaddr_in)) != 0) { |
| 60 | PLOG(ERROR) << "Failed to bind, retrying"; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 61 | close(fd()); |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 62 | std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 63 | continue; |
| 64 | } |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 65 | LOG(INFO) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")"; |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 66 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 67 | PCHECK(listen(fd(), 100) == 0); |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 68 | |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 69 | SetMaxReadSize(1000); |
| 70 | SetMaxWriteSize(1000); |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 71 | break; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 72 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame] | 75 | void SctpServer::SetPriorityScheduler([[maybe_unused]] sctp_assoc_t assoc_id) { |
| 76 | // Kernel 4.9 does not have SCTP_SS_PRIO |
| 77 | #ifdef SCTP_SS_PRIO |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 78 | struct sctp_assoc_value scheduler; |
| 79 | memset(&scheduler, 0, sizeof(scheduler)); |
| 80 | scheduler.assoc_id = assoc_id; |
| 81 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 82 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 83 | sizeof(scheduler)) != 0) { |
James Kuszmaul | 18e7118 | 2023-09-04 15:32:49 -0700 | [diff] [blame] | 84 | PLOG(FATAL) << "Failed to set scheduler."; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 85 | } |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame] | 86 | #endif |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame] | 89 | void SctpServer::SetStreamPriority([[maybe_unused]] sctp_assoc_t assoc_id, |
| 90 | [[maybe_unused]] int stream_id, |
| 91 | [[maybe_unused]] uint16_t priority) { |
| 92 | // Kernel 4.9 does not have SCTP_STREAM_SCHEDULER_VALUE |
| 93 | #ifdef SCTP_STREAM_SCHEDULER_VALUE |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 94 | struct sctp_stream_value sctp_priority; |
| 95 | memset(&sctp_priority, 0, sizeof(sctp_priority)); |
| 96 | sctp_priority.assoc_id = assoc_id; |
| 97 | sctp_priority.stream_id = stream_id; |
| 98 | sctp_priority.stream_value = priority; |
| 99 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE, |
| 100 | &sctp_priority, sizeof(sctp_priority)) != 0) { |
James Kuszmaul | 18e7118 | 2023-09-04 15:32:49 -0700 | [diff] [blame] | 101 | // Treat "Protocol not available" as equivalent to the |
| 102 | // SCTP_STREAM_SCHEDULER_VALUE not being defined--silently ignore it. |
| 103 | if (errno == ENOPROTOOPT) { |
| 104 | VLOG(1) << "Stream scheduler not supported on this kernel."; |
| 105 | return; |
| 106 | } |
| 107 | PLOG(FATAL) << "Failed to set scheduler."; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 108 | } |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame] | 109 | #endif |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 112 | } // namespace aos::message_bridge |