]> granicus.if.org Git - php/commitdiff
Windows support for stream_socket_pair(), by moving the socketpair() implementation...
authorKalle Sommer Nielsen <kalle@php.net>
Fri, 23 Jan 2009 15:48:58 +0000 (15:48 +0000)
committerKalle Sommer Nielsen <kalle@php.net>
Fri, 23 Jan 2009 15:48:58 +0000 (15:48 +0000)
ext/sockets/config.w32
ext/sockets/php_sockets.h
ext/sockets/sockets.c
ext/standard/streamsfuncs.c
ext/standard/streamsfuncs.h
ext/standard/tests/streams/stream_socket_pair.phpt
win32/build/config.w32
win32/build/config.w32.h.in
win32/sockets.c [moved from ext/sockets/php_sockets_win.c with 85% similarity]
win32/sockets.h [moved from ext/sockets/php_sockets_win.h with 75% similarity]

index 587b9a1b127ff855ff77f64273022fd2c37f48be..c0c6fc1c904ffe531f48da34eabd204fcd7745ea 100644 (file)
@@ -6,7 +6,7 @@ ARG_ENABLE("sockets", "SOCKETS support", "no");
 if (PHP_SOCKETS != "no") {
 
        if (CHECK_LIB("ws2_32.lib", "sockets", PHP_SOCKETS)) {
-               EXTENSION('sockets', 'sockets.c php_sockets_win.c');
+               EXTENSION('sockets', 'sockets.c');
                AC_DEFINE('HAVE_SOCKETS', 1);
        } else {
                WARNING("sockets not enabled; libraries and headers not found");
index f0c9cc5203aca5b02188bff16616c1e3c2244919..3db96dc2ab87a92b3833d168bb960fd39aec66d1 100644 (file)
@@ -43,7 +43,7 @@ PHP_RSHUTDOWN_FUNCTION(sockets);
 
 PHP_FUNCTION(socket_select);
 PHP_FUNCTION(socket_create_listen);
-#if defined(HAVE_SOCKETPAIR) || defined(PHP_WIN32)
+#ifdef HAVE_SOCKETPAIR
 PHP_FUNCTION(socket_create_pair);
 #endif
 PHP_FUNCTION(socket_accept);
@@ -84,6 +84,13 @@ typedef struct {
        int             blocking;
 } php_socket;
 
+#ifdef PHP_WIN32
+struct sockaddr_un {
+       short   sun_family;
+       char    sun_path[108];
+};
+#endif
+
 /* 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);
index 62a2039eea463e535f5dd5e6b3077b42719af0d4..d027d3bcecc5092415d42a61b2e064dc5d13263d 100644 (file)
 # include <windows.h>
 # include <Ws2tcpip.h>
 # include "php_sockets.h"
-# include "php_sockets_win.h"
+# include "win32/sockets.h"
 # define IS_INVALID_SOCKET(a)  (a->bsd_socket == INVALID_SOCKET)
+# 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)
 #else
 # include "php_sockets.h"
 # include <sys/types.h>
@@ -223,7 +232,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_option, 0, 0, 4)
        ZEND_ARG_INFO(0, optval)
 ZEND_END_ARG_INFO()
 
-#if defined(HAVE_SOCKETPAIR) || defined(PHP_WIN32)
+#ifdef HAVE_SOCKETPAIR
 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_pair, 0, 0, 4)
        ZEND_ARG_INFO(0, domain)
        ZEND_ARG_INFO(0, type)
@@ -254,7 +263,7 @@ const zend_function_entry sockets_functions[] = {
        PHP_FE(socket_select,                   arginfo_socket_select)
        PHP_FE(socket_create,                   arginfo_socket_create)
        PHP_FE(socket_create_listen,    arginfo_socket_create_listen)
-#if defined(HAVE_SOCKETPAIR) || defined(PHP_WIN32)
+#ifdef HAVE_SOCKETPAIR
        PHP_FE(socket_create_pair,              arginfo_socket_create_pair)
 #endif
        PHP_FE(socket_accept,                   arginfo_socket_accept)
@@ -1857,7 +1866,7 @@ PHP_FUNCTION(socket_set_option)
 }
 /* }}} */
 
-#if defined(HAVE_SOCKETPAIR) || defined(PHP_WIN32)
+#ifdef HAVE_SOCKETPAIR
 /* {{{ proto bool socket_create_pair(int domain, int type, int protocol, array &fd) U
    Creates a pair of indistinguishable sockets and stores them in fds. */
 PHP_FUNCTION(socket_create_pair)
index 80b49674271625824055abf4fa64d8c41ee7a50e..0ef4a695c127aee039dc5427a6456243f5d59a63 100644 (file)
@@ -36,6 +36,7 @@
 typedef unsigned long long php_timeout_ull;
 #else
 #include "win32/select.h"
+#include "win32/sockets.h"
 typedef unsigned __int64 php_timeout_ull;
 #endif
 
index aaa20c334ccb3e3d39ebb3704f4f992c7f8c35da..bb1ae03477b98a9813f4d55d36ee30b4d0518d9d 100644 (file)
@@ -57,11 +57,14 @@ PHP_FUNCTION(stream_filter_remove);
 PHP_FUNCTION(stream_encoding);
 PHP_FUNCTION(stream_socket_enable_crypto);
 PHP_FUNCTION(stream_socket_shutdown);
-PHP_FUNCTION(stream_socket_pair);
 PHP_FUNCTION(stream_resolve_include_path);
 PHP_FUNCTION(stream_is_local);
 PHP_FUNCTION(stream_supports_lock);
 
+#if HAVE_SOCKETPAIR
+PHP_FUNCTION(stream_socket_pair);
+#endif
+
 /*
  * Local variables:
  * tab-width: 4
index 5556d8e22c738090bfa8461cfa0382c2fada4481..4560228564b8f4e364d82a0cc933de2d82ab0fbe 100644 (file)
@@ -1,12 +1,9 @@
 --TEST--
 stream_socket_pair()
---SKIPIF--
-<?php
-if (substr(PHP_OS, 0, 3) == 'WIN') die("skip: non windows test");
-?>
 --FILE--
 <?php
-$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
+$domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? STREAM_PF_INET : STREAM_PF_UNIX);
+$sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, 0);
 var_dump($sockets);
 fwrite($sockets[0], b"foo");
 var_dump(fread($sockets[1], strlen(b"foo")));
index ea9c0302c706abe27734038b8d325f6c7649fb0e..7ecfd9d7e65c11608a76d5bd7d0cf48ead7ab288 100644 (file)
@@ -351,7 +351,7 @@ ADD_SOURCES("main", "main.c snprintf.c spprintf.c fopen_wrappers.c \
        php_open_temporary_file.c php_logos.c output.c internal_functions.c php_sprintf.c \
        getopt.c");
 
-ADD_SOURCES("win32", "inet.c fnmatch.c");
+ADD_SOURCES("win32", "inet.c fnmatch.c sockets.c");
 
 // Newer versions have it
 if (VCVERS <= 1300) {
index 6f35ae4868c60ba9fb33cfe534e701cf4feffc24..a1729cd479c5e1e337646c266181f30e31c7a0f3 100644 (file)
 /* Win32 supports strcoll */
 #define HAVE_STRCOLL 1
 
+/* Win32 supports socketpair by the emulation in win32/sockets.c */
+#define HAVE_SOCKETPAIR 1
+
 /* Win32 support proc_open */
 #define PHP_CAN_SUPPORT_PROC_OPEN 1
 
similarity index 85%
rename from ext/sockets/php_sockets_win.c
rename to win32/sockets.c
index 809b79c5577b35fba61a93cea82de9b186ec6a47..63fe26ac741eef3263006fd0b3a1420cbc8f9a17 100644 (file)
 
 /* $Id$ */
 
-
-#ifdef PHP_WIN32
+/* Code originally from ext/sockets */
 
 #include <stdio.h>
 #include <fcntl.h>
 
 #include "php.h"
-#include "php_sockets.h"
-#include "php_sockets_win.h"
 
-int socketpair(int domain, int type, int protocol, SOCKET sock[2]) {
+PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
+{
        struct sockaddr_in address;
        SOCKET redirect;
        int size = sizeof(address);
 
        if(domain != AF_INET) {
-               set_errno(WSAENOPROTOOPT);
+               WSASetLastError(WSAENOPROTOOPT);
                return -1;
        }
 
 
-       sock[0] = socket(domain, type, protocol);
-       address.sin_addr.s_addr = INADDR_ANY;
+       sock[0]                         = socket(domain, type, protocol);
+       address.sin_addr.s_addr         = INADDR_ANY;
        address.sin_family              = AF_INET;
        address.sin_port                = 0;
 
        bind(sock[0], (struct sockaddr*)&address, sizeof(address));
-       if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) {
 
-    }
+       if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) {
+       }
 
        listen(sock[0], 2);
        sock[1] = socket(domain, type, protocol);       
@@ -59,17 +57,15 @@ int socketpair(int domain, int type, int protocol, SOCKET sock[2]) {
        connect(sock[1], (struct sockaddr*)&address, sizeof(address));
        redirect = accept(sock[0],(struct sockaddr*)&address, &size);
 
-       close(sock[0]);
+       closesocket(sock[0]);
        sock[0] = redirect;
 
        if(sock[0] == INVALID_SOCKET ) {
-               close(sock[0]);
-               close(sock[1]);
-               set_errno(WSAECONNABORTED);
+               closesocket(sock[0]);
+               closesocket(sock[1]);
+               WSASetLastError(WSAECONNABORTED);
                return -1;
        }
        
        return 0;
 }
-
-#endif
similarity index 75%
rename from ext/sockets/php_sockets_win.h
rename to win32/sockets.h
index f7e7c55e7857eafff2add87430e020bcfec0adc5..0d8bb07c8a29feb6e122d8f34cf713aa5e828ec6 100644 (file)
 
 /* $Id$ */
 
+/* Code originally from ext/sockets */
 
-#ifdef PHP_WIN32
-
-#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)
-
-struct sockaddr_un {
-       short   sun_family;
-       char    sun_path[108];
-};
-
-int socketpair(int domain, int type, int protocol, SOCKET sock[2]);
-int inet_aton(const char *cp, struct in_addr *inp);
-
-#endif
+PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2]);
\ No newline at end of file