Fix uses of std::nextafter on Android
[folly.git] / folly / portability / Sockets.h
1 /*
2  * Copyright 2016 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 #ifdef _WIN32
20 #define FOLLY_HAVE_UNIX_SOCKETS 0
21 #else
22 #define FOLLY_HAVE_UNIX_SOCKETS 1
23 #endif
24
25 #ifndef _WIN32
26 #include <netdb.h>
27 #include <poll.h>
28
29 #include <arpa/inet.h>
30 #include <netinet/in.h>
31 #include <netinet/tcp.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #else
35 #include <folly/portability/IOVec.h>
36 #include <folly/portability/SysTypes.h>
37 #include <folly/portability/Windows.h>
38
39 #include <WS2tcpip.h>
40
41 // We don't actually support either of these flags
42 // currently.
43 #define MSG_DONTWAIT 1
44 #define MSG_EOR 0
45 struct msghdr {
46   void* msg_name;
47   socklen_t msg_namelen;
48   struct iovec* msg_iov;
49   size_t msg_iovlen;
50   void* msg_control;
51   size_t msg_controllen;
52   int msg_flags;
53 };
54
55 #define SHUT_RD SD_RECEIVE
56 #define SHUT_WR SD_SEND
57 #define SHUT_RDWR SD_BOTH
58
59 using nfds_t = int;
60
61 // This is named differently in posix.
62 #define sa_family_t ADDRESS_FAMILY
63 // Someone thought it would be a good idea
64 // to define a field via a macro...
65 #undef s_host
66 #endif
67
68 namespace folly {
69 namespace portability {
70 namespace sockets {
71 #ifndef _WIN32
72 using ::bind;
73 using ::connect;
74 using ::getpeername;
75 using ::getsockname;
76 using ::getsockopt;
77 using ::inet_ntop;
78 using ::listen;
79 using ::poll;
80 using ::recv;
81 using ::recvfrom;
82 using ::send;
83 using ::sendto;
84 using ::sendmsg;
85 using ::setsockopt;
86 using ::shutdown;
87 using ::socket;
88 #else
89 // Some Windows specific helper functions.
90 bool is_fh_socket(int fh);
91 SOCKET fd_to_socket(int fd);
92 int socket_to_fd(SOCKET s);
93
94 // These aren't additional overloads, but rather other functions that
95 // are referenced that we need to wrap, or, in the case of inet_aton,
96 // implement.
97 int accept(int s, struct sockaddr* addr, socklen_t* addrlen);
98 int inet_aton(const char* cp, struct in_addr* inp);
99
100 // Unless you have a case where you would normally have
101 // to reference the function as being explicitly in the
102 // global scope, then you shouldn't be calling these directly.
103 int bind(int s, const struct sockaddr* name, socklen_t namelen);
104 int connect(int s, const struct sockaddr* name, socklen_t namelen);
105 int getpeername(int s, struct sockaddr* name, socklen_t* namelen);
106 int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
107 int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
108 const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
109 int listen(int s, int backlog);
110 int poll(struct pollfd fds[], nfds_t nfds, int timeout);
111 ssize_t recv(int s, void* buf, size_t len, int flags);
112 ssize_t recvfrom(
113     int s,
114     void* buf,
115     size_t len,
116     int flags,
117     struct sockaddr* from,
118     socklen_t* fromlen);
119 ssize_t send(int s, const void* buf, size_t len, int flags);
120 ssize_t sendto(
121     int s,
122     const void* buf,
123     size_t len,
124     int flags,
125     const sockaddr* to,
126     socklen_t tolen);
127 ssize_t sendmsg(int socket, const struct msghdr* message, int flags);
128 int setsockopt(
129     int s,
130     int level,
131     int optname,
132     const void* optval,
133     socklen_t optlen);
134 int shutdown(int s, int how);
135
136 // This is the only function that _must_ be referenced via the namespace
137 // because there is no difference in parameter types to overload
138 // on.
139 int socket(int af, int type, int protocol);
140
141 // Windows needs a few extra overloads of some of the functions in order to
142 // resolve to our portability functions rather than the SOCKET accepting
143 // ones.
144 int getsockopt(int s, int level, int optname, char* optval, socklen_t* optlen);
145 ssize_t recv(int s, char* buf, int len, int flags);
146 ssize_t recv(int s, void* buf, int len, int flags);
147 ssize_t recvfrom(
148     int s,
149     char* buf,
150     int len,
151     int flags,
152     struct sockaddr* from,
153     socklen_t* fromlen);
154 ssize_t recvfrom(
155     int s,
156     void* buf,
157     int len,
158     int flags,
159     struct sockaddr* from,
160     socklen_t* fromlen);
161 ssize_t recvmsg(int s, struct msghdr* message, int fl);
162 ssize_t send(int s, const char* buf, int len, int flags);
163 ssize_t send(int s, const void* buf, int len, int flags);
164 ssize_t sendto(
165     int s,
166     const char* buf,
167     int len,
168     int flags,
169     const sockaddr* to,
170     socklen_t tolen);
171 ssize_t sendto(
172     int s,
173     const void* buf,
174     int len,
175     int flags,
176     const sockaddr* to,
177     socklen_t tolen);
178 int setsockopt(
179     int s,
180     int level,
181     int optname,
182     const char* optval,
183     socklen_t optlen);
184 #endif
185 }
186 }
187 }
188
189 #ifdef _WIN32
190 // Add our helpers to the overload set.
191 /* using override */ using namespace folly::portability::sockets;
192 #endif