Fix some copyright lines in folly/detail/ and folly/test/
[folly.git] / folly / detail / SocketFastOpen.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/detail/SocketFastOpen.h>
18
19 #include <folly/portability/Sockets.h>
20
21 #include <cerrno>
22 #include <cstdio>
23
24 namespace folly {
25 namespace detail {
26
27 #if FOLLY_ALLOW_TFO && defined(__linux__)
28
29 // Sometimes these flags are not present in the headers,
30 // so define them if not present.
31 #if !defined(MSG_FASTOPEN)
32 #define MSG_FASTOPEN 0x20000000
33 #endif
34
35 #if !defined(TCP_FASTOPEN)
36 #define TCP_FASTOPEN 23
37 #endif
38
39 #if !defined(TCPI_OPT_SYN_DATA)
40 #define TCPI_OPT_SYN_DATA 32
41 #endif
42
43 ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
44   flags |= MSG_FASTOPEN;
45   return sendmsg(sockfd, msg, flags);
46 }
47
48 int tfo_enable(int sockfd, size_t max_queue_size) {
49   return setsockopt(
50       sockfd, SOL_TCP, TCP_FASTOPEN, &max_queue_size, sizeof(max_queue_size));
51 }
52
53 bool tfo_succeeded(int sockfd) {
54   // Call getsockopt to check if TFO was used.
55   struct tcp_info info;
56   socklen_t info_len = sizeof(info);
57   errno = 0;
58   if (getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, &info_len) != 0) {
59     // errno is set from getsockopt
60     return false;
61   }
62   return info.tcpi_options & TCPI_OPT_SYN_DATA;
63 }
64
65 #elif FOLLY_ALLOW_TFO && defined(__APPLE__)
66
67 ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
68   sa_endpoints_t endpoints;
69   endpoints.sae_srcif = 0;
70   endpoints.sae_srcaddr = nullptr;
71   endpoints.sae_srcaddrlen = 0;
72   endpoints.sae_dstaddr = (struct sockaddr*)msg->msg_name;
73   endpoints.sae_dstaddrlen = msg->msg_namelen;
74   int ret = connectx(
75       sockfd,
76       &endpoints,
77       SAE_ASSOCID_ANY,
78       CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
79       nullptr,
80       0,
81       nullptr,
82       nullptr);
83
84   if (ret != 0) {
85     return ret;
86   }
87   ret = sendmsg(sockfd, msg, flags);
88   return ret;
89 }
90
91 int tfo_enable(int sockfd, size_t max_queue_size) {
92   return setsockopt(
93       sockfd,
94       IPPROTO_TCP,
95       TCP_FASTOPEN,
96       &max_queue_size,
97       sizeof(max_queue_size));
98 }
99
100 bool tfo_succeeded(int sockfd) {
101   errno = EOPNOTSUPP;
102   return false;
103 }
104
105 #else
106
107 ssize_t
108 tfo_sendmsg(int /* sockfd */, const struct msghdr* /* msg */, int /* flags */) {
109   errno = EOPNOTSUPP;
110   return -1;
111 }
112
113 int tfo_enable(int /* sockfd */, size_t /* max_queue_size */) {
114   errno = ENOPROTOOPT;
115   return -1;
116 }
117
118 bool tfo_succeeded(int /* sockfd */) {
119   errno = EOPNOTSUPP;
120   return false;
121 }
122
123 #endif
124 } // namespace detail
125 } // namespace folly