1 /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. 2 Copyright (c) 2012, 2020, MariaDB Corporation. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; version 2 of the License. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software Foundation, Inc., 15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */ 16 17 /* 18 * Vio Lite. 19 * Purpose: include file for Vio that will work with C and C++ 20 */ 21 22 #ifndef vio_violite_h_ 23 #define vio_violite_h_ 24 25 #include "my_net.h" /* needed because of struct in_addr */ 26 #include <mysql/psi/mysql_socket.h> 27 28 /* Simple vio interface in C; The functions are implemented in violite.c */ 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif /* __cplusplus */ 33 34 #ifdef __cplusplus 35 typedef struct st_vio Vio; 36 #endif /* __cplusplus */ 37 38 enum enum_vio_type 39 { 40 VIO_CLOSED, VIO_TYPE_TCPIP, VIO_TYPE_SOCKET, VIO_TYPE_NAMEDPIPE, 41 VIO_TYPE_SSL 42 }; 43 44 /** 45 VIO I/O events. 46 */ 47 enum enum_vio_io_event 48 { 49 VIO_IO_EVENT_READ, 50 VIO_IO_EVENT_WRITE, 51 VIO_IO_EVENT_CONNECT 52 }; 53 54 struct vio_keepalive_opts 55 { 56 int interval; 57 int idle; 58 int probes; 59 }; 60 61 62 #define VIO_TLSv1_0 1 63 #define VIO_TLSv1_1 2 64 #define VIO_TLSv1_2 4 65 #define VIO_TLSv1_3 8 66 67 #define VIO_LOCALHOST 1U /* a localhost connection */ 68 #define VIO_BUFFERED_READ 2U /* use buffered read */ 69 #define VIO_READ_BUFFER_SIZE 16384U /* size of read buffer */ 70 #define VIO_DESCRIPTION_SIZE 30 /* size of description */ 71 72 Vio* vio_new(my_socket sd, enum enum_vio_type type, uint flags); 73 Vio* mysql_socket_vio_new(MYSQL_SOCKET mysql_socket, enum enum_vio_type type, uint flags); 74 #ifdef __WIN__ 75 Vio* vio_new_win32pipe(HANDLE hPipe); 76 #else 77 #define HANDLE void * 78 #endif /* __WIN__ */ 79 80 void vio_delete(Vio* vio); 81 int vio_close(Vio* vio); 82 my_bool vio_reset(Vio* vio, enum enum_vio_type type, 83 my_socket sd, void *ssl, uint flags); 84 size_t vio_read(Vio *vio, uchar * buf, size_t size); 85 size_t vio_read_buff(Vio *vio, uchar * buf, size_t size); 86 size_t vio_write(Vio *vio, const uchar * buf, size_t size); 87 int vio_blocking(Vio *vio, my_bool onoff, my_bool *old_mode); 88 my_bool vio_is_blocking(Vio *vio); 89 /* setsockopt TCP_NODELAY at IPPROTO_TCP level, when possible */ 90 int vio_nodelay(Vio *vio, my_bool on); 91 int vio_fastsend(Vio *vio); 92 /* setsockopt SO_KEEPALIVE at SOL_SOCKET level, when possible */ 93 int vio_keepalive(Vio *vio, my_bool onoff); 94 int vio_set_keepalive_options(Vio * vio, const struct vio_keepalive_opts *opts); 95 /* Whenever we should retry the last read/write operation. */ 96 my_bool vio_should_retry(Vio *vio); 97 /* Check that operation was timed out */ 98 my_bool vio_was_timeout(Vio *vio); 99 /* Short text description of the socket for those, who are curious.. */ 100 const char* vio_description(Vio *vio); 101 /* Return the type of the connection */ 102 enum enum_vio_type vio_type(Vio* vio); 103 /* Return last error number */ 104 int vio_errno(Vio*vio); 105 /* Get socket number */ 106 my_socket vio_fd(Vio*vio); 107 /* Remote peer's address and name in text form */ 108 my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen); 109 /* Wait for an I/O event notification. */ 110 int vio_io_wait(Vio *vio, enum enum_vio_io_event event, int timeout); 111 my_bool vio_is_connected(Vio *vio); 112 ssize_t vio_pending(Vio *vio); 113 /* Set timeout for a network operation. */ 114 extern int vio_timeout(Vio *vio, uint which, int timeout_sec); 115 extern void vio_set_wait_callback(void (*before_wait)(void), 116 void (*after_wait)(void)); 117 /* Connect to a peer. */ 118 my_bool vio_socket_connect(Vio *vio, struct sockaddr *addr, socklen_t len, 119 int timeout); 120 121 void vio_get_normalized_ip(const struct sockaddr *src, size_t src_length, struct sockaddr *dst); 122 123 my_bool vio_get_normalized_ip_string(const struct sockaddr *addr, size_t addr_length, 124 char *ip_string, size_t ip_string_size); 125 126 my_bool vio_is_no_name_error(int err_code); 127 128 int vio_getnameinfo(const struct sockaddr *sa, 129 char *hostname, size_t hostname_size, 130 char *port, size_t port_size, 131 int flags); 132 133 #ifdef HAVE_OPENSSL 134 /* apple deprecated openssl in MacOSX Lion */ 135 #ifdef __APPLE__ 136 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 137 #endif 138 139 #define HEADER_DES_LOCL_H dummy_something 140 #define YASSL_MYSQL_COMPATIBLE 141 #ifndef YASSL_PREFIX 142 #define YASSL_PREFIX 143 #endif 144 /* Set yaSSL to use same type as MySQL do for socket handles */ 145 typedef my_socket YASSL_SOCKET_T; 146 #define YASSL_SOCKET_T_DEFINED 147 #define template _template /* bug in WolfSSL 4.4.0, see also my_crypt.cc */ 148 #include <openssl/ssl.h> 149 #undef template 150 #include <openssl/err.h> 151 #ifdef DEPRECATED 152 #undef DEPRECATED 153 #endif 154 155 enum enum_ssl_init_error 156 { 157 SSL_INITERR_NOERROR= 0, SSL_INITERR_CERT, SSL_INITERR_KEY, 158 SSL_INITERR_NOMATCH, SSL_INITERR_BAD_PATHS, SSL_INITERR_CIPHERS, 159 SSL_INITERR_MEMFAIL, SSL_INITERR_DH, SSL_INITERR_PROTOCOL, 160 SSL_INITERR_LASTERR 161 }; 162 const char* sslGetErrString(enum enum_ssl_init_error err); 163 164 struct st_VioSSLFd 165 { 166 SSL_CTX *ssl_context; 167 }; 168 169 int sslaccept(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr); 170 int sslconnect(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr); 171 172 void vio_check_ssl_init(); 173 174 struct st_VioSSLFd 175 *new_VioSSLConnectorFd(const char *key_file, const char *cert_file, 176 const char *ca_file, const char *ca_path, 177 const char *cipher, enum enum_ssl_init_error *error, 178 const char *crl_file, const char *crl_path); 179 struct st_VioSSLFd 180 *new_VioSSLAcceptorFd(const char *key_file, const char *cert_file, 181 const char *ca_file,const char *ca_path, 182 const char *cipher, enum enum_ssl_init_error *error, 183 const char *crl_file, const char *crl_path, 184 ulonglong tls_version); 185 void free_vio_ssl_acceptor_fd(struct st_VioSSLFd *fd); 186 #endif /* HAVE_OPENSSL */ 187 188 void vio_end(void); 189 190 #ifdef __cplusplus 191 } 192 #endif 193 194 #if !defined(DONT_MAP_VIO) 195 #define vio_delete(vio) (vio)->viodelete(vio) 196 #define vio_errno(vio) (vio)->vioerrno(vio) 197 #define vio_read(vio, buf, size) ((vio)->read)(vio,buf,size) 198 #define vio_write(vio, buf, size) ((vio)->write)(vio, buf, size) 199 #define vio_blocking(vio, set_blocking_mode, old_mode)\ 200 (vio)->vioblocking(vio, set_blocking_mode, old_mode) 201 #define vio_is_blocking(vio) (vio)->is_blocking(vio) 202 #define vio_fastsend(vio) (vio)->fastsend(vio) 203 #define vio_keepalive(vio, set_keep_alive) (vio)->viokeepalive(vio, set_keep_alive) 204 #define vio_should_retry(vio) (vio)->should_retry(vio) 205 #define vio_was_timeout(vio) (vio)->was_timeout(vio) 206 #define vio_close(vio) ((vio)->vioclose)(vio) 207 #define vio_shutdown(vio,how) ((vio)->shutdown)(vio,how) 208 #define vio_peer_addr(vio, buf, prt, buflen) (vio)->peer_addr(vio, buf, prt, buflen) 209 #define vio_io_wait(vio, event, timeout) (vio)->io_wait(vio, event, timeout) 210 #define vio_is_connected(vio) (vio)->is_connected(vio) 211 #endif /* !defined(DONT_MAP_VIO) */ 212 213 #ifdef _WIN32 214 215 /* shutdown(2) flags */ 216 #ifndef SHUT_RD 217 #define SHUT_RD SD_RECEIVE 218 #endif 219 220 #endif 221 222 /* This enumerator is used in parser - should be always visible */ 223 enum SSL_type 224 { 225 SSL_TYPE_NOT_SPECIFIED= -1, 226 SSL_TYPE_NONE, 227 SSL_TYPE_ANY, 228 SSL_TYPE_X509, 229 SSL_TYPE_SPECIFIED 230 }; 231 232 /* HFTODO - hide this if we don't want client in embedded server */ 233 /* This structure is for every connection on both sides */ 234 struct st_vio 235 { 236 MYSQL_SOCKET mysql_socket; /* Instrumented socket */ 237 my_bool localhost; /* Are we from localhost? */ 238 int fcntl_mode; /* Buffered fcntl(sd,F_GETFL) */ 239 struct sockaddr_storage local; /* Local internet address */ 240 struct sockaddr_storage remote; /* Remote internet address */ 241 enum enum_vio_type type; /* Type of connection */ 242 const char *desc; /* String description */ 243 char *read_buffer; /* buffer for vio_read_buff */ 244 char *read_pos; /* start of unfetched data in the 245 read buffer */ 246 char *read_end; /* end of unfetched data */ 247 struct mysql_async_context *async_context; /* For non-blocking API */ 248 int read_timeout; /* Timeout value (ms) for read ops. */ 249 int write_timeout; /* Timeout value (ms) for write ops. */ 250 /* function pointers. They are similar for socket/SSL/whatever */ 251 void (*viodelete)(Vio*); 252 int (*vioerrno)(Vio*); 253 size_t (*read)(Vio*, uchar *, size_t); 254 size_t (*write)(Vio*, const uchar *, size_t); 255 int (*timeout)(Vio*, uint, my_bool); 256 int (*vioblocking)(Vio*, my_bool, my_bool *); 257 my_bool (*is_blocking)(Vio*); 258 int (*viokeepalive)(Vio*, my_bool); 259 int (*fastsend)(Vio*); 260 my_bool (*peer_addr)(Vio*, char *, uint16*, size_t); 261 void (*in_addr)(Vio*, struct sockaddr_storage*); 262 my_bool (*should_retry)(Vio*); 263 my_bool (*was_timeout)(Vio*); 264 int (*vioclose)(Vio*); 265 my_bool (*is_connected)(Vio*); 266 int (*shutdown)(Vio *, int); 267 my_bool (*has_data) (Vio*); 268 int (*io_wait)(Vio*, enum enum_vio_io_event, int); 269 my_bool (*connect)(Vio*, struct sockaddr *, socklen_t, int); 270 #ifdef HAVE_OPENSSL 271 void *ssl_arg; 272 #endif 273 #ifdef _WIN32 274 HANDLE hPipe; 275 OVERLAPPED overlapped; 276 int shutdown_flag; 277 #endif 278 }; 279 #endif /* vio_violite_h_ */ 280