summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '3rdParty/Boost/src/boost/asio/detail/impl/socket_ops.ipp')
-rw-r--r--3rdParty/Boost/src/boost/asio/detail/impl/socket_ops.ipp304
1 files changed, 251 insertions, 53 deletions
diff --git a/3rdParty/Boost/src/boost/asio/detail/impl/socket_ops.ipp b/3rdParty/Boost/src/boost/asio/detail/impl/socket_ops.ipp
index e240acd..16e9536 100644
--- a/3rdParty/Boost/src/boost/asio/detail/impl/socket_ops.ipp
+++ b/3rdParty/Boost/src/boost/asio/detail/impl/socket_ops.ipp
@@ -2,7 +2,7 @@
// detail/impl/socket_ops.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
-// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -18,6 +18,7 @@
#include <boost/asio/detail/config.hpp>
#include <boost/assert.hpp>
#include <boost/detail/workaround.hpp>
+#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -148,7 +149,7 @@ socket_type sync_accept(socket_type s, state_type state,
return invalid_socket;
// Wait for socket to become ready.
- if (socket_ops::poll_read(s, ec) < 0)
+ if (socket_ops::poll_read(s, 0, ec) < 0)
return invalid_socket;
}
}
@@ -278,28 +279,9 @@ int close(socket_type s, state_type& state,
int result = 0;
if (s != invalid_socket)
{
-#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
- if ((state & non_blocking) && (state & user_set_linger))
- {
- ioctl_arg_type arg = 0;
- ::ioctlsocket(s, FIONBIO, &arg);
- state &= ~non_blocking;
- }
-#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
- if (state & non_blocking)
- {
-#if defined(__SYMBIAN32__)
- int flags = ::fcntl(s, F_GETFL, 0);
- if (flags >= 0)
- ::fcntl(s, F_SETFL, flags & ~O_NONBLOCK);
-#else // defined(__SYMBIAN32__)
- ioctl_arg_type arg = 0;
- ::ioctl(s, FIONBIO, &arg);
-#endif // defined(__SYMBIAN32__)
- state &= ~non_blocking;
- }
-#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
-
+ // We don't want the destructor to block, so set the socket to linger in
+ // the background. If the user doesn't like this behaviour then they need
+ // to explicitly close the socket.
if (destruction && (state & user_set_linger))
{
::linger opt;
@@ -316,6 +298,39 @@ int close(socket_type s, state_type& state,
#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
result = error_wrapper(::close(s), ec);
#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+
+ if (result != 0
+ && (ec == boost::asio::error::would_block
+ || ec == boost::asio::error::try_again))
+ {
+ // According to UNIX Network Programming Vol. 1, it is possible for
+ // close() to fail with EWOULDBLOCK under certain circumstances. What
+ // isn't clear is the state of the descriptor after this error. The one
+ // current OS where this behaviour is seen, Windows, says that the socket
+ // remains open. Therefore we'll put the descriptor back into blocking
+ // mode and have another attempt at closing it.
+#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ ioctl_arg_type arg = 0;
+ ::ioctlsocket(s, FIONBIO, &arg);
+#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+# if defined(__SYMBIAN32__)
+ int flags = ::fcntl(s, F_GETFL, 0);
+ if (flags >= 0)
+ ::fcntl(s, F_SETFL, flags & ~O_NONBLOCK);
+# else // defined(__SYMBIAN32__)
+ ioctl_arg_type arg = 0;
+ ::ioctl(s, FIONBIO, &arg);
+# endif // defined(__SYMBIAN32__)
+#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ state &= ~non_blocking;
+
+ clear_last_error();
+#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ result = error_wrapper(::closesocket(s), ec);
+#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ result = error_wrapper(::close(s), ec);
+#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ }
}
if (result == 0)
@@ -323,8 +338,52 @@ int close(socket_type s, state_type& state,
return result;
}
+bool set_user_non_blocking(socket_type s,
+ state_type& state, bool value, boost::system::error_code& ec)
+{
+ if (s == invalid_socket)
+ {
+ ec = boost::asio::error::bad_descriptor;
+ return false;
+ }
+
+ clear_last_error();
+#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ ioctl_arg_type arg = (value ? 1 : 0);
+ int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
+#elif defined(__SYMBIAN32__)
+ int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
+ if (result >= 0)
+ {
+ clear_last_error();
+ int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
+ result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
+ }
+#else
+ ioctl_arg_type arg = (value ? 1 : 0);
+ int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
+#endif
+
+ if (result >= 0)
+ {
+ ec = boost::system::error_code();
+ if (value)
+ state |= user_set_non_blocking;
+ else
+ {
+ // Clearing the user-set non-blocking mode always overrides any
+ // internally-set non-blocking flag. Any subsequent asynchronous
+ // operations will need to re-enable non-blocking I/O.
+ state &= ~(user_set_non_blocking | internal_non_blocking);
+ }
+ return true;
+ }
+
+ return false;
+}
+
bool set_internal_non_blocking(socket_type s,
- state_type& state, boost::system::error_code& ec)
+ state_type& state, bool value, boost::system::error_code& ec)
{
if (s == invalid_socket)
{
@@ -332,26 +391,39 @@ bool set_internal_non_blocking(socket_type s,
return false;
}
+ if (!value && (state & user_set_non_blocking))
+ {
+ // It does not make sense to clear the internal non-blocking flag if the
+ // user still wants non-blocking behaviour. Return an error and let the
+ // caller figure out whether to update the user-set non-blocking flag.
+ ec = boost::asio::error::invalid_argument;
+ return false;
+ }
+
clear_last_error();
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
- ioctl_arg_type arg = 1;
+ ioctl_arg_type arg = (value ? 1 : 0);
int result = error_wrapper(::ioctlsocket(s, FIONBIO, &arg), ec);
#elif defined(__SYMBIAN32__)
int result = error_wrapper(::fcntl(s, F_GETFL, 0), ec);
if (result >= 0)
{
clear_last_error();
- result = error_wrapper(::fcntl(s, F_SETFL, result | O_NONBLOCK), ec);
+ int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
+ result = error_wrapper(::fcntl(s, F_SETFL, flag), ec);
}
#else
- ioctl_arg_type arg = 1;
+ ioctl_arg_type arg = (value ? 1 : 0);
int result = error_wrapper(::ioctl(s, FIONBIO, &arg), ec);
#endif
if (result >= 0)
{
ec = boost::system::error_code();
- state |= internal_non_blocking;
+ if (value)
+ state |= internal_non_blocking;
+ else
+ state &= ~internal_non_blocking;
return true;
}
@@ -394,6 +466,10 @@ int connect(socket_type s, const socket_addr_type* addr,
&msghdr::msg_namelen, s, addr, addrlen), ec);
if (result == 0)
ec = boost::system::error_code();
+#if defined(__linux__)
+ else if (ec == boost::asio::error::try_again)
+ ec = boost::asio::error::no_buffer_space;
+#endif // defined(__linux__)
return result;
}
@@ -664,7 +740,7 @@ size_t sync_recv(socket_type s, state_type state, buf* bufs,
return 0;
// Wait for socket to become ready.
- if (socket_ops::poll_read(s, ec) < 0)
+ if (socket_ops::poll_read(s, 0, ec) < 0)
return 0;
}
}
@@ -802,7 +878,7 @@ size_t sync_recvfrom(socket_type s, state_type state, buf* bufs,
return 0;
// Wait for socket to become ready.
- if (socket_ops::poll_read(s, ec) < 0)
+ if (socket_ops::poll_read(s, 0, ec) < 0)
return 0;
}
}
@@ -863,6 +939,116 @@ bool non_blocking_recvfrom(socket_type s,
#endif // defined(BOOST_ASIO_HAS_IOCP)
+int recvmsg(socket_type s, buf* bufs, size_t count,
+ int in_flags, int& out_flags, boost::system::error_code& ec)
+{
+ clear_last_error();
+#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ out_flags = 0;
+ return socket_ops::recv(s, bufs, count, in_flags, ec);
+#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ msghdr msg = msghdr();
+ msg.msg_iov = bufs;
+ msg.msg_iovlen = count;
+ int result = error_wrapper(::recvmsg(s, &msg, in_flags), ec);
+ if (result >= 0)
+ {
+ ec = boost::system::error_code();
+ out_flags = msg.msg_flags;
+ }
+ else
+ out_flags = 0;
+ return result;
+#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+}
+
+size_t sync_recvmsg(socket_type s, state_type state,
+ buf* bufs, size_t count, int in_flags, int& out_flags,
+ boost::system::error_code& ec)
+{
+ if (s == invalid_socket)
+ {
+ ec = boost::asio::error::bad_descriptor;
+ return 0;
+ }
+
+ // Read some data.
+ for (;;)
+ {
+ // Try to complete the operation without blocking.
+ int bytes = socket_ops::recvmsg(s, bufs, count, in_flags, out_flags, ec);
+
+ // Check if operation succeeded.
+ if (bytes >= 0)
+ return bytes;
+
+ // Operation failed.
+ if ((state & user_set_non_blocking)
+ || (ec != boost::asio::error::would_block
+ && ec != boost::asio::error::try_again))
+ return 0;
+
+ // Wait for socket to become ready.
+ if (socket_ops::poll_read(s, 0, ec) < 0)
+ return 0;
+ }
+}
+
+#if defined(BOOST_ASIO_HAS_IOCP)
+
+void complete_iocp_recvmsg(
+ const weak_cancel_token_type& cancel_token,
+ boost::system::error_code& ec)
+{
+ // Map non-portable errors to their portable counterparts.
+ if (ec.value() == ERROR_NETNAME_DELETED)
+ {
+ if (cancel_token.expired())
+ ec = boost::asio::error::operation_aborted;
+ else
+ ec = boost::asio::error::connection_reset;
+ }
+ else if (ec.value() == ERROR_PORT_UNREACHABLE)
+ {
+ ec = boost::asio::error::connection_refused;
+ }
+}
+
+#else // defined(BOOST_ASIO_HAS_IOCP)
+
+bool non_blocking_recvmsg(socket_type s,
+ buf* bufs, size_t count, int in_flags, int& out_flags,
+ boost::system::error_code& ec, size_t& bytes_transferred)
+{
+ for (;;)
+ {
+ // Read some data.
+ int bytes = socket_ops::recvmsg(s, bufs, count, in_flags, out_flags, ec);
+
+ // Retry operation if interrupted by signal.
+ if (ec == boost::asio::error::interrupted)
+ continue;
+
+ // Check if we need to run the operation again.
+ if (ec == boost::asio::error::would_block
+ || ec == boost::asio::error::try_again)
+ return false;
+
+ // Operation is complete.
+ if (bytes >= 0)
+ {
+ ec = boost::system::error_code();
+ bytes_transferred = bytes;
+ }
+ else
+ bytes_transferred = 0;
+
+ return true;
+ }
+}
+
+#endif // defined(BOOST_ASIO_HAS_IOCP)
+
int send(socket_type s, const buf* bufs, size_t count, int flags,
boost::system::error_code& ec)
{
@@ -929,7 +1115,7 @@ size_t sync_send(socket_type s, state_type state, const buf* bufs,
return 0;
// Wait for socket to become ready.
- if (socket_ops::poll_write(s, ec) < 0)
+ if (socket_ops::poll_write(s, 0, ec) < 0)
return 0;
}
}
@@ -1052,7 +1238,7 @@ size_t sync_sendto(socket_type s, state_type state, const buf* bufs,
return 0;
// Wait for socket to become ready.
- if (socket_ops::poll_write(s, ec) < 0)
+ if (socket_ops::poll_write(s, 0, ec) < 0)
return 0;
}
}
@@ -1502,7 +1688,7 @@ int select(int nfds, fd_set* readfds, fd_set* writefds,
#endif
}
-int poll_read(socket_type s, boost::system::error_code& ec)
+int poll_read(socket_type s, state_type state, boost::system::error_code& ec)
{
if (s == invalid_socket)
{
@@ -1516,11 +1702,12 @@ int poll_read(socket_type s, boost::system::error_code& ec)
fd_set fds;
FD_ZERO(&fds);
FD_SET(s, &fds);
+ timeval zero_timeout;
+ zero_timeout.tv_sec = 0;
+ zero_timeout.tv_usec = 0;
+ timeval* timeout = (state & user_set_non_blocking) ? &zero_timeout : 0;
clear_last_error();
- int result = error_wrapper(::select(s, &fds, 0, 0, 0), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
+ int result = error_wrapper(::select(s, &fds, 0, 0, timeout), ec);
#else // defined(BOOST_WINDOWS)
// || defined(__CYGWIN__)
// || defined(__SYMBIAN32__)
@@ -1528,17 +1715,21 @@ int poll_read(socket_type s, boost::system::error_code& ec)
fds.fd = s;
fds.events = POLLIN;
fds.revents = 0;
+ int timeout = (state & user_set_non_blocking) ? 0 : -1;
clear_last_error();
- int result = error_wrapper(::poll(&fds, 1, -1), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
+ int result = error_wrapper(::poll(&fds, 1, timeout), ec);
#endif // defined(BOOST_WINDOWS)
// || defined(__CYGWIN__)
// || defined(__SYMBIAN32__)
+ if (result == 0)
+ ec = (state & user_set_non_blocking)
+ ? boost::asio::error::would_block : boost::system::error_code();
+ else if (result > 0)
+ ec = boost::system::error_code();
+ return result;
}
-int poll_write(socket_type s, boost::system::error_code& ec)
+int poll_write(socket_type s, state_type state, boost::system::error_code& ec)
{
if (s == invalid_socket)
{
@@ -1552,11 +1743,12 @@ int poll_write(socket_type s, boost::system::error_code& ec)
fd_set fds;
FD_ZERO(&fds);
FD_SET(s, &fds);
+ timeval zero_timeout;
+ zero_timeout.tv_sec = 0;
+ zero_timeout.tv_usec = 0;
+ timeval* timeout = (state & user_set_non_blocking) ? &zero_timeout : 0;
clear_last_error();
- int result = error_wrapper(::select(s, 0, &fds, 0, 0), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
+ int result = error_wrapper(::select(s, 0, &fds, 0, timeout), ec);
#else // defined(BOOST_WINDOWS)
// || defined(__CYGWIN__)
// || defined(__SYMBIAN32__)
@@ -1564,14 +1756,18 @@ int poll_write(socket_type s, boost::system::error_code& ec)
fds.fd = s;
fds.events = POLLOUT;
fds.revents = 0;
+ int timeout = (state & user_set_non_blocking) ? 0 : -1;
clear_last_error();
- int result = error_wrapper(::poll(&fds, 1, -1), ec);
- if (result >= 0)
- ec = boost::system::error_code();
- return result;
+ int result = error_wrapper(::poll(&fds, 1, timeout), ec);
#endif // defined(BOOST_WINDOWS)
// || defined(__CYGWIN__)
// || defined(__SYMBIAN32__)
+ if (result == 0)
+ ec = (state & user_set_non_blocking)
+ ? boost::asio::error::would_block : boost::system::error_code();
+ else if (result > 0)
+ ec = boost::system::error_code();
+ return result;
}
int poll_connect(socket_type s, boost::system::error_code& ec)
@@ -1680,7 +1876,8 @@ const char* inet_ntop(int af, const void* src, char* dest, size_t length,
using namespace std; // For strcat and sprintf.
char if_name[IF_NAMESIZE + 1] = "%";
const in6_addr_type* ipv6_address = static_cast<const in6_addr_type*>(src);
- bool is_link_local = IN6_IS_ADDR_LINKLOCAL(ipv6_address);
+ bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
+ && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
if (!is_link_local || if_indextoname(scope_id, if_name + 1) == 0)
sprintf(if_name + 1, "%lu", scope_id);
strcat(dest, if_name);
@@ -1764,7 +1961,8 @@ int inet_pton(int af, const char* src, void* dest,
if (const char* if_name = strchr(src, '%'))
{
in6_addr_type* ipv6_address = static_cast<in6_addr_type*>(dest);
- bool is_link_local = IN6_IS_ADDR_LINKLOCAL(ipv6_address);
+ bool is_link_local = ((ipv6_address->s6_addr[0] == 0xfe)
+ && ((ipv6_address->s6_addr[1] & 0xc0) == 0x80));
if (is_link_local)
*scope_id = if_nametoindex(if_name + 1);
if (*scope_id == 0)