trilogy 2.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of trilogy might be problematic. Click here for more details.

Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +74 -0
  4. data/Rakefile +18 -0
  5. data/ext/trilogy-ruby/cast.c +272 -0
  6. data/ext/trilogy-ruby/cext.c +933 -0
  7. data/ext/trilogy-ruby/extconf.rb +16 -0
  8. data/ext/trilogy-ruby/inc/trilogy/blocking.h +163 -0
  9. data/ext/trilogy-ruby/inc/trilogy/buffer.h +64 -0
  10. data/ext/trilogy-ruby/inc/trilogy/builder.h +161 -0
  11. data/ext/trilogy-ruby/inc/trilogy/charset.h +277 -0
  12. data/ext/trilogy-ruby/inc/trilogy/client.h +546 -0
  13. data/ext/trilogy-ruby/inc/trilogy/error.h +43 -0
  14. data/ext/trilogy-ruby/inc/trilogy/packet_parser.h +34 -0
  15. data/ext/trilogy-ruby/inc/trilogy/protocol.h +756 -0
  16. data/ext/trilogy-ruby/inc/trilogy/reader.h +212 -0
  17. data/ext/trilogy-ruby/inc/trilogy/socket.h +111 -0
  18. data/ext/trilogy-ruby/inc/trilogy/vendor/curl_hostcheck.h +29 -0
  19. data/ext/trilogy-ruby/inc/trilogy/vendor/openssl_hostname_validation.h +51 -0
  20. data/ext/trilogy-ruby/inc/trilogy.h +8 -0
  21. data/ext/trilogy-ruby/src/blocking.c +241 -0
  22. data/ext/trilogy-ruby/src/buffer.c +60 -0
  23. data/ext/trilogy-ruby/src/builder.c +198 -0
  24. data/ext/trilogy-ruby/src/charset.c +212 -0
  25. data/ext/trilogy-ruby/src/client.c +728 -0
  26. data/ext/trilogy-ruby/src/error.c +17 -0
  27. data/ext/trilogy-ruby/src/packet_parser.c +140 -0
  28. data/ext/trilogy-ruby/src/protocol.c +676 -0
  29. data/ext/trilogy-ruby/src/reader.c +244 -0
  30. data/ext/trilogy-ruby/src/socket.c +623 -0
  31. data/ext/trilogy-ruby/src/vendor/curl_hostcheck.c +206 -0
  32. data/ext/trilogy-ruby/src/vendor/openssl_hostname_validation.c +175 -0
  33. data/ext/trilogy-ruby/trilogy-ruby.h +36 -0
  34. data/lib/trilogy/version.rb +3 -0
  35. data/lib/trilogy.rb +61 -0
  36. data/trilogy.gemspec +27 -0
  37. metadata +106 -0
@@ -0,0 +1,756 @@
1
+ #ifndef TRILOGY_PROTOCOL_H
2
+ #define TRILOGY_PROTOCOL_H
3
+
4
+ #include "trilogy/builder.h"
5
+ #include "trilogy/charset.h"
6
+
7
+ #include <stdbool.h>
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+
12
+ #define TRILOGY_CAPABILITIES(XX) \
13
+ XX(TRILOGY_CAPABILITIES_NONE, 0) \
14
+ /* Not used. This flag is assumed by current servers. \
15
+ * \
16
+ * From client: tells the server the client intends to use the newer \
17
+ * password hashing algorithm. \
18
+ */ \
19
+ XX(TRILOGY_CAPABILITIES_LONG_PASSWORD, 0x00000001) \
20
+ /* From client: tells the server to set the affected_rows field to the \
21
+ * number of rows found by the query instead of the actual number of rows \
22
+ * updated. \
23
+ * \
24
+ * For example, the following update statement would set affected_rows to \
25
+ * 1: `UPDATE users SET login = "brianmario1" WHERE login = "brianmario";` \
26
+ * \
27
+ * But an update statement which didn't actually perform any updates like: \
28
+ * `UPDATE users SET login = "brianmario" WHERE login = "brianmario";` \
29
+ * would have normally set affected_rows to 0. That's where this flag \
30
+ * comes in to play. By setting this flag during the authentication phase \
31
+ * of a connection - the above query will set affected_rows to 1. \
32
+ */ \
33
+ XX(TRILOGY_CAPABILITIES_FOUND_ROWS, 0x00000002) \
34
+ /* Not used. This flag was only used by the older (pre-4.1) protocol. \
35
+ * \
36
+ * From server: the server supports a longer flags field in column \
37
+ * definition packets. \
38
+ * \
39
+ * From client: the client supports longer flags field in column \
40
+ * definition packets. \
41
+ */ \
42
+ XX(TRILOGY_CAPABILITIES_LONG_FLAG, 0x00000004) \
43
+ /* From server: the server supports a database name being passed in the \
44
+ * handshake response packet. \
45
+ * \
46
+ * From client: tells the server there is a database name in the handshake \
47
+ * response packet. \
48
+ */ \
49
+ XX(TRILOGY_CAPABILITIES_CONNECT_WITH_DB, 0x00000008) \
50
+ /* From client: tells the server to not allow `database.table.column` \
51
+ * notation in query syntax. \
52
+ */ \
53
+ XX(TRILOGY_CAPABILITIES_NO_SCHEMA, 0x00000010) \
54
+ /* Not implemented. \
55
+ * \
56
+ * From server: the server supports compression. \
57
+ * \
58
+ * From client: tells the server to enable the compression protocol. \
59
+ */ \
60
+ XX(TRILOGY_CAPABILITIES_COMPRESS, 0x00000020) \
61
+ /* Not used since 3.22. \
62
+ */ \
63
+ XX(TRILOGY_CAPABILITIES_ODBC, 0x00000040) \
64
+ /* Not implemented. \
65
+ * \
66
+ * From server: server support `LOAD DATA INFILE` and `LOAD XML`. \
67
+ * \
68
+ * From client: tells the server that the client supports `LOAD DATA LOCAL \
69
+ * INFILE`. \
70
+ */ \
71
+ XX(TRILOGY_CAPABILITIES_LOCAL_FILES, 0x00000080) \
72
+ /* From server: the query parser can ignore spaces before the '(' \
73
+ * character. \
74
+ * \
75
+ * From client: tells the server to ignore spaces before the '(' \
76
+ * character. \
77
+ */ \
78
+ XX(TRILOGY_CAPABILITIES_IGNORE_SPACE, 0x00000100) \
79
+ /* From server: the server supports the 4.1+ protocol. \
80
+ * \
81
+ * From client: the client is using the 4.1+ protocol. This will always be \
82
+ * set, as trilogy only supports the 4.1+ protocol. \
83
+ */ \
84
+ XX(TRILOGY_CAPABILITIES_PROTOCOL_41, 0x00000200) \
85
+ /* Not used. \
86
+ * \
87
+ * From server: the server supports interactive and non-interactive \
88
+ * clients. \
89
+ * \
90
+ * From client: the client is interactive. \
91
+ */ \
92
+ XX(TRILOGY_CAPABILITIES_INTERACTIVE, 0x00000400) \
93
+ /* Not implemented. \
94
+ * \
95
+ * From server: the server supports ssl. \
96
+ * \
97
+ * From client: tells the server it should switch to an ssl connection. \
98
+ */ \
99
+ XX(TRILOGY_CAPABILITIES_SSL, 0x00000800) \
100
+ /* Not used. \
101
+ * \
102
+ * This is assumed for the 4.1+ protocol. \
103
+ */ \
104
+ XX(TRILOGY_CAPABILITIES_TRANSACTIONS, 0x00002000) \
105
+ /* Not used. \
106
+ */ \
107
+ XX(TRILOGY_CAPABILITIES_RESERVED, 0x00004000) \
108
+ /* From server: server supports the 4.1+ protocol's native authentication \
109
+ * scheme. \
110
+ * \
111
+ * From client: client supports the 4.1+ protocol's native authentication \
112
+ * scheme. This will always be set. \
113
+ */ \
114
+ XX(TRILOGY_CAPABILITIES_SECURE_CONNECTION, 0x00008000) \
115
+ /* Not implemented. \
116
+ * \
117
+ * From server: the server can handle multiple statements per \
118
+ * query/prepared statement. \
119
+ * \
120
+ * From client: tells the server it may send multiple statements per \
121
+ * query/ prepared statement. \
122
+ */ \
123
+ XX(TRILOGY_CAPABILITIES_MULTI_STATEMENTS, 0x00010000) \
124
+ /* Not implemented. \
125
+ * \
126
+ * From server: the server is capable of sending multiple result sets from \
127
+ * a query. \
128
+ * \
129
+ * From client: tells the server it's capable of handling multiple result \
130
+ * sets from a query. \
131
+ */ \
132
+ XX(TRILOGY_CAPABILITIES_MULTI_RESULTS, 0x00020000) \
133
+ /* Not implemented. \
134
+ * \
135
+ * From server: the server is capable of sending multiple result sets from \
136
+ * a prepared statement. \
137
+ * \
138
+ * From client: tells the server it's capable of handling multiple result \
139
+ * sets from a prepared statement. \
140
+ */ \
141
+ XX(TRILOGY_CAPABILITIES_PS_MULTI_RESULTS, 0x00040000) \
142
+ /* Not implemented. \
143
+ * \
144
+ * From server: the server supports the pluggable authentication protocol. \
145
+ * \
146
+ * From client: tells the server that the client supports the pluggable \
147
+ * authentication protocol. \
148
+ */ \
149
+ XX(TRILOGY_CAPABILITIES_PLUGIN_AUTH, 0x00080000) \
150
+ /* Not implemented. \
151
+ * \
152
+ * From server: the server allows connection attributes to be set in the \
153
+ * handshake response packet during authentication. \
154
+ * \
155
+ * From client: tells the server that there are connection attributes in \
156
+ * the handshake response. \
157
+ */ \
158
+ XX(TRILOGY_CAPABILITIES_CONNECT_ATTRS, 0x00100000) \
159
+ /* Not implemented. \
160
+ * \
161
+ * From server: the server is capable of parsing length-encoded data in \
162
+ * the handshake response during authentication. \
163
+ * \
164
+ * From client: tells the server that the authentication data in the \
165
+ * handshake response is length-encoded. \
166
+ */ \
167
+ XX(TRILOGY_CAPABILITIES_PLUGIN_AUTH_LENENC_CLIENT_DATA, 0x00200000) \
168
+ /* From server: the server supports the expired password extension. \
169
+ * \
170
+ * From client: the client supports expired passwords. \
171
+ */ \
172
+ XX(TRILOGY_CAPABILITIES_CAN_HANDLE_EXPIRED_PASSWORDS, 0x00400000) \
173
+ /* From server: the server may set the \
174
+ * TRILOGY_SERVER_STATUS_SESSION_STATE_CHANGED flag in OK packets. Which \
175
+ * will also mean the OK packet includes session-state change data. \
176
+ * \
177
+ * From client: tells the server that the client expects the server to \
178
+ * send session-state change data in OK packets. \
179
+ */ \
180
+ XX(TRILOGY_CAPABILITIES_SESSION_TRACK, 0x00800000) \
181
+ /* From server: the server will send OK packets in place of EOF packets. \
182
+ * \
183
+ * From client: tells the server that it expects to be sent OK packets in \
184
+ * place of EOF packets. \
185
+ */ \
186
+ XX(TRILOGY_CAPABILITIES_DEPRECATE_EOF, 0x01000000)
187
+
188
+ typedef enum {
189
+ #define XX(name, code) name = code,
190
+ TRILOGY_CAPABILITIES(XX)
191
+ #undef XX
192
+
193
+ /* A convenience bitmask with common client capabilities set. */
194
+ TRILOGY_CAPABILITIES_CLIENT = (TRILOGY_CAPABILITIES_PROTOCOL_41 | TRILOGY_CAPABILITIES_SECURE_CONNECTION |
195
+ TRILOGY_CAPABILITIES_DEPRECATE_EOF | TRILOGY_CAPABILITIES_SESSION_TRACK |
196
+ TRILOGY_CAPABILITIES_PLUGIN_AUTH | TRILOGY_CAPABILITIES_TRANSACTIONS)
197
+ } TRILOGY_CAPABILITIES_t;
198
+
199
+ #define TRILOGY_SERVER_STATUS(XX) \
200
+ /* The connection session is in a transaction. \
201
+ */ \
202
+ XX(TRILOGY_SERVER_STATUS_IN_TRANS, 0x0001) \
203
+ /* The connection session has `autocommit` enabled. `autocommit` mode \
204
+ * makes the database write any updates to tables immediately. This mode is \
205
+ * enabled by default in. This mode is implicitly disabled for all \
206
+ * statements between a `START TRANSACTION` and corresponding `COMMIT`. It \
207
+ * can be disabled with the statement: `SET autocommit=0`. \
208
+ */ \
209
+ XX(TRILOGY_SERVER_STATUS_AUTOCOMMIT, 0x0002) \
210
+ \
211
+ /* This flag means there are more results available to be read from the \
212
+ * result set. It will be set on the last (EOF/OK) packet from a result \
213
+ * set. \
214
+ */ \
215
+ XX(TRILOGY_SERVER_STATUS_MORE_RESULTS_EXISTS, 0x0008) \
216
+ \
217
+ /* No good index was used to perform the query. \
218
+ */ \
219
+ XX(TRILOGY_SERVER_STATUS_NO_GOOD_INDEX_USED, 0x0010) \
220
+ \
221
+ /* No index was used to perform the query. \
222
+ */ \
223
+ XX(TRILOGY_SERVER_STATUS_NO_INDEX_USED, 0x0020) \
224
+ \
225
+ /* When using the prepared statement protocol, this will be set when a \
226
+ * read- only, non-scrollable cursor was opened from an `execute` command. \
227
+ * It will also be set for replies to `fetch` commands. \
228
+ */ \
229
+ XX(TRILOGY_SERVER_STATUS_CURSOR_EXISTS, 0x0040) \
230
+ \
231
+ /* When using the prepared statement protocol, this will be set when a \
232
+ * read- only cursor has been exhausted. This will be set for replies to \
233
+ * `fetch` commands. \
234
+ */ \
235
+ XX(TRILOGY_SERVER_STATUS_LAST_ROW_SENT, 0x0080) \
236
+ \
237
+ /* This will be set if a database was dropped. \
238
+ */ \
239
+ XX(TRILOGY_SERVER_STATUS_DB_DROPPED, 0x0100) \
240
+ \
241
+ /* This will be set if the `NO_BACKSLASH_ESCAPES` sql mode is enabled. The \
242
+ * caller can enable this mode by using the statement: \
243
+ * `SET SQL_MODE=NO_BACKSLASH_ESCAPES`. \
244
+ * \
245
+ * The `NO_BACKSLASH_ESCAPES` sql mode disables the use of the backslash \
246
+ * ('\') character as an escape character. \
247
+ */ \
248
+ XX(TRILOGY_SERVER_STATUS_NO_BACKSLASH_ESCAPES, 0x0200) \
249
+ \
250
+ /* This will be set if a re-prepare of a prepared statement meant that a \
251
+ * different number of columns would be returned as part of the result \
252
+ * set. \
253
+ */ \
254
+ XX(TRILOGY_SERVER_STATUS_METADATA_CHANGED, 0x0400) \
255
+ \
256
+ /* This will be set if the last query that was executed took longer than \
257
+ * the `long_query_time` system variable. \
258
+ */ \
259
+ XX(TRILOGY_SERVER_STATUS_QUERY_WAS_SLOW, 0x0800) \
260
+ \
261
+ /* The prepared statement result set contains out parameters. \
262
+ */ \
263
+ XX(TRILOGY_SERVER_STATUS_PS_OUT_PARAMS, 0x1000) \
264
+ \
265
+ /* Set if the current multi-statement transaction is a read-only \
266
+ * transaction. If this is set, `TRILOGY_SERVER_STATUS_IN_TRANS` will be set \
267
+ * as well. \
268
+ */ \
269
+ XX(TRILOGY_SERVER_STATUS_STATUS_IN_TRANS_READONLY, 0x2000) \
270
+ \
271
+ /* If set, the OK packet contains includes session-state change data. \
272
+ */ \
273
+ XX(TRILOGY_SERVER_STATUS_SESSION_STATE_CHANGED, 0x4000)
274
+
275
+ typedef enum {
276
+ #define XX(name, code) name = code,
277
+ TRILOGY_SERVER_STATUS(XX)
278
+ #undef XX
279
+ } TRILOGY_SERVER_STATUS_t;
280
+
281
+ #define TRILOGY_TYPES(XX) \
282
+ XX(TRILOGY_TYPE_DECIMAL, 0x00) \
283
+ XX(TRILOGY_TYPE_TINY, 0x01) \
284
+ XX(TRILOGY_TYPE_SHORT, 0x02) \
285
+ XX(TRILOGY_TYPE_LONG, 0x03) \
286
+ XX(TRILOGY_TYPE_FLOAT, 0x04) \
287
+ XX(TRILOGY_TYPE_DOUBLE, 0x05) \
288
+ XX(TRILOGY_TYPE_NULL, 0x06) \
289
+ XX(TRILOGY_TYPE_TIMESTAMP, 0x07) \
290
+ XX(TRILOGY_TYPE_LONGLONG, 0x08) \
291
+ XX(TRILOGY_TYPE_INT24, 0x09) \
292
+ XX(TRILOGY_TYPE_DATE, 0x0a) \
293
+ XX(TRILOGY_TYPE_TIME, 0x0b) \
294
+ XX(TRILOGY_TYPE_DATETIME, 0x0c) \
295
+ XX(TRILOGY_TYPE_YEAR, 0x0d) \
296
+ XX(TRILOGY_TYPE_VARCHAR, 0x0f) \
297
+ XX(TRILOGY_TYPE_BIT, 0x10) \
298
+ XX(TRILOGY_TYPE_JSON, 0xf5) \
299
+ XX(TRILOGY_TYPE_NEWDECIMAL, 0xf6) \
300
+ XX(TRILOGY_TYPE_ENUM, 0xf7) \
301
+ XX(TRILOGY_TYPE_SET, 0xf8) \
302
+ XX(TRILOGY_TYPE_TINY_BLOB, 0xf9) \
303
+ XX(TRILOGY_TYPE_MEDIUM_BLOB, 0xfa) \
304
+ XX(TRILOGY_TYPE_LONG_BLOB, 0xfb) \
305
+ XX(TRILOGY_TYPE_BLOB, 0xfc) \
306
+ XX(TRILOGY_TYPE_VAR_STRING, 0xfd) \
307
+ XX(TRILOGY_TYPE_STRING, 0xfe) \
308
+ XX(TRILOGY_TYPE_GEOMETRY, 0xff)
309
+
310
+ typedef enum {
311
+ #define XX(name, code) name = code,
312
+ TRILOGY_TYPES(XX)
313
+ #undef XX
314
+ } TRILOGY_TYPE_t;
315
+
316
+ #define TRILOGY_COLUMN_FLAGS(XX) \
317
+ XX(TRILOGY_COLUMN_FLAG_NONE, 0x0) \
318
+ /* The column has the `NOT NULL` flag set. Requiring all values to not be \
319
+ * NULL. \
320
+ */ \
321
+ XX(TRILOGY_COLUMN_FLAG_NOT_NULL, 0x1) \
322
+ /* The column is part of a primary key. \
323
+ */ \
324
+ XX(TRILOGY_COLUMN_FLAG_PRI_KEY, 0x2) \
325
+ /* The column has the `UNIQUE` flag set. Requring all values to be unique. \
326
+ */ \
327
+ XX(TRILOGY_COLUMN_FLAG_UNIQUE_KEY, 0x4) \
328
+ /* The column is part of a key. \
329
+ */ \
330
+ XX(TRILOGY_COLUMN_FLAG_MULTIPLE_KEY, 0x8) \
331
+ /* The column is a blob. \
332
+ */ \
333
+ XX(TRILOGY_COLUMN_FLAG_BLOB, 0x10) \
334
+ /* The column is a numeric type and has the `UNSIGNED` flag set. \
335
+ */ \
336
+ XX(TRILOGY_COLUMN_FLAG_UNSIGNED, 0x20) \
337
+ /* The column is flagged as zero fill. \
338
+ */ \
339
+ XX(TRILOGY_COLUMN_FLAG_ZEROFILL, 0x40) \
340
+ /* This column is flagged as binary. This will be set for any of the \
341
+ * binary field types like BINARY, VARBINARY, TINYBLOB, BLOB, MEDIUMBLOB \
342
+ * and LONGBLOB. \
343
+ */ \
344
+ XX(TRILOGY_COLUMN_FLAG_BINARY, 0x80) \
345
+ /* The column is an `ENUM` \
346
+ */ \
347
+ XX(TRILOGY_COLUMN_FLAG_ENUM, 0x100) \
348
+ /* The column is configured to auto-increment. \
349
+ */ \
350
+ XX(TRILOGY_COLUMN_FLAG_AUTO_INCREMENT, 0x200) \
351
+ /* The column is a `TIMESTAMP`. \
352
+ */ \
353
+ XX(TRILOGY_COLUMN_FLAG_TIMESTAMP, 0x400) \
354
+ /* The column is a `SET`. \
355
+ */ \
356
+ XX(TRILOGY_COLUMN_FLAG_SET, 0x800) \
357
+ /* The column has no default value configured. \
358
+ */ \
359
+ XX(TRILOGY_COLUMN_FLAG_NO_DEFAULT_VALUE, 0x1000) \
360
+ /* The column is configured to set it's value to `NOW()` on row update. \
361
+ */ \
362
+ XX(TRILOGY_COLUMN_FLAG_ON_UPDATE_NOW, 0x2000) \
363
+ /* The column is used in a partition function. \
364
+ */ \
365
+ XX(TRILOGY_COLUMN_FLAG_IN_PART_FUNC, 0x80000)
366
+
367
+ typedef enum {
368
+ #define XX(name, code) name = code,
369
+ TRILOGY_COLUMN_FLAGS(XX)
370
+ #undef XX
371
+ } TRILOGY_COLUMN_FLAG_t;
372
+
373
+ /*
374
+ * Data between client and server is exchanged in packets of max 16MByte size.
375
+ */
376
+ #define TRILOGY_MAX_PROTO_PACKET_LEN 0xffffff
377
+
378
+ // Typical response packet types
379
+ typedef enum {
380
+ TRILOGY_PACKET_OK = 0x0,
381
+ TRILOGY_PACKET_EOF = 0xfe,
382
+ TRILOGY_PACKET_ERR = 0xff,
383
+ TRILOGY_PACKET_UNKNOWN
384
+ } TRILOGY_PACKET_TYPE_t;
385
+
386
+ /*
387
+ * source_uuid:transaction_id
388
+ * (UUID string) ":" (bigint string)
389
+ * 36 + 1 + 20 = 57
390
+ */
391
+ #define TRILOGY_MAX_LAST_GTID_LEN 57
392
+
393
+ #define TRILOGY_SESSION_TRACK(XX) \
394
+ XX(TRILOGY_SESSION_TRACK_SYSTEM_VARIABLES, 0x00) \
395
+ XX(TRILOGY_SESSION_TRACK_SCHEMA, 0x01) \
396
+ XX(TRILOGY_SESSION_TRACK_STATE_CHANGE, 0x02) \
397
+ XX(TRILOGY_SESSION_TRACK_GTIDS, 0x03) \
398
+ XX(TRILOGY_SESSION_TRACK_TRANSACTION_CHARACTERISTICS, 0x04) \
399
+ XX(TRILOGY_SESSION_TRACK_TRANSACTION_STATE, 0x05)
400
+
401
+ typedef enum {
402
+ #define XX(name, code) name = code,
403
+ TRILOGY_SESSION_TRACK(XX)
404
+ #undef XX
405
+ } TRILOGY_SESSION_TRACK_TYPE_t;
406
+
407
+ /* trilogy_build_auth_packet - Build a handshake response (or authentication)
408
+ * packet.
409
+ *
410
+ * This should be sent in response to the initial handshake packet the server
411
+ * sends upon connection.
412
+ *
413
+ * builder - A pointer to a pre-initialized trilogy_builder_t.
414
+ * user - The username to use for authentication. Must be a C-string.
415
+ * pass - The password to use for authentication. Optional, and can be NULL.
416
+ * pass_len - The length of password in bytes.
417
+ * auth_plugin - Plugin authentication mechanism that the server requested.
418
+ * scramble - The scramble value the server sent in the initial handshake.
419
+ * flags - Bitmask of TRILOGY_CAPABILITIES_t flags.
420
+ * The TRILOGY_CAPABILITIES_PROTOCOL_41 and
421
+ * TRILOGY_CAPABILITIES_SECURE_CONNECTION flags will always be set
422
+ * internally.
423
+ *
424
+ * Return values:
425
+ * TRILOGY_OK - The packet was successfully built and written to the
426
+ * builder's internal buffer.
427
+ * TRILOGY_SYSERR - A system error occurred, check errno.
428
+ */
429
+ int trilogy_build_auth_packet(trilogy_builder_t *builder, const char *user, const char *pass, size_t pass_len,
430
+ const char *database, const char *auth_plugin, const char *scramble,
431
+ TRILOGY_CAPABILITIES_t flags);
432
+
433
+ /* trilogy_build_auth_switch_response_packet - Build a response for when
434
+ * authentication switching it requested.
435
+ *
436
+ * This should be sent in response to the initial switch request packet the server
437
+ * sends upon connection.
438
+ *
439
+ * builder - A pointer to a pre-initialized trilogy_builder_t.
440
+ * pass - The password to use for authentication.
441
+ * pass_len - The length of password in bytes.
442
+ * auth_plugin - Plugin authentication mechanism that the server requested.
443
+ * scramble - The scramble value received from the server.
444
+ *
445
+ * Return values:
446
+ * TRILOGY_OK - The packet was successfully built and written to the
447
+ * builder's internal buffer.
448
+ * TRILOGY_SYSERR - A system error occurred, check errno.
449
+ */
450
+ int trilogy_build_auth_switch_response_packet(trilogy_builder_t *builder, const char *pass, size_t pass_len,
451
+ const char *auth_plugin, const char *scramble);
452
+
453
+ /* trilogy_build_change_db_packet - Build a change database command packet. This
454
+ * command will change the default database for the connection.
455
+ *
456
+ * builder - A pointer to a pre-initialized trilogy_builder_t.
457
+ * name - The name of the databaset to set as the default.
458
+ * name_len - The length of name in bytes.
459
+ *
460
+ * Return values:
461
+ * TRILOGY_OK - The packet was successfully built and written to the
462
+ * builder's internal buffer.
463
+ * TRILOGY_SYSERR - A system error occurred, check errno.
464
+ */
465
+ int trilogy_build_change_db_packet(trilogy_builder_t *builder, const char *name, size_t name_len);
466
+
467
+ /* trilogy_build_ping_packet - Build a ping command packet.
468
+ *
469
+ * builder - A pointer to a pre-initialized trilogy_builder_t.
470
+ *
471
+ * Return values:
472
+ * TRILOGY_OK - The packet was successfully built and written to the
473
+ * builder's internal buffer.
474
+ * TRILOGY_SYSERR - A system error occurred, check errno.
475
+ */
476
+ int trilogy_build_ping_packet(trilogy_builder_t *builder);
477
+
478
+ /* trilogy_build_query_packet - Build a query command packet.
479
+ *
480
+ * builder - A pointer to a pre-initialized trilogy_builder_t.
481
+ * query - The query string to be used by the command.
482
+ * query_len - The length of query in bytes.
483
+ * Return values:
484
+ * TRILOGY_OK - The packet was successfully built and written to the
485
+ * builder's internal buffer.
486
+ * TRILOGY_SYSERR - A system error occurred, check errno.
487
+ */
488
+ int trilogy_build_query_packet(trilogy_builder_t *builder, const char *sql, size_t sql_len);
489
+
490
+ /* trilogy_build_quit_packet - Build a quit command packet.
491
+ *
492
+ * builder - A pointer to a pre-initialized trilogy_builder_t.
493
+ *
494
+ * Return values:
495
+ * TRILOGY_OK - The packet was successfully built and written to the
496
+ * builder's internal buffer.
497
+ * TRILOGY_SYSERR - A system error occurred, check errno.
498
+ */
499
+ int trilogy_build_quit_packet(trilogy_builder_t *builder);
500
+
501
+ /* trilogy_build_ssl_request_packet - Build an SSL request packet.
502
+ *
503
+ * This should be sent in response to the initial handshake packet the server
504
+ * sends upon connection, where an auth packet would normally be sent. A regular
505
+ * auth packet is to be sent after the SSL handshake completes.
506
+ *
507
+ * builder - A pointer to a pre-initialized trilogy_builder_t.
508
+ * flags - Bitmask of TRILOGY_CAPABILITIES_t flags.
509
+ * The TRILOGY_CAPABILITIES_PROTOCOL_41 and
510
+ * TRILOGY_CAPABILITIES_SECURE_CONNECTION flags will always be set
511
+ * internally.
512
+ * The TRILOGY_CAPABILITIES_SSL flag will also be set.
513
+ *
514
+ * Return values:
515
+ * TRILOGY_OK - The packet was successfully built and written to the
516
+ * builder's internal buffer.
517
+ * TRILOGY_SYSERR - A system error occurred, check errno.
518
+ */
519
+ int trilogy_build_ssl_request_packet(trilogy_builder_t *builder, TRILOGY_CAPABILITIES_t flags);
520
+
521
+ #define TRILOGY_SERVER_VERSION_SIZE 32
522
+
523
+ typedef struct {
524
+ uint8_t proto_version;
525
+ char server_version[TRILOGY_SERVER_VERSION_SIZE];
526
+ uint32_t conn_id;
527
+ char scramble[21];
528
+ uint32_t capabilities;
529
+ TRILOGY_CHARSET_t server_charset;
530
+ uint16_t server_status;
531
+ char auth_plugin[32];
532
+ } trilogy_handshake_t;
533
+
534
+ typedef struct {
535
+ uint64_t affected_rows;
536
+ uint64_t last_insert_id;
537
+ uint16_t status_flags;
538
+ uint16_t warning_count;
539
+ uint16_t txn_status_flags;
540
+ const char *session_status;
541
+ size_t session_status_len;
542
+ const char *session_state_changes;
543
+ size_t session_state_changes_len;
544
+ const char *info;
545
+ size_t info_len;
546
+ const char *last_gtid;
547
+ size_t last_gtid_len;
548
+ } trilogy_ok_packet_t;
549
+
550
+ typedef struct {
551
+ uint16_t warning_count;
552
+ uint16_t status_flags;
553
+ } trilogy_eof_packet_t;
554
+
555
+ typedef struct {
556
+ uint16_t error_code;
557
+ uint8_t sql_state_marker[1];
558
+ uint8_t sql_state[5];
559
+ const char *error_message;
560
+ size_t error_message_len;
561
+ } trilogy_err_packet_t;
562
+
563
+ typedef struct {
564
+ char auth_plugin[32];
565
+ char scramble[21];
566
+ } trilogy_auth_switch_request_packet_t;
567
+
568
+ typedef struct {
569
+ const char *catalog;
570
+ size_t catalog_len;
571
+ const char *schema;
572
+ size_t schema_len;
573
+ const char *table;
574
+ size_t table_len;
575
+ const char *original_table;
576
+ size_t original_table_len;
577
+ const char *name;
578
+ size_t name_len;
579
+ const char *original_name;
580
+ size_t original_name_len;
581
+ TRILOGY_CHARSET_t charset;
582
+ uint32_t len;
583
+ TRILOGY_TYPE_t type;
584
+ uint16_t flags;
585
+ uint8_t decimals;
586
+ const char *default_value;
587
+ size_t default_value_len;
588
+ } trilogy_column_packet_t;
589
+
590
+ typedef struct {
591
+ uint64_t column_count;
592
+ } trilogy_result_packet_t;
593
+
594
+ typedef struct {
595
+ bool is_null;
596
+ const void *data;
597
+ size_t data_len;
598
+ } trilogy_value_t;
599
+
600
+ /* The following parsing functions assume the buffer and length passed in point
601
+ * to one full MySQL-compatible packet. If the buffer contains more than one packet or
602
+ * has any extra data at the end, these functions will return
603
+ * TRILOGY_EXTRA_DATA_IN_PACKET.
604
+ */
605
+
606
+ /* trilogy_parse_handshake_packet - Parse an initial handshake packet from a
607
+ * buffer.
608
+ *
609
+ * buff - A pointer to the buffer containing the initial handshake packet
610
+ * data.
611
+ * len - The length of buff in bytes.
612
+ * out_packet - Out parameter; A pointer to a pre-allocated trilogy_handshake_t.
613
+ *
614
+ * Return values:
615
+ * TRILOGY_OK - The packet was was parsed and the out
616
+ * parameter has been filled in.
617
+ * TRILOGY_TRUNCATED_PACKET - There isn't enough data in the buffer
618
+ * to parse the packet.
619
+ * TRILOGY_PROTOCOL_VIOLATION - The protocol version parsed wasn't what
620
+ * the Trilogy API supports (0xa); Or the
621
+ * packet is corrupt.
622
+ * TRILOGY_INVALID_CHARSET - The charset parsed isn't in the range
623
+ * supported by the Trilogy API.
624
+ * TRILOGY_EXTRA_DATA_IN_PACKET - There are unparsed bytes left in the
625
+ * buffer.
626
+ */
627
+ int trilogy_parse_handshake_packet(const uint8_t *buff, size_t len, trilogy_handshake_t *out_packet);
628
+
629
+ /* trilogy_parse_ok_packet - Parse an OK packet.
630
+ *
631
+ * buff - A pointer to the buffer containing the OK packet data.
632
+ * len - The length of buff in bytes.
633
+ * capabilities - A bitmask of TRILOGY_CAPABILITIES_t flags.
634
+ * out_packet - Out parameter; A pointer to a pre-allocated trilogy_ok_packet_t.
635
+ *
636
+ * Return values:
637
+ * TRILOGY_OK - The packet was was parsed and the out
638
+ * parameter has been filled in.
639
+ * TRILOGY_TRUNCATED_PACKET - There isn't enough data in the buffer
640
+ * to parse the packet.
641
+ * TRILOGY_EXTRA_DATA_IN_PACKET - There are unparsed bytes left in the
642
+ * buffer.
643
+ */
644
+ int trilogy_parse_ok_packet(const uint8_t *buff, size_t len, uint32_t capabilities, trilogy_ok_packet_t *out_packet);
645
+
646
+ /* trilogy_parse_eof_packet - Parse an EOF packet.
647
+ *
648
+ * buff - A pointer to the buffer containing the EOF packet data.
649
+ * len - The lenght of buff in bytes.
650
+ * capabilities - A bitmask of TRILOGY_CAPABILITIES_t flags.
651
+ * out_packet - Out parameter; A pointer to a pre-allocated
652
+ * trilogy_eof_packet_t.
653
+ *
654
+ * Return values:
655
+ * TRILOGY_OK - The packet was was parsed and the out
656
+ * parameter has been filled in.
657
+ * TRILOGY_TRUNCATED_PACKET - There isn't enough data in the buffer
658
+ * to parse the packet.
659
+ * TRILOGY_EXTRA_DATA_IN_PACKET - There are unparsed bytes left in the
660
+ * buffer.
661
+ */
662
+ int trilogy_parse_eof_packet(const uint8_t *buff, size_t len, uint32_t capabilities, trilogy_eof_packet_t *out_packet);
663
+
664
+ /* trilogy_parse_err_packet - Parse an ERR packet.
665
+ *
666
+ * buff - A pointer to the buffer containing the ERR packet data.
667
+ * len - The length of buffer in bytes.
668
+ * capabilities - A bitmask of TRILOGY_CAPABILITIES_t flags.
669
+ * out_packet - Out parameter; A pointer to a pre-allocated
670
+ * trilogy_err_packet_t.
671
+ *
672
+ * Return values:
673
+ * TRILOGY_OK - The packet was was parsed and the out
674
+ * parameter has been filled in.
675
+ * TRILOGY_TRUNCATED_PACKET - There isn't enough data in the buffer
676
+ * to parse the packet.
677
+ * TRILOGY_EXTRA_DATA_IN_PACKET - There are unparsed bytes left in the
678
+ * buffer.
679
+ */
680
+ int trilogy_parse_err_packet(const uint8_t *buff, size_t len, uint32_t capabilities, trilogy_err_packet_t *out_packet);
681
+
682
+ /* trilogy_parse_auth_switch_request_packet - Parse an AuthSwitchRequest packet.
683
+ *
684
+ * buff - A pointer to the buffer containing the AuthSwitchRequest packet data.
685
+ * len - The length of buffer in bytes.
686
+ * capabilities - A bitmask of TRILOGY_CAPABILITIES_t flags.
687
+ * out_packet - Out parameter; A pointer to a pre-allocated
688
+ * trilogy_auth_switch_request_t.
689
+ *
690
+ * Return values:
691
+ * TRILOGY_OK - The packet was was parsed and the out
692
+ * parameter has been filled in.
693
+ * TRILOGY_TRUNCATED_PACKET - There isn't enough data in the buffer
694
+ * to parse the packet.
695
+ * TRILOGY_EXTRA_DATA_IN_PACKET - There are unparsed bytes left in the
696
+ * buffer.
697
+ */
698
+ int trilogy_parse_auth_switch_request_packet(const uint8_t *buff, size_t len, uint32_t capabilities,
699
+ trilogy_auth_switch_request_packet_t *out_packet);
700
+
701
+ /* trilogy_parse_result_packet - Parse a result packet.
702
+ *
703
+ * buff - A pointer to the buffer containing the result packet data.
704
+ * len - The length of buffer in bytes.
705
+ * out_packet - Out parameter; A pointer to a pre-allocated
706
+ * trilogy_result_packet_t.
707
+ *
708
+ * Return values:
709
+ * TRILOGY_OK - The packet was was parsed and the out
710
+ * parameter has been filled in. TRILOGY_TRUNCATED_PACKET - There isn't enough
711
+ * data in the buffer to parse the packet. TRILOGY_EXTRA_DATA_IN_PACKET - There
712
+ * are unparsed bytes left in the buffer.
713
+ */
714
+ int trilogy_parse_result_packet(const uint8_t *buff, size_t len, trilogy_result_packet_t *out_packet);
715
+
716
+ /* trilogy_parse_column_packet - Parse a column info packet.
717
+ *
718
+ * buff - A pointer to the buffer containing the column packet data.
719
+ * len - The length of buffer in bytes.
720
+ * field_list - Boolean to tell the parser it should expect default value
721
+ * information at the end of the packet. This will be the case
722
+ * when parsing column info packets in response to a field list
723
+ * command.
724
+ * out_packet - Out parameter; A pointer to a pre-allocated
725
+ * trilogy_column_packet_t.
726
+ *
727
+ * Return values:
728
+ * TRILOGY_OK - The packet was was parsed and the out
729
+ * parameter has been filled in. TRILOGY_TRUNCATED_PACKET - There isn't enough
730
+ * data in the buffer to parse the packet. TRILOGY_EXTRA_DATA_IN_PACKET - There
731
+ * are unparsed bytes left in the buffer.
732
+ */
733
+ int trilogy_parse_column_packet(const uint8_t *buff, size_t len, bool field_list, trilogy_column_packet_t *out_packet);
734
+
735
+ /* trilogy_parse_row_packet - Parse a row packet.
736
+ *
737
+ * buff - A pointer to the buffer containing the result packet data.
738
+ * len - The length of buffer in bytes.
739
+ * column_count - The number of columns in the response. This parser needs this
740
+ * in order to know how many values to parse.
741
+ * out_packet - Out parameter; A pointer to a pre-allocated array of
742
+ * trilogy_value_t's. There must be enough space to fit all of the
743
+ * values. This can be computed with:
744
+ * `(sizeof(trilogy_value_t) * column_count)`.
745
+ *
746
+ * Return values:
747
+ * TRILOGY_OK - The packet was was parsed and the out
748
+ * parameter has been filled in.
749
+ * TRILOGY_TRUNCATED_PACKET - There isn't enough data in the buffer
750
+ * to parse the packet.
751
+ * TRILOGY_EXTRA_DATA_IN_PACKET - There are unparsed bytes left in the
752
+ * buffer.
753
+ */
754
+ int trilogy_parse_row_packet(const uint8_t *buff, size_t len, uint64_t column_count, trilogy_value_t *out_values);
755
+
756
+ #endif