Sockets.h 6.0 KB

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