51
51
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
53
fprintf(stderr, __function ":%s\n", drizzle_error(__drizzle)); \
53
printf(__function ":%s\n", drizzle_error(__drizzle)); \
57
static void proxy(drizzle_st *drizzle,
58
drizzle_con_st *server,
59
drizzle_con_st *client,
60
drizzle_column_st *column);
57
static void proxy(drizzle_st *drizzle, drizzle_con_st *server,
58
drizzle_con_st *client, drizzle_result_st *server_result,
59
drizzle_result_st *client_result, drizzle_column_st *column);
62
61
int main(int argc, char *argv[])
135
fprintf(stderr, "\nusage: %s [-c <count>] [-h <host>] [-H <host>] [-m] [-M] "
138
printf("\nusage: %s [-c <count>] [-h <host>] [-H <host>] [-m] [-M] "
136
139
"[-p <port>] [-p <port>] [-v]\n", argv[0]);
137
fprintf(stderr, "\t-c <count> - Number of connections to accept before exiting\n");
138
fprintf(stderr, "\t-h <host> - Host to listen on\n");
139
fprintf(stderr, "\t-H <host> - Host to connect to\n");
140
fprintf(stderr, "\t-m - Use MySQL protocol for incoming connections\n");
141
fprintf(stderr, "\t-M - Use MySQL protocol for outgoing connectionsn\n");
142
fprintf(stderr, "\t-p <port> - Port to listen on\n");
143
fprintf(stderr, "\t-P <port> - Port to connect to\n");
144
fprintf(stderr, "\t-v - Increase verbosity level\n");
140
printf("\t-c <count> - Number of connections to accept before exiting\n");
141
printf("\t-h <host> - Host to listen on\n");
142
printf("\t-H <host> - Host to connect to\n");
143
printf("\t-m - Use MySQL protocol for incoming connections\n");
144
printf("\t-M - Use MySQL protocol for outgoing connectionsn\n");
145
printf("\t-p <port> - Port to listen on\n");
146
printf("\t-P <port> - Port to connect to\n");
147
printf("\t-v - Increase verbosity level\n");
149
if ((drizzle= drizzle_create()) == NULL)
152
if (drizzle_create(&drizzle) == NULL)
151
fprintf(stderr, "drizzle_create:NULL\n");
154
printf("drizzle_create:NULL\n");
155
drizzle_set_option(drizzle, DRIZZLE_FREE_OBJECTS, true);
156
drizzle_set_verbose(drizzle, verbose);
158
drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS);
159
drizzle_set_verbose(&drizzle, verbose);
158
drizzle_con_st *con_listen;
159
if ((con_listen= drizzle_con_create(drizzle)) == NULL)
161
if (drizzle_con_create(&drizzle, con_listen) == NULL)
161
fprintf(stderr, "drizzle_con_create:NULL\n");
163
printf("drizzle_con_create:NULL\n");
166
168
drizzle_con_set_tcp(con_listen, server_host, server_port);
168
170
if (server_mysql)
170
171
drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
173
173
if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
175
fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(drizzle));
175
printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle));
181
drizzle_con_st *server= drizzle_con_accept(drizzle, &ret);
181
(void)drizzle_con_accept(&drizzle, server, &ret);
182
182
if (ret != DRIZZLE_RETURN_OK)
184
fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(drizzle));
184
printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle));
188
drizzle_con_st *client;
189
if ((client= drizzle_con_create(drizzle)) == NULL)
188
if (drizzle_con_create(&drizzle, client) == NULL)
191
fprintf(stderr, "drizzle_con_create:NULL\n");
190
printf("drizzle_con_create:NULL\n");
195
194
drizzle_con_add_options(client,
196
195
DRIZZLE_CON_RAW_PACKET | DRIZZLE_CON_RAW_SCRAMBLE);
197
196
if (client_mysql)
199
197
drizzle_con_add_options(client, DRIZZLE_CON_MYSQL);
201
198
drizzle_con_set_tcp(client, client_host, client_port);
203
200
ret= drizzle_con_connect(client);
204
201
if (ret != DRIZZLE_RETURN_OK)
206
fprintf(stderr, "drizzle_con_connect:%s\n", drizzle_error(drizzle));
203
printf("drizzle_con_connect:%s\n", drizzle_error(&drizzle));
210
proxy(drizzle, server, client, &column);
207
proxy(&drizzle, server, client, &server_result, &client_result, &column);
212
209
drizzle_con_free(client);
213
210
drizzle_con_free(server);
244
245
/* Handshake packets. */
245
246
ret= drizzle_handshake_server_read(client);
246
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_read", drizzle);
247
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_read", drizzle)
248
249
drizzle_con_copy_handshake(server, client);
250
251
ret= drizzle_handshake_server_write(server);
251
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle);
252
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle)
253
254
ret= drizzle_handshake_client_read(server);
254
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle);
255
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle)
256
257
drizzle_con_copy_handshake(client, server);
258
259
ret= drizzle_handshake_client_write(client);
259
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_write", drizzle);
260
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_write", drizzle)
261
drizzle_result_st *client_result= drizzle_result_read(client, NULL, &ret);
262
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_read", drizzle);
262
(void)drizzle_result_read(client, client_result, &ret);
263
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_read", drizzle)
264
265
drizzle_con_set_status(server, drizzle_con_status(client));
266
drizzle_result_st *server_result;
267
if ((server_result= drizzle_result_clone(server, client_result)) == NULL)
269
DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle);
267
if (drizzle_result_clone(server, server_result, client_result) == NULL)
268
DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle)
272
270
ret= drizzle_result_write(server, server_result, true);
273
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle);
271
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
275
273
if (drizzle_result_error_code(client_result) != 0 ||
276
274
drizzle_result_eof(client_result))
292
data= (uint8_t *)drizzle_con_command_read(server, &command, &offset, &size, &total, &ret);
290
data= (uint8_t *)drizzle_con_command_read(server, &command, &offset, &size, &total,
293
292
if (ret == DRIZZLE_RETURN_LOST_CONNECTION)
298
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_read", drizzle);
300
(void)drizzle_con_command_write(client, NULL, command, data, size, total, &ret);
301
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_write", drizzle);
295
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_read", drizzle)
297
(void)drizzle_con_command_write(client, NULL, command, data, size, total,
299
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_write", drizzle)
303
301
if ((offset + size) == total)
307
305
if (command == DRIZZLE_COMMAND_QUIT)
311
307
else if (command == DRIZZLE_COMMAND_FIELD_LIST)
313
if ((client_result= drizzle_result_create(client)) == NULL)
309
if (drizzle_result_create(client, client_result) == NULL)
315
310
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
318
if ((server_result= drizzle_result_create(server)) == NULL)
320
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle);
312
if (drizzle_result_create(server, server_result) == NULL)
313
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
325
317
(void)drizzle_result_read(client, client_result, &ret);
326
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_read", drizzle);
318
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_read", drizzle)
328
320
drizzle_con_set_status(server, drizzle_con_status(client));
329
if ((server_result= drizzle_result_clone(server, client_result)) == NULL)
331
DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle);
321
if (drizzle_result_clone(server, server_result, client_result) == NULL)
322
DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle)
334
324
if (drizzle_result_column_count(client_result) == 0)
336
326
/* Simple result with no column, row, or field data. */
337
327
ret= drizzle_result_write(server, server_result, true);
338
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle);
328
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
343
333
ret= drizzle_result_write(server, server_result, false);
344
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle);
334
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
350
340
if (drizzle_column_read(client_result, column, &ret) == NULL)
352
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle);
342
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle)
356
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle);
346
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle)
358
348
ret= drizzle_column_write(server_result, column);
359
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle);
349
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
361
351
drizzle_column_free(column);
367
357
if (command == DRIZZLE_COMMAND_FIELD_LIST)
369
359
ret= drizzle_result_write(server, server_result, true);
370
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle);
360
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
375
365
ret= drizzle_result_write(server, server_result, false);
376
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle);
366
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
382
372
row= drizzle_row_read(client_result, &ret);
383
DRIZZLE_RETURN_CHECK(ret, "drizzle_row_read", drizzle);
373
DRIZZLE_RETURN_CHECK(ret, "drizzle_row_read", drizzle)
410
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_read", drizzle);
399
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_read", drizzle)
413
401
ret= drizzle_field_write(server_result, field, size, total);
414
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle);
402
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
418
406
ret= drizzle_result_write(server, server_result, true);
419
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle);
407
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)