ipc_connecter.cpp 5.36 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    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
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    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.
25 26 27 28 29

    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/>.
*/

30
#include "precompiled.hpp"
31 32
#include "ipc_connecter.hpp"

33
#if defined ZMQ_HAVE_IPC
34

35 36 37 38 39 40
#include <new>
#include <string>

#include "io_thread.hpp"
#include "random.hpp"
#include "err.hpp"
41
#include "ip.hpp"
42 43
#include "address.hpp"
#include "ipc_address.hpp"
44
#include "session_base.hpp"
45

46 47 48
#ifdef _MSC_VER
#include <afunix.h>
#else
49 50 51
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
52
#include <sys/un.h>
53
#endif
54

55
zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
56 57
                                       class session_base_t *session_,
                                       const options_t &options_,
58
                                       address_t *addr_,
59
                                       bool delayed_start_) :
60 61
    stream_connecter_base_t (
      io_thread_, session_, options_, addr_, delayed_start_)
62
{
63
    zmq_assert (_addr->protocol == protocol_name::ipc);
64 65 66 67
}

void zmq::ipc_connecter_t::out_event ()
{
68
    const fd_t fd = connect ();
69
    rm_handle ();
70 71 72 73

    //  Handle the error condition by attempt to reconnect.
    if (fd == retired_fd) {
        close ();
74
        add_reconnect_timer ();
75 76 77
        return;
    }

78
    create_engine (fd, get_socket_name<ipc_address_t> (fd, socket_end_local));
79 80 81 82 83
}

void zmq::ipc_connecter_t::start_connecting ()
{
    //  Open the connecting socket.
84
    const int rc = open ();
85 86 87

    //  Connect may succeed in synchronous manner.
    if (rc == 0) {
88
        _handle = add_fd (_s);
89 90 91
        out_event ();
    }

92
    //  Connection establishment may be delayed. Poll for its completion.
93
    else if (rc == -1 && errno == EINPROGRESS) {
94 95
        _handle = add_fd (_s);
        set_pollout (_handle);
96 97
        _socket->event_connect_delayed (
          make_unconnected_connect_endpoint_pair (_endpoint), zmq_errno ());
98 99 100 101

        // TODO, tcp_connecter_t adds a connect timer in this case; maybe this
        // should be done here as well (and then this could be pulled up to
        // stream_connecter_base_t).
102 103 104
    }

    //  Handle any other error condition by eventual reconnect.
105
    else {
106
        if (_s != retired_fd)
107
            close ();
108 109
        add_reconnect_timer ();
    }
110 111 112 113
}

int zmq::ipc_connecter_t::open ()
{
114
    zmq_assert (_s == retired_fd);
115 116

    //  Create the socket.
117 118
    _s = open_socket (AF_UNIX, SOCK_STREAM, 0);
    if (_s == -1)
119 120
        return -1;

121
    //  Set the non-blocking flag.
122
    unblock_socket (_s);
123 124

    //  Connect to the remote peer.
125 126
    const int rc = ::connect (_s, _addr->resolved.ipc_addr->addr (),
                              _addr->resolved.ipc_addr->addrlen ());
127

128
    //  Connect was successful immediately.
129 130
    if (rc == 0)
        return 0;
131

132 133 134 135 136 137 138 139 140
        //  Translate other error codes indicating asynchronous connect has been
        //  launched to a uniform EINPROGRESS.
#ifdef ZMQ_HAVE_WINDOWS
    const int last_error = WSAGetLastError ();
    if (last_error == WSAEINPROGRESS || last_error == WSAEWOULDBLOCK)
        errno = EINPROGRESS;
    else
        errno = wsa_error_to_errno (last_error);
#else
141 142 143
    if (rc == -1 && errno == EINTR) {
        errno = EINPROGRESS;
    }
144
#endif
145

146
    //  Forward the error.
147 148 149 150 151 152 153 154
    return -1;
}

zmq::fd_t zmq::ipc_connecter_t::connect ()
{
    //  Following code should handle both Berkeley-derived socket
    //  implementations and Solaris.
    int err = 0;
155
    zmq_socklen_t len = static_cast<zmq_socklen_t> (sizeof (err));
156 157
    const int rc = getsockopt (_s, SOL_SOCKET, SO_ERROR,
                               reinterpret_cast<char *> (&err), &len);
158 159 160
    if (rc == -1) {
        if (errno == ENOPROTOOPT)
            errno = 0;
161
        err = errno;
162
    }
163 164 165 166
    if (err != 0) {
        //  Assert if the error was caused by 0MQ bug.
        //  Networking problems are OK. No need to assert.
        errno = err;
167 168 169
        errno_assert (errno == ECONNREFUSED || errno == ECONNRESET
                      || errno == ETIMEDOUT || errno == EHOSTUNREACH
                      || errno == ENETUNREACH || errno == ENETDOWN);
170 171 172 173

        return retired_fd;
    }

174
    const fd_t result = _s;
175
    _s = retired_fd;
176 177 178 179
    return result;
}

#endif