forked from Qihoo360/Atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork-mysqld.c
More file actions
1937 lines (1612 loc) · 54.4 KB
/
network-mysqld.c
File metadata and controls
1937 lines (1612 loc) · 54.4 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) 2007, 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%$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h> /* required for FIONREAD on solaris */
#endif
#ifndef _WIN32
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h> /** inet_ntoa */
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#else
#include <winsock2.h>
#include <io.h>
#define ioctl ioctlsocket
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#include <glib.h>
#include <mysql.h>
#include <mysqld_error.h>
#include "network-debug.h"
#include "network-mysqld.h"
#include "network-mysqld-proto.h"
#include "network-mysqld-packet.h"
#include "network-conn-pool.h"
#include "chassis-mainloop.h"
#include "chassis-event-thread.h"
#include "lua-scope.h"
#include "glib-ext.h"
#include "network-mysqld-lua.h"
#if defined(HAVE_SYS_SDT_H) && defined(ENABLE_DTRACE)
#include <sys/sdt.h>
#include "proxy-dtrace-provider.h"
#else
#include "disable-dtrace.h"
#endif
#ifdef HAVE_WRITEV
#define USE_BUFFERED_NETIO
#else
#undef USE_BUFFERED_NETIO
#endif
#ifdef _WIN32
#define E_NET_CONNRESET WSAECONNRESET
#define E_NET_CONNABORTED WSAECONNABORTED
#define E_NET_WOULDBLOCK WSAEWOULDBLOCK
#define E_NET_INPROGRESS WSAEINPROGRESS
#else
#define E_NET_CONNRESET ECONNRESET
#define E_NET_CONNABORTED ECONNABORTED
#define E_NET_INPROGRESS EINPROGRESS
#if EWOULDBLOCK == EAGAIN
/**
* some system make EAGAIN == EWOULDBLOCK which would lead to a
* error in the case handling
*
* set it to -1 as this error should never happen
*/
#define E_NET_WOULDBLOCK -1
#else
#define E_NET_WOULDBLOCK EWOULDBLOCK
#endif
#endif
/**
* a handy marco for constant strings
*/
#define C(x) x, sizeof(x) - 1
#define S(x) x->str, x->len
//static GMutex con_mutex;
/**
* call the cleanup callback for the current connection
*
* @param srv global context
* @param con connection context
*
* @return NETWORK_SOCKET_SUCCESS on success
*/
network_socket_retval_t plugin_call_cleanup(chassis *srv, network_mysqld_con *con) {
NETWORK_MYSQLD_PLUGIN_FUNC(func) = NULL;
network_socket_retval_t retval = NETWORK_SOCKET_SUCCESS;
func = con->plugins.con_cleanup;
if (!func) return retval;
// LOCK_LUA(srv->priv->sc); /*remove lock*/
retval = (*func)(srv, con);
// UNLOCK_LUA(srv->priv->sc); /*remove lock*/
return retval;
}
int network_mysqld_init(chassis *srv, gchar *default_file) {
/* store the pointer to the chassis in the Lua registry */
srv->sc = lua_scope_new();
lua_State *L = srv->sc->L;
lua_pushlightuserdata(L, (void*)srv);
lua_setfield(L, LUA_REGISTRYINDEX, CHASSIS_LUA_REGISTRY_KEY);
srv->backends = network_backends_new(srv->event_thread_count, default_file);
return 0;
}
/**
* create a connection
*
* @return a connection context
*/
network_mysqld_con *network_mysqld_con_new() {
network_mysqld_con *con;
con = g_new0(network_mysqld_con, 1);
con->parse.command = -1;
con->is_in_transaction = con->is_in_select_calc_found_rows = con->is_not_autocommit = FALSE;
con->charset_client = g_string_new(NULL);
con->charset_results = g_string_new(NULL);
con->charset_connection = g_string_new(NULL);
con->locks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
con->merge_res = g_new(merge_res_t, 1);
con->merge_res->rows = g_ptr_array_new();
con->challenge = g_string_sized_new(20);
return con;
}
void network_mysqld_add_connection(chassis *srv, network_mysqld_con *con) {
con->srv = srv;
/*
g_mutex_lock(&con_mutex);
g_ptr_array_add(srv->priv->cons, con);
g_mutex_unlock(&con_mutex);
*/
}
/**
* free a connection
*
* closes the client and server sockets
*
* @param con connection context
*/
void network_mysqld_con_free(network_mysqld_con *con) {
if (!con) return;
if (con->parse.data && con->parse.data_free) {
con->parse.data_free(con->parse.data);
}
if (con->server) network_socket_free(con->server);
if (con->client) network_socket_free(con->client);
/* we are still in the conns-array */
/*
g_mutex_lock(&con_mutex);
g_ptr_array_remove_fast(con->srv->priv->cons, con);
g_mutex_unlock(&con_mutex);
*/
g_string_free(con->charset_client, TRUE);
g_string_free(con->charset_results, TRUE);
g_string_free(con->charset_connection, TRUE);
g_hash_table_remove_all(con->locks);
g_hash_table_destroy(con->locks);
if (con->merge_res) {
GPtrArray* rows = con->merge_res->rows;
if (rows) {
guint i;
for (i = 0; i < rows->len; ++i) {
GPtrArray* row = g_ptr_array_index(rows, i);
guint j;
for (j = 0; j < row->len; ++j) {
g_free(g_ptr_array_index(row, j));
}
g_ptr_array_free(row, TRUE);
}
g_ptr_array_free(rows, TRUE);
}
g_free(con->merge_res);
}
if (con->challenge) g_string_free(con->challenge, TRUE);
g_free(con);
}
#if 0
static void dump_str(const char *msg, const unsigned char *s, size_t len) {
GString *hex;
size_t i;
hex = g_string_new(NULL);
for (i = 0; i < len; i++) {
g_string_append_printf(hex, "%02x", s[i]);
if ((i + 1) % 16 == 0) {
g_string_append(hex, "\n");
} else {
g_string_append_c(hex, ' ');
}
}
g_message("(%s): %s", msg, hex->str);
g_string_free(hex, TRUE);
}
#endif
int network_mysqld_queue_reset(network_socket *sock) {
sock->packet_id_is_reset = TRUE;
return 0;
}
/**
* synchronize the packet-ids of two network-sockets
*/
int network_mysqld_queue_sync(network_socket *dst, network_socket *src) {
g_assert_cmpint(src->packet_id_is_reset, ==, FALSE);
if (dst->packet_id_is_reset == FALSE) {
/* this shouldn't really happen */
}
dst->last_packet_id = src->last_packet_id - 1;
return 0;
}
/**
* appends a raw MySQL packet to the queue
*
* the packet is append the queue directly and shouldn't be used by the caller afterwards anymore
* and has to by in the MySQL Packet format
*
*/
int network_mysqld_queue_append_raw(network_socket *sock, network_queue *queue, GString *data) {
guint32 packet_len;
guint8 packet_id;
/* check that the length header is valid */
if (queue != sock->send_queue &&
queue != sock->recv_queue) {
g_critical("%s: queue = %p doesn't belong to sock %p",
G_STRLOC,
(void *)queue,
(void *)sock);
return -1;
}
g_assert_cmpint(data->len, >=, 4);
packet_len = network_mysqld_proto_get_packet_len(data);
packet_id = network_mysqld_proto_get_packet_id(data);
g_assert_cmpint(packet_len, ==, data->len - 4);
if (sock->packet_id_is_reset) {
/* the ->last_packet_id is undefined, accept what we get */
sock->last_packet_id = packet_id;
sock->packet_id_is_reset = FALSE;
} else if (packet_id != (guint8)(sock->last_packet_id + 1)) {
sock->last_packet_id++;
#if 0
g_critical("%s: packet-id %d doesn't match for socket's last packet %d, patching it",
G_STRLOC,
packet_id,
sock->last_packet_id);
#endif
network_mysqld_proto_set_packet_id(data, sock->last_packet_id);
} else {
sock->last_packet_id++;
}
network_queue_append(queue, data);
return 0;
}
/**
* appends a payload to the queue
*
* the packet is copied and prepened with the mysql packet header before it is appended to the queue
* if neccesary the payload is spread over multiple mysql packets
*/
int network_mysqld_queue_append(network_socket *sock, network_queue *queue, const char *data, size_t packet_len) {
gsize packet_offset = 0;
do {
GString *s;
gsize cur_packet_len = MIN(packet_len, PACKET_LEN_MAX);
s = g_string_sized_new(packet_len + 4);
if (sock->packet_id_is_reset) {
sock->packet_id_is_reset = FALSE;
sock->last_packet_id = 0xff; /** the ++last_packet_id will make sure we send a 0 */
}
network_mysqld_proto_append_packet_len(s, cur_packet_len);
network_mysqld_proto_append_packet_id(s, ++sock->last_packet_id);
g_string_append_len(s, data + packet_offset, cur_packet_len);
network_queue_append(queue, s);
if (packet_len == PACKET_LEN_MAX) {
s = g_string_sized_new(4);
network_mysqld_proto_append_packet_len(s, 0);
network_mysqld_proto_append_packet_id(s, ++sock->last_packet_id);
network_queue_append(queue, s);
}
packet_len -= cur_packet_len;
packet_offset += cur_packet_len;
} while (packet_len > 0);
return 0;
}
/**
* create a OK packet and append it to the send-queue
*
* @param con a client socket
* @param affected_rows affected rows
* @param insert_id insert_id
* @param server_status server_status (bitfield of SERVER_STATUS_*)
* @param warnings number of warnings to fetch with SHOW WARNINGS
* @return 0
*
* @todo move to network_mysqld_proto
*/
int network_mysqld_con_send_ok_full(network_socket *con, guint64 affected_rows, guint64 insert_id, guint16 server_status, guint16 warnings ) {
GString *packet = g_string_new(NULL);
network_mysqld_ok_packet_t *ok_packet;
ok_packet = network_mysqld_ok_packet_new();
ok_packet->affected_rows = affected_rows;
ok_packet->insert_id = insert_id;
ok_packet->server_status = server_status;
ok_packet->warnings = warnings;
network_mysqld_proto_append_ok_packet(packet, ok_packet);
network_mysqld_queue_append(con, con->send_queue, S(packet));
network_mysqld_queue_reset(con);
g_string_free(packet, TRUE);
network_mysqld_ok_packet_free(ok_packet);
return 0;
}
/**
* send a simple OK packet
*
* - no affected rows
* - no insert-id
* - AUTOCOMMIT
* - no warnings
*
* @param con a client socket
*/
int network_mysqld_con_send_ok(network_socket *con) {
return network_mysqld_con_send_ok_full(con, 0, 0, SERVER_STATUS_AUTOCOMMIT, 0);
}
static int network_mysqld_con_send_error_full_all(network_socket *con,
const char *errmsg, gsize errmsg_len,
guint errorcode,
const gchar *sqlstate,
gboolean is_41_protocol) {
GString *packet;
network_mysqld_err_packet_t *err_packet;
packet = g_string_sized_new(10 + errmsg_len);
err_packet = is_41_protocol ? network_mysqld_err_packet_new() : network_mysqld_err_packet_new_pre41();
err_packet->errcode = errorcode;
if (errmsg) g_string_assign_len(err_packet->errmsg, errmsg, errmsg_len);
if (sqlstate) g_string_assign_len(err_packet->sqlstate, sqlstate, strlen(sqlstate));
network_mysqld_proto_append_err_packet(packet, err_packet);
network_mysqld_queue_append(con, con->send_queue, S(packet));
network_mysqld_queue_reset(con);
network_mysqld_err_packet_free(err_packet);
g_string_free(packet, TRUE);
return 0;
}
/**
* send a error packet to the client connection
*
* @note the sqlstate has to match the SQL standard. If no matching SQL state is known, leave it at NULL
*
* @param con the client connection
* @param errmsg the error message
* @param errmsg_len byte-len of the error-message
* @param errorcode mysql error-code we want to send
* @param sqlstate if none-NULL, 5-char SQL state to send, if NULL, default SQL state is used
*
* @return 0 on success
*/
int network_mysqld_con_send_error_full(network_socket *con, const char *errmsg, gsize errmsg_len, guint errorcode, const gchar *sqlstate) {
return network_mysqld_con_send_error_full_all(con, errmsg, errmsg_len, errorcode, sqlstate, TRUE);
}
/**
* send a error-packet to the client connection
*
* errorcode is 1000, sqlstate is NULL
*
* @param con the client connection
* @param errmsg the error message
* @param errmsg_len byte-len of the error-message
*
* @see network_mysqld_con_send_error_full
*/
int network_mysqld_con_send_error(network_socket *con, const char *errmsg, gsize errmsg_len) {
return network_mysqld_con_send_error_full(con, errmsg, errmsg_len, ER_UNKNOWN_ERROR, NULL);
}
/**
* send a error packet to the client connection (pre-4.1 protocol)
*
* @param con the client connection
* @param errmsg the error message
* @param errmsg_len byte-len of the error-message
* @param errorcode mysql error-code we want to send
*
* @return 0 on success
*/
int network_mysqld_con_send_error_pre41_full(network_socket *con, const char *errmsg, gsize errmsg_len, guint errorcode) {
return network_mysqld_con_send_error_full_all(con, errmsg, errmsg_len, errorcode, NULL, FALSE);
}
/**
* send a error-packet to the client connection (pre-4.1 protocol)
*
* @param con the client connection
* @param errmsg the error message
* @param errmsg_len byte-len of the error-message
*
* @see network_mysqld_con_send_error_pre41_full
*/
int network_mysqld_con_send_error_pre41(network_socket *con, const char *errmsg, gsize errmsg_len) {
return network_mysqld_con_send_error_pre41_full(con, errmsg, errmsg_len, ER_UNKNOWN_ERROR);
}
/**
* get a full packet from the raw queue and move it to the packet queue
*/
network_socket_retval_t network_mysqld_con_get_packet(chassis G_GNUC_UNUSED*chas, network_socket *con) {
GString *packet = NULL;
GString header;
char header_str[NET_HEADER_SIZE + 1] = "";
guint32 packet_len;
guint8 packet_id;
/**
* read the packet header (4 bytes)
*/
header.str = header_str;
header.allocated_len = sizeof(header_str);
header.len = 0;
/* read the packet len if the leading packet */
if (!network_queue_peek_string(con->recv_queue_raw, NET_HEADER_SIZE, &header)) {
/* too small */
return NETWORK_SOCKET_WAIT_FOR_EVENT;
}
packet_len = network_mysqld_proto_get_packet_len(&header);
packet_id = network_mysqld_proto_get_packet_id(&header);
/* move the packet from the raw queue to the recv-queue */
if ((packet = network_queue_pop_string(con->recv_queue_raw, packet_len + NET_HEADER_SIZE, NULL))) {
#ifdef NETWORK_DEBUG_TRACE_IO
/* to trace the data we received from the socket, enable this */
g_debug_hexdump(G_STRLOC, S(packet));
#endif
if (con->packet_id_is_reset) {
con->last_packet_id = packet_id;
con->packet_id_is_reset = FALSE;
} else if (packet_id != (guint8)(con->last_packet_id + 1)) {
g_critical("%s: received packet-id %d, but expected %d ... out of sync.",
G_STRLOC,
packet_id,
con->last_packet_id + 1);
return NETWORK_SOCKET_ERROR;
} else {
con->last_packet_id = packet_id;
}
network_queue_append(con->recv_queue, packet);
} else {
return NETWORK_SOCKET_WAIT_FOR_EVENT;
}
return NETWORK_SOCKET_SUCCESS;
}
/**
* read a MySQL packet from the socket
*
* the packet is added to the con->recv_queue and contains a full mysql packet
* with packet-header and everything
*/
network_socket_retval_t network_mysqld_read(chassis G_GNUC_UNUSED*chas, network_socket *con) {
switch (network_socket_read(con)) {
case NETWORK_SOCKET_WAIT_FOR_EVENT:
return NETWORK_SOCKET_WAIT_FOR_EVENT;
case NETWORK_SOCKET_ERROR:
return NETWORK_SOCKET_ERROR;
case NETWORK_SOCKET_SUCCESS:
break;
case NETWORK_SOCKET_ERROR_RETRY:
g_error("NETWORK_SOCKET_ERROR_RETRY wasn't expected");
break;
}
return network_mysqld_con_get_packet(chas, con);
}
network_socket_retval_t network_mysqld_write(chassis G_GNUC_UNUSED*chas, network_socket *con) {
network_socket_retval_t ret;
ret = network_socket_write(con, -1);
return ret;
}
/**
* call the hooks of the plugins for each state
*
* if the plugin doesn't implement a hook, we provide a default operation
*
* @param srv the global context
* @param con the connection context
* @param state state to handle
* @return NETWORK_SOCKET_SUCCESS on success
*/
network_socket_retval_t plugin_call(chassis *srv, network_mysqld_con *con, int state) {
network_socket_retval_t ret;
NETWORK_MYSQLD_PLUGIN_FUNC(func) = NULL;
switch (state) {
case CON_STATE_INIT:
func = con->plugins.con_init;
if (!func) { /* default implementation */
con->state = CON_STATE_CONNECT_SERVER;
}
break;
case CON_STATE_CONNECT_SERVER:
func = con->plugins.con_connect_server;
if (!func) { /* default implementation */
con->state = CON_STATE_READ_HANDSHAKE;
}
break;
case CON_STATE_SEND_HANDSHAKE:
func = con->plugins.con_send_handshake;
if (!func) { /* default implementation */
con->state = CON_STATE_READ_AUTH;
}
break;
case CON_STATE_READ_HANDSHAKE:
func = con->plugins.con_read_handshake;
break;
case CON_STATE_READ_AUTH:
func = con->plugins.con_read_auth;
break;
case CON_STATE_SEND_AUTH:
func = con->plugins.con_send_auth;
if (!func) { /* default implementation */
con->state = CON_STATE_READ_AUTH_RESULT;
}
break;
case CON_STATE_READ_AUTH_RESULT:
func = con->plugins.con_read_auth_result;
break;
case CON_STATE_SEND_AUTH_RESULT:
func = con->plugins.con_send_auth_result;
if (!func) { /* default implementation */
switch (con->auth_result_state) {
case MYSQLD_PACKET_OK:
con->state = CON_STATE_READ_QUERY;
break;
case MYSQLD_PACKET_ERR:
con->state = CON_STATE_ERROR;
break;
case MYSQLD_PACKET_EOF:
/**
* the MySQL 4.0 hash in a MySQL 4.1+ connection
*/
con->state = CON_STATE_READ_AUTH_OLD_PASSWORD;
break;
default:
g_error("%s.%d: unexpected state for SEND_AUTH_RESULT: %02x",
__FILE__, __LINE__,
con->auth_result_state);
}
}
break;
case CON_STATE_READ_AUTH_OLD_PASSWORD: {
/** move the packet to the send queue */
GString *packet;
GList *chunk;
network_socket *recv_sock, *send_sock;
recv_sock = con->client;
send_sock = con->server;
if (NULL == con->server) {
/**
* we have to auth against same backend as we did before
* but the user changed it
*/
g_message("%s.%d: (lua) read-auth-old-password failed as backend_ndx got reset.", __FILE__, __LINE__);
network_mysqld_con_send_error(con->client, C("(lua) read-auth-old-password failed as backend_ndx got reset."));
con->state = CON_STATE_SEND_ERROR;
break;
}
chunk = recv_sock->recv_queue->chunks->head;
packet = chunk->data;
/* we aren't finished yet */
network_queue_append(send_sock->send_queue, packet);
g_queue_delete_link(recv_sock->recv_queue->chunks, chunk);
/**
* send it out to the client
*/
con->state = CON_STATE_SEND_AUTH_OLD_PASSWORD;
break; }
case CON_STATE_SEND_AUTH_OLD_PASSWORD:
/**
* data is at the server, read the response next
*/
con->state = CON_STATE_READ_AUTH_RESULT;
break;
case CON_STATE_READ_QUERY:
func = con->plugins.con_read_query;
break;
case CON_STATE_READ_QUERY_RESULT:
func = con->plugins.con_read_query_result;
break;
case CON_STATE_SEND_QUERY_RESULT:
func = con->plugins.con_send_query_result;
if (!func) { /* default implementation */
con->state = CON_STATE_READ_QUERY;
}
break;
case CON_STATE_SEND_LOCAL_INFILE_DATA:
func = con->plugins.con_send_local_infile_data;
if (!func) { /* default implementation */
con->state = CON_STATE_READ_LOCAL_INFILE_RESULT;
}
break;
case CON_STATE_READ_LOCAL_INFILE_DATA:
func = con->plugins.con_read_local_infile_data;
if (!func) { /* the plugins have to implement this function to track LOAD DATA LOCAL INFILE handling work */
con->state = CON_STATE_ERROR;
}
break;
case CON_STATE_SEND_LOCAL_INFILE_RESULT:
func = con->plugins.con_send_local_infile_result;
if (!func) { /* default implementation */
con->state = CON_STATE_READ_QUERY;
}
break;
case CON_STATE_READ_LOCAL_INFILE_RESULT:
func = con->plugins.con_read_local_infile_result;
if (!func) { /* the plugins have to implement this function to track LOAD DATA LOCAL INFILE handling work */
con->state = CON_STATE_ERROR;
}
break;
case CON_STATE_ERROR:
g_debug("%s.%d: not executing plugin function in state CON_STATE_ERROR", __FILE__, __LINE__);
return NETWORK_SOCKET_SUCCESS;
default:
g_error("%s.%d: unhandled state: %d",
__FILE__, __LINE__,
state);
}
if (!func) return NETWORK_SOCKET_SUCCESS;
// LOCK_LUA(srv->priv->sc); /*remove lock*/
ret = (*func)(srv, con);
// UNLOCK_LUA(srv->priv->sc); /*remove lock*/
return ret;
}
/**
* reset the command-response parsing
*
* some commands needs state information and we have to
* reset the parsing as soon as we add a new command to the send-queue
*/
void network_mysqld_con_reset_command_response_state(network_mysqld_con *con) {
con->parse.command = -1;
if (con->parse.data && con->parse.data_free) {
con->parse.data_free(con->parse.data);
con->parse.data = NULL;
con->parse.data_free = NULL;
}
}
/**
* get the name of a connection state
*/
const char *network_mysqld_con_state_get_name(network_mysqld_con_state_t state) {
switch (state) {
case CON_STATE_INIT: return "CON_STATE_INIT";
case CON_STATE_CONNECT_SERVER: return "CON_STATE_CONNECT_SERVER";
case CON_STATE_READ_HANDSHAKE: return "CON_STATE_READ_HANDSHAKE";
case CON_STATE_SEND_HANDSHAKE: return "CON_STATE_SEND_HANDSHAKE";
case CON_STATE_READ_AUTH: return "CON_STATE_READ_AUTH";
case CON_STATE_SEND_AUTH: return "CON_STATE_SEND_AUTH";
case CON_STATE_READ_AUTH_OLD_PASSWORD: return "CON_STATE_READ_AUTH_OLD_PASSWORD";
case CON_STATE_SEND_AUTH_OLD_PASSWORD: return "CON_STATE_SEND_AUTH_OLD_PASSWORD";
case CON_STATE_READ_AUTH_RESULT: return "CON_STATE_READ_AUTH_RESULT";
case CON_STATE_SEND_AUTH_RESULT: return "CON_STATE_SEND_AUTH_RESULT";
case CON_STATE_READ_QUERY: return "CON_STATE_READ_QUERY";
case CON_STATE_SEND_QUERY: return "CON_STATE_SEND_QUERY";
case CON_STATE_READ_QUERY_RESULT: return "CON_STATE_READ_QUERY_RESULT";
case CON_STATE_SEND_QUERY_RESULT: return "CON_STATE_SEND_QUERY_RESULT";
case CON_STATE_READ_LOCAL_INFILE_DATA: return "CON_STATE_READ_LOCAL_INFILE_DATA";
case CON_STATE_SEND_LOCAL_INFILE_DATA: return "CON_STATE_SEND_LOCAL_INFILE_DATA";
case CON_STATE_READ_LOCAL_INFILE_RESULT: return "CON_STATE_READ_LOCAL_INFILE_RESULT";
case CON_STATE_SEND_LOCAL_INFILE_RESULT: return "CON_STATE_SEND_LOCAL_INFILE_RESULT";
case CON_STATE_CLOSE_CLIENT: return "CON_STATE_CLOSE_CLIENT";
case CON_STATE_CLOSE_SERVER: return "CON_STATE_CLOSE_SERVER";
case CON_STATE_ERROR: return "CON_STATE_ERROR";
case CON_STATE_SEND_ERROR: return "CON_STATE_SEND_ERROR";
}
return "unknown";
}
/**
* handle the different states of the MySQL protocol
*
* @param event_fd fd on which the event was fired
* @param events the event that was fired
* @param user_data the connection handle
*/
void network_mysqld_con_handle(int event_fd, short events, void *user_data) {
network_mysqld_con_state_t ostate;
network_mysqld_con *con = user_data;
chassis *srv = con->srv;
int retval;
network_socket_retval_t call_ret;
g_assert(srv);
g_assert(con);
if (events == EV_READ) {
int b = -1;
/**
* check how much data there is to read
*
* ioctl()
* - returns 0 if connection is closed
* - or -1 and ECONNRESET on solaris
* or -1 and EPIPE on HP/UX
*/
if (ioctl(event_fd, FIONREAD, &b)) {
switch (errno) {
case E_NET_CONNRESET: /* solaris */
case EPIPE: /* hp/ux */
if (con->client && event_fd == con->client->fd) {
/* the client closed the connection, let's keep the server side open */
con->state = CON_STATE_CLOSE_CLIENT;
} else if (con->server && event_fd == con->server->fd && con->com_quit_seen) {
con->state = CON_STATE_CLOSE_SERVER;
} else {
/* server side closed on use, oops, close both sides */
con->state = CON_STATE_ERROR;
}
break;
default:
g_critical("ioctl(%d, FIONREAD, ...) failed: %s", event_fd, g_strerror(errno));
con->state = CON_STATE_ERROR;
break;
}
} else if (b != 0) {
if (con->client && event_fd == con->client->fd) {
con->client->to_read = b;
} else if (con->server && event_fd == con->server->fd) {
con->server->to_read = b;
} else {
g_error("%s.%d: neither nor", __FILE__, __LINE__);
}
} else { /* Linux */
if (con->client && event_fd == con->client->fd) {
/* the client closed the connection, let's keep the server side open */
con->state = CON_STATE_CLOSE_CLIENT;
} else if (con->server && event_fd == con->server->fd && con->com_quit_seen) {
con->state = CON_STATE_CLOSE_SERVER;
} else {
/* server side closed on use, oops, close both sides */
con->state = CON_STATE_ERROR;
}
}
}
#define WAIT_FOR_EVENT(ev_struct, ev_type, timeout) \
event_set(&(ev_struct->event), ev_struct->fd, ev_type, network_mysqld_con_handle, user_data); \
chassis_event_add_self(srv, &(ev_struct->event), timeout);
/**
* loop on the same connection as long as we don't end up in a stable state
*/
if (event_fd != -1) {
NETWORK_MYSQLD_CON_TRACK_TIME(con, "wait_for_event::done");
} else {
NETWORK_MYSQLD_CON_TRACK_TIME(con, "con_handle_start");
}
do {
ostate = con->state;
#ifdef NETWORK_DEBUG_TRACE_STATE_CHANGES
/* if you need the state-change information without dtrace, enable this */
g_debug("%s: [%d] %s",
G_STRLOC,
getpid(),
network_mysqld_con_state_get_name(con->state));
#endif
MYSQLPROXY_STATE_CHANGE(event_fd, events, con->state);
switch (con->state) {
case CON_STATE_ERROR:
/* we can't go on, close the connection */
/*
{
gchar *which_connection = "a";
if (con->server && event_fd == con->server->fd) {
which_connection = "server";
} else if (con->client && event_fd == con->client->fd) {
which_connection = "client";
}
g_debug("[%s]: error on %s connection (fd: %d event: %d). closing client connection.",
G_STRLOC, which_connection, event_fd, events);
}
*/
plugin_call_cleanup(srv, con);
network_mysqld_con_free(con);
con = NULL;
return;
case CON_STATE_CLOSE_CLIENT:
case CON_STATE_CLOSE_SERVER:
/* FIXME: this comment has nothing to do with reality...
* the server connection is still fine,
* let's keep it open for reuse */
plugin_call_cleanup(srv, con);
network_mysqld_con_free(con);
con = NULL;
return;
case CON_STATE_INIT:
/* if we are a proxy ask the remote server for the hand-shake packet
* if not, we generate one */
switch (plugin_call(srv, con, con->state)) {
case NETWORK_SOCKET_SUCCESS:
break;
default:
/**
* no luck, let's close the connection
*/
g_critical("%s.%d: plugin_call(CON_STATE_INIT) != NETWORK_SOCKET_SUCCESS", __FILE__, __LINE__);
con->state = CON_STATE_ERROR;
break;
}
break;
case CON_STATE_CONNECT_SERVER:
if (events == EV_TIMEOUT) {
network_mysqld_con_lua_t *st = con->plugin_con_state;
if (st->backend->state != BACKEND_STATE_OFFLINE) st->backend->state = BACKEND_STATE_DOWN;
network_socket_free(con->server);
con->server = NULL;
ostate = CON_STATE_INIT;
g_warning("timeout in connecting server");
break;
}
switch ((retval = plugin_call(srv, con, con->state))) {
case NETWORK_SOCKET_SUCCESS:
/**
* hmm, if this is success and we have something in the clients send-queue
* we just send it out ... who needs a server ? */
if ((con->client != NULL && con->client->send_queue->chunks->length > 0) &&
con->server == NULL) {
/* we want to send something to the client */
con->state = CON_STATE_SEND_HANDSHAKE;