2 // detail/impl/socket_ops.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
12 #define BOOST_ASIO_DETAIL_SOCKET_OPS_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
26 #include <boost/asio/detail/assert.hpp>
27 #include <boost/asio/detail/socket_ops.hpp>
28 #include <boost/asio/error.hpp>
30 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
34 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
36 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) \
37 || defined(__MACH__) && defined(__APPLE__)
38 # if defined(BOOST_ASIO_HAS_PTHREADS)
40 # endif // defined(BOOST_ASIO_HAS_PTHREADS)
41 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
42 // || defined(__MACH__) && defined(__APPLE__)
44 #include <boost/asio/detail/push_options.hpp>
49 namespace socket_ops {
51 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
53 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
54 struct msghdr { int msg_namelen; };
55 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
58 // HP-UX doesn't declare these functions extern "C", so they are declared again
59 // here to avoid linker errors about undefined symbols.
60 extern "C" char* if_indextoname(unsigned int, char*);
61 extern "C" unsigned int if_nametoindex(const char*);
62 #endif // defined(__hpux)
64 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
66 inline void clear_last_error()
68 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
75 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
77 template <typename ReturnType>
78 inline ReturnType error_wrapper(ReturnType return_value,
79 boost::system::error_code& ec)
81 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
82 ec = boost::system::error_code(WSAGetLastError(),
83 boost::asio::error::get_system_category());
85 ec = boost::system::error_code(errno,
86 boost::asio::error::get_system_category());
91 template <typename SockLenType>
92 inline socket_type call_accept(SockLenType msghdr::*,
93 socket_type s, socket_addr_type* addr, std::size_t* addrlen)
95 SockLenType tmp_addrlen = addrlen ? (SockLenType)*addrlen : 0;
96 socket_type result = ::accept(s, addr, addrlen ? &tmp_addrlen : 0);
98 *addrlen = (std::size_t)tmp_addrlen;
102 socket_type accept(socket_type s, socket_addr_type* addr,
103 std::size_t* addrlen, boost::system::error_code& ec)
105 if (s == invalid_socket)
107 ec = boost::asio::error::bad_descriptor;
108 return invalid_socket;
113 socket_type new_s = error_wrapper(call_accept(
114 &msghdr::msg_namelen, s, addr, addrlen), ec);
115 if (new_s == invalid_socket)
118 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
120 int result = error_wrapper(::setsockopt(new_s,
121 SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)), ec);
125 return invalid_socket;
129 ec = boost::system::error_code();
133 socket_type sync_accept(socket_type s, state_type state,
134 socket_addr_type* addr, std::size_t* addrlen, boost::system::error_code& ec)
139 // Try to complete the operation without blocking.
140 socket_type new_socket = socket_ops::accept(s, addr, addrlen, ec);
142 // Check if operation succeeded.
143 if (new_socket != invalid_socket)
147 if (ec == boost::asio::error::would_block
148 || ec == boost::asio::error::try_again)
150 if (state & user_set_non_blocking)
151 return invalid_socket;
152 // Fall through to retry operation.
154 else if (ec == boost::asio::error::connection_aborted)
156 if (state & enable_connection_aborted)
157 return invalid_socket;
158 // Fall through to retry operation.
161 else if (ec.value() == EPROTO)
163 if (state & enable_connection_aborted)
164 return invalid_socket;
165 // Fall through to retry operation.
167 #endif // defined(EPROTO)
169 return invalid_socket;
171 // Wait for socket to become ready.
172 if (socket_ops::poll_read(s, 0, ec) < 0)
173 return invalid_socket;
177 #if defined(BOOST_ASIO_HAS_IOCP)
179 void complete_iocp_accept(socket_type s,
180 void* output_buffer, DWORD address_length,
181 socket_addr_type* addr, std::size_t* addrlen,
182 socket_type new_socket, boost::system::error_code& ec)
184 // Map non-portable errors to their portable counterparts.
185 if (ec.value() == ERROR_NETNAME_DELETED)
186 ec = boost::asio::error::connection_aborted;
190 // Get the address of the peer.
193 LPSOCKADDR local_addr = 0;
194 int local_addr_length = 0;
195 LPSOCKADDR remote_addr = 0;
196 int remote_addr_length = 0;
197 GetAcceptExSockaddrs(output_buffer, 0, address_length,
198 address_length, &local_addr, &local_addr_length,
199 &remote_addr, &remote_addr_length);
200 if (static_cast<std::size_t>(remote_addr_length) > *addrlen)
202 ec = boost::asio::error::invalid_argument;
206 using namespace std; // For memcpy.
207 memcpy(addr, remote_addr, remote_addr_length);
208 *addrlen = static_cast<std::size_t>(remote_addr_length);
212 // Need to set the SO_UPDATE_ACCEPT_CONTEXT option so that getsockname
213 // and getpeername will work on the accepted socket.
214 SOCKET update_ctx_param = s;
215 socket_ops::state_type state = 0;
216 socket_ops::setsockopt(new_socket, state,
217 SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
218 &update_ctx_param, sizeof(SOCKET), ec);
222 #else // defined(BOOST_ASIO_HAS_IOCP)
224 bool non_blocking_accept(socket_type s,
225 state_type state, socket_addr_type* addr, std::size_t* addrlen,
226 boost::system::error_code& ec, socket_type& new_socket)
230 // Accept the waiting connection.
231 new_socket = socket_ops::accept(s, addr, addrlen, ec);
233 // Check if operation succeeded.
234 if (new_socket != invalid_socket)
237 // Retry operation if interrupted by signal.
238 if (ec == boost::asio::error::interrupted)
242 if (ec == boost::asio::error::would_block
243 || ec == boost::asio::error::try_again)
245 if (state & user_set_non_blocking)
247 // Fall through to retry operation.
249 else if (ec == boost::asio::error::connection_aborted)
251 if (state & enable_connection_aborted)
253 // Fall through to retry operation.
256 else if (ec.value() == EPROTO)
258 if (state & enable_connection_aborted)
260 // Fall through to retry operation.
262 #endif // defined(EPROTO)
270 #endif // defined(BOOST_ASIO_HAS_IOCP)
272 template <typename SockLenType>
273 inline int call_bind(SockLenType msghdr::*,
274 socket_type s, const socket_addr_type* addr, std::size_t addrlen)
276 return ::bind(s, addr, (SockLenType)addrlen);
279 int bind(socket_type s, const socket_addr_type* addr,
280 std::size_t addrlen, boost::system::error_code& ec)
282 if (s == invalid_socket)
284 ec = boost::asio::error::bad_descriptor;
285 return socket_error_retval;
289 int result = error_wrapper(call_bind(
290 &msghdr::msg_namelen, s, addr, addrlen), ec);
292 ec = boost::system::error_code();
296 int close(socket_type s, state_type& state,
297 bool destruction, boost::system::error_code& ec)
300 if (s != invalid_socket)
302 // We don't want the destructor to block, so set the socket to linger in
303 // the background. If the user doesn't like this behaviour then they need
304 // to explicitly close the socket.
305 if (destruction && (state & user_set_linger))
310 boost::system::error_code ignored_ec;
311 socket_ops::setsockopt(s, state, SOL_SOCKET,
312 SO_LINGER, &opt, sizeof(opt), ignored_ec);
316 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
317 result = error_wrapper(::closesocket(s), ec);
318 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
319 result = error_wrapper(::close(s), ec);
320 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
323 && (ec == boost::asio::error::would_block
324 || ec == boost::asio::error::try_again))
326 // According to UNIX Network Programming Vol. 1, it is possible for
327 // close() to fail with EWOULDBLOCK under certain circumstances. What
328 // isn't clear is the state of the descriptor after this error. The one
329 // current OS where this behaviour is seen, Windows, says that the socket
330 // remains open. Therefore we'll put the descriptor back into blocking
331 // mode and have another attempt at closing it.
332 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
333 ioctl_arg_type arg = 0;
334 ::ioctlsocket(s, FIONBIO, &arg);
335 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
336 # if defined(__SYMBIAN32__)
337 int flags = ::fcntl(s, F_GETFL, 0);
339 ::fcntl(s, F_SETFL, flags & ~O_NONBLOCK);
340 # else // defined(__SYMBIAN32__)
341 ioctl_arg_type arg = 0;
342 ::ioctl(s, FIONBIO, &arg);
343 # endif // defined(__SYMBIAN32__)
344 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
345 state &= ~non_blocking;
348 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
349 result = error_wrapper(::closesocket(s), ec);
350 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
351 result = error_wrapper(::close(s), ec);
352 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
357 ec = boost::system::error_code();
361 bool set_user_non_blocking(socket_type s,
362 state_type& state, bool value, boost::system::error_code& ec)
364 if (s == invalid_socket)
366 ec = boost::asio::error::bad_descriptor;
371 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
372 ioctl_arg_type arg = (value ? 1 : 0);
373 int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
374 #elif defined(__SYMBIAN32__)
375 int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
379 int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
380 result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
383 ioctl_arg_type arg = (value ? 1 : 0);
384 int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
389 ec = boost::system::error_code();
391 state |= user_set_non_blocking;
394 // Clearing the user-set non-blocking mode always overrides any
395 // internally-set non-blocking flag. Any subsequent asynchronous
396 // operations will need to re-enable non-blocking I/O.
397 state &= ~(user_set_non_blocking | internal_non_blocking);
405 bool set_internal_non_blocking(socket_type s,
406 state_type& state, bool value, boost::system::error_code& ec)
408 if (s == invalid_socket)
410 ec = boost::asio::error::bad_descriptor;
414 if (!value && (state & user_set_non_blocking))
416 // It does not make sense to clear the internal non-blocking flag if the
417 // user still wants non-blocking behaviour. Return an error and let the
418 // caller figure out whether to update the user-set non-blocking flag.
419 ec = boost::asio::error::invalid_argument;
424 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
425 ioctl_arg_type arg = (value ? 1 : 0);
426 int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
427 #elif defined(__SYMBIAN32__)
428 int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
432 int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
433 result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
436 ioctl_arg_type arg = (value ? 1 : 0);
437 int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
442 ec = boost::system::error_code();
444 state |= internal_non_blocking;
446 state &= ~internal_non_blocking;
453 int shutdown(socket_type s, int what, boost::system::error_code& ec)
455 if (s == invalid_socket)
457 ec = boost::asio::error::bad_descriptor;
458 return socket_error_retval;
462 int result = error_wrapper(::shutdown(s, what), ec);
464 ec = boost::system::error_code();
468 template <typename SockLenType>
469 inline int call_connect(SockLenType msghdr::*,
470 socket_type s, const socket_addr_type* addr, std::size_t addrlen)
472 return ::connect(s, addr, (SockLenType)addrlen);
475 int connect(socket_type s, const socket_addr_type* addr,
476 std::size_t addrlen, boost::system::error_code& ec)
478 if (s == invalid_socket)
480 ec = boost::asio::error::bad_descriptor;
481 return socket_error_retval;
485 int result = error_wrapper(call_connect(
486 &msghdr::msg_namelen, s, addr, addrlen), ec);
488 ec = boost::system::error_code();
489 #if defined(__linux__)
490 else if (ec == boost::asio::error::try_again)
491 ec = boost::asio::error::no_buffer_space;
492 #endif // defined(__linux__)
496 void sync_connect(socket_type s, const socket_addr_type* addr,
497 std::size_t addrlen, boost::system::error_code& ec)
499 // Perform the connect operation.
500 socket_ops::connect(s, addr, addrlen, ec);
501 if (ec != boost::asio::error::in_progress
502 && ec != boost::asio::error::would_block)
504 // The connect operation finished immediately.
508 // Wait for socket to become ready.
509 if (socket_ops::poll_connect(s, ec) < 0)
512 // Get the error code from the connect operation.
513 int connect_error = 0;
514 size_t connect_error_len = sizeof(connect_error);
515 if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_ERROR,
516 &connect_error, &connect_error_len, ec) == socket_error_retval)
519 // Return the result of the connect operation.
520 ec = boost::system::error_code(connect_error,
521 boost::asio::error::get_system_category());
524 #if defined(BOOST_ASIO_HAS_IOCP)
526 void complete_iocp_connect(socket_type s, boost::system::error_code& ec)
528 // Map non-portable errors to their portable counterparts.
531 case ERROR_CONNECTION_REFUSED:
532 ec = boost::asio::error::connection_refused;
534 case ERROR_NETWORK_UNREACHABLE:
535 ec = boost::asio::error::network_unreachable;
537 case ERROR_HOST_UNREACHABLE:
538 ec = boost::asio::error::host_unreachable;
540 case ERROR_SEM_TIMEOUT:
541 ec = boost::asio::error::timed_out;
549 // Need to set the SO_UPDATE_CONNECT_CONTEXT option so that getsockname
550 // and getpeername will work on the connected socket.
551 socket_ops::state_type state = 0;
552 const int so_update_connect_context = 0x7010;
553 socket_ops::setsockopt(s, state, SOL_SOCKET,
554 so_update_connect_context, 0, 0, ec);
558 #endif // defined(BOOST_ASIO_HAS_IOCP)
560 bool non_blocking_connect(socket_type s, boost::system::error_code& ec)
562 // Check if the connect operation has finished. This is required since we may
563 // get spurious readiness notifications from the reactor.
564 #if defined(BOOST_ASIO_WINDOWS) \
565 || defined(__CYGWIN__) \
566 || defined(__SYMBIAN32__)
569 FD_SET(s, &write_fds);
571 FD_ZERO(&except_fds);
572 FD_SET(s, &except_fds);
573 timeval zero_timeout;
574 zero_timeout.tv_sec = 0;
575 zero_timeout.tv_usec = 0;
576 int ready = ::select(s + 1, 0, &write_fds, &except_fds, &zero_timeout);
577 #else // defined(BOOST_ASIO_WINDOWS)
578 // || defined(__CYGWIN__)
579 // || defined(__SYMBIAN32__)
582 fds.events = POLLOUT;
584 int ready = ::poll(&fds, 1, 0);
585 #endif // defined(BOOST_ASIO_WINDOWS)
586 // || defined(__CYGWIN__)
587 // || defined(__SYMBIAN32__)
590 // The asynchronous connect operation is still in progress.
594 // Get the error code from the connect operation.
595 int connect_error = 0;
596 size_t connect_error_len = sizeof(connect_error);
597 if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_ERROR,
598 &connect_error, &connect_error_len, ec) == 0)
602 ec = boost::system::error_code(connect_error,
603 boost::asio::error::get_system_category());
606 ec = boost::system::error_code();
612 int socketpair(int af, int type, int protocol,
613 socket_type sv[2], boost::system::error_code& ec)
615 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
620 ec = boost::asio::error::operation_not_supported;
621 return socket_error_retval;
624 int result = error_wrapper(::socketpair(af, type, protocol, sv), ec);
626 ec = boost::system::error_code();
631 bool sockatmark(socket_type s, boost::system::error_code& ec)
633 if (s == invalid_socket)
635 ec = boost::asio::error::bad_descriptor;
639 #if defined(SIOCATMARK)
640 ioctl_arg_type value = 0;
641 # if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
642 int result = error_wrapper(::ioctlsocket(s, SIOCATMARK, &value), ec);
643 # else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
644 int result = error_wrapper(::ioctl(s, SIOCATMARK, &value), ec);
645 # endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
647 ec = boost::system::error_code();
649 if (ec.value() == ENOTTY)
650 ec = boost::asio::error::not_socket;
651 # endif // defined(ENOTTY)
652 #else // defined(SIOCATMARK)
653 int value = error_wrapper(::sockatmark(s), ec);
655 ec = boost::system::error_code();
656 #endif // defined(SIOCATMARK)
658 return ec ? false : value != 0;
661 size_t available(socket_type s, boost::system::error_code& ec)
663 if (s == invalid_socket)
665 ec = boost::asio::error::bad_descriptor;
669 ioctl_arg_type value = 0;
670 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
671 int result = error_wrapper(::ioctlsocket(s, FIONREAD, &value), ec);
672 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
673 int result = error_wrapper(::ioctl(s, FIONREAD, &value), ec);
674 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
676 ec = boost::system::error_code();
678 if (ec.value() == ENOTTY)
679 ec = boost::asio::error::not_socket;
680 #endif // defined(ENOTTY)
682 return ec ? static_cast<size_t>(0) : static_cast<size_t>(value);
685 int listen(socket_type s, int backlog, boost::system::error_code& ec)
687 if (s == invalid_socket)
689 ec = boost::asio::error::bad_descriptor;
690 return socket_error_retval;
694 int result = error_wrapper(::listen(s, backlog), ec);
696 ec = boost::system::error_code();
700 inline void init_buf_iov_base(void*& base, void* addr)
705 template <typename T>
706 inline void init_buf_iov_base(T& base, void* addr)
708 base = static_cast<T>(addr);
711 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
713 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
715 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
717 void init_buf(buf& b, void* data, size_t size)
719 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
720 b.buf = static_cast<char*>(data);
721 b.len = static_cast<u_long>(size);
722 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
723 init_buf_iov_base(b.iov_base, data);
725 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
728 void init_buf(buf& b, const void* data, size_t size)
730 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
731 b.buf = static_cast<char*>(const_cast<void*>(data));
732 b.len = static_cast<u_long>(size);
733 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
734 init_buf_iov_base(b.iov_base, const_cast<void*>(data));
736 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
739 inline void init_msghdr_msg_name(void*& name, socket_addr_type* addr)
744 inline void init_msghdr_msg_name(void*& name, const socket_addr_type* addr)
746 name = const_cast<socket_addr_type*>(addr);
749 template <typename T>
750 inline void init_msghdr_msg_name(T& name, socket_addr_type* addr)
752 name = reinterpret_cast<T>(addr);
755 template <typename T>
756 inline void init_msghdr_msg_name(T& name, const socket_addr_type* addr)
758 name = reinterpret_cast<T>(const_cast<socket_addr_type*>(addr));
761 signed_size_type recv(socket_type s, buf* bufs, size_t count,
762 int flags, boost::system::error_code& ec)
765 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
766 // Receive some data.
767 DWORD recv_buf_count = static_cast<DWORD>(count);
768 DWORD bytes_transferred = 0;
769 DWORD recv_flags = flags;
770 int result = error_wrapper(::WSARecv(s, bufs,
771 recv_buf_count, &bytes_transferred, &recv_flags, 0, 0), ec);
772 if (ec.value() == ERROR_NETNAME_DELETED)
773 ec = boost::asio::error::connection_reset;
774 else if (ec.value() == ERROR_PORT_UNREACHABLE)
775 ec = boost::asio::error::connection_refused;
777 return socket_error_retval;
778 ec = boost::system::error_code();
779 return bytes_transferred;
780 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
781 msghdr msg = msghdr();
783 msg.msg_iovlen = static_cast<int>(count);
784 signed_size_type result = error_wrapper(::recvmsg(s, &msg, flags), ec);
786 ec = boost::system::error_code();
788 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
791 size_t sync_recv(socket_type s, state_type state, buf* bufs,
792 size_t count, int flags, bool all_empty, boost::system::error_code& ec)
794 if (s == invalid_socket)
796 ec = boost::asio::error::bad_descriptor;
800 // A request to read 0 bytes on a stream is a no-op.
801 if (all_empty && (state & stream_oriented))
803 ec = boost::system::error_code();
810 // Try to complete the operation without blocking.
811 signed_size_type bytes = socket_ops::recv(s, bufs, count, flags, ec);
813 // Check if operation succeeded.
818 if ((state & stream_oriented) && bytes == 0)
820 ec = boost::asio::error::eof;
825 if ((state & user_set_non_blocking)
826 || (ec != boost::asio::error::would_block
827 && ec != boost::asio::error::try_again))
830 // Wait for socket to become ready.
831 if (socket_ops::poll_read(s, 0, ec) < 0)
836 #if defined(BOOST_ASIO_HAS_IOCP)
838 void complete_iocp_recv(state_type state,
839 const weak_cancel_token_type& cancel_token, bool all_empty,
840 boost::system::error_code& ec, size_t bytes_transferred)
842 // Map non-portable errors to their portable counterparts.
843 if (ec.value() == ERROR_NETNAME_DELETED)
845 if (cancel_token.expired())
846 ec = boost::asio::error::operation_aborted;
848 ec = boost::asio::error::connection_reset;
850 else if (ec.value() == ERROR_PORT_UNREACHABLE)
852 ec = boost::asio::error::connection_refused;
855 // Check for connection closed.
856 else if (!ec && bytes_transferred == 0
857 && (state & stream_oriented) != 0
860 ec = boost::asio::error::eof;
864 #else // defined(BOOST_ASIO_HAS_IOCP)
866 bool non_blocking_recv(socket_type s,
867 buf* bufs, size_t count, int flags, bool is_stream,
868 boost::system::error_code& ec, size_t& bytes_transferred)
873 signed_size_type bytes = socket_ops::recv(s, bufs, count, flags, ec);
875 // Check for end of stream.
876 if (is_stream && bytes == 0)
878 ec = boost::asio::error::eof;
882 // Retry operation if interrupted by signal.
883 if (ec == boost::asio::error::interrupted)
886 // Check if we need to run the operation again.
887 if (ec == boost::asio::error::would_block
888 || ec == boost::asio::error::try_again)
891 // Operation is complete.
894 ec = boost::system::error_code();
895 bytes_transferred = bytes;
898 bytes_transferred = 0;
904 #endif // defined(BOOST_ASIO_HAS_IOCP)
906 signed_size_type recvfrom(socket_type s, buf* bufs, size_t count,
907 int flags, socket_addr_type* addr, std::size_t* addrlen,
908 boost::system::error_code& ec)
911 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
912 // Receive some data.
913 DWORD recv_buf_count = static_cast<DWORD>(count);
914 DWORD bytes_transferred = 0;
915 DWORD recv_flags = flags;
916 int tmp_addrlen = (int)*addrlen;
917 int result = error_wrapper(::WSARecvFrom(s, bufs, recv_buf_count,
918 &bytes_transferred, &recv_flags, addr, &tmp_addrlen, 0, 0), ec);
919 *addrlen = (std::size_t)tmp_addrlen;
920 if (ec.value() == ERROR_NETNAME_DELETED)
921 ec = boost::asio::error::connection_reset;
922 else if (ec.value() == ERROR_PORT_UNREACHABLE)
923 ec = boost::asio::error::connection_refused;
925 return socket_error_retval;
926 ec = boost::system::error_code();
927 return bytes_transferred;
928 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
929 msghdr msg = msghdr();
930 init_msghdr_msg_name(msg.msg_name, addr);
931 msg.msg_namelen = static_cast<int>(*addrlen);
933 msg.msg_iovlen = static_cast<int>(count);
934 signed_size_type result = error_wrapper(::recvmsg(s, &msg, flags), ec);
935 *addrlen = msg.msg_namelen;
937 ec = boost::system::error_code();
939 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
942 size_t sync_recvfrom(socket_type s, state_type state, buf* bufs,
943 size_t count, int flags, socket_addr_type* addr,
944 std::size_t* addrlen, boost::system::error_code& ec)
946 if (s == invalid_socket)
948 ec = boost::asio::error::bad_descriptor;
955 // Try to complete the operation without blocking.
956 signed_size_type bytes = socket_ops::recvfrom(
957 s, bufs, count, flags, addr, addrlen, ec);
959 // Check if operation succeeded.
964 if ((state & user_set_non_blocking)
965 || (ec != boost::asio::error::would_block
966 && ec != boost::asio::error::try_again))
969 // Wait for socket to become ready.
970 if (socket_ops::poll_read(s, 0, ec) < 0)
975 #if defined(BOOST_ASIO_HAS_IOCP)
977 void complete_iocp_recvfrom(
978 const weak_cancel_token_type& cancel_token,
979 boost::system::error_code& ec)
981 // Map non-portable errors to their portable counterparts.
982 if (ec.value() == ERROR_NETNAME_DELETED)
984 if (cancel_token.expired())
985 ec = boost::asio::error::operation_aborted;
987 ec = boost::asio::error::connection_reset;
989 else if (ec.value() == ERROR_PORT_UNREACHABLE)
991 ec = boost::asio::error::connection_refused;
995 #else // defined(BOOST_ASIO_HAS_IOCP)
997 bool non_blocking_recvfrom(socket_type s,
998 buf* bufs, size_t count, int flags,
999 socket_addr_type* addr, std::size_t* addrlen,
1000 boost::system::error_code& ec, size_t& bytes_transferred)
1005 signed_size_type bytes = socket_ops::recvfrom(
1006 s, bufs, count, flags, addr, addrlen, ec);
1008 // Retry operation if interrupted by signal.
1009 if (ec == boost::asio::error::interrupted)
1012 // Check if we need to run the operation again.
1013 if (ec == boost::asio::error::would_block
1014 || ec == boost::asio::error::try_again)
1017 // Operation is complete.
1020 ec = boost::system::error_code();
1021 bytes_transferred = bytes;
1024 bytes_transferred = 0;
1030 #endif // defined(BOOST_ASIO_HAS_IOCP)
1032 signed_size_type recvmsg(socket_type s, buf* bufs, size_t count,
1033 int in_flags, int& out_flags, boost::system::error_code& ec)
1036 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1038 return socket_ops::recv(s, bufs, count, in_flags, ec);
1039 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1040 msghdr msg = msghdr();
1042 msg.msg_iovlen = static_cast<int>(count);
1043 signed_size_type result = error_wrapper(::recvmsg(s, &msg, in_flags), ec);
1046 ec = boost::system::error_code();
1047 out_flags = msg.msg_flags;
1052 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1055 size_t sync_recvmsg(socket_type s, state_type state,
1056 buf* bufs, size_t count, int in_flags, int& out_flags,
1057 boost::system::error_code& ec)
1059 if (s == invalid_socket)
1061 ec = boost::asio::error::bad_descriptor;
1068 // Try to complete the operation without blocking.
1069 signed_size_type bytes = socket_ops::recvmsg(
1070 s, bufs, count, in_flags, out_flags, ec);
1072 // Check if operation succeeded.
1076 // Operation failed.
1077 if ((state & user_set_non_blocking)
1078 || (ec != boost::asio::error::would_block
1079 && ec != boost::asio::error::try_again))
1082 // Wait for socket to become ready.
1083 if (socket_ops::poll_read(s, 0, ec) < 0)
1088 #if defined(BOOST_ASIO_HAS_IOCP)
1090 void complete_iocp_recvmsg(
1091 const weak_cancel_token_type& cancel_token,
1092 boost::system::error_code& ec)
1094 // Map non-portable errors to their portable counterparts.
1095 if (ec.value() == ERROR_NETNAME_DELETED)
1097 if (cancel_token.expired())
1098 ec = boost::asio::error::operation_aborted;
1100 ec = boost::asio::error::connection_reset;
1102 else if (ec.value() == ERROR_PORT_UNREACHABLE)
1104 ec = boost::asio::error::connection_refused;
1108 #else // defined(BOOST_ASIO_HAS_IOCP)
1110 bool non_blocking_recvmsg(socket_type s,
1111 buf* bufs, size_t count, int in_flags, int& out_flags,
1112 boost::system::error_code& ec, size_t& bytes_transferred)
1117 signed_size_type bytes = socket_ops::recvmsg(
1118 s, bufs, count, in_flags, out_flags, ec);
1120 // Retry operation if interrupted by signal.
1121 if (ec == boost::asio::error::interrupted)
1124 // Check if we need to run the operation again.
1125 if (ec == boost::asio::error::would_block
1126 || ec == boost::asio::error::try_again)
1129 // Operation is complete.
1132 ec = boost::system::error_code();
1133 bytes_transferred = bytes;
1136 bytes_transferred = 0;
1142 #endif // defined(BOOST_ASIO_HAS_IOCP)
1144 signed_size_type send(socket_type s, const buf* bufs, size_t count,
1145 int flags, boost::system::error_code& ec)
1148 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1150 DWORD send_buf_count = static_cast<DWORD>(count);
1151 DWORD bytes_transferred = 0;
1152 DWORD send_flags = flags;
1153 int result = error_wrapper(::WSASend(s, const_cast<buf*>(bufs),
1154 send_buf_count, &bytes_transferred, send_flags, 0, 0), ec);
1155 if (ec.value() == ERROR_NETNAME_DELETED)
1156 ec = boost::asio::error::connection_reset;
1157 else if (ec.value() == ERROR_PORT_UNREACHABLE)
1158 ec = boost::asio::error::connection_refused;
1160 return socket_error_retval;
1161 ec = boost::system::error_code();
1162 return bytes_transferred;
1163 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1164 msghdr msg = msghdr();
1165 msg.msg_iov = const_cast<buf*>(bufs);
1166 msg.msg_iovlen = static_cast<int>(count);
1167 #if defined(__linux__)
1168 flags |= MSG_NOSIGNAL;
1169 #endif // defined(__linux__)
1170 signed_size_type result = error_wrapper(::sendmsg(s, &msg, flags), ec);
1172 ec = boost::system::error_code();
1174 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1177 size_t sync_send(socket_type s, state_type state, const buf* bufs,
1178 size_t count, int flags, bool all_empty, boost::system::error_code& ec)
1180 if (s == invalid_socket)
1182 ec = boost::asio::error::bad_descriptor;
1186 // A request to write 0 bytes to a stream is a no-op.
1187 if (all_empty && (state & stream_oriented))
1189 ec = boost::system::error_code();
1196 // Try to complete the operation without blocking.
1197 signed_size_type bytes = socket_ops::send(s, bufs, count, flags, ec);
1199 // Check if operation succeeded.
1203 // Operation failed.
1204 if ((state & user_set_non_blocking)
1205 || (ec != boost::asio::error::would_block
1206 && ec != boost::asio::error::try_again))
1209 // Wait for socket to become ready.
1210 if (socket_ops::poll_write(s, 0, ec) < 0)
1215 #if defined(BOOST_ASIO_HAS_IOCP)
1217 void complete_iocp_send(
1218 const weak_cancel_token_type& cancel_token,
1219 boost::system::error_code& ec)
1221 // Map non-portable errors to their portable counterparts.
1222 if (ec.value() == ERROR_NETNAME_DELETED)
1224 if (cancel_token.expired())
1225 ec = boost::asio::error::operation_aborted;
1227 ec = boost::asio::error::connection_reset;
1229 else if (ec.value() == ERROR_PORT_UNREACHABLE)
1231 ec = boost::asio::error::connection_refused;
1235 #else // defined(BOOST_ASIO_HAS_IOCP)
1237 bool non_blocking_send(socket_type s,
1238 const buf* bufs, size_t count, int flags,
1239 boost::system::error_code& ec, size_t& bytes_transferred)
1244 signed_size_type bytes = socket_ops::send(s, bufs, count, flags, ec);
1246 // Retry operation if interrupted by signal.
1247 if (ec == boost::asio::error::interrupted)
1250 // Check if we need to run the operation again.
1251 if (ec == boost::asio::error::would_block
1252 || ec == boost::asio::error::try_again)
1255 // Operation is complete.
1258 ec = boost::system::error_code();
1259 bytes_transferred = bytes;
1262 bytes_transferred = 0;
1268 #endif // defined(BOOST_ASIO_HAS_IOCP)
1270 signed_size_type sendto(socket_type s, const buf* bufs, size_t count,
1271 int flags, const socket_addr_type* addr, std::size_t addrlen,
1272 boost::system::error_code& ec)
1275 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1277 DWORD send_buf_count = static_cast<DWORD>(count);
1278 DWORD bytes_transferred = 0;
1279 int result = error_wrapper(::WSASendTo(s, const_cast<buf*>(bufs),
1280 send_buf_count, &bytes_transferred, flags, addr,
1281 static_cast<int>(addrlen), 0, 0), ec);
1282 if (ec.value() == ERROR_NETNAME_DELETED)
1283 ec = boost::asio::error::connection_reset;
1284 else if (ec.value() == ERROR_PORT_UNREACHABLE)
1285 ec = boost::asio::error::connection_refused;
1287 return socket_error_retval;
1288 ec = boost::system::error_code();
1289 return bytes_transferred;
1290 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1291 msghdr msg = msghdr();
1292 init_msghdr_msg_name(msg.msg_name, addr);
1293 msg.msg_namelen = static_cast<int>(addrlen);
1294 msg.msg_iov = const_cast<buf*>(bufs);
1295 msg.msg_iovlen = static_cast<int>(count);
1296 #if defined(__linux__)
1297 flags |= MSG_NOSIGNAL;
1298 #endif // defined(__linux__)
1299 signed_size_type result = error_wrapper(::sendmsg(s, &msg, flags), ec);
1301 ec = boost::system::error_code();
1303 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1306 size_t sync_sendto(socket_type s, state_type state, const buf* bufs,
1307 size_t count, int flags, const socket_addr_type* addr,
1308 std::size_t addrlen, boost::system::error_code& ec)
1310 if (s == invalid_socket)
1312 ec = boost::asio::error::bad_descriptor;
1319 // Try to complete the operation without blocking.
1320 signed_size_type bytes = socket_ops::sendto(
1321 s, bufs, count, flags, addr, addrlen, ec);
1323 // Check if operation succeeded.
1327 // Operation failed.
1328 if ((state & user_set_non_blocking)
1329 || (ec != boost::asio::error::would_block
1330 && ec != boost::asio::error::try_again))
1333 // Wait for socket to become ready.
1334 if (socket_ops::poll_write(s, 0, ec) < 0)
1339 #if !defined(BOOST_ASIO_HAS_IOCP)
1341 bool non_blocking_sendto(socket_type s,
1342 const buf* bufs, size_t count, int flags,
1343 const socket_addr_type* addr, std::size_t addrlen,
1344 boost::system::error_code& ec, size_t& bytes_transferred)
1349 signed_size_type bytes = socket_ops::sendto(
1350 s, bufs, count, flags, addr, addrlen, ec);
1352 // Retry operation if interrupted by signal.
1353 if (ec == boost::asio::error::interrupted)
1356 // Check if we need to run the operation again.
1357 if (ec == boost::asio::error::would_block
1358 || ec == boost::asio::error::try_again)
1361 // Operation is complete.
1364 ec = boost::system::error_code();
1365 bytes_transferred = bytes;
1368 bytes_transferred = 0;
1374 #endif // !defined(BOOST_ASIO_HAS_IOCP)
1376 socket_type socket(int af, int type, int protocol,
1377 boost::system::error_code& ec)
1380 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1381 socket_type s = error_wrapper(::WSASocketW(af, type, protocol, 0, 0,
1382 WSA_FLAG_OVERLAPPED), ec);
1383 if (s == invalid_socket)
1386 if (af == BOOST_ASIO_OS_DEF(AF_INET6))
1388 // Try to enable the POSIX default behaviour of having IPV6_V6ONLY set to
1389 // false. This will only succeed on Windows Vista and later versions of
1390 // Windows, where a dual-stack IPv4/v6 implementation is available.
1392 ::setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
1393 reinterpret_cast<const char*>(&optval), sizeof(optval));
1396 ec = boost::system::error_code();
1399 #elif defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
1400 socket_type s = error_wrapper(::socket(af, type, protocol), ec);
1401 if (s == invalid_socket)
1405 int result = error_wrapper(::setsockopt(s,
1406 SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)), ec);
1410 return invalid_socket;
1415 int s = error_wrapper(::socket(af, type, protocol), ec);
1417 ec = boost::system::error_code();
1422 template <typename SockLenType>
1423 inline int call_setsockopt(SockLenType msghdr::*,
1424 socket_type s, int level, int optname,
1425 const void* optval, std::size_t optlen)
1427 return ::setsockopt(s, level, optname,
1428 (const char*)optval, (SockLenType)optlen);
1431 int setsockopt(socket_type s, state_type& state, int level, int optname,
1432 const void* optval, std::size_t optlen, boost::system::error_code& ec)
1434 if (s == invalid_socket)
1436 ec = boost::asio::error::bad_descriptor;
1437 return socket_error_retval;
1440 if (level == custom_socket_option_level && optname == always_fail_option)
1442 ec = boost::asio::error::invalid_argument;
1443 return socket_error_retval;
1446 if (level == custom_socket_option_level
1447 && optname == enable_connection_aborted_option)
1449 if (optlen != sizeof(int))
1451 ec = boost::asio::error::invalid_argument;
1452 return socket_error_retval;
1455 if (*static_cast<const int*>(optval))
1456 state |= enable_connection_aborted;
1458 state &= ~enable_connection_aborted;
1459 ec = boost::system::error_code();
1463 if (level == SOL_SOCKET && optname == SO_LINGER)
1464 state |= user_set_linger;
1466 #if defined(__BORLANDC__)
1467 // Mysteriously, using the getsockopt and setsockopt functions directly with
1468 // Borland C++ results in incorrect values being set and read. The bug can be
1469 // worked around by using function addresses resolved with GetProcAddress.
1470 if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
1472 typedef int (WSAAPI *sso_t)(SOCKET, int, int, const char*, int);
1473 if (sso_t sso = (sso_t)::GetProcAddress(winsock_module, "setsockopt"))
1476 return error_wrapper(sso(s, level, optname,
1477 reinterpret_cast<const char*>(optval),
1478 static_cast<int>(optlen)), ec);
1481 ec = boost::asio::error::fault;
1482 return socket_error_retval;
1483 #else // defined(__BORLANDC__)
1485 int result = error_wrapper(call_setsockopt(&msghdr::msg_namelen,
1486 s, level, optname, optval, optlen), ec);
1489 ec = boost::system::error_code();
1491 #if defined(__MACH__) && defined(__APPLE__) \
1492 || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
1493 // To implement portable behaviour for SO_REUSEADDR with UDP sockets we
1494 // need to also set SO_REUSEPORT on BSD-based platforms.
1495 if ((state & datagram_oriented)
1496 && level == SOL_SOCKET && optname == SO_REUSEADDR)
1498 call_setsockopt(&msghdr::msg_namelen, s,
1499 SOL_SOCKET, SO_REUSEPORT, optval, optlen);
1505 #endif // defined(__BORLANDC__)
1508 template <typename SockLenType>
1509 inline int call_getsockopt(SockLenType msghdr::*,
1510 socket_type s, int level, int optname,
1511 void* optval, std::size_t* optlen)
1513 SockLenType tmp_optlen = (SockLenType)*optlen;
1514 int result = ::getsockopt(s, level, optname, (char*)optval, &tmp_optlen);
1515 *optlen = (std::size_t)tmp_optlen;
1519 int getsockopt(socket_type s, state_type state, int level, int optname,
1520 void* optval, size_t* optlen, boost::system::error_code& ec)
1522 if (s == invalid_socket)
1524 ec = boost::asio::error::bad_descriptor;
1525 return socket_error_retval;
1528 if (level == custom_socket_option_level && optname == always_fail_option)
1530 ec = boost::asio::error::invalid_argument;
1531 return socket_error_retval;
1534 if (level == custom_socket_option_level
1535 && optname == enable_connection_aborted_option)
1537 if (*optlen != sizeof(int))
1539 ec = boost::asio::error::invalid_argument;
1540 return socket_error_retval;
1543 *static_cast<int*>(optval) = (state & enable_connection_aborted) ? 1 : 0;
1544 ec = boost::system::error_code();
1548 #if defined(__BORLANDC__)
1549 // Mysteriously, using the getsockopt and setsockopt functions directly with
1550 // Borland C++ results in incorrect values being set and read. The bug can be
1551 // worked around by using function addresses resolved with GetProcAddress.
1552 if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
1554 typedef int (WSAAPI *gso_t)(SOCKET, int, int, char*, int*);
1555 if (gso_t gso = (gso_t)::GetProcAddress(winsock_module, "getsockopt"))
1558 int tmp_optlen = static_cast<int>(*optlen);
1559 int result = error_wrapper(gso(s, level, optname,
1560 reinterpret_cast<char*>(optval), &tmp_optlen), ec);
1561 *optlen = static_cast<size_t>(tmp_optlen);
1562 if (result != 0 && level == IPPROTO_IPV6 && optname == IPV6_V6ONLY
1563 && ec.value() == WSAENOPROTOOPT && *optlen == sizeof(DWORD))
1565 // Dual-stack IPv4/v6 sockets, and the IPV6_V6ONLY socket option, are
1566 // only supported on Windows Vista and later. To simplify program logic
1567 // we will fake success of getting this option and specify that the
1568 // value is non-zero (i.e. true). This corresponds to the behavior of
1569 // IPv6 sockets on Windows platforms pre-Vista.
1570 *static_cast<DWORD*>(optval) = 1;
1571 ec = boost::system::error_code();
1576 ec = boost::asio::error::fault;
1577 return socket_error_retval;
1578 #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1580 int result = error_wrapper(call_getsockopt(&msghdr::msg_namelen,
1581 s, level, optname, optval, optlen), ec);
1582 if (result != 0 && level == IPPROTO_IPV6 && optname == IPV6_V6ONLY
1583 && ec.value() == WSAENOPROTOOPT && *optlen == sizeof(DWORD))
1585 // Dual-stack IPv4/v6 sockets, and the IPV6_V6ONLY socket option, are only
1586 // supported on Windows Vista and later. To simplify program logic we will
1587 // fake success of getting this option and specify that the value is
1588 // non-zero (i.e. true). This corresponds to the behavior of IPv6 sockets
1589 // on Windows platforms pre-Vista.
1590 *static_cast<DWORD*>(optval) = 1;
1591 ec = boost::system::error_code();
1594 ec = boost::system::error_code();
1596 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1598 int result = error_wrapper(call_getsockopt(&msghdr::msg_namelen,
1599 s, level, optname, optval, optlen), ec);
1600 #if defined(__linux__)
1601 if (result == 0 && level == SOL_SOCKET && *optlen == sizeof(int)
1602 && (optname == SO_SNDBUF || optname == SO_RCVBUF))
1604 // On Linux, setting SO_SNDBUF or SO_RCVBUF to N actually causes the kernel
1605 // to set the buffer size to N*2. Linux puts additional stuff into the
1606 // buffers so that only about half is actually available to the application.
1607 // The retrieved value is divided by 2 here to make it appear as though the
1608 // correct value has been set.
1609 *static_cast<int*>(optval) /= 2;
1611 #endif // defined(__linux__)
1613 ec = boost::system::error_code();
1615 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1618 template <typename SockLenType>
1619 inline int call_getpeername(SockLenType msghdr::*,
1620 socket_type s, socket_addr_type* addr, std::size_t* addrlen)
1622 SockLenType tmp_addrlen = (SockLenType)*addrlen;
1623 int result = ::getpeername(s, addr, &tmp_addrlen);
1624 *addrlen = (std::size_t)tmp_addrlen;
1628 int getpeername(socket_type s, socket_addr_type* addr,
1629 std::size_t* addrlen, bool cached, boost::system::error_code& ec)
1631 if (s == invalid_socket)
1633 ec = boost::asio::error::bad_descriptor;
1634 return socket_error_retval;
1637 #if defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP) \
1638 || defined(__CYGWIN__)
1641 // Check if socket is still connected.
1642 DWORD connect_time = 0;
1643 size_t connect_time_len = sizeof(connect_time);
1644 if (socket_ops::getsockopt(s, 0, SOL_SOCKET, SO_CONNECT_TIME,
1645 &connect_time, &connect_time_len, ec) == socket_error_retval)
1647 return socket_error_retval;
1649 if (connect_time == 0xFFFFFFFF)
1651 ec = boost::asio::error::not_connected;
1652 return socket_error_retval;
1655 // The cached value is still valid.
1656 ec = boost::system::error_code();
1659 #else // defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP)
1660 // || defined(__CYGWIN__)
1662 #endif // defined(BOOST_ASIO_WINDOWS) && !defined(BOOST_ASIO_WINDOWS_APP)
1663 // || defined(__CYGWIN__)
1666 int result = error_wrapper(call_getpeername(
1667 &msghdr::msg_namelen, s, addr, addrlen), ec);
1669 ec = boost::system::error_code();
1673 template <typename SockLenType>
1674 inline int call_getsockname(SockLenType msghdr::*,
1675 socket_type s, socket_addr_type* addr, std::size_t* addrlen)
1677 SockLenType tmp_addrlen = (SockLenType)*addrlen;
1678 int result = ::getsockname(s, addr, &tmp_addrlen);
1679 *addrlen = (std::size_t)tmp_addrlen;
1683 int getsockname(socket_type s, socket_addr_type* addr,
1684 std::size_t* addrlen, boost::system::error_code& ec)
1686 if (s == invalid_socket)
1688 ec = boost::asio::error::bad_descriptor;
1689 return socket_error_retval;
1693 int result = error_wrapper(call_getsockname(
1694 &msghdr::msg_namelen, s, addr, addrlen), ec);
1696 ec = boost::system::error_code();
1700 int ioctl(socket_type s, state_type& state, int cmd,
1701 ioctl_arg_type* arg, boost::system::error_code& ec)
1703 if (s == invalid_socket)
1705 ec = boost::asio::error::bad_descriptor;
1706 return socket_error_retval;
1710 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1711 int result = error_wrapper(::ioctlsocket(s, cmd, arg), ec);
1712 #elif defined(__MACH__) && defined(__APPLE__) \
1713 || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
1714 int result = error_wrapper(::ioctl(s,
1715 static_cast<unsigned int>(cmd), arg), ec);
1717 int result = error_wrapper(::ioctl(s, cmd, arg), ec);
1721 ec = boost::system::error_code();
1723 // When updating the non-blocking mode we always perform the ioctl syscall,
1724 // even if the flags would otherwise indicate that the socket is already in
1725 // the correct state. This ensures that the underlying socket is put into
1726 // the state that has been requested by the user. If the ioctl syscall was
1727 // successful then we need to update the flags to match.
1728 if (cmd == static_cast<int>(FIONBIO))
1732 state |= user_set_non_blocking;
1736 // Clearing the non-blocking mode always overrides any internally-set
1737 // non-blocking flag. Any subsequent asynchronous operations will need
1738 // to re-enable non-blocking I/O.
1739 state &= ~(user_set_non_blocking | internal_non_blocking);
1747 int select(int nfds, fd_set* readfds, fd_set* writefds,
1748 fd_set* exceptfds, timeval* timeout, boost::system::error_code& ec)
1751 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1752 if (!readfds && !writefds && !exceptfds && timeout)
1754 DWORD milliseconds = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
1755 if (milliseconds == 0)
1756 milliseconds = 1; // Force context switch.
1757 ::Sleep(milliseconds);
1758 ec = boost::system::error_code();
1762 // The select() call allows timeout values measured in microseconds, but the
1763 // system clock (as wrapped by boost::posix_time::microsec_clock) typically
1764 // has a resolution of 10 milliseconds. This can lead to a spinning select
1765 // reactor, meaning increased CPU usage, when waiting for the earliest
1766 // scheduled timeout if it's less than 10 milliseconds away. To avoid a tight
1767 // spin we'll use a minimum timeout of 1 millisecond.
1768 if (timeout && timeout->tv_sec == 0
1769 && timeout->tv_usec > 0 && timeout->tv_usec < 1000)
1770 timeout->tv_usec = 1000;
1771 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1773 #if defined(__hpux) && defined(__SELECT)
1775 ts.tv_sec = timeout ? timeout->tv_sec : 0;
1776 ts.tv_nsec = timeout ? timeout->tv_usec * 1000 : 0;
1777 return error_wrapper(::pselect(nfds, readfds,
1778 writefds, exceptfds, timeout ? &ts : 0, 0), ec);
1780 int result = error_wrapper(::select(nfds, readfds,
1781 writefds, exceptfds, timeout), ec);
1783 ec = boost::system::error_code();
1788 int poll_read(socket_type s, state_type state, boost::system::error_code& ec)
1790 if (s == invalid_socket)
1792 ec = boost::asio::error::bad_descriptor;
1793 return socket_error_retval;
1796 #if defined(BOOST_ASIO_WINDOWS) \
1797 || defined(__CYGWIN__) \
1798 || defined(__SYMBIAN32__)
1802 timeval zero_timeout;
1803 zero_timeout.tv_sec = 0;
1804 zero_timeout.tv_usec = 0;
1805 timeval* timeout = (state & user_set_non_blocking) ? &zero_timeout : 0;
1807 int result = error_wrapper(::select(s + 1, &fds, 0, 0, timeout), ec);
1808 #else // defined(BOOST_ASIO_WINDOWS)
1809 // || defined(__CYGWIN__)
1810 // || defined(__SYMBIAN32__)
1813 fds.events = POLLIN;
1815 int timeout = (state & user_set_non_blocking) ? 0 : -1;
1817 int result = error_wrapper(::poll(&fds, 1, timeout), ec);
1818 #endif // defined(BOOST_ASIO_WINDOWS)
1819 // || defined(__CYGWIN__)
1820 // || defined(__SYMBIAN32__)
1822 ec = (state & user_set_non_blocking)
1823 ? boost::asio::error::would_block : boost::system::error_code();
1824 else if (result > 0)
1825 ec = boost::system::error_code();
1829 int poll_write(socket_type s, state_type state, boost::system::error_code& ec)
1831 if (s == invalid_socket)
1833 ec = boost::asio::error::bad_descriptor;
1834 return socket_error_retval;
1837 #if defined(BOOST_ASIO_WINDOWS) \
1838 || defined(__CYGWIN__) \
1839 || defined(__SYMBIAN32__)
1843 timeval zero_timeout;
1844 zero_timeout.tv_sec = 0;
1845 zero_timeout.tv_usec = 0;
1846 timeval* timeout = (state & user_set_non_blocking) ? &zero_timeout : 0;
1848 int result = error_wrapper(::select(s + 1, 0, &fds, 0, timeout), ec);
1849 #else // defined(BOOST_ASIO_WINDOWS)
1850 // || defined(__CYGWIN__)
1851 // || defined(__SYMBIAN32__)
1854 fds.events = POLLOUT;
1856 int timeout = (state & user_set_non_blocking) ? 0 : -1;
1858 int result = error_wrapper(::poll(&fds, 1, timeout), ec);
1859 #endif // defined(BOOST_ASIO_WINDOWS)
1860 // || defined(__CYGWIN__)
1861 // || defined(__SYMBIAN32__)
1863 ec = (state & user_set_non_blocking)
1864 ? boost::asio::error::would_block : boost::system::error_code();
1865 else if (result > 0)
1866 ec = boost::system::error_code();
1870 int poll_connect(socket_type s, boost::system::error_code& ec)
1872 if (s == invalid_socket)
1874 ec = boost::asio::error::bad_descriptor;
1875 return socket_error_retval;
1878 #if defined(BOOST_ASIO_WINDOWS) \
1879 || defined(__CYGWIN__) \
1880 || defined(__SYMBIAN32__)
1882 FD_ZERO(&write_fds);
1883 FD_SET(s, &write_fds);
1885 FD_ZERO(&except_fds);
1886 FD_SET(s, &except_fds);
1888 int result = error_wrapper(::select(
1889 s + 1, 0, &write_fds, &except_fds, 0), ec);
1891 ec = boost::system::error_code();
1893 #else // defined(BOOST_ASIO_WINDOWS)
1894 // || defined(__CYGWIN__)
1895 // || defined(__SYMBIAN32__)
1898 fds.events = POLLOUT;
1901 int result = error_wrapper(::poll(&fds, 1, -1), ec);
1903 ec = boost::system::error_code();
1905 #endif // defined(BOOST_ASIO_WINDOWS)
1906 // || defined(__CYGWIN__)
1907 // || defined(__SYMBIAN32__)
1910 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
1912 const char* inet_ntop(int af, const void* src, char* dest, size_t length,
1913 unsigned long scope_id, boost::system::error_code& ec)
1916 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
1917 using namespace std; // For sprintf.
1918 const unsigned char* bytes = static_cast<const unsigned char*>(src);
1919 if (af == BOOST_ASIO_OS_DEF(AF_INET))
1921 sprintf_s(dest, length, "%u.%u.%u.%u",
1922 bytes[0], bytes[1], bytes[2], bytes[3]);
1925 else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
1927 size_t n = 0, b = 0, z = 0;
1928 while (n < length && b < 16)
1930 if (bytes[b] == 0 && bytes[b + 1] == 0 && z == 0)
1932 do b += 2; while (b < 16 && bytes[b] == 0 && bytes[b + 1] == 0);
1933 n += sprintf_s(dest + n, length - n, ":%s", b < 16 ? "" : ":"), ++z;
1937 n += sprintf_s(dest + n, length - n, "%s%x", b ? ":" : "",
1938 (static_cast<u_long_type>(bytes[b]) << 8) | bytes[b + 1]);
1943 n += sprintf_s(dest + n, length - n, "%%%lu", scope_id);
1948 ec = boost::asio::error::address_family_not_supported;
1951 #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
1952 using namespace std; // For memcpy.
1954 if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
1956 ec = boost::asio::error::address_family_not_supported;
1962 socket_addr_type base;
1963 sockaddr_storage_type storage;
1964 sockaddr_in4_type v4;
1965 sockaddr_in6_type v6;
1967 DWORD address_length;
1968 if (af == BOOST_ASIO_OS_DEF(AF_INET))
1970 address_length = sizeof(sockaddr_in4_type);
1971 address.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
1972 address.v4.sin_port = 0;
1973 memcpy(&address.v4.sin_addr, src, sizeof(in4_addr_type));
1977 address_length = sizeof(sockaddr_in6_type);
1978 address.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
1979 address.v6.sin6_port = 0;
1980 address.v6.sin6_flowinfo = 0;
1981 address.v6.sin6_scope_id = scope_id;
1982 memcpy(&address.v6.sin6_addr, src, sizeof(in6_addr_type));
1985 DWORD string_length = static_cast<DWORD>(length);
1986 #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
1987 LPWSTR string_buffer = (LPWSTR)_alloca(length * sizeof(WCHAR));
1988 int result = error_wrapper(::WSAAddressToStringW(&address.base,
1989 address_length, 0, string_buffer, &string_length), ec);
1990 ::WideCharToMultiByte(CP_ACP, 0, string_buffer, -1,
1991 dest, static_cast<int>(length), 0, 0);
1993 int result = error_wrapper(::WSAAddressToStringA(
1994 &address.base, address_length, 0, dest, &string_length), ec);
1997 // Windows may set error code on success.
1998 if (result != socket_error_retval)
1999 ec = boost::system::error_code();
2001 // Windows may not set an error code on failure.
2002 else if (result == socket_error_retval && !ec)
2003 ec = boost::asio::error::invalid_argument;
2005 return result == socket_error_retval ? 0 : dest;
2006 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
2007 const char* result = error_wrapper(::inet_ntop(
2008 af, src, dest, static_cast<int>(length)), ec);
2009 if (result == 0 && !ec)
2010 ec = boost::asio::error::invalid_argument;
2011 if (result != 0 && af == BOOST_ASIO_OS_DEF(AF_INET6) && scope_id != 0)
2013 using namespace std; // For strcat and sprintf.
2014 char if_name[IF_NAMESIZE + 1] = "%";
2015 const in6_addr_type* ipv6_address = static_cast<const in6_addr_type*>(src);
2016 bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
2017 && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
2018 bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
2019 && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
2020 if ((!is_link_local && !is_multicast_link_local)
2021 || if_indextoname(static_cast<unsigned>(scope_id), if_name + 1) == 0)
2022 sprintf(if_name + 1, "%lu", scope_id);
2023 strcat(dest, if_name);
2026 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
2029 int inet_pton(int af, const char* src, void* dest,
2030 unsigned long* scope_id, boost::system::error_code& ec)
2033 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
2034 using namespace std; // For sscanf.
2035 unsigned char* bytes = static_cast<unsigned char*>(dest);
2036 if (af == BOOST_ASIO_OS_DEF(AF_INET))
2038 unsigned int b0, b1, b2, b3;
2039 if (sscanf_s(src, "%u.%u.%u.%u", &b0, &b1, &b2, &b3) != 4)
2041 ec = boost::asio::error::invalid_argument;
2044 if (b0 > 255 || b1 > 255 || b2 > 255 || b3 > 255)
2046 ec = boost::asio::error::invalid_argument;
2049 bytes[0] = static_cast<unsigned char>(b0);
2050 bytes[1] = static_cast<unsigned char>(b1);
2051 bytes[2] = static_cast<unsigned char>(b2);
2052 bytes[3] = static_cast<unsigned char>(b3);
2053 ec = boost::system::error_code();
2056 else if (af == BOOST_ASIO_OS_DEF(AF_INET6))
2058 unsigned char* bytes = static_cast<unsigned char*>(dest);
2059 std::memset(bytes, 0, 16);
2060 unsigned char back_bytes[16] = { 0 };
2061 int num_front_bytes = 0, num_back_bytes = 0;
2062 const char* p = src;
2064 enum { fword, fcolon, bword, scope, done } state = fword;
2065 unsigned long current_word = 0;
2066 while (state != done)
2068 if (current_word > 0xFFFF)
2070 ec = boost::asio::error::invalid_argument;
2077 if (*p >= '0' && *p <= '9')
2078 current_word = current_word * 16 + *p++ - '0';
2079 else if (*p >= 'a' && *p <= 'f')
2080 current_word = current_word * 16 + *p++ - 'a' + 10;
2081 else if (*p >= 'A' && *p <= 'F')
2082 current_word = current_word * 16 + *p++ - 'A' + 10;
2085 if (num_front_bytes == 16)
2087 ec = boost::asio::error::invalid_argument;
2091 bytes[num_front_bytes++] = (current_word >> 8) & 0xFF;
2092 bytes[num_front_bytes++] = current_word & 0xFF;
2096 state = fcolon, ++p;
2103 ec = boost::asio::error::invalid_argument;
2117 if (*p >= '0' && *p <= '9')
2118 current_word = current_word * 16 + *p++ - '0';
2119 else if (*p >= 'a' && *p <= 'f')
2120 current_word = current_word * 16 + *p++ - 'a' + 10;
2121 else if (*p >= 'A' && *p <= 'F')
2122 current_word = current_word * 16 + *p++ - 'A' + 10;
2125 if (num_front_bytes + num_back_bytes == 16)
2127 ec = boost::asio::error::invalid_argument;
2131 back_bytes[num_back_bytes++] = (current_word >> 8) & 0xFF;
2132 back_bytes[num_back_bytes++] = current_word & 0xFF;
2143 ec = boost::asio::error::invalid_argument;
2150 if (*p >= '0' && *p <= '9')
2151 current_word = current_word * 10 + *p++ - '0';
2153 *scope_id = current_word, state = done;
2156 ec = boost::asio::error::invalid_argument;
2166 for (int i = 0; i < num_back_bytes; ++i)
2167 bytes[16 - num_back_bytes + i] = back_bytes[i];
2169 ec = boost::system::error_code();
2174 ec = boost::asio::error::address_family_not_supported;
2177 #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
2178 using namespace std; // For memcpy and strcmp.
2180 if (af != BOOST_ASIO_OS_DEF(AF_INET) && af != BOOST_ASIO_OS_DEF(AF_INET6))
2182 ec = boost::asio::error::address_family_not_supported;
2188 socket_addr_type base;
2189 sockaddr_storage_type storage;
2190 sockaddr_in4_type v4;
2191 sockaddr_in6_type v6;
2193 int address_length = sizeof(sockaddr_storage_type);
2194 #if defined(BOOST_NO_ANSI_APIS) || (defined(_MSC_VER) && (_MSC_VER >= 1800))
2195 int num_wide_chars = static_cast<int>(strlen(src)) + 1;
2196 LPWSTR wide_buffer = (LPWSTR)_alloca(num_wide_chars * sizeof(WCHAR));
2197 ::MultiByteToWideChar(CP_ACP, 0, src, -1, wide_buffer, num_wide_chars);
2198 int result = error_wrapper(::WSAStringToAddressW(
2199 wide_buffer, af, 0, &address.base, &address_length), ec);
2201 int result = error_wrapper(::WSAStringToAddressA(
2202 const_cast<char*>(src), af, 0, &address.base, &address_length), ec);
2205 if (af == BOOST_ASIO_OS_DEF(AF_INET))
2207 if (result != socket_error_retval)
2209 memcpy(dest, &address.v4.sin_addr, sizeof(in4_addr_type));
2210 ec = boost::system::error_code();
2212 else if (strcmp(src, "255.255.255.255") == 0)
2214 static_cast<in4_addr_type*>(dest)->s_addr = INADDR_NONE;
2215 ec = boost::system::error_code();
2220 if (result != socket_error_retval)
2222 memcpy(dest, &address.v6.sin6_addr, sizeof(in6_addr_type));
2224 *scope_id = address.v6.sin6_scope_id;
2225 ec = boost::system::error_code();
2229 // Windows may not set an error code on failure.
2230 if (result == socket_error_retval && !ec)
2231 ec = boost::asio::error::invalid_argument;
2233 if (result != socket_error_retval)
2234 ec = boost::system::error_code();
2236 return result == socket_error_retval ? -1 : 1;
2237 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
2238 using namespace std; // For strchr, memcpy and atoi.
2240 // On some platforms, inet_pton fails if an address string contains a scope
2241 // id. Detect and remove the scope id before passing the string to inet_pton.
2242 const bool is_v6 = (af == BOOST_ASIO_OS_DEF(AF_INET6));
2243 const char* if_name = is_v6 ? strchr(src, '%') : 0;
2244 char src_buf[max_addr_v6_str_len + 1];
2245 const char* src_ptr = src;
2248 if (if_name - src > max_addr_v6_str_len)
2250 ec = boost::asio::error::invalid_argument;
2253 memcpy(src_buf, src, if_name - src);
2254 src_buf[if_name - src] = 0;
2258 int result = error_wrapper(::inet_pton(af, src_ptr, dest), ec);
2259 if (result <= 0 && !ec)
2260 ec = boost::asio::error::invalid_argument;
2261 if (result > 0 && is_v6 && scope_id)
2263 using namespace std; // For strchr and atoi.
2267 in6_addr_type* ipv6_address = static_cast<in6_addr_type*>(dest);
2268 bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
2269 && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
2270 bool is_multicast_link_local = ((ipv6_address->s6_addr[0] == 0xff)
2271 && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02));
2272 if (is_link_local || is_multicast_link_local)
2273 *scope_id = if_nametoindex(if_name + 1);
2275 *scope_id = atoi(if_name + 1);
2279 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
2282 int gethostname(char* name, int namelen, boost::system::error_code& ec)
2285 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
2288 using namespace Windows::Foundation::Collections;
2289 using namespace Windows::Networking;
2290 using namespace Windows::Networking::Connectivity;
2291 IVectorView<HostName^>^ hostnames = NetworkInformation::GetHostNames();
2292 for (unsigned i = 0; i < hostnames->Size; ++i)
2294 HostName^ hostname = hostnames->GetAt(i);
2295 if (hostname->Type == HostNameType::DomainName)
2297 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
2298 std::string raw_name = converter.to_bytes(hostname->RawName->Data());
2299 if (namelen > 0 && raw_name.size() < static_cast<std::size_t>(namelen))
2301 strcpy_s(name, namelen, raw_name.c_str());
2308 catch (Platform::Exception^ e)
2310 ec = boost::system::error_code(e->HResult,
2311 boost::system::system_category());
2314 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
2315 int result = error_wrapper(::gethostname(name, namelen), ec);
2316 # if defined(BOOST_ASIO_WINDOWS)
2318 ec = boost::system::error_code();
2319 # endif // defined(BOOST_ASIO_WINDOWS)
2321 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
2324 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
2326 #if !defined(BOOST_ASIO_HAS_GETADDRINFO)
2328 // The following functions are only needed for emulation of getaddrinfo and
2331 inline boost::system::error_code translate_netdb_error(int error)
2336 return boost::system::error_code();
2337 case HOST_NOT_FOUND:
2338 return boost::asio::error::host_not_found;
2340 return boost::asio::error::host_not_found_try_again;
2342 return boost::asio::error::no_recovery;
2344 return boost::asio::error::no_data;
2346 BOOST_ASIO_ASSERT(false);
2347 return boost::asio::error::invalid_argument;
2351 inline hostent* gethostbyaddr(const char* addr, int length, int af,
2352 hostent* result, char* buffer, int buflength, boost::system::error_code& ec)
2355 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
2358 hostent* retval = error_wrapper(::gethostbyaddr(addr, length, af), ec);
2361 ec = boost::system::error_code();
2364 #elif defined(__sun) || defined(__QNX__)
2366 hostent* retval = error_wrapper(::gethostbyaddr_r(addr, length, af, result,
2367 buffer, buflength, &error), ec);
2369 ec = translate_netdb_error(error);
2371 #elif defined(__MACH__) && defined(__APPLE__)
2375 hostent* retval = error_wrapper(::getipnodebyaddr(
2376 addr, length, af, &error), ec);
2378 ec = translate_netdb_error(error);
2384 hostent* retval = 0;
2386 error_wrapper(::gethostbyaddr_r(addr, length, af, result, buffer,
2387 buflength, &retval, &error), ec);
2389 ec = translate_netdb_error(error);
2394 inline hostent* gethostbyname(const char* name, int af, struct hostent* result,
2395 char* buffer, int buflength, int ai_flags, boost::system::error_code& ec)
2398 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
2402 if (af != BOOST_ASIO_OS_DEF(AF_INET))
2404 ec = boost::asio::error::address_family_not_supported;
2407 hostent* retval = error_wrapper(::gethostbyname(name), ec);
2410 ec = boost::system::error_code();
2413 #elif defined(__sun) || defined(__QNX__)
2415 if (af != BOOST_ASIO_OS_DEF(AF_INET))
2417 ec = boost::asio::error::address_family_not_supported;
2421 hostent* retval = error_wrapper(::gethostbyname_r(name, result, buffer,
2422 buflength, &error), ec);
2424 ec = translate_netdb_error(error);
2426 #elif defined(__MACH__) && defined(__APPLE__)
2430 hostent* retval = error_wrapper(::getipnodebyname(
2431 name, af, ai_flags, &error), ec);
2433 ec = translate_netdb_error(error);
2440 if (af != BOOST_ASIO_OS_DEF(AF_INET))
2442 ec = boost::asio::error::address_family_not_supported;
2445 hostent* retval = 0;
2447 error_wrapper(::gethostbyname_r(name, result,
2448 buffer, buflength, &retval, &error), ec);
2450 ec = translate_netdb_error(error);
2455 inline void freehostent(hostent* h)
2457 #if defined(__MACH__) && defined(__APPLE__)
2465 // Emulation of getaddrinfo based on implementation in:
2466 // Stevens, W. R., UNIX Network Programming Vol. 1, 2nd Ed., Prentice-Hall 1998.
2474 inline int gai_nsearch(const char* host,
2475 const addrinfo_type* hints, gai_search (&search)[2])
2477 int search_count = 0;
2478 if (host == 0 || host[0] == '\0')
2480 if (hints->ai_flags & AI_PASSIVE)
2482 // No host and AI_PASSIVE implies wildcard bind.
2483 switch (hints->ai_family)
2485 case BOOST_ASIO_OS_DEF(AF_INET):
2486 search[search_count].host = "0.0.0.0";
2487 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
2490 case BOOST_ASIO_OS_DEF(AF_INET6):
2491 search[search_count].host = "0::0";
2492 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
2495 case BOOST_ASIO_OS_DEF(AF_UNSPEC):
2496 search[search_count].host = "0::0";
2497 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
2499 search[search_count].host = "0.0.0.0";
2500 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
2509 // No host and not AI_PASSIVE means connect to local host.
2510 switch (hints->ai_family)
2512 case BOOST_ASIO_OS_DEF(AF_INET):
2513 search[search_count].host = "localhost";
2514 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
2517 case BOOST_ASIO_OS_DEF(AF_INET6):
2518 search[search_count].host = "localhost";
2519 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
2522 case BOOST_ASIO_OS_DEF(AF_UNSPEC):
2523 search[search_count].host = "localhost";
2524 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
2526 search[search_count].host = "localhost";
2527 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
2537 // Host is specified.
2538 switch (hints->ai_family)
2540 case BOOST_ASIO_OS_DEF(AF_INET):
2541 search[search_count].host = host;
2542 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
2545 case BOOST_ASIO_OS_DEF(AF_INET6):
2546 search[search_count].host = host;
2547 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
2550 case BOOST_ASIO_OS_DEF(AF_UNSPEC):
2551 search[search_count].host = host;
2552 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET6);
2554 search[search_count].host = host;
2555 search[search_count].family = BOOST_ASIO_OS_DEF(AF_INET);
2562 return search_count;
2565 template <typename T>
2566 inline T* gai_alloc(std::size_t size = sizeof(T))
2568 using namespace std;
2569 T* p = static_cast<T*>(::operator new(size, std::nothrow));
2575 inline void gai_free(void* p)
2577 ::operator delete(p);
2580 inline void gai_strcpy(char* target, const char* source, std::size_t max_size)
2582 using namespace std;
2583 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
2584 strcpy_s(target, max_size, source);
2585 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
2588 strncat(target, source, max_size - 1);
2589 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
2592 enum { gai_clone_flag = 1 << 30 };
2594 inline int gai_aistruct(addrinfo_type*** next, const addrinfo_type* hints,
2595 const void* addr, int family)
2597 using namespace std;
2599 addrinfo_type* ai = gai_alloc<addrinfo_type>();
2605 *next = &ai->ai_next;
2607 ai->ai_canonname = 0;
2608 ai->ai_socktype = hints->ai_socktype;
2609 if (ai->ai_socktype == 0)
2610 ai->ai_flags |= gai_clone_flag;
2611 ai->ai_protocol = hints->ai_protocol;
2612 ai->ai_family = family;
2614 switch (ai->ai_family)
2616 case BOOST_ASIO_OS_DEF(AF_INET):
2618 sockaddr_in4_type* sinptr = gai_alloc<sockaddr_in4_type>();
2621 sinptr->sin_family = BOOST_ASIO_OS_DEF(AF_INET);
2622 memcpy(&sinptr->sin_addr, addr, sizeof(in4_addr_type));
2623 ai->ai_addr = reinterpret_cast<sockaddr*>(sinptr);
2624 ai->ai_addrlen = sizeof(sockaddr_in4_type);
2627 case BOOST_ASIO_OS_DEF(AF_INET6):
2629 sockaddr_in6_type* sin6ptr = gai_alloc<sockaddr_in6_type>();
2632 sin6ptr->sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
2633 memcpy(&sin6ptr->sin6_addr, addr, sizeof(in6_addr_type));
2634 ai->ai_addr = reinterpret_cast<sockaddr*>(sin6ptr);
2635 ai->ai_addrlen = sizeof(sockaddr_in6_type);
2645 inline addrinfo_type* gai_clone(addrinfo_type* ai)
2647 using namespace std;
2649 addrinfo_type* new_ai = gai_alloc<addrinfo_type>();
2653 new_ai->ai_next = ai->ai_next;
2654 ai->ai_next = new_ai;
2656 new_ai->ai_flags = 0;
2657 new_ai->ai_family = ai->ai_family;
2658 new_ai->ai_socktype = ai->ai_socktype;
2659 new_ai->ai_protocol = ai->ai_protocol;
2660 new_ai->ai_canonname = 0;
2661 new_ai->ai_addrlen = ai->ai_addrlen;
2662 new_ai->ai_addr = gai_alloc<sockaddr>(ai->ai_addrlen);
2663 memcpy(new_ai->ai_addr, ai->ai_addr, ai->ai_addrlen);
2668 inline int gai_port(addrinfo_type* aihead, int port, int socktype)
2672 for (addrinfo_type* ai = aihead; ai; ai = ai->ai_next)
2674 if (ai->ai_flags & gai_clone_flag)
2676 if (ai->ai_socktype != 0)
2681 // ai now points to newly cloned entry.
2684 else if (ai->ai_socktype != socktype)
2686 // Ignore if mismatch on socket type.
2690 ai->ai_socktype = socktype;
2692 switch (ai->ai_family)
2694 case BOOST_ASIO_OS_DEF(AF_INET):
2696 sockaddr_in4_type* sinptr =
2697 reinterpret_cast<sockaddr_in4_type*>(ai->ai_addr);
2698 sinptr->sin_port = port;
2702 case BOOST_ASIO_OS_DEF(AF_INET6):
2704 sockaddr_in6_type* sin6ptr =
2705 reinterpret_cast<sockaddr_in6_type*>(ai->ai_addr);
2706 sin6ptr->sin6_port = port;
2718 inline int gai_serv(addrinfo_type* aihead,
2719 const addrinfo_type* hints, const char* serv)
2721 using namespace std;
2726 #if defined(AI_NUMERICSERV)
2727 (hints->ai_flags & AI_NUMERICSERV) ||
2729 isdigit(static_cast<unsigned char>(serv[0])))
2731 int port = htons(atoi(serv));
2732 if (hints->ai_socktype)
2734 // Caller specifies socket type.
2735 int rc = gai_port(aihead, port, hints->ai_socktype);
2742 // Caller does not specify socket type.
2743 int rc = gai_port(aihead, port, SOCK_STREAM);
2747 rc = gai_port(aihead, port, SOCK_DGRAM);
2755 // Try service name with TCP first, then UDP.
2756 if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_STREAM)
2758 servent* sptr = getservbyname(serv, "tcp");
2761 int rc = gai_port(aihead, sptr->s_port, SOCK_STREAM);
2767 if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_DGRAM)
2769 servent* sptr = getservbyname(serv, "udp");
2772 int rc = gai_port(aihead, sptr->s_port, SOCK_DGRAM);
2782 if (hints->ai_socktype == 0)
2784 // All calls to getservbyname() failed.
2789 // Service not supported for socket type.
2797 inline int gai_echeck(const char* host, const char* service,
2798 int flags, int family, int socktype, int protocol)
2803 // Host or service must be specified.
2804 if (host == 0 || host[0] == '\0')
2805 if (service == 0 || service[0] == '\0')
2808 // Check combination of family and socket type.
2811 case BOOST_ASIO_OS_DEF(AF_UNSPEC):
2813 case BOOST_ASIO_OS_DEF(AF_INET):
2814 case BOOST_ASIO_OS_DEF(AF_INET6):
2815 if (service != 0 && service[0] != '\0')
2816 if (socktype != 0 && socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
2817 return EAI_SOCKTYPE;
2826 inline void freeaddrinfo_emulation(addrinfo_type* aihead)
2828 addrinfo_type* ai = aihead;
2831 gai_free(ai->ai_addr);
2832 gai_free(ai->ai_canonname);
2833 addrinfo_type* ainext = ai->ai_next;
2839 inline int getaddrinfo_emulation(const char* host, const char* service,
2840 const addrinfo_type* hintsp, addrinfo_type** result)
2842 // Set up linked list of addrinfo structures.
2843 addrinfo_type* aihead = 0;
2844 addrinfo_type** ainext = &aihead;
2847 // Supply default hints if not specified by caller.
2848 addrinfo_type hints = addrinfo_type();
2849 hints.ai_family = BOOST_ASIO_OS_DEF(AF_UNSPEC);
2853 // If the resolution is not specifically for AF_INET6, remove the AI_V4MAPPED
2854 // and AI_ALL flags.
2855 #if defined(AI_V4MAPPED)
2856 if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
2857 hints.ai_flags &= ~AI_V4MAPPED;
2860 if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
2861 hints.ai_flags &= ~AI_ALL;
2864 // Basic error checking.
2865 int rc = gai_echeck(host, service, hints.ai_flags, hints.ai_family,
2866 hints.ai_socktype, hints.ai_protocol);
2869 freeaddrinfo_emulation(aihead);
2873 gai_search search[2];
2874 int search_count = gai_nsearch(host, &hints, search);
2875 for (gai_search* sptr = search; sptr < search + search_count; ++sptr)
2877 // Check for IPv4 dotted decimal string.
2878 in4_addr_type inaddr;
2879 boost::system::error_code ec;
2880 if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET),
2881 sptr->host, &inaddr, 0, ec) == 1)
2883 if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
2884 && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET))
2886 freeaddrinfo_emulation(aihead);
2890 if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET))
2892 rc = gai_aistruct(&ainext, &hints, &inaddr, BOOST_ASIO_OS_DEF(AF_INET));
2895 freeaddrinfo_emulation(aihead);
2903 // Check for IPv6 hex string.
2904 in6_addr_type in6addr;
2905 if (socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET6),
2906 sptr->host, &in6addr, 0, ec) == 1)
2908 if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
2909 && hints.ai_family != BOOST_ASIO_OS_DEF(AF_INET6))
2911 freeaddrinfo_emulation(aihead);
2915 if (sptr->family == BOOST_ASIO_OS_DEF(AF_INET6))
2917 rc = gai_aistruct(&ainext, &hints, &in6addr,
2918 BOOST_ASIO_OS_DEF(AF_INET6));
2921 freeaddrinfo_emulation(aihead);
2929 // Look up hostname.
2931 char hbuf[8192] = "";
2932 hostent* hptr = socket_ops::gethostbyname(sptr->host,
2933 sptr->family, &hent, hbuf, sizeof(hbuf), hints.ai_flags, ec);
2936 if (search_count == 2)
2938 // Failure is OK if there are multiple searches.
2941 freeaddrinfo_emulation(aihead);
2943 if (ec == boost::asio::error::host_not_found)
2945 if (ec == boost::asio::error::host_not_found_try_again)
2947 if (ec == boost::asio::error::no_recovery)
2949 if (ec == boost::asio::error::no_data)
2954 // Check for address family mismatch if one was specified.
2955 if (hints.ai_family != BOOST_ASIO_OS_DEF(AF_UNSPEC)
2956 && hints.ai_family != hptr->h_addrtype)
2958 freeaddrinfo_emulation(aihead);
2960 socket_ops::freehostent(hptr);
2964 // Save canonical name first time.
2965 if (host != 0 && host[0] != '\0' && hptr->h_name && hptr->h_name[0]
2966 && (hints.ai_flags & AI_CANONNAME) && canon == 0)
2968 std::size_t canon_len = strlen(hptr->h_name) + 1;
2969 canon = gai_alloc<char>(canon_len);
2972 freeaddrinfo_emulation(aihead);
2973 socket_ops::freehostent(hptr);
2976 gai_strcpy(canon, hptr->h_name, canon_len);
2979 // Create an addrinfo structure for each returned address.
2980 for (char** ap = hptr->h_addr_list; *ap; ++ap)
2982 rc = gai_aistruct(&ainext, &hints, *ap, hptr->h_addrtype);
2985 freeaddrinfo_emulation(aihead);
2987 socket_ops::freehostent(hptr);
2992 socket_ops::freehostent(hptr);
2995 // Check if we found anything.
3002 // Return canonical name in first entry.
3003 if (host != 0 && host[0] != '\0' && (hints.ai_flags & AI_CANONNAME))
3007 aihead->ai_canonname = canon;
3012 std::size_t canonname_len = strlen(search[0].host) + 1;
3013 aihead->ai_canonname = gai_alloc<char>(canonname_len);
3014 if (aihead->ai_canonname == 0)
3016 freeaddrinfo_emulation(aihead);
3019 gai_strcpy(aihead->ai_canonname, search[0].host, canonname_len);
3024 // Process the service name.
3025 if (service != 0 && service[0] != '\0')
3027 rc = gai_serv(aihead, &hints, service);
3030 freeaddrinfo_emulation(aihead);
3035 // Return result to caller.
3040 inline boost::system::error_code getnameinfo_emulation(
3041 const socket_addr_type* sa, std::size_t salen, char* host,
3042 std::size_t hostlen, char* serv, std::size_t servlen, int flags,
3043 boost::system::error_code& ec)
3045 using namespace std;
3049 unsigned short port;
3050 switch (sa->sa_family)
3052 case BOOST_ASIO_OS_DEF(AF_INET):
3053 if (salen != sizeof(sockaddr_in4_type))
3055 return ec = boost::asio::error::invalid_argument;
3057 addr = reinterpret_cast<const char*>(
3058 &reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_addr);
3059 addr_len = sizeof(in4_addr_type);
3060 port = reinterpret_cast<const sockaddr_in4_type*>(sa)->sin_port;
3062 case BOOST_ASIO_OS_DEF(AF_INET6):
3063 if (salen != sizeof(sockaddr_in6_type))
3065 return ec = boost::asio::error::invalid_argument;
3067 addr = reinterpret_cast<const char*>(
3068 &reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_addr);
3069 addr_len = sizeof(in6_addr_type);
3070 port = reinterpret_cast<const sockaddr_in6_type*>(sa)->sin6_port;
3073 return ec = boost::asio::error::address_family_not_supported;
3076 if (host && hostlen > 0)
3078 if (flags & NI_NUMERICHOST)
3080 if (socket_ops::inet_ntop(sa->sa_family, addr, host, hostlen, 0, ec) == 0)
3088 char hbuf[8192] = "";
3089 hostent* hptr = socket_ops::gethostbyaddr(addr,
3090 static_cast<int>(addr_len), sa->sa_family,
3091 &hent, hbuf, sizeof(hbuf), ec);
3092 if (hptr && hptr->h_name && hptr->h_name[0] != '\0')
3094 if (flags & NI_NOFQDN)
3096 char* dot = strchr(hptr->h_name, '.');
3102 gai_strcpy(host, hptr->h_name, hostlen);
3103 socket_ops::freehostent(hptr);
3107 socket_ops::freehostent(hptr);
3108 if (flags & NI_NAMEREQD)
3110 return ec = boost::asio::error::host_not_found;
3112 if (socket_ops::inet_ntop(sa->sa_family,
3113 addr, host, hostlen, 0, ec) == 0)
3121 if (serv && servlen > 0)
3123 if (flags & NI_NUMERICSERV)
3127 return ec = boost::asio::error::no_buffer_space;
3129 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
3130 sprintf_s(serv, servlen, "%u", ntohs(port));
3131 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
3132 sprintf(serv, "%u", ntohs(port));
3133 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
3137 #if defined(BOOST_ASIO_HAS_PTHREADS)
3138 static ::pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
3139 ::pthread_mutex_lock(&mutex);
3140 #endif // defined(BOOST_ASIO_HAS_PTHREADS)
3141 servent* sptr = ::getservbyport(port, (flags & NI_DGRAM) ? "udp" : 0);
3142 if (sptr && sptr->s_name && sptr->s_name[0] != '\0')
3144 gai_strcpy(serv, sptr->s_name, servlen);
3150 return ec = boost::asio::error::no_buffer_space;
3152 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
3153 sprintf_s(serv, servlen, "%u", ntohs(port));
3154 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
3155 sprintf(serv, "%u", ntohs(port));
3156 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
3158 #if defined(BOOST_ASIO_HAS_PTHREADS)
3159 ::pthread_mutex_unlock(&mutex);
3160 #endif // defined(BOOST_ASIO_HAS_PTHREADS)
3164 ec = boost::system::error_code();
3168 #endif // !defined(BOOST_ASIO_HAS_GETADDRINFO)
3170 inline boost::system::error_code translate_addrinfo_error(int error)
3175 return boost::system::error_code();
3177 return boost::asio::error::host_not_found_try_again;
3179 return boost::asio::error::invalid_argument;
3181 return boost::asio::error::no_recovery;
3183 return boost::asio::error::address_family_not_supported;
3185 return boost::asio::error::no_memory;
3187 #if defined(EAI_ADDRFAMILY)
3188 case EAI_ADDRFAMILY:
3190 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
3193 return boost::asio::error::host_not_found;
3195 return boost::asio::error::service_not_found;
3197 return boost::asio::error::socket_type_not_supported;
3198 default: // Possibly the non-portable EAI_SYSTEM.
3199 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
3200 return boost::system::error_code(
3201 WSAGetLastError(), boost::asio::error::get_system_category());
3203 return boost::system::error_code(
3204 errno, boost::asio::error::get_system_category());
3209 boost::system::error_code getaddrinfo(const char* host,
3210 const char* service, const addrinfo_type& hints,
3211 addrinfo_type** result, boost::system::error_code& ec)
3213 host = (host && *host) ? host : 0;
3214 service = (service && *service) ? service : 0;
3216 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
3217 # if defined(BOOST_ASIO_HAS_GETADDRINFO)
3218 // Building for Windows XP, Windows Server 2003, or later.
3219 int error = ::getaddrinfo(host, service, &hints, result);
3220 return ec = translate_addrinfo_error(error);
3222 // Building for Windows 2000 or earlier.
3223 typedef int (WSAAPI *gai_t)(const char*,
3224 const char*, const addrinfo_type*, addrinfo_type**);
3225 if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
3227 if (gai_t gai = (gai_t)::GetProcAddress(winsock_module, "getaddrinfo"))
3229 int error = gai(host, service, &hints, result);
3230 return ec = translate_addrinfo_error(error);
3233 int error = getaddrinfo_emulation(host, service, &hints, result);
3234 return ec = translate_addrinfo_error(error);
3236 #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
3237 int error = getaddrinfo_emulation(host, service, &hints, result);
3238 return ec = translate_addrinfo_error(error);
3240 int error = ::getaddrinfo(host, service, &hints, result);
3241 return ec = translate_addrinfo_error(error);
3245 boost::system::error_code background_getaddrinfo(
3246 const weak_cancel_token_type& cancel_token, const char* host,
3247 const char* service, const addrinfo_type& hints,
3248 addrinfo_type** result, boost::system::error_code& ec)
3250 if (cancel_token.expired())
3251 ec = boost::asio::error::operation_aborted;
3253 socket_ops::getaddrinfo(host, service, hints, result, ec);
3257 void freeaddrinfo(addrinfo_type* ai)
3259 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
3260 # if defined(BOOST_ASIO_HAS_GETADDRINFO)
3261 // Building for Windows XP, Windows Server 2003, or later.
3264 // Building for Windows 2000 or earlier.
3265 typedef int (WSAAPI *fai_t)(addrinfo_type*);
3266 if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
3268 if (fai_t fai = (fai_t)::GetProcAddress(winsock_module, "freeaddrinfo"))
3274 freeaddrinfo_emulation(ai);
3276 #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
3277 freeaddrinfo_emulation(ai);
3283 boost::system::error_code getnameinfo(const socket_addr_type* addr,
3284 std::size_t addrlen, char* host, std::size_t hostlen,
3285 char* serv, std::size_t servlen, int flags, boost::system::error_code& ec)
3287 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
3288 # if defined(BOOST_ASIO_HAS_GETADDRINFO)
3289 // Building for Windows XP, Windows Server 2003, or later.
3291 int error = ::getnameinfo(addr, static_cast<socklen_t>(addrlen),
3292 host, static_cast<DWORD>(hostlen),
3293 serv, static_cast<DWORD>(servlen), flags);
3294 return ec = translate_addrinfo_error(error);
3296 // Building for Windows 2000 or earlier.
3297 typedef int (WSAAPI *gni_t)(const socket_addr_type*,
3298 int, char*, DWORD, char*, DWORD, int);
3299 if (HMODULE winsock_module = ::GetModuleHandleA("ws2_32"))
3301 if (gni_t gni = (gni_t)::GetProcAddress(winsock_module, "getnameinfo"))
3304 int error = gni(addr, static_cast<int>(addrlen),
3305 host, static_cast<DWORD>(hostlen),
3306 serv, static_cast<DWORD>(servlen), flags);
3307 return ec = translate_addrinfo_error(error);
3311 return getnameinfo_emulation(addr, addrlen,
3312 host, hostlen, serv, servlen, flags, ec);
3314 #elif !defined(BOOST_ASIO_HAS_GETADDRINFO)
3315 using namespace std; // For memcpy.
3316 sockaddr_storage_type tmp_addr;
3317 memcpy(&tmp_addr, addr, addrlen);
3318 tmp_addr.ss_len = addrlen;
3319 addr = reinterpret_cast<socket_addr_type*>(&tmp_addr);
3321 return getnameinfo_emulation(addr, addrlen,
3322 host, hostlen, serv, servlen, flags, ec);
3325 int error = ::getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags);
3326 return ec = translate_addrinfo_error(error);
3330 boost::system::error_code sync_getnameinfo(
3331 const socket_addr_type* addr, std::size_t addrlen,
3332 char* host, std::size_t hostlen, char* serv,
3333 std::size_t servlen, int sock_type, boost::system::error_code& ec)
3335 // First try resolving with the service name. If that fails try resolving
3336 // but allow the service to be returned as a number.
3337 int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
3338 socket_ops::getnameinfo(addr, addrlen, host,
3339 hostlen, serv, servlen, flags, ec);
3342 socket_ops::getnameinfo(addr, addrlen, host, hostlen,
3343 serv, servlen, flags | NI_NUMERICSERV, ec);
3349 boost::system::error_code background_getnameinfo(
3350 const weak_cancel_token_type& cancel_token,
3351 const socket_addr_type* addr, std::size_t addrlen,
3352 char* host, std::size_t hostlen, char* serv,
3353 std::size_t servlen, int sock_type, boost::system::error_code& ec)
3355 if (cancel_token.expired())
3357 ec = boost::asio::error::operation_aborted;
3361 // First try resolving with the service name. If that fails try resolving
3362 // but allow the service to be returned as a number.
3363 int flags = (sock_type == SOCK_DGRAM) ? NI_DGRAM : 0;
3364 socket_ops::getnameinfo(addr, addrlen, host,
3365 hostlen, serv, servlen, flags, ec);
3368 socket_ops::getnameinfo(addr, addrlen, host, hostlen,
3369 serv, servlen, flags | NI_NUMERICSERV, ec);
3376 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
3378 u_long_type network_to_host_long(u_long_type value)
3380 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
3381 unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
3382 u_long_type result = (static_cast<u_long_type>(value_p[0]) << 24)
3383 | (static_cast<u_long_type>(value_p[1]) << 16)
3384 | (static_cast<u_long_type>(value_p[2]) << 8)
3385 | static_cast<u_long_type>(value_p[3]);
3387 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
3388 return ntohl(value);
3389 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
3392 u_long_type host_to_network_long(u_long_type value)
3394 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
3396 unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
3397 result_p[0] = static_cast<unsigned char>((value >> 24) & 0xFF);
3398 result_p[1] = static_cast<unsigned char>((value >> 16) & 0xFF);
3399 result_p[2] = static_cast<unsigned char>((value >> 8) & 0xFF);
3400 result_p[3] = static_cast<unsigned char>(value & 0xFF);
3402 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
3403 return htonl(value);
3404 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
3407 u_short_type network_to_host_short(u_short_type value)
3409 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
3410 unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
3411 u_short_type result = (static_cast<u_short_type>(value_p[0]) << 8)
3412 | static_cast<u_short_type>(value_p[1]);
3414 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
3415 return ntohs(value);
3416 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
3419 u_short_type host_to_network_short(u_short_type value)
3421 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
3422 u_short_type result;
3423 unsigned char* result_p = reinterpret_cast<unsigned char*>(&result);
3424 result_p[0] = static_cast<unsigned char>((value >> 8) & 0xFF);
3425 result_p[1] = static_cast<unsigned char>(value & 0xFF);
3427 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
3428 return htons(value);
3429 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
3432 } // namespace socket_ops
3433 } // namespace detail
3435 } // namespace boost
3437 #include <boost/asio/detail/pop_options.hpp>
3439 #endif // BOOST_ASIO_DETAIL_SOCKET_OPS_IPP