forked from Qihoo360/Atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork-mysqld-packet.c
More file actions
1918 lines (1535 loc) · 53 KB
/
network-mysqld-packet.c
File metadata and controls
1918 lines (1535 loc) · 53 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
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
/* $%BEGINLICENSE%$
Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; version 2 of the
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
$%ENDLICENSE%$ */
/**
* codec's for the MySQL client protocol
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "network-mysqld-packet.h"
#include "network_mysqld_type.h"
#include "network_mysqld_proto_binary.h"
#include "glib-ext.h"
#include "../lib/sql-tokenizer.h"
#define C(x) x, sizeof(x) - 1
#define S(x) x->str, x->len
gboolean sql_token_equal(sql_token *token, sql_token_id token_id, const char *text)
{
if( token->token_id != token_id)
return FALSE;
if(token_id == TK_LITERAL) {
g_string_ascii_up(token->text);
if( 0 != strcmp(token->text->str, text) )
return FALSE;
}
return TRUE;
}
void update_charset(network_mysqld_con* con) {
GString* charset_client = con->charset_client;
GString* charset_results = con->charset_results;
GString* charset_connection = con->charset_connection;
if (charset_client->len > 0) {
if (con->server) g_string_assign_len(con->server->charset_client, S(charset_client));
g_string_assign_len(con->client->charset_client, S(charset_client));
}
if (charset_results->len > 0) {
if (con->server) g_string_assign_len(con->server->charset_results, S(charset_results));
g_string_assign_len(con->client->charset_results, S(charset_results));
}
if (charset_connection->len > 0) {
if (con->server) g_string_assign_len(con->server->charset_connection, S(charset_connection));
g_string_assign_len(con->client->charset_connection, S(charset_connection));
}
}
network_mysqld_com_query_result_t *network_mysqld_com_query_result_new() {
network_mysqld_com_query_result_t *com_query;
com_query = g_new0(network_mysqld_com_query_result_t, 1);
com_query->state = PARSE_COM_QUERY_INIT;
com_query->query_status = MYSQLD_PACKET_NULL; /* can have 3 values: NULL for unknown, OK for a OK packet, ERR for a error-packet */
return com_query;
}
void network_mysqld_com_query_result_free(network_mysqld_com_query_result_t *udata) {
if (!udata) return;
g_free(udata);
}
/**
* unused
*
* @deprecated will be removed in 0.9
* @see network_mysqld_proto_get_com_query_result
*/
int network_mysqld_com_query_result_track_state(network_packet G_GNUC_UNUSED *packet, network_mysqld_com_query_result_t G_GNUC_UNUSED *udata) {
g_error("%s: this function is deprecated and network_mysqld_proto_get_com_query_result() should be used instead",
G_STRLOC);
}
/**
* @return -1 on error
* 0 on success and done
* 1 on success and need more
*/
int network_mysqld_proto_get_com_query_result(network_packet *packet, network_mysqld_com_query_result_t *query, network_mysqld_con *con, gboolean use_binary_row_data) {
int is_finished = 0;
guint8 status;
int err = 0;
network_mysqld_eof_packet_t *eof_packet;
network_mysqld_ok_packet_t *ok_packet;
/**
* if we get a OK in the first packet there will be no result-set
*/
switch (query->state) {
case PARSE_COM_QUERY_INIT:
err = err || network_mysqld_proto_peek_int8(packet, &status);
if (err) break;
switch (status) {
case MYSQLD_PACKET_ERR: /* e.g. SELECT * FROM dual -> ERROR 1096 (HY000): No tables used */
query->query_status = MYSQLD_PACKET_ERR;
is_finished = 1;
break;
case MYSQLD_PACKET_OK: /* e.g. DELETE FROM tbl */
/**
* trace the change of charset
*/
update_charset(con);
query->query_status = MYSQLD_PACKET_OK;
ok_packet = network_mysqld_ok_packet_new();
err = err || network_mysqld_proto_get_ok_packet(packet, ok_packet);
if (!err) {
if (ok_packet->server_status & SERVER_MORE_RESULTS_EXISTS) {
} else {
is_finished = 1;
}
query->server_status = ok_packet->server_status;
query->warning_count = ok_packet->warnings;
query->affected_rows = ok_packet->affected_rows;
query->insert_id = ok_packet->insert_id;
query->was_resultset = 0;
query->binary_encoded= use_binary_row_data;
}
network_mysqld_ok_packet_free(ok_packet);
break;
case MYSQLD_PACKET_NULL:
/* OH NO, LOAD DATA INFILE :) */
query->state = PARSE_COM_QUERY_LOCAL_INFILE_DATA;
is_finished = 1;
break;
case MYSQLD_PACKET_EOF:
g_critical("%s: COM_QUERY packet should not be (EOF), got: 0x%02x",
G_STRLOC,
status);
err = 1;
break;
default:
query->query_status = MYSQLD_PACKET_OK;
/* looks like a result */
query->state = PARSE_COM_QUERY_FIELD;
break;
}
break;
case PARSE_COM_QUERY_FIELD:
err = err || network_mysqld_proto_peek_int8(packet, &status);
if (err) break;
switch (status) {
case MYSQLD_PACKET_ERR:
case MYSQLD_PACKET_OK:
case MYSQLD_PACKET_NULL:
g_critical("%s: COM_QUERY should not be (OK|NULL|ERR), got: 0x%02x",
G_STRLOC,
status);
err = 1;
break;
case MYSQLD_PACKET_EOF:
/**
* in 5.0 we have CURSORs which have no rows, just a field definition
*
* TODO: find a test-case for it, is it COM_STMT_EXECUTE only?
*/
if (packet->data->len == 9) {
eof_packet = network_mysqld_eof_packet_new();
err = err || network_mysqld_proto_get_eof_packet(packet, eof_packet);
if (!err) {
#if MYSQL_VERSION_ID >= 50000
/* 5.5 may send a SERVER_MORE_RESULTS_EXISTS as part of the first
* EOF together with SERVER_STATUS_CURSOR_EXISTS. In that case,
* we aren't finished. (#61998)
*
* Only if _CURSOR_EXISTS is set alone, we have a field-definition-only
* resultset
*/
if (eof_packet->server_status & SERVER_STATUS_CURSOR_EXISTS &&
!(eof_packet->server_status & SERVER_MORE_RESULTS_EXISTS)) {
is_finished = 1;
} else {
query->state = PARSE_COM_QUERY_RESULT;
}
#else
query->state = PARSE_COM_QUERY_RESULT;
#endif
/* track the server_status of the 1st EOF packet */
query->server_status = eof_packet->server_status;
}
network_mysqld_eof_packet_free(eof_packet);
} else {
query->state = PARSE_COM_QUERY_RESULT;
}
break;
default:
break;
}
break;
case PARSE_COM_QUERY_RESULT:
err = err || network_mysqld_proto_peek_int8(packet, &status);
if (err) break;
switch (status) {
case MYSQLD_PACKET_EOF:
if (packet->data->len == 9) {
eof_packet = network_mysqld_eof_packet_new();
err = err || network_mysqld_proto_get_eof_packet(packet, eof_packet);
if (!err) {
query->was_resultset = 1;
#ifndef SERVER_PS_OUT_PARAMS
#define SERVER_PS_OUT_PARAMS 4096
#endif
/**
* a PS_OUT_PARAMS is set if a COM_STMT_EXECUTE executes a CALL sp(?) where sp is a PROCEDURE with OUT params
*
* ...
* 05 00 00 12 fe 00 00 0a 10 -- end column-def (auto-commit, more-results, ps-out-params)
* ...
* 05 00 00 14 fe 00 00 02 00 -- end of rows (auto-commit), see the missing (more-results, ps-out-params)
* 07 00 00 15 00 00 00 02 00 00 00 -- OK for the CALL
*
* for all other resultsets we trust the status-flags of the 2nd EOF packet
*/
if (!(query->server_status & SERVER_PS_OUT_PARAMS)) {
query->server_status = eof_packet->server_status;
}
query->warning_count = eof_packet->warnings;
if (query->server_status & SERVER_MORE_RESULTS_EXISTS) {
query->state = PARSE_COM_QUERY_INIT;
} else {
is_finished = 1;
}
}
network_mysqld_eof_packet_free(eof_packet);
}
break;
case MYSQLD_PACKET_ERR:
/* like
*
* EXPLAIN SELECT * FROM dual; returns an error
*
* EXPLAIN SELECT 1 FROM dual; returns a result-set
* */
is_finished = 1;
break;
case MYSQLD_PACKET_OK:
case MYSQLD_PACKET_NULL:
if (use_binary_row_data) {
/* fallthrough to default:
0x00 is part of the protocol for binary row packets
*/
} else {
/* the first field might be a NULL for a text row packet */
break;
}
default:
query->rows++;
query->bytes += packet->data->len;
break;
}
break;
case PARSE_COM_QUERY_LOCAL_INFILE_DATA:
/* we will receive a empty packet if we are done */
if (packet->data->len == packet->offset) {
query->state = PARSE_COM_QUERY_LOCAL_INFILE_RESULT;
is_finished = 1;
}
break;
case PARSE_COM_QUERY_LOCAL_INFILE_RESULT:
err = err || network_mysqld_proto_get_int8(packet, &status);
if (err) break;
switch (status) {
case MYSQLD_PACKET_OK:
is_finished = 1;
break;
case MYSQLD_PACKET_NULL:
case MYSQLD_PACKET_ERR:
case MYSQLD_PACKET_EOF:
default:
g_critical("%s: COM_QUERY,should be (OK), got: 0x%02x",
G_STRLOC,
status);
err = 1;
break;
}
break;
}
if (err) return -1;
return is_finished;
}
/**
* check if the we are in the LOCAL INFILE 'send data from client' state
*
* is deprecated as the name doesn't reflect its purpose:
* - it isn't triggered for LOAD DATA INFILE (w/o LOCAL)
* - it also covers LOAD XML LOCAL INFILE
*
* @deprecated use network_mysqld_com_query_result_is_local_infile() instead
*/
gboolean network_mysqld_com_query_result_is_load_data(network_mysqld_com_query_result_t *udata) {
return network_mysqld_com_query_result_is_local_infile(udata);
}
/**
* check if the we are in the LOCAL INFILE 'send data from client' state
*/
gboolean network_mysqld_com_query_result_is_local_infile(network_mysqld_com_query_result_t *udata) {
return (udata->state == PARSE_COM_QUERY_LOCAL_INFILE_DATA) ? TRUE : FALSE;
}
network_mysqld_com_stmt_prepare_result_t *network_mysqld_com_stmt_prepare_result_new() {
network_mysqld_com_stmt_prepare_result_t *udata;
udata = g_new0(network_mysqld_com_stmt_prepare_result_t, 1);
udata->first_packet = TRUE;
return udata;
}
void network_mysqld_com_stmt_prepare_result_free(network_mysqld_com_stmt_prepare_result_t *udata) {
if (!udata) return;
g_free(udata);
}
int network_mysqld_proto_get_com_stmt_prepare_result(
network_packet *packet,
network_mysqld_com_stmt_prepare_result_t *udata) {
guint8 status;
int is_finished = 0;
int err = 0;
err = err || network_mysqld_proto_get_int8(packet, &status);
if (udata->first_packet == 1) {
udata->first_packet = 0;
switch (status) {
case MYSQLD_PACKET_OK:
g_assert(packet->data->len == 12 + NET_HEADER_SIZE);
/* the header contains the number of EOFs we expect to see
* - no params -> 0
* - params | fields -> 1
* - params + fields -> 2
*/
udata->want_eofs = 0;
if (packet->data->str[NET_HEADER_SIZE + 5] != 0 || packet->data->str[NET_HEADER_SIZE + 6] != 0) {
udata->want_eofs++;
}
if (packet->data->str[NET_HEADER_SIZE + 7] != 0 || packet->data->str[NET_HEADER_SIZE + 8] != 0) {
udata->want_eofs++;
}
if (udata->want_eofs == 0) {
is_finished = 1;
}
break;
case MYSQLD_PACKET_ERR:
is_finished = 1;
break;
default:
g_error("%s.%d: COM_STMT_PREPARE should either get a (OK|ERR), got %02x",
__FILE__, __LINE__,
status);
break;
}
} else {
switch (status) {
case MYSQLD_PACKET_OK:
case MYSQLD_PACKET_NULL:
case MYSQLD_PACKET_ERR:
g_error("%s.%d: COM_STMT_PREPARE should not be (OK|ERR|NULL), got: %02x",
__FILE__, __LINE__,
status);
break;
case MYSQLD_PACKET_EOF:
if (--udata->want_eofs == 0) {
is_finished = 1;
}
break;
default:
break;
}
}
if (err) return -1;
return is_finished;
}
network_mysqld_com_init_db_result_t *network_mysqld_com_init_db_result_new() {
network_mysqld_com_init_db_result_t *udata;
udata = g_new0(network_mysqld_com_init_db_result_t, 1);
udata->db_name = NULL;
return udata;
}
void network_mysqld_com_init_db_result_free(network_mysqld_com_init_db_result_t *udata) {
if (udata->db_name) g_string_free(udata->db_name, TRUE);
g_free(udata);
}
int network_mysqld_com_init_db_result_track_state(network_packet *packet, network_mysqld_com_init_db_result_t *udata) {
network_mysqld_proto_skip_network_header(packet);
network_mysqld_proto_skip(packet, 1); /* the command */
if (packet->offset != packet->data->len) {
udata->db_name = g_string_new(NULL);
network_mysqld_proto_get_gstring_len(packet, packet->data->len - packet->offset, udata->db_name);
} else {
if (udata->db_name) g_string_free(udata->db_name, TRUE);
udata->db_name = NULL;
}
return 0;
}
int network_mysqld_proto_get_com_init_db(
network_packet *packet,
network_mysqld_com_init_db_result_t *udata,
network_mysqld_con *con) {
guint8 status;
int is_finished;
int err = 0;
/**
* in case we have a init-db statement we track the db-change on the server-side
* connection
*/
err = err || network_mysqld_proto_get_int8(packet, &status);
switch (status) {
case MYSQLD_PACKET_ERR:
is_finished = 1;
break;
case MYSQLD_PACKET_OK:
/**
* track the change of the init_db */
if (con->server) g_string_truncate(con->server->default_db, 0);
g_string_truncate(con->client->default_db, 0);
if (udata->db_name && udata->db_name->len) {
if (con->server) {
g_string_append_len(con->server->default_db,
S(udata->db_name));
}
g_string_append_len(con->client->default_db,
S(udata->db_name));
}
is_finished = 1;
break;
default:
g_critical("%s.%d: COM_INIT_DB should be (ERR|OK), got %02x",
__FILE__, __LINE__,
status);
return -1;
}
if (err) return -1;
return is_finished;
}
/**
* init the tracking of the sub-states of the protocol
*/
int network_mysqld_con_command_states_init(network_mysqld_con *con, network_packet *packet) {
guint8 cmd;
int err = 0;
err = err || network_mysqld_proto_skip_network_header(packet);
err = err || network_mysqld_proto_get_int8(packet, &cmd);
if (err) return -1;
con->parse.command = cmd;
packet->offset = 0; /* reset the offset again for the next functions */
/* init the parser for the commands */
switch (con->parse.command) {
case COM_QUERY:
case COM_PROCESS_INFO:
case COM_STMT_EXECUTE:
con->parse.data = network_mysqld_com_query_result_new();
con->parse.data_free = (GDestroyNotify)network_mysqld_com_query_result_free;
break;
case COM_STMT_PREPARE:
con->parse.data = network_mysqld_com_stmt_prepare_result_new();
con->parse.data_free = (GDestroyNotify)network_mysqld_com_stmt_prepare_result_free;
break;
case COM_INIT_DB:
con->parse.data = network_mysqld_com_init_db_result_new();
con->parse.data_free = (GDestroyNotify)network_mysqld_com_init_db_result_free;
network_mysqld_com_init_db_result_track_state(packet, con->parse.data);
break;
case COM_QUIT:
/* track COM_QUIT going to the server, to be able to tell if the server
* a) simply went away or
* b) closed the connection because the client asked it to
* If b) we should not print a message at the next EV_READ event from the server fd
*/
con->com_quit_seen = TRUE;
default:
break;
}
return 0;
}
/**
* @param packet the current packet that is passing by
*
*
* @return -1 on invalid packet,
* 0 need more packets,
* 1 for the last packet
*/
int network_mysqld_proto_get_query_result(network_packet *packet, network_mysqld_con *con) {
guint8 status;
int is_finished = 0;
int err = 0;
network_mysqld_eof_packet_t *eof_packet;
err = err || network_mysqld_proto_skip_network_header(packet);
if (err) return -1;
/* forward the response to the client */
switch (con->parse.command) {
case COM_CHANGE_USER:
/**
* - OK
* - ERR (in 5.1.12+ + a duplicate ERR)
*/
err = err || network_mysqld_proto_get_int8(packet, &status);
if (err) return -1;
switch (status) {
case MYSQLD_PACKET_ERR:
is_finished = 1;
break;
case MYSQLD_PACKET_OK:
is_finished = 1;
break;
default:
g_error("%s.%d: COM_(0x%02x) should be (ERR|OK), got %02x",
__FILE__, __LINE__,
con->parse.command, status);
break;
}
break;
case COM_INIT_DB:
is_finished = network_mysqld_proto_get_com_init_db(packet, con->parse.data, con);
break;
case COM_REFRESH:
case COM_STMT_RESET:
case COM_PING:
case COM_TIME:
case COM_REGISTER_SLAVE:
case COM_PROCESS_KILL:
err = err || network_mysqld_proto_get_int8(packet, &status);
if (err) return -1;
switch (status) {
case MYSQLD_PACKET_ERR:
case MYSQLD_PACKET_OK:
is_finished = 1;
break;
default:
g_error("%s.%d: COM_(0x%02x) should be (ERR|OK), got 0x%02x",
__FILE__, __LINE__,
con->parse.command, (guint8)status);
break;
}
break;
case COM_DEBUG:
case COM_SET_OPTION:
case COM_SHUTDOWN:
err = err || network_mysqld_proto_get_int8(packet, &status);
if (err) return -1;
switch (status) {
case MYSQLD_PACKET_ERR: /* COM_DEBUG may not have the right permissions */
case MYSQLD_PACKET_EOF:
is_finished = 1;
break;
default:
g_error("%s.%d: COM_(0x%02x) should be EOF, got x%02x",
__FILE__, __LINE__,
con->parse.command, (guint8)status);
break;
}
break;
case COM_FIELD_LIST:
err = err || network_mysqld_proto_get_int8(packet, &status);
if (err) return -1;
/* we transfer some data and wait for the EOF */
switch (status) {
case MYSQLD_PACKET_ERR:
case MYSQLD_PACKET_EOF:
is_finished = 1;
break;
case MYSQLD_PACKET_NULL:
case MYSQLD_PACKET_OK:
g_error("%s.%d: COM_(0x%02x) should not be (OK|ERR|NULL), got: %02x",
__FILE__, __LINE__,
con->parse.command, status);
break;
default:
break;
}
break;
#if MYSQL_VERSION_ID >= 50000
case COM_STMT_FETCH:
/* */
err = err || network_mysqld_proto_peek_int8(packet, &status);
if (err) return -1;
switch (status) {
case MYSQLD_PACKET_EOF:
eof_packet = network_mysqld_eof_packet_new();
err = err || network_mysqld_proto_get_eof_packet(packet, eof_packet);
if (!err) {
if ((eof_packet->server_status & SERVER_STATUS_LAST_ROW_SENT) ||
(eof_packet->server_status & SERVER_STATUS_CURSOR_EXISTS)) {
is_finished = 1;
}
}
network_mysqld_eof_packet_free(eof_packet);
break;
case MYSQLD_PACKET_ERR:
is_finished = 1;
break;
default:
break;
}
break;
#endif
case COM_QUIT: /* sometimes we get a packet before the connection closes */
case COM_STATISTICS:
/* just one packet, no EOF */
is_finished = 1;
break;
case COM_STMT_PREPARE:
is_finished = network_mysqld_proto_get_com_stmt_prepare_result(packet, con->parse.data);
break;
case COM_STMT_EXECUTE:
/* COM_STMT_EXECUTE result packets are basically the same as COM_QUERY ones,
* the only difference is the encoding of the actual data - fields are in there, too.
*/
is_finished = network_mysqld_proto_get_com_query_result(packet, con->parse.data, con, TRUE);
break;
case COM_PROCESS_INFO:
case COM_QUERY:
#ifdef DEBUG_TRACE_QUERY
g_debug("now con->current_query is %s\n", con->current_query->str);
#endif
is_finished = network_mysqld_proto_get_com_query_result(packet, con->parse.data, con, FALSE);
break;
case COM_BINLOG_DUMP:
/**
* the binlog-dump event stops, forward all packets as we see them
* and keep the command active
*/
is_finished = 1;
break;
default:
g_critical("%s: COM_(0x%02x) is not handled",
G_STRLOC,
con->parse.command);
err = 1;
break;
}
if (err) return -1;
return is_finished;
}
int network_mysqld_proto_get_fielddef(network_packet *packet, network_mysqld_proto_fielddef_t *field, guint32 capabilities) {
int err = 0;
if (capabilities & CLIENT_PROTOCOL_41) {
guint16 field_charsetnr;
guint32 field_length;
guint8 field_type;
guint16 field_flags;
guint8 field_decimals;
err = err || network_mysqld_proto_get_lenenc_string(packet, &field->catalog, NULL);
err = err || network_mysqld_proto_get_lenenc_string(packet, &field->db, NULL);
err = err || network_mysqld_proto_get_lenenc_string(packet, &field->table, NULL);
err = err || network_mysqld_proto_get_lenenc_string(packet, &field->org_table, NULL);
err = err || network_mysqld_proto_get_lenenc_string(packet, &field->name, NULL);
err = err || network_mysqld_proto_get_lenenc_string(packet, &field->org_name, NULL);
err = err || network_mysqld_proto_skip(packet, 1); /* filler */
err = err || network_mysqld_proto_get_int16(packet, &field_charsetnr);
err = err || network_mysqld_proto_get_int32(packet, &field_length);
err = err || network_mysqld_proto_get_int8(packet, &field_type);
err = err || network_mysqld_proto_get_int16(packet, &field_flags);
err = err || network_mysqld_proto_get_int8(packet, &field_decimals);
err = err || network_mysqld_proto_skip(packet, 2); /* filler */
if (!err) {
field->charsetnr = field_charsetnr;
field->length = field_length;
field->type = field_type;
field->flags = field_flags;
field->decimals = field_decimals;
}
} else {
guint8 len;
guint32 field_length;
guint8 field_type;
guint8 field_decimals;
/* see protocol.cc Protocol::send_fields */
err = err || network_mysqld_proto_get_lenenc_string(packet, &field->table, NULL);
err = err || network_mysqld_proto_get_lenenc_string(packet, &field->name, NULL);
err = err || network_mysqld_proto_get_int8(packet, &len);
err = err || (len != 3);
err = err || network_mysqld_proto_get_int24(packet, &field_length);
err = err || network_mysqld_proto_get_int8(packet, &len);
err = err || (len != 1);
err = err || network_mysqld_proto_get_int8(packet, &field_type);
err = err || network_mysqld_proto_get_int8(packet, &len);
if (len == 3) { /* the CLIENT_LONG_FLAG is set */
guint16 field_flags;
err = err || network_mysqld_proto_get_int16(packet, &field_flags);
if (!err) field->flags = field_flags;
} else if (len == 2) {
guint8 field_flags;
err = err || network_mysqld_proto_get_int8(packet, &field_flags);
if (!err) field->flags = field_flags;
} else {
err = -1;
}
err = err || network_mysqld_proto_get_int8(packet, &field_decimals);
if (!err) {
field->charsetnr = 0x08 /* latin1_swedish_ci */;
field->length = field_length;
field->type = field_type;
field->decimals = field_decimals;
}
}
return err ? -1 : 0;
}
/**
* parse the result-set packet and extract the fields
*
* @param chunk list of mysql packets
* @param fields empty array where the fields shall be stored in
*
* @return NULL if there is no resultset
* pointer to the chunk after the fields (to the EOF packet)
*/
GList *network_mysqld_proto_get_fielddefs(GList *chunk, GPtrArray *fields) {
network_packet packet;
guint64 field_count;
guint i;
int err = 0;
guint32 capabilities = CLIENT_PROTOCOL_41;
network_mysqld_lenenc_type lenenc_type;
packet.data = chunk->data;
packet.offset = 0;
err = err || network_mysqld_proto_skip_network_header(&packet);
err = err || network_mysqld_proto_peek_lenenc_type(&packet, &lenenc_type);
if (err) return NULL; /* packet too short */
/* make sure that we have a valid length-encoded integer here */
switch (lenenc_type) {
case NETWORK_MYSQLD_LENENC_TYPE_INT:
break;
default:
/* we shouldn't be here, we expected to get a valid length-encoded field count */
return NULL;
}
err = err || network_mysqld_proto_get_lenenc_int(&packet, &field_count);
if (err) return NULL; /* packet to short */
if (field_count == 0) {
/* shouldn't happen, the upper layer should have checked that this is a OK packet */
return NULL;
}
/* the next chunk, the field-def */
for (i = 0; i < field_count; i++) {
network_mysqld_proto_fielddef_t *field;
chunk = chunk->next;
g_assert(chunk);
packet.data = chunk->data;
packet.offset = 0;
field = network_mysqld_proto_fielddef_new();
err = err || network_mysqld_proto_skip_network_header(&packet);
err = err || network_mysqld_proto_get_fielddef(&packet, field, capabilities);
g_ptr_array_add(fields, field); /* even if we had an error, append it so that we can free it later */
if (err) return NULL;
}
/* this should be EOF chunk */
chunk = chunk->next;
if (!chunk) return NULL;
packet.data = chunk->data;
packet.offset = 0;
err = err || network_mysqld_proto_skip_network_header(&packet);
err = err || network_mysqld_proto_peek_lenenc_type(&packet, &lenenc_type);
err = err || (lenenc_type != NETWORK_MYSQLD_LENENC_TYPE_EOF);
if (err) return NULL;
return chunk;
}
network_mysqld_ok_packet_t *network_mysqld_ok_packet_new() {
network_mysqld_ok_packet_t *ok_packet;
ok_packet = g_new0(network_mysqld_ok_packet_t, 1);
return ok_packet;
}
void network_mysqld_ok_packet_free(network_mysqld_ok_packet_t *ok_packet) {
if (!ok_packet) return;
g_free(ok_packet);
}
/**
* decode a OK packet from the network packet
*/
int network_mysqld_proto_get_ok_packet(network_packet *packet, network_mysqld_ok_packet_t *ok_packet) {
guint8 field_count;
guint64 affected, insert_id;
guint16 server_status, warning_count = 0;
guint32 capabilities = CLIENT_PROTOCOL_41;
int err = 0;
err = err || network_mysqld_proto_get_int8(packet, &field_count);
if (err) return -1;
if (field_count != 0) {
g_critical("%s: expected the first byte to be 0, got %d",
G_STRLOC,
field_count);
return -1;
}
err = err || network_mysqld_proto_get_lenenc_int(packet, &affected);
err = err || network_mysqld_proto_get_lenenc_int(packet, &insert_id);
err = err || network_mysqld_proto_get_int16(packet, &server_status);
if (capabilities & CLIENT_PROTOCOL_41) {
err = err || network_mysqld_proto_get_int16(packet, &warning_count);
}
if (!err) {
ok_packet->affected_rows = affected;
ok_packet->insert_id = insert_id;
ok_packet->server_status = server_status;
ok_packet->warnings = warning_count;
}
return err ? -1 : 0;
}
int network_mysqld_proto_append_ok_packet(GString *packet, network_mysqld_ok_packet_t *ok_packet) {
guint32 capabilities = CLIENT_PROTOCOL_41;
network_mysqld_proto_append_int8(packet, 0); /* no fields */
network_mysqld_proto_append_lenenc_int(packet, ok_packet->affected_rows);
network_mysqld_proto_append_lenenc_int(packet, ok_packet->insert_id);
network_mysqld_proto_append_int16(packet, ok_packet->server_status); /* autocommit */
if (capabilities & CLIENT_PROTOCOL_41) {
network_mysqld_proto_append_int16(packet, ok_packet->warnings); /* no warnings */
}
return 0;
}
static network_mysqld_err_packet_t *network_mysqld_err_packet_new_full(network_mysqld_protocol_t version) {
network_mysqld_err_packet_t *err_packet;
err_packet = g_new0(network_mysqld_err_packet_t, 1);
err_packet->sqlstate = g_string_new(NULL);
err_packet->errmsg = g_string_new(NULL);
err_packet->version = version;
return err_packet;
}
network_mysqld_err_packet_t *network_mysqld_err_packet_new() {
return network_mysqld_err_packet_new_full(NETWORK_MYSQLD_PROTOCOL_VERSION_41);
}
network_mysqld_err_packet_t *network_mysqld_err_packet_new_pre41() {
return network_mysqld_err_packet_new_full(NETWORK_MYSQLD_PROTOCOL_VERSION_PRE41);
}
void network_mysqld_err_packet_free(network_mysqld_err_packet_t *err_packet) {
if (!err_packet) return;
g_string_free(err_packet->sqlstate, TRUE);
g_string_free(err_packet->errmsg, TRUE);
g_free(err_packet);
}
/**