forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_engine_base.cpp
772 lines (661 loc) · 21.3 KB
/
stream_engine_base.cpp
1
2
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* SPDX-License-Identifier: MPL-2.0 */
#include "precompiled.hpp"
#include "macros.hpp"
#include <limits.h>
#include <string.h>
#ifndef ZMQ_HAVE_WINDOWS
#include <unistd.h>
#endif
#include <new>
#include <sstream>
#include "stream_engine_base.hpp"
#include "io_thread.hpp"
#include "session_base.hpp"
#include "v1_encoder.hpp"
#include "v1_decoder.hpp"
#include "v2_encoder.hpp"
#include "v2_decoder.hpp"
#include "null_mechanism.hpp"
#include "plain_client.hpp"
#include "plain_server.hpp"
#include "gssapi_client.hpp"
#include "gssapi_server.hpp"
#include "curve_client.hpp"
#include "curve_server.hpp"
#include "raw_decoder.hpp"
#include "raw_encoder.hpp"
#include "config.hpp"
#include "err.hpp"
#include "ip.hpp"
#include "tcp.hpp"
#include "likely.hpp"
#include "wire.hpp"
static std::string get_peer_address (zmq::fd_t s_)
{
std::string peer_address;
const int family = zmq::get_peer_ip_address (s_, peer_address);
if (family == 0)
peer_address.clear ();
#if defined ZMQ_HAVE_SO_PEERCRED
else if (family == PF_UNIX) {
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
else if (family == PF_UNIX) {
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
return peer_address;
}
zmq::stream_engine_base_t::stream_engine_base_t (
fd_t fd_,
const options_t &options_,
const endpoint_uri_pair_t &endpoint_uri_pair_,
bool has_handshake_stage_) :
_options (options_),
_inpos (NULL),
_insize (0),
_decoder (NULL),
_outpos (NULL),
_outsize (0),
_encoder (NULL),
_mechanism (NULL),
_next_msg (NULL),
_process_msg (NULL),
_metadata (NULL),
_input_stopped (false),
_output_stopped (false),
_endpoint_uri_pair (endpoint_uri_pair_),
_has_handshake_timer (false),
_has_ttl_timer (false),
_has_timeout_timer (false),
_has_heartbeat_timer (false),
_peer_address (get_peer_address (fd_)),
_s (fd_),
_handle (static_cast<handle_t> (NULL)),
_plugged (false),
_handshaking (true),
_io_error (false),
_session (NULL),
_socket (NULL),
_has_handshake_stage (has_handshake_stage_)
{
const int rc = _tx_msg.init ();
errno_assert (rc == 0);
// Put the socket into non-blocking mode.
unblock_socket (_s);
}
zmq::stream_engine_base_t::~stream_engine_base_t ()
{
zmq_assert (!_plugged);
if (_s != retired_fd) {
#ifdef ZMQ_HAVE_WINDOWS
const int rc = closesocket (_s);
wsa_assert (rc != SOCKET_ERROR);
#else
int rc = close (_s);
#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
// FreeBSD may return ECONNRESET on close() under load but this is not
// an error.
if (rc == -1 && errno == ECONNRESET)
rc = 0;
#endif
errno_assert (rc == 0);
#endif
_s = retired_fd;
}
const int rc = _tx_msg.close ();
errno_assert (rc == 0);
// Drop reference to metadata and destroy it if we are
// the only user.
if (_metadata != NULL) {
if (_metadata->drop_ref ()) {
LIBZMQ_DELETE (_metadata);
}
}
LIBZMQ_DELETE (_encoder);
LIBZMQ_DELETE (_decoder);
LIBZMQ_DELETE (_mechanism);
}
void zmq::stream_engine_base_t::plug (io_thread_t *io_thread_,
session_base_t *session_)
{
zmq_assert (!_plugged);
_plugged = true;
// Connect to session object.
zmq_assert (!_session);
zmq_assert (session_);
_session = session_;
_socket = _session->get_socket ();
// Connect to I/O threads poller object.
io_object_t::plug (io_thread_);
_handle = add_fd (_s);
_io_error = false;
plug_internal ();
}
void zmq::stream_engine_base_t::unplug ()
{
zmq_assert (_plugged);
_plugged = false;
// Cancel all timers.
if (_has_handshake_timer) {
cancel_timer (handshake_timer_id);
_has_handshake_timer = false;
}
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;
}
// Cancel all fd subscriptions.
if (!_io_error)
rm_fd (_handle);
// Disconnect from I/O threads poller object.
io_object_t::unplug ();
_session = NULL;
}
void zmq::stream_engine_base_t::terminate ()
{
unplug ();
delete this;
}
void zmq::stream_engine_base_t::in_event ()
{
// ignore errors
const bool res = in_event_internal ();
LIBZMQ_UNUSED (res);
}
bool zmq::stream_engine_base_t::in_event_internal ()
{
zmq_assert (!_io_error);
// If still handshaking, receive and process the greeting message.
if (unlikely (_handshaking)) {
if (handshake ()) {
// Handshaking was successful.
// Switch into the normal message flow.
_handshaking = false;
if (_mechanism == NULL && _has_handshake_stage) {
_session->engine_ready ();
if (_has_handshake_timer) {
cancel_timer (handshake_timer_id);
_has_handshake_timer = false;
}
}
} else
return false;
}
zmq_assert (_decoder);
// If there has been an I/O error, stop polling.
if (_input_stopped) {
rm_fd (_handle);
_io_error = true;
return true; // TODO or return false in this case too?
}
// 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.
size_t bufsize = 0;
_decoder->get_buffer (&_inpos, &bufsize);
const int rc = read (_inpos, bufsize);
if (rc == -1) {
if (errno != EAGAIN) {
error (connection_error);
return false;
}
return true;
}
// Adjust input size
_insize = static_cast<size_t> (rc);
// Adjust buffer size to received bytes
_decoder->resize_buffer (_insize);
}
int rc = 0;
size_t processed = 0;
while (_insize > 0) {
rc = _decoder->decode (_inpos, _insize, processed);
zmq_assert (processed <= _insize);
_inpos += processed;
_insize -= processed;
if (rc == 0 || rc == -1)
break;
rc = (this->*_process_msg) (_decoder->msg ());
if (rc == -1)
break;
}
// 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) {
// In cases where the src/dst have the same IP and the dst uses an ephemeral port, reconnection
// eventually results in the src and dest IP and port clashing (google tcp self connection)
// While this is a protocol_error (you have the single zmq socket handshaking with itself)
// we do not want to to stop reconnection from happening
if (!_endpoint_uri_pair.clash ()) {
error (protocol_error);
return false;
}
}
_input_stopped = true;
reset_pollin (_handle);
}
_session->flush ();
return true;
}
void zmq::stream_engine_base_t::out_event ()
{
zmq_assert (!_io_error);
// If write buffer is empty, try to read new data from the encoder.
if (!_outsize) {
// 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;
}
_outpos = NULL;
_outsize = _encoder->encode (&_outpos, 0);
while (_outsize < static_cast<size_t> (_options.out_batch_size)) {
if ((this->*_next_msg) (&_tx_msg) == -1) {
// ws_engine can cause an engine error and delete it, so
// bail out immediately to avoid use-after-free
if (errno == ECONNRESET)
return;
else
break;
}
_encoder->load_msg (&_tx_msg);
unsigned char *bufptr = _outpos + _outsize;
const size_t n =
_encoder->encode (&bufptr, _options.out_batch_size - _outsize);
zmq_assert (n > 0);
if (_outpos == NULL)
_outpos = bufptr;
_outsize += n;
}
// If there is no data to send, stop polling for output.
if (_outsize == 0) {
_output_stopped = true;
reset_pollout ();
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
// arbitrarily large. However, we assume that underlying TCP layer has
// limited transmission buffer and thus the actual number of bytes
// written should be reasonably modest.
const int nbytes = write (_outpos, _outsize);
// IO error has occurred. We stop waiting for output events.
// The engine is not terminated until we detect input error;
// this is necessary to prevent losing incoming messages.
if (nbytes == -1) {
reset_pollout ();
return;
}
_outpos += nbytes;
_outsize -= nbytes;
// If we are still handshaking and there are no data
// to send, stop polling for output.
if (unlikely (_handshaking))
if (_outsize == 0)
reset_pollout ();
}
void zmq::stream_engine_base_t::restart_output ()
{
if (unlikely (_io_error))
return;
if (likely (_output_stopped)) {
set_pollout ();
_output_stopped = false;
}
// 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 ();
}
bool zmq::stream_engine_base_t::restart_input ()
{
zmq_assert (_input_stopped);
zmq_assert (_session != NULL);
zmq_assert (_decoder != NULL);
int rc = (this->*_process_msg) (_decoder->msg ());
if (rc == -1) {
if (errno == EAGAIN)
_session->flush ();
else {
error (protocol_error);
return false;
}
return true;
}
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;
rc = (this->*_process_msg) (_decoder->msg ());
if (rc == -1)
break;
}
if (rc == -1 && errno == EAGAIN)
_session->flush ();
else if (_io_error) {
error (connection_error);
return false;
} else if (rc == -1) {
error (protocol_error);
return false;
}
else {
_input_stopped = false;
set_pollin ();
_session->flush ();
// Speculative read.
if (!in_event_internal ())
return false;
}
return true;
}
int zmq::stream_engine_base_t::next_handshake_command (msg_t *msg_)
{
zmq_assert (_mechanism != NULL);
if (_mechanism->status () == mechanism_t::ready) {
mechanism_ready ();
return pull_and_encode (msg_);
}
if (_mechanism->status () == mechanism_t::error) {
errno = EPROTO;
return -1;
}
const int rc = _mechanism->next_handshake_command (msg_);
if (rc == 0)
msg_->set_flags (msg_t::command);
return rc;
}
int zmq::stream_engine_base_t::process_handshake_command (msg_t *msg_)
{
zmq_assert (_mechanism != NULL);
const int rc = _mechanism->process_handshake_command (msg_);
if (rc == 0) {
if (_mechanism->status () == mechanism_t::ready)
mechanism_ready ();
else if (_mechanism->status () == mechanism_t::error) {
errno = EPROTO;
return -1;
}
if (_output_stopped)
restart_output ();
}
return rc;
}
void zmq::stream_engine_base_t::zap_msg_available ()
{
zmq_assert (_mechanism != NULL);
const int rc = _mechanism->zap_msg_available ();
if (rc == -1) {
error (protocol_error);
return;
}
if (_input_stopped)
if (!restart_input ())
return;
if (_output_stopped)
restart_output ();
}
const zmq::endpoint_uri_pair_t &zmq::stream_engine_base_t::get_endpoint () const
{
return _endpoint_uri_pair;
}
void zmq::stream_engine_base_t::mechanism_ready ()
{
if (_options.heartbeat_interval > 0 && !_has_heartbeat_timer) {
add_timer (_options.heartbeat_interval, heartbeat_ivl_timer_id);
_has_heartbeat_timer = true;
}
if (_has_handshake_stage)
_session->engine_ready ();
bool flush_session = false;
if (_options.recv_routing_id) {
msg_t routing_id;
_mechanism->peer_routing_id (&routing_id);
const int rc = _session->push_msg (&routing_id);
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 routing id set.
return;
}
errno_assert (rc == 0);
flush_session = true;
}
if (_options.router_notify & ZMQ_NOTIFY_CONNECT) {
msg_t connect_notification;
connect_notification.init ();
const int rc = _session->push_msg (&connect_notification);
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 notification.
return;
}
errno_assert (rc == 0);
flush_session = true;
}
if (flush_session)
_session->flush ();
_next_msg = &stream_engine_base_t::pull_and_encode;
_process_msg = &stream_engine_base_t::write_credential;
// Compile metadata.
properties_t properties;
init_properties (properties);
// Add ZAP properties.
const properties_t &zap_properties = _mechanism->get_zap_properties ();
properties.insert (zap_properties.begin (), zap_properties.end ());
// Add ZMTP properties.
const properties_t &zmtp_properties = _mechanism->get_zmtp_properties ();
properties.insert (zmtp_properties.begin (), zmtp_properties.end ());
zmq_assert (_metadata == NULL);
if (!properties.empty ()) {
_metadata = new (std::nothrow) metadata_t (properties);
alloc_assert (_metadata);
}
if (_has_handshake_timer) {
cancel_timer (handshake_timer_id);
_has_handshake_timer = false;
}
_socket->event_handshake_succeeded (_endpoint_uri_pair, 0);
}
int zmq::stream_engine_base_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;
}
}
_process_msg = &stream_engine_base_t::decode_and_push;
return decode_and_push (msg_);
}
int zmq::stream_engine_base_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_base_t::decode_and_push (msg_t *msg_)
{
zmq_assert (_mechanism != NULL);
if (_mechanism->decode (msg_) == -1)
return -1;
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) {
process_command_message (msg_);
}
if (_metadata)
msg_->set_metadata (_metadata);
if (_session->push_msg (msg_) == -1) {
if (errno == EAGAIN)
_process_msg = &stream_engine_base_t::push_one_then_decode_and_push;
return -1;
}
return 0;
}
int zmq::stream_engine_base_t::push_one_then_decode_and_push (msg_t *msg_)
{
const int rc = _session->push_msg (msg_);
if (rc == 0)
_process_msg = &stream_engine_base_t::decode_and_push;
return rc;
}
int zmq::stream_engine_base_t::pull_msg_from_session (msg_t *msg_)
{
return _session->pull_msg (msg_);
}
int zmq::stream_engine_base_t::push_msg_to_session (msg_t *msg_)
{
return _session->push_msg (msg_);
}
void zmq::stream_engine_base_t::error (error_reason_t reason_)
{
zmq_assert (_session);
if ((_options.router_notify & ZMQ_NOTIFY_DISCONNECT) && !_handshaking) {
// For router sockets with disconnect notification, rollback
// any incomplete message in the pipe, and push the disconnect
// notification message.
_session->rollback ();
msg_t disconnect_notification;
disconnect_notification.init ();
_session->push_msg (&disconnect_notification);
}
// protocol errors have been signaled already at the point where they occurred
if (reason_ != protocol_error
&& (_mechanism == NULL
|| _mechanism->status () == mechanism_t::handshaking)) {
const int err = errno;
_socket->event_handshake_failed_no_detail (_endpoint_uri_pair, err);
// special case: connecting to non-ZMTP process which immediately drops connection,
// or which never responds with greeting, should be treated as a protocol error
// (i.e. stop reconnect)
if (((reason_ == connection_error) || (reason_ == timeout_error))
&& (_options.reconnect_stop
& ZMQ_RECONNECT_STOP_HANDSHAKE_FAILED)) {
reason_ = protocol_error;
}
}
_socket->event_disconnected (_endpoint_uri_pair, _s);
_session->flush ();
_session->engine_error (
!_handshaking
&& (_mechanism == NULL
|| _mechanism->status () != mechanism_t::handshaking),
reason_);
unplug ();
delete this;
}
void zmq::stream_engine_base_t::set_handshake_timer ()
{
zmq_assert (!_has_handshake_timer);
if (_options.handshake_ivl > 0) {
add_timer (_options.handshake_ivl, handshake_timer_id);
_has_handshake_timer = true;
}
}
bool zmq::stream_engine_base_t::init_properties (properties_t &properties_)
{
if (_peer_address.empty ())
return false;
properties_.ZMQ_MAP_INSERT_OR_EMPLACE (
std::string (ZMQ_MSG_PROPERTY_PEER_ADDRESS), _peer_address);
// Private property to support deprecated SRCFD
std::ostringstream stream;
stream << static_cast<int> (_s);
std::string fd_string = stream.str ();
properties_.ZMQ_MAP_INSERT_OR_EMPLACE (std::string ("__fd"),
ZMQ_MOVE (fd_string));
return true;
}
void zmq::stream_engine_base_t::timer_event (int id_)
{
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_base_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_base_t::read (void *data_, size_t size_)
{
const int rc = zmq::tcp_read (_s, data_, size_);
if (rc == 0) {
// connection closed by peer
errno = EPIPE;
return -1;
}
return rc;
}
int zmq::stream_engine_base_t::write (const void *data_, size_t size_)
{
return zmq::tcp_write (_s, data_, size_);
}