socket_ops.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //
  2. // detail/socket_ops.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_SOCKET_OPS_HPP
  11. #define ASIO_DETAIL_SOCKET_OPS_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/error_code.hpp"
  17. #include "asio/detail/memory.hpp"
  18. #include "asio/detail/socket_types.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. namespace detail {
  22. namespace socket_ops {
  23. // Socket state bits.
  24. enum
  25. {
  26. // The user wants a non-blocking socket.
  27. user_set_non_blocking = 1,
  28. // The socket has been set non-blocking.
  29. internal_non_blocking = 2,
  30. // Helper "state" used to determine whether the socket is non-blocking.
  31. non_blocking = user_set_non_blocking | internal_non_blocking,
  32. // User wants connection_aborted errors, which are disabled by default.
  33. enable_connection_aborted = 4,
  34. // The user set the linger option. Needs to be checked when closing.
  35. user_set_linger = 8,
  36. // The socket is stream-oriented.
  37. stream_oriented = 16,
  38. // The socket is datagram-oriented.
  39. datagram_oriented = 32,
  40. // The socket may have been dup()-ed.
  41. possible_dup = 64
  42. };
  43. typedef unsigned char state_type;
  44. struct noop_deleter { void operator()(void*) {} };
  45. typedef shared_ptr<void> shared_cancel_token_type;
  46. typedef weak_ptr<void> weak_cancel_token_type;
  47. #if !defined(ASIO_WINDOWS_RUNTIME)
  48. ASIO_DECL socket_type accept(socket_type s, socket_addr_type* addr,
  49. std::size_t* addrlen, asio::error_code& ec);
  50. ASIO_DECL socket_type sync_accept(socket_type s,
  51. state_type state, socket_addr_type* addr,
  52. std::size_t* addrlen, asio::error_code& ec);
  53. #if defined(ASIO_HAS_IOCP)
  54. ASIO_DECL void complete_iocp_accept(socket_type s,
  55. void* output_buffer, DWORD address_length,
  56. socket_addr_type* addr, std::size_t* addrlen,
  57. socket_type new_socket, asio::error_code& ec);
  58. #else // defined(ASIO_HAS_IOCP)
  59. ASIO_DECL bool non_blocking_accept(socket_type s,
  60. state_type state, socket_addr_type* addr, std::size_t* addrlen,
  61. asio::error_code& ec, socket_type& new_socket);
  62. #endif // defined(ASIO_HAS_IOCP)
  63. ASIO_DECL int bind(socket_type s, const socket_addr_type* addr,
  64. std::size_t addrlen, asio::error_code& ec);
  65. ASIO_DECL int close(socket_type s, state_type& state,
  66. bool destruction, asio::error_code& ec);
  67. ASIO_DECL bool set_user_non_blocking(socket_type s,
  68. state_type& state, bool value, asio::error_code& ec);
  69. ASIO_DECL bool set_internal_non_blocking(socket_type s,
  70. state_type& state, bool value, asio::error_code& ec);
  71. ASIO_DECL int shutdown(socket_type s,
  72. int what, asio::error_code& ec);
  73. ASIO_DECL int connect(socket_type s, const socket_addr_type* addr,
  74. std::size_t addrlen, asio::error_code& ec);
  75. ASIO_DECL void sync_connect(socket_type s, const socket_addr_type* addr,
  76. std::size_t addrlen, asio::error_code& ec);
  77. #if defined(ASIO_HAS_IOCP)
  78. ASIO_DECL void complete_iocp_connect(socket_type s,
  79. asio::error_code& ec);
  80. #endif // defined(ASIO_HAS_IOCP)
  81. ASIO_DECL bool non_blocking_connect(socket_type s,
  82. asio::error_code& ec);
  83. ASIO_DECL int socketpair(int af, int type, int protocol,
  84. socket_type sv[2], asio::error_code& ec);
  85. ASIO_DECL bool sockatmark(socket_type s, asio::error_code& ec);
  86. ASIO_DECL size_t available(socket_type s, asio::error_code& ec);
  87. ASIO_DECL int listen(socket_type s,
  88. int backlog, asio::error_code& ec);
  89. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  90. typedef WSABUF buf;
  91. #else // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  92. typedef iovec buf;
  93. #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  94. ASIO_DECL void init_buf(buf& b, void* data, size_t size);
  95. ASIO_DECL void init_buf(buf& b, const void* data, size_t size);
  96. ASIO_DECL signed_size_type recv(socket_type s, buf* bufs,
  97. size_t count, int flags, asio::error_code& ec);
  98. ASIO_DECL signed_size_type recv1(socket_type s,
  99. void* data, size_t size, int flags, asio::error_code& ec);
  100. ASIO_DECL size_t sync_recv(socket_type s, state_type state, buf* bufs,
  101. size_t count, int flags, bool all_empty, asio::error_code& ec);
  102. ASIO_DECL size_t sync_recv1(socket_type s, state_type state,
  103. void* data, size_t size, int flags, asio::error_code& ec);
  104. #if defined(ASIO_HAS_IOCP)
  105. ASIO_DECL void complete_iocp_recv(state_type state,
  106. const weak_cancel_token_type& cancel_token, bool all_empty,
  107. asio::error_code& ec, size_t bytes_transferred);
  108. #else // defined(ASIO_HAS_IOCP)
  109. ASIO_DECL bool non_blocking_recv(socket_type s,
  110. buf* bufs, size_t count, int flags, bool is_stream,
  111. asio::error_code& ec, size_t& bytes_transferred);
  112. ASIO_DECL bool non_blocking_recv1(socket_type s,
  113. void* data, size_t size, int flags, bool is_stream,
  114. asio::error_code& ec, size_t& bytes_transferred);
  115. #endif // defined(ASIO_HAS_IOCP)
  116. ASIO_DECL signed_size_type recvfrom(socket_type s, buf* bufs,
  117. size_t count, int flags, socket_addr_type* addr,
  118. std::size_t* addrlen, asio::error_code& ec);
  119. ASIO_DECL signed_size_type recvfrom1(socket_type s, void* data,
  120. size_t size, int flags, socket_addr_type* addr,
  121. std::size_t* addrlen, asio::error_code& ec);
  122. ASIO_DECL size_t sync_recvfrom(socket_type s, state_type state,
  123. buf* bufs, size_t count, int flags, socket_addr_type* addr,
  124. std::size_t* addrlen, asio::error_code& ec);
  125. ASIO_DECL size_t sync_recvfrom1(socket_type s, state_type state,
  126. void* data, size_t size, int flags, socket_addr_type* addr,
  127. std::size_t* addrlen, asio::error_code& ec);
  128. #if defined(ASIO_HAS_IOCP)
  129. ASIO_DECL void complete_iocp_recvfrom(
  130. const weak_cancel_token_type& cancel_token,
  131. asio::error_code& ec);
  132. #else // defined(ASIO_HAS_IOCP)
  133. ASIO_DECL bool non_blocking_recvfrom(socket_type s,
  134. buf* bufs, size_t count, int flags,
  135. socket_addr_type* addr, std::size_t* addrlen,
  136. asio::error_code& ec, size_t& bytes_transferred);
  137. ASIO_DECL bool non_blocking_recvfrom1(socket_type s,
  138. void* data, size_t size, int flags,
  139. socket_addr_type* addr, std::size_t* addrlen,
  140. asio::error_code& ec, size_t& bytes_transferred);
  141. #endif // defined(ASIO_HAS_IOCP)
  142. ASIO_DECL signed_size_type recvmsg(socket_type s, buf* bufs,
  143. size_t count, int in_flags, int& out_flags,
  144. asio::error_code& ec);
  145. ASIO_DECL size_t sync_recvmsg(socket_type s, state_type state,
  146. buf* bufs, size_t count, int in_flags, int& out_flags,
  147. asio::error_code& ec);
  148. #if defined(ASIO_HAS_IOCP)
  149. ASIO_DECL void complete_iocp_recvmsg(
  150. const weak_cancel_token_type& cancel_token,
  151. asio::error_code& ec);
  152. #else // defined(ASIO_HAS_IOCP)
  153. ASIO_DECL bool non_blocking_recvmsg(socket_type s,
  154. buf* bufs, size_t count, int in_flags, int& out_flags,
  155. asio::error_code& ec, size_t& bytes_transferred);
  156. #endif // defined(ASIO_HAS_IOCP)
  157. ASIO_DECL signed_size_type send(socket_type s, const buf* bufs,
  158. size_t count, int flags, asio::error_code& ec);
  159. ASIO_DECL signed_size_type send1(socket_type s,
  160. const void* data, size_t size, int flags, asio::error_code& ec);
  161. ASIO_DECL size_t sync_send(socket_type s, state_type state,
  162. const buf* bufs, size_t count, int flags,
  163. bool all_empty, asio::error_code& ec);
  164. ASIO_DECL size_t sync_send1(socket_type s, state_type state,
  165. const void* data, size_t size, int flags, asio::error_code& ec);
  166. #if defined(ASIO_HAS_IOCP)
  167. ASIO_DECL void complete_iocp_send(
  168. const weak_cancel_token_type& cancel_token,
  169. asio::error_code& ec);
  170. #else // defined(ASIO_HAS_IOCP)
  171. ASIO_DECL bool non_blocking_send(socket_type s,
  172. const buf* bufs, size_t count, int flags,
  173. asio::error_code& ec, size_t& bytes_transferred);
  174. ASIO_DECL bool non_blocking_send1(socket_type s,
  175. const void* data, size_t size, int flags,
  176. asio::error_code& ec, size_t& bytes_transferred);
  177. #endif // defined(ASIO_HAS_IOCP)
  178. ASIO_DECL signed_size_type sendto(socket_type s, const buf* bufs,
  179. size_t count, int flags, const socket_addr_type* addr,
  180. std::size_t addrlen, asio::error_code& ec);
  181. ASIO_DECL signed_size_type sendto1(socket_type s, const void* data,
  182. size_t size, int flags, const socket_addr_type* addr,
  183. std::size_t addrlen, asio::error_code& ec);
  184. ASIO_DECL size_t sync_sendto(socket_type s, state_type state,
  185. const buf* bufs, size_t count, int flags, const socket_addr_type* addr,
  186. std::size_t addrlen, asio::error_code& ec);
  187. ASIO_DECL size_t sync_sendto1(socket_type s, state_type state,
  188. const void* data, size_t size, int flags, const socket_addr_type* addr,
  189. std::size_t addrlen, asio::error_code& ec);
  190. #if !defined(ASIO_HAS_IOCP)
  191. ASIO_DECL bool non_blocking_sendto(socket_type s,
  192. const buf* bufs, size_t count, int flags,
  193. const socket_addr_type* addr, std::size_t addrlen,
  194. asio::error_code& ec, size_t& bytes_transferred);
  195. ASIO_DECL bool non_blocking_sendto1(socket_type s,
  196. const void* data, size_t size, int flags,
  197. const socket_addr_type* addr, std::size_t addrlen,
  198. asio::error_code& ec, size_t& bytes_transferred);
  199. #endif // !defined(ASIO_HAS_IOCP)
  200. ASIO_DECL socket_type socket(int af, int type, int protocol,
  201. asio::error_code& ec);
  202. ASIO_DECL int setsockopt(socket_type s, state_type& state,
  203. int level, int optname, const void* optval,
  204. std::size_t optlen, asio::error_code& ec);
  205. ASIO_DECL int getsockopt(socket_type s, state_type state,
  206. int level, int optname, void* optval,
  207. size_t* optlen, asio::error_code& ec);
  208. ASIO_DECL int getpeername(socket_type s, socket_addr_type* addr,
  209. std::size_t* addrlen, bool cached, asio::error_code& ec);
  210. ASIO_DECL int getsockname(socket_type s, socket_addr_type* addr,
  211. std::size_t* addrlen, asio::error_code& ec);
  212. ASIO_DECL int ioctl(socket_type s, state_type& state,
  213. int cmd, ioctl_arg_type* arg, asio::error_code& ec);
  214. ASIO_DECL int select(int nfds, fd_set* readfds, fd_set* writefds,
  215. fd_set* exceptfds, timeval* timeout, asio::error_code& ec);
  216. ASIO_DECL int poll_read(socket_type s,
  217. state_type state, int msec, asio::error_code& ec);
  218. ASIO_DECL int poll_write(socket_type s,
  219. state_type state, int msec, asio::error_code& ec);
  220. ASIO_DECL int poll_error(socket_type s,
  221. state_type state, int msec, asio::error_code& ec);
  222. ASIO_DECL int poll_connect(socket_type s,
  223. int msec, asio::error_code& ec);
  224. #endif // !defined(ASIO_WINDOWS_RUNTIME)
  225. ASIO_DECL const char* inet_ntop(int af, const void* src, char* dest,
  226. size_t length, unsigned long scope_id, asio::error_code& ec);
  227. ASIO_DECL int inet_pton(int af, const char* src, void* dest,
  228. unsigned long* scope_id, asio::error_code& ec);
  229. ASIO_DECL int gethostname(char* name,
  230. int namelen, asio::error_code& ec);
  231. #if !defined(ASIO_WINDOWS_RUNTIME)
  232. ASIO_DECL asio::error_code getaddrinfo(const char* host,
  233. const char* service, const addrinfo_type& hints,
  234. addrinfo_type** result, asio::error_code& ec);
  235. ASIO_DECL asio::error_code background_getaddrinfo(
  236. const weak_cancel_token_type& cancel_token, const char* host,
  237. const char* service, const addrinfo_type& hints,
  238. addrinfo_type** result, asio::error_code& ec);
  239. ASIO_DECL void freeaddrinfo(addrinfo_type* ai);
  240. ASIO_DECL asio::error_code getnameinfo(
  241. const socket_addr_type* addr, std::size_t addrlen,
  242. char* host, std::size_t hostlen, char* serv,
  243. std::size_t servlen, int flags, asio::error_code& ec);
  244. ASIO_DECL asio::error_code sync_getnameinfo(
  245. const socket_addr_type* addr, std::size_t addrlen,
  246. char* host, std::size_t hostlen, char* serv,
  247. std::size_t servlen, int sock_type, asio::error_code& ec);
  248. ASIO_DECL asio::error_code background_getnameinfo(
  249. const weak_cancel_token_type& cancel_token,
  250. const socket_addr_type* addr, std::size_t addrlen,
  251. char* host, std::size_t hostlen, char* serv,
  252. std::size_t servlen, int sock_type, asio::error_code& ec);
  253. #endif // !defined(ASIO_WINDOWS_RUNTIME)
  254. ASIO_DECL u_long_type network_to_host_long(u_long_type value);
  255. ASIO_DECL u_long_type host_to_network_long(u_long_type value);
  256. ASIO_DECL u_short_type network_to_host_short(u_short_type value);
  257. ASIO_DECL u_short_type host_to_network_short(u_short_type value);
  258. } // namespace socket_ops
  259. } // namespace detail
  260. } // namespace asio
  261. #include "asio/detail/pop_options.hpp"
  262. #if defined(ASIO_HEADER_ONLY)
  263. # include "asio/detail/impl/socket_ops.ipp"
  264. #endif // defined(ASIO_HEADER_ONLY)
  265. #endif // ASIO_DETAIL_SOCKET_OPS_HPP