mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
send/recvmsg() support for Windows
This commit is contained in:
parent
8561680533
commit
7066cc7267
12 changed files with 300 additions and 54 deletions
|
@ -7,7 +7,7 @@ if (PHP_SOCKETS != "no") {
|
|||
if (CHECK_LIB("ws2_32.lib", "sockets", PHP_SOCKETS)
|
||||
&& CHECK_LIB("Iphlpapi.lib", "sockets", PHP_SOCKETS)
|
||||
&& CHECK_HEADER_ADD_INCLUDE("winsock.h", "CFLAGS_SOCKETS")) {
|
||||
EXTENSION('sockets', 'sockets.c multicast.c');
|
||||
EXTENSION('sockets', 'sockets.c multicast.c conversions.c sockaddr_conv.c sendrecvmsg.c');
|
||||
AC_DEFINE('HAVE_SOCKETS', 1);
|
||||
PHP_INSTALL_HEADERS("ext/sockets", "php_sockets.h");
|
||||
} else {
|
||||
|
|
|
@ -1,24 +1,67 @@
|
|||
#include "conversions.h"
|
||||
#include "sockaddr_conv.h"
|
||||
#include "conversions.h"
|
||||
#include "sendrecvmsg.h" /* for ancillary registry */
|
||||
#include "windows_common.h"
|
||||
|
||||
#include <Zend/zend_llist.h>
|
||||
#include <ext/standard/php_smart_str.h>
|
||||
|
||||
#ifndef PHP_WIN32
|
||||
# include <sys/types.h>
|
||||
# include <sys/socket.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <netinet/in.h>
|
||||
# include <sys/un.h>
|
||||
|
||||
# include <sys/ioctl.h>
|
||||
# include <net/if.h>
|
||||
#else
|
||||
# include <win32/php_stdint.h>
|
||||
#endif
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef PHP_WIN32
|
||||
typedef unsigned short sa_family_t;
|
||||
# define msghdr _WSAMSG
|
||||
/*
|
||||
struct _WSAMSG {
|
||||
LPSOCKADDR name; //void *msg_name
|
||||
INT namelen; //socklen_t msg_namelen
|
||||
LPWSABUF lpBuffers; //struct iovec *msg_iov
|
||||
ULONG dwBufferCount; //size_t msg_iovlen
|
||||
WSABUF Control; //void *msg_control, size_t msg_controllen
|
||||
DWORD dwFlags; //int msg_flags
|
||||
}
|
||||
struct __WSABUF {
|
||||
u_long len; //size_t iov_len (2nd member)
|
||||
char FAR *buf; //void *iov_base (1st member)
|
||||
}
|
||||
struct _WSACMSGHDR {
|
||||
UINT cmsg_len; //socklen_t cmsg_len
|
||||
INT cmsg_level; //int cmsg_level
|
||||
INT cmsg_type; //int cmsg_type;
|
||||
followed by UCHAR cmsg_data[]
|
||||
}
|
||||
*/
|
||||
# define msg_name name
|
||||
# define msg_namelen namelen
|
||||
# define msg_iov lpBuffers
|
||||
# define msg_iovlen dwBufferCount
|
||||
# define msg_control Control.buf
|
||||
# define msg_controllen Control.len
|
||||
# define msg_flags dwFlags
|
||||
# define iov_base buf
|
||||
# define iov_len len
|
||||
|
||||
# define cmsghdr _WSACMSGHDR
|
||||
# ifdef CMSG_DATA
|
||||
# undef CMSG_DATA
|
||||
# endif
|
||||
# define CMSG_DATA WSA_CMSG_DATA
|
||||
#endif
|
||||
|
||||
#define MAX_USER_BUFF_SIZE ((size_t)(100*1024*1024))
|
||||
#define DEFAULT_BUFF_SIZE 8192
|
||||
|
||||
|
@ -132,7 +175,7 @@ static void do_from_to_zval_err(struct err_s *err,
|
|||
efree(user_msg);
|
||||
smart_str_free_ex(&path, 0);
|
||||
}
|
||||
__attribute__ ((format (printf, 2, 3)))
|
||||
ZEND_ATTRIBUTE_FORMAT(printf, 2 ,3)
|
||||
static void do_from_zval_err(ser_context *ctx, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
@ -141,7 +184,7 @@ static void do_from_zval_err(ser_context *ctx, const char *fmt, ...)
|
|||
do_from_to_zval_err(&ctx->err, &ctx->keys, "user", fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
__attribute__ ((format (printf, 2, 3)))
|
||||
ZEND_ATTRIBUTE_FORMAT(printf, 2 ,3)
|
||||
static void do_to_zval_err(res_context *ctx, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
@ -1149,7 +1192,7 @@ static void to_zval_read_iov(const char *msghdr_c, zval *zv, res_context *ctx)
|
|||
|
||||
for (i = 0; bytes_left > 0 && i < (uint)iovlen; i++) {
|
||||
zval *elem;
|
||||
size_t len = MIN(msghdr->msg_iov[i].iov_len, bytes_left);
|
||||
size_t len = MIN(msghdr->msg_iov[i].iov_len, (size_t)bytes_left);
|
||||
char *buf = safe_emalloc(1, len, 1);
|
||||
|
||||
MAKE_STD_ZVAL(elem);
|
||||
|
|
|
@ -2,8 +2,14 @@
|
|||
#define PHP_SOCK_CONVERSIONS_H 1
|
||||
|
||||
#include <php.h>
|
||||
|
||||
#ifndef PHP_WIN32
|
||||
# include <netinet/in.h>
|
||||
# include <sys/socket.h>
|
||||
#else
|
||||
# include <Ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
#include "php_sockets.h"
|
||||
|
||||
/* TYPE DEFINITIONS */
|
||||
|
|
|
@ -26,11 +26,13 @@
|
|||
|
||||
#if HAVE_SOCKETS
|
||||
|
||||
#include <php.h>
|
||||
|
||||
extern zend_module_entry sockets_module_entry;
|
||||
#define phpext_sockets_ptr &sockets_module_entry
|
||||
|
||||
#ifdef PHP_WIN32
|
||||
#include <winsock.h>
|
||||
#include <Winsock2.h>
|
||||
#else
|
||||
#if HAVE_SYS_SOCKET_H
|
||||
#include <sys/socket.h>
|
||||
|
|
|
@ -30,6 +30,44 @@
|
|||
#define DEFAULT_BUFF_SIZE 8192
|
||||
#define MAX_ARRAY_KEY_SIZE 128
|
||||
|
||||
#ifdef PHP_WIN32
|
||||
#include "windows_common.h"
|
||||
#include <Mswsock.h>
|
||||
#define IPV6_RECVPKTINFO IPV6_PKTINFO
|
||||
#define IPV6_RECVHOPLIMIT IPV6_HOPLIMIT
|
||||
#define msghdr _WSAMSG
|
||||
|
||||
static GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
|
||||
static __declspec(thread) LPFN_WSARECVMSG WSARecvMsg = NULL;
|
||||
inline ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
|
||||
{
|
||||
DWORD recvd = 0,
|
||||
bytesReturned;
|
||||
|
||||
if (WSARecvMsg == NULL) {
|
||||
int res = WSAIoctl((SOCKET) sockfd, SIO_GET_EXTENSION_FUNCTION_POINTER,
|
||||
&WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
|
||||
&WSARecvMsg, sizeof(WSARecvMsg),
|
||||
&bytesReturned, NULL, NULL);
|
||||
if (res != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
msg->dwFlags = (DWORD)flags;
|
||||
return WSARecvMsg((SOCKET)sockfd, msg, &recvd, NULL, NULL) == 0
|
||||
? (ssize_t)recvd
|
||||
: -1;
|
||||
}
|
||||
inline ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
|
||||
{
|
||||
DWORD sent = 0;
|
||||
return WSASendMsg((SOCKET)sockfd, (struct msghdr*)msg, (DWORD)flags, &sent, NULL, NULL) == 0
|
||||
? (ssize_t)sent
|
||||
: -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#define LONG_CHECK_VALID_INT(l) \
|
||||
do { \
|
||||
if ((l) < INT_MIN && (l) > INT_MAX) { \
|
||||
|
@ -158,9 +196,7 @@ PHP_FUNCTION(socket_sendmsg)
|
|||
|
||||
RETURN_LONG((long)res);
|
||||
} else {
|
||||
SOCKETS_G(last_error) = errno;
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error in sendmsg [%d]: %s",
|
||||
errno, sockets_strerror(errno TSRMLS_CC));
|
||||
PHP_SOCKET_ERROR(php_sock, "error in sendmsg", errno);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
}
|
||||
|
@ -285,6 +321,18 @@ int php_do_setsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname,
|
|||
switch (optname) {
|
||||
#ifdef IPV6_PKTINFO
|
||||
case IPV6_PKTINFO:
|
||||
#ifdef PHP_WIN32
|
||||
if (Z_TYPE_PP(arg4) == IS_ARRAY) {
|
||||
php_error_docref0(NULL TSRMLS_CC, E_WARNING, "Windows does not "
|
||||
"support sticky IPV6_PKTINFO");
|
||||
return FAILURE;
|
||||
} else {
|
||||
/* windows has no IPV6_RECVPKTINFO, and uses IPV6_PKTINFO
|
||||
* for the same effect. We define IPV6_RECVPKTINFO to be
|
||||
* IPV6_PKTINFO, so assume the assume user used IPV6_RECVPKTINFO */
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
opt_ptr = from_zval_run_conversions(*arg4, php_sock, from_zval_write_in6_pktinfo,
|
||||
sizeof(struct in6_pktinfo), "in6_pktinfo", &allocations, &err);
|
||||
if (err.has_error) {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "php_sockets.h"
|
||||
|
||||
#ifdef PHP_WIN32
|
||||
#include <Ws2tcpip.h>
|
||||
#include "windows_common.h"
|
||||
#else
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
|
|
|
@ -1,8 +1,16 @@
|
|||
#ifndef PHP_SOCKADR_CONV_H
|
||||
#define PHP_SOCKADR_CONV_H
|
||||
|
||||
#define HAVE_SOCKETS 1
|
||||
#include <php_network.h>
|
||||
#include "php_sockets.h"
|
||||
#include "php_sockets.h" /* php_socket */
|
||||
|
||||
#ifndef PHP_WIN32
|
||||
# include <netinet/in.h>
|
||||
#else
|
||||
# include <Winsock2.h>
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Convert an IPv6 literal or a hostname info a sockaddr_in6.
|
||||
|
|
|
@ -35,32 +35,12 @@
|
|||
#include "ext/standard/info.h"
|
||||
#include "php_ini.h"
|
||||
#ifdef PHP_WIN32
|
||||
# include "win32/inet.h"
|
||||
# include <winsock2.h>
|
||||
# include "windows_common.h"
|
||||
# include <win32/inet.h>
|
||||
# include <windows.h>
|
||||
# include <Ws2tcpip.h>
|
||||
# include "php_sockets.h"
|
||||
# include "win32/sockets.h"
|
||||
# define IS_INVALID_SOCKET(a) (a->bsd_socket == INVALID_SOCKET)
|
||||
# ifdef EPROTONOSUPPORT
|
||||
# undef EPROTONOSUPPORT
|
||||
# endif
|
||||
# ifdef ECONNRESET
|
||||
# undef ECONNRESET
|
||||
# endif
|
||||
# define EPROTONOSUPPORT WSAEPROTONOSUPPORT
|
||||
# define ECONNRESET WSAECONNRESET
|
||||
# ifdef errno
|
||||
# undef errno
|
||||
# endif
|
||||
# define errno WSAGetLastError()
|
||||
# define h_errno WSAGetLastError()
|
||||
# define set_errno(a) WSASetLastError(a)
|
||||
# define close(a) closesocket(a)
|
||||
# include <IPHlpApi.h>
|
||||
# if _WIN32_WINNT >= 0x0600
|
||||
# define HAVE_IF_NAMETOINDEX 1
|
||||
# endif
|
||||
# include <win32/sockets.h>
|
||||
#else
|
||||
# include <sys/types.h>
|
||||
# include <sys/socket.h>
|
||||
|
@ -650,8 +630,12 @@ PHP_MINIT_FUNCTION(sockets)
|
|||
REGISTER_LONG_CONSTANT("MSG_TRUNC", MSG_TRUNC, CONST_CS | CONST_PERSISTENT);
|
||||
REGISTER_LONG_CONSTANT("MSG_PEEK", MSG_PEEK, CONST_CS | CONST_PERSISTENT);
|
||||
REGISTER_LONG_CONSTANT("MSG_DONTROUTE", MSG_DONTROUTE, CONST_CS | CONST_PERSISTENT);
|
||||
#ifdef MSG_EOR
|
||||
REGISTER_LONG_CONSTANT("MSG_EOR", MSG_EOR, CONST_CS | CONST_PERSISTENT);
|
||||
#endif
|
||||
#ifdef MSG_EOF
|
||||
REGISTER_LONG_CONSTANT("MSG_EOF", MSG_EOF, CONST_CS | CONST_PERSISTENT);
|
||||
#endif
|
||||
|
||||
#ifdef MSG_CONFIRM
|
||||
REGISTER_LONG_CONSTANT("MSG_CONFIRM", MSG_CONFIRM, CONST_CS | CONST_PERSISTENT);
|
||||
|
|
110
ext/sockets/tests/socket_sendrecvmsg_multi_msg-win32.phpt
Normal file
110
ext/sockets/tests/socket_sendrecvmsg_multi_msg-win32.phpt
Normal file
|
@ -0,0 +1,110 @@
|
|||
--TEST--
|
||||
sendmsg()/recvmsg(): test ability to receive multiple messages (WIN32)
|
||||
--SKIPIF--
|
||||
<?php
|
||||
if (!extension_loaded('sockets'))
|
||||
die('skip sockets extension not available.');
|
||||
if (!defined('IPPROTO_IPV6'))
|
||||
die('skip IPv6 not available.');
|
||||
if (substr(PHP_OS, 0, 3) != 'WIN')
|
||||
die('skip Only for Windows!');
|
||||
/* Windows supports IPV6_RECVTCLASS and is able to receive the tclass via
|
||||
* WSARecvMsg (though only the top 6 bits seem to reported), but WSASendMsg
|
||||
* does not accept IPV6_TCLASS messages. We still test that sendmsg() works
|
||||
* corectly by sending an IPV6_PKTINFO message that will have no effect */
|
||||
|
||||
--FILE--
|
||||
<?php
|
||||
include __DIR__."/mcast_helpers.php.inc";
|
||||
$addr = '::1';
|
||||
|
||||
echo "creating send socket\n";
|
||||
$sends1 = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP) or die("err");
|
||||
var_dump($sends1);
|
||||
$br = socket_bind($sends1, '::', 7001) or die("err");
|
||||
var_dump($br);
|
||||
socket_set_nonblock($sends1) or die("Could not put in non-blocking mode");
|
||||
|
||||
echo "creating receive socket\n";
|
||||
$s = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP) or die("err");
|
||||
var_dump($s);
|
||||
$br = socket_bind($s, '::0', 3000) or die("err");
|
||||
var_dump($br);
|
||||
|
||||
socket_set_option($s, IPPROTO_IPV6, IPV6_RECVPKTINFO, 1) or die("err");
|
||||
socket_set_option($s, IPPROTO_IPV6, IPV6_RECVTCLASS, 1) or die("err");
|
||||
|
||||
$r = socket_sendmsg($sends1, [
|
||||
"name" => [ "addr" => "::1", "port" => 3000],
|
||||
"iov" => ["test ", "thing", "\n"],
|
||||
"control" => [[
|
||||
"level" => IPPROTO_IPV6,
|
||||
"type" => IPV6_PKTINFO,
|
||||
"data" => [
|
||||
'addr' => '::1',
|
||||
'ifindex' => 1 /* we're assuming loopback is 1. Is this a safe assumption? */
|
||||
],
|
||||
]]
|
||||
], 0);
|
||||
var_dump($r);
|
||||
checktimeout($s, 500);
|
||||
|
||||
$data = [
|
||||
"name" => ["family" => AF_INET6, "addr" => "::1"],
|
||||
"buffer_size" => 2000,
|
||||
"controllen" => socket_cmsg_space(IPPROTO_IPV6, IPV6_PKTINFO) +
|
||||
socket_cmsg_space(IPPROTO_IPV6, IPV6_TCLASS),
|
||||
];
|
||||
if (!socket_recvmsg($s, $data, 0)) die("recvmsg");
|
||||
print_r($data);
|
||||
|
||||
--EXPECTF--
|
||||
creating send socket
|
||||
resource(5) of type (Socket)
|
||||
bool(true)
|
||||
creating receive socket
|
||||
resource(6) of type (Socket)
|
||||
bool(true)
|
||||
int(11)
|
||||
Array
|
||||
(
|
||||
[name] => Array
|
||||
(
|
||||
[family] => %d
|
||||
[addr] => ::1
|
||||
[port] => 7001
|
||||
[flowinfo] => 0
|
||||
[scope_id] => 0
|
||||
)
|
||||
|
||||
[control] => Array
|
||||
(
|
||||
[0] => Array
|
||||
(
|
||||
[level] => %d
|
||||
[type] => %d
|
||||
[data] => Array
|
||||
(
|
||||
[addr] => ::1
|
||||
[ifindex] => %d
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
[1] => Array
|
||||
(
|
||||
[level] => %d
|
||||
[type] => %d
|
||||
[data] => 0
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
[iov] => Array
|
||||
(
|
||||
[0] => test thing
|
||||
|
||||
)
|
||||
|
||||
[flags] => 0
|
||||
)
|
|
@ -2,12 +2,15 @@
|
|||
sendmsg()/recvmsg(): test ability to receive multiple messages
|
||||
--SKIPIF--
|
||||
<?php
|
||||
if (!extension_loaded('sockets')) {
|
||||
if (!extension_loaded('sockets'))
|
||||
die('skip sockets extension not available.');
|
||||
}
|
||||
if (!defined('IPPROTO_IPV6')) {
|
||||
if (!defined('IPPROTO_IPV6'))
|
||||
die('skip IPv6 not available.');
|
||||
}
|
||||
if (substr(PHP_OS, 0, 3) == 'WIN')
|
||||
die('skip Not for the Windows!');
|
||||
/* Windows supports IPV6_RECVTCLASS and is able to receive the tclass via
|
||||
* WSARecvMsg (though only the top 6 bits seem to reported), but WSASendMsg
|
||||
* does not accept IPV6_TCLASS messages */
|
||||
|
||||
--FILE--
|
||||
<?php
|
||||
|
@ -36,7 +39,7 @@ $r = socket_sendmsg($sends1, [
|
|||
"control" => [[
|
||||
"level" => IPPROTO_IPV6,
|
||||
"type" => IPV6_TCLASS,
|
||||
"data" => 42,
|
||||
"data" => 40,
|
||||
]]
|
||||
], 0);
|
||||
var_dump($r);
|
||||
|
@ -88,7 +91,7 @@ Array
|
|||
(
|
||||
[level] => %d
|
||||
[type] => %d
|
||||
[data] => 42
|
||||
[data] => 40
|
||||
)
|
||||
|
||||
)
|
||||
|
|
|
@ -8,7 +8,8 @@ die('skip sockets extension not available.');
|
|||
if (!defined('IPPROTO_IPV6')) {
|
||||
die('skip IPv6 not available.');
|
||||
}
|
||||
|
||||
if (substr(PHP_OS, 0, 3) == 'WIN')
|
||||
die('skip Not for Windows!');
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
|
|
41
ext/sockets/windows_common.h
Normal file
41
ext/sockets/windows_common.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2012 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 3.01 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_01.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. |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
#ifndef WINDOWS_COMMON_H
|
||||
#define WINDOWS_COMMON_H
|
||||
|
||||
#include <Winsock2.h>
|
||||
#include <IPHlpApi.h> /* conflicting definition of CMSG_DATA */
|
||||
|
||||
#define HAVE_IF_NAMETOINDEX 1
|
||||
|
||||
#define IS_INVALID_SOCKET(a) (a->bsd_socket == INVALID_SOCKET)
|
||||
#ifdef EPROTONOSUPPORT
|
||||
# undef EPROTONOSUPPORT
|
||||
#endif
|
||||
#ifdef ECONNRESET
|
||||
# undef ECONNRESET
|
||||
#endif
|
||||
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
|
||||
#define ECONNRESET WSAECONNRESET
|
||||
#ifdef errno
|
||||
# undef errno
|
||||
#endif
|
||||
#define errno WSAGetLastError()
|
||||
#define h_errno WSAGetLastError()
|
||||
#define set_errno(a) WSASetLastError(a)
|
||||
#define close(a) closesocket(a)
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue