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