#include "config.h"
#endif
#include "php.h"
+#include <ancillary.h>
#define PHP_ANCILLARY_VERSION "1.0"
#define PHP_ANCILLARY_EXTNAME "ancillary"
#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}
};
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
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);
}
+
--- /dev/null
+#!/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