php_stream_transport.h 7.5 KB

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