1/*
2 Copyright (c) 2007-2019 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#include <new>
32
33#include <string>
34#include <stdio.h>
35
36#include "ws_listener.hpp"
37#include "io_thread.hpp"
38#include "config.hpp"
39#include "err.hpp"
40#include "ip.hpp"
41#include "tcp.hpp"
42#include "socket_base.hpp"
43#include "address.hpp"
44#include "ws_engine.hpp"
45#include "wss_address.hpp"
46#include "session_base.hpp"
47
48#ifdef ZMQ_HAVE_WSS
49#include "wss_engine.hpp"
50#endif
51
52#ifndef ZMQ_HAVE_WINDOWS
53#include <unistd.h>
54#include <sys/socket.h>
55#include <arpa/inet.h>
56#include <netinet/tcp.h>
57#include <netinet/in.h>
58#include <netdb.h>
59#include <fcntl.h>
60#ifdef ZMQ_HAVE_VXWORKS
61#include <sockLib.h>
62#endif
63#endif
64
65#ifdef ZMQ_HAVE_OPENVMS
66#include <ioctl.h>
67#endif
68
69zmq::ws_listener_t::ws_listener_t (io_thread_t *io_thread_,
70 socket_base_t *socket_,
71 const options_t &options_,
72 bool wss_) :
73 stream_listener_base_t (io_thread_, socket_, options_),
74 _wss (wss_)
75{
76#ifdef ZMQ_HAVE_WSS
77 if (_wss) {
78 int rc = gnutls_certificate_allocate_credentials (&_tls_cred);
79 assert (rc == GNUTLS_E_SUCCESS);
80
81 gnutls_datum_t cert = {(unsigned char *) options_.wss_cert_pem.c_str (),
82 (unsigned int) options_.wss_cert_pem.length ()};
83 gnutls_datum_t key = {(unsigned char *) options_.wss_key_pem.c_str (),
84 (unsigned int) options_.wss_key_pem.length ()};
85 rc = gnutls_certificate_set_x509_key_mem (_tls_cred, &cert, &key,
86 GNUTLS_X509_FMT_PEM);
87 assert (rc == GNUTLS_E_SUCCESS);
88 }
89#endif
90}
91
92zmq::ws_listener_t::~ws_listener_t ()
93{
94#ifdef ZMQ_HAVE_WSS
95 if (_wss)
96 gnutls_certificate_free_credentials (_tls_cred);
97#endif
98}
99
100void zmq::ws_listener_t::in_event ()
101{
102 fd_t fd = accept ();
103
104 // If connection was reset by the peer in the meantime, just ignore it.
105 // TODO: Handle specific errors like ENFILE/EMFILE etc.
106 if (fd == retired_fd) {
107 _socket->event_accept_failed (
108 make_unconnected_bind_endpoint_pair (_endpoint), zmq_errno ());
109 return;
110 }
111
112 int rc = tune_tcp_socket (fd);
113 rc = rc | tune_tcp_maxrt (fd, options.tcp_maxrt);
114 if (rc != 0) {
115 _socket->event_accept_failed (
116 make_unconnected_bind_endpoint_pair (_endpoint), zmq_errno ());
117 return;
118 }
119
120 // Create the engine object for this connection.
121 create_engine (fd);
122}
123
124std::string zmq::ws_listener_t::get_socket_name (zmq::fd_t fd_,
125 socket_end_t socket_end_) const
126{
127 if (_wss)
128 return zmq::get_socket_name<wss_address_t> (fd_, socket_end_);
129
130 return zmq::get_socket_name<ws_address_t> (fd_, socket_end_);
131}
132
133int zmq::ws_listener_t::create_socket (const char *addr_)
134{
135 tcp_address_t address;
136 _s = tcp_open_socket (addr_, options, true, true, &address);
137 if (_s == retired_fd) {
138 return -1;
139 }
140
141 // TODO why is this only done for the listener?
142 make_socket_noninheritable (_s);
143
144 // Allow reusing of the address.
145 int flag = 1;
146 int rc;
147#ifdef ZMQ_HAVE_WINDOWS
148 // TODO this was changed for Windows from SO_REUSEADDRE to
149 // SE_EXCLUSIVEADDRUSE by 0ab65324195ad70205514d465b03d851a6de051c,
150 // so the comment above is no longer correct; also, now the settings are
151 // different between listener and connecter with a src address.
152 // is this intentional?
153 rc = setsockopt (_s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
154 reinterpret_cast<const char *> (&flag), sizeof (int));
155 wsa_assert (rc != SOCKET_ERROR);
156#elif defined ZMQ_HAVE_VXWORKS
157 rc =
158 setsockopt (_s, SOL_SOCKET, SO_REUSEADDR, (char *) &flag, sizeof (int));
159 errno_assert (rc == 0);
160#else
161 rc = setsockopt (_s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
162 errno_assert (rc == 0);
163#endif
164
165 // Bind the socket to the network interface and port.
166#if defined ZMQ_HAVE_VXWORKS
167 rc = bind (_s, (sockaddr *) _address.addr (), _address.addrlen ());
168#else
169 rc = bind (_s, address.addr (), address.addrlen ());
170#endif
171#ifdef ZMQ_HAVE_WINDOWS
172 if (rc == SOCKET_ERROR) {
173 errno = wsa_error_to_errno (WSAGetLastError ());
174 goto error;
175 }
176#else
177 if (rc != 0)
178 goto error;
179#endif
180
181 // Listen for incoming connections.
182 rc = listen (_s, options.backlog);
183#ifdef ZMQ_HAVE_WINDOWS
184 if (rc == SOCKET_ERROR) {
185 errno = wsa_error_to_errno (WSAGetLastError ());
186 goto error;
187 }
188#else
189 if (rc != 0)
190 goto error;
191#endif
192
193 return 0;
194
195error:
196 int err = errno;
197 close ();
198 errno = err;
199 return -1;
200}
201
202int zmq::ws_listener_t::set_local_address (const char *addr_)
203{
204 if (options.use_fd != -1) {
205 // in this case, the addr_ passed is not used and ignored, since the
206 // socket was already created by the application
207 _s = options.use_fd;
208 } else {
209 int rc = _address.resolve (addr_, true, options.ipv6);
210 if (rc != 0)
211 return -1;
212
213 // remove the path, otherwise resolving the port will fail with wildcard
214 const char *delim = strrchr (addr_, '/');
215 std::string host_port = std::string (addr_, delim - addr_);
216 if (create_socket (host_port.c_str ()) == -1)
217 return -1;
218 }
219
220 _endpoint = get_socket_name (_s, socket_end_local);
221
222 _socket->event_listening (make_unconnected_bind_endpoint_pair (_endpoint),
223 _s);
224 return 0;
225}
226
227zmq::fd_t zmq::ws_listener_t::accept ()
228{
229 // The situation where connection cannot be accepted due to insufficient
230 // resources is considered valid and treated by ignoring the connection.
231 // Accept one connection and deal with different failure modes.
232 zmq_assert (_s != retired_fd);
233
234 struct sockaddr_storage ss;
235 memset (&ss, 0, sizeof (ss));
236#if defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_VXWORKS
237 int ss_len = sizeof (ss);
238#else
239 socklen_t ss_len = sizeof (ss);
240#endif
241#if defined ZMQ_HAVE_SOCK_CLOEXEC && defined HAVE_ACCEPT4
242 fd_t sock = ::accept4 (_s, reinterpret_cast<struct sockaddr *> (&ss),
243 &ss_len, SOCK_CLOEXEC);
244#else
245 fd_t sock =
246 ::accept (_s, reinterpret_cast<struct sockaddr *> (&ss), &ss_len);
247#endif
248
249 if (sock == retired_fd) {
250#if defined ZMQ_HAVE_WINDOWS
251 const int last_error = WSAGetLastError ();
252 wsa_assert (last_error == WSAEWOULDBLOCK || last_error == WSAECONNRESET
253 || last_error == WSAEMFILE || last_error == WSAENOBUFS);
254#elif defined ZMQ_HAVE_ANDROID
255 errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR
256 || errno == ECONNABORTED || errno == EPROTO
257 || errno == ENOBUFS || errno == ENOMEM || errno == EMFILE
258 || errno == ENFILE || errno == EINVAL);
259#else
260 errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR
261 || errno == ECONNABORTED || errno == EPROTO
262 || errno == ENOBUFS || errno == ENOMEM || errno == EMFILE
263 || errno == ENFILE);
264#endif
265 return retired_fd;
266 }
267
268 make_socket_noninheritable (sock);
269
270 if (zmq::set_nosigpipe (sock)) {
271#ifdef ZMQ_HAVE_WINDOWS
272 int rc = closesocket (sock);
273 wsa_assert (rc != SOCKET_ERROR);
274#else
275 int rc = ::close (sock);
276 errno_assert (rc == 0);
277#endif
278 return retired_fd;
279 }
280
281 // Set the IP Type-Of-Service priority for this client socket
282 if (options.tos != 0)
283 set_ip_type_of_service (sock, options.tos);
284
285 return sock;
286}
287
288void zmq::ws_listener_t::create_engine (fd_t fd_)
289{
290 const endpoint_uri_pair_t endpoint_pair (
291 get_socket_name (fd_, socket_end_local),
292 get_socket_name (fd_, socket_end_remote), endpoint_type_bind);
293
294 i_engine *engine = NULL;
295 if (_wss)
296#ifdef ZMQ_HAVE_WSS
297 engine = new (std::nothrow) wss_engine_t (
298 fd_, options, endpoint_pair, _address, false, _tls_cred, NULL);
299#else
300 assert (false);
301#endif
302 else
303 engine = new (std::nothrow)
304 ws_engine_t (fd_, options, endpoint_pair, _address, false);
305 alloc_assert (engine);
306
307 // Choose I/O thread to run connecter in. Given that we are already
308 // running in an I/O thread, there must be at least one available.
309 io_thread_t *io_thread = choose_io_thread (options.affinity);
310 zmq_assert (io_thread);
311
312 // Create and launch a session object.
313 session_base_t *session =
314 session_base_t::create (io_thread, false, _socket, options, NULL);
315 errno_assert (session);
316 session->inc_seqnum ();
317 launch_child (session);
318 send_attach (session, engine, false);
319
320 _socket->event_accepted (endpoint_pair, fd_);
321}
322