fsock.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Paul Panotzki - Bunyip Information Systems |
  16. | Jim Winstead <jimw@php.net> |
  17. | Sascha Schumann <sascha@schumann.cx> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #include "php.h"
  22. #include "php_globals.h"
  23. #include <stdlib.h>
  24. #include <stddef.h>
  25. #include "php_network.h"
  26. #include "file.h"
  27. /* {{{ php_fsockopen() */
  28. static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  29. {
  30. char *host;
  31. int host_len;
  32. long port = -1;
  33. zval *zerrno = NULL, *zerrstr = NULL;
  34. double timeout = FG(default_socket_timeout);
  35. unsigned long conv;
  36. struct timeval tv;
  37. char *hashkey = NULL;
  38. php_stream *stream = NULL;
  39. int err;
  40. char *hostname = NULL;
  41. long hostname_len;
  42. char *errstr = NULL;
  43. RETVAL_FALSE;
  44. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzzd", &host, &host_len, &port, &zerrno, &zerrstr, &timeout) == FAILURE) {
  45. RETURN_FALSE;
  46. }
  47. if (persistent) {
  48. spprintf(&hashkey, 0, "pfsockopen__%s:%ld", host, port);
  49. }
  50. if (port > 0) {
  51. hostname_len = spprintf(&hostname, 0, "%s:%ld", host, port);
  52. } else {
  53. hostname_len = host_len;
  54. hostname = host;
  55. }
  56. /* prepare the timeout value for use */
  57. conv = (unsigned long) (timeout * 1000000.0);
  58. tv.tv_sec = conv / 1000000;
  59. tv.tv_usec = conv % 1000000;
  60. if (zerrno) {
  61. zval_dtor(zerrno);
  62. ZVAL_LONG(zerrno, 0);
  63. }
  64. if (zerrstr) {
  65. zval_dtor(zerrstr);
  66. ZVAL_STRING(zerrstr, "", 1);
  67. }
  68. stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
  69. STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
  70. if (port > 0) {
  71. efree(hostname);
  72. }
  73. if (stream == NULL) {
  74. php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:%ld (%s)", host, port, errstr == NULL ? "Unknown error" : errstr);
  75. }
  76. if (hashkey) {
  77. efree(hashkey);
  78. }
  79. if (stream == NULL) {
  80. if (zerrno) {
  81. zval_dtor(zerrno);
  82. ZVAL_LONG(zerrno, err);
  83. }
  84. if (zerrstr && errstr) {
  85. /* no need to dup; we need to efree buf anyway */
  86. zval_dtor(zerrstr);
  87. ZVAL_STRING(zerrstr, errstr, 0);
  88. }
  89. else if (!zerrstr && errstr) {
  90. efree(errstr);
  91. }
  92. RETURN_FALSE;
  93. }
  94. if (errstr) {
  95. efree(errstr);
  96. }
  97. php_stream_to_zval(stream, return_value);
  98. }
  99. /* }}} */
  100. /* {{{ proto resource fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
  101. Open Internet or Unix domain socket connection */
  102. PHP_FUNCTION(fsockopen)
  103. {
  104. php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  105. }
  106. /* }}} */
  107. /* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
  108. Open persistent Internet or Unix domain socket connection */
  109. PHP_FUNCTION(pfsockopen)
  110. {
  111. php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  112. }
  113. /* }}} */
  114. /*
  115. * Local variables:
  116. * tab-width: 4
  117. * c-basic-offset: 4
  118. * End:
  119. * vim600: sw=4 ts=4 fdm=marker
  120. * vim<600: sw=4 ts=4
  121. */