forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzap_client.cpp
286 lines (247 loc) · 8.94 KB
/
zap_client.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
/* SPDX-License-Identifier: MPL-2.0 */
#include "precompiled.hpp"
#include "zap_client.hpp"
#include "msg.hpp"
#include "session_base.hpp"
namespace zmq
{
const char zap_version[] = "1.0";
const size_t zap_version_len = sizeof (zap_version) - 1;
const char id[] = "1";
const size_t id_len = sizeof (id) - 1;
zap_client_t::zap_client_t (session_base_t *const session_,
const std::string &peer_address_,
const options_t &options_) :
mechanism_base_t (session_, options_), peer_address (peer_address_)
{
}
void zap_client_t::send_zap_request (const char *mechanism_,
size_t mechanism_length_,
const uint8_t *credentials_,
size_t credentials_size_)
{
send_zap_request (mechanism_, mechanism_length_, &credentials_,
&credentials_size_, 1);
}
void zap_client_t::send_zap_request (const char *mechanism_,
size_t mechanism_length_,
const uint8_t **credentials_,
size_t *credentials_sizes_,
size_t credentials_count_)
{
// write_zap_msg cannot fail. It could only fail if the HWM was exceeded,
// but on the ZAP socket, the HWM is disabled.
int rc;
msg_t msg;
// Address delimiter frame
rc = msg.init ();
errno_assert (rc == 0);
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
// Version frame
rc = msg.init_size (zap_version_len);
errno_assert (rc == 0);
memcpy (msg.data (), zap_version, zap_version_len);
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
// Request ID frame
rc = msg.init_size (id_len);
errno_assert (rc == 0);
memcpy (msg.data (), id, id_len);
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
// Domain frame
rc = msg.init_size (options.zap_domain.length ());
errno_assert (rc == 0);
memcpy (msg.data (), options.zap_domain.c_str (),
options.zap_domain.length ());
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
// Address frame
rc = msg.init_size (peer_address.length ());
errno_assert (rc == 0);
memcpy (msg.data (), peer_address.c_str (), peer_address.length ());
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
// Routing id frame
rc = msg.init_size (options.routing_id_size);
errno_assert (rc == 0);
memcpy (msg.data (), options.routing_id, options.routing_id_size);
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
// Mechanism frame
rc = msg.init_size (mechanism_length_);
errno_assert (rc == 0);
memcpy (msg.data (), mechanism_, mechanism_length_);
if (credentials_count_)
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
// Credentials frames
for (size_t i = 0; i < credentials_count_; ++i) {
rc = msg.init_size (credentials_sizes_[i]);
errno_assert (rc == 0);
if (i < credentials_count_ - 1)
msg.set_flags (msg_t::more);
memcpy (msg.data (), credentials_[i], credentials_sizes_[i]);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
}
}
int zap_client_t::receive_and_process_zap_reply ()
{
int rc = 0;
const size_t zap_reply_frame_count = 7;
msg_t msg[zap_reply_frame_count];
// Initialize all reply frames
for (size_t i = 0; i < zap_reply_frame_count; i++) {
rc = msg[i].init ();
errno_assert (rc == 0);
}
for (size_t i = 0; i < zap_reply_frame_count; i++) {
rc = session->read_zap_msg (&msg[i]);
if (rc == -1) {
if (errno == EAGAIN) {
return 1;
}
return close_and_return (msg, -1);
}
if ((msg[i].flags () & msg_t::more)
== (i < zap_reply_frame_count - 1 ? 0 : msg_t::more)) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY);
errno = EPROTO;
return close_and_return (msg, -1);
}
}
// Address delimiter frame
if (msg[0].size () > 0) {
// TODO can a ZAP handler produce such a message at all?
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_UNSPECIFIED);
errno = EPROTO;
return close_and_return (msg, -1);
}
// Version frame
if (msg[1].size () != zap_version_len
|| memcmp (msg[1].data (), zap_version, zap_version_len)) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION);
errno = EPROTO;
return close_and_return (msg, -1);
}
// Request id frame
if (msg[2].size () != id_len || memcmp (msg[2].data (), id, id_len)) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID);
errno = EPROTO;
return close_and_return (msg, -1);
}
// Status code frame, only 200, 300, 400 and 500 are valid status codes
const char *status_code_data = static_cast<const char *> (msg[3].data ());
if (msg[3].size () != 3 || status_code_data[0] < '2'
|| status_code_data[0] > '5' || status_code_data[1] != '0'
|| status_code_data[2] != '0') {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_INVALID_STATUS_CODE);
errno = EPROTO;
return close_and_return (msg, -1);
}
// Save status code
status_code.assign (static_cast<char *> (msg[3].data ()), 3);
// Save user id
set_user_id (msg[5].data (), msg[5].size ());
// Process metadata frame
rc = parse_metadata (static_cast<const unsigned char *> (msg[6].data ()),
msg[6].size (), true);
if (rc != 0) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_INVALID_METADATA);
errno = EPROTO;
return close_and_return (msg, -1);
}
// Close all reply frames
for (size_t i = 0; i < zap_reply_frame_count; i++) {
const int rc2 = msg[i].close ();
errno_assert (rc2 == 0);
}
handle_zap_status_code ();
return 0;
}
void zap_client_t::handle_zap_status_code ()
{
// we can assume here that status_code is a valid ZAP status code,
// i.e. 200, 300, 400 or 500
int status_code_numeric = 0;
switch (status_code[0]) {
case '2':
return;
case '3':
status_code_numeric = 300;
break;
case '4':
status_code_numeric = 400;
break;
case '5':
status_code_numeric = 500;
break;
}
session->get_socket ()->event_handshake_failed_auth (
session->get_endpoint (), status_code_numeric);
}
zap_client_common_handshake_t::zap_client_common_handshake_t (
session_base_t *const session_,
const std::string &peer_address_,
const options_t &options_,
state_t zap_reply_ok_state_) :
mechanism_base_t (session_, options_),
zap_client_t (session_, peer_address_, options_),
state (waiting_for_hello),
_zap_reply_ok_state (zap_reply_ok_state_)
{
}
zmq::mechanism_t::status_t zap_client_common_handshake_t::status () const
{
if (state == ready)
return mechanism_t::ready;
if (state == error_sent)
return mechanism_t::error;
return mechanism_t::handshaking;
}
int zap_client_common_handshake_t::zap_msg_available ()
{
zmq_assert (state == waiting_for_zap_reply);
return receive_and_process_zap_reply () == -1 ? -1 : 0;
}
void zap_client_common_handshake_t::handle_zap_status_code ()
{
zap_client_t::handle_zap_status_code ();
// we can assume here that status_code is a valid ZAP status code,
// i.e. 200, 300, 400 or 500
switch (status_code[0]) {
case '2':
state = _zap_reply_ok_state;
break;
case '3':
// a 300 error code (temporary failure)
// should NOT result in an ERROR message, but instead the
// client should be silently disconnected (see CURVEZMQ RFC)
// therefore, go immediately to state error_sent
state = error_sent;
break;
default:
state = sending_error;
}
}
int zap_client_common_handshake_t::receive_and_process_zap_reply ()
{
zmq_assert (state == waiting_for_zap_reply);
return zap_client_t::receive_and_process_zap_reply ();
}
}