1 /* Copyright (c) 2000, 2015, 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 Foundation, Inc.,
14    51 Franklin Street, Suite 500, Boston, MA 02110-1335 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 
26 
27 /* Simple vio interface in C;  The functions are implemented in violite.c */
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif /* __cplusplus */
32 
33 #ifdef __cplusplus
34 typedef struct st_vio Vio;
35 #endif /* __cplusplus */
36 
37 enum enum_vio_type
38 {
39   VIO_CLOSED, VIO_TYPE_TCPIP, VIO_TYPE_SOCKET, VIO_TYPE_NAMEDPIPE,
40   VIO_TYPE_SSL, VIO_TYPE_SHARED_MEMORY
41 };
42 
43 
44 #define VIO_LOCALHOST 1                         /* a localhost connection */
45 #define VIO_BUFFERED_READ 2                     /* use buffered read */
46 #define VIO_READ_BUFFER_SIZE 16384              /* size of read buffer */
47 
48 Vio*	vio_new(my_socket sd, enum enum_vio_type type, uint flags);
49 #ifdef __WIN__
50 Vio* vio_new_win32pipe(HANDLE hPipe);
51 Vio* vio_new_win32shared_memory(HANDLE handle_file_map,
52                                 HANDLE handle_map,
53                                 HANDLE event_server_wrote,
54                                 HANDLE event_server_read,
55                                 HANDLE event_client_wrote,
56                                 HANDLE event_client_read,
57                                 HANDLE event_conn_closed);
58 #else
59 #define HANDLE void *
60 #endif /* __WIN__ */
61 
62 /* backport from 5.6 where it is part of PSI, not vio_*() */
63 int	mysql_socket_shutdown(my_socket mysql_socket, int how);
64 
65 void	vio_delete(Vio* vio);
66 int	vio_close(Vio* vio);
67 void    vio_reset(Vio* vio, enum enum_vio_type type,
68                   my_socket sd, HANDLE hPipe, uint flags);
69 size_t	vio_read(Vio *vio, uchar *	buf, size_t size);
70 size_t  vio_read_buff(Vio *vio, uchar * buf, size_t size);
71 size_t	vio_write(Vio *vio, const uchar * buf, size_t size);
72 int	vio_blocking(Vio *vio, my_bool onoff, my_bool *old_mode);
73 my_bool	vio_is_blocking(Vio *vio);
74 /* setsockopt TCP_NODELAY at IPPROTO_TCP level, when possible */
75 int	vio_fastsend(Vio *vio);
76 /* setsockopt SO_KEEPALIVE at SOL_SOCKET level, when possible */
77 int	vio_keepalive(Vio *vio, my_bool	onoff);
78 /* Whenever we should retry the last read/write operation. */
79 my_bool	vio_should_retry(Vio *vio);
80 /* Check that operation was timed out */
81 my_bool	vio_was_interrupted(Vio *vio);
82 /* Short text description of the socket for those, who are curious.. */
83 const char* vio_description(Vio *vio);
84 /* Return the type of the connection */
85 enum enum_vio_type vio_type(Vio* vio);
86 /* Return last error number */
87 int	vio_errno(Vio*vio);
88 /* Get socket number */
89 my_socket vio_fd(Vio*vio);
90 /* Remote peer's address and name in text form */
91 my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen);
92 my_bool vio_poll_read(Vio *vio, uint timeout);
93 my_bool vio_is_connected(Vio *vio);
94 ssize_t vio_pending(Vio *vio);
95 
96 my_bool vio_get_normalized_ip_string(const struct sockaddr *addr, int addr_length,
97                                      char *ip_string, size_t ip_string_size);
98 
99 my_bool vio_is_no_name_error(int err_code);
100 
101 int vio_getnameinfo(const struct sockaddr *sa,
102                     char *hostname, size_t hostname_size,
103                     char *port, size_t port_size,
104                     int flags);
105 
106 #ifdef HAVE_OPENSSL
107 #include <openssl/opensslv.h>
108 #if OPENSSL_VERSION_NUMBER < 0x0090700f
109 #define DES_cblock des_cblock
110 #define DES_key_schedule des_key_schedule
111 #define DES_set_key_unchecked(k,ks) des_set_key_unchecked((k),*(ks))
112 #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))
113 #endif
114 
115 #define HEADER_DES_LOCL_H dummy_something
116 #define YASSL_MYSQL_COMPATIBLE
117 #ifndef YASSL_PREFIX
118 #define YASSL_PREFIX
119 #endif
120 /* Set yaSSL to use same type as MySQL do for socket handles */
121 typedef my_socket YASSL_SOCKET_T;
122 #define YASSL_SOCKET_T_DEFINED
123 #include <openssl/ssl.h>
124 #include <openssl/err.h>
125 
126 #ifndef EMBEDDED_LIBRARY
127 enum enum_ssl_init_error
128 {
129   SSL_INITERR_NOERROR= 0, SSL_INITERR_CERT, SSL_INITERR_KEY,
130   SSL_INITERR_NOMATCH, SSL_INITERR_BAD_PATHS, SSL_INITERR_CIPHERS,
131   SSL_INITERR_MEMFAIL, SSL_INITERR_DHFAIL, SSL_INITERR_LASTERR
132 };
133 const char* sslGetErrString(enum enum_ssl_init_error err);
134 
135 struct st_VioSSLFd
136 {
137   SSL_CTX *ssl_context;
138 };
139 
140 int sslaccept(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr);
141 int sslconnect(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr);
142 
143 struct st_VioSSLFd
144 *new_VioSSLConnectorFd(const char *key_file, const char *cert_file,
145 		       const char *ca_file,  const char *ca_path,
146 		       const char *cipher, enum enum_ssl_init_error* error);
147 struct st_VioSSLFd
148 *new_VioSSLAcceptorFd(const char *key_file, const char *cert_file,
149 		      const char *ca_file,const char *ca_path,
150 		      const char *cipher, enum enum_ssl_init_error* error);
151 void free_vio_ssl_acceptor_fd(struct st_VioSSLFd *fd);
152 #endif /* ! EMBEDDED_LIBRARY */
153 #endif /* HAVE_OPENSSL */
154 
155 void vio_end(void);
156 
157 #ifdef	__cplusplus
158 }
159 #endif
160 
161 #if !defined(DONT_MAP_VIO)
162 #define vio_delete(vio) 			(vio)->viodelete(vio)
163 #define vio_errno(vio)	 			(vio)->vioerrno(vio)
164 #define vio_read(vio, buf, size)                ((vio)->read)(vio,buf,size)
165 #define vio_write(vio, buf, size)               ((vio)->write)(vio, buf, size)
166 #define vio_blocking(vio, set_blocking_mode, old_mode)\
167  	(vio)->vioblocking(vio, set_blocking_mode, old_mode)
168 #define vio_is_blocking(vio) 			(vio)->is_blocking(vio)
169 #define vio_fastsend(vio)			(vio)->fastsend(vio)
170 #define vio_keepalive(vio, set_keep_alive)	(vio)->viokeepalive(vio, set_keep_alive)
171 #define vio_should_retry(vio) 			(vio)->should_retry(vio)
172 #define vio_was_interrupted(vio) 		(vio)->was_interrupted(vio)
173 #define vio_close(vio)				((vio)->vioclose)(vio)
174 #define vio_peer_addr(vio, buf, prt, buflen)	(vio)->peer_addr(vio, buf, prt, buflen)
175 #define vio_timeout(vio, which, seconds)	(vio)->timeout(vio, which, seconds)
176 #define vio_poll_read(vio, timeout)             (vio)->poll_read(vio, timeout)
177 #define vio_is_connected(vio)                   (vio)->is_connected(vio)
178 #endif /* !defined(DONT_MAP_VIO) */
179 
180 /* This enumerator is used in parser - should be always visible */
181 enum SSL_type
182 {
183   SSL_TYPE_NOT_SPECIFIED= -1,
184   SSL_TYPE_NONE,
185   SSL_TYPE_ANY,
186   SSL_TYPE_X509,
187   SSL_TYPE_SPECIFIED
188 };
189 
190 
191 /* HFTODO - hide this if we don't want client in embedded server */
192 /* This structure is for every connection on both sides */
193 struct st_vio
194 {
195   my_socket		sd;		/* my_socket - real or imaginary */
196   HANDLE hPipe;
197   my_bool		localhost;	/* Are we from localhost? */
198   int			fcntl_mode;	/* Buffered fcntl(sd,F_GETFL) */
199   struct sockaddr_storage	local;		/* Local internet address */
200   struct sockaddr_storage	remote;		/* Remote internet address */
201   int addrLen;                          /* Length of remote address */
202   enum enum_vio_type	type;		/* Type of connection */
203   char			desc[30];	/* String description */
204   char                  *read_buffer;   /* buffer for vio_read_buff */
205   char                  *read_pos;      /* start of unfetched data in the
206                                            read buffer */
207   char                  *read_end;      /* end of unfetched data */
208   /* function pointers. They are similar for socket/SSL/whatever */
209   void    (*viodelete)(Vio*);
210   int     (*vioerrno)(Vio*);
211   size_t  (*read)(Vio*, uchar *, size_t);
212   size_t  (*write)(Vio*, const uchar *, size_t);
213   int     (*vioblocking)(Vio*, my_bool, my_bool *);
214   my_bool (*is_blocking)(Vio*);
215   int     (*viokeepalive)(Vio*, my_bool);
216   int     (*fastsend)(Vio*);
217   my_bool (*peer_addr)(Vio*, char *, uint16*, size_t);
218   void    (*in_addr)(Vio*, struct sockaddr_storage*);
219   my_bool (*should_retry)(Vio*);
220   my_bool (*was_interrupted)(Vio*);
221   int     (*vioclose)(Vio*);
222   void	  (*timeout)(Vio*, unsigned int which, unsigned int timeout);
223   my_bool (*poll_read)(Vio *vio, uint timeout);
224   my_bool (*is_connected)(Vio*);
225   my_bool (*has_data) (Vio*);
226 #ifdef HAVE_OPENSSL
227   void	  *ssl_arg;
228 #endif
229 #ifdef HAVE_SMEM
230   HANDLE  handle_file_map;
231   char    *handle_map;
232   HANDLE  event_server_wrote;
233   HANDLE  event_server_read;
234   HANDLE  event_client_wrote;
235   HANDLE  event_client_read;
236   HANDLE  event_conn_closed;
237   size_t  shared_memory_remain;
238   char    *shared_memory_pos;
239 #endif /* HAVE_SMEM */
240 #ifdef _WIN32
241   OVERLAPPED pipe_overlapped;
242   DWORD read_timeout_ms;
243   DWORD write_timeout_ms;
244 #endif
245 };
246 #endif /* vio_violite_h_ */
247