| 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 | libzmq is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | by the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | As a special exception, the Contributors give you permission to link |
| 12 | this library with independent modules to produce an executable, |
| 13 | regardless of the license terms of these independent modules, and to |
| 14 | copy and distribute the resulting executable under terms of your choice, |
| 15 | provided that you also meet, for each linked independent module, the |
| 16 | terms and conditions of the license of that module. An independent |
| 17 | module is a module which is not derived from or based on this library. |
| 18 | If you modify this library, you must extend this exception to your |
| 19 | version of the library. |
| 20 | |
| 21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
| 22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 24 | License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU Lesser General Public License |
| 27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | */ |
| 29 | |
| 30 | #include "precompiled.hpp" |
| 31 | |
| 32 | #include "tipc_connecter.hpp" |
| 33 | |
| 34 | #if defined ZMQ_HAVE_TIPC |
| 35 | |
| 36 | #include <new> |
| 37 | #include <string> |
| 38 | |
| 39 | #include "io_thread.hpp" |
| 40 | #include "platform.hpp" |
| 41 | #include "random.hpp" |
| 42 | #include "err.hpp" |
| 43 | #include "ip.hpp" |
| 44 | #include "address.hpp" |
| 45 | #include "tipc_address.hpp" |
| 46 | #include "session_base.hpp" |
| 47 | |
| 48 | #include <unistd.h> |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/socket.h> |
| 51 | #ifdef ZMQ_HAVE_VXWORKS |
| 52 | #include <sockLib.h> |
| 53 | #endif |
| 54 | |
| 55 | zmq::tipc_connecter_t::tipc_connecter_t (class io_thread_t *io_thread_, |
| 56 | class session_base_t *session_, |
| 57 | const options_t &options_, |
| 58 | address_t *addr_, |
| 59 | bool delayed_start_) : |
| 60 | stream_connecter_base_t ( |
| 61 | io_thread_, session_, options_, addr_, delayed_start_) |
| 62 | { |
| 63 | zmq_assert (_addr->protocol == "tipc" ); |
| 64 | } |
| 65 | |
| 66 | void zmq::tipc_connecter_t::out_event () |
| 67 | { |
| 68 | fd_t fd = connect (); |
| 69 | rm_handle (); |
| 70 | |
| 71 | // Handle the error condition by attempt to reconnect. |
| 72 | if (fd == retired_fd) { |
| 73 | close (); |
| 74 | add_reconnect_timer (); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | create_engine (fd, get_socket_name<tipc_address_t> (fd, socket_end_local)); |
| 79 | } |
| 80 | |
| 81 | void zmq::tipc_connecter_t::start_connecting () |
| 82 | { |
| 83 | // Open the connecting socket. |
| 84 | int rc = open (); |
| 85 | |
| 86 | // Connect may succeed in synchronous manner. |
| 87 | if (rc == 0) { |
| 88 | _handle = add_fd (_s); |
| 89 | out_event (); |
| 90 | } |
| 91 | |
| 92 | // Connection establishment may be delayed. Poll for its completion. |
| 93 | else if (rc == -1 && errno == EINPROGRESS) { |
| 94 | _handle = add_fd (_s); |
| 95 | set_pollout (_handle); |
| 96 | _socket->event_connect_delayed ( |
| 97 | make_unconnected_connect_endpoint_pair (_endpoint), zmq_errno ()); |
| 98 | } |
| 99 | |
| 100 | // Handle any other error condition by eventual reconnect. |
| 101 | else { |
| 102 | if (_s != retired_fd) |
| 103 | close (); |
| 104 | add_reconnect_timer (); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | int zmq::tipc_connecter_t::open () |
| 109 | { |
| 110 | zmq_assert (_s == retired_fd); |
| 111 | |
| 112 | // Cannot connect to random tipc addresses |
| 113 | if (_addr->resolved.tipc_addr->is_random ()) { |
| 114 | errno = EINVAL; |
| 115 | return -1; |
| 116 | } |
| 117 | // Create the socket. |
| 118 | _s = open_socket (AF_TIPC, SOCK_STREAM, 0); |
| 119 | if (_s == -1) |
| 120 | return -1; |
| 121 | |
| 122 | // Set the non-blocking flag. |
| 123 | unblock_socket (_s); |
| 124 | // Connect to the remote peer. |
| 125 | #ifdef ZMQ_HAVE_VXWORKS |
| 126 | int rc = ::connect (s, (sockaddr *) addr->resolved.tipc_addr->addr (), |
| 127 | addr->resolved.tipc_addr->addrlen ()); |
| 128 | #else |
| 129 | int rc = ::connect (_s, _addr->resolved.tipc_addr->addr (), |
| 130 | _addr->resolved.tipc_addr->addrlen ()); |
| 131 | #endif |
| 132 | // Connect was successful immediately. |
| 133 | if (rc == 0) |
| 134 | return 0; |
| 135 | |
| 136 | // Translate other error codes indicating asynchronous connect has been |
| 137 | // launched to a uniform EINPROGRESS. |
| 138 | if (rc == -1 && errno == EINTR) { |
| 139 | errno = EINPROGRESS; |
| 140 | return -1; |
| 141 | } |
| 142 | // Forward the error. |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | zmq::fd_t zmq::tipc_connecter_t::connect () |
| 147 | { |
| 148 | // Following code should handle both Berkeley-derived socket |
| 149 | // implementations and Solaris. |
| 150 | int err = 0; |
| 151 | #ifdef ZMQ_HAVE_VXWORKS |
| 152 | int len = sizeof (err); |
| 153 | #else |
| 154 | socklen_t len = sizeof (err); |
| 155 | #endif |
| 156 | int rc = getsockopt (_s, SOL_SOCKET, SO_ERROR, |
| 157 | reinterpret_cast<char *> (&err), &len); |
| 158 | if (rc == -1) |
| 159 | err = errno; |
| 160 | if (err != 0) { |
| 161 | // Assert if the error was caused by 0MQ bug. |
| 162 | // Networking problems are OK. No need to assert. |
| 163 | errno = err; |
| 164 | errno_assert (errno == ECONNREFUSED || errno == ECONNRESET |
| 165 | || errno == ETIMEDOUT || errno == EHOSTUNREACH |
| 166 | || errno == ENETUNREACH || errno == ENETDOWN); |
| 167 | |
| 168 | return retired_fd; |
| 169 | } |
| 170 | fd_t result = _s; |
| 171 | _s = retired_fd; |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | #endif |
| 176 | |