fsock.c 3.7 KB

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