~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/proxy.cc

  • Committer: Mark Atwood
  • Date: 2011-11-30 07:06:47 UTC
  • mfrom: (2463.1.6 drizzle-include)
  • Revision ID: me@mark.atwood.name-20111130070647-ixp7oalp70hkbt6l
mergeĀ lp:~brianaker/drizzle/libdrizzle-2.0-not-install

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