diff options
author | Jason Greene <jason@php.net> | 2003-08-16 06:34:36 +0000 |
---|---|---|
committer | Jason Greene <jason@php.net> | 2003-08-16 06:34:36 +0000 |
commit | 90289924c031c5b0b2aa1f99ecf317640c926a62 (patch) | |
tree | d08435172a40ac3831ba4a80892be6aae2cdeabe /ext/sockets | |
parent | 78a3fe8fca8aff9a034a4300edc01f0b0f4ed4a5 (diff) | |
download | php-git-90289924c031c5b0b2aa1f99ecf317640c926a62.tar.gz |
Remove all vector based functions for the following reasons:
- This solves alot of platform compatibility problems
- The possible security issue of allocating an incredibly large vector
pool is prevented
- They are of little to no benefit in a high level language
- 99% of all things done with these functions can be done using
sendto/recvfrom
Diffstat (limited to 'ext/sockets')
-rw-r--r-- | ext/sockets/php_sockets.h | 17 | ||||
-rw-r--r-- | ext/sockets/php_sockets_win.c | 72 | ||||
-rw-r--r-- | ext/sockets/php_sockets_win.h | 29 | ||||
-rw-r--r-- | ext/sockets/sockets.c | 588 |
4 files changed, 0 insertions, 706 deletions
diff --git a/ext/sockets/php_sockets.h b/ext/sockets/php_sockets.h index e7b7365ffa..043f83c3d6 100644 --- a/ext/sockets/php_sockets.h +++ b/ext/sockets/php_sockets.h @@ -44,12 +44,6 @@ PHP_MINFO_FUNCTION(sockets); PHP_RINIT_FUNCTION(sockets); PHP_RSHUTDOWN_FUNCTION(sockets); -PHP_FUNCTION(socket_iovec_alloc); -PHP_FUNCTION(socket_iovec_free); -PHP_FUNCTION(socket_iovec_set); -PHP_FUNCTION(socket_iovec_fetch); -PHP_FUNCTION(socket_iovec_add); -PHP_FUNCTION(socket_iovec_delete); PHP_FUNCTION(socket_select); PHP_FUNCTION(socket_create_listen); PHP_FUNCTION(socket_create_pair); @@ -70,23 +64,12 @@ PHP_FUNCTION(socket_recv); PHP_FUNCTION(socket_send); PHP_FUNCTION(socket_recvfrom); PHP_FUNCTION(socket_sendto); -#ifdef HAVE_CMSGHDR -PHP_FUNCTION(socket_recvmsg); -#endif -PHP_FUNCTION(socket_sendmsg); -PHP_FUNCTION(socket_readv); -PHP_FUNCTION(socket_writev); PHP_FUNCTION(socket_get_option); PHP_FUNCTION(socket_set_option); PHP_FUNCTION(socket_shutdown); PHP_FUNCTION(socket_last_error); PHP_FUNCTION(socket_clear_error); -typedef struct php_iovec { - struct iovec *iov_array; - unsigned int count; -} php_iovec_t; - #ifndef PHP_WIN32 typedef int PHP_SOCKET; #else diff --git a/ext/sockets/php_sockets_win.c b/ext/sockets/php_sockets_win.c index b5407a748b..9a749b64aa 100644 --- a/ext/sockets/php_sockets_win.c +++ b/ext/sockets/php_sockets_win.c @@ -31,78 +31,6 @@ #include "php_sockets.h" #include "php_sockets_win.h" -ssize_t readv(SOCKET sock, const struct iovec *iov, int iovcnt) { - size_t bytes, remain, len, pos = 0; - ssize_t retval; - int i; - char *buffer = NULL; - - for(bytes=0, i=0; i<iovcnt; i++) { - bytes += iov[i].iov_len; - } - - buffer = (char*)emalloc(bytes); - if (buffer == NULL) { - return -1; - } - - retval = recv(sock, buffer, bytes, 0); - - if(retval < 0) { - efree(buffer); - return retval; - } - - remain = bytes = (size_t) retval; - - for(i=0; i<iovcnt; i++) { - len = ((unsigned int)iov[i].iov_len < remain) ? iov[i].iov_len : remain; - memcpy(iov[i].iov_base, buffer+pos, len); - pos += len; - remain -= len; - } - - efree(buffer); - return bytes; -} - -ssize_t writev(SOCKET sock, const struct iovec *iov, int iovcnt) { - size_t bytes, pos = 0; - ssize_t retval; - int i; - char *buffer = NULL; - - for(bytes=0, i=0; i<iovcnt; i++) { - bytes += iov[i].iov_len; - } - - buffer = (char*)emalloc(bytes); - - if(buffer == NULL) { - return -1; - } - - for(i=0; i<iovcnt; i++) { - memcpy(buffer+pos, iov[i].iov_base, iov[i].iov_len); - pos += iov[i].iov_len; - } - - retval = send(sock, buffer, bytes, 0); - efree(buffer); - - return retval; -} - -ssize_t recvmsg(SOCKET sock, struct msghdr *msg, int flags) { - set_errno(WSAEOPNOTSUPP); - return -1; -} - -ssize_t sendmsg(SOCKET sock, struct msghdr *msg, int flags) { - set_errno(WSAEOPNOTSUPP); - return -1; -} - int socketpair(int domain, int type, int protocol, SOCKET sock[2]) { struct sockaddr_in address; SOCKET redirect; diff --git a/ext/sockets/php_sockets_win.h b/ext/sockets/php_sockets_win.h index 5c6e7f277c..5adb8934de 100644 --- a/ext/sockets/php_sockets_win.h +++ b/ext/sockets/php_sockets_win.h @@ -43,41 +43,12 @@ #define set_errno(a) WSASetLastError(a) #define set_h_errno(a) WSASetLastError(a) #define close(a) closesocket(a) -#define CMSG_DATA(cmsg) ((cmsg)->cmsg_data) - -typedef int ssize_t; struct sockaddr_un { short sun_family; char sun_path[108]; }; -struct iovec { - char * iov_base; - int iov_len; -}; - -struct msghdr { - void* msg_name; - socklen_t msg_namelen; - struct iovec* msg_iov; - int msg_iovlen; - void* msg_control; - socklen_t msg_controllen; - int msg_flags; -}; - -struct cmsghdr { - socklen_t cmsg_len; - int cmsg_level; - int cmsg_type; - unsigned char cmsg_data[]; -}; - -ssize_t readv(SOCKET sock, const struct iovec *iov, int iovcnt); -ssize_t writev(SOCKET sock, const struct iovec *iov, int iovcnt); -ssize_t recvmsg(SOCKET sock, struct msghdr *msg, int flags); -ssize_t sendmsg(SOCKET sock, struct msghdr *msg, int flags); int socketpair(int domain, int type, int protocol, SOCKET sock[2]); int inet_aton(const char *cp, struct in_addr *inp); int fcntl(int fd, int cmd, ...); diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c index 237a66a824..d782f2629e 100644 --- a/ext/sockets/sockets.c +++ b/ext/sockets/sockets.c @@ -29,13 +29,6 @@ #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" @@ -91,8 +84,6 @@ static char *php_strerror(int error TSRMLS_DC); 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" @@ -134,12 +125,6 @@ static /* {{{ 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) PHP_FE(socket_select, first_through_third_args_force_ref) PHP_FE(socket_create, NULL) PHP_FE(socket_create_listen, NULL) @@ -160,12 +145,6 @@ function_entry sockets_functions[] = { 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) @@ -201,21 +180,6 @@ ZEND_GET_MODULE(sockets) /* 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) { php_socket *php_sock = (php_socket *) rsrc->ptr; @@ -480,7 +444,6 @@ PHP_MINIT_FUNCTION(sockets) 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); @@ -1255,229 +1218,6 @@ PHP_FUNCTION(socket_bind) } /* }}} */ -/* {{{ proto resource socket_iovec_alloc(int num_vectors [, int ...]) - 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 = safe_emalloc(argc, sizeof(zval**), 0); - - 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 = safe_emalloc(sizeof(struct iovec), (num_vectors+1), 0); - - 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) -{ - 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) -{ - zval *iovec_id; - php_iovec_t *vector; - int new_val_len; - unsigned long iovec_position; - 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*)safe_emalloc(sizeof(struct iovec), (vector->count + 2), 0); - 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 = safe_emalloc(vector->count, sizeof(struct iovec), 0); - - 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) { - 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) { - 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) Receives data from a connected socket */ PHP_FUNCTION(socket_recv) @@ -1733,334 +1473,6 @@ PHP_FUNCTION(socket_sendto) } /* }}} */ -/* {{{ 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; -#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) { - 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) { -#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)); - hdr.msg_name = (void *) sin; - 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(sin->sin_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 = inet_ntoa(sin->sin_addr); - if (tmp == NULL) { - ZVAL_STRING(arg6, "0.0.0.0", 1); - } else { - ZVAL_STRING(arg6, tmp, 1); - } - } - - RETURN_TRUE; - } - break; - - case AF_UNIX: - memset(sa, 0, sizeof(sa_storage)); - 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) { - 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 - - - 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; - - if (mhdr != NULL) { - - zval_dtor(arg3); - zval_dtor(arg4); - zval_dtor(arg5); - zval_dtor(arg6); - - ZVAL_LONG(arg4, hdr.msg_controllen); -#ifndef MISSING_MSGHDR_MSGFLAGS - ZVAL_LONG(arg5, hdr.msg_flags); -#endif - - 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); - } - - - ZVAL_STRING(arg6, s_un->sun_path, 1); - RETURN_TRUE; - } - break; - - 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) { - PHP_SOCKET_ERROR(php_sock, "unable to send messge", errno); - RETURN_FALSE; - } - - switch(sa.sa_family) { -#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; - - 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(sin, 0, sizeof(sa)); - - 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; - } - - if (sendmsg(php_sock->bsd_socket, &hdr, flags) == -1) { - PHP_SOCKET_ERROR(php_sock, "unable to send message", errno); - } - - RETURN_TRUE; - } - break; - - case AF_UNIX: - { - struct msghdr hdr; - struct sockaddr_un *s_un = (struct sockaddr_un *) &sa; - - set_errno(0); - - hdr.msg_name = (void *) s_un; - hdr.msg_iov = iov->iov_array; - hdr.msg_iovlen = iov->count; - - snprintf(s_un->sun_path, 108, "%s", addr); - - hdr.msg_namelen = SUN_LEN(s_un); - - if (sendmsg(php_sock->bsd_socket, &hdr, flags) == -1) { - PHP_SOCKET_ERROR(php_sock, "unable to send message", errno); - RETURN_FALSE; - } - - RETURN_TRUE; - } - 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) |