first working version (one shot version)
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Tue, 19 Jun 2012 05:37:04 +0000 (07:37 +0200)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Tue, 19 Jun 2012 05:41:19 +0000 (07:41 +0200)
php-ancillary.c
test001.php [new file with mode: 0755]

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);
 }
diff --git a/test001.php b/test001.php
new file mode 100755 (executable)
index 0000000..fd2e1f4
--- /dev/null
@@ -0,0 +1,31 @@
+#!/usr/bin/php
+<?php
+
+$FILE_SOCKET = "/tmp/test001.sock";
+$UNIX_SOCKET = "unix://$FILE_SOCKET";
+if (file_exists($FILE_SOCKET)) {
+    unlink($FILE_SOCKET);
+}
+
+$old_umask = umask(0);
+if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
+    exit(11);
+}
+umask($old_umask);
+
+$new_socket = stream_socket_accept($list);
+
+$fp = ancillary_getstream($new_socket);
+printf("FP: %d\n", $fp);
+
+
+if ($fp == FALSE)
+    printf("ERROR\n");
+else
+    printf("GOOD\n");
+
+fwrite($fp, "DI QUI CI PASSO2");
+fflush($fp);
+fclose($fp);
+
+?>
\ No newline at end of file