1 /*
2  * Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2.0,
6  * as published by the Free Software Foundation.
7  *
8  * This program is also distributed with certain software (including
9  * but not limited to OpenSSL) that is licensed under separate terms,
10  * as designated in a particular file or component or in included license
11  * documentation.  The authors of MySQL hereby grant you an additional
12  * permission to link the program and your derivative works with the
13  * separately licensed software that they have included with MySQL.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License, version 2.0, for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301  USA
24  */
25 
26 // MySQL DB access module, for use by plugins and others
27 // For the module that implements interactive DB functionality see mod_db
28 
29 
30 #ifndef _MYSQLX_CONNECTION_H_
31 #define _MYSQLX_CONNECTION_H_
32 
33 #include "mysqlx_error.h"
34 
35 #include "violite.h"
36 
37 #ifdef WIN32
38 #define SHUT_RD   SD_RECEIVE
39 #define SHUT_WR   SD_SEND
40 #endif
41 
42 #define CR_UNKNOWN_ERROR        2000
43 #define CR_SOCKET_CREATE_ERROR  2001
44 #define CR_CONNECTION_ERROR     2002
45 #define CR_UNKNOWN_HOST         2005
46 #define CR_SERVER_GONE_ERROR    2006
47 #define CR_BROKEN_PIPE          2007
48 #define CR_WRONG_HOST_INFO      2009
49 #define CR_COMMANDS_OUT_OF_SYNC 2014
50 #define CR_NAMEDPIPE_CONNECTION 2015
51 #define CR_NAMEDPIPEWAIT_ERROR  2016
52 #define CR_NAMEDPIPEOPEN_ERROR  2017
53 #define CR_NAMEDPIPESETSTATE_ERROR 2018
54 #define CR_SSL_CONNECTION_ERROR 2026
55 #define CR_MALFORMED_PACKET     2027
56 #define CR_INVALID_AUTH_METHOD  2028
57 
58 struct sockaddr_un;
59 
60 namespace mysqlx
61 {
62 
63 enum Shutdown_type
64 {
65   Shutdown_send = SHUT_WR,
66   Shutdown_recv = SHUT_RD,
67   Shutdown_both = SHUT_RDWR
68 };
69 
70 class Connection
71 {
72 public:
73   Connection(const char *ssl_key = NULL,
74                          const char *ssl_ca = NULL, const char *ssl_ca_path = NULL,
75                          const char *ssl_cert = NULL, const char *ssl_cipher = NULL,
76                          const char *tls_version = NULL, const std::size_t timeout = 0l);
77 
78   ~Connection();
79 
80   Error connect_to_localhost(const std::string &named_pipe_or_unix_socket);
81   Error connect(sockaddr *sockaddr, const std::size_t addr_size);
82   Error connect(my_socket s, sockaddr *sockaddr, const std::size_t addr_size);
83 
84   Error activate_tls();
85   Error shutdown(Shutdown_type how_to_shutdown);
86 
87   Error write(const void *data, const std::size_t data_length);
88   Error read(void *data, const std::size_t data_length);
89   Error read_with_timeout(void *data, std::size_t &data_length, const int deadline_milliseconds);
90 
91   void close();
92 
93   bool supports_ssl();
94 
95 private:
96 
97   Error get_ssl_init_error(const int init_error_id);
98   Error get_ssl_error(const int error_id);
99   Error get_socket_error(const int error_id);
100   std::string get_socket_error_description(const int error_id);
101 
102   const std::size_t   m_timeout;
103   st_VioSSLFd        *m_vioSslFd;
104   Vio                *m_vio;
105   bool                m_ssl;
106   bool                m_ssl_active;
107   enum_ssl_init_error m_ssl_init_error;
108 
109 };
110 
111 
112 } // namespace mysqlx
113 
114 
115 #endif // _MYSQLX_CONNECTION_H_
116