2015-12-07 18:19:45 +06:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2015-12-07 18:19:45 +06:00
|
|
|
|
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
|
|
|
|
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-12-10 16:44:51 +03:00
|
|
|
#include "precompiled.hpp"
|
|
|
|
|
2015-12-07 18:19:45 +06:00
|
|
|
#include "vmci_connecter.hpp"
|
|
|
|
|
|
|
|
#if defined ZMQ_HAVE_VMCI
|
|
|
|
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
#include "io_thread.hpp"
|
|
|
|
#include "platform.hpp"
|
|
|
|
#include "random.hpp"
|
|
|
|
#include "err.hpp"
|
|
|
|
#include "ip.hpp"
|
|
|
|
#include "address.hpp"
|
2016-01-22 20:34:23 +06:00
|
|
|
#include "vmci_address.hpp"
|
2015-12-07 18:19:45 +06:00
|
|
|
#include "vmci.hpp"
|
2021-02-20 18:08:25 +00:00
|
|
|
#include "session_base.hpp"
|
2015-12-07 18:19:45 +06:00
|
|
|
|
|
|
|
zmq::vmci_connecter_t::vmci_connecter_t (class io_thread_t *io_thread_,
|
|
|
|
class session_base_t *session_,
|
|
|
|
const options_t &options_,
|
2021-02-20 18:08:25 +00:00
|
|
|
address_t *addr_,
|
2015-12-07 18:19:45 +06:00
|
|
|
bool delayed_start_) :
|
2021-02-20 18:08:25 +00:00
|
|
|
stream_connecter_base_t (
|
|
|
|
io_thread_, session_, options_, addr_, delayed_start_),
|
|
|
|
_connect_timer_started (false)
|
2015-12-07 18:19:45 +06:00
|
|
|
{
|
2021-02-20 18:08:25 +00:00
|
|
|
zmq_assert (_addr->protocol == protocol_name::vmci);
|
2015-12-07 18:19:45 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::vmci_connecter_t::~vmci_connecter_t ()
|
|
|
|
{
|
2021-02-20 18:08:25 +00:00
|
|
|
zmq_assert (!_connect_timer_started);
|
2015-12-07 18:19:45 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::vmci_connecter_t::process_term (int linger_)
|
|
|
|
{
|
2021-02-20 18:08:25 +00:00
|
|
|
if (_connect_timer_started) {
|
|
|
|
cancel_timer (connect_timer_id);
|
|
|
|
_connect_timer_started = false;
|
2015-12-07 18:19:45 +06:00
|
|
|
}
|
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
stream_connecter_base_t::process_term (linger_);
|
2015-12-07 18:19:45 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::vmci_connecter_t::in_event ()
|
|
|
|
{
|
|
|
|
// We are not polling for incoming data, so we are actually called
|
|
|
|
// because of error here. However, we can get error on out event as well
|
|
|
|
// on some platforms, so we'll simply handle both events in the same way.
|
|
|
|
out_event ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::vmci_connecter_t::out_event ()
|
|
|
|
{
|
2021-02-20 18:08:25 +00:00
|
|
|
if (_connect_timer_started) {
|
|
|
|
cancel_timer (connect_timer_id);
|
|
|
|
_connect_timer_started = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO this is still very similar to (t)ipc_connecter_t, maybe the
|
|
|
|
// differences can be factored out
|
|
|
|
|
|
|
|
rm_handle ();
|
|
|
|
|
|
|
|
const fd_t fd = connect ();
|
|
|
|
|
|
|
|
if (fd == retired_fd
|
|
|
|
&& ((options.reconnect_stop & ZMQ_RECONNECT_STOP_CONN_REFUSED)
|
|
|
|
&& errno == ECONNREFUSED)) {
|
|
|
|
send_conn_failed (_session);
|
|
|
|
close ();
|
|
|
|
terminate ();
|
|
|
|
return;
|
|
|
|
}
|
2015-12-07 18:19:45 +06:00
|
|
|
|
|
|
|
// Handle the error condition by attempt to reconnect.
|
|
|
|
if (fd == retired_fd) {
|
|
|
|
close ();
|
|
|
|
add_reconnect_timer ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:12:11 +06:00
|
|
|
tune_vmci_buffer_size (this->get_ctx (), fd, options.vmci_buffer_size,
|
|
|
|
options.vmci_buffer_min_size,
|
|
|
|
options.vmci_buffer_max_size);
|
2015-12-07 18:19:45 +06:00
|
|
|
|
|
|
|
if (options.vmci_connect_timeout > 0) {
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
tune_vmci_connect_timeout (this->get_ctx (), fd,
|
|
|
|
options.vmci_connect_timeout);
|
|
|
|
#else
|
|
|
|
struct timeval timeout = {0, options.vmci_connect_timeout * 1000};
|
|
|
|
tune_vmci_connect_timeout (this->get_ctx (), fd, timeout);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
create_engine (
|
|
|
|
fd, zmq::vmci_connecter_t::get_socket_name (fd, socket_end_local));
|
|
|
|
}
|
2015-12-07 18:19:45 +06:00
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
std::string
|
|
|
|
zmq::vmci_connecter_t::get_socket_name (zmq::fd_t fd_,
|
|
|
|
socket_end_t socket_end_) const
|
|
|
|
{
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
const zmq_socklen_t sl = get_socket_address (fd_, socket_end_, &ss);
|
|
|
|
if (sl == 0) {
|
|
|
|
return std::string ();
|
|
|
|
}
|
2015-12-07 18:19:45 +06:00
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
const vmci_address_t addr (reinterpret_cast<struct sockaddr *> (&ss), sl,
|
|
|
|
this->get_ctx ());
|
|
|
|
std::string address_string;
|
|
|
|
addr.to_string (address_string);
|
|
|
|
return address_string;
|
2015-12-07 18:19:45 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::vmci_connecter_t::timer_event (int id_)
|
|
|
|
{
|
2021-02-20 18:08:25 +00:00
|
|
|
if (id_ == connect_timer_id) {
|
|
|
|
_connect_timer_started = false;
|
|
|
|
rm_handle ();
|
|
|
|
close ();
|
|
|
|
add_reconnect_timer ();
|
|
|
|
} else
|
|
|
|
stream_connecter_base_t::timer_event (id_);
|
2015-12-07 18:19:45 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::vmci_connecter_t::start_connecting ()
|
|
|
|
{
|
|
|
|
// Open the connecting socket.
|
2021-02-20 18:08:25 +00:00
|
|
|
const int rc = open ();
|
2015-12-07 18:19:45 +06:00
|
|
|
|
|
|
|
// Connect may succeed in synchronous manner.
|
|
|
|
if (rc == 0) {
|
2021-02-20 18:08:25 +00:00
|
|
|
_handle = add_fd (_s);
|
2015-12-07 18:19:45 +06:00
|
|
|
out_event ();
|
|
|
|
}
|
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
// Connection establishment may be delayed. Poll for its completion.
|
|
|
|
else if (rc == -1 && errno == EINPROGRESS) {
|
|
|
|
_handle = add_fd (_s);
|
|
|
|
set_pollout (_handle);
|
|
|
|
_socket->event_connect_delayed (
|
|
|
|
make_unconnected_connect_endpoint_pair (_endpoint), zmq_errno ());
|
|
|
|
|
|
|
|
// add userspace connect timeout
|
|
|
|
add_connect_timer ();
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:19:45 +06:00
|
|
|
// Handle any other error condition by eventual reconnect.
|
|
|
|
else {
|
2021-02-20 18:08:25 +00:00
|
|
|
if (_s != retired_fd)
|
2015-12-07 18:19:45 +06:00
|
|
|
close ();
|
|
|
|
add_reconnect_timer ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
void zmq::vmci_connecter_t::add_connect_timer ()
|
2015-12-07 18:19:45 +06:00
|
|
|
{
|
2021-02-20 18:08:25 +00:00
|
|
|
if (options.connect_timeout > 0) {
|
|
|
|
add_timer (options.connect_timeout, connect_timer_id);
|
|
|
|
_connect_timer_started = true;
|
2018-06-12 17:23:39 -05:00
|
|
|
}
|
2015-12-07 18:19:45 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::vmci_connecter_t::open ()
|
|
|
|
{
|
2021-02-20 18:08:25 +00:00
|
|
|
zmq_assert (_s == retired_fd);
|
2015-12-07 18:19:45 +06:00
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
// Resolve the address
|
|
|
|
if (_addr->resolved.vmci_addr != NULL) {
|
|
|
|
LIBZMQ_DELETE (_addr->resolved.vmci_addr);
|
|
|
|
}
|
2015-12-07 18:19:45 +06:00
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
_addr->resolved.vmci_addr =
|
|
|
|
new (std::nothrow) vmci_address_t (this->get_ctx ());
|
|
|
|
alloc_assert (_addr->resolved.vmci_addr);
|
|
|
|
_s = vmci_open_socket (_addr->address.c_str (), options,
|
|
|
|
_addr->resolved.vmci_addr);
|
|
|
|
if (_s == retired_fd) {
|
|
|
|
// TODO we should emit some event in this case!
|
|
|
|
|
|
|
|
LIBZMQ_DELETE (_addr->resolved.vmci_addr);
|
2016-02-03 19:12:11 +06:00
|
|
|
return -1;
|
|
|
|
}
|
2021-02-20 18:08:25 +00:00
|
|
|
zmq_assert (_addr->resolved.vmci_addr != NULL);
|
2015-12-07 18:19:45 +06:00
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
// Set the socket to non-blocking mode so that we get async connect().
|
|
|
|
unblock_socket (_s);
|
|
|
|
|
|
|
|
const vmci_address_t *const vmci_addr = _addr->resolved.vmci_addr;
|
2015-12-07 18:19:45 +06:00
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
// Connect to the remote peer.
|
|
|
|
#if defined ZMQ_HAVE_VXWORKS
|
|
|
|
rc = ::connect (_s, (sockaddr *) vmci_addr->addr (), vmci_addr->addrlen ());
|
|
|
|
#else
|
|
|
|
rc = ::connect (_s, vmci_addr->addr (), vmci_addr->addrlen ());
|
|
|
|
#endif
|
2015-12-07 18:19:45 +06:00
|
|
|
// Connect was successful immediately.
|
2021-02-20 18:08:25 +00:00
|
|
|
if (rc == 0) {
|
2015-12-07 18:19:45 +06:00
|
|
|
return 0;
|
2021-02-20 18:08:25 +00:00
|
|
|
}
|
2015-12-07 18:19:45 +06:00
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
// Translate error codes indicating asynchronous connect has been
|
|
|
|
// launched to a uniform EINPROGRESS.
|
2016-01-22 20:34:23 +06:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2021-02-20 18:08:25 +00:00
|
|
|
const int last_error = WSAGetLastError ();
|
|
|
|
if (last_error == WSAEINPROGRESS || last_error == WSAEWOULDBLOCK)
|
|
|
|
errno = EINPROGRESS;
|
|
|
|
else
|
|
|
|
errno = wsa_error_to_errno (last_error);
|
2016-01-22 20:34:23 +06:00
|
|
|
#else
|
2021-02-20 18:08:25 +00:00
|
|
|
if (errno == EINTR)
|
|
|
|
errno = EINPROGRESS;
|
2016-01-22 20:34:23 +06:00
|
|
|
#endif
|
2021-02-20 18:08:25 +00:00
|
|
|
return -1;
|
2015-12-07 18:19:45 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::fd_t zmq::vmci_connecter_t::connect ()
|
|
|
|
{
|
2021-02-20 18:08:25 +00:00
|
|
|
// Async connect has finished. Check whether an error occurred
|
2015-12-07 18:19:45 +06:00
|
|
|
int err = 0;
|
2021-02-20 18:08:25 +00:00
|
|
|
#if defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_VXWORKS
|
|
|
|
int len = sizeof err;
|
2015-12-07 18:19:45 +06:00
|
|
|
#else
|
2021-02-20 18:08:25 +00:00
|
|
|
socklen_t len = sizeof err;
|
2015-12-07 18:19:45 +06:00
|
|
|
#endif
|
2021-02-20 18:08:25 +00:00
|
|
|
|
|
|
|
const int rc = getsockopt (_s, SOL_SOCKET, SO_ERROR,
|
|
|
|
reinterpret_cast<char *> (&err), &len);
|
2016-02-03 19:12:11 +06:00
|
|
|
|
|
|
|
// Assert if the error was caused by 0MQ bug.
|
|
|
|
// Networking problems are OK. No need to assert.
|
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
if (err != 0) {
|
2021-02-20 18:08:25 +00:00
|
|
|
if (err == WSAEBADF || err == WSAENOPROTOOPT || err == WSAENOTSOCK
|
|
|
|
|| err == WSAENOBUFS) {
|
2016-02-03 19:12:11 +06:00
|
|
|
wsa_assert_no (err);
|
|
|
|
}
|
2021-02-20 18:08:25 +00:00
|
|
|
errno = wsa_error_to_errno (err);
|
2016-02-03 19:12:11 +06:00
|
|
|
return retired_fd;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Following code should handle both Berkeley-derived socket
|
|
|
|
// implementations and Solaris.
|
2015-12-07 18:19:45 +06:00
|
|
|
if (rc == -1)
|
|
|
|
err = errno;
|
|
|
|
if (err != 0) {
|
|
|
|
errno = err;
|
2021-02-20 18:08:25 +00:00
|
|
|
#if !defined(TARGET_OS_IPHONE) || !TARGET_OS_IPHONE
|
|
|
|
errno_assert (errno != EBADF && errno != ENOPROTOOPT
|
|
|
|
&& errno != ENOTSOCK && errno != ENOBUFS);
|
|
|
|
#else
|
|
|
|
errno_assert (errno != ENOPROTOOPT && errno != ENOTSOCK
|
|
|
|
&& errno != ENOBUFS);
|
|
|
|
#endif
|
2015-12-07 18:19:45 +06:00
|
|
|
return retired_fd;
|
|
|
|
}
|
2016-02-03 19:12:11 +06:00
|
|
|
#endif
|
2015-12-07 18:19:45 +06:00
|
|
|
|
2021-02-20 18:08:25 +00:00
|
|
|
// Return the newly connected socket.
|
|
|
|
const fd_t result = _s;
|
|
|
|
_s = retired_fd;
|
2015-12-07 18:19:45 +06:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|