1/*
2 Copyright (c) 2007-2019 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#ifndef __ZMQ_ZMTP_ENGINE_HPP_INCLUDED__
31#define __ZMQ_ZMTP_ENGINE_HPP_INCLUDED__
32
33#include <stddef.h>
34
35#include "fd.hpp"
36#include "i_engine.hpp"
37#include "io_object.hpp"
38#include "i_encoder.hpp"
39#include "i_decoder.hpp"
40#include "options.hpp"
41#include "socket_base.hpp"
42#include "metadata.hpp"
43#include "msg.hpp"
44#include "stream_engine_base.hpp"
45
46namespace zmq
47{
48// Protocol revisions
49enum
50{
51 ZMTP_1_0 = 0,
52 ZMTP_2_0 = 1
53};
54
55class io_thread_t;
56class session_base_t;
57class mechanism_t;
58
59// This engine handles any socket with SOCK_STREAM semantics,
60// e.g. TCP socket or an UNIX domain socket.
61
62class zmtp_engine_t : public stream_engine_base_t
63{
64 public:
65 zmtp_engine_t (fd_t fd_,
66 const options_t &options_,
67 const endpoint_uri_pair_t &endpoint_uri_pair_);
68 ~zmtp_engine_t ();
69
70 protected:
71 // Detects the protocol used by the peer.
72 bool handshake ();
73
74 void plug_internal ();
75
76 int process_command_message (msg_t *msg_);
77 int produce_ping_message (msg_t *msg_);
78 int process_heartbeat_message (msg_t *msg_);
79 int produce_pong_message (msg_t *msg_);
80
81 private:
82 // Receive the greeting from the peer.
83 int receive_greeting ();
84 void receive_greeting_versioned ();
85
86 typedef bool (zmtp_engine_t::*handshake_fun_t) ();
87 static handshake_fun_t select_handshake_fun (bool unversioned,
88 unsigned char revision);
89
90 bool handshake_v1_0_unversioned ();
91 bool handshake_v1_0 ();
92 bool handshake_v2_0 ();
93 bool handshake_v3_0 ();
94
95 int routing_id_msg (msg_t *msg_);
96 int process_routing_id_msg (msg_t *msg_);
97
98 msg_t _routing_id_msg;
99
100 // Need to store PING payload for PONG
101 msg_t _pong_msg;
102
103 static const size_t signature_size = 10;
104
105 // Size of ZMTP/1.0 and ZMTP/2.0 greeting message
106 static const size_t v2_greeting_size = 12;
107
108 // Size of ZMTP/3.0 greeting message
109 static const size_t v3_greeting_size = 64;
110
111 // Expected greeting size.
112 size_t _greeting_size;
113
114 // Greeting received from, and sent to peer
115 unsigned char _greeting_recv[v3_greeting_size];
116 unsigned char _greeting_send[v3_greeting_size];
117
118 // Size of greeting received so far
119 unsigned int _greeting_bytes_read;
120
121 // Indicates whether the engine is to inject a phantom
122 // subscription message into the incoming stream.
123 // Needed to support old peers.
124 bool _subscription_required;
125
126 int _heartbeat_timeout;
127
128 ZMQ_NON_COPYABLE_NOR_MOVABLE (zmtp_engine_t)
129};
130}
131
132#endif
133