Annotation of embedaddon/php/ext/sockets/sockets.c, revision 1.1.1.4
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:
1.1.1.4 ! misho 355: /* for downwards compatibility */
1.1 misho 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);
1.1.1.4 ! misho 846: #ifdef SO_BINDTODEVICE
! 847: REGISTER_LONG_CONSTANT("SO_BINDTODEVICE", SO_BINDTODEVICE, CONST_CS | CONST_PERSISTENT);
! 848: #endif
1.1 misho 849: REGISTER_LONG_CONSTANT("SOL_SOCKET", SOL_SOCKET, CONST_CS | CONST_PERSISTENT);
850: REGISTER_LONG_CONSTANT("SOMAXCONN", SOMAXCONN, CONST_CS | CONST_PERSISTENT);
851: #ifdef TCP_NODELAY
852: REGISTER_LONG_CONSTANT("TCP_NODELAY", TCP_NODELAY, CONST_CS | CONST_PERSISTENT);
853: #endif
854: REGISTER_LONG_CONSTANT("PHP_NORMAL_READ", PHP_NORMAL_READ, CONST_CS | CONST_PERSISTENT);
855: REGISTER_LONG_CONSTANT("PHP_BINARY_READ", PHP_BINARY_READ, CONST_CS | CONST_PERSISTENT);
856:
1.1.1.2 misho 857: #ifndef RFC3678_API
858: #define MCAST_JOIN_GROUP IP_ADD_MEMBERSHIP
859: #define MCAST_LEAVE_GROUP IP_DROP_MEMBERSHIP
860: #ifdef HAS_MCAST_EXT
861: #define MCAST_BLOCK_SOURCE IP_BLOCK_SOURCE
862: #define MCAST_UNBLOCK_SOURCE IP_UNBLOCK_SOURCE
863: #define MCAST_JOIN_SOURCE_GROUP IP_ADD_SOURCE_MEMBERSHIP
864: #define MCAST_LEAVE_SOURCE_GROUP IP_DROP_SOURCE_MEMBERSHIP
865: #endif
866: #endif
867:
868: REGISTER_LONG_CONSTANT("MCAST_JOIN_GROUP", MCAST_JOIN_GROUP, CONST_CS | CONST_PERSISTENT);
869: REGISTER_LONG_CONSTANT("MCAST_LEAVE_GROUP", MCAST_LEAVE_GROUP, CONST_CS | CONST_PERSISTENT);
870: #ifdef HAS_MCAST_EXT
871: REGISTER_LONG_CONSTANT("MCAST_BLOCK_SOURCE", MCAST_BLOCK_SOURCE, CONST_CS | CONST_PERSISTENT);
872: REGISTER_LONG_CONSTANT("MCAST_UNBLOCK_SOURCE", MCAST_UNBLOCK_SOURCE, CONST_CS | CONST_PERSISTENT);
873: REGISTER_LONG_CONSTANT("MCAST_JOIN_SOURCE_GROUP", MCAST_JOIN_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT);
874: REGISTER_LONG_CONSTANT("MCAST_LEAVE_SOURCE_GROUP", MCAST_LEAVE_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT);
875: #endif
876:
877: REGISTER_LONG_CONSTANT("IP_MULTICAST_IF", IP_MULTICAST_IF, CONST_CS | CONST_PERSISTENT);
878: REGISTER_LONG_CONSTANT("IP_MULTICAST_TTL", IP_MULTICAST_TTL, CONST_CS | CONST_PERSISTENT);
879: REGISTER_LONG_CONSTANT("IP_MULTICAST_LOOP", IP_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT);
880: #if HAVE_IPV6
881: REGISTER_LONG_CONSTANT("IPV6_MULTICAST_IF", IPV6_MULTICAST_IF, CONST_CS | CONST_PERSISTENT);
882: REGISTER_LONG_CONSTANT("IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS, CONST_CS | CONST_PERSISTENT);
883: REGISTER_LONG_CONSTANT("IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT);
884: #endif
885:
1.1 misho 886: #ifndef WIN32
887: # include "unix_socket_constants.h"
888: #else
889: # include "win32_socket_constants.h"
890: #endif
891:
1.1.1.2 misho 892: REGISTER_LONG_CONSTANT("IPPROTO_IP", IPPROTO_IP, CONST_CS | CONST_PERSISTENT);
893: #if HAVE_IPV6
894: REGISTER_LONG_CONSTANT("IPPROTO_IPV6", IPPROTO_IPV6, CONST_CS | CONST_PERSISTENT);
895: #endif
1.1 misho 896:
1.1.1.2 misho 897: REGISTER_LONG_CONSTANT("SOL_TCP", IPPROTO_TCP, CONST_CS | CONST_PERSISTENT);
898: REGISTER_LONG_CONSTANT("SOL_UDP", IPPROTO_UDP, CONST_CS | CONST_PERSISTENT);
1.1 misho 899:
900: return SUCCESS;
901: }
902: /* }}} */
903:
904: /* {{{ PHP_MINFO_FUNCTION
905: */
906: PHP_MINFO_FUNCTION(sockets)
907: {
908: php_info_print_table_start();
909: php_info_print_table_row(2, "Sockets Support", "enabled");
910: php_info_print_table_end();
911: }
912: /* }}} */
913:
914: /* {{{ PHP_RSHUTDOWN_FUNCTION */
915: PHP_RSHUTDOWN_FUNCTION(sockets)
916: {
917: if (SOCKETS_G(strerror_buf)) {
918: efree(SOCKETS_G(strerror_buf));
919: SOCKETS_G(strerror_buf) = NULL;
920: }
921:
922: return SUCCESS;
923: }
924: /* }}} */
925:
926: static int php_sock_array_to_fd_set(zval *sock_array, fd_set *fds, PHP_SOCKET *max_fd TSRMLS_DC) /* {{{ */
927: {
928: zval **element;
929: php_socket *php_sock;
930: int num = 0;
931:
932: if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
933:
934: for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(sock_array));
935: zend_hash_get_current_data(Z_ARRVAL_P(sock_array), (void **) &element) == SUCCESS;
936: zend_hash_move_forward(Z_ARRVAL_P(sock_array))) {
937:
938: php_sock = (php_socket*) zend_fetch_resource(element TSRMLS_CC, -1, le_socket_name, NULL, 1, le_socket);
939: if (!php_sock) continue; /* If element is not a resource, skip it */
940:
941: PHP_SAFE_FD_SET(php_sock->bsd_socket, fds);
942: if (php_sock->bsd_socket > *max_fd) {
943: *max_fd = php_sock->bsd_socket;
944: }
945: num++;
946: }
947:
948: return num ? 1 : 0;
949: }
950: /* }}} */
951:
952: static int php_sock_array_from_fd_set(zval *sock_array, fd_set *fds TSRMLS_DC) /* {{{ */
953: {
954: zval **element;
955: zval **dest_element;
956: php_socket *php_sock;
957: HashTable *new_hash;
958: char *key;
959: int num = 0;
960: ulong num_key;
961: uint key_len;
962:
963: if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
964:
965: ALLOC_HASHTABLE(new_hash);
966: zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(sock_array)), NULL, ZVAL_PTR_DTOR, 0);
967: for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(sock_array));
968: zend_hash_get_current_data(Z_ARRVAL_P(sock_array), (void **) &element) == SUCCESS;
969: zend_hash_move_forward(Z_ARRVAL_P(sock_array))) {
970:
971: php_sock = (php_socket*) zend_fetch_resource(element TSRMLS_CC, -1, le_socket_name, NULL, 1, le_socket);
972: if (!php_sock) continue; /* If element is not a resource, skip it */
973:
974: if (PHP_SAFE_FD_ISSET(php_sock->bsd_socket, fds)) {
975: /* Add fd to new array */
976: switch (zend_hash_get_current_key_ex(Z_ARRVAL_P(sock_array), &key, &key_len, &num_key, 0, NULL)) {
977: case HASH_KEY_IS_STRING:
978: zend_hash_add(new_hash, key, key_len, (void *)element, sizeof(zval *), (void **)&dest_element);
979: break;
980: case HASH_KEY_IS_LONG:
981: zend_hash_index_update(new_hash, num_key, (void *)element, sizeof(zval *), (void **)&dest_element);
982: break;
983: }
984: if (dest_element) zval_add_ref(dest_element);
985: }
986: num++;
987: }
988:
989: /* Destroy old array, add new one */
990: zend_hash_destroy(Z_ARRVAL_P(sock_array));
991: efree(Z_ARRVAL_P(sock_array));
992:
993: zend_hash_internal_pointer_reset(new_hash);
994: Z_ARRVAL_P(sock_array) = new_hash;
995:
996: return num ? 1 : 0;
997: }
998: /* }}} */
999:
1000: /* {{{ proto int socket_select(array &read_fds, array &write_fds, array &except_fds, int tv_sec[, int tv_usec]) U
1001: Runs the select() system call on the sets mentioned with a timeout specified by tv_sec and tv_usec */
1002: PHP_FUNCTION(socket_select)
1003: {
1004: zval *r_array, *w_array, *e_array, *sec;
1005: struct timeval tv;
1006: struct timeval *tv_p = NULL;
1007: fd_set rfds, wfds, efds;
1008: PHP_SOCKET max_fd = 0;
1009: int retval, sets = 0;
1010: long usec = 0;
1011:
1012: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!a!z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE) {
1013: return;
1014: }
1015:
1016: FD_ZERO(&rfds);
1017: FD_ZERO(&wfds);
1018: FD_ZERO(&efds);
1019:
1020: if (r_array != NULL) sets += php_sock_array_to_fd_set(r_array, &rfds, &max_fd TSRMLS_CC);
1021: if (w_array != NULL) sets += php_sock_array_to_fd_set(w_array, &wfds, &max_fd TSRMLS_CC);
1022: if (e_array != NULL) sets += php_sock_array_to_fd_set(e_array, &efds, &max_fd TSRMLS_CC);
1023:
1024: if (!sets) {
1025: php_error_docref(NULL TSRMLS_CC, E_WARNING, "no resource arrays were passed to select");
1026: RETURN_FALSE;
1027: }
1028:
1029: PHP_SAFE_MAX_FD(max_fd, 0); /* someone needs to make this look more like stream_socket_select */
1030:
1031: /* If seconds is not set to null, build the timeval, else we wait indefinitely */
1032: if (sec != NULL) {
1033: zval tmp;
1034:
1035: if (Z_TYPE_P(sec) != IS_LONG) {
1036: tmp = *sec;
1037: zval_copy_ctor(&tmp);
1038: convert_to_long(&tmp);
1039: sec = &tmp;
1040: }
1041:
1042: /* Solaris + BSD do not like microsecond values which are >= 1 sec */
1043: if (usec > 999999) {
1044: tv.tv_sec = Z_LVAL_P(sec) + (usec / 1000000);
1045: tv.tv_usec = usec % 1000000;
1046: } else {
1047: tv.tv_sec = Z_LVAL_P(sec);
1048: tv.tv_usec = usec;
1049: }
1050:
1051: tv_p = &tv;
1052:
1053: if (sec == &tmp) {
1054: zval_dtor(&tmp);
1055: }
1056: }
1057:
1058: retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
1059:
1060: if (retval == -1) {
1061: SOCKETS_G(last_error) = errno;
1062: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to select [%d]: %s", errno, php_strerror(errno TSRMLS_CC));
1063: RETURN_FALSE;
1064: }
1065:
1066: if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds TSRMLS_CC);
1067: if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds TSRMLS_CC);
1068: if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds TSRMLS_CC);
1069:
1070: RETURN_LONG(retval);
1071: }
1072: /* }}} */
1073:
1074: /* {{{ proto resource socket_create_listen(int port[, int backlog]) U
1075: Opens a socket on port to accept connections */
1076: PHP_FUNCTION(socket_create_listen)
1077: {
1078: php_socket *php_sock;
1079: long port, backlog = 128;
1080:
1081: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &port, &backlog) == FAILURE) {
1082: return;
1083: }
1084:
1085: if (!php_open_listen_sock(&php_sock, port, backlog TSRMLS_CC)) {
1086: RETURN_FALSE;
1087: }
1088:
1089: php_sock->error = 0;
1090: php_sock->blocking = 1;
1091:
1092: ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
1093: }
1094: /* }}} */
1095:
1096: /* {{{ proto resource socket_accept(resource socket) U
1097: Accepts a connection on the listening socket fd */
1098: PHP_FUNCTION(socket_accept)
1099: {
1100: zval *arg1;
1101: php_socket *php_sock, *new_sock;
1102: php_sockaddr_storage sa;
1103: socklen_t php_sa_len = sizeof(sa);
1104:
1105: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
1106: return;
1107: }
1108:
1109: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1110:
1111: if (!php_accept_connect(php_sock, &new_sock, (struct sockaddr*)&sa, &php_sa_len TSRMLS_CC)) {
1112: RETURN_FALSE;
1113: }
1114:
1115: ZEND_REGISTER_RESOURCE(return_value, new_sock, le_socket);
1116: }
1117: /* }}} */
1118:
1119: /* {{{ proto bool socket_set_nonblock(resource socket) U
1120: Sets nonblocking mode on a socket resource */
1121: PHP_FUNCTION(socket_set_nonblock)
1122: {
1123: zval *arg1;
1124: php_socket *php_sock;
1125:
1126: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
1127: return;
1128: }
1129:
1130: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1.1.1.2 misho 1131:
1132: if (php_sock->zstream != NULL) {
1133: php_stream *stream;
1134: /* omit notice if resource doesn't exist anymore */
1135: stream = zend_fetch_resource(&php_sock->zstream TSRMLS_CC, -1,
1136: NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
1137: if (stream != NULL) {
1138: if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 0,
1139: NULL) != -1) {
1.1.1.3 misho 1140: php_sock->blocking = 0;
1.1.1.2 misho 1141: RETURN_TRUE;
1142: }
1143: }
1144: }
1.1 misho 1145:
1146: if (php_set_sock_blocking(php_sock->bsd_socket, 0 TSRMLS_CC) == SUCCESS) {
1147: php_sock->blocking = 0;
1148: RETURN_TRUE;
1149: } else {
1150: PHP_SOCKET_ERROR(php_sock, "unable to set nonblocking mode", errno);
1151: RETURN_FALSE;
1152: }
1153: }
1154: /* }}} */
1155:
1156: /* {{{ proto bool socket_set_block(resource socket) U
1157: Sets blocking mode on a socket resource */
1158: PHP_FUNCTION(socket_set_block)
1159: {
1160: zval *arg1;
1161: php_socket *php_sock;
1162:
1163: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
1164: return;
1165: }
1166:
1167: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1.1.1.2 misho 1168:
1169: /* if socket was created from a stream, give the stream a chance to take
1170: * care of the operation itself, thereby allowing it to update its internal
1171: * state */
1172: if (php_sock->zstream != NULL) {
1173: php_stream *stream;
1174: stream = zend_fetch_resource(&php_sock->zstream TSRMLS_CC, -1,
1175: NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
1176: if (stream != NULL) {
1177: if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 1,
1178: NULL) != -1) {
1179: php_sock->blocking = 1;
1180: RETURN_TRUE;
1181: }
1182: }
1183: }
1.1 misho 1184:
1185: if (php_set_sock_blocking(php_sock->bsd_socket, 1 TSRMLS_CC) == SUCCESS) {
1186: php_sock->blocking = 1;
1187: RETURN_TRUE;
1188: } else {
1189: PHP_SOCKET_ERROR(php_sock, "unable to set blocking mode", errno);
1190: RETURN_FALSE;
1191: }
1192: }
1193: /* }}} */
1194:
1195: /* {{{ proto bool socket_listen(resource socket[, int backlog]) U
1196: Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
1197: PHP_FUNCTION(socket_listen)
1198: {
1199: zval *arg1;
1200: php_socket *php_sock;
1201: long backlog = 0;
1202:
1203: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &backlog) == FAILURE) {
1204: return;
1205: }
1206:
1207: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1208:
1209: if (listen(php_sock->bsd_socket, backlog) != 0) {
1210: PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
1211: RETURN_FALSE;
1212: }
1213: RETURN_TRUE;
1214: }
1215: /* }}} */
1216:
1217: /* {{{ proto void socket_close(resource socket) U
1218: Closes a file descriptor */
1219: PHP_FUNCTION(socket_close)
1220: {
1221: zval *arg1;
1222: php_socket *php_sock;
1223:
1224: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
1225: return;
1226: }
1227:
1228: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1.1.1.2 misho 1229: if (php_sock->zstream != NULL) {
1230: php_stream *stream = NULL;
1231: php_stream_from_zval_no_verify(stream, &php_sock->zstream);
1232: if (stream != NULL) {
1233: /* close & destroy stream, incl. removing it from the rsrc list;
1234: * resource stored in php_sock->zstream will become invalid */
1235: php_stream_free(stream, PHP_STREAM_FREE_CLOSE |
1236: (stream->is_persistent?PHP_STREAM_FREE_CLOSE_PERSISTENT:0));
1237: }
1238: }
1.1 misho 1239: zend_list_delete(Z_RESVAL_P(arg1));
1240: }
1241: /* }}} */
1242:
1243: /* {{{ proto int socket_write(resource socket, string buf[, int length])
1244: Writes the buffer to the socket resource, length is optional */
1245: PHP_FUNCTION(socket_write)
1246: {
1247: zval *arg1;
1248: php_socket *php_sock;
1249: int retval, str_len;
1250: long length = 0;
1251: char *str;
1252:
1253: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &str, &str_len, &length) == FAILURE) {
1254: return;
1255: }
1256:
1257: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1258:
1259: if (ZEND_NUM_ARGS() < 3) {
1260: length = str_len;
1261: }
1262:
1263: #ifndef PHP_WIN32
1264: retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
1265: #else
1266: retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
1267: #endif
1268:
1269: if (retval < 0) {
1270: PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
1271: RETURN_FALSE;
1272: }
1273:
1274: RETURN_LONG(retval);
1275: }
1276: /* }}} */
1277:
1278: /* {{{ proto string socket_read(resource socket, int length [, int type]) U
1279: Reads a maximum of length bytes from socket */
1280: PHP_FUNCTION(socket_read)
1281: {
1282: zval *arg1;
1283: php_socket *php_sock;
1284: char *tmpbuf;
1285: int retval;
1286: long length, type = PHP_BINARY_READ;
1287:
1288: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &arg1, &length, &type) == FAILURE) {
1289: return;
1290: }
1291:
1292: /* overflow check */
1293: if ((length + 1) < 2) {
1294: RETURN_FALSE;
1295: }
1296:
1297: tmpbuf = emalloc(length + 1);
1298:
1299: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1300:
1301: if (type == PHP_NORMAL_READ) {
1302: retval = php_read(php_sock, tmpbuf, length, 0);
1303: } else {
1304: retval = recv(php_sock->bsd_socket, tmpbuf, length, 0);
1305: }
1306:
1307: if (retval == -1) {
1308: /* if the socket is in non-blocking mode and there's no data to read,
1309: don't output any error, as this is a normal situation, and not an error */
1310: if (errno == EAGAIN
1311: #ifdef EWOULDBLOCK
1312: || errno == EWOULDBLOCK
1313: #endif
1314: ) {
1315: php_sock->error = errno;
1316: SOCKETS_G(last_error) = errno;
1317: } else {
1318: PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
1319: }
1320:
1321: efree(tmpbuf);
1322: RETURN_FALSE;
1323: } else if (!retval) {
1324: efree(tmpbuf);
1325: RETURN_EMPTY_STRING();
1326: }
1327:
1328: tmpbuf = erealloc(tmpbuf, retval + 1);
1329: tmpbuf[retval] = '\0' ;
1330:
1331: RETURN_STRINGL(tmpbuf, retval, 0);
1332: }
1333: /* }}} */
1334:
1335: /* {{{ proto bool socket_getsockname(resource socket, string &addr[, int &port])
1336: 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. */
1337: PHP_FUNCTION(socket_getsockname)
1338: {
1339: zval *arg1, *addr, *port = NULL;
1340: php_sockaddr_storage sa_storage;
1341: php_socket *php_sock;
1342: struct sockaddr *sa;
1343: struct sockaddr_in *sin;
1344: #if HAVE_IPV6
1345: struct sockaddr_in6 *sin6;
1346: char addr6[INET6_ADDRSTRLEN+1];
1347: #endif
1348: struct sockaddr_un *s_un;
1349: char *addr_string;
1350: socklen_t salen = sizeof(php_sockaddr_storage);
1351:
1352: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &arg1, &addr, &port) == FAILURE) {
1353: return;
1354: }
1355:
1356: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1357:
1358: sa = (struct sockaddr *) &sa_storage;
1359:
1360: if (getsockname(php_sock->bsd_socket, sa, &salen) != 0) {
1361: PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket name", errno);
1362: RETURN_FALSE;
1363: }
1364:
1365: switch (sa->sa_family) {
1366: #if HAVE_IPV6
1367: case AF_INET6:
1368: sin6 = (struct sockaddr_in6 *) sa;
1369: inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
1370: zval_dtor(addr);
1371: ZVAL_STRING(addr, addr6, 1);
1372:
1373: if (port != NULL) {
1374: zval_dtor(port);
1375: ZVAL_LONG(port, htons(sin6->sin6_port));
1376: }
1377: RETURN_TRUE;
1378: break;
1379: #endif
1380: case AF_INET:
1381: sin = (struct sockaddr_in *) sa;
1382: while (inet_ntoa_lock == 1);
1383: inet_ntoa_lock = 1;
1384: addr_string = inet_ntoa(sin->sin_addr);
1385: inet_ntoa_lock = 0;
1386:
1387: zval_dtor(addr);
1388: ZVAL_STRING(addr, addr_string, 1);
1389:
1390: if (port != NULL) {
1391: zval_dtor(port);
1392: ZVAL_LONG(port, htons(sin->sin_port));
1393: }
1394: RETURN_TRUE;
1395: break;
1396:
1397: case AF_UNIX:
1398: s_un = (struct sockaddr_un *) sa;
1399:
1400: zval_dtor(addr);
1401: ZVAL_STRING(addr, s_un->sun_path, 1);
1402: RETURN_TRUE;
1403: break;
1404:
1405: default:
1406: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
1407: RETURN_FALSE;
1408: }
1409: }
1410: /* }}} */
1411:
1412: /* {{{ proto bool socket_getpeername(resource socket, string &addr[, int &port])
1413: 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. */
1414: PHP_FUNCTION(socket_getpeername)
1415: {
1416: zval *arg1, *arg2, *arg3 = NULL;
1417: php_sockaddr_storage sa_storage;
1418: php_socket *php_sock;
1419: struct sockaddr *sa;
1420: struct sockaddr_in *sin;
1421: #if HAVE_IPV6
1422: struct sockaddr_in6 *sin6;
1423: char addr6[INET6_ADDRSTRLEN+1];
1424: #endif
1425: struct sockaddr_un *s_un;
1426: char *addr_string;
1427: socklen_t salen = sizeof(php_sockaddr_storage);
1428:
1429: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &arg1, &arg2, &arg3) == FAILURE) {
1430: return;
1431: }
1432:
1433: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1434:
1435: sa = (struct sockaddr *) &sa_storage;
1436:
1437: if (getpeername(php_sock->bsd_socket, sa, &salen) < 0) {
1438: PHP_SOCKET_ERROR(php_sock, "unable to retrieve peer name", errno);
1439: RETURN_FALSE;
1440: }
1441:
1442: switch (sa->sa_family) {
1443: #if HAVE_IPV6
1444: case AF_INET6:
1445: sin6 = (struct sockaddr_in6 *) sa;
1446: inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
1447: zval_dtor(arg2);
1448: ZVAL_STRING(arg2, addr6, 1);
1449:
1450: if (arg3 != NULL) {
1451: zval_dtor(arg3);
1452: ZVAL_LONG(arg3, htons(sin6->sin6_port));
1453: }
1454:
1455: RETURN_TRUE;
1456: break;
1457: #endif
1458: case AF_INET:
1459: sin = (struct sockaddr_in *) sa;
1460: while (inet_ntoa_lock == 1);
1461: inet_ntoa_lock = 1;
1462: addr_string = inet_ntoa(sin->sin_addr);
1463: inet_ntoa_lock = 0;
1464:
1465: zval_dtor(arg2);
1466: ZVAL_STRING(arg2, addr_string, 1);
1467:
1468: if (arg3 != NULL) {
1469: zval_dtor(arg3);
1470: ZVAL_LONG(arg3, htons(sin->sin_port));
1471: }
1472:
1473: RETURN_TRUE;
1474: break;
1475:
1476: case AF_UNIX:
1477: s_un = (struct sockaddr_un *) sa;
1478:
1479: zval_dtor(arg2);
1480: ZVAL_STRING(arg2, s_un->sun_path, 1);
1481: RETURN_TRUE;
1482: break;
1483:
1484: default:
1485: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
1486: RETURN_FALSE;
1487: }
1488: }
1489: /* }}} */
1490:
1491: /* {{{ proto resource socket_create(int domain, int type, int protocol) U
1492: Creates an endpoint for communication in the domain specified by domain, of type specified by type */
1493: PHP_FUNCTION(socket_create)
1494: {
1495: long arg1, arg2, arg3;
1.1.1.2 misho 1496: php_socket *php_sock = php_create_socket();
1.1 misho 1497:
1498: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &arg1, &arg2, &arg3) == FAILURE) {
1499: efree(php_sock);
1500: return;
1501: }
1502:
1503: if (arg1 != AF_UNIX
1504: #if HAVE_IPV6
1505: && arg1 != AF_INET6
1506: #endif
1507: && arg1 != AF_INET) {
1508: php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%ld] specified for argument 1, assuming AF_INET", arg1);
1509: arg1 = AF_INET;
1510: }
1511:
1512: if (arg2 > 10) {
1513: php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%ld] specified for argument 2, assuming SOCK_STREAM", arg2);
1514: arg2 = SOCK_STREAM;
1515: }
1516:
1517: php_sock->bsd_socket = socket(arg1, arg2, arg3);
1518: php_sock->type = arg1;
1519:
1520: if (IS_INVALID_SOCKET(php_sock)) {
1521: SOCKETS_G(last_error) = errno;
1522: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create socket [%d]: %s", errno, php_strerror(errno TSRMLS_CC));
1523: efree(php_sock);
1524: RETURN_FALSE;
1525: }
1526:
1527: php_sock->error = 0;
1528: php_sock->blocking = 1;
1529:
1530: ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
1531: }
1532: /* }}} */
1533:
1534: /* {{{ proto bool socket_connect(resource socket, string addr [, int port])
1535: Opens a connection to addr:port on the socket specified by socket */
1536: PHP_FUNCTION(socket_connect)
1537: {
1538: zval *arg1;
1539: php_socket *php_sock;
1540: char *addr;
1541: int retval, addr_len;
1542: long port = 0;
1543: int argc = ZEND_NUM_ARGS();
1544:
1545: if (zend_parse_parameters(argc TSRMLS_CC, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
1546: return;
1547: }
1548:
1549: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1550:
1551: switch(php_sock->type) {
1552: #if HAVE_IPV6
1.1.1.2 misho 1553: case AF_INET6: {
1554: struct sockaddr_in6 sin6 = {0};
1555:
1.1 misho 1556: if (argc != 3) {
1557: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET6 requires 3 arguments");
1558: RETURN_FALSE;
1559: }
1560:
1561: memset(&sin6, 0, sizeof(struct sockaddr_in6));
1562:
1563: sin6.sin6_family = AF_INET6;
1564: sin6.sin6_port = htons((unsigned short int)port);
1565:
1566: if (! php_set_inet6_addr(&sin6, addr, php_sock TSRMLS_CC)) {
1567: RETURN_FALSE;
1568: }
1569:
1570: retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6));
1571: break;
1.1.1.2 misho 1572: }
1.1 misho 1573: #endif
1.1.1.2 misho 1574: case AF_INET: {
1575: struct sockaddr_in sin = {0};
1576:
1.1 misho 1577: if (argc != 3) {
1578: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET requires 3 arguments");
1579: RETURN_FALSE;
1580: }
1581:
1582: sin.sin_family = AF_INET;
1583: sin.sin_port = htons((unsigned short int)port);
1584:
1585: if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
1586: RETURN_FALSE;
1587: }
1588:
1589: retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
1590: break;
1.1.1.2 misho 1591: }
1.1 misho 1592:
1.1.1.2 misho 1593: case AF_UNIX: {
1594: struct sockaddr_un s_un = {0};
1595:
1.1 misho 1596: if (addr_len >= sizeof(s_un.sun_path)) {
1597: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Path too long");
1598: RETURN_FALSE;
1599: }
1600:
1601: s_un.sun_family = AF_UNIX;
1602: memcpy(&s_un.sun_path, addr, addr_len);
1.1.1.2 misho 1603: retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un,
1604: (socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len));
1.1 misho 1605: break;
1.1.1.2 misho 1606: }
1.1 misho 1607:
1608: default:
1609: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
1610: RETURN_FALSE;
1611: }
1612:
1613: if (retval != 0) {
1614: PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
1615: RETURN_FALSE;
1616: }
1617:
1618: RETURN_TRUE;
1619: }
1620: /* }}} */
1621:
1622: /* {{{ proto string socket_strerror(int errno)
1623: Returns a string describing an error */
1624: PHP_FUNCTION(socket_strerror)
1625: {
1626: long arg1;
1627:
1628: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &arg1) == FAILURE) {
1629: return;
1630: }
1631:
1632: RETURN_STRING(php_strerror(arg1 TSRMLS_CC), 1);
1633: }
1634: /* }}} */
1635:
1636: /* {{{ proto bool socket_bind(resource socket, string addr [, int port])
1637: Binds an open socket to a listening port, port is only specified in AF_INET family. */
1638: PHP_FUNCTION(socket_bind)
1639: {
1640: zval *arg1;
1641: php_sockaddr_storage sa_storage;
1642: struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
1643: php_socket *php_sock;
1644: char *addr;
1645: int addr_len;
1646: long port = 0;
1647: long retval = 0;
1648:
1649: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
1650: return;
1651: }
1652:
1653: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1654:
1655: switch(php_sock->type) {
1656: case AF_UNIX:
1657: {
1658: struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
1659: memset(sa, 0, sizeof(sa_storage));
1660: sa->sun_family = AF_UNIX;
1661: snprintf(sa->sun_path, 108, "%s", addr);
1662: retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa, SUN_LEN(sa));
1663: break;
1664: }
1665:
1666: case AF_INET:
1667: {
1668: struct sockaddr_in *sa = (struct sockaddr_in *) sock_type;
1669:
1670: memset(sa, 0, sizeof(sa_storage)); /* Apparently, Mac OSX needs this */
1671:
1672: sa->sin_family = AF_INET;
1673: sa->sin_port = htons((unsigned short) port);
1674:
1675: if (! php_set_inet_addr(sa, addr, php_sock TSRMLS_CC)) {
1676: RETURN_FALSE;
1677: }
1678:
1679: retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
1680: break;
1681: }
1682: #if HAVE_IPV6
1683: case AF_INET6:
1684: {
1685: struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
1686:
1687: memset(sa, 0, sizeof(sa_storage)); /* Apparently, Mac OSX needs this */
1688:
1689: sa->sin6_family = AF_INET6;
1690: sa->sin6_port = htons((unsigned short) port);
1691:
1692: if (! php_set_inet6_addr(sa, addr, php_sock TSRMLS_CC)) {
1693: RETURN_FALSE;
1694: }
1695:
1696: retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in6));
1697: break;
1698: }
1699: #endif
1700: default:
1701: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6", php_sock->type);
1702: RETURN_FALSE;
1703: }
1704:
1705: if (retval != 0) {
1706: PHP_SOCKET_ERROR(php_sock, "unable to bind address", errno);
1707: RETURN_FALSE;
1708: }
1709:
1710: RETURN_TRUE;
1711: }
1712: /* }}} */
1713:
1714: /* {{{ proto int socket_recv(resource socket, string &buf, int len, int flags)
1715: Receives data from a connected socket */
1716: PHP_FUNCTION(socket_recv)
1717: {
1718: zval *php_sock_res, *buf;
1719: char *recv_buf;
1720: php_socket *php_sock;
1721: int retval;
1722: long len, flags;
1723:
1724: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzll", &php_sock_res, &buf, &len, &flags) == FAILURE) {
1725: return;
1726: }
1727:
1728: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &php_sock_res, -1, le_socket_name, le_socket);
1729:
1730: /* overflow check */
1731: if ((len + 1) < 2) {
1732: RETURN_FALSE;
1733: }
1734:
1735: recv_buf = emalloc(len + 1);
1736: memset(recv_buf, 0, len + 1);
1737:
1738: if ((retval = recv(php_sock->bsd_socket, recv_buf, len, flags)) < 1) {
1739: efree(recv_buf);
1740:
1741: zval_dtor(buf);
1742: Z_TYPE_P(buf) = IS_NULL;
1743: } else {
1744: recv_buf[retval] = '\0';
1745:
1746: /* Rebuild buffer zval */
1747: zval_dtor(buf);
1748:
1749: Z_STRVAL_P(buf) = recv_buf;
1750: Z_STRLEN_P(buf) = retval;
1751: Z_TYPE_P(buf) = IS_STRING;
1752: }
1753:
1754: if (retval == -1) {
1755: PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
1756: RETURN_FALSE;
1757: }
1758:
1759: RETURN_LONG(retval);
1760: }
1761: /* }}} */
1762:
1763: /* {{{ proto int socket_send(resource socket, string buf, int len, int flags)
1764: Sends data to a connected socket */
1765: PHP_FUNCTION(socket_send)
1766: {
1767: zval *arg1;
1768: php_socket *php_sock;
1769: int buf_len, retval;
1770: long len, flags;
1771: char *buf;
1772:
1773: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsll", &arg1, &buf, &buf_len, &len, &flags) == FAILURE) {
1774: return;
1775: }
1776:
1777: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1778:
1779: retval = send(php_sock->bsd_socket, buf, (buf_len < len ? buf_len : len), flags);
1780:
1781: if (retval == -1) {
1782: PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
1783: RETURN_FALSE;
1784: }
1785:
1786: RETURN_LONG(retval);
1787: }
1788: /* }}} */
1789:
1790: /* {{{ proto int socket_recvfrom(resource socket, string &buf, int len, int flags, string &name [, int &port])
1791: Receives data from a socket, connected or not */
1792: PHP_FUNCTION(socket_recvfrom)
1793: {
1794: zval *arg1, *arg2, *arg5, *arg6 = NULL;
1795: php_socket *php_sock;
1796: struct sockaddr_un s_un;
1797: struct sockaddr_in sin;
1798: #if HAVE_IPV6
1799: struct sockaddr_in6 sin6;
1800: char addr6[INET6_ADDRSTRLEN];
1801: #endif
1802: socklen_t slen;
1803: int retval;
1804: long arg3, arg4;
1805: char *recv_buf, *address;
1806:
1807: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzllz|z", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
1808: return;
1809: }
1810:
1811: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1812:
1813: /* overflow check */
1814: if ((arg3 + 2) < 3) {
1815: RETURN_FALSE;
1816: }
1817:
1818: recv_buf = emalloc(arg3 + 2);
1819: memset(recv_buf, 0, arg3 + 2);
1820:
1821: switch (php_sock->type) {
1822: case AF_UNIX:
1823: slen = sizeof(s_un);
1824: s_un.sun_family = AF_UNIX;
1825: retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
1826:
1827: if (retval < 0) {
1828: PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
1.1.1.3 misho 1829: efree(recv_buf);
1.1 misho 1830: RETURN_FALSE;
1831: }
1832:
1833: zval_dtor(arg2);
1834: zval_dtor(arg5);
1835:
1836: ZVAL_STRINGL(arg2, recv_buf, retval, 0);
1837: ZVAL_STRING(arg5, s_un.sun_path, 1);
1838: break;
1839:
1840: case AF_INET:
1841: slen = sizeof(sin);
1842: memset(&sin, 0, slen);
1843: sin.sin_family = AF_INET;
1844:
1845: if (arg6 == NULL) {
1846: efree(recv_buf);
1847: WRONG_PARAM_COUNT;
1848: }
1849:
1850: retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
1851:
1852: if (retval < 0) {
1853: PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
1.1.1.3 misho 1854: efree(recv_buf);
1.1 misho 1855: RETURN_FALSE;
1856: }
1857:
1858: zval_dtor(arg2);
1859: zval_dtor(arg5);
1860: zval_dtor(arg6);
1861:
1862: address = inet_ntoa(sin.sin_addr);
1863:
1864: ZVAL_STRINGL(arg2, recv_buf, retval, 0);
1865: ZVAL_STRING(arg5, address ? address : "0.0.0.0", 1);
1866: ZVAL_LONG(arg6, ntohs(sin.sin_port));
1867: break;
1868: #if HAVE_IPV6
1869: case AF_INET6:
1870: slen = sizeof(sin6);
1871: memset(&sin6, 0, slen);
1872: sin6.sin6_family = AF_INET6;
1873:
1874: if (arg6 == NULL) {
1875: efree(recv_buf);
1876: WRONG_PARAM_COUNT;
1877: }
1878:
1879: retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
1880:
1881: if (retval < 0) {
1882: PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
1.1.1.3 misho 1883: efree(recv_buf);
1.1 misho 1884: RETURN_FALSE;
1885: }
1886:
1887: zval_dtor(arg2);
1888: zval_dtor(arg5);
1889: zval_dtor(arg6);
1890:
1891: memset(addr6, 0, INET6_ADDRSTRLEN);
1892: inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
1893:
1894: ZVAL_STRINGL(arg2, recv_buf, retval, 0);
1895: ZVAL_STRING(arg5, addr6[0] ? addr6 : "::", 1);
1896: ZVAL_LONG(arg6, ntohs(sin6.sin6_port));
1897: break;
1898: #endif
1899: default:
1900: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
1901: RETURN_FALSE;
1902: }
1903:
1904: RETURN_LONG(retval);
1905: }
1906: /* }}} */
1907:
1908: /* {{{ proto int socket_sendto(resource socket, string buf, int len, int flags, string addr [, int port])
1909: Sends a message to a socket, whether it is connected or not */
1910: PHP_FUNCTION(socket_sendto)
1911: {
1912: zval *arg1;
1913: php_socket *php_sock;
1914: struct sockaddr_un s_un;
1915: struct sockaddr_in sin;
1916: #if HAVE_IPV6
1917: struct sockaddr_in6 sin6;
1918: #endif
1919: int retval, buf_len, addr_len;
1920: long len, flags, port = 0;
1921: char *buf, *addr;
1922: int argc = ZEND_NUM_ARGS();
1923:
1924: if (zend_parse_parameters(argc TSRMLS_CC, "rslls|l", &arg1, &buf, &buf_len, &len, &flags, &addr, &addr_len, &port) == FAILURE) {
1925: return;
1926: }
1927:
1928: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
1929:
1930: switch (php_sock->type) {
1931: case AF_UNIX:
1932: memset(&s_un, 0, sizeof(s_un));
1933: s_un.sun_family = AF_UNIX;
1934: snprintf(s_un.sun_path, 108, "%s", addr);
1935:
1936: retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
1937: break;
1938:
1939: case AF_INET:
1940: if (argc != 6) {
1941: WRONG_PARAM_COUNT;
1942: }
1943:
1944: memset(&sin, 0, sizeof(sin));
1945: sin.sin_family = AF_INET;
1946: sin.sin_port = htons((unsigned short) port);
1947:
1948: if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
1949: RETURN_FALSE;
1950: }
1951:
1952: retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin, sizeof(sin));
1953: break;
1954: #if HAVE_IPV6
1955: case AF_INET6:
1956: if (argc != 6) {
1957: WRONG_PARAM_COUNT;
1958: }
1959:
1960: memset(&sin6, 0, sizeof(sin6));
1961: sin6.sin6_family = AF_INET6;
1962: sin6.sin6_port = htons((unsigned short) port);
1963:
1964: if (! php_set_inet6_addr(&sin6, addr, php_sock TSRMLS_CC)) {
1965: RETURN_FALSE;
1966: }
1967:
1968: retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin6, sizeof(sin6));
1969: break;
1970: #endif
1971: default:
1972: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
1973: RETURN_FALSE;
1974: }
1975:
1976: if (retval == -1) {
1977: PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
1978: RETURN_FALSE;
1979: }
1980:
1981: RETURN_LONG(retval);
1982: }
1983: /* }}} */
1984:
1985: /* {{{ proto mixed socket_get_option(resource socket, int level, int optname) U
1986: Gets socket options for the socket */
1987: PHP_FUNCTION(socket_get_option)
1988: {
1989: zval *arg1;
1990: struct linger linger_val;
1991: struct timeval tv;
1992: #ifdef PHP_WIN32
1993: int timeout = 0;
1994: #endif
1995: socklen_t optlen;
1996: php_socket *php_sock;
1997: int other_val;
1998: long level, optname;
1999:
2000: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &arg1, &level, &optname) == FAILURE) {
2001: return;
2002: }
2003:
2004: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
2005:
1.1.1.2 misho 2006: if (level == IPPROTO_IP) {
2007: switch (optname) {
2008: case IP_MULTICAST_IF: {
2009: struct in_addr if_addr;
2010: unsigned int if_index;
2011: optlen = sizeof(if_addr);
2012: if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&if_addr, &optlen) != 0) {
2013: PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2014: RETURN_FALSE;
2015: }
2016: if (php_add4_to_if_index(&if_addr, php_sock, &if_index TSRMLS_CC) == SUCCESS) {
2017: RETURN_LONG((long) if_index);
2018: } else {
2019: RETURN_FALSE;
2020: }
2021: }
2022: }
2023: }
2024:
2025: /* sol_socket options and general case */
1.1 misho 2026: switch(optname) {
2027: case SO_LINGER:
2028: optlen = sizeof(linger_val);
2029:
2030: if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&linger_val, &optlen) != 0) {
2031: PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2032: RETURN_FALSE;
2033: }
2034:
2035: array_init(return_value);
2036: add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
2037: add_assoc_long(return_value, "l_linger", linger_val.l_linger);
2038: break;
2039:
2040: case SO_RCVTIMEO:
2041: case SO_SNDTIMEO:
2042: #ifndef PHP_WIN32
2043: optlen = sizeof(tv);
2044:
2045: if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
2046: PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2047: RETURN_FALSE;
2048: }
2049: #else
2050: optlen = sizeof(int);
2051:
2052: if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&timeout, &optlen) != 0) {
2053: PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2054: RETURN_FALSE;
2055: }
2056:
2057: tv.tv_sec = timeout ? timeout / 1000 : 0;
2058: tv.tv_usec = timeout ? (timeout * 1000) % 1000000 : 0;
2059: #endif
2060:
2061: array_init(return_value);
2062:
2063: add_assoc_long(return_value, "sec", tv.tv_sec);
2064: add_assoc_long(return_value, "usec", tv.tv_usec);
2065: break;
1.1.1.2 misho 2066:
1.1 misho 2067: default:
2068: optlen = sizeof(other_val);
2069:
2070: if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&other_val, &optlen) != 0) {
2071: PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
2072: RETURN_FALSE;
2073: }
1.1.1.2 misho 2074: if (optlen == 1)
2075: other_val = *((unsigned char *)&other_val);
1.1 misho 2076:
2077: RETURN_LONG(other_val);
2078: break;
2079: }
2080: }
2081: /* }}} */
2082:
1.1.1.2 misho 2083: static int php_do_mcast_opt(php_socket *php_sock, int level, int optname, zval **arg4 TSRMLS_DC)
2084: {
2085: HashTable *opt_ht;
2086: unsigned int if_index;
2087: int retval;
2088: int (*mcast_req_fun)(php_socket *, int, struct sockaddr *, socklen_t,
2089: unsigned TSRMLS_DC);
2090: #ifdef HAS_MCAST_EXT
2091: int (*mcast_sreq_fun)(php_socket *, int, struct sockaddr *, socklen_t,
2092: struct sockaddr *, socklen_t, unsigned TSRMLS_DC);
2093: #endif
2094:
2095: switch (optname) {
2096: case MCAST_JOIN_GROUP:
2097: mcast_req_fun = &php_mcast_join;
2098: goto mcast_req_fun;
2099: case MCAST_LEAVE_GROUP:
2100: {
2101: php_sockaddr_storage group = {0};
2102: socklen_t glen;
2103:
2104: mcast_req_fun = &php_mcast_leave;
2105: mcast_req_fun:
2106: convert_to_array_ex(arg4);
2107: opt_ht = HASH_OF(*arg4);
2108:
2109: if (php_get_address_from_array(opt_ht, "group", php_sock, &group,
2110: &glen TSRMLS_CC) == FAILURE) {
2111: return FAILURE;
2112: }
2113: if (php_get_if_index_from_array(opt_ht, "interface", php_sock,
2114: &if_index TSRMLS_CC) == FAILURE) {
2115: return FAILURE;
2116: }
2117:
2118: retval = mcast_req_fun(php_sock, level, (struct sockaddr*)&group,
2119: glen, if_index TSRMLS_CC);
2120: break;
2121: }
2122:
2123: #ifdef HAS_MCAST_EXT
2124: case MCAST_BLOCK_SOURCE:
2125: mcast_sreq_fun = &php_mcast_block_source;
2126: goto mcast_sreq_fun;
2127: case MCAST_UNBLOCK_SOURCE:
2128: mcast_sreq_fun = &php_mcast_unblock_source;
2129: goto mcast_sreq_fun;
2130: case MCAST_JOIN_SOURCE_GROUP:
2131: mcast_sreq_fun = &php_mcast_join_source;
2132: goto mcast_sreq_fun;
2133: case MCAST_LEAVE_SOURCE_GROUP:
2134: {
2135: php_sockaddr_storage group = {0},
2136: source = {0};
2137: socklen_t glen,
2138: slen;
2139:
2140: mcast_sreq_fun = &php_mcast_leave_source;
2141: mcast_sreq_fun:
2142: convert_to_array_ex(arg4);
2143: opt_ht = HASH_OF(*arg4);
2144:
2145: if (php_get_address_from_array(opt_ht, "group", php_sock, &group,
2146: &glen TSRMLS_CC) == FAILURE) {
2147: return FAILURE;
2148: }
2149: if (php_get_address_from_array(opt_ht, "source", php_sock, &source,
2150: &slen TSRMLS_CC) == FAILURE) {
2151: return FAILURE;
2152: }
2153: if (php_get_if_index_from_array(opt_ht, "interface", php_sock,
2154: &if_index TSRMLS_CC) == FAILURE) {
2155: return FAILURE;
2156: }
2157:
2158: retval = mcast_sreq_fun(php_sock, level, (struct sockaddr*)&group,
2159: glen, (struct sockaddr*)&source, slen, if_index TSRMLS_CC);
2160: break;
2161: }
2162: #endif
2163: default:
2164: php_error_docref(NULL TSRMLS_CC, E_WARNING,
2165: "unexpected option in php_do_mcast_opt (level %d, option %d). "
2166: "This is a bug.", level, optname);
2167: return FAILURE;
2168: }
2169:
2170: if (retval != 0) {
2171: if (retval != -2) { /* error, but message already emitted */
2172: PHP_SOCKET_ERROR(php_sock, "unable to set socket option", errno);
2173: }
2174: return FAILURE;
2175: }
2176: return SUCCESS;
2177: }
2178:
1.1 misho 2179: /* {{{ proto bool socket_set_option(resource socket, int level, int optname, int|array optval)
2180: Sets socket options for the socket */
2181: PHP_FUNCTION(socket_set_option)
2182: {
1.1.1.2 misho 2183: zval *arg1, **arg4;
2184: struct linger lv;
2185: php_socket *php_sock;
2186: int ov, optlen, retval;
1.1 misho 2187: #ifdef PHP_WIN32
1.1.1.2 misho 2188: int timeout;
1.1 misho 2189: #else
1.1.1.2 misho 2190: struct timeval tv;
1.1 misho 2191: #endif
1.1.1.2 misho 2192: long level, optname;
2193: void *opt_ptr;
2194: HashTable *opt_ht;
2195: zval **l_onoff, **l_linger;
2196: zval **sec, **usec;
2197:
2198: /* Multicast */
2199: unsigned int if_index;
2200: struct in_addr if_addr;
2201: unsigned char ipv4_mcast_ttl_lback;
2202:
1.1 misho 2203: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllZ", &arg1, &level, &optname, &arg4) == FAILURE) {
2204: return;
2205: }
2206:
2207: ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
2208:
2209: set_errno(0);
2210:
1.1.1.2 misho 2211: if (level == IPPROTO_IP) {
2212: switch (optname) {
2213: case MCAST_JOIN_GROUP:
2214: case MCAST_LEAVE_GROUP:
2215: #ifdef HAS_MCAST_EXT
2216: case MCAST_BLOCK_SOURCE:
2217: case MCAST_UNBLOCK_SOURCE:
2218: case MCAST_JOIN_SOURCE_GROUP:
2219: case MCAST_LEAVE_SOURCE_GROUP:
2220: #endif
2221: if (php_do_mcast_opt(php_sock, level, optname, arg4 TSRMLS_CC) == FAILURE) {
2222: RETURN_FALSE;
2223: } else {
2224: RETURN_TRUE;
2225: }
2226:
2227: case IP_MULTICAST_IF:
2228: if (php_get_if_index_from_zval(*arg4, &if_index TSRMLS_CC) == FAILURE) {
2229: RETURN_FALSE;
2230: }
2231:
2232: if (php_if_index_to_addr4(if_index, php_sock, &if_addr TSRMLS_CC) == FAILURE) {
2233: RETURN_FALSE;
2234: }
2235: opt_ptr = &if_addr;
2236: optlen = sizeof(if_addr);
2237: goto dosockopt;
2238:
2239: case IP_MULTICAST_LOOP:
2240: convert_to_boolean_ex(arg4);
2241: goto ipv4_loop_ttl;
2242: case IP_MULTICAST_TTL:
2243: convert_to_long_ex(arg4);
2244: if (Z_LVAL_PP(arg4) < 0L || Z_LVAL_PP(arg4) > 255L) {
2245: php_error_docref(NULL TSRMLS_CC, E_WARNING,
2246: "Expected a value between 0 and 255");
2247: RETURN_FALSE;
2248: }
2249: ipv4_loop_ttl:
2250: ipv4_mcast_ttl_lback = (unsigned char) Z_LVAL_PP(arg4);
2251: opt_ptr = &ipv4_mcast_ttl_lback;
2252: optlen = sizeof(ipv4_mcast_ttl_lback);
2253: goto dosockopt;
2254: }
2255: }
2256:
2257: #if HAVE_IPV6
2258: else if (level == IPPROTO_IPV6) {
2259: switch (optname) {
2260: case MCAST_JOIN_GROUP:
2261: case MCAST_LEAVE_GROUP:
2262: #ifdef HAS_MCAST_EXT
2263: case MCAST_BLOCK_SOURCE:
2264: case MCAST_UNBLOCK_SOURCE:
2265: case MCAST_JOIN_SOURCE_GROUP:
2266: case MCAST_LEAVE_SOURCE_GROUP:
2267: #endif
2268: if (php_do_mcast_opt(php_sock, level, optname, arg4 TSRMLS_CC) == FAILURE) {
2269: RETURN_FALSE;
2270: } else {
2271: RETURN_TRUE;
2272: }
2273:
2274: case IPV6_MULTICAST_IF:
2275: if (php_get_if_index_from_zval(*arg4, &if_index TSRMLS_CC) == FAILURE) {
2276: RETURN_FALSE;
2277: }
2278:
2279: opt_ptr = &if_index;
2280: optlen = sizeof(if_index);
2281: goto dosockopt;
2282:
2283: case IPV6_MULTICAST_LOOP:
2284: convert_to_boolean_ex(arg4);
2285: goto ipv6_loop_hops;
2286: case IPV6_MULTICAST_HOPS:
2287: convert_to_long_ex(arg4);
2288: if (Z_LVAL_PP(arg4) < -1L || Z_LVAL_PP(arg4) > 255L) {
2289: php_error_docref(NULL TSRMLS_CC, E_WARNING,
2290: "Expected a value between -1 and 255");
2291: RETURN_FALSE;
2292: }
2293: ipv6_loop_hops:
2294: ov = (int) Z_LVAL_PP(arg4);
2295: opt_ptr = &ov;
2296: optlen = sizeof(ov);
2297: goto dosockopt;
2298: }
2299: }
2300: #endif
2301:
1.1 misho 2302: switch (optname) {
1.1.1.2 misho 2303: case SO_LINGER: {
2304: const char l_onoff_key[] = "l_onoff";
2305: const char l_linger_key[] = "l_linger";
2306:
1.1 misho 2307: convert_to_array_ex(arg4);
2308: opt_ht = HASH_OF(*arg4);
2309:
1.1.1.2 misho 2310: if (zend_hash_find(opt_ht, l_onoff_key, sizeof(l_onoff_key), (void **)&l_onoff) == FAILURE) {
1.1 misho 2311: php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", l_onoff_key);
2312: RETURN_FALSE;
2313: }
1.1.1.2 misho 2314: if (zend_hash_find(opt_ht, l_linger_key, sizeof(l_linger_key), (void **)&l_linger) == FAILURE) {
1.1 misho 2315: php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", l_linger_key);
2316: RETURN_FALSE;
2317: }
2318:
2319: convert_to_long_ex(l_onoff);
2320: convert_to_long_ex(l_linger);
2321:
2322: lv.l_onoff = (unsigned short)Z_LVAL_PP(l_onoff);
2323: lv.l_linger = (unsigned short)Z_LVAL_PP(l_linger);
2324:
2325: optlen = sizeof(lv);
2326: opt_ptr = &lv;
2327: break;
1.1.1.2 misho 2328: }
1.1 misho 2329:
2330: case SO_RCVTIMEO:
1.1.1.2 misho 2331: case SO_SNDTIMEO: {
2332: const char sec_key[] = "sec";
2333: const char usec_key[] = "usec";
2334:
1.1 misho 2335: convert_to_array_ex(arg4);
2336: opt_ht = HASH_OF(*arg4);
2337:
1.1.1.2 misho 2338: if (zend_hash_find(opt_ht, sec_key, sizeof(sec_key), (void **)&sec) == FAILURE) {
1.1 misho 2339: php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", sec_key);
2340: RETURN_FALSE;
2341: }
1.1.1.2 misho 2342: if (zend_hash_find(opt_ht, usec_key, sizeof(usec_key), (void **)&usec) == FAILURE) {
1.1 misho 2343: php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", usec_key);
2344: RETURN_FALSE;
2345: }
2346:
2347: convert_to_long_ex(sec);
2348: convert_to_long_ex(usec);
2349: #ifndef PHP_WIN32
2350: tv.tv_sec = Z_LVAL_PP(sec);
2351: tv.tv_usec = Z_LVAL_PP(usec);
2352: optlen = sizeof(tv);
2353: opt_ptr = &tv;
2354: #else
2355: timeout = Z_LVAL_PP(sec) * 1000 + Z_LVAL_PP(usec) / 1000;
2356: optlen = sizeof(int);
2357: opt_ptr = &timeout;
2358: #endif
2359: break;
1.1.1.2 misho 2360: }
1.1.1.4 ! misho 2361: #ifdef SO_BINDTODEVICE
! 2362: case SO_BINDTODEVICE: {
! 2363: if (Z_TYPE_PP(arg4) == IS_STRING) {
! 2364: opt_ptr = Z_STRVAL_PP(arg4);
! 2365: optlen = Z_STRLEN_PP(arg4);
! 2366: } else {
! 2367: opt_ptr = "";
! 2368: optlen = 0;
! 2369: }
! 2370: break;
! 2371: }
! 2372: #endif
! 2373:
1.1 misho 2374: default:
2375: convert_to_long_ex(arg4);
2376: ov = Z_LVAL_PP(arg4);
2377:
2378: optlen = sizeof(ov);
2379: opt_ptr = &ov;
2380: break;
2381: }
2382:
1.1.1.2 misho 2383: dosockopt:
1.1 misho 2384: retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
2385: if (retval != 0) {
1.1.1.2 misho 2386: if (retval != -2) { /* error, but message already emitted */
2387: PHP_SOCKET_ERROR(php_sock, "unable to set socket option", errno);
2388: }
1.1 misho 2389: RETURN_FALSE;
2390: }
2391:
2392: RETURN_TRUE;
2393: }
2394: /* }}} */
2395:
2396: #ifdef HAVE_SOCKETPAIR
2397: /* {{{ proto bool socket_create_pair(int domain, int type, int protocol, array &fd) U
2398: Creates a pair of indistinguishable sockets and stores them in fds. */
2399: PHP_FUNCTION(socket_create_pair)
2400: {
2401: zval *retval[2], *fds_array_zval;
2402: php_socket *php_sock[2];
2403: PHP_SOCKET fds_array[2];
2404: long domain, type, protocol;
2405:
2406: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lllz", &domain, &type, &protocol, &fds_array_zval) == FAILURE) {
2407: return;
2408: }
2409:
1.1.1.2 misho 2410: php_sock[0] = php_create_socket();
2411: php_sock[1] = php_create_socket();
1.1 misho 2412:
2413: if (domain != AF_INET
2414: #if HAVE_IPV6
2415: && domain != AF_INET6
2416: #endif
2417: && domain != AF_UNIX) {
2418: php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%ld] specified for argument 1, assuming AF_INET", domain);
2419: domain = AF_INET;
2420: }
2421:
2422: if (type > 10) {
2423: php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%ld] specified for argument 2, assuming SOCK_STREAM", type);
2424: type = SOCK_STREAM;
2425: }
2426:
2427: if (socketpair(domain, type, protocol, fds_array) != 0) {
2428: SOCKETS_G(last_error) = errno;
2429: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to create socket pair [%d]: %s", errno, php_strerror(errno TSRMLS_CC));
2430: efree(php_sock[0]);
2431: efree(php_sock[1]);
2432: RETURN_FALSE;
2433: }
2434:
2435: zval_dtor(fds_array_zval);
2436: array_init(fds_array_zval);
2437:
2438: MAKE_STD_ZVAL(retval[0]);
2439: MAKE_STD_ZVAL(retval[1]);
2440:
2441: php_sock[0]->bsd_socket = fds_array[0];
2442: php_sock[1]->bsd_socket = fds_array[1];
2443: php_sock[0]->type = domain;
2444: php_sock[1]->type = domain;
2445: php_sock[0]->error = 0;
2446: php_sock[1]->error = 0;
2447: php_sock[0]->blocking = 1;
2448: php_sock[1]->blocking = 1;
2449:
2450: ZEND_REGISTER_RESOURCE(retval[0], php_sock[0], le_socket);
2451: ZEND_REGISTER_RESOURCE(retval[1], php_sock[1], le_socket);
2452:
2453: add_index_zval(fds_array_zval, 0, retval[0]);
2454: add_index_zval(fds_array_zval, 1, retval[1]);
2455:
2456: RETURN_TRUE;
2457: }
2458: /* }}} */
2459: #endif
2460:
2461: #ifdef HAVE_SHUTDOWN
2462: /* {{{ proto bool socket_shutdown(resource socket[, int how]) U
2463: Shuts down a socket for receiving, sending, or both. */
2464: PHP_FUNCTION(socket_shutdown)
2465: {
2466: zval *arg1;
2467: long how_shutdown = 2;
2468: php_socket *php_sock;
2469:
2470: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &how_shutdown) == FAILURE) {
2471: return;
2472: }
2473:
2474: ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
2475:
2476: if (shutdown(php_sock->bsd_socket, how_shutdown) != 0) {
2477: PHP_SOCKET_ERROR(php_sock, "unable to shutdown socket", errno);
2478: RETURN_FALSE;
2479: }
2480:
2481: RETURN_TRUE;
2482: }
2483: /* }}} */
2484: #endif
2485:
2486: /* {{{ proto int socket_last_error([resource socket]) U
2487: Returns the last socket error (either the last used or the provided socket resource) */
2488: PHP_FUNCTION(socket_last_error)
2489: {
2490: zval *arg1 = NULL;
2491: php_socket *php_sock;
2492:
2493: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE) {
2494: return;
2495: }
2496:
2497: if (arg1) {
2498: ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
2499: RETVAL_LONG(php_sock->error);
2500: } else {
2501: RETVAL_LONG(SOCKETS_G(last_error));
2502: }
2503: }
2504: /* }}} */
2505:
2506: /* {{{ proto void socket_clear_error([resource socket]) U
2507: Clears the error on the socket or the last error code. */
2508: PHP_FUNCTION(socket_clear_error)
2509: {
2510: zval *arg1 = NULL;
2511: php_socket *php_sock;
2512:
2513: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE) {
2514: return;
2515: }
2516:
2517: if (arg1) {
2518: ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
2519: php_sock->error = 0;
2520: } else {
2521: SOCKETS_G(last_error) = 0;
2522: }
2523:
2524: return;
2525: }
2526: /* }}} */
2527:
1.1.1.2 misho 2528: /* {{{ proto void socket_import_stream(resource stream)
2529: Imports a stream that encapsulates a socket into a socket extension resource. */
2530: PHP_FUNCTION(socket_import_stream)
2531: {
2532: zval *zstream;
2533: php_stream *stream;
2534: php_socket *retsock = NULL;
2535: PHP_SOCKET socket; /* fd */
2536: php_sockaddr_storage addr;
2537: socklen_t addr_len = sizeof(addr);
2538: #ifndef PHP_WIN32
2539: int t;
2540: #endif
2541:
2542: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) == FAILURE) {
2543: return;
2544: }
2545: php_stream_from_zval(stream, &zstream);
2546:
2547: if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) {
2548: /* error supposedly already shown */
2549: RETURN_FALSE;
2550: }
2551:
2552: retsock = php_create_socket();
2553:
2554: retsock->bsd_socket = socket;
2555:
2556: /* determine family */
2557: if (getsockname(socket, (struct sockaddr*)&addr, &addr_len) == 0) {
2558: retsock->type = addr.ss_family;
2559: } else {
2560: PHP_SOCKET_ERROR(retsock, "unable to obtain socket family", errno);
2561: goto error;
2562: }
2563:
2564: /* determine blocking mode */
2565: #ifndef PHP_WIN32
2566: t = fcntl(socket, F_GETFL);
2567: if(t == -1) {
2568: PHP_SOCKET_ERROR(retsock, "unable to obtain blocking state", errno);
2569: goto error;
2570: } else {
2571: retsock->blocking = !(t & O_NONBLOCK);
2572: }
2573: #else
2574: /* on windows, check if the stream is a socket stream and read its
2575: * private data; otherwise assume it's in non-blocking mode */
2576: if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) {
2577: retsock->blocking =
2578: ((php_netstream_data_t *)stream->abstract)->is_blocked;
2579: } else {
2580: retsock->blocking = 1;
2581: }
2582: #endif
2583:
2584: /* hold a zval reference to the stream (holding a php_stream* directly could
2585: * also be done, but this might be slightly better if in the future we want
2586: * to provide a socket_export_stream) */
2587: MAKE_STD_ZVAL(retsock->zstream);
2588: *retsock->zstream = *zstream;
2589: zval_copy_ctor(retsock->zstream);
2590: Z_UNSET_ISREF_P(retsock->zstream);
2591: Z_SET_REFCOUNT_P(retsock->zstream, 1);
2592:
2593: php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER,
2594: PHP_STREAM_BUFFER_NONE, NULL);
2595:
2596: ZEND_REGISTER_RESOURCE(return_value, retsock, le_socket);
2597: return;
2598: error:
2599: if (retsock != NULL)
2600: efree(retsock);
2601: RETURN_FALSE;
2602: }
2603: /* }}} */
2604:
1.1 misho 2605: #endif
2606:
2607: /*
2608: * Local variables:
2609: * tab-width: 4
2610: * c-basic-offset: 4
2611: * End:
2612: * vim600: fdm=marker
2613: * vim: noet sw=4 ts=4
2614: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>