2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are
11
* * Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* * Redistributions in binary form must reproduce the above
15
* copyright notice, this list of conditions and the following disclaimer
16
* in the documentation and/or other materials provided with the
19
* * The names of its contributors may not be used to endorse or
20
* promote products derived from this software without specific prior
23
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
#include <libdrizzle/drizzle_client.h>
44
#include <libdrizzle/drizzle_server.h>
46
#define DRIZZLE_RETURN_CHECK(__ret, __function, __drizzle) \
48
if ((__ret) != DRIZZLE_RETURN_OK) \
49
DRIZZLE_RETURN_ERROR(__function, __drizzle) \
52
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
54
printf(__function ":%s\n", drizzle_error(__drizzle)); \
58
static void proxy(drizzle_st *drizzle, drizzle_con_st *server,
59
drizzle_con_st *client, drizzle_result_st *server_result,
60
drizzle_result_st *client_result, drizzle_column_st *column);
62
int main(int argc, char *argv[])
66
const char *server_host= NULL;
67
const char *client_host= NULL;
68
bool server_mysql= false;
69
bool client_mysql= false;
70
in_port_t server_port= 0;
71
in_port_t client_port= 0;
72
drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
75
drizzle_con_st *con_listen= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
76
drizzle_con_st *server= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
77
drizzle_con_st *client= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
78
drizzle_result_st server_result;
79
drizzle_result_st client_result;
80
drizzle_column_st column;
82
while((c = getopt(argc, argv, "c:h:H:mMp:P:v")) != -1)
87
count= (uint32_t)atoi(optarg);
107
server_port= (in_port_t)atoi(optarg);
111
client_port= (in_port_t)atoi(optarg);
119
printf("\nusage: %s [-c <count>] [-h <host>] [-H <host>] [-m] [-M] "
120
"[-p <port>] [-p <port>] [-v]\n", argv[0]);
121
printf("\t-c <count> - Number of connections to accept before exiting\n");
122
printf("\t-h <host> - Host to listen on\n");
123
printf("\t-H <host> - Host to connect to\n");
124
printf("\t-m - Use MySQL protocol for incoming connections\n");
125
printf("\t-M - Use MySQL protocol for outgoing connectionsn\n");
126
printf("\t-p <port> - Port to listen on\n");
127
printf("\t-P <port> - Port to connect to\n");
128
printf("\t-v - Increase verbosity level\n");
133
if (drizzle_create(&drizzle) == NULL)
135
printf("drizzle_create:NULL\n");
139
drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS);
140
drizzle_set_verbose(&drizzle, verbose);
142
if (drizzle_con_create(&drizzle, con_listen) == NULL)
144
printf("drizzle_con_create:NULL\n");
148
drizzle_con_add_options(con_listen, DRIZZLE_CON_LISTEN);
149
drizzle_con_set_tcp(con_listen, server_host, server_port);
152
drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
154
if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
156
printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle));
162
(void)drizzle_con_accept(&drizzle, server, &ret);
163
if (ret != DRIZZLE_RETURN_OK)
165
printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle));
169
if (drizzle_con_create(&drizzle, client) == NULL)
171
printf("drizzle_con_create:NULL\n");
175
drizzle_con_add_options(client,
176
DRIZZLE_CON_RAW_PACKET | DRIZZLE_CON_RAW_SCRAMBLE);
178
drizzle_con_add_options(client, DRIZZLE_CON_MYSQL);
179
drizzle_con_set_tcp(client, client_host, client_port);
181
ret= drizzle_con_connect(client);
182
if (ret != DRIZZLE_RETURN_OK)
184
printf("drizzle_con_connect:%s\n", drizzle_error(&drizzle));
188
proxy(&drizzle, server, client, &server_result, &client_result, &column);
190
drizzle_con_free(client);
191
drizzle_con_free(server);
202
drizzle_con_free(con_listen);
203
drizzle_free(&drizzle);
212
static void proxy(drizzle_st *drizzle, drizzle_con_st *server,
213
drizzle_con_st *client, drizzle_result_st *server_result,
214
drizzle_result_st *client_result, drizzle_column_st *column)
216
drizzle_return_t ret;
217
drizzle_command_t command;
224
drizzle_field_t field;
226
/* Handshake packets. */
227
ret= drizzle_handshake_server_read(client);
228
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_read", drizzle)
230
drizzle_con_copy_handshake(server, client);
232
ret= drizzle_handshake_server_write(server);
233
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle)
235
ret= drizzle_handshake_client_read(server);
236
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle)
238
drizzle_con_copy_handshake(client, server);
240
ret= drizzle_handshake_client_write(client);
241
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_write", drizzle)
243
(void)drizzle_result_read(client, client_result, &ret);
244
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_read", drizzle)
246
drizzle_con_set_status(server, drizzle_con_status(client));
248
if (drizzle_result_clone(server, server_result, client_result) == NULL)
249
DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle)
251
ret= drizzle_result_write(server, server_result, true);
252
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
254
if (drizzle_result_error_code(client_result) != 0 ||
255
drizzle_result_eof(client_result))
257
/* There was a handshake or authentication error. */
261
drizzle_con_add_options(client, DRIZZLE_CON_READY);
266
drizzle_result_free(server_result);
267
drizzle_result_free(client_result);
271
data= drizzle_con_command_read(server, &command, &offset, &size, &total,
273
if (ret == DRIZZLE_RETURN_LOST_CONNECTION)
276
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_read", drizzle)
278
(void)drizzle_con_command_write(client, NULL, command, data, size, total,
280
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_write", drizzle)
282
if ((offset + size) == total)
286
if (command == DRIZZLE_COMMAND_QUIT)
288
else if (command == DRIZZLE_COMMAND_FIELD_LIST)
290
if (drizzle_result_create(client, client_result) == NULL)
291
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
293
if (drizzle_result_create(server, server_result) == NULL)
294
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
298
(void)drizzle_result_read(client, client_result, &ret);
299
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_read", drizzle)
301
drizzle_con_set_status(server, drizzle_con_status(client));
302
if (drizzle_result_clone(server, server_result, client_result) == NULL)
303
DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle)
305
if (drizzle_result_column_count(client_result) == 0)
307
/* Simple result with no column, row, or field data. */
308
ret= drizzle_result_write(server, server_result, true);
309
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
314
ret= drizzle_result_write(server, server_result, false);
315
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
321
if (drizzle_column_read(client_result, column, &ret) == NULL)
323
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle)
327
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle)
329
ret= drizzle_column_write(server_result, column);
330
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
332
drizzle_column_free(column);
335
drizzle_con_set_status(server, drizzle_con_status(client));
336
drizzle_result_set_eof(server_result, true);
338
if (command == DRIZZLE_COMMAND_FIELD_LIST)
340
ret= drizzle_result_write(server, server_result, true);
341
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
346
ret= drizzle_result_write(server, server_result, false);
347
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
353
row= drizzle_row_read(client_result, &ret);
354
DRIZZLE_RETURN_CHECK(ret, "drizzle_row_read", drizzle)
359
drizzle_result_set_row_size(server_result,
360
drizzle_result_row_size(client_result));
362
ret= drizzle_row_write(server_result);
363
DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle)
367
while (row_break == false)
369
field= drizzle_field_read(client_result, &offset, &size, &total, &ret);
370
if (ret == DRIZZLE_RETURN_ROW_END)
372
else if (ret == DRIZZLE_RETURN_ROW_BREAK)
380
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_read", drizzle)
382
ret= drizzle_field_write(server_result, field, size, total);
383
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
387
ret= drizzle_result_write(server, server_result, true);
388
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)