violite.h 7.75 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4
/* Copyright (C) 2000 MySQL AB

   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
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6 7

   This program is distributed in the hope that it will be useful,
unknown's avatar
unknown committed
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
9 10 11 12 13 14
   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
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
unknown's avatar
unknown committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

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

#ifndef vio_violite_h_
#define	vio_violite_h_

#include "my_net.h"			/* needed because of struct in_addr */


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

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

unknown's avatar
unknown committed
33 34 35 36 37
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
38

39 40 41 42 43 44

#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 */

Vio*	vio_new(my_socket sd, enum enum_vio_type type, uint flags);
unknown's avatar
unknown committed
45
#ifdef __WIN__
unknown's avatar
unknown committed
46 47 48 49 50 51 52 53
Vio* vio_new_win32pipe(HANDLE hPipe);
Vio* vio_new_win32shared_memory(NET *net,HANDLE handle_file_map,
                                HANDLE handle_map,
                                HANDLE event_server_wrote,
                                HANDLE event_server_read,
                                HANDLE event_client_wrote,
                                HANDLE event_client_read,
                                HANDLE event_conn_closed);
54 55
size_t vio_read_pipe(Vio *vio, uchar * buf, size_t size);
size_t vio_write_pipe(Vio *vio, const uchar * buf, size_t size);
unknown's avatar
unknown committed
56
int vio_close_pipe(Vio * vio);
unknown's avatar
unknown committed
57 58 59 60
#else
#define HANDLE void *
#endif /* __WIN__ */

61
void	vio_delete(Vio* vio);
62
int	vio_close(Vio* vio);
63
void    vio_reset(Vio* vio, enum enum_vio_type type,
64
                  my_socket sd, HANDLE hPipe, uint flags);
65 66 67
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);
68
int	vio_blocking(Vio *vio, my_bool onoff, my_bool *old_mode);
69
my_bool	vio_is_blocking(Vio *vio);
70
/* setsockopt TCP_NODELAY at IPPROTO_TCP level, when possible */
71
int	vio_fastsend(Vio *vio);
72
/* setsockopt SO_KEEPALIVE at SOL_SOCKET level, when possible */
73
int	vio_keepalive(Vio *vio, my_bool	onoff);
74
/* Whenever we should retry the last read/write operation. */
75
my_bool	vio_should_retry(Vio *vio);
76 77
/* Check that operation was timed out */
my_bool	vio_was_interrupted(Vio *vio);
78
/* Short text description of the socket for those, who are curious.. */
79
const char* vio_description(Vio *vio);
unknown's avatar
unknown committed
80
/* Return the type of the connection */
81
enum enum_vio_type vio_type(Vio* vio);
unknown's avatar
unknown committed
82
/* Return last error number */
unknown's avatar
unknown committed
83
int	vio_errno(Vio*vio);
unknown's avatar
unknown committed
84
/* Get socket number */
unknown's avatar
unknown committed
85
my_socket vio_fd(Vio*vio);
86
/* Remote peer's address and name in text form */
unknown's avatar
unknown committed
87
my_bool	vio_peer_addr(Vio* vio, char *buf, uint16 *port);
unknown's avatar
unknown committed
88
/* Remotes in_addr */
unknown's avatar
unknown committed
89 90
void	vio_in_addr(Vio *vio, struct in_addr *in);
my_bool	vio_poll_read(Vio *vio,uint timeout);
unknown's avatar
unknown committed
91

unknown's avatar
unknown committed
92
#ifdef HAVE_OPENSSL
93 94 95 96 97
#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))
98
#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))
99 100
#endif

101
#define HEADER_DES_LOCL_H dummy_something
102
#define YASSL_MYSQL_COMPATIBLE
unknown's avatar
unknown committed
103
#ifndef YASSL_PREFIX
104
#define YASSL_PREFIX
unknown's avatar
unknown committed
105
#endif
106 107 108
/* 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
109 110
#include <openssl/ssl.h>
#include <openssl/err.h>
unknown's avatar
unknown committed
111

unknown's avatar
unknown committed
112
struct st_VioSSLFd
unknown's avatar
unknown committed
113
{
114
  SSL_CTX *ssl_context;
unknown's avatar
unknown committed
115
};
unknown's avatar
unknown committed
116

unknown's avatar
unknown committed
117 118
int sslaccept(struct st_VioSSLFd*, Vio *, long timeout);
int sslconnect(struct st_VioSSLFd*, Vio *, long timeout);
119

unknown's avatar
unknown committed
120
struct st_VioSSLFd
121 122 123
*new_VioSSLConnectorFd(const char *key_file, const char *cert_file,
		       const char *ca_file,  const char *ca_path,
		       const char *cipher);
unknown's avatar
unknown committed
124
struct st_VioSSLFd
125 126 127
*new_VioSSLAcceptorFd(const char *key_file, const char *cert_file,
		      const char *ca_file,const char *ca_path,
		      const char *cipher);
128
void free_vio_ssl_acceptor_fd(struct st_VioSSLFd *fd);
unknown's avatar
unknown committed
129
#endif /* HAVE_OPENSSL */
130

131
#ifdef HAVE_SMEM
132 133
size_t vio_read_shared_memory(Vio *vio, uchar * buf, size_t size);
size_t vio_write_shared_memory(Vio *vio, const uchar * buf, size_t size);
134 135
int vio_close_shared_memory(Vio * vio);
#endif
136

137 138
void vio_end(void);

unknown's avatar
unknown committed
139 140
#ifdef	__cplusplus
}
141 142
#endif

143
#if !defined(DONT_MAP_VIO)
unknown's avatar
unknown committed
144 145
#define vio_delete(vio) 			(vio)->viodelete(vio)
#define vio_errno(vio)	 			(vio)->vioerrno(vio)
146 147
#define vio_read(vio, buf, size)                ((vio)->read)(vio,buf,size)
#define vio_write(vio, buf, size)               ((vio)->write)(vio, buf, size)
148 149
#define vio_blocking(vio, set_blocking_mode, old_mode)\
 	(vio)->vioblocking(vio, set_blocking_mode, old_mode)
unknown's avatar
unknown committed
150 151 152 153
#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)
154
#define vio_was_interrupted(vio) 		(vio)->was_interrupted(vio)
unknown's avatar
unknown committed
155
#define vio_close(vio)				((vio)->vioclose)(vio)
156
#define vio_peer_addr(vio, buf, prt)		(vio)->peer_addr(vio, buf, prt)
unknown's avatar
unknown committed
157
#define vio_in_addr(vio, in)			(vio)->in_addr(vio, in)
158
#define vio_timeout(vio, which, seconds)	(vio)->timeout(vio, which, seconds)
159
#endif /* !defined(DONT_MAP_VIO) */
unknown's avatar
unknown committed
160

161
/* This enumerator is used in parser - should be always visible */
162 163 164 165 166 167 168 169
enum SSL_type
{
  SSL_TYPE_NOT_SPECIFIED= -1,
  SSL_TYPE_NONE,
  SSL_TYPE_ANY,
  SSL_TYPE_X509,
  SSL_TYPE_SPECIFIED
};
170

unknown's avatar
unknown committed
171

unknown's avatar
SCRUM  
unknown committed
172
/* HFTODO - hide this if we don't want client in embedded server */
unknown's avatar
unknown committed
173 174 175 176 177 178 179 180 181 182 183
/* This structure is for every connection on both sides */
struct st_vio
{
  my_socket		sd;		/* my_socket - real or imaginary */
  HANDLE hPipe;
  my_bool		localhost;	/* Are we from localhost? */
  int			fcntl_mode;	/* Buffered fcntl(sd,F_GETFL) */
  struct sockaddr_in	local;		/* Local internet address */
  struct sockaddr_in	remote;		/* Remote internet address */
  enum enum_vio_type	type;		/* Type of connection */
  char			desc[30];	/* String description */
184 185 186 187
  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 */
unknown's avatar
unknown committed
188
  /* function pointers. They are similar for socket/SSL/whatever */
189 190
  void    (*viodelete)(Vio*);
  int     (*vioerrno)(Vio*);
191 192
  size_t  (*read)(Vio*, uchar *, size_t);
  size_t  (*write)(Vio*, const uchar *, size_t);
193
  int     (*vioblocking)(Vio*, my_bool, my_bool *);
194 195 196
  my_bool (*is_blocking)(Vio*);
  int     (*viokeepalive)(Vio*, my_bool);
  int     (*fastsend)(Vio*);
unknown's avatar
unknown committed
197
  my_bool (*peer_addr)(Vio*, char *, uint16*);
198 199
  void    (*in_addr)(Vio*, struct in_addr*);
  my_bool (*should_retry)(Vio*);
200
  my_bool (*was_interrupted)(Vio*);
201
  int     (*vioclose)(Vio*);
202
  void	  (*timeout)(Vio*, unsigned int which, unsigned int timeout);
203
#ifdef HAVE_OPENSSL
204
  void	  *ssl_arg;
205
#endif
206
#ifdef HAVE_SMEM
unknown's avatar
unknown committed
207 208 209 210 211 212
  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
213
  HANDLE  event_conn_closed;
214
  size_t  shared_memory_remain;
unknown's avatar
unknown committed
215 216
  char    *shared_memory_pos;
  NET     *net;
217
#endif /* HAVE_SMEM */
unknown's avatar
unknown committed
218
};
219
#endif /* vio_violite_h_ */