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