folly: fix sysMembarrier() with newer kernel headers
[folly.git] / folly / portability / Sockets.h
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 #pragma once
18
19 #ifndef _WIN32
20 #include <netdb.h>
21 #include <poll.h>
22
23 #include <arpa/inet.h>
24 #include <netinet/in.h>
25 #include <netinet/tcp.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28
29 #ifdef MSG_ERRQUEUE
30 #define FOLLY_HAVE_MSG_ERRQUEUE 1
31 /* for struct sock_extended_err*/
32 #include <linux/errqueue.h>
33 #endif
34
35 #ifndef SO_EE_ORIGIN_ZEROCOPY
36 #define SO_EE_ORIGIN_ZEROCOPY 5
37 #endif
38
39 #ifndef SO_EE_CODE_ZEROCOPY_COPIED
40 #define SO_EE_CODE_ZEROCOPY_COPIED 1
41 #endif
42
43 #ifndef SO_ZEROCOPY
44 #define SO_ZEROCOPY 60
45 #endif
46
47 #ifndef MSG_ZEROCOPY
48 #define MSG_ZEROCOPY 0x4000000
49 #endif
50
51 #else
52 #include <folly/portability/IOVec.h>
53 #include <folly/portability/SysTypes.h>
54 #include <folly/portability/Windows.h>
55
56 #include <WS2tcpip.h> // @manual
57
58 using nfds_t = int;
59 using sa_family_t = ADDRESS_FAMILY;
60
61 // these are not supported
62 #define SO_EE_ORIGIN_ZEROCOPY 0
63 #define SO_ZEROCOPY 0
64 #define MSG_ZEROCOPY 0x0
65
66 // We don't actually support either of these flags
67 // currently.
68 #define MSG_DONTWAIT 0x1000
69 #define MSG_EOR 0
70 struct msghdr {
71   void* msg_name;
72   socklen_t msg_namelen;
73   struct iovec* msg_iov;
74   size_t msg_iovlen;
75   void* msg_control;
76   size_t msg_controllen;
77   int msg_flags;
78 };
79
80 struct sockaddr_un {
81   sa_family_t sun_family;
82   char sun_path[108];
83 };
84
85 #define SHUT_RD SD_RECEIVE
86 #define SHUT_WR SD_SEND
87 #define SHUT_RDWR SD_BOTH
88
89 // These are the same, but PF_LOCAL
90 // isn't defined by WinSock.
91 #define AF_LOCAL PF_UNIX
92 #define PF_LOCAL PF_UNIX
93
94 // This isn't defined by Windows, and we need to
95 // distinguish it from SO_REUSEADDR
96 #define SO_REUSEPORT 0x7001
97
98 // Someone thought it would be a good idea
99 // to define a field via a macro...
100 #undef s_host
101 #endif
102
103 namespace folly {
104 namespace portability {
105 namespace sockets {
106 #ifndef _WIN32
107 using ::accept;
108 using ::bind;
109 using ::connect;
110 using ::getpeername;
111 using ::getsockname;
112 using ::getsockopt;
113 using ::inet_ntop;
114 using ::listen;
115 using ::poll;
116 using ::recv;
117 using ::recvfrom;
118 using ::send;
119 using ::sendto;
120 using ::sendmsg;
121 using ::setsockopt;
122 using ::shutdown;
123 using ::socket;
124 #else
125 // Some Windows specific helper functions.
126 bool is_fh_socket(int fh);
127 SOCKET fd_to_socket(int fd);
128 int socket_to_fd(SOCKET s);
129 int translate_wsa_error(int wsaErr);
130
131 // These aren't additional overloads, but rather other functions that
132 // are referenced that we need to wrap, or, in the case of inet_aton,
133 // implement.
134 int accept(int s, struct sockaddr* addr, socklen_t* addrlen);
135 int inet_aton(const char* cp, struct in_addr* inp);
136 int socketpair(int domain, int type, int protocol, int sv[2]);
137
138 // Unless you have a case where you would normally have
139 // to reference the function as being explicitly in the
140 // global scope, then you shouldn't be calling these directly.
141 int bind(int s, const struct sockaddr* name, socklen_t namelen);
142 int connect(int s, const struct sockaddr* name, socklen_t namelen);
143 int getpeername(int s, struct sockaddr* name, socklen_t* namelen);
144 int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
145 int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
146 const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
147 int listen(int s, int backlog);
148 int poll(struct pollfd fds[], nfds_t nfds, int timeout);
149 ssize_t recv(int s, void* buf, size_t len, int flags);
150 ssize_t recvfrom(
151     int s,
152     void* buf,
153     size_t len,
154     int flags,
155     struct sockaddr* from,
156     socklen_t* fromlen);
157 ssize_t send(int s, const void* buf, size_t len, int flags);
158 ssize_t sendto(
159     int s,
160     const void* buf,
161     size_t len,
162     int flags,
163     const sockaddr* to,
164     socklen_t tolen);
165 ssize_t sendmsg(int socket, const struct msghdr* message, int flags);
166 int setsockopt(
167     int s,
168     int level,
169     int optname,
170     const void* optval,
171     socklen_t optlen);
172 int shutdown(int s, int how);
173
174 // This is the only function that _must_ be referenced via the namespace
175 // because there is no difference in parameter types to overload
176 // on.
177 int socket(int af, int type, int protocol);
178
179 // Windows needs a few extra overloads of some of the functions in order to
180 // resolve to our portability functions rather than the SOCKET accepting
181 // ones.
182 int getsockopt(int s, int level, int optname, char* optval, socklen_t* optlen);
183 ssize_t recv(int s, char* buf, int len, int flags);
184 ssize_t recv(int s, void* buf, int len, int flags);
185 ssize_t recvfrom(
186     int s,
187     char* buf,
188     int len,
189     int flags,
190     struct sockaddr* from,
191     socklen_t* fromlen);
192 ssize_t recvfrom(
193     int s,
194     void* buf,
195     int len,
196     int flags,
197     struct sockaddr* from,
198     socklen_t* fromlen);
199 ssize_t recvmsg(int s, struct msghdr* message, int fl);
200 ssize_t send(int s, const char* buf, int len, int flags);
201 ssize_t send(int s, const void* buf, int len, int flags);
202 ssize_t sendto(
203     int s,
204     const char* buf,
205     int len,
206     int flags,
207     const sockaddr* to,
208     socklen_t tolen);
209 ssize_t sendto(
210     int s,
211     const void* buf,
212     int len,
213     int flags,
214     const sockaddr* to,
215     socklen_t tolen);
216 int setsockopt(
217     int s,
218     int level,
219     int optname,
220     const char* optval,
221     socklen_t optlen);
222 #endif
223 } // namespace sockets
224 } // namespace portability
225 } // namespace folly
226
227 #ifdef _WIN32
228 // Add our helpers to the overload set.
229 /* using override */
230 using namespace folly::portability::sockets;
231 #endif