first working version (one shot version)
[php-ancillary.git] / php-ancillary.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include "php.h"
5 #include <ancillary.h>
6  
7 #define PHP_ANCILLARY_VERSION "1.0"
8 #define PHP_ANCILLARY_EXTNAME "ancillary"
9  
10 extern zend_module_entry ancillary_module_entry;
11 #define phpext_ancillary_ptr &ancillary_module_entry
12  
13 // declaration of a custom mop_function()
14 PHP_FUNCTION(ancillary_getstream);
15  
16 // list of custom PHP functions provided by this extension
17 // set {NULL, NULL, NULL} as the last record to mark the end of list
18 static function_entry ancillary_getstream[] = {
19     PHP_FE(ancillary_getstream, NULL)
20     {NULL, NULL, NULL}
21 };
22  
23 // the following code creates an entry for the module and registers it with Zend.
24 zend_module_entry ancillary_module_entry = {
25 #if ZEND_MODULE_API_NO >= 20010901
26     STANDARD_MODULE_HEADER,
27 #endif
28     PHP_ANCILLARY_EXTNAME,
29     ancillary_getstream,
30     NULL, // name of the MINIT function or NULL if not applicable
31     NULL, // name of the MSHUTDOWN function or NULL if not applicable
32     NULL, // name of the RINIT function or NULL if not applicable
33     NULL, // name of the RSHUTDOWN function or NULL if not applicable
34     NULL, // name of the MINFO function or NULL if not applicable
35 #if ZEND_MODULE_API_NO >= 20010901
36     PHP_ANCILLARY_VERSION,
37 #endif
38     STANDARD_MODULE_PROPERTIES
39 };
40  
41 ZEND_GET_MODULE(ancillary)
42  
43 // starting from a unix socket receive a socket from an external process
44 PHP_FUNCTION(ancillary_getstream)
45 {
46     zval *zstream;
47     php_stream *stream;
48     int fd_in, fd_out;
49
50     if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream))
51         return;
52     
53     php_stream_from_zval(stream, &zstream);
54
55     if (php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd_in, REPORT_ERRORS) == FAILURE) {
56         RETURN_FALSE;
57     }
58
59     if(ancil_recv_fd(fd_in, &fd_out)) {
60         RETURN_FALSE;
61     }
62
63     if ((stream = php_stream_fopen_from_fd(fd_out, "r+b", NULL)) == NULL) {
64         RETURN_FALSE;
65     }
66
67     php_stream_to_zval(stream, return_value);
68 }
69