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