2
Copyright (c) 2009-2011 250bpm s.r.o.
3
Copyright (c) 2007-2010 iMatix Corporation
4
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
6
This file is part of 0MQ.
8
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
10
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
13
0MQ is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
26
#include "platform.hpp"
27
#include "tcp_listener.hpp"
28
#include "stream_engine.hpp"
29
#include "io_thread.hpp"
30
#include "session_base.hpp"
35
#ifdef ZMQ_HAVE_WINDOWS
36
#include "windows.hpp"
39
#include <sys/socket.h>
40
#include <arpa/inet.h>
41
#include <netinet/tcp.h>
42
#include <netinet/in.h>
47
#ifdef ZMQ_HAVE_OPENVMS
51
zmq::tcp_listener_t::tcp_listener_t (io_thread_t *io_thread_,
52
socket_base_t *socket_, const options_t &options_) :
53
own_t (io_thread_, options_),
54
io_object_t (io_thread_),
61
zmq::tcp_listener_t::~tcp_listener_t ()
67
void zmq::tcp_listener_t::process_plug ()
69
// Start polling for incoming connections.
74
void zmq::tcp_listener_t::process_term (int linger_)
77
own_t::process_term (linger_);
80
void zmq::tcp_listener_t::in_event ()
84
// If connection was reset by the peer in the meantime, just ignore it.
85
// TODO: Handle specific errors like ENFILE/EMFILE etc.
91
// Create the engine object for this connection.
92
stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options);
93
alloc_assert (engine);
95
// Choose I/O thread to run connecter in. Given that we are already
96
// running in an I/O thread, there must be at least one available.
97
io_thread_t *io_thread = choose_io_thread (options.affinity);
98
zmq_assert (io_thread);
100
// Create and launch a session object.
101
session_base_t *session = session_base_t::create (io_thread, false, socket,
102
options, NULL, NULL);
103
errno_assert (session);
104
session->inc_seqnum ();
105
launch_child (session);
106
send_attach (session, engine, false);
109
void zmq::tcp_listener_t::close ()
111
zmq_assert (s != retired_fd);
112
#ifdef ZMQ_HAVE_WINDOWS
113
int rc = closesocket (s);
114
wsa_assert (rc != SOCKET_ERROR);
116
int rc = ::close (s);
117
errno_assert (rc == 0);
122
int zmq::tcp_listener_t::set_address (const char *addr_)
124
// Convert the textual address into address structure.
125
int rc = address.resolve (addr_, true, options.ipv4only ? true : false);
129
// Create a listening socket.
130
s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
131
#ifdef ZMQ_HAVE_WINDOWS
132
if (s == INVALID_SOCKET)
133
wsa_error_to_errno ();
136
// IPv6 address family not supported, try automatic downgrade to IPv4.
137
if (address.family () == AF_INET6 && errno == EAFNOSUPPORT &&
139
rc = address.resolve (addr_, true, true);
142
s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
145
#ifdef ZMQ_HAVE_WINDOWS
146
if (s == INVALID_SOCKET) {
147
wsa_error_to_errno ();
155
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
156
// Switch it on in such cases.
157
if (address.family () == AF_INET6)
158
enable_ipv4_mapping (s);
160
// Allow reusing of the address.
162
#ifdef ZMQ_HAVE_WINDOWS
163
rc = setsockopt (s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
164
(const char*) &flag, sizeof (int));
165
wsa_assert (rc != SOCKET_ERROR);
167
rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
168
errno_assert (rc == 0);
171
// Bind the socket to the network interface and port.
172
rc = bind (s, address.addr (), address.addrlen ());
173
#ifdef ZMQ_HAVE_WINDOWS
174
if (rc == SOCKET_ERROR) {
175
wsa_error_to_errno ();
183
// Listen for incomming connections.
184
rc = listen (s, options.backlog);
185
#ifdef ZMQ_HAVE_WINDOWS
186
if (rc == SOCKET_ERROR) {
187
wsa_error_to_errno ();
198
zmq::fd_t zmq::tcp_listener_t::accept ()
200
// Accept one connection and deal with different failure modes.
201
zmq_assert (s != retired_fd);
202
fd_t sock = ::accept (s, NULL, NULL);
203
#ifdef ZMQ_HAVE_WINDOWS
204
if (sock == INVALID_SOCKET) {
205
wsa_assert (WSAGetLastError () == WSAEWOULDBLOCK ||
206
WSAGetLastError () == WSAECONNRESET);
211
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK ||
212
errno == EINTR || errno == ECONNABORTED || errno == EPROTO ||