socket_ops.hpp 11 KB

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