Folly parseJson doesn't handle minInt properly
[folly.git] / folly / detail / SocketFastOpen.cpp
1 /*
2  * Copyright 2016 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 <cerrno>
20
21 namespace folly {
22 namespace detail {
23
24 #if FOLLY_ALLOW_TFO
25
26 #include <netinet/tcp.h>
27 #include <stdio.h>
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 ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
40   flags |= MSG_FASTOPEN;
41   return sendmsg(sockfd, msg, flags);
42 }
43
44 int tfo_enable(int sockfd, size_t max_queue_size) {
45   return setsockopt(
46       sockfd, SOL_TCP, TCP_FASTOPEN, &max_queue_size, sizeof(max_queue_size));
47 }
48
49 #else
50
51 ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
52   errno = EOPNOTSUPP;
53   return -1;
54 }
55
56 int tfo_enable(int sockfd, size_t max_queue_size) {
57   errno = ENOPROTOOPT;
58   return -1;
59 }
60
61 #endif
62 }
63 }