2013-05-13 22:34:27 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2013-05-13 22:34:27 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2013-05-13 22:34:27 +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
|
2013-05-13 22:34:27 +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.
|
2013-05-13 22:34:27 +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"
|
2013-05-13 22:34:27 +02:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "err.hpp"
|
|
|
|
#include "msg.hpp"
|
2013-07-18 10:10:10 +02:00
|
|
|
#include "session_base.hpp"
|
2013-05-13 22:34:27 +02:00
|
|
|
#include "wire.hpp"
|
|
|
|
#include "null_mechanism.hpp"
|
|
|
|
|
2013-07-18 10:10:10 +02:00
|
|
|
zmq::null_mechanism_t::null_mechanism_t (session_base_t *session_,
|
|
|
|
const std::string &peer_address_,
|
|
|
|
const options_t &options_) :
|
|
|
|
mechanism_t (options_),
|
2017-08-16 13:42:24 +02:00
|
|
|
zap_client_t (session_, peer_address_, options_),
|
2013-07-18 10:10:10 +02:00
|
|
|
ready_command_sent (false),
|
2014-05-07 16:43:06 +02:00
|
|
|
error_command_sent (false),
|
2013-07-18 10:10:10 +02:00
|
|
|
ready_command_received (false),
|
2014-05-07 16:43:06 +02:00
|
|
|
error_command_received (false),
|
2013-07-18 10:10:10 +02:00
|
|
|
zap_connected (false),
|
|
|
|
zap_request_sent (false),
|
|
|
|
zap_reply_received (false)
|
2013-05-13 22:34:27 +02:00
|
|
|
{
|
2013-09-09 20:40:34 +02:00
|
|
|
// NULL mechanism only uses ZAP if there's a domain defined
|
|
|
|
// This prevents ZAP requests on naive sockets
|
|
|
|
if (options.zap_domain.size () > 0
|
|
|
|
&& session->zap_connect () == 0)
|
2013-07-18 10:10:10 +02:00
|
|
|
zap_connected = true;
|
2013-05-13 22:34:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::null_mechanism_t::~null_mechanism_t ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-04 17:59:45 +02:00
|
|
|
int zmq::null_mechanism_t::next_handshake_command (msg_t *msg_)
|
2013-05-13 22:34:27 +02:00
|
|
|
{
|
2014-05-07 16:43:06 +02:00
|
|
|
if (ready_command_sent || error_command_sent) {
|
2013-05-13 22:34:27 +02:00
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-18 10:10:10 +02:00
|
|
|
if (zap_connected && !zap_reply_received) {
|
|
|
|
if (zap_request_sent) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-29 10:43:56 -07:00
|
|
|
int rc = send_zap_request ();
|
|
|
|
if (rc != 0)
|
|
|
|
return -1;
|
2013-07-18 10:10:10 +02:00
|
|
|
zap_request_sent = true;
|
2017-03-29 10:43:56 -07:00
|
|
|
rc = receive_and_process_zap_reply ();
|
2013-07-18 10:10:10 +02:00
|
|
|
if (rc != 0)
|
|
|
|
return -1;
|
|
|
|
zap_reply_received = true;
|
|
|
|
}
|
2013-05-13 22:34:27 +02:00
|
|
|
|
2017-08-16 13:42:24 +02:00
|
|
|
if (zap_reply_received && status_code != "200") {
|
|
|
|
const size_t status_code_len = 3;
|
|
|
|
const int rc = msg_->init_size (6 + 1 + status_code_len);
|
2014-05-07 16:43:06 +02:00
|
|
|
zmq_assert (rc == 0);
|
2017-08-16 13:42:24 +02:00
|
|
|
unsigned char *msg_data = static_cast<unsigned char *> (msg_->data ());
|
2014-05-07 16:43:06 +02:00
|
|
|
memcpy (msg_data, "\5ERROR", 6);
|
2017-08-16 13:42:24 +02:00
|
|
|
msg_data [6] = status_code_len;
|
|
|
|
memcpy (msg_data + 7, status_code.c_str (), status_code_len);
|
2014-05-07 16:43:06 +02:00
|
|
|
error_command_sent = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 19:42:31 +02:00
|
|
|
make_command_with_basic_properties (msg_, "\5READY", 6);
|
2013-05-13 22:34:27 +02:00
|
|
|
|
|
|
|
ready_command_sent = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-04 17:59:45 +02:00
|
|
|
int zmq::null_mechanism_t::process_handshake_command (msg_t *msg_)
|
2013-05-13 22:34:27 +02:00
|
|
|
{
|
2014-05-07 16:43:06 +02:00
|
|
|
if (ready_command_received || error_command_received) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("NULL I: client sent invalid NULL handshake (duplicate READY)");
|
2013-05-13 22:34:27 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-07 16:43:06 +02:00
|
|
|
const unsigned char *cmd_data =
|
2013-05-13 22:34:27 +02:00
|
|
|
static_cast <unsigned char *> (msg_->data ());
|
2014-05-07 16:43:06 +02:00
|
|
|
const size_t data_size = msg_->size ();
|
2013-05-13 22:34:27 +02:00
|
|
|
|
2014-05-07 16:43:06 +02:00
|
|
|
int rc = 0;
|
|
|
|
if (data_size >= 6 && !memcmp (cmd_data, "\5READY", 6))
|
|
|
|
rc = process_ready_command (cmd_data, data_size);
|
|
|
|
else
|
|
|
|
if (data_size >= 6 && !memcmp (cmd_data, "\5ERROR", 6))
|
|
|
|
rc = process_error_command (cmd_data, data_size);
|
|
|
|
else {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("NULL I: client sent invalid NULL handshake (not READY)");
|
2013-05-13 22:34:27 +02:00
|
|
|
errno = EPROTO;
|
2014-05-07 16:43:06 +02:00
|
|
|
rc = -1;
|
2013-05-13 22:34:27 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 19:02:08 +02:00
|
|
|
if (rc == 0) {
|
2016-02-21 15:49:47 -06:00
|
|
|
rc = msg_->close ();
|
2013-06-22 19:02:08 +02:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = msg_->init ();
|
|
|
|
errno_assert (rc == 0);
|
2013-05-13 22:34:27 +02:00
|
|
|
}
|
2014-05-07 16:43:06 +02:00
|
|
|
return rc;
|
|
|
|
}
|
2013-05-13 22:34:27 +02:00
|
|
|
|
2014-05-07 16:43:06 +02:00
|
|
|
int zmq::null_mechanism_t::process_ready_command (
|
|
|
|
const unsigned char *cmd_data, size_t data_size)
|
|
|
|
{
|
2013-05-13 22:34:27 +02:00
|
|
|
ready_command_received = true;
|
2014-05-07 16:43:06 +02:00
|
|
|
return parse_metadata (cmd_data + 6, data_size - 6);
|
|
|
|
}
|
2013-05-13 22:34:27 +02:00
|
|
|
|
2014-05-07 16:43:06 +02:00
|
|
|
int zmq::null_mechanism_t::process_error_command (
|
|
|
|
const unsigned char *cmd_data, size_t data_size)
|
|
|
|
{
|
2014-05-14 14:12:04 +02:00
|
|
|
if (data_size < 7) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const size_t error_reason_len = static_cast <size_t> (cmd_data [6]);
|
|
|
|
if (error_reason_len > data_size - 7) {
|
2014-05-07 16:43:06 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-14 06:50:47 +02:00
|
|
|
error_command_received = true;
|
2014-05-07 16:43:06 +02:00
|
|
|
return 0;
|
2013-05-13 22:34:27 +02:00
|
|
|
}
|
|
|
|
|
2013-07-18 10:10:10 +02:00
|
|
|
int zmq::null_mechanism_t::zap_msg_available ()
|
|
|
|
{
|
|
|
|
if (zap_reply_received) {
|
|
|
|
errno = EFSM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const int rc = receive_and_process_zap_reply ();
|
|
|
|
if (rc == 0)
|
|
|
|
zap_reply_received = true;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-05-06 17:07:50 +02:00
|
|
|
zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const
|
2013-05-13 22:34:27 +02:00
|
|
|
{
|
2014-05-07 16:43:06 +02:00
|
|
|
const bool command_sent =
|
|
|
|
ready_command_sent || error_command_sent;
|
|
|
|
const bool command_received =
|
|
|
|
ready_command_received || error_command_received;
|
|
|
|
|
|
|
|
if (ready_command_sent && ready_command_received)
|
|
|
|
return ready;
|
2014-05-06 17:07:50 +02:00
|
|
|
else
|
2014-05-07 16:43:06 +02:00
|
|
|
if (command_sent && command_received)
|
|
|
|
return error;
|
|
|
|
else
|
|
|
|
return handshaking;
|
2013-05-13 22:34:27 +02:00
|
|
|
}
|
2013-07-18 10:10:10 +02:00
|
|
|
|
2017-03-29 10:43:56 -07:00
|
|
|
int zmq::null_mechanism_t::send_zap_request ()
|
2013-07-18 10:10:10 +02:00
|
|
|
{
|
2017-08-16 13:42:24 +02:00
|
|
|
return zap_client_t::send_zap_request ("NULL", 4, NULL, NULL, 0);
|
2013-07-18 10:10:10 +02:00
|
|
|
}
|
|
|
|
|