socket_ops.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // detail/socket_ops.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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, void* addr,
  49. std::size_t* addrlen, asio::error_code& ec);
  50. ASIO_DECL socket_type sync_accept(socket_type s, state_type state,
  51. void* addr, std::size_t* addrlen, asio::error_code& ec);
  52. #if defined(ASIO_HAS_IOCP)
  53. ASIO_DECL void complete_iocp_accept(socket_type s, void* output_buffer,
  54. DWORD address_length, void* addr, std::size_t* addrlen,
  55. socket_type new_socket, asio::error_code& ec);
  56. #else // defined(ASIO_HAS_IOCP)
  57. ASIO_DECL bool non_blocking_accept(socket_type s,
  58. state_type state, void* addr, std::size_t* addrlen,
  59. asio::error_code& ec, socket_type& new_socket);
  60. #endif // defined(ASIO_HAS_IOCP)
  61. ASIO_DECL int bind(socket_type s, const void* addr,
  62. std::size_t addrlen, asio::error_code& ec);
  63. ASIO_DECL int close(socket_type s, state_type& state,
  64. bool destruction, asio::error_code& ec);
  65. ASIO_DECL bool set_user_non_blocking(socket_type s,
  66. state_type& state, bool value, asio::error_code& ec);
  67. ASIO_DECL bool set_internal_non_blocking(socket_type s,
  68. state_type& state, bool value, asio::error_code& ec);
  69. ASIO_DECL int shutdown(socket_type s,
  70. int what, asio::error_code& ec);
  71. ASIO_DECL int connect(socket_type s, const void* addr,
  72. std::size_t addrlen, asio::error_code& ec);
  73. ASIO_DECL void sync_connect(socket_type s, const void* addr,
  74. std::size_t addrlen, asio::error_code& ec);
  75. #if defined(ASIO_HAS_IOCP)
  76. ASIO_DECL void complete_iocp_connect(socket_type s,
  77. asio::error_code& ec);
  78. #endif // defined(ASIO_HAS_IOCP)
  79. ASIO_DECL bool non_blocking_connect(socket_type s,
  80. asio::error_code& ec);
  81. ASIO_DECL int socketpair(int af, int type, int protocol,
  82. socket_type sv[2], asio::error_code& ec);
  83. ASIO_DECL bool sockatmark(socket_type s, asio::error_code& ec);
  84. ASIO_DECL size_t available(socket_type s, asio::error_code& ec);
  85. ASIO_DECL int listen(socket_type s,
  86. int backlog, asio::error_code& ec);
  87. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  88. typedef WSABUF buf;
  89. #else // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  90. typedef iovec buf;
  91. #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  92. ASIO_DECL void init_buf(buf& b, void* data, size_t size);
  93. ASIO_DECL void init_buf(buf& b, const void* data, size_t size);
  94. ASIO_DECL signed_size_type recv(socket_type s, buf* bufs,
  95. size_t count, int flags, asio::error_code& ec);
  96. ASIO_DECL signed_size_type recv1(socket_type s,
  97. void* data, size_t size, int flags, asio::error_code& ec);
  98. ASIO_DECL size_t sync_recv(socket_type s, state_type state, buf* bufs,
  99. size_t count, int flags, bool all_empty, asio::error_code& ec);
  100. ASIO_DECL size_t sync_recv1(socket_type s, state_type state,
  101. void* data, size_t size, int flags, asio::error_code& ec);
  102. #if defined(ASIO_HAS_IOCP)
  103. ASIO_DECL void complete_iocp_recv(state_type state,
  104. const weak_cancel_token_type& cancel_token, bool all_empty,
  105. asio::error_code& ec, size_t bytes_transferred);
  106. #else // defined(ASIO_HAS_IOCP)
  107. ASIO_DECL bool non_blocking_recv(socket_type s,
  108. buf* bufs, size_t count, int flags, bool is_stream,
  109. asio::error_code& ec, size_t& bytes_transferred);
  110. ASIO_DECL bool non_blocking_recv1(socket_type s,
  111. void* data, size_t size, int flags, bool is_stream,
  112. asio::error_code& ec, size_t& bytes_transferred);
  113. #endif // defined(ASIO_HAS_IOCP)
  114. ASIO_DECL signed_size_type recvfrom(socket_type s,
  115. buf* bufs, size_t count, int flags, void* addr,
  116. std::size_t* addrlen, asio::error_code& ec);
  117. ASIO_DECL signed_size_type recvfrom1(socket_type s,
  118. void* data, size_t size, int flags, void* addr,
  119. std::size_t* addrlen, asio::error_code& ec);
  120. ASIO_DECL size_t sync_recvfrom(socket_type s, state_type state,
  121. buf* bufs, size_t count, int flags, void* addr,
  122. std::size_t* addrlen, asio::error_code& ec);
  123. ASIO_DECL size_t sync_recvfrom1(socket_type s, state_type state,
  124. void* data, size_t size, int flags, void* addr,
  125. std::size_t* addrlen, asio::error_code& ec);
  126. #if defined(ASIO_HAS_IOCP)
  127. ASIO_DECL void complete_iocp_recvfrom(
  128. const weak_cancel_token_type& cancel_token,
  129. asio::error_code& ec);
  130. #else // defined(ASIO_HAS_IOCP)
  131. ASIO_DECL bool non_blocking_recvfrom(socket_type s, buf* bufs,
  132. size_t count, int flags, void* addr, std::size_t* addrlen,
  133. asio::error_code& ec, size_t& bytes_transferred);
  134. ASIO_DECL bool non_blocking_recvfrom1(socket_type s, void* data,
  135. size_t size, int flags, void* addr, std::size_t* addrlen,
  136. asio::error_code& ec, size_t& bytes_transferred);
  137. #endif // defined(ASIO_HAS_IOCP)
  138. ASIO_DECL signed_size_type recvmsg(socket_type s, buf* bufs,
  139. size_t count, int in_flags, int& out_flags,
  140. asio::error_code& ec);
  141. ASIO_DECL size_t sync_recvmsg(socket_type s, state_type state,
  142. buf* bufs, size_t count, int in_flags, int& out_flags,
  143. asio::error_code& ec);
  144. #if defined(ASIO_HAS_IOCP)
  145. ASIO_DECL void complete_iocp_recvmsg(
  146. const weak_cancel_token_type& cancel_token,
  147. asio::error_code& ec);
  148. #else // defined(ASIO_HAS_IOCP)
  149. ASIO_DECL bool non_blocking_recvmsg(socket_type s,
  150. buf* bufs, size_t count, int in_flags, int& out_flags,
  151. asio::error_code& ec, size_t& bytes_transferred);
  152. #endif // defined(ASIO_HAS_IOCP)
  153. ASIO_DECL signed_size_type send(socket_type s, const buf* bufs,
  154. size_t count, int flags, asio::error_code& ec);
  155. ASIO_DECL signed_size_type send1(socket_type s,
  156. const void* data, size_t size, int flags, asio::error_code& ec);
  157. ASIO_DECL size_t sync_send(socket_type s, state_type state,
  158. const buf* bufs, size_t count, int flags,
  159. bool all_empty, asio::error_code& ec);
  160. ASIO_DECL size_t sync_send1(socket_type s, state_type state,
  161. const void* data, size_t size, int flags, asio::error_code& ec);
  162. #if defined(ASIO_HAS_IOCP)
  163. ASIO_DECL void complete_iocp_send(
  164. const weak_cancel_token_type& cancel_token,
  165. asio::error_code& ec);
  166. #else // defined(ASIO_HAS_IOCP)
  167. ASIO_DECL bool non_blocking_send(socket_type s,
  168. const buf* bufs, size_t count, int flags,
  169. asio::error_code& ec, size_t& bytes_transferred);
  170. ASIO_DECL bool non_blocking_send1(socket_type s,
  171. const void* data, size_t size, int flags,
  172. asio::error_code& ec, size_t& bytes_transferred);
  173. #endif // defined(ASIO_HAS_IOCP)
  174. ASIO_DECL signed_size_type sendto(socket_type s,
  175. const buf* bufs, size_t count, int flags, const void* addr,
  176. std::size_t addrlen, asio::error_code& ec);
  177. ASIO_DECL signed_size_type sendto1(socket_type s,
  178. const void* data, size_t size, int flags, const void* addr,
  179. std::size_t addrlen, asio::error_code& ec);
  180. ASIO_DECL size_t sync_sendto(socket_type s, state_type state,
  181. const buf* bufs, size_t count, int flags, const void* addr,
  182. std::size_t addrlen, asio::error_code& ec);
  183. ASIO_DECL size_t sync_sendto1(socket_type s, state_type state,
  184. const void* data, size_t size, int flags, const void* addr,
  185. std::size_t addrlen, asio::error_code& ec);
  186. #if !defined(ASIO_HAS_IOCP)
  187. ASIO_DECL bool non_blocking_sendto(socket_type s, const buf* bufs,
  188. size_t count, int flags, const void* addr, std::size_t addrlen,
  189. asio::error_code& ec, size_t& bytes_transferred);
  190. ASIO_DECL bool non_blocking_sendto1(socket_type s, const void* data,
  191. size_t size, int flags, const void* addr, std::size_t addrlen,
  192. asio::error_code& ec, size_t& bytes_transferred);
  193. #endif // !defined(ASIO_HAS_IOCP)
  194. ASIO_DECL socket_type socket(int af, int type, int protocol,
  195. asio::error_code& ec);
  196. ASIO_DECL int setsockopt(socket_type s, state_type& state,
  197. int level, int optname, const void* optval,
  198. std::size_t optlen, asio::error_code& ec);
  199. ASIO_DECL int getsockopt(socket_type s, state_type state,
  200. int level, int optname, void* optval,
  201. size_t* optlen, asio::error_code& ec);
  202. ASIO_DECL int getpeername(socket_type s, void* addr,
  203. std::size_t* addrlen, bool cached, asio::error_code& ec);
  204. ASIO_DECL int getsockname(socket_type s, void* addr,
  205. std::size_t* addrlen, asio::error_code& ec);
  206. ASIO_DECL int ioctl(socket_type s, state_type& state,
  207. int cmd, ioctl_arg_type* arg, asio::error_code& ec);
  208. ASIO_DECL int select(int nfds, fd_set* readfds, fd_set* writefds,
  209. fd_set* exceptfds, timeval* timeout, asio::error_code& ec);
  210. ASIO_DECL int poll_read(socket_type s,
  211. state_type state, int msec, asio::error_code& ec);
  212. ASIO_DECL int poll_write(socket_type s,
  213. state_type state, int msec, asio::error_code& ec);
  214. ASIO_DECL int poll_error(socket_type s,
  215. state_type state, int msec, asio::error_code& ec);
  216. ASIO_DECL int poll_connect(socket_type s,
  217. int msec, asio::error_code& ec);
  218. #endif // !defined(ASIO_WINDOWS_RUNTIME)
  219. ASIO_DECL const char* inet_ntop(int af, const void* src, char* dest,
  220. size_t length, unsigned long scope_id, asio::error_code& ec);
  221. ASIO_DECL int inet_pton(int af, const char* src, void* dest,
  222. unsigned long* scope_id, asio::error_code& ec);
  223. ASIO_DECL int gethostname(char* name,
  224. int namelen, asio::error_code& ec);
  225. #if !defined(ASIO_WINDOWS_RUNTIME)
  226. ASIO_DECL asio::error_code getaddrinfo(const char* host,
  227. const char* service, const addrinfo_type& hints,
  228. addrinfo_type** result, asio::error_code& ec);
  229. ASIO_DECL asio::error_code background_getaddrinfo(
  230. const weak_cancel_token_type& cancel_token, const char* host,
  231. const char* service, const addrinfo_type& hints,
  232. addrinfo_type** result, asio::error_code& ec);
  233. ASIO_DECL void freeaddrinfo(addrinfo_type* ai);
  234. ASIO_DECL asio::error_code getnameinfo(const void* addr,
  235. std::size_t addrlen, char* host, std::size_t hostlen, char* serv,
  236. std::size_t servlen, int flags, asio::error_code& ec);
  237. ASIO_DECL asio::error_code sync_getnameinfo(const void* addr,
  238. std::size_t addrlen, char* host, std::size_t hostlen, char* serv,
  239. std::size_t servlen, int sock_type, asio::error_code& ec);
  240. ASIO_DECL asio::error_code background_getnameinfo(
  241. const weak_cancel_token_type& cancel_token,
  242. const void* addr, std::size_t addrlen,
  243. char* host, std::size_t hostlen, char* serv,
  244. std::size_t servlen, int sock_type, asio::error_code& ec);
  245. #endif // !defined(ASIO_WINDOWS_RUNTIME)
  246. ASIO_DECL u_long_type network_to_host_long(u_long_type value);
  247. ASIO_DECL u_long_type host_to_network_long(u_long_type value);
  248. ASIO_DECL u_short_type network_to_host_short(u_short_type value);
  249. ASIO_DECL u_short_type host_to_network_short(u_short_type value);
  250. } // namespace socket_ops
  251. } // namespace detail
  252. } // namespace asio
  253. #include "asio/detail/pop_options.hpp"
  254. #if defined(ASIO_HEADER_ONLY)
  255. # include "asio/detail/impl/socket_ops.ipp"
  256. #endif // defined(ASIO_HEADER_ONLY)
  257. #endif // ASIO_DETAIL_SOCKET_OPS_HPP