php_stream_transport.h 7.7 KB

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