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