2011-07-26 00:43:57 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2011-07-26 00:43:57 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq 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 GNU Lesser General Public
|
|
|
|
License for more details.
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-18 10:56:52 -06:00
|
|
|
#include "precompiled.hpp"
|
2015-08-21 16:12:22 -07:00
|
|
|
#include "macros.hpp"
|
2011-07-26 00:43:57 +02:00
|
|
|
#include "platform.hpp"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <new>
|
2013-12-06 14:28:44 -08:00
|
|
|
#include <sstream>
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
#include "stream_engine.hpp"
|
2011-07-26 00:43:57 +02:00
|
|
|
#include "io_thread.hpp"
|
2011-09-15 10:00:23 +02:00
|
|
|
#include "session_base.hpp"
|
2012-09-05 02:01:19 +02:00
|
|
|
#include "v1_encoder.hpp"
|
|
|
|
#include "v1_decoder.hpp"
|
2013-03-12 15:26:07 +01:00
|
|
|
#include "v2_encoder.hpp"
|
|
|
|
#include "v2_decoder.hpp"
|
2013-05-13 22:34:27 +02:00
|
|
|
#include "null_mechanism.hpp"
|
2014-05-12 06:08:28 +02:00
|
|
|
#include "plain_client.hpp"
|
|
|
|
#include "plain_server.hpp"
|
2013-09-30 14:13:03 -05:00
|
|
|
#include "gssapi_client.hpp"
|
|
|
|
#include "gssapi_server.hpp"
|
2013-06-22 11:48:33 +02:00
|
|
|
#include "curve_client.hpp"
|
|
|
|
#include "curve_server.hpp"
|
2012-10-29 00:03:36 -07:00
|
|
|
#include "raw_decoder.hpp"
|
|
|
|
#include "raw_encoder.hpp"
|
2011-07-26 00:43:57 +02:00
|
|
|
#include "config.hpp"
|
|
|
|
#include "err.hpp"
|
2011-07-29 09:37:43 +02:00
|
|
|
#include "ip.hpp"
|
2014-06-22 10:32:22 +02:00
|
|
|
#include "tcp.hpp"
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
#include "likely.hpp"
|
|
|
|
#include "wire.hpp"
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2014-04-25 13:47:07 +09:30
|
|
|
zmq::stream_engine_t::stream_engine_t (fd_t fd_, const options_t &options_,
|
2013-05-17 17:46:30 +02:00
|
|
|
const std::string &endpoint_) :
|
2011-07-28 08:06:21 +02:00
|
|
|
s (fd_),
|
2016-02-21 23:42:12 +00:00
|
|
|
as_server(false),
|
|
|
|
handle(NULL),
|
2011-07-26 00:43:57 +02:00
|
|
|
inpos (NULL),
|
|
|
|
insize (0),
|
2012-09-04 19:44:20 +02:00
|
|
|
decoder (NULL),
|
2011-07-26 00:43:57 +02:00
|
|
|
outpos (NULL),
|
|
|
|
outsize (0),
|
2012-09-04 19:44:20 +02:00
|
|
|
encoder (NULL),
|
2014-05-02 12:33:26 +02:00
|
|
|
metadata (NULL),
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
handshaking (true),
|
2013-04-28 10:15:25 +02:00
|
|
|
greeting_size (v2_greeting_size),
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
greeting_bytes_read (0),
|
2011-07-26 00:43:57 +02:00
|
|
|
session (NULL),
|
|
|
|
options (options_),
|
2012-11-17 10:06:09 +00:00
|
|
|
endpoint (endpoint_),
|
2012-09-21 12:53:31 +01:00
|
|
|
plugged (false),
|
2014-05-05 19:51:38 +02:00
|
|
|
next_msg (&stream_engine_t::identity_msg),
|
|
|
|
process_msg (&stream_engine_t::process_identity_msg),
|
2013-03-18 02:00:00 +01:00
|
|
|
io_error (false),
|
|
|
|
subscription_required (false),
|
2013-05-13 22:34:27 +02:00
|
|
|
mechanism (NULL),
|
2013-09-26 09:37:04 +02:00
|
|
|
input_stopped (false),
|
|
|
|
output_stopped (false),
|
2014-05-09 13:54:24 +00:00
|
|
|
has_handshake_timer (false),
|
2015-03-16 21:39:16 -04:00
|
|
|
has_ttl_timer (false),
|
|
|
|
has_timeout_timer (false),
|
|
|
|
has_heartbeat_timer (false),
|
2015-06-26 14:08:08 -04:00
|
|
|
heartbeat_timeout (0),
|
2012-09-21 12:53:31 +01:00
|
|
|
socket (NULL)
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
2013-03-18 02:00:00 +01:00
|
|
|
int rc = tx_msg.init ();
|
|
|
|
errno_assert (rc == 0);
|
2014-04-25 13:47:07 +09:30
|
|
|
|
2012-04-29 17:13:18 +02:00
|
|
|
// Put the socket into non-blocking mode.
|
2011-07-29 09:37:43 +02:00
|
|
|
unblock_socket (s);
|
2011-07-28 08:06:21 +02:00
|
|
|
|
2013-12-06 14:28:44 -08:00
|
|
|
int family = get_peer_ip_address (s, peer_address);
|
|
|
|
if (family == 0)
|
2014-10-09 09:41:49 +04:00
|
|
|
peer_address.clear();
|
2013-12-06 14:28:44 -08:00
|
|
|
#if defined ZMQ_HAVE_SO_PEERCRED
|
2014-01-01 17:11:40 +01:00
|
|
|
else
|
|
|
|
if (family == PF_UNIX) {
|
2013-12-06 14:28:44 -08:00
|
|
|
struct ucred cred;
|
|
|
|
socklen_t size = sizeof (cred);
|
|
|
|
if (!getsockopt (s, SOL_SOCKET, SO_PEERCRED, &cred, &size)) {
|
|
|
|
std::ostringstream buf;
|
|
|
|
buf << ":" << cred.uid << ":" << cred.gid << ":" << cred.pid;
|
|
|
|
peer_address += buf.str ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined ZMQ_HAVE_LOCAL_PEERCRED
|
2014-01-01 17:11:40 +01:00
|
|
|
else
|
|
|
|
if (family == PF_UNIX) {
|
2013-12-06 14:28:44 -08:00
|
|
|
struct xucred cred;
|
|
|
|
socklen_t size = sizeof (cred);
|
|
|
|
if (!getsockopt (s, 0, LOCAL_PEERCRED, &cred, &size)
|
|
|
|
&& cred.cr_version == XUCRED_VERSION) {
|
|
|
|
std::ostringstream buf;
|
|
|
|
buf << ":" << cred.cr_uid << ":";
|
|
|
|
if (cred.cr_ngroups > 0)
|
|
|
|
buf << cred.cr_groups[0];
|
|
|
|
buf << ":";
|
|
|
|
peer_address += buf.str ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-18 09:39:19 +02:00
|
|
|
|
2012-06-06 15:08:00 +02:00
|
|
|
#ifdef SO_NOSIGPIPE
|
2011-07-28 08:06:21 +02:00
|
|
|
// Make sure that SIGPIPE signal is not generated when writing to a
|
|
|
|
// connection that was already closed by the peer.
|
|
|
|
int set = 1;
|
2013-04-23 22:58:28 +01:00
|
|
|
rc = setsockopt (s, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof (int));
|
2011-07-28 08:06:21 +02:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
#endif
|
2015-06-26 14:08:08 -04:00
|
|
|
if(options.heartbeat_interval > 0) {
|
|
|
|
heartbeat_timeout = options.heartbeat_timeout;
|
|
|
|
if(heartbeat_timeout == -1)
|
|
|
|
heartbeat_timeout = options.heartbeat_interval;
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
zmq::stream_engine_t::~stream_engine_t ()
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
|
|
|
zmq_assert (!plugged);
|
|
|
|
|
2011-07-28 08:06:21 +02:00
|
|
|
if (s != retired_fd) {
|
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2013-03-12 15:26:07 +01:00
|
|
|
int rc = closesocket (s);
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
2011-07-28 08:06:21 +02:00
|
|
|
#else
|
2013-03-12 15:26:07 +01:00
|
|
|
int rc = close (s);
|
2011-07-28 08:06:21 +02:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
#endif
|
2013-03-12 15:26:07 +01:00
|
|
|
s = retired_fd;
|
2011-07-28 08:06:21 +02:00
|
|
|
}
|
2012-09-04 19:44:20 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
int rc = tx_msg.close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2014-05-02 12:33:26 +02:00
|
|
|
// Drop reference to metadata and destroy it if we are
|
|
|
|
// the only user.
|
2015-08-17 00:35:11 +04:30
|
|
|
if (metadata != NULL) {
|
|
|
|
if (metadata->drop_ref ()) {
|
|
|
|
LIBZMQ_DELETE(metadata);
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 12:33:26 +02:00
|
|
|
|
2015-08-17 00:35:11 +04:30
|
|
|
LIBZMQ_DELETE(encoder);
|
|
|
|
LIBZMQ_DELETE(decoder);
|
|
|
|
LIBZMQ_DELETE(mechanism);
|
2011-07-26 00:43:57 +02:00
|
|
|
}
|
|
|
|
|
2011-09-15 10:00:23 +02:00
|
|
|
void zmq::stream_engine_t::plug (io_thread_t *io_thread_,
|
|
|
|
session_base_t *session_)
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
|
|
|
zmq_assert (!plugged);
|
|
|
|
plugged = true;
|
|
|
|
|
|
|
|
// Connect to session object.
|
|
|
|
zmq_assert (!session);
|
|
|
|
zmq_assert (session_);
|
|
|
|
session = session_;
|
2012-09-21 12:53:31 +01:00
|
|
|
socket = session-> get_socket ();
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
// Connect to I/O threads poller object.
|
|
|
|
io_object_t::plug (io_thread_);
|
|
|
|
handle = add_fd (s);
|
2013-03-18 02:00:00 +01:00
|
|
|
io_error = false;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
2015-01-23 15:25:40 +01:00
|
|
|
if (options.raw_socket) {
|
2012-10-29 00:03:36 -07:00
|
|
|
// no handshaking for raw sock, instantiate raw encoder and decoders
|
2016-02-09 10:13:49 +01:00
|
|
|
encoder = new (std::nothrow) raw_encoder_t (options.sndbuf);
|
2012-10-29 00:03:36 -07:00
|
|
|
alloc_assert (encoder);
|
|
|
|
|
2016-02-09 10:13:49 +01:00
|
|
|
decoder = new (std::nothrow) raw_decoder_t (options.rcvbuf);
|
2012-10-29 00:03:36 -07:00
|
|
|
alloc_assert (decoder);
|
|
|
|
|
|
|
|
// disable handshaking for raw socket
|
|
|
|
handshaking = false;
|
2013-04-12 11:59:49 +02:00
|
|
|
|
2014-05-05 19:51:38 +02:00
|
|
|
next_msg = &stream_engine_t::pull_msg_from_session;
|
2015-02-18 12:28:58 -06:00
|
|
|
process_msg = &stream_engine_t::push_raw_msg_to_session;
|
|
|
|
|
2015-02-18 21:24:57 -06:00
|
|
|
properties_t properties;
|
|
|
|
if (init_properties(properties)) {
|
2015-02-18 12:28:58 -06:00
|
|
|
// Compile metadata.
|
|
|
|
zmq_assert (metadata == NULL);
|
|
|
|
metadata = new (std::nothrow) metadata_t (properties);
|
|
|
|
}
|
2014-01-17 23:21:42 +01:00
|
|
|
|
2015-01-23 15:25:40 +01:00
|
|
|
if (options.raw_notify) {
|
|
|
|
// For raw sockets, send an initial 0-length message to the
|
|
|
|
// application so that it knows a peer has connected.
|
|
|
|
msg_t connector;
|
|
|
|
connector.init();
|
2015-02-18 12:28:58 -06:00
|
|
|
push_raw_msg_to_session (&connector);
|
2015-01-23 15:25:40 +01:00
|
|
|
connector.close();
|
|
|
|
session->flush ();
|
|
|
|
}
|
2012-10-30 12:18:13 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-05-09 13:54:24 +00:00
|
|
|
// start optional timer, to prevent handshake hanging on no input
|
|
|
|
set_handshake_timer ();
|
|
|
|
|
2012-10-29 00:03:36 -07:00
|
|
|
// Send the 'length' and 'flags' fields of the identity message.
|
|
|
|
// The 'length' field is encoded in the long format.
|
2013-03-12 15:56:10 +01:00
|
|
|
outpos = greeting_send;
|
2012-10-29 00:03:36 -07:00
|
|
|
outpos [outsize++] = 0xff;
|
|
|
|
put_uint64 (&outpos [outsize], options.identity_size + 1);
|
|
|
|
outsize += 8;
|
|
|
|
outpos [outsize++] = 0x7f;
|
|
|
|
}
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
set_pollin (handle);
|
|
|
|
set_pollout (handle);
|
|
|
|
// Flush all the data that may have been already received downstream.
|
|
|
|
in_event ();
|
|
|
|
}
|
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
void zmq::stream_engine_t::unplug ()
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
|
|
|
zmq_assert (plugged);
|
|
|
|
plugged = false;
|
|
|
|
|
2014-05-09 13:54:24 +00:00
|
|
|
// Cancel all timers.
|
|
|
|
if (has_handshake_timer) {
|
|
|
|
cancel_timer (handshake_timer_id);
|
|
|
|
has_handshake_timer = false;
|
|
|
|
}
|
|
|
|
|
2015-03-16 21:39:16 -04:00
|
|
|
if (has_ttl_timer) {
|
|
|
|
cancel_timer (heartbeat_ttl_timer_id);
|
|
|
|
has_ttl_timer = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_timeout_timer) {
|
|
|
|
cancel_timer (heartbeat_timeout_timer_id);
|
|
|
|
has_timeout_timer = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_heartbeat_timer) {
|
|
|
|
cancel_timer (heartbeat_ivl_timer_id);
|
|
|
|
has_heartbeat_timer = false;
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
// Cancel all fd subscriptions.
|
2013-03-18 02:00:00 +01:00
|
|
|
if (!io_error)
|
2012-11-10 23:05:10 +01:00
|
|
|
rm_fd (handle);
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
// Disconnect from I/O threads poller object.
|
|
|
|
io_object_t::unplug ();
|
|
|
|
|
|
|
|
session = NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
void zmq::stream_engine_t::terminate ()
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
|
|
|
unplug ();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
void zmq::stream_engine_t::in_event ()
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
2014-01-08 21:11:51 +01:00
|
|
|
zmq_assert (!io_error);
|
2013-03-18 02:00:00 +01:00
|
|
|
|
2012-12-23 17:47:44 +01:00
|
|
|
// If still handshaking, receive and process the greeting message.
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
if (unlikely (handshaking))
|
|
|
|
if (!handshake ())
|
|
|
|
return;
|
|
|
|
|
2012-09-04 19:44:20 +02:00
|
|
|
zmq_assert (decoder);
|
2013-03-18 02:00:00 +01:00
|
|
|
|
|
|
|
// If there has been an I/O error, stop polling.
|
2013-09-26 09:37:04 +02:00
|
|
|
if (input_stopped) {
|
2013-03-18 02:00:00 +01:00
|
|
|
rm_fd (handle);
|
|
|
|
io_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
// If there's no data to process in the buffer...
|
|
|
|
if (!insize) {
|
|
|
|
|
|
|
|
// Retrieve the buffer and read as much data as possible.
|
|
|
|
// Note that buffer can be arbitrarily large. However, we assume
|
|
|
|
// the underlying TCP layer has fixed buffer size and thus the
|
|
|
|
// number of bytes read will be always limited.
|
2013-10-09 10:51:30 +02:00
|
|
|
size_t bufsize = 0;
|
|
|
|
decoder->get_buffer (&inpos, &bufsize);
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2014-06-22 10:32:22 +02:00
|
|
|
const int rc = tcp_read (s, inpos, bufsize);
|
2015-06-10 22:09:55 +02:00
|
|
|
|
2013-10-09 10:51:30 +02:00
|
|
|
if (rc == 0) {
|
2014-05-17 21:04:38 +02:00
|
|
|
error (connection_error);
|
2013-03-18 02:00:00 +01:00
|
|
|
return;
|
2011-07-26 00:43:57 +02:00
|
|
|
}
|
2013-10-09 10:51:30 +02:00
|
|
|
if (rc == -1) {
|
|
|
|
if (errno != EAGAIN)
|
2014-05-17 21:04:38 +02:00
|
|
|
error (connection_error);
|
2013-10-09 10:51:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
// Adjust input size
|
2013-10-09 10:51:30 +02:00
|
|
|
insize = static_cast <size_t> (rc);
|
2015-06-10 22:09:55 +02:00
|
|
|
// Adjust buffer size to received bytes
|
|
|
|
decoder->resize_buffer(insize);
|
2012-10-30 12:18:13 +01:00
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
int rc = 0;
|
|
|
|
size_t processed = 0;
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
while (insize > 0) {
|
|
|
|
rc = decoder->decode (inpos, insize, processed);
|
|
|
|
zmq_assert (processed <= insize);
|
2011-07-26 00:43:57 +02:00
|
|
|
inpos += processed;
|
|
|
|
insize -= processed;
|
2013-03-18 02:00:00 +01:00
|
|
|
if (rc == 0 || rc == -1)
|
|
|
|
break;
|
2014-05-05 19:51:38 +02:00
|
|
|
rc = (this->*process_msg) (decoder->msg ());
|
2013-03-18 02:00:00 +01:00
|
|
|
if (rc == -1)
|
|
|
|
break;
|
2011-07-26 00:43:57 +02:00
|
|
|
}
|
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
// Tear down the connection if we have failed to decode input data
|
|
|
|
// or the session has rejected the message.
|
|
|
|
if (rc == -1) {
|
|
|
|
if (errno != EAGAIN) {
|
2014-05-18 08:12:17 +02:00
|
|
|
error (protocol_error);
|
2013-03-18 02:00:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-09-26 09:37:04 +02:00
|
|
|
input_stopped = true;
|
2013-03-18 02:00:00 +01:00
|
|
|
reset_pollin (handle);
|
2012-04-29 17:13:18 +02:00
|
|
|
}
|
2013-03-18 02:00:00 +01:00
|
|
|
|
|
|
|
session->flush ();
|
2011-07-26 00:43:57 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
void zmq::stream_engine_t::out_event ()
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
2013-03-18 02:00:00 +01:00
|
|
|
zmq_assert (!io_error);
|
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
// If write buffer is empty, try to read new data from the encoder.
|
|
|
|
if (!outsize) {
|
|
|
|
|
2012-11-02 11:11:14 +01:00
|
|
|
// Even when we stop polling as soon as there is no
|
|
|
|
// data to send, the poller may invoke out_event one
|
|
|
|
// more time due to 'speculative write' optimisation.
|
|
|
|
if (unlikely (encoder == NULL)) {
|
|
|
|
zmq_assert (handshaking);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
outpos = NULL;
|
2013-03-18 02:00:00 +01:00
|
|
|
outsize = encoder->encode (&outpos, 0);
|
|
|
|
|
2016-02-09 10:13:49 +01:00
|
|
|
while (outsize < (size_t) options.sndbuf) {
|
2014-05-05 19:51:38 +02:00
|
|
|
if ((this->*next_msg) (&tx_msg) == -1)
|
2013-03-18 02:00:00 +01:00
|
|
|
break;
|
|
|
|
encoder->load_msg (&tx_msg);
|
|
|
|
unsigned char *bufptr = outpos + outsize;
|
2016-02-09 10:13:49 +01:00
|
|
|
size_t n = encoder->encode (&bufptr, options.sndbuf - outsize);
|
2013-03-18 02:00:00 +01:00
|
|
|
zmq_assert (n > 0);
|
|
|
|
if (outpos == NULL)
|
|
|
|
outpos = bufptr;
|
|
|
|
outsize += n;
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
// If there is no data to send, stop polling for output.
|
|
|
|
if (outsize == 0) {
|
2013-09-26 09:37:04 +02:00
|
|
|
output_stopped = true;
|
2011-07-26 00:43:57 +02:00
|
|
|
reset_pollout (handle);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are any data to write in write buffer, write as much as
|
|
|
|
// possible to the socket. Note that amount of data to write can be
|
2013-03-12 15:26:07 +01:00
|
|
|
// arbitrarily large. However, we assume that underlying TCP layer has
|
2011-07-26 00:43:57 +02:00
|
|
|
// limited transmission buffer and thus the actual number of bytes
|
|
|
|
// written should be reasonably modest.
|
2014-06-22 10:32:22 +02:00
|
|
|
const int nbytes = tcp_write (s, outpos, outsize);
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2012-04-29 17:13:18 +02:00
|
|
|
// IO error has occurred. We stop waiting for output events.
|
|
|
|
// The engine is not terminated until we detect input error;
|
2013-03-12 15:26:07 +01:00
|
|
|
// this is necessary to prevent losing incoming messages.
|
2011-07-26 00:43:57 +02:00
|
|
|
if (nbytes == -1) {
|
2012-04-29 17:13:18 +02:00
|
|
|
reset_pollout (handle);
|
2011-07-26 00:43:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
outpos += nbytes;
|
|
|
|
outsize -= nbytes;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
|
|
|
// If we are still handshaking and there are no data
|
|
|
|
// to send, stop polling for output.
|
|
|
|
if (unlikely (handshaking))
|
|
|
|
if (outsize == 0)
|
|
|
|
reset_pollout (handle);
|
2011-07-26 00:43:57 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 09:37:04 +02:00
|
|
|
void zmq::stream_engine_t::restart_output ()
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
2013-03-18 02:00:00 +01:00
|
|
|
if (unlikely (io_error))
|
|
|
|
return;
|
|
|
|
|
2013-09-26 09:37:04 +02:00
|
|
|
if (likely (output_stopped)) {
|
2013-06-06 13:13:10 +02:00
|
|
|
set_pollout (handle);
|
2013-09-26 09:37:04 +02:00
|
|
|
output_stopped = false;
|
2013-06-06 13:13:10 +02:00
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
// Speculative write: The assumption is that at the moment new message
|
|
|
|
// was sent by the user the socket is probably available for writing.
|
|
|
|
// Thus we try to write the data to socket avoiding polling for POLLOUT.
|
|
|
|
// Consequently, the latency should be better in request/reply scenarios.
|
|
|
|
out_event ();
|
|
|
|
}
|
|
|
|
|
2013-09-26 09:37:04 +02:00
|
|
|
void zmq::stream_engine_t::restart_input ()
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
2013-09-26 09:37:04 +02:00
|
|
|
zmq_assert (input_stopped);
|
2013-03-18 02:00:00 +01:00
|
|
|
zmq_assert (session != NULL);
|
|
|
|
zmq_assert (decoder != NULL);
|
|
|
|
|
2014-05-05 19:51:38 +02:00
|
|
|
int rc = (this->*process_msg) (decoder->msg ());
|
2013-03-18 02:00:00 +01:00
|
|
|
if (rc == -1) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
session->flush ();
|
|
|
|
else
|
2014-05-17 21:04:38 +02:00
|
|
|
error (protocol_error);
|
2012-04-29 17:13:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
while (insize > 0) {
|
|
|
|
size_t processed = 0;
|
|
|
|
rc = decoder->decode (inpos, insize, processed);
|
|
|
|
zmq_assert (processed <= insize);
|
|
|
|
inpos += processed;
|
|
|
|
insize -= processed;
|
|
|
|
if (rc == 0 || rc == -1)
|
|
|
|
break;
|
2014-05-05 19:51:38 +02:00
|
|
|
rc = (this->*process_msg) (decoder->msg ());
|
2013-03-18 02:00:00 +01:00
|
|
|
if (rc == -1)
|
|
|
|
break;
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
if (rc == -1 && errno == EAGAIN)
|
|
|
|
session->flush ();
|
|
|
|
else
|
2014-05-17 21:04:38 +02:00
|
|
|
if (io_error)
|
|
|
|
error (connection_error);
|
|
|
|
else
|
|
|
|
if (rc == -1)
|
|
|
|
error (protocol_error);
|
2013-03-18 02:00:00 +01:00
|
|
|
else {
|
2013-09-26 09:37:04 +02:00
|
|
|
input_stopped = false;
|
2013-03-18 02:00:00 +01:00
|
|
|
set_pollin (handle);
|
|
|
|
session->flush ();
|
|
|
|
|
|
|
|
// Speculative read.
|
|
|
|
in_event ();
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
}
|
|
|
|
|
2012-09-05 16:37:20 +02:00
|
|
|
bool zmq::stream_engine_t::handshake ()
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
{
|
2012-09-05 16:37:20 +02:00
|
|
|
zmq_assert (handshaking);
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
zmq_assert (greeting_bytes_read < greeting_size);
|
2012-09-05 16:37:20 +02:00
|
|
|
// Receive the greeting.
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
while (greeting_bytes_read < greeting_size) {
|
2014-06-22 10:32:22 +02:00
|
|
|
const int n = tcp_read (s, greeting_recv + greeting_bytes_read,
|
|
|
|
greeting_size - greeting_bytes_read);
|
2013-10-09 10:51:30 +02:00
|
|
|
if (n == 0) {
|
2014-05-17 21:04:38 +02:00
|
|
|
error (connection_error);
|
2012-09-05 16:37:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-09 10:51:30 +02:00
|
|
|
if (n == -1) {
|
|
|
|
if (errno != EAGAIN)
|
2014-05-17 21:04:38 +02:00
|
|
|
error (connection_error);
|
2012-09-05 16:37:20 +02:00
|
|
|
return false;
|
2013-10-09 10:51:30 +02:00
|
|
|
}
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
|
|
|
greeting_bytes_read += n;
|
|
|
|
|
2012-09-05 16:37:20 +02:00
|
|
|
// We have received at least one byte from the peer.
|
|
|
|
// If the first byte is not 0xff, we know that the
|
|
|
|
// peer is using unversioned protocol.
|
2013-03-12 15:56:10 +01:00
|
|
|
if (greeting_recv [0] != 0xff)
|
2012-09-05 16:37:20 +02:00
|
|
|
break;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
2013-04-28 10:15:25 +02:00
|
|
|
if (greeting_bytes_read < signature_size)
|
2012-09-05 16:37:20 +02:00
|
|
|
continue;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
2012-09-05 16:37:20 +02:00
|
|
|
// Inspect the right-most bit of the 10th byte (which coincides
|
|
|
|
// with the 'flags' field if a regular message was sent).
|
|
|
|
// Zero indicates this is a header of identity message
|
|
|
|
// (i.e. the peer is using the unversioned protocol).
|
2013-03-12 15:56:10 +01:00
|
|
|
if (!(greeting_recv [9] & 0x01))
|
2012-09-05 16:37:20 +02:00
|
|
|
break;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
2012-09-05 16:37:20 +02:00
|
|
|
// The peer is using versioned protocol.
|
2013-04-28 10:15:25 +02:00
|
|
|
// Send the major version number.
|
|
|
|
if (outpos + outsize == greeting_send + signature_size) {
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
if (outsize == 0)
|
|
|
|
set_pollout (handle);
|
2013-04-28 10:15:25 +02:00
|
|
|
outpos [outsize++] = 3; // Major version number
|
|
|
|
}
|
|
|
|
|
|
|
|
if (greeting_bytes_read > signature_size) {
|
|
|
|
if (outpos + outsize == greeting_send + signature_size + 1) {
|
|
|
|
if (outsize == 0)
|
|
|
|
set_pollout (handle);
|
|
|
|
|
|
|
|
// Use ZMTP/2.0 to talk to older peers.
|
|
|
|
if (greeting_recv [10] == ZMTP_1_0
|
|
|
|
|| greeting_recv [10] == ZMTP_2_0)
|
|
|
|
outpos [outsize++] = options.type;
|
|
|
|
else {
|
|
|
|
outpos [outsize++] = 0; // Minor version number
|
|
|
|
memset (outpos + outsize, 0, 20);
|
2013-06-22 11:48:33 +02:00
|
|
|
|
|
|
|
zmq_assert (options.mechanism == ZMQ_NULL
|
|
|
|
|| options.mechanism == ZMQ_PLAIN
|
2013-09-24 22:20:01 -05:00
|
|
|
|| options.mechanism == ZMQ_CURVE
|
|
|
|
|| options.mechanism == ZMQ_GSSAPI);
|
2013-06-22 11:48:33 +02:00
|
|
|
|
2013-05-17 15:49:26 +02:00
|
|
|
if (options.mechanism == ZMQ_NULL)
|
|
|
|
memcpy (outpos + outsize, "NULL", 4);
|
|
|
|
else
|
2013-06-22 11:48:33 +02:00
|
|
|
if (options.mechanism == ZMQ_PLAIN)
|
2013-05-17 15:49:26 +02:00
|
|
|
memcpy (outpos + outsize, "PLAIN", 5);
|
2013-06-22 11:48:33 +02:00
|
|
|
else
|
2013-09-24 22:20:01 -05:00
|
|
|
if (options.mechanism == ZMQ_GSSAPI)
|
|
|
|
memcpy (outpos + outsize, "GSSAPI", 6);
|
2013-06-22 11:48:33 +02:00
|
|
|
else
|
2014-04-29 22:21:58 +02:00
|
|
|
if (options.mechanism == ZMQ_CURVE)
|
2013-06-22 11:48:33 +02:00
|
|
|
memcpy (outpos + outsize, "CURVE", 5);
|
2013-04-28 10:15:25 +02:00
|
|
|
outsize += 20;
|
|
|
|
memset (outpos + outsize, 0, 32);
|
|
|
|
outsize += 32;
|
|
|
|
greeting_size = v3_greeting_size;
|
|
|
|
}
|
|
|
|
}
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 15:26:07 +01:00
|
|
|
// Position of the revision field in the greeting.
|
|
|
|
const size_t revision_pos = 10;
|
2012-09-05 02:01:19 +02:00
|
|
|
|
2013-03-12 15:26:07 +01:00
|
|
|
// Is the peer using ZMTP/1.0 with no revision number?
|
|
|
|
// If so, we send and receive rest of identity message
|
2013-03-12 15:56:10 +01:00
|
|
|
if (greeting_recv [0] != 0xff || !(greeting_recv [9] & 0x01)) {
|
2014-12-03 13:46:28 -08:00
|
|
|
if (session->zap_enabled ()) {
|
2014-12-03 12:39:28 -08:00
|
|
|
// reject ZMTP 1.0 connections if ZAP is enabled
|
|
|
|
error (protocol_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-09 10:13:49 +01:00
|
|
|
encoder = new (std::nothrow) v1_encoder_t (options.sndbuf);
|
2012-09-05 02:01:19 +02:00
|
|
|
alloc_assert (encoder);
|
|
|
|
|
2016-02-09 10:13:49 +01:00
|
|
|
decoder = new (std::nothrow) v1_decoder_t (options.rcvbuf, options.maxmsgsize);
|
2012-09-05 02:01:19 +02:00
|
|
|
alloc_assert (decoder);
|
|
|
|
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
// We have already sent the message header.
|
|
|
|
// Since there is no way to tell the encoder to
|
|
|
|
// skip the message header, we simply throw that
|
|
|
|
// header data away.
|
|
|
|
const size_t header_size = options.identity_size + 1 >= 255 ? 10 : 2;
|
|
|
|
unsigned char tmp [10], *bufferp = tmp;
|
2013-06-21 16:23:38 +02:00
|
|
|
|
|
|
|
// Prepare the identity message and load it into encoder.
|
|
|
|
// Then consume bytes we have already sent to the peer.
|
|
|
|
const int rc = tx_msg.init_size (options.identity_size);
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
memcpy (tx_msg.data (), options.identity, options.identity_size);
|
|
|
|
encoder->load_msg (&tx_msg);
|
2013-03-18 02:00:00 +01:00
|
|
|
size_t buffer_size = encoder->encode (&bufferp, header_size);
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
zmq_assert (buffer_size == header_size);
|
|
|
|
|
|
|
|
// Make sure the decoder sees the data we have already received.
|
2013-03-12 15:56:10 +01:00
|
|
|
inpos = greeting_recv;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
insize = greeting_bytes_read;
|
2012-09-02 18:19:15 +02:00
|
|
|
|
|
|
|
// To allow for interoperability with peers that do not forward
|
2013-09-04 17:59:45 +02:00
|
|
|
// their subscriptions, we inject a phantom subscription message
|
|
|
|
// message into the incoming message stream.
|
2012-09-02 18:19:15 +02:00
|
|
|
if (options.type == ZMQ_PUB || options.type == ZMQ_XPUB)
|
2013-03-18 02:00:00 +01:00
|
|
|
subscription_required = true;
|
2013-11-30 21:24:32 +01:00
|
|
|
|
|
|
|
// We are sending our identity now and the next message
|
|
|
|
// will come from the socket.
|
2014-05-05 19:51:38 +02:00
|
|
|
next_msg = &stream_engine_t::pull_msg_from_session;
|
2013-11-30 21:24:32 +01:00
|
|
|
|
|
|
|
// We are expecting identity message.
|
2014-05-05 19:51:38 +02:00
|
|
|
process_msg = &stream_engine_t::process_identity_msg;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
}
|
2012-09-05 02:01:19 +02:00
|
|
|
else
|
2013-03-12 15:56:10 +01:00
|
|
|
if (greeting_recv [revision_pos] == ZMTP_1_0) {
|
2014-12-03 13:46:28 -08:00
|
|
|
if (session->zap_enabled ()) {
|
2014-12-03 12:39:28 -08:00
|
|
|
// reject ZMTP 1.0 connections if ZAP is enabled
|
|
|
|
error (protocol_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-12 15:38:48 +01:00
|
|
|
encoder = new (std::nothrow) v1_encoder_t (
|
2016-02-09 10:13:49 +01:00
|
|
|
options.sndbuf);
|
2012-09-05 02:01:19 +02:00
|
|
|
alloc_assert (encoder);
|
|
|
|
|
2013-03-12 15:38:48 +01:00
|
|
|
decoder = new (std::nothrow) v1_decoder_t (
|
2016-02-09 10:13:49 +01:00
|
|
|
options.rcvbuf, options.maxmsgsize);
|
2012-09-05 02:01:19 +02:00
|
|
|
alloc_assert (decoder);
|
|
|
|
}
|
2013-04-28 10:15:25 +02:00
|
|
|
else
|
|
|
|
if (greeting_recv [revision_pos] == ZMTP_2_0) {
|
2014-12-03 13:46:28 -08:00
|
|
|
if (session->zap_enabled ()) {
|
2014-12-03 12:39:28 -08:00
|
|
|
// reject ZMTP 2.0 connections if ZAP is enabled
|
|
|
|
error (protocol_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-09 10:13:49 +01:00
|
|
|
encoder = new (std::nothrow) v2_encoder_t (options.sndbuf);
|
2013-04-28 10:15:25 +02:00
|
|
|
alloc_assert (encoder);
|
|
|
|
|
|
|
|
decoder = new (std::nothrow) v2_decoder_t (
|
2016-02-09 10:13:49 +01:00
|
|
|
options.rcvbuf, options.maxmsgsize);
|
2013-04-28 10:15:25 +02:00
|
|
|
alloc_assert (decoder);
|
|
|
|
}
|
2013-04-14 22:47:17 +02:00
|
|
|
else {
|
2016-02-09 10:13:49 +01:00
|
|
|
encoder = new (std::nothrow) v2_encoder_t (options.sndbuf);
|
2012-09-05 02:01:19 +02:00
|
|
|
alloc_assert (encoder);
|
|
|
|
|
2013-03-12 15:38:48 +01:00
|
|
|
decoder = new (std::nothrow) v2_decoder_t (
|
2016-02-09 10:13:49 +01:00
|
|
|
options.rcvbuf, options.maxmsgsize);
|
2012-09-05 02:01:19 +02:00
|
|
|
alloc_assert (decoder);
|
2013-04-28 10:15:25 +02:00
|
|
|
|
2014-09-19 19:24:45 +02:00
|
|
|
if (options.mechanism == ZMQ_NULL
|
|
|
|
&& memcmp (greeting_recv + 12, "NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
|
2013-07-18 10:10:10 +02:00
|
|
|
mechanism = new (std::nothrow)
|
|
|
|
null_mechanism_t (session, peer_address, options);
|
2013-05-13 22:34:27 +02:00
|
|
|
alloc_assert (mechanism);
|
2013-05-14 10:41:37 +02:00
|
|
|
}
|
|
|
|
else
|
2014-09-19 19:24:45 +02:00
|
|
|
if (options.mechanism == ZMQ_PLAIN
|
|
|
|
&& memcmp (greeting_recv + 12, "PLAIN\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
|
2014-05-12 06:08:28 +02:00
|
|
|
if (options.as_server)
|
|
|
|
mechanism = new (std::nothrow)
|
|
|
|
plain_server_t (session, peer_address, options);
|
|
|
|
else
|
|
|
|
mechanism = new (std::nothrow)
|
|
|
|
plain_client_t (options);
|
2013-05-14 10:41:37 +02:00
|
|
|
alloc_assert (mechanism);
|
2013-04-28 10:15:25 +02:00
|
|
|
}
|
2016-02-11 13:32:01 +01:00
|
|
|
#ifdef ZMQ_HAVE_CURVE
|
2013-06-22 11:48:33 +02:00
|
|
|
else
|
2014-09-19 19:24:45 +02:00
|
|
|
if (options.mechanism == ZMQ_CURVE
|
|
|
|
&& memcmp (greeting_recv + 12, "CURVE\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
|
2013-06-22 11:48:33 +02:00
|
|
|
if (options.as_server)
|
2013-07-18 09:39:19 +02:00
|
|
|
mechanism = new (std::nothrow)
|
|
|
|
curve_server_t (session, peer_address, options);
|
2013-06-22 11:48:33 +02:00
|
|
|
else
|
|
|
|
mechanism = new (std::nothrow) curve_client_t (options);
|
|
|
|
alloc_assert (mechanism);
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-25 13:47:07 +09:30
|
|
|
#ifdef HAVE_LIBGSSAPI_KRB5
|
2013-09-24 22:20:01 -05:00
|
|
|
else
|
2014-09-19 19:24:45 +02:00
|
|
|
if (options.mechanism == ZMQ_GSSAPI
|
|
|
|
&& memcmp (greeting_recv + 12, "GSSAPI\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
|
2013-09-30 14:13:03 -05:00
|
|
|
if (options.as_server)
|
|
|
|
mechanism = new (std::nothrow)
|
|
|
|
gssapi_server_t (session, peer_address, options);
|
|
|
|
else
|
|
|
|
mechanism = new (std::nothrow) gssapi_client_t (options);
|
2013-09-24 22:20:01 -05:00
|
|
|
alloc_assert (mechanism);
|
|
|
|
}
|
2013-06-22 11:48:33 +02:00
|
|
|
#endif
|
2013-04-28 10:15:25 +02:00
|
|
|
else {
|
2014-05-17 21:04:38 +02:00
|
|
|
error (protocol_error);
|
2013-04-28 10:15:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-05 19:51:38 +02:00
|
|
|
next_msg = &stream_engine_t::next_handshake_command;
|
|
|
|
process_msg = &stream_engine_t::process_handshake_command;
|
2012-09-05 02:01:19 +02:00
|
|
|
}
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
|
|
|
// Start polling for output if necessary.
|
|
|
|
if (outsize == 0)
|
|
|
|
set_pollout (handle);
|
|
|
|
|
|
|
|
// Handshaking was successful.
|
|
|
|
// Switch into the normal message flow.
|
|
|
|
handshaking = false;
|
|
|
|
|
2014-05-09 13:54:24 +00:00
|
|
|
if (has_handshake_timer) {
|
|
|
|
cancel_timer (handshake_timer_id);
|
|
|
|
has_handshake_timer = false;
|
|
|
|
}
|
|
|
|
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-05 19:51:38 +02:00
|
|
|
int zmq::stream_engine_t::identity_msg (msg_t *msg_)
|
2012-09-02 18:19:15 +02:00
|
|
|
{
|
2013-04-12 11:59:49 +02:00
|
|
|
int rc = msg_->init_size (options.identity_size);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
if (options.identity_size > 0)
|
|
|
|
memcpy (msg_->data (), options.identity, options.identity_size);
|
2014-05-05 19:51:38 +02:00
|
|
|
next_msg = &stream_engine_t::pull_msg_from_session;
|
2013-04-12 11:59:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-09-02 18:19:15 +02:00
|
|
|
|
2014-05-05 19:51:38 +02:00
|
|
|
int zmq::stream_engine_t::process_identity_msg (msg_t *msg_)
|
2013-04-12 11:59:49 +02:00
|
|
|
{
|
|
|
|
if (options.recv_identity) {
|
|
|
|
msg_->set_flags (msg_t::identity);
|
|
|
|
int rc = session->push_msg (msg_);
|
2013-03-18 02:00:00 +01:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
2013-04-12 11:59:49 +02:00
|
|
|
else {
|
|
|
|
int rc = msg_->close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = msg_->init ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subscription_required)
|
2014-05-05 19:51:38 +02:00
|
|
|
process_msg = &stream_engine_t::write_subscription_msg;
|
2013-04-12 11:59:49 +02:00
|
|
|
else
|
2014-05-05 19:51:38 +02:00
|
|
|
process_msg = &stream_engine_t::push_msg_to_session;
|
2012-09-02 18:19:15 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-09-02 18:19:15 +02:00
|
|
|
|
2013-09-04 17:59:45 +02:00
|
|
|
int zmq::stream_engine_t::next_handshake_command (msg_t *msg_)
|
2013-04-28 10:15:25 +02:00
|
|
|
{
|
2013-05-13 22:34:27 +02:00
|
|
|
zmq_assert (mechanism != NULL);
|
|
|
|
|
2014-05-06 17:07:50 +02:00
|
|
|
if (mechanism->status () == mechanism_t::ready) {
|
|
|
|
mechanism_ready ();
|
|
|
|
return pull_and_encode (msg_);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (mechanism->status () == mechanism_t::error) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const int rc = mechanism->next_handshake_command (msg_);
|
|
|
|
if (rc == 0)
|
|
|
|
msg_->set_flags (msg_t::command);
|
|
|
|
return rc;
|
2013-05-13 22:34:27 +02:00
|
|
|
}
|
2013-04-28 10:15:25 +02:00
|
|
|
}
|
|
|
|
|
2013-09-04 17:59:45 +02:00
|
|
|
int zmq::stream_engine_t::process_handshake_command (msg_t *msg_)
|
2013-04-28 10:15:25 +02:00
|
|
|
{
|
2013-05-13 22:34:27 +02:00
|
|
|
zmq_assert (mechanism != NULL);
|
2013-09-04 17:59:45 +02:00
|
|
|
const int rc = mechanism->process_handshake_command (msg_);
|
2013-05-13 22:34:27 +02:00
|
|
|
if (rc == 0) {
|
2014-05-06 17:07:50 +02:00
|
|
|
if (mechanism->status () == mechanism_t::ready)
|
2013-05-13 22:34:27 +02:00
|
|
|
mechanism_ready ();
|
2014-05-06 17:07:50 +02:00
|
|
|
else
|
|
|
|
if (mechanism->status () == mechanism_t::error) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-26 09:37:04 +02:00
|
|
|
if (output_stopped)
|
|
|
|
restart_output ();
|
2013-04-28 10:15:25 +02:00
|
|
|
}
|
|
|
|
|
2013-05-13 22:34:27 +02:00
|
|
|
return rc;
|
2013-04-28 10:15:25 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 13:13:10 +02:00
|
|
|
void zmq::stream_engine_t::zap_msg_available ()
|
|
|
|
{
|
|
|
|
zmq_assert (mechanism != NULL);
|
|
|
|
|
|
|
|
const int rc = mechanism->zap_msg_available ();
|
|
|
|
if (rc == -1) {
|
2014-05-17 21:04:38 +02:00
|
|
|
error (protocol_error);
|
2013-06-06 13:13:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-09-26 09:37:04 +02:00
|
|
|
if (input_stopped)
|
|
|
|
restart_input ();
|
|
|
|
if (output_stopped)
|
|
|
|
restart_output ();
|
2013-06-06 13:13:10 +02:00
|
|
|
}
|
|
|
|
|
2013-05-13 22:34:27 +02:00
|
|
|
void zmq::stream_engine_t::mechanism_ready ()
|
2013-04-28 10:15:25 +02:00
|
|
|
{
|
2015-11-30 20:18:25 +00:00
|
|
|
if (options.heartbeat_interval > 0) {
|
|
|
|
add_timer(options.heartbeat_interval, heartbeat_ivl_timer_id);
|
|
|
|
has_heartbeat_timer = true;
|
|
|
|
}
|
|
|
|
|
2013-05-13 22:34:27 +02:00
|
|
|
if (options.recv_identity) {
|
|
|
|
msg_t identity;
|
|
|
|
mechanism->peer_identity (&identity);
|
|
|
|
const int rc = session->push_msg (&identity);
|
2013-06-22 10:58:16 +01:00
|
|
|
if (rc == -1 && errno == EAGAIN) {
|
|
|
|
// If the write is failing at this stage with
|
|
|
|
// an EAGAIN the pipe must be being shut down,
|
|
|
|
// so we can just bail out of the identity set.
|
|
|
|
return;
|
|
|
|
}
|
2013-05-13 22:34:27 +02:00
|
|
|
errno_assert (rc == 0);
|
2013-09-26 09:38:09 +02:00
|
|
|
session->flush ();
|
2013-04-28 10:15:25 +02:00
|
|
|
}
|
2013-05-13 22:34:27 +02:00
|
|
|
|
2014-05-05 19:51:38 +02:00
|
|
|
next_msg = &stream_engine_t::pull_and_encode;
|
|
|
|
process_msg = &stream_engine_t::write_credential;
|
2014-05-02 12:33:26 +02:00
|
|
|
|
|
|
|
// Compile metadata.
|
|
|
|
properties_t properties;
|
2015-02-18 21:24:57 -06:00
|
|
|
init_properties(properties);
|
2015-01-16 14:52:16 -06:00
|
|
|
|
2014-05-02 22:19:30 +02:00
|
|
|
// Add ZAP properties.
|
|
|
|
const properties_t& zap_properties = mechanism->get_zap_properties ();
|
2015-01-16 15:04:19 -06:00
|
|
|
properties.insert(zap_properties.begin (), zap_properties.end ());
|
2014-05-02 22:19:30 +02:00
|
|
|
|
2014-05-02 23:14:27 +02:00
|
|
|
// Add ZMTP properties.
|
|
|
|
const properties_t& zmtp_properties = mechanism->get_zmtp_properties ();
|
2015-01-16 15:04:19 -06:00
|
|
|
properties.insert(zmtp_properties.begin (), zmtp_properties.end ());
|
2014-05-02 23:14:27 +02:00
|
|
|
|
2014-05-02 12:33:26 +02:00
|
|
|
zmq_assert (metadata == NULL);
|
|
|
|
if (!properties.empty ())
|
|
|
|
metadata = new (std::nothrow) metadata_t (properties);
|
2013-04-28 10:15:25 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 11:59:49 +02:00
|
|
|
int zmq::stream_engine_t::pull_msg_from_session (msg_t *msg_)
|
2013-03-18 02:00:00 +01:00
|
|
|
{
|
2013-04-12 11:59:49 +02:00
|
|
|
return session->pull_msg (msg_);
|
|
|
|
}
|
2013-03-18 02:00:00 +01:00
|
|
|
|
2013-04-12 11:59:49 +02:00
|
|
|
int zmq::stream_engine_t::push_msg_to_session (msg_t *msg_)
|
|
|
|
{
|
|
|
|
return session->push_msg (msg_);
|
|
|
|
}
|
|
|
|
|
2015-02-18 12:28:58 -06:00
|
|
|
int zmq::stream_engine_t::push_raw_msg_to_session (msg_t *msg_) {
|
2015-11-18 15:02:19 +02:00
|
|
|
if (metadata && metadata != msg_->metadata())
|
2015-02-18 12:28:58 -06:00
|
|
|
msg_->set_metadata(metadata);
|
|
|
|
return push_msg_to_session(msg_);
|
|
|
|
}
|
|
|
|
|
2014-01-12 21:58:36 +01:00
|
|
|
int zmq::stream_engine_t::write_credential (msg_t *msg_)
|
|
|
|
{
|
|
|
|
zmq_assert (mechanism != NULL);
|
|
|
|
zmq_assert (session != NULL);
|
|
|
|
|
|
|
|
const blob_t credential = mechanism->get_user_id ();
|
|
|
|
if (credential.size () > 0) {
|
|
|
|
msg_t msg;
|
|
|
|
int rc = msg.init_size (credential.size ());
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), credential.data (), credential.size ());
|
|
|
|
msg.set_flags (msg_t::credential);
|
|
|
|
rc = session->push_msg (&msg);
|
|
|
|
if (rc == -1) {
|
|
|
|
rc = msg.close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-05-05 19:51:38 +02:00
|
|
|
process_msg = &stream_engine_t::decode_and_push;
|
2014-01-12 21:58:36 +01:00
|
|
|
return decode_and_push (msg_);
|
|
|
|
}
|
|
|
|
|
2013-06-22 11:48:33 +02:00
|
|
|
int zmq::stream_engine_t::pull_and_encode (msg_t *msg_)
|
|
|
|
{
|
|
|
|
zmq_assert (mechanism != NULL);
|
|
|
|
|
|
|
|
if (session->pull_msg (msg_) == -1)
|
|
|
|
return -1;
|
|
|
|
if (mechanism->encode (msg_) == -1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::stream_engine_t::decode_and_push (msg_t *msg_)
|
|
|
|
{
|
|
|
|
zmq_assert (mechanism != NULL);
|
|
|
|
|
|
|
|
if (mechanism->decode (msg_) == -1)
|
|
|
|
return -1;
|
2015-03-16 21:39:16 -04:00
|
|
|
|
|
|
|
if(has_timeout_timer) {
|
|
|
|
has_timeout_timer = false;
|
|
|
|
cancel_timer(heartbeat_timeout_timer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(has_ttl_timer) {
|
|
|
|
has_ttl_timer = false;
|
|
|
|
cancel_timer(heartbeat_ttl_timer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(msg_->flags() & msg_t::command) {
|
|
|
|
uint8_t cmd_id = *((uint8_t*)msg_->data());
|
|
|
|
if(cmd_id == 4)
|
|
|
|
process_heartbeat_message(msg_);
|
|
|
|
}
|
|
|
|
|
2014-04-30 14:17:38 +02:00
|
|
|
if (metadata)
|
2014-05-03 21:00:42 +02:00
|
|
|
msg_->set_metadata (metadata);
|
2013-06-22 11:48:33 +02:00
|
|
|
if (session->push_msg (msg_) == -1) {
|
|
|
|
if (errno == EAGAIN)
|
2014-05-05 19:51:38 +02:00
|
|
|
process_msg = &stream_engine_t::push_one_then_decode_and_push;
|
2013-06-22 11:48:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::stream_engine_t::push_one_then_decode_and_push (msg_t *msg_)
|
|
|
|
{
|
|
|
|
const int rc = session->push_msg (msg_);
|
|
|
|
if (rc == 0)
|
2014-05-05 19:51:38 +02:00
|
|
|
process_msg = &stream_engine_t::decode_and_push;
|
2013-06-22 11:48:33 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-04-12 11:59:49 +02:00
|
|
|
int zmq::stream_engine_t::write_subscription_msg (msg_t *msg_)
|
|
|
|
{
|
|
|
|
msg_t subscription;
|
2012-09-02 18:19:15 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
// Inject the subscription message, so that also
|
|
|
|
// ZMQ 2.x peers receive published messages.
|
2013-04-12 11:59:49 +02:00
|
|
|
int rc = subscription.init_size (1);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
*(unsigned char*) subscription.data () = 1;
|
|
|
|
rc = session->push_msg (&subscription);
|
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
2013-03-18 02:00:00 +01:00
|
|
|
|
2014-05-05 19:51:38 +02:00
|
|
|
process_msg = &stream_engine_t::push_msg_to_session;
|
2013-04-12 11:59:49 +02:00
|
|
|
return push_msg_to_session (msg_);
|
2012-09-02 18:19:15 +02:00
|
|
|
}
|
|
|
|
|
2014-05-17 21:04:38 +02:00
|
|
|
void zmq::stream_engine_t::error (error_reason_t reason)
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
2015-07-24 05:12:11 +08:00
|
|
|
if (options.raw_socket && options.raw_notify) {
|
2014-01-12 22:36:47 -05:00
|
|
|
// For raw sockets, send a final 0-length message to the application
|
|
|
|
// so that it knows the peer has been disconnected.
|
|
|
|
msg_t terminator;
|
|
|
|
terminator.init();
|
2014-05-05 19:51:38 +02:00
|
|
|
(this->*process_msg) (&terminator);
|
2014-01-12 22:36:47 -05:00
|
|
|
terminator.close();
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
zmq_assert (session);
|
2015-04-21 22:26:32 -07:00
|
|
|
socket->event_disconnected (endpoint, (int) s);
|
2013-03-18 02:00:00 +01:00
|
|
|
session->flush ();
|
2014-05-17 21:04:38 +02:00
|
|
|
session->engine_error (reason);
|
2011-07-26 00:43:57 +02:00
|
|
|
unplug ();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2014-05-09 13:54:24 +00:00
|
|
|
void zmq::stream_engine_t::set_handshake_timer ()
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
2014-05-09 13:54:24 +00:00
|
|
|
zmq_assert (!has_handshake_timer);
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2015-01-23 15:25:40 +01:00
|
|
|
if (!options.raw_socket && options.handshake_ivl > 0) {
|
2014-05-09 13:54:24 +00:00
|
|
|
add_timer (options.handshake_ivl, handshake_timer_id);
|
|
|
|
has_handshake_timer = true;
|
2012-09-29 11:14:44 +02:00
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
}
|
|
|
|
|
2015-02-18 21:24:57 -06:00
|
|
|
bool zmq::stream_engine_t::init_properties (properties_t & properties) {
|
|
|
|
if (peer_address.empty()) return false;
|
|
|
|
properties.insert (std::make_pair("Peer-Address", peer_address));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-09 13:54:24 +00:00
|
|
|
void zmq::stream_engine_t::timer_event (int id_)
|
2011-07-26 00:43:57 +02:00
|
|
|
{
|
2015-03-16 21:39:16 -04:00
|
|
|
if(id_ == handshake_timer_id) {
|
|
|
|
has_handshake_timer = false;
|
|
|
|
// handshake timer expired before handshake completed, so engine fail
|
|
|
|
error (timeout_error);
|
|
|
|
}
|
|
|
|
else if(id_ == heartbeat_ivl_timer_id) {
|
|
|
|
next_msg = &stream_engine_t::produce_ping_message;
|
|
|
|
out_event();
|
|
|
|
add_timer(options.heartbeat_interval, heartbeat_ivl_timer_id);
|
|
|
|
}
|
|
|
|
else if(id_ == heartbeat_ttl_timer_id) {
|
|
|
|
has_ttl_timer = false;
|
|
|
|
error(timeout_error);
|
|
|
|
}
|
|
|
|
else if(id_ == heartbeat_timeout_timer_id) {
|
|
|
|
has_timeout_timer = false;
|
|
|
|
error(timeout_error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// There are no other valid timer ids!
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::stream_engine_t::produce_ping_message(msg_t * msg_)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
zmq_assert (mechanism != NULL);
|
|
|
|
|
|
|
|
// 16-bit TTL + \4PING == 7
|
2016-02-21 15:49:47 -06:00
|
|
|
rc = msg_->init_size(7);
|
2016-02-21 23:16:44 +00:00
|
|
|
errno_assert(rc == 0);
|
|
|
|
msg_->set_flags(msg_t::command);
|
2015-03-16 21:39:16 -04:00
|
|
|
// Copy in the command message
|
|
|
|
memcpy(msg_->data(), "\4PING", 5);
|
|
|
|
|
|
|
|
uint16_t ttl_val = htons(options.heartbeat_ttl);
|
|
|
|
memcpy(((uint8_t*)msg_->data()) + 5, &ttl_val, sizeof(ttl_val));
|
|
|
|
|
|
|
|
rc = mechanism->encode (msg_);
|
|
|
|
next_msg = &stream_engine_t::pull_and_encode;
|
2015-06-26 14:08:08 -04:00
|
|
|
if(!has_timeout_timer && heartbeat_timeout > 0) {
|
|
|
|
add_timer(heartbeat_timeout, heartbeat_timeout_timer_id);
|
2015-03-16 21:39:16 -04:00
|
|
|
has_timeout_timer = true;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::stream_engine_t::produce_pong_message(msg_t * msg_)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
zmq_assert (mechanism != NULL);
|
|
|
|
|
2016-02-21 15:49:47 -06:00
|
|
|
rc = msg_->init_size(5);
|
2016-02-21 23:16:44 +00:00
|
|
|
errno_assert(rc == 0);
|
|
|
|
msg_->set_flags(msg_t::command);
|
2015-03-16 21:39:16 -04:00
|
|
|
|
|
|
|
memcpy(msg_->data(), "\4PONG", 5);
|
|
|
|
|
|
|
|
rc = mechanism->encode (msg_);
|
|
|
|
next_msg = &stream_engine_t::pull_and_encode;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::stream_engine_t::process_heartbeat_message(msg_t * msg_)
|
|
|
|
{
|
|
|
|
if(memcmp(msg_->data(), "\4PING", 5) == 0) {
|
|
|
|
uint16_t remote_heartbeat_ttl;
|
|
|
|
// Get the remote heartbeat TTL to setup the timer
|
|
|
|
memcpy(&remote_heartbeat_ttl, (uint8_t*)msg_->data() + 5, 2);
|
|
|
|
remote_heartbeat_ttl = ntohs(remote_heartbeat_ttl);
|
|
|
|
// The remote heartbeat is in 10ths of a second
|
2015-06-26 14:08:08 -04:00
|
|
|
// so we multiply it by 100 to get the timer interval in ms.
|
|
|
|
remote_heartbeat_ttl *= 100;
|
2015-03-16 21:39:16 -04:00
|
|
|
|
|
|
|
if(!has_ttl_timer && remote_heartbeat_ttl > 0) {
|
|
|
|
add_timer(remote_heartbeat_ttl, heartbeat_ttl_timer_id);
|
|
|
|
has_ttl_timer = true;
|
|
|
|
}
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2015-03-16 21:39:16 -04:00
|
|
|
next_msg = &stream_engine_t::produce_pong_message;
|
|
|
|
out_event();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-07-28 08:06:21 +02:00
|
|
|
}
|