2000-07-03 12:35:57 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| PHP Version 4 |
|
2000-07-03 12:35:57 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2003-01-01 00:08:15 +08:00
|
|
|
| Copyright (c) 1997-2003 The PHP Group |
|
2000-07-03 12:35:57 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2003-06-11 04:04:29 +08:00
|
|
|
| This source file is subject to version 3.0 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: |
|
|
|
|
| http://www.php.net/license/3_0.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> |
|
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"
|
|
|
|
|
2000-08-20 18:31:27 +08:00
|
|
|
#if HAVE_SOCKETS
|
|
|
|
|
2003-04-25 16:10:11 +08:00
|
|
|
#ifndef _XOPEN_SOURCE_EXTENDED
|
2000-10-22 14:45:03 +08:00
|
|
|
#define _XOPEN_SOURCE_EXTENDED
|
2003-04-25 16:10:11 +08:00
|
|
|
#endif
|
|
|
|
|
2001-01-04 02:06:10 +08:00
|
|
|
#define _XPG4_2
|
|
|
|
#define __EXTENSIONS__
|
2000-10-22 14:45:03 +08:00
|
|
|
|
2002-01-27 14:55:10 +08:00
|
|
|
#include "php_network.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"
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
#ifndef PHP_WIN32
|
|
|
|
# include "php_sockets.h"
|
|
|
|
# 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)
|
|
|
|
# define set_h_errno(a) (h_errno = a)
|
|
|
|
#else /* windows */
|
|
|
|
# include "php_sockets.h"
|
|
|
|
# include "php_sockets_win.h"
|
2001-09-05 06:27:30 +08:00
|
|
|
# define IS_INVALID_SOCKET(a) (a->bsd_socket == INVALID_SOCKET)
|
2001-05-18 01:02:37 +08:00
|
|
|
#endif
|
2000-07-03 12:35:57 +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
|
|
|
|
|
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
|
|
|
|
|
2003-05-28 09:04:06 +08:00
|
|
|
static char *php_strerror(int error TSRMLS_DC);
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2000-10-30 09:09:15 +08:00
|
|
|
#define PHP_NORMAL_READ 0x0001
|
|
|
|
#define PHP_BINARY_READ 0x0002
|
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
#define PHP_SOCKET_ERROR(socket,msg,errn) socket->error = errn; \
|
2003-01-14 10:39:10 +08:00
|
|
|
SOCKETS_G(last_error) = errn; \
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s [%d]: %s", msg, errn, php_strerror(errn TSRMLS_CC))
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
static int le_iov;
|
|
|
|
#define le_iov_name "Socket I/O vector"
|
|
|
|
static int le_socket;
|
|
|
|
#define le_socket_name "Socket"
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2002-05-01 12:46:59 +08:00
|
|
|
static unsigned char first_through_third_args_force_ref[] =
|
|
|
|
{3, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE};
|
|
|
|
|
2000-07-03 12:35:57 +08:00
|
|
|
static unsigned char second_and_third_args_force_ref[] =
|
|
|
|
{3, BYREF_NONE, BYREF_FORCE, BYREF_FORCE};
|
|
|
|
|
2002-03-09 14:41:19 +08:00
|
|
|
static unsigned char second_arg_of_four_force_ref[] =
|
|
|
|
{4, BYREF_NONE, BYREF_FORCE, BYREF_NONE, BYREF_NONE};
|
|
|
|
|
2002-05-01 18:41:10 +08:00
|
|
|
static unsigned char fourth_arg_force_ref[] =
|
2002-05-01 23:14:49 +08:00
|
|
|
{4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
|
2002-05-01 18:41:10 +08:00
|
|
|
|
2000-07-08 09:50:34 +08:00
|
|
|
static unsigned char second_fifth_and_sixth_args_force_ref[] =
|
|
|
|
{6, BYREF_NONE, BYREF_FORCE, BYREF_NONE, BYREF_NONE, BYREF_FORCE, BYREF_FORCE};
|
|
|
|
|
|
|
|
static unsigned char third_through_seventh_args_force_ref[] =
|
|
|
|
{7, BYREF_NONE, BYREF_NONE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE};
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ sockets_functions[]
|
|
|
|
*/
|
2000-08-20 18:31:27 +08:00
|
|
|
function_entry sockets_functions[] = {
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_iovec_alloc, NULL)
|
|
|
|
PHP_FE(socket_iovec_free, NULL)
|
|
|
|
PHP_FE(socket_iovec_set, NULL)
|
|
|
|
PHP_FE(socket_iovec_fetch, NULL)
|
|
|
|
PHP_FE(socket_iovec_add, NULL)
|
|
|
|
PHP_FE(socket_iovec_delete, NULL)
|
2002-05-01 15:35:01 +08:00
|
|
|
PHP_FE(socket_select, first_through_third_args_force_ref)
|
|
|
|
PHP_FE(socket_create, NULL)
|
|
|
|
PHP_FE(socket_create_listen, NULL)
|
2002-05-01 18:41:10 +08:00
|
|
|
PHP_FE(socket_create_pair, fourth_arg_force_ref)
|
2002-05-01 15:35:01 +08:00
|
|
|
PHP_FE(socket_accept, NULL)
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_set_nonblock, NULL)
|
2002-04-29 23:06:48 +08:00
|
|
|
PHP_FE(socket_set_block, NULL)
|
2002-05-01 15:35:01 +08:00
|
|
|
PHP_FE(socket_listen, NULL)
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_close, NULL)
|
2002-05-01 15:35:01 +08:00
|
|
|
PHP_FE(socket_write, NULL)
|
|
|
|
PHP_FE(socket_read, NULL)
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_getsockname, second_and_third_args_force_ref)
|
|
|
|
PHP_FE(socket_getpeername, second_and_third_args_force_ref)
|
2002-05-01 15:35:01 +08:00
|
|
|
PHP_FE(socket_connect, NULL)
|
|
|
|
PHP_FE(socket_strerror, NULL)
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_bind, NULL)
|
2002-03-09 14:41:19 +08:00
|
|
|
PHP_FE(socket_recv, second_arg_of_four_force_ref)
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_send, NULL)
|
|
|
|
PHP_FE(socket_recvfrom, second_fifth_and_sixth_args_force_ref)
|
|
|
|
PHP_FE(socket_sendto, NULL)
|
2003-04-04 21:02:43 +08:00
|
|
|
#ifdef HAVE_CMSGHDR
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_recvmsg, third_through_seventh_args_force_ref)
|
2003-04-04 21:02:43 +08:00
|
|
|
#endif
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_sendmsg, NULL)
|
|
|
|
PHP_FE(socket_readv, NULL)
|
|
|
|
PHP_FE(socket_writev, NULL)
|
2002-05-01 19:08:08 +08:00
|
|
|
PHP_FE(socket_get_option, NULL)
|
|
|
|
PHP_FE(socket_set_option, NULL)
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FE(socket_shutdown, NULL)
|
2001-11-02 00:51:59 +08:00
|
|
|
PHP_FE(socket_last_error, NULL)
|
2002-03-06 13:21:56 +08:00
|
|
|
PHP_FE(socket_clear_error, NULL)
|
2002-05-01 19:08:08 +08:00
|
|
|
|
|
|
|
/* for downwards compatability */
|
|
|
|
PHP_FALIAS(socket_getopt, socket_get_option, NULL)
|
|
|
|
PHP_FALIAS(socket_setopt, socket_set_option, NULL)
|
|
|
|
|
2000-08-20 18:31:27 +08:00
|
|
|
{NULL, NULL, NULL}
|
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),
|
2000-08-20 18:31:27 +08:00
|
|
|
NULL,
|
2002-09-30 10:09:42 +08:00
|
|
|
PHP_RINIT(sockets),
|
|
|
|
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,
|
2000-07-03 12:35:57 +08:00
|
|
|
STANDARD_MODULE_PROPERTIES
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
static void php_destroy_iovec(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-05-18 01:02:37 +08:00
|
|
|
unsigned int i;
|
2001-06-26 12:19:39 +08:00
|
|
|
php_iovec_t *iov = (php_iovec_t *) rsrc->ptr;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
|
|
|
if (iov->count && iov->iov_array) {
|
|
|
|
for (i = 0; i < iov->count; i++) {
|
|
|
|
efree(iov->iov_array[i].iov_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
efree(iov->iov_array);
|
|
|
|
efree(iov);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
static void php_destroy_socket(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
2001-06-04 05:38:23 +08:00
|
|
|
{
|
2001-06-26 12:19:39 +08:00
|
|
|
php_socket *php_sock = (php_socket *) rsrc->ptr;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-09-05 06:27:30 +08:00
|
|
|
close(php_sock->bsd_socket);
|
2001-05-18 01:02:37 +08:00
|
|
|
efree(php_sock);
|
|
|
|
}
|
|
|
|
|
2003-04-18 07:18:58 +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;
|
|
|
|
php_socket *sock = (php_socket*)emalloc(sizeof(php_socket));
|
|
|
|
|
|
|
|
*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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (bind(sock->bsd_socket, (struct sockaddr *)&la, sizeof(la)) < 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(sock, "unable to bind to given adress", 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
|
|
|
|
2001-11-02 00:51:59 +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
|
|
|
}
|
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
static int php_accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la TSRMLS_DC)
|
2001-05-18 01:02:37 +08:00
|
|
|
{
|
2002-01-27 15:06:09 +08:00
|
|
|
socklen_t salen;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *out_sock = (php_socket*)emalloc(sizeof(php_socket));
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
*new_sock = out_sock;
|
2002-01-27 15:06:09 +08:00
|
|
|
salen = sizeof(*la);
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2002-01-27 15:06:09 +08:00
|
|
|
out_sock->bsd_socket = accept(in_sock->bsd_socket, la, &salen);
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
|
2003-04-18 07:18:58 +08:00
|
|
|
static int php_read(int bsd_socket, 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;
|
|
|
|
|
2001-09-05 06:27:30 +08:00
|
|
|
m = fcntl(bsd_socket, F_GETFL);
|
2001-05-22 01:34:18 +08:00
|
|
|
if (m < 0) {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
nonblock = (m & O_NONBLOCK);
|
|
|
|
m = 0;
|
|
|
|
|
|
|
|
set_errno(0);
|
|
|
|
|
|
|
|
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.. */
|
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
if (no_read > 200) {
|
|
|
|
set_errno(ECONNRESET);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
if (n < maxlen) {
|
2002-10-28 11:05:04 +08:00
|
|
|
m = recv(bsd_socket, (void *) t, 1, flags);
|
2001-05-22 01:34:18 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-05-22 01:34:18 +08:00
|
|
|
if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_errno(0);
|
|
|
|
}
|
2001-05-18 01:02:37 +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
|
|
|
}
|
2001-05-18 01:02:37 +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
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
static char *php_strerror(int error TSRMLS_DC)
|
|
|
|
{
|
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
|
|
|
|
{
|
2002-09-30 10:09:42 +08:00
|
|
|
sprintf(SOCKETS_G(strerror_buf), "Host lookup error %d", error);
|
|
|
|
buf = SOCKETS_G(strerror_buf);
|
2001-11-02 00:51:59 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
buf = strerror(error);
|
|
|
|
}
|
2002-07-07 15:01:17 +08:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &tmp, 0, NULL)) {
|
|
|
|
strlcpy(SOCKETS_G(strerror_buf), (char *) tmp, 10000);
|
|
|
|
LocalFree(tmp);
|
2002-07-07 15:01:17 +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
|
|
|
|
|
|
|
|
return (buf ? (char *) buf : "");
|
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
/* Sets addr by hostname, or by ip in string form (AF_INET6) */
|
2003-04-18 07:18:58 +08:00
|
|
|
static int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock TSRMLS_DC)
|
|
|
|
{
|
2003-04-18 05:04:47 +08:00
|
|
|
struct in6_addr tmp;
|
2003-07-08 11:38:00 +08:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
2003-07-08 06:27:32 +08:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *addrinfo = NULL;
|
2003-07-08 11:38:00 +08:00
|
|
|
#endif
|
2003-04-18 05:04:47 +08:00
|
|
|
|
|
|
|
if (inet_pton(AF_INET6, string, &tmp)) {
|
|
|
|
memcpy(&(sin6->sin6_addr.s6_addr), &(tmp.s6_addr), sizeof(struct in6_addr));
|
|
|
|
} else {
|
2003-07-08 11:38:00 +08:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
|
2003-07-08 06:27:32 +08:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = PF_INET6;
|
|
|
|
getaddrinfo(string, NULL, &hints, &addrinfo);
|
|
|
|
if (!addrinfo) {
|
2003-04-18 05:04:47 +08:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
|
|
|
|
#else
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2003-07-08 06:27:32 +08:00
|
|
|
if (addrinfo->ai_family != PF_INET6 || addrinfo->ai_addrlen != sizeof(struct sockaddr_in6)) {
|
2003-04-18 05:04:47 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: Non AF_INET6 domain returned on AF_INET6 socket");
|
2003-07-08 06:27:32 +08:00
|
|
|
freeaddrinfo(addrinfo);
|
2003-04-18 05:04:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2003-07-08 06:27:32 +08:00
|
|
|
|
|
|
|
memcpy(&(sin6->sin6_addr.s6_addr), ((struct sockaddr_in6*)(addrinfo->ai_addr))->sin6_addr.s6_addr, sizeof(struct in6_addr));
|
|
|
|
freeaddrinfo(addrinfo);
|
2003-07-08 11:38:00 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
/* No IPv6 specific hostname resolution is available on this system? */
|
2003-07-08 13:00:13 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: getaddrinfo() not available on system.");
|
2003-07-08 11:38:00 +08:00
|
|
|
reurn 0;
|
|
|
|
#endif
|
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-03-04 13:27:04 +08:00
|
|
|
/* Sets addr by hostname, or by ip in string form (AF_INET) */
|
2003-04-18 07:18:58 +08:00
|
|
|
static int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock TSRMLS_DC)
|
|
|
|
{
|
2002-03-04 13:27:04 +08:00
|
|
|
struct in_addr tmp;
|
|
|
|
struct hostent *host_entry;
|
|
|
|
|
|
|
|
if (inet_aton(string, &tmp)) {
|
|
|
|
sin->sin_addr.s_addr = tmp.s_addr;
|
|
|
|
} else {
|
|
|
|
if (! (host_entry = gethostbyname(string))) {
|
|
|
|
/* Note: < -10000 indicates a host lookup error */
|
2002-07-07 15:01:17 +08:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
|
|
|
|
#else
|
2002-03-04 13:27:04 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
|
2002-07-07 15:01:17 +08:00
|
|
|
#endif
|
2002-03-04 13:27:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (host_entry->h_addrtype != AF_INET) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: Non AF_INET domain returned on AF_INET socket");
|
2002-03-04 13:27:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(&(sin->sin_addr.s_addr), host_entry->h_addr_list[0], host_entry->h_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2002-04-29 22:52:29 +08:00
|
|
|
static void php_sockets_init_globals(zend_sockets_globals *sockets_globals TSRMLS_DC)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION
|
|
|
|
*/
|
2000-07-03 12:35:57 +08:00
|
|
|
PHP_MINIT_FUNCTION(sockets)
|
|
|
|
{
|
2001-05-18 01:02:37 +08:00
|
|
|
struct protoent *pe;
|
|
|
|
|
2002-04-29 22:52:29 +08:00
|
|
|
ZEND_INIT_MODULE_GLOBALS(sockets, php_sockets_init_globals, NULL);
|
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
le_socket = zend_register_list_destructors_ex(php_destroy_socket, NULL, le_socket_name, module_number);
|
|
|
|
le_iov = zend_register_list_destructors_ex(php_destroy_iovec, NULL, le_iov_name, module_number);
|
2001-05-18 01:02:37 +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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
REGISTER_LONG_CONSTANT("AF_INET6", AF_INET6, CONST_CS | CONST_PERSISTENT);
|
|
|
|
#endif
|
2001-05-18 01:02:37 +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_PEEK", MSG_PEEK, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MSG_DONTROUTE", MSG_DONTROUTE, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_DEBUG", SO_DEBUG, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_REUSEADDR", SO_REUSEADDR, CONST_CS | CONST_PERSISTENT);
|
|
|
|
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);
|
|
|
|
REGISTER_LONG_CONSTANT("SO_ERROR", SO_ERROR, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("SOL_SOCKET", SOL_SOCKET, CONST_CS | CONST_PERSISTENT);
|
2002-05-01 17:25:26 +08:00
|
|
|
REGISTER_LONG_CONSTANT("SOMAXCONN", SOMAXCONN, CONST_CS | CONST_PERSISTENT);
|
2000-10-30 09:09:15 +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);
|
|
|
|
|
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
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
if ((pe = getprotobyname("tcp"))) {
|
|
|
|
REGISTER_LONG_CONSTANT("SOL_TCP", pe->p_proto, CONST_CS | CONST_PERSISTENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pe = getprotobyname("udp"))) {
|
|
|
|
REGISTER_LONG_CONSTANT("SOL_UDP", pe->p_proto, CONST_CS | CONST_PERSISTENT);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2000-08-20 18:31:27 +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
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ PHP_MINFO_FUNCTION
|
|
|
|
*/
|
2000-07-03 12:35:57 +08:00
|
|
|
PHP_MINFO_FUNCTION(sockets)
|
|
|
|
{
|
|
|
|
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_RINIT_FUNCTION */
|
|
|
|
PHP_RINIT_FUNCTION(sockets)
|
|
|
|
{
|
2003-01-19 03:28:10 +08:00
|
|
|
SOCKETS_G(strerror_buf) = emalloc(16384);
|
|
|
|
|
|
|
|
return SUCCESS;
|
2002-09-30 10:09:42 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ PHP_RSHUTDOWN_FUNCTION */
|
|
|
|
PHP_RSHUTDOWN_FUNCTION(sockets)
|
|
|
|
{
|
|
|
|
efree(SOCKETS_G(strerror_buf));
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2003-04-18 07:18:58 +08:00
|
|
|
|
2003-06-17 12:44:30 +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
|
|
|
{
|
2002-03-07 04:19:09 +08:00
|
|
|
zval **element;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
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
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(sock_array));
|
2002-04-29 23:06:48 +08:00
|
|
|
zend_hash_get_current_data(Z_ARRVAL_P(sock_array), (void **) &element) == SUCCESS;
|
|
|
|
zend_hash_move_forward(Z_ARRVAL_P(sock_array))) {
|
2002-03-11 09:24:42 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2001-07-26 06:03:55 +08:00
|
|
|
}
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
return 1;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
static int php_sock_array_from_fd_set(zval *sock_array, fd_set *fds TSRMLS_DC)
|
|
|
|
{
|
2002-03-07 04:19:09 +08:00
|
|
|
zval **element;
|
|
|
|
zval **dest_element;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
2002-03-07 04:19:09 +08:00
|
|
|
HashTable *new_hash;
|
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
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
ALLOC_HASHTABLE(new_hash);
|
|
|
|
zend_hash_init(new_hash, 0, NULL, ZVAL_PTR_DTOR, 0);
|
|
|
|
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(sock_array));
|
2002-04-29 23:06:48 +08:00
|
|
|
zend_hash_get_current_data(Z_ARRVAL_P(sock_array), (void **) &element) == SUCCESS;
|
|
|
|
zend_hash_move_forward(Z_ARRVAL_P(sock_array))) {
|
2002-03-11 09:24:42 +08:00
|
|
|
|
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
|
|
|
if (FD_ISSET(php_sock->bsd_socket, fds)) {
|
2002-04-29 23:06:48 +08:00
|
|
|
/* Add fd to new array */
|
2002-03-11 09:24:42 +08:00
|
|
|
zend_hash_next_index_insert(new_hash, (void *)element, sizeof(zval *), (void **)&dest_element);
|
|
|
|
if (dest_element) zval_add_ref(dest_element);
|
2001-07-26 06:03:55 +08:00
|
|
|
}
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
/* Destroy old array, add new one */
|
|
|
|
zend_hash_destroy(Z_ARRVAL_P(sock_array));
|
2002-05-01 12:46:59 +08:00
|
|
|
efree(Z_ARRVAL_P(sock_array));
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
zend_hash_internal_pointer_reset(new_hash);
|
|
|
|
Z_ARRVAL_P(sock_array) = new_hash;
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2002-03-07 04:19:09 +08:00
|
|
|
return 1;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2002-03-11 09:24:42 +08:00
|
|
|
|
2002-03-07 04:19:09 +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;
|
2003-06-17 12:44:30 +08:00
|
|
|
PHP_SOCKET max_fd = 0;
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval, sets = 0;
|
|
|
|
long usec = 0;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2002-03-09 12:37:54 +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;
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (Z_TYPE_P(sec) != IS_LONG) {
|
|
|
|
tmp = *sec;
|
|
|
|
zval_copy_ctor(&tmp);
|
|
|
|
convert_to_long(&tmp);
|
|
|
|
sec = &tmp;
|
|
|
|
}
|
2003-07-22 15:20:55 +08:00
|
|
|
|
|
|
|
/* Solaris + BSD do not like microsecond values which are >= 1 sec */
|
|
|
|
if (usec > 999999) {
|
|
|
|
tv.tv_sec = Z_LVAL_P(sec) + (usec / 1000000);
|
|
|
|
tv.tv_usec = usec % 1000000;
|
|
|
|
} else {
|
|
|
|
tv.tv_sec = Z_LVAL_P(sec);
|
|
|
|
tv.tv_usec = usec;
|
|
|
|
}
|
|
|
|
|
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;
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to select [%d]: %s", errno, php_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
|
|
|
|
2002-03-09 12:37:54 +08:00
|
|
|
RETURN_LONG(retval);
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-06-26 12:19:39 +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;
|
2003-03-07 07:07:28 +08:00
|
|
|
long port, backlog = 128;
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &port, &backlog) == FAILURE)
|
|
|
|
return;
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2001-06-26 12:19:39 +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
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock, *new_sock;
|
|
|
|
struct sockaddr_in sa;
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE)
|
|
|
|
return;
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
if (!php_accept_connect(php_sock, &new_sock, (struct sockaddr *) &sa TSRMLS_CC)) {
|
2003-01-14 10:39:10 +08:00
|
|
|
PHP_SOCKET_ERROR(new_sock, "unable to accept socket connection", errno);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
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
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +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;
|
2002-04-29 23:06:48 +08:00
|
|
|
int flags;
|
2002-03-06 13:21:56 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE)
|
|
|
|
return;
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
|
|
|
flags = fcntl(php_sock->bsd_socket, F_GETFL);
|
2002-03-06 13:21:56 +08:00
|
|
|
|
|
|
|
/* Safely append non blocking to other flags unless the get fails.
|
|
|
|
* Note: This does not abort on failure becuse getfl will always fail
|
|
|
|
* under the current win32 code. */
|
|
|
|
if (flags > -1) flags |= O_NONBLOCK;
|
2002-04-29 23:06:48 +08:00
|
|
|
else flags = O_NONBLOCK;
|
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
if (fcntl(php_sock->bsd_socket, F_SETFL, flags) > -1) {
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto bool socket_set_block(resource socket)
|
|
|
|
Sets blocking mode on a socket resource */
|
|
|
|
PHP_FUNCTION(socket_set_block)
|
|
|
|
{
|
|
|
|
zval *arg1;
|
|
|
|
php_socket *php_sock;
|
2002-04-29 23:06:48 +08:00
|
|
|
int flags;
|
2002-03-06 13:21:56 +08:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
|
|
|
flags = fcntl(php_sock->bsd_socket, F_GETFL);
|
2002-03-06 13:21:56 +08:00
|
|
|
|
|
|
|
/* Safely remove blocking from flags unless the get fails.
|
|
|
|
* Note: This does not abort on failure becuse getfl will always fail
|
|
|
|
* under the current win32 code. */
|
|
|
|
if (flags > -1) flags &= ~O_NONBLOCK;
|
2002-04-29 23:06:48 +08:00
|
|
|
else flags = 0;
|
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
if (fcntl(php_sock->bsd_socket, F_SETFL, flags) > -1) {
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_FALSE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-06-26 12:19:39 +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;
|
2003-03-07 07:07:28 +08:00
|
|
|
long backlog = 0;
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &backlog) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +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
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE)
|
|
|
|
return;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
zend_list_delete(Z_RESVAL_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;
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval, str_len;
|
|
|
|
long length;
|
2001-11-02 00:51:59 +08:00
|
|
|
char *str;
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &str, &str_len, &length) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
|
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
|
|
|
|
2001-05-18 01:02:37 +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
|
|
|
|
2002-01-10 00:51:53 +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;
|
|
|
|
char *tmpbuf;
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval;
|
|
|
|
long length, type = PHP_BINARY_READ;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &arg1, &length, &type) == FAILURE)
|
|
|
|
return;
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2002-12-09 21:35:57 +08:00
|
|
|
tmpbuf = emalloc(length + 1);
|
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
|
|
|
|
if (type == PHP_NORMAL_READ) {
|
2002-12-09 21:35:57 +08:00
|
|
|
retval = php_read(php_sock->bsd_socket, tmpbuf, length, 0);
|
|
|
|
} else {
|
|
|
|
retval = recv(php_sock->bsd_socket, tmpbuf, length, 0);
|
2000-10-23 07:43:48 +08:00
|
|
|
}
|
|
|
|
|
2001-11-29 16:09:39 +08:00
|
|
|
if (retval == -1) {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
|
2000-09-15 05:48:15 +08:00
|
|
|
efree(tmpbuf);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-03 12:35:57 +08:00
|
|
|
}
|
2001-11-29 16:09:39 +08:00
|
|
|
|
|
|
|
tmpbuf = erealloc(tmpbuf, retval + 1);
|
2002-12-02 06:15:39 +08:00
|
|
|
tmpbuf[retval] = '\0' ;
|
2001-11-29 16:18:50 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
RETURN_STRINGL(tmpbuf, retval, 0);
|
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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
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
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &arg1, &addr, &port) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
2003-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
sin6 = (struct sockaddr_in6 *) sa;
|
|
|
|
inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
|
|
|
|
zval_dtor(addr);
|
|
|
|
ZVAL_STRING(addr, addr6, 1);
|
|
|
|
|
|
|
|
if (port != NULL) {
|
|
|
|
zval_dtor(port);
|
|
|
|
ZVAL_LONG(port, htons(sin6->sin6_port));
|
|
|
|
}
|
|
|
|
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;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(addr);
|
|
|
|
ZVAL_STRING(addr, addr_string, 1);
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (port != NULL) {
|
|
|
|
zval_dtor(port);
|
|
|
|
ZVAL_LONG(port, htons(sin->sin_port));
|
|
|
|
}
|
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);
|
|
|
|
ZVAL_STRING(addr, s_un->sun_path, 1);
|
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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
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
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &arg1, &arg2, &arg3) == FAILURE)
|
|
|
|
return;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
sin6 = (struct sockaddr_in6 *) sa;
|
|
|
|
inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
|
|
|
|
zval_dtor(arg2);
|
|
|
|
ZVAL_STRING(arg2, addr6, 1);
|
|
|
|
|
|
|
|
if (arg3 != NULL) {
|
|
|
|
zval_dtor(arg3);
|
|
|
|
ZVAL_LONG(arg3, htons(sin6->sin6_port));
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(arg2);
|
|
|
|
ZVAL_STRING(arg2, addr_string, 1);
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (arg3 != NULL) {
|
|
|
|
zval_dtor(arg3);
|
|
|
|
ZVAL_LONG(arg3, htons(sin->sin_port));
|
|
|
|
}
|
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);
|
|
|
|
ZVAL_STRING(arg2, s_un->sun_path, 1);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-01-10 00:51:53 +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
|
|
|
{
|
2003-03-07 07:07:28 +08:00
|
|
|
long arg1, arg2, arg3;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock = (php_socket*)emalloc(sizeof(php_socket));
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-11-12 08:58:16 +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;
|
2001-11-12 08:58:16 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
if (arg1 != AF_UNIX
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
&& arg1 != AF_INET6
|
|
|
|
#endif
|
|
|
|
&& arg1 != AF_INET) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%d] 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) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%d] 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
|
|
|
}
|
2000-08-20 18:31:27 +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;
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create socket [%d]: %s", errno, php_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
struct sockaddr_in sin;
|
2003-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
struct sockaddr_un s_un;
|
|
|
|
char *addr;
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval, addr_len;
|
2003-04-18 05:04:47 +08:00
|
|
|
long port;
|
|
|
|
int argc = ZEND_NUM_ARGS();
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2003-04-18 05:04:47 +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;
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-11-02 00:51:59 +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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
if (argc != 3) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET6 requires 3 arguments");
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
case AF_INET:
|
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
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2003-04-18 07:18:58 +08:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons((unsigned short int)port);
|
2001-06-26 12:19:39 +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;
|
|
|
|
|
|
|
|
case AF_UNIX:
|
|
|
|
s_un.sun_family = AF_UNIX;
|
|
|
|
snprintf(s_un.sun_path, 108, "%s", addr);
|
|
|
|
retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
|
|
|
|
break;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
{
|
2003-03-07 07:07:28 +08:00
|
|
|
long arg1;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &arg1) == FAILURE)
|
|
|
|
return;
|
2001-05-09 12:54:46 +08:00
|
|
|
|
2002-09-30 10:09:42 +08:00
|
|
|
RETURN_STRING(php_strerror(arg1 TSRMLS_CC), 1);
|
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;
|
|
|
|
php_sockaddr_storage sa_storage;
|
|
|
|
struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
|
|
|
|
php_socket *php_sock;
|
|
|
|
char *addr;
|
2003-03-07 07:07:28 +08:00
|
|
|
int addr_len;
|
2003-03-10 23:52:10 +08:00
|
|
|
long port = 0;
|
2001-11-02 00:51:59 +08:00
|
|
|
long retval = 0;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
case AF_UNIX:
|
|
|
|
{
|
|
|
|
struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
|
|
|
|
memset(sa, 0, sizeof(sa_storage));
|
|
|
|
sa->sun_family = AF_UNIX;
|
|
|
|
snprintf(sa->sun_path, 108, "%s", addr);
|
|
|
|
retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa, SUN_LEN(sa));
|
2001-11-12 08:58:16 +08:00
|
|
|
break;
|
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;
|
|
|
|
|
|
|
|
memset(sa, 0, sizeof(sa_storage)); /* Apparently, Mac OSX needs this */
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2002-03-04 13:27:04 +08:00
|
|
|
sa->sin_family = AF_INET;
|
|
|
|
sa->sin_port = htons((unsigned short) port);
|
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;
|
|
|
|
}
|
2002-04-29 23:06:48 +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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
|
|
|
|
|
|
|
|
memset(sa, 0, sizeof(sa_storage)); /* Apparently, Mac OSX needs this */
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#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
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto resource socket_iovec_alloc(int num_vectors [, int ...])
|
2000-08-23 21:47:00 +08:00
|
|
|
Builds a 'struct iovec' for use with sendmsg, recvmsg, writev, and readv */
|
2000-07-08 09:50:34 +08:00
|
|
|
/* First parameter is number of vectors, each additional parameter is the
|
|
|
|
length of the vector to create.
|
|
|
|
*/
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_iovec_alloc)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval ***args = (zval ***)NULL;
|
|
|
|
php_iovec_t *vector;
|
|
|
|
struct iovec *vector_array;
|
|
|
|
int i, j, num_vectors, argc = ZEND_NUM_ARGS();
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
args = emalloc(argc*sizeof(zval**));
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
if (argc < 1 || zend_get_parameters_array_ex(argc, args) == FAILURE) {
|
|
|
|
efree(args);
|
2000-07-08 09:50:34 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
|
|
|
convert_to_long_ex(args[0]);
|
|
|
|
num_vectors = Z_LVAL_PP(args[0]);
|
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
vector_array = emalloc(sizeof(struct iovec)*(num_vectors+1));
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2000-08-20 18:31:27 +08:00
|
|
|
for (i = 0, j = 1; i < num_vectors; i++, j++) {
|
|
|
|
convert_to_long_ex(args[j]);
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
vector_array[i].iov_base = (char*)emalloc(Z_LVAL_PP(args[j]));
|
|
|
|
vector_array[i].iov_len = Z_LVAL_PP(args[j]);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2002-09-10 12:12:55 +08:00
|
|
|
|
|
|
|
efree(args);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
vector = emalloc(sizeof(php_iovec_t));
|
2000-07-08 09:50:34 +08:00
|
|
|
vector->iov_array = vector_array;
|
2000-08-20 18:31:27 +08:00
|
|
|
vector->count = num_vectors;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2000-12-04 03:17:09 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, vector, le_iov);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto string socket_iovec_fetch(resource iovec, int iovec_position)
|
2000-07-08 23:13:31 +08:00
|
|
|
Returns the data held in the iovec specified by iovec_id[iovec_position] */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_iovec_fetch)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 04:10:17 +08:00
|
|
|
zval *iovec_id;
|
|
|
|
php_iovec_t *vector;
|
2003-03-07 07:07:28 +08:00
|
|
|
unsigned long iovec_position;
|
2000-12-04 03:17:09 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &iovec_id, &iovec_position) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &iovec_id, -1, le_iov_name, le_iov);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2002-09-10 12:12:55 +08:00
|
|
|
if (iovec_position >= vector->count) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't access a vector position past the amount of vectors set in the array");
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_EMPTY_STRING();
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_STRINGL(vector->iov_array[iovec_position].iov_base, vector->iov_array[iovec_position].iov_len, 1);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_iovec_set(resource iovec, int iovec_position, string new_val)
|
2000-07-08 23:13:31 +08:00
|
|
|
Sets the data held in iovec_id[iovec_position] to new_val */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_iovec_set)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 04:10:17 +08:00
|
|
|
zval *iovec_id;
|
|
|
|
php_iovec_t *vector;
|
|
|
|
int new_val_len;
|
2003-03-07 07:07:28 +08:00
|
|
|
unsigned long iovec_position;
|
2001-11-02 04:10:17 +08:00
|
|
|
char *new_val;
|
2000-12-04 03:17:09 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rls", &iovec_id, &iovec_position, &new_val, &new_val_len) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &iovec_id, -1, le_iov_name, le_iov);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2002-09-10 12:12:55 +08:00
|
|
|
if (iovec_position >= vector->count) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't access a vector position outside of the vector array bounds");
|
2000-07-08 09:50:34 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (vector->iov_array[iovec_position].iov_base) {
|
|
|
|
efree(vector->iov_array[iovec_position].iov_base);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
vector->iov_array[iovec_position].iov_base = estrdup(new_val);
|
|
|
|
vector->iov_array[iovec_position].iov_len = strlen(new_val);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_iovec_add(resource iovec, int iov_len)
|
2000-07-08 09:50:34 +08:00
|
|
|
Adds a new vector to the scatter/gather array */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_iovec_add)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *iovec_id;
|
|
|
|
php_iovec_t *vector;
|
|
|
|
struct iovec *vector_array;
|
2003-03-07 07:07:28 +08:00
|
|
|
long iov_len;
|
2000-12-04 03:17:09 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &iovec_id, &iov_len) == FAILURE)
|
|
|
|
return;
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &iovec_id, -1, le_iov_name, le_iov);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
vector_array = (struct iovec*)emalloc(sizeof(struct iovec) * (vector->count + 2));
|
2000-09-12 00:19:53 +08:00
|
|
|
memcpy(vector_array, vector->iov_array, sizeof(struct iovec) * vector->count);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
vector_array[vector->count].iov_base = (char*)emalloc(iov_len);
|
|
|
|
vector_array[vector->count].iov_len = iov_len;
|
2000-07-08 09:50:34 +08:00
|
|
|
efree(vector->iov_array);
|
|
|
|
vector->iov_array = vector_array;
|
|
|
|
vector->count++;
|
|
|
|
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_iovec_delete(resource iovec, int iov_pos)
|
2000-07-08 09:50:34 +08:00
|
|
|
Deletes a vector from an array of vectors */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_iovec_delete)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *iovec_id;
|
|
|
|
php_iovec_t *vector;
|
|
|
|
struct iovec *vector_array;
|
2003-03-07 07:07:28 +08:00
|
|
|
unsigned int i;
|
|
|
|
unsigned long iov_pos;
|
2000-12-04 03:17:09 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &iovec_id, &iov_pos) == FAILURE)
|
|
|
|
return;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &iovec_id, -1, le_iov_name, le_iov);
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (iov_pos > vector->count) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't delete an IO vector that is out of array bounds");
|
2000-07-08 09:50:34 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2001-06-26 12:19:39 +08:00
|
|
|
vector_array = emalloc(vector->count * sizeof(struct iovec));
|
2000-07-08 09:50:34 +08:00
|
|
|
|
|
|
|
for (i = 0; i < vector->count; i++) {
|
2001-11-02 00:51:59 +08:00
|
|
|
if (i < iov_pos) {
|
2000-09-12 00:19:53 +08:00
|
|
|
memcpy(&(vector->iov_array[i]), &(vector_array[i]), sizeof(struct iovec));
|
2001-11-02 00:51:59 +08:00
|
|
|
} else if (i > iov_pos) {
|
2000-09-12 00:19:53 +08:00
|
|
|
memcpy(&(vector->iov_array[i]), &(vector_array[i - 1]), sizeof(struct iovec));
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
efree(vector->iov_array);
|
|
|
|
vector->iov_array = vector_array;
|
|
|
|
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_iovec_free(resource iovec)
|
2000-07-08 23:13:31 +08:00
|
|
|
Frees the iovec specified by iovec_id */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_iovec_free)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *iovec_id;
|
|
|
|
php_iovec_t *vector;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &iovec_id) == FAILURE)
|
|
|
|
return;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &iovec_id, -1, le_iov_name, le_iov);
|
|
|
|
|
|
|
|
zend_list_delete(Z_RESVAL_P(iovec_id));
|
2000-07-08 09:50:34 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_readv(resource socket, resource iovec_id)
|
2000-07-08 23:13:31 +08:00
|
|
|
Reads from an fd, using the scatter-gather array defined by iovec_id */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_readv)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1, *arg2;
|
|
|
|
php_iovec_t *vector;
|
|
|
|
php_socket *php_sock;
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &arg1, &arg2) == FAILURE)
|
|
|
|
return;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &arg2, -1, le_iov_name, le_iov);
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-09-05 06:27:30 +08:00
|
|
|
if (readv(php_sock->bsd_socket, vector->iov_array, vector->count) != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_writev(resource socket, resource iovec_id)
|
2000-08-20 18:31:27 +08:00
|
|
|
Writes to a file descriptor, fd, using the scatter-gather array defined by iovec_id */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_writev)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1, *arg2;
|
|
|
|
php_iovec_t *vector;
|
|
|
|
php_socket *php_sock;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &arg1, &arg2) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &arg2, -1, le_iov_name, le_iov);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-09-05 06:27:30 +08:00
|
|
|
if (writev(php_sock->bsd_socket, vector->iov_array, vector->count) != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "Unable to write to socket", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2000-07-08 09:50:34 +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;
|
2001-11-02 00:51:59 +08:00
|
|
|
char *recv_buf;
|
|
|
|
php_socket *php_sock;
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval;
|
|
|
|
long len, flags;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzll", &php_sock_res, &buf, &len, &flags) == FAILURE)
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2000-07-03 12:35:57 +08:00
|
|
|
|
2002-03-06 13:21:56 +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
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
recv_buf = emalloc(len + 1);
|
|
|
|
memset(recv_buf, 0, len + 1);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
if ((retval = recv(php_sock->bsd_socket, recv_buf, len, flags)) < 1) {
|
2000-07-08 09:50:34 +08:00
|
|
|
efree(recv_buf);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2002-03-06 13:21:56 +08:00
|
|
|
zval_dtor(buf);
|
2002-04-29 23:06:48 +08:00
|
|
|
Z_TYPE_P(buf) = IS_NULL;
|
2002-03-06 13:21:56 +08:00
|
|
|
} else {
|
2002-05-08 23:30:04 +08:00
|
|
|
recv_buf[retval] = '\0';
|
2002-03-06 13:21:56 +08:00
|
|
|
|
|
|
|
/* Rebuild buffer zval */
|
|
|
|
zval_dtor(buf);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
|
|
|
Z_STRVAL_P(buf) = recv_buf;
|
|
|
|
Z_STRLEN_P(buf) = retval;
|
|
|
|
Z_TYPE_P(buf) = IS_STRING;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
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 read from socket", errno);
|
2002-03-09 12:37:54 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2002-03-06 13:21:56 +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;
|
2003-03-07 07:07:28 +08:00
|
|
|
int buf_len, retval;
|
|
|
|
long len, flags;
|
2001-11-02 00:51:59 +08:00
|
|
|
char *buf;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsll", &arg1, &buf, &buf_len, &len, &flags) == FAILURE)
|
|
|
|
return;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +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
|
|
|
}
|
|
|
|
|
2001-05-18 01:02:37 +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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
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;
|
|
|
|
long arg3, arg4;
|
2001-11-02 00:51:59 +08:00
|
|
|
char *recv_buf, *address;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2002-02-16 01:33:08 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzllz|z", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE)
|
2001-11-02 00:51:59 +08:00
|
|
|
return;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +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-11-02 00:51:59 +08:00
|
|
|
recv_buf = emalloc(arg3 + 2);
|
|
|
|
memset(recv_buf, 0, arg3 + 2);
|
2000-08-20 18:31:27 +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;
|
|
|
|
retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
|
2001-06-13 00:41:51 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (retval < 0) {
|
|
|
|
efree(recv_buf);
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(arg2);
|
|
|
|
zval_dtor(arg5);
|
|
|
|
|
2002-08-28 14:15:13 +08:00
|
|
|
ZVAL_STRINGL(arg2, recv_buf, retval, 0);
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_STRING(arg5, s_un.sun_path, 1);
|
|
|
|
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;
|
2001-06-26 12:19:39 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (arg6 == NULL) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
|
2001-06-13 00:41:51 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (retval < 0) {
|
|
|
|
efree(recv_buf);
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
|
2001-11-02 00:51:59 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-04-29 23:06:48 +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);
|
|
|
|
|
2002-08-28 14:15:13 +08:00
|
|
|
ZVAL_STRINGL(arg2, recv_buf, retval, 0);
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_STRING(arg5, address ? address : "0.0.0.0", 1);
|
|
|
|
ZVAL_LONG(arg6, ntohs(sin.sin_port));
|
|
|
|
break;
|
2003-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
slen = sizeof(sin6);
|
|
|
|
memset(&sin6, 0, slen);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
|
|
|
|
if (arg6 == NULL) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
|
|
|
|
|
|
|
|
if (retval < 0) {
|
|
|
|
efree(recv_buf);
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
zval_dtor(arg2);
|
|
|
|
zval_dtor(arg5);
|
|
|
|
zval_dtor(arg6);
|
|
|
|
|
|
|
|
inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
ZVAL_STRINGL(arg2, recv_buf, retval, 0);
|
|
|
|
ZVAL_STRING(arg5, addr6 ? addr6 : "::", 1);
|
|
|
|
ZVAL_LONG(arg6, ntohs(sin6.sin6_port));
|
|
|
|
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
|
|
|
|
|
|
|
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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
#endif
|
2003-03-07 07:07:28 +08:00
|
|
|
int retval, buf_len, addr_len;
|
|
|
|
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
|
|
|
|
2003-04-18 05:04:47 +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;
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-11-02 00:51:59 +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);
|
|
|
|
|
|
|
|
if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +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-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (! php_set_inet6_addr(&sin6, addr, php_sock TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin, sizeof(sin));
|
|
|
|
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
|
|
|
|
|
|
|
RETURN_LONG(retval);
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_recvmsg(resource socket, resource iovec, array &control, int &controllen, int &flags, string &addr [, int &port])
|
2000-07-08 23:13:31 +08:00
|
|
|
Used to receive messages on a socket, whether connection-oriented or not */
|
2003-04-04 21:02:43 +08:00
|
|
|
#ifdef HAVE_CMSGHDR
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_recvmsg)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1, *arg2, *arg3, *arg4, *arg5, *arg6, *arg7 = NULL;
|
|
|
|
php_iovec_t *iov;
|
|
|
|
struct msghdr hdr;
|
|
|
|
php_sockaddr_storage sa_storage;
|
|
|
|
php_socket *php_sock;
|
|
|
|
struct cmsghdr *ctl_buf;
|
|
|
|
struct sockaddr *sa = (struct sockaddr *) &sa_storage;
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
|
2003-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
|
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
struct sockaddr_un *s_un = (struct sockaddr_un *) sa;
|
|
|
|
socklen_t salen = sizeof(sa_storage);
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrzzzz|z", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
ZEND_FETCH_RESOURCE(iov, php_iovec_t *, &arg2, -1, le_iov_name, le_iov);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
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 receive message", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ctl_buf = (Z_LVAL_P(arg4) > sizeof(struct cmsghdr)) ? (struct cmsghdr*)emalloc(Z_LVAL_P(arg4)) : NULL;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
switch (sa->sa_family) {
|
2003-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
|
|
|
|
if (arg7 == NULL) {
|
|
|
|
efree(ctl_buf);
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(sa, 0, sizeof(sa_storage));
|
|
|
|
hdr.msg_name = (void *) sin6;
|
|
|
|
hdr.msg_namelen = sizeof(sa_storage);
|
|
|
|
hdr.msg_iov = iov->iov_array;
|
|
|
|
hdr.msg_iovlen = iov->count;
|
|
|
|
|
|
|
|
hdr.msg_control = ctl_buf ? (void *) ctl_buf : NULL;
|
|
|
|
hdr.msg_controllen = ctl_buf ? Z_LVAL_P(arg4) : 0;
|
|
|
|
#ifndef MISSING_MSGHDR_MSGFLAGS
|
|
|
|
hdr.msg_flags = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (recvmsg(php_sock->bsd_socket, &hdr, Z_LVAL_P(arg5)) < 0) {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to receive message", errno);
|
|
|
|
RETURN_FALSE;
|
|
|
|
} else {
|
|
|
|
struct cmsghdr *mhdr = (struct cmsghdr *) hdr.msg_control;
|
|
|
|
|
|
|
|
zval_dtor(arg3);
|
|
|
|
zval_dtor(arg4);
|
|
|
|
zval_dtor(arg5);
|
|
|
|
zval_dtor(arg6);
|
|
|
|
zval_dtor(arg7);
|
|
|
|
|
|
|
|
ZVAL_LONG(arg4, hdr.msg_controllen);
|
|
|
|
#ifndef MISSING_MSGHDR_MSGFLAGS
|
|
|
|
ZVAL_LONG(arg5, hdr.msg_flags);
|
|
|
|
#endif
|
|
|
|
ZVAL_LONG(arg7, ntohs(sin6->sin6_port));
|
|
|
|
|
|
|
|
array_init(arg3);
|
|
|
|
|
|
|
|
if (mhdr != NULL) {
|
|
|
|
add_assoc_long(arg3, "cmsg_level", mhdr->cmsg_level);
|
|
|
|
add_assoc_long(arg3, "cmsg_type", mhdr->cmsg_type);
|
|
|
|
add_assoc_string(arg3, "cmsg_data", CMSG_DATA(mhdr), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char tmp[INET6_ADDRSTRLEN+1];
|
|
|
|
if (inet_ntop(AF_INET6, &sin6->sin6_addr, tmp, INET6_ADDRSTRLEN)) {
|
|
|
|
ZVAL_STRING(arg6, tmp, 1);
|
|
|
|
} else {
|
|
|
|
ZVAL_STRING(arg6, "::", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2000-08-20 18:31:27 +08:00
|
|
|
case AF_INET:
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (arg7 == NULL) {
|
2000-07-08 09:50:34 +08:00
|
|
|
efree(ctl_buf);
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2000-10-22 15:54:49 +08:00
|
|
|
memset(sa, 0, sizeof(sa_storage));
|
2002-08-16 22:23:27 +08:00
|
|
|
hdr.msg_name = (void *) sin;
|
2001-11-02 00:51:59 +08:00
|
|
|
hdr.msg_namelen = sizeof(sa_storage);
|
|
|
|
hdr.msg_iov = iov->iov_array;
|
|
|
|
hdr.msg_iovlen = iov->count;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2002-08-16 22:23:27 +08:00
|
|
|
hdr.msg_control = ctl_buf ? (void *) ctl_buf : NULL;
|
2001-11-02 00:51:59 +08:00
|
|
|
hdr.msg_controllen = ctl_buf ? Z_LVAL_P(arg4) : 0;
|
2002-05-12 15:22:47 +08:00
|
|
|
#ifndef MISSING_MSGHDR_MSGFLAGS
|
2001-11-02 00:51:59 +08:00
|
|
|
hdr.msg_flags = 0;
|
2002-05-12 15:22:47 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (recvmsg(php_sock->bsd_socket, &hdr, Z_LVAL_P(arg5)) < 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to receive message", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +08:00
|
|
|
} else {
|
|
|
|
struct cmsghdr *mhdr = (struct cmsghdr *) hdr.msg_control;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(arg3);
|
|
|
|
zval_dtor(arg4);
|
|
|
|
zval_dtor(arg5);
|
|
|
|
zval_dtor(arg6);
|
|
|
|
zval_dtor(arg7);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_LONG(arg4, hdr.msg_controllen);
|
2002-05-12 15:22:47 +08:00
|
|
|
#ifndef MISSING_MSGHDR_MSGFLAGS
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_LONG(arg5, hdr.msg_flags);
|
2002-05-12 15:22:47 +08:00
|
|
|
#endif
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_LONG(arg7, ntohs(sin->sin_port));
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2003-01-14 10:39:10 +08:00
|
|
|
array_init(arg3);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2002-05-12 15:22:47 +08:00
|
|
|
if (mhdr != NULL) {
|
|
|
|
add_assoc_long(arg3, "cmsg_level", mhdr->cmsg_level);
|
|
|
|
add_assoc_long(arg3, "cmsg_type", mhdr->cmsg_type);
|
|
|
|
add_assoc_string(arg3, "cmsg_data", CMSG_DATA(mhdr), 1);
|
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
{
|
|
|
|
char *tmp = inet_ntoa(sin->sin_addr);
|
|
|
|
if (tmp == NULL) {
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_STRING(arg6, "0.0.0.0", 1);
|
2001-05-18 01:02:37 +08:00
|
|
|
} else {
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_STRING(arg6, tmp, 1);
|
2000-10-22 15:54:49 +08:00
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
|
|
|
case AF_UNIX:
|
|
|
|
memset(sa, 0, sizeof(sa_storage));
|
2002-08-16 22:23:27 +08:00
|
|
|
hdr.msg_name = (void *) s_un;
|
2001-11-02 00:51:59 +08:00
|
|
|
hdr.msg_namelen = sizeof(struct sockaddr_un);
|
|
|
|
hdr.msg_iov = iov->iov_array;
|
|
|
|
hdr.msg_iovlen = iov->count;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
|
|
|
if (ctl_buf) {
|
2002-08-16 22:23:27 +08:00
|
|
|
hdr.msg_control = (void *) ctl_buf;
|
2001-11-02 00:51:59 +08:00
|
|
|
hdr.msg_controllen = Z_LVAL_P(arg4);
|
2001-05-18 01:02:37 +08:00
|
|
|
} else {
|
|
|
|
hdr.msg_control = NULL;
|
|
|
|
hdr.msg_controllen = 0;
|
|
|
|
}
|
2002-05-12 15:22:47 +08:00
|
|
|
#ifndef MISSING_MSGHDR_MSGFLAGS
|
2001-05-18 01:02:37 +08:00
|
|
|
hdr.msg_flags = 0;
|
2002-05-12 15:22:47 +08:00
|
|
|
#endif
|
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (recvmsg(php_sock->bsd_socket, &hdr, Z_LVAL_P(arg5)) != 0) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to receive message", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
} else {
|
|
|
|
struct cmsghdr *mhdr = (struct cmsghdr *) hdr.msg_control;
|
|
|
|
|
|
|
|
if (mhdr != NULL) {
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
zval_dtor(arg3);
|
|
|
|
zval_dtor(arg4);
|
|
|
|
zval_dtor(arg5);
|
|
|
|
zval_dtor(arg6);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_LONG(arg4, hdr.msg_controllen);
|
2002-05-12 15:22:47 +08:00
|
|
|
#ifndef MISSING_MSGHDR_MSGFLAGS
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_LONG(arg5, hdr.msg_flags);
|
2002-05-12 15:22:47 +08:00
|
|
|
#endif
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2003-01-14 10:39:10 +08:00
|
|
|
array_init(arg3);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
add_assoc_long(arg3, "cmsg_level", mhdr->cmsg_level);
|
|
|
|
add_assoc_long(arg3, "cmsg_type", mhdr->cmsg_type);
|
|
|
|
add_assoc_string(arg3, "cmsg_data", CMSG_DATA(mhdr), 1);
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZVAL_STRING(arg6, s_un->sun_path, 1);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2000-07-08 09:50:34 +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-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
}
|
2003-04-04 21:02:43 +08:00
|
|
|
#endif
|
2000-07-08 09:50:34 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
/* {{{ proto bool socket_sendmsg(resource socket, resource iovec, int flags, string addr [, int port])
|
2000-07-08 09:50:34 +08:00
|
|
|
Sends a message to a socket, regardless of whether it is connection-oriented or not */
|
2001-05-18 01:02:37 +08:00
|
|
|
PHP_FUNCTION(socket_sendmsg)
|
2000-07-08 09:50:34 +08:00
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1, *arg2;
|
|
|
|
php_iovec_t *iov;
|
|
|
|
php_socket *php_sock;
|
|
|
|
struct sockaddr sa;
|
|
|
|
char *addr;
|
2002-01-27 15:06:09 +08:00
|
|
|
socklen_t salen;
|
2003-03-07 07:07:28 +08:00
|
|
|
int addr_len;
|
|
|
|
long flags, port;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrls|l", &arg1, &arg2, &flags, &addr, &addr_len, &port) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
ZEND_FETCH_RESOURCE(iov, php_iovec_t *, &arg2, -1, le_iov_name, le_iov);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
salen = sizeof(sa);
|
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 send messge", 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
|
|
|
switch(sa.sa_family) {
|
2003-04-18 05:04:47 +08:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct msghdr hdr;
|
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &sa;
|
|
|
|
|
|
|
|
set_h_errno(0);
|
|
|
|
set_errno(0);
|
|
|
|
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
hdr.msg_name = (void *) &sa;
|
|
|
|
hdr.msg_namelen = sizeof(sa);
|
|
|
|
hdr.msg_iov = iov->iov_array;
|
|
|
|
hdr.msg_iovlen = iov->count;
|
|
|
|
|
|
|
|
memset(sin6, 0, sizeof(sa));
|
|
|
|
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_port = htons((unsigned short)port);
|
|
|
|
|
|
|
|
if (! php_set_inet6_addr(sin6, addr, php_sock TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sendmsg(php_sock->bsd_socket, &hdr, flags) == -1) {
|
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to send message", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2000-07-08 09:50:34 +08:00
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct msghdr hdr;
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *) &sa;
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
set_h_errno(0);
|
|
|
|
set_errno(0);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2000-09-12 00:19:53 +08:00
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
2002-08-16 22:23:27 +08:00
|
|
|
hdr.msg_name = (void *) &sa;
|
2000-07-08 09:50:34 +08:00
|
|
|
hdr.msg_namelen = sizeof(sa);
|
|
|
|
hdr.msg_iov = iov->iov_array;
|
|
|
|
hdr.msg_iovlen = iov->count;
|
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
memset(sin, 0, sizeof(sa));
|
|
|
|
|
2002-03-04 13:27:04 +08:00
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_port = htons((unsigned short)port);
|
2002-04-29 23:06:48 +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
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2002-03-04 13:27:04 +08:00
|
|
|
if (sendmsg(php_sock->bsd_socket, &hdr, flags) == -1) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to send message", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
}
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2000-07-08 09:50:34 +08:00
|
|
|
case AF_UNIX:
|
|
|
|
{
|
|
|
|
struct msghdr hdr;
|
2000-10-22 15:54:49 +08:00
|
|
|
struct sockaddr_un *s_un = (struct sockaddr_un *) &sa;
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
set_errno(0);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2002-08-16 22:23:27 +08:00
|
|
|
hdr.msg_name = (void *) s_un;
|
2000-07-08 09:50:34 +08:00
|
|
|
hdr.msg_iov = iov->iov_array;
|
|
|
|
hdr.msg_iovlen = iov->count;
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
snprintf(s_un->sun_path, 108, "%s", addr);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-06-13 05:06:01 +08:00
|
|
|
hdr.msg_namelen = SUN_LEN(s_un);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2002-03-04 13:27:04 +08:00
|
|
|
if (sendmsg(php_sock->bsd_socket, &hdr, flags) == -1) {
|
2001-11-12 09:54:55 +08:00
|
|
|
PHP_SOCKET_ERROR(php_sock, "unable to send message", errno);
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
RETURN_TRUE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
2003-04-18 05:04:47 +08:00
|
|
|
break;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2000-07-08 09:50:34 +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-05-18 01:02:37 +08:00
|
|
|
RETURN_FALSE;
|
2000-07-08 09:50:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-05-01 19:08:08 +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;
|
2001-12-06 12:44:23 +08:00
|
|
|
struct timeval tv;
|
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;
|
|
|
|
long level, optname;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &arg1, &level, &optname) == FAILURE)
|
|
|
|
return;
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
switch(optname) {
|
|
|
|
case SO_LINGER:
|
|
|
|
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);
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
|
|
|
|
add_assoc_long(return_value, "l_linger", linger_val.l_linger);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SO_RCVTIMEO:
|
|
|
|
case SO_SNDTIMEO:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-01-14 10:39:10 +08:00
|
|
|
array_init(return_value);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
add_assoc_long(return_value, "sec", tv.tv_sec);
|
|
|
|
add_assoc_long(return_value, "usec", tv.tv_usec);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
optlen = sizeof(other_val);
|
2002-04-29 23:06:48 +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;
|
|
|
|
}
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
RETURN_LONG(other_val);
|
|
|
|
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
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *arg1, *arg4;
|
|
|
|
struct linger lv;
|
2001-12-06 12:44:23 +08:00
|
|
|
struct timeval tv;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
2003-03-07 07:07:28 +08:00
|
|
|
int ov, optlen, retval;
|
|
|
|
long level, optname;
|
2001-12-06 12:44:23 +08:00
|
|
|
void *opt_ptr;
|
2002-04-29 23:06:48 +08:00
|
|
|
|
|
|
|
HashTable *opt_ht;
|
2001-12-06 12:44:23 +08:00
|
|
|
zval **l_onoff, **l_linger;
|
2002-04-29 23:06:48 +08:00
|
|
|
zval **sec, **usec;
|
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
/* key name constants */
|
2002-04-29 23:06:48 +08:00
|
|
|
char *l_onoff_key = "l_onoff";
|
|
|
|
char *l_linger_key = "l_linger";
|
|
|
|
char *sec_key = "sec";
|
|
|
|
char *usec_key = "usec";
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllz", &arg1, &level, &optname, &arg4) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
switch (optname) {
|
2002-04-29 23:06:48 +08:00
|
|
|
case SO_LINGER:
|
2001-12-06 12:44:23 +08:00
|
|
|
convert_to_array_ex(&arg4);
|
|
|
|
opt_ht = HASH_OF(arg4);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
if (zend_hash_find(opt_ht, l_onoff_key, strlen(l_onoff_key) + 1, (void **)&l_onoff) == FAILURE) {
|
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;
|
|
|
|
}
|
|
|
|
if (zend_hash_find(opt_ht, l_linger_key, strlen(l_linger_key) + 1, (void **)&l_linger) == FAILURE) {
|
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
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
convert_to_long_ex(l_onoff);
|
|
|
|
convert_to_long_ex(l_linger);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
lv.l_onoff = (unsigned short)Z_LVAL_PP(l_onoff);
|
|
|
|
lv.l_linger = (unsigned short)Z_LVAL_PP(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;
|
|
|
|
case SO_RCVTIMEO:
|
|
|
|
case SO_SNDTIMEO:
|
|
|
|
convert_to_array_ex(&arg4);
|
|
|
|
opt_ht = HASH_OF(arg4);
|
2000-07-08 09:50:34 +08:00
|
|
|
|
2001-12-06 12:44:23 +08:00
|
|
|
if (zend_hash_find(opt_ht, sec_key, strlen(sec_key) + 1, (void **)&sec) == FAILURE) {
|
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;
|
|
|
|
}
|
|
|
|
if (zend_hash_find(opt_ht, usec_key, strlen(usec_key) + 1, (void **)&usec) == FAILURE) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
convert_to_long_ex(sec);
|
|
|
|
convert_to_long_ex(usec);
|
2002-04-29 23:06:48 +08:00
|
|
|
tv.tv_sec = Z_LVAL_PP(sec);
|
|
|
|
tv.tv_usec = Z_LVAL_PP(usec);
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2002-04-29 23:06:48 +08:00
|
|
|
optlen = sizeof(tv);
|
|
|
|
opt_ptr = &tv;
|
2001-12-06 12:44:23 +08:00
|
|
|
break;
|
|
|
|
default:
|
2002-04-29 23:06:48 +08:00
|
|
|
convert_to_long_ex(&arg4);
|
2001-12-06 12:44:23 +08:00
|
|
|
ov = Z_LVAL_P(arg4);
|
2002-04-29 23:06:48 +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);
|
2002-04-29 23:06:48 +08:00
|
|
|
|
2001-07-26 06:03:55 +08:00
|
|
|
if (retval != 0) {
|
2001-11-12 09:54:55 +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
|
|
|
|
2001-05-18 01:02:37 +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
|
|
|
{
|
2001-11-02 00:51:59 +08:00
|
|
|
zval *retval[2], *fds_array_zval;
|
|
|
|
php_socket *php_sock[2];
|
2003-06-17 12:44:30 +08:00
|
|
|
PHP_SOCKET fds_array[2];
|
2003-03-07 07:07:28 +08:00
|
|
|
long domain, type, protocol;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2002-05-01 18:41:10 +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;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
php_sock[0] = (php_socket*)emalloc(sizeof(php_socket));
|
|
|
|
php_sock[1] = (php_socket*)emalloc(sizeof(php_socket));
|
2000-08-20 18:31:27 +08:00
|
|
|
|
2003-04-18 05:04:47 +08:00
|
|
|
if (domain != AF_INET
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
&& domain != AF_INET6
|
|
|
|
#endif
|
|
|
|
&& domain != AF_UNIX) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%d] 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
|
|
|
}
|
|
|
|
|
2001-11-02 00:51:59 +08:00
|
|
|
if (type > 10) {
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%d] 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
|
|
|
}
|
|
|
|
|
2002-05-01 18:41:10 +08:00
|
|
|
if (socketpair(domain, type, protocol, fds_array) != 0) {
|
|
|
|
SOCKETS_G(last_error) = errno;
|
2003-01-14 10:39:10 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to create socket pair [%d]: %s", errno, php_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-05-18 01:02:37 +08:00
|
|
|
MAKE_STD_ZVAL(retval[0]);
|
|
|
|
MAKE_STD_ZVAL(retval[1]);
|
|
|
|
|
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;
|
2001-05-18 01:02:37 +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
|
|
|
|
2001-11-02 00:51:59 +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
|
|
|
/* }}} */
|
|
|
|
|
2001-05-18 01:02:37 +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;
|
2003-03-07 07:07:28 +08:00
|
|
|
long how_shutdown = 2;
|
2001-11-02 00:51:59 +08:00
|
|
|
php_socket *php_sock;
|
2001-05-18 01:02:37 +08:00
|
|
|
|
2001-11-02 00:51:59 +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;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
|
2000-08-20 18:31:27 +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;
|
|
|
|
}
|
2000-08-20 18:31:27 +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
|
|
|
/* }}} */
|
|
|
|
|
2002-06-25 14:24:45 +08:00
|
|
|
/* {{{ proto int socket_last_error([resource socket])
|
|
|
|
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;
|
|
|
|
|
2002-04-29 22:52:29 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE)
|
|
|
|
return;
|
2001-11-02 00:51:59 +08:00
|
|
|
|
2002-04-29 22:52:29 +08:00
|
|
|
if (arg1) {
|
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
RETVAL_LONG(php_sock->error);
|
|
|
|
} else {
|
|
|
|
RETVAL_LONG(SOCKETS_G(last_error));
|
|
|
|
}
|
|
|
|
}
|
2002-03-06 13:21:56 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2002-05-01 06:03:51 +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;
|
|
|
|
|
2002-05-01 06:01:06 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE)
|
2002-04-29 23:06:48 +08:00
|
|
|
return;
|
2002-03-06 13:21:56 +08:00
|
|
|
|
2002-05-01 06:01:06 +08:00
|
|
|
if (arg1) {
|
|
|
|
ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
|
|
|
|
php_sock->error = 0;
|
|
|
|
} else {
|
|
|
|
SOCKETS_G(last_error) = 0;
|
|
|
|
}
|
2002-03-06 13:21:56 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2001-11-02 00:51:59 +08:00
|
|
|
/* }}} */
|
2002-03-06 13:21:56 +08:00
|
|
|
|
2001-05-18 01:02:37 +08:00
|
|
|
#endif
|
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
|
|
|
*/
|