-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_client_coro.cc
1134 lines (1033 loc) · 34.9 KB
/
swoole_client_coro.cc
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
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole_cxx.h"
#include "socks5.h"
#include "mqtt.h"
#include "ext/standard/basic_functions.h"
using swoole::coroutine::Socket;
using namespace swoole;
static zend_class_entry *swoole_client_coro_ce;
static zend_object_handlers swoole_client_coro_handlers;
typedef struct
{
Socket *sock;
zend_object std;
} client_coro;
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_construct, 0, 0, 1)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_set, 0, 0, 1)
ZEND_ARG_ARRAY_INFO(0, settings, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_connect, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, sock_flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_recv, 0, 0, 0)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_send, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_peek, 0, 0, 0)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_sendfile, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_sendto, 0, 0, 3)
ZEND_ARG_INFO(0, address)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_coro_recvfrom, 0, 0, 2)
ZEND_ARG_INFO(0, length)
ZEND_ARG_INFO(1, address)
ZEND_ARG_INFO(1, port)
ZEND_END_ARG_INFO()
static PHP_METHOD(swoole_client_coro, __construct);
static PHP_METHOD(swoole_client_coro, __destruct);
static PHP_METHOD(swoole_client_coro, set);
static PHP_METHOD(swoole_client_coro, connect);
static PHP_METHOD(swoole_client_coro, recv);
static PHP_METHOD(swoole_client_coro, peek);
static PHP_METHOD(swoole_client_coro, send);
static PHP_METHOD(swoole_client_coro, sendfile);
static PHP_METHOD(swoole_client_coro, sendto);
static PHP_METHOD(swoole_client_coro, recvfrom);
#ifdef SW_USE_OPENSSL
static PHP_METHOD(swoole_client_coro, enableSSL);
static PHP_METHOD(swoole_client_coro, getPeerCert);
static PHP_METHOD(swoole_client_coro, verifyPeerCert);
#endif
static PHP_METHOD(swoole_client_coro, exportSocket);
static PHP_METHOD(swoole_client_coro, isConnected);
static PHP_METHOD(swoole_client_coro, getsockname);
static PHP_METHOD(swoole_client_coro, getpeername);
static PHP_METHOD(swoole_client_coro, close);
static Socket* client_coro_new(zval *zobject, int port = 0);
void php_swoole_client_coro_socket_free(Socket *cli);
static const zend_function_entry swoole_client_coro_methods[] =
{
PHP_ME(swoole_client_coro, __construct, arginfo_swoole_client_coro_construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, __destruct, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, set, arginfo_swoole_client_coro_set, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, connect, arginfo_swoole_client_coro_connect, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, recv, arginfo_swoole_client_coro_recv, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, peek, arginfo_swoole_client_coro_peek, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, send, arginfo_swoole_client_coro_send, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, sendfile, arginfo_swoole_client_coro_sendfile, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, sendto, arginfo_swoole_client_coro_sendto, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, recvfrom, arginfo_swoole_client_coro_recvfrom, ZEND_ACC_PUBLIC)
#ifdef SW_USE_OPENSSL
PHP_ME(swoole_client_coro, enableSSL, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, getPeerCert, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, verifyPeerCert, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
#endif
PHP_ME(swoole_client_coro, isConnected, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, getsockname, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, getpeername, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, close, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client_coro, exportSocket, arginfo_swoole_client_coro_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static sw_inline client_coro* php_swoole_client_coro_fetch_object(zend_object *obj)
{
return (client_coro *) ((char *) obj - swoole_client_coro_handlers.offset);
}
static sw_inline client_coro* php_swoole_get_client(zval *zobject)
{
return php_swoole_client_coro_fetch_object(Z_OBJ_P(zobject));
}
static sw_inline Socket* php_swoole_get_sock(zval *zobject)
{
return php_swoole_get_client(zobject)->sock;
}
static void php_swoole_client_coro_free_object(zend_object *object)
{
client_coro *client = php_swoole_client_coro_fetch_object(object);
if (client->sock)
{
php_swoole_client_coro_socket_free(client->sock);
}
zend_object_std_dtor(&client->std);
}
static zend_object *php_swoole_client_coro_create_object(zend_class_entry *ce)
{
client_coro *sock_t = (client_coro *) zend_object_alloc(sizeof(client_coro), ce);
zend_object_std_init(&sock_t->std, ce);
object_properties_init(&sock_t->std, ce);
sock_t->std.handlers = &swoole_client_coro_handlers;
return &sock_t->std;
}
void php_swoole_client_coro_minit(int module_number)
{
SW_INIT_CLASS_ENTRY(swoole_client_coro, "Swoole\\Coroutine\\Client", NULL, "Co\\Client", swoole_client_coro_methods);
SW_SET_CLASS_SERIALIZABLE(swoole_client_coro, zend_class_serialize_deny, zend_class_unserialize_deny);
SW_SET_CLASS_CLONEABLE(swoole_client_coro, sw_zend_class_clone_deny);
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_client_coro, sw_zend_class_unset_property_deny);
SW_SET_CLASS_CUSTOM_OBJECT(swoole_client_coro, php_swoole_client_coro_create_object, php_swoole_client_coro_free_object, client_coro, std);
zend_declare_property_long(swoole_client_coro_ce, ZEND_STRL("errCode"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_string(swoole_client_coro_ce, ZEND_STRL("errMsg"), "", ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_client_coro_ce, ZEND_STRL("fd"), -1, ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_client_coro_ce, ZEND_STRL("socket"), ZEND_ACC_PRIVATE);
zend_declare_property_long(swoole_client_coro_ce, ZEND_STRL("type"), SW_SOCK_TCP, ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_client_coro_ce, ZEND_STRL("setting"), ZEND_ACC_PUBLIC);
zend_declare_property_bool(swoole_client_coro_ce, ZEND_STRL("connected"), 0, ZEND_ACC_PUBLIC);
zend_declare_class_constant_long(swoole_client_coro_ce, ZEND_STRL("MSG_OOB"), MSG_OOB);
zend_declare_class_constant_long(swoole_client_coro_ce, ZEND_STRL("MSG_PEEK"), MSG_PEEK);
zend_declare_class_constant_long(swoole_client_coro_ce, ZEND_STRL("MSG_DONTWAIT"), MSG_DONTWAIT);
zend_declare_class_constant_long(swoole_client_coro_ce, ZEND_STRL("MSG_WAITALL"), MSG_WAITALL);
}
static sw_inline Socket* client_get_ptr(zval *zobject, bool silent = false)
{
Socket *cli = php_swoole_get_client(zobject)->sock;
if (cli)
{
return cli;
}
else
{
if (!silent)
{
zend_update_property_long(swoole_client_coro_ce, zobject, ZEND_STRL("errCode"), SW_ERROR_CLIENT_NO_CONNECTION);
zend_update_property_string(swoole_client_coro_ce, zobject, ZEND_STRL("errMsg"), swoole_strerror(SW_ERROR_CLIENT_NO_CONNECTION));
}
return nullptr;
}
}
static Socket* client_coro_new(zval *zobject, int port)
{
zval *ztype = sw_zend_read_property(Z_OBJCE_P(zobject), zobject, ZEND_STRL("type"), 0);
zend_long type = zval_get_long(ztype);
if ((type == SW_SOCK_TCP || type == SW_SOCK_TCP6) && (port <= 0 || port > SW_CLIENT_MAX_PORT))
{
php_swoole_fatal_error(E_WARNING, "The port is invalid");
return NULL;
}
php_swoole_check_reactor();
Socket *cli = new Socket((enum swSocket_type) type);
if (UNEXPECTED(cli->get_fd() < 0))
{
php_swoole_sys_error(E_WARNING, "new Socket() failed");
zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("errCode"), errno);
zend_update_property_string(Z_OBJCE_P(zobject), zobject, ZEND_STRL("errMsg"), swoole_strerror(errno));
delete cli;
return NULL;
}
zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("fd"), cli->get_fd());
#ifdef SW_USE_OPENSSL
if (type & SW_SOCK_SSL)
{
cli->open_ssl = true;
}
#endif
php_swoole_get_client(zobject)->sock = cli;
return cli;
}
static bool client_coro_close(zval *zobject)
{
Socket *cli = php_swoole_get_sock(zobject);
if (cli)
{
zend_update_property_bool(Z_OBJCE_P(zobject), zobject, ZEND_STRL("connected"), 0);
if (!cli->get_bound_cid())
{
php_swoole_get_client(zobject)->sock = nullptr;
}
php_swoole_client_coro_socket_free(cli);
return true;
}
return false;
}
void php_swoole_client_coro_socket_free_socks5_proxy(Socket *cli)
{
if (cli->socks5_proxy)
{
if (cli->socks5_proxy->host)
{
efree((void* )cli->socks5_proxy->host);
cli->socks5_proxy->host = nullptr;
}
if (cli->socks5_proxy->username)
{
efree((void* )cli->socks5_proxy->username);
cli->socks5_proxy->username = nullptr;
}
if (cli->socks5_proxy->password)
{
efree((void* )cli->socks5_proxy->password);
cli->socks5_proxy->password = nullptr;
}
efree(cli->socks5_proxy);
cli->socks5_proxy = nullptr;
}
}
void php_swoole_client_coro_socket_free_http_proxy(Socket *cli)
{
if (cli->http_proxy)
{
if (cli->http_proxy->proxy_host)
{
efree((void* ) cli->http_proxy->proxy_host);
cli->http_proxy->proxy_host = nullptr;
}
if (cli->http_proxy->user)
{
efree((void* ) cli->http_proxy->user);
cli->http_proxy->user = nullptr;
}
if (cli->http_proxy->password)
{
efree((void* ) cli->http_proxy->password);
cli->http_proxy->password = nullptr;
}
efree(cli->http_proxy);
cli->http_proxy = nullptr;
}
}
void php_swoole_client_coro_socket_free(Socket *cli)
{
//FIXME: move to Socket method, we should not manage it externally
if (!cli->has_bound())
{
php_swoole_client_coro_socket_free_socks5_proxy(cli);
php_swoole_client_coro_socket_free_http_proxy(cli);
if (cli->protocol.private_data)
{
sw_zend_fci_cache_discard((zend_fcall_info_cache *) cli->protocol.private_data);
efree(cli->protocol.private_data);
cli->protocol.private_data = nullptr;
}
}
if (cli->close())
{
delete cli;
}
}
bool php_swoole_client_set(Socket *cli, zval *zset)
{
HashTable *vht = Z_ARRVAL_P(zset);
zval *ztmp;
bool ret = true;
/**
* timeout
*/
if (php_swoole_array_get_value(vht, "timeout", ztmp))
{
cli->set_timeout(zval_get_double(ztmp));
}
if (php_swoole_array_get_value(vht, "connect_timeout", ztmp))
{
cli->set_timeout(zval_get_double(ztmp), SW_TIMEOUT_CONNECT);
}
if (php_swoole_array_get_value(vht, "read_timeout", ztmp))
{
cli->set_timeout(zval_get_double(ztmp), SW_TIMEOUT_READ);
}
if (php_swoole_array_get_value(vht, "write_timeout", ztmp))
{
cli->set_timeout(zval_get_double(ztmp), SW_TIMEOUT_WRITE);
}
/**
* bind port
*/
if (php_swoole_array_get_value(vht, "bind_port", ztmp))
{
zend_long v = zval_get_long(ztmp);
int bind_port = SW_MAX(0, SW_MIN(v, UINT16_MAX));
/**
* bind address
*/
if (php_swoole_array_get_value(vht, "bind_address", ztmp))
{
if (!cli->bind(zend::string(ztmp).val(), bind_port))
{
ret = false;
}
}
}
/**
* socket send/recv buffer size
*/
if (php_swoole_array_get_value(vht, "socket_buffer_size", ztmp))
{
zend_long size = zval_get_long(ztmp);
if (size <= 0)
{
php_swoole_fatal_error(E_WARNING, "socket buffer size must be greater than 0, got " ZEND_LONG_FMT, size);
ret = false;
}
else
{
cli->set_option(SOL_SOCKET, SO_RCVBUF, size) && cli->set_option(SOL_SOCKET, SO_SNDBUF, size);
}
}
/**
* client: tcp_nodelay
*/
if (php_swoole_array_get_value(vht, "open_tcp_nodelay", ztmp))
{
if (cli->get_type() == SW_SOCK_TCP || cli->get_type() != SW_SOCK_TCP6)
{
cli->set_option(IPPROTO_TCP, TCP_NODELAY, zval_is_true(ztmp));
}
}
/**
* openssl and protocol options
*/
if (!php_swoole_socket_set_protocol(cli, zset))
{
ret = false;
}
/**
* socks5 proxy
*/
if (php_swoole_array_get_value(vht, "socks5_host", ztmp))
{
zend::string host(ztmp);
if (php_swoole_array_get_value(vht, "socks5_port", ztmp))
{
php_swoole_client_coro_socket_free_socks5_proxy(cli);
cli->socks5_proxy = (struct _swSocks5 *) ecalloc(1, sizeof(swSocks5));
cli->socks5_proxy->host = estrdup(host.val());
cli->socks5_proxy->port = zval_get_long(ztmp);
cli->socks5_proxy->dns_tunnel = 1;
if (php_swoole_array_get_value(vht, "socks5_username", ztmp))
{
zend::string username(ztmp);
if (username.len() > 0 && php_swoole_array_get_value(vht, "socks5_password", ztmp))
{
zend::string password(ztmp);
if (password.len() > 0)
{
cli->socks5_proxy->method = 0x02;
cli->socks5_proxy->username = estrdup(username.val());
cli->socks5_proxy->l_username = username.len();
cli->socks5_proxy->password = estrdup(password.val());
cli->socks5_proxy->l_password = password.len();
}
}
else
{
php_swoole_fatal_error(E_WARNING, "socks5_password should not be null");
ret = false;
}
}
}
else
{
php_swoole_fatal_error(E_WARNING, "socks5_port should not be null");
ret = false;
}
}
/**
* http proxy
*/
else if (php_swoole_array_get_value(vht, "http_proxy_host", ztmp))
{
zend::string host(ztmp);
if (php_swoole_array_get_value(vht, "http_proxy_port", ztmp))
{
php_swoole_client_coro_socket_free_http_proxy(cli);
cli->http_proxy = (struct _http_proxy*) ecalloc(1, sizeof(*cli->http_proxy) - sizeof(cli->http_proxy->buf));
cli->http_proxy->proxy_host = estrdup(host.val());
cli->http_proxy->proxy_port = zval_get_long(ztmp);
if (php_swoole_array_get_value(vht, "http_proxy_username", ztmp) || php_swoole_array_get_value(vht, "http_proxy_user", ztmp))
{
zend::string username(ztmp);
if (username.len() > 0 && php_swoole_array_get_value(vht, "http_proxy_password", ztmp))
{
zend::string password(ztmp);
if (password.len() > 0)
{
cli->http_proxy->user = estrdup(username.val());
cli->http_proxy->l_user = username.len();
cli->http_proxy->password = estrdup(password.val());
cli->http_proxy->l_password = password.len();
}
}
else
{
php_swoole_fatal_error(E_WARNING, "http_proxy_password should not be null");
ret = false;
}
}
}
else
{
php_swoole_fatal_error(E_WARNING, "http_proxy_port should not be null");
ret = false;
}
}
return ret;
}
#ifdef SW_USE_OPENSSL
bool php_swoole_socket_set_ssl(Socket *sock, zval *zset)
{
HashTable *vht = Z_ARRVAL_P(zset);
zval *ztmp;
bool ret = true;
if (php_swoole_array_get_value(vht, "ssl_method", ztmp))
{
zend_long v = zval_get_long(ztmp);
sock->ssl_option.method = SW_MAX(0, SW_MIN(v, UINT8_MAX));
}
if (php_swoole_array_get_value(vht, "ssl_protocols", ztmp))
{
zend_long v = zval_get_long(ztmp);
sock->ssl_option.disable_protocols = (SW_SSL_SSLv2 | SW_SSL_SSLv3 | SW_SSL_TLSv1 | SW_SSL_TLSv1_1
| SW_SSL_TLSv1_2) ^ v;
}
if (php_swoole_array_get_value(vht, "ssl_compress", ztmp))
{
sock->ssl_option.disable_compress = !zval_is_true(ztmp);
}
else if (php_swoole_array_get_value(vht, "ssl_disable_compression", ztmp))
{
sock->ssl_option.disable_compress = !zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "ssl_cert_file", ztmp))
{
zend::string str_v(ztmp);
if (sock->ssl_option.cert_file)
{
sw_free(sock->ssl_option.cert_file);
sock->ssl_option.cert_file = nullptr;
}
if (access(str_v.val(), R_OK) == 0)
{
sock->ssl_option.cert_file = str_v.dup();
}
else
{
php_swoole_fatal_error(E_WARNING, "ssl cert file[%s] not found", sock->ssl_option.cert_file);
ret = false;
}
}
if (php_swoole_array_get_value(vht, "ssl_key_file", ztmp))
{
zend::string str_v(ztmp);
if (sock->ssl_option.key_file)
{
sw_free(sock->ssl_option.key_file);
sock->ssl_option.key_file = nullptr;
}
if (access(str_v.val(), R_OK) == 0)
{
sock->ssl_option.key_file = str_v.dup();
}
else
{
php_swoole_fatal_error(E_WARNING, "ssl key file[%s] not found", sock->ssl_option.key_file);
ret = false;
}
}
if (sock->ssl_option.cert_file && !sock->ssl_option.key_file)
{
php_swoole_fatal_error(E_WARNING, "ssl require key file");
}
else if (sock->ssl_option.key_file && !sock->ssl_option.cert_file)
{
php_swoole_fatal_error(E_WARNING, "ssl require cert file");
}
if (php_swoole_array_get_value(vht, "ssl_passphrase", ztmp))
{
if (sock->ssl_option.passphrase)
{
sw_free(sock->ssl_option.passphrase);
}
sock->ssl_option.passphrase = zend::string(ztmp).dup();
}
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
if (php_swoole_array_get_value(vht, "ssl_host_name", ztmp))
{
if (sock->ssl_option.tls_host_name)
{
sw_free(sock->ssl_option.tls_host_name);
}
sock->ssl_option.tls_host_name = zend::string(ztmp).dup();
/* if user set empty ssl_host_name, disable it, otherwise the underlying may set it automatically */
sock->ssl_option.disable_tls_host_name = !sock->ssl_option.tls_host_name;
}
#endif
if (php_swoole_array_get_value(vht, "ssl_verify_peer", ztmp))
{
sock->ssl_option.verify_peer = zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "ssl_allow_self_signed", ztmp))
{
sock->ssl_option.allow_self_signed = zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "ssl_cafile", ztmp))
{
if (sock->ssl_option.cafile)
{
sw_free(sock->ssl_option.cafile);
}
sock->ssl_option.cafile = zend::string(ztmp).dup();
}
if (php_swoole_array_get_value(vht, "ssl_capath", ztmp))
{
if (sock->ssl_option.capath)
{
sw_free( sock->ssl_option.capath);
}
sock->ssl_option.capath = zend::string(ztmp).dup();
}
if (php_swoole_array_get_value(vht, "ssl_verify_depth", ztmp))
{
zend_long v = zval_get_long(ztmp);
sock->ssl_option.verify_depth = SW_MAX(0, SW_MIN(v, UINT8_MAX));
}
return ret;
}
#endif
static PHP_METHOD(swoole_client_coro, __construct)
{
if (php_swoole_get_client(ZEND_THIS)->sock)
{
php_swoole_fatal_error(E_ERROR, "Constructor of %s can only be called once", SW_Z_OBJCE_NAME_VAL_P(ZEND_THIS));
}
zend_long type = 0;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 1, 1)
Z_PARAM_LONG(type)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
int client_type = php_swoole_socktype(type);
if (client_type < SW_SOCK_TCP || client_type > SW_SOCK_UNIX_DGRAM)
{
const char *space, *class_name = get_active_class_name(&space);
zend_type_error(
"%s%s%s() expects parameter %d to be client type, unknown type " ZEND_LONG_FMT " given",
class_name, space, get_active_function_name(), 1, type
);
RETURN_FALSE;
}
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("type"), type);
RETURN_TRUE;
}
static PHP_METHOD(swoole_client_coro, __destruct) { }
static PHP_METHOD(swoole_client_coro, set)
{
Socket *cli = client_get_ptr(ZEND_THIS, true);
zval *zset, *zsetting;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(zset)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (php_swoole_array_length(zset) == 0)
{
RETURN_FALSE;
}
else
{
zsetting = sw_zend_read_and_convert_property_array(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("setting"), 0);
php_array_merge(Z_ARRVAL_P(zsetting), Z_ARRVAL_P(zset));
if (cli)
{
RETURN_BOOL(php_swoole_client_set(cli, zset));
}
RETURN_TRUE;
}
}
static PHP_METHOD(swoole_client_coro, connect)
{
char *host;
size_t host_len;
zend_long port = 0;
double timeout = 0;
zend_long sock_flag = 0;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_STRING(host, host_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(port)
Z_PARAM_DOUBLE(timeout)
Z_PARAM_LONG(sock_flag)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (host_len == 0)
{
php_swoole_fatal_error(E_WARNING, "The host is empty");
RETURN_FALSE;
}
Socket *cli = php_swoole_get_sock(ZEND_THIS);
if (cli)
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), EISCONN);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), swoole_strerror(EISCONN));
RETURN_FALSE;
}
cli = client_coro_new(ZEND_THIS, (int) port);
if (!cli)
{
RETURN_FALSE;
}
zval *zset = sw_zend_read_property(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("setting"), 0);
if (zset && ZVAL_IS_ARRAY(zset))
{
php_swoole_client_set(cli, zset);
}
cli->set_timeout(timeout, SW_TIMEOUT_CONNECT);
if (!cli->connect(host, port, sock_flag))
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), cli->errCode);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), cli->errMsg);
client_coro_close(ZEND_THIS);
RETURN_FALSE;
}
cli->set_timeout(timeout, SW_TIMEOUT_RDWR);
zend_update_property_bool(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("connected"), 1);
RETURN_TRUE;
}
static PHP_METHOD(swoole_client_coro, send)
{
char *data;
size_t data_len;
double timeout = 0;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(data, data_len)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (data_len == 0)
{
php_swoole_fatal_error(E_WARNING, "data to send is empty");
RETURN_FALSE;
}
Socket *cli = client_get_ptr(ZEND_THIS);
if (!cli)
{
RETURN_FALSE;
}
Socket::timeout_setter ts(cli, timeout, SW_TIMEOUT_WRITE);
ssize_t ret = cli->send_all(data, data_len);
if (ret < 0)
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), cli->errCode);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), cli->errMsg);
RETVAL_FALSE;
}
else
{
if ((size_t) ret < data_len && cli->errCode)
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), cli->errCode);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), cli->errMsg);
}
RETURN_LONG(ret);
}
}
static PHP_METHOD(swoole_client_coro, sendto)
{
char* host;
size_t host_len;
long port;
char *data;
size_t len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &host, &host_len, &port, &data, &len) == FAILURE)
{
RETURN_FALSE;
}
if (len == 0)
{
RETURN_FALSE;
}
Socket *cli = php_swoole_get_sock(ZEND_THIS);
if (!cli)
{
cli = client_coro_new(ZEND_THIS, (int) port);
if (!cli)
{
RETURN_FALSE;
}
}
ssize_t ret = cli->sendto(host, port, data, len);
if (ret < 0)
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), cli->errCode);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), cli->errMsg);
RETURN_FALSE;
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_client_coro, recvfrom)
{
zend_long length;
zval *address, *port;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz/|z/", &length, &address, &port) == FAILURE)
{
RETURN_FALSE;
}
if (length <= 0)
{
RETURN_FALSE;
}
Socket *cli = php_swoole_get_sock(ZEND_THIS);
if (!cli)
{
cli = client_coro_new(ZEND_THIS);
if (!cli)
{
RETURN_FALSE;
}
}
zend_string *retval = zend_string_alloc(length + 1, 0);
ssize_t n_bytes = cli->recvfrom(ZSTR_VAL(retval), length);
if (n_bytes < 0)
{
zend_string_free(retval);
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), cli->errCode);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), cli->errMsg);
RETURN_FALSE;
}
else
{
zval_ptr_dtor(address);
ZVAL_STRING(address, cli->get_ip());
if (port)
{
zval_ptr_dtor(port);
ZVAL_LONG(port, cli->get_port());
}
ZSTR_LEN(retval) = n_bytes;
ZSTR_VAL(retval)[ZSTR_LEN(retval)] = '\0';
RETURN_STR(retval);
}
}
static PHP_METHOD(swoole_client_coro, sendfile)
{
char *file;
size_t file_len;
zend_long offset = 0;
zend_long length = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &file, &file_len, &offset, &length) == FAILURE)
{
RETURN_FALSE;
}
if (file_len == 0)
{
php_swoole_fatal_error(E_WARNING, "file to send is empty");
RETURN_FALSE;
}
Socket *cli = client_get_ptr(ZEND_THIS);
if (!cli)
{
RETURN_FALSE;
}
//only stream socket can sendfile
if (!(cli->get_type() == SW_SOCK_TCP || cli->get_type() == SW_SOCK_TCP6 || cli->get_type() == SW_SOCK_UNIX_STREAM))
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), EINVAL);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), "dgram socket cannot use sendfile");
RETURN_FALSE;
}
if (!cli->sendfile(file, offset, length))
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), cli->errCode);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), cli->errMsg);
RETVAL_FALSE;
}
else
{
RETVAL_TRUE;
}
}
static PHP_METHOD(swoole_client_coro, recv)
{
double timeout = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
Socket *cli = client_get_ptr(ZEND_THIS);
if (!cli)
{
RETURN_FALSE;
}
ssize_t retval ;
if (cli->open_length_check || cli->open_eof_check)
{
retval = cli->recv_packet(timeout);
if (retval > 0)
{
RETVAL_STRINGL(cli->get_read_buffer()->str, retval);
}
}
else
{
zend_string *result = zend_string_alloc(SW_PHP_CLIENT_BUFFER_SIZE - sizeof(zend_string), 0);
Socket::timeout_setter ts(cli, timeout, SW_TIMEOUT_READ);
retval = cli->recv(ZSTR_VAL(result), SW_PHP_CLIENT_BUFFER_SIZE - sizeof(zend_string));
if (retval > 0)
{
ZSTR_VAL(result)[retval] = '\0';
ZSTR_LEN(result) = retval;
RETURN_STR(result);
}
else
{
zend_string_free(result);
}
}
if (retval < 0)
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), cli->errCode);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), cli->errMsg);
RETURN_FALSE;
}
else if (retval == 0)
{
RETURN_EMPTY_STRING();
}
}
static PHP_METHOD(swoole_client_coro, peek)
{
zend_long buf_len = SW_PHP_CLIENT_BUFFER_SIZE;
int ret;
char *buf = NULL;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(buf_len)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
Socket *cli = client_get_ptr(ZEND_THIS);
if (!cli)
{
RETURN_FALSE;
}
buf = (char *) emalloc(buf_len + 1);
ret = cli->peek(buf, buf_len);
if (ret < 0)
{
zend_update_property_long(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), cli->errCode);
zend_update_property_string(swoole_client_coro_ce, ZEND_THIS, ZEND_STRL("errMsg"), cli->errMsg);
efree(buf);
RETURN_FALSE;
}
else
{
buf[ret] = 0;
RETVAL_STRINGL(buf, ret);
efree(buf);
}
}
static PHP_METHOD(swoole_client_coro, isConnected)
{
Socket *cli = php_swoole_get_sock(ZEND_THIS);
if (cli && cli->is_connect())
{
RETURN_TRUE;
}
else
{
RETURN_FALSE;
}
}
static PHP_METHOD(swoole_client_coro, getsockname)
{
Socket *cli = client_get_ptr(ZEND_THIS);
if (!cli)
{
RETURN_FALSE;
}
swSocketAddress sa;