-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathngx_stream_lua_socket_tcp.c
5073 lines (3785 loc) · 136 KB
/
ngx_stream_lua_socket_tcp.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
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_stream_lua_socket_tcp.h"
#include "ngx_stream_lua_util.h"
#include "ngx_stream_lua_uthread.h"
#include "ngx_stream_lua_contentby.h"
#include "ngx_stream_lua_output.h"
static int ngx_stream_lua_socket_tcp(lua_State *L);
static int ngx_stream_lua_socket_tcp_connect(lua_State *L);
#if (NGX_STREAM_SSL)
static int ngx_stream_lua_socket_tcp_sslhandshake(lua_State *L);
#endif
static int ngx_stream_lua_socket_tcp_receive(lua_State *L);
static int ngx_stream_lua_socket_tcp_send(lua_State *L);
static int ngx_stream_lua_socket_tcp_close(lua_State *L);
static int ngx_stream_lua_socket_tcp_setoption(lua_State *L);
static int ngx_stream_lua_socket_tcp_settimeout(lua_State *L);
static void ngx_stream_lua_socket_tcp_handler(ngx_event_t *ev);
static ngx_int_t ngx_stream_lua_socket_tcp_get_peer(ngx_peer_connection_t *pc,
void *data);
static void ngx_stream_lua_socket_read_handler(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_send_handler(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_connected_handler(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_tcp_cleanup(void *data);
static void ngx_stream_lua_socket_tcp_finalize(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_tcp_finalize_read_part(
ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_tcp_finalize_write_part(
ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u);
static ngx_int_t ngx_stream_lua_socket_send(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static ngx_int_t ngx_stream_lua_socket_test_connect(ngx_stream_session_t *s,
ngx_connection_t *c);
static void ngx_stream_lua_socket_handle_conn_error(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, ngx_uint_t ft_type);
static void ngx_stream_lua_socket_handle_read_error(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, ngx_uint_t ft_type);
static void ngx_stream_lua_socket_handle_write_error(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, ngx_uint_t ft_type);
static void ngx_stream_lua_socket_handle_conn_success(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_handle_read_success(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_handle_write_success(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static int ngx_stream_lua_socket_tcp_send_retval_handler(
ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u,
lua_State *L);
static int ngx_stream_lua_socket_tcp_conn_retval_handler(
ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u,
lua_State *L);
static void ngx_stream_lua_socket_dummy_handler(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static ngx_int_t ngx_stream_lua_socket_tcp_read(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_read_handler(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static int ngx_stream_lua_socket_tcp_receive_retval_handler(
ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u,
lua_State *L);
static ngx_int_t ngx_stream_lua_socket_read_line(void *data, ssize_t bytes);
static void ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx);
static int ngx_stream_lua_socket_resolve_retval_handler(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, lua_State *L);
static int ngx_stream_lua_socket_conn_error_retval_handler(
ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u,
lua_State *L);
static int ngx_stream_lua_socket_read_error_retval_handler(
ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u,
lua_State *L);
static int ngx_stream_lua_socket_write_error_retval_handler(
ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u,
lua_State *L);
static ngx_int_t ngx_stream_lua_socket_read_all(void *data, ssize_t bytes);
static ngx_int_t ngx_stream_lua_socket_read_until(void *data, ssize_t bytes);
static ngx_int_t ngx_stream_lua_socket_read_chunk(void *data, ssize_t bytes);
static int ngx_stream_lua_socket_tcp_receiveuntil(lua_State *L);
static int ngx_stream_lua_socket_receiveuntil_iterator(lua_State *L);
static ngx_int_t ngx_stream_lua_socket_compile_pattern(u_char *data, size_t len,
ngx_stream_lua_socket_compiled_pattern_t *cp, ngx_log_t *log);
static int ngx_stream_lua_socket_cleanup_compiled_pattern(lua_State *L);
static int ngx_stream_lua_tcp_req_socket(lua_State *L);
static void ngx_stream_lua_req_socket_rev_handler(ngx_stream_session_t *s,
ngx_stream_lua_ctx_t *ctx);
static int ngx_stream_lua_socket_tcp_getreusedtimes(lua_State *L);
static int ngx_stream_lua_socket_tcp_setkeepalive(lua_State *L);
static ngx_int_t ngx_stream_lua_get_keepalive_peer(ngx_stream_session_t *s,
lua_State *L, int key_index,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_keepalive_dummy_handler(ngx_event_t *ev);
static ngx_int_t ngx_stream_lua_socket_keepalive_close_handler(ngx_event_t *ev);
static void ngx_stream_lua_socket_keepalive_rev_handler(ngx_event_t *ev);
static void ngx_stream_lua_socket_free_pool(ngx_log_t *log,
ngx_stream_lua_socket_pool_t *spool);
static int ngx_stream_lua_socket_tcp_upstream_destroy(lua_State *L);
static int ngx_stream_lua_socket_downstream_destroy(lua_State *L);
static ngx_int_t ngx_stream_lua_socket_push_input_data(ngx_stream_session_t *s,
ngx_stream_lua_ctx_t *ctx, ngx_stream_lua_socket_tcp_upstream_t *u,
lua_State *L);
static ngx_int_t ngx_stream_lua_socket_add_pending_data(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, u_char *pos, size_t len,
u_char *pat, int prefix, int old_state);
static ngx_int_t ngx_stream_lua_socket_add_input_buffer(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u);
static ngx_int_t ngx_stream_lua_socket_insert_buffer(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, u_char *pat, size_t prefix);
static ngx_int_t ngx_stream_lua_socket_tcp_conn_resume(ngx_stream_session_t *s,
ngx_stream_lua_ctx_t *ctx);
static ngx_int_t ngx_stream_lua_socket_tcp_read_resume(ngx_stream_session_t *s,
ngx_stream_lua_ctx_t *ctx);
static ngx_int_t ngx_stream_lua_socket_tcp_write_resume(ngx_stream_session_t *s,
ngx_stream_lua_ctx_t *ctx);
static ngx_int_t ngx_stream_lua_socket_tcp_resume_helper(
ngx_stream_session_t *s, ngx_stream_lua_ctx_t *ctx, int socket_op);
static void ngx_stream_lua_tcp_resolve_cleanup(ngx_stream_lua_co_ctx_t *data);
static void ngx_stream_lua_coctx_cleanup(ngx_stream_lua_co_ctx_t *coctx);
static int ngx_stream_lua_socket_shutdown_pool(lua_State *L);
static void
ngx_stream_lua_socket_empty_resolve_handler(ngx_resolver_ctx_t *ctx);
static int ngx_stream_lua_socket_prepare_error_retvals(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, lua_State *L, ngx_uint_t ft_type);
#if (NGX_STREAM_SSL)
static int ngx_stream_lua_ssl_handshake_retval_handler(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, lua_State *L);
static void ngx_stream_lua_ssl_handshake_handler(ngx_connection_t *c);
static int ngx_stream_lua_ssl_free_session(lua_State *L);
#endif
static void ngx_stream_lua_socket_tcp_close_connection(ngx_connection_t *c);
enum {
SOCKET_CTX_INDEX = 1,
SOCKET_TIMEOUT_INDEX = 2,
SOCKET_KEY_INDEX = 3
};
enum {
SOCKET_OP_CONNECT,
SOCKET_OP_READ,
SOCKET_OP_WRITE
};
#define ngx_stream_lua_socket_check_busy_connecting(s, u, L) \
if ((u)->conn_waiting) { \
lua_pushnil(L); \
lua_pushliteral(L, "socket busy connecting"); \
return 2; \
}
#define ngx_stream_lua_socket_check_busy_reading(s, u, L) \
if ((u)->read_waiting) { \
lua_pushnil(L); \
lua_pushliteral(L, "socket busy reading"); \
return 2; \
}
#define ngx_stream_lua_socket_check_busy_writing(s, u, L) \
if ((u)->write_waiting) { \
lua_pushnil(L); \
lua_pushliteral(L, "socket busy writing"); \
return 2; \
} \
if ((u)->raw_downstream \
&& ((s)->connection->buffered)) \
{ \
lua_pushnil(L); \
lua_pushliteral(L, "socket busy writing"); \
return 2; \
}
#if 0
static char ngx_stream_lua_req_socket_metatable_key;
#endif
static char ngx_stream_lua_raw_req_socket_metatable_key;
static char ngx_stream_lua_socket_tcp_metatable_key;
static char ngx_stream_lua_upstream_udata_metatable_key;
static char ngx_stream_lua_downstream_udata_metatable_key;
static char ngx_stream_lua_pool_udata_metatable_key;
static char ngx_stream_lua_pattern_udata_metatable_key;
#if (NGX_STREAM_SSL)
static char ngx_stream_lua_ssl_session_metatable_key;
#endif
void
ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
{
ngx_int_t rc;
lua_createtable(L, 0, 3 /* nrec */); /* ngx.socket */
lua_pushcfunction(L, ngx_stream_lua_socket_tcp);
lua_setfield(L, -2, "tcp");
{
const char buf[] = "local sock = ngx.socket.tcp()"
" local ok, err = sock:connect(...)"
" if ok then return sock else return nil, err end";
rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "=ngx.socket.connect");
}
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_CRIT, log, 0,
"failed to load Lua code for ngx.socket.connect(): %i",
rc);
} else {
lua_setfield(L, -2, "connect");
}
lua_setfield(L, -2, "socket");
#if 0
/* {{{req socket object metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_req_socket_metatable_key);
lua_createtable(L, 0 /* narr */, 4 /* nrec */);
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_receive);
lua_setfield(L, -2, "receive");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_receiveuntil);
lua_setfield(L, -2, "receiveuntil");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_settimeout);
lua_setfield(L, -2, "settimeout"); /* ngx socket mt */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
#endif
/* {{{raw req socket object metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_raw_req_socket_metatable_key);
lua_createtable(L, 0 /* narr */, 5 /* nrec */);
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_receive);
lua_setfield(L, -2, "receive");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_receiveuntil);
lua_setfield(L, -2, "receiveuntil");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_send);
lua_setfield(L, -2, "send");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_settimeout);
lua_setfield(L, -2, "settimeout"); /* ngx socket mt */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
/* {{{tcp object metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_socket_tcp_metatable_key);
lua_createtable(L, 0 /* narr */, 11 /* nrec */);
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_connect);
lua_setfield(L, -2, "connect");
#if (NGX_STREAM_SSL)
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_sslhandshake);
lua_setfield(L, -2, "sslhandshake");
#endif
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_receive);
lua_setfield(L, -2, "receive");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_receiveuntil);
lua_setfield(L, -2, "receiveuntil");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_send);
lua_setfield(L, -2, "send");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_close);
lua_setfield(L, -2, "close");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_setoption);
lua_setfield(L, -2, "setoption");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_settimeout);
lua_setfield(L, -2, "settimeout"); /* ngx socket mt */
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_getreusedtimes);
lua_setfield(L, -2, "getreusedtimes");
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_setkeepalive);
lua_setfield(L, -2, "setkeepalive");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
/* {{{upstream userdata metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_upstream_udata_metatable_key);
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_upstream_destroy);
lua_setfield(L, -2, "__gc");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
/* {{{downstream userdata metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_downstream_udata_metatable_key);
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
lua_pushcfunction(L, ngx_stream_lua_socket_downstream_destroy);
lua_setfield(L, -2, "__gc");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
/* {{{socket pool userdata metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_pool_udata_metatable_key);
lua_createtable(L, 0, 1); /* metatable */
lua_pushcfunction(L, ngx_stream_lua_socket_shutdown_pool);
lua_setfield(L, -2, "__gc");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
/* {{{socket compiled pattern userdata metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_pattern_udata_metatable_key);
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
lua_pushcfunction(L, ngx_stream_lua_socket_cleanup_compiled_pattern);
lua_setfield(L, -2, "__gc");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
#if (NGX_STREAM_SSL)
/* {{{ssl session userdata metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_ssl_session_metatable_key);
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
lua_pushcfunction(L, ngx_stream_lua_ssl_free_session);
lua_setfield(L, -2, "__gc");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
#endif
}
static int
ngx_stream_lua_socket_tcp(lua_State *L)
{
ngx_stream_session_t *s;
ngx_stream_lua_ctx_t *ctx;
if (lua_gettop(L) != 0) {
return luaL_error(L, "expecting zero arguments, but got %d",
lua_gettop(L));
}
s = ngx_stream_lua_get_session(L);
if (s == NULL) {
return luaL_error(L, "no request found");
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}
ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER);
lua_createtable(L, 3 /* narr */, 1 /* nrec */);
lua_pushlightuserdata(L, &ngx_stream_lua_socket_tcp_metatable_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
dd("top: %d", lua_gettop(L));
return 1;
}
static int
ngx_stream_lua_socket_tcp_connect(lua_State *L)
{
ngx_stream_session_t *s;
ngx_stream_lua_ctx_t *ctx;
ngx_str_t host;
int port;
ngx_resolver_ctx_t *rctx, temp;
int saved_top;
int n;
u_char *p;
size_t len;
ngx_url_t url;
ngx_int_t rc;
ngx_stream_lua_srv_conf_t *lscf;
ngx_peer_connection_t *pc;
int timeout;
unsigned custom_pool;
int key_index;
const char *msg;
ngx_stream_lua_co_ctx_t *coctx;
ngx_stream_lua_socket_tcp_upstream_t *u;
n = lua_gettop(L);
if (n != 2 && n != 3 && n != 4) {
return luaL_error(L, "ngx.socket connect: expecting 2, 3, or 4 "
"arguments (including the object), but seen %d", n);
}
s = ngx_stream_lua_get_session(L);
if (s == NULL) {
return luaL_error(L, "no request found");
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}
ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER);
luaL_checktype(L, 1, LUA_TTABLE);
p = (u_char *) luaL_checklstring(L, 2, &len);
key_index = 2;
custom_pool = 0;
if (lua_type(L, n) == LUA_TTABLE) {
/* found the last optional option table */
lua_getfield(L, n, "pool");
switch (lua_type(L, -1)) {
case LUA_TNUMBER:
lua_tostring(L, -1);
case LUA_TSTRING:
custom_pool = 1;
lua_pushvalue(L, -1);
lua_rawseti(L, 1, SOCKET_KEY_INDEX);
key_index = n + 1;
break;
case LUA_TNIL:
lua_pop(L, 2);
break;
default:
msg = lua_pushfstring(L, "bad \"pool\" option type: %s",
luaL_typename(L, -1));
luaL_argerror(L, n, msg);
break;
}
n--;
}
if (n == 3) {
port = luaL_checkinteger(L, 3);
if (port < 0 || port > 65536) {
lua_pushnil(L);
lua_pushfstring(L, "bad port number: %d", port);
return 2;
}
if (!custom_pool) {
lua_pushliteral(L, ":");
lua_insert(L, 3);
lua_concat(L, 3);
}
dd("socket key: %s", lua_tostring(L, -1));
} else { /* n == 2 */
port = 0;
}
if (!custom_pool) {
/* the key's index is 2 */
lua_pushvalue(L, 2);
lua_rawseti(L, 1, SOCKET_KEY_INDEX);
}
lua_rawgeti(L, 1, SOCKET_CTX_INDEX);
u = lua_touserdata(L, -1);
lua_pop(L, 1);
if (u) {
if (u->request && u->request != s) {
return luaL_error(L, "bad request");
}
ngx_stream_lua_socket_check_busy_connecting(s, u, L);
ngx_stream_lua_socket_check_busy_reading(s, u, L);
ngx_stream_lua_socket_check_busy_writing(s, u, L);
if (u->raw_downstream) {
return luaL_error(L, "attempt to re-connect a request socket");
}
if (u->peer.connection) {
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua tcp socket reconnect without shutting "
"down");
ngx_stream_lua_socket_tcp_finalize(s, u);
}
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua reuse socket upstream ctx");
} else {
u = lua_newuserdata(L, sizeof(ngx_stream_lua_socket_tcp_upstream_t));
if (u == NULL) {
return luaL_error(L, "no memory");
}
#if 1
lua_pushlightuserdata(L, &ngx_stream_lua_upstream_udata_metatable_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
#endif
lua_rawseti(L, 1, SOCKET_CTX_INDEX);
}
ngx_memzero(u, sizeof(ngx_stream_lua_socket_tcp_upstream_t));
coctx = ctx->cur_co_ctx;
u->request = s; /* set the controlling request */
lscf = ngx_stream_get_module_srv_conf(s, ngx_stream_lua_module);
u->conf = lscf;
pc = &u->peer;
pc->log = s->connection->log;
pc->log_error = NGX_ERROR_ERR;
dd("lua peer connection log: %p", pc->log);
lua_rawgeti(L, 1, SOCKET_TIMEOUT_INDEX);
timeout = (ngx_int_t) lua_tointeger(L, -1);
lua_pop(L, 1);
if (timeout > 0) {
u->send_timeout = (ngx_msec_t) timeout;
u->read_timeout = (ngx_msec_t) timeout;
u->connect_timeout = (ngx_msec_t) timeout;
} else {
u->read_timeout = u->conf->read_timeout;
u->send_timeout = u->conf->send_timeout;
u->connect_timeout = u->conf->connect_timeout;
}
rc = ngx_stream_lua_get_keepalive_peer(s, L, key_index, u);
if (rc == NGX_OK) {
lua_pushinteger(L, 1);
return 1;
}
if (rc == NGX_ERROR) {
lua_pushnil(L);
lua_pushliteral(L, "error in get keepalive peer");
return 2;
}
/* rc == NGX_DECLINED */
/* TODO: we should avoid this in-pool allocation */
host.data = ngx_palloc(s->connection->pool, len + 1);
if (host.data == NULL) {
return luaL_error(L, "no memory");
}
host.len = len;
ngx_memcpy(host.data, p, len);
host.data[len] = '\0';
ngx_memzero(&url, sizeof(ngx_url_t));
url.url.len = host.len;
url.url.data = host.data;
url.default_port = (in_port_t) port;
url.no_resolve = 1;
if (ngx_parse_url(s->connection->pool, &url) != NGX_OK) {
lua_pushnil(L);
if (url.err) {
lua_pushfstring(L, "failed to parse host name \"%s\": %s",
host.data, url.err);
} else {
lua_pushfstring(L, "failed to parse host name \"%s\"", host.data);
}
return 2;
}
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua tcp socket connect timeout: %M",
u->connect_timeout);
u->resolved = ngx_pcalloc(s->connection->pool,
sizeof(ngx_stream_lua_resolved_t));
if (u->resolved == NULL) {
return luaL_error(L, "no memory");
}
if (url.addrs && url.addrs[0].sockaddr) {
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua tcp socket network address given directly");
u->resolved->sockaddr = url.addrs[0].sockaddr;
u->resolved->socklen = url.addrs[0].socklen;
u->resolved->naddrs = 1;
u->resolved->host = url.addrs[0].name;
} else {
u->resolved->host = host;
u->resolved->port = (in_port_t) port;
}
if (u->resolved->sockaddr) {
rc = ngx_stream_lua_socket_resolve_retval_handler(s, u, L);
if (rc == NGX_AGAIN) {
return lua_yield(L, 0);
}
return rc;
}
temp.name = host;
rctx = ngx_resolve_start(lscf->resolver, &temp);
if (rctx == NULL) {
u->ft_type |= NGX_STREAM_LUA_SOCKET_FT_RESOLVER;
lua_pushnil(L);
lua_pushliteral(L, "failed to start the resolver");
return 2;
}
if (rctx == NGX_NO_RESOLVER) {
u->ft_type |= NGX_STREAM_LUA_SOCKET_FT_RESOLVER;
lua_pushnil(L);
lua_pushfstring(L, "no lua_resolver defined to resolve \"%s\"",
host.data);
return 2;
}
rctx->name = host;
#if !defined(nginx_version) || nginx_version < 1005008
rctx->type = NGX_RESOLVE_A;
#endif
rctx->handler = ngx_stream_lua_socket_resolve_handler;
rctx->data = u;
rctx->timeout = lscf->resolver_timeout;
u->resolved->ctx = rctx;
u->write_co_ctx = ctx->cur_co_ctx;
ngx_stream_lua_cleanup_pending_operation(coctx);
coctx->cleanup = ngx_stream_lua_tcp_resolve_cleanup;
coctx->data = u;
saved_top = lua_gettop(L);
if (ngx_resolve_name(rctx) != NGX_OK) {
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua tcp socket fail to run resolver "
"immediately");
u->ft_type |= NGX_STREAM_LUA_SOCKET_FT_RESOLVER;
u->resolved->ctx = NULL;
lua_pushnil(L);
lua_pushfstring(L, "%s could not be resolved", host.data);
return 2;
}
if (u->conn_waiting) {
dd("resolved and already connecting");
return lua_yield(L, 0);
}
n = lua_gettop(L) - saved_top;
if (n) {
dd("errors occurred during resolving or connecting"
"or already connected");
return n;
}
/* still resolving */
u->conn_waiting = 1;
u->write_prepare_retvals = ngx_stream_lua_socket_resolve_retval_handler;
dd("setting data to %p", u);
ctx->write_event_handler = ngx_stream_lua_content_wev_handler;
return lua_yield(L, 0);
}
static void
ngx_stream_lua_socket_empty_resolve_handler(ngx_resolver_ctx_t *ctx)
{
/* do nothing */
}
static void
ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
{
ngx_stream_session_t *s;
ngx_stream_lua_resolved_t *ur;
ngx_stream_lua_ctx_t *lctx;
lua_State *L;
ngx_stream_lua_socket_tcp_upstream_t *u;
u_char *p;
size_t len;
#if defined(nginx_version) && nginx_version >= 1005008
socklen_t socklen;
struct sockaddr *sockaddr;
#else
struct sockaddr_in *sin;
#endif
ngx_uint_t i;
unsigned waiting;
u = ctx->data;
s = u->request;
ur = u->resolved;
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua tcp socket resolve handler");
lctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
if (lctx == NULL) {
return;
}
lctx->cur_co_ctx = u->write_co_ctx;
u->write_co_ctx->cleanup = NULL;
L = lctx->cur_co_ctx->co;
waiting = u->conn_waiting;
if (ctx->state) {
ngx_log_debug2(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua tcp socket resolver error: %s "
"(connect waiting: %d)",
ngx_resolver_strerror(ctx->state), (int) waiting);
lua_pushnil(L);
lua_pushlstring(L, (char *) ctx->name.data, ctx->name.len);
lua_pushfstring(L, " could not be resolved (%d: %s)",
(int) ctx->state,
ngx_resolver_strerror(ctx->state));
lua_concat(L, 2);
u->write_prepare_retvals =
ngx_stream_lua_socket_conn_error_retval_handler;
ngx_stream_lua_socket_handle_conn_error(s, u,
NGX_STREAM_LUA_SOCKET_FT_RESOLVER);
return;
}
ur->naddrs = ctx->naddrs;
ur->addrs = ctx->addrs;
#if (NGX_DEBUG)
{
# if defined(nginx_version) && nginx_version >= 1005008
u_char text[NGX_SOCKADDR_STRLEN];
ngx_str_t addr;
# else
in_addr_t addr;
# endif
ngx_uint_t i;
# if defined(nginx_version) && nginx_version >= 1005008
addr.data = text;
for (i = 0; i < ctx->naddrs; i++) {
addr.len = ngx_sock_ntop(ur->addrs[i].sockaddr, ur->addrs[i].socklen,
text, NGX_SOCKADDR_STRLEN, 0);
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"name was resolved to %V", &addr);
}
# else
for (i = 0; i < ctx->naddrs; i++) {
dd("addr i: %d %p", (int) i, &ctx->addrs[i]);
addr = ntohl(ctx->addrs[i]);
ngx_log_debug4(NGX_LOG_DEBUG_STREAM, c->log, 0,
"name was resolved to %ud.%ud.%ud.%ud",
(addr >> 24) & 0xff, (addr >> 16) & 0xff,
(addr >> 8) & 0xff, addr & 0xff);
}
# endif
}
#endif
ngx_stream_lua_assert(ur->naddrs > 0);
if (ur->naddrs == 1) {
i = 0;
} else {
i = ngx_random() % ur->naddrs;
}
dd("selected addr index: %d", (int) i);
#if defined(nginx_version) && nginx_version >= 1005008
socklen = ur->addrs[i].socklen;
sockaddr = ngx_palloc(s->connection->pool, socklen);
if (sockaddr == NULL) {
goto nomem;
}
ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
switch (sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
((struct sockaddr_in6 *) sockaddr)->sin6_port = htons(ur->port);
break;
#endif
default: /* AF_INET */
((struct sockaddr_in *) sockaddr)->sin_port = htons(ur->port);
}
p = ngx_pnalloc(s->connection->pool, NGX_SOCKADDR_STRLEN);
if (p == NULL) {
goto nomem;
}
len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
ur->sockaddr = sockaddr;
ur->socklen = socklen;
#else
/* for nginx older than 1.5.8 */
len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
p = ngx_pnalloc(s->connection->pool, len + sizeof(struct sockaddr_in));
if (p == NULL) {
goto nomem;
}
sin = (struct sockaddr_in *) &p[len];
ngx_memzero(sin, sizeof(struct sockaddr_in));
len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN);
len = ngx_sprintf(&p[len], ":%d", ur->port) - p;
sin->sin_family = AF_INET;
sin->sin_port = htons(ur->port);
sin->sin_addr.s_addr = ur->addrs[i];
ur->sockaddr = (struct sockaddr *) sin;
ur->socklen = sizeof(struct sockaddr_in);
#endif
ur->host.data = p;
ur->host.len = len;
ur->naddrs = 1;
ngx_resolve_name_done(ctx);
ur->ctx = NULL;
u->conn_waiting = 0;
u->write_co_ctx = NULL;
if (waiting) {
lctx->resume_handler = ngx_stream_lua_socket_tcp_conn_resume;
lctx->write_event_handler(s, lctx);
} else {
(void) ngx_stream_lua_socket_resolve_retval_handler(s, u, L);
}
return;
nomem:
if (ur->ctx) {
ngx_resolve_name_done(ctx);
ur->ctx = NULL;
}
u->write_prepare_retvals = ngx_stream_lua_socket_conn_error_retval_handler;
ngx_stream_lua_socket_handle_conn_error(s, u,
NGX_STREAM_LUA_SOCKET_FT_NOMEM);
if (waiting) {
dd("run posted requests");
} else {
lua_pushnil(L);
lua_pushliteral(L, "no memory");
}
}
static int
ngx_stream_lua_socket_resolve_retval_handler(ngx_stream_session_t *s,
ngx_stream_lua_socket_tcp_upstream_t *u, lua_State *L)
{
ngx_stream_lua_ctx_t *ctx;
ngx_peer_connection_t *pc;
ngx_connection_t *c;
ngx_stream_lua_cleanup_t *cln;
ngx_stream_lua_resolved_t *ur;
ngx_int_t rc;
ngx_stream_lua_co_ctx_t *coctx;
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua tcp socket resolve retval handler");
if (u->ft_type & NGX_STREAM_LUA_SOCKET_FT_RESOLVER) {
return 2;
}
pc = &u->peer;
ur = u->resolved;
if (ur->sockaddr) {
pc->sockaddr = ur->sockaddr;
pc->socklen = ur->socklen;
pc->name = &ur->host;
} else {
lua_pushnil(L);
lua_pushliteral(L, "resolver not working");
return 2;
}
pc->get = ngx_stream_lua_socket_tcp_get_peer;
rc = ngx_event_connect_peer(pc);
if (rc == NGX_ERROR) {
u->socket_errno = ngx_socket_errno;
}
if (u->cleanup == NULL) {
cln = ngx_stream_lua_cleanup_add(s, 0);
if (cln == NULL) {
u->ft_type |= NGX_STREAM_LUA_SOCKET_FT_ERROR;
lua_pushnil(L);
lua_pushliteral(L, "no memory");