mechanism.cpp 9.29 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 32 33 34 35 36 37
#include <string.h>

#include "mechanism.hpp"
#include "options.hpp"
#include "msg.hpp"
#include "err.hpp"
#include "wire.hpp"
38
#include "session_base.hpp"
39 40 41 42 43 44 45 46 47 48

zmq::mechanism_t::mechanism_t (const options_t &options_) :
    options (options_)
{
}

zmq::mechanism_t::~mechanism_t ()
{
}

49
void zmq::mechanism_t::set_peer_routing_id (const void *id_ptr, size_t id_size)
50
{
51
    routing_id.set (static_cast <const unsigned char*> (id_ptr), id_size);
52 53
}

54
void zmq::mechanism_t::peer_routing_id (msg_t *msg_)
55
{
56
    const int rc = msg_->init_size (routing_id.size ());
57
    errno_assert (rc == 0);
58 59
    memcpy (msg_->data (), routing_id.data (), routing_id.size ());
    msg_->set_flags (msg_t::routing_id);
60 61
}

62 63
void zmq::mechanism_t::set_user_id (const void *data_, size_t size_)
{
64
    user_id.set (static_cast <const unsigned char*> (data_), size_);
65 66
    zap_properties.ZMQ_MAP_INSERT_OR_EMPLACE (
      ZMQ_MSG_PROPERTY_USER_ID, std::string ((char *) data_, size_));
67 68
}

69
const zmq::blob_t &zmq::mechanism_t::get_user_id () const
70 71 72 73
{
    return user_id;
}

74 75 76 77
const char *zmq::mechanism_t::socket_type_string (int socket_type) const
{
    static const char *names [] = {"PAIR", "PUB", "SUB", "REQ", "REP",
                                   "DEALER", "ROUTER", "PULL", "PUSH",
somdoron's avatar
somdoron committed
78 79
                                   "XPUB", "XSUB", "STREAM",
                                   "SERVER", "CLIENT",
somdoron's avatar
somdoron committed
80
                                   "RADIO", "DISH",
81 82
                                   "GATHER", "SCATTER", "DGRAM"};
    zmq_assert (socket_type >= 0 && socket_type <= 18);
83 84 85
    return names [socket_type];
}

86 87 88 89 90 91
static size_t property_len (size_t name_len, size_t value_len)
{
    return 1 + name_len + 4 + value_len;
}

static size_t name_len (const char *name)
92 93 94
{
    const size_t name_len = strlen (name);
    zmq_assert (name_len <= 255);
95 96 97 98 99 100 101 102 103 104 105 106 107
    return name_len;
}

size_t zmq::mechanism_t::add_property (unsigned char *ptr,
                                       size_t ptr_capacity,
                                       const char *name,
                                       const void *value,
                                       size_t value_len)
{
    const size_t name_len = ::name_len (name);
    const size_t total_len = ::property_len (name_len, value_len);
    zmq_assert (total_len <= ptr_capacity);

108 109 110
    *ptr++ = static_cast <unsigned char> (name_len);
    memcpy (ptr, name, name_len);
    ptr += name_len;
111
    zmq_assert (value_len <= 0x7FFFFFFF);
112 113 114 115
    put_uint32 (ptr, static_cast <uint32_t> (value_len));
    ptr += 4;
    memcpy (ptr, value, value_len);

116 117 118 119 120 121 122 123
    return total_len;
}

size_t zmq::mechanism_t::property_len (const char *name, size_t value_len)
{
    return ::property_len (name_len (name), value_len);
}

124 125 126
#define ZMTP_PROPERTY_SOCKET_TYPE "Socket-Type"
#define ZMTP_PROPERTY_IDENTITY "Identity"

127 128 129 130 131 132 133 134
size_t zmq::mechanism_t::add_basic_properties (unsigned char *buf,
                                               size_t buf_capacity) const
{
    unsigned char *ptr = buf;

    //  Add socket type property
    const char *socket_type = socket_type_string (options.type);
    ptr += add_property (ptr, buf_capacity,
135
                         ZMTP_PROPERTY_SOCKET_TYPE, socket_type,
136 137
                         strlen (socket_type));

138
    //  Add identity (aka routing id) property
139 140 141
    if (options.type == ZMQ_REQ || options.type == ZMQ_DEALER
        || options.type == ZMQ_ROUTER)
        ptr += add_property (ptr, buf_capacity - (ptr - buf),
142
                             ZMTP_PROPERTY_IDENTITY, options.routing_id,
143
                             options.routing_id_size);
144 145 146 147 148 149 150

    return ptr - buf;
}

size_t zmq::mechanism_t::basic_properties_len() const
{
    const char *socket_type = socket_type_string (options.type);
151
    return property_len (ZMTP_PROPERTY_SOCKET_TYPE, strlen (socket_type))
152 153
           + ((options.type == ZMQ_REQ || options.type == ZMQ_DEALER
               || options.type == ZMQ_ROUTER)
154
                ? property_len (ZMTP_PROPERTY_IDENTITY,
155
                                options.routing_id_size)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
                : 0);
}

void zmq::mechanism_t::make_command_with_basic_properties (
  msg_t *msg_, const char *prefix, size_t prefix_len) const
{
    const size_t command_size = prefix_len + basic_properties_len ();
    const int rc = msg_->init_size (command_size);
    errno_assert (rc == 0);

    unsigned char *ptr = (unsigned char *) msg_->data ();

    //  Add prefix
    memcpy (ptr, prefix, prefix_len);
    ptr += prefix_len;

172
    add_basic_properties (
173
      ptr, command_size - (ptr - (unsigned char *) msg_->data ()));
174
}
175

176
int zmq::mechanism_t::parse_metadata (const unsigned char *ptr_,
177 178
                                      size_t length_,
                                      bool zap_flag)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
    size_t bytes_left = length_;

    while (bytes_left > 1) {
        const size_t name_length = static_cast <size_t> (*ptr_);
        ptr_ += 1;
        bytes_left -= 1;
        if (bytes_left < name_length)
            break;

        const std::string name = std::string ((char *) ptr_, name_length);
        ptr_ += name_length;
        bytes_left -= name_length;
        if (bytes_left < 4)
            break;

        const size_t value_length = static_cast <size_t> (get_uint32 (ptr_));
        ptr_ += 4;
        bytes_left -= 4;
        if (bytes_left < value_length)
            break;

        const uint8_t *value = ptr_;
        ptr_ += value_length;
        bytes_left -= value_length;

205
        if (name == ZMTP_PROPERTY_IDENTITY && options.recv_routing_id)
206
            set_peer_routing_id (value, value_length);
207
        else
208
        if (name == ZMTP_PROPERTY_SOCKET_TYPE) {
209 210 211 212 213 214 215 216 217 218 219
            const std::string socket_type ((char *) value, value_length);
            if (!check_socket_type (socket_type)) {
                errno = EINVAL;
                return -1;
            }
        }
        else {
            const int rc = property (name, value, value_length);
            if (rc == -1)
                return -1;
        }
220
        if (zap_flag)
221 222
            zap_properties.ZMQ_MAP_INSERT_OR_EMPLACE (
                    name, std::string ((char *) value, value_length));
223
        else
224 225
            zmtp_properties.ZMQ_MAP_INSERT_OR_EMPLACE (
                    name, std::string ((char *) value, value_length));
226 227 228 229 230 231 232 233
    }
    if (bytes_left > 0) {
        errno = EPROTO;
        return -1;
    }
    return 0;
}

234
int zmq::mechanism_t::property (const std::string& /* name_ */,
235
                                const void * /* value_ */, size_t /* length_ */)
236 237 238 239 240
{
    //  Default implementation does not check
    //  property values and returns 0 to signal success.
    return 0;
}
241

242
bool zmq::mechanism_t::check_socket_type (const std::string& type_) const
243 244 245 246 247 248 249
{
    switch (options.type) {
        case ZMQ_REQ:
            return type_ == "REP" || type_ == "ROUTER";
        case ZMQ_REP:
            return type_ == "REQ" || type_ == "DEALER";
        case ZMQ_DEALER:
250
            return type_ == "REP" || type_ == "DEALER" || type_ == "ROUTER";
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        case ZMQ_ROUTER:
            return type_ == "REQ" || type_ == "DEALER" || type_ == "ROUTER";
        case ZMQ_PUSH:
            return type_ == "PULL";
        case ZMQ_PULL:
            return type_ == "PUSH";
        case ZMQ_PUB:
            return type_ == "SUB" || type_ == "XSUB";
        case ZMQ_SUB:
            return type_ == "PUB" || type_ == "XPUB";
        case ZMQ_XPUB:
            return type_ == "SUB" || type_ == "XSUB";
        case ZMQ_XSUB:
            return type_ == "PUB" || type_ == "XPUB";
        case ZMQ_PAIR:
            return type_ == "PAIR";
267
        case ZMQ_SERVER:
268 269
            return type_ == "CLIENT";
        case ZMQ_CLIENT:
270
            return type_ == "SERVER";
somdoron's avatar
somdoron committed
271 272 273 274
        case ZMQ_RADIO:
            return type_ == "DISH";
        case ZMQ_DISH:
            return type_ == "RADIO";
somdoron's avatar
somdoron committed
275 276 277 278
        case ZMQ_GATHER:
            return type_ == "SCATTER";
        case ZMQ_SCATTER:
            return type_ == "GATHER";
279 280
        case ZMQ_DGRAM:
            return type_ == "DGRAM";
281 282 283 284 285
        default:
            break;
    }
    return false;
}