-
Notifications
You must be signed in to change notification settings - Fork 20
/
proto.c
596 lines (509 loc) · 13 KB
/
proto.c
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
/*
* See LICENSE file for copyright and license details.
*
* Copyright (c) 2013 Joyent, Inc.
* Copyright (c) 2024 MNX Cloud, Inc.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "base64.h"
#include "common.h"
#include "crc32.h"
#include "dynstr.h"
#include "plat.h"
#include "proto.h"
#include "reqid.h"
/*
* Receive timeout used prior to V2 negotiation:
*/
#define RECV_TIMEOUT_MS 6000
/*
* Receive timeout once we've successfully negotiated the V2 protocol with the
* host. Some V2 operations, like PUT, can take longer than 6 seconds to
* complete.
*/
#define RECV_TIMEOUT_MS_V2 45000
typedef enum mdata_proto_state {
MDPS_MESSAGE_HEADER = 1,
MDPS_MESSAGE_DATA,
MDPS_MESSAGE_V2,
MDPS_READY,
MDPS_ERROR
} mdata_proto_state_t;
typedef enum mdata_proto_version {
MDPV_VERSION_1 = 1,
MDPV_VERSION_2 = 2
} mdata_proto_version_t;
typedef struct mdata_command {
char mdc_reqid[REQID_LEN];
string_t *mdc_request;
string_t *mdc_response_data;
mdata_response_t mdc_response;
int mdc_done;
} mdata_command_t;
struct mdata_proto {
mdata_plat_t *mdp_plat;
mdata_command_t *mdp_command;
mdata_proto_state_t mdp_state;
mdata_proto_version_t mdp_version;
boolean_t mdp_in_reset;
const char *mdp_errmsg;
const char *mdp_parse_errmsg;
};
static int proto_send(mdata_proto_t *mdp);
static int proto_recv(mdata_proto_t *mdp);
static int
proto_negotiate(mdata_proto_t *mdp)
{
mdata_command_t *mdcsave;
mdata_response_t mdr;
string_t *rdata = NULL;
int ret = -1;
mdcsave = mdp->mdp_command;
mdp->mdp_command = NULL;
/*
* Assume Protocol Version 1 until we negotiate up to Version 2.
*/
mdp->mdp_version = MDPV_VERSION_1;
if (proto_execute(mdp, "NEGOTIATE", "V2", &mdr, &rdata) == 0) {
if (mdr == MDR_V2_OK)
mdp->mdp_version = MDPV_VERSION_2;
ret = 0;
}
mdp->mdp_command = mdcsave;
if (rdata != NULL)
dynstr_free(rdata);
return (ret);
}
static int
proto_reset(mdata_proto_t *mdp)
{
int permfail = 0;
/*
* Prevent proto_execute() from calling back into proto_reset()
* while we're resetting:
*/
mdp->mdp_in_reset = B_TRUE;
retry:
mdp->mdp_errmsg = NULL;
/*
* Close our existing platform-specific code handle if we have
* one open:
*/
if (mdp->mdp_plat != NULL) {
plat_fini(mdp->mdp_plat);
mdp->mdp_plat = NULL;
}
mdp->mdp_state = MDPS_READY;
/*
* Initialise the platform-specific code:
*/
if (plat_init(&mdp->mdp_plat, &mdp->mdp_errmsg, &permfail) == -1) {
if (permfail) {
return (-1);
} else {
sleep(1);
goto retry;
}
}
/*
* Determine what protocol our host supports:
*/
if (proto_negotiate(mdp) == -1) {
sleep(1);
goto retry;
}
mdp->mdp_in_reset = B_FALSE;
mdp->mdp_errmsg = NULL;
return (0);
}
static int
proto_parse_v2(mdata_proto_t *mdp, string_t *input, string_t *request_id,
string_t *command, string_t *response_data)
{
const char *endp = dynstr_cstr(input);
char *endp2;
unsigned long clen, crc32;
mdp->mdp_parse_errmsg = NULL;
if (strstr(endp, "V2 ") != endp) {
mdp->mdp_parse_errmsg = "message did not start with V2";
return (-1);
}
endp += 3;
/*
* Read Content Length:
*/
if ((clen = strtoul(endp, &endp2, 10)) == 0) {
mdp->mdp_parse_errmsg = "invalid content length";
return (-1);
}
endp = endp2;
/*
* Skip whitespace:
*/
while (*endp == ' ')
endp++;
/*
* Read CRC32 checksum:
*/
if ((crc32 = strtoul(endp, &endp2, 16)) == 0) {
mdp->mdp_parse_errmsg = "invalid crc32 in frame";
return (-1);
}
endp = endp2;
/*
* Skip whitespace:
*/
while (*endp == ' ')
endp++;
/*
* Ensure Content Length and CRC32 values from header match
* reality:
*/
if (strlen(endp) != clen || crc32_calc(endp, clen) != crc32) {
mdp->mdp_parse_errmsg = "clen/crc32 mismatch";
return (-1);
}
/*
* Read Request ID:
*/
while (*endp != ' ' && *endp != '\0') {
dynstr_appendc(request_id, *endp++);
}
if (dynstr_len(request_id) == 0) {
mdp->mdp_parse_errmsg = "missing request id";
return (-1);
}
/*
* Skip whitespace:
*/
while (*endp == ' ')
endp++;
/*
* Read Command/Code:
*/
while (*endp != ' ' && *endp != '\0') {
dynstr_appendc(command, *endp++);
}
if (dynstr_len(command) == 0) {
mdp->mdp_parse_errmsg = "missing command/code";
return (-1);
}
/*
* Skip Whitespace:
*/
while (*endp == ' ')
endp++;
/*
* Read the Response Data:
*/
if (base64_decode(endp, strlen(endp), response_data) == -1) {
mdp->mdp_parse_errmsg = "base64 error";
return (-1);
}
return (0);
}
static void
process_input(mdata_proto_t *mdp, string_t *input)
{
const char *cstr = dynstr_cstr(input);
string_t *command, *request_id;
switch (mdp->mdp_state) {
case MDPS_MESSAGE_V2:
command = dynstr_new();
request_id = dynstr_new();
dynstr_reset(mdp->mdp_command->mdc_response_data);
if (proto_parse_v2(mdp, input, request_id, command,
mdp->mdp_command->mdc_response_data) == -1) {
/*
* XXX Presently, drop frames that we can't
* parse.
*/
} else if (strcmp(dynstr_cstr(request_id),
mdp->mdp_command->mdc_reqid) != 0) {
/*
* XXX Presently, drop frames that are not for
* the currently outstanding request.
*/
} else if (strcmp(dynstr_cstr(command), "NOTFOUND") == 0) {
mdp->mdp_state = MDPS_READY;
mdp->mdp_command->mdc_response = MDR_NOTFOUND;
mdp->mdp_command->mdc_done = 1;
} else if (strcmp(dynstr_cstr(command), "SUCCESS") == 0) {
mdp->mdp_state = MDPS_READY;
mdp->mdp_command->mdc_response = MDR_SUCCESS;
mdp->mdp_command->mdc_done = 1;
} else {
mdp->mdp_state = MDPS_READY;
mdp->mdp_command->mdc_response = MDR_UNKNOWN;
mdp->mdp_command->mdc_done = 1;
}
dynstr_free(command);
dynstr_free(request_id);
break;
case MDPS_MESSAGE_HEADER:
if (strcmp(cstr, "NOTFOUND") == 0) {
mdp->mdp_state = MDPS_READY;
mdp->mdp_command->mdc_response = MDR_NOTFOUND;
mdp->mdp_command->mdc_done = 1;
} else if (strcmp(cstr, "SUCCESS") == 0) {
mdp->mdp_state = MDPS_MESSAGE_DATA;
mdp->mdp_command->mdc_response = MDR_SUCCESS;
} else if (strcmp(cstr, "V2_OK") == 0) {
mdp->mdp_state = MDPS_READY;
mdp->mdp_command->mdc_response = MDR_V2_OK;
mdp->mdp_command->mdc_done = 1;
} else if (strcmp(cstr, "invalid command") == 0) {
mdp->mdp_state = MDPS_READY;
mdp->mdp_command->mdc_response = MDR_INVALID_COMMAND;
mdp->mdp_command->mdc_done = 1;
} else {
mdp->mdp_state = MDPS_READY;
dynstr_append(mdp->mdp_command->mdc_response_data, cstr);
mdp->mdp_command->mdc_response = MDR_UNKNOWN;
mdp->mdp_command->mdc_done = 1;
}
break;
case MDPS_MESSAGE_DATA:
if (strcmp(cstr, ".") == 0) {
mdp->mdp_state = MDPS_READY;
mdp->mdp_command->mdc_done = 1;
} else {
string_t *respdata = mdp->mdp_command->mdc_response_data;
int offs = cstr[0] == '.' ? 1 : 0;
if (dynstr_len(respdata) > 0)
dynstr_append(respdata, "\n");
dynstr_append(respdata, cstr + offs);
}
break;
case MDPS_READY:
case MDPS_ERROR:
break;
default:
ABORT("process_input: UNKNOWN STATE\n");
}
}
static int
proto_send(mdata_proto_t *mdp)
{
VERIFY(mdp->mdp_command);
if (plat_send(mdp->mdp_plat, mdp->mdp_command->mdc_request) == -1) {
mdp->mdp_state = MDPS_ERROR;
return (-1);
}
/*
* Wait for response header from remote peer:
*/
switch (mdp->mdp_version) {
case MDPV_VERSION_1:
mdp->mdp_state = MDPS_MESSAGE_HEADER;
break;
case MDPV_VERSION_2:
mdp->mdp_state = MDPS_MESSAGE_V2;
break;
default:
ABORT("unknown protocol version");
}
return (0);
}
static int
proto_recv(mdata_proto_t *mdp)
{
int ret = -1;
string_t *line = dynstr_new();
for (;;) {
int recv_timeout_ms = mdp->mdp_version == MDPV_VERSION_2 ?
RECV_TIMEOUT_MS_V2 : RECV_TIMEOUT_MS;
if (plat_recv(mdp->mdp_plat, line, recv_timeout_ms) == -1) {
mdp->mdp_state = MDPS_ERROR;
goto bail;
}
process_input(mdp, line);
dynstr_reset(line);
if (mdp->mdp_command->mdc_done)
break;
}
ret = 0;
bail:
dynstr_free(line);
return (ret);
}
/*
* Version 2 of the Metadata Protocol is, after a fashion, a framed
* protocol. Each 'frame' is really just a LF-terminated line of
* text. Request and Response payloads are BASE64-encoded to allow the
* transmission of embedded LFs, and other potentially binary data.
*
* The line format is as follows:
* ,- Body (CRC32/Length is of
* _____________________ <--/ this string)
* V2 21 265ae1d8 dc4fae17 SUCCESS W10=\n
* ^ ^ ^ ^ ^ ^ ^
* | | | | | | \--- Terminating Linefeed.
* | | | | | \------- BASE64-encoded payload.
* | | | | \--------------- Request Command or Response Code
* | | | \------------------------ Request ID (8 random hex digits)
* | | \--------------------------------- CRC32 of Body as 8 hex digits
* | \------------------------------------ Content Length of Body, base 10
* \-------------------------------------- So that this can be a V1 command
* as well, we start with "V2".
* This will be a FAILURE on a
* host that only supports the
* V1 protocol.
*/
static void
proto_make_request_v2(const char *command, const char *argument,
string_t *output, char *reqidbuf)
{
char strbuf[23 + 1 + 8 + 1]; /* strlen(UINT64_MAX) + ' ' + %08x + \0 */
string_t *body = dynstr_new();
/*
* Generate the BODY of the V2 message, which is the portion we
* use when generating the Content Length and CRC32 checksum for
* the message HEADER.
*/
dynstr_append(body, reqid(reqidbuf));
dynstr_append(body, " ");
dynstr_append(body, command);
if (argument != NULL) {
dynstr_append(body, " ");
base64_encode(argument, strlen(argument), body);
}
/*
* Generate the HEADER directly into the output, and then
* append the BODY:
*/
dynstr_append(output, "V2 ");
sprintf(strbuf, "%u %08x", (unsigned int) dynstr_len(body), crc32_calc(
dynstr_cstr(body), dynstr_len(body)));
dynstr_append(output, strbuf);
dynstr_append(output, " ");
dynstr_append(output, dynstr_cstr(body));
dynstr_append(output, "\n");
dynstr_free(body);
}
static void
proto_make_request_v1(const char *command, const char *argument,
string_t *output)
{
dynstr_append(output, command);
if (argument != NULL) {
dynstr_append(output, " ");
dynstr_append(output, argument);
}
dynstr_append(output, "\n");
}
int
proto_execute(mdata_proto_t *mdp, const char *command, const char *argument,
mdata_response_t *response, string_t **response_data)
{
mdata_command_t mdc;
/*
* Initialise new command structure:
*/
bzero(&mdc, sizeof (mdc));
mdc.mdc_request = dynstr_new();
mdc.mdc_response_data = dynstr_new();
mdc.mdc_response = MDR_PENDING;
mdc.mdc_done = 0;
VERIFY0(mdp->mdp_command);
mdp->mdp_command = &mdc;
retry:
/*
* (Re-)generate request string to send to remote peer:
*/
dynstr_reset(mdc.mdc_request);
switch (mdp->mdp_version) {
case MDPV_VERSION_1:
proto_make_request_v1(command, argument, mdc.mdc_request);
break;
case MDPV_VERSION_2:
proto_make_request_v2(command, argument, mdc.mdc_request,
mdc.mdc_reqid);
break;
default:
ABORT("unknown protocol version");
}
/*
* Attempt to send the request to the remote peer:
*/
if (mdp->mdp_state == MDPS_ERROR || proto_send(mdp) != 0 ||
proto_recv(mdp) != 0) {
/*
* Discard existing response data and reset the command
* state:
*/
dynstr_reset(mdp->mdp_command->mdc_response_data);
mdc.mdc_response = MDR_PENDING;
/*
* If the command we're trying to send is part of a
* protocol reset sequence, just fail immediately:
*/
if (mdp->mdp_in_reset)
goto bail;
/*
* We could not send the request, so reset the stream
* and try again:
*/
fprintf(stderr, "receive timeout, resetting "
"protocol...\n");
if (proto_reset(mdp) == -1) {
/*
* We could not do a reset, so abort the whole
* thing.
*/
fprintf(stderr, "ERROR: while resetting connection: "
"%s\n", mdp->mdp_errmsg);
goto bail;
} else {
/*
* We were able to reset OK, so keep trying.
*/
goto retry;
}
}
if (mdp->mdp_state != MDPS_READY)
ABORT("proto state not MDPS_READY\n");
/*
* We were able to send a command and receive a response.
* Examine the response and decide what to do:
*/
*response = mdc.mdc_response;
*response_data = mdc.mdc_response_data;
dynstr_free(mdc.mdc_request);
mdp->mdp_command = NULL;
return (0);
bail:
dynstr_free(mdc.mdc_request);
dynstr_free(mdc.mdc_response_data);
mdp->mdp_command = NULL;
return (-1);
}
int
proto_version(mdata_proto_t *mdp)
{
return ((int)mdp->mdp_version);
}
int
proto_init(mdata_proto_t **out, const char **errmsg)
{
mdata_proto_t *mdp;
reqid_init();
if ((mdp = calloc(1, sizeof (*mdp))) == NULL)
return (-1);
if (proto_reset(mdp) == -1) {
*errmsg = mdp->mdp_errmsg;
free(mdp);
return (-1);
}
*out = mdp;
return (0);
}