| 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_listener.hpp" |
| 33 | |
| 34 | #if defined ZMQ_HAVE_TIPC |
| 35 | |
| 36 | #include <new> |
| 37 | |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "tipc_address.hpp" |
| 41 | #include "io_thread.hpp" |
| 42 | #include "config.hpp" |
| 43 | #include "err.hpp" |
| 44 | #include "ip.hpp" |
| 45 | #include "socket_base.hpp" |
| 46 | #include "address.hpp" |
| 47 | |
| 48 | #include <unistd.h> |
| 49 | #include <sys/socket.h> |
| 50 | #include <fcntl.h> |
| 51 | #if defined ZMQ_HAVE_VXWORKS |
| 52 | #include <sockLib.h> |
| 53 | #include <tipc/tipc.h> |
| 54 | #else |
| 55 | #include <linux/tipc.h> |
| 56 | #endif |
| 57 | |
| 58 | zmq::tipc_listener_t::tipc_listener_t (io_thread_t *io_thread_, |
| 59 | socket_base_t *socket_, |
| 60 | const options_t &options_) : |
| 61 | stream_listener_base_t (io_thread_, socket_, options_) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void zmq::tipc_listener_t::in_event () |
| 66 | { |
| 67 | fd_t fd = accept (); |
| 68 | |
| 69 | // If connection was reset by the peer in the meantime, just ignore it. |
| 70 | // TODO: Handle specific errors like ENFILE/EMFILE etc. |
| 71 | if (fd == retired_fd) { |
| 72 | _socket->event_accept_failed ( |
| 73 | make_unconnected_bind_endpoint_pair (_endpoint), zmq_errno ()); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | // Create the engine object for this connection. |
| 78 | create_engine (fd); |
| 79 | } |
| 80 | |
| 81 | std::string |
| 82 | zmq::tipc_listener_t::get_socket_name (zmq::fd_t fd_, |
| 83 | socket_end_t socket_end_) const |
| 84 | { |
| 85 | return zmq::get_socket_name<tipc_address_t> (fd_, socket_end_); |
| 86 | } |
| 87 | |
| 88 | int zmq::tipc_listener_t::set_local_address (const char *addr_) |
| 89 | { |
| 90 | // Convert str to address struct |
| 91 | int rc = _address.resolve (addr_); |
| 92 | if (rc != 0) |
| 93 | return -1; |
| 94 | |
| 95 | // Cannot bind non-random Port Identity |
| 96 | struct sockaddr_tipc *a = (sockaddr_tipc *) _address.addr (); |
| 97 | if (!_address.is_random () && a->addrtype == TIPC_ADDR_ID) { |
| 98 | errno = EINVAL; |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | // Create a listening socket. |
| 103 | _s = open_socket (AF_TIPC, SOCK_STREAM, 0); |
| 104 | if (_s == -1) |
| 105 | return -1; |
| 106 | |
| 107 | // If random Port Identity, update address object to reflect the assigned address |
| 108 | if (_address.is_random ()) { |
| 109 | struct sockaddr_storage ss; |
| 110 | const zmq_socklen_t sl = get_socket_address (_s, socket_end_local, &ss); |
| 111 | if (sl == 0) |
| 112 | goto error; |
| 113 | |
| 114 | _address = |
| 115 | tipc_address_t (reinterpret_cast<struct sockaddr *> (&ss), sl); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | _address.to_string (_endpoint); |
| 120 | |
| 121 | // Bind the socket to tipc name |
| 122 | if (_address.is_service ()) { |
| 123 | #ifdef ZMQ_HAVE_VXWORKS |
| 124 | rc = bind (_s, (sockaddr *) address.addr (), address.addrlen ()); |
| 125 | #else |
| 126 | rc = bind (_s, _address.addr (), _address.addrlen ()); |
| 127 | #endif |
| 128 | if (rc != 0) |
| 129 | goto error; |
| 130 | } |
| 131 | |
| 132 | // Listen for incoming connections. |
| 133 | rc = listen (_s, options.backlog); |
| 134 | if (rc != 0) |
| 135 | goto error; |
| 136 | |
| 137 | _socket->event_listening (make_unconnected_bind_endpoint_pair (_endpoint), |
| 138 | _s); |
| 139 | return 0; |
| 140 | |
| 141 | error: |
| 142 | int err = errno; |
| 143 | close (); |
| 144 | errno = err; |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | zmq::fd_t zmq::tipc_listener_t::accept () |
| 149 | { |
| 150 | // Accept one connection and deal with different failure modes. |
| 151 | // The situation where connection cannot be accepted due to insufficient |
| 152 | // resources is considered valid and treated by ignoring the connection. |
| 153 | struct sockaddr_storage ss = {}; |
| 154 | socklen_t ss_len = sizeof (ss); |
| 155 | |
| 156 | zmq_assert (_s != retired_fd); |
| 157 | #ifdef ZMQ_HAVE_VXWORKS |
| 158 | fd_t sock = ::accept (_s, (struct sockaddr *) &ss, (int *) &ss_len); |
| 159 | #else |
| 160 | fd_t sock = |
| 161 | ::accept (_s, reinterpret_cast<struct sockaddr *> (&ss), &ss_len); |
| 162 | #endif |
| 163 | if (sock == -1) { |
| 164 | errno_assert (errno == EAGAIN || errno == EWOULDBLOCK |
| 165 | || errno == ENOBUFS || errno == EINTR |
| 166 | || errno == ECONNABORTED || errno == EPROTO |
| 167 | || errno == EMFILE || errno == ENFILE); |
| 168 | return retired_fd; |
| 169 | } |
| 170 | /*FIXME Accept filters?*/ |
| 171 | return sock; |
| 172 | } |
| 173 | |
| 174 | #endif |
| 175 | |