tcp_listener.cpp 5.66 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3 4
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
10 11 12 13 14 15
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ 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
16
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
17

18
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
19 20 21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

22 23
#include <new>

24 25
#include <string.h>

Martin Sustrik's avatar
Martin Sustrik committed
26
#include "platform.hpp"
27
#include "tcp_listener.hpp"
28
#include "stream_engine.hpp"
29
#include "io_thread.hpp"
30
#include "session_base.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
31 32
#include "config.hpp"
#include "err.hpp"
33
#include "ip.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
34

Martin Sustrik's avatar
Martin Sustrik committed
35
#ifdef ZMQ_HAVE_WINDOWS
36 37 38 39 40 41 42 43 44 45
#include "windows.hpp"
#else
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#endif
46 47 48

#ifdef ZMQ_HAVE_OPENVMS
#include <ioctl.h>
49
#endif
Martin Sustrik's avatar
Martin Sustrik committed
50

51 52 53 54
zmq::tcp_listener_t::tcp_listener_t (io_thread_t *io_thread_,
      socket_base_t *socket_, const options_t &options_) :
    own_t (io_thread_, options_),
    io_object_t (io_thread_),
55
    has_file (false),
56 57
    s (retired_fd),
    socket (socket_)
58 59 60 61 62 63 64 65 66
{
}

zmq::tcp_listener_t::~tcp_listener_t ()
{
    if (s != retired_fd)
        close ();
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
void zmq::tcp_listener_t::process_plug ()
{
    //  Start polling for incoming connections.
    handle = add_fd (s);
    set_pollin (handle);
}

void zmq::tcp_listener_t::process_term (int linger_)
{
    rm_fd (handle);
    own_t::process_term (linger_);
}

void zmq::tcp_listener_t::in_event ()
{
    fd_t fd = accept ();

    //  If connection was reset by the peer in the meantime, just ignore it.
    //  TODO: Handle specific errors like ENFILE/EMFILE etc.
    if (fd == retired_fd)
        return;
88

89
    tune_tcp_socket (fd);
90

91
    //  Create the engine object for this connection.
92
    stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options);
93 94 95 96 97 98 99 100
    alloc_assert (engine);

    //  Choose I/O thread to run connecter in. Given that we are already
    //  running in an I/O thread, there must be at least one available.
    io_thread_t *io_thread = choose_io_thread (options.affinity);
    zmq_assert (io_thread);

    //  Create and launch a session object. 
101 102 103
    session_base_t *session = session_base_t::create (io_thread, false, socket,
        options, NULL, NULL);
    errno_assert (session);
104 105 106 107 108
    session->inc_seqnum ();
    launch_child (session);
    send_attach (session, engine, false);
}

109 110 111
void zmq::tcp_listener_t::close ()
{
    zmq_assert (s != retired_fd);
112
#ifdef ZMQ_HAVE_WINDOWS
113 114 115 116 117 118 119 120
    int rc = closesocket (s);
    wsa_assert (rc != SOCKET_ERROR);
#else
    int rc = ::close (s);
    errno_assert (rc == 0);
#endif
    s = retired_fd;
}
121

122
int zmq::tcp_listener_t::set_address (const char *addr_)
123
{
124 125
    //  Convert the textual address into address structure.
    int rc = address.resolve (addr_, true, options.ipv4only ? true : false);
126
    if (rc != 0)
127
        return -1;
128 129

    //  Create a listening socket.
130
    s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
131 132 133 134 135 136
#ifdef ZMQ_HAVE_WINDOWS
    if (s == INVALID_SOCKET)
        wsa_error_to_errno ();
#endif

    //  IPv6 address family not supported, try automatic downgrade to IPv4.
137
    if (address.family () == AF_INET6 && errno == EAFNOSUPPORT &&
138
          !options.ipv4only) {
139
        rc = address.resolve (addr_, true, true);
140 141
        if (rc != 0)
            return rc;
142
        s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
143 144
    }

145
#ifdef ZMQ_HAVE_WINDOWS
146 147 148 149
    if (s == INVALID_SOCKET) {
        wsa_error_to_errno ();
        return -1;
    }
150 151 152 153
#else
    if (s == -1)
        return -1;
#endif
154

155 156
    //  On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
    //  Switch it on in such cases.
157
    if (address.family () == AF_INET6)
158 159
        enable_ipv4_mapping (s);

160 161
    //  Allow reusing of the address.
    int flag = 1;
162
#ifdef ZMQ_HAVE_WINDOWS
163
    rc = setsockopt (s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
164 165
        (const char*) &flag, sizeof (int));
    wsa_assert (rc != SOCKET_ERROR);
166 167 168 169
#else
    rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
    errno_assert (rc == 0);
#endif
170 171

    //  Bind the socket to the network interface and port.
172
    rc = bind (s, address.addr (), address.addrlen ());
173
#ifdef ZMQ_HAVE_WINDOWS
174 175 176 177
    if (rc == SOCKET_ERROR) {
        wsa_error_to_errno ();
        return -1;
    }
Martin Sustrik's avatar
Martin Sustrik committed
178
#else
179 180
    if (rc != 0)
        return -1;
181
#endif
182

183 184
    //  Listen for incomming connections.
    rc = listen (s, options.backlog);
185 186 187
#ifdef ZMQ_HAVE_WINDOWS
    if (rc == SOCKET_ERROR) {
        wsa_error_to_errno ();
188
        return -1;
189
    }
190
#else
Martin Sustrik's avatar
Martin Sustrik committed
191 192
    if (rc != 0)
        return -1;
Brett Cameron's avatar
Brett Cameron committed
193
#endif
194

Martin Sustrik's avatar
Martin Sustrik committed
195 196 197
    return 0;
}

Martin Sustrik's avatar
Martin Sustrik committed
198
zmq::fd_t zmq::tcp_listener_t::accept ()
Martin Sustrik's avatar
Martin Sustrik committed
199
{
200
    //  Accept one connection and deal with different failure modes.
Martin Sustrik's avatar
Martin Sustrik committed
201
    zmq_assert (s != retired_fd);
Martin Sustrik's avatar
Martin Sustrik committed
202
    fd_t sock = ::accept (s, NULL, NULL);
203
#ifdef ZMQ_HAVE_WINDOWS
204
    if (sock == INVALID_SOCKET) {
205 206
        wsa_assert (WSAGetLastError () == WSAEWOULDBLOCK ||
            WSAGetLastError () == WSAECONNRESET);
Martin Sustrik's avatar
Martin Sustrik committed
207
        return retired_fd;
208
    }
Brett Cameron's avatar
Brett Cameron committed
209
#else
210 211 212 213 214 215
    if (sock == -1) {
        errno_assert (errno == EAGAIN || errno == EWOULDBLOCK ||
            errno == EINTR || errno == ECONNABORTED || errno == EPROTO ||
            errno == ENOBUFS);
        return retired_fd;
    }
Brett Cameron's avatar
Brett Cameron committed
216
#endif
Martin Sustrik's avatar
Martin Sustrik committed
217 218 219
    return sock;
}