violite.h 10.5 KB
Newer Older
1 2
/* Copyright (c) 2000, 2012, Oracle and/or its affiliates.
   Copyright (c) 2012 Monty Program Ab
unknown's avatar
unknown committed
3 4 5

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
6
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
7 8

   This program is distributed in the hope that it will be useful,
unknown's avatar
unknown committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
10 11 12 13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
14 15
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
unknown's avatar
unknown committed
16 17 18 19 20 21 22 23 24

/*
 * Vio Lite.
 * Purpose: include file for Vio that will work with C and C++
 */

#ifndef vio_violite_h_
#define	vio_violite_h_

25 26
#include "my_net.h"   /* needed because of struct in_addr */
#include <mysql/psi/mysql_socket.h>
unknown's avatar
unknown committed
27 28 29 30 31 32 33

/* Simple vio interface in C;  The functions are implemented in violite.c */

#ifdef	__cplusplus
extern "C" {
#endif /* __cplusplus */

34 35 36 37
#ifdef __cplusplus
typedef struct st_vio Vio;
#endif /* __cplusplus */

unknown's avatar
unknown committed
38 39 40 41 42
enum enum_vio_type
{
  VIO_CLOSED, VIO_TYPE_TCPIP, VIO_TYPE_SOCKET, VIO_TYPE_NAMEDPIPE,
  VIO_TYPE_SSL, VIO_TYPE_SHARED_MEMORY
};
unknown's avatar
unknown committed
43

44 45 46 47 48 49 50 51 52
/**
  VIO I/O events.
*/
enum enum_vio_io_event
{
  VIO_IO_EVENT_READ,
  VIO_IO_EVENT_WRITE,
  VIO_IO_EVENT_CONNECT
};
53 54 55 56

#define VIO_LOCALHOST 1                         /* a localhost connection */
#define VIO_BUFFERED_READ 2                     /* use buffered read */
#define VIO_READ_BUFFER_SIZE 16384              /* size of read buffer */
57
#define VIO_DESCRIPTION_SIZE 30                 /* size of description */
58

59 60
Vio* vio_new(my_socket sd, enum enum_vio_type type, uint flags);
Vio*  mysql_socket_vio_new(MYSQL_SOCKET mysql_socket, enum enum_vio_type type, uint flags);
unknown's avatar
unknown committed
61
#ifdef __WIN__
unknown's avatar
unknown committed
62
Vio* vio_new_win32pipe(HANDLE hPipe);
63
Vio* vio_new_win32shared_memory(HANDLE handle_file_map,
unknown's avatar
unknown committed
64 65 66 67 68 69
                                HANDLE handle_map,
                                HANDLE event_server_wrote,
                                HANDLE event_server_read,
                                HANDLE event_client_wrote,
                                HANDLE event_client_read,
                                HANDLE event_conn_closed);
unknown's avatar
unknown committed
70 71 72 73
#else
#define HANDLE void *
#endif /* __WIN__ */

74
void	vio_delete(Vio* vio);
75
int	vio_close(Vio* vio);
76 77
my_bool vio_reset(Vio* vio, enum enum_vio_type type,
                  my_socket sd, void *ssl, uint flags);
78 79 80
size_t	vio_read(Vio *vio, uchar *	buf, size_t size);
size_t  vio_read_buff(Vio *vio, uchar * buf, size_t size);
size_t	vio_write(Vio *vio, const uchar * buf, size_t size);
81
int	vio_blocking(Vio *vio, my_bool onoff, my_bool *old_mode);
82
my_bool	vio_is_blocking(Vio *vio);
83
/* setsockopt TCP_NODELAY at IPPROTO_TCP level, when possible */
84
int	vio_fastsend(Vio *vio);
85
/* setsockopt SO_KEEPALIVE at SOL_SOCKET level, when possible */
86
int	vio_keepalive(Vio *vio, my_bool	onoff);
87
/* Whenever we should retry the last read/write operation. */
88
my_bool	vio_should_retry(Vio *vio);
89
/* Check that operation was timed out */
90
my_bool vio_was_timeout(Vio *vio);
91
/* Short text description of the socket for those, who are curious.. */
92
const char* vio_description(Vio *vio);
unknown's avatar
unknown committed
93
/* Return the type of the connection */
94
enum enum_vio_type vio_type(Vio* vio);
unknown's avatar
unknown committed
95
/* Return last error number */
unknown's avatar
unknown committed
96
int	vio_errno(Vio*vio);
unknown's avatar
unknown committed
97
/* Get socket number */
unknown's avatar
unknown committed
98
my_socket vio_fd(Vio*vio);
99
/* Remote peer's address and name in text form */
100
my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen);
101 102
/* Wait for an I/O event notification. */
int vio_io_wait(Vio *vio, enum enum_vio_io_event event, int timeout);
103
my_bool vio_is_connected(Vio *vio);
104
#ifndef DBUG_OFF
105
ssize_t vio_pending(Vio *vio);
106 107
#endif
/* Set timeout for a network operation. */
108 109 110
extern int vio_timeout(Vio *vio, uint which, int timeout_sec);
extern void vio_set_wait_callback(void (*before_wait)(void),
                                void (*after_wait)(void));
111 112 113
/* Connect to a peer. */
my_bool vio_socket_connect(Vio *vio, struct sockaddr *addr, socklen_t len,
                           int timeout);
unknown's avatar
unknown committed
114

115 116 117
my_bool vio_get_normalized_ip_string(const struct sockaddr *addr, int addr_length,
                                     char *ip_string, size_t ip_string_size);

118 119
my_bool vio_is_no_name_error(int err_code);

120 121 122 123 124
int vio_getnameinfo(const struct sockaddr *sa,
                    char *hostname, size_t hostname_size,
                    char *port, size_t port_size,
                    int flags);

unknown's avatar
unknown committed
125
#ifdef HAVE_OPENSSL
126 127 128 129 130
#include <openssl/opensslv.h>
#if OPENSSL_VERSION_NUMBER < 0x0090700f
#define DES_cblock des_cblock
#define DES_key_schedule des_key_schedule
#define DES_set_key_unchecked(k,ks) des_set_key_unchecked((k),*(ks))
131
#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))
132
#endif
133 134 135 136
/* apple deprecated openssl in MacOSX Lion */
#ifdef __APPLE__
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
137

138
#define HEADER_DES_LOCL_H dummy_something
139
#define YASSL_MYSQL_COMPATIBLE
unknown's avatar
unknown committed
140
#ifndef YASSL_PREFIX
141
#define YASSL_PREFIX
unknown's avatar
unknown committed
142
#endif
143 144 145
/* Set yaSSL to use same type as MySQL do for socket handles */
typedef my_socket YASSL_SOCKET_T;
#define YASSL_SOCKET_T_DEFINED
unknown's avatar
unknown committed
146 147
#include <openssl/ssl.h>
#include <openssl/err.h>
unknown's avatar
unknown committed
148

149 150 151 152
#ifdef HAVE_ERR_remove_thread_state
#define ERR_remove_state(X) ERR_remove_thread_state(NULL)
#endif

153 154 155 156
enum enum_ssl_init_error
{
  SSL_INITERR_NOERROR= 0, SSL_INITERR_CERT, SSL_INITERR_KEY, 
  SSL_INITERR_NOMATCH, SSL_INITERR_BAD_PATHS, SSL_INITERR_CIPHERS, 
157
  SSL_INITERR_MEMFAIL, SSL_INITERR_DH, SSL_INITERR_LASTERR
158 159 160
};
const char* sslGetErrString(enum enum_ssl_init_error err);

unknown's avatar
unknown committed
161
struct st_VioSSLFd
unknown's avatar
unknown committed
162
{
163
  SSL_CTX *ssl_context;
unknown's avatar
unknown committed
164
};
unknown's avatar
unknown committed
165

166 167
int sslaccept(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr);
int sslconnect(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr);
168

unknown's avatar
unknown committed
169
struct st_VioSSLFd
170 171
*new_VioSSLConnectorFd(const char *key_file, const char *cert_file,
		       const char *ca_file,  const char *ca_path,
172 173
		       const char *cipher, enum enum_ssl_init_error *error,
                       const char *crl_file, const char *crl_path);
unknown's avatar
unknown committed
174
struct st_VioSSLFd
175 176
*new_VioSSLAcceptorFd(const char *key_file, const char *cert_file,
		      const char *ca_file,const char *ca_path,
177 178
		      const char *cipher, enum enum_ssl_init_error *error,
                      const char *crl_file, const char *crl_path);
179
void free_vio_ssl_acceptor_fd(struct st_VioSSLFd *fd);
unknown's avatar
unknown committed
180
#endif /* HAVE_OPENSSL */
181

182 183
void vio_end(void);

unknown's avatar
unknown committed
184 185
#ifdef	__cplusplus
}
186 187
#endif

188
#if !defined(DONT_MAP_VIO)
unknown's avatar
unknown committed
189 190
#define vio_delete(vio) 			(vio)->viodelete(vio)
#define vio_errno(vio)	 			(vio)->vioerrno(vio)
191 192
#define vio_read(vio, buf, size)                ((vio)->read)(vio,buf,size)
#define vio_write(vio, buf, size)               ((vio)->write)(vio, buf, size)
193 194
#define vio_blocking(vio, set_blocking_mode, old_mode)\
 	(vio)->vioblocking(vio, set_blocking_mode, old_mode)
unknown's avatar
unknown committed
195 196 197 198
#define vio_is_blocking(vio) 			(vio)->is_blocking(vio)
#define vio_fastsend(vio)			(vio)->fastsend(vio)
#define vio_keepalive(vio, set_keep_alive)	(vio)->viokeepalive(vio, set_keep_alive)
#define vio_should_retry(vio) 			(vio)->should_retry(vio)
199
#define vio_was_timeout(vio)                    (vio)->was_timeout(vio)
unknown's avatar
unknown committed
200
#define vio_close(vio)				((vio)->vioclose)(vio)
201
#define vio_shutdown(vio,how)			((vio)->shutdown)(vio,how)
202
#define vio_peer_addr(vio, buf, prt, buflen)	(vio)->peer_addr(vio, buf, prt, buflen)
203
#define vio_io_wait(vio, event, timeout)        (vio)->io_wait(vio, event, timeout)
204
#define vio_is_connected(vio)                   (vio)->is_connected(vio)
205
#endif /* !defined(DONT_MAP_VIO) */
unknown's avatar
unknown committed
206

207
#ifdef _WIN32
208 209 210 211 212 213

/* shutdown(2) flags */
#ifndef SHUT_RD
#define SHUT_RD SD_BOTH
#endif

214 215 216 217 218 219 220 221 222 223
/*
  Set thread id for io cancellation (required on Windows XP only,
  and should to be removed if XP is no more supported)
*/

#define vio_set_thread_id(vio, tid) if(vio) vio->thread_id= tid
#else
#define vio_set_thread_id(vio, tid)
#endif

224
/* This enumerator is used in parser - should be always visible */
225 226 227 228 229 230 231 232
enum SSL_type
{
  SSL_TYPE_NOT_SPECIFIED= -1,
  SSL_TYPE_NONE,
  SSL_TYPE_ANY,
  SSL_TYPE_X509,
  SSL_TYPE_SPECIFIED
};
233

unknown's avatar
unknown committed
234

unknown's avatar
SCRUM  
unknown committed
235
/* HFTODO - hide this if we don't want client in embedded server */
unknown's avatar
unknown committed
236 237 238
/* This structure is for every connection on both sides */
struct st_vio
{
239
  MYSQL_SOCKET  mysql_socket;     /* Instrumented socket */
unknown's avatar
unknown committed
240 241
  my_bool		localhost;	/* Are we from localhost? */
  int			fcntl_mode;	/* Buffered fcntl(sd,F_GETFL) */
242 243
  struct sockaddr_storage local;	/* Local internet address */
  struct sockaddr_storage remote;	/* Remote internet address */
244
  int addrLen;                          /* Length of remote address */
unknown's avatar
unknown committed
245
  enum enum_vio_type	type;		/* Type of connection */
Michael Widenius's avatar
Michael Widenius committed
246
  const char		*desc;		/* String description */
247 248 249 250
  char                  *read_buffer;   /* buffer for vio_read_buff */
  char                  *read_pos;      /* start of unfetched data in the
                                           read buffer */
  char                  *read_end;      /* end of unfetched data */
251
  struct mysql_async_context *async_context; /* For non-blocking API */
252 253
  int                   read_timeout;   /* Timeout value (ms) for read ops. */
  int                   write_timeout;  /* Timeout value (ms) for write ops. */
unknown's avatar
unknown committed
254
  /* function pointers. They are similar for socket/SSL/whatever */
255 256
  void    (*viodelete)(Vio*);
  int     (*vioerrno)(Vio*);
257 258
  size_t  (*read)(Vio*, uchar *, size_t);
  size_t  (*write)(Vio*, const uchar *, size_t);
259
  int     (*timeout)(Vio*, uint, my_bool);
260
  int     (*vioblocking)(Vio*, my_bool, my_bool *);
261 262 263
  my_bool (*is_blocking)(Vio*);
  int     (*viokeepalive)(Vio*, my_bool);
  int     (*fastsend)(Vio*);
264 265
  my_bool (*peer_addr)(Vio*, char *, uint16*, size_t);
  void    (*in_addr)(Vio*, struct sockaddr_storage*);
266
  my_bool (*should_retry)(Vio*);
267
  my_bool (*was_timeout)(Vio*);
268
  int     (*vioclose)(Vio*);
269
  my_bool (*is_connected)(Vio*);
270
  int (*shutdown)(Vio *, int);
271
  my_bool (*has_data) (Vio*);
272 273
  int (*io_wait)(Vio*, enum enum_vio_io_event, int);
  my_bool (*connect)(Vio*, struct sockaddr *, socklen_t, int);
274
#ifdef HAVE_OPENSSL
275
  void	  *ssl_arg;
276
#endif
277
#ifdef HAVE_SMEM
unknown's avatar
unknown committed
278 279 280 281 282 283
  HANDLE  handle_file_map;
  char    *handle_map;
  HANDLE  event_server_wrote;
  HANDLE  event_server_read;
  HANDLE  event_client_wrote;
  HANDLE  event_client_read;
unknown's avatar
unknown committed
284
  HANDLE  event_conn_closed;
285
  size_t  shared_memory_remain;
unknown's avatar
unknown committed
286
  char    *shared_memory_pos;
287
#endif /* HAVE_SMEM */
288 289 290 291 292 293 294
#ifdef _WIN32
  HANDLE hPipe;
  OVERLAPPED overlapped;
  DWORD thread_id; /* Used on XP only by vio_shutdown() */
  DWORD read_timeout_ms;
  DWORD write_timeout_ms;
#endif
unknown's avatar
unknown committed
295
};
296
#endif /* vio_violite_h_ */