1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org> |
16 | Sterling Hughes <sterling@php.net> |
17 | Jason Greene <jason@php.net> |
18 | Gustavo Lopes <cataphract@php.net> |
19 | WinSock: Daniel Beulshausen <daniel@php4win.de> |
20 +----------------------------------------------------------------------+
21 */
22
23 /* $Id$ */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "php.h"
30
31 #include "php_network.h"
32 #include "ext/standard/file.h"
33 #include "ext/standard/info.h"
34 #include "php_ini.h"
35 #ifdef PHP_WIN32
36 # include "windows_common.h"
37 # include <win32/inet.h>
38 # include <windows.h>
39 # include <Ws2tcpip.h>
40 # include "php_sockets.h"
41 # include <win32/sockets.h>
42 #else
43 # include <sys/types.h>
44 # include <sys/socket.h>
45 # include <netdb.h>
46 # include <netinet/in.h>
47 # include <netinet/tcp.h>
48 # include <sys/un.h>
49 # include <arpa/inet.h>
50 # include <sys/time.h>
51 # include <unistd.h>
52 # include <errno.h>
53 # include <fcntl.h>
54 # include <signal.h>
55 # include <sys/uio.h>
56 # define IS_INVALID_SOCKET(a) (a->bsd_socket < 0)
57 # define set_errno(a) (errno = a)
58 # include "php_sockets.h"
59 # if HAVE_IF_NAMETOINDEX
60 # include <net/if.h>
61 # endif
62 #endif
63
64 #include <stddef.h>
65
66 #include "sockaddr_conv.h"
67 #include "multicast.h"
68 #include "sendrecvmsg.h"
69
70 ZEND_DECLARE_MODULE_GLOBALS(sockets)
71
72 #ifndef MSG_WAITALL
73 #ifdef LINUX
74 #define MSG_WAITALL 0x00000100
75 #else
76 #define MSG_WAITALL 0x00000000
77 #endif
78 #endif
79
80 #ifndef MSG_EOF
81 #ifdef MSG_FIN
82 #define MSG_EOF MSG_FIN
83 #endif
84 #endif
85
86 #ifndef SUN_LEN
87 #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
88 #endif
89
90 #ifndef PF_INET
91 #define PF_INET AF_INET
92 #endif
93
94 #define PHP_NORMAL_READ 0x0001
95 #define PHP_BINARY_READ 0x0002
96
97 static int le_socket;
98 #define le_socket_name php_sockets_le_socket_name
99
100 static int le_addrinfo;
101 #define le_addrinfo_name php_sockets_le_addrinfo_name
102
103 /* {{{ arginfo */
104 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_select, 0, 0, 4)
105 ZEND_ARG_INFO(1, read_fds)
106 ZEND_ARG_INFO(1, write_fds)
107 ZEND_ARG_INFO(1, except_fds)
108 ZEND_ARG_INFO(0, tv_sec)
109 ZEND_ARG_INFO(0, tv_usec)
110 ZEND_END_ARG_INFO()
111
112 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_listen, 0, 0, 1)
113 ZEND_ARG_INFO(0, port)
114 ZEND_ARG_INFO(0, backlog)
115 ZEND_END_ARG_INFO()
116
117 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_accept, 0, 0, 1)
118 ZEND_ARG_INFO(0, socket)
119 ZEND_END_ARG_INFO()
120
121 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_nonblock, 0, 0, 1)
122 ZEND_ARG_INFO(0, socket)
123 ZEND_END_ARG_INFO()
124
125 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_block, 0, 0, 1)
126 ZEND_ARG_INFO(0, socket)
127 ZEND_END_ARG_INFO()
128
129 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_listen, 0, 0, 1)
130 ZEND_ARG_INFO(0, socket)
131 ZEND_ARG_INFO(0, backlog)
132 ZEND_END_ARG_INFO()
133
134 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_close, 0, 0, 1)
135 ZEND_ARG_INFO(0, socket)
136 ZEND_END_ARG_INFO()
137
138 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_write, 0, 0, 2)
139 ZEND_ARG_INFO(0, socket)
140 ZEND_ARG_INFO(0, buf)
141 ZEND_ARG_INFO(0, length)
142 ZEND_END_ARG_INFO()
143
144 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_read, 0, 0, 2)
145 ZEND_ARG_INFO(0, socket)
146 ZEND_ARG_INFO(0, length)
147 ZEND_ARG_INFO(0, type)
148 ZEND_END_ARG_INFO()
149
150 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_getsockname, 0, 0, 2)
151 ZEND_ARG_INFO(0, socket)
152 ZEND_ARG_INFO(1, addr)
153 ZEND_ARG_INFO(1, port)
154 ZEND_END_ARG_INFO()
155
156 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_getpeername, 0, 0, 2)
157 ZEND_ARG_INFO(0, socket)
158 ZEND_ARG_INFO(1, addr)
159 ZEND_ARG_INFO(1, port)
160 ZEND_END_ARG_INFO()
161
162 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create, 0, 0, 3)
163 ZEND_ARG_INFO(0, domain)
164 ZEND_ARG_INFO(0, type)
165 ZEND_ARG_INFO(0, protocol)
166 ZEND_END_ARG_INFO()
167
168 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_connect, 0, 0, 2)
169 ZEND_ARG_INFO(0, socket)
170 ZEND_ARG_INFO(0, addr)
171 ZEND_ARG_INFO(0, port)
172 ZEND_END_ARG_INFO()
173
174 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_strerror, 0, 0, 1)
175 ZEND_ARG_INFO(0, errno)
176 ZEND_END_ARG_INFO()
177
178 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_bind, 0, 0, 2)
179 ZEND_ARG_INFO(0, socket)
180 ZEND_ARG_INFO(0, addr)
181 ZEND_ARG_INFO(0, port)
182 ZEND_END_ARG_INFO()
183
184 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recv, 0, 0, 4)
185 ZEND_ARG_INFO(0, socket)
186 ZEND_ARG_INFO(1, buf)
187 ZEND_ARG_INFO(0, len)
188 ZEND_ARG_INFO(0, flags)
189 ZEND_END_ARG_INFO()
190
191 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_send, 0, 0, 4)
192 ZEND_ARG_INFO(0, socket)
193 ZEND_ARG_INFO(0, buf)
194 ZEND_ARG_INFO(0, len)
195 ZEND_ARG_INFO(0, flags)
196 ZEND_END_ARG_INFO()
197
198 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recvfrom, 0, 0, 5)
199 ZEND_ARG_INFO(0, socket)
200 ZEND_ARG_INFO(1, buf)
201 ZEND_ARG_INFO(0, len)
202 ZEND_ARG_INFO(0, flags)
203 ZEND_ARG_INFO(1, name)
204 ZEND_ARG_INFO(1, port)
205 ZEND_END_ARG_INFO()
206
207 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_sendto, 0, 0, 5)
208 ZEND_ARG_INFO(0, socket)
209 ZEND_ARG_INFO(0, buf)
210 ZEND_ARG_INFO(0, len)
211 ZEND_ARG_INFO(0, flags)
212 ZEND_ARG_INFO(0, addr)
213 ZEND_ARG_INFO(0, port)
214 ZEND_END_ARG_INFO()
215
216 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_get_option, 0, 0, 3)
217 ZEND_ARG_INFO(0, socket)
218 ZEND_ARG_INFO(0, level)
219 ZEND_ARG_INFO(0, optname)
220 ZEND_END_ARG_INFO()
221
222 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_option, 0, 0, 4)
223 ZEND_ARG_INFO(0, socket)
224 ZEND_ARG_INFO(0, level)
225 ZEND_ARG_INFO(0, optname)
226 ZEND_ARG_INFO(0, optval)
227 ZEND_END_ARG_INFO()
228
229 #ifdef HAVE_SOCKETPAIR
230 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_pair, 0, 0, 4)
231 ZEND_ARG_INFO(0, domain)
232 ZEND_ARG_INFO(0, type)
233 ZEND_ARG_INFO(0, protocol)
234 ZEND_ARG_INFO(1, fd)
235 ZEND_END_ARG_INFO()
236 #endif
237
238 #ifdef HAVE_SHUTDOWN
239 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_shutdown, 0, 0, 1)
240 ZEND_ARG_INFO(0, socket)
241 ZEND_ARG_INFO(0, how)
242 ZEND_END_ARG_INFO()
243 #endif
244
245 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_last_error, 0, 0, 0)
246 ZEND_ARG_INFO(0, socket)
247 ZEND_END_ARG_INFO()
248
249 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_clear_error, 0, 0, 0)
250 ZEND_ARG_INFO(0, socket)
251 ZEND_END_ARG_INFO()
252
253 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_import_stream, 0, 0, 1)
254 ZEND_ARG_INFO(0, stream)
255 ZEND_END_ARG_INFO()
256
257 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_export_stream, 0, 0, 1)
258 ZEND_ARG_INFO(0, socket)
259 ZEND_END_ARG_INFO()
260
261 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_sendmsg, 0, 0, 3)
262 ZEND_ARG_INFO(0, socket)
263 ZEND_ARG_INFO(0, msghdr)
264 ZEND_ARG_INFO(0, flags)
265 ZEND_END_ARG_INFO()
266
267 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recvmsg, 0, 0, 3)
268 ZEND_ARG_INFO(0, socket)
269 ZEND_ARG_INFO(1, msghdr)
270 ZEND_ARG_INFO(0, flags)
271 ZEND_END_ARG_INFO()
272
273 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_cmsg_space, 0, 0, 2)
274 ZEND_ARG_INFO(0, level)
275 ZEND_ARG_INFO(0, type)
276 ZEND_END_ARG_INFO()
277
278 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_addrinfo_lookup, 0, 0, 1)
279 ZEND_ARG_INFO(0, host)
280 ZEND_ARG_INFO(0, service)
281 ZEND_ARG_INFO(0, hints)
282 ZEND_END_ARG_INFO()
283
284 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_addrinfo_connect, 0, 0, 1)
285 ZEND_ARG_INFO(0, addr)
286 ZEND_END_ARG_INFO()
287
288 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_addrinfo_bind, 0, 0, 1)
289 ZEND_ARG_INFO(0, addr)
290 ZEND_END_ARG_INFO()
291
292 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_addrinfo_explain, 0, 0, 1)
293 ZEND_ARG_INFO(0, addr)
294 ZEND_END_ARG_INFO()
295
296 /* }}} */
297
298 static PHP_GINIT_FUNCTION(sockets);
299 static PHP_MINIT_FUNCTION(sockets);
300 static PHP_MSHUTDOWN_FUNCTION(sockets);
301 static PHP_MINFO_FUNCTION(sockets);
302 static PHP_RSHUTDOWN_FUNCTION(sockets);
303
304 PHP_FUNCTION(socket_select);
305 PHP_FUNCTION(socket_create_listen);
306 #ifdef HAVE_SOCKETPAIR
307 PHP_FUNCTION(socket_create_pair);
308 #endif
309 PHP_FUNCTION(socket_accept);
310 PHP_FUNCTION(socket_set_nonblock);
311 PHP_FUNCTION(socket_set_block);
312 PHP_FUNCTION(socket_listen);
313 PHP_FUNCTION(socket_close);
314 PHP_FUNCTION(socket_write);
315 PHP_FUNCTION(socket_read);
316 PHP_FUNCTION(socket_getsockname);
317 PHP_FUNCTION(socket_getpeername);
318 PHP_FUNCTION(socket_create);
319 PHP_FUNCTION(socket_connect);
320 PHP_FUNCTION(socket_strerror);
321 PHP_FUNCTION(socket_bind);
322 PHP_FUNCTION(socket_recv);
323 PHP_FUNCTION(socket_send);
324 PHP_FUNCTION(socket_recvfrom);
325 PHP_FUNCTION(socket_sendto);
326 PHP_FUNCTION(socket_get_option);
327 PHP_FUNCTION(socket_set_option);
328 #ifdef HAVE_SHUTDOWN
329 PHP_FUNCTION(socket_shutdown);
330 #endif
331 PHP_FUNCTION(socket_last_error);
332 PHP_FUNCTION(socket_clear_error);
333 PHP_FUNCTION(socket_import_stream);
334 PHP_FUNCTION(socket_export_stream);
335 PHP_FUNCTION(socket_addrinfo_lookup);
336 PHP_FUNCTION(socket_addrinfo_connect);
337 PHP_FUNCTION(socket_addrinfo_bind);
338 PHP_FUNCTION(socket_addrinfo_explain);
339
340 /* {{{ sockets_functions[]
341 */
342 const zend_function_entry sockets_functions[] = {
343 PHP_FE(socket_select, arginfo_socket_select)
344 PHP_FE(socket_create, arginfo_socket_create)
345 PHP_FE(socket_create_listen, arginfo_socket_create_listen)
346 #ifdef HAVE_SOCKETPAIR
347 PHP_FE(socket_create_pair, arginfo_socket_create_pair)
348 #endif
349 PHP_FE(socket_accept, arginfo_socket_accept)
350 PHP_FE(socket_set_nonblock, arginfo_socket_set_nonblock)
351 PHP_FE(socket_set_block, arginfo_socket_set_block)
352 PHP_FE(socket_listen, arginfo_socket_listen)
353 PHP_FE(socket_close, arginfo_socket_close)
354 PHP_FE(socket_write, arginfo_socket_write)
355 PHP_FE(socket_read, arginfo_socket_read)
356 PHP_FE(socket_getsockname, arginfo_socket_getsockname)
357 PHP_FE(socket_getpeername, arginfo_socket_getpeername)
358 PHP_FE(socket_connect, arginfo_socket_connect)
359 PHP_FE(socket_strerror, arginfo_socket_strerror)
360 PHP_FE(socket_bind, arginfo_socket_bind)
361 PHP_FE(socket_recv, arginfo_socket_recv)
362 PHP_FE(socket_send, arginfo_socket_send)
363 PHP_FE(socket_recvfrom, arginfo_socket_recvfrom)
364 PHP_FE(socket_sendto, arginfo_socket_sendto)
365 PHP_FE(socket_get_option, arginfo_socket_get_option)
366 PHP_FE(socket_set_option, arginfo_socket_set_option)
367 #ifdef HAVE_SHUTDOWN
368 PHP_FE(socket_shutdown, arginfo_socket_shutdown)
369 #endif
370 PHP_FE(socket_last_error, arginfo_socket_last_error)
371 PHP_FE(socket_clear_error, arginfo_socket_clear_error)
372 PHP_FE(socket_import_stream, arginfo_socket_import_stream)
373 PHP_FE(socket_export_stream, arginfo_socket_export_stream)
374 PHP_FE(socket_sendmsg, arginfo_socket_sendmsg)
375 PHP_FE(socket_recvmsg, arginfo_socket_recvmsg)
376 PHP_FE(socket_cmsg_space, arginfo_socket_cmsg_space)
377 PHP_FE(socket_addrinfo_lookup, arginfo_socket_addrinfo_lookup)
378 PHP_FE(socket_addrinfo_connect, arginfo_socket_addrinfo_connect)
379 PHP_FE(socket_addrinfo_bind, arginfo_socket_addrinfo_bind)
380 PHP_FE(socket_addrinfo_explain, arginfo_socket_addrinfo_explain)
381
382 /* for downwards compatibility */
383 PHP_FALIAS(socket_getopt, socket_get_option, arginfo_socket_get_option)
384 PHP_FALIAS(socket_setopt, socket_set_option, arginfo_socket_set_option)
385
386 PHP_FE_END
387 };
388 /* }}} */
389
390 zend_module_entry sockets_module_entry = {
391 STANDARD_MODULE_HEADER,
392 "sockets",
393 sockets_functions,
394 PHP_MINIT(sockets),
395 PHP_MSHUTDOWN(sockets),
396 NULL,
397 PHP_RSHUTDOWN(sockets),
398 PHP_MINFO(sockets),
399 PHP_SOCKETS_VERSION,
400 PHP_MODULE_GLOBALS(sockets),
401 PHP_GINIT(sockets),
402 NULL,
403 NULL,
404 STANDARD_MODULE_PROPERTIES_EX
405 };
406
407
408 #ifdef COMPILE_DL_SOCKETS
409 #ifdef ZTS
410 ZEND_TSRMLS_CACHE_DEFINE()
411 #endif
412 ZEND_GET_MODULE(sockets)
413 #endif
414
415 /* inet_ntop should be used instead of inet_ntoa */
416 int inet_ntoa_lock = 0;
417
php_sockets_le_socket(void)418 PHP_SOCKETS_API int php_sockets_le_socket(void) /* {{{ */
419 {
420 return le_socket;
421 }
422 /* }}} */
423
424 /* allocating function to make programming errors due to uninitialized fields
425 * less likely */
php_create_socket(void)426 PHP_SOCKETS_API php_socket *php_create_socket(void) /* {{{ */
427 {
428 php_socket *php_sock = emalloc(sizeof(php_socket));
429
430 php_sock->bsd_socket = -1; /* invalid socket */
431 php_sock->type = PF_UNSPEC;
432 php_sock->error = 0;
433 php_sock->blocking = 1;
434 ZVAL_UNDEF(&php_sock->zstream);
435
436 return php_sock;
437 }
438 /* }}} */
439
php_destroy_socket(zend_resource * rsrc)440 PHP_SOCKETS_API void php_destroy_socket(zend_resource *rsrc) /* {{{ */
441 {
442 php_socket *php_sock = rsrc->ptr;
443
444 if (Z_ISUNDEF(php_sock->zstream)) {
445 if (!IS_INVALID_SOCKET(php_sock)) {
446 close(php_sock->bsd_socket);
447 }
448 } else {
449 zval_ptr_dtor(&php_sock->zstream);
450 }
451 efree(php_sock);
452 }
453 /* }}} */
454
php_destroy_addrinfo(zend_resource * rsrc)455 PHP_SOCKETS_API void php_destroy_addrinfo(zend_resource *rsrc) /* {{{ */
456 {
457 struct addrinfo *addr = rsrc->ptr;
458 efree(addr->ai_addr);
459 if (addr->ai_canonname != NULL) {
460 efree(addr->ai_canonname);
461 }
462 efree(addr);
463 }
464 /* }}} */
465
php_open_listen_sock(php_socket ** php_sock,int port,int backlog)466 static int php_open_listen_sock(php_socket **php_sock, int port, int backlog) /* {{{ */
467 {
468 struct sockaddr_in la;
469 struct hostent *hp;
470 php_socket *sock = php_create_socket();
471
472 *php_sock = sock;
473
474 #ifndef PHP_WIN32
475 if ((hp = php_network_gethostbyname("0.0.0.0")) == NULL) {
476 #else
477 if ((hp = php_network_gethostbyname("localhost")) == NULL) {
478 #endif
479 efree(sock);
480 return 0;
481 }
482
483 memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
484 la.sin_family = hp->h_addrtype;
485 la.sin_port = htons((unsigned short) port);
486
487 sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
488 sock->blocking = 1;
489
490 if (IS_INVALID_SOCKET(sock)) {
491 PHP_SOCKET_ERROR(sock, "unable to create listening socket", errno);
492 efree(sock);
493 return 0;
494 }
495
496 sock->type = PF_INET;
497
498 if (bind(sock->bsd_socket, (struct sockaddr *)&la, sizeof(la)) != 0) {
499 PHP_SOCKET_ERROR(sock, "unable to bind to given address", errno);
500 close(sock->bsd_socket);
501 efree(sock);
502 return 0;
503 }
504
505 if (listen(sock->bsd_socket, backlog) != 0) {
506 PHP_SOCKET_ERROR(sock, "unable to listen on socket", errno);
507 close(sock->bsd_socket);
508 efree(sock);
509 return 0;
510 }
511
512 return 1;
513 }
514 /* }}} */
515
516 static int php_accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la, socklen_t *la_len) /* {{{ */
517 {
518 php_socket *out_sock = php_create_socket();
519
520 *new_sock = out_sock;
521
522 out_sock->bsd_socket = accept(in_sock->bsd_socket, la, la_len);
523
524 if (IS_INVALID_SOCKET(out_sock)) {
525 PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
526 efree(out_sock);
527 return 0;
528 }
529
530 out_sock->error = 0;
531 out_sock->blocking = 1;
532 out_sock->type = la->sa_family;
533
534 return 1;
535 }
536 /* }}} */
537
538 /* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
539 static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags)
540 {
541 int m = 0;
542 size_t n = 0;
543 int no_read = 0;
544 int nonblock = 0;
545 char *t = (char *) buf;
546
547 #ifndef PHP_WIN32
548 m = fcntl(sock->bsd_socket, F_GETFL);
549 if (m < 0) {
550 return m;
551 }
552 nonblock = (m & O_NONBLOCK);
553 m = 0;
554 #else
555 nonblock = !sock->blocking;
556 #endif
557 set_errno(0);
558
559 *t = '\0';
560 while (*t != '\n' && *t != '\r' && n < maxlen) {
561 if (m > 0) {
562 t++;
563 n++;
564 } else if (m == 0) {
565 no_read++;
566 if (nonblock && no_read >= 2) {
567 return n;
568 /* The first pass, m always is 0, so no_read becomes 1
569 * in the first pass. no_read becomes 2 in the second pass,
570 * and if this is nonblocking, we should return.. */
571 }
572
573 if (no_read > 200) {
574 set_errno(ECONNRESET);
575 return -1;
576 }
577 }
578
579 if (n < maxlen) {
580 m = recv(sock->bsd_socket, (void *) t, 1, flags);
581 }
582
583 if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
584 return -1;
585 }
586
587 set_errno(0);
588 }
589
590 if (n < maxlen) {
591 n++;
592 /* The only reasons it makes it to here is
593 * if '\n' or '\r' are encountered. So, increase
594 * the return by 1 to make up for the lack of the
595 * '\n' or '\r' in the count (since read() takes
596 * place at the end of the loop..) */
597 }
598
599 return n;
600 }
601 /* }}} */
602
603 char *sockets_strerror(int error) /* {{{ */
604 {
605 const char *buf;
606
607 #ifndef PHP_WIN32
608 if (error < -10000) {
609 error = -error - 10000;
610
611 #ifdef HAVE_HSTRERROR
612 buf = hstrerror(error);
613 #else
614 {
615 if (SOCKETS_G(strerror_buf)) {
616 efree(SOCKETS_G(strerror_buf));
617 }
618
619 spprintf(&(SOCKETS_G(strerror_buf)), 0, "Host lookup error %d", error);
620 buf = SOCKETS_G(strerror_buf);
621 }
622 #endif
623 } else {
624 buf = strerror(error);
625 }
626 #else
627 {
628 LPTSTR tmp = NULL;
629 buf = NULL;
630
631 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
632 NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &tmp, 0, NULL)
633 ) {
634 if (SOCKETS_G(strerror_buf)) {
635 efree(SOCKETS_G(strerror_buf));
636 }
637
638 SOCKETS_G(strerror_buf) = estrdup(tmp);
639 LocalFree(tmp);
640
641 buf = SOCKETS_G(strerror_buf);
642 }
643 }
644 #endif
645
646 return (buf ? (char *) buf : "");
647 }
648 /* }}} */
649
650 /* {{{ PHP_GINIT_FUNCTION */
651 static PHP_GINIT_FUNCTION(sockets)
652 {
653 #if defined(COMPILE_DL_SOCKETS) && defined(ZTS)
654 ZEND_TSRMLS_CACHE_UPDATE();
655 #endif
656 sockets_globals->last_error = 0;
657 sockets_globals->strerror_buf = NULL;
658 }
659 /* }}} */
660
661 /* {{{ PHP_MINIT_FUNCTION
662 */
663 static PHP_MINIT_FUNCTION(sockets)
664 {
665 #if defined(COMPILE_DL_SOCKETS) && defined(ZTS)
666 ZEND_TSRMLS_CACHE_UPDATE();
667 #endif
668 le_socket = zend_register_list_destructors_ex(php_destroy_socket, NULL, le_socket_name, module_number);
669 le_addrinfo = zend_register_list_destructors_ex(php_destroy_addrinfo, NULL, le_addrinfo_name, module_number);
670
671 REGISTER_LONG_CONSTANT("AF_UNIX", AF_UNIX, CONST_CS | CONST_PERSISTENT);
672 REGISTER_LONG_CONSTANT("AF_INET", AF_INET, CONST_CS | CONST_PERSISTENT);
673 #if HAVE_IPV6
674 REGISTER_LONG_CONSTANT("AF_INET6", AF_INET6, CONST_CS | CONST_PERSISTENT);
675 #endif
676 REGISTER_LONG_CONSTANT("SOCK_STREAM", SOCK_STREAM, CONST_CS | CONST_PERSISTENT);
677 REGISTER_LONG_CONSTANT("SOCK_DGRAM", SOCK_DGRAM, CONST_CS | CONST_PERSISTENT);
678 REGISTER_LONG_CONSTANT("SOCK_RAW", SOCK_RAW, CONST_CS | CONST_PERSISTENT);
679 REGISTER_LONG_CONSTANT("SOCK_SEQPACKET",SOCK_SEQPACKET, CONST_CS | CONST_PERSISTENT);
680 REGISTER_LONG_CONSTANT("SOCK_RDM", SOCK_RDM, CONST_CS | CONST_PERSISTENT);
681
682 REGISTER_LONG_CONSTANT("MSG_OOB", MSG_OOB, CONST_CS | CONST_PERSISTENT);
683 REGISTER_LONG_CONSTANT("MSG_WAITALL", MSG_WAITALL, CONST_CS | CONST_PERSISTENT);
684 REGISTER_LONG_CONSTANT("MSG_CTRUNC", MSG_CTRUNC, CONST_CS | CONST_PERSISTENT);
685 REGISTER_LONG_CONSTANT("MSG_TRUNC", MSG_TRUNC, CONST_CS | CONST_PERSISTENT);
686 REGISTER_LONG_CONSTANT("MSG_PEEK", MSG_PEEK, CONST_CS | CONST_PERSISTENT);
687 REGISTER_LONG_CONSTANT("MSG_DONTROUTE", MSG_DONTROUTE, CONST_CS | CONST_PERSISTENT);
688 #ifdef MSG_EOR
689 REGISTER_LONG_CONSTANT("MSG_EOR", MSG_EOR, CONST_CS | CONST_PERSISTENT);
690 #endif
691 #ifdef MSG_EOF
692 REGISTER_LONG_CONSTANT("MSG_EOF", MSG_EOF, CONST_CS | CONST_PERSISTENT);
693 #endif
694
695 #ifdef MSG_CONFIRM
696 REGISTER_LONG_CONSTANT("MSG_CONFIRM", MSG_CONFIRM, CONST_CS | CONST_PERSISTENT);
697 #endif
698 #ifdef MSG_ERRQUEUE
699 REGISTER_LONG_CONSTANT("MSG_ERRQUEUE", MSG_ERRQUEUE, CONST_CS | CONST_PERSISTENT);
700 #endif
701 #ifdef MSG_NOSIGNAL
702 REGISTER_LONG_CONSTANT("MSG_NOSIGNAL", MSG_NOSIGNAL, CONST_CS | CONST_PERSISTENT);
703 #endif
704 #ifdef MSG_DONTWAIT
705 REGISTER_LONG_CONSTANT("MSG_DONTWAIT", MSG_DONTWAIT, CONST_CS | CONST_PERSISTENT);
706 #endif
707 #ifdef MSG_MORE
708 REGISTER_LONG_CONSTANT("MSG_MORE", MSG_MORE, CONST_CS | CONST_PERSISTENT);
709 #endif
710 #ifdef MSG_WAITFORONE
711 REGISTER_LONG_CONSTANT("MSG_WAITFORONE",MSG_WAITFORONE, CONST_CS | CONST_PERSISTENT);
712 #endif
713 #ifdef MSG_CMSG_CLOEXEC
714 REGISTER_LONG_CONSTANT("MSG_CMSG_CLOEXEC",MSG_CMSG_CLOEXEC,CONST_CS | CONST_PERSISTENT);
715 #endif
716
717 REGISTER_LONG_CONSTANT("SO_DEBUG", SO_DEBUG, CONST_CS | CONST_PERSISTENT);
718 REGISTER_LONG_CONSTANT("SO_REUSEADDR", SO_REUSEADDR, CONST_CS | CONST_PERSISTENT);
719 #ifdef SO_REUSEPORT
720 REGISTER_LONG_CONSTANT("SO_REUSEPORT", SO_REUSEPORT, CONST_CS | CONST_PERSISTENT);
721 #endif
722 REGISTER_LONG_CONSTANT("SO_KEEPALIVE", SO_KEEPALIVE, CONST_CS | CONST_PERSISTENT);
723 REGISTER_LONG_CONSTANT("SO_DONTROUTE", SO_DONTROUTE, CONST_CS | CONST_PERSISTENT);
724 REGISTER_LONG_CONSTANT("SO_LINGER", SO_LINGER, CONST_CS | CONST_PERSISTENT);
725 REGISTER_LONG_CONSTANT("SO_BROADCAST", SO_BROADCAST, CONST_CS | CONST_PERSISTENT);
726 REGISTER_LONG_CONSTANT("SO_OOBINLINE", SO_OOBINLINE, CONST_CS | CONST_PERSISTENT);
727 REGISTER_LONG_CONSTANT("SO_SNDBUF", SO_SNDBUF, CONST_CS | CONST_PERSISTENT);
728 REGISTER_LONG_CONSTANT("SO_RCVBUF", SO_RCVBUF, CONST_CS | CONST_PERSISTENT);
729 REGISTER_LONG_CONSTANT("SO_SNDLOWAT", SO_SNDLOWAT, CONST_CS | CONST_PERSISTENT);
730 REGISTER_LONG_CONSTANT("SO_RCVLOWAT", SO_RCVLOWAT, CONST_CS | CONST_PERSISTENT);
731 REGISTER_LONG_CONSTANT("SO_SNDTIMEO", SO_SNDTIMEO, CONST_CS | CONST_PERSISTENT);
732 REGISTER_LONG_CONSTANT("SO_RCVTIMEO", SO_RCVTIMEO, CONST_CS | CONST_PERSISTENT);
733 REGISTER_LONG_CONSTANT("SO_TYPE", SO_TYPE, CONST_CS | CONST_PERSISTENT);
734 #ifdef SO_FAMILY
735 REGISTER_LONG_CONSTANT("SO_FAMILY", SO_FAMILY, CONST_CS | CONST_PERSISTENT);
736 #endif
737 REGISTER_LONG_CONSTANT("SO_ERROR", SO_ERROR, CONST_CS | CONST_PERSISTENT);
738 #ifdef SO_BINDTODEVICE
739 REGISTER_LONG_CONSTANT("SO_BINDTODEVICE", SO_BINDTODEVICE, CONST_CS | CONST_PERSISTENT);
740 #endif
741 REGISTER_LONG_CONSTANT("SOL_SOCKET", SOL_SOCKET, CONST_CS | CONST_PERSISTENT);
742 REGISTER_LONG_CONSTANT("SOMAXCONN", SOMAXCONN, CONST_CS | CONST_PERSISTENT);
743 #ifdef TCP_NODELAY
744 REGISTER_LONG_CONSTANT("TCP_NODELAY", TCP_NODELAY, CONST_CS | CONST_PERSISTENT);
745 #endif
746 REGISTER_LONG_CONSTANT("PHP_NORMAL_READ", PHP_NORMAL_READ, CONST_CS | CONST_PERSISTENT);
747 REGISTER_LONG_CONSTANT("PHP_BINARY_READ", PHP_BINARY_READ, CONST_CS | CONST_PERSISTENT);
748
749 REGISTER_LONG_CONSTANT("MCAST_JOIN_GROUP", PHP_MCAST_JOIN_GROUP, CONST_CS | CONST_PERSISTENT);
750 REGISTER_LONG_CONSTANT("MCAST_LEAVE_GROUP", PHP_MCAST_LEAVE_GROUP, CONST_CS | CONST_PERSISTENT);
751 #ifdef HAS_MCAST_EXT
752 REGISTER_LONG_CONSTANT("MCAST_BLOCK_SOURCE", PHP_MCAST_BLOCK_SOURCE, CONST_CS | CONST_PERSISTENT);
753 REGISTER_LONG_CONSTANT("MCAST_UNBLOCK_SOURCE", PHP_MCAST_UNBLOCK_SOURCE, CONST_CS | CONST_PERSISTENT);
754 REGISTER_LONG_CONSTANT("MCAST_JOIN_SOURCE_GROUP", PHP_MCAST_JOIN_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT);
755 REGISTER_LONG_CONSTANT("MCAST_LEAVE_SOURCE_GROUP", PHP_MCAST_LEAVE_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT);
756 #endif
757
758 REGISTER_LONG_CONSTANT("IP_MULTICAST_IF", IP_MULTICAST_IF, CONST_CS | CONST_PERSISTENT);
759 REGISTER_LONG_CONSTANT("IP_MULTICAST_TTL", IP_MULTICAST_TTL, CONST_CS | CONST_PERSISTENT);
760 REGISTER_LONG_CONSTANT("IP_MULTICAST_LOOP", IP_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT);
761 #if HAVE_IPV6
762 REGISTER_LONG_CONSTANT("IPV6_MULTICAST_IF", IPV6_MULTICAST_IF, CONST_CS | CONST_PERSISTENT);
763 REGISTER_LONG_CONSTANT("IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS, CONST_CS | CONST_PERSISTENT);
764 REGISTER_LONG_CONSTANT("IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT);
765 #endif
766
767 #ifdef IPV6_V6ONLY
768 REGISTER_LONG_CONSTANT("IPV6_V6ONLY", IPV6_V6ONLY, CONST_CS | CONST_PERSISTENT);
769 #endif
770
771 #ifndef WIN32
772 # include "unix_socket_constants.h"
773 #else
774 # include "win32_socket_constants.h"
775 #endif
776
777 REGISTER_LONG_CONSTANT("IPPROTO_IP", IPPROTO_IP, CONST_CS | CONST_PERSISTENT);
778 #if HAVE_IPV6
779 REGISTER_LONG_CONSTANT("IPPROTO_IPV6", IPPROTO_IPV6, CONST_CS | CONST_PERSISTENT);
780 #endif
781
782 REGISTER_LONG_CONSTANT("SOL_TCP", IPPROTO_TCP, CONST_CS | CONST_PERSISTENT);
783 REGISTER_LONG_CONSTANT("SOL_UDP", IPPROTO_UDP, CONST_CS | CONST_PERSISTENT);
784
785 #if HAVE_IPV6
786 REGISTER_LONG_CONSTANT("IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS, CONST_CS | CONST_PERSISTENT);
787 #endif
788
789 REGISTER_LONG_CONSTANT("AI_PASSIVE", AI_PASSIVE, CONST_CS | CONST_PERSISTENT);
790 REGISTER_LONG_CONSTANT("AI_CANONNAME", AI_CANONNAME, CONST_CS | CONST_PERSISTENT);
791 REGISTER_LONG_CONSTANT("AI_NUMERICHOST", AI_NUMERICHOST, CONST_CS | CONST_PERSISTENT);
792 #if HAVE_AI_V4MAPPED
793 REGISTER_LONG_CONSTANT("AI_V4MAPPED", AI_V4MAPPED, CONST_CS | CONST_PERSISTENT);
794 #endif
795 #if HAVE_AI_ALL
796 REGISTER_LONG_CONSTANT("AI_ALL", AI_ALL, CONST_CS | CONST_PERSISTENT);
797 #endif
798 REGISTER_LONG_CONSTANT("AI_ADDRCONFIG", AI_ADDRCONFIG, CONST_CS | CONST_PERSISTENT);
799 #if HAVE_AI_IDN
800 REGISTER_LONG_CONSTANT("AI_IDN", AI_IDN, CONST_CS | CONST_PERSISTENT);
801 REGISTER_LONG_CONSTANT("AI_CANONIDN", AI_CANONIDN, CONST_CS | CONST_PERSISTENT);
802 REGISTER_LONG_CONSTANT("AI_IDN_ALLOW_UNASSIGNED", AI_IDN_ALLOW_UNASSIGNED, CONST_CS | CONST_PERSISTENT);
803 REGISTER_LONG_CONSTANT("AI_IDN_USE_STD3_ASCII_RULES", AI_IDN_USE_STD3_ASCII_RULES, CONST_CS | CONST_PERSISTENT);
804 #endif
805 #ifdef AI_NUMERICSERV
806 REGISTER_LONG_CONSTANT("AI_NUMERICSERV", AI_NUMERICSERV, CONST_CS | CONST_PERSISTENT);
807 #endif
808
809 php_socket_sendrecvmsg_init(INIT_FUNC_ARGS_PASSTHRU);
810
811 return SUCCESS;
812 }
813 /* }}} */
814
815 /* {{{ PHP_MSHUTDOWN_FUNCTION
816 */
817 static PHP_MSHUTDOWN_FUNCTION(sockets)
818 {
819 php_socket_sendrecvmsg_shutdown(SHUTDOWN_FUNC_ARGS_PASSTHRU);
820
821 return SUCCESS;
822 }
823 /* }}} */
824
825 /* {{{ PHP_MINFO_FUNCTION
826 */
827 static PHP_MINFO_FUNCTION(sockets)
828 {
829 php_info_print_table_start();
830 php_info_print_table_row(2, "Sockets Support", "enabled");
831 php_info_print_table_end();
832 }
833 /* }}} */
834
835 /* {{{ PHP_RSHUTDOWN_FUNCTION */
836 static PHP_RSHUTDOWN_FUNCTION(sockets)
837 {
838 if (SOCKETS_G(strerror_buf)) {
839 efree(SOCKETS_G(strerror_buf));
840 SOCKETS_G(strerror_buf) = NULL;
841 }
842
843 return SUCCESS;
844 }
845 /* }}} */
846
847 static int php_sock_array_to_fd_set(zval *sock_array, fd_set *fds, PHP_SOCKET *max_fd) /* {{{ */
848 {
849 zval *element;
850 php_socket *php_sock;
851 int num = 0;
852
853 if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
854
855 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(sock_array), element) {
856 ZVAL_DEREF(element);
857 php_sock = (php_socket*) zend_fetch_resource_ex(element, le_socket_name, le_socket);
858 if (!php_sock) continue; /* If element is not a resource, skip it */
859
860 PHP_SAFE_FD_SET(php_sock->bsd_socket, fds);
861 if (php_sock->bsd_socket > *max_fd) {
862 *max_fd = php_sock->bsd_socket;
863 }
864 num++;
865 } ZEND_HASH_FOREACH_END();
866
867 return num ? 1 : 0;
868 }
869 /* }}} */
870
871 static int php_sock_array_from_fd_set(zval *sock_array, fd_set *fds) /* {{{ */
872 {
873 zval *element;
874 zval *dest_element;
875 php_socket *php_sock;
876 zval new_hash;
877 int num = 0;
878 zend_ulong num_key;
879 zend_string *key;
880
881 if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
882
883 array_init(&new_hash);
884 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(sock_array), num_key, key, element) {
885 ZVAL_DEREF(element);
886 php_sock = (php_socket*) zend_fetch_resource_ex(element, le_socket_name, le_socket);
887 if (!php_sock) continue; /* If element is not a resource, skip it */
888
889 if (PHP_SAFE_FD_ISSET(php_sock->bsd_socket, fds)) {
890 /* Add fd to new array */
891 if (key) {
892 dest_element = zend_hash_add(Z_ARRVAL(new_hash), key, element);
893 } else {
894 dest_element = zend_hash_index_update(Z_ARRVAL(new_hash), num_key, element);
895 }
896 if (dest_element) {
897 Z_ADDREF_P(dest_element);
898 }
899 }
900 num++;
901 } ZEND_HASH_FOREACH_END();
902
903 /* Destroy old array, add new one */
904 zval_ptr_dtor(sock_array);
905
906 ZVAL_COPY_VALUE(sock_array, &new_hash);
907
908 return num ? 1 : 0;
909 }
910 /* }}} */
911
912 /* {{{ proto int socket_select(array &read_fds, array &write_fds, array &except_fds, int tv_sec[, int tv_usec])
913 Runs the select() system call on the sets mentioned with a timeout specified by tv_sec and tv_usec */
914 PHP_FUNCTION(socket_select)
915 {
916 zval *r_array, *w_array, *e_array, *sec;
917 struct timeval tv;
918 struct timeval *tv_p = NULL;
919 fd_set rfds, wfds, efds;
920 PHP_SOCKET max_fd = 0;
921 int retval, sets = 0;
922 zend_long usec = 0;
923
924 if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/!a/!a/!z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE) {
925 return;
926 }
927
928 FD_ZERO(&rfds);
929 FD_ZERO(&wfds);
930 FD_ZERO(&efds);
931
932 if (r_array != NULL) sets += php_sock_array_to_fd_set(r_array, &rfds, &max_fd);
933 if (w_array != NULL) sets += php_sock_array_to_fd_set(w_array, &wfds, &max_fd);
934 if (e_array != NULL) sets += php_sock_array_to_fd_set(e_array, &efds, &max_fd);
935
936 if (!sets) {
937 php_error_docref(NULL, E_WARNING, "no resource arrays were passed to select");
938 RETURN_FALSE;
939 }
940
941 PHP_SAFE_MAX_FD(max_fd, 0); /* someone needs to make this look more like stream_socket_select */
942
943 /* If seconds is not set to null, build the timeval, else we wait indefinitely */
944 if (sec != NULL) {
945 zval tmp;
946
947 if (Z_TYPE_P(sec) != IS_LONG) {
948 tmp = *sec;
949 zval_copy_ctor(&tmp);
950 convert_to_long(&tmp);
951 sec = &tmp;
952 }
953
954 /* Solaris + BSD do not like microsecond values which are >= 1 sec */
955 if (usec > 999999) {
956 tv.tv_sec = Z_LVAL_P(sec) + (usec / 1000000);
957 tv.tv_usec = usec % 1000000;
958 } else {
959 tv.tv_sec = Z_LVAL_P(sec);
960 tv.tv_usec = usec;
961 }
962
963 tv_p = &tv;
964
965 if (sec == &tmp) {
966 zval_dtor(&tmp);
967 }
968 }
969
970 retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
971
972 if (retval == -1) {
973 SOCKETS_G(last_error) = errno;
974 php_error_docref(NULL, E_WARNING, "unable to select [%d]: %s", errno, sockets_strerror(errno));
975 RETURN_FALSE;
976 }
977
978 if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds);
979 if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds);
980 if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds);
981
982 RETURN_LONG(retval);
983 }
984 /* }}} */
985
986 /* {{{ proto resource socket_create_listen(int port[, int backlog])
987 Opens a socket on port to accept connections */
988 PHP_FUNCTION(socket_create_listen)
989 {
990 php_socket *php_sock;
991 zend_long port, backlog = 128;
992
993 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &port, &backlog) == FAILURE) {
994 return;
995 }
996
997 if (!php_open_listen_sock(&php_sock, port, backlog)) {
998 RETURN_FALSE;
999 }
1000
1001 php_sock->error = 0;
1002 php_sock->blocking = 1;
1003
1004 RETURN_RES(zend_register_resource(php_sock, le_socket));
1005 }
1006 /* }}} */
1007
1008 /* {{{ proto resource socket_accept(resource socket)
1009 Accepts a connection on the listening socket fd */
1010 PHP_FUNCTION(socket_accept)
1011 {
1012 zval *arg1;
1013 php_socket *php_sock, *new_sock;
1014 php_sockaddr_storage sa;
1015 socklen_t php_sa_len = sizeof(sa);
1016
1017 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
1018 return;
1019 }
1020
1021 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1022 RETURN_FALSE;
1023 }
1024
1025 if (!php_accept_connect(php_sock, &new_sock, (struct sockaddr*)&sa, &php_sa_len)) {
1026 RETURN_FALSE;
1027 }
1028
1029 RETURN_RES(zend_register_resource(new_sock, le_socket));
1030 }
1031 /* }}} */
1032
1033 /* {{{ proto bool socket_set_nonblock(resource socket)
1034 Sets nonblocking mode on a socket resource */
1035 PHP_FUNCTION(socket_set_nonblock)
1036 {
1037 zval *arg1;
1038 php_socket *php_sock;
1039
1040 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
1041 return;
1042 }
1043
1044 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1045 RETURN_FALSE;
1046 }
1047
1048 if (!Z_ISUNDEF(php_sock->zstream)) {
1049 php_stream *stream;
1050 /* omit notice if resource doesn't exist anymore */
1051 stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
1052 if (stream != NULL) {
1053 if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 0,
1054 NULL) != -1) {
1055 php_sock->blocking = 0;
1056 RETURN_TRUE;
1057 }
1058 }
1059 }
1060
1061 if (php_set_sock_blocking(php_sock->bsd_socket, 0) == SUCCESS) {
1062 php_sock->blocking = 0;
1063 RETURN_TRUE;
1064 } else {
1065 PHP_SOCKET_ERROR(php_sock, "unable to set nonblocking mode", errno);
1066 RETURN_FALSE;
1067 }
1068 }
1069 /* }}} */
1070
1071 /* {{{ proto bool socket_set_block(resource socket)
1072 Sets blocking mode on a socket resource */
1073 PHP_FUNCTION(socket_set_block)
1074 {
1075 zval *arg1;
1076 php_socket *php_sock;
1077
1078 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
1079 return;
1080 }
1081
1082 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1083 RETURN_FALSE;
1084 }
1085
1086 /* if socket was created from a stream, give the stream a chance to take
1087 * care of the operation itself, thereby allowing it to update its internal
1088 * state */
1089 if (!Z_ISUNDEF(php_sock->zstream)) {
1090 php_stream *stream;
1091 stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
1092 if (stream != NULL) {
1093 if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 1,
1094 NULL) != -1) {
1095 php_sock->blocking = 1;
1096 RETURN_TRUE;
1097 }
1098 }
1099 }
1100
1101 if (php_set_sock_blocking(php_sock->bsd_socket, 1) == SUCCESS) {
1102 php_sock->blocking = 1;
1103 RETURN_TRUE;
1104 } else {
1105 PHP_SOCKET_ERROR(php_sock, "unable to set blocking mode", errno);
1106 RETURN_FALSE;
1107 }
1108 }
1109 /* }}} */
1110
1111 /* {{{ proto bool socket_listen(resource socket[, int backlog])
1112 Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
1113 PHP_FUNCTION(socket_listen)
1114 {
1115 zval *arg1;
1116 php_socket *php_sock;
1117 zend_long backlog = 0;
1118
1119 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &arg1, &backlog) == FAILURE) {
1120 return;
1121 }
1122
1123 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1124 RETURN_FALSE;
1125 }
1126
1127 if (listen(php_sock->bsd_socket, backlog) != 0) {
1128 PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
1129 RETURN_FALSE;
1130 }
1131 RETURN_TRUE;
1132 }
1133 /* }}} */
1134
1135 /* {{{ proto void socket_close(resource socket)
1136 Closes a file descriptor */
1137 PHP_FUNCTION(socket_close)
1138 {
1139 zval *arg1;
1140 php_socket *php_sock;
1141
1142 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
1143 return;
1144 }
1145
1146 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1147 RETURN_FALSE;
1148 }
1149
1150 if (!Z_ISUNDEF(php_sock->zstream)) {
1151 php_stream *stream = NULL;
1152 php_stream_from_zval_no_verify(stream, &php_sock->zstream);
1153 if (stream != NULL) {
1154 /* close & destroy stream, incl. removing it from the rsrc list;
1155 * resource stored in php_sock->zstream will become invalid */
1156 php_stream_free(stream,
1157 PHP_STREAM_FREE_KEEP_RSRC | PHP_STREAM_FREE_CLOSE |
1158 (stream->is_persistent?PHP_STREAM_FREE_CLOSE_PERSISTENT:0));
1159 }
1160 }
1161 zend_list_close(Z_RES_P(arg1));
1162 }
1163 /* }}} */
1164
1165 /* {{{ proto int socket_write(resource socket, string buf[, int length])
1166 Writes the buffer to the socket resource, length is optional */
1167 PHP_FUNCTION(socket_write)
1168 {
1169 zval *arg1;
1170 php_socket *php_sock;
1171 int retval;
1172 size_t str_len;
1173 zend_long length = 0;
1174 char *str;
1175
1176 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &arg1, &str, &str_len, &length) == FAILURE) {
1177 return;
1178 }
1179
1180 if (length < 0) {
1181 php_error_docref(NULL, E_WARNING, "Length cannot be negative");
1182 RETURN_FALSE;
1183 }
1184
1185 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1186 RETURN_FALSE;
1187 }
1188
1189 if (ZEND_NUM_ARGS() < 3) {
1190 length = str_len;
1191 }
1192
1193 #ifndef PHP_WIN32
1194 retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
1195 #else
1196 retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
1197 #endif
1198
1199 if (retval < 0) {
1200 PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
1201 RETURN_FALSE;
1202 }
1203
1204 RETURN_LONG(retval);
1205 }
1206 /* }}} */
1207
1208 /* {{{ proto string socket_read(resource socket, int length [, int type])
1209 Reads a maximum of length bytes from socket */
1210 PHP_FUNCTION(socket_read)
1211 {
1212 zval *arg1;
1213 php_socket *php_sock;
1214 zend_string *tmpbuf;
1215 int retval;
1216 zend_long length, type = PHP_BINARY_READ;
1217
1218 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl|l", &arg1, &length, &type) == FAILURE) {
1219 return;
1220 }
1221
1222 /* overflow check */
1223 if ((length + 1) < 2) {
1224 RETURN_FALSE;
1225 }
1226
1227 tmpbuf = zend_string_alloc(length, 0);
1228
1229 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1230 RETURN_FALSE;
1231 }
1232
1233 if (type == PHP_NORMAL_READ) {
1234 retval = php_read(php_sock, ZSTR_VAL(tmpbuf), length, 0);
1235 } else {
1236 retval = recv(php_sock->bsd_socket, ZSTR_VAL(tmpbuf), length, 0);
1237 }
1238
1239 if (retval == -1) {
1240 /* if the socket is in non-blocking mode and there's no data to read,
1241 don't output any error, as this is a normal situation, and not an error */
1242 if (errno == EAGAIN
1243 #ifdef EWOULDBLOCK
1244 || errno == EWOULDBLOCK
1245 #endif
1246 ) {
1247 php_sock->error = errno;
1248 SOCKETS_G(last_error) = errno;
1249 } else {
1250 PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
1251 }
1252
1253 zend_string_free(tmpbuf);
1254 RETURN_FALSE;
1255 } else if (!retval) {
1256 zend_string_free(tmpbuf);
1257 RETURN_EMPTY_STRING();
1258 }
1259
1260 tmpbuf = zend_string_truncate(tmpbuf, retval, 0);
1261 ZSTR_LEN(tmpbuf) = retval;
1262 ZSTR_VAL(tmpbuf)[ZSTR_LEN(tmpbuf)] = '\0' ;
1263
1264 RETURN_NEW_STR(tmpbuf);
1265 }
1266 /* }}} */
1267
1268 /* {{{ proto bool socket_getsockname(resource socket, string &addr[, int &port])
1269 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. */
1270 PHP_FUNCTION(socket_getsockname)
1271 {
1272 zval *arg1, *addr, *port = NULL;
1273 php_sockaddr_storage sa_storage;
1274 php_socket *php_sock;
1275 struct sockaddr *sa;
1276 struct sockaddr_in *sin;
1277 #if HAVE_IPV6
1278 struct sockaddr_in6 *sin6;
1279 char addr6[INET6_ADDRSTRLEN+1];
1280 #endif
1281 struct sockaddr_un *s_un;
1282 char *addr_string;
1283 socklen_t salen = sizeof(php_sockaddr_storage);
1284
1285 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/|z/", &arg1, &addr, &port) == FAILURE) {
1286 return;
1287 }
1288
1289 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1290 RETURN_FALSE;
1291 }
1292
1293 sa = (struct sockaddr *) &sa_storage;
1294
1295 if (getsockname(php_sock->bsd_socket, sa, &salen) != 0) {
1296 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket name", errno);
1297 RETURN_FALSE;
1298 }
1299
1300 if (port != NULL) {
1301 ZVAL_DEREF(port);
1302 }
1303
1304 switch (sa->sa_family) {
1305 #if HAVE_IPV6
1306 case AF_INET6:
1307 sin6 = (struct sockaddr_in6 *) sa;
1308 inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
1309 zval_ptr_dtor(addr);
1310 ZVAL_STRING(addr, addr6);
1311
1312 if (port != NULL) {
1313 zval_ptr_dtor(port);
1314 ZVAL_LONG(port, htons(sin6->sin6_port));
1315 }
1316 RETURN_TRUE;
1317 break;
1318 #endif
1319 case AF_INET:
1320 sin = (struct sockaddr_in *) sa;
1321 while (inet_ntoa_lock == 1);
1322 inet_ntoa_lock = 1;
1323 addr_string = inet_ntoa(sin->sin_addr);
1324 inet_ntoa_lock = 0;
1325
1326 zval_ptr_dtor(addr);
1327 ZVAL_STRING(addr, addr_string);
1328
1329 if (port != NULL) {
1330 zval_ptr_dtor(port);
1331 ZVAL_LONG(port, htons(sin->sin_port));
1332 }
1333 RETURN_TRUE;
1334 break;
1335
1336 case AF_UNIX:
1337 s_un = (struct sockaddr_un *) sa;
1338
1339 zval_ptr_dtor(addr);
1340 ZVAL_STRING(addr, s_un->sun_path);
1341 RETURN_TRUE;
1342 break;
1343
1344 default:
1345 php_error_docref(NULL, E_WARNING, "Unsupported address family %d", sa->sa_family);
1346 RETURN_FALSE;
1347 }
1348 }
1349 /* }}} */
1350
1351 /* {{{ proto bool socket_getpeername(resource socket, string &addr[, int &port])
1352 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. */
1353 PHP_FUNCTION(socket_getpeername)
1354 {
1355 zval *arg1, *arg2, *arg3 = NULL;
1356 php_sockaddr_storage sa_storage;
1357 php_socket *php_sock;
1358 struct sockaddr *sa;
1359 struct sockaddr_in *sin;
1360 #if HAVE_IPV6
1361 struct sockaddr_in6 *sin6;
1362 char addr6[INET6_ADDRSTRLEN+1];
1363 #endif
1364 struct sockaddr_un *s_un;
1365 char *addr_string;
1366 socklen_t salen = sizeof(php_sockaddr_storage);
1367
1368 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/|z/", &arg1, &arg2, &arg3) == FAILURE) {
1369 return;
1370 }
1371
1372 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1373 RETURN_FALSE;
1374 }
1375
1376 sa = (struct sockaddr *) &sa_storage;
1377
1378 if (getpeername(php_sock->bsd_socket, sa, &salen) < 0) {
1379 PHP_SOCKET_ERROR(php_sock, "unable to retrieve peer name", errno);
1380 RETURN_FALSE;
1381 }
1382
1383 switch (sa->sa_family) {
1384 #if HAVE_IPV6
1385 case AF_INET6:
1386 sin6 = (struct sockaddr_in6 *) sa;
1387 inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
1388 zval_ptr_dtor(arg2);
1389 ZVAL_STRING(arg2, addr6);
1390
1391 if (arg3 != NULL) {
1392 zval_ptr_dtor(arg3);
1393 ZVAL_LONG(arg3, htons(sin6->sin6_port));
1394 }
1395
1396 RETURN_TRUE;
1397 break;
1398 #endif
1399 case AF_INET:
1400 sin = (struct sockaddr_in *) sa;
1401 while (inet_ntoa_lock == 1);
1402 inet_ntoa_lock = 1;
1403 addr_string = inet_ntoa(sin->sin_addr);
1404 inet_ntoa_lock = 0;
1405
1406 zval_ptr_dtor(arg2);
1407 ZVAL_STRING(arg2, addr_string);
1408
1409 if (arg3 != NULL) {
1410 zval_ptr_dtor(arg3);
1411 ZVAL_LONG(arg3, htons(sin->sin_port));
1412 }
1413
1414 RETURN_TRUE;
1415 break;
1416
1417 case AF_UNIX:
1418 s_un = (struct sockaddr_un *) sa;
1419
1420 zval_ptr_dtor(arg2);
1421 ZVAL_STRING(arg2, s_un->sun_path);
1422 RETURN_TRUE;
1423 break;
1424
1425 default:
1426 php_error_docref(NULL, E_WARNING, "Unsupported address family %d", sa->sa_family);
1427 RETURN_FALSE;
1428 }
1429 }
1430 /* }}} */
1431
1432 /* {{{ proto resource socket_create(int domain, int type, int protocol)
1433 Creates an endpoint for communication in the domain specified by domain, of type specified by type */
1434 PHP_FUNCTION(socket_create)
1435 {
1436 zend_long arg1, arg2, arg3;
1437 php_socket *php_sock = php_create_socket();
1438
1439 if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &arg1, &arg2, &arg3) == FAILURE) {
1440 efree(php_sock);
1441 return;
1442 }
1443
1444 if (arg1 != AF_UNIX
1445 #if HAVE_IPV6
1446 && arg1 != AF_INET6
1447 #endif
1448 && arg1 != AF_INET) {
1449 php_error_docref(NULL, E_WARNING, "invalid socket domain [" ZEND_LONG_FMT "] specified for argument 1, assuming AF_INET", arg1);
1450 arg1 = AF_INET;
1451 }
1452
1453 if (arg2 > 10) {
1454 php_error_docref(NULL, E_WARNING, "invalid socket type [" ZEND_LONG_FMT "] specified for argument 2, assuming SOCK_STREAM", arg2);
1455 arg2 = SOCK_STREAM;
1456 }
1457
1458 php_sock->bsd_socket = socket(arg1, arg2, arg3);
1459 php_sock->type = arg1;
1460
1461 if (IS_INVALID_SOCKET(php_sock)) {
1462 SOCKETS_G(last_error) = errno;
1463 php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
1464 efree(php_sock);
1465 RETURN_FALSE;
1466 }
1467
1468 php_sock->error = 0;
1469 php_sock->blocking = 1;
1470
1471 RETURN_RES(zend_register_resource(php_sock, le_socket));
1472 }
1473 /* }}} */
1474
1475 /* {{{ proto bool socket_connect(resource socket, string addr [, int port])
1476 Opens a connection to addr:port on the socket specified by socket */
1477 PHP_FUNCTION(socket_connect)
1478 {
1479 zval *arg1;
1480 php_socket *php_sock;
1481 char *addr;
1482 int retval;
1483 size_t addr_len;
1484 zend_long port = 0;
1485 int argc = ZEND_NUM_ARGS();
1486
1487 if (zend_parse_parameters(argc, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
1488 return;
1489 }
1490
1491 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1492 RETURN_FALSE;
1493 }
1494
1495 switch(php_sock->type) {
1496 #if HAVE_IPV6
1497 case AF_INET6: {
1498 struct sockaddr_in6 sin6 = {0};
1499
1500 if (argc != 3) {
1501 php_error_docref(NULL, E_WARNING, "Socket of type AF_INET6 requires 3 arguments");
1502 RETURN_FALSE;
1503 }
1504
1505 memset(&sin6, 0, sizeof(struct sockaddr_in6));
1506
1507 sin6.sin6_family = AF_INET6;
1508 sin6.sin6_port = htons((unsigned short int)port);
1509
1510 if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
1511 RETURN_FALSE;
1512 }
1513
1514 retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6));
1515 break;
1516 }
1517 #endif
1518 case AF_INET: {
1519 struct sockaddr_in sin = {0};
1520
1521 if (argc != 3) {
1522 php_error_docref(NULL, E_WARNING, "Socket of type AF_INET requires 3 arguments");
1523 RETURN_FALSE;
1524 }
1525
1526 sin.sin_family = AF_INET;
1527 sin.sin_port = htons((unsigned short int)port);
1528
1529 if (! php_set_inet_addr(&sin, addr, php_sock)) {
1530 RETURN_FALSE;
1531 }
1532
1533 retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
1534 break;
1535 }
1536
1537 case AF_UNIX: {
1538 struct sockaddr_un s_un = {0};
1539
1540 if (addr_len >= sizeof(s_un.sun_path)) {
1541 php_error_docref(NULL, E_WARNING, "Path too long");
1542 RETURN_FALSE;
1543 }
1544
1545 s_un.sun_family = AF_UNIX;
1546 memcpy(&s_un.sun_path, addr, addr_len);
1547 retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un,
1548 (socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len));
1549 break;
1550 }
1551
1552 default:
1553 php_error_docref(NULL, E_WARNING, "Unsupported socket type %d", php_sock->type);
1554 RETURN_FALSE;
1555 }
1556
1557 if (retval != 0) {
1558 PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
1559 RETURN_FALSE;
1560 }
1561
1562 RETURN_TRUE;
1563 }
1564 /* }}} */
1565
1566 /* {{{ proto string socket_strerror(int errno)
1567 Returns a string describing an error */
1568 PHP_FUNCTION(socket_strerror)
1569 {
1570 zend_long arg1;
1571
1572 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &arg1) == FAILURE) {
1573 return;
1574 }
1575
1576 RETURN_STRING(sockets_strerror(arg1));
1577 }
1578 /* }}} */
1579
1580 /* {{{ proto bool socket_bind(resource socket, string addr [, int port])
1581 Binds an open socket to a listening port, port is only specified in AF_INET family. */
1582 PHP_FUNCTION(socket_bind)
1583 {
1584 zval *arg1;
1585 php_sockaddr_storage sa_storage = {0};
1586 struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
1587 php_socket *php_sock;
1588 char *addr;
1589 size_t addr_len;
1590 zend_long port = 0;
1591 zend_long retval = 0;
1592
1593 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
1594 return;
1595 }
1596
1597 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1598 RETURN_FALSE;
1599 }
1600
1601 switch(php_sock->type) {
1602 case AF_UNIX:
1603 {
1604 struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
1605
1606 sa->sun_family = AF_UNIX;
1607
1608 if (addr_len >= sizeof(sa->sun_path)) {
1609 php_error_docref(NULL, E_WARNING,
1610 "Invalid path: too long (maximum size is %d)",
1611 (int)sizeof(sa->sun_path) - 1);
1612 RETURN_FALSE;
1613 }
1614 memcpy(&sa->sun_path, addr, addr_len);
1615
1616 retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa,
1617 offsetof(struct sockaddr_un, sun_path) + addr_len);
1618 break;
1619 }
1620
1621 case AF_INET:
1622 {
1623 struct sockaddr_in *sa = (struct sockaddr_in *) sock_type;
1624
1625 sa->sin_family = AF_INET;
1626 sa->sin_port = htons((unsigned short) port);
1627
1628 if (! php_set_inet_addr(sa, addr, php_sock)) {
1629 RETURN_FALSE;
1630 }
1631
1632 retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
1633 break;
1634 }
1635 #if HAVE_IPV6
1636 case AF_INET6:
1637 {
1638 struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
1639
1640 sa->sin6_family = AF_INET6;
1641 sa->sin6_port = htons((unsigned short) port);
1642
1643 if (! php_set_inet6_addr(sa, addr, php_sock)) {
1644 RETURN_FALSE;
1645 }
1646
1647 retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in6));
1648 break;
1649 }
1650 #endif
1651 default:
1652 php_error_docref(NULL, E_WARNING, "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6", php_sock->type);
1653 RETURN_FALSE;
1654 }
1655
1656 if (retval != 0) {
1657 PHP_SOCKET_ERROR(php_sock, "unable to bind address", errno);
1658 RETURN_FALSE;
1659 }
1660
1661 RETURN_TRUE;
1662 }
1663 /* }}} */
1664
1665 /* {{{ proto int socket_recv(resource socket, string &buf, int len, int flags)
1666 Receives data from a connected socket */
1667 PHP_FUNCTION(socket_recv)
1668 {
1669 zval *php_sock_res, *buf;
1670 zend_string *recv_buf;
1671 php_socket *php_sock;
1672 int retval;
1673 zend_long len, flags;
1674
1675 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/ll", &php_sock_res, &buf, &len, &flags) == FAILURE) {
1676 return;
1677 }
1678
1679 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(php_sock_res), le_socket_name, le_socket)) == NULL) {
1680 RETURN_FALSE;
1681 }
1682
1683 /* overflow check */
1684 if ((len + 1) < 2) {
1685 RETURN_FALSE;
1686 }
1687
1688 recv_buf = zend_string_alloc(len, 0);
1689
1690 if ((retval = recv(php_sock->bsd_socket, ZSTR_VAL(recv_buf), len, flags)) < 1) {
1691 zend_string_free(recv_buf);
1692
1693 zval_ptr_dtor(buf);
1694 ZVAL_NULL(buf);
1695 } else {
1696 ZSTR_LEN(recv_buf) = retval;
1697 ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1698
1699 /* Rebuild buffer zval */
1700 zval_ptr_dtor(buf);
1701 ZVAL_NEW_STR(buf, recv_buf);
1702 }
1703
1704 if (retval == -1) {
1705 PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
1706 RETURN_FALSE;
1707 }
1708
1709 RETURN_LONG(retval);
1710 }
1711 /* }}} */
1712
1713 /* {{{ proto int socket_send(resource socket, string buf, int len, int flags)
1714 Sends data to a connected socket */
1715 PHP_FUNCTION(socket_send)
1716 {
1717 zval *arg1;
1718 php_socket *php_sock;
1719 size_t buf_len, retval;
1720 zend_long len, flags;
1721 char *buf;
1722
1723 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsll", &arg1, &buf, &buf_len, &len, &flags) == FAILURE) {
1724 return;
1725 }
1726
1727 if (len < 0) {
1728 php_error_docref(NULL, E_WARNING, "Length cannot be negative");
1729 RETURN_FALSE;
1730 }
1731
1732 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1733 RETURN_FALSE;
1734 }
1735
1736 retval = send(php_sock->bsd_socket, buf, (buf_len < (size_t)len ? buf_len : (size_t)len), flags);
1737
1738 if (retval == (size_t)-1) {
1739 PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
1740 RETURN_FALSE;
1741 }
1742
1743 RETURN_LONG(retval);
1744 }
1745 /* }}} */
1746
1747 /* {{{ proto int socket_recvfrom(resource socket, string &buf, int len, int flags, string &name [, int &port])
1748 Receives data from a socket, connected or not */
1749 PHP_FUNCTION(socket_recvfrom)
1750 {
1751 zval *arg1, *arg2, *arg5, *arg6 = NULL;
1752 php_socket *php_sock;
1753 struct sockaddr_un s_un;
1754 struct sockaddr_in sin;
1755 #if HAVE_IPV6
1756 struct sockaddr_in6 sin6;
1757 char addr6[INET6_ADDRSTRLEN];
1758 #endif
1759 socklen_t slen;
1760 int retval;
1761 zend_long arg3, arg4;
1762 char *address;
1763 zend_string *recv_buf;
1764
1765 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/llz/|z/", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
1766 return;
1767 }
1768
1769 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1770 RETURN_FALSE;
1771 }
1772
1773 /* overflow check */
1774 if ((arg3 + 2) < 3) {
1775 RETURN_FALSE;
1776 }
1777
1778 recv_buf = zend_string_alloc(arg3 + 1, 0);
1779
1780 switch (php_sock->type) {
1781 case AF_UNIX:
1782 slen = sizeof(s_un);
1783 memset(&s_un, 0, slen);
1784 s_un.sun_family = AF_UNIX;
1785
1786 retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
1787
1788 if (retval < 0) {
1789 PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
1790 zend_string_free(recv_buf);
1791 RETURN_FALSE;
1792 }
1793 ZSTR_LEN(recv_buf) = retval;
1794 ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1795
1796 zval_ptr_dtor(arg2);
1797 zval_ptr_dtor(arg5);
1798
1799 ZVAL_NEW_STR(arg2, recv_buf);
1800 ZVAL_STRING(arg5, s_un.sun_path);
1801 break;
1802
1803 case AF_INET:
1804 slen = sizeof(sin);
1805 memset(&sin, 0, slen);
1806 sin.sin_family = AF_INET;
1807
1808 if (arg6 == NULL) {
1809 zend_string_free(recv_buf);
1810 WRONG_PARAM_COUNT;
1811 }
1812
1813 retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
1814
1815 if (retval < 0) {
1816 PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
1817 zend_string_free(recv_buf);
1818 RETURN_FALSE;
1819 }
1820 ZSTR_LEN(recv_buf) = retval;
1821 ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1822
1823 zval_ptr_dtor(arg2);
1824 zval_ptr_dtor(arg5);
1825 zval_ptr_dtor(arg6);
1826
1827 address = inet_ntoa(sin.sin_addr);
1828
1829 ZVAL_NEW_STR(arg2, recv_buf);
1830 ZVAL_STRING(arg5, address ? address : "0.0.0.0");
1831 ZVAL_LONG(arg6, ntohs(sin.sin_port));
1832 break;
1833 #if HAVE_IPV6
1834 case AF_INET6:
1835 slen = sizeof(sin6);
1836 memset(&sin6, 0, slen);
1837 sin6.sin6_family = AF_INET6;
1838
1839 if (arg6 == NULL) {
1840 zend_string_free(recv_buf);
1841 WRONG_PARAM_COUNT;
1842 }
1843
1844 retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
1845
1846 if (retval < 0) {
1847 PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
1848 zend_string_free(recv_buf);
1849 RETURN_FALSE;
1850 }
1851 ZSTR_LEN(recv_buf) = retval;
1852 ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1853
1854 zval_ptr_dtor(arg2);
1855 zval_ptr_dtor(arg5);
1856 zval_ptr_dtor(arg6);
1857
1858 memset(addr6, 0, INET6_ADDRSTRLEN);
1859 inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
1860
1861 ZVAL_NEW_STR(arg2, recv_buf);
1862 ZVAL_STRING(arg5, addr6[0] ? addr6 : "::");
1863 ZVAL_LONG(arg6, ntohs(sin6.sin6_port));
1864 break;
1865 #endif
1866 default:
1867 php_error_docref(NULL, E_WARNING, "Unsupported socket type %d", php_sock->type);
1868 RETURN_FALSE;
1869 }
1870
1871 RETURN_LONG(retval);
1872 }
1873 /* }}} */
1874
1875 /* {{{ proto int socket_sendto(resource socket, string buf, int len, int flags, string addr [, int port])
1876 Sends a message to a socket, whether it is connected or not */
1877 PHP_FUNCTION(socket_sendto)
1878 {
1879 zval *arg1;
1880 php_socket *php_sock;
1881 struct sockaddr_un s_un;
1882 struct sockaddr_in sin;
1883 #if HAVE_IPV6
1884 struct sockaddr_in6 sin6;
1885 #endif
1886 int retval;
1887 size_t buf_len, addr_len;
1888 zend_long len, flags, port = 0;
1889 char *buf, *addr;
1890 int argc = ZEND_NUM_ARGS();
1891
1892 if (zend_parse_parameters(argc, "rslls|l", &arg1, &buf, &buf_len, &len, &flags, &addr, &addr_len, &port) == FAILURE) {
1893 return;
1894 }
1895
1896 if (len < 0) {
1897 php_error_docref(NULL, E_WARNING, "Length cannot be negative");
1898 RETURN_FALSE;
1899 }
1900
1901 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1902 RETURN_FALSE;
1903 }
1904
1905 switch (php_sock->type) {
1906 case AF_UNIX:
1907 memset(&s_un, 0, sizeof(s_un));
1908 s_un.sun_family = AF_UNIX;
1909 snprintf(s_un.sun_path, 108, "%s", addr);
1910
1911 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));
1912 break;
1913
1914 case AF_INET:
1915 if (argc != 6) {
1916 WRONG_PARAM_COUNT;
1917 }
1918
1919 memset(&sin, 0, sizeof(sin));
1920 sin.sin_family = AF_INET;
1921 sin.sin_port = htons((unsigned short) port);
1922
1923 if (! php_set_inet_addr(&sin, addr, php_sock)) {
1924 RETURN_FALSE;
1925 }
1926
1927 retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin, sizeof(sin));
1928 break;
1929 #if HAVE_IPV6
1930 case AF_INET6:
1931 if (argc != 6) {
1932 WRONG_PARAM_COUNT;
1933 }
1934
1935 memset(&sin6, 0, sizeof(sin6));
1936 sin6.sin6_family = AF_INET6;
1937 sin6.sin6_port = htons((unsigned short) port);
1938
1939 if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
1940 RETURN_FALSE;
1941 }
1942
1943 retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin6, sizeof(sin6));
1944 break;
1945 #endif
1946 default:
1947 php_error_docref(NULL, E_WARNING, "Unsupported socket type %d", php_sock->type);
1948 RETURN_FALSE;
1949 }
1950
1951 if (retval == -1) {
1952 PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
1953 RETURN_FALSE;
1954 }
1955
1956 RETURN_LONG(retval);
1957 }
1958 /* }}} */
1959
1960 /* {{{ proto mixed socket_get_option(resource socket, int level, int optname)
1961 Gets socket options for the socket */
1962 PHP_FUNCTION(socket_get_option)
1963 {
1964 zval *arg1;
1965 struct linger linger_val;
1966 struct timeval tv;
1967 #ifdef PHP_WIN32
1968 int timeout = 0;
1969 #endif
1970 socklen_t optlen;
1971 php_socket *php_sock;
1972 int other_val;
1973 zend_long level, optname;
1974
1975 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rll", &arg1, &level, &optname) == FAILURE) {
1976 return;
1977 }
1978
1979 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
1980 RETURN_FALSE;
1981 }
1982
1983 if (level == IPPROTO_IP) {
1984 switch (optname) {
1985 case IP_MULTICAST_IF: {
1986 struct in_addr if_addr;
1987 unsigned int if_index;
1988 optlen = sizeof(if_addr);
1989 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&if_addr, &optlen) != 0) {
1990 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
1991 RETURN_FALSE;
1992 }
1993 if (php_add4_to_if_index(&if_addr, php_sock, &if_index) == SUCCESS) {
1994 RETURN_LONG((zend_long) if_index);
1995 } else {
1996 RETURN_FALSE;
1997 }
1998 }
1999 }
2000 }
2001 #if HAVE_IPV6
2002 else if (level == IPPROTO_IPV6) {
2003 int ret = php_do_getsockopt_ipv6_rfc3542(php_sock, level, optname, return_value);
2004 if (ret == SUCCESS) {
2005 return;
2006 } else if (ret == FAILURE) {
2007 RETURN_FALSE;
2008 } /* else continue */
2009 }
2010 #endif
2011
2012 /* sol_socket options and general case */
2013 switch(optname) {
2014 case SO_LINGER:
2015 optlen = sizeof(linger_val);
2016
2017 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&linger_val, &optlen) != 0) {
2018 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2019 RETURN_FALSE;
2020 }
2021
2022 array_init(return_value);
2023 add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
2024 add_assoc_long(return_value, "l_linger", linger_val.l_linger);
2025 break;
2026
2027 case SO_RCVTIMEO:
2028 case SO_SNDTIMEO:
2029 #ifndef PHP_WIN32
2030 optlen = sizeof(tv);
2031
2032 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
2033 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2034 RETURN_FALSE;
2035 }
2036 #else
2037 optlen = sizeof(int);
2038
2039 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&timeout, &optlen) != 0) {
2040 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2041 RETURN_FALSE;
2042 }
2043
2044 tv.tv_sec = timeout ? timeout / 1000 : 0;
2045 tv.tv_usec = timeout ? (timeout * 1000) % 1000000 : 0;
2046 #endif
2047
2048 array_init(return_value);
2049
2050 add_assoc_long(return_value, "sec", tv.tv_sec);
2051 add_assoc_long(return_value, "usec", tv.tv_usec);
2052 break;
2053
2054 default:
2055 optlen = sizeof(other_val);
2056
2057 if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&other_val, &optlen) != 0) {
2058 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2059 RETURN_FALSE;
2060 }
2061 if (optlen == 1)
2062 other_val = *((unsigned char *)&other_val);
2063
2064 RETURN_LONG(other_val);
2065 break;
2066 }
2067 }
2068 /* }}} */
2069
2070 /* {{{ proto bool socket_set_option(resource socket, int level, int optname, int|array optval)
2071 Sets socket options for the socket */
2072 PHP_FUNCTION(socket_set_option)
2073 {
2074 zval *arg1, *arg4;
2075 struct linger lv;
2076 php_socket *php_sock;
2077 int ov, optlen, retval;
2078 #ifdef PHP_WIN32
2079 int timeout;
2080 #else
2081 struct timeval tv;
2082 #endif
2083 zend_long level, optname;
2084 void *opt_ptr;
2085 HashTable *opt_ht;
2086 zval *l_onoff, *l_linger;
2087 zval *sec, *usec;
2088
2089
2090 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllz", &arg1, &level, &optname, &arg4) == FAILURE) {
2091 return;
2092 }
2093
2094 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
2095 RETURN_FALSE;
2096 }
2097
2098 set_errno(0);
2099
2100 #define HANDLE_SUBCALL(res) \
2101 do { \
2102 if (res == 1) { goto default_case; } \
2103 else if (res == SUCCESS) { RETURN_TRUE; } \
2104 else { RETURN_FALSE; } \
2105 } while (0)
2106
2107
2108 if (level == IPPROTO_IP) {
2109 int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4);
2110 HANDLE_SUBCALL(res);
2111 }
2112
2113 #if HAVE_IPV6
2114 else if (level == IPPROTO_IPV6) {
2115 int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4);
2116 if (res == 1) {
2117 res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4);
2118 }
2119 HANDLE_SUBCALL(res);
2120 }
2121 #endif
2122
2123 switch (optname) {
2124 case SO_LINGER: {
2125 const char l_onoff_key[] = "l_onoff";
2126 const char l_linger_key[] = "l_linger";
2127
2128 convert_to_array_ex(arg4);
2129 opt_ht = Z_ARRVAL_P(arg4);
2130
2131 if ((l_onoff = zend_hash_str_find(opt_ht, l_onoff_key, sizeof(l_onoff_key) - 1)) == NULL) {
2132 php_error_docref(NULL, E_WARNING, "no key \"%s\" passed in optval", l_onoff_key);
2133 RETURN_FALSE;
2134 }
2135 if ((l_linger = zend_hash_str_find(opt_ht, l_linger_key, sizeof(l_linger_key) - 1)) == NULL) {
2136 php_error_docref(NULL, E_WARNING, "no key \"%s\" passed in optval", l_linger_key);
2137 RETURN_FALSE;
2138 }
2139
2140 convert_to_long_ex(l_onoff);
2141 convert_to_long_ex(l_linger);
2142
2143 lv.l_onoff = (unsigned short)Z_LVAL_P(l_onoff);
2144 lv.l_linger = (unsigned short)Z_LVAL_P(l_linger);
2145
2146 optlen = sizeof(lv);
2147 opt_ptr = &lv;
2148 break;
2149 }
2150
2151 case SO_RCVTIMEO:
2152 case SO_SNDTIMEO: {
2153 const char sec_key[] = "sec";
2154 const char usec_key[] = "usec";
2155
2156 convert_to_array_ex(arg4);
2157 opt_ht = Z_ARRVAL_P(arg4);
2158
2159 if ((sec = zend_hash_str_find(opt_ht, sec_key, sizeof(sec_key) - 1)) == NULL) {
2160 php_error_docref(NULL, E_WARNING, "no key \"%s\" passed in optval", sec_key);
2161 RETURN_FALSE;
2162 }
2163 if ((usec = zend_hash_str_find(opt_ht, usec_key, sizeof(usec_key) - 1)) == NULL) {
2164 php_error_docref(NULL, E_WARNING, "no key \"%s\" passed in optval", usec_key);
2165 RETURN_FALSE;
2166 }
2167
2168 convert_to_long_ex(sec);
2169 convert_to_long_ex(usec);
2170 #ifndef PHP_WIN32
2171 tv.tv_sec = Z_LVAL_P(sec);
2172 tv.tv_usec = Z_LVAL_P(usec);
2173 optlen = sizeof(tv);
2174 opt_ptr = &tv;
2175 #else
2176 timeout = Z_LVAL_P(sec) * 1000 + Z_LVAL_P(usec) / 1000;
2177 optlen = sizeof(int);
2178 opt_ptr = &timeout;
2179 #endif
2180 break;
2181 }
2182 #ifdef SO_BINDTODEVICE
2183 case SO_BINDTODEVICE: {
2184 if (Z_TYPE_P(arg4) == IS_STRING) {
2185 opt_ptr = Z_STRVAL_P(arg4);
2186 optlen = Z_STRLEN_P(arg4);
2187 } else {
2188 opt_ptr = "";
2189 optlen = 0;
2190 }
2191 break;
2192 }
2193 #endif
2194
2195 default:
2196 default_case:
2197 convert_to_long_ex(arg4);
2198 ov = Z_LVAL_P(arg4);
2199
2200 optlen = sizeof(ov);
2201 opt_ptr = &ov;
2202 break;
2203 }
2204
2205 retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
2206 if (retval != 0) {
2207 PHP_SOCKET_ERROR(php_sock, "unable to set socket option", errno);
2208 RETURN_FALSE;
2209 }
2210
2211 RETURN_TRUE;
2212 }
2213 /* }}} */
2214
2215 #ifdef HAVE_SOCKETPAIR
2216 /* {{{ proto bool socket_create_pair(int domain, int type, int protocol, array &fd)
2217 Creates a pair of indistinguishable sockets and stores them in fds. */
2218 PHP_FUNCTION(socket_create_pair)
2219 {
2220 zval retval[2], *fds_array_zval;
2221 php_socket *php_sock[2];
2222 PHP_SOCKET fds_array[2];
2223 zend_long domain, type, protocol;
2224
2225 if (zend_parse_parameters(ZEND_NUM_ARGS(), "lllz/", &domain, &type, &protocol, &fds_array_zval) == FAILURE) {
2226 return;
2227 }
2228
2229 php_sock[0] = php_create_socket();
2230 php_sock[1] = php_create_socket();
2231
2232 if (domain != AF_INET
2233 #if HAVE_IPV6
2234 && domain != AF_INET6
2235 #endif
2236 && domain != AF_UNIX) {
2237 php_error_docref(NULL, E_WARNING, "invalid socket domain [" ZEND_LONG_FMT "] specified for argument 1, assuming AF_INET", domain);
2238 domain = AF_INET;
2239 }
2240
2241 if (type > 10) {
2242 php_error_docref(NULL, E_WARNING, "invalid socket type [" ZEND_LONG_FMT "] specified for argument 2, assuming SOCK_STREAM", type);
2243 type = SOCK_STREAM;
2244 }
2245
2246 if (socketpair(domain, type, protocol, fds_array) != 0) {
2247 SOCKETS_G(last_error) = errno;
2248 php_error_docref(NULL, E_WARNING, "unable to create socket pair [%d]: %s", errno, sockets_strerror(errno));
2249 efree(php_sock[0]);
2250 efree(php_sock[1]);
2251 RETURN_FALSE;
2252 }
2253
2254 zval_ptr_dtor(fds_array_zval);
2255 array_init(fds_array_zval);
2256
2257 php_sock[0]->bsd_socket = fds_array[0];
2258 php_sock[1]->bsd_socket = fds_array[1];
2259 php_sock[0]->type = domain;
2260 php_sock[1]->type = domain;
2261 php_sock[0]->error = 0;
2262 php_sock[1]->error = 0;
2263 php_sock[0]->blocking = 1;
2264 php_sock[1]->blocking = 1;
2265
2266 ZVAL_RES(&retval[0], zend_register_resource(php_sock[0], le_socket));
2267 ZVAL_RES(&retval[1], zend_register_resource(php_sock[1], le_socket));
2268
2269 add_index_zval(fds_array_zval, 0, &retval[0]);
2270 add_index_zval(fds_array_zval, 1, &retval[1]);
2271
2272 RETURN_TRUE;
2273 }
2274 /* }}} */
2275 #endif
2276
2277 #ifdef HAVE_SHUTDOWN
2278 /* {{{ proto bool socket_shutdown(resource socket[, int how])
2279 Shuts down a socket for receiving, sending, or both. */
2280 PHP_FUNCTION(socket_shutdown)
2281 {
2282 zval *arg1;
2283 zend_long how_shutdown = 2;
2284 php_socket *php_sock;
2285
2286 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &arg1, &how_shutdown) == FAILURE) {
2287 return;
2288 }
2289
2290 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
2291 RETURN_FALSE;
2292 }
2293
2294 if (shutdown(php_sock->bsd_socket, how_shutdown) != 0) {
2295 PHP_SOCKET_ERROR(php_sock, "unable to shutdown socket", errno);
2296 RETURN_FALSE;
2297 }
2298
2299 RETURN_TRUE;
2300 }
2301 /* }}} */
2302 #endif
2303
2304 /* {{{ proto int socket_last_error([resource socket])
2305 Returns the last socket error (either the last used or the provided socket resource) */
2306 PHP_FUNCTION(socket_last_error)
2307 {
2308 zval *arg1 = NULL;
2309 php_socket *php_sock;
2310
2311 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &arg1) == FAILURE) {
2312 return;
2313 }
2314
2315 if (arg1) {
2316 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
2317 RETURN_FALSE;
2318 }
2319 RETVAL_LONG(php_sock->error);
2320 } else {
2321 RETVAL_LONG(SOCKETS_G(last_error));
2322 }
2323 }
2324 /* }}} */
2325
2326 /* {{{ proto void socket_clear_error([resource socket])
2327 Clears the error on the socket or the last error code. */
2328 PHP_FUNCTION(socket_clear_error)
2329 {
2330 zval *arg1 = NULL;
2331 php_socket *php_sock;
2332
2333 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &arg1) == FAILURE) {
2334 return;
2335 }
2336
2337 if (arg1) {
2338 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
2339 RETURN_FALSE;
2340 }
2341 php_sock->error = 0;
2342 } else {
2343 SOCKETS_G(last_error) = 0;
2344 }
2345
2346 return;
2347 }
2348 /* }}} */
2349
2350 php_socket *socket_import_file_descriptor(PHP_SOCKET socket)
2351 {
2352 #ifdef SO_DOMAIN
2353 int type;
2354 socklen_t type_len = sizeof(type);
2355 #endif
2356 php_socket *retsock;
2357 php_sockaddr_storage addr;
2358 socklen_t addr_len = sizeof(addr);
2359 #ifndef PHP_WIN32
2360 int t;
2361 #endif
2362
2363 retsock = php_create_socket();
2364 retsock->bsd_socket = socket;
2365
2366 /* determine family */
2367 #ifdef SO_DOMAIN
2368 if (getsockopt(socket, SOL_SOCKET, SO_DOMAIN, &type, &type_len) == 0) {
2369 retsock->type = type;
2370 } else
2371 #endif
2372 if (getsockname(socket, (struct sockaddr*)&addr, &addr_len) == 0) {
2373 retsock->type = addr.ss_family;
2374 } else {
2375 PHP_SOCKET_ERROR(retsock, "unable to obtain socket family", errno);
2376 goto error;
2377 }
2378
2379 /* determine blocking mode */
2380 #ifndef PHP_WIN32
2381 t = fcntl(socket, F_GETFL);
2382 if (t == -1) {
2383 PHP_SOCKET_ERROR(retsock, "unable to obtain blocking state", errno);
2384 goto error;
2385 } else {
2386 retsock->blocking = !(t & O_NONBLOCK);
2387 }
2388 #endif
2389
2390 return retsock;
2391
2392 error:
2393 efree(retsock);
2394 return NULL;
2395 }
2396
2397 /* {{{ proto resource socket_import_stream(resource stream)
2398 Imports a stream that encapsulates a socket into a socket extension resource. */
2399 PHP_FUNCTION(socket_import_stream)
2400 {
2401 zval *zstream;
2402 php_stream *stream;
2403 php_socket *retsock = NULL;
2404 PHP_SOCKET socket; /* fd */
2405
2406 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream) == FAILURE) {
2407 return;
2408 }
2409 php_stream_from_zval(stream, zstream);
2410
2411 if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) {
2412 /* error supposedly already shown */
2413 RETURN_FALSE;
2414 }
2415
2416 retsock = socket_import_file_descriptor(socket);
2417 if (retsock == NULL) {
2418 RETURN_FALSE;
2419 }
2420
2421 #ifdef PHP_WIN32
2422 /* on windows, check if the stream is a socket stream and read its
2423 * private data; otherwise assume it's in non-blocking mode */
2424 if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) {
2425 retsock->blocking =
2426 ((php_netstream_data_t *)stream->abstract)->is_blocked;
2427 } else {
2428 retsock->blocking = 1;
2429 }
2430 #endif
2431
2432 /* hold a zval reference to the stream (holding a php_stream* directly could
2433 * also be done, but this makes socket_export_stream a bit simpler) */
2434 ZVAL_COPY(&retsock->zstream, zstream);
2435
2436 php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER,
2437 PHP_STREAM_BUFFER_NONE, NULL);
2438
2439 RETURN_RES(zend_register_resource(retsock, le_socket));
2440 }
2441 /* }}} */
2442
2443 /* {{{ proto resource socket_export_stream(resource socket)
2444 Exports a socket extension resource into a stream that encapsulates a socket. */
2445 PHP_FUNCTION(socket_export_stream)
2446 {
2447 zval *zsocket;
2448 php_socket *socket;
2449 php_stream *stream = NULL;
2450 php_netstream_data_t *stream_data;
2451 char *protocol = NULL;
2452 size_t protocollen = 0;
2453
2454 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zsocket) == FAILURE) {
2455 return;
2456 }
2457 if ((socket = (php_socket *) zend_fetch_resource(Z_RES_P(zsocket), le_socket_name, le_socket)) == NULL) {
2458 RETURN_FALSE;
2459 }
2460
2461 /* Either we already exported a stream or the socket came from an import,
2462 * just return the existing stream */
2463 if (!Z_ISUNDEF(socket->zstream)) {
2464 RETURN_ZVAL(&socket->zstream, 1, 0);
2465 }
2466
2467 /* Determine if socket is using a protocol with one of the default registered
2468 * socket stream wrappers */
2469 if (socket->type == PF_INET
2470 #if HAVE_IPV6
2471 || socket->type == PF_INET6
2472 #endif
2473 ) {
2474 int protoid;
2475 socklen_t protoidlen = sizeof(protoid);
2476
2477 getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &protoid, &protoidlen);
2478
2479 if (protoid == SOCK_STREAM) {
2480 /* SO_PROTOCOL is not (yet?) supported on OS X, so lets assume it's TCP there */
2481 #ifdef SO_PROTOCOL
2482 protoidlen = sizeof(protoid);
2483 getsockopt(socket->bsd_socket, SOL_SOCKET, SO_PROTOCOL, (char *) &protoid, &protoidlen);
2484 if (protoid == IPPROTO_TCP)
2485 #endif
2486 {
2487 protocol = "tcp";
2488 protocollen = 3;
2489 }
2490 } else if (protoid == SOCK_DGRAM) {
2491 protocol = "udp";
2492 protocollen = 3;
2493 }
2494 #ifdef PF_UNIX
2495 } else if (socket->type == PF_UNIX) {
2496 int type;
2497 socklen_t typelen = sizeof(type);
2498
2499 getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &type, &typelen);
2500
2501 if (type == SOCK_STREAM) {
2502 protocol = "unix";
2503 protocollen = 4;
2504 } else if (type == SOCK_DGRAM) {
2505 protocol = "udg";
2506 protocollen = 3;
2507 }
2508 #endif
2509 }
2510
2511 /* Try to get a stream with the registered sockops for the protocol in use
2512 * We don't want streams to actually *do* anything though, so don't give it
2513 * anything apart from the protocol */
2514 if (protocol != NULL) {
2515 stream = php_stream_xport_create(protocol, protocollen, 0, 0, NULL, NULL, NULL, NULL, NULL);
2516 }
2517
2518 /* Fall back to creating a generic socket stream */
2519 if (stream == NULL) {
2520 stream = php_stream_sock_open_from_socket(socket->bsd_socket, 0);
2521
2522 if (stream == NULL) {
2523 php_error_docref(NULL, E_WARNING, "failed to create stream");
2524 RETURN_FALSE;
2525 }
2526 }
2527
2528 stream_data = (php_netstream_data_t *) stream->abstract;
2529 stream_data->socket = socket->bsd_socket;
2530 stream_data->is_blocked = socket->blocking;
2531 stream_data->timeout.tv_sec = FG(default_socket_timeout);
2532 stream_data->timeout.tv_usec = 0;
2533
2534 php_stream_to_zval(stream, &socket->zstream);
2535
2536 RETURN_ZVAL(&socket->zstream, 1, 0);
2537 }
2538 /* }}} */
2539
2540 /* {{{ proto resource addrinfo socket_addrinfo_lookup(string hostname[, mixed service, array hints])
2541 Gets array with contents of getaddrinfo about the given hostname. */
2542 PHP_FUNCTION(socket_addrinfo_lookup)
2543 {
2544 char *service = NULL;
2545 size_t service_len;
2546 zend_string *hostname, *key;
2547 zval *hint, *zhints = NULL;
2548
2549 struct addrinfo hints, *result, *rp, *res;
2550
2551 memset(&hints, 0, sizeof(hints));
2552
2553 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|sa", &hostname, &service, &service_len, &zhints) == FAILURE) {
2554 RETURN_NULL();
2555 }
2556
2557 if (zhints) {
2558 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
2559 if (key) {
2560 if (zend_string_equals_literal(key, "ai_flags")) {
2561 hints.ai_flags = zval_get_long(hint);
2562 } else if (zend_string_equals_literal(key, "ai_socktype")) {
2563 hints.ai_socktype = zval_get_long(hint);
2564 } else if (zend_string_equals_literal(key, "ai_protocol")) {
2565 hints.ai_protocol = zval_get_long(hint);
2566 } else if (zend_string_equals_literal(key, "ai_family")) {
2567 hints.ai_family = zval_get_long(hint);
2568 } else {
2569 php_error_docref(NULL, E_NOTICE, "Unknown hint %s", ZSTR_VAL(key));
2570 }
2571 }
2572 } ZEND_HASH_FOREACH_END();
2573 }
2574
2575 if (getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result) != 0) {
2576 RETURN_FALSE;
2577 }
2578
2579 array_init(return_value);
2580
2581 for (rp = result; rp != NULL; rp = rp->ai_next) {
2582 if (rp->ai_family != AF_UNSPEC) {
2583 res = emalloc(sizeof(struct addrinfo));
2584 memcpy(res, rp, sizeof(struct addrinfo));
2585
2586 res->ai_addr = emalloc(rp->ai_addrlen);
2587 memcpy(res->ai_addr, rp->ai_addr, rp->ai_addrlen);
2588
2589 if (rp->ai_canonname != NULL) {
2590 res->ai_canonname = estrdup(rp->ai_canonname);
2591 }
2592
2593 add_next_index_resource(return_value, zend_register_resource(res, le_addrinfo));
2594 }
2595 }
2596
2597 freeaddrinfo(result);
2598 }
2599 /* }}} */
2600
2601 /* {{{ proto resource socket_addrinfo_bind(resource addrinfo)
2602 Creates and binds to a socket from a given addrinfo resource */
2603 PHP_FUNCTION(socket_addrinfo_bind)
2604 {
2605 zval *arg1;
2606 int retval;
2607 struct addrinfo *ai;
2608 php_socket *php_sock;
2609
2610 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
2611 return;
2612 }
2613
2614 if ((ai = (struct addrinfo *) zend_fetch_resource(Z_RES_P(arg1), le_addrinfo_name, le_addrinfo)) == NULL) {
2615 RETURN_FALSE;
2616 }
2617
2618 php_sock = php_create_socket();
2619 php_sock->bsd_socket = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
2620 php_sock->type = ai->ai_family;
2621
2622 if (IS_INVALID_SOCKET(php_sock)) {
2623 SOCKETS_G(last_error) = errno;
2624 php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
2625 efree(php_sock);
2626 RETURN_FALSE;
2627 }
2628
2629 php_sock->error = 0;
2630 php_sock->blocking = 1;
2631
2632 switch(php_sock->type) {
2633 case AF_UNIX:
2634 {
2635 // AF_UNIX sockets via getaddrino are not implemented due to security problems
2636 close(php_sock->bsd_socket);
2637 efree(php_sock);
2638 RETURN_FALSE;
2639 }
2640
2641 case AF_INET:
2642 #if HAVE_IPV6
2643 case AF_INET6:
2644 #endif
2645 {
2646 retval = bind(php_sock->bsd_socket, ai->ai_addr, ai->ai_addrlen);
2647 break;
2648 }
2649 default:
2650 php_error_docref(NULL, E_WARNING, "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6", php_sock->type);
2651 close(php_sock->bsd_socket);
2652 efree(php_sock);
2653 RETURN_FALSE;
2654 }
2655
2656 if (retval != 0) {
2657 PHP_SOCKET_ERROR(php_sock, "unable to bind address", errno);
2658 close(php_sock->bsd_socket);
2659 efree(php_sock);
2660 RETURN_FALSE;
2661 }
2662
2663 RETURN_RES(zend_register_resource(php_sock, le_socket));
2664 }
2665 /* }}} */
2666
2667 /* {{{ proto resource socket_addrinfo_connect(resource addrinfo)
2668 Creates and connects to a socket from a given addrinfo resource */
2669 PHP_FUNCTION(socket_addrinfo_connect)
2670 {
2671 zval *arg1;
2672 int retval;
2673 struct addrinfo *ai;
2674 php_socket *php_sock;
2675
2676 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
2677 return;
2678 }
2679
2680 if ((ai = (struct addrinfo *) zend_fetch_resource(Z_RES_P(arg1), le_addrinfo_name, le_addrinfo)) == NULL) {
2681 RETURN_FALSE;
2682 }
2683
2684 php_sock = php_create_socket();
2685 php_sock->bsd_socket = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
2686 php_sock->type = ai->ai_family;
2687
2688 if (IS_INVALID_SOCKET(php_sock)) {
2689 SOCKETS_G(last_error) = errno;
2690 php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
2691 efree(php_sock);
2692 RETURN_FALSE;
2693 }
2694
2695 php_sock->error = 0;
2696 php_sock->blocking = 1;
2697
2698 switch(php_sock->type) {
2699 case AF_UNIX:
2700 {
2701 // AF_UNIX sockets via getaddrino are not implemented due to security problems
2702 close(php_sock->bsd_socket);
2703 efree(php_sock);
2704 RETURN_FALSE;
2705 }
2706
2707 case AF_INET:
2708 #if HAVE_IPV6
2709 case AF_INET6:
2710 #endif
2711 {
2712 retval = connect(php_sock->bsd_socket, ai->ai_addr, ai->ai_addrlen);
2713 break;
2714 }
2715 default:
2716 php_error_docref(NULL, E_WARNING, "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6", php_sock->type);
2717 close(php_sock->bsd_socket);
2718 efree(php_sock);
2719 RETURN_FALSE;
2720 }
2721
2722 if (retval != 0) {
2723 PHP_SOCKET_ERROR(php_sock, "unable to connect address", errno);
2724 close(php_sock->bsd_socket);
2725 efree(php_sock);
2726 RETURN_FALSE;
2727 }
2728
2729 RETURN_RES(zend_register_resource(php_sock, le_socket));
2730 }
2731 /* }}} */
2732
2733 /* {{{ proto resource socket_addrinfo_explain(resource addrinfo)
2734 Creates and connects to a socket from a given addrinfo resource */
2735 PHP_FUNCTION(socket_addrinfo_explain)
2736 {
2737 zval *arg1, sockaddr;
2738 struct addrinfo *ai;
2739
2740 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
2741 return;
2742 }
2743
2744 if ((ai = (struct addrinfo *) zend_fetch_resource(Z_RES_P(arg1), le_addrinfo_name, le_addrinfo)) == NULL) {
2745 RETURN_FALSE;
2746 }
2747
2748 array_init(return_value);
2749
2750 add_assoc_long(return_value, "ai_flags", ai->ai_flags);
2751 add_assoc_long(return_value, "ai_family", ai->ai_family);
2752 add_assoc_long(return_value, "ai_socktype", ai->ai_socktype);
2753 add_assoc_long(return_value, "ai_protocol", ai->ai_protocol);
2754 if (ai->ai_canonname != NULL) {
2755 add_assoc_string(return_value, "ai_canonname", ai->ai_canonname);
2756 }
2757
2758 array_init(&sockaddr);
2759 switch(ai->ai_family) {
2760 case AF_INET:
2761 {
2762 struct sockaddr_in *sa = (struct sockaddr_in *) ai->ai_addr;
2763 char addr[INET_ADDRSTRLEN];
2764
2765 add_assoc_long(&sockaddr, "sin_port", ntohs((unsigned short) sa->sin_port));
2766 inet_ntop(ai->ai_family, &sa->sin_addr, addr, sizeof(addr));
2767 add_assoc_string(&sockaddr, "sin_addr", addr);
2768 break;
2769 }
2770 #if HAVE_IPV6
2771 case AF_INET6:
2772 {
2773 struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->ai_addr;
2774 char addr[INET6_ADDRSTRLEN];
2775
2776 add_assoc_long(&sockaddr, "sin6_port", ntohs((unsigned short) sa->sin6_port));
2777 inet_ntop(ai->ai_family, &sa->sin6_addr, addr, sizeof(addr));
2778 add_assoc_string(&sockaddr, "sin6_addr", addr);
2779 break;
2780 }
2781 #endif
2782 }
2783
2784 add_assoc_zval(return_value, "ai_addr", &sockaddr);
2785 }
2786 /* }}} */
2787
2788 /*
2789 * Local variables:
2790 * tab-width: 4
2791 * c-basic-offset: 4
2792 * End:
2793 * vim600: fdm=marker
2794 * vim: noet sw=4 ts=4
2795 */
2796