-
Notifications
You must be signed in to change notification settings - Fork 363
/
ngx_tcp_upstream_check.c
1959 lines (1443 loc) · 50.6 KB
/
ngx_tcp_upstream_check.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 <ngx_config.h>
#include <ngx_core.h>
#include <ngx_tcp.h>
#include <ngx_http.h>
/* ngx_spinlock is defined without a matching unlock primitive */
#define ngx_spinlock_unlock(lock) \
(void) ngx_atomic_cmp_set(lock, ngx_pid, 0)
static ngx_int_t ngx_tcp_check_get_shm_name(ngx_str_t *shm_name,
ngx_pool_t *pool);
static ngx_int_t ngx_tcp_upstream_check_init_shm_zone(ngx_shm_zone_t *shm_zone,
void *data);
static ngx_int_t ngx_tcp_check_init_process(ngx_cycle_t *cycle);
static void ngx_tcp_check_peek_handler(ngx_event_t *event);
static void ngx_tcp_check_send_handler(ngx_event_t *event);
static void ngx_tcp_check_recv_handler(ngx_event_t *event);
static ngx_int_t ngx_tcp_check_http_init(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_http_parse(ngx_tcp_check_peer_conf_t *peer_conf);
static void ngx_tcp_check_http_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_ssl_hello_init(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_ssl_hello_parse(ngx_tcp_check_peer_conf_t *peer_conf);
static void ngx_tcp_check_ssl_hello_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_smtp_init(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_smtp_parse(ngx_tcp_check_peer_conf_t *peer_conf);
static void ngx_tcp_check_smtp_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_mysql_init(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_mysql_parse(ngx_tcp_check_peer_conf_t *peer_conf);
static void ngx_tcp_check_mysql_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_pop3_init(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_pop3_parse(ngx_tcp_check_peer_conf_t *peer_conf);
static void ngx_tcp_check_pop3_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_imap_init(ngx_tcp_check_peer_conf_t *peer_conf);
static ngx_int_t ngx_tcp_check_imap_parse(ngx_tcp_check_peer_conf_t *peer_conf);
static void ngx_tcp_check_imap_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
static char * ngx_tcp_upstream_check_status_set_status(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char * ngx_tcp_upstream_check_status(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
#define RANDOM "NGX_TCP_CHECK_SSL_HELLO\n\n\n\n\n"
/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
* check type of ssl_hello to ensure that the remote server speaks SSL.
*
* Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
*
* Some codes copy from HAProxy 1.4.1
*/
static const char sslv3_client_hello_pkt[] = {
"\x16" /* ContentType : 0x16 = Handshake */
"\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
"\x00\x79" /* ContentLength : 0x79 bytes after this one */
"\x01" /* HandshakeType : 0x01 = CLIENT HELLO */
"\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
"\x03\x00" /* Hello Version : 0x0300 = v3 */
"\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
RANDOM /* Random : must be exactly 28 bytes */
"\x00" /* Session ID length : empty (no session ID) */
"\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
"\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
"\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
"\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
"\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
"\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
"\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
"\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
"\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
"\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
"\x00\x38" "\x00\x39" "\x00\x3A"
"\x01" /* Compression Length : 0x01 = 1 byte for types */
"\x00" /* Compression Type : 0x00 = NULL compression */
};
#define HANDSHAKE 0x16
#define SERVER_HELLO 0x02
static check_conf_t ngx_check_types[] = {
{ NGX_TCP_CHECK_TCP,
"tcp",
ngx_null_string,
0,
ngx_tcp_check_peek_handler,
ngx_tcp_check_peek_handler,
NULL,
NULL,
NULL,
0 },
{ NGX_TCP_CHECK_HTTP,
"http",
ngx_string("GET / HTTP/1.0\r\n\r\n"),
NGX_CONF_BITMASK_SET | NGX_CHECK_HTTP_2XX | NGX_CHECK_HTTP_3XX,
ngx_tcp_check_send_handler,
ngx_tcp_check_recv_handler,
ngx_tcp_check_http_init,
ngx_tcp_check_http_parse,
ngx_tcp_check_http_reinit,
1 },
{ NGX_TCP_CHECK_SSL_HELLO,
"ssl_hello",
ngx_string(sslv3_client_hello_pkt),
0,
ngx_tcp_check_send_handler,
ngx_tcp_check_recv_handler,
ngx_tcp_check_ssl_hello_init,
ngx_tcp_check_ssl_hello_parse,
ngx_tcp_check_ssl_hello_reinit,
1 },
{ NGX_TCP_CHECK_SMTP,
"smtp",
ngx_string("HELO smtp.localdomain\r\n"),
NGX_CONF_BITMASK_SET | NGX_CHECK_SMTP_2XX,
ngx_tcp_check_send_handler,
ngx_tcp_check_recv_handler,
ngx_tcp_check_smtp_init,
ngx_tcp_check_smtp_parse,
ngx_tcp_check_smtp_reinit,
1 },
{ NGX_TCP_CHECK_MYSQL,
"mysql",
ngx_null_string,
0,
ngx_tcp_check_send_handler,
ngx_tcp_check_recv_handler,
ngx_tcp_check_mysql_init,
ngx_tcp_check_mysql_parse,
ngx_tcp_check_mysql_reinit,
1 },
{ NGX_TCP_CHECK_POP3,
"pop3",
ngx_null_string,
0,
ngx_tcp_check_send_handler,
ngx_tcp_check_recv_handler,
ngx_tcp_check_pop3_init,
ngx_tcp_check_pop3_parse,
ngx_tcp_check_pop3_reinit,
1 },
{ NGX_TCP_CHECK_IMAP,
"imap",
ngx_null_string,
0,
ngx_tcp_check_send_handler,
ngx_tcp_check_recv_handler,
ngx_tcp_check_imap_init,
ngx_tcp_check_imap_parse,
ngx_tcp_check_imap_reinit,
1 },
{0, "", ngx_null_string, 0, NULL, NULL, NULL, NULL, NULL, 0}
};
static ngx_conf_deprecated_t ngx_conf_deprecated_check_status = {
ngx_conf_deprecated, "check_status", "tcp_check_status"
};
static ngx_command_t ngx_tcp_upstream_check_status_commands[] = {
{ ngx_string("check_status"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_tcp_upstream_check_status_set_status,
0,
0,
NULL },
{ ngx_string("tcp_check_status"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_tcp_upstream_check_status,
0,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_tcp_upstream_check_status_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_tcp_upstream_check_status_module = {
NGX_MODULE_V1,
&ngx_tcp_upstream_check_status_module_ctx, /* module context */
ngx_tcp_upstream_check_status_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_tcp_check_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_uint_t ngx_tcp_check_shm_generation = 0;
static ngx_tcp_check_peers_conf_t *check_peers_ctx = NULL;
check_conf_t *
ngx_tcp_get_check_type_conf(ngx_str_t *str)
{
ngx_uint_t i;
for (i = 0; ;i++) {
if (ngx_check_types[i].type == 0) {
break;
}
if (ngx_strncmp(str->data, (u_char *)ngx_check_types[i].name,
str->len) == 0) {
return &ngx_check_types[i];
}
}
return NULL;
}
ngx_uint_t
ngx_tcp_check_add_peer(ngx_conf_t *cf, ngx_tcp_upstream_srv_conf_t *uscf,
ngx_peer_addr_t *peer, ngx_uint_t max_busy)
{
ngx_tcp_check_peer_conf_t *peer_conf;
ngx_tcp_check_peers_conf_t *peers_conf;
ngx_tcp_upstream_main_conf_t *umcf;
umcf = ngx_tcp_conf_get_module_main_conf(cf, ngx_tcp_upstream_module);
peers_conf = umcf->peers_conf;
peer_conf = ngx_array_push(&peers_conf->peers);
if (peer_conf == NULL) {
return NGX_INVALID_CHECK_INDEX;
}
ngx_memzero(peer_conf, sizeof(ngx_tcp_check_peer_conf_t));
peer_conf->index = peers_conf->peers.nelts - 1;
peer_conf->max_busy = max_busy;
peer_conf->conf = uscf;
peer_conf->peer = peer;
return peer_conf->index;
}
ngx_uint_t
ngx_tcp_check_peer_down(ngx_uint_t index)
{
ngx_tcp_check_peer_conf_t *peer_conf;
if (check_peers_ctx == NULL || index >= check_peers_ctx->peers.nelts) {
return 0;
}
peer_conf = check_peers_ctx->peers.elts;
return (peer_conf[index].shm->down ||
(peer_conf[index].shm->busyness > peer_conf[index].max_busy));
}
ngx_uint_t
ngx_tcp_check_get_peer_busyness(ngx_uint_t index)
{
ngx_tcp_check_peer_conf_t *peer_conf;
if (check_peers_ctx == NULL || index >= check_peers_ctx->peers.nelts) {
return (ngx_uint_t) (-1);
}
peer_conf = check_peers_ctx->peers.elts;
return peer_conf[index].shm->busyness;
}
void
ngx_tcp_check_get_peer(ngx_uint_t index)
{
ngx_tcp_check_peer_conf_t *peer_conf;
if (check_peers_ctx == NULL || index >= check_peers_ctx->peers.nelts) {
return;
}
peer_conf = check_peers_ctx->peers.elts;
ngx_spinlock(&peer_conf[index].shm->lock, ngx_pid, 1024);
peer_conf[index].shm->busyness++;
peer_conf[index].shm->access_count++;
ngx_spinlock_unlock(&peer_conf[index].shm->lock);
}
void
ngx_tcp_check_free_peer(ngx_uint_t index)
{
ngx_tcp_check_peer_conf_t *peer_conf;
if (check_peers_ctx == NULL || index >= check_peers_ctx->peers.nelts) {
return;
}
peer_conf = check_peers_ctx->peers.elts;
ngx_spinlock(&peer_conf[index].shm->lock, ngx_pid, 1024);
if (peer_conf[index].shm->busyness > 0) {
peer_conf[index].shm->busyness--;
}
ngx_spinlock_unlock(&peer_conf[index].shm->lock);
}
#define SHM_NAME_LEN 256
static ngx_int_t
ngx_tcp_upstream_check_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data)
{
ngx_uint_t i;
ngx_slab_pool_t *shpool;
ngx_tcp_check_peer_shm_t *peer_shm;
ngx_tcp_check_peers_shm_t *peers_shm;
ngx_tcp_check_peers_conf_t *peers_conf;
peers_conf = shm_zone->data;
if (peers_conf == NULL || peers_conf->peers.nelts == 0) {
return NGX_OK;
}
if (data) {
peers_shm = data;
} else {
shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
peers_shm = ngx_slab_alloc(shpool, sizeof(*peers_shm) +
(peers_conf->peers.nelts - 1) * sizeof(ngx_tcp_check_peer_shm_t));
if (peers_shm == NULL) {
ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
"tcp upstream check_shm_size is too small, "
"you should set a larger size.");
return NGX_ERROR;
}
}
peers_shm->generation = ngx_tcp_check_shm_generation;
for (i = 0; i < peers_conf->peers.nelts; i++) {
peer_shm = &peers_shm->peers[i];
peer_shm->owner = NGX_INVALID_PID;
peer_shm->access_time = 0;
peer_shm->access_count = 0;
peer_shm->fall_count = 0;
peer_shm->rise_count = 0;
peer_shm->busyness = 0;
peer_shm->down = 1;
}
peers_conf->peers_shm = peers_shm;
return NGX_OK;
}
static ngx_int_t
ngx_tcp_check_get_shm_name(ngx_str_t *shm_name, ngx_pool_t *pool)
{
u_char *last;
shm_name->data = ngx_palloc(pool, SHM_NAME_LEN);
if (shm_name->data == NULL) {
return NGX_ERROR;
}
last = ngx_snprintf(shm_name->data, SHM_NAME_LEN, "%s#%ui",
"ngx_tcp_upstream", ngx_tcp_check_shm_generation);
shm_name->len = last - shm_name->data;
return NGX_OK;
}
static ngx_shm_zone_t *
ngx_shared_memory_find(ngx_cycle_t *cycle, ngx_str_t *name, void *tag)
{
ngx_uint_t i;
ngx_shm_zone_t *shm_zone;
ngx_list_part_t *part;
part = (ngx_list_part_t *) & (cycle->shared_memory.part);
shm_zone = part->elts;
for (i = 0; /* void */ ; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
shm_zone = part->elts;
i = 0;
}
if (name->len != shm_zone[i].shm.name.len) {
continue;
}
if (ngx_strncmp(name->data, shm_zone[i].shm.name.data, name->len)
!= 0)
{
continue;
}
if (tag != shm_zone[i].tag) {
continue;
}
return &shm_zone[i];
}
return NULL;
}
static void
ngx_tcp_check_clean_event(ngx_tcp_check_peer_conf_t *peer_conf)
{
ngx_connection_t *c;
c = peer_conf->pc.connection;
ngx_log_debug2(NGX_LOG_DEBUG_TCP, c->log, 0,
"tcp check clean event: index:%d, fd: %d",
peer_conf->index, c->fd);
ngx_close_connection(c);
if (peer_conf->check_timeout_ev.timer_set) {
ngx_del_timer(&peer_conf->check_timeout_ev);
}
peer_conf->state = NGX_TCP_CHECK_ALL_DONE;
if (peer_conf->check_data != NULL && peer_conf->reinit) {
peer_conf->reinit(peer_conf);
}
ngx_spinlock(&peer_conf->shm->lock, ngx_pid, 1024);
peer_conf->shm->owner = NGX_INVALID_PID;
ngx_spinlock_unlock(&peer_conf->shm->lock);
}
static void
ngx_tcp_check_clear_all_events()
{
ngx_uint_t i;
static ngx_flag_t has_cleared = 0;
ngx_tcp_check_peer_shm_t *peer_shm;
ngx_tcp_check_peers_shm_t *peers_shm;
ngx_tcp_check_peer_conf_t *peer_conf;
ngx_tcp_check_peers_conf_t *peers_conf;
if (has_cleared || check_peers_ctx == NULL) {
return;
}
has_cleared = 1;
peers_conf = check_peers_ctx;
peers_shm = peers_conf->peers_shm;
peer_conf = peers_conf->peers.elts;
peer_shm = peers_shm->peers;
for (i = 0; i < peers_conf->peers.nelts; i++) {
if (peer_conf[i].check_ev.timer_set) {
ngx_del_timer(&peer_conf[i].check_ev);
}
if (peer_shm[i].owner == ngx_pid) {
ngx_tcp_check_clean_event(&peer_conf[i]);
}
if (peer_conf[i].pool != NULL) {
ngx_destroy_pool(peer_conf[i].pool);
}
}
}
static ngx_int_t
ngx_tcp_check_need_exit()
{
if (ngx_terminate || ngx_exiting || ngx_quit) {
ngx_tcp_check_clear_all_events();
return 1;
}
return 0;
}
static void
ngx_tcp_check_finish_handler(ngx_event_t *event)
{
if (ngx_tcp_check_need_exit()) {
return;
}
}
static void
ngx_tcp_check_status_update(ngx_tcp_check_peer_conf_t *peer_conf,
ngx_int_t result)
{
ngx_tcp_upstream_srv_conf_t *uscf;
uscf = peer_conf->conf;
if (result) {
peer_conf->shm->rise_count++;
peer_conf->shm->fall_count = 0;
if (peer_conf->shm->down
&& (peer_conf->shm->rise_count >= uscf->rise_count)) {
peer_conf->shm->down = 0;
}
} else {
peer_conf->shm->rise_count = 0;
peer_conf->shm->fall_count++;
if (!peer_conf->shm->down
&& (peer_conf->shm->fall_count >= uscf->fall_count)) {
peer_conf->shm->down = 1;
}
}
peer_conf->shm->access_time = ngx_current_msec;
}
static void
ngx_tcp_check_timeout_handler(ngx_event_t *event)
{
ngx_tcp_check_peer_conf_t *peer_conf;
if (ngx_tcp_check_need_exit()) {
return;
}
peer_conf = event->data;
ngx_log_error(NGX_LOG_ERR, event->log, 0,
"check time out with peer: %V ", &peer_conf->peer->name);
ngx_tcp_check_status_update(peer_conf, 0);
ngx_tcp_check_clean_event(peer_conf);
}
static void
ngx_tcp_check_peek_handler(ngx_event_t *event)
{
char buf[1];
ngx_int_t n;
ngx_err_t err;
ngx_connection_t *c;
ngx_tcp_check_peer_conf_t *peer_conf;
if (ngx_tcp_check_need_exit()) {
return;
}
c = event->data;
peer_conf = c->data;
n = recv(c->fd, buf, 1, MSG_PEEK);
err = ngx_socket_errno;
ngx_log_debug2(NGX_LOG_DEBUG_TCP, c->log, err,
"tcp check upstream recv(): %d, fd: %d",
n, c->fd);
if (n >= 0 || err == NGX_EAGAIN) {
ngx_tcp_check_status_update(peer_conf, 1);
} else {
c->error = 1;
ngx_tcp_check_status_update(peer_conf, 0);
}
ngx_tcp_check_clean_event(peer_conf);
/* dummy */
ngx_tcp_check_finish_handler(event);
}
void
http_field(void *data, const signed char *field,
size_t flen, const signed char *value, size_t vlen)
{
#if (NGX_DEBUG)
ngx_str_t str_field, str_value;
str_field.data = (u_char *) field;
str_field.len = flen;
str_value.data = (u_char *) value;
str_value.len = vlen;
ngx_log_debug2(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"%V: %V", &str_field, &str_value);
#endif
}
void
http_version(void *data, const signed char *at, size_t length)
{
#if (NGX_DEBUG)
ngx_str_t str;
str.data = (u_char *) at;
str.len = length;
ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"VERSION: \"%V\"", &str);
#endif
}
void
status_code(void *data, const signed char *at, size_t length)
{
int code;
ngx_tcp_check_ctx *ctx;
http_response_parser *hp;
ngx_tcp_check_peer_conf_t *peer_conf = data;
#if (NGX_DEBUG)
ngx_str_t str;
str.data = (u_char *) at;
str.len = length;
ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"STATUS_CODE: \"%V\"", &str);
#endif
ctx = peer_conf->check_data;
hp = ctx->parser;
code = ngx_atoi((u_char*)at, length);
if (code >= 200 && code < 300) {
hp->status_code_n = NGX_CHECK_HTTP_2XX;
} else if (code >= 300 && code < 400) {
hp->status_code_n = NGX_CHECK_HTTP_3XX;
} else if (code >= 400 && code < 500) {
hp->status_code_n = NGX_CHECK_HTTP_4XX;
} else if (code >= 500 && code < 600) {
hp->status_code_n = NGX_CHECK_HTTP_5XX;
} else {
hp->status_code_n = NGX_CHECK_HTTP_ERR;
}
}
void
reason_phrase(void *data, const signed char *at, size_t length)
{
#if (NGX_DEBUG)
ngx_str_t str;
str.data = (u_char *) at;
str.len = length;
ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"REASON_PHRASE: \"%V\"", &str);
#endif
}
void
header_done(void *data, const signed char *at, size_t length)
{
}
static void
check_http_response_parser_init(http_response_parser *hp,
void *data)
{
hp->data = data;
hp->http_field = http_field;
hp->http_version = http_version;
hp->status_code = status_code;
hp->status_code_n = 0;
hp->reason_phrase = reason_phrase;
hp->header_done = header_done;
http_response_parser_init(hp);
}
static ngx_int_t
ngx_tcp_check_http_init(ngx_tcp_check_peer_conf_t *peer_conf)
{
ngx_tcp_check_ctx *ctx;
ngx_tcp_upstream_srv_conf_t *uscf;
ctx = peer_conf->check_data;
uscf = peer_conf->conf;
ctx->send.start = ctx->send.pos = (u_char *)uscf->send.data;
ctx->send.end = ctx->send.last = ctx->send.start + uscf->send.len;
ctx->recv.start = ctx->recv.pos = NULL;
ctx->recv.end = ctx->recv.last = NULL;
ctx->parser = ngx_pcalloc(peer_conf->pool, sizeof(http_response_parser));
if (ctx->parser == NULL) {
return NGX_ERROR;
}
check_http_response_parser_init(ctx->parser, peer_conf);
return NGX_OK;
}
static ngx_int_t
ngx_tcp_check_http_parse(ngx_tcp_check_peer_conf_t *peer_conf)
{
ssize_t n, offset, length;
ngx_tcp_check_ctx *ctx;
http_response_parser *hp;
ngx_tcp_upstream_srv_conf_t *uscf;
uscf = peer_conf->conf;
ctx = peer_conf->check_data;
hp = ctx->parser;
if ((ctx->recv.last - ctx->recv.pos) > 0) {
offset = ctx->recv.pos - ctx->recv.start;
length = ctx->recv.last - ctx->recv.start;
n = http_response_parser_execute(hp, (signed char *)ctx->recv.start,
length, offset);
ctx->recv.pos = ctx->recv.start + n;
if (http_response_parser_finish(hp) == -1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
"http parse error with peer: %V, recv data: %s",
&peer_conf->peer->name, ctx->recv.start);
return NGX_ERROR;
}
ngx_log_debug2(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"http_parse: hp->status_code_n: %d, conf: %d",
hp->status_code_n, uscf->code.status_alive);
if (hp->status_code_n == 0) {
return NGX_AGAIN;
} else if (hp->status_code_n & uscf->code.status_alive) {
return NGX_OK;
} else {
return NGX_ERROR;
}
} else {
return NGX_AGAIN;
}
return NGX_OK;
}
static void
ngx_tcp_check_http_reinit(ngx_tcp_check_peer_conf_t *peer_conf)
{
ngx_tcp_check_ctx *ctx;
ctx = peer_conf->check_data;
ctx->send.pos = ctx->send.start;
ctx->send.last = ctx->send.end;
ctx->recv.pos = ctx->recv.last = ctx->recv.start;
check_http_response_parser_init(ctx->parser, peer_conf);
}
static ngx_int_t
ngx_tcp_check_ssl_hello_init(ngx_tcp_check_peer_conf_t *peer_conf)
{
ngx_tcp_check_ctx *ctx;
ngx_tcp_upstream_srv_conf_t *uscf;
ctx = peer_conf->check_data;
uscf = peer_conf->conf;
ctx->send.start = ctx->send.pos = (u_char *)uscf->send.data;
ctx->send.end = ctx->send.last = ctx->send.start + uscf->send.len;
ctx->recv.start = ctx->recv.pos = NULL;
ctx->recv.end = ctx->recv.last = NULL;
return NGX_OK;
}
/* a rough check of server ssl_hello responses */
static ngx_int_t
ngx_tcp_check_ssl_hello_parse(ngx_tcp_check_peer_conf_t *peer_conf)
{
size_t size;
server_ssl_hello_t *resp;
ngx_tcp_check_ctx *ctx;
ctx = peer_conf->check_data;
size = ctx->recv.last - ctx->recv.pos;
if (size < sizeof(server_ssl_hello_t)) {
return NGX_AGAIN;
}
resp = (server_ssl_hello_t *) ctx->recv.pos;
ngx_log_debug7(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"tcp check ssl_parse, type: %d, version: %d.%d, "
"length: %d, handshake_type: %d, "
"hello_version: %d.%d",
resp->msg_type, resp->version.major, resp->version.minor,
ntohs(resp->length), resp->handshake_type,
resp->hello_version.major, resp->hello_version.minor);
if (resp->msg_type != HANDSHAKE) {
return NGX_ERROR;
}
if (resp->handshake_type != SERVER_HELLO) {
return NGX_ERROR;
}
return NGX_OK;
}
static void
ngx_tcp_check_ssl_hello_reinit(ngx_tcp_check_peer_conf_t *peer_conf)
{
ngx_tcp_check_ctx *ctx;
ctx = peer_conf->check_data;
ctx->send.pos = ctx->send.start;
ctx->send.last = ctx->send.end;
ctx->recv.pos = ctx->recv.last = ctx->recv.start;
}
static void
domain(void *data, const signed char *at, size_t length)
{
#if (NGX_DEBUG)
ngx_str_t str;
str.data = (u_char *) at;
str.len = length;
ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"DOMAIN: \"%V\"", &str);
#endif
}
static void
greeting_text(void *data, const signed char *at, size_t length)
{
#if (NGX_DEBUG)
ngx_str_t str;
str.data = (u_char *) at;
str.len = length;
ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"GREETING_TEXT: \"%V\"", &str);
#endif
}
static void
reply_code(void *data, const signed char *at, size_t length)
{
int code;
smtp_parser *sp;
ngx_tcp_check_ctx *ctx;
ngx_tcp_check_peer_conf_t *peer_conf = data;
#if (NGX_DEBUG)
ngx_str_t str;
str.data = (u_char *) at;
str.len = length;
ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"REPLY_CODE: \"%V\"", &str);
#endif
ctx = peer_conf->check_data;
sp = ctx->parser;
code = ngx_atoi((u_char*)at, length);
if (code >= 200 && code < 300) {
sp->hello_reply_code = NGX_CHECK_SMTP_2XX;
} else if (code >= 300 && code < 400) {
sp->hello_reply_code = NGX_CHECK_SMTP_3XX;
} else if (code >= 400 && code < 500) {
sp->hello_reply_code = NGX_CHECK_SMTP_4XX;
} else if (code >= 500 && code < 600) {
sp->hello_reply_code = NGX_CHECK_SMTP_5XX;
} else {
sp->hello_reply_code = NGX_CHECK_SMTP_ERR;
}
}
static void
reply_text(void *data, const signed char *at, size_t length)
{
#if (NGX_DEBUG)
ngx_str_t str;
str.data = (u_char *) at;
str.len = length;
ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"REPLY_TEXT: \"%V\"", &str);
#endif