1 /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. 2 Copyright (c) 2012, 2017, 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, VIO_TYPE_SHARED_MEMORY 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_LOCALHOST 1U /* a localhost connection */ 63 #define VIO_BUFFERED_READ 2U /* use buffered read */ 64 #define VIO_READ_BUFFER_SIZE 16384U /* size of read buffer */ 65 #define VIO_DESCRIPTION_SIZE 30 /* size of description */ 66 67 Vio* vio_new(my_socket sd, enum enum_vio_type type, uint flags); 68 Vio* mysql_socket_vio_new(MYSQL_SOCKET mysql_socket, enum enum_vio_type type, uint flags); 69 #ifdef __WIN__ 70 Vio* vio_new_win32pipe(HANDLE hPipe); 71 Vio* vio_new_win32shared_memory(HANDLE handle_file_map, 72 HANDLE handle_map, 73 HANDLE event_server_wrote, 74 HANDLE event_server_read, 75 HANDLE event_client_wrote, 76 HANDLE event_client_read, 77 HANDLE event_conn_closed); 78 #else 79 #define HANDLE void * 80 #endif /* __WIN__ */ 81 82 void vio_delete(Vio* vio); 83 int vio_close(Vio* vio); 84 my_bool vio_reset(Vio* vio, enum enum_vio_type type, 85 my_socket sd, void *ssl, uint flags); 86 size_t vio_read(Vio *vio, uchar * buf, size_t size); 87 size_t vio_read_buff(Vio *vio, uchar * buf, size_t size); 88 size_t vio_write(Vio *vio, const uchar * buf, size_t size); 89 int vio_blocking(Vio *vio, my_bool onoff, my_bool *old_mode); 90 my_bool vio_is_blocking(Vio *vio); 91 /* setsockopt TCP_NODELAY at IPPROTO_TCP level, when possible */ 92 int vio_fastsend(Vio *vio); 93 /* setsockopt SO_KEEPALIVE at SOL_SOCKET level, when possible */ 94 int vio_keepalive(Vio *vio, my_bool onoff); 95 int vio_set_keepalive_options(Vio * vio, const struct vio_keepalive_opts *opts); 96 /* Whenever we should retry the last read/write operation. */ 97 my_bool vio_should_retry(Vio *vio); 98 /* Check that operation was timed out */ 99 my_bool vio_was_timeout(Vio *vio); 100 /* Short text description of the socket for those, who are curious.. */ 101 const char* vio_description(Vio *vio); 102 /* Return the type of the connection */ 103 enum enum_vio_type vio_type(Vio* vio); 104 /* Return last error number */ 105 int vio_errno(Vio*vio); 106 /* Get socket number */ 107 my_socket vio_fd(Vio*vio); 108 /* Remote peer's address and name in text form */ 109 my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen); 110 /* Wait for an I/O event notification. */ 111 int vio_io_wait(Vio *vio, enum enum_vio_io_event event, int timeout); 112 my_bool vio_is_connected(Vio *vio); 113 ssize_t vio_pending(Vio *vio); 114 /* Set timeout for a network operation. */ 115 extern int vio_timeout(Vio *vio, uint which, int timeout_sec); 116 extern void vio_set_wait_callback(void (*before_wait)(void), 117 void (*after_wait)(void)); 118 /* Connect to a peer. */ 119 my_bool vio_socket_connect(Vio *vio, struct sockaddr *addr, socklen_t len, 120 int timeout); 121 122 void vio_get_normalized_ip(const struct sockaddr *src, size_t src_length, struct sockaddr *dst); 123 124 my_bool vio_get_normalized_ip_string(const struct sockaddr *addr, size_t addr_length, 125 char *ip_string, size_t ip_string_size); 126 127 my_bool vio_is_no_name_error(int err_code); 128 129 int vio_getnameinfo(const struct sockaddr *sa, 130 char *hostname, size_t hostname_size, 131 char *port, size_t port_size, 132 int flags); 133 134 #ifdef HAVE_OPENSSL 135 /* apple deprecated openssl in MacOSX Lion */ 136 #ifdef __APPLE__ 137 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 138 #endif 139 140 #define HEADER_DES_LOCL_H dummy_something 141 #define YASSL_MYSQL_COMPATIBLE 142 #ifndef YASSL_PREFIX 143 #define YASSL_PREFIX 144 #endif 145 /* Set yaSSL to use same type as MySQL do for socket handles */ 146 typedef my_socket YASSL_SOCKET_T; 147 #define YASSL_SOCKET_T_DEFINED 148 #include <openssl/ssl.h> 149 #include <openssl/err.h> 150 #ifdef DEPRECATED 151 #undef DEPRECATED 152 #endif 153 154 enum enum_ssl_init_error 155 { 156 SSL_INITERR_NOERROR= 0, SSL_INITERR_CERT, SSL_INITERR_KEY, 157 SSL_INITERR_NOMATCH, SSL_INITERR_BAD_PATHS, SSL_INITERR_CIPHERS, 158 SSL_INITERR_MEMFAIL, SSL_INITERR_DH, SSL_INITERR_LASTERR 159 }; 160 const char* sslGetErrString(enum enum_ssl_init_error err); 161 162 struct st_VioSSLFd 163 { 164 SSL_CTX *ssl_context; 165 }; 166 167 int sslaccept(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr); 168 int sslconnect(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr); 169 170 struct st_VioSSLFd 171 *new_VioSSLConnectorFd(const char *key_file, const char *cert_file, 172 const char *ca_file, const char *ca_path, 173 const char *cipher, enum enum_ssl_init_error *error, 174 const char *crl_file, const char *crl_path); 175 struct st_VioSSLFd 176 *new_VioSSLAcceptorFd(const char *key_file, const char *cert_file, 177 const char *ca_file,const char *ca_path, 178 const char *cipher, enum enum_ssl_init_error *error, 179 const char *crl_file, const char *crl_path); 180 void free_vio_ssl_acceptor_fd(struct st_VioSSLFd *fd); 181 #endif /* HAVE_OPENSSL */ 182 183 void vio_end(void); 184 185 #ifdef __cplusplus 186 } 187 #endif 188 189 #if !defined(DONT_MAP_VIO) 190 #define vio_delete(vio) (vio)->viodelete(vio) 191 #define vio_errno(vio) (vio)->vioerrno(vio) 192 #define vio_read(vio, buf, size) ((vio)->read)(vio,buf,size) 193 #define vio_write(vio, buf, size) ((vio)->write)(vio, buf, size) 194 #define vio_blocking(vio, set_blocking_mode, old_mode)\ 195 (vio)->vioblocking(vio, set_blocking_mode, old_mode) 196 #define vio_is_blocking(vio) (vio)->is_blocking(vio) 197 #define vio_fastsend(vio) (vio)->fastsend(vio) 198 #define vio_keepalive(vio, set_keep_alive) (vio)->viokeepalive(vio, set_keep_alive) 199 #define vio_should_retry(vio) (vio)->should_retry(vio) 200 #define vio_was_timeout(vio) (vio)->was_timeout(vio) 201 #define vio_close(vio) ((vio)->vioclose)(vio) 202 #define vio_shutdown(vio,how) ((vio)->shutdown)(vio,how) 203 #define vio_peer_addr(vio, buf, prt, buflen) (vio)->peer_addr(vio, buf, prt, buflen) 204 #define vio_io_wait(vio, event, timeout) (vio)->io_wait(vio, event, timeout) 205 #define vio_is_connected(vio) (vio)->is_connected(vio) 206 #endif /* !defined(DONT_MAP_VIO) */ 207 208 #ifdef _WIN32 209 210 /* shutdown(2) flags */ 211 #ifndef SHUT_RD 212 #define SHUT_RD SD_RECEIVE 213 #endif 214 215 #endif 216 217 /* This enumerator is used in parser - should be always visible */ 218 enum SSL_type 219 { 220 SSL_TYPE_NOT_SPECIFIED= -1, 221 SSL_TYPE_NONE, 222 SSL_TYPE_ANY, 223 SSL_TYPE_X509, 224 SSL_TYPE_SPECIFIED 225 }; 226 227 /* HFTODO - hide this if we don't want client in embedded server */ 228 /* This structure is for every connection on both sides */ 229 struct st_vio 230 { 231 MYSQL_SOCKET mysql_socket; /* Instrumented socket */ 232 my_bool localhost; /* Are we from localhost? */ 233 int fcntl_mode; /* Buffered fcntl(sd,F_GETFL) */ 234 struct sockaddr_storage local; /* Local internet address */ 235 struct sockaddr_storage remote; /* Remote internet address */ 236 enum enum_vio_type type; /* Type of connection */ 237 const char *desc; /* String description */ 238 char *read_buffer; /* buffer for vio_read_buff */ 239 char *read_pos; /* start of unfetched data in the 240 read buffer */ 241 char *read_end; /* end of unfetched data */ 242 struct mysql_async_context *async_context; /* For non-blocking API */ 243 int read_timeout; /* Timeout value (ms) for read ops. */ 244 int write_timeout; /* Timeout value (ms) for write ops. */ 245 /* function pointers. They are similar for socket/SSL/whatever */ 246 void (*viodelete)(Vio*); 247 int (*vioerrno)(Vio*); 248 size_t (*read)(Vio*, uchar *, size_t); 249 size_t (*write)(Vio*, const uchar *, size_t); 250 int (*timeout)(Vio*, uint, my_bool); 251 int (*vioblocking)(Vio*, my_bool, my_bool *); 252 my_bool (*is_blocking)(Vio*); 253 int (*viokeepalive)(Vio*, my_bool); 254 int (*fastsend)(Vio*); 255 my_bool (*peer_addr)(Vio*, char *, uint16*, size_t); 256 void (*in_addr)(Vio*, struct sockaddr_storage*); 257 my_bool (*should_retry)(Vio*); 258 my_bool (*was_timeout)(Vio*); 259 int (*vioclose)(Vio*); 260 my_bool (*is_connected)(Vio*); 261 int (*shutdown)(Vio *, int); 262 my_bool (*has_data) (Vio*); 263 int (*io_wait)(Vio*, enum enum_vio_io_event, int); 264 my_bool (*connect)(Vio*, struct sockaddr *, socklen_t, int); 265 #ifdef HAVE_OPENSSL 266 void *ssl_arg; 267 #endif 268 #ifdef HAVE_SMEM 269 HANDLE handle_file_map; 270 char *handle_map; 271 HANDLE event_server_wrote; 272 HANDLE event_server_read; 273 HANDLE event_client_wrote; 274 HANDLE event_client_read; 275 HANDLE event_conn_closed; 276 size_t shared_memory_remain; 277 char *shared_memory_pos; 278 #endif /* HAVE_SMEM */ 279 #ifdef _WIN32 280 HANDLE hPipe; 281 OVERLAPPED overlapped; 282 DWORD read_timeout_ms; 283 DWORD write_timeout_ms; 284 int shutdown_flag; 285 #endif 286 }; 287 #endif /* vio_violite_h_ */ 288