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.
17 #include <folly/portability/Sockets.h>
24 #include <event2/util.h>
28 #include <folly/ScopeGuard.h>
31 namespace portability {
34 // We have to startup WSA.
35 static struct FSPInit {
38 WSAStartup(MAKEWORD(2, 2), &dat);
45 bool is_fh_socket(int fh) {
46 SOCKET h = fd_to_socket(fh);
47 constexpr long kDummyEvents = 0xABCDEF12;
49 e.lNetworkEvents = kDummyEvents;
50 WSAEnumNetworkEvents(h, nullptr, &e);
51 return e.lNetworkEvents != kDummyEvents;
54 SOCKET fd_to_socket(int fd) {
56 return INVALID_SOCKET;
58 // We do this in a roundabout way to allow us to compile even if
59 // we're doing a bit of trickery to ensure that things aren't
60 // being implicitly converted to a SOCKET by temporarily
61 // adjusting the windows headers to define SOCKET as a
63 static_assert(sizeof(HANDLE) == sizeof(SOCKET), "Handle size mismatch.");
64 HANDLE tmp = (HANDLE)_get_osfhandle(fd);
65 return *(SOCKET*)&tmp;
68 int socket_to_fd(SOCKET s) {
69 if (s == INVALID_SOCKET) {
72 return _open_osfhandle((intptr_t)s, O_RDWR | O_BINARY);
75 int translate_wsa_error(int wsaErr) {
84 template <class R, class F, class... Args>
85 static R wrapSocketFunction(F f, int s, Args... args) {
86 SOCKET h = fd_to_socket(s);
87 R ret = f(h, args...);
88 errno = translate_wsa_error(WSAGetLastError());
92 int accept(int s, struct sockaddr* addr, socklen_t* addrlen) {
93 return socket_to_fd(wrapSocketFunction<SOCKET>(::accept, s, addr, addrlen));
96 int bind(int s, const struct sockaddr* name, socklen_t namelen) {
97 return wrapSocketFunction<int>(::bind, s, name, namelen);
100 int connect(int s, const struct sockaddr* name, socklen_t namelen) {
101 auto r = wrapSocketFunction<int>(::connect, s, name, namelen);
102 if (r == -1 && WSAGetLastError() == WSAEWOULDBLOCK) {
108 int getpeername(int s, struct sockaddr* name, socklen_t* namelen) {
109 return wrapSocketFunction<int>(::getpeername, s, name, namelen);
112 int getsockname(int s, struct sockaddr* name, socklen_t* namelen) {
113 return wrapSocketFunction<int>(::getsockname, s, name, namelen);
116 int getsockopt(int s, int level, int optname, char* optval, socklen_t* optlen) {
117 return getsockopt(s, level, optname, (void*)optval, optlen);
120 int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen) {
121 auto ret = wrapSocketFunction<int>(
122 ::getsockopt, s, level, optname, (char*)optval, (int*)optlen);
123 if (optname == TCP_NODELAY && *optlen == 1) {
124 // Windows is weird about this value, and documents it as a
125 // BOOL (ie. int) but expects the variable to be bool (1-byte),
126 // so we get to adapt the interface to work that way.
127 *(int*)optval = *(uint8_t*)optval;
128 *optlen = sizeof(int);
133 int inet_aton(const char* cp, struct in_addr* inp) {
134 inp->s_addr = inet_addr(cp);
135 return inp->s_addr == INADDR_NONE ? 0 : 1;
138 const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) {
139 return ::inet_ntop(af, (char*)src, dst, size_t(size));
142 int listen(int s, int backlog) {
143 return wrapSocketFunction<int>(::listen, s, backlog);
146 int poll(struct pollfd fds[], nfds_t nfds, int timeout) {
147 // TODO: Allow both file descriptors and SOCKETs in this.
148 for (int i = 0; i < nfds; i++) {
149 fds[i].fd = fd_to_socket((int)fds[i].fd);
151 return ::WSAPoll(fds, (ULONG)nfds, timeout);
154 ssize_t recv(int s, void* buf, size_t len, int flags) {
155 if ((flags & MSG_DONTWAIT) == MSG_DONTWAIT) {
156 flags &= ~MSG_DONTWAIT;
158 u_long pendingRead = 0;
159 if (ioctlsocket(fd_to_socket(s), FIONREAD, &pendingRead)) {
160 errno = translate_wsa_error(WSAGetLastError());
166 FD_SET(fd_to_socket(s), &readSet);
167 timeval timeout{0, 0};
168 auto ret = select(1, &readSet, nullptr, nullptr, &timeout);
174 return wrapSocketFunction<ssize_t>(::recv, s, (char*)buf, (int)len, flags);
177 ssize_t recv(int s, char* buf, int len, int flags) {
178 return recv(s, (void*)buf, (size_t)len, flags);
181 ssize_t recv(int s, void* buf, int len, int flags) {
182 return recv(s, (void*)buf, (size_t)len, flags);
190 struct sockaddr* from,
191 socklen_t* fromlen) {
192 if ((flags & MSG_TRUNC) == MSG_TRUNC) {
193 SOCKET h = fd_to_socket(s);
196 wBuf.buf = (CHAR*)buf;
197 wBuf.len = (ULONG)len;
199 wMsg.dwBufferCount = 1;
200 wMsg.lpBuffers = &wBuf;
202 if (fromlen != nullptr) {
203 wMsg.namelen = *fromlen;
206 // WSARecvMsg is an extension, so we don't get
207 // the convenience of being able to call it directly, even though
208 // WSASendMsg is part of the normal API -_-...
209 LPFN_WSARECVMSG WSARecvMsg;
210 GUID WSARecgMsg_GUID = WSAID_WSARECVMSG;
214 SIO_GET_EXTENSION_FUNCTION_POINTER,
216 sizeof(WSARecgMsg_GUID),
224 int res = WSARecvMsg(h, &wMsg, &bytesReceived, nullptr, nullptr);
225 errno = translate_wsa_error(WSAGetLastError());
227 return bytesReceived;
229 if (fromlen != nullptr) {
230 *fromlen = wMsg.namelen;
232 if ((wMsg.dwFlags & MSG_TRUNC) == MSG_TRUNC) {
237 return wrapSocketFunction<ssize_t>(
238 ::recvfrom, s, (char*)buf, (int)len, flags, from, (int*)fromlen);
246 struct sockaddr* from,
247 socklen_t* fromlen) {
248 return recvfrom(s, (void*)buf, (size_t)len, flags, from, fromlen);
256 struct sockaddr* from,
257 socklen_t* fromlen) {
258 return recvfrom(s, (void*)buf, (size_t)len, flags, from, fromlen);
261 ssize_t recvmsg(int s, struct msghdr* message, int /* flags */) {
262 SOCKET h = fd_to_socket(s);
264 // Don't currently support the name translation.
265 if (message->msg_name != nullptr || message->msg_namelen != 0) {
271 msg.Control.buf = (CHAR*)message->msg_control;
272 msg.Control.len = (ULONG)message->msg_controllen;
274 msg.dwBufferCount = (DWORD)message->msg_iovlen;
275 msg.lpBuffers = new WSABUF[message->msg_iovlen];
277 delete[] msg.lpBuffers;
279 for (size_t i = 0; i < message->msg_iovlen; i++) {
280 msg.lpBuffers[i].buf = (CHAR*)message->msg_iov[i].iov_base;
281 msg.lpBuffers[i].len = (ULONG)message->msg_iov[i].iov_len;
284 // WSARecvMsg is an extension, so we don't get
285 // the convenience of being able to call it directly, even though
286 // WSASendMsg is part of the normal API -_-...
287 LPFN_WSARECVMSG WSARecvMsg;
288 GUID WSARecgMsg_GUID = WSAID_WSARECVMSG;
292 SIO_GET_EXTENSION_FUNCTION_POINTER,
294 sizeof(WSARecgMsg_GUID),
302 int res = WSARecvMsg(h, &msg, &bytesReceived, nullptr, nullptr);
303 errno = translate_wsa_error(WSAGetLastError());
304 return res == 0 ? (ssize_t)bytesReceived : -1;
307 ssize_t send(int s, const void* buf, size_t len, int flags) {
308 return wrapSocketFunction<ssize_t>(
309 ::send, s, (const char*)buf, (int)len, flags);
312 ssize_t send(int s, const char* buf, int len, int flags) {
313 return send(s, (const void*)buf, (size_t)len, flags);
316 ssize_t send(int s, const void* buf, int len, int flags) {
317 return send(s, (const void*)buf, (size_t)len, flags);
320 ssize_t sendmsg(int s, const struct msghdr* message, int /* flags */) {
321 SOCKET h = fd_to_socket(s);
323 // Unfortunately, WSASendMsg requires the socket to have been opened
324 // as either SOCK_DGRAM or SOCK_RAW, but sendmsg has no such requirement,
325 // so we have to implement it based on send instead :(
326 ssize_t bytesSent = 0;
327 for (size_t i = 0; i < message->msg_iovlen; i++) {
329 if (message->msg_name != nullptr) {
332 (const char*)message->msg_iov[i].iov_base,
333 (int)message->msg_iov[i].iov_len,
335 (const sockaddr*)message->msg_name,
336 (int)message->msg_namelen);
340 (const char*)message->msg_iov[i].iov_base,
341 (int)message->msg_iov[i].iov_len,
344 if (r == -1 || size_t(r) != message->msg_iov[i].iov_len) {
345 errno = translate_wsa_error(WSAGetLastError());
346 if (WSAGetLastError() == WSAEWOULDBLOCK && bytesSent > 0) {
363 return wrapSocketFunction<ssize_t>(
364 ::sendto, s, (const char*)buf, (int)len, flags, to, (int)tolen);
374 return sendto(s, (const void*)buf, (size_t)len, flags, to, tolen);
384 return sendto(s, buf, (size_t)len, flags, to, tolen);
393 if (optname == SO_REUSEADDR) {
394 // We don't have an equivelent to the Linux & OSX meaning of this
395 // on Windows, so ignore it.
397 } else if (optname == SO_REUSEPORT) {
398 // Windows's SO_REUSEADDR option is closer to SO_REUSEPORT than
399 // it is to the Linux & OSX meaning of SO_REUSEADDR.
402 return wrapSocketFunction<int>(
403 ::setsockopt, s, level, optname, (char*)optval, optlen);
412 return setsockopt(s, level, optname, (const void*)optval, optlen);
415 int shutdown(int s, int how) {
416 return wrapSocketFunction<int>(::shutdown, s, how);
419 int socket(int af, int type, int protocol) {
420 return socket_to_fd(::socket(af, type, protocol));
423 int socketpair(int domain, int type, int protocol, int sv[2]) {
424 if (domain != PF_UNIX || type != SOCK_STREAM || protocol != 0) {
428 auto r = evutil_socketpair(AF_INET, type, protocol, pair);
432 sv[0] = _open_osfhandle(pair[0], O_RDWR | O_BINARY);
433 sv[1] = _open_osfhandle(pair[1], O_RDWR | O_BINARY);