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