1/*
2 * Copyright 2016-present 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#include <folly/net/NetOps.h>
20
21namespace folly {
22namespace portability {
23namespace sockets {
24#ifndef _WIN32
25using ::accept;
26using ::bind;
27using ::connect;
28using ::getpeername;
29using ::getsockname;
30using ::getsockopt;
31using ::inet_ntop;
32using ::listen;
33using ::poll;
34using ::recv;
35using ::recvfrom;
36using ::send;
37using ::sendmsg;
38using ::sendto;
39using ::setsockopt;
40using ::shutdown;
41using ::socket;
42#else
43// Some Windows specific helper functions.
44bool is_fh_socket(int fh);
45SOCKET fd_to_socket(int fd);
46int socket_to_fd(SOCKET s);
47int translate_wsa_error(int wsaErr);
48
49// These aren't additional overloads, but rather other functions that
50// are referenced that we need to wrap, or, in the case of inet_aton,
51// implement.
52int accept(int s, struct sockaddr* addr, socklen_t* addrlen);
53int inet_aton(const char* cp, struct in_addr* inp);
54int socketpair(int domain, int type, int protocol, int sv[2]);
55
56// Unless you have a case where you would normally have
57// to reference the function as being explicitly in the
58// global scope, then you shouldn't be calling these directly.
59int bind(int s, const struct sockaddr* name, socklen_t namelen);
60int connect(int s, const struct sockaddr* name, socklen_t namelen);
61int getpeername(int s, struct sockaddr* name, socklen_t* namelen);
62int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
63int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
64const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
65int listen(int s, int backlog);
66int poll(struct pollfd fds[], nfds_t nfds, int timeout);
67ssize_t recv(int s, void* buf, size_t len, int flags);
68ssize_t recvfrom(
69 int s,
70 void* buf,
71 size_t len,
72 int flags,
73 struct sockaddr* from,
74 socklen_t* fromlen);
75ssize_t send(int s, const void* buf, size_t len, int flags);
76ssize_t sendto(
77 int s,
78 const void* buf,
79 size_t len,
80 int flags,
81 const sockaddr* to,
82 socklen_t tolen);
83ssize_t sendmsg(int socket, const struct msghdr* message, int flags);
84int setsockopt(
85 int s,
86 int level,
87 int optname,
88 const void* optval,
89 socklen_t optlen);
90int shutdown(int s, int how);
91
92// This is the only function that _must_ be referenced via the namespace
93// because there is no difference in parameter types to overload
94// on.
95int socket(int af, int type, int protocol);
96
97// Windows needs a few extra overloads of some of the functions in order to
98// resolve to our portability functions rather than the SOCKET accepting
99// ones.
100int getsockopt(int s, int level, int optname, char* optval, socklen_t* optlen);
101ssize_t recv(int s, char* buf, int len, int flags);
102ssize_t recv(int s, void* buf, int len, int flags);
103ssize_t recvfrom(
104 int s,
105 char* buf,
106 int len,
107 int flags,
108 struct sockaddr* from,
109 socklen_t* fromlen);
110ssize_t recvfrom(
111 int s,
112 void* buf,
113 int len,
114 int flags,
115 struct sockaddr* from,
116 socklen_t* fromlen);
117ssize_t recvmsg(int s, struct msghdr* message, int fl);
118ssize_t send(int s, const char* buf, int len, int flags);
119ssize_t send(int s, const void* buf, int len, int flags);
120ssize_t sendto(
121 int s,
122 const char* buf,
123 int len,
124 int flags,
125 const sockaddr* to,
126 socklen_t tolen);
127ssize_t sendto(
128 int s,
129 const void* buf,
130 int len,
131 int flags,
132 const sockaddr* to,
133 socklen_t tolen);
134int setsockopt(
135 int s,
136 int level,
137 int optname,
138 const char* optval,
139 socklen_t optlen);
140#endif
141} // namespace sockets
142} // namespace portability
143} // namespace folly
144
145#ifdef _WIN32
146// Add our helpers to the overload set.
147/* using override */
148using namespace folly::portability::sockets;
149#endif
150