add new files
[php-ancillary.git] / php-ancillary.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include "php.h"
5 #include <php5/main/php_network.h>
6 #include <php5/ext/standard/file.h>
7 #include <ancillary.h>
8
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12  
13 #define PHP_ANCILLARY_VERSION "1.0"
14 #define PHP_ANCILLARY_EXTNAME "ancillary"
15  
16 extern zend_module_entry ancillary_module_entry;
17 #define phpext_ancillary_ptr &ancillary_module_entry
18  
19 // declaration of a custom mop_function()
20 PHP_FUNCTION(ancillary_getstream);
21  
22 // list of custom PHP functions provided by this extension
23 // set {NULL, NULL, NULL} as the last record to mark the end of list
24 static function_entry ancillary_getstream[] = {
25     PHP_FE(ancillary_getstream, NULL)
26     {NULL, NULL, NULL}
27 };
28  
29 // the following code creates an entry for the module and registers it with Zend.
30 zend_module_entry ancillary_module_entry = {
31 #if ZEND_MODULE_API_NO >= 20010901
32     STANDARD_MODULE_HEADER,
33 #endif
34     PHP_ANCILLARY_EXTNAME,
35     ancillary_getstream,
36     NULL, // name of the MINIT function or NULL if not applicable
37     NULL, // name of the MSHUTDOWN function or NULL if not applicable
38     NULL, // name of the RINIT function or NULL if not applicable
39     NULL, // name of the RSHUTDOWN function or NULL if not applicable
40     NULL, // name of the MINFO function or NULL if not applicable
41 #if ZEND_MODULE_API_NO >= 20010901
42     PHP_ANCILLARY_VERSION,
43 #endif
44     STANDARD_MODULE_PROPERTIES
45 };
46  
47 ZEND_GET_MODULE(ancillary)
48
49 #define CTRL_BUFF_MAX_SZ (8*1024)
50  
51 // starting from a unix socket receive a socket from an external process
52 PHP_FUNCTION(ancillary_getstream)
53 {
54     zval *zstream;
55     php_stream *stream;
56     int fd_in, fd_out[2], retrecv, curpos = 0;
57     char *headers;
58     zval *zheaders;
59     php_netstream_data_t *sock;
60
61     if ((headers = calloc(CTRL_BUFF_MAX_SZ, 1)) == NULL) {
62         return;
63     }
64
65     if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &zstream, &zheaders)) {
66         free(headers);
67         return;
68     }
69     
70     php_stream_from_zval(stream, &zstream);
71
72     if (php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd_in, REPORT_ERRORS) == FAILURE) {
73         free(headers);
74         RETURN_FALSE;
75     }
76
77     if(ancil_recv_fds(fd_in, fd_out, 2) == 0) {
78         free(headers);
79         RETURN_FALSE;
80     }
81
82     {
83         char bf[2048];
84
85         sprintf(bf, "zero: [%d]    uno: [%d]\n", fd_out[0], fd_out[1]);
86         write(1, bf, strlen(bf));
87     }
88     while(1) {
89         write(1, "LOOP\n", 5);
90         retrecv = recv(fd_out[1], &(headers[curpos]), CTRL_BUFF_MAX_SZ - curpos - 1, 0);
91         if (retrecv < 0) {
92             free(headers);
93             RETURN_FALSE;
94         }
95         else if (retrecv == 0) {
96             break;
97         }
98         else {
99             char bf[1024];
100             curpos += retrecv;
101             sprintf(bf, "CURPOS: %d\n", curpos);
102             write(1, bf, strlen(bf));
103             if (curpos == CTRL_BUFF_MAX_SZ - 1) {
104                 free(headers);
105                 RETURN_FALSE;
106             }
107         }
108     }
109     shutdown(fd_out[1], SHUT_RDWR);
110     close(fd_out[1]);
111     ZVAL_STRING(zheaders, headers, 1);
112     free(headers);
113
114     if (0 == 1) {
115         int fh;
116
117         if ((fh = open("/tmp/out_php-anc.txt", O_WRONLY | O_CREAT | O_APPEND)) > -1) {
118             write(fh, headers, curpos);
119             close(fh);
120         }
121     }
122
123     sock = pemalloc(sizeof(php_netstream_data_t), 0);
124     memset(sock, 0, sizeof(php_netstream_data_t));
125
126     sock->is_blocked = 1;
127     sock->timeout.tv_sec = FG(default_socket_timeout);
128     sock->timeout.tv_usec = 0;
129
130     /* we don't know the socket until we have determined if we are binding or
131      * connecting */
132     sock->socket = fd_out[0];
133
134     stream = php_stream_alloc_rel(&php_stream_socket_ops, sock, NULL, "r+");
135
136     if (stream == NULL) {
137         pefree(sock, 0);
138         return NULL;
139     }
140     php_stream_to_zval(stream, return_value);
141 }
142