php_stream_transport.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. | Author: Wez Furlong <wez@thebrainroom.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef PHP_WIN32
  17. #include "config.w32.h"
  18. #include <Ws2tcpip.h>
  19. #endif
  20. #if HAVE_SYS_SOCKET_H
  21. # include <sys/socket.h>
  22. #endif
  23. typedef php_stream *(php_stream_transport_factory_func)(const char *proto, size_t protolen,
  24. const char *resourcename, size_t resourcenamelen,
  25. const char *persistent_id, int options, int flags,
  26. struct timeval *timeout,
  27. php_stream_context *context STREAMS_DC);
  28. typedef php_stream_transport_factory_func *php_stream_transport_factory;
  29. BEGIN_EXTERN_C()
  30. PHPAPI int php_stream_xport_register(const char *protocol, php_stream_transport_factory factory);
  31. PHPAPI int php_stream_xport_unregister(const char *protocol);
  32. #define STREAM_XPORT_CLIENT 0
  33. #define STREAM_XPORT_SERVER 1
  34. #define STREAM_XPORT_CONNECT 2
  35. #define STREAM_XPORT_BIND 4
  36. #define STREAM_XPORT_LISTEN 8
  37. #define STREAM_XPORT_CONNECT_ASYNC 16
  38. /* Open a client or server socket connection */
  39. PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, int options,
  40. int flags, const char *persistent_id,
  41. struct timeval *timeout,
  42. php_stream_context *context,
  43. zend_string **error_string,
  44. int *error_code
  45. STREAMS_DC);
  46. #define php_stream_xport_create(name, namelen, options, flags, persistent_id, timeout, context, estr, ecode) \
  47. _php_stream_xport_create(name, namelen, options, flags, persistent_id, timeout, context, estr, ecode STREAMS_CC)
  48. /* Bind the stream to a local address */
  49. PHPAPI int php_stream_xport_bind(php_stream *stream,
  50. const char *name, size_t namelen,
  51. zend_string **error_text
  52. );
  53. /* Connect to a remote address */
  54. PHPAPI int php_stream_xport_connect(php_stream *stream,
  55. const char *name, size_t namelen,
  56. int asynchronous,
  57. struct timeval *timeout,
  58. zend_string **error_text,
  59. int *error_code
  60. );
  61. /* Prepare to listen */
  62. PHPAPI int php_stream_xport_listen(php_stream *stream,
  63. int backlog,
  64. zend_string **error_text
  65. );
  66. /* Get the next client and their address as a string, or the underlying address
  67. * structure. You must efree either of these if you request them */
  68. PHPAPI int php_stream_xport_accept(php_stream *stream, php_stream **client,
  69. zend_string **textaddr,
  70. void **addr, socklen_t *addrlen,
  71. struct timeval *timeout,
  72. zend_string **error_text
  73. );
  74. /* Get the name of either the socket or it's peer */
  75. PHPAPI int php_stream_xport_get_name(php_stream *stream, int want_peer,
  76. zend_string **textaddr,
  77. void **addr, socklen_t *addrlen
  78. );
  79. enum php_stream_xport_send_recv_flags {
  80. STREAM_OOB = 1,
  81. STREAM_PEEK = 2
  82. };
  83. /* Similar to recv() system call; read data from the stream, optionally
  84. * peeking, optionally retrieving OOB data */
  85. PHPAPI int php_stream_xport_recvfrom(php_stream *stream, char *buf, size_t buflen,
  86. int flags, void **addr, socklen_t *addrlen,
  87. zend_string **textaddr);
  88. /* Similar to send() system call; send data to the stream, optionally
  89. * sending it as OOB data */
  90. PHPAPI int php_stream_xport_sendto(php_stream *stream, const char *buf, size_t buflen,
  91. int flags, void *addr, socklen_t addrlen);
  92. typedef enum {
  93. STREAM_SHUT_RD,
  94. STREAM_SHUT_WR,
  95. STREAM_SHUT_RDWR
  96. } stream_shutdown_t;
  97. /* Similar to shutdown() system call; shut down part of a full-duplex
  98. * connection */
  99. PHPAPI int php_stream_xport_shutdown(php_stream *stream, stream_shutdown_t how);
  100. END_EXTERN_C()
  101. /* Structure definition for the set_option interface that the above functions wrap */
  102. typedef struct _php_stream_xport_param {
  103. enum {
  104. STREAM_XPORT_OP_BIND, STREAM_XPORT_OP_CONNECT,
  105. STREAM_XPORT_OP_LISTEN, STREAM_XPORT_OP_ACCEPT,
  106. STREAM_XPORT_OP_CONNECT_ASYNC,
  107. STREAM_XPORT_OP_GET_NAME,
  108. STREAM_XPORT_OP_GET_PEER_NAME,
  109. STREAM_XPORT_OP_RECV,
  110. STREAM_XPORT_OP_SEND,
  111. STREAM_XPORT_OP_SHUTDOWN
  112. } op;
  113. unsigned int want_addr:1;
  114. unsigned int want_textaddr:1;
  115. unsigned int want_errortext:1;
  116. unsigned int how:2;
  117. struct {
  118. char *name;
  119. size_t namelen;
  120. struct timeval *timeout;
  121. struct sockaddr *addr;
  122. char *buf;
  123. size_t buflen;
  124. socklen_t addrlen;
  125. int backlog;
  126. int flags;
  127. } inputs;
  128. struct {
  129. php_stream *client;
  130. struct sockaddr *addr;
  131. socklen_t addrlen;
  132. zend_string *textaddr;
  133. zend_string *error_text;
  134. int returncode;
  135. int error_code;
  136. } outputs;
  137. } php_stream_xport_param;
  138. /* Because both client and server streams use the same mechanisms
  139. for encryption we use the LSB to denote clients.
  140. */
  141. typedef enum {
  142. STREAM_CRYPTO_METHOD_SSLv2_CLIENT = (1 << 1 | 1),
  143. STREAM_CRYPTO_METHOD_SSLv3_CLIENT = (1 << 2 | 1),
  144. /* v23 no longer negotiates SSL2 or SSL3 */
  145. STREAM_CRYPTO_METHOD_SSLv23_CLIENT = ((1 << 3) | (1 << 4) | (1 << 5) | 1),
  146. STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT = (1 << 3 | 1),
  147. STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT = (1 << 4 | 1),
  148. STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT = (1 << 5 | 1),
  149. STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT = (1 << 6 | 1),
  150. /* TLS equates to TLS_ANY as of PHP 7.2 */
  151. STREAM_CRYPTO_METHOD_TLS_CLIENT = ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | 1),
  152. STREAM_CRYPTO_METHOD_TLS_ANY_CLIENT = ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | 1),
  153. STREAM_CRYPTO_METHOD_ANY_CLIENT = ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | 1),
  154. STREAM_CRYPTO_METHOD_SSLv2_SERVER = (1 << 1),
  155. STREAM_CRYPTO_METHOD_SSLv3_SERVER = (1 << 2),
  156. /* v23 no longer negotiates SSL2 or SSL3 */
  157. STREAM_CRYPTO_METHOD_SSLv23_SERVER = ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6)),
  158. STREAM_CRYPTO_METHOD_TLSv1_0_SERVER = (1 << 3),
  159. STREAM_CRYPTO_METHOD_TLSv1_1_SERVER = (1 << 4),
  160. STREAM_CRYPTO_METHOD_TLSv1_2_SERVER = (1 << 5),
  161. STREAM_CRYPTO_METHOD_TLSv1_3_SERVER = (1 << 6),
  162. /* TLS equates to TLS_ANY as of PHP 7.2 */
  163. STREAM_CRYPTO_METHOD_TLS_SERVER = ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6)),
  164. STREAM_CRYPTO_METHOD_TLS_ANY_SERVER = ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6)),
  165. STREAM_CRYPTO_METHOD_ANY_SERVER = ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6))
  166. } php_stream_xport_crypt_method_t;
  167. /* These functions provide crypto support on the underlying transport */
  168. BEGIN_EXTERN_C()
  169. PHPAPI int php_stream_xport_crypto_setup(php_stream *stream, php_stream_xport_crypt_method_t crypto_method, php_stream *session_stream);
  170. PHPAPI int php_stream_xport_crypto_enable(php_stream *stream, int activate);
  171. END_EXTERN_C()
  172. typedef struct _php_stream_xport_crypto_param {
  173. struct {
  174. php_stream *session;
  175. int activate;
  176. php_stream_xport_crypt_method_t method;
  177. } inputs;
  178. struct {
  179. int returncode;
  180. } outputs;
  181. enum {
  182. STREAM_XPORT_CRYPTO_OP_SETUP,
  183. STREAM_XPORT_CRYPTO_OP_ENABLE
  184. } op;
  185. } php_stream_xport_crypto_param;
  186. BEGIN_EXTERN_C()
  187. PHPAPI HashTable *php_stream_xport_get_hash(void);
  188. PHPAPI php_stream_transport_factory_func php_stream_generic_socket_factory;
  189. END_EXTERN_C()