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"
34
#include "socket_base.hpp"
36
#ifdef ZMQ_HAVE_WINDOWS
37
#include "windows.hpp"
40
#include <sys/socket.h>
41
#include <arpa/inet.h>
42
#include <netinet/tcp.h>
43
#include <netinet/in.h>
48
#ifdef ZMQ_HAVE_OPENVMS
52
zmq::tcp_listener_t::tcp_listener_t (io_thread_t *io_thread_,
53
socket_base_t *socket_, const options_t &options_) :
54
own_t (io_thread_, options_),
55
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_)
78
own_t::process_term (linger_);
81
void zmq::tcp_listener_t::in_event ()
85
// If connection was reset by the peer in the meantime, just ignore it.
86
// TODO: Handle specific errors like ENFILE/EMFILE etc.
87
if (fd == retired_fd) {
88
socket->monitor_event (ZMQ_EVENT_ACCEPT_FAILED, endpoint.c_str(), zmq_errno());
93
tune_tcp_keepalives (fd, options.tcp_keepalive, options.tcp_keepalive_cnt, options.tcp_keepalive_idle, options.tcp_keepalive_intvl);
95
// Create the engine object for this connection.
96
stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options);
97
alloc_assert (engine);
99
// Choose I/O thread to run connecter in. Given that we are already
100
// running in an I/O thread, there must be at least one available.
101
io_thread_t *io_thread = choose_io_thread (options.affinity);
102
zmq_assert (io_thread);
104
// Create and launch a session object.
105
session_base_t *session = session_base_t::create (io_thread, false, socket,
107
errno_assert (session);
108
session->inc_seqnum ();
109
launch_child (session);
110
send_attach (session, engine, false);
111
socket->monitor_event (ZMQ_EVENT_ACCEPTED, endpoint.c_str(), fd);
114
void zmq::tcp_listener_t::close ()
116
zmq_assert (s != retired_fd);
117
#ifdef ZMQ_HAVE_WINDOWS
118
int rc = closesocket (s);
119
if (unlikely (rc != SOCKET_ERROR))
120
socket->monitor_event (ZMQ_EVENT_CLOSE_FAILED, endpoint.c_str(), zmq_errno());
121
wsa_assert (rc != SOCKET_ERROR);
123
int rc = ::close (s);
124
if (unlikely (rc == 0))
125
socket->monitor_event (ZMQ_EVENT_CLOSE_FAILED, endpoint.c_str(), zmq_errno());
126
errno_assert (rc == 0);
128
socket->monitor_event (ZMQ_EVENT_CLOSED, endpoint.c_str(), s);
132
int zmq::tcp_listener_t::get_address (std::string &addr_)
134
// Get the details of the TCP socket
135
struct sockaddr_storage ss;
136
socklen_t sl = sizeof (ss);
137
int rc = getsockname (s, (struct sockaddr *) &ss, &sl);
144
tcp_address_t addr ((struct sockaddr *) &ss, sl);
145
return addr.to_string (addr_);
148
int zmq::tcp_listener_t::set_address (const char *addr_)
150
// Convert the textual address into address structure.
151
int rc = address.resolve (addr_, true, options.ipv4only ? true : false);
155
// Create a listening socket.
156
s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
157
#ifdef ZMQ_HAVE_WINDOWS
158
if (s == INVALID_SOCKET)
159
errno = wsa_error_to_errno (WSAGetLastError ());
162
// IPv6 address family not supported, try automatic downgrade to IPv4.
163
if (address.family () == AF_INET6 && errno == EAFNOSUPPORT &&
165
rc = address.resolve (addr_, true, true);
168
s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
171
#ifdef ZMQ_HAVE_WINDOWS
172
if (s == INVALID_SOCKET) {
173
errno = wsa_error_to_errno (WSAGetLastError ());
176
// On Windows, preventing sockets to be inherited by child processes.
177
BOOL brc = SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0);
184
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
185
// Switch it on in such cases.
186
if (address.family () == AF_INET6)
187
enable_ipv4_mapping (s);
189
// Allow reusing of the address.
191
#ifdef ZMQ_HAVE_WINDOWS
192
rc = setsockopt (s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
193
(const char*) &flag, sizeof (int));
194
wsa_assert (rc != SOCKET_ERROR);
196
rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
197
errno_assert (rc == 0);
200
address.to_string (endpoint);
202
// Bind the socket to the network interface and port.
203
rc = bind (s, address.addr (), address.addrlen ());
204
#ifdef ZMQ_HAVE_WINDOWS
205
if (rc == SOCKET_ERROR) {
206
errno = wsa_error_to_errno (WSAGetLastError ());
214
// Listen for incomming connections.
215
rc = listen (s, options.backlog);
216
#ifdef ZMQ_HAVE_WINDOWS
217
if (rc == SOCKET_ERROR) {
218
errno = wsa_error_to_errno (WSAGetLastError ());
226
socket->monitor_event (ZMQ_EVENT_LISTENING, addr_, s);
230
zmq::fd_t zmq::tcp_listener_t::accept ()
232
// Accept one connection and deal with different failure modes.
233
zmq_assert (s != retired_fd);
235
struct sockaddr_storage ss = {0};
236
socklen_t ss_len = sizeof (ss);
237
fd_t sock = ::accept (s, (struct sockaddr *) &ss, &ss_len);
239
#ifdef ZMQ_HAVE_WINDOWS
240
if (sock == INVALID_SOCKET) {
241
wsa_assert (WSAGetLastError () == WSAEWOULDBLOCK ||
242
WSAGetLastError () == WSAECONNRESET);
245
// On Windows, preventing sockets to be inherited by child processes.
246
BOOL brc = SetHandleInformation ((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
250
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK ||
251
errno == EINTR || errno == ECONNABORTED || errno == EPROTO ||
257
if (!options.tcp_accept_filters.empty ()) {
258
bool matched = false;
259
for (options_t::tcp_accept_filters_t::size_type i = 0; i != options.tcp_accept_filters.size (); ++i) {
260
if (options.tcp_accept_filters[i].match_address ((struct sockaddr *) &ss, ss_len)) {
266
#ifdef ZMQ_HAVE_WINDOWS
267
int rc = closesocket (sock);
268
wsa_assert (rc != SOCKET_ERROR);
270
int rc = ::close (sock);
271
errno_assert (rc == 0);