php-src/ext/standard/fsock.c

156 lines
4.0 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
1999-07-16 13:13:16 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Paul Panotzki - Bunyip Information Systems |
| Jim Winstead <jimw@php.net> |
1999-12-01 17:07:25 +00:00
| Sascha Schumann <sascha@schumann.cx> |
+----------------------------------------------------------------------+
*/
1999-06-18 13:42:21 +00:00
#include "php.h"
#include "php_globals.h"
#include <stdlib.h>
#include <stddef.h>
#include "php_network.h"
#include "file.h"
1999-12-18 04:01:20 +00:00
/* {{{ php_fsockopen() */
1999-06-15 21:51:00 +00:00
2002-03-15 21:03:08 +00:00
static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
char *host;
size_t host_len;
2014-08-25 17:24:55 +00:00
zend_long port = -1;
zval *zerrno = NULL, *zerrstr = NULL;
double timeout;
bool timeout_is_null = 1;
2014-10-23 09:05:47 +00:00
#ifndef PHP_WIN32
time_t conv;
#else
long conv;
#endif
2002-03-15 21:03:08 +00:00
struct timeval tv;
char *hashkey = NULL;
php_stream *stream = NULL;
2002-07-13 09:26:36 +00:00
int err;
char *hostname = NULL;
2014-10-23 09:05:47 +00:00
size_t hostname_len;
zend_string *errstr = NULL;
2016-12-31 02:03:33 +00:00
ZEND_PARSE_PARAMETERS_START(1, 5)
Z_PARAM_STRING(host, host_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(port)
Z_PARAM_ZVAL(zerrno)
Z_PARAM_ZVAL(zerrstr)
Z_PARAM_DOUBLE_OR_NULL(timeout, timeout_is_null)
ZEND_PARSE_PARAMETERS_END();
2002-03-15 21:03:08 +00:00
RETVAL_FALSE;
if (timeout_is_null) {
timeout = (double)FG(default_socket_timeout);
}
if (persistent) {
2014-08-25 18:22:49 +00:00
spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port);
}
if (port > 0) {
2014-08-25 18:22:49 +00:00
hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port);
} else {
hostname_len = host_len;
hostname = host;
}
2015-01-03 09:22:58 +00:00
2002-03-15 21:03:08 +00:00
/* prepare the timeout value for use */
if (timeout != -1.0 && !(timeout >= 0.0 && timeout <= (double) PHP_TIMEOUT_ULL_MAX / 1000000.0)) {
if (port > 0) {
efree(hostname);
}
if (hashkey) {
efree(hashkey);
}
zend_argument_value_error(6, "must be -1 or between 0 and " ZEND_ULONG_FMT, ((double) PHP_TIMEOUT_ULL_MAX / 1000000.0));
RETURN_THROWS();
} else {
2014-10-23 09:05:47 +00:00
#ifndef PHP_WIN32
conv = (time_t) (timeout * 1000000.0);
tv.tv_sec = conv / 1000000;
2014-10-23 09:05:47 +00:00
#else
conv = (long) (timeout * 1000000.0);
tv.tv_sec = conv / 1000000;
2014-10-23 09:05:47 +00:00
#endif
tv.tv_usec = conv % 1000000;
}
stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
2002-03-15 21:03:08 +00:00
if (port > 0) {
efree(hostname);
}
if (stream == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
}
if (hashkey) {
efree(hashkey);
}
2015-01-03 09:22:58 +00:00
if (stream == NULL) {
if (zerrno) {
ZEND_TRY_ASSIGN_REF_LONG(zerrno, err);
2002-03-15 21:03:08 +00:00
}
if (errstr) {
if (zerrstr) {
ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr);
2019-02-08 09:49:54 +00:00
} else {
zend_string_release(errstr);
}
2015-01-03 09:22:58 +00:00
}
2002-03-15 21:03:08 +00:00
RETURN_FALSE;
}
if (zerrno) {
ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
}
if (zerrstr) {
ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr);
}
if (errstr) {
zend_string_release_ex(errstr, 0);
}
2015-01-03 09:22:58 +00:00
php_stream_to_zval(stream, return_value);
}
2002-03-15 21:03:08 +00:00
/* }}} */
/* {{{ Open Internet or Unix domain socket connection */
PHP_FUNCTION(fsockopen)
{
2002-03-15 21:03:08 +00:00
php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
2014-05-09 16:43:02 +00:00
/* {{{ Open persistent Internet or Unix domain socket connection */
PHP_FUNCTION(pfsockopen)
{
2002-03-15 21:03:08 +00:00
php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */