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> |
| 4 | #include <net/if.h> |
| 5 | #include <netdb.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <netinet/sctp.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 | |
| 16 | #include "aos/network/sctp_lib.h" |
| 17 | #include "aos/unique_malloc_ptr.h" |
| 18 | #include "glog/logging.h" |
| 19 | |
| 20 | namespace aos { |
| 21 | namespace message_bridge { |
| 22 | |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 23 | SctpServer::SctpServer(int streams, std::string_view local_host, |
| 24 | int local_port) { |
| 25 | bool use_ipv6 = Ipv6Enabled(); |
| 26 | sockaddr_local_ = ResolveSocket(local_host, local_port, use_ipv6); |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 27 | while (true) { |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 28 | sctp_.OpenSocket(sockaddr_local_); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 30 | { |
Brian Silverman | 833dfb6 | 2021-11-03 10:47:55 -0700 | [diff] [blame] | 31 | struct sctp_initmsg initmsg; |
| 32 | memset(&initmsg, 0, sizeof(struct sctp_initmsg)); |
| 33 | initmsg.sinit_num_ostreams = streams; |
| 34 | initmsg.sinit_max_instreams = streams; |
| 35 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg, |
| 36 | sizeof(struct sctp_initmsg)) == 0); |
| 37 | } |
| 38 | |
| 39 | { |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 40 | // Turn off the NAGLE algorithm. |
| 41 | int on = 1; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 42 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) == |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 43 | 0); |
| 44 | } |
| 45 | |
Austin Schuh | f6ed452 | 2020-12-13 16:40:38 -0800 | [diff] [blame] | 46 | { |
| 47 | int on = 1; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 48 | LOG(INFO) << "setsockopt(" << fd() |
| 49 | << ", SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)"; |
| 50 | PCHECK(setsockopt(fd(), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) == 0); |
Austin Schuh | f6ed452 | 2020-12-13 16:40:38 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 53 | // And go! |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 54 | if (bind(fd(), (struct sockaddr *)&sockaddr_local_, |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 55 | sockaddr_local_.ss_family == AF_INET6 |
| 56 | ? sizeof(struct sockaddr_in6) |
| 57 | : sizeof(struct sockaddr_in)) != 0) { |
| 58 | PLOG(ERROR) << "Failed to bind, retrying"; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 59 | close(fd()); |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 60 | std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 61 | continue; |
| 62 | } |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 63 | LOG(INFO) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")"; |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 64 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 65 | PCHECK(listen(fd(), 100) == 0); |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 66 | |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame^] | 67 | SetMaxReadSize(1000); |
| 68 | SetMaxWriteSize(1000); |
Austin Schuh | 387b7de | 2020-03-15 14:28:07 -0700 | [diff] [blame] | 69 | break; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 70 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame] | 73 | void SctpServer::SetPriorityScheduler([[maybe_unused]] sctp_assoc_t assoc_id) { |
| 74 | // Kernel 4.9 does not have SCTP_SS_PRIO |
| 75 | #ifdef SCTP_SS_PRIO |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 76 | struct sctp_assoc_value scheduler; |
| 77 | memset(&scheduler, 0, sizeof(scheduler)); |
| 78 | scheduler.assoc_id = assoc_id; |
| 79 | scheduler.assoc_value = SCTP_SS_PRIO; |
| 80 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler, |
| 81 | sizeof(scheduler)) != 0) { |
Sarah Newman | 29da5ed | 2022-04-28 18:51:18 -0700 | [diff] [blame] | 82 | LOG_FIRST_N(WARNING, 1) << "Failed to set scheduler: " << strerror(errno) |
| 83 | << " [" << errno << "]"; |
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) { |
Sarah Newman | 29da5ed | 2022-04-28 18:51:18 -0700 | [diff] [blame] | 100 | LOG_FIRST_N(WARNING, 1) << "Failed to set scheduler: " << strerror(errno) |
| 101 | << " [" << errno << "]"; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 102 | } |
Sarah Newman | 1e1b049 | 2023-01-12 14:57:31 -0800 | [diff] [blame] | 103 | #endif |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace message_bridge |
| 107 | } // namespace aos |