forked from sogou/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLMessage.inl
More file actions
417 lines (340 loc) · 8.89 KB
/
Copy pathMySQLMessage.inl
File metadata and controls
417 lines (340 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Copyright (c) 2019 Sogou, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Authors: Xie Han ([email protected])
Wu Jiaxu ([email protected])
*/
#include <stdint.h>
#include <string.h>
#include <utility>
#include <string>
#include <openssl/ssl.h>
#include "SSLWrapper.h"
namespace protocol
{
class MySQLHandshakeRequest : public MySQLRequest
{
private:
virtual int encode(struct iovec vectors[], int max) { return 0; }
};
class MySQLHandshakeResponse : public MySQLResponse
{
public:
std::string get_server_version() const { return server_version_; }
std::string get_auth_plugin_name() const { return auth_plugin_name_; }
void get_seed(unsigned char seed[20]) const
{
memcpy(seed, auth_plugin_data_, 20);
}
virtual int encode(struct iovec vectors[], int max);
void server_set(uint8_t protocol_version, const std::string server_version,
uint32_t connection_id, const unsigned char seed[20],
uint32_t capability_flags, uint8_t character_set,
uint16_t status_flags)
{
protocol_version_ = protocol_version;
server_version_ = server_version;
connection_id_ = connection_id;
memcpy(auth_plugin_data_, seed, 20);
capability_flags_ = capability_flags;
character_set_ = character_set;
status_flags_ = status_flags;
}
bool host_disallowed() const { return disallowed_; }
uint32_t get_capability_flags() const { return capability_flags_; }
uint16_t get_status_flags() const { return status_flags_; }
private:
virtual int decode_packet(const unsigned char *buf, size_t buflen);
std::string server_version_;
std::string auth_plugin_name_;
unsigned char auth_plugin_data_[20];
uint32_t connection_id_;
uint32_t capability_flags_;
uint16_t status_flags_;
uint8_t character_set_;
uint8_t auth_plugin_data_len_;
uint8_t protocol_version_;
bool disallowed_;
public:
MySQLHandshakeResponse() : disallowed_(false) { }
//move constructor
MySQLHandshakeResponse(MySQLHandshakeResponse&& move) = default;
//move operator
MySQLHandshakeResponse& operator= (MySQLHandshakeResponse&& move) = default;
};
class MySQLSSLRequest : public MySQLRequest
{
private:
virtual int encode(struct iovec vectors[], int max);
/* Do not support server side with SSL currently. */
virtual int decode_packet(const unsigned char *buf, size_t buflen)
{
return -2;
}
private:
int character_set_;
SSLHandshaker ssl_handshaker_;
public:
MySQLSSLRequest(int character_set, SSL *ssl) : ssl_handshaker_(ssl)
{
character_set_ = character_set;
}
MySQLSSLRequest(MySQLSSLRequest&& move) = default;
MySQLSSLRequest& operator= (MySQLSSLRequest&& move) = default;
};
class MySQLAuthRequest : public MySQLRequest
{
public:
void set_auth(const std::string username,
const std::string password,
const std::string db,
int character_set)
{
username_ = std::move(username);
password_ = std::move(password);
db_ = std::move(db);
character_set_ = character_set;
}
void set_auth_plugin_name(std::string name)
{
auth_plugin_name_ = std::move(name);
}
void set_seed(const unsigned char seed[20])
{
memcpy(seed_, seed, 20);
}
private:
virtual int encode(struct iovec vectors[], int max);
virtual int decode_packet(const unsigned char *buf, size_t buflen);
std::string username_;
std::string password_;
std::string db_;
std::string auth_plugin_name_;
unsigned char seed_[20];
int character_set_;
public:
MySQLAuthRequest() : character_set_(33) { }
//move constructor
MySQLAuthRequest(MySQLAuthRequest&& move) = default;
//move operator
MySQLAuthRequest& operator= (MySQLAuthRequest&& move) = default;
};
class MySQLAuthResponse : public MySQLResponse
{
public:
std::string get_auth_plugin_name() const { return auth_plugin_name_; }
void get_seed(unsigned char seed[20]) const
{
memcpy(seed, seed_, 20);
}
bool is_continue() const
{
return continue_;
}
private:
virtual int decode_packet(const unsigned char *buf, size_t buflen);
private:
std::string auth_plugin_name_;
unsigned char seed_[20];
bool continue_;
public:
MySQLAuthResponse() : continue_(false) { }
//move constructor
MySQLAuthResponse(MySQLAuthResponse&& move) = default;
//move operator
MySQLAuthResponse& operator= (MySQLAuthResponse&& move) = default;
};
class MySQLAuthSwitchRequest : public MySQLRequest
{
public:
void set_password(std::string password)
{
password_ = std::move(password);
}
void set_auth_plugin_name(std::string name)
{
auth_plugin_name_ = std::move(name);
}
void set_seed(const unsigned char seed[20])
{
memcpy(seed_, seed, 20);
}
private:
virtual int encode(struct iovec vectors[], int max);
/* Not implemented. */
virtual int decode_packet(const unsigned char *buf, size_t buflen)
{
return -2;
}
std::string password_;
std::string auth_plugin_name_;
unsigned char seed_[20];
public:
MySQLAuthSwitchRequest() { }
//move constructor
MySQLAuthSwitchRequest(MySQLAuthSwitchRequest&& move) = default;
//move operator
MySQLAuthSwitchRequest& operator= (MySQLAuthSwitchRequest&& move) = default;
};
class MySQLPublicKeyRequest : public MySQLRequest
{
public:
void set_caching_sha2() { byte_ = 0x02; }
void set_sha256() { byte_ = 0x01; }
private:
virtual int encode(struct iovec vectors[], int max)
{
buf_.assign(&byte_, 1);
return MySQLRequest::encode(vectors, max);
}
/* Not implemented. */
virtual int decode_packet(const unsigned char *buf, size_t buflen)
{
return -2;
}
char byte_;
public:
MySQLPublicKeyRequest() : byte_(0x01) { }
//move constructor
MySQLPublicKeyRequest(MySQLPublicKeyRequest&& move) = default;
//move operator
MySQLPublicKeyRequest& operator= (MySQLPublicKeyRequest&& move) = default;
};
class MySQLPublicKeyResponse : public MySQLResponse
{
public:
std::string get_public_key() const
{
return public_key_;
}
void set_public_key(std::string key)
{
public_key_ = std::move(key);
}
private:
virtual int encode(struct iovec vectors[], int max);
virtual int decode_packet(const unsigned char *buf, size_t buflen);
std::string public_key_;
public:
MySQLPublicKeyResponse() { }
//move constructor
MySQLPublicKeyResponse(MySQLPublicKeyResponse&& move) = default;
//move operator
MySQLPublicKeyResponse& operator= (MySQLPublicKeyResponse&& move) = default;
};
class MySQLRSAAuthRequest : public MySQLRequest
{
public:
void set_password(std::string password)
{
password_ = std::move(password);
}
void set_public_key(std::string key)
{
public_key_ = std::move(key);
}
void set_seed(const unsigned char seed[20])
{
memcpy(seed_, seed, 20);
}
private:
virtual int encode(struct iovec vectors[], int max);
/* Not implemented. */
virtual int decode_packet(const unsigned char *buf, size_t buflen)
{
return -2;
}
int rsa_encrypt(void *ctx);
std::string password_;
std::string public_key_;
unsigned char seed_[20];
public:
MySQLRSAAuthRequest() { }
//move constructor
MySQLRSAAuthRequest(MySQLRSAAuthRequest&& move) = default;
//move operator
MySQLRSAAuthRequest& operator= (MySQLRSAAuthRequest&& move) = default;
};
//////////
inline mysql_parser_t *MySQLMessage::get_parser() const
{
return parser_;
}
inline int MySQLMessage::get_seqid() const
{
return seqid_;
}
inline void MySQLMessage::set_seqid(int seqid)
{
seqid_ = seqid;
}
inline int MySQLMessage::get_command() const
{
return parser_->cmd;
}
inline void MySQLMessage::set_command(int cmd) const
{
mysql_parser_set_command(cmd, parser_);
}
inline MySQLMessage::MySQLMessage():
stream_(new mysql_stream_t),
parser_(new mysql_parser_t),
seqid_(0),
cur_size_(0)
{
mysql_stream_init(stream_);
mysql_parser_init(parser_);
}
inline bool MySQLRequest::query_is_unset() const
{
return buf_.empty();
}
inline void MySQLRequest::set_query(const char *query)
{
set_query(query, strlen(query));
}
inline void MySQLRequest::set_query(const std::string& query)
{
set_query(query.c_str(), query.size());
}
inline int MySQLResponse::get_packet_type() const
{
return parser_->packet_type;
}
inline int MySQLResponse::get_error_code() const
{
return is_error_packet() ? parser_->error : 0;
}
inline std::string MySQLResponse::get_error_msg() const
{
if (is_error_packet())
{
const char *s;
size_t slen;
mysql_parser_get_err_msg(&s, &slen, parser_);
if (slen > 0)
return std::string(s, slen);
}
return std::string();
}
inline std::string MySQLResponse::get_sql_state() const
{
if (is_error_packet())
{
const char *s;
size_t slen;
mysql_parser_get_net_state(&s, &slen, parser_);
if (slen > 0)
return std::string(s, slen);
}
return std::string();
}
}