blob: 7f7a0ae6c1b1acf570d78e03ff4d56da9c37fc44 [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#include "aos/network/sctp_server.h"
2
3#include <arpa/inet.h>
Adam Snaiderbe263512023-05-18 20:40:23 -07004#include <linux/sctp.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08005#include <net/if.h>
6#include <netdb.h>
7#include <netinet/in.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08008#include <sys/socket.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07009
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080013#include <memory>
Austin Schuh387b7de2020-03-15 14:28:07 -070014#include <thread>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080015
Philipp Schrader790cb542023-07-05 21:06:52 -070016#include "glog/logging.h"
17
Austin Schuhe84c3ed2019-12-14 15:29:48 -080018#include "aos/network/sctp_lib.h"
19#include "aos/unique_malloc_ptr.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080020
Stephan Pleinesf63bde82024-01-13 15:59:33 -080021namespace aos::message_bridge {
Austin Schuhe84c3ed2019-12-14 15:29:48 -080022
Adam Snaider96a0f4b2023-05-18 20:41:19 -070023SctpServer::SctpServer(int streams, std::string_view local_host, int local_port,
Adam Snaider9bb33442023-06-26 16:31:37 -070024 SctpAuthMethod requested_authentication)
25 : sctp_(requested_authentication) {
Austin Schuh0a0a8272021-12-08 13:19:32 -080026 bool use_ipv6 = Ipv6Enabled();
27 sockaddr_local_ = ResolveSocket(local_host, local_port, use_ipv6);
Austin Schuh387b7de2020-03-15 14:28:07 -070028 while (true) {
Austin Schuh507f7582021-07-31 20:39:55 -070029 sctp_.OpenSocket(sockaddr_local_);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080030
Austin Schuh387b7de2020-03-15 14:28:07 -070031 {
Brian Silverman833dfb62021-11-03 10:47:55 -070032 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 Schuh387b7de2020-03-15 14:28:07 -070041 // Turn off the NAGLE algorithm.
42 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070043 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) ==
Austin Schuh387b7de2020-03-15 14:28:07 -070044 0);
45 }
46
Austin Schuhf6ed4522020-12-13 16:40:38 -080047 {
48 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070049 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 Schuhf6ed4522020-12-13 16:40:38 -080052 }
53
Austin Schuh387b7de2020-03-15 14:28:07 -070054 // And go!
Austin Schuh507f7582021-07-31 20:39:55 -070055 if (bind(fd(), (struct sockaddr *)&sockaddr_local_,
Austin Schuh387b7de2020-03-15 14:28:07 -070056 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 Schuh507f7582021-07-31 20:39:55 -070060 close(fd());
Austin Schuh387b7de2020-03-15 14:28:07 -070061 std::this_thread::sleep_for(std::chrono::seconds(5));
62 continue;
63 }
Austin Schuh507f7582021-07-31 20:39:55 -070064 LOG(INFO) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")";
Austin Schuh387b7de2020-03-15 14:28:07 -070065
Austin Schuh507f7582021-07-31 20:39:55 -070066 PCHECK(listen(fd(), 100) == 0);
Austin Schuh387b7de2020-03-15 14:28:07 -070067
Austin Schuh89e1e9c2023-05-15 14:38:44 -070068 SetMaxReadSize(1000);
69 SetMaxWriteSize(1000);
Austin Schuh387b7de2020-03-15 14:28:07 -070070 break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080071 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080072}
73
Sarah Newman1e1b0492023-01-12 14:57:31 -080074void 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 Schuhe84c3ed2019-12-14 15:29:48 -080077 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 Kuszmaul18e71182023-09-04 15:32:49 -070083 PLOG(FATAL) << "Failed to set scheduler.";
Austin Schuhe84c3ed2019-12-14 15:29:48 -080084 }
Sarah Newman1e1b0492023-01-12 14:57:31 -080085#endif
Austin Schuhe84c3ed2019-12-14 15:29:48 -080086}
87
Sarah Newman1e1b0492023-01-12 14:57:31 -080088void 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 Schuhe84c3ed2019-12-14 15:29:48 -080093 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 Kuszmaul18e71182023-09-04 15:32:49 -0700100 // 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 Schuhe84c3ed2019-12-14 15:29:48 -0800107 }
Sarah Newman1e1b0492023-01-12 14:57:31 -0800108#endif
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800109}
110
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800111} // namespace aos::message_bridge