modified to have access to remote hostname
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Fri, 20 Jul 2012 05:46:34 +0000 (07:46 +0200)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Fri, 20 Jul 2012 05:46:34 +0000 (07:46 +0200)
php-ancillary.c

index ea64de4..8c43397 100644 (file)
@@ -2,6 +2,8 @@
 #include "config.h"
 #endif
 #include "php.h"
+#include <php5/main/php_network.h>
+#include <php5/ext/standard/file.h>
 #include <ancillary.h>
 
 #include <sys/types.h>
@@ -54,21 +56,26 @@ PHP_FUNCTION(ancillary_getstream)
     int fd_in, fd_out[2], retrecv, curpos = 0;
     char *headers;
     zval *zheaders;
+    php_netstream_data_t *sock;
 
     if ((headers = calloc(CTRL_BUFF_MAX_SZ, 1)) == NULL) {
         return;
     }
 
-    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &zstream, &zheaders))
+    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &zstream, &zheaders)) {
+        free(headers);
         return;
+    }
     
     php_stream_from_zval(stream, &zstream);
 
     if (php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd_in, REPORT_ERRORS) == FAILURE) {
+        free(headers);
         RETURN_FALSE;
     }
 
     if(ancil_recv_fds(fd_in, fd_out, 2) == 0) {
+        free(headers);
         RETURN_FALSE;
     }
 
@@ -82,6 +89,7 @@ PHP_FUNCTION(ancillary_getstream)
         write(1, "LOOP\n", 5);
         retrecv = recv(fd_out[1], &(headers[curpos]), CTRL_BUFF_MAX_SZ - curpos - 1, 0);
         if (retrecv < 0) {
+            free(headers);
             RETURN_FALSE;
         }
         else if (retrecv == 0) {
@@ -112,10 +120,23 @@ PHP_FUNCTION(ancillary_getstream)
         }
     }
 
-    if ((stream = php_stream_fopen_from_fd(fd_out[0], "r+b", NULL)) == NULL) {
-        RETURN_FALSE;
-    }
+    sock = pemalloc(sizeof(php_netstream_data_t), 0);
+    memset(sock, 0, sizeof(php_netstream_data_t));
+
+    sock->is_blocked = 1;
+    sock->timeout.tv_sec = FG(default_socket_timeout);
+    sock->timeout.tv_usec = 0;
 
+    /* we don't know the socket until we have determined if we are binding or
+     * connecting */
+    sock->socket = fd_out[0];
+
+    stream = php_stream_alloc_rel(&php_stream_socket_ops, sock, NULL, "r+");
+
+    if (stream == NULL)        {
+        pefree(sock, 0);
+        return NULL;
+    }
     php_stream_to_zval(stream, return_value);
 }