php-src/ext/standard/fsock.c

142 lines
3.9 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
2004-01-08 08:18:22 +00:00
| PHP Version 5 |
+----------------------------------------------------------------------+
2007-01-01 09:36:18 +00:00
| Copyright (c) 1997-2007 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: |
2006-01-01 12:51:34 +00:00
| http://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
/* $Id$ */
1999-06-15 21:51:00 +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;
2004-06-16 23:57:25 +00:00
int host_len;
long port = -1;
zval *zerrno = NULL, *zerrstr = NULL;
double timeout = FG(default_socket_timeout);
1999-07-21 16:12:13 +00:00
unsigned long conv;
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;
long hostname_len;
char *errstr = NULL;
RETVAL_FALSE;
2002-03-15 21:03:08 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzzd", &host, &host_len, &port, &zerrno, &zerrstr, &timeout) == FAILURE) {
2002-03-15 21:03:08 +00:00
RETURN_FALSE;
}
2002-03-15 21:03:08 +00:00
if (persistent) {
2003-08-28 15:16:20 +00:00
spprintf(&hashkey, 0, "pfsockopen__%s:%ld", host, port);
}
if (port > 0) {
2003-08-28 16:28:33 +00:00
hostname_len = spprintf(&hostname, 0, "%s:%ld", host, port);
} else {
hostname_len = host_len;
hostname = host;
}
2002-03-15 21:03:08 +00:00
/* prepare the timeout value for use */
conv = (unsigned long) (timeout * 1000000.0);
tv.tv_sec = conv / 1000000;
tv.tv_usec = conv % 1000000;
2002-03-15 21:03:08 +00:00
if (zerrno) {
zval_dtor(zerrno);
ZVAL_LONG(zerrno, 0);
}
if (zerrstr) {
2002-03-15 21:03:08 +00:00
zval_dtor(zerrstr);
ZVAL_STRING(zerrstr, "", 1);
2002-03-15 21:03:08 +00:00
}
stream = php_stream_xport_create(hostname, hostname_len, ENFORCE_SAFE_MODE | 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) {
2003-08-28 16:49:57 +00:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:%ld (%s)", host, port, errstr == NULL ? "Unknown error" : errstr);
}
if (hashkey) {
efree(hashkey);
}
2002-03-15 21:03:08 +00:00
if (stream == NULL) {
if (zerrno) {
2002-03-15 21:03:08 +00:00
zval_dtor(zerrno);
ZVAL_LONG(zerrno, err);
2002-03-15 21:03:08 +00:00
}
if (zerrstr && errstr) {
/* no need to dup; we need to efree buf anyway */
zval_dtor(zerrstr);
ZVAL_STRING(zerrstr, errstr, 0);
}
else if (!zerrstr && errstr) {
efree(errstr);
}
2002-03-15 21:03:08 +00:00
RETURN_FALSE;
}
if (errstr) {
efree(errstr);
}
2002-03-15 21:03:08 +00:00
php_stream_to_zval(stream, return_value);
}
2002-03-15 21:03:08 +00:00
/* }}} */
2003-12-31 10:56:32 +00:00
/* {{{ proto resource fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
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);
}
/* }}} */
2003-12-31 10:56:32 +00:00
/* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
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);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/