X-Git-Url: https://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=php-ancillary.c;h=b00eb2086ebc729f387c561e5740747b6f54a6ca;hb=8350cbc890f6d7a9da6f57ed2b072dfbd4d2d54f;hp=be249c9a28716625541f27c6dff6f08990bc4331;hpb=5f3d6c49a07662e84b39bc99be7cfc2d5f10b70e;p=php-ancillary.git diff --git a/php-ancillary.c b/php-ancillary.c index be249c9..b00eb20 100644 --- a/php-ancillary.c +++ b/php-ancillary.c @@ -2,6 +2,13 @@ #include "config.h" #endif #include "php.h" +#include +#include +#include + +#include +#include +#include #define PHP_ANCILLARY_VERSION "1.0" #define PHP_ANCILLARY_EXTNAME "ancillary" @@ -10,12 +17,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 +32,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 @@ -38,9 +45,98 @@ zend_module_entry ancillary_module_entry = { }; ZEND_GET_MODULE(ancillary) + +#define CTRL_BUFF_MAX_SZ (8*1024) -// 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[2], retrecv, curpos = 0; + char *headers; + zval *zheaders; + php_netstream_data_t *sock; + + if ((headers = calloc(CTRL_BUFF_MAX_SZ, 1)) == NULL) { + return; + } + + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &zstream, &zheaders)) { + free(headers); + return; + } + + php_stream_from_zval(stream, &zstream); + + if (php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd_in, REPORT_ERRORS) == FAILURE) { + free(headers); + RETURN_FALSE; + } + + if(ancil_recv_fds(fd_in, fd_out, 2) == 0) { + free(headers); + RETURN_FALSE; + } + + { + char bf[2048]; + + sprintf(bf, "zero: [%d] uno: [%d]\n", fd_out[0], fd_out[1]); + write(1, bf, strlen(bf)); + } + while(1) { + write(1, "LOOP\n", 5); + retrecv = recv(fd_out[1], &(headers[curpos]), CTRL_BUFF_MAX_SZ - curpos - 1, 0); + if (retrecv < 0) { + free(headers); + RETURN_FALSE; + } + else if (retrecv == 0) { + break; + } + else { + char bf[1024]; + curpos += retrecv; + sprintf(bf, "CURPOS: %d\n", curpos); + write(1, bf, strlen(bf)); + if (curpos == CTRL_BUFF_MAX_SZ - 1) { + free(headers); + RETURN_FALSE; + } + } + } + shutdown(fd_out[1], SHUT_RDWR); + close(fd_out[1]); + ZVAL_STRING(zheaders, headers, 1); + free(headers); + + if (0 == 1) { + int fh; + + if ((fh = open("/tmp/out_php-anc.txt", O_WRONLY | O_CREAT | O_APPEND)) > -1) { + write(fh, headers, curpos); + close(fh); + } + } + + sock = pemalloc(sizeof(php_netstream_data_t), 0); + memset(sock, 0, sizeof(php_netstream_data_t)); + + sock->is_blocked = 1; + sock->timeout.tv_sec = FG(default_socket_timeout); + sock->timeout.tv_usec = 0; + + /* we don't know the socket until we have determined if we are binding or + * connecting */ + sock->socket = fd_out[0]; + + stream = php_stream_alloc_rel(&php_stream_socket_ops, sock, NULL, "r+"); + + if (stream == NULL) { + pefree(sock, 0); + return NULL; + } + php_stream_to_zval(stream, return_value); } +