stream_engine.cpp 26.6 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ 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.

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

#include "platform.hpp"
#if defined ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#else
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#endif

#include <string.h>
#include <new>
35
#include <sstream>
Pieter Hintjens's avatar
Pieter Hintjens committed
36
#include <iostream>
37

38
#include "stream_engine.hpp"
39
#include "io_thread.hpp"
40
#include "session_base.hpp"
41 42
#include "v1_encoder.hpp"
#include "v1_decoder.hpp"
43 44
#include "v2_encoder.hpp"
#include "v2_decoder.hpp"
45
#include "null_mechanism.hpp"
46 47
#include "plain_client.hpp"
#include "plain_server.hpp"
48 49
#include "gssapi_client.hpp"
#include "gssapi_server.hpp"
50 51
#include "curve_client.hpp"
#include "curve_server.hpp"
52 53
#include "raw_decoder.hpp"
#include "raw_encoder.hpp"
54 55
#include "config.hpp"
#include "err.hpp"
56
#include "ip.hpp"
57
#include "tcp.hpp"
Martin Hurton's avatar
Martin Hurton committed
58 59
#include "likely.hpp"
#include "wire.hpp"
60

Chris Laws's avatar
Chris Laws committed
61
zmq::stream_engine_t::stream_engine_t (fd_t fd_, const options_t &options_,
62
                                       const std::string &endpoint_) :
63
    s (fd_),
64 65
    inpos (NULL),
    insize (0),
66
    decoder (NULL),
67 68
    outpos (NULL),
    outsize (0),
69
    encoder (NULL),
70
    metadata (NULL),
Martin Hurton's avatar
Martin Hurton committed
71
    handshaking (true),
72
    greeting_size (v2_greeting_size),
Martin Hurton's avatar
Martin Hurton committed
73
    greeting_bytes_read (0),
74 75
    session (NULL),
    options (options_),
76
    endpoint (endpoint_),
77
    plugged (false),
Martin Hurton's avatar
Martin Hurton committed
78 79
    next_msg (&stream_engine_t::identity_msg),
    process_msg (&stream_engine_t::process_identity_msg),
80 81
    io_error (false),
    subscription_required (false),
82
    mechanism (NULL),
83 84
    input_stopped (false),
    output_stopped (false),
85
    has_handshake_timer (false),
86
    socket (NULL)
87
{
88 89
    int rc = tx_msg.init ();
    errno_assert (rc == 0);
Chris Laws's avatar
Chris Laws committed
90

Martin Hurton's avatar
Martin Hurton committed
91
    //  Put the socket into non-blocking mode.
92
    unblock_socket (s);
93

94 95
    int family = get_peer_ip_address (s, peer_address);
    if (family == 0)
96
        peer_address = "";
97
#if defined ZMQ_HAVE_SO_PEERCRED
98 99
    else
    if (family == PF_UNIX) {
100 101 102 103 104 105 106 107 108
        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
109 110
    else
    if (family == PF_UNIX) {
111 112 113 114 115 116 117 118 119 120 121 122 123
        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
124

125
#ifdef SO_NOSIGPIPE
126 127 128
    //  Make sure that SIGPIPE signal is not generated when writing to a
    //  connection that was already closed by the peer.
    int set = 1;
129
    rc = setsockopt (s, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof (int));
130 131
    errno_assert (rc == 0);
#endif
132 133
}

134
zmq::stream_engine_t::~stream_engine_t ()
135 136 137
{
    zmq_assert (!plugged);

138 139
    if (s != retired_fd) {
#ifdef ZMQ_HAVE_WINDOWS
140 141
        int rc = closesocket (s);
        wsa_assert (rc != SOCKET_ERROR);
142
#else
143
        int rc = close (s);
144 145
        errno_assert (rc == 0);
#endif
146
        s = retired_fd;
147
    }
148

149 150 151
    int rc = tx_msg.close ();
    errno_assert (rc == 0);

152 153 154 155 156 157
    //  Drop reference to metadata and destroy it if we are
    //  the only user.
    if (metadata != NULL)
        if (metadata->drop_ref ())
            delete metadata;

158 159 160
    delete encoder;
    delete decoder;
    delete mechanism;
161 162
}

163 164
void zmq::stream_engine_t::plug (io_thread_t *io_thread_,
    session_base_t *session_)
165 166 167 168 169 170 171 172
{
    zmq_assert (!plugged);
    plugged = true;

    //  Connect to session object.
    zmq_assert (!session);
    zmq_assert (session_);
    session = session_;
173
    socket = session-> get_socket ();
174 175 176 177

    //  Connect to I/O threads poller object.
    io_object_t::plug (io_thread_);
    handle = add_fd (s);
178
    io_error = false;
Martin Hurton's avatar
Martin Hurton committed
179

Martin Hurton's avatar
Martin Hurton committed
180
    if (options.raw_sock) {
181
        // no handshaking for raw sock, instantiate raw encoder and decoders
182
        encoder = new (std::nothrow) raw_encoder_t (out_batch_size);
183 184
        alloc_assert (encoder);

185
        decoder = new (std::nothrow) raw_decoder_t (in_batch_size);
186 187 188 189
        alloc_assert (decoder);

        // disable handshaking for raw socket
        handshaking = false;
190

Martin Hurton's avatar
Martin Hurton committed
191 192
        next_msg = &stream_engine_t::pull_msg_from_session;
        process_msg = &stream_engine_t::push_msg_to_session;
193 194 195 196 197

        //  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();
Martin Hurton's avatar
Martin Hurton committed
198
        push_msg_to_session (&connector);
199 200
        connector.close();
        session->flush ();
Martin Hurton's avatar
Martin Hurton committed
201 202
    }
    else {
203 204 205
        // start optional timer, to prevent handshake hanging on no input
        set_handshake_timer ();

206 207
        //  Send the 'length' and 'flags' fields of the identity message.
        //  The 'length' field is encoded in the long format.
Pieter Hintjens's avatar
Pieter Hintjens committed
208
        outpos = greeting_send;
209 210 211 212 213
        outpos [outsize++] = 0xff;
        put_uint64 (&outpos [outsize], options.identity_size + 1);
        outsize += 8;
        outpos [outsize++] = 0x7f;
    }
Martin Hurton's avatar
Martin Hurton committed
214

215 216 217 218 219 220
    set_pollin (handle);
    set_pollout (handle);
    //  Flush all the data that may have been already received downstream.
    in_event ();
}

221
void zmq::stream_engine_t::unplug ()
222 223 224 225
{
    zmq_assert (plugged);
    plugged = false;

226 227 228 229 230 231
    //  Cancel all timers.
    if (has_handshake_timer) {
        cancel_timer (handshake_timer_id);
        has_handshake_timer = false;
    }

232
    //  Cancel all fd subscriptions.
233
    if (!io_error)
Martin Hurton's avatar
Martin Hurton committed
234
        rm_fd (handle);
235 236 237 238 239 240 241

    //  Disconnect from I/O threads poller object.
    io_object_t::unplug ();

    session = NULL;
}

242
void zmq::stream_engine_t::terminate ()
243 244 245 246 247
{
    unplug ();
    delete this;
}

248
void zmq::stream_engine_t::in_event ()
249
{
250
    zmq_assert (!io_error);
251

Pieter Hintjens's avatar
Pieter Hintjens committed
252
    //  If still handshaking, receive and process the greeting message.
Martin Hurton's avatar
Martin Hurton committed
253 254 255 256
    if (unlikely (handshaking))
        if (!handshake ())
            return;

257
    zmq_assert (decoder);
258 259

    //  If there has been an I/O error, stop polling.
260
    if (input_stopped) {
261 262 263 264
        rm_fd (handle);
        io_error = true;
        return;
    }
265 266 267 268 269 270 271 272

    //  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.
273 274
        size_t bufsize = 0;
        decoder->get_buffer (&inpos, &bufsize);
275

276
        const int rc = tcp_read (s, inpos, bufsize);
277
        if (rc == 0) {
278
            error (connection_error);
279
            return;
280
        }
281 282
        if (rc == -1) {
            if (errno != EAGAIN)
283
                error (connection_error);
284 285
            return;
        }
286

287
        //  Adjust input size
288
        insize = static_cast <size_t> (rc);
Martin Hurton's avatar
Martin Hurton committed
289
    }
290

291 292
    int rc = 0;
    size_t processed = 0;
293

294 295 296
    while (insize > 0) {
        rc = decoder->decode (inpos, insize, processed);
        zmq_assert (processed <= insize);
297 298
        inpos += processed;
        insize -= processed;
299 300
        if (rc == 0 || rc == -1)
            break;
Martin Hurton's avatar
Martin Hurton committed
301
        rc = (this->*process_msg) (decoder->msg ());
302 303
        if (rc == -1)
            break;
304 305
    }

306 307 308 309
    //  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) {
310
            error (protocol_error);
311 312
            return;
        }
313
        input_stopped = true;
314
        reset_pollin (handle);
Martin Hurton's avatar
Martin Hurton committed
315
    }
316 317

    session->flush ();
318 319
}

320
void zmq::stream_engine_t::out_event ()
321
{
322 323
    zmq_assert (!io_error);

324 325 326
    //  If write buffer is empty, try to read new data from the encoder.
    if (!outsize) {

Martin Hurton's avatar
Martin Hurton committed
327 328 329 330 331 332 333 334
        //  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;
        }

335
        outpos = NULL;
336 337 338
        outsize = encoder->encode (&outpos, 0);

        while (outsize < out_batch_size) {
Martin Hurton's avatar
Martin Hurton committed
339
            if ((this->*next_msg) (&tx_msg) == -1)
340 341 342 343 344 345 346 347 348
                break;
            encoder->load_msg (&tx_msg);
            unsigned char *bufptr = outpos + outsize;
            size_t n = encoder->encode (&bufptr, out_batch_size - outsize);
            zmq_assert (n > 0);
            if (outpos == NULL)
                outpos = bufptr;
            outsize += n;
        }
349 350 351

        //  If there is no data to send, stop polling for output.
        if (outsize == 0) {
352
            output_stopped = true;
353 354 355 356 357 358 359
            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
360
    //  arbitrarily large. However, we assume that underlying TCP layer has
361 362
    //  limited transmission buffer and thus the actual number of bytes
    //  written should be reasonably modest.
363
    const int nbytes = tcp_write (s, outpos, outsize);
364

Martin Hurton's avatar
Martin Hurton committed
365 366
    //  IO error has occurred. We stop waiting for output events.
    //  The engine is not terminated until we detect input error;
367
    //  this is necessary to prevent losing incoming messages.
368
    if (nbytes == -1) {
Martin Hurton's avatar
Martin Hurton committed
369
        reset_pollout (handle);
370 371 372 373 374
        return;
    }

    outpos += nbytes;
    outsize -= nbytes;
Martin Hurton's avatar
Martin Hurton committed
375 376 377 378 379 380

    //  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);
381 382
}

383
void zmq::stream_engine_t::restart_output ()
384
{
385 386 387
    if (unlikely (io_error))
        return;

388
    if (likely (output_stopped)) {
389
        set_pollout (handle);
390
        output_stopped = false;
391
    }
392 393 394 395 396 397 398 399

    //  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 ();
}

400
void zmq::stream_engine_t::restart_input ()
401
{
402
    zmq_assert (input_stopped);
403 404 405
    zmq_assert (session != NULL);
    zmq_assert (decoder != NULL);

Martin Hurton's avatar
Martin Hurton committed
406
    int rc = (this->*process_msg) (decoder->msg ());
407 408 409 410
    if (rc == -1) {
        if (errno == EAGAIN)
            session->flush ();
        else
411
            error (protocol_error);
Martin Hurton's avatar
Martin Hurton committed
412 413 414
        return;
    }

415 416 417 418 419 420 421 422
    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;
Martin Hurton's avatar
Martin Hurton committed
423
        rc = (this->*process_msg) (decoder->msg ());
424 425 426
        if (rc == -1)
            break;
    }
427

428 429 430
    if (rc == -1 && errno == EAGAIN)
        session->flush ();
    else
431 432 433 434 435
    if (io_error)
        error (connection_error);
    else
    if (rc == -1)
        error (protocol_error);
436
    else {
437
        input_stopped = false;
438 439 440 441 442 443
        set_pollin (handle);
        session->flush ();

        //  Speculative read.
        in_event ();
    }
444 445
}

446
bool zmq::stream_engine_t::handshake ()
Martin Hurton's avatar
Martin Hurton committed
447
{
448
    zmq_assert (handshaking);
Martin Hurton's avatar
Martin Hurton committed
449
    zmq_assert (greeting_bytes_read < greeting_size);
450
    //  Receive the greeting.
Martin Hurton's avatar
Martin Hurton committed
451
    while (greeting_bytes_read < greeting_size) {
452 453
        const int n = tcp_read (s, greeting_recv + greeting_bytes_read,
                                greeting_size - greeting_bytes_read);
454
        if (n == 0) {
455
            error (connection_error);
456 457
            return false;
        }
458 459
        if (n == -1) {
            if (errno != EAGAIN)
460
                error (connection_error);
461
            return false;
462
        }
Martin Hurton's avatar
Martin Hurton committed
463 464 465

        greeting_bytes_read += n;

466 467 468
        //  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.
Pieter Hintjens's avatar
Pieter Hintjens committed
469
        if (greeting_recv [0] != 0xff)
470
            break;
Martin Hurton's avatar
Martin Hurton committed
471

472
        if (greeting_bytes_read < signature_size)
473
            continue;
Martin Hurton's avatar
Martin Hurton committed
474

475 476 477 478
        //  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).
Pieter Hintjens's avatar
Pieter Hintjens committed
479
        if (!(greeting_recv [9] & 0x01))
480
            break;
Martin Hurton's avatar
Martin Hurton committed
481

482
        //  The peer is using versioned protocol.
483 484
        //  Send the major version number.
        if (outpos + outsize == greeting_send + signature_size) {
Martin Hurton's avatar
Martin Hurton committed
485 486
            if (outsize == 0)
                set_pollout (handle);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
            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);
502 503 504

                    zmq_assert (options.mechanism == ZMQ_NULL
                            ||  options.mechanism == ZMQ_PLAIN
505 506
                            ||  options.mechanism == ZMQ_CURVE
                            ||  options.mechanism == ZMQ_GSSAPI);
507

508 509 510
                    if (options.mechanism == ZMQ_NULL)
                        memcpy (outpos + outsize, "NULL", 4);
                    else
511
                    if (options.mechanism == ZMQ_PLAIN)
512
                        memcpy (outpos + outsize, "PLAIN", 5);
513 514 515
                    else
                    if (options.mechanism == ZMQ_GSSAPI)
                        memcpy (outpos + outsize, "GSSAPI", 6);
516
                    else
517
                    if (options.mechanism == ZMQ_CURVE)
518
                        memcpy (outpos + outsize, "CURVE", 5);
519 520 521 522 523 524
                    outsize += 20;
                    memset (outpos + outsize, 0, 32);
                    outsize += 32;
                    greeting_size = v3_greeting_size;
                }
            }
Martin Hurton's avatar
Martin Hurton committed
525 526 527
        }
    }

528 529
    //  Position of the revision field in the greeting.
    const size_t revision_pos = 10;
530

531 532
    //  Is the peer using ZMTP/1.0 with no revision number?
    //  If so, we send and receive rest of identity message
Pieter Hintjens's avatar
Pieter Hintjens committed
533
    if (greeting_recv [0] != 0xff || !(greeting_recv [9] & 0x01)) {
534
        encoder = new (std::nothrow) v1_encoder_t (out_batch_size);
535 536
        alloc_assert (encoder);

537
        decoder = new (std::nothrow) v1_decoder_t (in_batch_size, options.maxmsgsize);
538 539
        alloc_assert (decoder);

Martin Hurton's avatar
Martin Hurton committed
540 541 542 543 544 545
        //  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;
546 547 548 549 550 551 552

        //  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);
553
        size_t buffer_size = encoder->encode (&bufferp, header_size);
Martin Hurton's avatar
Martin Hurton committed
554 555 556
        zmq_assert (buffer_size == header_size);

        //  Make sure the decoder sees the data we have already received.
Pieter Hintjens's avatar
Pieter Hintjens committed
557
        inpos = greeting_recv;
Martin Hurton's avatar
Martin Hurton committed
558
        insize = greeting_bytes_read;
559 560

        //  To allow for interoperability with peers that do not forward
561 562
        //  their subscriptions, we inject a phantom subscription message
        //  message into the incoming message stream.
563
        if (options.type == ZMQ_PUB || options.type == ZMQ_XPUB)
564
            subscription_required = true;
Martin Hurton's avatar
Martin Hurton committed
565 566 567

        //  We are sending our identity now and the next message
        //  will come from the socket.
Martin Hurton's avatar
Martin Hurton committed
568
        next_msg = &stream_engine_t::pull_msg_from_session;
Martin Hurton's avatar
Martin Hurton committed
569 570

        //  We are expecting identity message.
Martin Hurton's avatar
Martin Hurton committed
571
        process_msg = &stream_engine_t::process_identity_msg;
Martin Hurton's avatar
Martin Hurton committed
572
    }
573
    else
Pieter Hintjens's avatar
Pieter Hintjens committed
574
    if (greeting_recv [revision_pos] == ZMTP_1_0) {
575 576
        encoder = new (std::nothrow) v1_encoder_t (
            out_batch_size);
577 578
        alloc_assert (encoder);

579 580
        decoder = new (std::nothrow) v1_decoder_t (
            in_batch_size, options.maxmsgsize);
581 582
        alloc_assert (decoder);
    }
583 584 585 586 587 588 589 590 591
    else
    if (greeting_recv [revision_pos] == ZMTP_2_0) {
        encoder = new (std::nothrow) v2_encoder_t (out_batch_size);
        alloc_assert (encoder);

        decoder = new (std::nothrow) v2_decoder_t (
            in_batch_size, options.maxmsgsize);
        alloc_assert (decoder);
    }
592
    else {
593
        encoder = new (std::nothrow) v2_encoder_t (out_batch_size);
594 595
        alloc_assert (encoder);

596
        decoder = new (std::nothrow) v2_decoder_t (
597
            in_batch_size, options.maxmsgsize);
598
        alloc_assert (decoder);
599 600

        if (memcmp (greeting_recv + 12, "NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
601 602
            mechanism = new (std::nothrow)
                null_mechanism_t (session, peer_address, options);
603
            alloc_assert (mechanism);
604 605 606
        }
        else
        if (memcmp (greeting_recv + 12, "PLAIN\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
607 608 609 610 611 612
            if (options.as_server)
                mechanism = new (std::nothrow)
                    plain_server_t (session, peer_address, options);
            else
                mechanism = new (std::nothrow)
                    plain_client_t (options);
613
            alloc_assert (mechanism);
614
        }
615 616 617 618
#ifdef HAVE_LIBSODIUM
        else
        if (memcmp (greeting_recv + 12, "CURVE\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
            if (options.as_server)
619 620
                mechanism = new (std::nothrow)
                    curve_server_t (session, peer_address, options);
621 622 623 624 625
            else
                mechanism = new (std::nothrow) curve_client_t (options);
            alloc_assert (mechanism);
        }
#endif
Chris Laws's avatar
Chris Laws committed
626
#ifdef HAVE_LIBGSSAPI_KRB5
627 628
        else
        if (memcmp (greeting_recv + 12, "GSSAPI\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
629 630 631 632 633
            if (options.as_server)
                mechanism = new (std::nothrow)
                    gssapi_server_t (session, peer_address, options);
            else
                mechanism = new (std::nothrow) gssapi_client_t (options);
634 635
            alloc_assert (mechanism);
        }
Chris Laws's avatar
Chris Laws committed
636
#endif
637
        else {
638
            error (protocol_error);
639 640
            return false;
        }
Martin Hurton's avatar
Martin Hurton committed
641 642
        next_msg = &stream_engine_t::next_handshake_command;
        process_msg = &stream_engine_t::process_handshake_command;
643
    }
Martin Hurton's avatar
Martin Hurton committed
644 645 646 647 648 649 650 651 652

    // Start polling for output if necessary.
    if (outsize == 0)
        set_pollout (handle);

    //  Handshaking was successful.
    //  Switch into the normal message flow.
    handshaking = false;

653 654 655 656 657
    if (has_handshake_timer) {
        cancel_timer (handshake_timer_id);
        has_handshake_timer = false;
    }

Martin Hurton's avatar
Martin Hurton committed
658 659 660
    return true;
}

Martin Hurton's avatar
Martin Hurton committed
661
int zmq::stream_engine_t::identity_msg (msg_t *msg_)
662
{
663 664 665 666
    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);
Martin Hurton's avatar
Martin Hurton committed
667
    next_msg = &stream_engine_t::pull_msg_from_session;
668 669
    return 0;
}
670

Martin Hurton's avatar
Martin Hurton committed
671
int zmq::stream_engine_t::process_identity_msg (msg_t *msg_)
672 673 674 675
{
    if (options.recv_identity) {
        msg_->set_flags (msg_t::identity);
        int rc = session->push_msg (msg_);
676 677
        errno_assert (rc == 0);
    }
678 679 680 681 682 683 684 685
    else {
        int rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
    }

    if (subscription_required)
Martin Hurton's avatar
Martin Hurton committed
686
        process_msg = &stream_engine_t::write_subscription_msg;
687
    else
Martin Hurton's avatar
Martin Hurton committed
688
        process_msg = &stream_engine_t::push_msg_to_session;
689

690 691
    return 0;
}
692

693
int zmq::stream_engine_t::next_handshake_command (msg_t *msg_)
694
{
695 696
    zmq_assert (mechanism != NULL);

697 698 699 700 701 702 703 704 705 706 707 708 709 710
    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;
711
    }
712 713
}

714
int zmq::stream_engine_t::process_handshake_command (msg_t *msg_)
715
{
716
    zmq_assert (mechanism != NULL);
717
    const int rc = mechanism->process_handshake_command (msg_);
718
    if (rc == 0) {
719
        if (mechanism->status () == mechanism_t::ready)
720
            mechanism_ready ();
721 722 723 724 725
        else
        if (mechanism->status () == mechanism_t::error) {
            errno = EPROTO;
            return -1;
        }
726 727
        if (output_stopped)
            restart_output ();
728 729
    }

730
    return rc;
731 732
}

733 734 735 736 737 738
void zmq::stream_engine_t::zap_msg_available ()
{
    zmq_assert (mechanism != NULL);

    const int rc = mechanism->zap_msg_available ();
    if (rc == -1) {
739
        error (protocol_error);
740 741
        return;
    }
742 743 744 745
    if (input_stopped)
        restart_input ();
    if (output_stopped)
        restart_output ();
746 747
}

748
void zmq::stream_engine_t::mechanism_ready ()
749
{
750 751 752 753
    if (options.recv_identity) {
        msg_t identity;
        mechanism->peer_identity (&identity);
        const int rc = session->push_msg (&identity);
754 755 756 757 758 759
        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;
        }
760
        errno_assert (rc == 0);
761
        session->flush ();
762
    }
763

Martin Hurton's avatar
Martin Hurton committed
764 765
    next_msg = &stream_engine_t::pull_and_encode;
    process_msg = &stream_engine_t::write_credential;
766 767 768 769 770 771

    //  Compile metadata.
    typedef metadata_t::dict_t properties_t;
    properties_t properties;
    properties_t::const_iterator it;

Pieter Hintjens's avatar
Pieter Hintjens committed
772 773 774 775 776 777 778 779
    //  Add ZAP properties.
    const properties_t& zap_properties = mechanism->get_zap_properties ();
    it = zap_properties.begin ();
    while (it != zap_properties.end ()) {
        properties.insert (properties_t::value_type (it->first, it->second));
        it++;
    }

780 781 782 783 784 785 786 787
    //  Add ZMTP properties.
    const properties_t& zmtp_properties = mechanism->get_zmtp_properties ();
    it = zmtp_properties.begin ();
    while (it != zmtp_properties.end ()) {
        properties.insert (properties_t::value_type (it->first, it->second));
        it++;
    }

788 789 790
    zmq_assert (metadata == NULL);
    if (!properties.empty ())
        metadata = new (std::nothrow) metadata_t (properties);
791 792
}

793
int zmq::stream_engine_t::pull_msg_from_session (msg_t *msg_)
794
{
795 796
    return session->pull_msg (msg_);
}
797

798 799 800 801 802
int zmq::stream_engine_t::push_msg_to_session (msg_t *msg_)
{
    return session->push_msg (msg_);
}

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
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;
        }
    }
Martin Hurton's avatar
Martin Hurton committed
822
    process_msg = &stream_engine_t::decode_and_push;
823 824 825
    return decode_and_push (msg_);
}

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
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;
843
    if (metadata)
844
        msg_->set_metadata (metadata);
845 846
    if (session->push_msg (msg_) == -1) {
        if (errno == EAGAIN)
Martin Hurton's avatar
Martin Hurton committed
847
            process_msg = &stream_engine_t::push_one_then_decode_and_push;
848 849 850 851 852 853 854 855 856
        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)
Martin Hurton's avatar
Martin Hurton committed
857
        process_msg = &stream_engine_t::decode_and_push;
858 859 860
    return rc;
}

861 862 863
int zmq::stream_engine_t::write_subscription_msg (msg_t *msg_)
{
    msg_t subscription;
864

865 866
    //  Inject the subscription message, so that also
    //  ZMQ 2.x peers receive published messages.
867 868 869 870 871 872
    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;
873

Martin Hurton's avatar
Martin Hurton committed
874
    process_msg = &stream_engine_t::push_msg_to_session;
875
    return push_msg_to_session (msg_);
876 877
}

878
void zmq::stream_engine_t::error (error_reason_t reason)
879
{
880 881 882 883 884
    if (options.raw_sock) {
        //  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();
Martin Hurton's avatar
Martin Hurton committed
885
        (this->*process_msg) (&terminator);
886 887
        terminator.close();
    }
888
    zmq_assert (session);
889
    socket->event_disconnected (endpoint, s);
890
    session->flush ();
891
    session->engine_error (reason);
892 893 894 895
    unplug ();
    delete this;
}

896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
void zmq::stream_engine_t::set_handshake_timer ()
{
    zmq_assert (!has_handshake_timer);

    if (!options.raw_sock && options.handshake_ivl > 0) {
        add_timer (options.handshake_ivl, handshake_timer_id);
        has_handshake_timer = true;
    }
}

void zmq::stream_engine_t::timer_event (int id_)
{
    zmq_assert (id_ == handshake_timer_id);
    has_handshake_timer = false;

    //  handshake timer expired before handshake completed, so engine fails
912
    error (timeout_error);
913
}