~drizzle-trunk/drizzle/development

1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
1
/*
2
 * Drizzle Client & Protocol Library
3
 *
4
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
5
 * All rights reserved.
6
 *
1971.2.1 by kalebral at gmail
update files that did not have license or had incorrect license structure
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are
9
 * met:
10
 *
11
 *     * Redistributions of source code must retain the above copyright
12
 * notice, this list of conditions and the following disclaimer.
13
 *
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
17
 * distribution.
18
 *
19
 *     * The names of its contributors may not be used to endorse or
20
 * promote products derived from this software without specific prior
21
 * written permission.
22
 *
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.
34
 *
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
35
 */
36
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
37
#include <config.h>
2164.1.1 by Monty Taylor
Merged in outstanding changes from the win32 work.
38
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
39
#include <errno.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <unistd.h>
43
44
#include <libdrizzle/drizzle_server.h>
45
46
#define DRIZZLE_FIELD_MAX 32
47
#define DRIZZLE_RESULT_ROWS 20
48
49
#define DRIZZLE_RETURN_CHECK(__ret, __function, __drizzle) \
50
{ \
51
  if ((__ret) != DRIZZLE_RETURN_OK) \
52
    DRIZZLE_RETURN_ERROR(__function, __drizzle) \
53
}
54
55
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
56
{ \
57
  printf(__function ":%s\n", drizzle_error(__drizzle)); \
58
  return; \
59
}
60
61
static void server(drizzle_st *drizzle, drizzle_con_st *con,
62
                   drizzle_result_st *result, drizzle_column_st *column);
63
64
int main(int argc, char *argv[])
65
{
66
  int c;
67
  uint32_t count= 0;
68
  const char *host= NULL;
69
  bool mysql= false;
70
  in_port_t port= 0;
71
  drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
72
  drizzle_return_t ret;
73
  drizzle_st drizzle;
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
74
  drizzle_con_st *con_listen= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
75
  drizzle_con_st *con= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
76
  drizzle_result_st result;
77
  drizzle_column_st column;
78
79
  while((c = getopt(argc, argv, "c:h:mp:v")) != -1)
80
  {
81
    switch(c)
82
    {
83
    case 'c':
84
      count= (uint32_t)atoi(optarg);
85
      break;
86
87
    case 'h':
88
      host= optarg;
89
      break;
90
91
    case 'm':
92
      mysql= true;
93
      break;
94
95
    case 'p':
96
      port= (in_port_t)atoi(optarg);
97
      break;
98
99
    case 'v':
1992.6.2 by Monty Taylor
Cleaned up for additional gcc 4.5 warnings.
100
      switch(verbose)
101
      {
102
      case DRIZZLE_VERBOSE_NEVER:
103
        verbose= DRIZZLE_VERBOSE_FATAL;
104
        break;
105
      case DRIZZLE_VERBOSE_FATAL:
106
        verbose= DRIZZLE_VERBOSE_ERROR;
107
        break;
108
      case DRIZZLE_VERBOSE_ERROR:
109
        verbose= DRIZZLE_VERBOSE_INFO;
110
        break;
111
      case DRIZZLE_VERBOSE_INFO:
112
        verbose= DRIZZLE_VERBOSE_DEBUG;
113
        break;
114
      case DRIZZLE_VERBOSE_DEBUG:
115
        verbose= DRIZZLE_VERBOSE_CRAZY;
116
        break;
117
      case DRIZZLE_VERBOSE_CRAZY:
118
      case DRIZZLE_VERBOSE_MAX:
119
        break;
120
      }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
121
      break;
122
123
    default:
124
      printf("\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v]\n",
125
             argv[0]);
126
      printf("\t-c <count> - Number of connections to accept before exiting\n");
127
      printf("\t-h <host>  - Host to listen on\n");
128
      printf("\t-m         - Use the MySQL protocol\n");
129
      printf("\t-p <port>  - Port to listen on\n");
130
      printf("\t-v         - Increase verbosity level\n");
131
      return 1;
132
    }
133
  }
134
135
  if (drizzle_create(&drizzle) == NULL)
136
  {
137
    printf("drizzle_create:NULL\n");
138
    return 1;
139
  }
140
141
  drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS);
142
  drizzle_set_verbose(&drizzle, verbose);
143
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
144
  if (drizzle_con_create(&drizzle, con_listen) == NULL)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
145
  {
146
    printf("drizzle_con_create:NULL\n");
147
    return 1;
148
  }
149
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
150
  drizzle_con_add_options(con_listen, DRIZZLE_CON_LISTEN);
151
  drizzle_con_set_tcp(con_listen, host, port);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
152
153
  if (mysql)
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
154
    drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
155
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
156
  if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
157
  {
158
    printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle));
159
    return 1;
160
  }
161
162
  while (1)
163
  {
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
164
    (void)drizzle_con_accept(&drizzle, con, &ret);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
165
    if (ret != DRIZZLE_RETURN_OK)
166
    {
167
      printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle));
168
      return 1;
169
    }
170
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
171
    server(&drizzle, con, &result, &column);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
172
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
173
    drizzle_con_free(con);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
174
175
    if (count > 0)
176
    {
177
      count--;
178
179
      if (count == 0)
180
        break;
181
    }
182
  }
183
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
184
  drizzle_con_free(con_listen);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
185
  drizzle_free(&drizzle);
186
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
187
  free(con);
188
  free(con_listen);
189
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
190
  return 0;
191
}
192
193
static void server(drizzle_st *drizzle, drizzle_con_st *con,
194
                   drizzle_result_st *result, drizzle_column_st *column)
195
{
196
  drizzle_return_t ret;
197
  drizzle_command_t command;
198
  uint8_t *data= NULL;
199
  size_t total;
200
  char *field[2];
201
  char field1[DRIZZLE_FIELD_MAX];
202
  char field2[DRIZZLE_FIELD_MAX];
203
  size_t size[2];
204
  uint64_t x;
205
206
  field[0]= field1;
207
  field[1]= field2;
208
209
  /* Handshake packets. */
210
  drizzle_con_set_protocol_version(con, 10);
211
  drizzle_con_set_server_version(con, "libdrizzle example 1.2.3");
212
  drizzle_con_set_thread_id(con, 1);
213
  drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
214
  drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
215
  drizzle_con_set_charset(con, 8);
216
  drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
217
  drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);
218
219
  ret= drizzle_handshake_server_write(con);
220
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle)
221
222
  ret= drizzle_handshake_client_read(con);
223
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle)
224
225
  if (drizzle_result_create(con, result) == NULL)
226
    DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
227
228
  ret= drizzle_result_write(con, result, true);
229
  DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
230
231
  /* Command loop. */
232
  while (1)
233
  {
234
    drizzle_result_free(result);
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
235
    free(data);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
236
1992.6.2 by Monty Taylor
Cleaned up for additional gcc 4.5 warnings.
237
    data= (uint8_t *)drizzle_con_command_buffer(con, &command, &total, &ret);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
238
    if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
239
        (ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT))
240
    {
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
241
      free(data);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
242
      return;
243
    }
244
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", drizzle)
245
246
    if (drizzle_result_create(con, result) == NULL)
247
      DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
248
249
    if (command != DRIZZLE_COMMAND_QUERY)
250
    {
251
      ret= drizzle_result_write(con, result, true);
252
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
253
      continue;
254
    }
255
256
    drizzle_result_set_column_count(result, 2);
257
258
    ret= drizzle_result_write(con, result, false);
259
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
260
261
    /* Columns. */
262
    if (drizzle_column_create(result, column) == NULL)
263
      DRIZZLE_RETURN_ERROR("drizzle_column_create", drizzle)
264
265
    drizzle_column_set_catalog(column, "default");
266
    drizzle_column_set_db(column, "drizzle_test_db");
267
    drizzle_column_set_table(column, "drizzle_test_table");
268
    drizzle_column_set_orig_table(column, "drizzle_test_table");
269
    drizzle_column_set_name(column, "test_column_1");
270
    drizzle_column_set_orig_name(column, "test_column_1");
271
    drizzle_column_set_charset(column, 8);
272
    drizzle_column_set_size(column, DRIZZLE_FIELD_MAX);
273
    drizzle_column_set_type(column, DRIZZLE_COLUMN_TYPE_VARCHAR);
274
275
    ret= drizzle_column_write(result, column);
276
    DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
277
278
    drizzle_column_set_name(column, "test_column_2");
279
    drizzle_column_set_orig_name(column, "test_column_2");
280
281
    ret= drizzle_column_write(result, column);
282
    DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
283
284
    drizzle_column_free(column);
285
286
    drizzle_result_set_eof(result, true);
287
288
    ret= drizzle_result_write(con, result, false);
289
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
290
291
    /* Rows. */
292
    for (x= 0; x < DRIZZLE_RESULT_ROWS; x++)
293
    {
294
      size[0]= (size_t)snprintf(field[0], DRIZZLE_FIELD_MAX,
295
                                "field %" PRIu64 "-1", x);
296
      if (size[0] >= DRIZZLE_FIELD_MAX)
297
        size[0]= DRIZZLE_FIELD_MAX - 1;
298
299
      size[1]= (size_t)snprintf(field[1], DRIZZLE_FIELD_MAX,
300
                                "field %" PRIu64 "-2", x);
301
      if (size[1] >= DRIZZLE_FIELD_MAX)
302
        size[1]= DRIZZLE_FIELD_MAX - 1;
303
304
      /* This is needed for MySQL and old Drizzle protocol. */
305
      drizzle_result_calc_row_size(result, (drizzle_field_t *)field, size);
306
307
      ret= drizzle_row_write(result);
308
      DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle)
309
310
      /* Fields. */
311
      ret= drizzle_field_write(result, (drizzle_field_t)field[0], size[0],
312
                               size[0]);
313
      DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
314
315
      ret= drizzle_field_write(result, (drizzle_field_t)field[1], size[1],
316
                               size[1]);
317
      DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
318
    }
319
320
    ret= drizzle_result_write(con, result, true);
321
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
322
  }
323
}