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