php-src/ext/sockets/sockets.c

2332 lines
62 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
2001-12-11 23:32:16 +08:00
| PHP Version 4 |
+----------------------------------------------------------------------+
2003-01-01 00:08:15 +08:00
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| 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. |
+----------------------------------------------------------------------+
| Authors: Chris Vandomelen <chrisv@b0rked.dhs.org> |
| Sterling Hughes <sterling@php.net> |
2001-12-14 13:58:28 +08:00
| Jason Greene <jason@php.net> |
| WinSock: Daniel Beulshausen <daniel@php4win.de> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#if HAVE_SOCKETS
#ifndef _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED
#endif
#define _XPG4_2
#define __EXTENSIONS__
#include "php_network.h"
#include "ext/standard/info.h"
#include "php_ini.h"
#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>
# define IS_INVALID_SOCKET(a) (a->bsd_socket < 0)
# 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"
# define IS_INVALID_SOCKET(a) (a->bsd_socket == INVALID_SOCKET)
#endif
ZEND_DECLARE_MODULE_GLOBALS(sockets)
#ifndef MSG_WAITALL
#ifdef LINUX
#define MSG_WAITALL 0x00000100
#else
#define MSG_WAITALL 0x00000000
#endif
#endif
#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))
#endif
#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);
#define PHP_NORMAL_READ 0x0001
#define PHP_BINARY_READ 0x0002
#define PHP_SOCKET_ERROR(socket,msg,errn) socket->error = errn; \
SOCKETS_G(last_error) = errn; \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s [%d]: %s", msg, errn, php_strerror(errn TSRMLS_CC))
static int le_iov;
#define le_iov_name "Socket I/O vector"
static int le_socket;
#define le_socket_name "Socket"
static unsigned char first_through_third_args_force_ref[] =
{3, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE};
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};
static unsigned char fourth_arg_force_ref[] =
{4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
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};
/* {{{ sockets_functions[]
*/
function_entry sockets_functions[] = {
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)
PHP_FE(socket_create_pair, fourth_arg_force_ref)
2002-05-01 15:35:01 +08:00
PHP_FE(socket_accept, NULL)
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)
PHP_FE(socket_close, NULL)
2002-05-01 15:35:01 +08:00
PHP_FE(socket_write, NULL)
PHP_FE(socket_read, NULL)
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)
PHP_FE(socket_bind, NULL)
2002-03-09 14:41:19 +08:00
PHP_FE(socket_recv, second_arg_of_four_force_ref)
PHP_FE(socket_send, NULL)
PHP_FE(socket_recvfrom, second_fifth_and_sixth_args_force_ref)
PHP_FE(socket_sendto, NULL)
#ifdef HAVE_CMSGHDR
PHP_FE(socket_recvmsg, third_through_seventh_args_force_ref)
#endif
PHP_FE(socket_sendmsg, NULL)
PHP_FE(socket_readv, NULL)
PHP_FE(socket_writev, NULL)
PHP_FE(socket_get_option, NULL)
PHP_FE(socket_set_option, NULL)
PHP_FE(socket_shutdown, NULL)
PHP_FE(socket_last_error, NULL)
PHP_FE(socket_clear_error, NULL)
/* for downwards compatability */
PHP_FALIAS(socket_getopt, socket_get_option, NULL)
PHP_FALIAS(socket_setopt, socket_set_option, NULL)
{NULL, NULL, NULL}
};
/* }}} */
zend_module_entry sockets_module_entry = {
STANDARD_MODULE_HEADER,
"sockets",
sockets_functions,
PHP_MINIT(sockets),
NULL,
2002-09-30 10:09:42 +08:00
PHP_RINIT(sockets),
PHP_RSHUTDOWN(sockets),
PHP_MINFO(sockets),
2002-04-29 23:06:48 +08:00
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_SOCKETS
ZEND_GET_MODULE(sockets)
#endif
/* inet_ntop should be used instead of inet_ntoa */
int inet_ntoa_lock = 0;
static void php_destroy_iovec(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
unsigned int i;
php_iovec_t *iov = (php_iovec_t *) rsrc->ptr;
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);
}
}
static void php_destroy_socket(zend_rsrc_list_entry *rsrc TSRMLS_DC)
2001-06-04 05:38:23 +08:00
{
php_socket *php_sock = (php_socket *) rsrc->ptr;
close(php_sock->bsd_socket);
efree(php_sock);
}
static int php_open_listen_sock(php_socket **php_sock, int port, int backlog TSRMLS_DC)
{
struct sockaddr_in la;
struct hostent *hp;
php_socket *sock = (php_socket*)emalloc(sizeof(php_socket));
*php_sock = sock;
#ifndef PHP_WIN32
if ((hp = gethostbyname("0.0.0.0")) == NULL) {
#else
if ((hp = gethostbyname("localhost")) == NULL) {
#endif
efree(sock);
return 0;
}
memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
la.sin_family = hp->h_addrtype;
la.sin_port = htons((unsigned short) port);
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);
efree(sock);
return 0;
}
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);
close(sock->bsd_socket);
efree(sock);
return 0;
}
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);
close(sock->bsd_socket);
efree(sock);
return 0;
}
return 1;
}
static int php_accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la TSRMLS_DC)
{
socklen_t salen;
php_socket *out_sock = (php_socket*)emalloc(sizeof(php_socket));
*new_sock = out_sock;
salen = sizeof(*la);
out_sock->bsd_socket = accept(in_sock->bsd_socket, la, &salen);
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);
efree(out_sock);
return 0;
}
return 1;
}
/* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
static int php_read(int bsd_socket, void *buf, size_t maxlen, int flags)
{
int m = 0;
size_t n = 0;
int no_read = 0;
int nonblock = 0;
char *t = (char *) buf;
m = fcntl(bsd_socket, F_GETFL);
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.. */
}
if (no_read > 200) {
set_errno(ECONNRESET);
return -1;
}
}
if (n < maxlen) {
m = recv(bsd_socket, (void *) t, 1, flags);
}
if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
return -1;
}
set_errno(0);
}
if (n < maxlen) {
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..) */
}
return n;
}
/* }}} */
static char *php_strerror(int error TSRMLS_DC)
{
const char *buf;
#ifndef PHP_WIN32
if (error < -10000) {
2002-03-05 11:45:23 +08:00
error = -error - 10000;
#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);
}
#endif
} else {
buf = strerror(error);
}
#else
{
LPTSTR tmp = NULL;
2003-04-17 01:31:31 +08:00
buf = NULL;
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);
2003-04-17 01:31:31 +08:00
buf = SOCKETS_G(strerror_buf);
}
}
#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) */
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;
#ifdef HAVE_GETADDRINFO
struct addrinfo hints;
struct addrinfo *addrinfo = NULL;
#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 {
#ifdef HAVE_GETADDRINFO
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;
}
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");
freeaddrinfo(addrinfo);
2003-04-18 05:04:47 +08:00
return 0;
}
memcpy(&(sin6->sin6_addr.s6_addr), ((struct sockaddr_in6*)(addrinfo->ai_addr))->sin6_addr.s6_addr, sizeof(struct in6_addr));
freeaddrinfo(addrinfo);
#else
/* No IPv6 specific hostname resolution is available on this system? */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: getaddrinfo() not available on system.");
reurn 0;
#endif
2003-04-18 05:04:47 +08:00
}
return 1;
}
#endif
/* Sets addr by hostname, or by ip in string form (AF_INET) */
static int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock TSRMLS_DC)
{
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 */
#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;
}
if (host_entry->h_addrtype != AF_INET) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: Non AF_INET domain returned on AF_INET socket");
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
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;
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(sockets)
{
struct protoent *pe;
ZEND_INIT_MODULE_GLOBALS(sockets, php_sockets_init_globals, NULL);
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);
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
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);
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);
#ifndef WIN32
# include "unix_socket_constants.h"
#else
# include "win32_socket_constants.h"
#endif
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);
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(sockets)
{
php_info_print_table_start();
php_info_print_table_row(2, "Sockets Support", "enabled");
php_info_print_table_end();
}
/* }}} */
2002-09-30 10:09:42 +08:00
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(sockets)
{
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;
}
/* }}} */
static int php_sock_array_to_fd_set(zval *sock_array, fd_set *fds, PHP_SOCKET *max_fd TSRMLS_DC)
{
zval **element;
php_socket *php_sock;
if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
2002-03-11 09:24:42 +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
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 */
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;
}
}
2002-03-11 09:24:42 +08:00
return 1;
}
static int php_sock_array_from_fd_set(zval *sock_array, fd_set *fds TSRMLS_DC)
{
zval **element;
zval **dest_element;
php_socket *php_sock;
HashTable *new_hash;
if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
2002-03-11 09:24:42 +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
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 */
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);
}
}
/* Destroy old array, add new one */
zend_hash_destroy(Z_ARRVAL_P(sock_array));
efree(Z_ARRVAL_P(sock_array));
2002-03-11 09:24:42 +08:00
zend_hash_internal_pointer_reset(new_hash);
Z_ARRVAL_P(sock_array) = new_hash;
2002-03-11 09:24:42 +08:00
return 1;
}
2002-03-11 09:24:42 +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 */
PHP_FUNCTION(socket_select)
{
zval *r_array, *w_array, *e_array, *sec;
struct timeval tv;
2002-04-29 23:06:48 +08:00
struct timeval *tv_p = NULL;
fd_set rfds, wfds, efds;
PHP_SOCKET max_fd = 0;
int retval, sets = 0;
long usec = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!a!z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE)
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
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);
if (!sets) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no resource arrays were passed to select");
RETURN_FALSE;
}
/* 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;
}
/* 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);
if (retval == -1) {
SOCKETS_G(last_error) = errno;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to select [%d]: %s", errno, php_strerror(errno TSRMLS_CC));
RETURN_FALSE;
}
2002-03-11 09:24:42 +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
RETURN_LONG(retval);
}
/* }}} */
/* {{{ proto resource socket_create_listen(int port[, int backlog])
Opens a socket on port to accept connections */
PHP_FUNCTION(socket_create_listen)
{
2002-04-29 23:06:48 +08:00
php_socket *php_sock;
long port, backlog = 128;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &port, &backlog) == FAILURE)
return;
if (!php_open_listen_sock(&php_sock, port, backlog TSRMLS_CC)) {
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
}
/* }}} */
/* {{{ proto resource socket_accept(resource socket)
Accepts a connection on the listening socket fd */
PHP_FUNCTION(socket_accept)
{
zval *arg1;
php_socket *php_sock, *new_sock;
struct sockaddr_in sa;
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);
if (!php_accept_connect(php_sock, &new_sock, (struct sockaddr *) &sa TSRMLS_CC)) {
PHP_SOCKET_ERROR(new_sock, "unable to accept socket connection", errno);
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, new_sock, le_socket);
}
/* }}} */
/* {{{ proto bool socket_set_nonblock(resource socket)
Sets nonblocking mode on a socket resource */
PHP_FUNCTION(socket_set_nonblock)
{
zval *arg1;
php_socket *php_sock;
2002-04-29 23:06:48 +08:00
int flags;
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);
/* 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;
if (fcntl(php_sock->bsd_socket, F_SETFL, flags) > -1) {
RETURN_TRUE;
}
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;
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);
/* 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;
if (fcntl(php_sock->bsd_socket, F_SETFL, flags) > -1) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool socket_listen(resource socket[, int backlog])
Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
PHP_FUNCTION(socket_listen)
{
zval *arg1;
php_socket *php_sock;
long backlog = 0;
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);
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);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void socket_close(resource socket)
2000-08-23 21:47:00 +08:00
Closes a file descriptor */
PHP_FUNCTION(socket_close)
{
zval *arg1;
php_socket *php_sock;
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);
zend_list_delete(Z_RESVAL_P(arg1));
}
/* }}} */
/* {{{ proto int socket_write(resource socket, string buf[, int length])
Writes the buffer to the socket resource, length is optional */
PHP_FUNCTION(socket_write)
{
zval *arg1;
php_socket *php_sock;
int retval, str_len;
long length;
char *str;
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) {
length = str_len;
}
#ifndef PHP_WIN32
retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
#else
retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
#endif
if (retval < 0) {
PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
RETURN_FALSE;
}
RETURN_LONG(retval);
}
/* }}} */
2002-01-10 00:51:53 +08:00
/* {{{ proto string socket_read(resource socket, int length [, int type])
Reads a maximum of length bytes from socket */
PHP_FUNCTION(socket_read)
{
zval *arg1;
php_socket *php_sock;
char *tmpbuf;
int retval;
long length, type = PHP_BINARY_READ;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &arg1, &length, &type) == FAILURE)
return;
2002-12-09 21:35:57 +08:00
tmpbuf = emalloc(length + 1);
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);
}
if (retval == -1) {
PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
2000-09-15 05:48:15 +08:00
efree(tmpbuf);
RETURN_FALSE;
}
tmpbuf = erealloc(tmpbuf, retval + 1);
tmpbuf[retval] = '\0' ;
2001-11-29 16:18:50 +08:00
RETURN_STRINGL(tmpbuf, retval, 0);
}
/* }}} */
/* {{{ proto bool socket_getsockname(resource socket, string &addr[, int &port])
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. */
PHP_FUNCTION(socket_getsockname)
{
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
struct sockaddr_un *s_un;
char *addr_string;
socklen_t salen = sizeof(php_sockaddr_storage);
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);
sa = (struct sockaddr *) &sa_storage;
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);
RETURN_FALSE;
}
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
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;
zval_dtor(addr);
ZVAL_STRING(addr, addr_string, 1);
if (port != NULL) {
zval_dtor(port);
ZVAL_LONG(port, htons(sin->sin_port));
}
RETURN_TRUE;
2003-04-18 05:04:47 +08:00
break;
case AF_UNIX:
s_un = (struct sockaddr_un *) sa;
zval_dtor(addr);
ZVAL_STRING(addr, s_un->sun_path, 1);
RETURN_TRUE;
2003-04-18 05:04:47 +08:00
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto bool socket_getpeername(resource socket, string &addr[, int &port])
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. */
PHP_FUNCTION(socket_getpeername)
{
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
struct sockaddr_un *s_un;
char *addr_string;
socklen_t salen = sizeof(php_sockaddr_storage);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &arg1, &arg2, &arg3) == FAILURE)
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
sa = (struct sockaddr *) &sa_storage;
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);
RETURN_FALSE;
}
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
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
zval_dtor(arg2);
ZVAL_STRING(arg2, addr_string, 1);
if (arg3 != NULL) {
zval_dtor(arg3);
ZVAL_LONG(arg3, htons(sin->sin_port));
}
RETURN_TRUE;
2003-04-18 05:04:47 +08:00
break;
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;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
RETURN_FALSE;
}
}
/* }}} */
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 */
PHP_FUNCTION(socket_create)
{
long arg1, arg2, arg3;
php_socket *php_sock = (php_socket*)emalloc(sizeof(php_socket));
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);
return;
2001-11-12 08:58:16 +08:00
}
2003-04-18 05:04:47 +08:00
if (arg1 != AF_UNIX
#ifdef HAVE_IPV6
&& arg1 != AF_INET6
#endif
&& arg1 != AF_INET) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%d] specified for argument 1, assuming AF_INET", arg1);
arg1 = AF_INET;
}
if (arg2 > 10) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%d] specified for argument 2, assuming SOCK_STREAM", arg2);
arg2 = SOCK_STREAM;
}
php_sock->bsd_socket = socket(arg1, arg2, arg3);
php_sock->type = arg1;
if (IS_INVALID_SOCKET(php_sock)) {
SOCKETS_G(last_error) = errno;
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);
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
}
/* }}} */
/* {{{ 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)
{
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
struct sockaddr_un s_un;
char *addr;
int retval, addr_len;
2003-04-18 05:04:47 +08:00
long port;
int argc = ZEND_NUM_ARGS();
2003-04-18 05:04:47 +08:00
if (zend_parse_parameters(argc 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);
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;
}
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
case AF_INET:
2003-04-18 05:04:47 +08:00
if (argc != 3) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET requires 3 arguments");
RETURN_FALSE;
}
sin.sin_family = AF_INET;
sin.sin_port = htons((unsigned short int)port);
if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
RETURN_FALSE;
}
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:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
RETURN_FALSE;
}
if (retval != 0) {
2001-11-12 09:54:55 +08:00
PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string socket_strerror(int errno)
Returns a string describing an error */
PHP_FUNCTION(socket_strerror)
{
long arg1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &arg1) == FAILURE)
return;
2002-09-30 10:09:42 +08:00
RETURN_STRING(php_strerror(arg1 TSRMLS_CC), 1);
}
/* }}} */
/* {{{ 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)
{
zval *arg1;
php_sockaddr_storage sa_storage;
struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
php_socket *php_sock;
char *addr;
int addr_len;
long port = 0;
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);
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;
}
case AF_INET:
{
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
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)) {
RETURN_FALSE;
}
2002-04-29 23:06:48 +08:00
retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
2001-11-12 08:58:16 +08:00
break;
}
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
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);
RETURN_FALSE;
}
if (retval != 0) {
2001-11-12 09:54:55 +08:00
PHP_SOCKET_ERROR(php_sock, "unable to bind address", errno);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ 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 */
/* First parameter is number of vectors, each additional parameter is the
length of the vector to create.
*/
PHP_FUNCTION(socket_iovec_alloc)
{
zval ***args = (zval ***)NULL;
php_iovec_t *vector;
struct iovec *vector_array;
int i, j, num_vectors, argc = ZEND_NUM_ARGS();
args = emalloc(argc*sizeof(zval**));
if (argc < 1 || zend_get_parameters_array_ex(argc, args) == FAILURE) {
efree(args);
WRONG_PARAM_COUNT;
}
convert_to_long_ex(args[0]);
num_vectors = Z_LVAL_PP(args[0]);
vector_array = emalloc(sizeof(struct iovec)*(num_vectors+1));
for (i = 0, j = 1; i < num_vectors; i++, j++) {
convert_to_long_ex(args[j]);
vector_array[i].iov_base = (char*)emalloc(Z_LVAL_PP(args[j]));
vector_array[i].iov_len = Z_LVAL_PP(args[j]);
}
efree(args);
vector = emalloc(sizeof(php_iovec_t));
vector->iov_array = vector_array;
vector->count = num_vectors;
ZEND_REGISTER_RESOURCE(return_value, vector, le_iov);
}
/* }}} */
/* {{{ proto string socket_iovec_fetch(resource iovec, int iovec_position)
Returns the data held in the iovec specified by iovec_id[iovec_position] */
PHP_FUNCTION(socket_iovec_fetch)
{
2001-11-02 04:10:17 +08:00
zval *iovec_id;
php_iovec_t *vector;
unsigned long iovec_position;
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);
if (iovec_position >= vector->count) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't access a vector position past the amount of vectors set in the array");
RETURN_EMPTY_STRING();
}
RETURN_STRINGL(vector->iov_array[iovec_position].iov_base, vector->iov_array[iovec_position].iov_len, 1);
}
/* }}} */
/* {{{ proto bool socket_iovec_set(resource iovec, int iovec_position, string new_val)
Sets the data held in iovec_id[iovec_position] to new_val */
PHP_FUNCTION(socket_iovec_set)
{
2001-11-02 04:10:17 +08:00
zval *iovec_id;
php_iovec_t *vector;
int new_val_len;
unsigned long iovec_position;
2001-11-02 04:10:17 +08:00
char *new_val;
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);
if (iovec_position >= vector->count) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't access a vector position outside of the vector array bounds");
RETURN_FALSE;
}
if (vector->iov_array[iovec_position].iov_base) {
efree(vector->iov_array[iovec_position].iov_base);
}
vector->iov_array[iovec_position].iov_base = estrdup(new_val);
vector->iov_array[iovec_position].iov_len = strlen(new_val);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool socket_iovec_add(resource iovec, int iov_len)
Adds a new vector to the scatter/gather array */
PHP_FUNCTION(socket_iovec_add)
{
zval *iovec_id;
php_iovec_t *vector;
struct iovec *vector_array;
long iov_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &iovec_id, &iov_len) == FAILURE)
return;
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &iovec_id, -1, le_iov_name, le_iov);
vector_array = (struct iovec*)emalloc(sizeof(struct iovec) * (vector->count + 2));
memcpy(vector_array, vector->iov_array, sizeof(struct iovec) * vector->count);
vector_array[vector->count].iov_base = (char*)emalloc(iov_len);
vector_array[vector->count].iov_len = iov_len;
efree(vector->iov_array);
vector->iov_array = vector_array;
vector->count++;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool socket_iovec_delete(resource iovec, int iov_pos)
Deletes a vector from an array of vectors */
PHP_FUNCTION(socket_iovec_delete)
{
zval *iovec_id;
php_iovec_t *vector;
struct iovec *vector_array;
unsigned int i;
unsigned long iov_pos;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &iovec_id, &iov_pos) == FAILURE)
return;
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &iovec_id, -1, le_iov_name, le_iov);
if (iov_pos > vector->count) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't delete an IO vector that is out of array bounds");
RETURN_FALSE;
}
vector_array = emalloc(vector->count * sizeof(struct iovec));
for (i = 0; i < vector->count; i++) {
if (i < iov_pos) {
memcpy(&(vector->iov_array[i]), &(vector_array[i]), sizeof(struct iovec));
} else if (i > iov_pos) {
memcpy(&(vector->iov_array[i]), &(vector_array[i - 1]), sizeof(struct iovec));
}
}
efree(vector->iov_array);
vector->iov_array = vector_array;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool socket_iovec_free(resource iovec)
Frees the iovec specified by iovec_id */
PHP_FUNCTION(socket_iovec_free)
{
zval *iovec_id;
php_iovec_t *vector;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &iovec_id) == FAILURE)
return;
ZEND_FETCH_RESOURCE(vector, php_iovec_t *, &iovec_id, -1, le_iov_name, le_iov);
zend_list_delete(Z_RESVAL_P(iovec_id));
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool socket_readv(resource socket, resource iovec_id)
Reads from an fd, using the scatter-gather array defined by iovec_id */
PHP_FUNCTION(socket_readv)
{
zval *arg1, *arg2;
php_iovec_t *vector;
php_socket *php_sock;
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);
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);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool socket_writev(resource socket, resource iovec_id)
Writes to a file descriptor, fd, using the scatter-gather array defined by iovec_id */
PHP_FUNCTION(socket_writev)
{
zval *arg1, *arg2;
php_iovec_t *vector;
php_socket *php_sock;
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);
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);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ 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 */
PHP_FUNCTION(socket_recv)
{
zval *php_sock_res, *buf;
char *recv_buf;
php_socket *php_sock;
int retval;
long len, flags;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzll", &php_sock_res, &buf, &len, &flags) == FAILURE)
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &php_sock_res, -1, le_socket_name, le_socket);
recv_buf = emalloc(len + 1);
memset(recv_buf, 0, len + 1);
if ((retval = recv(php_sock->bsd_socket, recv_buf, len, flags)) < 1) {
efree(recv_buf);
2002-04-29 23:06:48 +08:00
zval_dtor(buf);
2002-04-29 23:06:48 +08:00
Z_TYPE_P(buf) = IS_NULL;
} else {
2002-05-08 23:30:04 +08:00
recv_buf[retval] = '\0';
/* 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;
}
2002-04-29 23:06:48 +08:00
if (retval == -1) {
2002-04-29 23:06:48 +08:00
PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
RETURN_FALSE;
}
2002-04-29 23:06:48 +08:00
RETURN_LONG(retval);
}
/* }}} */
/* {{{ 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 */
PHP_FUNCTION(socket_send)
{
zval *arg1;
php_socket *php_sock;
int buf_len, retval;
long len, flags;
char *buf;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsll", &arg1, &buf, &buf_len, &len, &flags) == FAILURE)
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
retval = send(php_sock->bsd_socket, buf, (buf_len < len ? buf_len : len), flags);
if (retval == -1) {
2002-04-29 23:06:48 +08:00
PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
RETURN_FALSE;
}
RETURN_LONG(retval);
}
/* }}} */
/* {{{ 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 */
PHP_FUNCTION(socket_recvfrom)
{
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
socklen_t slen;
int retval;
long arg3, arg4;
char *recv_buf, *address;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzllz|z", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE)
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
recv_buf = emalloc(arg3 + 2);
memset(recv_buf, 0, arg3 + 2);
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
if (retval < 0) {
efree(recv_buf);
2001-11-12 09:54:55 +08:00
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
RETURN_FALSE;
}
zval_dtor(arg2);
zval_dtor(arg5);
ZVAL_STRINGL(arg2, recv_buf, retval, 0);
ZVAL_STRING(arg5, s_un.sun_path, 1);
break;
case AF_INET:
slen = sizeof(sin);
memset(&sin, 0, slen);
sin.sin_family = AF_INET;
if (arg6 == NULL) {
WRONG_PARAM_COUNT;
}
2002-04-29 23:06:48 +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
if (retval < 0) {
efree(recv_buf);
2001-11-12 09:54:55 +08:00
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
RETURN_FALSE;
}
2002-04-29 23:06:48 +08:00
zval_dtor(arg2);
zval_dtor(arg5);
zval_dtor(arg6);
address = inet_ntoa(sin.sin_addr);
ZVAL_STRINGL(arg2, recv_buf, retval, 0);
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);
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
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
RETURN_FALSE;
}
RETURN_LONG(retval);
}
/* }}} */
/* {{{ proto int socket_sendto(resource socket, string buf, int len, int flags, string addr [, int port])
Sends a message to a socket, whether it is connected or not */
PHP_FUNCTION(socket_sendto)
{
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
int retval, buf_len, addr_len;
long len, flags, port = 0;
char *buf, *addr;
2003-04-18 05:04:47 +08:00
int argc = ZEND_NUM_ARGS();
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)
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
switch (php_sock->type) {
case AF_UNIX:
2001-06-13 00:41:51 +08:00
memset(&s_un, 0, sizeof(s_un));
s_un.sun_family = AF_UNIX;
snprintf(s_un.sun_path, 108, "%s", addr);
retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
break;
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-06-13 00:41:51 +08:00
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons((unsigned short) port);
if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
RETURN_FALSE;
}
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));
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
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
RETURN_FALSE;
}
if (retval == -1) {
2002-04-29 23:06:48 +08:00
PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
RETURN_FALSE;
}
RETURN_LONG(retval);
}
/* }}} */
/* {{{ proto bool socket_recvmsg(resource socket, resource iovec, array &control, int &controllen, int &flags, string &addr [, int &port])
Used to receive messages on a socket, whether connection-oriented or not */
#ifdef HAVE_CMSGHDR
PHP_FUNCTION(socket_recvmsg)
{
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
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);
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);
RETURN_FALSE;
}
ctl_buf = (Z_LVAL_P(arg4) > sizeof(struct cmsghdr)) ? (struct cmsghdr*)emalloc(Z_LVAL_P(arg4)) : NULL;
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
case AF_INET:
if (arg7 == NULL) {
efree(ctl_buf);
WRONG_PARAM_COUNT;
}
memset(sa, 0, sizeof(sa_storage));
2002-08-16 22:23:27 +08:00
hdr.msg_name = (void *) sin;
hdr.msg_namelen = sizeof(sa_storage);
hdr.msg_iov = iov->iov_array;
hdr.msg_iovlen = iov->count;
2002-08-16 22:23:27 +08:00
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) {
2001-11-12 09:54:55 +08:00
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);
2002-04-29 23:06:48 +08:00
ZVAL_LONG(arg4, hdr.msg_controllen);
#ifndef MISSING_MSGHDR_MSGFLAGS
ZVAL_LONG(arg5, hdr.msg_flags);
#endif
ZVAL_LONG(arg7, ntohs(sin->sin_port));
2002-04-29 23:06:48 +08:00
array_init(arg3);
2002-04-29 23:06:48 +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
{
char *tmp = inet_ntoa(sin->sin_addr);
if (tmp == NULL) {
ZVAL_STRING(arg6, "0.0.0.0", 1);
} else {
ZVAL_STRING(arg6, tmp, 1);
}
}
2002-04-29 23:06:48 +08:00
RETURN_TRUE;
}
2003-04-18 05:04:47 +08:00
break;
case AF_UNIX:
memset(sa, 0, sizeof(sa_storage));
2002-08-16 22:23:27 +08:00
hdr.msg_name = (void *) s_un;
hdr.msg_namelen = sizeof(struct sockaddr_un);
hdr.msg_iov = iov->iov_array;
hdr.msg_iovlen = iov->count;
if (ctl_buf) {
2002-08-16 22:23:27 +08:00
hdr.msg_control = (void *) ctl_buf;
hdr.msg_controllen = Z_LVAL_P(arg4);
} else {
hdr.msg_control = NULL;
hdr.msg_controllen = 0;
}
#ifndef MISSING_MSGHDR_MSGFLAGS
hdr.msg_flags = 0;
#endif
2002-04-29 23:06:48 +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);
RETURN_FALSE;
} else {
struct cmsghdr *mhdr = (struct cmsghdr *) hdr.msg_control;
if (mhdr != NULL) {
2002-04-29 23:06:48 +08:00
zval_dtor(arg3);
zval_dtor(arg4);
zval_dtor(arg5);
zval_dtor(arg6);
2002-04-29 23:06:48 +08:00
ZVAL_LONG(arg4, hdr.msg_controllen);
#ifndef MISSING_MSGHDR_MSGFLAGS
ZVAL_LONG(arg5, hdr.msg_flags);
#endif
2002-04-29 23:06:48 +08:00
array_init(arg3);
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
ZVAL_STRING(arg6, s_un->sun_path, 1);
RETURN_TRUE;
}
2003-04-18 05:04:47 +08:00
break;
2002-04-29 23:06:48 +08:00
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
RETURN_FALSE;
}
}
#endif
/* }}} */
/* {{{ proto bool socket_sendmsg(resource socket, resource iovec, int flags, string addr [, int port])
Sends a message to a socket, regardless of whether it is connection-oriented or not */
PHP_FUNCTION(socket_sendmsg)
{
zval *arg1, *arg2;
php_iovec_t *iov;
php_socket *php_sock;
struct sockaddr sa;
char *addr;
socklen_t salen;
int addr_len;
long flags, port;
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);
salen = sizeof(sa);
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);
RETURN_FALSE;
}
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
case AF_INET:
{
struct msghdr hdr;
struct sockaddr_in *sin = (struct sockaddr_in *) &sa;
2002-04-29 23:06:48 +08:00
set_h_errno(0);
set_errno(0);
2002-04-29 23:06:48 +08:00
memset(&hdr, 0, sizeof(hdr));
2002-08-16 22:23:27 +08:00
hdr.msg_name = (void *) &sa;
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));
sin->sin_family = AF_INET;
sin->sin_port = htons((unsigned short)port);
2002-04-29 23:06:48 +08:00
if (! php_set_inet_addr(sin, addr, php_sock TSRMLS_CC)) {
RETURN_FALSE;
}
2002-04-29 23:06:48 +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);
}
2002-04-29 23:06:48 +08:00
RETURN_TRUE;
}
2003-04-18 05:04:47 +08:00
break;
2002-04-29 23:06:48 +08:00
case AF_UNIX:
{
struct msghdr hdr;
struct sockaddr_un *s_un = (struct sockaddr_un *) &sa;
2002-04-29 23:06:48 +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;
hdr.msg_iov = iov->iov_array;
hdr.msg_iovlen = iov->count;
2002-04-29 23:06:48 +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
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);
RETURN_FALSE;
}
RETURN_TRUE;
}
2003-04-18 05:04:47 +08:00
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa.sa_family);
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto mixed socket_get_option(resource socket, int level, int optname)
Gets socket options for the socket */
PHP_FUNCTION(socket_get_option)
{
zval *arg1;
struct linger linger_val;
struct timeval tv;
socklen_t optlen;
php_socket *php_sock;
int other_val;
long level, optname;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &arg1, &level, &optname) == FAILURE)
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
2002-04-29 23:06:48 +08:00
switch(optname) {
case SO_LINGER:
optlen = sizeof(linger_val);
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;
}
array_init(return_value);
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);
2002-04-29 23:06:48 +08:00
if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
RETURN_FALSE;
}
array_init(return_value);
2002-04-29 23:06:48 +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
break;
default:
optlen = sizeof(other_val);
2002-04-29 23:06:48 +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;
}
RETURN_LONG(other_val);
break;
}
}
/* }}} */
/* {{{ proto bool socket_set_option(resource socket, int level, int optname, int|array optval)
Sets socket options for the socket */
PHP_FUNCTION(socket_set_option)
{
zval *arg1, *arg4;
struct linger lv;
struct timeval tv;
php_socket *php_sock;
int ov, optlen, retval;
long level, optname;
void *opt_ptr;
2002-04-29 23:06:48 +08:00
HashTable *opt_ht;
zval **l_onoff, **l_linger;
2002-04-29 23:06:48 +08:00
zval **sec, **usec;
/* 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";
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);
set_errno(0);
switch (optname) {
2002-04-29 23:06:48 +08:00
case SO_LINGER:
convert_to_array_ex(&arg4);
opt_ht = HASH_OF(arg4);
if (zend_hash_find(opt_ht, l_onoff_key, strlen(l_onoff_key) + 1, (void **)&l_onoff) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", l_onoff_key);
RETURN_FALSE;
}
if (zend_hash_find(opt_ht, l_linger_key, strlen(l_linger_key) + 1, (void **)&l_linger) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", l_linger_key);
RETURN_FALSE;
}
convert_to_long_ex(l_onoff);
convert_to_long_ex(l_linger);
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);
optlen = sizeof(lv);
2002-04-29 23:06:48 +08:00
opt_ptr = &lv;
break;
case SO_RCVTIMEO:
case SO_SNDTIMEO:
convert_to_array_ex(&arg4);
opt_ht = HASH_OF(arg4);
if (zend_hash_find(opt_ht, sec_key, strlen(sec_key) + 1, (void **)&sec) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", sec_key);
RETURN_FALSE;
}
if (zend_hash_find(opt_ht, usec_key, strlen(usec_key) + 1, (void **)&usec) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", usec_key);
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);
2002-04-29 23:06:48 +08:00
optlen = sizeof(tv);
opt_ptr = &tv;
break;
default:
2002-04-29 23:06:48 +08:00
convert_to_long_ex(&arg4);
ov = Z_LVAL_P(arg4);
2002-04-29 23:06:48 +08:00
optlen = sizeof(ov);
2002-04-29 23:06:48 +08:00
opt_ptr = &ov;
break;
}
retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
2002-04-29 23:06:48 +08:00
if (retval != 0) {
2001-11-12 09:54:55 +08:00
PHP_SOCKET_ERROR(php_sock, "unable to set socket option", errno);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool socket_create_pair(int domain, int type, int protocol, array &fd)
Creates a pair of indistinguishable sockets and stores them in fds. */
PHP_FUNCTION(socket_create_pair)
{
zval *retval[2], *fds_array_zval;
php_socket *php_sock[2];
PHP_SOCKET fds_array[2];
long domain, type, protocol;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lllz", &domain, &type, &protocol, &fds_array_zval) == FAILURE)
return;
php_sock[0] = (php_socket*)emalloc(sizeof(php_socket));
php_sock[1] = (php_socket*)emalloc(sizeof(php_socket));
2003-04-18 05:04:47 +08:00
if (domain != AF_INET
#ifdef HAVE_IPV6
&& domain != AF_INET6
#endif
&& domain != AF_UNIX) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%d] specified for argument 1, assuming AF_INET", domain);
domain = AF_INET;
}
if (type > 10) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%d] specified for argument 2, assuming SOCK_STREAM", type);
type = SOCK_STREAM;
}
if (socketpair(domain, type, protocol, fds_array) != 0) {
SOCKETS_G(last_error) = errno;
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]);
RETURN_FALSE;
}
zval_dtor(fds_array_zval);
array_init(fds_array_zval);
MAKE_STD_ZVAL(retval[0]);
MAKE_STD_ZVAL(retval[1]);
php_sock[0]->bsd_socket = fds_array[0];
php_sock[1]->bsd_socket = fds_array[1];
php_sock[0]->type = domain;
php_sock[1]->type = domain;
ZEND_REGISTER_RESOURCE(retval[0], php_sock[0], le_socket);
ZEND_REGISTER_RESOURCE(retval[1], php_sock[1], le_socket);
add_index_zval(fds_array_zval, 0, retval[0]);
add_index_zval(fds_array_zval, 1, retval[1]);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool socket_shutdown(resource socket[, int how])
Shuts down a socket for receiving, sending, or both. */
PHP_FUNCTION(socket_shutdown)
{
zval *arg1;
long how_shutdown = 2;
php_socket *php_sock;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &how_shutdown) == FAILURE)
2002-04-29 23:06:48 +08:00
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
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);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
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) */
PHP_FUNCTION(socket_last_error)
{
zval *arg1 = NULL;
php_socket *php_sock;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE)
return;
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-05-01 06:03:51 +08:00
/* {{{ proto void socket_clear_error([resource socket])
Clears the error on the socket or the last error code. */
PHP_FUNCTION(socket_clear_error)
{
zval *arg1 = NULL;
php_socket *php_sock;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE)
2002-04-29 23:06:48 +08:00
return;
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;
}
return;
}
/* }}} */
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/