forked from haproxy/haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpcheck.c
5165 lines (4582 loc) · 157 KB
/
tcpcheck.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
/*
* Health-checks functions.
*
* Copyright 2000-2009,2020 Willy Tarreau <w@1wt.eu>
* Copyright 2007-2010 Krzysztof Piotr Oledzki <ole@ans.pl>
* Copyright 2013 Baptiste Assmann <bedis9@gmail.com>
* Copyright 2020 Gaetan Rivet <grive@u256.net>
* Copyright 2020 Christopher Faulet <cfaulet@haproxy.com>
*
* 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 <sys/resource.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <haproxy/action.h>
#include <haproxy/api.h>
#include <haproxy/cfgparse.h>
#include <haproxy/check.h>
#include <haproxy/chunk.h>
#include <haproxy/connection.h>
#include <haproxy/errors.h>
#include <haproxy/global.h>
#include <haproxy/h1.h>
#include <haproxy/http.h>
#include <haproxy/http_htx.h>
#include <haproxy/htx.h>
#include <haproxy/istbuf.h>
#include <haproxy/list.h>
#include <haproxy/log.h>
#include <haproxy/net_helper.h>
#include <haproxy/protocol.h>
#include <haproxy/proxy-t.h>
#include <haproxy/regex.h>
#include <haproxy/sample.h>
#include <haproxy/server.h>
#include <haproxy/ssl_sock.h>
#include <haproxy/task.h>
#include <haproxy/tcpcheck.h>
#include <haproxy/time.h>
#include <haproxy/tools.h>
#include <haproxy/trace.h>
#include <haproxy/vars.h>
#define TRACE_SOURCE &trace_check
/* Global tree to share all tcp-checks */
struct eb_root shared_tcpchecks = EB_ROOT;
DECLARE_POOL(pool_head_tcpcheck_rule, "tcpcheck_rule", sizeof(struct tcpcheck_rule));
/**************************************************************************/
/*************** Init/deinit tcp-check rules and ruleset ******************/
/**************************************************************************/
/* Releases memory allocated for a log-format string */
static void free_tcpcheck_fmt(struct list *fmt)
{
struct logformat_node *lf, *lfb;
list_for_each_entry_safe(lf, lfb, fmt, list) {
LIST_DELETE(&lf->list);
release_sample_expr(lf->expr);
free(lf->arg);
free(lf);
}
}
/* Releases memory allocated for an HTTP header used in a tcp-check send rule */
void free_tcpcheck_http_hdr(struct tcpcheck_http_hdr *hdr)
{
if (!hdr)
return;
free_tcpcheck_fmt(&hdr->value);
istfree(&hdr->name);
free(hdr);
}
/* Releases memory allocated for an HTTP header list used in a tcp-check send
* rule
*/
static void free_tcpcheck_http_hdrs(struct list *hdrs)
{
struct tcpcheck_http_hdr *hdr, *bhdr;
list_for_each_entry_safe(hdr, bhdr, hdrs, list) {
LIST_DELETE(&hdr->list);
free_tcpcheck_http_hdr(hdr);
}
}
/* Releases memory allocated for a tcp-check. If in_pool is set, it means the
* tcp-check was allocated using a memory pool (it is used to instantiate email
* alerts).
*/
void free_tcpcheck(struct tcpcheck_rule *rule, int in_pool)
{
if (!rule)
return;
free(rule->comment);
switch (rule->action) {
case TCPCHK_ACT_SEND:
switch (rule->send.type) {
case TCPCHK_SEND_STRING:
case TCPCHK_SEND_BINARY:
istfree(&rule->send.data);
break;
case TCPCHK_SEND_STRING_LF:
case TCPCHK_SEND_BINARY_LF:
free_tcpcheck_fmt(&rule->send.fmt);
break;
case TCPCHK_SEND_HTTP:
free(rule->send.http.meth.str.area);
if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT))
istfree(&rule->send.http.uri);
else
free_tcpcheck_fmt(&rule->send.http.uri_fmt);
istfree(&rule->send.http.vsn);
free_tcpcheck_http_hdrs(&rule->send.http.hdrs);
if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT))
istfree(&rule->send.http.body);
else
free_tcpcheck_fmt(&rule->send.http.body_fmt);
break;
case TCPCHK_SEND_UNDEF:
break;
}
break;
case TCPCHK_ACT_EXPECT:
free_tcpcheck_fmt(&rule->expect.onerror_fmt);
free_tcpcheck_fmt(&rule->expect.onsuccess_fmt);
release_sample_expr(rule->expect.status_expr);
switch (rule->expect.type) {
case TCPCHK_EXPECT_HTTP_STATUS:
free(rule->expect.codes.codes);
break;
case TCPCHK_EXPECT_STRING:
case TCPCHK_EXPECT_BINARY:
case TCPCHK_EXPECT_HTTP_BODY:
istfree(&rule->expect.data);
break;
case TCPCHK_EXPECT_STRING_REGEX:
case TCPCHK_EXPECT_BINARY_REGEX:
case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
case TCPCHK_EXPECT_HTTP_BODY_REGEX:
regex_free(rule->expect.regex);
break;
case TCPCHK_EXPECT_STRING_LF:
case TCPCHK_EXPECT_BINARY_LF:
case TCPCHK_EXPECT_HTTP_BODY_LF:
free_tcpcheck_fmt(&rule->expect.fmt);
break;
case TCPCHK_EXPECT_HTTP_HEADER:
if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_REG)
regex_free(rule->expect.hdr.name_re);
else if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HNAME_FMT)
free_tcpcheck_fmt(&rule->expect.hdr.name_fmt);
else
istfree(&rule->expect.hdr.name);
if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_REG)
regex_free(rule->expect.hdr.value_re);
else if (rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_FMT)
free_tcpcheck_fmt(&rule->expect.hdr.value_fmt);
else if (!(rule->expect.flags & TCPCHK_EXPT_FL_HTTP_HVAL_NONE))
istfree(&rule->expect.hdr.value);
break;
case TCPCHK_EXPECT_CUSTOM:
case TCPCHK_EXPECT_UNDEF:
break;
}
break;
case TCPCHK_ACT_CONNECT:
free(rule->connect.sni);
free(rule->connect.alpn);
release_sample_expr(rule->connect.port_expr);
break;
case TCPCHK_ACT_COMMENT:
break;
case TCPCHK_ACT_ACTION_KW:
free(rule->action_kw.rule);
break;
}
if (in_pool)
pool_free(pool_head_tcpcheck_rule, rule);
else
free(rule);
}
/* Creates a tcp-check variable used in preset variables before executing a
* tcp-check ruleset.
*/
struct tcpcheck_var *create_tcpcheck_var(const struct ist name)
{
struct tcpcheck_var *var = NULL;
var = calloc(1, sizeof(*var));
if (var == NULL)
return NULL;
var->name = istdup(name);
if (!isttest(var->name)) {
free(var);
return NULL;
}
LIST_INIT(&var->list);
return var;
}
/* Releases memory allocated for a preset tcp-check variable */
void free_tcpcheck_var(struct tcpcheck_var *var)
{
if (!var)
return;
istfree(&var->name);
if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN)
free(var->data.u.str.area);
else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER)
free(var->data.u.meth.str.area);
free(var);
}
/* Releases a list of preset tcp-check variables */
void free_tcpcheck_vars(struct list *vars)
{
struct tcpcheck_var *var, *back;
list_for_each_entry_safe(var, back, vars, list) {
LIST_DELETE(&var->list);
free_tcpcheck_var(var);
}
}
/* Duplicate a list of preset tcp-check variables */
int dup_tcpcheck_vars(struct list *dst, const struct list *src)
{
const struct tcpcheck_var *var;
struct tcpcheck_var *new = NULL;
list_for_each_entry(var, src, list) {
new = create_tcpcheck_var(var->name);
if (!new)
goto error;
new->data.type = var->data.type;
if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) {
if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL)
goto error;
if (var->data.type == SMP_T_STR)
new->data.u.str.area[new->data.u.str.data] = 0;
}
else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) {
if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL)
goto error;
new->data.u.str.area[new->data.u.str.data] = 0;
new->data.u.meth.meth = var->data.u.meth.meth;
}
else
new->data.u = var->data.u;
LIST_APPEND(dst, &new->list);
}
return 1;
error:
free(new);
return 0;
}
/* Looks for a shared tcp-check ruleset given its name. */
struct tcpcheck_ruleset *find_tcpcheck_ruleset(const char *name)
{
struct tcpcheck_ruleset *rs;
struct ebpt_node *node;
node = ebis_lookup_len(&shared_tcpchecks, name, strlen(name));
if (node) {
rs = container_of(node, typeof(*rs), node);
return rs;
}
return NULL;
}
/* Creates a new shared tcp-check ruleset and insert it in shared_tcpchecks
* tree.
*/
struct tcpcheck_ruleset *create_tcpcheck_ruleset(const char *name)
{
struct tcpcheck_ruleset *rs;
rs = calloc(1, sizeof(*rs));
if (rs == NULL)
return NULL;
rs->node.key = strdup(name);
if (rs->node.key == NULL) {
free(rs);
return NULL;
}
LIST_INIT(&rs->rules);
ebis_insert(&shared_tcpchecks, &rs->node);
return rs;
}
/* Releases memory allocated by a tcp-check ruleset. */
void free_tcpcheck_ruleset(struct tcpcheck_ruleset *rs)
{
struct tcpcheck_rule *r, *rb;
if (!rs)
return;
ebpt_delete(&rs->node);
free(rs->node.key);
list_for_each_entry_safe(r, rb, &rs->rules, list) {
LIST_DELETE(&r->list);
free_tcpcheck(r, 0);
}
free(rs);
}
/**************************************************************************/
/**************** Everything about tcp-checks execution *******************/
/**************************************************************************/
/* Returns the id of a step in a tcp-check ruleset */
int tcpcheck_get_step_id(const struct check *check, const struct tcpcheck_rule *rule)
{
if (!rule)
rule = check->current_step;
/* no last started step => first step */
if (!rule)
return 1;
/* last step is the first implicit connect */
if (rule->index == 0 &&
rule->action == TCPCHK_ACT_CONNECT &&
(rule->connect.options & TCPCHK_OPT_IMPLICIT))
return 0;
return rule->index + 1;
}
/* Returns the first non COMMENT/ACTION_KW tcp-check rule from list <list> or
* NULL if none was found.
*/
struct tcpcheck_rule *get_first_tcpcheck_rule(const struct tcpcheck_rules *rules)
{
struct tcpcheck_rule *r;
list_for_each_entry(r, rules->list, list) {
if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW)
return r;
}
return NULL;
}
/* Returns the last non COMMENT/ACTION_KW tcp-check rule from list <list> or
* NULL if none was found.
*/
static struct tcpcheck_rule *get_last_tcpcheck_rule(struct tcpcheck_rules *rules)
{
struct tcpcheck_rule *r;
list_for_each_entry_rev(r, rules->list, list) {
if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW)
return r;
}
return NULL;
}
/* Returns the non COMMENT/ACTION_KW tcp-check rule from list <list> following
* <start> or NULL if non was found. If <start> is NULL, it relies on
* get_first_tcpcheck_rule().
*/
static struct tcpcheck_rule *get_next_tcpcheck_rule(struct tcpcheck_rules *rules, struct tcpcheck_rule *start)
{
struct tcpcheck_rule *r;
if (!start)
return get_first_tcpcheck_rule(rules);
r = LIST_NEXT(&start->list, typeof(r), list);
list_for_each_entry_from(r, rules->list, list) {
if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW)
return r;
}
return NULL;
}
/* Creates info message when a tcp-check healthcheck fails on an expect rule */
static void tcpcheck_expect_onerror_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule,
int match, struct ist info)
{
struct sample *smp;
/* Follows these step to produce the info message:
* 1. if info field is already provided, copy it
* 2. if the expect rule provides an onerror log-format string,
* use it to produce the message
* 3. the expect rule is part of a protocol check (http, redis, mysql...), do nothing
* 4. Otherwise produce the generic tcp-check info message
*/
if (istlen(info)) {
chunk_strncat(msg, istptr(info), istlen(info));
goto comment;
}
else if (!LIST_ISEMPTY(&rule->expect.onerror_fmt)) {
msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg), &rule->expect.onerror_fmt);
goto comment;
}
if (check->type == PR_O2_TCPCHK_CHK &&
(check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) != TCPCHK_RULES_TCP_CHK)
goto comment;
chunk_strcat(msg, (match ? "TCPCHK matched unwanted content" : "TCPCHK did not match content"));
switch (rule->expect.type) {
case TCPCHK_EXPECT_HTTP_STATUS:
chunk_appendf(msg, "(status codes) at step %d", tcpcheck_get_step_id(check, rule));
break;
case TCPCHK_EXPECT_STRING:
case TCPCHK_EXPECT_HTTP_BODY:
chunk_appendf(msg, " '%.*s' at step %d", (unsigned int)istlen(rule->expect.data), istptr(rule->expect.data),
tcpcheck_get_step_id(check, rule));
break;
case TCPCHK_EXPECT_BINARY:
chunk_appendf(msg, " (binary) at step %d", tcpcheck_get_step_id(check, rule));
break;
case TCPCHK_EXPECT_STRING_REGEX:
case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
case TCPCHK_EXPECT_HTTP_BODY_REGEX:
chunk_appendf(msg, " (regex) at step %d", tcpcheck_get_step_id(check, rule));
break;
case TCPCHK_EXPECT_BINARY_REGEX:
chunk_appendf(msg, " (binary regex) at step %d", tcpcheck_get_step_id(check, rule));
break;
case TCPCHK_EXPECT_STRING_LF:
case TCPCHK_EXPECT_HTTP_BODY_LF:
chunk_appendf(msg, " (log-format string) at step %d", tcpcheck_get_step_id(check, rule));
break;
case TCPCHK_EXPECT_BINARY_LF:
chunk_appendf(msg, " (log-format binary) at step %d", tcpcheck_get_step_id(check, rule));
break;
case TCPCHK_EXPECT_CUSTOM:
chunk_appendf(msg, " (custom function) at step %d", tcpcheck_get_step_id(check, rule));
break;
case TCPCHK_EXPECT_HTTP_HEADER:
chunk_appendf(msg, " (header pattern) at step %d", tcpcheck_get_step_id(check, rule));
case TCPCHK_EXPECT_UNDEF:
/* Should never happen. */
return;
}
comment:
/* If the failing expect rule provides a comment, it is concatenated to
* the info message.
*/
if (rule->comment) {
chunk_strcat(msg, " comment: ");
chunk_strcat(msg, rule->comment);
}
/* Finally, the check status code is set if the failing expect rule
* defines a status expression.
*/
if (rule->expect.status_expr) {
smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
rule->expect.status_expr, SMP_T_STR);
if (smp && sample_casts[smp->data.type][SMP_T_SINT] &&
sample_casts[smp->data.type][SMP_T_SINT](smp))
check->code = smp->data.u.sint;
}
*(b_tail(msg)) = '\0';
}
/* Creates info message when a tcp-check healthcheck succeeds on an expect rule */
static void tcpcheck_expect_onsuccess_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule,
struct ist info)
{
struct sample *smp;
/* Follows these step to produce the info message:
* 1. if info field is already provided, copy it
* 2. if the expect rule provides an onsucces log-format string,
* use it to produce the message
* 3. the expect rule is part of a protocol check (http, redis, mysql...), do nothing
* 4. Otherwise produce the generic tcp-check info message
*/
if (istlen(info))
chunk_strncat(msg, istptr(info), istlen(info));
if (!LIST_ISEMPTY(&rule->expect.onsuccess_fmt))
msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg),
&rule->expect.onsuccess_fmt);
else if (check->type == PR_O2_TCPCHK_CHK &&
(check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK)
chunk_strcat(msg, "(tcp-check)");
/* Finally, the check status code is set if the expect rule defines a
* status expression.
*/
if (rule->expect.status_expr) {
smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
rule->expect.status_expr, SMP_T_STR);
if (smp && sample_casts[smp->data.type][SMP_T_SINT] &&
sample_casts[smp->data.type][SMP_T_SINT](smp))
check->code = smp->data.u.sint;
}
*(b_tail(msg)) = '\0';
}
/* Internal functions to parse and validate a MySQL packet in the context of an
* expect rule. It start to parse the input buffer at the offset <offset>. If
* <last_read> is set, no more data are expected.
*/
static enum tcpcheck_eval_ret tcpcheck_mysql_expect_packet(struct check *check, struct tcpcheck_rule *rule,
unsigned int offset, int last_read)
{
enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
enum healthcheck_status status;
struct buffer *msg = NULL;
struct ist desc = IST_NULL;
unsigned int err = 0, plen = 0;
TRACE_ENTER(CHK_EV_TCPCHK_EXP, check);
/* 3 Bytes for the packet length and 1 byte for the sequence id */
if (b_data(&check->bi) < offset+4) {
if (!last_read)
goto wait_more_data;
/* invalid length or truncated response */
status = HCHK_STATUS_L7RSP;
goto error;
}
plen = ((unsigned char) *b_peek(&check->bi, offset)) +
(((unsigned char) *(b_peek(&check->bi, offset+1))) << 8) +
(((unsigned char) *(b_peek(&check->bi, offset+2))) << 16);
if (b_data(&check->bi) < offset+plen+4) {
if (!last_read)
goto wait_more_data;
/* invalid length or truncated response */
status = HCHK_STATUS_L7RSP;
goto error;
}
if (*b_peek(&check->bi, offset+4) == '\xff') {
/* MySQL Error packet always begin with field_count = 0xff */
status = HCHK_STATUS_L7STS;
err = ((unsigned char) *b_peek(&check->bi, offset+5)) +
(((unsigned char) *(b_peek(&check->bi, offset+6))) << 8);
desc = ist2(b_peek(&check->bi, offset+7), b_data(&check->bi) - offset - 7);
goto error;
}
if (get_next_tcpcheck_rule(check->tcpcheck_rules, rule) != NULL) {
/* Not the last rule, continue */
goto out;
}
/* We set the MySQL Version in description for information purpose
* FIXME : it can be cool to use MySQL Version for other purpose,
* like mark as down old MySQL server.
*/
status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
set_server_check_status(check, status, b_peek(&check->bi, 5));
out:
free_trash_chunk(msg);
TRACE_LEAVE(CHK_EV_TCPCHK_EXP, check, 0, 0, (size_t[]){ret});
return ret;
error:
ret = TCPCHK_EVAL_STOP;
check->code = err;
msg = alloc_trash_chunk();
if (msg)
tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
goto out;
wait_more_data:
TRACE_DEVEL("waiting for more data", CHK_EV_TCPCHK_EXP, check);
ret = TCPCHK_EVAL_WAIT;
goto out;
}
/* Custom tcp-check expect function to parse and validate the MySQL initial
* handshake packet. Returns TCPCHK_EVAL_WAIT to wait for more data,
* TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an
* error occurred.
*/
enum tcpcheck_eval_ret tcpcheck_mysql_expect_iniths(struct check *check, struct tcpcheck_rule *rule, int last_read)
{
return tcpcheck_mysql_expect_packet(check, rule, 0, last_read);
}
/* Custom tcp-check expect function to parse and validate the MySQL OK packet
* following the initial handshake. Returns TCPCHK_EVAL_WAIT to wait for more
* data, TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if
* an error occurred.
*/
enum tcpcheck_eval_ret tcpcheck_mysql_expect_ok(struct check *check, struct tcpcheck_rule *rule, int last_read)
{
unsigned int hslen = 0;
hslen = 4 + ((unsigned char) *b_head(&check->bi)) +
(((unsigned char) *(b_peek(&check->bi, 1))) << 8) +
(((unsigned char) *(b_peek(&check->bi, 2))) << 16);
return tcpcheck_mysql_expect_packet(check, rule, hslen, last_read);
}
/* Custom tcp-check expect function to parse and validate the LDAP bind response
* package packet. Returns TCPCHK_EVAL_WAIT to wait for more data,
* TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an
* error occurred.
*/
enum tcpcheck_eval_ret tcpcheck_ldap_expect_bindrsp(struct check *check, struct tcpcheck_rule *rule, int last_read)
{
enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
enum healthcheck_status status;
struct buffer *msg = NULL;
struct ist desc = IST_NULL;
char *ptr;
unsigned short nbytes = 0;
size_t msglen = 0;
TRACE_ENTER(CHK_EV_TCPCHK_EXP, check);
/* Check if the server speaks LDAP (ASN.1/BER)
* http://en.wikipedia.org/wiki/Basic_Encoding_Rules
* http://tools.ietf.org/html/rfc4511
*/
ptr = b_head(&check->bi) + 1;
/* size of LDAPMessage */
if (*ptr & 0x80) {
/* For message size encoded on several bytes, we only handle
* size encoded on 2 or 4 bytes. There is no reason to make this
* part to complex because only Active Directory is known to
* encode BindReponse length on 4 bytes.
*/
nbytes = (*ptr & 0x7f);
if (b_data(&check->bi) < 1 + nbytes)
goto too_short;
switch (nbytes) {
case 4: msglen = read_n32(ptr+1); break;
case 2: msglen = read_n16(ptr+1); break;
default:
status = HCHK_STATUS_L7RSP;
desc = ist("Not LDAPv3 protocol");
goto error;
}
}
else
msglen = *ptr;
ptr += 1 + nbytes;
if (b_data(&check->bi) < 2 + nbytes + msglen)
goto too_short;
/* http://tools.ietf.org/html/rfc4511#section-4.2.2
* messageID: 0x02 0x01 0x01: INTEGER 1
* protocolOp: 0x61: bindResponse
*/
if (memcmp(ptr, "\x02\x01\x01\x61", 4) != 0) {
status = HCHK_STATUS_L7RSP;
desc = ist("Not LDAPv3 protocol");
goto error;
}
ptr += 4;
/* skip size of bindResponse */
nbytes = 0;
if (*ptr & 0x80)
nbytes = (*ptr & 0x7f);
ptr += 1 + nbytes;
/* http://tools.ietf.org/html/rfc4511#section-4.1.9
* ldapResult: 0x0a 0x01: ENUMERATION
*/
if (memcmp(ptr, "\x0a\x01", 2) != 0) {
status = HCHK_STATUS_L7RSP;
desc = ist("Not LDAPv3 protocol");
goto error;
}
ptr += 2;
/* http://tools.ietf.org/html/rfc4511#section-4.1.9
* resultCode
*/
check->code = *ptr;
if (check->code) {
status = HCHK_STATUS_L7STS;
desc = ist("See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
goto error;
}
status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
set_server_check_status(check, status, "Success");
out:
free_trash_chunk(msg);
TRACE_LEAVE(CHK_EV_TCPCHK_EXP, check, 0, 0, (size_t[]){ret});
return ret;
error:
ret = TCPCHK_EVAL_STOP;
msg = alloc_trash_chunk();
if (msg)
tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
goto out;
too_short:
if (!last_read)
goto wait_more_data;
/* invalid length or truncated response */
status = HCHK_STATUS_L7RSP;
goto error;
wait_more_data:
TRACE_DEVEL("waiting for more data", CHK_EV_TCPCHK_EXP, check);
ret = TCPCHK_EVAL_WAIT;
goto out;
}
/* Custom tcp-check expect function to parse and validate the SPOP hello agent
* frame. Returns TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE
* to evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred.
*/
enum tcpcheck_eval_ret tcpcheck_spop_expect_agenthello(struct check *check, struct tcpcheck_rule *rule, int last_read)
{
enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
enum healthcheck_status status;
struct buffer *msg = NULL;
struct ist desc = IST_NULL;
unsigned int framesz;
TRACE_ENTER(CHK_EV_TCPCHK_EXP, check);
memcpy(&framesz, b_head(&check->bi), 4);
framesz = ntohl(framesz);
if (!last_read && b_data(&check->bi) < (4+framesz))
goto wait_more_data;
memset(b_orig(&trash), 0, b_size(&trash));
if (spoe_handle_healthcheck_response(b_peek(&check->bi, 4), framesz, b_orig(&trash), HCHK_DESC_LEN) == -1) {
status = HCHK_STATUS_L7RSP;
desc = ist2(b_orig(&trash), strlen(b_orig(&trash)));
goto error;
}
status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
set_server_check_status(check, status, "SPOA server is ok");
out:
free_trash_chunk(msg);
TRACE_LEAVE(CHK_EV_TCPCHK_EXP, check, 0, 0, (size_t[]){ret});
return ret;
error:
ret = TCPCHK_EVAL_STOP;
msg = alloc_trash_chunk();
if (msg)
tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
goto out;
wait_more_data:
TRACE_DEVEL("waiting for more data", CHK_EV_TCPCHK_EXP, check);
ret = TCPCHK_EVAL_WAIT;
goto out;
}
/* Custom tcp-check expect function to parse and validate the agent-check
* reply. Returns TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE
* to evaluate the next rule or TCPCHK_EVAL_STOP if an error occurred.
*/
enum tcpcheck_eval_ret tcpcheck_agent_expect_reply(struct check *check, struct tcpcheck_rule *rule, int last_read)
{
enum tcpcheck_eval_ret ret = TCPCHK_EVAL_STOP;
enum healthcheck_status status = HCHK_STATUS_CHECKED;
const char *hs = NULL; /* health status */
const char *as = NULL; /* admin status */
const char *ps = NULL; /* performance status */
const char *cs = NULL; /* maxconn */
const char *err = NULL; /* first error to report */
const char *wrn = NULL; /* first warning to report */
char *cmd, *p;
TRACE_ENTER(CHK_EV_TCPCHK_EXP, check);
/* We're getting an agent check response. The agent could
* have been disabled in the mean time with a long check
* still pending. It is important that we ignore the whole
* response.
*/
if (!(check->state & CHK_ST_ENABLED))
goto out;
/* The agent supports strings made of a single line ended by the
* first CR ('\r') or LF ('\n'). This line is composed of words
* delimited by spaces (' '), tabs ('\t'), or commas (','). The
* line may optionally contained a description of a state change
* after a sharp ('#'), which is only considered if a health state
* is announced.
*
* Words may be composed of :
* - a numeric weight suffixed by the percent character ('%').
* - a health status among "up", "down", "stopped", and "fail".
* - an admin status among "ready", "drain", "maint".
*
* These words may appear in any order. If multiple words of the
* same category appear, the last one wins.
*/
p = b_head(&check->bi);
while (*p && *p != '\n' && *p != '\r')
p++;
if (!*p) {
if (!last_read)
goto wait_more_data;
/* at least inform the admin that the agent is mis-behaving */
set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
goto out;
}
*p = 0;
cmd = b_head(&check->bi);
while (*cmd) {
/* look for next word */
if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
cmd++;
continue;
}
if (*cmd == '#') {
/* this is the beginning of a health status description,
* skip the sharp and blanks.
*/
cmd++;
while (*cmd == '\t' || *cmd == ' ')
cmd++;
break;
}
/* find the end of the word so that we have a null-terminated
* word between <cmd> and <p>.
*/
p = cmd + 1;
while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
p++;
if (*p)
*p++ = 0;
/* first, health statuses */
if (strcasecmp(cmd, "up") == 0) {
check->health = check->rise + check->fall - 1;
status = HCHK_STATUS_L7OKD;
hs = cmd;
}
else if (strcasecmp(cmd, "down") == 0) {
check->health = 0;
status = HCHK_STATUS_L7STS;
hs = cmd;
}
else if (strcasecmp(cmd, "stopped") == 0) {
check->health = 0;
status = HCHK_STATUS_L7STS;
hs = cmd;
}
else if (strcasecmp(cmd, "fail") == 0) {
check->health = 0;
status = HCHK_STATUS_L7STS;
hs = cmd;
}
/* admin statuses */
else if (strcasecmp(cmd, "ready") == 0) {
as = cmd;
}
else if (strcasecmp(cmd, "drain") == 0) {
as = cmd;
}
else if (strcasecmp(cmd, "maint") == 0) {
as = cmd;
}
/* try to parse a weight here and keep the last one */
else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
ps = cmd;
}
/* try to parse a maxconn here */
else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
cs = cmd;
}
else {
/* keep a copy of the first error */
if (!err)
err = cmd;
}
/* skip to next word */
cmd = p;
}
/* here, cmd points either to \0 or to the beginning of a
* description. Skip possible leading spaces.
*/
while (*cmd == ' ' || *cmd == '\n')
cmd++;
/* First, update the admin status so that we avoid sending other
* possibly useless warnings and can also update the health if
* present after going back up.
*/
if (as) {
if (strcasecmp(as, "drain") == 0) {
TRACE_DEVEL("set server into DRAIN mode", CHK_EV_TCPCHK_EXP, check);
srv_adm_set_drain(check->server);
}
else if (strcasecmp(as, "maint") == 0) {
TRACE_DEVEL("set server into MAINT mode", CHK_EV_TCPCHK_EXP, check);
srv_adm_set_maint(check->server);
}
else {
TRACE_DEVEL("set server into READY mode", CHK_EV_TCPCHK_EXP, check);
srv_adm_set_ready(check->server);
}
}
/* now change weights */
if (ps) {
const char *msg;
TRACE_DEVEL("change server weight", CHK_EV_TCPCHK_EXP, check);
msg = server_parse_weight_change_request(check->server, ps);
if (!wrn || !*wrn)
wrn = msg;
}
if (cs) {
const char *msg;
cs += strlen("maxconn:");
TRACE_DEVEL("change server maxconn", CHK_EV_TCPCHK_EXP, check);
/* This is safe to call server_parse_maxconn_change_request
* because the server lock is held during the check.
*/
msg = server_parse_maxconn_change_request(check->server, cs);
if (!wrn || !*wrn)
wrn = msg;
}
/* and finally health status */
if (hs) {
/* We'll report some of the warnings and errors we have
* here. Down reports are critical, we leave them untouched.
* Lack of report, or report of 'UP' leaves the room for
* ERR first, then WARN.
*/
const char *msg = cmd;