~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/proxy.cc

  • Committer: Mark Atwood
  • Date: 2011-11-22 17:04:41 UTC
  • mfrom: (2462.1.3 drizzle-include)
  • Revision ID: me@mark.atwood.name-20111122170441-5dehm0e0ax20z19b
mergeĀ lp:~brianaker/drizzle/fedora-16-fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
 
51
51
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
52
52
{ \
53
 
  fprintf(stderr, __function ":%s\n", drizzle_error(__drizzle)); \
 
53
  printf(__function ":%s\n", drizzle_error(__drizzle)); \
54
54
  return; \
55
55
}
56
56
 
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);
61
60
 
62
61
int main(int argc, char *argv[])
63
62
{
71
70
  in_port_t client_port= 0;
72
71
  drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
73
72
  drizzle_return_t ret;
74
 
  drizzle_st *drizzle;
 
73
  drizzle_st drizzle;
 
74
  drizzle_con_st *con_listen= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
 
75
  drizzle_con_st *server= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
 
76
  drizzle_con_st *client= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
 
77
  drizzle_result_st server_result;
75
78
  drizzle_result_st client_result;
76
79
  drizzle_column_st column;
77
80
 
132
135
      break;
133
136
 
134
137
    default:
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");
145
148
      return 1;
146
149
    }
147
150
  }
148
151
 
149
 
  if ((drizzle= drizzle_create()) == NULL)
 
152
  if (drizzle_create(&drizzle) == NULL)
150
153
  {
151
 
    fprintf(stderr, "drizzle_create:NULL\n");
 
154
    printf("drizzle_create:NULL\n");
152
155
    return 1;
153
156
  }
154
157
 
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);
157
160
 
158
 
  drizzle_con_st *con_listen;
159
 
  if ((con_listen= drizzle_con_create(drizzle)) == NULL)
 
161
  if (drizzle_con_create(&drizzle, con_listen) == NULL)
160
162
  {
161
 
    fprintf(stderr, "drizzle_con_create:NULL\n");
 
163
    printf("drizzle_con_create:NULL\n");
162
164
    return 1;
163
165
  }
164
166
 
166
168
  drizzle_con_set_tcp(con_listen, server_host, server_port);
167
169
 
168
170
  if (server_mysql)
169
 
  {
170
171
    drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
171
 
  }
172
172
 
173
173
  if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
174
174
  {
175
 
    fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(drizzle));
 
175
    printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle));
176
176
    return 1;
177
177
  }
178
178
 
179
179
  while (1)
180
180
  {
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)
183
183
    {
184
 
      fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(drizzle));
 
184
      printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle));
185
185
      return 1;
186
186
    }
187
187
 
188
 
    drizzle_con_st *client;
189
 
    if ((client= drizzle_con_create(drizzle)) == NULL)
 
188
    if (drizzle_con_create(&drizzle, client) == NULL)
190
189
    {
191
 
      fprintf(stderr, "drizzle_con_create:NULL\n");
 
190
      printf("drizzle_con_create:NULL\n");
192
191
      return 1;
193
192
    }
194
193
 
195
194
    drizzle_con_add_options(client,
196
195
                            DRIZZLE_CON_RAW_PACKET | DRIZZLE_CON_RAW_SCRAMBLE);
197
196
    if (client_mysql)
198
 
    {
199
197
      drizzle_con_add_options(client, DRIZZLE_CON_MYSQL);
200
 
    }
201
198
    drizzle_con_set_tcp(client, client_host, client_port);
202
199
 
203
200
    ret= drizzle_con_connect(client);
204
201
    if (ret != DRIZZLE_RETURN_OK)
205
202
    {
206
 
      fprintf(stderr, "drizzle_con_connect:%s\n", drizzle_error(drizzle));
 
203
      printf("drizzle_con_connect:%s\n", drizzle_error(&drizzle));
207
204
      return 1;
208
205
    }
209
206
 
210
 
    proxy(drizzle, server, client, &column);
 
207
    proxy(&drizzle, server, client, &server_result, &client_result, &column);
211
208
 
212
209
    drizzle_con_free(client);
213
210
    drizzle_con_free(server);
222
219
  }
223
220
 
224
221
  drizzle_con_free(con_listen);
225
 
  drizzle_free(drizzle);
 
222
  drizzle_free(&drizzle);
 
223
 
 
224
  free(con_listen);
 
225
  free(server);
 
226
  free(client);
226
227
 
227
228
  return 0;
228
229
}
229
230
 
230
231
static void proxy(drizzle_st *drizzle, drizzle_con_st *server,
231
 
                  drizzle_con_st *client,
232
 
                  drizzle_column_st *column)
 
232
                  drizzle_con_st *client, drizzle_result_st *server_result,
 
233
                  drizzle_result_st *client_result, drizzle_column_st *column)
233
234
{
234
235
  drizzle_return_t ret;
235
236
  drizzle_command_t command;
243
244
 
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)
247
248
 
248
249
  drizzle_con_copy_handshake(server, client);
249
250
 
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)
252
253
 
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)
255
256
 
256
257
  drizzle_con_copy_handshake(client, server);
257
258
 
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)
260
261
 
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)
263
264
 
264
265
  drizzle_con_set_status(server, drizzle_con_status(client));
265
266
 
266
 
  drizzle_result_st *server_result;
267
 
  if ((server_result= drizzle_result_clone(server, client_result)) == NULL)
268
 
  {
269
 
    DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle);
270
 
  }
 
267
  if (drizzle_result_clone(server, server_result, client_result) == NULL)
 
268
    DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle)
271
269
 
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)
274
272
 
275
273
  if (drizzle_result_error_code(client_result) != 0 ||
276
274
      drizzle_result_eof(client_result))
289
287
 
290
288
    while (1)
291
289
    {
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,
 
291
                                     &ret);
293
292
      if (ret == DRIZZLE_RETURN_LOST_CONNECTION)
294
 
      {
295
293
        return;
296
 
      }
297
 
 
298
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_read", drizzle);
299
 
 
300
 
      (void)drizzle_con_command_write(client, NULL, command, data, size, total, &ret);
301
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_write", drizzle);
 
294
 
 
295
      DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_read", drizzle)
 
296
 
 
297
      (void)drizzle_con_command_write(client, NULL, command, data, size, total,
 
298
                                      &ret);
 
299
      DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_write", drizzle)
302
300
 
303
301
      if ((offset + size) == total)
304
302
        break;
305
303
    }
306
304
 
307
305
    if (command == DRIZZLE_COMMAND_QUIT)
308
 
    {
309
306
      return;
310
 
    }
311
307
    else if (command == DRIZZLE_COMMAND_FIELD_LIST)
312
308
    {
313
 
      if ((client_result= drizzle_result_create(client)) == NULL)
314
 
      {
 
309
      if (drizzle_result_create(client, client_result) == NULL)
315
310
        DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
316
 
      }
317
311
 
318
 
      if ((server_result= drizzle_result_create(server)) == NULL)
319
 
      {
320
 
        DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle);
321
 
      }
 
312
      if (drizzle_result_create(server, server_result) == NULL)
 
313
        DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
322
314
    }
323
315
    else
324
316
    {
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)
327
319
 
328
320
      drizzle_con_set_status(server, drizzle_con_status(client));
329
 
      if ((server_result= drizzle_result_clone(server, client_result)) == NULL)
330
 
      {
331
 
        DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle);
332
 
      }
 
321
      if (drizzle_result_clone(server, server_result, client_result) == NULL)
 
322
        DRIZZLE_RETURN_ERROR("drizzle_result_clone", drizzle)
333
323
 
334
324
      if (drizzle_result_column_count(client_result) == 0)
335
325
      {
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)
339
329
 
340
330
        continue;
341
331
      }
342
332
 
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)
345
335
    }
346
336
 
347
337
    /* Columns. */
349
339
    {
350
340
      if (drizzle_column_read(client_result, column, &ret) == NULL)
351
341
      {
352
 
        DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle);
 
342
        DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle)
353
343
        break;
354
344
      }
355
345
 
356
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle);
 
346
      DRIZZLE_RETURN_CHECK(ret, "drizzle_column_read", drizzle)
357
347
 
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)
360
350
 
361
351
      drizzle_column_free(column);
362
352
    }
367
357
    if (command == DRIZZLE_COMMAND_FIELD_LIST)
368
358
    {
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)
371
361
      continue;
372
362
    }
373
363
    else
374
364
    {
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)
377
367
    }
378
368
 
379
369
    /* Rows. */
380
370
    while (1)
381
371
    {
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)
384
374
 
385
375
      if (row == 0)
386
376
        break;
389
379
                                  drizzle_result_row_size(client_result));
390
380
 
391
381
      ret= drizzle_row_write(server_result);
392
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle);
 
382
      DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle)
393
383
 
394
384
      /* Fields. */
395
385
      row_break= false;
406
396
          row_break= true;
407
397
        }
408
398
        else
409
 
        {
410
 
          DRIZZLE_RETURN_CHECK(ret, "drizzle_field_read", drizzle);
411
 
        }
 
399
          DRIZZLE_RETURN_CHECK(ret, "drizzle_field_read", drizzle)
412
400
 
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)
415
403
      }
416
404
    }
417
405
 
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)
420
408
  }
421
409
}