fsock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #include "php.h"
  21. #include "php_globals.h"
  22. #include <stdlib.h>
  23. #include <stddef.h>
  24. #include "php_network.h"
  25. #include "file.h"
  26. /* {{{ php_fsockopen() */
  27. static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  28. {
  29. char *host;
  30. size_t host_len;
  31. zend_long port = -1;
  32. zval *zerrno = NULL, *zerrstr = NULL;
  33. double timeout = (double)FG(default_socket_timeout);
  34. #ifndef PHP_WIN32
  35. time_t conv;
  36. #else
  37. long conv;
  38. #endif
  39. struct timeval tv;
  40. char *hashkey = NULL;
  41. php_stream *stream = NULL;
  42. int err;
  43. char *hostname = NULL;
  44. size_t hostname_len;
  45. zend_string *errstr = NULL;
  46. RETVAL_FALSE;
  47. ZEND_PARSE_PARAMETERS_START(1, 5)
  48. Z_PARAM_STRING(host, host_len)
  49. Z_PARAM_OPTIONAL
  50. Z_PARAM_LONG(port)
  51. Z_PARAM_ZVAL_DEREF(zerrno)
  52. Z_PARAM_ZVAL_DEREF(zerrstr)
  53. Z_PARAM_DOUBLE(timeout)
  54. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  55. if (persistent) {
  56. spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port);
  57. }
  58. if (port > 0) {
  59. hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port);
  60. } else {
  61. hostname_len = host_len;
  62. hostname = host;
  63. }
  64. /* prepare the timeout value for use */
  65. #ifndef PHP_WIN32
  66. conv = (time_t) (timeout * 1000000.0);
  67. tv.tv_sec = conv / 1000000;
  68. #else
  69. conv = (long) (timeout * 1000000.0);
  70. tv.tv_sec = conv / 1000000;
  71. #endif
  72. tv.tv_usec = conv % 1000000;
  73. if (zerrno) {
  74. zval_ptr_dtor(zerrno);
  75. ZVAL_LONG(zerrno, 0);
  76. }
  77. if (zerrstr) {
  78. zval_ptr_dtor(zerrstr);
  79. ZVAL_EMPTY_STRING(zerrstr);
  80. }
  81. stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
  82. STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
  83. if (port > 0) {
  84. efree(hostname);
  85. }
  86. if (stream == NULL) {
  87. php_error_docref(NULL, E_WARNING, "unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
  88. }
  89. if (hashkey) {
  90. efree(hashkey);
  91. }
  92. if (stream == NULL) {
  93. if (zerrno) {
  94. zval_ptr_dtor(zerrno);
  95. ZVAL_LONG(zerrno, err);
  96. }
  97. if (zerrstr && errstr) {
  98. /* no need to dup; we need to efree buf anyway */
  99. zval_ptr_dtor(zerrstr);
  100. ZVAL_STR(zerrstr, errstr);
  101. } else if (!zerrstr && errstr) {
  102. zend_string_release_ex(errstr, 0);
  103. }
  104. RETURN_FALSE;
  105. }
  106. if (errstr) {
  107. zend_string_release_ex(errstr, 0);
  108. }
  109. php_stream_to_zval(stream, return_value);
  110. }
  111. /* }}} */
  112. /* {{{ proto resource fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
  113. Open Internet or Unix domain socket connection */
  114. PHP_FUNCTION(fsockopen)
  115. {
  116. php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  117. }
  118. /* }}} */
  119. /* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
  120. Open persistent Internet or Unix domain socket connection */
  121. PHP_FUNCTION(pfsockopen)
  122. {
  123. php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  124. }
  125. /* }}} */
  126. /*
  127. * Local variables:
  128. * tab-width: 4
  129. * c-basic-offset: 4
  130. * End:
  131. * vim600: sw=4 ts=4 fdm=marker
  132. * vim<600: sw=4 ts=4
  133. */