plain_client.cpp 7.42 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    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
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    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.
25 26 27 28 29

    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/>.
*/

30
#include "precompiled.hpp"
31
#include "macros.hpp"
32 33

#include <string>
34
#include <limits.h>
35 36 37 38

#include "msg.hpp"
#include "err.hpp"
#include "plain_client.hpp"
39
#include "session_base.hpp"
40
#include "plain_common.hpp"
41

42 43 44
zmq::plain_client_t::plain_client_t (session_base_t *const session_,
                                     const options_t &options_) :
    mechanism_base_t (session_, options_),
45
    _state (sending_hello)
46 47 48 49 50 51 52 53 54 55 56
{
}

zmq::plain_client_t::~plain_client_t ()
{
}

int zmq::plain_client_t::next_handshake_command (msg_t *msg_)
{
    int rc = 0;

57
    switch (_state) {
58
        case sending_hello:
59 60
            produce_hello (msg_);
            _state = waiting_for_welcome;
61 62
            break;
        case sending_initiate:
63 64
            produce_initiate (msg_);
            _state = waiting_for_ready;
65 66 67 68 69 70 71 72 73 74
            break;
        default:
            errno = EAGAIN;
            rc = -1;
    }
    return rc;
}

int zmq::plain_client_t::process_handshake_command (msg_t *msg_)
{
75
    const unsigned char *cmd_data =
76
      static_cast<unsigned char *> (msg_->data ());
77
    const size_t data_size = msg_->size ();
78

79
    int rc = 0;
80 81
    if (data_size >= welcome_prefix_len
        && !memcmp (cmd_data, welcome_prefix, welcome_prefix_len))
82
        rc = process_welcome (cmd_data, data_size);
83 84
    else if (data_size >= ready_prefix_len
             && !memcmp (cmd_data, ready_prefix, ready_prefix_len))
85
        rc = process_ready (cmd_data, data_size);
86 87
    else if (data_size >= error_prefix_len
             && !memcmp (cmd_data, error_prefix, error_prefix_len))
88 89
        rc = process_error (cmd_data, data_size);
    else {
90
        session->get_socket ()->event_handshake_failed_protocol (
91
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
92 93
        errno = EPROTO;
        rc = -1;
94
    }
95

96 97 98 99 100 101
    if (rc == 0) {
        rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
    }
102

103 104 105 106 107
    return rc;
}

zmq::mechanism_t::status_t zmq::plain_client_t::status () const
{
108 109 110 111 112 113 114 115
    switch (_state) {
        case ready:
            return mechanism_t::ready;
        case error_command_received:
            return mechanism_t::error;
        default:
            return mechanism_t::handshaking;
    }
116 117
}

118
void zmq::plain_client_t::produce_hello (msg_t *msg_) const
119 120
{
    const std::string username = options.plain_username;
121
    zmq_assert (username.length () <= UCHAR_MAX);
122 123

    const std::string password = options.plain_password;
124
    zmq_assert (password.length () <= UCHAR_MAX);
125

126 127 128
    const size_t command_size = hello_prefix_len + brief_len_size
                                + username.length () + brief_len_size
                                + password.length ();
129 130 131 132

    const int rc = msg_->init_size (command_size);
    errno_assert (rc == 0);

133
    unsigned char *ptr = static_cast<unsigned char *> (msg_->data ());
134 135
    memcpy (ptr, hello_prefix, hello_prefix_len);
    ptr += hello_prefix_len;
136

137
    *ptr++ = static_cast<unsigned char> (username.length ());
138 139 140
    memcpy (ptr, username.c_str (), username.length ());
    ptr += username.length ();

141
    *ptr++ = static_cast<unsigned char> (password.length ());
142 143 144
    memcpy (ptr, password.c_str (), password.length ());
}

145 146
int zmq::plain_client_t::process_welcome (const unsigned char *cmd_data_,
                                          size_t data_size_)
147
{
148
    LIBZMQ_UNUSED (cmd_data_);
149

150
    if (_state != waiting_for_welcome) {
151 152
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
153 154 155
        errno = EPROTO;
        return -1;
    }
156
    if (data_size_ != welcome_prefix_len) {
157 158 159
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_WELCOME);
160 161 162
        errno = EPROTO;
        return -1;
    }
163
    _state = sending_initiate;
164 165 166
    return 0;
}

167
void zmq::plain_client_t::produce_initiate (msg_t *msg_) const
168
{
169 170
    make_command_with_basic_properties (msg_, initiate_prefix,
                                        initiate_prefix_len);
171 172
}

173 174
int zmq::plain_client_t::process_ready (const unsigned char *cmd_data_,
                                        size_t data_size_)
175
{
176
    if (_state != waiting_for_ready) {
177 178
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
179 180 181
        errno = EPROTO;
        return -1;
    }
182 183
    const int rc = parse_metadata (cmd_data_ + ready_prefix_len,
                                   data_size_ - ready_prefix_len);
184
    if (rc == 0)
185
        _state = ready;
186 187 188 189
    else
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_METADATA);

190 191
    return rc;
}
192

193 194
int zmq::plain_client_t::process_error (const unsigned char *cmd_data_,
                                        size_t data_size_)
195
{
196
    if (_state != waiting_for_welcome && _state != waiting_for_ready) {
197 198
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
199 200 201
        errno = EPROTO;
        return -1;
    }
202 203
    const size_t start_of_error_reason = error_prefix_len + brief_len_size;
    if (data_size_ < start_of_error_reason) {
204 205 206
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_ERROR);
207 208 209
        errno = EPROTO;
        return -1;
    }
210 211 212
    const size_t error_reason_len =
      static_cast<size_t> (cmd_data_[error_prefix_len]);
    if (error_reason_len > data_size_ - start_of_error_reason) {
213 214 215
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_ERROR);
216 217 218
        errno = EPROTO;
        return -1;
    }
219 220
    const char *error_reason =
      reinterpret_cast<const char *> (cmd_data_) + start_of_error_reason;
221
    handle_error_reason (error_reason, error_reason_len);
222
    _state = error_command_received;
223
    return 0;
224
}