first working version (one shot version)
[php-ancillary.git] / php-ancillary.c
index be249c9..115221a 100644 (file)
@@ -2,6 +2,7 @@
 #include "config.h"
 #endif
 #include "php.h"
+#include <ancillary.h>
  
 #define PHP_ANCILLARY_VERSION "1.0"
 #define PHP_ANCILLARY_EXTNAME "ancillary"
@@ -10,12 +11,12 @@ extern zend_module_entry ancillary_module_entry;
 #define phpext_ancillary_ptr &ancillary_module_entry
  
 // declaration of a custom mop_function()
-PHP_FUNCTION(mop_function);
+PHP_FUNCTION(ancillary_getstream);
  
 // list of custom PHP functions provided by this extension
 // set {NULL, NULL, NULL} as the last record to mark the end of list
-static function_entry mop_functions[] = {
-    PHP_FE(mop_function, NULL)
+static function_entry ancillary_getstream[] = {
+    PHP_FE(ancillary_getstream, NULL)
     {NULL, NULL, NULL}
 };
  
@@ -25,7 +26,7 @@ zend_module_entry ancillary_module_entry = {
     STANDARD_MODULE_HEADER,
 #endif
     PHP_ANCILLARY_EXTNAME,
-    mop_functions,
+    ancillary_getstream,
     NULL, // name of the MINIT function or NULL if not applicable
     NULL, // name of the MSHUTDOWN function or NULL if not applicable
     NULL, // name of the RINIT function or NULL if not applicable
@@ -39,8 +40,30 @@ zend_module_entry ancillary_module_entry = {
  
 ZEND_GET_MODULE(ancillary)
  
-// implementation of a custom mop_function()
-PHP_FUNCTION(mop_function)
+// starting from a unix socket receive a socket from an external process
+PHP_FUNCTION(ancillary_getstream)
 {
-    RETURN_STRING("This is my function", 1);
+    zval *zstream;
+    php_stream *stream;
+    int fd_in, fd_out;
+
+    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream))
+        return;
+    
+    php_stream_from_zval(stream, &zstream);
+
+    if (php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd_in, REPORT_ERRORS) == FAILURE) {
+        RETURN_FALSE;
+    }
+
+    if(ancil_recv_fd(fd_in, &fd_out)) {
+        RETURN_FALSE;
+    }
+
+    if ((stream = php_stream_fopen_from_fd(fd_out, "r+b", NULL)) == NULL) {
+        RETURN_FALSE;
+    }
+
+    php_stream_to_zval(stream, return_value);
 }