-
Notifications
You must be signed in to change notification settings - Fork 72
/
client.c
1015 lines (777 loc) · 26 KB
/
client.c
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <fcntl.h>
#include <string.h>
#include "trilogy/client.h"
#include "trilogy/error.h"
#define CHECKED(expr) \
if ((rc = (expr)) < 0) { \
return rc; \
}
static inline TRILOGY_PACKET_TYPE_t current_packet_type(trilogy_conn_t *conn)
{
return (TRILOGY_PACKET_TYPE_t)conn->packet_buffer.buff[0];
}
static int on_packet_begin(void *opaque)
{
trilogy_buffer_t *buff = opaque;
buff->len = 0;
return 0;
}
static int on_packet_data(void *opaque, const uint8_t *data, size_t len)
{
trilogy_buffer_t *buff = opaque;
int rc = TRILOGY_OK;
rc = trilogy_buffer_expand(buff, len);
if (rc < 0)
return rc;
memcpy(buff->buff + buff->len, data, len);
buff->len += len;
return 0;
}
static int on_packet_end(void *opaque)
{
(void)opaque;
// pause packet parsing so we can return the packet we just read to the
// caller
return 1;
}
static trilogy_packet_parser_callbacks_t packet_parser_callbacks = {
.on_packet_begin = on_packet_begin,
.on_packet_data = on_packet_data,
.on_packet_end = on_packet_end,
};
static int begin_command_phase(trilogy_builder_t *builder, trilogy_conn_t *conn, uint8_t seq)
{
int rc = trilogy_builder_init(builder, &conn->packet_buffer, seq);
if (rc < 0) {
return rc;
}
if (conn->socket->opts.max_allowed_packet > 0) {
trilogy_builder_set_max_packet_length(builder, conn->socket->opts.max_allowed_packet);
}
conn->packet_parser.sequence_number = seq + 1;
return 0;
}
static int read_packet(trilogy_conn_t *conn)
{
if (conn->recv_buff_pos == conn->recv_buff_len) {
ssize_t nread = trilogy_sock_read(conn->socket, conn->recv_buff, sizeof(conn->recv_buff));
if (nread < 0) {
int rc = (int)nread;
return rc;
}
if (nread == 0) {
return TRILOGY_CLOSED_CONNECTION;
}
conn->recv_buff_len = (size_t)nread;
conn->recv_buff_pos = 0;
}
const uint8_t *ptr = conn->recv_buff + conn->recv_buff_pos;
size_t len = conn->recv_buff_len - conn->recv_buff_pos;
int rc;
conn->recv_buff_pos += trilogy_packet_parser_execute(&conn->packet_parser, ptr, len, &rc);
if (rc < 0) {
// an error occurred in one of the callbacks
return rc;
}
if (rc > 0) {
// on_packet_end paused the parser, meaning we read a complete packet
return TRILOGY_OK;
}
// we didn't read a complete packet yet, return TRILOGY_AGAIN so the caller
// can retry
return TRILOGY_AGAIN;
}
static int begin_write(trilogy_conn_t *conn)
{
conn->packet_buffer_written = 0;
// perform a single write(2), if this does not end up writing the entire
// packet buffer, then we'll end up returning TRILOGY_AGAIN here and it'll be
// up to the caller to pump trilogy_flush_writes() until it returns TRILOGY_OK
return trilogy_flush_writes(conn);
}
int trilogy_init(trilogy_conn_t *conn)
{
int rc;
conn->affected_rows = 0;
conn->last_insert_id = 0;
conn->warning_count = 0;
conn->last_gtid_len = 0;
memset(conn->last_gtid, 0, TRILOGY_MAX_LAST_GTID_LEN);
conn->error_code = 0;
conn->error_message = NULL;
conn->error_message_len = 0;
conn->capabilities = 0;
conn->server_status = 0;
conn->socket = NULL;
conn->recv_buff_pos = 0;
conn->recv_buff_len = 0;
trilogy_packet_parser_init(&conn->packet_parser, &packet_parser_callbacks);
conn->packet_parser.user_data = &conn->packet_buffer;
CHECKED(trilogy_buffer_init(&conn->packet_buffer, TRILOGY_DEFAULT_BUF_SIZE));
return TRILOGY_OK;
}
int trilogy_flush_writes(trilogy_conn_t *conn)
{
void *ptr = conn->packet_buffer.buff + conn->packet_buffer_written;
size_t len = conn->packet_buffer.len - conn->packet_buffer_written;
ssize_t bytes = trilogy_sock_write(conn->socket, ptr, len);
if (bytes < 0) {
int rc = (int)bytes;
return rc;
}
conn->packet_buffer_written += (size_t)bytes;
if (conn->packet_buffer_written < conn->packet_buffer.len) {
return TRILOGY_AGAIN;
}
return TRILOGY_OK;
}
static void set_error(trilogy_conn_t *conn, const trilogy_err_packet_t *packet)
{
conn->error_code = packet->error_code;
conn->error_message = packet->error_message;
conn->error_message_len = packet->error_message_len;
}
static int read_ok_packet(trilogy_conn_t *conn)
{
trilogy_ok_packet_t ok_packet;
int rc = trilogy_parse_ok_packet(conn->packet_buffer.buff, conn->packet_buffer.len, conn->capabilities, &ok_packet);
if (rc != TRILOGY_OK) {
return rc;
}
if (conn->capabilities & TRILOGY_CAPABILITIES_PROTOCOL_41) {
conn->warning_count = ok_packet.warning_count;
conn->server_status = ok_packet.status_flags;
}
conn->affected_rows = ok_packet.affected_rows;
conn->last_insert_id = ok_packet.last_insert_id;
if (ok_packet.last_gtid_len > 0 && ok_packet.last_gtid_len < TRILOGY_MAX_LAST_GTID_LEN) {
memcpy(conn->last_gtid, ok_packet.last_gtid, ok_packet.last_gtid_len);
conn->last_gtid_len = ok_packet.last_gtid_len;
}
return TRILOGY_OK;
}
static int read_err_packet(trilogy_conn_t *conn)
{
trilogy_err_packet_t err_packet;
int rc =
trilogy_parse_err_packet(conn->packet_buffer.buff, conn->packet_buffer.len, conn->capabilities, &err_packet);
if (rc != TRILOGY_OK) {
return rc;
}
set_error(conn, &err_packet);
return TRILOGY_ERR;
}
static int read_eof_packet(trilogy_conn_t *conn)
{
trilogy_eof_packet_t eof_packet;
int rc =
trilogy_parse_eof_packet(conn->packet_buffer.buff, conn->packet_buffer.len, conn->capabilities, &eof_packet);
if (rc != TRILOGY_OK) {
return rc;
}
if (conn->capabilities & TRILOGY_CAPABILITIES_PROTOCOL_41) {
conn->warning_count = eof_packet.warning_count;
conn->server_status = eof_packet.status_flags;
}
return TRILOGY_EOF;
}
static int read_auth_switch_packet(trilogy_conn_t *conn, trilogy_handshake_t *handshake)
{
trilogy_auth_switch_request_packet_t auth_switch_packet;
int rc = trilogy_parse_auth_switch_request_packet(conn->packet_buffer.buff, conn->packet_buffer.len,
conn->capabilities, &auth_switch_packet);
if (rc != TRILOGY_OK) {
return rc;
}
if (strcmp("mysql_native_password", auth_switch_packet.auth_plugin) &&
strcmp("caching_sha2_password", auth_switch_packet.auth_plugin) &&
strcmp("mysql_clear_password", auth_switch_packet.auth_plugin)) {
// Only support native password, caching sha2 and cleartext password here.
return TRILOGY_PROTOCOL_VIOLATION;
}
memcpy(handshake->auth_plugin, auth_switch_packet.auth_plugin, sizeof(auth_switch_packet.auth_plugin));
memcpy(handshake->scramble, auth_switch_packet.scramble, sizeof(auth_switch_packet.scramble));
return TRILOGY_AUTH_SWITCH;
}
static int handle_generic_response(trilogy_conn_t *conn) {
switch (current_packet_type(conn)) {
case TRILOGY_PACKET_OK:
return read_ok_packet(conn);
case TRILOGY_PACKET_ERR:
return read_err_packet(conn);
default:
return TRILOGY_UNEXPECTED_PACKET;
}
}
static int read_generic_response(trilogy_conn_t *conn)
{
int rc = read_packet(conn);
if (rc < 0) {
return rc;
}
return handle_generic_response(conn);
}
int trilogy_connect_send(trilogy_conn_t *conn, const trilogy_sockopt_t *opts)
{
trilogy_sock_t *sock = trilogy_sock_new(opts);
if (sock == NULL) {
return TRILOGY_ERR;
}
int rc = trilogy_sock_resolve(sock);
if (rc < 0) {
return rc;
}
return trilogy_connect_send_socket(conn, sock);
}
int trilogy_connect_send_socket(trilogy_conn_t *conn, trilogy_sock_t *sock)
{
int rc = trilogy_sock_connect(sock);
if (rc < 0)
return rc;
conn->socket = sock;
conn->packet_parser.sequence_number = 0;
return TRILOGY_OK;
}
int trilogy_connect_recv(trilogy_conn_t *conn, trilogy_handshake_t *handshake_out)
{
int rc = read_packet(conn);
if (rc < 0) {
return rc;
}
// In rare cases, the server will actually send an error packet as the
// initial packet instead of a handshake packet. For example, if there are
// too many connected clients already.
if (current_packet_type(conn) == TRILOGY_PACKET_ERR) {
return read_err_packet(conn);
}
rc = trilogy_parse_handshake_packet(conn->packet_buffer.buff, conn->packet_buffer.len, handshake_out);
if (rc < 0) {
return rc;
}
conn->capabilities = handshake_out->capabilities;
conn->server_status = handshake_out->server_status;
return TRILOGY_OK;
}
int trilogy_auth_send(trilogy_conn_t *conn, const trilogy_handshake_t *handshake)
{
trilogy_builder_t builder;
int rc = begin_command_phase(&builder, conn, conn->packet_parser.sequence_number);
if (rc < 0) {
return rc;
}
rc = trilogy_build_auth_packet(&builder, conn->socket->opts.username, conn->socket->opts.password,
conn->socket->opts.password_len, conn->socket->opts.database,
conn->socket->opts.encoding, handshake->auth_plugin, handshake->scramble,
conn->socket->opts.flags);
if (rc < 0) {
return rc;
}
return begin_write(conn);
}
int trilogy_ssl_request_send(trilogy_conn_t *conn)
{
trilogy_builder_t builder;
int rc = begin_command_phase(&builder, conn, conn->packet_parser.sequence_number);
if (rc < 0) {
return rc;
}
conn->socket->opts.flags |= TRILOGY_CAPABILITIES_SSL;
rc = trilogy_build_ssl_request_packet(&builder, conn->socket->opts.flags, conn->socket->opts.encoding);
if (rc < 0) {
return rc;
}
return begin_write(conn);
}
int trilogy_auth_switch_send(trilogy_conn_t *conn, const trilogy_handshake_t *handshake)
{
trilogy_builder_t builder;
int rc = begin_command_phase(&builder, conn, conn->packet_parser.sequence_number);
if (rc < 0) {
return rc;
}
rc = trilogy_build_auth_switch_response_packet(&builder, conn->socket->opts.password,
conn->socket->opts.password_len, handshake->auth_plugin,
handshake->scramble, conn->socket->opts.enable_cleartext_plugin);
if (rc < 0) {
return rc;
}
return begin_write(conn);
}
void trilogy_auth_clear_password(trilogy_conn_t *conn)
{
if (conn->socket->opts.password) {
memset(conn->socket->opts.password, 0, conn->socket->opts.password_len);
}
}
#define FAST_AUTH_OK 3
#define FAST_AUTH_FAIL 4
int trilogy_auth_recv(trilogy_conn_t *conn, trilogy_handshake_t *handshake)
{
int rc = read_packet(conn);
if (rc < 0) {
return rc;
}
switch (current_packet_type(conn)) {
case TRILOGY_PACKET_AUTH_MORE_DATA: {
bool use_ssl = (conn->socket->opts.flags & TRILOGY_CAPABILITIES_SSL) != 0;
bool has_unix_socket = (conn->socket->opts.path != NULL);
if (!use_ssl && !has_unix_socket) {
return TRILOGY_UNSUPPORTED;
}
uint8_t byte = conn->packet_buffer.buff[1];
switch (byte) {
case FAST_AUTH_OK:
break;
case FAST_AUTH_FAIL:
{
trilogy_builder_t builder;
int err = begin_command_phase(&builder, conn, conn->packet_parser.sequence_number);
if (err < 0) {
return err;
}
err = trilogy_build_auth_clear_password(&builder, conn->socket->opts.password, conn->socket->opts.password_len);
if (err < 0) {
return err;
}
int rc = begin_write(conn);
while (rc == TRILOGY_AGAIN) {
rc = trilogy_sock_wait_write(conn->socket);
if (rc != TRILOGY_OK) {
return rc;
}
rc = trilogy_flush_writes(conn);
}
if (rc != TRILOGY_OK) {
return rc;
}
break;
}
default:
return TRILOGY_UNEXPECTED_PACKET;
}
while (1) {
rc = read_packet(conn);
if (rc == TRILOGY_OK) {
break;
}
else if (rc == TRILOGY_AGAIN) {
rc = trilogy_sock_wait_read(conn->socket);
}
if (rc != TRILOGY_OK) {
return rc;
}
}
trilogy_auth_clear_password(conn);
return handle_generic_response(conn);
}
case TRILOGY_PACKET_EOF:
// EOF is returned here if an auth switch is requested.
// We still need the password for the switch, it will be cleared
// in a follow up call to this function after the switch.
return read_auth_switch_packet(conn, handshake);
case TRILOGY_PACKET_OK:
case TRILOGY_PACKET_ERR:
default:
trilogy_auth_clear_password(conn);
return handle_generic_response(conn);
}
return read_generic_response(conn);
}
int trilogy_change_db_send(trilogy_conn_t *conn, const char *name, size_t name_len)
{
trilogy_builder_t builder;
int err = begin_command_phase(&builder, conn, 0);
if (err < 0) {
return err;
}
err = trilogy_build_change_db_packet(&builder, name, name_len);
if (err < 0) {
return err;
}
return begin_write(conn);
}
int trilogy_change_db_recv(trilogy_conn_t *conn) { return read_generic_response(conn); }
int trilogy_set_option_send(trilogy_conn_t *conn, const uint16_t option)
{
trilogy_builder_t builder;
int err = begin_command_phase(&builder, conn, 0);
if (err < 0) {
return err;
}
err = trilogy_build_set_option_packet(&builder, option);
if (err < 0) {
return err;
}
return begin_write(conn);
}
int trilogy_set_option_recv(trilogy_conn_t *conn) {
int rc = read_packet(conn);
if (rc < 0) {
return rc;
}
switch (current_packet_type(conn)) {
case TRILOGY_PACKET_OK:
case TRILOGY_PACKET_EOF: // COM_SET_OPTION returns an EOF packet, but it should be treated as an OK packet.
return read_ok_packet(conn);
case TRILOGY_PACKET_ERR:
return read_err_packet(conn);
default:
return TRILOGY_UNEXPECTED_PACKET;
}
}
int trilogy_ping_send(trilogy_conn_t *conn)
{
trilogy_builder_t builder;
int err = begin_command_phase(&builder, conn, 0);
if (err < 0) {
return err;
}
err = trilogy_build_ping_packet(&builder);
if (err < 0) {
return err;
}
return begin_write(conn);
}
int trilogy_ping_recv(trilogy_conn_t *conn) { return read_generic_response(conn); }
int trilogy_query_send(trilogy_conn_t *conn, const char *query, size_t query_len)
{
int err = 0;
trilogy_builder_t builder;
err = begin_command_phase(&builder, conn, 0);
if (err < 0) {
return err;
}
err = trilogy_build_query_packet(&builder, query, query_len);
if (err < 0) {
return err;
}
conn->packet_parser.sequence_number = builder.seq;
return begin_write(conn);
}
int trilogy_query_recv(trilogy_conn_t *conn, uint64_t *column_count_out)
{
int err = read_packet(conn);
if (err < 0) {
return err;
}
switch (current_packet_type(conn)) {
case TRILOGY_PACKET_OK:
return read_ok_packet(conn);
case TRILOGY_PACKET_ERR:
return read_err_packet(conn);
default: {
trilogy_result_packet_t result_packet;
err = trilogy_parse_result_packet(conn->packet_buffer.buff, conn->packet_buffer.len, &result_packet);
if (err < 0) {
return err;
}
conn->column_count = result_packet.column_count;
*column_count_out = result_packet.column_count;
conn->started_reading_rows = false;
return TRILOGY_HAVE_RESULTS;
}
}
}
int trilogy_read_column(trilogy_conn_t *conn, trilogy_column_t *column_out)
{
int err = read_packet(conn);
if (err < 0) {
return err;
}
return trilogy_parse_column_packet(conn->packet_buffer.buff, conn->packet_buffer.len, 0, column_out);
}
static int read_eof(trilogy_conn_t *conn)
{
int rc = read_packet(conn);
if (rc < 0) {
return rc;
}
if (conn->capabilities & TRILOGY_CAPABILITIES_DEPRECATE_EOF) {
return read_ok_packet(conn);
} else {
if ((rc = read_eof_packet(conn)) != TRILOGY_EOF) {
return rc;
}
return TRILOGY_OK;
}
}
int trilogy_read_row(trilogy_conn_t *conn, trilogy_value_t *values_out)
{
if (!conn->started_reading_rows) {
if ((conn->capabilities & TRILOGY_CAPABILITIES_DEPRECATE_EOF) == 0) {
// we need to skip over the EOF packet that arrives after the column
// packets
int rc = read_eof(conn);
if (rc < 0) {
return rc;
}
}
conn->started_reading_rows = true;
}
int rc = read_packet(conn);
if (rc < 0) {
return rc;
}
if (conn->capabilities & TRILOGY_CAPABILITIES_DEPRECATE_EOF && current_packet_type(conn) == TRILOGY_PACKET_EOF) {
if ((rc = read_ok_packet(conn)) != TRILOGY_OK) {
return rc;
}
return TRILOGY_EOF;
} else if (current_packet_type(conn) == TRILOGY_PACKET_EOF && conn->packet_buffer.len < 9) {
return read_eof_packet(conn);
} else if (current_packet_type(conn) == TRILOGY_PACKET_ERR) {
return read_err_packet(conn);
} else {
return trilogy_parse_row_packet(conn->packet_buffer.buff, conn->packet_buffer.len, conn->column_count,
values_out);
}
}
int trilogy_drain_results(trilogy_conn_t *conn)
{
if (!conn->started_reading_rows) {
// we need to skip over the EOF packet that arrives after the column
// packets
int rc = read_eof(conn);
if (rc < 0) {
return rc;
}
conn->started_reading_rows = true;
}
while (1) {
int rc = read_packet(conn);
if (rc < 0) {
return rc;
}
if (current_packet_type(conn) == TRILOGY_PACKET_EOF && conn->packet_buffer.len < 9) {
return TRILOGY_OK;
}
}
}
static const uint8_t escape_lookup_table[256] = {
['"'] = '"', ['\0'] = '0', ['\''] = '\'', ['\\'] = '\\', ['\n'] = 'n', ['\r'] = 'r', [26] = 'Z',
};
int trilogy_escape(trilogy_conn_t *conn, const char *str, size_t len, const char **escaped_str_out,
size_t *escaped_len_out)
{
int rc;
trilogy_buffer_t *b = &conn->packet_buffer;
b->len = 0;
// Escaped string will be at least as large as the source string,
// so might as well pre-expand the buffer.
CHECKED(trilogy_buffer_expand(b, len));
const uint8_t *cursor = (const uint8_t *)str;
const uint8_t *end = cursor + len;
if (conn->server_status & TRILOGY_SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
while (cursor < end) {
uint8_t *next_escape = memchr(cursor, '\'', (size_t)(end - cursor));
if (next_escape) {
CHECKED(trilogy_buffer_write(b, cursor, (size_t)(next_escape - cursor)));
CHECKED(trilogy_buffer_write(b, (uint8_t *)"\'\'", 2));
cursor = next_escape + 1;
} else {
CHECKED(trilogy_buffer_write(b, cursor, (size_t)(end - cursor)));
break;
}
}
} else {
while (cursor < end) {
uint8_t escaped = 0;
const uint8_t *start = cursor;
while (cursor < end && !(escaped = escape_lookup_table[*cursor])) {
cursor++;
}
CHECKED(trilogy_buffer_write(b, start, (size_t)(cursor - start)));
if (escaped) {
CHECKED(trilogy_buffer_putc(b, '\\'));
CHECKED(trilogy_buffer_putc(b, escaped));
cursor++;
} else {
break;
}
}
}
*escaped_str_out = (const char *)b->buff;
*escaped_len_out = b->len;
return TRILOGY_OK;
}
int trilogy_close_send(trilogy_conn_t *conn)
{
trilogy_builder_t builder;
int rc = begin_command_phase(&builder, conn, 0);
if (rc < 0) {
return rc;
}
rc = trilogy_build_quit_packet(&builder);
if (rc < 0) {
return rc;
}
return begin_write(conn);
}
int trilogy_close_recv(trilogy_conn_t *conn)
{
trilogy_sock_shutdown(conn->socket);
int rc = read_packet(conn);
switch (rc) {
case TRILOGY_CLOSED_CONNECTION:
return TRILOGY_OK;
case TRILOGY_OK:
// we need to handle TRILOGY_OK specially and translate it into
// TRILOGY_PROTOCOL_VIOLATION so we don't end up returning TRILOGY_OK
// in the default case
return TRILOGY_PROTOCOL_VIOLATION;
default:
return rc;
}
}
void trilogy_free(trilogy_conn_t *conn)
{
if (conn->socket != NULL) {
trilogy_sock_close(conn->socket);
conn->socket = NULL;
}
trilogy_buffer_free(&conn->packet_buffer);
}
int trilogy_discard(trilogy_conn_t *conn)
{
int rc = trilogy_sock_shutdown(conn->socket);
if (rc == TRILOGY_OK) {
trilogy_free(conn);
}
return rc;
}
int trilogy_stmt_prepare_send(trilogy_conn_t *conn, const char *stmt, size_t stmt_len)
{
trilogy_builder_t builder;
int err = begin_command_phase(&builder, conn, 0);
if (err < 0) {
return err;
}
err = trilogy_build_stmt_prepare_packet(&builder, stmt, stmt_len);
if (err < 0) {
return err;
}
return begin_write(conn);
}
int trilogy_stmt_prepare_recv(trilogy_conn_t *conn, trilogy_stmt_t *stmt_out)
{
int err = read_packet(conn);
if (err < 0) {
return err;
}
switch (current_packet_type(conn)) {
case TRILOGY_PACKET_OK: {
err = trilogy_parse_stmt_ok_packet(conn->packet_buffer.buff, conn->packet_buffer.len, stmt_out);
if (err < 0) {
return err;
}
conn->warning_count = stmt_out->warning_count;
return TRILOGY_OK;
}
case TRILOGY_PACKET_ERR:
return read_err_packet(conn);
default:
return TRILOGY_UNEXPECTED_PACKET;
}
}
int trilogy_stmt_execute_send(trilogy_conn_t *conn, trilogy_stmt_t *stmt, uint8_t flags, trilogy_binary_value_t *binds)
{
trilogy_builder_t builder;
int err = begin_command_phase(&builder, conn, 0);
if (err < 0) {
return err;
}
err = trilogy_build_stmt_execute_packet(&builder, stmt->id, flags, binds, stmt->parameter_count);
if (err < 0) {
return err;
}
conn->packet_parser.sequence_number = builder.seq;
return begin_write(conn);
}
int trilogy_stmt_execute_recv(trilogy_conn_t *conn, uint64_t *column_count_out)
{
int err = read_packet(conn);
if (err < 0) {
return err;
}
switch (current_packet_type(conn)) {
case TRILOGY_PACKET_OK:
return read_ok_packet(conn);
case TRILOGY_PACKET_ERR:
return read_err_packet(conn);
default: {
trilogy_result_packet_t result_packet;
err = trilogy_parse_result_packet(conn->packet_buffer.buff, conn->packet_buffer.len, &result_packet);
if (err < 0) {
return err;
}
conn->column_count = result_packet.column_count;
*column_count_out = result_packet.column_count;
return TRILOGY_OK;
}
}
}
int trilogy_stmt_bind_data_send(trilogy_conn_t *conn, trilogy_stmt_t *stmt, uint16_t param_num, uint8_t *data,
size_t data_len)
{
trilogy_builder_t builder;
int err = begin_command_phase(&builder, conn, 0);
if (err < 0) {
return err;
}
err = trilogy_build_stmt_bind_data_packet(&builder, stmt->id, param_num, data, data_len);
if (err < 0) {
return err;
}
return begin_write(conn);
}
int trilogy_stmt_read_row(trilogy_conn_t *conn, trilogy_stmt_t *stmt, trilogy_column_packet_t *columns,
trilogy_binary_value_t *values_out)
{
int err = read_packet(conn);
if (err < 0) {
return err;
}
if (conn->capabilities & TRILOGY_CAPABILITIES_DEPRECATE_EOF && current_packet_type(conn) == TRILOGY_PACKET_EOF) {
if ((err = read_ok_packet(conn)) != TRILOGY_OK) {
return err;
}
return TRILOGY_EOF;
} else if (current_packet_type(conn) == TRILOGY_PACKET_EOF && conn->packet_buffer.len < 9) {
return read_eof_packet(conn);
} else if (current_packet_type(conn) == TRILOGY_PACKET_ERR) {
return read_err_packet(conn);
} else {
return trilogy_parse_stmt_row_packet(conn->packet_buffer.buff, conn->packet_buffer.len, columns,
stmt->column_count, values_out);
}
}
int trilogy_stmt_reset_send(trilogy_conn_t *conn, trilogy_stmt_t *stmt)
{
trilogy_builder_t builder;
int err = begin_command_phase(&builder, conn, 0);
if (err < 0) {
return err;
}
err = trilogy_build_stmt_reset_packet(&builder, stmt->id);
if (err < 0) {
return err;
}
return begin_write(conn);
}
int trilogy_stmt_reset_recv(trilogy_conn_t *conn) {
return read_generic_response(conn);
}
int trilogy_stmt_close_send(trilogy_conn_t *conn, trilogy_stmt_t *stmt)
{
trilogy_builder_t builder;