socket_ops.hpp 10 KB

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