2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <arpa/inet.h>
24 #include <netinet/in.h>
25 #include <netinet/tcp.h>
26 #include <sys/socket.h>
30 /* for struct sock_extended_err*/
31 #include <linux/errqueue.h>
34 #ifndef SO_EE_ORIGIN_ZEROCOPY
35 #define SO_EE_ORIGIN_ZEROCOPY 5
39 #define SO_ZEROCOPY 60
43 #define MSG_ZEROCOPY 0x4000000
47 #include <folly/portability/IOVec.h>
48 #include <folly/portability/SysTypes.h>
49 #include <folly/portability/Windows.h>
51 #include <WS2tcpip.h> // @manual
54 using sa_family_t = ADDRESS_FAMILY;
56 // these are not supported
57 #define SO_EE_ORIGIN_ZEROCOPY 0
59 #define MSG_ZEROCOPY 0x0
61 // We don't actually support either of these flags
63 #define MSG_DONTWAIT 0x1000
67 socklen_t msg_namelen;
68 struct iovec* msg_iov;
71 size_t msg_controllen;
76 sa_family_t sun_family;
80 #define SHUT_RD SD_RECEIVE
81 #define SHUT_WR SD_SEND
82 #define SHUT_RDWR SD_BOTH
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
89 // This isn't defined by Windows, and we need to
90 // distinguish it from SO_REUSEADDR
91 #define SO_REUSEPORT 0x7001
93 // Someone thought it would be a good idea
94 // to define a field via a macro...
99 namespace portability {
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);
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,
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]);
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);
150 struct sockaddr* from,
152 ssize_t send(int s, const void* buf, size_t len, int flags);
160 ssize_t sendmsg(int socket, const struct msghdr* message, int flags);
167 int shutdown(int s, int how);
169 // This is the only function that _must_ be referenced via the namespace
170 // because there is no difference in parameter types to overload
172 int socket(int af, int type, int protocol);
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
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);
185 struct sockaddr* from,
192 struct sockaddr* from,
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);
223 // Add our helpers to the overload set.
225 using namespace folly::portability::sockets;