Skip to content

Commit

Permalink
Merge branch 'PHP-5.4' into PHP-5.5
Browse files Browse the repository at this point in the history
* PHP-5.4:
  Fix wrong blocking state being set
  Fix tests (Windows)
  Remove a Windows only warning
  Move & improve PHP_SOCKET_ERROR def
  Move some declarations to sockets.c
  Fix overbroad skipif include
  • Loading branch information
cataphract committed Feb 2, 2013
2 parents 114245c + 73de4d8 commit e8f0e86
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 59 deletions.
49 changes: 7 additions & 42 deletions ext/sockets/php_sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,6 @@ extern zend_module_entry sockets_module_entry;
#endif
#endif

PHP_MINIT_FUNCTION(sockets);
PHP_MINFO_FUNCTION(sockets);
PHP_RSHUTDOWN_FUNCTION(sockets);

PHP_FUNCTION(socket_select);
PHP_FUNCTION(socket_create_listen);
#ifdef HAVE_SOCKETPAIR
PHP_FUNCTION(socket_create_pair);
#endif
PHP_FUNCTION(socket_accept);
PHP_FUNCTION(socket_set_nonblock);
PHP_FUNCTION(socket_set_block);
PHP_FUNCTION(socket_listen);
PHP_FUNCTION(socket_close);
PHP_FUNCTION(socket_write);
PHP_FUNCTION(socket_read);
PHP_FUNCTION(socket_getsockname);
PHP_FUNCTION(socket_getpeername);
PHP_FUNCTION(socket_create);
PHP_FUNCTION(socket_connect);
PHP_FUNCTION(socket_strerror);
PHP_FUNCTION(socket_bind);
PHP_FUNCTION(socket_recv);
PHP_FUNCTION(socket_send);
PHP_FUNCTION(socket_recvfrom);
PHP_FUNCTION(socket_sendto);
PHP_FUNCTION(socket_get_option);
PHP_FUNCTION(socket_set_option);
#ifdef HAVE_SHUTDOWN
PHP_FUNCTION(socket_shutdown);
#endif
PHP_FUNCTION(socket_last_error);
PHP_FUNCTION(socket_clear_error);
PHP_FUNCTION(socket_import_stream);

#ifndef PHP_WIN32
typedef int PHP_SOCKET;
# define PHP_SOCKETS_API PHPAPI
Expand Down Expand Up @@ -99,13 +64,13 @@ PHP_SOCKETS_API int php_sockets_le_socket(void);

#define php_sockets_le_socket_name "Socket"

/* Prototypes */
#ifdef ilia_0 /* not needed, only causes a compiler warning */
static int php_open_listen_sock(php_socket **php_sock, int port, int backlog TSRMLS_DC);
static int php_accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la TSRMLS_DC);
static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags);
static char *php_strerror(int error TSRMLS_DC);
#endif
#define PHP_SOCKET_ERROR(socket, msg, errn) \
do { \
int _err = (errn); /* save value to avoid repeated calls to WSAGetLastError() on Windows */ \
(socket)->error = _err; \
SOCKETS_G(last_error) = _err; \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s [%d]: %s", msg, _err, php_strerror(_err TSRMLS_CC)); \
} while (0)

ZEND_BEGIN_MODULE_GLOBALS(sockets)
int last_error;
Expand Down
41 changes: 36 additions & 5 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ 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_socket;
#define le_socket_name php_sockets_le_socket_name

Expand Down Expand Up @@ -280,6 +276,41 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_import_stream, 0, 0, 1)
ZEND_END_ARG_INFO()
/* }}} */

PHP_MINIT_FUNCTION(sockets);
PHP_MINFO_FUNCTION(sockets);
PHP_RSHUTDOWN_FUNCTION(sockets);

PHP_FUNCTION(socket_select);
PHP_FUNCTION(socket_create_listen);
#ifdef HAVE_SOCKETPAIR
PHP_FUNCTION(socket_create_pair);
#endif
PHP_FUNCTION(socket_accept);
PHP_FUNCTION(socket_set_nonblock);
PHP_FUNCTION(socket_set_block);
PHP_FUNCTION(socket_listen);
PHP_FUNCTION(socket_close);
PHP_FUNCTION(socket_write);
PHP_FUNCTION(socket_read);
PHP_FUNCTION(socket_getsockname);
PHP_FUNCTION(socket_getpeername);
PHP_FUNCTION(socket_create);
PHP_FUNCTION(socket_connect);
PHP_FUNCTION(socket_strerror);
PHP_FUNCTION(socket_bind);
PHP_FUNCTION(socket_recv);
PHP_FUNCTION(socket_send);
PHP_FUNCTION(socket_recvfrom);
PHP_FUNCTION(socket_sendto);
PHP_FUNCTION(socket_get_option);
PHP_FUNCTION(socket_set_option);
#ifdef HAVE_SHUTDOWN
PHP_FUNCTION(socket_shutdown);
#endif
PHP_FUNCTION(socket_last_error);
PHP_FUNCTION(socket_clear_error);
PHP_FUNCTION(socket_import_stream);

/* {{{ sockets_functions[]
*/
const zend_function_entry sockets_functions[] = {
Expand Down Expand Up @@ -1096,7 +1127,7 @@ PHP_FUNCTION(socket_set_nonblock)
if (stream != NULL) {
if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 0,
NULL) != -1) {
php_sock->blocking = 1;
php_sock->blocking = 0;
RETURN_TRUE;
}
}
Expand Down
6 changes: 2 additions & 4 deletions ext/sockets/tests/ipv6_skipif.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
if (!defined("AF_INET6")) {
die('skip no IPv6 support');
}
/* If IPv6 is supported on the platform this will error out with code 111 - Connection refused.
If IPv6 is NOT supported, $errno will be set to something else (indicating parse/getaddrinfo error) */
@stream_socket_client('tcp://[::1]:0', $errno);
if ($errno != 111) die('skip no IPv6 support');
if (@stream_socket_client('udp://[::1]:8888') === false)
die('skip no IPv6 support');
3 changes: 0 additions & 3 deletions ext/sockets/tests/socket_import_stream-4-win.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ stream_set_blocking
Warning: stream_set_blocking(): %d is not a valid stream resource in %s on line %d

socket_set_block
Warning: socket_set_block(): An operation was attempted on something that is not a socket.
in %ssocket_import_stream-4-win.php on line %d

Warning: socket_set_block(): unable to set blocking mode [%d]: An operation was attempted on something that is not a socket.
in %ssocket_import_stream-4-win.php on line %d

Expand Down
62 changes: 62 additions & 0 deletions ext/sockets/tests/socket_sentto_recvfrom_ipv6_udp-win32.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--TEST--
Test if socket_recvfrom() receives data sent by socket_sendto() via IPv6 UDP (Win32)
--SKIPIF--
<?php
if (!extension_loaded('sockets')) {
die('SKIP The sockets extension is not loaded.');
}
if (substr(PHP_OS, 0, 3) != 'WIN') {
die('skip only for Windows');
}
require 'ipv6_skipif.inc';
--FILE--
<?php
$socket = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP);
if (!$socket) {
die('Unable to create AF_INET6 socket');
}
if (!socket_set_nonblock($socket)) {
die('Unable to set nonblocking mode for socket');
}
socket_recvfrom($socket, $buf, 12, 0, $from, $port); // cause warning
$address = '::1';
socket_sendto($socket, '', 1, 0, $address); // cause warning
if (!socket_bind($socket, $address, 1223)) {
die("Unable to bind to $address:1223");
}

$msg = "Ping!";
$len = strlen($msg);
$bytes_sent = socket_sendto($socket, $msg, $len, 0, $address, 1223);
if ($bytes_sent == -1) {
die('An error occurred while sending to the socket');
} else if ($bytes_sent != $len) {
die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected');
}

$from = "";
$port = 0;
socket_recvfrom($socket, $buf, 12, 0); // cause warning
socket_recvfrom($socket, $buf, 12, 0, $from); // cause warning
$bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from, $port);
if ($bytes_received == -1) {
die('An error occurred while receiving from the socket');
} else if ($bytes_received != $len) {
die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected');
}
echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;

socket_close($socket);
--EXPECTF--
Warning: socket_recvfrom(): unable to recvfrom [10022]: An invalid argument was supplied.
in %s on line %d

Warning: Wrong parameter count for socket_sendto() in %s on line %d

Warning: socket_recvfrom() expects at least 5 parameters, 4 given in %s on line %d

Warning: Wrong parameter count for socket_recvfrom() in %s on line %d
Received Ping! from remote address ::1 and remote port 1223
--CREDITS--
Falko Menge <mail at falko-menge dot de>
PHP Testfest Berlin 2009-05-09
3 changes: 3 additions & 0 deletions ext/sockets/tests/socket_sentto_recvfrom_ipv6_udp.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Test if socket_recvfrom() receives data sent by socket_sendto() via IPv6 UDP
if (!extension_loaded('sockets')) {
die('SKIP The sockets extension is not loaded.');
}
if (substr(PHP_OS, 0, 3) == 'WIN') {
die('skip Not valid for Windows');
}
require 'ipv6_skipif.inc';
--FILE--
<?php
Expand Down
5 changes: 0 additions & 5 deletions main/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1079,11 +1079,6 @@ PHPAPI int php_set_sock_blocking(int socketd, int block TSRMLS_DC)
/* with ioctlsocket, a non-zero sets nonblocking, a zero sets blocking */
flags = !block;
if (ioctlsocket(socketd, FIONBIO, &flags) == SOCKET_ERROR) {
char *error_string;

error_string = php_socket_strerror(WSAGetLastError(), NULL, 0);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", error_string);
efree(error_string);
ret = FAILURE;
}
#else
Expand Down

0 comments on commit e8f0e86

Please sign in to comment.