2000-07-03 12:35:57 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
2000-07-03 12:35:57 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:08:10 +08:00
|
|
|
| Copyright (c) 1997-2014 The PHP Group |
|
2000-07-03 12:35:57 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2000-07-03 12:35:57 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2000-07-03 12:35:57 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2000-08-20 18:31:27 +08:00
|
|
|
| Authors: Chris Vandomelen <chrisv@b0rked.dhs.org> |
|
2001-05-18 01:02:37 +08:00
|
|
|
| Sterling Hughes <sterling@php.net> |
|
2001-12-14 13:58:28 +08:00
|
|
|
| Jason Greene <jason@php.net> |
|
2011-03-20 10:03:29 +08:00
|
|
|
| Gustavo Lopes <cataphract@php.net> |
|
2001-05-18 01:02:37 +08:00
|
|
|
| WinSock: Daniel Beulshausen <daniel@php4win.de> |
|
2000-07-03 12:35:57 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2001-05-24 18:07:29 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2000-07-03 12:35:57 +08:00
|
|
|
#include "php.h"
|
|
|
|
|
2002-01-27 14:55:10 +08:00
|
|
|
#include "php_network.h"
|
2007-07-23 07:01:20 +08:00
|
|
|
#include "ext/standard/file.h"
|
2000-08-20 18:31:27 +08:00
|
|
|
#include "ext/standard/info.h"
|
2000-10-23 07:43:48 +08:00
|
|
|
#include "php_ini.h"
|
2008-08-24 03:22:10 +08:00
|
|
|
#ifdef PHP_WIN32
|
2013-01-31 21:01:31 +08:00
|
|
|
# include "windows_common.h"
|
|
|
|
# include <win32/inet.h>
|
2008-08-24 03:22:10 +08:00
|
|
|
# include <windows.h>
|
|
|
|
# include <Ws2tcpip.h>
|
|
|
|
# include "php_sockets.h"
|
2013-01-31 21:01:31 +08:00
|
|
|
# include <win32/sockets.h>
|
2008-08-24 03:22:10 +08:00
|
|
|
#else
|
2001-05-18 01:02:37 +08:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <netdb.h>
|
|
|
|
# include <netinet/in.h>
|
|
|
|
# include <netinet/tcp.h>
|
|
|
|
# include <sys/un.h>
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <errno.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# include <sys/uio.h>
|
2001-09-05 06:27:30 +08:00
|
|
|
# define IS_INVALID_SOCKET(a) (a->bsd_socket < 0)
|
2001-05-18 01:02:37 +08:00
|
|
|
# define set_errno(a) (errno = a)
|
2009-01-24 21:08:04 +08:00
|
|
|
# include "php_sockets.h"
|
2011-03-14 08:08:29 +08:00
|
|
|
# if HAVE_IF_NAMETOINDEX
|
|
|
|
# include <net/if.h>
|
|
|
|
# endif
|
2001-05-18 01:02:37 +08:00
|
|
|
#endif
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2013-07-18 21:28:20 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2012-11-02 03:38:42 +08:00
|
|
|
#include "sockaddr_conv.h"
|
2011-03-14 08:08:29 +08:00
|
|
|
#include "multicast.h"
|
2012-11-02 03:38:42 +08:00
|
|
|
#include "sendrecvmsg.h"
|
2011-03-14 08:08:29 +08:00
|
|
|
|
2002-04-29 22:52:29 +08:00
|
|
|
ZEND_DECLARE_MODULE_GLOBALS(sockets)
|
2000-10-22 14:45:03 +08:00
|
|
|
|
|
|
|
#ifndef MSG_WAITALL
|
|
|
|
#ifdef LINUX
|
|
|
|
#define MSG_WAITALL 0x00000100
|
|
|
|
#else
|
|
|
|
#define MSG_WAITALL 0x00000000
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2007-07-24 19:35:08 +08:00
|
|
|
#ifndef MSG_EOF
|
|
|
|
#ifdef MSG_FIN
|
|
|
|
#define MSG_EOF MSG_FIN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2001-01-04 02:06:10 +08:00
|
|
|
#ifndef SUN_LEN
|
2001-06-13 05:06:01 +08:00
|
|
|
#define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
|
2001-01-04 02:06:10 +08:00
|
|
|
#endif
|
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
#ifndef PF_INET
|
|
|
|
#define PF_INET AF_INET
|
|
|
|
#endif
|
|
|
|
|
2000-10-30 09:09:15 +08:00
|
|
|
#define PHP_NORMAL_READ 0x0001
|
|
|
|
#define PHP_BINARY_READ 0x0002
|
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
static int le_socket;
|
2009-12-27 09:00:50 +08:00
|
|
|
#define le_socket_name php_sockets_le_socket_name
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2008-07-04 22:08:16 +08:00
|
|
|
/* {{{ arginfo */
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_select, 0, 0, 4)
|
|
|
|
ZEND_ARG_INFO(1, read_fds)
|
|
|
|
ZEND_ARG_INFO(1, write_fds)
|
|
|
|
ZEND_ARG_INFO(1, except_fds)
|
|
|
|
ZEND_ARG_INFO(0, tv_sec)
|
|
|
|
ZEND_ARG_INFO(0, tv_usec)
|
|
|
|
ZEND_END_ARG_INFO()
|
2003-08-04 01:44:39 +08:00
|
|
|
|
2008-07-04 22:08:16 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_listen, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, port)
|
|
|
|
ZEND_ARG_INFO(0, backlog)
|
|
|
|
ZEND_END_ARG_INFO()
|
2002-05-01 18:41:10 +08:00
|
|
|
|
2008-07-04 22:08:16 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_accept, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_nonblock, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_block, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_listen, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, backlog)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_close, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_write, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, buf)
|
|
|
|
ZEND_ARG_INFO(0, length)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_read, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, length)
|
|
|
|
ZEND_ARG_INFO(0, type)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_getsockname, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(1, addr)
|
|
|
|
ZEND_ARG_INFO(1, port)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_getpeername, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(1, addr)
|
|
|
|
ZEND_ARG_INFO(1, port)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create, 0, 0, 3)
|
|
|
|
ZEND_ARG_INFO(0, domain)
|
|
|
|
ZEND_ARG_INFO(0, type)
|
|
|
|
ZEND_ARG_INFO(0, protocol)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_connect, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, addr)
|
|
|
|
ZEND_ARG_INFO(0, port)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_strerror, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, errno)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_bind, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, addr)
|
|
|
|
ZEND_ARG_INFO(0, port)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recv, 0, 0, 4)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(1, buf)
|
|
|
|
ZEND_ARG_INFO(0, len)
|
|
|
|
ZEND_ARG_INFO(0, flags)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_send, 0, 0, 4)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, buf)
|
|
|
|
ZEND_ARG_INFO(0, len)
|
|
|
|
ZEND_ARG_INFO(0, flags)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recvfrom, 0, 0, 5)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(1, buf)
|
|
|
|
ZEND_ARG_INFO(0, len)
|
|
|
|
ZEND_ARG_INFO(0, flags)
|
|
|
|
ZEND_ARG_INFO(1, name)
|
|
|
|
ZEND_ARG_INFO(1, port)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_sendto, 0, 0, 5)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, buf)
|
|
|
|
ZEND_ARG_INFO(0, len)
|
|
|
|
ZEND_ARG_INFO(0, flags)
|
|
|
|
ZEND_ARG_INFO(0, addr)
|
|
|
|
ZEND_ARG_INFO(0, port)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_get_option, 0, 0, 3)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, level)
|
|
|
|
ZEND_ARG_INFO(0, optname)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_option, 0, 0, 4)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, level)
|
|
|
|
ZEND_ARG_INFO(0, optname)
|
|
|
|
ZEND_ARG_INFO(0, optval)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2009-01-23 23:49:49 +08:00
|
|
|
#ifdef HAVE_SOCKETPAIR
|
2008-07-04 22:08:16 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_pair, 0, 0, 4)
|
|
|
|
ZEND_ARG_INFO(0, domain)
|
|
|
|
ZEND_ARG_INFO(0, type)
|
|
|
|
ZEND_ARG_INFO(0, protocol)
|
|
|
|
ZEND_ARG_INFO(1, fd)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SHUTDOWN
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_shutdown, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, how)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_last_error, 0, 0, 0)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_clear_error, 0, 0, 0)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_END_ARG_INFO()
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_import_stream, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, stream)
|
|
|
|
ZEND_END_ARG_INFO()
|
2012-11-02 03:38:42 +08:00
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_sendmsg, 0, 0, 3)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(0, msghdr)
|
|
|
|
ZEND_ARG_INFO(0, flags)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recvmsg, 0, 0, 3)
|
|
|
|
ZEND_ARG_INFO(0, socket)
|
|
|
|
ZEND_ARG_INFO(1, msghdr)
|
|
|
|
ZEND_ARG_INFO(0, flags)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_cmsg_space, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, level)
|
|
|
|
ZEND_ARG_INFO(0, type)
|
|
|
|
ZEND_END_ARG_INFO()
|
2008-07-04 22:08:16 +08:00
|
|
|
/* }}} */
|
2003-08-04 01:44:39 +08:00
|
|
|
|
2013-02-24 10:40:22 +08:00
|
|
|
static PHP_GINIT_FUNCTION(sockets);
|
|
|
|
static PHP_MINIT_FUNCTION(sockets);
|
|
|
|
static PHP_MSHUTDOWN_FUNCTION(sockets);
|
|
|
|
static PHP_MINFO_FUNCTION(sockets);
|
|
|
|
static PHP_RSHUTDOWN_FUNCTION(sockets);
|
2013-02-01 21:35:14 +08:00
|
|
|
|
|
|
|
PHP_FUNCTION(socket_select);
|
|
|
|
PHP_FUNCTION(socket_create_listen);
|
|
|
|
#ifdef HAVE_SOCKETPAIR
|
|
|
|
PHP_FUNCTION(socket_create_pair);
|
|
|
|
#endif
|
|
|
|
PHP_FUNCTION(socket_accept);
|
|
|
|
PHP_FUNCTION(socket_set_nonblock);
|
|
|
|
PHP_FUNCTION(socket_set_block);
|
|
|
|
PHP_FUNCTION(socket_listen);
|
|
|
|
PHP_FUNCTION(socket_close);
|
|
|
|
PHP_FUNCTION(socket_write);
|
|
|
|
PHP_FUNCTION(socket_read);
|
|
|
|
PHP_FUNCTION(socket_getsockname);
|
|
|
|
PHP_FUNCTION(socket_getpeername);
|
|
|
|
PHP_FUNCTION(socket_create);
|
|
|
|
PHP_FUNCTION(socket_connect);
|
|
|
|
PHP_FUNCTION(socket_strerror);
|
|
|
|
PHP_FUNCTION(socket_bind);
|
|
|
|
PHP_FUNCTION(socket_recv);
|
|
|
|
PHP_FUNCTION(socket_send);
|
|
|
|
PHP_FUNCTION(socket_recvfrom);
|
|
|
|
PHP_FUNCTION(socket_sendto);
|
|
|
|
PHP_FUNCTION(socket_get_option);
|
|
|
|
PHP_FUNCTION(socket_set_option);
|
|
|
|
#ifdef HAVE_SHUTDOWN
|
|
|
|
PHP_FUNCTION(socket_shutdown);
|
|
|
|
#endif
|
|
|
|
PHP_FUNCTION(socket_last_error);
|
|
|
|
PHP_FUNCTION(socket_clear_error);
|
|
|
|
PHP_FUNCTION(socket_import_stream);
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ sockets_functions[]
|
|
|
|
*/
|
2007-09-28 02:00:48 +08:00
|
|
|
const zend_function_entry sockets_functions[] = {
|
2008-07-04 22:08:16 +08:00
|
|
|
PHP_FE(socket_select, arginfo_socket_select)
|
|
|
|
PHP_FE(socket_create, arginfo_socket_create)
|
|
|
|
PHP_FE(socket_create_listen, arginfo_socket_create_listen)
|
2009-01-23 23:49:49 +08:00
|
|
|
#ifdef HAVE_SOCKETPAIR
|
2008-07-04 22:08:16 +08:00
|
|
|
PHP_FE(socket_create_pair, arginfo_socket_create_pair)
|
2005-09-23 17:54:31 +08:00
|
|
|
#endif
|
2008-07-04 22:08:16 +08:00
|
|
|
PHP_FE(socket_accept, arginfo_socket_accept)
|
|
|
|
PHP_FE(socket_set_nonblock, arginfo_socket_set_nonblock)
|
|
|
|
PHP_FE(socket_set_block, arginfo_socket_set_block)
|
|
|
|
PHP_FE(socket_listen, arginfo_socket_listen)
|
|
|
|
PHP_FE(socket_close, arginfo_socket_close)
|
|
|
|
PHP_FE(socket_write, arginfo_socket_write)
|
|
|
|
PHP_FE(socket_read, arginfo_socket_read)
|
|
|
|
PHP_FE(socket_getsockname, arginfo_socket_getsockname)
|
|
|
|
PHP_FE(socket_getpeername, arginfo_socket_getpeername)
|
|
|
|
PHP_FE(socket_connect, arginfo_socket_connect)
|
|
|
|
PHP_FE(socket_strerror, arginfo_socket_strerror)
|
|
|
|
PHP_FE(socket_bind, arginfo_socket_bind)
|
|
|
|
PHP_FE(socket_recv, arginfo_socket_recv)
|
|
|
|
PHP_FE(socket_send, arginfo_socket_send)
|
|
|
|
PHP_FE(socket_recvfrom, arginfo_socket_recvfrom)
|
|
|
|
PHP_FE(socket_sendto, arginfo_socket_sendto)
|
|
|
|
PHP_FE(socket_get_option, arginfo_socket_get_option)
|
|
|
|
PHP_FE(socket_set_option, arginfo_socket_set_option)
|
2007-01-10 23:25:07 +08:00
|
|
|
#ifdef HAVE_SHUTDOWN
|
2008-07-04 22:08:16 +08:00
|
|
|
PHP_FE(socket_shutdown, arginfo_socket_shutdown)
|
2007-01-10 23:25:07 +08:00
|
|
|
#endif
|
2008-07-04 22:08:16 +08:00
|
|
|
PHP_FE(socket_last_error, arginfo_socket_last_error)
|
|
|
|
PHP_FE(socket_clear_error, arginfo_socket_clear_error)
|
2011-03-22 08:44:23 +08:00
|
|
|
PHP_FE(socket_import_stream, arginfo_socket_import_stream)
|
2012-11-02 03:38:42 +08:00
|
|
|
PHP_FE(socket_sendmsg, arginfo_socket_sendmsg)
|
|
|
|
PHP_FE(socket_recvmsg, arginfo_socket_recvmsg)
|
|
|
|
PHP_FE(socket_cmsg_space, arginfo_socket_cmsg_space)
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2013-07-13 20:15:21 +08:00
|
|
|
/* for downwards compatibility */
|
2008-07-04 22:08:16 +08:00
|
|
|
PHP_FALIAS(socket_getopt, socket_get_option, arginfo_socket_get_option)
|
|
|
|
PHP_FALIAS(socket_setopt, socket_set_option, arginfo_socket_set_option)
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2011-07-25 19:35:02 +08:00
|
|
|
PHP_FE_END
|
2000-07-03 12:35:57 +08:00
|
|
|
};
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2000-08-20 18:31:27 +08:00
|
|
|
zend_module_entry sockets_module_entry = {
|
2001-10-12 07:33:59 +08:00
|
|
|
STANDARD_MODULE_HEADER,
|
2000-07-03 12:35:57 +08:00
|
|
|
"sockets",
|
|
|
|
sockets_functions,
|
|
|
|
PHP_MINIT(sockets),
|
2013-02-24 10:40:22 +08:00
|
|
|
PHP_MSHUTDOWN(sockets),
|
2007-05-20 01:58:22 +08:00
|
|
|
NULL,
|
2002-09-30 10:09:42 +08:00
|
|
|
PHP_RSHUTDOWN(sockets),
|
2000-07-03 12:35:57 +08:00
|
|
|
PHP_MINFO(sockets),
|
2002-04-29 23:06:48 +08:00
|
|
|
NO_VERSION_YET,
|
2006-06-16 02:33:09 +08:00
|
|
|
PHP_MODULE_GLOBALS(sockets),
|
|
|
|
PHP_GINIT(sockets),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
STANDARD_MODULE_PROPERTIES_EX
|
2000-07-03 12:35:57 +08:00
|
|
|
};
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2000-07-03 12:35:57 +08:00
|
|
|
#ifdef COMPILE_DL_SOCKETS
|
|
|
|
ZEND_GET_MODULE(sockets)
|
|
|
|
#endif
|
|
|
|
|
2001-06-25 05:16:21 +08:00
|
|
|
/* inet_ntop should be used instead of inet_ntoa */
|
|
|
|
int inet_ntoa_lock = 0;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2009-09-05 03:53:39 +08:00
|
|
|
PHP_SOCKETS_API int php_sockets_le_socket(void) /* {{{ */
|
2009-08-31 16:41:03 +08:00
|
|
|
{
|
|
|
|
return le_socket;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
/* allocating function to make programming errors due to uninitialized fields
|
|
|
|
* less likely */
|
|
|
|
static php_socket *php_create_socket(void) /* {{{ */
|
|
|
|
{
|
2014-05-17 10:23:46 +08:00
|
|
|
php_socket *php_sock = emalloc(sizeof(php_socket));
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
php_sock->bsd_socket = -1; /* invalid socket */
|
|
|
|
php_sock->type = PF_UNSPEC;
|
|
|
|
php_sock->error = 0;
|
|
|
|
php_sock->blocking = 1;
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_UNDEF(&php_sock->zstream);
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
return php_sock;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
static void php_destroy_socket(zend_resource *rsrc TSRMLS_DC) /* {{{ */
|
2001-06-04 05:38:23 +08:00
|
|
|
{
|
2011-03-22 08:44:23 +08:00
|
|
|
php_socket *php_sock = rsrc->ptr;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
if (Z_ISUNDEF(php_sock->zstream)) {
|
2011-03-22 08:44:23 +08:00
|
|
|
if (!IS_INVALID_SOCKET(php_sock)) {
|
|
|
|
close(php_sock->bsd_socket);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
zval_ptr_dtor(&php_sock->zstream);
|
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
efree(php_sock);
|
|
|
|
}
|
2007-07-23 06:22:09 +08:00
|
|
|
/* }}} */
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
static int php_open_listen_sock(php_socket **php_sock, int port, int backlog TSRMLS_DC) /* {{{ */
|
2001-05-18 01:02:37 +08:00
|
|
|
{
|
2001-06-26 12:19:39 +08:00
|
|
|
struct sockaddr_in la;
|
2001-11-02 00:51:59 +08:00
|
|
|
struct hostent *hp;
|
2011-03-22 08:44:23 +08:00
|
|
|
php_socket *sock = php_create_socket();
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
*php_sock = sock;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
|
|
|
#ifndef PHP_WIN32
|
|
|
|
if ((hp = gethostbyname("0.0.0.0")) == NULL) {
|
|
|
|
#else
|
|
|
|
if ((hp = gethostbyname("localhost")) == NULL) {
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
efree(sock);
|
|
|
|
return 0;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
|
2001-05-18 01:02:37 +08:00
|
|
|
la.sin_family = hp->h_addrtype;
|
2001-06-26 12:19:39 +08:00
|
|
|
la.sin_port = htons((unsigned short) port);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
|
2007-07-23 07:01:20 +08:00
|
|
|
sock->blocking = 1;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
if (IS_INVALID_SOCKET(sock)) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(sock, "unable to create listening socket", errno);
|
2001-11-02 00:51:59 +08:00
|
|
|
efree(sock);
|
|
|
|
return 0;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
sock->type = PF_INET;
|
|
|
|
|
2009-09-04 15:59:48 +08:00
|
|
|
if (bind(sock->bsd_socket, (struct sockaddr *)&la, sizeof(la)) != 0) {
|
2009-04-16 13:33:13 +08:00
|
|
|
PHP_SOCKET_ERROR(sock, "unable to bind to given address", errno);
|
2001-11-02 00:51:59 +08:00
|
|
|
close(sock->bsd_socket);
|
|
|
|
efree(sock);
|
|
|
|
return 0;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2009-09-04 15:59:48 +08:00
|
|
|
if (listen(sock->bsd_socket, backlog) != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(sock, "unable to listen on socket", errno);
|
2001-11-02 00:51:59 +08:00
|
|
|
close(sock->bsd_socket);
|
|
|
|
efree(sock);
|
|
|
|
return 0;
|
2001-06-26 12:19:39 +08:00
|
|
|
}
|
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
return 1;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2007-07-23 06:22:09 +08:00
|
|
|
/* }}} */
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2011-03-15 06:59:05 +08:00
|
|
|
static int php_accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la, socklen_t *la_len TSRMLS_DC) /* {{{ */
|
2001-05-18 01:02:37 +08:00
|
|
|
{
|
2011-03-22 08:44:23 +08:00
|
|
|
php_socket *out_sock = php_create_socket();
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
*new_sock = out_sock;
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2011-03-15 06:59:05 +08:00
|
|
|
out_sock->bsd_socket = accept(in_sock->bsd_socket, la, la_len);
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
if (IS_INVALID_SOCKET(out_sock)) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
|
2001-11-02 00:51:59 +08:00
|
|
|
efree(out_sock);
|
|
|
|
return 0;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2011-03-15 06:59:05 +08:00
|
|
|
out_sock->error = 0;
|
|
|
|
out_sock->blocking = 1;
|
|
|
|
out_sock->type = la->sa_family;
|
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
return 1;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2007-07-23 06:22:09 +08:00
|
|
|
/* }}} */
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
|
2007-07-23 07:01:20 +08:00
|
|
|
static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags)
|
2001-05-18 01:02:37 +08:00
|
|
|
{
|
2003-04-17 03:18:35 +08:00
|
|
|
int m = 0;
|
|
|
|
size_t n = 0;
|
2001-05-22 01:34:18 +08:00
|
|
|
int no_read = 0;
|
|
|
|
int nonblock = 0;
|
|
|
|
char *t = (char *) buf;
|
|
|
|
|
2007-07-23 07:01:20 +08:00
|
|
|
#ifndef PHP_WIN32
|
|
|
|
m = fcntl(sock->bsd_socket, F_GETFL);
|
2001-05-22 01:34:18 +08:00
|
|
|
if (m < 0) {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
nonblock = (m & O_NONBLOCK);
|
|
|
|
m = 0;
|
2007-07-23 07:01:20 +08:00
|
|
|
#else
|
|
|
|
nonblock = !sock->blocking;
|
|
|
|
#endif
|
2001-05-22 01:34:18 +08:00
|
|
|
set_errno(0);
|
|
|
|
|
2004-02-26 06:10:09 +08:00
|
|
|
*t = '\0';
|
2001-05-22 01:34:18 +08:00
|
|
|
while (*t != '\n' && *t != '\r' && n < maxlen) {
|
|
|
|
if (m > 0) {
|
|
|
|
t++;
|
|
|
|
n++;
|
|
|
|
} else if (m == 0) {
|
|
|
|
no_read++;
|
|
|
|
if (nonblock && no_read >= 2) {
|
|
|
|
return n;
|
|
|
|
/* The first pass, m always is 0, so no_read becomes 1
|
|
|
|
* in the first pass. no_read becomes 2 in the second pass,
|
|
|
|
* and if this is nonblocking, we should return.. */
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
if (no_read > 200) {
|
|
|
|
set_errno(ECONNRESET);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
if (n < maxlen) {
|
2007-07-23 07:01:20 +08:00
|
|
|
m = recv(sock->bsd_socket, (void *) t, 1, flags);
|
2001-05-22 01:34:18 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
set_errno(0);
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
if (n < maxlen) {
|
2001-05-18 01:02:37 +08:00
|
|
|
n++;
|
|
|
|
/* The only reasons it makes it to here is
|
|
|
|
* if '\n' or '\r' are encountered. So, increase
|
|
|
|
* the return by 1 to make up for the lack of the
|
|
|
|
* '\n' or '\r' in the count (since read() takes
|
|
|
|
* place at the end of the loop..) */
|
2001-05-22 01:34:18 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
return n;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2012-11-02 03:38:42 +08:00
|
|
|
char *sockets_strerror(int error TSRMLS_DC) /* {{{ */
|
2003-04-18 07:18:58 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
const char *buf;
|
|
|
|
|
|
|
|
#ifndef PHP_WIN32
|
|
|
|
if (error < -10000) {
|
2002-03-05 11:45:23 +08:00
|
|
|
error = -error - 10000;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_HSTRERROR
|
|
|
|
buf = hstrerror(error);
|
|
|
|
#else
|
|
|
|
{
|
2005-05-13 00:26:26 +08:00
|
|
|
if (SOCKETS_G(strerror_buf)) {
|
|
|
|
efree(SOCKETS_G(strerror_buf));
|
|
|
|
}
|
|
|
|
|
2004-06-07 13:00:37 +08:00
|
|
|
spprintf(&(SOCKETS_G(strerror_buf)), 0, "Host lookup error %d", error);
|
2002-09-30 10:09:42 +08:00
|
|
|
buf = SOCKETS_G(strerror_buf);
|
2001-11-02 00:51:59 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
buf = strerror(error);
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
#else
|
2002-07-07 15:01:17 +08:00
|
|
|
{
|
|
|
|
LPTSTR tmp = NULL;
|
2003-04-17 01:31:31 +08:00
|
|
|
buf = NULL;
|
2002-07-07 15:01:17 +08:00
|
|
|
|
2003-04-17 01:31:31 +08:00
|
|
|
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2007-07-23 06:47:18 +08:00
|
|
|
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &tmp, 0, NULL)
|
|
|
|
) {
|
2005-05-13 00:26:26 +08:00
|
|
|
if (SOCKETS_G(strerror_buf)) {
|
|
|
|
efree(SOCKETS_G(strerror_buf));
|
|
|
|
}
|
|
|
|
|
2004-05-18 18:49:06 +08:00
|
|
|
SOCKETS_G(strerror_buf) = estrdup(tmp);
|
2003-04-17 01:31:31 +08:00
|
|
|
LocalFree(tmp);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2003-04-17 01:31:31 +08:00
|
|
|
buf = SOCKETS_G(strerror_buf);
|
|
|
|
}
|
2002-07-07 15:01:17 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
#endif
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
return (buf ? (char *) buf : "");
|
|
|
|
}
|
2007-07-23 06:22:09 +08:00
|
|
|
/* }}} */
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
/* {{{ PHP_GINIT_FUNCTION */
|
2006-06-16 02:33:09 +08:00
|
|
|
static PHP_GINIT_FUNCTION(sockets)
|
2002-04-29 22:52:29 +08:00
|
|
|
{
|
|
|
|
sockets_globals->last_error = 0;
|
2002-09-30 10:09:42 +08:00
|
|
|
sockets_globals->strerror_buf = NULL;
|
2002-04-29 22:52:29 +08:00
|
|
|
}
|
2007-07-23 06:22:09 +08:00
|
|
|
/* }}} */
|
2002-04-29 22:52:29 +08:00
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION
|
|
|
|
*/
|
2013-02-24 10:40:22 +08:00
|
|
|
static PHP_MINIT_FUNCTION(sockets)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2003-04-18 07:18:58 +08:00
|
|
|
le_socket = zend_register_list_destructors_ex(php_destroy_socket, NULL, le_socket_name, module_number);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("AF_UNIX", AF_UNIX, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("AF_INET", AF_INET, CONST_CS | CONST_PERSISTENT);
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("AF_INET6", AF_INET6, CONST_CS | CONST_PERSISTENT);
|
2003-04-18 05:04:47 +08:00
|
|
|
#endif
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SOCK_STREAM", SOCK_STREAM, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SOCK_DGRAM", SOCK_DGRAM, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SOCK_RAW", SOCK_RAW, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SOCK_SEQPACKET",SOCK_SEQPACKET, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SOCK_RDM", SOCK_RDM, CONST_CS | CONST_PERSISTENT);
|
|
|
|
|
|
|
|
REGISTER_LONG_CONSTANT("MSG_OOB", MSG_OOB, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MSG_WAITALL", MSG_WAITALL, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MSG_CTRUNC", MSG_CTRUNC, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MSG_TRUNC", MSG_TRUNC, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MSG_PEEK", MSG_PEEK, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MSG_DONTROUTE", MSG_DONTROUTE, CONST_CS | CONST_PERSISTENT);
|
2013-01-31 21:01:31 +08:00
|
|
|
#ifdef MSG_EOR
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_EOR", MSG_EOR, CONST_CS | CONST_PERSISTENT);
|
2013-01-31 21:01:31 +08:00
|
|
|
#endif
|
|
|
|
#ifdef MSG_EOF
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_EOF", MSG_EOF, CONST_CS | CONST_PERSISTENT);
|
2013-01-31 21:01:31 +08:00
|
|
|
#endif
|
2012-11-05 23:12:21 +08:00
|
|
|
|
|
|
|
#ifdef MSG_CONFIRM
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_CONFIRM", MSG_CONFIRM, CONST_CS | CONST_PERSISTENT);
|
2012-11-05 23:12:21 +08:00
|
|
|
#endif
|
|
|
|
#ifdef MSG_ERRQUEUE
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_ERRQUEUE", MSG_ERRQUEUE, CONST_CS | CONST_PERSISTENT);
|
2012-11-05 23:12:21 +08:00
|
|
|
#endif
|
|
|
|
#ifdef MSG_NOSIGNAL
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_NOSIGNAL", MSG_NOSIGNAL, CONST_CS | CONST_PERSISTENT);
|
2007-07-24 19:35:08 +08:00
|
|
|
#endif
|
2012-11-05 23:12:21 +08:00
|
|
|
#ifdef MSG_DONTWAIT
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_DONTWAIT", MSG_DONTWAIT, CONST_CS | CONST_PERSISTENT);
|
2012-11-05 23:12:21 +08:00
|
|
|
#endif
|
|
|
|
#ifdef MSG_MORE
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_MORE", MSG_MORE, CONST_CS | CONST_PERSISTENT);
|
2012-11-05 23:12:21 +08:00
|
|
|
#endif
|
|
|
|
#ifdef MSG_WAITFORONE
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_WAITFORONE",MSG_WAITFORONE, CONST_CS | CONST_PERSISTENT);
|
2012-11-05 23:12:21 +08:00
|
|
|
#endif
|
|
|
|
#ifdef MSG_CMSG_CLOEXEC
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MSG_CMSG_CLOEXEC",MSG_CMSG_CLOEXEC,CONST_CS | CONST_PERSISTENT);
|
2012-11-05 23:12:21 +08:00
|
|
|
#endif
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SO_DEBUG", SO_DEBUG, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_REUSEADDR", SO_REUSEADDR, CONST_CS | CONST_PERSISTENT);
|
2012-09-03 05:18:45 +08:00
|
|
|
#ifdef SO_REUSEPORT
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SO_REUSEPORT", SO_REUSEPORT, CONST_CS | CONST_PERSISTENT);
|
2012-09-03 05:18:45 +08:00
|
|
|
#endif
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SO_KEEPALIVE", SO_KEEPALIVE, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_DONTROUTE", SO_DONTROUTE, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_LINGER", SO_LINGER, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_BROADCAST", SO_BROADCAST, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_OOBINLINE", SO_OOBINLINE, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_SNDBUF", SO_SNDBUF, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_RCVBUF", SO_RCVBUF, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_SNDLOWAT", SO_SNDLOWAT, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_RCVLOWAT", SO_RCVLOWAT, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_SNDTIMEO", SO_SNDTIMEO, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_RCVTIMEO", SO_RCVTIMEO, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_TYPE", SO_TYPE, CONST_CS | CONST_PERSISTENT);
|
2012-10-23 19:09:38 +08:00
|
|
|
#ifdef SO_FAMILY
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SO_FAMILY", SO_FAMILY, CONST_CS | CONST_PERSISTENT);
|
2012-10-23 19:09:38 +08:00
|
|
|
#endif
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SO_ERROR", SO_ERROR, CONST_CS | CONST_PERSISTENT);
|
2013-06-10 18:11:14 +08:00
|
|
|
#ifdef SO_BINDTODEVICE
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SO_BINDTODEVICE", SO_BINDTODEVICE, CONST_CS | CONST_PERSISTENT);
|
2013-06-10 18:11:14 +08:00
|
|
|
#endif
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SOL_SOCKET", SOL_SOCKET, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SOMAXCONN", SOMAXCONN, CONST_CS | CONST_PERSISTENT);
|
2008-10-23 02:59:33 +08:00
|
|
|
#ifdef TCP_NODELAY
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("TCP_NODELAY", TCP_NODELAY, CONST_CS | CONST_PERSISTENT);
|
2008-10-23 02:59:33 +08:00
|
|
|
#endif
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("PHP_NORMAL_READ", PHP_NORMAL_READ, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PHP_BINARY_READ", PHP_BINARY_READ, CONST_CS | CONST_PERSISTENT);
|
2000-10-30 09:09:15 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MCAST_JOIN_GROUP", PHP_MCAST_JOIN_GROUP, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MCAST_LEAVE_GROUP", PHP_MCAST_LEAVE_GROUP, CONST_CS | CONST_PERSISTENT);
|
2011-06-12 04:20:50 +08:00
|
|
|
#ifdef HAS_MCAST_EXT
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("MCAST_BLOCK_SOURCE", PHP_MCAST_BLOCK_SOURCE, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MCAST_UNBLOCK_SOURCE", PHP_MCAST_UNBLOCK_SOURCE, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MCAST_JOIN_SOURCE_GROUP", PHP_MCAST_JOIN_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MCAST_LEAVE_SOURCE_GROUP", PHP_MCAST_LEAVE_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT);
|
2011-06-12 04:20:50 +08:00
|
|
|
#endif
|
2011-03-14 08:08:29 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("IP_MULTICAST_IF", IP_MULTICAST_IF, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("IP_MULTICAST_TTL", IP_MULTICAST_TTL, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("IP_MULTICAST_LOOP", IP_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT);
|
2011-03-14 08:08:29 +08:00
|
|
|
#if HAVE_IPV6
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("IPV6_MULTICAST_IF", IPV6_MULTICAST_IF, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT);
|
2011-03-14 08:08:29 +08:00
|
|
|
#endif
|
|
|
|
|
2002-08-26 07:28:42 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
# include "unix_socket_constants.h"
|
2002-09-23 11:34:21 +08:00
|
|
|
#else
|
|
|
|
# include "win32_socket_constants.h"
|
2002-08-26 07:28:42 +08:00
|
|
|
#endif
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("IPPROTO_IP", IPPROTO_IP, CONST_CS | CONST_PERSISTENT);
|
2011-03-14 08:08:29 +08:00
|
|
|
#if HAVE_IPV6
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("IPPROTO_IPV6", IPPROTO_IPV6, CONST_CS | CONST_PERSISTENT);
|
2011-03-14 08:08:29 +08:00
|
|
|
#endif
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SOL_TCP", IPPROTO_TCP, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SOL_UDP", IPPROTO_UDP, CONST_CS | CONST_PERSISTENT);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2012-11-02 22:02:47 +08:00
|
|
|
#if HAVE_IPV6
|
2014-08-26 01:24:55 +08:00
|
|
|
REGISTER_LONG_CONSTANT("IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS, CONST_CS | CONST_PERSISTENT);
|
2012-11-02 22:02:47 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-06 20:38:57 +08:00
|
|
|
php_socket_sendrecvmsg_init(INIT_FUNC_ARGS_PASSTHRU);
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2000-07-03 12:35:57 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2013-02-24 10:40:22 +08:00
|
|
|
/* {{{ PHP_MSHUTDOWN_FUNCTION
|
|
|
|
*/
|
|
|
|
static PHP_MSHUTDOWN_FUNCTION(sockets)
|
|
|
|
{
|
|
|
|
php_socket_sendrecvmsg_shutdown(SHUTDOWN_FUNC_ARGS_PASSTHRU);
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ PHP_MINFO_FUNCTION
|
|
|
|
*/
|
2013-02-24 10:40:22 +08:00
|
|
|
static PHP_MINFO_FUNCTION(sockets)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
|
|
|
php_info_print_table_start();
|
2001-05-18 01:02:37 +08:00
|
|
|
php_info_print_table_row(2, "Sockets Support", "enabled");
|
2000-07-03 12:35:57 +08:00
|
|
|
php_info_print_table_end();
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2002-09-30 10:09:42 +08:00
|
|
|
/* {{{ PHP_RSHUTDOWN_FUNCTION */
|
2013-02-24 10:40:22 +08:00
|
|
|
static PHP_RSHUTDOWN_FUNCTION(sockets)
|
2002-09-30 10:09:42 +08:00
|
|
|
{
|
2004-05-16 23:34:53 +08:00
|
|
|
if (SOCKETS_G(strerror_buf)) {
|
|
|
|
efree(SOCKETS_G(strerror_buf));
|
|
|
|
SOCKETS_G(strerror_buf) = NULL;
|
|
|
|
}
|
|
|
|
|
2002-09-30 10:09:42 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2003-04-18 07:18:58 +08:00
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
static int php_sock_array_to_fd_set(zval *sock_array, fd_set *fds, PHP_SOCKET *max_fd TSRMLS_DC) /* {{{ */
|
2003-04-18 07:18:58 +08:00
|
|
|
{
|
2014-05-17 10:23:46 +08:00
|
|
|
zval *element;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
2006-07-30 19:54:17 +08:00
|
|
|
int num = 0;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(sock_array), element) {
|
2002-03-07 04:19:09 +08:00
|
|
|
php_sock = (php_socket*) zend_fetch_resource(element TSRMLS_CC, -1, le_socket_name, NULL, 1, le_socket);
|
2002-03-11 09:24:42 +08:00
|
|
|
if (!php_sock) continue; /* If element is not a resource, skip it */
|
2002-03-07 04:19:09 +08:00
|
|
|
|
2004-09-17 20:44:56 +08:00
|
|
|
PHP_SAFE_FD_SET(php_sock->bsd_socket, fds);
|
2002-04-29 23:06:48 +08:00
|
|
|
if (php_sock->bsd_socket > *max_fd) {
|
|
|
|
*max_fd = php_sock->bsd_socket;
|
|
|
|
}
|
2006-07-30 19:54:17 +08:00
|
|
|
num++;
|
2014-05-17 10:23:46 +08:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2006-07-30 19:54:17 +08:00
|
|
|
return num ? 1 : 0;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2007-07-23 06:22:09 +08:00
|
|
|
/* }}} */
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
static int php_sock_array_from_fd_set(zval *sock_array, fd_set *fds TSRMLS_DC) /* {{{ */
|
2003-04-18 07:18:58 +08:00
|
|
|
{
|
2014-05-17 10:23:46 +08:00
|
|
|
zval *element;
|
|
|
|
zval *dest_element;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
2014-05-17 10:23:46 +08:00
|
|
|
zval new_hash;
|
2006-07-30 19:54:17 +08:00
|
|
|
int num = 0;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong num_key;
|
2014-05-17 10:23:46 +08:00
|
|
|
zend_string *key;
|
2003-04-18 07:18:58 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
array_init(&new_hash);
|
|
|
|
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(sock_array), num_key, key, element) {
|
2002-03-07 04:19:09 +08:00
|
|
|
php_sock = (php_socket*) zend_fetch_resource(element TSRMLS_CC, -1, le_socket_name, NULL, 1, le_socket);
|
2002-03-11 09:24:42 +08:00
|
|
|
if (!php_sock) continue; /* If element is not a resource, skip it */
|
|
|
|
|
2004-09-17 20:44:56 +08:00
|
|
|
if (PHP_SAFE_FD_ISSET(php_sock->bsd_socket, fds)) {
|
2002-04-29 23:06:48 +08:00
|
|
|
/* Add fd to new array */
|
2014-05-17 10:23:46 +08:00
|
|
|
if (key) {
|
|
|
|
dest_element = zend_hash_add(Z_ARRVAL(new_hash), key, element);
|
|
|
|
} else {
|
|
|
|
dest_element = zend_hash_index_update(Z_ARRVAL(new_hash), num_key, element);
|
|
|
|
}
|
|
|
|
if (dest_element) {
|
|
|
|
Z_ADDREF_P(dest_element);
|
2008-02-21 10:56:57 +08:00
|
|
|
}
|
2001-07-26 06:03:55 +08:00
|
|
|
}
|
2006-07-30 19:54:17 +08:00
|
|
|
num++;
|
2014-05-17 10:23:46 +08:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
/* Destroy old array, add new one */
|
2014-05-17 10:23:46 +08:00
|
|
|
zval_ptr_dtor(sock_array);
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_COPY_VALUE(sock_array, &new_hash);
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2006-07-30 19:54:17 +08:00
|
|
|
return num ? 1 : 0;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2007-07-23 06:22:09 +08:00
|
|
|
/* }}} */
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto int socket_select(array &read_fds, array &write_fds, array &except_fds, int tv_sec[, int tv_usec])
|
2000-08-23 21:47:00 +08:00
|
|
|
Runs the select() system call on the sets mentioned with a timeout specified by tv_sec and tv_usec */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_select)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2002-03-09 12:37:54 +08:00
|
|
|
zval *r_array, *w_array, *e_array, *sec;
|
2001-11-02 00:51:59 +08:00
|
|
|
struct timeval tv;
|
2002-04-29 23:06:48 +08:00
|
|
|
struct timeval *tv_p = NULL;
|
2002-03-07 04:19:09 +08:00
|
|
|
fd_set rfds, wfds, efds;
|
2007-07-23 06:47:18 +08:00
|
|
|
PHP_SOCKET max_fd = 0;
|
|
|
|
int retval, sets = 0;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long usec = 0;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!a/!a/!z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_ZERO(&efds);
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
if (r_array != NULL) sets += php_sock_array_to_fd_set(r_array, &rfds, &max_fd TSRMLS_CC);
|
|
|
|
if (w_array != NULL) sets += php_sock_array_to_fd_set(w_array, &wfds, &max_fd TSRMLS_CC);
|
2002-03-11 09:24:42 +08:00
|
|
|
if (e_array != NULL) sets += php_sock_array_to_fd_set(e_array, &efds, &max_fd TSRMLS_CC);
|
|
|
|
|
2001-07-26 06:03:55 +08:00
|
|
|
if (!sets) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no resource arrays were passed to select");
|
2001-07-26 06:03:55 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-09-17 20:44:56 +08:00
|
|
|
PHP_SAFE_MAX_FD(max_fd, 0); /* someone needs to make this look more like stream_socket_select */
|
|
|
|
|
2002-03-09 12:37:54 +08:00
|
|
|
/* If seconds is not set to null, build the timeval, else we wait indefinitely */
|
|
|
|
if (sec != NULL) {
|
2003-04-04 22:16:59 +08:00
|
|
|
zval tmp;
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (Z_TYPE_P(sec) != IS_LONG) {
|
2003-04-04 22:16:59 +08:00
|
|
|
tmp = *sec;
|
|
|
|
zval_copy_ctor(&tmp);
|
2014-08-26 03:51:49 +08:00
|
|
|
convert_to_long(&tmp);
|
2003-04-04 22:16:59 +08:00
|
|
|
sec = &tmp;
|
|
|
|
}
|
2003-07-22 15:20:55 +08:00
|
|
|
|
2007-07-23 06:47:18 +08:00
|
|
|
/* Solaris + BSD do not like microsecond values which are >= 1 sec */
|
2003-07-22 15:20:55 +08:00
|
|
|
if (usec > 999999) {
|
2014-08-26 01:24:55 +08:00
|
|
|
tv.tv_sec = Z_LVAL_P(sec) + (usec / 1000000);
|
2003-07-22 15:20:55 +08:00
|
|
|
tv.tv_usec = usec % 1000000;
|
|
|
|
} else {
|
2014-08-26 01:24:55 +08:00
|
|
|
tv.tv_sec = Z_LVAL_P(sec);
|
2003-07-22 15:20:55 +08:00
|
|
|
tv.tv_usec = usec;
|
2007-07-23 06:47:18 +08:00
|
|
|
}
|
2003-07-22 15:20:55 +08:00
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
tv_p = &tv;
|
2003-04-04 22:16:59 +08:00
|
|
|
|
|
|
|
if (sec == &tmp) {
|
|
|
|
zval_dtor(&tmp);
|
|
|
|
}
|
2002-03-11 09:24:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
|
2002-03-09 12:37:54 +08:00
|
|
|
|
|
|
|
if (retval == -1) {
|
2002-04-29 22:52:29 +08:00
|
|
|
SOCKETS_G(last_error) = errno;
|
2012-11-02 03:38:42 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to select [%d]: %s", errno, sockets_strerror(errno TSRMLS_CC));
|
2002-03-09 12:37:54 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds TSRMLS_CC);
|
|
|
|
if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds TSRMLS_CC);
|
2002-04-29 23:06:48 +08:00
|
|
|
if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds TSRMLS_CC);
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
RETURN_LONG(retval);
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto resource socket_create_listen(int port[, int backlog])
|
2000-08-20 18:31:27 +08:00
|
|
|
Opens a socket on port to accept connections */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_create_listen)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2002-04-29 23:06:48 +08:00
|
|
|
php_socket *php_sock;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long port, backlog = 128;
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &port, &backlog) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
if (!php_open_listen_sock(&php_sock, port, backlog TSRMLS_CC)) {
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2005-02-15 06:16:06 +08:00
|
|
|
php_sock->error = 0;
|
2007-07-23 07:01:20 +08:00
|
|
|
php_sock->blocking = 1;
|
2005-02-15 06:16:06 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
/* }}} */
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto resource socket_accept(resource socket)
|
2000-08-20 18:31:27 +08:00
|
|
|
Accepts a connection on the listening socket fd */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_accept)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2011-03-15 06:59:05 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock, *new_sock;
|
|
|
|
php_sockaddr_storage sa;
|
2011-10-14 06:56:05 +08:00
|
|
|
socklen_t php_sa_len = sizeof(sa);
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2011-10-14 06:56:05 +08:00
|
|
|
if (!php_accept_connect(php_sock, &new_sock, (struct sockaddr*)&sa, &php_sa_len TSRMLS_CC)) {
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2005-02-15 06:16:06 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, new_sock, le_socket);
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto bool socket_set_nonblock(resource socket)
|
2002-03-06 13:21:56 +08:00
|
|
|
Sets nonblocking mode on a socket resource */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_set_nonblock)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
if (!Z_ISUNDEF(php_sock->zstream)) {
|
2011-03-22 08:44:23 +08:00
|
|
|
php_stream *stream;
|
|
|
|
/* omit notice if resource doesn't exist anymore */
|
|
|
|
stream = zend_fetch_resource(&php_sock->zstream TSRMLS_CC, -1,
|
|
|
|
NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
|
|
|
|
if (stream != NULL) {
|
|
|
|
if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 0,
|
|
|
|
NULL) != -1) {
|
2013-02-01 23:38:54 +08:00
|
|
|
php_sock->blocking = 0;
|
2011-03-22 08:44:23 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2007-07-23 07:01:20 +08:00
|
|
|
if (php_set_sock_blocking(php_sock->bsd_socket, 0 TSRMLS_CC) == SUCCESS) {
|
|
|
|
php_sock->blocking = 0;
|
2002-03-06 13:21:56 +08:00
|
|
|
RETURN_TRUE;
|
2011-03-22 08:44:23 +08:00
|
|
|
} else {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to set nonblocking mode", errno);
|
|
|
|
RETURN_FALSE;
|
2002-03-06 13:21:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto bool socket_set_block(resource socket)
|
2002-03-06 13:21:56 +08:00
|
|
|
Sets blocking mode on a socket resource */
|
|
|
|
PHP_FUNCTION(socket_set_block)
|
|
|
|
{
|
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
|
2002-03-06 13:21:56 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
/* if socket was created from a stream, give the stream a chance to take
|
|
|
|
* care of the operation itself, thereby allowing it to update its internal
|
|
|
|
* state */
|
2014-05-17 10:23:46 +08:00
|
|
|
if (!Z_ISUNDEF(php_sock->zstream)) {
|
2011-03-22 08:44:23 +08:00
|
|
|
php_stream *stream;
|
|
|
|
stream = zend_fetch_resource(&php_sock->zstream TSRMLS_CC, -1,
|
|
|
|
NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
|
|
|
|
if (stream != NULL) {
|
|
|
|
if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 1,
|
|
|
|
NULL) != -1) {
|
|
|
|
php_sock->blocking = 1;
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2007-07-23 07:01:20 +08:00
|
|
|
if (php_set_sock_blocking(php_sock->bsd_socket, 1 TSRMLS_CC) == SUCCESS) {
|
|
|
|
php_sock->blocking = 1;
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2011-03-22 08:44:23 +08:00
|
|
|
} else {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to set blocking mode", errno);
|
|
|
|
RETURN_FALSE;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto bool socket_listen(resource socket[, int backlog])
|
2000-07-08 23:13:31 +08:00
|
|
|
Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_listen)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long backlog = 0;
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &backlog) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-09-19 23:12:30 +08:00
|
|
|
if (listen(php_sock->bsd_socket, backlog) != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto void socket_close(resource socket)
|
2000-08-23 21:47:00 +08:00
|
|
|
Closes a file descriptor */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_close)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
|
|
|
if (!Z_ISUNDEF(php_sock->zstream)) {
|
2011-03-22 08:44:23 +08:00
|
|
|
php_stream *stream = NULL;
|
|
|
|
php_stream_from_zval_no_verify(stream, &php_sock->zstream);
|
|
|
|
if (stream != NULL) {
|
|
|
|
/* close & destroy stream, incl. removing it from the rsrc list;
|
|
|
|
* resource stored in php_sock->zstream will become invalid */
|
|
|
|
php_stream_free(stream, PHP_STREAM_FREE_CLOSE |
|
|
|
|
(stream->is_persistent?PHP_STREAM_FREE_CLOSE_PERSISTENT:0));
|
|
|
|
}
|
|
|
|
}
|
2014-05-17 10:23:46 +08:00
|
|
|
zend_list_close(Z_RES_P(arg1));
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto int socket_write(resource socket, string buf[, int length])
|
2002-05-02 00:38:22 +08:00
|
|
|
Writes the buffer to the socket resource, length is optional */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_write)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
2014-08-28 01:25:28 +08:00
|
|
|
int retval;
|
|
|
|
size_t str_len;
|
|
|
|
zend_long length = 0;
|
2001-11-02 00:51:59 +08:00
|
|
|
char *str;
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &str, &str_len, &length) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2002-12-20 23:44:51 +08:00
|
|
|
if (ZEND_NUM_ARGS() < 3) {
|
2001-11-02 00:51:59 +08:00
|
|
|
length = str_len;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
#ifndef PHP_WIN32
|
2001-11-02 00:51:59 +08:00
|
|
|
retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
|
2001-05-18 01:02:37 +08:00
|
|
|
#else
|
2001-11-02 00:51:59 +08:00
|
|
|
retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
|
2001-05-18 01:02:37 +08:00
|
|
|
#endif
|
2000-10-22 14:45:03 +08:00
|
|
|
|
2001-11-29 16:09:39 +08:00
|
|
|
if (retval < 0) {
|
2002-05-03 00:45:22 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
|
2001-06-26 12:19:39 +08:00
|
|
|
RETURN_FALSE;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2000-10-22 14:45:03 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
RETURN_LONG(retval);
|
2000-09-16 07:44:30 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
/* }}} */
|
2000-09-16 07:44:30 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto string socket_read(resource socket, int length [, int type])
|
2002-05-02 00:38:22 +08:00
|
|
|
Reads a maximum of length bytes from socket */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_read)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
2014-05-17 10:23:46 +08:00
|
|
|
zend_string *tmpbuf;
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long length, type = PHP_BINARY_READ;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &arg1, &length, &type) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2004-02-26 06:10:09 +08:00
|
|
|
/* overflow check */
|
|
|
|
if ((length + 1) < 2) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
tmpbuf = zend_string_alloc(length, 0);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
if (type == PHP_NORMAL_READ) {
|
2014-05-17 10:23:46 +08:00
|
|
|
retval = php_read(php_sock, tmpbuf->val, length, 0);
|
2002-12-09 21:35:57 +08:00
|
|
|
} else {
|
2014-05-17 10:23:46 +08:00
|
|
|
retval = recv(php_sock->bsd_socket, tmpbuf->val, length, 0);
|
2000-10-23 07:43:48 +08:00
|
|
|
}
|
|
|
|
|
2001-11-29 16:09:39 +08:00
|
|
|
if (retval == -1) {
|
2005-12-05 01:30:43 +08:00
|
|
|
/* if the socket is in non-blocking mode and there's no data to read,
|
|
|
|
don't output any error, as this is a normal situation, and not an error */
|
|
|
|
if (errno == EAGAIN
|
|
|
|
#ifdef EWOULDBLOCK
|
|
|
|
|| errno == EWOULDBLOCK
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
php_sock->error = errno;
|
|
|
|
SOCKETS_G(last_error) = errno;
|
|
|
|
} else {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(tmpbuf);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2009-02-04 03:22:55 +08:00
|
|
|
} else if (!retval) {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(tmpbuf);
|
2009-02-04 03:22:55 +08:00
|
|
|
RETURN_EMPTY_STRING();
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
tmpbuf = zend_string_realloc(tmpbuf, retval, 0);
|
2014-05-17 10:23:46 +08:00
|
|
|
tmpbuf->len = retval;
|
|
|
|
tmpbuf->val[tmpbuf->len] = '\0' ;
|
2001-11-29 16:18:50 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
RETURN_STR(tmpbuf);
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_getsockname(resource socket, string &addr[, int &port])
|
2002-05-02 00:08:50 +08:00
|
|
|
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. */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_getsockname)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1, *addr, *port = NULL;
|
|
|
|
php_sockaddr_storage sa_storage;
|
|
|
|
php_socket *php_sock;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
struct sockaddr_in *sin;
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
char addr6[INET6_ADDRSTRLEN+1];
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
struct sockaddr_un *s_un;
|
|
|
|
char *addr_string;
|
2002-01-27 15:06:09 +08:00
|
|
|
socklen_t salen = sizeof(php_sockaddr_storage);
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-06-05 20:04:11 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/|z/", &arg1, &addr, &port) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2000-10-22 15:54:49 +08:00
|
|
|
sa = (struct sockaddr *) &sa_storage;
|
|
|
|
|
2001-09-05 06:27:30 +08:00
|
|
|
if (getsockname(php_sock->bsd_socket, sa, &salen) != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket name", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2001-11-02 00:51:59 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-05-18 12:02:05 +08:00
|
|
|
if (port != NULL) {
|
|
|
|
ZVAL_DEREF(port);
|
|
|
|
}
|
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
switch (sa->sa_family) {
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
case AF_INET6:
|
|
|
|
sin6 = (struct sockaddr_in6 *) sa;
|
2007-07-23 06:47:18 +08:00
|
|
|
inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
|
2003-04-18 05:04:47 +08:00
|
|
|
zval_dtor(addr);
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STRING(addr, addr6);
|
2003-04-18 05:04:47 +08:00
|
|
|
|
|
|
|
if (port != NULL) {
|
|
|
|
zval_dtor(port);
|
2014-08-26 01:24:55 +08:00
|
|
|
ZVAL_LONG(port, htons(sin6->sin6_port));
|
2003-04-18 05:04:47 +08:00
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
|
|
|
break;
|
|
|
|
#endif
|
2001-06-26 12:19:39 +08:00
|
|
|
case AF_INET:
|
|
|
|
sin = (struct sockaddr_in *) sa;
|
|
|
|
while (inet_ntoa_lock == 1);
|
|
|
|
inet_ntoa_lock = 1;
|
|
|
|
addr_string = inet_ntoa(sin->sin_addr);
|
|
|
|
inet_ntoa_lock = 0;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(addr);
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STRING(addr, addr_string);
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (port != NULL) {
|
|
|
|
zval_dtor(port);
|
2014-08-26 01:24:55 +08:00
|
|
|
ZVAL_LONG(port, htons(sin->sin_port));
|
2001-11-02 00:51:59 +08:00
|
|
|
}
|
2001-06-26 12:19:39 +08:00
|
|
|
RETURN_TRUE;
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
case AF_UNIX:
|
|
|
|
s_un = (struct sockaddr_un *) sa;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
zval_dtor(addr);
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STRING(addr, s_un->sun_path);
|
2001-06-26 12:19:39 +08:00
|
|
|
RETURN_TRUE;
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
default:
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
|
2001-06-26 12:19:39 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_getpeername(resource socket, string &addr[, int &port])
|
2002-05-02 00:08:50 +08:00
|
|
|
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. */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_getpeername)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1, *arg2, *arg3 = NULL;
|
|
|
|
php_sockaddr_storage sa_storage;
|
|
|
|
php_socket *php_sock;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
struct sockaddr_in *sin;
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
char addr6[INET6_ADDRSTRLEN+1];
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
struct sockaddr_un *s_un;
|
|
|
|
char *addr_string;
|
2002-01-27 15:06:09 +08:00
|
|
|
socklen_t salen = sizeof(php_sockaddr_storage);
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-06-05 20:04:11 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/|z/", &arg1, &arg2, &arg3) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2000-10-22 15:54:49 +08:00
|
|
|
sa = (struct sockaddr *) &sa_storage;
|
|
|
|
|
2001-09-05 06:27:30 +08:00
|
|
|
if (getpeername(php_sock->bsd_socket, sa, &salen) < 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to retrieve peer name", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2001-06-26 12:19:39 +08:00
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
switch (sa->sa_family) {
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
case AF_INET6:
|
|
|
|
sin6 = (struct sockaddr_in6 *) sa;
|
2007-07-23 06:47:18 +08:00
|
|
|
inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
|
2003-04-18 05:04:47 +08:00
|
|
|
zval_dtor(arg2);
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STRING(arg2, addr6);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
if (arg3 != NULL) {
|
|
|
|
zval_dtor(arg3);
|
2014-08-26 01:24:55 +08:00
|
|
|
ZVAL_LONG(arg3, htons(sin6->sin6_port));
|
2003-04-18 05:04:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_TRUE;
|
|
|
|
break;
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
case AF_INET:
|
|
|
|
sin = (struct sockaddr_in *) sa;
|
|
|
|
while (inet_ntoa_lock == 1);
|
|
|
|
inet_ntoa_lock = 1;
|
|
|
|
addr_string = inet_ntoa(sin->sin_addr);
|
|
|
|
inet_ntoa_lock = 0;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(arg2);
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STRING(arg2, addr_string);
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (arg3 != NULL) {
|
|
|
|
zval_dtor(arg3);
|
2014-08-26 01:24:55 +08:00
|
|
|
ZVAL_LONG(arg3, htons(sin->sin_port));
|
2001-11-02 00:51:59 +08:00
|
|
|
}
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_TRUE;
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
case AF_UNIX:
|
|
|
|
s_un = (struct sockaddr_un *) sa;
|
|
|
|
|
|
|
|
zval_dtor(arg2);
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STRING(arg2, s_un->sun_path);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_TRUE;
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
default:
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto resource socket_create(int domain, int type, int protocol)
|
2000-07-06 02:33:32 +08:00
|
|
|
Creates an endpoint for communication in the domain specified by domain, of type specified by type */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_create)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long arg1, arg2, arg3;
|
2011-03-22 08:44:23 +08:00
|
|
|
php_socket *php_sock = php_create_socket();
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &arg1, &arg2, &arg3) == FAILURE) {
|
2001-11-12 09:07:17 +08:00
|
|
|
efree(php_sock);
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 07:01:20 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2007-07-23 06:47:18 +08:00
|
|
|
if (arg1 != AF_UNIX
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
&& arg1 != AF_INET6
|
|
|
|
#endif
|
|
|
|
&& arg1 != AF_INET) {
|
2014-08-19 19:22:27 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%pd] specified for argument 1, assuming AF_INET", arg1);
|
2001-11-02 00:51:59 +08:00
|
|
|
arg1 = AF_INET;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (arg2 > 10) {
|
2014-08-19 19:22:27 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%pd] specified for argument 2, assuming SOCK_STREAM", arg2);
|
2001-11-02 00:51:59 +08:00
|
|
|
arg2 = SOCK_STREAM;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
php_sock->bsd_socket = socket(arg1, arg2, arg3);
|
|
|
|
php_sock->type = arg1;
|
|
|
|
|
2001-07-26 06:03:55 +08:00
|
|
|
if (IS_INVALID_SOCKET(php_sock)) {
|
2002-04-29 22:52:29 +08:00
|
|
|
SOCKETS_G(last_error) = errno;
|
2012-11-02 03:38:42 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno TSRMLS_CC));
|
2001-10-30 14:40:06 +08:00
|
|
|
efree(php_sock);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2005-02-15 07:44:23 +08:00
|
|
|
php_sock->error = 0;
|
2007-07-23 07:01:20 +08:00
|
|
|
php_sock->blocking = 1;
|
2005-02-15 07:44:23 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_connect(resource socket, string addr [, int port])
|
|
|
|
Opens a connection to addr:port on the socket specified by socket */
|
|
|
|
PHP_FUNCTION(socket_connect)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
|
|
|
char *addr;
|
2014-08-28 01:25:28 +08:00
|
|
|
int retval;
|
|
|
|
size_t addr_len;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long port = 0;
|
2003-04-18 05:04:47 +08:00
|
|
|
int argc = ZEND_NUM_ARGS();
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(argc TSRMLS_CC, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2000-08-23 16:29:16 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
switch(php_sock->type) {
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2011-06-12 04:20:50 +08:00
|
|
|
case AF_INET6: {
|
|
|
|
struct sockaddr_in6 sin6 = {0};
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
if (argc != 3) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET6 requires 3 arguments");
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2007-02-26 06:59:32 +08:00
|
|
|
memset(&sin6, 0, sizeof(struct sockaddr_in6));
|
2003-04-18 05:04:47 +08:00
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_port = htons((unsigned short int)port);
|
2003-04-18 05:04:47 +08:00
|
|
|
|
|
|
|
if (! php_set_inet6_addr(&sin6, addr, php_sock TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6));
|
|
|
|
break;
|
2011-06-12 04:20:50 +08:00
|
|
|
}
|
2003-04-18 05:04:47 +08:00
|
|
|
#endif
|
2011-06-12 04:20:50 +08:00
|
|
|
case AF_INET: {
|
|
|
|
struct sockaddr_in sin = {0};
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
if (argc != 3) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET requires 3 arguments");
|
2001-06-26 12:19:39 +08:00
|
|
|
RETURN_FALSE;
|
2000-08-23 14:41:51 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons((unsigned short int)port);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2002-03-04 13:27:04 +08:00
|
|
|
if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
2001-11-02 00:51:59 +08:00
|
|
|
}
|
2002-03-04 13:27:04 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
|
|
|
|
break;
|
2011-06-12 04:20:50 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2011-06-12 04:20:50 +08:00
|
|
|
case AF_UNIX: {
|
|
|
|
struct sockaddr_un s_un = {0};
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-05-24 08:05:50 +08:00
|
|
|
if (addr_len >= sizeof(s_un.sun_path)) {
|
2011-05-24 08:10:00 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Path too long");
|
2011-05-24 08:05:50 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
s_un.sun_family = AF_UNIX;
|
2008-07-16 22:08:38 +08:00
|
|
|
memcpy(&s_un.sun_path, addr, addr_len);
|
2011-06-12 04:20:50 +08:00
|
|
|
retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un,
|
|
|
|
(socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len));
|
2001-11-02 00:51:59 +08:00
|
|
|
break;
|
2011-06-12 04:20:50 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
default:
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
2007-07-23 06:47:18 +08:00
|
|
|
}
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
if (retval != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2000-08-23 14:41:51 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-05-22 00:55:27 +08:00
|
|
|
RETURN_TRUE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto string socket_strerror(int errno)
|
2000-07-03 12:35:57 +08:00
|
|
|
Returns a string describing an error */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_strerror)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long arg1;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &arg1) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-05-09 12:54:46 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
RETURN_STRING(sockets_strerror(arg1 TSRMLS_CC));
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_bind(resource socket, string addr [, int port])
|
|
|
|
Binds an open socket to a listening port, port is only specified in AF_INET family. */
|
|
|
|
PHP_FUNCTION(socket_bind)
|
2000-07-03 12:35:57 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
2013-07-15 07:51:15 +08:00
|
|
|
php_sockaddr_storage sa_storage = {0};
|
2001-11-02 00:51:59 +08:00
|
|
|
struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
|
|
|
|
php_socket *php_sock;
|
|
|
|
char *addr;
|
2014-08-27 21:31:48 +08:00
|
|
|
size_t addr_len;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long port = 0;
|
|
|
|
zend_long retval = 0;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
switch(php_sock->type) {
|
|
|
|
case AF_UNIX:
|
|
|
|
{
|
|
|
|
struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
|
2013-07-15 07:51:15 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
sa->sun_family = AF_UNIX;
|
2013-07-15 07:51:15 +08:00
|
|
|
|
|
|
|
if (addr_len >= sizeof(sa->sun_path)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING,
|
|
|
|
"Invalid path: too long (maximum size is %d)",
|
|
|
|
(int)sizeof(sa->sun_path) - 1);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
memcpy(&sa->sun_path, addr, addr_len);
|
|
|
|
|
|
|
|
retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa,
|
|
|
|
offsetof(struct sockaddr_un, sun_path) + addr_len);
|
2001-11-12 08:58:16 +08:00
|
|
|
break;
|
2001-11-02 00:51:59 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
case AF_INET:
|
|
|
|
{
|
2002-03-04 13:27:04 +08:00
|
|
|
struct sockaddr_in *sa = (struct sockaddr_in *) sock_type;
|
|
|
|
|
|
|
|
sa->sin_family = AF_INET;
|
|
|
|
sa->sin_port = htons((unsigned short) port);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
if (! php_set_inet_addr(sa, addr, php_sock TSRMLS_CC)) {
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2002-05-08 02:47:17 +08:00
|
|
|
retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
|
2001-11-12 08:58:16 +08:00
|
|
|
break;
|
2001-11-02 00:51:59 +08:00
|
|
|
}
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
|
|
|
|
|
|
|
|
sa->sin6_family = AF_INET6;
|
|
|
|
sa->sin6_port = htons((unsigned short) port);
|
|
|
|
|
|
|
|
if (! php_set_inet6_addr(sa, addr, php_sock TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in6));
|
|
|
|
break;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
default:
|
2003-04-18 05:04:47 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6", php_sock->type);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
if (retval != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to bind address", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_TRUE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
/* {{{ proto int socket_recv(resource socket, string &buf, int len, int flags)
|
2000-08-23 21:47:00 +08:00
|
|
|
Receives data from a connected socket */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_recv)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2002-03-06 13:21:56 +08:00
|
|
|
zval *php_sock_res, *buf;
|
2014-05-17 10:23:46 +08:00
|
|
|
zend_string *recv_buf;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long len, flags;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/ll", &php_sock_res, &buf, &len, &flags) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, php_sock_res, -1, le_socket_name, le_socket);
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2004-02-26 06:10:09 +08:00
|
|
|
/* overflow check */
|
|
|
|
if ((len + 1) < 2) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
recv_buf = zend_string_alloc(len, 0);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
if ((retval = recv(php_sock->bsd_socket, recv_buf->val, len, flags)) < 1) {
|
2000-07-08 09:50:34 +08:00
|
|
|
efree(recv_buf);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
zval_dtor(buf);
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_NULL(buf);
|
2002-03-06 13:21:56 +08:00
|
|
|
} else {
|
2014-05-17 10:23:46 +08:00
|
|
|
recv_buf->len = retval;
|
|
|
|
recv_buf->val[recv_buf->len] = '\0';
|
2002-03-06 13:21:56 +08:00
|
|
|
|
|
|
|
/* Rebuild buffer zval */
|
|
|
|
zval_dtor(buf);
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STR(buf, recv_buf);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
|
|
|
if (retval == -1) {
|
2002-04-29 23:06:48 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
|
2002-03-09 12:37:54 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
RETURN_LONG(retval);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto int socket_send(resource socket, string buf, int len, int flags)
|
2000-08-23 21:47:00 +08:00
|
|
|
Sends data to a connected socket */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_send)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
2014-08-27 21:31:48 +08:00
|
|
|
size_t buf_len, retval;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long len, flags;
|
2001-11-02 00:51:59 +08:00
|
|
|
char *buf;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsll", &arg1, &buf, &buf_len, &len, &flags) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
retval = send(php_sock->bsd_socket, buf, (buf_len < len ? buf_len : len), flags);
|
2002-03-09 12:37:54 +08:00
|
|
|
|
|
|
|
if (retval == -1) {
|
2002-04-29 23:06:48 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
|
|
|
|
RETURN_FALSE;
|
2002-03-09 12:37:54 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
RETURN_LONG(retval);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto int socket_recvfrom(resource socket, string &buf, int len, int flags, string &name [, int &port])
|
2000-08-23 21:47:00 +08:00
|
|
|
Receives data from a socket, connected or not */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_recvfrom)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1, *arg2, *arg5, *arg6 = NULL;
|
|
|
|
php_socket *php_sock;
|
|
|
|
struct sockaddr_un s_un;
|
|
|
|
struct sockaddr_in sin;
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
char addr6[INET6_ADDRSTRLEN];
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
socklen_t slen;
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long arg3, arg4;
|
2014-05-17 10:23:46 +08:00
|
|
|
char *address;
|
|
|
|
zend_string *recv_buf;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/llz/|z/", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2004-02-26 06:10:09 +08:00
|
|
|
/* overflow check */
|
|
|
|
if ((arg3 + 2) < 3) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
recv_buf = zend_string_alloc(arg3 + 1, 0);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
switch (php_sock->type) {
|
|
|
|
case AF_UNIX:
|
|
|
|
slen = sizeof(s_un);
|
|
|
|
s_un.sun_family = AF_UNIX;
|
2014-05-17 10:23:46 +08:00
|
|
|
retval = recvfrom(php_sock->bsd_socket, recv_buf->val, arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (retval < 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(recv_buf);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2014-05-17 10:23:46 +08:00
|
|
|
recv_buf->len = retval;
|
|
|
|
recv_buf->val[recv_buf->len] = '\0';
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(arg2);
|
|
|
|
zval_dtor(arg5);
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STR(arg2, recv_buf);
|
|
|
|
ZVAL_STRING(arg5, s_un.sun_path);
|
2001-11-02 00:51:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
slen = sizeof(sin);
|
2002-08-28 14:15:13 +08:00
|
|
|
memset(&sin, 0, slen);
|
2001-11-02 00:51:59 +08:00
|
|
|
sin.sin_family = AF_INET;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (arg6 == NULL) {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(recv_buf);
|
2001-11-02 00:51:59 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
retval = recvfrom(php_sock->bsd_socket, recv_buf->val, arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (retval < 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(recv_buf);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2014-05-17 10:23:46 +08:00
|
|
|
recv_buf->len = retval;
|
|
|
|
recv_buf->val[recv_buf->len] = '\0';
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(arg2);
|
|
|
|
zval_dtor(arg5);
|
|
|
|
zval_dtor(arg6);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
address = inet_ntoa(sin.sin_addr);
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STR(arg2, recv_buf);
|
|
|
|
ZVAL_STRING(arg5, address ? address : "0.0.0.0");
|
2014-08-26 01:24:55 +08:00
|
|
|
ZVAL_LONG(arg6, ntohs(sin.sin_port));
|
2001-11-02 00:51:59 +08:00
|
|
|
break;
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
case AF_INET6:
|
|
|
|
slen = sizeof(sin6);
|
|
|
|
memset(&sin6, 0, slen);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
if (arg6 == NULL) {
|
2009-05-10 09:03:45 +08:00
|
|
|
efree(recv_buf);
|
2003-04-18 05:04:47 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
retval = recvfrom(php_sock->bsd_socket, recv_buf->val, arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
if (retval < 0) {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(recv_buf);
|
2003-04-18 05:04:47 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2014-05-17 10:23:46 +08:00
|
|
|
recv_buf->len = retval;
|
|
|
|
recv_buf->val[recv_buf->len] = '\0';
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
zval_dtor(arg2);
|
|
|
|
zval_dtor(arg5);
|
|
|
|
zval_dtor(arg6);
|
|
|
|
|
2009-06-05 02:17:43 +08:00
|
|
|
memset(addr6, 0, INET6_ADDRSTRLEN);
|
2003-04-18 05:04:47 +08:00
|
|
|
inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_STR(arg2, recv_buf);
|
|
|
|
ZVAL_STRING(arg5, addr6[0] ? addr6 : "::");
|
2014-08-26 01:24:55 +08:00
|
|
|
ZVAL_LONG(arg6, ntohs(sin6.sin6_port));
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
default:
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
RETURN_LONG(retval);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto int socket_sendto(resource socket, string buf, int len, int flags, string addr [, int port])
|
2000-07-08 23:13:31 +08:00
|
|
|
Sends a message to a socket, whether it is connected or not */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_sendto)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
|
|
|
struct sockaddr_un s_un;
|
|
|
|
struct sockaddr_in sin;
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
#endif
|
2014-08-28 01:25:28 +08:00
|
|
|
int retval;
|
|
|
|
size_t buf_len, addr_len;
|
|
|
|
zend_long len, flags, port = 0;
|
2001-11-02 00:51:59 +08:00
|
|
|
char *buf, *addr;
|
2003-04-18 05:04:47 +08:00
|
|
|
int argc = ZEND_NUM_ARGS();
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(argc TSRMLS_CC, "rslls|l", &arg1, &buf, &buf_len, &len, &flags, &addr, &addr_len, &port) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
switch (php_sock->type) {
|
2000-08-20 18:31:27 +08:00
|
|
|
case AF_UNIX:
|
2001-06-13 00:41:51 +08:00
|
|
|
memset(&s_un, 0, sizeof(s_un));
|
|
|
|
s_un.sun_family = AF_UNIX;
|
2001-11-02 00:51:59 +08:00
|
|
|
snprintf(s_un.sun_path, 108, "%s", addr);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
|
|
|
|
break;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2000-08-20 18:31:27 +08:00
|
|
|
case AF_INET:
|
2003-04-18 05:04:47 +08:00
|
|
|
if (argc != 6) {
|
2001-06-13 00:41:51 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-06-13 00:41:51 +08:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
2002-03-04 13:27:04 +08:00
|
|
|
sin.sin_port = htons((unsigned short) port);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2002-03-04 13:27:04 +08:00
|
|
|
if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin, sizeof(sin));
|
2001-11-02 00:51:59 +08:00
|
|
|
break;
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
case AF_INET6:
|
|
|
|
if (argc != 6) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2001-06-13 00:41:51 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_port = htons((unsigned short) port);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
if (! php_set_inet6_addr(&sin6, addr, php_sock TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2005-12-01 22:02:54 +08:00
|
|
|
retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin6, sizeof(sin6));
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2000-08-20 18:31:27 +08:00
|
|
|
default:
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
|
2002-03-09 12:37:54 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == -1) {
|
2002-04-29 23:06:48 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
|
2002-03-09 12:37:54 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
RETURN_LONG(retval);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto mixed socket_get_option(resource socket, int level, int optname)
|
2000-07-08 23:13:31 +08:00
|
|
|
Gets socket options for the socket */
|
2002-05-01 19:08:08 +08:00
|
|
|
PHP_FUNCTION(socket_get_option)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
struct linger linger_val;
|
2007-07-23 06:47:18 +08:00
|
|
|
struct timeval tv;
|
2005-12-04 07:24:29 +08:00
|
|
|
#ifdef PHP_WIN32
|
2005-11-03 23:00:51 +08:00
|
|
|
int timeout = 0;
|
2005-12-04 07:24:29 +08:00
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
socklen_t optlen;
|
|
|
|
php_socket *php_sock;
|
2003-03-07 07:07:28 +08:00
|
|
|
int other_val;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long level, optname;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &arg1, &level, &optname) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2011-03-14 08:08:29 +08:00
|
|
|
if (level == IPPROTO_IP) {
|
|
|
|
switch (optname) {
|
|
|
|
case IP_MULTICAST_IF: {
|
|
|
|
struct in_addr if_addr;
|
|
|
|
unsigned int if_index;
|
|
|
|
optlen = sizeof(if_addr);
|
|
|
|
if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&if_addr, &optlen) != 0) {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
if (php_add4_to_if_index(&if_addr, php_sock, &if_index TSRMLS_CC) == SUCCESS) {
|
2014-08-26 17:23:25 +08:00
|
|
|
RETURN_LONG((zend_long) if_index);
|
2011-03-14 08:08:29 +08:00
|
|
|
} else {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 01:51:39 +08:00
|
|
|
}
|
|
|
|
#if HAVE_IPV6
|
|
|
|
else if (level == IPPROTO_IPV6) {
|
2013-01-02 06:38:19 +08:00
|
|
|
int ret = php_do_getsockopt_ipv6_rfc3542(php_sock, level, optname, return_value TSRMLS_CC);
|
2012-11-07 00:27:08 +08:00
|
|
|
if (ret == SUCCESS) {
|
|
|
|
return;
|
|
|
|
} else if (ret == FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
} /* else continue */
|
2011-03-14 08:08:29 +08:00
|
|
|
}
|
2013-05-09 01:51:39 +08:00
|
|
|
#endif
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-03-14 08:08:29 +08:00
|
|
|
/* sol_socket options and general case */
|
2001-12-06 12:44:23 +08:00
|
|
|
switch(optname) {
|
2007-07-23 06:47:18 +08:00
|
|
|
case SO_LINGER:
|
2001-12-06 12:44:23 +08:00
|
|
|
optlen = sizeof(linger_val);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&linger_val, &optlen) != 0) {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2003-01-14 10:39:10 +08:00
|
|
|
array_init(return_value);
|
2014-08-26 01:24:55 +08:00
|
|
|
add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
|
|
|
|
add_assoc_long(return_value, "l_linger", linger_val.l_linger);
|
2007-07-23 06:47:18 +08:00
|
|
|
break;
|
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
case SO_RCVTIMEO:
|
|
|
|
case SO_SNDTIMEO:
|
2005-11-03 23:00:51 +08:00
|
|
|
#ifndef PHP_WIN32
|
2001-12-06 12:44:23 +08:00
|
|
|
optlen = sizeof(tv);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
|
2001-12-06 12:44:23 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2005-11-03 23:00:51 +08:00
|
|
|
#else
|
|
|
|
optlen = sizeof(int);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2005-11-03 23:00:51 +08:00
|
|
|
if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&timeout, &optlen) != 0) {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2005-11-03 23:00:51 +08:00
|
|
|
tv.tv_sec = timeout ? timeout / 1000 : 0;
|
|
|
|
tv.tv_usec = timeout ? (timeout * 1000) % 1000000 : 0;
|
|
|
|
#endif
|
2001-12-06 12:44:23 +08:00
|
|
|
|
2003-01-14 10:39:10 +08:00
|
|
|
array_init(return_value);
|
2007-07-23 07:01:20 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
add_assoc_long(return_value, "sec", tv.tv_sec);
|
|
|
|
add_assoc_long(return_value, "usec", tv.tv_usec);
|
2001-12-06 12:44:23 +08:00
|
|
|
break;
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
default:
|
|
|
|
optlen = sizeof(other_val);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&other_val, &optlen) != 0) {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2011-03-14 08:08:29 +08:00
|
|
|
if (optlen == 1)
|
|
|
|
other_val = *((unsigned char *)&other_val);
|
2007-07-23 07:01:20 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
RETURN_LONG(other_val);
|
2001-12-06 12:44:23 +08:00
|
|
|
break;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-05-01 19:08:08 +08:00
|
|
|
/* {{{ proto bool socket_set_option(resource socket, int level, int optname, int|array optval)
|
2000-07-08 23:13:31 +08:00
|
|
|
Sets socket options for the socket */
|
2002-05-01 19:08:08 +08:00
|
|
|
PHP_FUNCTION(socket_set_option)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2014-05-17 10:23:46 +08:00
|
|
|
zval *arg1, *arg4;
|
2011-03-14 08:08:29 +08:00
|
|
|
struct linger lv;
|
|
|
|
php_socket *php_sock;
|
|
|
|
int ov, optlen, retval;
|
2005-12-04 07:24:29 +08:00
|
|
|
#ifdef PHP_WIN32
|
2011-03-14 08:08:29 +08:00
|
|
|
int timeout;
|
2007-07-23 06:47:18 +08:00
|
|
|
#else
|
2011-03-14 08:08:29 +08:00
|
|
|
struct timeval tv;
|
2005-12-04 07:24:29 +08:00
|
|
|
#endif
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long level, optname;
|
2011-03-14 08:08:29 +08:00
|
|
|
void *opt_ptr;
|
|
|
|
HashTable *opt_ht;
|
2014-05-17 10:23:46 +08:00
|
|
|
zval *l_onoff, *l_linger;
|
|
|
|
zval *sec, *usec;
|
2012-11-02 03:38:42 +08:00
|
|
|
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllz", &arg1, &level, &optname, &arg4) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, le_socket);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
set_errno(0);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2012-11-06 19:48:47 +08:00
|
|
|
#define HANDLE_SUBCALL(res) \
|
|
|
|
do { \
|
|
|
|
if (res == 1) { goto default_case; } \
|
|
|
|
else if (res == SUCCESS) { RETURN_TRUE; } \
|
|
|
|
else { RETURN_FALSE; } \
|
|
|
|
} while (0)
|
2011-03-14 08:08:29 +08:00
|
|
|
|
|
|
|
|
2012-11-06 19:48:47 +08:00
|
|
|
if (level == IPPROTO_IP) {
|
2013-01-02 06:38:19 +08:00
|
|
|
int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4 TSRMLS_CC);
|
2012-11-06 19:48:47 +08:00
|
|
|
HANDLE_SUBCALL(res);
|
2011-03-14 08:08:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_IPV6
|
|
|
|
else if (level == IPPROTO_IPV6) {
|
2013-01-02 06:38:19 +08:00
|
|
|
int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4 TSRMLS_CC);
|
2012-11-07 00:27:08 +08:00
|
|
|
if (res == 1) {
|
2013-01-02 06:38:19 +08:00
|
|
|
res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4 TSRMLS_CC);
|
2012-11-07 00:27:08 +08:00
|
|
|
}
|
2012-11-06 19:48:47 +08:00
|
|
|
HANDLE_SUBCALL(res);
|
2011-03-14 08:08:29 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
switch (optname) {
|
2011-03-14 08:08:29 +08:00
|
|
|
case SO_LINGER: {
|
|
|
|
const char l_onoff_key[] = "l_onoff";
|
|
|
|
const char l_linger_key[] = "l_linger";
|
|
|
|
|
2006-09-01 00:15:24 +08:00
|
|
|
convert_to_array_ex(arg4);
|
2014-05-17 10:23:46 +08:00
|
|
|
opt_ht = HASH_OF(arg4);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
if ((l_onoff = zend_hash_str_find(opt_ht, l_onoff_key, sizeof(l_onoff_key) - 1)) == NULL) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", l_onoff_key);
|
2001-12-06 12:44:23 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2014-05-17 10:23:46 +08:00
|
|
|
if ((l_linger = zend_hash_str_find(opt_ht, l_linger_key, sizeof(l_linger_key) - 1)) == NULL) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", l_linger_key);
|
2001-12-06 12:44:23 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-08-26 03:51:49 +08:00
|
|
|
convert_to_long_ex(l_onoff);
|
|
|
|
convert_to_long_ex(l_linger);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
lv.l_onoff = (unsigned short)Z_LVAL_P(l_onoff);
|
|
|
|
lv.l_linger = (unsigned short)Z_LVAL_P(l_linger);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
optlen = sizeof(lv);
|
2002-04-29 23:06:48 +08:00
|
|
|
opt_ptr = &lv;
|
2001-12-06 12:44:23 +08:00
|
|
|
break;
|
2011-03-14 08:08:29 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
case SO_RCVTIMEO:
|
2011-03-14 08:08:29 +08:00
|
|
|
case SO_SNDTIMEO: {
|
|
|
|
const char sec_key[] = "sec";
|
|
|
|
const char usec_key[] = "usec";
|
|
|
|
|
2006-09-01 00:15:24 +08:00
|
|
|
convert_to_array_ex(arg4);
|
2014-05-17 10:23:46 +08:00
|
|
|
opt_ht = HASH_OF(arg4);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
if ((sec = zend_hash_str_find(opt_ht, sec_key, sizeof(sec_key) - 1)) == NULL) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", sec_key);
|
2001-12-06 12:44:23 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2014-05-17 10:23:46 +08:00
|
|
|
if ((usec = zend_hash_str_find(opt_ht, usec_key, sizeof(usec_key) - 1)) == NULL) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", usec_key);
|
2001-12-06 12:44:23 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-08-26 03:51:49 +08:00
|
|
|
convert_to_long_ex(sec);
|
|
|
|
convert_to_long_ex(usec);
|
2005-11-03 23:00:51 +08:00
|
|
|
#ifndef PHP_WIN32
|
2014-08-26 01:24:55 +08:00
|
|
|
tv.tv_sec = Z_LVAL_P(sec);
|
|
|
|
tv.tv_usec = Z_LVAL_P(usec);
|
2002-04-29 23:06:48 +08:00
|
|
|
optlen = sizeof(tv);
|
|
|
|
opt_ptr = &tv;
|
2005-11-03 23:00:51 +08:00
|
|
|
#else
|
2014-08-26 01:24:55 +08:00
|
|
|
timeout = Z_LVAL_P(sec) * 1000 + Z_LVAL_P(usec) / 1000;
|
2005-11-03 23:00:51 +08:00
|
|
|
optlen = sizeof(int);
|
|
|
|
opt_ptr = &timeout;
|
|
|
|
#endif
|
2001-12-06 12:44:23 +08:00
|
|
|
break;
|
2011-03-14 08:08:29 +08:00
|
|
|
}
|
2013-06-10 18:11:14 +08:00
|
|
|
#ifdef SO_BINDTODEVICE
|
|
|
|
case SO_BINDTODEVICE: {
|
2014-05-17 10:23:46 +08:00
|
|
|
if (Z_TYPE_P(arg4) == IS_STRING) {
|
|
|
|
opt_ptr = Z_STRVAL_P(arg4);
|
2014-08-26 01:24:55 +08:00
|
|
|
optlen = Z_STRLEN_P(arg4);
|
2013-06-10 18:11:14 +08:00
|
|
|
} else {
|
|
|
|
opt_ptr = "";
|
|
|
|
optlen = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
default:
|
2012-11-06 19:48:47 +08:00
|
|
|
default_case:
|
2014-08-26 03:51:49 +08:00
|
|
|
convert_to_long_ex(arg4);
|
2014-08-26 01:24:55 +08:00
|
|
|
ov = Z_LVAL_P(arg4);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
optlen = sizeof(ov);
|
2002-04-29 23:06:48 +08:00
|
|
|
opt_ptr = &ov;
|
2001-12-06 12:44:23 +08:00
|
|
|
break;
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
|
2001-07-26 06:03:55 +08:00
|
|
|
if (retval != 0) {
|
2012-11-06 19:48:47 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to set socket option", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2000-08-20 18:31:27 +08:00
|
|
|
}
|
2000-09-14 07:33:39 +08:00
|
|
|
/* }}} */
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2009-01-23 23:49:49 +08:00
|
|
|
#ifdef HAVE_SOCKETPAIR
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto bool socket_create_pair(int domain, int type, int protocol, array &fd)
|
2000-09-14 07:33:39 +08:00
|
|
|
Creates a pair of indistinguishable sockets and stores them in fds. */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_create_pair)
|
2000-08-20 18:31:27 +08:00
|
|
|
{
|
2014-05-17 10:23:46 +08:00
|
|
|
zval retval[2], *fds_array_zval;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock[2];
|
2007-07-23 06:47:18 +08:00
|
|
|
PHP_SOCKET fds_array[2];
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long domain, type, protocol;
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lllz/", &domain, &type, &protocol, &fds_array_zval) == FAILURE) {
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
php_sock[0] = php_create_socket();
|
|
|
|
php_sock[1] = php_create_socket();
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2007-07-23 06:47:18 +08:00
|
|
|
if (domain != AF_INET
|
2003-12-06 08:00:31 +08:00
|
|
|
#if HAVE_IPV6
|
2003-04-18 05:04:47 +08:00
|
|
|
&& domain != AF_INET6
|
|
|
|
#endif
|
|
|
|
&& domain != AF_UNIX) {
|
2014-08-19 19:22:27 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%pd] specified for argument 1, assuming AF_INET", domain);
|
2001-11-02 00:51:59 +08:00
|
|
|
domain = AF_INET;
|
2000-08-20 18:31:27 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (type > 10) {
|
2014-08-19 19:22:27 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%pd] specified for argument 2, assuming SOCK_STREAM", type);
|
2001-11-02 00:51:59 +08:00
|
|
|
type = SOCK_STREAM;
|
2000-08-20 18:31:27 +08:00
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2002-05-01 18:41:10 +08:00
|
|
|
if (socketpair(domain, type, protocol, fds_array) != 0) {
|
|
|
|
SOCKETS_G(last_error) = errno;
|
2012-11-02 03:38:42 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to create socket pair [%d]: %s", errno, sockets_strerror(errno TSRMLS_CC));
|
2001-11-12 08:58:16 +08:00
|
|
|
efree(php_sock[0]);
|
|
|
|
efree(php_sock[1]);
|
2000-08-20 18:31:27 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2002-05-01 18:41:10 +08:00
|
|
|
zval_dtor(fds_array_zval);
|
2003-01-14 10:39:10 +08:00
|
|
|
array_init(fds_array_zval);
|
2000-09-14 07:33:39 +08:00
|
|
|
|
2001-09-05 06:27:30 +08:00
|
|
|
php_sock[0]->bsd_socket = fds_array[0];
|
|
|
|
php_sock[1]->bsd_socket = fds_array[1];
|
2001-11-02 00:51:59 +08:00
|
|
|
php_sock[0]->type = domain;
|
|
|
|
php_sock[1]->type = domain;
|
2005-02-15 07:44:23 +08:00
|
|
|
php_sock[0]->error = 0;
|
|
|
|
php_sock[1]->error = 0;
|
2007-07-23 07:01:20 +08:00
|
|
|
php_sock[0]->blocking = 1;
|
|
|
|
php_sock[1]->blocking = 1;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(&retval[0], php_sock[0], le_socket);
|
|
|
|
ZEND_REGISTER_RESOURCE(&retval[1], php_sock[1], le_socket);
|
2000-09-14 07:33:39 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
add_index_zval(fds_array_zval, 0, &retval[0]);
|
|
|
|
add_index_zval(fds_array_zval, 1, &retval[1]);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
|
|
|
RETURN_TRUE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2000-09-14 07:33:39 +08:00
|
|
|
/* }}} */
|
2005-09-23 17:54:31 +08:00
|
|
|
#endif
|
2000-09-14 07:33:39 +08:00
|
|
|
|
2007-01-10 23:25:07 +08:00
|
|
|
#ifdef HAVE_SHUTDOWN
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto bool socket_shutdown(resource socket[, int how])
|
2000-09-14 07:33:39 +08:00
|
|
|
Shuts down a socket for receiving, sending, or both. */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_shutdown)
|
2000-08-20 18:31:27 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long how_shutdown = 2;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &how_shutdown) == FAILURE) {
|
2002-04-29 23:06:48 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket*, arg1, -1, le_socket_name, le_socket);
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-09-05 06:27:30 +08:00
|
|
|
if (shutdown(php_sock->bsd_socket, how_shutdown) != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to shutdown socket", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2000-08-20 18:31:27 +08:00
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
/* }}} */
|
2007-01-10 23:25:07 +08:00
|
|
|
#endif
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto int socket_last_error([resource socket])
|
2002-06-25 14:24:45 +08:00
|
|
|
Returns the last socket error (either the last used or the provided socket resource) */
|
2001-11-02 00:51:59 +08:00
|
|
|
PHP_FUNCTION(socket_last_error)
|
|
|
|
{
|
2002-04-29 22:52:29 +08:00
|
|
|
zval *arg1 = NULL;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE) {
|
2002-04-29 22:52:29 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2002-04-29 22:52:29 +08:00
|
|
|
if (arg1) {
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket*, arg1, -1, le_socket_name, le_socket);
|
2014-08-26 01:24:55 +08:00
|
|
|
RETVAL_LONG(php_sock->error);
|
2002-04-29 22:52:29 +08:00
|
|
|
} else {
|
2014-08-26 01:24:55 +08:00
|
|
|
RETVAL_LONG(SOCKETS_G(last_error));
|
2002-04-29 22:52:29 +08:00
|
|
|
}
|
|
|
|
}
|
2002-03-06 13:21:56 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2014-05-17 10:23:46 +08:00
|
|
|
/* {{{ proto void socket_clear_error([resource socket])
|
2002-05-01 06:01:06 +08:00
|
|
|
Clears the error on the socket or the last error code. */
|
2002-03-06 13:21:56 +08:00
|
|
|
PHP_FUNCTION(socket_clear_error)
|
|
|
|
{
|
2002-05-01 06:01:06 +08:00
|
|
|
zval *arg1 = NULL;
|
2002-03-06 13:21:56 +08:00
|
|
|
php_socket *php_sock;
|
|
|
|
|
2007-07-23 06:22:09 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE) {
|
2002-04-29 23:06:48 +08:00
|
|
|
return;
|
2007-07-23 06:22:09 +08:00
|
|
|
}
|
2002-03-06 13:21:56 +08:00
|
|
|
|
2002-05-01 06:01:06 +08:00
|
|
|
if (arg1) {
|
2014-05-17 10:23:46 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket*, arg1, -1, le_socket_name, le_socket);
|
2002-05-01 06:01:06 +08:00
|
|
|
php_sock->error = 0;
|
|
|
|
} else {
|
|
|
|
SOCKETS_G(last_error) = 0;
|
|
|
|
}
|
2002-03-06 13:21:56 +08:00
|
|
|
|
|
|
|
return;
|
2007-07-23 06:47:18 +08:00
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
/* }}} */
|
2007-07-23 06:47:18 +08:00
|
|
|
|
2012-11-05 21:52:48 +08:00
|
|
|
php_socket *socket_import_file_descriptor(PHP_SOCKET socket TSRMLS_DC)
|
2011-03-22 08:44:23 +08:00
|
|
|
{
|
2012-11-05 21:52:48 +08:00
|
|
|
#ifdef SO_DOMAIN
|
|
|
|
int type;
|
|
|
|
socklen_t type_len = sizeof(type);
|
|
|
|
#endif
|
|
|
|
php_socket *retsock;
|
|
|
|
php_sockaddr_storage addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
2011-03-30 17:46:56 +08:00
|
|
|
#ifndef PHP_WIN32
|
2011-03-22 08:44:23 +08:00
|
|
|
int t;
|
2011-03-30 17:46:56 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-05 21:52:48 +08:00
|
|
|
retsock = php_create_socket();
|
|
|
|
retsock->bsd_socket = socket;
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2012-11-05 21:52:48 +08:00
|
|
|
/* determine family */
|
2012-10-23 19:09:38 +08:00
|
|
|
#ifdef SO_DOMAIN
|
2012-11-05 21:52:48 +08:00
|
|
|
if (getsockopt(socket, SOL_SOCKET, SO_DOMAIN, &type, &type_len) == 0) {
|
2012-10-23 19:09:38 +08:00
|
|
|
retsock->type = type;
|
|
|
|
} else
|
|
|
|
#endif
|
2011-03-22 08:44:23 +08:00
|
|
|
if (getsockname(socket, (struct sockaddr*)&addr, &addr_len) == 0) {
|
|
|
|
retsock->type = addr.ss_family;
|
|
|
|
} else {
|
|
|
|
PHP_SOCKET_ERROR(retsock, "unable to obtain socket family", errno);
|
|
|
|
goto error;
|
|
|
|
}
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2012-11-05 21:52:48 +08:00
|
|
|
/* determine blocking mode */
|
2011-03-22 08:44:23 +08:00
|
|
|
#ifndef PHP_WIN32
|
2012-11-05 21:52:48 +08:00
|
|
|
t = fcntl(socket, F_GETFL);
|
|
|
|
if (t == -1) {
|
2011-03-22 08:44:23 +08:00
|
|
|
PHP_SOCKET_ERROR(retsock, "unable to obtain blocking state", errno);
|
|
|
|
goto error;
|
2012-11-05 21:52:48 +08:00
|
|
|
} else {
|
|
|
|
retsock->blocking = !(t & O_NONBLOCK);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return retsock;
|
|
|
|
|
|
|
|
error:
|
|
|
|
efree(retsock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* {{{ proto void socket_import_stream(resource stream)
|
|
|
|
Imports a stream that encapsulates a socket into a socket extension resource. */
|
|
|
|
PHP_FUNCTION(socket_import_stream)
|
|
|
|
{
|
|
|
|
zval *zstream;
|
|
|
|
php_stream *stream;
|
|
|
|
php_socket *retsock = NULL;
|
|
|
|
PHP_SOCKET socket; /* fd */
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) == FAILURE) {
|
|
|
|
return;
|
2011-03-22 08:44:23 +08:00
|
|
|
}
|
2014-05-17 10:23:46 +08:00
|
|
|
php_stream_from_zval(stream, zstream);
|
2012-11-05 21:52:48 +08:00
|
|
|
|
|
|
|
if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) {
|
|
|
|
/* error supposedly already shown */
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2013-01-02 06:38:19 +08:00
|
|
|
retsock = socket_import_file_descriptor(socket TSRMLS_CC);
|
2012-11-05 21:52:48 +08:00
|
|
|
if (retsock == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
2011-03-22 08:44:23 +08:00
|
|
|
/* on windows, check if the stream is a socket stream and read its
|
|
|
|
* private data; otherwise assume it's in non-blocking mode */
|
|
|
|
if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) {
|
|
|
|
retsock->blocking =
|
2011-03-30 17:46:20 +08:00
|
|
|
((php_netstream_data_t *)stream->abstract)->is_blocked;
|
2011-03-22 08:44:23 +08:00
|
|
|
} else {
|
|
|
|
retsock->blocking = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
/* hold a zval reference to the stream (holding a php_stream* directly could
|
|
|
|
* also be done, but this might be slightly better if in the future we want
|
|
|
|
* to provide a socket_export_stream) */
|
2014-05-17 10:23:46 +08:00
|
|
|
ZVAL_COPY(&retsock->zstream, zstream);
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER,
|
|
|
|
PHP_STREAM_BUFFER_NONE, NULL);
|
2012-11-02 03:38:42 +08:00
|
|
|
|
2011-03-22 08:44:23 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, retsock, le_socket);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2000-07-03 12:35:57 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2002-12-02 06:15:39 +08:00
|
|
|
* vim600: fdm=marker
|
|
|
|
* vim: noet sw=4 ts=4
|
2001-05-22 00:55:27 +08:00
|
|
|
*/
|