Back to index

php5  5.3.10
fsock.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Paul Panotzki - Bunyip Information Systems                  |
00016    |          Jim Winstead <jimw@php.net>                                 |
00017    |          Sascha Schumann <sascha@schumann.cx>                        |
00018    +----------------------------------------------------------------------+
00019 */
00020 
00021 /* $Id: fsock.c 321634 2012-01-01 13:15:04Z felipe $ */
00022 
00023 #include "php.h"
00024 #include "php_globals.h"
00025 #include <stdlib.h>
00026 #include <stddef.h>
00027 #include "php_network.h"
00028 #include "file.h"
00029 
00030 /* {{{ php_fsockopen() */
00031 
00032 static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
00033 {
00034        char *host;
00035        int host_len;
00036        long port = -1;
00037        zval *zerrno = NULL, *zerrstr = NULL;
00038        double timeout = FG(default_socket_timeout);
00039        unsigned long conv;
00040        struct timeval tv;
00041        char *hashkey = NULL;
00042        php_stream *stream = NULL;
00043        int err;
00044        char *hostname = NULL;
00045        long hostname_len;
00046        char *errstr = NULL;
00047 
00048        RETVAL_FALSE;
00049        
00050        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzzd", &host, &host_len, &port, &zerrno, &zerrstr, &timeout) == FAILURE) {
00051               RETURN_FALSE;
00052        }
00053 
00054        if (persistent) {
00055               spprintf(&hashkey, 0, "pfsockopen__%s:%ld", host, port);
00056        }
00057 
00058        if (port > 0) {
00059               hostname_len = spprintf(&hostname, 0, "%s:%ld", host, port);
00060        } else {
00061               hostname_len = host_len;
00062               hostname = host;
00063        }
00064        
00065        /* prepare the timeout value for use */
00066        conv = (unsigned long) (timeout * 1000000.0);
00067        tv.tv_sec = conv / 1000000;
00068        tv.tv_usec = conv % 1000000;
00069 
00070        if (zerrno)   {
00071               zval_dtor(zerrno);
00072               ZVAL_LONG(zerrno, 0);
00073        }
00074        if (zerrstr) {
00075               zval_dtor(zerrstr);
00076               ZVAL_STRING(zerrstr, "", 1);
00077        }
00078 
00079        stream = php_stream_xport_create(hostname, hostname_len, ENFORCE_SAFE_MODE | REPORT_ERRORS,
00080                      STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
00081 
00082        if (port > 0) {
00083               efree(hostname);
00084        }
00085        if (stream == NULL) {
00086               php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:%ld (%s)", host, port, errstr == NULL ? "Unknown error" : errstr);
00087        }
00088 
00089        if (hashkey) {
00090               efree(hashkey);
00091        }
00092        
00093        if (stream == NULL)  {
00094               if (zerrno) {
00095                      zval_dtor(zerrno);
00096                      ZVAL_LONG(zerrno, err);
00097               }
00098               if (zerrstr && errstr) {
00099                      /* no need to dup; we need to efree buf anyway */
00100                      zval_dtor(zerrstr);
00101                      ZVAL_STRING(zerrstr, errstr, 0);
00102               }
00103               else if (!zerrstr && errstr) {
00104                      efree(errstr);
00105               } 
00106 
00107               RETURN_FALSE;
00108        }
00109 
00110        if (errstr) {
00111               efree(errstr);
00112        }
00113               
00114        php_stream_to_zval(stream, return_value);
00115 }
00116 
00117 /* }}} */
00118 
00119 /* {{{ proto resource fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
00120    Open Internet or Unix domain socket connection */
00121 PHP_FUNCTION(fsockopen)
00122 {
00123        php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
00124 }
00125 /* }}} */
00126 /* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
00127    Open persistent Internet or Unix domain socket connection */
00128 PHP_FUNCTION(pfsockopen)
00129 {
00130        php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
00131 }
00132 /* }}} */
00133 
00134 /*
00135  * Local variables:
00136  * tab-width: 4
00137  * c-basic-offset: 4
00138  * End:
00139  * vim600: sw=4 ts=4 fdm=marker
00140  * vim<600: sw=4 ts=4
00141  */