2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Use and distribution licensed under the BSD license. See
8
* the COPYING file in this directory for full text.
16
#include <libdrizzle/drizzle_client.h>
17
#include <libdrizzle/drizzle_server.h>
19
#define DRIZZLE_RETURN_CHECK(__ret, __function, __drizzle) \
21
if ((__ret) != DRIZZLE_RETURN_OK) \
22
DRIZZLE_RETURN_ERROR(__function, __drizzle) \
25
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
27
printf(__function ":%s\n", drizzle_error(__drizzle)); \
31
static void proxy(drizzle_st *drizzle, drizzle_con_st *server,
32
drizzle_con_st *client, drizzle_result_st *server_result,
33
drizzle_result_st *client_result, drizzle_column_st *column);
35
int main(int argc, char *argv[])
39
const char *server_host= NULL;
40
const char *client_host= NULL;
41
bool server_mysql= false;
42
bool client_mysql= false;
43
in_port_t server_port= 0;
44
in_port_t client_port= 0;
45
drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
48
drizzle_con_st con_listen;
49
drizzle_con_st server;
50
drizzle_con_st client;
51
drizzle_result_st server_result;
52
drizzle_result_st client_result;
53
drizzle_column_st column;
55
while((c = getopt(argc, argv, "c:h:H:mMp:P:v")) != -1)
60
count= (uint32_t)atoi(optarg);
80
server_port= (in_port_t)atoi(optarg);
84
client_port= (in_port_t)atoi(optarg);
92
printf("\nusage: %s [-c <count>] [-h <host>] [-H <host>] [-m] [-M] "
93
"[-p <port>] [-p <port>] [-v]\n", argv[0]);
94
printf("\t-c <count> - Number of connections to accept before exiting\n");
95
printf("\t-h <host> - Host to listen on\n");
96
printf("\t-H <host> - Host to connect to\n");
97
printf("\t-m - Use MySQL protocol for incoming connections\n");
98
printf("\t-M - Use MySQL protocol for outgoing connectionsn\n");
99
printf("\t-p <port> - Port to listen on\n");
100
printf("\t-P <port> - Port to connect to\n");
101
printf("\t-v - Increase verbosity level\n");
106
if (drizzle_create(&drizzle) == NULL)
108
printf("drizzle_create:NULL\n");
112
drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS);
113
drizzle_set_verbose(&drizzle, verbose);
115
if (drizzle_con_create(&drizzle, &con_listen) == NULL)
117
printf("drizzle_con_create:NULL\n");
121
drizzle_con_add_options(&con_listen, DRIZZLE_CON_LISTEN);
122
drizzle_con_set_tcp(&con_listen, server_host, server_port);
125
drizzle_con_add_options(&con_listen, DRIZZLE_CON_MYSQL);
127
if (drizzle_con_listen(&con_listen) != DRIZZLE_RETURN_OK)
129
printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle));
135
(void)drizzle_con_accept(&drizzle, &server, &ret);
136
if (ret != DRIZZLE_RETURN_OK)
138
printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle));
142
if (drizzle_con_create(&drizzle, &client) == NULL)
144
printf("drizzle_con_create:NULL\n");
148
drizzle_con_add_options(&client,
149
DRIZZLE_CON_RAW_PACKET | DRIZZLE_CON_RAW_SCRAMBLE);
151
drizzle_con_add_options(&client, DRIZZLE_CON_MYSQL);
152
drizzle_con_set_tcp(&client, client_host, client_port);
154
ret= drizzle_con_connect(&client);
155
if (ret != DRIZZLE_RETURN_OK)
157
printf("drizzle_con_connect:%s\n", drizzle_error(&drizzle));
161
proxy(&drizzle, &server, &client, &server_result, &client_result, &column);
163
drizzle_con_free(&client);
164
drizzle_con_free(&server);
175
drizzle_con_free(&con_listen);
176
drizzle_free(&drizzle);
181
static void proxy(drizzle_st *drizzle, drizzle_con_st *server,
182
drizzle_con_st *client, drizzle_result_st *server_result,
183
drizzle_result_st *client_result, drizzle_column_st *column)
185
drizzle_return_t ret;
186
drizzle_command_t command;
193
drizzle_field_t field;
195
/* Handshake packets. */
196
ret= drizzle_handshake_server_read(client);
197
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_read", drizzle)
199
drizzle_con_copy_handshake(server, client);
201
ret= drizzle_handshake_server_write(server);
202
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle)
204
ret= drizzle_handshake_client_read(server);
205
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle)
207
drizzle_con_copy_handshake(client, server);
209
ret= drizzle_handshake_client_write(client);
210
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_write", drizzle)
212
(void)drizzle_result_read(client, client_result, &ret);
213
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_read", drizzle)
215
drizzle_con_set_status(server, drizzle_con_status(client));
217
if (drizzle_result_clone(server, server_result, client_result) == NULL)
218
DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle)
220
ret= drizzle_result_write(server, server_result, true);
221
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
223
if (drizzle_result_error_code(client_result) != 0 ||
224
drizzle_result_eof(client_result))
226
/* There was a handshake or authentication error. */
230
drizzle_con_add_options(client, DRIZZLE_CON_READY);
235
drizzle_result_free(server_result);
236
drizzle_result_free(client_result);
240
data= drizzle_con_command_read(server, &command, &offset, &size, &total,
242
if (ret == DRIZZLE_RETURN_LOST_CONNECTION)
245
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_read", drizzle)
247
(void)drizzle_con_command_write(client, NULL, command, data, size, total,
249
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_write", drizzle)
251
if ((offset + size) == total)
255
if (command == DRIZZLE_COMMAND_QUIT)
257
else if (command == DRIZZLE_COMMAND_FIELD_LIST)
259
if (drizzle_result_create(client, client_result) == NULL)
260
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
262
if (drizzle_result_create(server, server_result) == NULL)
263
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
267
(void)drizzle_result_read(client, client_result, &ret);
268
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_read", drizzle)
270
drizzle_con_set_status(server, drizzle_con_status(client));
271
if (drizzle_result_clone(server, server_result, client_result) == NULL)
272
DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle)
274
if (drizzle_result_column_count(client_result) == 0)
276
/* Simple result with no column, row, or field data. */
277
ret= drizzle_result_write(server, server_result, true);
278
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
283
ret= drizzle_result_write(server, server_result, false);
284
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
290
if (drizzle_column_read(client_result, column, &ret) == NULL)
292
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle)
296
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle)
298
ret= drizzle_column_write(server_result, column);
299
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
301
drizzle_column_free(column);
304
drizzle_con_set_status(server, drizzle_con_status(client));
305
drizzle_result_set_eof(server_result, true);
307
if (command == DRIZZLE_COMMAND_FIELD_LIST)
309
ret= drizzle_result_write(server, server_result, true);
310
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
315
ret= drizzle_result_write(server, server_result, false);
316
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
322
row= drizzle_row_read(client_result, &ret);
323
DRIZZLE_RETURN_CHECK(ret, "drizzle_row_read", drizzle)
328
drizzle_result_set_row_size(server_result,
329
drizzle_result_row_size(client_result));
331
ret= drizzle_row_write(server_result);
332
DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle)
336
while (row_break == false)
338
field= drizzle_field_read(client_result, &offset, &size, &total, &ret);
339
if (ret == DRIZZLE_RETURN_ROW_END)
341
else if (ret == DRIZZLE_RETURN_ROW_BREAK)
349
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_read", drizzle)
351
ret= drizzle_field_write(server_result, field, size, total);
352
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
356
ret= drizzle_result_write(server, server_result, true);
357
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)