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