MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
violite.h
1 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /*
17  * Vio Lite.
18  * Purpose: include file for Vio that will work with C and C++
19  */
20 
21 #ifndef vio_violite_h_
22 #define vio_violite_h_
23 
24 #include "my_net.h" /* needed because of struct in_addr */
25 #include <mysql/psi/mysql_socket.h>
26 
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_TYPE_TCPIP, VIO_TYPE_SOCKET, VIO_TYPE_NAMEDPIPE, VIO_TYPE_SSL,
41  VIO_TYPE_SHARED_MEMORY
42 };
43 
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 #define VIO_LOCALHOST 1 /* a localhost connection */
55 #define VIO_BUFFERED_READ 2 /* use buffered read */
56 #define VIO_READ_BUFFER_SIZE 16384 /* size of read buffer */
57 #define VIO_DESCRIPTION_SIZE 30 /* size of description */
58 
59 Vio* vio_new(my_socket sd, enum enum_vio_type type, uint flags);
60 Vio* mysql_socket_vio_new(MYSQL_SOCKET mysql_socket, enum enum_vio_type type, uint flags);
61 #ifdef __WIN__
62 Vio* vio_new_win32pipe(HANDLE hPipe);
63 Vio* vio_new_win32shared_memory(HANDLE handle_file_map,
64  HANDLE handle_map,
65  HANDLE event_server_wrote,
66  HANDLE event_server_read,
67  HANDLE event_client_wrote,
68  HANDLE event_client_read,
69  HANDLE event_conn_closed);
70 #else
71 #define HANDLE void *
72 #endif /* __WIN__ */
73 
74 void vio_delete(Vio* vio);
75 int vio_shutdown(Vio* vio);
76 my_bool vio_reset(Vio* vio, enum enum_vio_type type,
77  my_socket sd, void *ssl, uint flags);
78 size_t vio_read(Vio *vio, uchar * buf, size_t size);
79 size_t vio_read_buff(Vio *vio, uchar * buf, size_t size);
80 size_t vio_write(Vio *vio, const uchar * buf, size_t size);
81 /* setsockopt TCP_NODELAY at IPPROTO_TCP level, when possible */
82 int vio_fastsend(Vio *vio);
83 /* setsockopt SO_KEEPALIVE at SOL_SOCKET level, when possible */
84 int vio_keepalive(Vio *vio, my_bool onoff);
85 /* Whenever we should retry the last read/write operation. */
86 my_bool vio_should_retry(Vio *vio);
87 /* Check that operation was timed out */
88 my_bool vio_was_timeout(Vio *vio);
89 /* Short text description of the socket for those, who are curious.. */
90 const char* vio_description(Vio *vio);
91 /* Return the type of the connection */
92 enum enum_vio_type vio_type(Vio* vio);
93 /* Return last error number */
94 int vio_errno(Vio*vio);
95 /* Get socket number */
96 my_socket vio_fd(Vio*vio);
97 /* Remote peer's address and name in text form */
98 my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen);
99 /* Wait for an I/O event notification. */
100 int vio_io_wait(Vio *vio, enum enum_vio_io_event event, int timeout);
101 my_bool vio_is_connected(Vio *vio);
102 #ifndef DBUG_OFF
103 ssize_t vio_pending(Vio *vio);
104 #endif
105 /* Set timeout for a network operation. */
106 int vio_timeout(Vio *vio, uint which, int timeout_sec);
107 /* Connect to a peer. */
108 my_bool vio_socket_connect(Vio *vio, struct sockaddr *addr, socklen_t len,
109  int timeout);
110 
111 my_bool vio_get_normalized_ip_string(const struct sockaddr *addr, int addr_length,
112  char *ip_string, size_t ip_string_size);
113 
114 my_bool vio_is_no_name_error(int err_code);
115 
116 int vio_getnameinfo(const struct sockaddr *sa,
117  char *hostname, size_t hostname_size,
118  char *port, size_t port_size,
119  int flags);
120 
121 #ifdef HAVE_OPENSSL
122 #include <openssl/opensslv.h>
123 #if OPENSSL_VERSION_NUMBER < 0x0090700f
124 #define DES_cblock des_cblock
125 #define DES_key_schedule des_key_schedule
126 #define DES_set_key_unchecked(k,ks) des_set_key_unchecked((k),*(ks))
127 #define DES_ede3_cbc_encrypt(i,o,l,k1,k2,k3,iv,e) des_ede3_cbc_encrypt((i),(o),(l),*(k1),*(k2),*(k3),(iv),(e))
128 #endif
129 /* apple deprecated openssl in MacOSX Lion */
130 #ifdef __APPLE__
131 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
132 #endif
133 
134 #define HEADER_DES_LOCL_H dummy_something
135 #define YASSL_MYSQL_COMPATIBLE
136 #ifndef YASSL_PREFIX
137 #define YASSL_PREFIX
138 #endif
139 /* Set yaSSL to use same type as MySQL do for socket handles */
140 typedef my_socket YASSL_SOCKET_T;
141 #define YASSL_SOCKET_T_DEFINED
142 #include <openssl/ssl.h>
143 #include <openssl/err.h>
144 
145 #ifndef EMBEDDED_LIBRARY
146 enum enum_ssl_init_error
147 {
148  SSL_INITERR_NOERROR= 0, SSL_INITERR_CERT, SSL_INITERR_KEY,
149  SSL_INITERR_NOMATCH, SSL_INITERR_BAD_PATHS, SSL_INITERR_CIPHERS,
150  SSL_INITERR_MEMFAIL, SSL_INITERR_LASTERR
151 };
152 const char* sslGetErrString(enum enum_ssl_init_error err);
153 
154 struct st_VioSSLFd
155 {
156  SSL_CTX *ssl_context;
157 };
158 
159 int sslaccept(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr);
160 int sslconnect(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr);
161 
162 struct st_VioSSLFd
163 *new_VioSSLConnectorFd(const char *key_file, const char *cert_file,
164  const char *ca_file, const char *ca_path,
165  const char *cipher, enum enum_ssl_init_error *error,
166  const char *crl_file, const char *crl_path);
167 struct st_VioSSLFd
168 *new_VioSSLAcceptorFd(const char *key_file, const char *cert_file,
169  const char *ca_file,const char *ca_path,
170  const char *cipher, enum enum_ssl_init_error *error,
171  const char *crl_file, const char *crl_path);
172 void free_vio_ssl_acceptor_fd(struct st_VioSSLFd *fd);
173 #endif /* ! EMBEDDED_LIBRARY */
174 #endif /* HAVE_OPENSSL */
175 
176 void ssl_start(void);
177 void vio_end(void);
178 
179 #ifdef __cplusplus
180 }
181 #endif
182 
183 #if !defined(DONT_MAP_VIO)
184 #define vio_delete(vio) (vio)->viodelete(vio)
185 #define vio_errno(vio) (vio)->vioerrno(vio)
186 #define vio_read(vio, buf, size) ((vio)->read)(vio,buf,size)
187 #define vio_write(vio, buf, size) ((vio)->write)(vio, buf, size)
188 #define vio_fastsend(vio) (vio)->fastsend(vio)
189 #define vio_keepalive(vio, set_keep_alive) (vio)->viokeepalive(vio, set_keep_alive)
190 #define vio_should_retry(vio) (vio)->should_retry(vio)
191 #define vio_was_timeout(vio) (vio)->was_timeout(vio)
192 #define vio_shutdown(vio) ((vio)->vioshutdown)(vio)
193 #define vio_peer_addr(vio, buf, prt, buflen) (vio)->peer_addr(vio, buf, prt, buflen)
194 #define vio_io_wait(vio, event, timeout) (vio)->io_wait(vio, event, timeout)
195 #define vio_is_connected(vio) (vio)->is_connected(vio)
196 #endif /* !defined(DONT_MAP_VIO) */
197 
198 /* This enumerator is used in parser - should be always visible */
199 enum SSL_type
200 {
201  SSL_TYPE_NOT_SPECIFIED= -1,
202  SSL_TYPE_NONE,
203  SSL_TYPE_ANY,
204  SSL_TYPE_X509,
205  SSL_TYPE_SPECIFIED
206 };
207 
208 
209 /* HFTODO - hide this if we don't want client in embedded server */
210 /* This structure is for every connection on both sides */
211 struct st_vio
212 {
213  MYSQL_SOCKET mysql_socket; /* Instrumented socket */
214  my_bool localhost; /* Are we from localhost? */
215  struct sockaddr_storage local; /* Local internet address */
216  struct sockaddr_storage remote; /* Remote internet address */
217  int addrLen; /* Length of remote address */
218  enum enum_vio_type type; /* Type of connection */
219  my_bool inactive; /* Connection inactive (has been shutdown) */
220  char desc[VIO_DESCRIPTION_SIZE]; /* Description string. This
221  member MUST NOT be
222  used directly, but only
223  via function
224  "vio_description" */
225  char *read_buffer; /* buffer for vio_read_buff */
226  char *read_pos; /* start of unfetched data in the
227  read buffer */
228  char *read_end; /* end of unfetched data */
229  int read_timeout; /* Timeout value (ms) for read ops. */
230  int write_timeout; /* Timeout value (ms) for write ops. */
231 
232  /*
233  VIO vtable interface to be implemented by VIO's like SSL, Socket,
234  Named Pipe, etc.
235  */
236 
237  /*
238  viodelete is responsible for cleaning up the VIO object by freeing
239  internal buffers, closing descriptors, handles.
240  */
241  void (*viodelete)(Vio*);
242  int (*vioerrno)(Vio*);
243  size_t (*read)(Vio*, uchar *, size_t);
244  size_t (*write)(Vio*, const uchar *, size_t);
245  int (*timeout)(Vio*, uint, my_bool);
246  int (*viokeepalive)(Vio*, my_bool);
247  int (*fastsend)(Vio*);
248  my_bool (*peer_addr)(Vio*, char *, uint16*, size_t);
249  void (*in_addr)(Vio*, struct sockaddr_storage*);
250  my_bool (*should_retry)(Vio*);
251  my_bool (*was_timeout)(Vio*);
252  /*
253  vioshutdown is resposnible to shutdown/close the channel, so that no
254  further communications can take place, however any related buffers,
255  descriptors, handles can remain valid after a shutdown.
256  */
257  int (*vioshutdown)(Vio*);
258  my_bool (*is_connected)(Vio*);
259  my_bool (*has_data) (Vio*);
260  int (*io_wait)(Vio*, enum enum_vio_io_event, int);
261  my_bool (*connect)(Vio*, struct sockaddr *, socklen_t, int);
262 #ifdef _WIN32
263  OVERLAPPED overlapped;
264  HANDLE hPipe;
265 #endif
266 #ifdef HAVE_OPENSSL
267  void *ssl_arg;
268 #endif
269 #ifdef HAVE_SMEM
270  HANDLE handle_file_map;
271  char *handle_map;
272  HANDLE event_server_wrote;
273  HANDLE event_server_read;
274  HANDLE event_client_wrote;
275  HANDLE event_client_read;
276  HANDLE event_conn_closed;
277  size_t shared_memory_remain;
278  char *shared_memory_pos;
279 #endif /* HAVE_SMEM */
280 };
281 #endif /* vio_violite_h_ */