-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcmc.c
1074 lines (957 loc) · 31.8 KB
/
mcmc.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
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
// TODO: move these structs into mcmc.h, but only expose them if
// MCMC_EXPOSE_INTERNALS is defined... for tests and this thing.
#include "mcmc.h"
// TODO: if there's a parse error or unknown status code, we likely have a
// protocol desync and need to disconnect.
// A "reasonable" minimum buffer size to work with.
// Callers are allowed to create a buffer of any size larger than this.
// TODO: Put the math/documentation in here.
// This is essentially the largest return value status line possible.
// at least doubled for wiggle room.
#define MIN_BUFFER_SIZE 2048
#define FLAG_BUF_WANTED_READ 0x4
#define STATE_DEFAULT 0 // looking for any kind of response
#define STATE_GET_RESP 1 // processing VALUE's until END
#define STATE_STAT_RESP 2 // processing STAT's until END
#define STATE_STAT_RESP_DONE 3
typedef struct mcmc_ctx {
int fd;
int gai_status; // getaddrinfo() last status.
int sent_bytes_partial; // note for partially sent buffers.
int error; // latest error code.
uint32_t status_flags; // internal only flags.
// FIXME: s/buffer_used/buffer_filled/ ?
size_t buffer_used; // amount of bytes read into the buffer so far.
char *buffer_head; // buffer pointer currently in use.
} mcmc_ctx_t;
// INTERNAL FUNCTIONS
#define TOKENIZER_MAXLEN USHRT_MAX
// Find the starting offsets of each token; ignoring length.
// This creates a fast small (<= cacheline) index into the request,
// where we later scan or directly feed data into API's.
#define _mcmc_tokenize(t, line, len, mstart, max) _mcmc_tokenize_meta(t, line, len, UINT8_MAX, max)
// specialized tokenizer for meta protocol commands or responses, fills in the
// bitflags while scanning.
// Function _assumes_ const char *line ends with \n or \r\n, but will not
// break so long as the passed in 'len' is reasonable.
MCMC_STATIC int _mcmc_tokenize_meta(mcmc_tokenizer_t *t, const char *line, size_t len, const int mstart, const int max) {
const char *s = line;
t->metaflags = 0;
// since multigets can be huge, we can't purely judge reqlen against this
// limit, but we also can't index past it since the tokens are shorts.
if (len > TOKENIZER_MAXLEN) {
len = TOKENIZER_MAXLEN;
}
if (line[len-2] == '\r') {
len -= 2;
} else {
len -= 1;
}
const char *end = s + len;
int curtoken = 0;
int state = 0;
while (s != end) {
switch (state) {
case 0:
// scanning for first non-space to find a token.
if (*s != ' ') {
if (curtoken >= mstart) {
if (*s > 64 && *s < 123) {
t->metaflags |= (uint64_t)1 << (*s - 65);
} else if (isdigit(*s) == 0) {
return MCMC_NOK;
}
}
t->tokens[curtoken] = s - line;
if (++curtoken == max) {
s++;
state = 2;
break;
}
state = 1;
}
s++;
break;
case 1:
// advance over a token
if (*s != ' ') {
s++;
} else {
state = 0;
}
break;
case 2:
// hit max tokens before end of the line.
// keep advancing so we can place endcap token.
if (*s == ' ') {
goto endloop;
}
s++;
break;
}
}
endloop:
// endcap token so we can quickly find the length of any token by looking
// at the next one.
t->tokens[curtoken] = s - line;
t->ntokens = curtoken;
t->mstart = mstart;
return MCMC_OK;
}
__attribute__((unused)) MCMC_STATIC int _mcmc_token_len(const char *line, mcmc_tokenizer_t *t, size_t token) {
const char *s = line + t->tokens[token];
const char *e = line + t->tokens[token+1];
// start of next token is after any space delimiters, so back those out.
while (*(e-1) == ' ') {
e--;
}
return e - s;
}
__attribute__((unused)) MCMC_STATIC const char *_mcmc_token(const char *line, mcmc_tokenizer_t *t, size_t token, int *len) {
const char *s = line + t->tokens[token];
if (len != NULL) {
const char *e = line + t->tokens[token+1];
while (*(e-1) == ' ') {
e--;
}
*len = e - s;
}
return s;
}
static int _mcmc_parse_value_line(const char *buf, size_t read, mcmc_resp_t *r) {
// we know that "VALUE " has matched, so skip that.
const char *p = buf+6;
size_t l = r->reslen;
// <key> <flags> <bytes> [<cas unique>]
const char *key = p;
int keylen;
p = memchr(p, ' ', l - 6);
if (p == NULL) {
return -MCMC_ERR_VALUE;
}
keylen = p - key;
// convert flags into something useful.
// FIXME: do we need to prevent overruns in strtoul?
// we know for sure the line will eventually end in a \n.
char *n = NULL;
errno = 0;
uint32_t flags = strtoul(p, &n, 10);
if ((errno == ERANGE) || (p == n) || (*n != ' ')) {
return -MCMC_ERR_VALUE;
}
p = n;
errno = 0;
uint32_t bytes = strtoul(p, &n, 10);
if ((errno == ERANGE) || (p == n)) {
return -MCMC_ERR_VALUE;
}
p = n;
// If next byte is a space, we read the optional CAS value.
uint64_t cas = 0;
if (*n == ' ') {
errno = 0;
cas = strtoull(p, &n, 10);
if ((errno == ERANGE) || (p == n)) {
return -MCMC_ERR_VALUE;
}
}
// If we made it this far, we've parsed everything, stuff the details into
// the context for fetching later.
r->vlen = bytes + 2; // add in the \r\n
int buffer_remain = read - r->reslen;
if (buffer_remain >= r->vlen) {
r->vlen_read = r->vlen;
} else {
r->vlen_read = buffer_remain;
}
r->key = key;
r->klen = keylen;
r->flags = flags;
r->cas = cas;
r->type = MCMC_RESP_GET;
// NOTE: if value_offset < read, has part of the value in the
// buffer already.
return MCMC_CODE_OK;
}
static int _mcmc_parse_stat_line(const char *buf, mcmc_resp_t *r) {
const char *p = buf+5; // pass "STAT "
size_t l = r->reslen;
// STAT key value
const char *sname = p;
p = memchr(p, ' ', l-5);
if (p == NULL) {
return -MCMC_ERR_VALUE;
}
int snamelen = p - sname;
while (*p == ' ') {
p++;
}
const char *stat = p;
int statlen = l - (p - buf) - 2;
r->sname = sname;
r->snamelen = snamelen;
r->stat = stat;
r->statlen = statlen;
return MCMC_CODE_OK;
}
// FIXME: This is broken for ASCII multiget.
// if we get VALUE back, we need to stay in ASCII GET read mode until an END
// is seen.
static int _mcmc_parse_response(const char *buf, size_t read, mcmc_resp_t *r) {
const char *cur = buf;
int rlen; // response code length.
int more = 0;
r->type = MCMC_RESP_FAIL;
// walk until the \r\n
// we can't enter this function without there being a '\n' in the buffer.
while (*cur != '\r' && *cur != '\n') {
if (*cur == ' ') {
more = 1;
break;
}
cur++;
}
rlen = cur - buf;
// incr/decr returns a number with no code :(
// not checking length first since buf must have at least one char to
// enter this function.
if (buf[0] >= '0' && buf[0] <= '9') {
// TODO: parse it as a number on request.
// TODO: validate whole thing as digits here?
r->type = MCMC_RESP_NUMERIC;
r->code = MCMC_CODE_OK;
return MCMC_OK;
}
if (rlen < 2) {
r->code = MCMC_ERR_SHORT;
return MCMC_ERR;
}
int code = MCMC_ERR;
switch (rlen) {
case 2:
// meta, "OK"
// FIXME: adding new return codes would make the client completely
// fail. The rest of the client is agnostic to requests/flags for
// meta.
// can we make it agnostic for return codes outside of "read this
// data" types?
// As-is it should fail down to the "send the return code to the
// user". not sure that's right.
r->type = MCMC_RESP_META;
switch (buf[0]) {
case 'E':
if (buf[1] == 'N') {
code = MCMC_CODE_END;
} else if (buf[1] == 'X') {
code = MCMC_CODE_EXISTS;
}
break;
case 'H':
if (buf[1] == 'D') {
// typical meta response.
code = MCMC_CODE_OK;
}
break;
case 'M':
if (buf[1] == 'N') {
// specific return code so user can see pipeline end.
code = MCMC_CODE_NOP;
} else if (buf[1] == 'E') {
// ME is the debug output line.
// TODO: this just gets returned as an rline?
// specific code? specific type?
// ME <key> <key=value debug line>
code = MCMC_CODE_OK;
}
break;
case 'N':
if (buf[1] == 'F') {
code = MCMC_CODE_NOT_FOUND;
} else if (buf[1] == 'S') {
code = MCMC_CODE_NOT_STORED;
}
break;
case 'O':
if (buf[1] == 'K') {
// Used by many random management commands
r->type = MCMC_RESP_GENERIC;
code = MCMC_CODE_OK;
}
break;
case 'V':
if (buf[1] == 'A') {
// VA <size> <flags>*\r\n
if (more) {
errno = 0;
char *n = NULL;
uint32_t vsize = strtoul(cur, &n, 10);
if ((errno == ERANGE) || (cur == n)) {
r->type = MCMC_RESP_FAIL;
code = -MCMC_ERR_PARSE;
} else {
r->vlen = vsize + 2; // tag in the \r\n.
// FIXME: macro.
int buffer_remain = read - r->reslen;
if (buffer_remain >= r->vlen) {
r->vlen_read = r->vlen;
} else {
r->vlen_read = buffer_remain;
}
cur = n;
if (*cur != ' ') {
more = 0;
}
code = MCMC_CODE_OK;
}
} else {
r->type = MCMC_RESP_FAIL;
code = -MCMC_ERR_PARSE;
}
}
break;
}
// maybe: if !rv and !fail, do something special?
// if (more), there are flags. shove them in the right place.
if (more) {
// walk until not space
while (*cur == ' ') {
cur++;
}
r->rline = cur;
r->rlen = r->reslen - (cur - buf);
// cut \n or \r\n
if (buf[r->reslen-2] == '\r') {
r->rlen -= 2;
} else {
r->rlen--;
}
} else {
r->rline = NULL;
r->rlen = 0;
}
break;
case 3:
if (memcmp(buf, "END", 3) == 0) {
// Either end of STAT results, or end of ascii GET key list.
code = MCMC_CODE_END;
r->type = MCMC_RESP_END;
}
break;
case 4:
if (memcmp(buf, "STAT", 4) == 0) {
r->type = MCMC_RESP_STAT;
code = _mcmc_parse_stat_line(buf, r);
}
break;
case 5:
if (memcmp(buf, "VALUE", 5) == 0) {
if (more) {
// <key> <flags> <bytes> [<cas unique>]
code = _mcmc_parse_value_line(buf, read, r);
} else {
code = -MCMC_ERR_PARSE;
}
} else if (memcmp(buf, "ERROR", 5) == 0) {
r->type = MCMC_RESP_ERRMSG;
code = -MCMC_CODE_ERROR;
}
break;
case 6:
if (memcmp(buf, "STORED", 6) == 0) {
r->type = MCMC_RESP_GENERIC;
code = MCMC_CODE_STORED;
} else if (memcmp(buf, "EXISTS", 6) == 0) {
r->type = MCMC_RESP_GENERIC;
code = MCMC_CODE_EXISTS;
// TODO: type -> ASCII?
}
break;
case 7:
if (memcmp(buf, "DELETED", 7) == 0) {
r->type = MCMC_RESP_GENERIC;
code = MCMC_CODE_DELETED;
} else if (memcmp(buf, "TOUCHED", 7) == 0) {
r->type = MCMC_RESP_GENERIC;
code = MCMC_CODE_TOUCHED;
} else if (memcmp(buf, "VERSION", 7) == 0) {
code = MCMC_CODE_VERSION;
r->type = MCMC_RESP_VERSION;
// TODO: prep the version line for return
}
break;
case 9:
if (memcmp(buf, "NOT_FOUND", 9) == 0) {
r->type = MCMC_RESP_GENERIC;
code = MCMC_CODE_NOT_FOUND;
}
break;
case 10:
if (memcmp(buf, "NOT_STORED", 10) == 0) {
r->type = MCMC_RESP_GENERIC;
code = MCMC_CODE_NOT_STORED;
}
break;
default:
// Unknown code, assume error.
if (memcmp(buf, "SERVER_ERROR", 12) == 0) {
r->type = MCMC_RESP_ERRMSG;
code = -MCMC_CODE_SERVER_ERROR;
} else if (memcmp(buf, "CLIENT_ERROR", 12) == 0) {
r->type = MCMC_RESP_ERRMSG;
code = -MCMC_CODE_CLIENT_ERROR;
}
break;
}
if (code < MCMC_OK) {
r->code = -code;
return MCMC_ERR;
} else {
r->code = code;
return MCMC_OK;
}
}
// TODO: possible to codegen the 32 vs 64 variants
// easy with a macro I just hate how that looks
#define MCMC_TOKTO32_MAX 11
#define MCMC_TOKTO64_MAX 22
// TODO: test if __builtin_add_overflow exists and use that instead for
// hardware boost.
// just need to split the mul and the add? if (__builtin_mul_overflow(etc))
// - need a method to force compile both functions for the test suite.
MCMC_STATIC int mcmc_toktou32(const char *t, size_t len, uint32_t *out) {
uint32_t sum = 0;
const char *pos = t;
// We clamp the possible length to make input length errors less likely to
// go for a walk through memory.
if (len > MCMC_TOKTO32_MAX) {
return MCMC_TOKTO_ELONG;
}
while (len--) {
char num = pos[0] - '0';
if (num > -1 && num < 10) {
uint32_t lim = (UINT32_MAX - num) / 10;
if (sum > lim) {
return MCMC_TOKTO_ERANGE;
}
sum = sum * 10 + num;
} else {
return MCMC_TOKTO_EINVALID;
}
pos++;
}
*out = sum;
return MCMC_OK;
}
MCMC_STATIC int mcmc_toktou64(const char *t, size_t len, uint64_t *out) {
uint64_t sum = 0;
const char *pos = t;
if (len > MCMC_TOKTO64_MAX) {
return MCMC_TOKTO_ELONG;
}
while (len--) {
char num = pos[0] - '0';
if (num > -1 && num < 10) {
uint64_t lim = (UINT64_MAX - num) / 10;
if (sum > lim) {
return MCMC_TOKTO_ERANGE;
}
sum = sum * 10 + num;
} else {
return MCMC_TOKTO_EINVALID;
}
pos++;
}
*out = sum;
return MCMC_OK;
}
// TODO: these funcs aren't defending against len == 0
// note that the command strings _must_ end in a \n so it _should_ be
// impossible to land after the buffer.
// However this should be adjusted next time I work on it:
// - instead of len, calculate end.
// - only do '-' check if pos != end
// - check pos against end in the while loop and just incr pos
MCMC_STATIC int mcmc_tokto32(const char *t, size_t len, int32_t *out) {
int32_t sum = 0;
const char *pos = t;
int is_sig = 0;
if (len > MCMC_TOKTO32_MAX) {
return MCMC_TOKTO_ELONG;
}
// If we're negative the first character must be -
if (pos[0] == '-') {
len--;
pos++;
is_sig = 1;
}
while (len--) {
char num = pos[0] - '0';
if (num > -1 && num < 10) {
if (is_sig) {
int32_t lim = (INT32_MIN + num) / 10;
if (sum < lim) {
return MCMC_TOKTO_ERANGE;
}
sum = sum * 10 - num;
} else {
int32_t lim = (INT32_MAX - num) / 10;
if (sum > lim) {
return MCMC_TOKTO_ERANGE;
}
sum = sum * 10 + num;
}
} else {
return MCMC_TOKTO_EINVALID;
}
pos++;
}
*out = sum;
return MCMC_OK;
}
MCMC_STATIC int mcmc_tokto64(const char *t, size_t len, int64_t *out) {
int64_t sum = 0;
const char *pos = t;
int is_sig = 0;
if (len > MCMC_TOKTO64_MAX) {
return MCMC_TOKTO_ELONG;
}
// If we're negative the first character must be -
if (pos[0] == '-') {
len--;
pos++;
is_sig = 1;
}
while (len--) {
char num = pos[0] - '0';
if (num > -1 && num < 10) {
if (is_sig) {
int64_t lim = (INT64_MIN + num) / 10;
if (sum < lim) {
return MCMC_TOKTO_ERANGE;
}
sum = sum * 10 - num;
} else {
int64_t lim = (INT64_MAX - num) / 10;
if (sum > lim) {
return MCMC_TOKTO_ERANGE;
}
sum = sum * 10 + num;
}
} else {
return MCMC_TOKTO_EINVALID;
}
pos++;
}
*out = sum;
return MCMC_OK;
}
// END INTERNAL FUNCTIONS
// Context-less bare API.
//MCMC_STATIC int _mcmc_tokenize_meta(mcmc_tokenizer_t *t, const char *line, size_t len, const int mstart, const int max) {
// TODO: ideally this does a macro test of t->ntokens before calling a
// function. are compilers smart enough that I don't have to do this tho?
int mcmc_tokenize_res(const char *l, size_t len, mcmc_tokenizer_t *t) {
if (t->ntokens == 0) {
return _mcmc_tokenize_meta(t, l, len, 1, MCMC_PARSER_MAX_TOKENS-1);
}
return 0;
}
const char *mcmc_token_get(const char *l, mcmc_tokenizer_t *t, int idx, int *len) {
if (idx > 0 && idx < t->ntokens) {
return _mcmc_token(l, t, idx, len);
} else {
return NULL;
}
}
#define X(n, p, c) \
int n(const char *l, mcmc_tokenizer_t *t, int idx, p *val) { \
if (idx > 0 && idx < t->ntokens) { \
int tlen = 0; \
const char *tok = _mcmc_token(l, t, idx, &tlen); \
return c(tok, tlen, val); \
} else { \
return MCMC_ERR; \
} \
}
X(mcmc_token_get_u32, uint32_t, mcmc_toktou32)
X(mcmc_token_get_u64, uint64_t, mcmc_toktou64)
X(mcmc_token_get_32, int32_t, mcmc_tokto32)
X(mcmc_token_get_64, int64_t, mcmc_tokto64)
#undef X
int mcmc_token_has_flag(const char *l, mcmc_tokenizer_t *t, char flag) {
if (flag < 65 || flag > 122) {
return MCMC_ERR;
}
uint64_t flagbit = (uint64_t)1 << (flag-65);
if (t->metaflags & flagbit) {
return MCMC_OK;
} else {
return MCMC_NOK;
}
}
// User can optionally call has_flag or has_flag_bit before calling these
// functions. Is optional because it can be either wasteful to check if the
// flag will always be there, or it's useful to the caller to know the flag
// exists but there wasn't a token attached or we failed to parse it.
const char *mcmc_token_get_flag(const char *l, mcmc_tokenizer_t *t, char flag, int *len) {
for (int x = t->mstart; x < t->ntokens; x++) {
const char *tflag = l + t->tokens[x];
if (tflag[0] == flag) {
int tlen = _mcmc_token_len(l, t, x) - 1; // subtract the flag.
// ensure the flag actually has a token.
if (tlen > 0) {
if (len) {
*len = tlen;
}
return tflag+1;
} else {
break;
}
}
}
return NULL;
}
int mcmc_token_get_flag_u32(const char *l, mcmc_tokenizer_t *t, char flag, uint32_t *val) {
int tlen = 0;
const char *tok = mcmc_token_get_flag(l, t, flag, &tlen);
if (tok) {
return mcmc_toktou32(tok, tlen, val);
}
return MCMC_NOK;
}
int mcmc_token_get_flag_u64(const char *l, mcmc_tokenizer_t *t, char flag, uint64_t *val) {
int tlen = 0;
const char *tok = mcmc_token_get_flag(l, t, flag, &tlen);
if (tok) {
return mcmc_toktou64(tok, tlen, val);
}
return MCMC_NOK;
}
int mcmc_token_get_flag_32(const char *l, mcmc_tokenizer_t *t, char flag, int32_t *val) {
int tlen = 0;
const char *tok = mcmc_token_get_flag(l, t, flag, &tlen);
if (tok) {
return mcmc_tokto32(tok, tlen, val);
}
return MCMC_NOK;
}
int mcmc_token_get_flag_64(const char *l, mcmc_tokenizer_t *t, char flag, int64_t *val) {
int tlen = 0;
const char *tok = mcmc_token_get_flag(l, t, flag, &tlen);
if (tok) {
return mcmc_tokto64(tok, tlen, val);
}
return MCMC_NOK;
}
int mcmc_token_get_flag_idx(const char *l, mcmc_tokenizer_t *t, char flag) {
for (int x = t->mstart; x < t->ntokens; x++) {
const char *tflag = l + t->tokens[x];
if (tflag[0] == flag) {
return x;
}
}
return -1;
}
// Directly parse a buffer with read data of size len.
// r->reslen + r->vlen_read is the bytes consumed from the buffer read.
// Caller manages how to retry if MCMC_WANT_READ or an error happens.
int mcmc_parse_buf(const char *buf, size_t read, mcmc_resp_t *r) {
char *el;
memset(r, 0, sizeof(*r));
el = memchr(buf, '\n', read);
if (el == NULL) {
r->code = MCMC_WANT_READ;
return MCMC_ERR;
}
// Consume through the newline, note where the value would start if exists
r->value = el+1;
r->reslen = r->value - buf;
// FIXME: the server must be stricter in what it sends back. should always
// have a \r. check for it and fail?
return _mcmc_parse_response(buf, read, r);
}
// Context-ful API.
int mcmc_fd(void *c) {
mcmc_ctx_t *ctx = (mcmc_ctx_t *)c;
return ctx->fd;
}
size_t mcmc_size(int options) {
return sizeof(mcmc_ctx_t);
}
// Allow returning this dynamically based on options set.
// FIXME: it might be more flexible to call this after mcmc_connect()...
// but this is probably more convenient for the caller if it's less dynamic.
size_t mcmc_min_buffer_size(int options) {
return MIN_BUFFER_SIZE;
}
/*** Functions wrapping syscalls **/
// TODO: should be able to flip between block and nonblock.
// used for checking on async connections.
int mcmc_check_nonblock_connect(void *c, int *err) {
mcmc_ctx_t *ctx = (mcmc_ctx_t *)c;
socklen_t errsize = sizeof(*err);
if (getsockopt(ctx->fd, SOL_SOCKET, SO_ERROR, err, &errsize) == 0) {
if (*err == 0) {
return MCMC_OK;
}
} else {
// getsockopt failed. still need to pass up the error.
*err = errno;
}
return MCMC_ERR;
}
// TODO:
// - option for connecting 4 -> 6 or 6 -> 4
// connect_unix()
// connect_bind_tcp()
// ^ fill an internal struct from the stack and call into this central
// connect?
int mcmc_connect(void *c, char *host, char *port, int options) {
mcmc_ctx_t *ctx = (mcmc_ctx_t *)c;
int s;
int sock;
int res = MCMC_CONNECTED;
struct addrinfo hints;
struct addrinfo *ai = NULL;
struct addrinfo *next = NULL;
// Since our cx memory was likely malloc'ed, ensure we start clear.
memset(ctx, 0, sizeof(mcmc_ctx_t));
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
ctx->fd = 0;
s = getaddrinfo(host, port, &hints, &ai);
if (s != 0) {
hints.ai_family = AF_INET6;
s = getaddrinfo(host, port, &hints, &ai);
if (s != 0) {
// TODO: gai_strerror(s)
ctx->gai_status = s;
res = MCMC_ERR;
goto end;
}
}
for (next = ai; next != NULL; next = next->ai_next) {
sock = socket(next->ai_family, next->ai_socktype,
next->ai_protocol);
if (sock == -1)
continue;
if (options & MCMC_OPTION_TCP_KEEPALIVE) {
int optval = 1;
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) < 0) {
res = MCMC_ERR;
close(sock);
goto end;
}
}
if (options & MCMC_OPTION_NONBLOCK) {
int flags = fcntl(sock, F_GETFL);
if (flags < 0) {
res = MCMC_ERR;
close(sock);
goto end;
}
if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
res = MCMC_ERR;
close(sock);
goto end;
}
res = MCMC_CONNECTING;
if (connect(sock, next->ai_addr, next->ai_addrlen) != -1) {
if (errno == EINPROGRESS) {
break; // We're good, stop the loop.
}
}
break;
} else {
// TODO: BIND local port.
if (connect(sock, next->ai_addr, next->ai_addrlen) != -1)
break;
}
close(sock);
}
// TODO: cache last connect status code?
if (next == NULL) {
res = MCMC_ERR;
goto end;
}
ctx->fd = sock;
end:
if (ai) {
freeaddrinfo(ai);
}
return res;
}
// NOTE: if WANT_WRITE returned, call with same arguments.
// FIXME: len -> size_t?
// TODO: rename to mcmc_request_send
int mcmc_send_request(void *c, const char *request, int len, int count) {
mcmc_ctx_t *ctx = (mcmc_ctx_t *)c;
// adjust our send buffer by how much has already been sent.
const char *r = request + ctx->sent_bytes_partial;
int l = len - ctx->sent_bytes_partial;
int sent = send(ctx->fd, r, l, 0);
if (sent == -1) {
// implicitly handle nonblock mode.
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return MCMC_WANT_WRITE;
} else {
return MCMC_ERR;
}
}
if (sent < len) {
// can happen anytime, but mostly in nonblocking mode.
ctx->sent_bytes_partial += sent;
return MCMC_WANT_WRITE;
} else {
ctx->sent_bytes_partial = 0;
}
return MCMC_OK;
}
// TODO: pretty sure I don't want this function chewing on a submitted iov
// stack, but it might make for less client code :(
// so for now, lets not.
int mcmc_request_writev(void *c, const struct iovec *iov, int iovcnt, ssize_t *sent, int count) {
mcmc_ctx_t *ctx = (mcmc_ctx_t *)c;
// need to track sent vs tosend to know when to update counters.
ssize_t tosend = 0;
for (int i = 0; i < iovcnt; i++) {
tosend += iov[i].iov_len;
}
*sent = writev(ctx->fd, iov, iovcnt);
if (*sent == -1) {
// implicitly handle nonblock mode.
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return MCMC_WANT_WRITE;
} else {
return MCMC_ERR;
}
}
if (*sent < tosend) {
// can happen anytime, but mostly in nonblocking mode.
return MCMC_WANT_WRITE;
}
return MCMC_OK;
}
// TODO: return consumed bytes and end ptr?
// FIXME: mcmc no longer tracks the buffer inbetween commands, which was
// causing issues with the API and bugs.
// This function wraps the recv call, so it needs to understand the buffer a
// little bit. Since memcached doesn't currently use this function I'm
// commenting it out with this note so it can be rewritten in terms of an
// external buffer later.
/*
int mcmc_read(void *c, char *buf, size_t bufsize, mcmc_resp_t *r) {
mcmc_ctx_t *ctx = (mcmc_ctx_t *)c;
char *el;
memset(r, 0, sizeof(*r));
// If there's still data in the buffer try to use it before potentially
// hanging on the network read.
// Also skip this check if we specifically wanted more bytes from net.
if (ctx->buffer_used && !(ctx->status_flags & FLAG_BUF_WANTED_READ)) {
el = memchr(buf, '\n', ctx->buffer_used);
if (el) {
goto parse;
}
}
// adjust buffer by how far we've already consumed.
char *b = buf + ctx->buffer_used;
size_t l = bufsize - ctx->buffer_used;
int read = recv(ctx->fd, b, l, 0);
if (read == 0) {
return MCMC_NOT_CONNECTED;
} else if (read == -1) {
// implicitly handle nonblocking configurations.
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return MCMC_WANT_READ;
} else {
return MCMC_ERR;
}
}
ctx->buffer_used += read;
// Always scan from the start of the original buffer.
el = memchr(buf, '\n', ctx->buffer_used);
if (!el) {
// FIXME: error if buffer is full but no \n is found.
ctx->status_flags |= FLAG_BUF_WANTED_READ;
return MCMC_WANT_READ;
}
parse:
// Consume through the newline.
r->value = el+1;
// FIXME: the server must be stricter in what it sends back. should always