Add a couple more things to the socket portability layer
[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 #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 1
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 // Someone thought it would be a good idea
66 // to define a field via a macro...
67 #undef s_host
68 #endif
69
70 namespace folly {
71 namespace portability {
72 namespace sockets {
73 #ifndef _WIN32
74 using ::bind;
75 using ::connect;
76 using ::getpeername;
77 using ::getsockname;
78 using ::getsockopt;
79 using ::inet_ntop;
80 using ::listen;
81 using ::poll;
82 using ::recv;
83 using ::recvfrom;
84 using ::send;
85 using ::sendto;
86 using ::sendmsg;
87 using ::setsockopt;
88 using ::shutdown;
89 using ::socket;
90 #else
91 // Some Windows specific helper functions.
92 bool is_fh_socket(int fh);
93 SOCKET fd_to_socket(int fd);
94 int socket_to_fd(SOCKET s);
95
96 // These aren't additional overloads, but rather other functions that
97 // are referenced that we need to wrap, or, in the case of inet_aton,
98 // implement.
99 int accept(int s, struct sockaddr* addr, socklen_t* addrlen);
100 int inet_aton(const char* cp, struct in_addr* inp);
101 int socketpair(int domain, int type, int protocol, int sv[2]);
102
103 // Unless you have a case where you would normally have
104 // to reference the function as being explicitly in the
105 // global scope, then you shouldn't be calling these directly.
106 int bind(int s, const struct sockaddr* name, socklen_t namelen);
107 int connect(int s, const struct sockaddr* name, socklen_t namelen);
108 int getpeername(int s, struct sockaddr* name, socklen_t* namelen);
109 int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
110 int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
111 const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
112 int listen(int s, int backlog);
113 int poll(struct pollfd fds[], nfds_t nfds, int timeout);
114 ssize_t recv(int s, void* buf, size_t len, int flags);
115 ssize_t recvfrom(
116     int s,
117     void* buf,
118     size_t len,
119     int flags,
120     struct sockaddr* from,
121     socklen_t* fromlen);
122 ssize_t send(int s, const void* buf, size_t len, int flags);
123 ssize_t sendto(
124     int s,
125     const void* buf,
126     size_t len,
127     int flags,
128     const sockaddr* to,
129     socklen_t tolen);
130 ssize_t sendmsg(int socket, const struct msghdr* message, int flags);
131 int setsockopt(
132     int s,
133     int level,
134     int optname,
135     const void* optval,
136     socklen_t optlen);
137 int shutdown(int s, int how);
138
139 // This is the only function that _must_ be referenced via the namespace
140 // because there is no difference in parameter types to overload
141 // on.
142 int socket(int af, int type, int protocol);
143
144 // Windows needs a few extra overloads of some of the functions in order to
145 // resolve to our portability functions rather than the SOCKET accepting
146 // ones.
147 int getsockopt(int s, int level, int optname, char* optval, socklen_t* optlen);
148 ssize_t recv(int s, char* buf, int len, int flags);
149 ssize_t recv(int s, void* buf, int len, int flags);
150 ssize_t recvfrom(
151     int s,
152     char* buf,
153     int len,
154     int flags,
155     struct sockaddr* from,
156     socklen_t* fromlen);
157 ssize_t recvfrom(
158     int s,
159     void* buf,
160     int len,
161     int flags,
162     struct sockaddr* from,
163     socklen_t* fromlen);
164 ssize_t recvmsg(int s, struct msghdr* message, int fl);
165 ssize_t send(int s, const char* buf, int len, int flags);
166 ssize_t send(int s, const void* buf, int len, int flags);
167 ssize_t sendto(
168     int s,
169     const char* buf,
170     int len,
171     int flags,
172     const sockaddr* to,
173     socklen_t tolen);
174 ssize_t sendto(
175     int s,
176     const void* buf,
177     int len,
178     int flags,
179     const sockaddr* to,
180     socklen_t tolen);
181 int setsockopt(
182     int s,
183     int level,
184     int optname,
185     const char* optval,
186     socklen_t optlen);
187 #endif
188 }
189 }
190 }
191
192 #ifdef _WIN32
193 // Add our helpers to the overload set.
194 /* using override */ using namespace folly::portability::sockets;
195 #endif