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 | |
21 | namespace folly { |
22 | namespace portability { |
23 | namespace sockets { |
24 | #ifndef _WIN32 |
25 | using ::accept; |
26 | using ::bind; |
27 | using ::connect; |
28 | using ::getpeername; |
29 | using ::getsockname; |
30 | using ::getsockopt; |
31 | using ::inet_ntop; |
32 | using ::listen; |
33 | using ::poll; |
34 | using ::recv; |
35 | using ::recvfrom; |
36 | using ::send; |
37 | using ::sendmsg; |
38 | using ::sendto; |
39 | using ::setsockopt; |
40 | using ::shutdown; |
41 | using ::socket; |
42 | #else |
43 | // Some Windows specific helper functions. |
44 | bool is_fh_socket(int fh); |
45 | SOCKET fd_to_socket(int fd); |
46 | int socket_to_fd(SOCKET s); |
47 | int 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. |
52 | int accept(int s, struct sockaddr* addr, socklen_t* addrlen); |
53 | int inet_aton(const char* cp, struct in_addr* inp); |
54 | int 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. |
59 | int bind(int s, const struct sockaddr* name, socklen_t namelen); |
60 | int connect(int s, const struct sockaddr* name, socklen_t namelen); |
61 | int getpeername(int s, struct sockaddr* name, socklen_t* namelen); |
62 | int getsockname(int s, struct sockaddr* name, socklen_t* namelen); |
63 | int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen); |
64 | const char* inet_ntop(int af, const void* src, char* dst, socklen_t size); |
65 | int listen(int s, int backlog); |
66 | int poll(struct pollfd fds[], nfds_t nfds, int timeout); |
67 | ssize_t recv(int s, void* buf, size_t len, int flags); |
68 | ssize_t recvfrom( |
69 | int s, |
70 | void* buf, |
71 | size_t len, |
72 | int flags, |
73 | struct sockaddr* from, |
74 | socklen_t* fromlen); |
75 | ssize_t send(int s, const void* buf, size_t len, int flags); |
76 | ssize_t sendto( |
77 | int s, |
78 | const void* buf, |
79 | size_t len, |
80 | int flags, |
81 | const sockaddr* to, |
82 | socklen_t tolen); |
83 | ssize_t sendmsg(int socket, const struct msghdr* message, int flags); |
84 | int setsockopt( |
85 | int s, |
86 | int level, |
87 | int optname, |
88 | const void* optval, |
89 | socklen_t optlen); |
90 | int 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. |
95 | int 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. |
100 | int getsockopt(int s, int level, int optname, char* optval, socklen_t* optlen); |
101 | ssize_t recv(int s, char* buf, int len, int flags); |
102 | ssize_t recv(int s, void* buf, int len, int flags); |
103 | ssize_t recvfrom( |
104 | int s, |
105 | char* buf, |
106 | int len, |
107 | int flags, |
108 | struct sockaddr* from, |
109 | socklen_t* fromlen); |
110 | ssize_t recvfrom( |
111 | int s, |
112 | void* buf, |
113 | int len, |
114 | int flags, |
115 | struct sockaddr* from, |
116 | socklen_t* fromlen); |
117 | ssize_t recvmsg(int s, struct msghdr* message, int fl); |
118 | ssize_t send(int s, const char* buf, int len, int flags); |
119 | ssize_t send(int s, const void* buf, int len, int flags); |
120 | ssize_t sendto( |
121 | int s, |
122 | const char* buf, |
123 | int len, |
124 | int flags, |
125 | const sockaddr* to, |
126 | socklen_t tolen); |
127 | ssize_t sendto( |
128 | int s, |
129 | const void* buf, |
130 | int len, |
131 | int flags, |
132 | const sockaddr* to, |
133 | socklen_t tolen); |
134 | int 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 */ |
148 | using namespace folly::portability::sockets; |
149 | #endif |
150 | |