violite.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Copyright (c) 2000, 2012, Oracle and/or its affiliates.
  2. Copyright (c) 2012 Monty Program Ab
  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. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. /*
  14. * Vio Lite.
  15. * Purpose: include file for Vio that will work with C and C++
  16. */
  17. #ifndef vio_violite_h_
  18. #define vio_violite_h_
  19. #include "my_net.h" /* needed because of struct in_addr */
  20. #include <mysql/psi/mysql_socket.h>
  21. /* Simple vio interface in C; The functions are implemented in violite.c */
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif /* __cplusplus */
  25. #ifdef __cplusplus
  26. typedef struct st_vio Vio;
  27. #endif /* __cplusplus */
  28. enum enum_vio_type
  29. {
  30. VIO_CLOSED, VIO_TYPE_TCPIP, VIO_TYPE_SOCKET, VIO_TYPE_NAMEDPIPE,
  31. VIO_TYPE_SSL, VIO_TYPE_SHARED_MEMORY
  32. };
  33. /**
  34. VIO I/O events.
  35. */
  36. enum enum_vio_io_event
  37. {
  38. VIO_IO_EVENT_READ,
  39. VIO_IO_EVENT_WRITE,
  40. VIO_IO_EVENT_CONNECT
  41. };
  42. #define VIO_LOCALHOST 1 /* a localhost connection */
  43. #define VIO_BUFFERED_READ 2 /* use buffered read */
  44. #define VIO_READ_BUFFER_SIZE 16384 /* size of read buffer */
  45. #define VIO_DESCRIPTION_SIZE 30 /* size of description */
  46. Vio* vio_new(my_socket sd, enum enum_vio_type type, uint flags);
  47. Vio* mysql_socket_vio_new(MYSQL_SOCKET mysql_socket, enum enum_vio_type type, uint flags);
  48. #ifdef __WIN__
  49. Vio* vio_new_win32pipe(HANDLE hPipe);
  50. Vio* vio_new_win32shared_memory(HANDLE handle_file_map,
  51. HANDLE handle_map,
  52. HANDLE event_server_wrote,
  53. HANDLE event_server_read,
  54. HANDLE event_client_wrote,
  55. HANDLE event_client_read,
  56. HANDLE event_conn_closed);
  57. #else
  58. #define HANDLE void *
  59. #endif /* __WIN__ */
  60. void vio_delete(Vio* vio);
  61. int vio_close(Vio* vio);
  62. my_bool vio_reset(Vio* vio, enum enum_vio_type type,
  63. my_socket sd, void *ssl, uint flags);
  64. size_t vio_read(Vio *vio, uchar * buf, size_t size);
  65. size_t vio_read_buff(Vio *vio, uchar * buf, size_t size);
  66. size_t vio_write(Vio *vio, const uchar * buf, size_t size);
  67. int vio_blocking(Vio *vio, my_bool onoff, my_bool *old_mode);
  68. my_bool vio_is_blocking(Vio *vio);
  69. /* setsockopt TCP_NODELAY at IPPROTO_TCP level, when possible */
  70. int vio_fastsend(Vio *vio);
  71. /* setsockopt SO_KEEPALIVE at SOL_SOCKET level, when possible */
  72. int vio_keepalive(Vio *vio, my_bool onoff);
  73. /* Whenever we should retry the last read/write operation. */
  74. my_bool vio_should_retry(Vio *vio);
  75. /* Check that operation was timed out */
  76. my_bool vio_was_timeout(Vio *vio);
  77. /* Short text description of the socket for those, who are curious.. */
  78. const char* vio_description(Vio *vio);
  79. /* Return the type of the connection */
  80. enum enum_vio_type vio_type(Vio* vio);
  81. /* Return last error number */
  82. int vio_errno(Vio*vio);
  83. /* Get socket number */
  84. my_socket vio_fd(Vio*vio);
  85. /* Remote peer's address and name in text form */
  86. my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen);
  87. /* Wait for an I/O event notification. */
  88. int vio_io_wait(Vio *vio, enum enum_vio_io_event event, int timeout);
  89. my_bool vio_is_connected(Vio *vio);
  90. #ifndef DBUG_OFF
  91. ssize_t vio_pending(Vio *vio);
  92. #endif
  93. /* Set timeout for a network operation. */
  94. extern int vio_timeout(Vio *vio, uint which, int timeout_sec);
  95. extern void vio_set_wait_callback(void (*before_wait)(void),
  96. void (*after_wait)(void));
  97. /* Connect to a peer. */
  98. my_bool vio_socket_connect(Vio *vio, struct sockaddr *addr, socklen_t len,
  99. int timeout);
  100. my_bool vio_get_normalized_ip_string(const struct sockaddr *addr, int addr_length,
  101. char *ip_string, size_t ip_string_size);
  102. my_bool vio_is_no_name_error(int err_code);
  103. int vio_getnameinfo(const struct sockaddr *sa,
  104. char *hostname, size_t hostname_size,
  105. char *port, size_t port_size,
  106. int flags);
  107. #ifdef HAVE_OPENSSL
  108. #include <openssl/opensslv.h>
  109. #if OPENSSL_VERSION_NUMBER < 0x0090700f
  110. #define DES_cblock des_cblock
  111. #define DES_key_schedule des_key_schedule
  112. #define DES_set_key_unchecked(k,ks) des_set_key_unchecked((k),*(ks))
  113. #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))
  114. #endif
  115. /* apple deprecated openssl in MacOSX Lion */
  116. #ifdef __APPLE__
  117. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  118. #endif
  119. #define HEADER_DES_LOCL_H dummy_something
  120. #define YASSL_MYSQL_COMPATIBLE
  121. #ifndef YASSL_PREFIX
  122. #define YASSL_PREFIX
  123. #endif
  124. /* Set yaSSL to use same type as MySQL do for socket handles */
  125. typedef my_socket YASSL_SOCKET_T;
  126. #define YASSL_SOCKET_T_DEFINED
  127. #include <openssl/ssl.h>
  128. #include <openssl/err.h>
  129. #ifdef HAVE_ERR_remove_thread_state
  130. #define ERR_remove_state(X) ERR_remove_thread_state(NULL)
  131. #endif
  132. enum enum_ssl_init_error
  133. {
  134. SSL_INITERR_NOERROR= 0, SSL_INITERR_CERT, SSL_INITERR_KEY,
  135. SSL_INITERR_NOMATCH, SSL_INITERR_BAD_PATHS, SSL_INITERR_CIPHERS,
  136. SSL_INITERR_MEMFAIL, SSL_INITERR_DH, SSL_INITERR_LASTERR
  137. };
  138. const char* sslGetErrString(enum enum_ssl_init_error err);
  139. struct st_VioSSLFd
  140. {
  141. SSL_CTX *ssl_context;
  142. };
  143. int sslaccept(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr);
  144. int sslconnect(struct st_VioSSLFd*, Vio *, long timeout, unsigned long *errptr);
  145. struct st_VioSSLFd
  146. *new_VioSSLConnectorFd(const char *key_file, const char *cert_file,
  147. const char *ca_file, const char *ca_path,
  148. const char *cipher, enum enum_ssl_init_error *error,
  149. const char *crl_file, const char *crl_path);
  150. struct st_VioSSLFd
  151. *new_VioSSLAcceptorFd(const char *key_file, const char *cert_file,
  152. const char *ca_file,const char *ca_path,
  153. const char *cipher, enum enum_ssl_init_error *error,
  154. const char *crl_file, const char *crl_path);
  155. void free_vio_ssl_acceptor_fd(struct st_VioSSLFd *fd);
  156. #endif /* HAVE_OPENSSL */
  157. void vio_end(void);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  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_timeout(vio) (vio)->was_timeout(vio)
  173. #define vio_close(vio) ((vio)->vioclose)(vio)
  174. #define vio_shutdown(vio,how) ((vio)->shutdown)(vio,how)
  175. #define vio_peer_addr(vio, buf, prt, buflen) (vio)->peer_addr(vio, buf, prt, buflen)
  176. #define vio_io_wait(vio, event, timeout) (vio)->io_wait(vio, event, timeout)
  177. #define vio_is_connected(vio) (vio)->is_connected(vio)
  178. #endif /* !defined(DONT_MAP_VIO) */
  179. #ifdef _WIN32
  180. /* shutdown(2) flags */
  181. #ifndef SHUT_RD
  182. #define SHUT_RD SD_RECEIVE
  183. #endif
  184. /*
  185. Set thread id for io cancellation (required on Windows XP only,
  186. and should to be removed if XP is no more supported)
  187. */
  188. #define vio_set_thread_id(vio, tid) if(vio) vio->thread_id= tid
  189. #else
  190. #define vio_set_thread_id(vio, tid)
  191. #endif
  192. /* This enumerator is used in parser - should be always visible */
  193. enum SSL_type
  194. {
  195. SSL_TYPE_NOT_SPECIFIED= -1,
  196. SSL_TYPE_NONE,
  197. SSL_TYPE_ANY,
  198. SSL_TYPE_X509,
  199. SSL_TYPE_SPECIFIED
  200. };
  201. /* HFTODO - hide this if we don't want client in embedded server */
  202. /* This structure is for every connection on both sides */
  203. struct st_vio
  204. {
  205. MYSQL_SOCKET mysql_socket; /* Instrumented socket */
  206. my_bool localhost; /* Are we from localhost? */
  207. int fcntl_mode; /* Buffered fcntl(sd,F_GETFL) */
  208. struct sockaddr_storage local; /* Local internet address */
  209. struct sockaddr_storage remote; /* Remote internet address */
  210. int addrLen; /* Length of remote address */
  211. enum enum_vio_type type; /* Type of connection */
  212. const char *desc; /* String description */
  213. char *read_buffer; /* buffer for vio_read_buff */
  214. char *read_pos; /* start of unfetched data in the
  215. read buffer */
  216. char *read_end; /* end of unfetched data */
  217. struct mysql_async_context *async_context; /* For non-blocking API */
  218. int read_timeout; /* Timeout value (ms) for read ops. */
  219. int write_timeout; /* Timeout value (ms) for write ops. */
  220. /* function pointers. They are similar for socket/SSL/whatever */
  221. void (*viodelete)(Vio*);
  222. int (*vioerrno)(Vio*);
  223. size_t (*read)(Vio*, uchar *, size_t);
  224. size_t (*write)(Vio*, const uchar *, size_t);
  225. int (*timeout)(Vio*, uint, my_bool);
  226. int (*vioblocking)(Vio*, my_bool, my_bool *);
  227. my_bool (*is_blocking)(Vio*);
  228. int (*viokeepalive)(Vio*, my_bool);
  229. int (*fastsend)(Vio*);
  230. my_bool (*peer_addr)(Vio*, char *, uint16*, size_t);
  231. void (*in_addr)(Vio*, struct sockaddr_storage*);
  232. my_bool (*should_retry)(Vio*);
  233. my_bool (*was_timeout)(Vio*);
  234. int (*vioclose)(Vio*);
  235. my_bool (*is_connected)(Vio*);
  236. int (*shutdown)(Vio *, int);
  237. my_bool (*has_data) (Vio*);
  238. int (*io_wait)(Vio*, enum enum_vio_io_event, int);
  239. my_bool (*connect)(Vio*, struct sockaddr *, socklen_t, int);
  240. #ifdef HAVE_OPENSSL
  241. void *ssl_arg;
  242. #endif
  243. #ifdef HAVE_SMEM
  244. HANDLE handle_file_map;
  245. char *handle_map;
  246. HANDLE event_server_wrote;
  247. HANDLE event_server_read;
  248. HANDLE event_client_wrote;
  249. HANDLE event_client_read;
  250. HANDLE event_conn_closed;
  251. size_t shared_memory_remain;
  252. char *shared_memory_pos;
  253. #endif /* HAVE_SMEM */
  254. #ifdef _WIN32
  255. HANDLE hPipe;
  256. OVERLAPPED overlapped;
  257. DWORD thread_id; /* Used on XP only by vio_shutdown() */
  258. DWORD read_timeout_ms;
  259. DWORD write_timeout_ms;
  260. #endif
  261. };
  262. #endif /* vio_violite_h_ */