-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspoa.c
1316 lines (1145 loc) · 31.3 KB
/
spoa.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
/*
* A Random IP reputation service acting as a Stream Processing Offload Agent
*
* This is a very simple service that implement a "random" ip reputation
* service. It will return random scores for all checked IP addresses. It only
* shows you how to implement a ip reputation service or such kind of services
* using the SPOE.
*
* Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
* Copyright 2018 OZON / Thierry Fournier <thierry.fournier@ozon.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include "spoa.h"
#define DEFAULT_PORT 12345
#define NUM_WORKERS 5
#define SLEN(str) (sizeof(str)-1)
/* Frame Types sent by HAProxy and by agents */
enum spoe_frame_type {
/* Frames sent by HAProxy */
SPOE_FRM_T_HAPROXY_HELLO = 1,
SPOE_FRM_T_HAPROXY_DISCON,
SPOE_FRM_T_HAPROXY_NOTIFY,
/* Frames sent by the agents */
SPOE_FRM_T_AGENT_HELLO = 101,
SPOE_FRM_T_AGENT_DISCON,
SPOE_FRM_T_AGENT_ACK
};
/* Errors triggered by SPOE applet */
enum spoe_frame_error {
SPOE_FRM_ERR_NONE = 0,
SPOE_FRM_ERR_IO,
SPOE_FRM_ERR_TOUT,
SPOE_FRM_ERR_TOO_BIG,
SPOE_FRM_ERR_INVALID,
SPOE_FRM_ERR_NO_VSN,
SPOE_FRM_ERR_NO_FRAME_SIZE,
SPOE_FRM_ERR_NO_CAP,
SPOE_FRM_ERR_BAD_VSN,
SPOE_FRM_ERR_BAD_FRAME_SIZE,
SPOE_FRM_ERR_UNKNOWN = 99,
SPOE_FRM_ERRS,
};
/* All supported SPOE actions */
enum spoe_action_type {
SPOE_ACT_T_SET_VAR = 1,
SPOE_ACT_T_UNSET_VAR,
SPOE_ACT_TYPES,
};
/* Masks to get data type or flags value */
#define SPOE_DATA_T_MASK 0x0F
#define SPOE_DATA_FL_MASK 0xF0
/* Flags to set Boolean values */
#define SPOE_DATA_FL_FALSE 0x00
#define SPOE_DATA_FL_TRUE 0x10
static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
[SPOE_FRM_ERR_NONE] = "normal",
[SPOE_FRM_ERR_IO] = "I/O error",
[SPOE_FRM_ERR_TOUT] = "a timeout occurred",
[SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
[SPOE_FRM_ERR_INVALID] = "invalid frame received",
[SPOE_FRM_ERR_NO_VSN] = "version value not found",
[SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
[SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
[SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
[SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
[SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
};
bool debug = false;
pthread_key_t worker_id;
static struct ps *ps_list = NULL;
static struct ps_message *ps_messages = NULL;
static int nfiles = 0;
static char **files = NULL;
static inline void add_file(const char *file)
{
nfiles++;
files = realloc(files, sizeof(*files) * nfiles);
if (files == NULL) {
fprintf(stderr, "Out of memory error\n");
exit(EXIT_FAILURE);
}
files[nfiles - 1] = strdup(file);
if (files[nfiles - 1] == NULL) {
fprintf(stderr, "Out of memory error\n");
exit(EXIT_FAILURE);
}
}
void ps_register(struct ps *ps)
{
ps->next = ps_list;
ps_list = ps;
}
void ps_register_message(struct ps *ps, const char *name, void *ref)
{
struct ps_message *msg;
/* Look for already registered name */
for (msg = ps_messages; msg; msg = msg->next) {
if (strcmp(name, msg->name) == 0) {
LOG("Message \"%s\" already registered\n", name);
exit(EXIT_FAILURE);
}
}
msg = calloc(1, sizeof(*msg));
if (msg == NULL) {
LOG("Out of memory error\n");
exit(EXIT_FAILURE);
}
msg->next = ps_messages;
ps_messages = msg;
msg->name = strdup(name);
if (msg->name == NULL) {
LOG("Out of memory error\n");
exit(EXIT_FAILURE);
}
msg->ref = ref;
msg->ps = ps;
}
static int
do_read(int sock, void *buf, int read_len)
{
fd_set readfds;
int n = 0, total = 0, bytesleft = read_len;
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
while (total < read_len) {
if (select(FD_SETSIZE, &readfds, NULL, NULL, NULL) == -1)
return -1;
if (!FD_ISSET(sock, &readfds))
return -1;
n = read(sock, buf + total, bytesleft);
if (n <= 0)
break;
total += n;
bytesleft -= n;
}
return (n == -1) ? -1 : total;
}
static int
do_write(int sock, void *buf, int write_len)
{
fd_set writefds;
int n = 0, total = 0, bytesleft = write_len;
FD_ZERO(&writefds);
FD_SET(sock, &writefds);
while (total < write_len) {
if (select(FD_SETSIZE, NULL, &writefds, NULL, NULL) == -1)
return -1;
if (!FD_ISSET(sock, &writefds))
return -1;
n = write(sock, buf + total, bytesleft);
if (n <= 0)
break;
total += n;
bytesleft -= n;
}
return (n == -1) ? -1 : total;
}
/* Receive a frame sent by HAProxy. It returns -1 if an error occurred,
* otherwise the number of read bytes.*/
static int
read_frame(int sock, struct worker *w)
{
uint32_t netint;
unsigned int framesz;
/* Read the frame size, on 4 bytes */
if (do_read(sock, &netint, sizeof(netint)) != 4) {
w->status_code = SPOE_FRM_ERR_IO;
return -1;
}
/* Check it against the max size */
framesz = ntohl(netint);
if (framesz > w->size) {
w->status_code = SPOE_FRM_ERR_TOO_BIG;
return -1;
}
/* Read the frame */
if (do_read(sock, w->buf, framesz) != framesz) {
w->status_code = SPOE_FRM_ERR_IO;
return -1;
}
w->len = framesz;
return framesz;
}
/* Send a frame to HAProxy. It returns -1 if an error occurred, otherwise the
* number of written bytes. */
static int
write_frame(int sock, struct worker *w)
{
uint32_t netint;
/* Write the frame size, on 4 bytes */
netint = htonl(w->len);
if (do_write(sock, &netint, sizeof(netint)) != 4) {
w->status_code = SPOE_FRM_ERR_IO;
return -1;
}
/* Write the frame */
if (do_write(sock, w->buf, w->len) != w->len) {
w->status_code = SPOE_FRM_ERR_IO;
return -1;
}
return w->len;
}
/* Encode a variable-length integer. This function never fails and returns the
* number of written bytes. */
static int
encode_spoe_varint(uint64_t i, char *buf)
{
int idx;
if (i < 240) {
buf[0] = (unsigned char)i;
return 1;
}
buf[0] = (unsigned char)i | 240;
i = (i - 240) >> 4;
for (idx = 1; i >= 128; ++idx) {
buf[idx] = (unsigned char)i | 128;
i = (i - 128) >> 7;
}
buf[idx++] = (unsigned char)i;
return idx;
}
/* Decode a varable-length integer. If the decoding fails, -1 is returned. This
* happens when the buffer's end in reached. On success, the number of read
* bytes is returned. */
static int
decode_spoe_varint(char *buf, char *end, uint64_t *i)
{
unsigned char *msg = (unsigned char *)buf;
int idx = 0;
if (msg > (unsigned char *)end)
return -1;
if (msg[0] < 240) {
*i = msg[0];
return 1;
}
*i = msg[0];
do {
++idx;
if (msg+idx > (unsigned char *)end)
return -1;
*i += (uint64_t)msg[idx] << (4 + 7 * (idx-1));
} while (msg[idx] >= 128);
return (idx + 1);
}
/* Encode a string. The string will be prefix by its length, encoded as a
* variable-length integer. This function never fails and returns the number of
* written bytes. */
static int
encode_spoe_string(const char *str, size_t len, char *dst)
{
int idx = 0;
if (!len) {
dst[0] = 0;
return 1;
}
idx += encode_spoe_varint(len, dst);
memcpy(dst+idx, str, len);
return (idx + len);
}
/* Decode a string. Its length is decoded first as a variable-length integer. If
* it succeeds, and if the string length is valid, the begin of the string is
* saved in <*str>, its length is saved in <*len> and the total numbre of bytes
* read is returned. If an error occurred, -1 is returned and <*str> remains
* NULL. */
static int
decode_spoe_string(char *buf, char *end, char **str, uint64_t *len)
{
int r, idx = 0;
*str = NULL;
*len = 0;
if ((r = decode_spoe_varint(buf, end, len)) == -1)
goto error;
idx += r;
if (buf + idx + *len > end)
goto error;
*str = buf+idx;
return (idx + *len);
error:
return -1;
}
/* Skip a typed data. If an error occurred, -1 is returned, otherwise the number
* of bytes read is returned. A types data is composed of a type (1 byte) and
* corresponding data:
* - boolean: non additional data (0 bytes)
* - integers: a variable-length integer (see decode_spoe_varint)
* - ipv4: 4 bytes
* - ipv6: 16 bytes
* - binary and string: a buffer prefixed by its size, a variable-length
* integer (see decode_spoe_string) */
static int
skip_spoe_data(char *frame, char *end)
{
uint64_t sz = 0;
int r, idx = 0;
if (frame > end)
return -1;
switch (frame[idx++] & SPOE_DATA_T_MASK) {
case SPOE_DATA_T_BOOL:
idx++;
break;
case SPOE_DATA_T_INT32:
case SPOE_DATA_T_INT64:
case SPOE_DATA_T_UINT32:
case SPOE_DATA_T_UINT64:
if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
return -1;
idx += r;
break;
case SPOE_DATA_T_IPV4:
idx += 4;
break;
case SPOE_DATA_T_IPV6:
idx += 16;
break;
case SPOE_DATA_T_STR:
case SPOE_DATA_T_BIN:
if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
return -1;
idx += r + sz;
break;
}
if (frame+idx > end)
return -1;
return idx;
}
/* Decode a typed data. If an error occurred, -1 is returned, otherwise the
* number of read bytes is returned. See skip_spoe_data for details. */
static int
decode_spoe_data(char *frame, char *end, struct spoe_data *data)
{
uint64_t sz = 0;
int type, r, idx = 0;
if (frame > end)
return -1;
type = frame[idx++];
data->type = (type & SPOE_DATA_T_MASK);
switch (data->type) {
case SPOE_DATA_T_BOOL:
data->u.boolean = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
break;
case SPOE_DATA_T_INT32:
if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
return -1;
data->u.sint32 = sz;
idx += r;
break;
case SPOE_DATA_T_INT64:
if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
return -1;
data->u.uint32 = sz;
idx += r;
break;
case SPOE_DATA_T_UINT32:
if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
return -1;
data->u.sint64 = sz;
idx += r;
break;
case SPOE_DATA_T_UINT64:
if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
return -1;
data->u.uint64 = sz;
idx += r;
break;
case SPOE_DATA_T_IPV4:
if (frame+idx+4 > end)
return -1;
memcpy(&data->u.ipv4, frame+idx, 4);
idx += 4;
break;
case SPOE_DATA_T_IPV6:
if (frame+idx+16 > end)
return -1;
memcpy(&data->u.ipv6, frame+idx, 16);
idx += 16;
break;
case SPOE_DATA_T_STR:
if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
return -1;
idx += r;
if (frame+idx+sz > end)
return -1;
data->u.buffer.str = frame+idx;
data->u.buffer.len = sz;
idx += sz;
break;
case SPOE_DATA_T_BIN:
if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
return -1;
idx += r;
if (frame+idx+sz > end)
return -1;
data->u.buffer.str = frame+idx;
data->u.buffer.len = sz;
idx += sz;
break;
default:
break;
}
if (frame+idx > end)
return -1;
return idx;
}
/* Check the protocol version. It returns -1 if an error occurred, the number of
* read bytes otherwise. */
static int
check_proto_version(struct worker *w, int idx)
{
char *str;
uint64_t sz;
/* Get the list of all supported versions by HAProxy */
if ((w->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
w->status_code = SPOE_FRM_ERR_INVALID;
return -1;
}
idx += decode_spoe_string(w->buf+idx, w->buf+w->len, &str, &sz);
if (str == NULL) {
w->status_code = SPOE_FRM_ERR_INVALID;
return -1;
}
/* TODO: Find the right version in supported ones */
return idx;
}
/* Check max frame size value. It returns -1 if an error occurred, the number of
* read bytes otherwise. */
static int
check_max_frame_size(struct worker *w, int idx)
{
uint64_t sz;
int type, i;
/* Get the max-frame-size value of HAProxy */
type = w->buf[idx++];
if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
(type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
(type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
(type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
w->status_code = SPOE_FRM_ERR_INVALID;
return -1;
}
if ((i = decode_spoe_varint(w->buf+idx, w->buf+w->len, &sz)) == -1) {
w->status_code = SPOE_FRM_ERR_INVALID;
return -1;
}
idx += i;
/* Keep the lower value */
if (sz < w->size)
w->size = sz;
return idx;
}
/* Check healthcheck value. It returns -1 if an error occurred, the number of
* read bytes otherwise. */
static int
check_healthcheck(struct worker *w, int idx)
{
int type;
/* Get the "healthcheck" value of HAProxy */
type = w->buf[idx++];
if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL) {
w->status_code = SPOE_FRM_ERR_INVALID;
return -1;
}
w->healthcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
return idx;
}
/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
* occurred, 0 if the frame must be skipped, otherwise the number of read
* bytes. */
static int
handle_hahello(struct worker *w)
{
char *end = w->buf+w->len;
int i, idx = 0;
/* Check frame type */
if (w->buf[idx++] != SPOE_FRM_T_HAPROXY_HELLO)
goto skip;
/* Skip flags */
idx += 4;
/* stream-id and frame-id must be cleared */
if (w->buf[idx] != 0 || w->buf[idx+1] != 0) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
idx += 2;
/* Loop on K/V items */
while (idx < w->len) {
char *str;
uint64_t sz;
/* Decode the item name */
idx += decode_spoe_string(w->buf+idx, end, &str, &sz);
if (str == NULL) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
/* Check "supported-versions" K/V item */
if (!memcmp(str, "supported-versions", sz)) {
if ((i = check_proto_version(w, idx)) == -1)
goto error;
idx = i;
}
/* Check "max-frame-size" K/V item "*/
else if (!memcmp(str, "max-frame-size", sz)) {
if ((i = check_max_frame_size(w, idx)) == -1)
goto error;
idx = i;
}
/* Check "healthcheck" K/V item "*/
else if (!memcmp(str, "healthcheck", sz)) {
if ((i = check_healthcheck(w, idx)) == -1)
goto error;
idx = i;
}
/* Skip "capabilities" K/V item for now */
else {
/* Silently ignore unknown item */
if ((i = skip_spoe_data(w->buf+idx, end)) == -1) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
idx += i;
}
}
return idx;
skip:
return 0;
error:
return -1;
}
/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
* occurred, 0 if the frame must be skipped, otherwise the number of read
* bytes. */
static int
handle_hadiscon(struct worker *w)
{
char *end = w->buf+w->len;
int i, idx = 0;
/* Check frame type */
if (w->buf[idx++] != SPOE_FRM_T_HAPROXY_DISCON)
goto skip;
/* Skip flags */
idx += 4;
/* stream-id and frame-id must be cleared */
if (w->buf[idx] != 0 || w->buf[idx+1] != 0) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
idx += 2;
/* Loop on K/V items */
while (idx < w->len) {
char *str;
uint64_t sz;
/* Decode item key */
idx += decode_spoe_string(w->buf+idx, end, &str, &sz);
if (str == NULL) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
/* Silently ignore unknown item */
if ((i = skip_spoe_data(w->buf+idx, end)) == -1) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
idx += i;
}
w->status_code = SPOE_FRM_ERR_NONE;
return idx;
skip:
return 0;
error:
return -1;
}
/* Encode a ACK frame to send it to HAProxy. It returns -1 if an error occurred,
* the number of written bytes otherwise. */
static void prepare_agentack(struct worker *w)
{
unsigned int flags = 0;
w->ack_len = 0;
/* Frame type */
w->ack[w->ack_len++] = SPOE_FRM_T_AGENT_ACK;
/* Set flags */
flags |= htonl(SPOE_FRM_FL_FIN);
memcpy(w->ack + w->ack_len, &flags, 4);
w->ack_len += 4;
/* Set stream-id and frame-id for ACK frames */
w->ack_len += encode_spoe_varint(w->stream_id, w->ack + w->ack_len);
w->ack_len += encode_spoe_varint(w->frame_id, w->ack + w->ack_len);
}
static inline
int set_var_name(struct worker *w, const char *name, int name_len, unsigned char scope)
{
w->ack[w->ack_len++] = SPOE_ACT_T_SET_VAR; /* Action type */
w->ack[w->ack_len++] = 3; /* Number of args */
w->ack[w->ack_len++] = scope; /* Arg 1: the scope */
w->ack_len += encode_spoe_string(name, name_len, w->ack+w->ack_len); /* Arg 2: variable name */
return 1;
}
int set_var_null(struct worker *w,
const char *name, int name_len,
unsigned char scope)
{
if (!set_var_name(w, name, name_len, scope))
return 0;
w->ack[w->ack_len++] = SPOE_DATA_T_NULL;
return 1;
}
int set_var_bool(struct worker *w,
const char *name, int name_len,
unsigned char scope, bool value)
{
if (!set_var_name(w, name, name_len, scope))
return 0;
w->ack[w->ack_len++] = SPOE_DATA_T_BOOL | (!!value << 4);
return 1;
}
static inline
int set_var_int(struct worker *w,
const char *name, int name_len,
unsigned char scope, int type, uint64_t value)
{
if (!set_var_name(w, name, name_len, scope))
return 0;
w->ack[w->ack_len++] = SPOE_DATA_T_UINT32;
w->ack_len += encode_spoe_varint(value, w->ack+w->ack_len); /* Arg 3: variable value */
return 1;
}
int set_var_uint32(struct worker *w,
const char *name, int name_len,
unsigned char scope, uint32_t value)
{
return set_var_int(w, name, name_len, scope, SPOE_DATA_T_UINT32, value);
}
int set_var_int32(struct worker *w,
const char *name, int name_len,
unsigned char scope, int32_t value)
{
return set_var_int(w, name, name_len, scope, SPOE_DATA_T_INT32, value);
}
int set_var_uint64(struct worker *w,
const char *name, int name_len,
unsigned char scope, uint64_t value)
{
return set_var_int(w, name, name_len, scope, SPOE_DATA_T_INT32, value);
}
int set_var_int64(struct worker *w,
const char *name, int name_len,
unsigned char scope, int64_t value)
{
return set_var_int(w, name, name_len, scope, SPOE_DATA_T_INT32, value);
}
int set_var_ipv4(struct worker *w,
const char *name, int name_len,
unsigned char scope,
struct in_addr *ipv4)
{
if (!set_var_name(w, name, name_len, scope))
return 0;
w->ack[w->ack_len++] = SPOE_DATA_T_IPV4;
memcpy(w->ack+w->ack_len, ipv4, 4);
w->ack_len += 4;
return 1;
}
int set_var_ipv6(struct worker *w,
const char *name, int name_len,
unsigned char scope,
struct in6_addr *ipv6)
{
if (!set_var_name(w, name, name_len, scope))
return 0;
w->ack[w->ack_len++] = SPOE_DATA_T_IPV6;
memcpy(w->ack+w->ack_len, ipv6, 16);
w->ack_len += 16;
return 1;
}
static inline
int set_var_buf(struct worker *w,
const char *name, int name_len,
unsigned char scope, int type,
const char *str, int str_len)
{
if (!set_var_name(w, name, name_len, scope))
return 0;
w->ack[w->ack_len++] = type;
w->ack_len += encode_spoe_string(str, str_len, w->ack+w->ack_len);
return 1;
}
int set_var_string(struct worker *w,
const char *name, int name_len,
unsigned char scope,
const char *str, int strlen)
{
return set_var_buf(w, name, name_len, scope, SPOE_DATA_T_STR, str, strlen);
}
int set_var_bin(struct worker *w,
const char *name, int name_len,
unsigned char scope,
const char *str, int strlen)
{
return set_var_buf(w, name, name_len, scope, SPOE_DATA_T_BIN, str, strlen);
}
/* This function is a little bit ugly,
* TODO: improve the response without copying the buffer
*/
static int commit_agentack(struct worker *w)
{
memcpy(w->buf, w->ack, w->ack_len);
w->len = w->ack_len;
return 1;
}
/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
* occurred, 0 if the frame must be skipped, otherwise the number of read
* bytes. */
static int
handle_hanotify(struct worker *w)
{
char *end = w->buf+w->len;
uint64_t stream_id, frame_id;
int nbargs, i, idx = 0;
int index;
struct spoe_kv args[256];
uint64_t length;
struct ps_message *msg;
/* Check frame type */
if (w->buf[idx++] != SPOE_FRM_T_HAPROXY_NOTIFY)
goto skip;
/* Skip flags */
idx += 4;
/* Read the stream-id */
if ((i = decode_spoe_varint(w->buf+idx, end, &stream_id)) == -1) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
idx += i;
/* Read the frame-id */
if ((i = decode_spoe_varint(w->buf+idx, end, &frame_id)) == -1) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
idx += i;
w->stream_id = (unsigned int)stream_id;
w->frame_id = (unsigned int)frame_id;
DEBUG("Notify frame received: stream-id=%u - frame-id=%u",
w->stream_id, w->frame_id);
/* Prepare ack, if the processing fails the ack will be cancelled */
prepare_agentack(w);
/* Loop on messages */
while (idx < w->len) {
char *str;
uint64_t sz;
/* Decode the message name */
idx += decode_spoe_string(w->buf+idx, end, &str, &sz);
if (str == NULL) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
DEBUG(" Message '%.*s' received", (int)sz, str);
/* Decode all SPOE data */
nbargs = (unsigned char)w->buf[idx++];
for (index = 0; index < nbargs; index++) {
/* Read the key name */
if ((i = decode_spoe_string(w->buf+idx, end,
&args[index].name.str,
&length)) == -1) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
if (length > INT_MAX) {
w->status_code = SPOE_FRM_ERR_TOO_BIG;
goto error;
}
args[index].name.len = length;
idx += i;
/* Read the value */
memset(&args[index].value, 0, sizeof(args[index].value));
if ((i = decode_spoe_data(w->buf+idx, end, &args[index].value)) == -1) {
w->status_code = SPOE_FRM_ERR_INVALID;
goto error;
}
idx += i;
}
/* Lookup for existing bindings. If no existing message
* where found, does nothing.
*/
for (msg = ps_messages; msg; msg = msg->next)
if (sz == strlen(msg->name) && strncmp(str, msg->name, sz) == 0)
break;
if (msg == NULL || msg->ps->exec_message == NULL) {
DEBUG(" Message '%.*s' have no bindings registered", (int)sz, str);
continue;
}
/* Process the message */
msg->ps->exec_message(w, msg->ref, nbargs, args);
}
return idx;
skip:
return 0;
error:
return -1;
}
/* Encode a HELLO frame to send it to HAProxy. It returns -1 if an error
* occurred, the number of written bytes otherwise. */
static int
prepare_agenthello(struct worker *w)
{
int idx = 0;
unsigned int flags = 0;
/* Frame Type */
w->buf[idx++] = SPOE_FRM_T_AGENT_HELLO;
/* Set flags */
flags |= htonl(SPOE_FRM_FL_FIN);
memcpy(w->buf+idx, &flags, 4);
idx += 4;
/* No stream-id and frame-id for HELLO frames */
w->buf[idx++] = 0;
w->buf[idx++] = 0;
/* "version" K/V item */
idx += encode_spoe_string("version", 7, w->buf+idx);
w->buf[idx++] = SPOE_DATA_T_STR;
idx += encode_spoe_string(SPOP_VERSION, SLEN(SPOP_VERSION), w->buf+idx);
/* "max-frame-size" K/V item */
idx += encode_spoe_string("max-frame-size", 14, w->buf+idx);
w->buf[idx++] = SPOE_DATA_T_UINT32;
idx += encode_spoe_varint(w->size, w->buf+idx);
/* "capabilities" K/V item */
idx += encode_spoe_string("capabilities", 12, w->buf+idx);
w->buf[idx++] = SPOE_DATA_T_STR;
idx += encode_spoe_string(SPOA_CAPABILITIES, SLEN(SPOA_CAPABILITIES), w->buf+idx);
w->len = idx;
return idx;
}
/* Encode a DISCONNECT frame to send it to HAProxy. It returns -1 if an error
* occurred, the number of written bytes otherwise. */
static int
prepare_agentdicon(struct worker *w)
{
const char *reason;
int rlen, idx = 0;
unsigned int flags = 0;
if (w->status_code >= SPOE_FRM_ERRS)
w->status_code = SPOE_FRM_ERR_UNKNOWN;
reason = spoe_frm_err_reasons[w->status_code];
rlen = strlen(reason);
/* Frame type */
w->buf[idx++] = SPOE_FRM_T_AGENT_DISCON;
/* Set flags */
flags |= htonl(SPOE_FRM_FL_FIN);
memcpy(w->buf+idx, &flags, 4);
idx += 4;