php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
sockets.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org> |
14 | Sterling Hughes <sterling@php.net> |
15 | Jason Greene <jason@php.net> |
16 | Gustavo Lopes <cataphract@php.net> |
17 | WinSock: Daniel Beulshausen <daniel@php4win.de> |
18 +----------------------------------------------------------------------+
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include "php.h"
26
27#include "php_network.h"
28#include "ext/standard/file.h"
29#include "ext/standard/info.h"
30#include "php_ini.h"
31#ifdef PHP_WIN32
32# include "windows_common.h"
33# include <windows.h>
34# include <Ws2tcpip.h>
35# include "php_sockets.h"
36# include <win32/sockets.h>
37# include <win32/winutil.h>
38#else
39# include <sys/types.h>
40# include <sys/socket.h>
41# include <netdb.h>
42# include <netinet/in.h>
43# include <netinet/tcp.h>
44# include <sys/un.h>
45# include <arpa/inet.h>
46# include <sys/time.h>
47# include <unistd.h>
48# include <errno.h>
49# include <fcntl.h>
50# include <signal.h>
51# include <sys/uio.h>
52# define set_errno(a) (errno = a)
53# include "php_sockets.h"
54# ifdef HAVE_IF_NAMETOINDEX
55# include <net/if.h>
56# endif
57# if defined(HAVE_LINUX_SOCK_DIAG_H)
58# include <linux/sock_diag.h>
59# else
60# undef SO_MEMINFO
61# endif
62# if defined(HAVE_LINUX_FILTER_H)
63# include <linux/filter.h>
64# else
65# undef SO_BPF_EXTENSIONS
66# endif
67#endif
68
69#include <stddef.h>
70
71#include "sockaddr_conv.h"
72#include "multicast.h"
73#include "sendrecvmsg.h"
74#include "sockets_arginfo.h"
75
77
78#define SUN_LEN_NO_UB(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
79/* The SUN_LEN macro does pointer arithmetics on NULL which triggers errors in the Clang UBSAN build */
80#ifdef __has_feature
81# if __has_feature(undefined_behavior_sanitizer)
82# undef SUN_LEN
83# define SUN_LEN(su) SUN_LEN_NO_UB(su)
84# endif
85#endif
86#ifndef SUN_LEN
87# define SUN_LEN(su) SUN_LEN_NO_UB(su)
88#endif
89
90#ifndef PF_INET
91#define PF_INET AF_INET
92#endif
93
94static PHP_GINIT_FUNCTION(sockets);
95static PHP_GSHUTDOWN_FUNCTION(sockets);
96static PHP_MINIT_FUNCTION(sockets);
97static PHP_MSHUTDOWN_FUNCTION(sockets);
98static PHP_MINFO_FUNCTION(sockets);
99static PHP_RSHUTDOWN_FUNCTION(sockets);
100
101/* Socket class */
102
104static zend_object_handlers socket_object_handlers;
105
106static zend_object *socket_create_object(zend_class_entry *class_type) {
107 php_socket *intern = zend_object_alloc(sizeof(php_socket), class_type);
108
109 zend_object_std_init(&intern->std, class_type);
110 object_properties_init(&intern->std, class_type);
111
112 intern->bsd_socket = -1; /* invalid socket */
113 intern->type = PF_UNSPEC;
114 intern->error = 0;
115 intern->blocking = 1;
116 ZVAL_UNDEF(&intern->zstream);
117
118 return &intern->std;
119}
120
121static zend_function *socket_get_constructor(zend_object *object) {
122 zend_throw_error(NULL, "Cannot directly construct Socket, use socket_create() instead");
123 return NULL;
124}
125
126static void socket_free_obj(zend_object *object)
127{
128 php_socket *socket = socket_from_obj(object);
129
130 if (Z_ISUNDEF(socket->zstream)) {
131 if (!IS_INVALID_SOCKET(socket)) {
132 close(socket->bsd_socket);
133 }
134 } else {
135 zval_ptr_dtor(&socket->zstream);
136 }
137
138 zend_object_std_dtor(&socket->std);
139}
140
141static HashTable *socket_get_gc(zend_object *object, zval **table, int *n)
142{
143 php_socket *socket = socket_from_obj(object);
144
145 *table = &socket->zstream;
146 *n = 1;
147
148 return zend_std_get_properties(object);
149}
150
151/* AddressInfo class */
152
153typedef struct {
157
159static zend_object_handlers address_info_object_handlers;
160
161static inline php_addrinfo *address_info_from_obj(zend_object *obj) {
162 return (php_addrinfo *)((char *)(obj) - XtOffsetOf(php_addrinfo, std));
163}
164
165#define Z_ADDRESS_INFO_P(zv) address_info_from_obj(Z_OBJ_P(zv))
166
167static zend_object *address_info_create_object(zend_class_entry *class_type) {
168 php_addrinfo *intern = zend_object_alloc(sizeof(php_addrinfo), class_type);
169
170 zend_object_std_init(&intern->std, class_type);
171 object_properties_init(&intern->std, class_type);
172
173 return &intern->std;
174}
175
176static zend_function *address_info_get_constructor(zend_object *object) {
177 zend_throw_error(NULL, "Cannot directly construct AddressInfo, use socket_addrinfo_lookup() instead");
178 return NULL;
179}
180
181static void address_info_free_obj(zend_object *object)
182{
183 php_addrinfo *address_info = address_info_from_obj(object);
184
185 if (address_info->addrinfo.ai_canonname != NULL) {
186 efree(address_info->addrinfo.ai_canonname);
187 }
188 efree(address_info->addrinfo.ai_addr);
189
190 zend_object_std_dtor(&address_info->std);
191}
192
193/* Module registration */
194
197 "sockets",
198 ext_functions,
199 PHP_MINIT(sockets),
200 PHP_MSHUTDOWN(sockets),
201 NULL,
202 PHP_RSHUTDOWN(sockets),
203 PHP_MINFO(sockets),
204 PHP_SOCKETS_VERSION,
205 PHP_MODULE_GLOBALS(sockets),
206 PHP_GINIT(sockets),
207 PHP_GSHUTDOWN(sockets),
208 NULL,
210};
211
212
213#ifdef COMPILE_DL_SOCKETS
214#ifdef ZTS
216#endif
217ZEND_GET_MODULE(sockets)
218#endif
219
220static bool php_open_listen_sock(php_socket *sock, int port, int backlog) /* {{{ */
221{
222 struct sockaddr_in la = {0};
223 struct hostent *hp;
224
225#ifndef PHP_WIN32
226 if ((hp = php_network_gethostbyname("0.0.0.0")) == NULL) {
227#else
228 if ((hp = php_network_gethostbyname("localhost")) == NULL) {
229#endif
230 return 0;
231 }
232
233 memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
234 la.sin_family = hp->h_addrtype;
235 la.sin_port = htons((unsigned short) port);
236
237 sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
238 sock->blocking = 1;
239
240 if (IS_INVALID_SOCKET(sock)) {
241 PHP_SOCKET_ERROR(sock, "unable to create listening socket", errno);
242 return 0;
243 }
244
245 sock->type = PF_INET;
246
247 if (bind(sock->bsd_socket, (struct sockaddr *)&la, sizeof(la)) != 0) {
248 PHP_SOCKET_ERROR(sock, "unable to bind to given address", errno);
249 close(sock->bsd_socket);
250 return 0;
251 }
252
253 if (listen(sock->bsd_socket, backlog) != 0) {
254 PHP_SOCKET_ERROR(sock, "unable to listen on socket", errno);
255 close(sock->bsd_socket);
256 return 0;
257 }
258
259 return 1;
260}
261/* }}} */
262
263static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct sockaddr *la, socklen_t *la_len) /* {{{ */
264{
265 out_sock->bsd_socket = accept(in_sock->bsd_socket, la, la_len);
266
267 if (IS_INVALID_SOCKET(out_sock)) {
268 PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
269 return 0;
270 }
271
272#if !defined(PHP_WIN32)
277 int mode;
278
279 if ((mode = fcntl(out_sock->bsd_socket, F_GETFD)) < 0) {
280 PHP_SOCKET_ERROR(out_sock, "unable to get fcntl mode on the socket", errno);
281 return 0;
282 }
283
284 int cloexec = (mode | FD_CLOEXEC);
285
286 if (mode != cloexec) {
287 if (fcntl(out_sock->bsd_socket, F_SETFD, cloexec) < 0) {
288 PHP_SOCKET_ERROR(out_sock, "unable to set cloexec mode on the socket", errno);
289 return 0;
290 }
291 }
292#endif
293
294 out_sock->error = 0;
295 out_sock->blocking = 1;
296 out_sock->type = la->sa_family;
297
298 return 1;
299}
300/* }}} */
301
302/* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
303static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags)
304{
305 int m = 0;
306 size_t n = 0;
307 int no_read = 0;
308 int nonblock = 0;
309 char *t = (char *) buf;
310
311#ifndef PHP_WIN32
312 m = fcntl(sock->bsd_socket, F_GETFL);
313 if (m < 0) {
314 return m;
315 }
316 nonblock = (m & O_NONBLOCK);
317 m = 0;
318#else
319 nonblock = !sock->blocking;
320#endif
321 set_errno(0);
322
323 *t = '\0';
324 while (*t != '\n' && *t != '\r' && n < maxlen) {
325 if (m > 0) {
326 t++;
327 n++;
328 } else if (m == 0) {
329 no_read++;
330 if (nonblock && no_read >= 2) {
331 return n;
332 /* The first pass, m always is 0, so no_read becomes 1
333 * in the first pass. no_read becomes 2 in the second pass,
334 * and if this is nonblocking, we should return.. */
335 }
336
337 if (no_read > 200) {
339 return -1;
340 }
341 }
342
343 if (n < maxlen) {
344 m = recv(sock->bsd_socket, (void *) t, 1, flags);
345 }
346
347 if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
348 return -1;
349 }
350
351 set_errno(0);
352 }
353
354 if (n < maxlen) {
355 n++;
356 /* The only reasons it makes it to here is
357 * if '\n' or '\r' are encountered. So, increase
358 * the return by 1 to make up for the lack of the
359 * '\n' or '\r' in the count (since read() takes
360 * place at the end of the loop..) */
361 }
362
363 return n;
364}
365/* }}} */
366
367char *sockets_strerror(int error) /* {{{ */
368{
369 const char *buf;
370
371#ifndef PHP_WIN32
372 if (error < -10000) {
373 if (error == INT_MIN) {
374 error = 2147473648;
375 } else {
376 error = -error - 10000;
377 }
378
379#ifdef HAVE_HSTRERROR
380 buf = hstrerror(error);
381#else
382 {
383 if (SOCKETS_G(strerror_buf)) {
384 efree(SOCKETS_G(strerror_buf));
385 }
386
387 spprintf(&(SOCKETS_G(strerror_buf)), 0, "Host lookup error %d", error);
388 buf = SOCKETS_G(strerror_buf);
389 }
390#endif
391 } else {
392 buf = strerror(error);
393 }
394#else
395 {
396 char *tmp = php_win32_error_to_msg(error);
397 buf = NULL;
398
399 if (tmp[0]) {
400 if (SOCKETS_G(strerror_buf)) {
401 efree(SOCKETS_G(strerror_buf));
402 }
403
404 SOCKETS_G(strerror_buf) = estrdup(tmp);
405 free(tmp);
406
407 buf = SOCKETS_G(strerror_buf);
408 }
409 }
410#endif
411
412 return (buf ? (char *) buf : "");
413}
414/* }}} */
415
416#ifdef PHP_WIN32
417static void sockets_destroy_wsa_info(zval *data)
418{/*{{{*/
419 HANDLE h = (HANDLE)Z_PTR_P(data);
420 CloseHandle(h);
421}/*}}}*/
422#endif
423
424/* {{{ PHP_GINIT_FUNCTION */
425static PHP_GINIT_FUNCTION(sockets)
426{
427#if defined(COMPILE_DL_SOCKETS) && defined(ZTS)
429#endif
430 sockets_globals->last_error = 0;
431 sockets_globals->strerror_buf = NULL;
432#ifdef PHP_WIN32
433 sockets_globals->wsa_child_count = 0;
434 zend_hash_init(&sockets_globals->wsa_info, 0, NULL, sockets_destroy_wsa_info, 1);
435#endif
436}
437/* }}} */
438
439/* {{{ PHP_GSHUTDOWN_FUNCTION */
440static PHP_GSHUTDOWN_FUNCTION(sockets)
441{
442#ifdef PHP_WIN32
443 zend_hash_destroy(&sockets_globals->wsa_info);
444#endif
445}
446/* }}} */
447
448/* {{{ PHP_MINIT_FUNCTION */
449static PHP_MINIT_FUNCTION(sockets)
450{
451#if defined(COMPILE_DL_SOCKETS) && defined(ZTS)
453#endif
454
455 socket_ce = register_class_Socket();
456 socket_ce->create_object = socket_create_object;
457 socket_ce->default_object_handlers = &socket_object_handlers;
458
459 memcpy(&socket_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
460 socket_object_handlers.offset = XtOffsetOf(php_socket, std);
461 socket_object_handlers.free_obj = socket_free_obj;
462 socket_object_handlers.get_constructor = socket_get_constructor;
463 socket_object_handlers.clone_obj = NULL;
464 socket_object_handlers.get_gc = socket_get_gc;
465 socket_object_handlers.compare = zend_objects_not_comparable;
466
467 address_info_ce = register_class_AddressInfo();
468 address_info_ce->create_object = address_info_create_object;
469 address_info_ce->default_object_handlers = &address_info_object_handlers;
470
471 memcpy(&address_info_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
472 address_info_object_handlers.offset = XtOffsetOf(php_addrinfo, std);
473 address_info_object_handlers.free_obj = address_info_free_obj;
474 address_info_object_handlers.get_constructor = address_info_get_constructor;
475 address_info_object_handlers.clone_obj = NULL;
476 address_info_object_handlers.compare = zend_objects_not_comparable;
477
478 register_sockets_symbols(module_number);
479
481
482 return SUCCESS;
483}
484/* }}} */
485
486/* {{{ PHP_MSHUTDOWN_FUNCTION */
487static PHP_MSHUTDOWN_FUNCTION(sockets)
488{
490
491 return SUCCESS;
492}
493/* }}} */
494
495/* {{{ PHP_MINFO_FUNCTION */
496static PHP_MINFO_FUNCTION(sockets)
497{
499 php_info_print_table_row(2, "Sockets Support", "enabled");
501}
502/* }}} */
503
504/* {{{ PHP_RSHUTDOWN_FUNCTION */
505static PHP_RSHUTDOWN_FUNCTION(sockets)
506{
507 if (SOCKETS_G(strerror_buf)) {
508 efree(SOCKETS_G(strerror_buf));
509 SOCKETS_G(strerror_buf) = NULL;
510 }
511
512 return SUCCESS;
513}
514/* }}} */
515
516static int php_sock_array_to_fd_set(uint32_t arg_num, zval *sock_array, fd_set *fds, PHP_SOCKET *max_fd) /* {{{ */
517{
518 zval *element;
519 php_socket *php_sock;
520 int num = 0;
521
522 if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
523
524 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(sock_array), element) {
525 ZVAL_DEREF(element);
526
527 if (Z_TYPE_P(element) != IS_OBJECT || Z_OBJCE_P(element) != socket_ce) {
528 zend_argument_type_error(arg_num, "must only have elements of type Socket, %s given", zend_zval_value_name(element));
529 return -1;
530 }
531
532 php_sock = Z_SOCKET_P(element);
533 if (IS_INVALID_SOCKET(php_sock)) {
534 zend_argument_type_error(arg_num, "contains a closed socket");
535 return -1;
536 }
537
538 PHP_SAFE_FD_SET(php_sock->bsd_socket, fds);
539 if (php_sock->bsd_socket > *max_fd) {
540 *max_fd = php_sock->bsd_socket;
541 }
542 num++;
544
545 return num ? 1 : 0;
546}
547/* }}} */
548
549static void php_sock_array_from_fd_set(zval *sock_array, fd_set *fds) /* {{{ */
550{
551 zval *element;
552 zval *dest_element;
553 php_socket *php_sock;
554 zval new_hash;
555 zend_ulong num_key;
557
558 ZEND_ASSERT(Z_TYPE_P(sock_array) == IS_ARRAY);
559
560 array_init(&new_hash);
561 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(sock_array), num_key, key, element) {
562 ZVAL_DEREF(element);
563
564 php_sock = Z_SOCKET_P(element);
565 ZEND_ASSERT(php_sock); /* element is supposed to be Socket object */
566 ZEND_ASSERT(!IS_INVALID_SOCKET(php_sock));
567
568 if (PHP_SAFE_FD_ISSET(php_sock->bsd_socket, fds)) {
569 /* Add fd to new array */
570 if (key) {
571 dest_element = zend_hash_add(Z_ARRVAL(new_hash), key, element);
572 } else {
573 dest_element = zend_hash_index_update(Z_ARRVAL(new_hash), num_key, element);
574 }
575 if (dest_element) {
576 Z_ADDREF_P(dest_element);
577 }
578 }
580
581 /* Destroy old array, add new one */
582 zval_ptr_dtor(sock_array);
583
584 ZVAL_COPY_VALUE(sock_array, &new_hash);
585}
586/* }}} */
587
588/* {{{ Runs the select() system call on the sets mentioned with a timeout specified by tv_sec and tv_usec */
590{
591 zval *r_array, *w_array, *e_array;
592 struct timeval tv;
593 struct timeval *tv_p = NULL;
594 fd_set rfds, wfds, efds;
595 PHP_SOCKET max_fd = 0;
596 int retval, sets = 0;
597 zend_long sec, usec = 0;
598 bool sec_is_null = 0;
599
601 Z_PARAM_ARRAY_EX2(r_array, 1, 1, 0)
602 Z_PARAM_ARRAY_EX2(w_array, 1, 1, 0)
603 Z_PARAM_ARRAY_EX2(e_array, 1, 1, 0)
604 Z_PARAM_LONG_OR_NULL(sec, sec_is_null)
606 Z_PARAM_LONG(usec)
608
609 FD_ZERO(&rfds);
610 FD_ZERO(&wfds);
611 FD_ZERO(&efds);
612
613 if (r_array != NULL) {
614 sets += retval = php_sock_array_to_fd_set(1, r_array, &rfds, &max_fd);
615 if (retval == -1) {
617 }
618 }
619 if (w_array != NULL) {
620 sets += retval = php_sock_array_to_fd_set(2, w_array, &wfds, &max_fd);
621 if (retval == -1) {
623 }
624 }
625 if (e_array != NULL) {
626 sets += retval = php_sock_array_to_fd_set(3, e_array, &efds, &max_fd);
627 if (retval == -1) {
629 }
630 }
631
632 if (!sets) {
633 zend_value_error("socket_select(): At least one array argument must be passed");
635 }
636
637 if (!PHP_SAFE_MAX_FD(max_fd, 0)) {
639 }
640
641 /* If seconds is not set to null, build the timeval, else we wait indefinitely */
642 if (!sec_is_null) {
643 /* Solaris + BSD do not like microsecond values which are >= 1 sec */
644 if (usec > 999999) {
645 tv.tv_sec = sec + (usec / 1000000);
646 tv.tv_usec = usec % 1000000;
647 } else {
648 tv.tv_sec = sec;
649 tv.tv_usec = usec;
650 }
651
652 tv_p = &tv;
653 }
654
655 retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
656
657 if (retval == -1) {
658 SOCKETS_G(last_error) = errno;
659 php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s", errno, sockets_strerror(errno));
661 }
662
663 if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds);
664 if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds);
665 if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds);
666
668}
669/* }}} */
670
671/* {{{ Opens a socket on port to accept connections */
673{
674 php_socket *php_sock;
675 zend_long port, backlog = SOMAXCONN;
676
678 Z_PARAM_LONG(port)
680 Z_PARAM_LONG(backlog)
682
684 php_sock = Z_SOCKET_P(return_value);
685
686 if (!php_open_listen_sock(php_sock, port, backlog)) {
689 }
690
691 php_sock->error = 0;
692 php_sock->blocking = 1;
693}
694/* }}} */
695
696/* {{{ Accepts a connection on the listening socket fd */
698{
699 zval *arg1;
700 php_socket *php_sock, *new_sock;
702 socklen_t php_sa_len = sizeof(sa);
703
707
708 php_sock = Z_SOCKET_P(arg1);
709 ENSURE_SOCKET_VALID(php_sock);
710
712 new_sock = Z_SOCKET_P(return_value);
713
714 if (!php_accept_connect(php_sock, new_sock, (struct sockaddr*)&sa, &php_sa_len)) {
717 }
718}
719/* }}} */
720
721/* {{{ Sets nonblocking mode on a socket resource */
723{
724 zval *arg1;
725 php_socket *php_sock;
726
730
731 php_sock = Z_SOCKET_P(arg1);
732 ENSURE_SOCKET_VALID(php_sock);
733
734 if (!Z_ISUNDEF(php_sock->zstream)) {
735 php_stream *stream;
736 /* omit notice if resource doesn't exist anymore */
737 stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
738 if (stream != NULL) {
740 NULL) != -1) {
741 php_sock->blocking = 0;
743 }
744 }
745 }
746
747 if (php_set_sock_blocking(php_sock->bsd_socket, 0) == SUCCESS) {
748 php_sock->blocking = 0;
750 } else {
751 PHP_SOCKET_ERROR(php_sock, "unable to set nonblocking mode", errno);
753 }
754}
755/* }}} */
756
757/* {{{ Sets blocking mode on a socket resource */
759{
760 zval *arg1;
761 php_socket *php_sock;
762
766
767 php_sock = Z_SOCKET_P(arg1);
768 ENSURE_SOCKET_VALID(php_sock);
769
770 /* if socket was created from a stream, give the stream a chance to take
771 * care of the operation itself, thereby allowing it to update its internal
772 * state */
773 if (!Z_ISUNDEF(php_sock->zstream)) {
774 php_stream *stream;
775 stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
776 if (stream != NULL) {
778 NULL) != -1) {
779 php_sock->blocking = 1;
781 }
782 }
783 }
784
785 if (php_set_sock_blocking(php_sock->bsd_socket, 1) == SUCCESS) {
786 php_sock->blocking = 1;
788 } else {
789 PHP_SOCKET_ERROR(php_sock, "unable to set blocking mode", errno);
791 }
792}
793/* }}} */
794
795/* {{{ Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
797{
798 zval *arg1;
799 php_socket *php_sock;
800 zend_long backlog = 0;
801
805 Z_PARAM_LONG(backlog)
807
808 php_sock = Z_SOCKET_P(arg1);
809 ENSURE_SOCKET_VALID(php_sock);
810
811 if (listen(php_sock->bsd_socket, backlog) != 0) {
812 PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
814 }
816}
817/* }}} */
818
819/* {{{ Closes a file descriptor */
821{
822 zval *arg1;
823 php_socket *php_socket;
824
828
829 php_socket = Z_SOCKET_P(arg1);
830 ENSURE_SOCKET_VALID(php_socket);
831
832 if (!Z_ISUNDEF(php_socket->zstream)) {
833 php_stream *stream = NULL;
834 php_stream_from_zval_no_verify(stream, &php_socket->zstream);
835 if (stream != NULL) {
836 /* close & destroy stream, incl. removing it from the rsrc list;
837 * resource stored in php_sock->zstream will become invalid */
838 php_stream_free(stream,
841 }
842 } else {
843 if (!IS_INVALID_SOCKET(php_socket)) {
844 close(php_socket->bsd_socket);
845 }
846 }
847
848 ZVAL_UNDEF(&php_socket->zstream);
849 php_socket->bsd_socket = -1;
850}
851/* }}} */
852
853/* {{{ Writes the buffer to the socket resource, length is optional */
855{
856 zval *arg1;
857 php_socket *php_sock;
858 int retval;
859 size_t str_len;
860 zend_long length = 0;
861 bool length_is_null = 1;
862 char *str;
863
866 Z_PARAM_STRING(str, str_len)
868 Z_PARAM_LONG_OR_NULL(length, length_is_null)
870
871 php_sock = Z_SOCKET_P(arg1);
872 ENSURE_SOCKET_VALID(php_sock);
873
874 if (length < 0) {
875 zend_argument_value_error(3, "must be greater than or equal to 0");
877 }
878
879 if (length_is_null) {
880 length = str_len;
881 }
882
883#ifndef PHP_WIN32
884 retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
885#else
886 retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
887#endif
888
889 if (retval < 0) {
890 PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
892 }
893
895}
896/* }}} */
897
898/* {{{ Reads a maximum of length bytes from socket */
900{
901 zval *arg1;
902 php_socket *php_sock;
903 zend_string *tmpbuf;
904 int retval;
906
909 Z_PARAM_LONG(length)
913
914 php_sock = Z_SOCKET_P(arg1);
915 ENSURE_SOCKET_VALID(php_sock);
916
917 /* overflow check */
918 if (length <= 0 || length == ZEND_LONG_MAX) {
920 }
921
922 tmpbuf = zend_string_alloc(length, 0);
923
924 if (type == PHP_NORMAL_READ) {
925 retval = php_read(php_sock, ZSTR_VAL(tmpbuf), length, 0);
926 } else {
927 retval = recv(php_sock->bsd_socket, ZSTR_VAL(tmpbuf), length, 0);
928 }
929
930 if (retval == -1) {
931 /* if the socket is in non-blocking mode and there's no data to read,
932 don't output any error, as this is a normal situation, and not an error */
934 php_sock->error = errno;
935 SOCKETS_G(last_error) = errno;
936 } else {
937 PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
938 }
939
940 zend_string_efree(tmpbuf);
942 } else if (!retval) {
943 zend_string_efree(tmpbuf);
945 }
946
947 tmpbuf = zend_string_truncate(tmpbuf, retval, 0);
948 ZSTR_LEN(tmpbuf) = retval;
949 ZSTR_VAL(tmpbuf)[ZSTR_LEN(tmpbuf)] = '\0' ;
950
951 RETURN_NEW_STR(tmpbuf);
952}
953/* }}} */
954
955/* {{{ Queries the remote side of the given socket which may either result in host/port or in a UNIX filesystem path, dependent on its type. */
957{
958 zval *arg1, *addr, *port = NULL;
959 php_sockaddr_storage sa_storage = {0};
960 php_socket *php_sock;
961 struct sockaddr *sa;
962 struct sockaddr_in *sin;
963#ifdef HAVE_IPV6
964 struct sockaddr_in6 *sin6;
965#endif
966 char addrbuf[INET6_ADDRSTRLEN];
967 struct sockaddr_un *s_un;
968 const char *addr_string;
969 socklen_t salen = sizeof(php_sockaddr_storage);
970
975 Z_PARAM_ZVAL(port)
977
978 php_sock = Z_SOCKET_P(arg1);
979 ENSURE_SOCKET_VALID(php_sock);
980
981 sa = (struct sockaddr *) &sa_storage;
982
983 if (getsockname(php_sock->bsd_socket, sa, &salen) != 0) {
984 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket name", errno);
986 }
987
988 switch (sa->sa_family) {
989#ifdef HAVE_IPV6
990 case AF_INET6:
991 sin6 = (struct sockaddr_in6 *) sa;
992 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
994
995 if (port != NULL) {
996 ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin6->sin6_port));
997 }
999 break;
1000#endif
1001 case AF_INET:
1002 sin = (struct sockaddr_in *) sa;
1003 addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
1004 ZEND_TRY_ASSIGN_REF_STRING(addr, addr_string);
1005
1006 if (port != NULL) {
1007 ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin->sin_port));
1008 }
1010 break;
1011
1012 case AF_UNIX:
1013 s_un = (struct sockaddr_un *) sa;
1014
1015 ZEND_TRY_ASSIGN_REF_STRING(addr, s_un->sun_path);
1017 break;
1018
1019 default:
1020 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1021 RETURN_THROWS();
1022 }
1023}
1024/* }}} */
1025
1026/* {{{ Queries the remote side of the given socket which may either result in host/port or in a UNIX filesystem path, dependent on its type. */
1028{
1029 zval *arg1, *arg2, *arg3 = NULL;
1030 php_sockaddr_storage sa_storage = {0};
1031 php_socket *php_sock;
1032 struct sockaddr *sa;
1033 struct sockaddr_in *sin;
1034#ifdef HAVE_IPV6
1035 struct sockaddr_in6 *sin6;
1036#endif
1037 char addrbuf[INET6_ADDRSTRLEN];
1038 struct sockaddr_un *s_un;
1039 const char *addr_string;
1040 socklen_t salen = sizeof(php_sockaddr_storage);
1041
1048
1049 php_sock = Z_SOCKET_P(arg1);
1050 ENSURE_SOCKET_VALID(php_sock);
1051
1052 sa = (struct sockaddr *) &sa_storage;
1053
1054 if (getpeername(php_sock->bsd_socket, sa, &salen) < 0) {
1055 PHP_SOCKET_ERROR(php_sock, "unable to retrieve peer name", errno);
1057 }
1058
1059 switch (sa->sa_family) {
1060#ifdef HAVE_IPV6
1061 case AF_INET6:
1062 sin6 = (struct sockaddr_in6 *) sa;
1063 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
1064
1066
1067 if (arg3 != NULL) {
1068 ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin6->sin6_port));
1069 }
1070
1072 break;
1073#endif
1074 case AF_INET:
1075 sin = (struct sockaddr_in *) sa;
1076 addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
1077 ZEND_TRY_ASSIGN_REF_STRING(arg2, addr_string);
1078
1079 if (arg3 != NULL) {
1080 ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin->sin_port));
1081 }
1082
1084 break;
1085
1086 case AF_UNIX:
1087 s_un = (struct sockaddr_un *) sa;
1088
1089 ZEND_TRY_ASSIGN_REF_STRING(arg2, s_un->sun_path);
1091 break;
1092
1093 default:
1094 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1095 RETURN_THROWS();
1096 }
1097}
1098/* }}} */
1099
1100/* {{{ Creates an endpoint for communication in the domain specified by domain, of type specified by type */
1102{
1103 zend_long domain, type, checktype, protocol;
1104 php_socket *php_sock;
1105
1107 Z_PARAM_LONG(domain)
1109 Z_PARAM_LONG(protocol)
1111
1112 if (domain != AF_UNIX
1113#ifdef HAVE_IPV6
1114 && domain != AF_INET6
1115#endif
1116 && domain != AF_INET) {
1117 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET6, or AF_INET");
1118 RETURN_THROWS();
1119 }
1120
1121 checktype = type;
1122#ifdef SOCK_NONBLOCK
1123 checktype &= ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1124#endif
1125
1126 if (checktype > 10) {
1127 zend_argument_value_error(2, "must be one of SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET,"
1128 " SOCK_RAW, or SOCK_RDM"
1129#ifdef SOCK_NONBLOCK
1130 " optionally OR'ed with SOCK_CLOEXEC, SOCK_NONBLOCK"
1131#endif
1132 );
1133 RETURN_THROWS();
1134 }
1135
1137 php_sock = Z_SOCKET_P(return_value);
1138
1139 php_sock->bsd_socket = socket(domain, type, protocol);
1140 php_sock->type = domain;
1141
1142 if (IS_INVALID_SOCKET(php_sock)) {
1143 SOCKETS_G(last_error) = errno;
1144 php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
1147 }
1148
1149 php_sock->error = 0;
1150 php_sock->blocking = 1;
1151}
1152/* }}} */
1153
1154/* {{{ Opens a connection to addr:port on the socket specified by socket */
1156{
1157 zval *resource_socket;
1158 php_socket *php_sock;
1159 char *addr;
1160 int retval;
1161 size_t addr_len;
1162 zend_long port;
1163 bool port_is_null = 1;
1164
1166 Z_PARAM_OBJECT_OF_CLASS(resource_socket, socket_ce)
1167 Z_PARAM_STRING(addr, addr_len)
1169 Z_PARAM_LONG_OR_NULL(port, port_is_null)
1171
1172 php_sock = Z_SOCKET_P(resource_socket);
1173 ENSURE_SOCKET_VALID(php_sock);
1174
1175 switch(php_sock->type) {
1176#ifdef HAVE_IPV6
1177 case AF_INET6: {
1178 struct sockaddr_in6 sin6 = {0};
1179
1180 if (port_is_null) {
1181 zend_argument_value_error(3, "cannot be null when the socket type is AF_INET6");
1182 RETURN_THROWS();
1183 }
1184
1185 memset(&sin6, 0, sizeof(struct sockaddr_in6));
1186
1187 sin6.sin6_family = AF_INET6;
1188 sin6.sin6_port = htons((unsigned short int)port);
1189
1190 if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
1192 }
1193
1194 retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6));
1195 break;
1196 }
1197#endif
1198 case AF_INET: {
1199 struct sockaddr_in sin = {0};
1200
1201 if (port_is_null) {
1202 zend_argument_value_error(3, "cannot be null when the socket type is AF_INET");
1203 RETURN_THROWS();
1204 }
1205
1206 sin.sin_family = AF_INET;
1207 sin.sin_port = htons((unsigned short int)port);
1208
1209 if (! php_set_inet_addr(&sin, addr, php_sock)) {
1211 }
1212
1213 retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
1214 break;
1215 }
1216
1217 case AF_UNIX: {
1218 struct sockaddr_un s_un = {0};
1219
1220 if (addr_len >= sizeof(s_un.sun_path)) {
1221 zend_argument_value_error(2, "must be less than %d", sizeof(s_un.sun_path));
1222 RETURN_THROWS();
1223 }
1224
1225 s_un.sun_family = AF_UNIX;
1226 memcpy(&s_un.sun_path, addr, addr_len);
1227 retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un,
1228 (socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len));
1229 break;
1230 }
1231
1232 default:
1233 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1234 RETURN_THROWS();
1235 }
1236
1237 if (retval != 0) {
1238 PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
1240 }
1241
1243}
1244/* }}} */
1245
1246/* {{{ Returns a string describing an error */
1248{
1250
1254
1256 zend_argument_value_error(1, "must be between %d and %d", INT_MIN, INT_MAX);
1257 RETURN_THROWS();
1258 }
1259
1261}
1262/* }}} */
1263
1264/* {{{ Binds an open socket to a listening port, port is only specified in AF_INET family. */
1266{
1267 zval *arg1;
1268 php_sockaddr_storage sa_storage = {0};
1269 struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
1270 php_socket *php_sock;
1271 char *addr;
1272 size_t addr_len;
1273 zend_long port = 0;
1274 zend_long retval = 0;
1275
1278 Z_PARAM_STRING(addr, addr_len)
1280 Z_PARAM_LONG(port)
1282
1283 php_sock = Z_SOCKET_P(arg1);
1284 ENSURE_SOCKET_VALID(php_sock);
1285
1286 switch(php_sock->type) {
1287 case AF_UNIX:
1288 {
1289 struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
1290
1291 sa->sun_family = AF_UNIX;
1292
1293 if (addr_len >= sizeof(sa->sun_path)) {
1294 zend_argument_value_error(2, "must be less than %d", sizeof(sa->sun_path));
1295 RETURN_THROWS();
1296 }
1297 memcpy(&sa->sun_path, addr, addr_len);
1298
1299 retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa,
1300 offsetof(struct sockaddr_un, sun_path) + addr_len);
1301 break;
1302 }
1303
1304 case AF_INET:
1305 {
1306 struct sockaddr_in *sa = (struct sockaddr_in *) sock_type;
1307
1308 sa->sin_family = AF_INET;
1309 sa->sin_port = htons((unsigned short) port);
1310
1311 if (! php_set_inet_addr(sa, addr, php_sock)) {
1313 }
1314
1315 retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
1316 break;
1317 }
1318#ifdef HAVE_IPV6
1319 case AF_INET6:
1320 {
1321 struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
1322
1323 sa->sin6_family = AF_INET6;
1324 sa->sin6_port = htons((unsigned short) port);
1325
1326 if (! php_set_inet6_addr(sa, addr, php_sock)) {
1328 }
1329
1330 retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in6));
1331 break;
1332 }
1333#endif
1334 default:
1335 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1336 RETURN_THROWS();
1337 }
1338
1339 if (retval != 0) {
1340 PHP_SOCKET_ERROR(php_sock, "Unable to bind address", errno);
1342 }
1343
1345}
1346/* }}} */
1347
1348/* {{{ Receives data from a connected socket */
1350{
1351 zval *php_sock_res, *buf;
1352 zend_string *recv_buf;
1353 php_socket *php_sock;
1354 int retval;
1356
1358 Z_PARAM_OBJECT_OF_CLASS(php_sock_res, socket_ce)
1363
1364 php_sock = Z_SOCKET_P(php_sock_res);
1365 ENSURE_SOCKET_VALID(php_sock);
1366
1367 /* overflow check */
1368 if (len <= 0 || len == ZEND_LONG_MAX) {
1370 }
1371
1372 recv_buf = zend_string_alloc(len, 0);
1373
1374 if ((retval = recv(php_sock->bsd_socket, ZSTR_VAL(recv_buf), len, flags)) < 1) {
1375 zend_string_efree(recv_buf);
1377 } else {
1378 ZSTR_LEN(recv_buf) = retval;
1379 ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1381 }
1382
1383 if (retval == -1) {
1384 PHP_SOCKET_ERROR(php_sock, "Unable to read from socket", errno);
1386 }
1387
1389}
1390/* }}} */
1391
1392/* {{{ Sends data to a connected socket */
1394{
1395 zval *arg1;
1396 php_socket *php_sock;
1397 size_t buf_len, retval;
1399 char *buf;
1400
1403 Z_PARAM_STRING(buf, buf_len)
1407
1408 php_sock = Z_SOCKET_P(arg1);
1409 ENSURE_SOCKET_VALID(php_sock);
1410
1411 if (len < 0) {
1412 zend_argument_value_error(3, "must be greater than or equal to 0");
1413 RETURN_THROWS();
1414 }
1415
1416 retval = send(php_sock->bsd_socket, buf, (buf_len < (size_t)len ? buf_len : (size_t)len), flags);
1417
1418 if (retval == (size_t)-1) {
1419 PHP_SOCKET_ERROR(php_sock, "Unable to write to socket", errno);
1421 }
1422
1424}
1425/* }}} */
1426
1427/* {{{ Receives data from a socket, connected or not */
1429{
1430 zval *arg1, *arg2, *arg5, *arg6 = NULL;
1431 php_socket *php_sock;
1432 struct sockaddr_un s_un;
1433 struct sockaddr_in sin;
1434#ifdef HAVE_IPV6
1435 struct sockaddr_in6 sin6;
1436#endif
1437 char addrbuf[INET6_ADDRSTRLEN];
1438 socklen_t slen;
1439 int retval;
1440 zend_long arg3, arg4;
1441 const char *address;
1442 zend_string *recv_buf;
1443
1448 Z_PARAM_LONG(arg4)
1449 Z_PARAM_ZVAL(arg5)
1451 Z_PARAM_ZVAL(arg6)
1453
1454 php_sock = Z_SOCKET_P(arg1);
1455 ENSURE_SOCKET_VALID(php_sock);
1456
1457 /* overflow check */
1458 /* Shouldthrow ? */
1459
1462 }
1463
1464 recv_buf = zend_string_alloc(arg3 + 1, 0);
1465
1466 switch (php_sock->type) {
1467 case AF_UNIX:
1468 slen = sizeof(s_un);
1469 memset(&s_un, 0, slen);
1470 s_un.sun_family = AF_UNIX;
1471
1472 retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
1473
1474 if (retval < 0) {
1475 PHP_SOCKET_ERROR(php_sock, "Unable to recvfrom", errno);
1476 zend_string_efree(recv_buf);
1478 }
1479 ZSTR_LEN(recv_buf) = retval;
1480 ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1481
1483 ZEND_TRY_ASSIGN_REF_STRING(arg5, s_un.sun_path);
1484 break;
1485
1486 case AF_INET:
1487 slen = sizeof(sin);
1488 memset(&sin, 0, slen);
1489 sin.sin_family = AF_INET;
1490
1491 if (arg6 == NULL) {
1492 zend_string_efree(recv_buf);
1494 }
1495
1496 retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
1497
1498 if (retval < 0) {
1499 PHP_SOCKET_ERROR(php_sock, "Unable to recvfrom", errno);
1500 zend_string_efree(recv_buf);
1502 }
1503 ZSTR_LEN(recv_buf) = retval;
1504 ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1505
1506 address = inet_ntop(AF_INET, &sin.sin_addr, addrbuf, sizeof(addrbuf));
1507
1509 ZEND_TRY_ASSIGN_REF_STRING(arg5, address ? address : "0.0.0.0");
1510 ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin.sin_port));
1511 break;
1512#ifdef HAVE_IPV6
1513 case AF_INET6:
1514 slen = sizeof(sin6);
1515 memset(&sin6, 0, slen);
1516 sin6.sin6_family = AF_INET6;
1517
1518 if (arg6 == NULL) {
1519 zend_string_efree(recv_buf);
1521 }
1522
1523 retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
1524
1525 if (retval < 0) {
1526 PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
1527 zend_string_efree(recv_buf);
1529 }
1530 ZSTR_LEN(recv_buf) = retval;
1531 ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1532
1533 memset(addrbuf, 0, INET6_ADDRSTRLEN);
1534 inet_ntop(AF_INET6, &sin6.sin6_addr, addrbuf, sizeof(addrbuf));
1535
1537 ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
1538 ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
1539 break;
1540#endif
1541 default:
1542 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1543 RETURN_THROWS();
1544 }
1545
1547}
1548/* }}} */
1549
1550/* {{{ Sends a message to a socket, whether it is connected or not */
1552{
1553 zval *arg1;
1554 php_socket *php_sock;
1555 struct sockaddr_un s_un;
1556 struct sockaddr_in sin;
1557#ifdef HAVE_IPV6
1558 struct sockaddr_in6 sin6;
1559#endif
1560 int retval;
1561 size_t buf_len, addr_len;
1562 zend_long len, flags, port;
1563 bool port_is_null = 1;
1564 char *buf, *addr;
1565
1568 Z_PARAM_STRING(buf, buf_len)
1571 Z_PARAM_STRING(addr, addr_len)
1573 Z_PARAM_LONG_OR_NULL(port, port_is_null)
1575
1576 php_sock = Z_SOCKET_P(arg1);
1577 ENSURE_SOCKET_VALID(php_sock);
1578
1579 if (len < 0) {
1580 zend_argument_value_error(3, "must be greater than or equal to 0");
1581 RETURN_THROWS();
1582 }
1583
1584 switch (php_sock->type) {
1585 case AF_UNIX:
1586 memset(&s_un, 0, sizeof(s_un));
1587 s_un.sun_family = AF_UNIX;
1588 snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", addr);
1589
1590 retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
1591 break;
1592
1593 case AF_INET:
1594 if (port_is_null) {
1595 zend_argument_value_error(6, "cannot be null when the socket type is AF_INET");
1596 RETURN_THROWS();
1597 }
1598
1599 memset(&sin, 0, sizeof(sin));
1600 sin.sin_family = AF_INET;
1601 sin.sin_port = htons((unsigned short) port);
1602
1603 if (! php_set_inet_addr(&sin, addr, php_sock)) {
1605 }
1606
1607 retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin, sizeof(sin));
1608 break;
1609#ifdef HAVE_IPV6
1610 case AF_INET6:
1611 if (port_is_null) {
1612 zend_argument_value_error(6, "cannot be null when the socket type is AF_INET6");
1613 RETURN_THROWS();
1614 }
1615
1616 memset(&sin6, 0, sizeof(sin6));
1617 sin6.sin6_family = AF_INET6;
1618 sin6.sin6_port = htons((unsigned short) port);
1619
1620 if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
1622 }
1623
1624 retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin6, sizeof(sin6));
1625 break;
1626#endif
1627 default:
1628 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1629 RETURN_THROWS();
1630 }
1631
1632 if (retval == -1) {
1633 PHP_SOCKET_ERROR(php_sock, "Unable to write to socket", errno);
1635 }
1636
1638}
1639/* }}} */
1640
1641/* {{{ Gets socket options for the socket */
1643{
1644 zval *arg1;
1645 struct linger linger_val;
1646 struct timeval tv;
1647#ifdef PHP_WIN32
1648 int timeout = 0;
1649#endif
1650 socklen_t optlen;
1651 php_socket *php_sock;
1652 int other_val;
1653 zend_long level, optname;
1654
1657 Z_PARAM_LONG(level)
1658 Z_PARAM_LONG(optname)
1660
1661 php_sock = Z_SOCKET_P(arg1);
1662 ENSURE_SOCKET_VALID(php_sock);
1663
1664 if (level == IPPROTO_IP) {
1665 switch (optname) {
1666 case IP_MULTICAST_IF: {
1667 struct in_addr if_addr;
1668 unsigned int if_index;
1669 optlen = sizeof(if_addr);
1670 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&if_addr, &optlen) != 0) {
1671 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1673 }
1674 if (php_add4_to_if_index(&if_addr, php_sock, &if_index) == SUCCESS) {
1675 RETURN_LONG((zend_long) if_index);
1676 } else {
1678 }
1679 }
1680 }
1681 }
1682#ifdef HAVE_IPV6
1683 else if (level == IPPROTO_IPV6) {
1684 int ret = php_do_getsockopt_ipv6_rfc3542(php_sock, level, optname, return_value);
1685 if (ret == SUCCESS) {
1686 return;
1687 } else if (ret == FAILURE) {
1689 } /* else continue */
1690 }
1691#endif
1692
1693 if (level == IPPROTO_TCP) {
1694 switch (optname) {
1695#ifdef TCP_CONGESTION
1696 case TCP_CONGESTION: {
1697 char name[16];
1698 optlen = sizeof(name);
1699 if (getsockopt(php_sock->bsd_socket, level, optname, name, &optlen) != 0) {
1700 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1702 } else {
1704
1705 add_assoc_string(return_value, "name", name);
1706 return;
1707 }
1708 }
1709#endif
1710 }
1711 }
1712
1713 if (level == SOL_SOCKET) {
1714 switch (optname) {
1715#ifdef SO_LINGER_SEC
1716 case SO_LINGER_SEC:
1717#endif
1718 case SO_LINGER:
1719 optlen = sizeof(linger_val);
1720
1721 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&linger_val, &optlen) != 0) {
1722 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1724 }
1725
1727 add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
1728 add_assoc_long(return_value, "l_linger", linger_val.l_linger);
1729 return;
1730
1731 case SO_RCVTIMEO:
1732 case SO_SNDTIMEO:
1733#ifndef PHP_WIN32
1734 optlen = sizeof(tv);
1735
1736 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
1737 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1739 }
1740#else
1741 optlen = sizeof(int);
1742
1743 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&timeout, &optlen) != 0) {
1744 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1746 }
1747
1748 tv.tv_sec = timeout ? timeout / 1000 : 0;
1749 tv.tv_usec = timeout ? (timeout * 1000) % 1000000 : 0;
1750#endif
1751
1753
1754 add_assoc_long(return_value, "sec", tv.tv_sec);
1755 add_assoc_long(return_value, "usec", tv.tv_usec);
1756 return;
1757#ifdef SO_MEMINFO
1758 case SO_MEMINFO: {
1759 uint32_t minfo[SK_MEMINFO_VARS];
1760 optlen = sizeof(minfo);
1761
1762 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)minfo, &optlen) != 0) {
1763 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1765 }
1766
1767 if (UNEXPECTED(optlen != sizeof(minfo))) {
1768 // unlikely since the kernel fills up the whole array if getsockopt succeeded
1769 // but just an extra precaution in case.
1770 php_error_docref(NULL, E_WARNING, "Unable to retrieve all socket meminfo data");
1772 }
1773
1775
1776 add_assoc_long(return_value, "rmem_alloc", minfo[SK_MEMINFO_RMEM_ALLOC]);
1777 add_assoc_long(return_value, "rcvbuf", minfo[SK_MEMINFO_RCVBUF]);
1778 add_assoc_long(return_value, "wmem_alloc", minfo[SK_MEMINFO_WMEM_ALLOC]);
1779 add_assoc_long(return_value, "sndbuf", minfo[SK_MEMINFO_SNDBUF]);
1780 add_assoc_long(return_value, "fwd_alloc", minfo[SK_MEMINFO_FWD_ALLOC]);
1781 add_assoc_long(return_value, "wmem_queued", minfo[SK_MEMINFO_WMEM_QUEUED]);
1782 add_assoc_long(return_value, "optmem", minfo[SK_MEMINFO_OPTMEM]);
1783 add_assoc_long(return_value, "backlog", minfo[SK_MEMINFO_BACKLOG]);
1784 add_assoc_long(return_value, "drops", minfo[SK_MEMINFO_DROPS]);
1785 return;
1786 }
1787#endif
1788#ifdef SO_ACCEPTFILTER
1789 case SO_ACCEPTFILTER: {
1790
1791 struct accept_filter_arg af = {0};
1792 optlen = sizeof(af);
1793
1794 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&af, &optlen) != 0) {
1795 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1797 }
1798
1800
1801 add_assoc_string(return_value, "af_name", af.af_name);
1802 return;
1803 }
1804#endif
1805
1806 }
1807 }
1808
1809#ifdef SOL_FILTER
1810 if (level == SOL_FILTER) {
1811 switch (optname) {
1812
1813 case FIL_LIST: {
1814 size_t i;
1815 struct fil_info fi[32] = {{0}};
1816 optlen = sizeof(fi);
1817
1818 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)fi, &optlen) != 0) {
1819 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1821 }
1822
1824
1825 for (i = 0; i < optlen / sizeof(struct fil_info); i++) {
1826 add_index_string(return_value, i, fi[i].fi_name);
1827 }
1828
1829 return;
1830 }
1831 }
1832 }
1833#endif
1834
1835 optlen = sizeof(other_val);
1836
1837 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&other_val, &optlen) != 0) {
1838 PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1840 }
1841
1842 if (optlen == 1) {
1843 other_val = *((unsigned char *)&other_val);
1844 }
1845
1846 RETURN_LONG(other_val);
1847}
1848/* }}} */
1849
1850/* {{{ Sets socket options for the socket */
1852{
1853 zval *arg1, *arg4;
1854 struct linger lv;
1855 php_socket *php_sock;
1856 int ov, optlen, retval;
1857#ifdef PHP_WIN32
1858 int timeout;
1859#else
1860 struct timeval tv;
1861#endif
1862 zend_long level, optname;
1863 void *opt_ptr;
1864 HashTable *opt_ht;
1865 zval *l_onoff, *l_linger;
1866 zval *sec, *usec;
1867
1870 Z_PARAM_LONG(level)
1871 Z_PARAM_LONG(optname)
1872 Z_PARAM_ZVAL(arg4)
1874
1875 php_sock = Z_SOCKET_P(arg1);
1876 ENSURE_SOCKET_VALID(php_sock);
1877
1878 set_errno(0);
1879
1880#define HANDLE_SUBCALL(res) \
1881 do { \
1882 if (res == 1) { goto default_case; } \
1883 else if (res == SUCCESS) { RETURN_TRUE; } \
1884 else { RETURN_FALSE; } \
1885 } while (0)
1886
1887
1888 if (level == IPPROTO_IP) {
1889 int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4);
1891 }
1892
1893#ifdef HAVE_IPV6
1894 else if (level == IPPROTO_IPV6) {
1895 int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4);
1896 if (res == 1) {
1897 res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4);
1898 }
1900 }
1901#endif
1902
1903 if (level == IPPROTO_TCP) {
1904 switch (optname) {
1905#ifdef TCP_CONGESTION
1906 case TCP_CONGESTION: {
1907 if (Z_TYPE_P(arg4) == IS_STRING) {
1908 opt_ptr = Z_STRVAL_P(arg4);
1909 optlen = Z_STRLEN_P(arg4);
1910 } else {
1911 opt_ptr = "";
1912 optlen = 0;
1913 }
1914 if (setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen) != 0) {
1915 PHP_SOCKET_ERROR(php_sock, "Unable to set socket option", errno);
1917 }
1918
1920 }
1921#endif
1922 }
1923 }
1924
1925 switch (optname) {
1926#ifdef SO_LINGER_SEC
1927 case SO_LINGER_SEC:
1928#endif
1929 case SO_LINGER: {
1930 const char l_onoff_key[] = "l_onoff";
1931 const char l_linger_key[] = "l_linger";
1932
1933 if (Z_TYPE_P(arg4) != IS_ARRAY) {
1934 if (UNEXPECTED(Z_TYPE_P(arg4) != IS_OBJECT)) {
1935 zend_argument_type_error(4, "must be of type array when argument #3 ($option) is SO_LINGER, %s given", zend_zval_value_name(arg4));
1936 RETURN_THROWS();
1937 } else {
1938 opt_ht = Z_OBJPROP_P(arg4);
1939 }
1940 } else {
1941 opt_ht = Z_ARRVAL_P(arg4);
1942 }
1943
1944 if ((l_onoff = zend_hash_str_find(opt_ht, l_onoff_key, sizeof(l_onoff_key) - 1)) == NULL) {
1945 zend_argument_value_error(4, "must have key \"%s\"", l_onoff_key);
1946 RETURN_THROWS();
1947 }
1948 if ((l_linger = zend_hash_str_find(opt_ht, l_linger_key, sizeof(l_linger_key) - 1)) == NULL) {
1949 zend_argument_value_error(4, "must have key \"%s\"", l_linger_key);
1950 RETURN_THROWS();
1951 }
1952
1953 zend_long val_lonoff = zval_get_long(l_onoff);
1954 zend_long val_linger = zval_get_long(l_linger);
1955
1956 if (val_lonoff < 0 || val_lonoff > USHRT_MAX) {
1957 zend_argument_value_error(4, "\"%s\" must be between 0 and %u", l_onoff_key, USHRT_MAX);
1958 RETURN_THROWS();
1959 }
1960
1961 if (val_linger < 0 || val_linger > USHRT_MAX) {
1962 zend_argument_value_error(4, "\"%s\" must be between 0 and %d", l_linger, USHRT_MAX);
1963 RETURN_THROWS();
1964 }
1965
1966 lv.l_onoff = (unsigned short)val_lonoff;
1967 lv.l_linger = (unsigned short)val_linger;
1968
1969 optlen = sizeof(lv);
1970 opt_ptr = &lv;
1971 break;
1972 }
1973
1974 case SO_RCVTIMEO:
1975 case SO_SNDTIMEO: {
1976 const char sec_key[] = "sec";
1977 const char usec_key[] = "usec";
1978
1979 if (Z_TYPE_P(arg4) != IS_ARRAY) {
1980 if (UNEXPECTED(Z_TYPE_P(arg4) != IS_OBJECT)) {
1981 zend_argument_type_error(4, "must be of type array when argument #3 ($option) is %s, %s given",
1982 optname == SO_RCVTIMEO ? "SO_RCVTIMEO" : "SO_SNDTIMEO",
1983 zend_zval_value_name(arg4));
1984 RETURN_THROWS();
1985 } else {
1986 opt_ht = Z_OBJPROP_P(arg4);
1987 }
1988 } else {
1989 opt_ht = Z_ARRVAL_P(arg4);
1990 }
1991
1992 if ((sec = zend_hash_str_find(opt_ht, sec_key, sizeof(sec_key) - 1)) == NULL) {
1993 zend_argument_value_error(4, "must have key \"%s\"", sec_key);
1994 RETURN_THROWS();
1995 }
1996 if ((usec = zend_hash_str_find(opt_ht, usec_key, sizeof(usec_key) - 1)) == NULL) {
1997 zend_argument_value_error(4, "must have key \"%s\"", usec_key);
1998 RETURN_THROWS();
1999 }
2000
2001 zend_long valsec = zval_get_long(sec);
2002 zend_long valusec = zval_get_long(usec);
2003#ifndef PHP_WIN32
2004 tv.tv_sec = valsec;
2005 tv.tv_usec = valusec;
2006 optlen = sizeof(tv);
2007 opt_ptr = &tv;
2008#else
2009 timeout = valsec * 1000 + valusec / 1000;
2010
2011 optlen = sizeof(int);
2012 opt_ptr = &timeout;
2013#endif
2014 break;
2015 }
2016#ifdef SO_BINDTODEVICE
2017 case SO_BINDTODEVICE: {
2018 if (Z_TYPE_P(arg4) == IS_STRING) {
2019 opt_ptr = Z_STRVAL_P(arg4);
2020 optlen = Z_STRLEN_P(arg4);
2021 } else {
2022 opt_ptr = "";
2023 optlen = 0;
2024 }
2025 break;
2026 }
2027#endif
2028
2029#ifdef SO_ACCEPTFILTER
2030 case SO_ACCEPTFILTER: {
2031 if (Z_TYPE_P(arg4) != IS_STRING) {
2032 php_error_docref(NULL, E_WARNING, "Invalid filter argument type");
2034 }
2035 struct accept_filter_arg af = {0};
2036 strlcpy(af.af_name, Z_STRVAL_P(arg4), sizeof(af.af_name));
2037 opt_ptr = &af;
2038 optlen = sizeof(af);
2039 break;
2040 }
2041#endif
2042
2043#ifdef FIL_ATTACH
2044 case FIL_ATTACH:
2045 case FIL_DETACH: {
2046 if (level != SOL_FILTER) {
2047 php_error_docref(NULL, E_WARNING, "Invalid level");
2049 }
2050 if (Z_TYPE_P(arg4) != IS_STRING) {
2051 php_error_docref(NULL, E_WARNING, "Invalid filter argument type");
2053 }
2054 opt_ptr = Z_STRVAL_P(arg4);
2055 optlen = Z_STRLEN_P(arg4);
2056 break;
2057 }
2058#endif
2059
2060#ifdef SO_ATTACH_REUSEPORT_CBPF
2062 zend_long cbpf_val = zval_get_long(arg4);
2063
2064 if (!cbpf_val) {
2065 ov = 1;
2066 optlen = sizeof(ov);
2067 opt_ptr = &ov;
2068 optname = SO_DETACH_BPF;
2069 } else {
2070 uint32_t k = (uint32_t)cbpf_val;
2071 static struct sock_filter cbpf[8] = {0};
2072 static struct sock_fprog bpfprog;
2073
2074 switch (k) {
2075 case SKF_AD_CPU:
2076 case SKF_AD_QUEUE:
2077 cbpf[0].code = (BPF_LD|BPF_W|BPF_ABS);
2078 cbpf[0].k = (uint32_t)(SKF_AD_OFF + k);
2079 cbpf[1].code = (BPF_RET|BPF_A);
2080 bpfprog.len = 2;
2081 break;
2082 default:
2083 php_error_docref(NULL, E_WARNING, "Unsupported CBPF filter");
2085 }
2086
2087 bpfprog.filter = cbpf;
2088 optlen = sizeof(bpfprog);
2089 opt_ptr = &bpfprog;
2090 }
2091 break;
2092 }
2093#endif
2094
2095 default:
2096default_case:
2097 ov = zval_get_long(arg4);
2098
2099 optlen = sizeof(ov);
2100 opt_ptr = &ov;
2101 break;
2102 }
2103
2104 retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
2105 if (retval != 0) {
2106 PHP_SOCKET_ERROR(php_sock, "Unable to set socket option", errno);
2108 }
2109
2111}
2112/* }}} */
2113
2114#ifdef HAVE_SOCKETPAIR
2115/* {{{ Creates a pair of indistinguishable sockets and stores them in fds. */
2117{
2118 zval retval[2], *fds_array_zval;
2119 php_socket *php_sock[2];
2120 PHP_SOCKET fds_array[2];
2121 zend_long domain, type, checktype, protocol;
2122
2124 Z_PARAM_LONG(domain)
2126 Z_PARAM_LONG(protocol)
2127 Z_PARAM_ZVAL(fds_array_zval)
2129
2130 if (domain != AF_INET
2131#ifdef HAVE_IPV6
2132 && domain != AF_INET6
2133#endif
2134 && domain != AF_UNIX) {
2135 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET6, or AF_INET");
2136 RETURN_THROWS();
2137 }
2138
2139 checktype = type;
2140#ifdef SOCK_NONBLOCK
2141 checktype &= ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
2142#endif
2143
2144 if (checktype > 10) {
2145 zend_argument_value_error(2, "must be one of SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET,"
2146 " SOCK_RAW, or SOCK_RDM"
2147#ifdef SOCK_NONBLOCK
2148 " optionally OR'ed with SOCK_CLOEXEC, SOCK_NONBLOCK"
2149#endif
2150 );
2151 RETURN_THROWS();
2152 }
2153
2155 php_sock[0] = Z_SOCKET_P(&retval[0]);
2156
2158 php_sock[1] = Z_SOCKET_P(&retval[1]);
2159
2160 if (socketpair(domain, type, protocol, fds_array) != 0) {
2161 SOCKETS_G(last_error) = errno;
2162 php_error_docref(NULL, E_WARNING, "Unable to create socket pair [%d]: %s", errno, sockets_strerror(errno));
2163 zval_ptr_dtor(&retval[0]);
2164 zval_ptr_dtor(&retval[1]);
2166 }
2167
2168 fds_array_zval = zend_try_array_init(fds_array_zval);
2169 if (!fds_array_zval) {
2170 zval_ptr_dtor(&retval[0]);
2171 zval_ptr_dtor(&retval[1]);
2172 RETURN_THROWS();
2173 }
2174
2175 php_sock[0]->bsd_socket = fds_array[0];
2176 php_sock[1]->bsd_socket = fds_array[1];
2177 php_sock[0]->type = domain;
2178 php_sock[1]->type = domain;
2179 php_sock[0]->error = 0;
2180 php_sock[1]->error = 0;
2181 php_sock[0]->blocking = 1;
2182 php_sock[1]->blocking = 1;
2183
2184 add_index_zval(fds_array_zval, 0, &retval[0]);
2185 add_index_zval(fds_array_zval, 1, &retval[1]);
2186
2188}
2189/* }}} */
2190#endif
2191
2192#ifdef HAVE_SHUTDOWN
2193/* {{{ Shuts down a socket for receiving, sending, or both. */
2195{
2196 zval *arg1;
2197 zend_long how_shutdown = 2;
2198 php_socket *php_sock;
2199
2203 Z_PARAM_LONG(how_shutdown)
2205
2206 php_sock = Z_SOCKET_P(arg1);
2207 ENSURE_SOCKET_VALID(php_sock);
2208
2209 if (shutdown(php_sock->bsd_socket, how_shutdown) != 0) {
2210 PHP_SOCKET_ERROR(php_sock, "Unable to shutdown socket", errno);
2212 }
2213
2215}
2216/* }}} */
2217#endif
2218
2219#ifdef HAVE_SOCKATMARK
2221{
2222 zval *arg1;
2223 php_socket *php_sock;
2224 int r;
2225
2229
2230 php_sock = Z_SOCKET_P(arg1);
2231 ENSURE_SOCKET_VALID(php_sock);
2232
2233 r = sockatmark(php_sock->bsd_socket);
2234 if (r < 0) {
2235 PHP_SOCKET_ERROR(php_sock, "Unable to apply sockmark", errno);
2237 } else if (r == 0) {
2239 } else {
2241 }
2242}
2243#endif
2244
2245/* {{{ Returns the last socket error (either the last used or the provided socket resource) */
2247{
2248 zval *arg1 = NULL;
2249 php_socket *php_sock;
2250
2255
2256 if (arg1) {
2257 php_sock = Z_SOCKET_P(arg1);
2258 ENSURE_SOCKET_VALID(php_sock);
2259
2260 RETVAL_LONG(php_sock->error);
2261 } else {
2262 RETVAL_LONG(SOCKETS_G(last_error));
2263 }
2264}
2265/* }}} */
2266
2267/* {{{ Clears the error on the socket or the last error code. */
2269{
2270 zval *arg1 = NULL;
2271 php_socket *php_sock;
2272
2277
2278 if (arg1) {
2279 php_sock = Z_SOCKET_P(arg1);
2280 ENSURE_SOCKET_VALID(php_sock);
2281
2282 php_sock->error = 0;
2283 } else {
2284 SOCKETS_G(last_error) = 0;
2285 }
2286
2287 return;
2288}
2289/* }}} */
2290
2291bool socket_import_file_descriptor(PHP_SOCKET socket, php_socket *retsock)
2292{
2293#ifdef SO_DOMAIN
2294 int type;
2295 socklen_t type_len = sizeof(type);
2296#endif
2298 socklen_t addr_len = sizeof(addr);
2299#ifndef PHP_WIN32
2300 int t;
2301#endif
2302
2303 retsock->bsd_socket = socket;
2304
2305 /* determine family */
2306#ifdef SO_DOMAIN
2307 if (getsockopt(socket, SOL_SOCKET, SO_DOMAIN, &type, &type_len) == 0) {
2308 retsock->type = type;
2309 } else
2310#endif
2311 if (getsockname(socket, (struct sockaddr*)&addr, &addr_len) == 0) {
2312 retsock->type = addr.ss_family;
2313 } else {
2314 PHP_SOCKET_ERROR(retsock, "Unable to obtain socket family", errno);
2315 return 0;
2316 }
2317
2318 /* determine blocking mode */
2319#ifndef PHP_WIN32
2320 t = fcntl(socket, F_GETFL);
2321 if (t == -1) {
2322 PHP_SOCKET_ERROR(retsock, "Unable to obtain blocking state", errno);
2323 return 0;
2324 } else {
2325 retsock->blocking = !(t & O_NONBLOCK);
2326 }
2327#endif
2328
2329 return 1;
2330}
2331
2332/* {{{ Imports a stream that encapsulates a socket into a socket extension resource. */
2334{
2335 zval *zstream;
2336 php_stream *stream;
2337 php_socket *retsock = NULL;
2338 PHP_SOCKET socket; /* fd */
2339
2341 Z_PARAM_RESOURCE(zstream)
2343 php_stream_from_zval(stream, zstream);
2344
2345 if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) {
2346 /* error supposedly already shown */
2348 }
2349
2351 retsock = Z_SOCKET_P(return_value);
2352
2353 if (!socket_import_file_descriptor(socket, retsock)) {
2356 }
2357
2358#ifdef PHP_WIN32
2359 /* on windows, check if the stream is a socket stream and read its
2360 * private data; otherwise assume it's in non-blocking mode */
2361 if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) {
2362 retsock->blocking =
2363 ((php_netstream_data_t *)stream->abstract)->is_blocked;
2364 } else {
2365 retsock->blocking = 1;
2366 }
2367#endif
2368
2369 /* hold a zval reference to the stream (holding a php_stream* directly could
2370 * also be done, but this makes socket_export_stream a bit simpler) */
2371 ZVAL_COPY(&retsock->zstream, zstream);
2372
2374}
2375/* }}} */
2376
2377/* {{{ Exports a socket extension resource into a stream that encapsulates a socket. */
2379{
2380 zval *zsocket;
2381 php_socket *socket;
2382 php_stream *stream = NULL;
2383 php_netstream_data_t *stream_data;
2384 const char *protocol = NULL;
2385 size_t protocollen = 0;
2386
2390
2391 socket = Z_SOCKET_P(zsocket);
2392 ENSURE_SOCKET_VALID(socket);
2393
2394 /* Either we already exported a stream or the socket came from an import,
2395 * just return the existing stream */
2396 if (!Z_ISUNDEF(socket->zstream)) {
2397 RETURN_COPY(&socket->zstream);
2398 }
2399
2400 /* Determine if socket is using a protocol with one of the default registered
2401 * socket stream wrappers */
2402 if (socket->type == PF_INET
2403#ifdef HAVE_IPV6
2404 || socket->type == PF_INET6
2405#endif
2406 ) {
2407 int protoid;
2408 socklen_t protoidlen = sizeof(protoid);
2409
2410 getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &protoid, &protoidlen);
2411
2412 if (protoid == SOCK_STREAM) {
2413 /* SO_PROTOCOL is not (yet?) supported on OS X, so let's assume it's TCP there */
2414#ifdef SO_PROTOCOL
2415 protoidlen = sizeof(protoid);
2416 getsockopt(socket->bsd_socket, SOL_SOCKET, SO_PROTOCOL, (char *) &protoid, &protoidlen);
2417 if (protoid == IPPROTO_TCP)
2418#endif
2419 {
2420 protocol = "tcp://";
2421 protocollen = sizeof("tcp://") - 1;
2422 }
2423 } else if (protoid == SOCK_DGRAM) {
2424 protocol = "udp://";
2425 protocollen = sizeof("udp://") - 1;
2426 }
2427#ifdef PF_UNIX
2428 } else if (socket->type == PF_UNIX) {
2429 int type;
2430 socklen_t typelen = sizeof(type);
2431
2432 getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &type, &typelen);
2433
2434 if (type == SOCK_STREAM) {
2435 protocol = "unix://";
2436 protocollen = sizeof("unix://") - 1;
2437 } else if (type == SOCK_DGRAM) {
2438 protocol = "udg://";
2439 protocollen = sizeof("udg://") - 1;
2440 }
2441#endif
2442 }
2443
2444 /* Try to get a stream with the registered sockops for the protocol in use
2445 * We don't want streams to actually *do* anything though, so don't give it
2446 * anything apart from the protocol */
2447 if (protocol != NULL) {
2448 stream = php_stream_xport_create(protocol, protocollen, 0, 0, NULL, NULL, NULL, NULL, NULL);
2449 }
2450
2451 /* Fall back to creating a generic socket stream */
2452 if (stream == NULL) {
2453 stream = php_stream_sock_open_from_socket(socket->bsd_socket, 0);
2454
2455 if (stream == NULL) {
2456 php_error_docref(NULL, E_WARNING, "Failed to create stream");
2458 }
2459 }
2460
2461 stream_data = (php_netstream_data_t *) stream->abstract;
2462 stream_data->socket = socket->bsd_socket;
2463 stream_data->is_blocked = socket->blocking;
2464 stream_data->timeout.tv_sec = FG(default_socket_timeout);
2465 stream_data->timeout.tv_usec = 0;
2466
2467 php_stream_to_zval(stream, &socket->zstream);
2468
2469 RETURN_COPY(&socket->zstream);
2470}
2471/* }}} */
2472
2473/* {{{ Gets array with contents of getaddrinfo about the given hostname. */
2475{
2476 char *service = NULL;
2477 size_t service_len = 0;
2478 zend_string *hostname, *key;
2479 zval *hint, *zhints = NULL;
2480
2481 struct addrinfo hints, *result, *rp;
2483
2485 Z_PARAM_STR(hostname)
2487 Z_PARAM_STRING_OR_NULL(service, service_len)
2488 Z_PARAM_ARRAY(zhints)
2490
2491 memset(&hints, 0, sizeof(hints));
2492
2493 if (zhints && !HT_IS_PACKED(Z_ARRVAL_P(zhints))) {
2495 if (key) {
2496 if (zend_string_equals_literal(key, "ai_flags")) {
2497 hints.ai_flags = zval_get_long(hint);
2498 } else if (zend_string_equals_literal(key, "ai_socktype")) {
2499 hints.ai_socktype = zval_get_long(hint);
2500 } else if (zend_string_equals_literal(key, "ai_protocol")) {
2501 hints.ai_protocol = zval_get_long(hint);
2502 } else if (zend_string_equals_literal(key, "ai_family")) {
2503 hints.ai_family = zval_get_long(hint);
2504 } else {
2505 zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
2506 "\"ai_protocol\", or \"ai_family\"");
2507 RETURN_THROWS();
2508 }
2509 }
2511 }
2512
2513 if (getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result) != 0) {
2515 }
2516
2518
2519 for (rp = result; rp != NULL; rp = rp->ai_next) {
2520 if (rp->ai_family != AF_UNSPEC) {
2521 zval zaddr;
2522
2524 res = Z_ADDRESS_INFO_P(&zaddr);
2525
2526 memcpy(&res->addrinfo, rp, sizeof(struct addrinfo));
2527
2528 res->addrinfo.ai_addr = emalloc(rp->ai_addrlen);
2529 memcpy(res->addrinfo.ai_addr, rp->ai_addr, rp->ai_addrlen);
2530
2531 if (rp->ai_canonname != NULL) {
2532 res->addrinfo.ai_canonname = estrdup(rp->ai_canonname);
2533 }
2534
2535 add_next_index_zval(return_value, &zaddr);
2536 }
2537 }
2538
2539 freeaddrinfo(result);
2540}
2541/* }}} */
2542
2543/* {{{ Creates and binds to a socket from a given addrinfo resource */
2545{
2546 zval *arg1;
2547 int retval;
2548 php_addrinfo *ai;
2549 php_socket *php_sock;
2550
2554
2555 ai = Z_ADDRESS_INFO_P(arg1);
2556
2558 php_sock = Z_SOCKET_P(return_value);
2559
2560 php_sock->bsd_socket = socket(ai->addrinfo.ai_family, ai->addrinfo.ai_socktype, ai->addrinfo.ai_protocol);
2561 php_sock->type = ai->addrinfo.ai_family;
2562
2563 if (IS_INVALID_SOCKET(php_sock)) {
2564 SOCKETS_G(last_error) = errno;
2565 php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
2568 }
2569
2570 php_sock->error = 0;
2571 php_sock->blocking = 1;
2572
2573 switch(php_sock->type) {
2574 case AF_UNIX:
2575 {
2576 // AF_UNIX sockets via getaddrino are not implemented due to security problems
2577 close(php_sock->bsd_socket);
2580 }
2581
2582 case AF_INET:
2583#ifdef HAVE_IPV6
2584 case AF_INET6:
2585#endif
2586 {
2587 retval = bind(php_sock->bsd_socket, ai->addrinfo.ai_addr, ai->addrinfo.ai_addrlen);
2588 break;
2589 }
2590 default:
2591 close(php_sock->bsd_socket);
2593 zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
2594 RETURN_THROWS();
2595 }
2596
2597 if (retval != 0) {
2598 PHP_SOCKET_ERROR(php_sock, "Unable to bind address", errno);
2599 close(php_sock->bsd_socket);
2602 }
2603}
2604/* }}} */
2605
2606/* {{{ Creates and connects to a socket from a given addrinfo resource */
2608{
2609 zval *arg1;
2610 int retval;
2611 php_addrinfo *ai;
2612 php_socket *php_sock;
2613
2617
2618 ai = Z_ADDRESS_INFO_P(arg1);
2619
2621 php_sock = Z_SOCKET_P(return_value);
2622
2623 php_sock->bsd_socket = socket(ai->addrinfo.ai_family, ai->addrinfo.ai_socktype, ai->addrinfo.ai_protocol);
2624 php_sock->type = ai->addrinfo.ai_family;
2625
2626 if (IS_INVALID_SOCKET(php_sock)) {
2627 SOCKETS_G(last_error) = errno;
2628 php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
2631 }
2632
2633 php_sock->error = 0;
2634 php_sock->blocking = 1;
2635
2636 switch(php_sock->type) {
2637 case AF_UNIX:
2638 {
2639 // AF_UNIX sockets via getaddrino are not implemented due to security problems
2640 close(php_sock->bsd_socket);
2643 }
2644
2645 case AF_INET:
2646#ifdef HAVE_IPV6
2647 case AF_INET6:
2648#endif
2649 {
2650 retval = connect(php_sock->bsd_socket, ai->addrinfo.ai_addr, ai->addrinfo.ai_addrlen);
2651 break;
2652 }
2653 default:
2654 zend_argument_value_error(1, "socket type must be one of AF_UNIX, AF_INET, or AF_INET6");
2655 close(php_sock->bsd_socket);
2657 RETURN_THROWS();
2658 }
2659
2660 if (retval != 0) {
2661 PHP_SOCKET_ERROR(php_sock, "Unable to connect address", errno);
2662 close(php_sock->bsd_socket);
2665 }
2666}
2667/* }}} */
2668
2669/* {{{ Creates and connects to a socket from a given addrinfo resource */
2671{
2672 zval *arg1, sockaddr;
2673 php_addrinfo *ai;
2674
2678
2679 ai = Z_ADDRESS_INFO_P(arg1);
2680
2682
2683 add_assoc_long(return_value, "ai_flags", ai->addrinfo.ai_flags);
2684 add_assoc_long(return_value, "ai_family", ai->addrinfo.ai_family);
2685 add_assoc_long(return_value, "ai_socktype", ai->addrinfo.ai_socktype);
2686 add_assoc_long(return_value, "ai_protocol", ai->addrinfo.ai_protocol);
2687 if (ai->addrinfo.ai_canonname != NULL) {
2688 add_assoc_string(return_value, "ai_canonname", ai->addrinfo.ai_canonname);
2689 }
2690
2691 array_init(&sockaddr);
2692 switch (ai->addrinfo.ai_family) {
2693 case AF_INET:
2694 {
2695 struct sockaddr_in *sa = (struct sockaddr_in *) ai->addrinfo.ai_addr;
2696 char addr[INET_ADDRSTRLEN];
2697
2698 add_assoc_long(&sockaddr, "sin_port", ntohs((unsigned short) sa->sin_port));
2699 inet_ntop(ai->addrinfo.ai_family, &sa->sin_addr, addr, sizeof(addr));
2700 add_assoc_string(&sockaddr, "sin_addr", addr);
2701 break;
2702 }
2703#ifdef HAVE_IPV6
2704 case AF_INET6:
2705 {
2706 struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->addrinfo.ai_addr;
2707 char addr[INET6_ADDRSTRLEN];
2708
2709 add_assoc_long(&sockaddr, "sin6_port", ntohs((unsigned short) sa->sin6_port));
2710 inet_ntop(ai->addrinfo.ai_family, &sa->sin6_addr, addr, sizeof(addr));
2711 add_assoc_string(&sockaddr, "sin6_addr", addr);
2712 break;
2713 }
2714#endif
2715 }
2716
2717 add_assoc_zval(return_value, "ai_addr", &sockaddr);
2718}
2719/* }}} */
2720
2721#ifdef PHP_WIN32
2722
2723/* {{{ Exports the network socket information suitable to be used in another process and returns the info id. */
2725{
2726 WSAPROTOCOL_INFO wi;
2727 zval *zsocket;
2728 php_socket *socket;
2729 zend_long target_pid;
2730 zend_string *seg_name;
2731 HANDLE map;
2732
2735 Z_PARAM_LONG(target_pid)
2737
2738 socket = Z_SOCKET_P(zsocket);
2739 ENSURE_SOCKET_VALID(socket);
2740
2741 if (SOCKET_ERROR == WSADuplicateSocket(socket->bsd_socket, (DWORD)target_pid, &wi)) {
2742 DWORD err = WSAGetLastError();
2744
2745 if (!buf[0]) {
2746 php_error_docref(NULL, E_WARNING, "Unable to export WSA protocol info [0x%08lx]", err);
2747 } else {
2748 php_error_docref(NULL, E_WARNING, "Unable to export WSA protocol info [0x%08lx]: %s", err, buf);
2749 }
2750
2752
2754 }
2755
2756 seg_name = zend_strpprintf(0, "php_wsa_for_%u", SOCKETS_G(wsa_child_count)++);
2757 map = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(WSAPROTOCOL_INFO), ZSTR_VAL(seg_name));
2758 if (NULL != map) {
2759 LPVOID view = MapViewOfFile(map, FILE_MAP_WRITE, 0, 0, 0);
2760 if (view) {
2761 memcpy(view, &wi, sizeof(wi));
2762 UnmapViewOfFile(view);
2763 zend_hash_add_ptr(&(SOCKETS_G(wsa_info)), seg_name, map);
2764 RETURN_STR(seg_name);
2765 } else {
2766 DWORD err = GetLastError();
2767 php_error_docref(NULL, E_WARNING, "Unable to map file view [0x%08lx]", err);
2768 }
2769 } else {
2770 DWORD err = GetLastError();
2771 php_error_docref(NULL, E_WARNING, "Unable to create file mapping [0x%08lx]", err);
2772 }
2773 zend_string_release_ex(seg_name, 0);
2774
2776}
2777/* }}} */
2778
2779/* {{{ Imports the network socket information using the supplied id and creates a new socket on its base. */
2781{
2782 char *id;
2783 size_t id_len;
2784 WSAPROTOCOL_INFO wi;
2785 PHP_SOCKET sock;
2786 php_socket *php_sock;
2787 HANDLE map;
2788
2790 Z_PARAM_STRING(id, id_len)
2792
2793 map = OpenFileMapping(FILE_MAP_READ, FALSE, id);
2794 if (map) {
2795 LPVOID view = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
2796 if (view) {
2797 memcpy(&wi, view, sizeof(WSAPROTOCOL_INFO));
2798 UnmapViewOfFile(view);
2799 } else {
2800 DWORD err = GetLastError();
2801 php_error_docref(NULL, E_WARNING, "Unable to map file view [0x%08lx]", err);
2803 }
2804 CloseHandle(map);
2805 } else {
2806 DWORD err = GetLastError();
2807 php_error_docref(NULL, E_WARNING, "Unable to open file mapping [0x%08lx]", err);
2809 }
2810
2811 sock = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, &wi, 0, 0);
2812 if (INVALID_SOCKET == sock) {
2813 DWORD err = WSAGetLastError();
2815
2816 if (!buf[0]) {
2817 php_error_docref(NULL, E_WARNING, "Unable to import WSA protocol info [0x%08lx]", err);
2818 } else {
2819 php_error_docref(NULL, E_WARNING, "Unable to import WSA protocol info [0x%08lx]: %s", err, buf);
2820 }
2821
2823
2825 }
2826
2828 php_sock = Z_SOCKET_P(return_value);
2829
2830 php_sock->bsd_socket = sock;
2831 php_sock->type = wi.iAddressFamily;
2832 php_sock->error = 0;
2833 php_sock->blocking = 1;
2834}
2835/* }}} */
2836
2837/* {{{ Frees the exported info and corresponding resources using the supplied id. */
2839{
2840 char *id;
2841 size_t id_len;
2842
2844 Z_PARAM_STRING(id, id_len)
2846
2847 RETURN_BOOL(SUCCESS == zend_hash_str_del(&(SOCKETS_G(wsa_info)), id, id_len));
2848}
2849/* }}} */
2850#endif
size_t len
Definition apprentice.c:174
sin(float $num)
#define DWORD
Definition exif.c:1762
#define PF_INET
Definition sockets.c:91
zend_class_entry * address_info_ce
Definition sockets.c:158
#define Z_ADDRESS_INFO_P(zv)
Definition sockets.c:165
#define set_errno(a)
Definition sockets.c:52
zend_class_entry * socket_ce
Definition sockets.c:103
char * sockets_strerror(int error)
Definition sockets.c:367
#define SUN_LEN(su)
Definition sockets.c:87
zend_module_entry sockets_module_entry
Definition sockets.c:195
bool socket_import_file_descriptor(PHP_SOCKET socket, php_socket *retsock)
Definition sockets.c:2291
#define HANDLE_SUBCALL(res)
error($message)
Definition ext_skel.php:22
unsigned int socklen_t
Definition fastcgi.c:87
zend_ffi_type * type
Definition ffi.c:3812
zend_long n
Definition ffi.c:4979
zend_string * res
Definition ffi.c:4692
memcpy(ptr1, ptr2, size)
char * err
Definition ffi.c:3029
memset(ptr, 0, type->size)
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
#define FD_CLOEXEC
Definition file.h:148
char * mode
zend_long maxlen
#define FALSE
Definition gd_gd.c:8
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
zend_result php_add4_to_if_index(struct in_addr *addr, php_socket *php_sock, unsigned *if_index)
Definition multicast.c:747
int php_do_setsockopt_ip_mcast(php_socket *php_sock, int level, int optname, zval *arg4)
Definition multicast.c:235
inet_ntop(AF_INET, addr, addr_str, sizeof(addr_str))
int php_do_setsockopt_ipv6_mcast(php_socket *php_sock, int level, int optname, zval *arg4)
Definition multicast.c:304
PHPAPI zend_result php_set_sock_blocking(php_socket_t socketd, bool block)
Definition network.c:1145
#define O_NONBLOCK
Definition network.c:282
PHPAPI struct hostent * php_network_gethostbyname(const char *name)
Definition network.c:1350
php_info_print_table_start()
Definition info.c:1064
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
Definition info.c:1074
#define PHP_GINIT
Definition php.h:397
#define PHP_FUNCTION
Definition php.h:364
#define INT_MIN
Definition php.h:241
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINFO
Definition php.h:396
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define strlcpy
Definition php.h:159
#define PHP_MSHUTDOWN
Definition php.h:393
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define PHP_GINIT_FUNCTION
Definition php.h:405
#define INT_MAX
Definition php.h:237
#define PHP_RSHUTDOWN
Definition php.h:395
#define PHP_RSHUTDOWN_FUNCTION
Definition php.h:403
#define PHP_GSHUTDOWN_FUNCTION
Definition php.h:406
#define PHP_MINIT
Definition php.h:392
#define PHP_MODULE_GLOBALS
Definition php.h:408
#define PHP_GSHUTDOWN
Definition php.h:398
#define offsetof(STRUCTURE, FIELD)
#define PHP_SAFE_FD_ISSET(fd, set)
struct _php_netstream_data_t php_netstream_data_t
#define PHP_SAFE_FD_SET(fd, set)
#define shutdown(s, n)
Definition php_network.h:30
#define php_stream_sock_open_from_socket(socket, persistent)
#define PHP_IS_TRANSIENT_ERROR(err)
Definition php_network.h:54
#define PHP_SAFE_MAX_FD(m, n)
#define PHP_STREAM_IS_SOCKET
int last_error
Definition php_pcntl.h:48
#define min(a, b)
unsigned char key[REFLECTION_KEY_LEN]
#define PHP_NORMAL_READ
#define PHP_BINARY_READ
#define php_stream_xport_create(name, namelen, options, flags, persistent_id, timeout, context, estr, ecode)
#define php_stream_cast(stream, as, ret, show_err)
struct _php_stream php_stream
Definition php_streams.h:96
#define PHP_STREAM_OPTION_BLOCKING
#define php_stream_from_zval(xstr, pzval)
#define PHP_STREAM_FREE_CLOSE_PERSISTENT
#define PHP_STREAM_BUFFER_NONE
#define php_stream_to_zval(stream, zval)
#define php_stream_free(stream, close_options)
#define PHP_STREAM_FREE_CLOSE
#define php_stream_is(stream, anops)
#define PHP_STREAM_OPTION_READ_BUFFER
#define PHP_STREAM_FREE_KEEP_RSRC
PHPAPI int php_file_le_pstream(void)
Definition streams.c:47
#define php_stream_from_zval_no_verify(xstr, pzval)
#define PHP_STREAM_AS_SOCKETD
#define php_stream_set_option(stream, option, value, ptrvalue)
PHPAPI int php_file_le_stream(void)
Definition streams.c:42
zend_constant * data
void php_socket_sendrecvmsg_shutdown(SHUTDOWN_FUNC_ARGS)
void php_socket_sendrecvmsg_init(INIT_FUNC_ARGS)
int php_do_setsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval *arg4)
int php_do_getsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval *result)
struct timeval tv
Definition session.c:1280
int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock)
int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock)
const SO_TYPE
const SOL_FILTER
const SKF_AD_CPU
socket_addrinfo_bind(AddressInfo $address)
const SO_ATTACH_REUSEPORT_CBPF
socket_last_error(?Socket $socket=null)
const SO_BINDTODEVICE
const IPPROTO_IP
const SKF_AD_QUEUE
socket_addrinfo_connect(AddressInfo $address)
socket_select(?array &$read, ?array &$write, ?array &$except, ?int $seconds, int $microseconds=0)
const SOCK_STREAM
socket_close(Socket $socket)
socket_accept(Socket $socket)
const SO_MEMINFO
const SOL_SOCKET
const SO_LINGER
socket_clear_error(?Socket $socket=null)
socket_set_nonblock(Socket $socket)
socket_addrinfo_lookup(string $host, ?string $service=null, array $hints=[])
socket_connect(Socket $socket, string $address, ?int $port=null)
socket_read(Socket $socket, int $length, int $mode=PHP_BINARY_READ)
const AF_INET
socket_recvfrom(Socket $socket, &$data, int $length, int $flags, &$address, &$port=null)
socket_wsaprotocol_info_export(Socket $socket, int $process_id)
socket_sendto(Socket $socket, string $data, int $length, int $flags, string $address, ?int $port=null)
const SO_DETACH_BPF
socket_create(int $domain, int $type, int $protocol)
socket_set_option(Socket $socket, int $level, int $option, $value)
socket_addrinfo_explain(AddressInfo $address)
socket_getsockname(Socket $socket, &$address, &$port=null)
const SO_LINGER_SEC
socket_wsaprotocol_info_release(string $info_id)
socket_recv(Socket $socket, &$data, int $length, int $flags)
socket_atmark(Socket $socket)
socket_bind(Socket $socket, string $address, int $port=0)
socket_write(Socket $socket, string $data, ?int $length=null)
const SOCK_CLOEXEC
const SOCK_NONBLOCK
const SOMAXCONN
const TCP_CONGESTION
const IPPROTO_IPV6
socket_shutdown(Socket $socket, int $mode=2)
socket_getpeername(Socket $socket, &$address, &$port=null)
socket_import_stream($stream)
const SO_RCVTIMEO
socket_listen(Socket $socket, int $backlog=0)
socket_wsaprotocol_info_import(string $info_id)
const SO_ACCEPTFILTER
socket_export_stream(Socket $socket)
socket_set_block(Socket $socket)
socket_strerror(int $error_code)
const SKF_AD_OFF
socket_send(Socket $socket, string $data, int $length, int $flags)
const FIL_ATTACH
const SO_SNDTIMEO
const AF_UNIX
const AF_INET6
socket_create_pair(int $domain, int $type, int $protocol, &$pair)
const IP_MULTICAST_IF
socket_get_option(Socket $socket, int $level, int $option)
socket_create_listen(int $port, int $backlog=SOMAXCONN)
const FIL_DETACH
const SOCK_DGRAM
#define spprintf
Definition spprintf.h:29
#define FG(v)
Definition file.h:117
struct timeval timeout
uint16_t is_persistent
void * abstract
struct addrinfo addrinfo
Definition sockets.c:154
zend_object std
Definition sockets.c:155
PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
Definition sockets.c:94
#define close(a)
#define IS_INVALID_SOCKET(a)
#define errno
#define ECONNRESET
#define EAGAIN
PHP_WINUTIL_API char * php_win32_error_to_msg(HRESULT error)
Definition winutil.c:25
PHP_WINUTIL_API void php_win32_error_msg_free(char *msg)
Definition winutil.c:50
ZEND_API zend_string * zend_strpprintf(size_t max_len, const char *format,...)
Definition zend.c:353
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API ZEND_COLD void zend_value_error(const char *format,...)
Definition zend.c:1849
#define ZEND_TSRMLS_CACHE_UPDATE()
Definition zend.h:69
#define ZEND_TSRMLS_CACHE_DEFINE()
Definition zend.h:68
ZEND_API const char * zend_zval_value_name(const zval *arg)
Definition zend_API.c:148
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type)
Definition zend_API.c:1849
ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str)
Definition zend_API.c:2087
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
Definition zend_API.c:1688
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:423
#define Z_PARAM_ARRAY_EX2(dest, check_null, deref, separate)
Definition zend_API.h:1671
#define RETURN_STRING(s)
Definition zend_API.h:1043
#define RETURN_COPY(zv)
Definition zend_API.h:1054
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define Z_PARAM_RESOURCE(dest)
Definition zend_API.h:2056
#define ZEND_DECLARE_MODULE_GLOBALS(module_name)
Definition zend_API.h:268
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define ZEND_GET_MODULE(name)
Definition zend_API.h:241
#define Z_PARAM_STRING(dest, dest_len)
Definition zend_API.h:2071
#define Z_PARAM_STR(dest)
Definition zend_API.h:2086
#define Z_PARAM_STRING_OR_NULL(dest, dest_len)
Definition zend_API.h:2074
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define ZEND_TRY_ASSIGN_REF_LONG(zv, lval)
Definition zend_API.h:1205
#define ZEND_TRY_ASSIGN_REF_NULL(zv)
Definition zend_API.h:1117
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_BOOL(b)
Definition zend_API.h:1035
#define RETURN_NEW_STR(s)
Definition zend_API.h:1041
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETURN_STR(s)
Definition zend_API.h:1039
#define Z_PARAM_LONG_OR_NULL(dest, is_null)
Definition zend_API.h:1899
#define ZEND_TRY_ASSIGN_REF_NEW_STR(zv, str)
Definition zend_API.h:1293
#define RETVAL_LONG(l)
Definition zend_API.h:1011
#define Z_PARAM_OBJECT_OF_CLASS_OR_NULL(dest, _ce)
Definition zend_API.h:1979
#define Z_PARAM_OBJECT_OF_CLASS(dest, _ce)
Definition zend_API.h:1976
#define RETURN_EMPTY_STRING()
Definition zend_API.h:1047
#define Z_PARAM_ARRAY(dest)
Definition zend_API.h:1682
#define Z_PARAM_ZVAL(dest)
Definition zend_API.h:2100
#define WRONG_PARAM_COUNT
Definition zend_API.h:529
#define ZEND_TRY_ASSIGN_REF_STRING(zv, string)
Definition zend_API.h:1315
#define RETURN_TRUE
Definition zend_API.h:1059
#define array_init(arg)
Definition zend_API.h:537
#define efree(ptr)
Definition zend_alloc.h:155
#define estrdup(s)
Definition zend_alloc.h:164
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
zend_string_release_ex(func->internal_function.function_name, 0)
#define snprintf
#define E_WARNING
Definition zend_errors.h:24
union _zend_function zend_function
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API zval *ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:2689
ZEND_API zval *ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData)
Definition zend_hash.c:1219
ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:1661
ZEND_API zval *ZEND_FASTCALL zend_hash_add(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:992
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
Definition zend_hash.h:108
#define HT_IS_PACKED(ht)
Definition zend_hash.h:59
#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val)
Definition zend_hash.h:1181
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
#define ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(ht, _key, _val)
Definition zend_hash.h:1374
#define ZEND_HASH_FOREACH_VAL(ht, _val)
Definition zend_hash.h:1102
ZEND_API void * zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
Definition zend_list.c:153
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
#define ZEND_LONG_MAX
Definition zend_long.h:45
struct _zend_string zend_string
#define STANDARD_MODULE_HEADER
#define SHUTDOWN_FUNC_ARGS_PASSTHRU
#define INIT_FUNC_ARGS_PASSTHRU
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES_EX
ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
ZEND_API HashTable * zend_std_get_properties(zend_object *zobj)
ZEND_API const zend_object_handlers std_object_handlers
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
#define MIN(a, b)
#define XtOffsetOf(s_type, field)
#define ZEND_ASSERT(c)
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define ZEND_LONG_EXCEEDS_INT(zlong)
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define zend_string_equals_literal(str, literal)
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define ZVAL_UNDEF(z)
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
#define ZVAL_DEREF(z)
#define IS_STRING
Definition zend_types.h:606
struct _zend_array HashTable
Definition zend_types.h:386
#define IS_ARRAY
Definition zend_types.h:607
#define Z_ISUNDEF(zval)
Definition zend_types.h:956
#define Z_PTR_P(zval_p)
#define Z_ADDREF_P(pz)
#define Z_STRLEN_P(zval_p)
Definition zend_types.h:978
#define Z_OBJCE_P(zval_p)
@ FAILURE
Definition zend_types.h:61
#define IS_OBJECT
Definition zend_types.h:608
#define ZVAL_COPY(z, v)
#define Z_OBJPROP_P(zval_p)
struct _zend_object_handlers zend_object_handlers
Definition zend_types.h:88
#define Z_ARRVAL(zval)
Definition zend_types.h:986
#define ZVAL_COPY_VALUE(z, v)
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval retval
zval * return_value
zval * arg1
zval * arg2
uint32_t arg_num
zend_string * name
zval * arg3
bool result
zval * ret