~drizzle-trunk/drizzle/development

2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
1
/*
2
 * Drizzle Client & Protocol Library
3
 *
4
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
5
 * All rights reserved.
6
 *
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
 *
35
 */
36
37
38
/**
39
 * @file
40
 * @brief Command definitions
41
 */
42
2449.1.2 by Brian Aker
Additional fixes for libdrizzle.
43
#include <libdrizzle-2.0/common.h>
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
44
45
/*
46
 * Private variables.
47
 */
48
49
static drizzle_command_drizzle_t _command_drizzle_map[]=
50
{
51
 DRIZZLE_COMMAND_DRIZZLE_END,
52
 DRIZZLE_COMMAND_DRIZZLE_QUIT,
53
 DRIZZLE_COMMAND_DRIZZLE_INIT_DB,
54
 DRIZZLE_COMMAND_DRIZZLE_QUERY,
55
 DRIZZLE_COMMAND_DRIZZLE_END,
56
 DRIZZLE_COMMAND_DRIZZLE_END,
57
 DRIZZLE_COMMAND_DRIZZLE_END,
58
 DRIZZLE_COMMAND_DRIZZLE_END,
59
 DRIZZLE_COMMAND_DRIZZLE_SHUTDOWN,
60
 DRIZZLE_COMMAND_DRIZZLE_END, // STATISTICS
61
 DRIZZLE_COMMAND_DRIZZLE_END, // PROCESS_INFO
62
 DRIZZLE_COMMAND_DRIZZLE_END, // CONNECT
63
 DRIZZLE_COMMAND_DRIZZLE_KILL,// PROCESS_KILL
64
 DRIZZLE_COMMAND_DRIZZLE_END,
65
 DRIZZLE_COMMAND_DRIZZLE_PING,
66
 DRIZZLE_COMMAND_DRIZZLE_END,
67
 DRIZZLE_COMMAND_DRIZZLE_END,
68
 DRIZZLE_COMMAND_DRIZZLE_END,
69
 DRIZZLE_COMMAND_DRIZZLE_END,
70
 DRIZZLE_COMMAND_DRIZZLE_END,
71
 DRIZZLE_COMMAND_DRIZZLE_END,
72
 DRIZZLE_COMMAND_DRIZZLE_END,
73
 DRIZZLE_COMMAND_DRIZZLE_END,
74
 DRIZZLE_COMMAND_DRIZZLE_END,
75
 DRIZZLE_COMMAND_DRIZZLE_END,
76
 DRIZZLE_COMMAND_DRIZZLE_END,
77
 DRIZZLE_COMMAND_DRIZZLE_END,
78
 DRIZZLE_COMMAND_DRIZZLE_END,
79
 DRIZZLE_COMMAND_DRIZZLE_END,
80
 DRIZZLE_COMMAND_DRIZZLE_END,
81
 DRIZZLE_COMMAND_DRIZZLE_END
82
};
83
84
/*
85
 * State Definitions
86
 */
87
88
drizzle_return_t drizzle_state_command_read(drizzle_con_st *con)
89
{
90
  drizzle_log_debug(con->drizzle, "drizzle_state_command_read");
91
92
  if (con->buffer_size == 0)
93
  {
94
    drizzle_state_push(con, drizzle_state_read);
95
    return DRIZZLE_RETURN_OK;
96
  }
97
98
  if (con->command_total == 0)
99
  {
100
    con->command= (drizzle_command_t)(con->buffer_ptr[0]);
101
    con->buffer_ptr++;
102
    con->buffer_size--;
103
104
    con->command_total= (con->packet_size - 1);
105
  }
106
107
  if (con->buffer_size < (con->command_total - con->command_offset))
108
  {
109
    con->command_size= con->buffer_size;
110
    con->command_offset+= con->command_size;
111
  }
112
  else
113
  {
114
    con->command_size= (con->command_total - con->command_offset);
115
    con->command_offset= con->command_total;
116
  }
117
118
  con->command_data= con->buffer_ptr;
119
  con->buffer_ptr+= con->command_size;
120
  con->buffer_size-= con->command_size;
121
122
  if (con->command_offset == con->command_total)
2461.1.1 by Brian Aker
Fix safety issues around calling API with no check for NULL
123
  {
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
124
    drizzle_state_pop(con);
2461.1.1 by Brian Aker
Fix safety issues around calling API with no check for NULL
125
  }
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
126
  else
2461.1.1 by Brian Aker
Fix safety issues around calling API with no check for NULL
127
  {
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
128
    return DRIZZLE_RETURN_PAUSE;
2461.1.1 by Brian Aker
Fix safety issues around calling API with no check for NULL
129
  }
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
130
131
  return DRIZZLE_RETURN_OK;
132
}
133
134
drizzle_return_t drizzle_state_command_write(drizzle_con_st *con)
135
{
136
  uint8_t *start;
137
  uint8_t *ptr;
138
  size_t free_size;
139
  drizzle_return_t ret;
140
141
  drizzle_log_debug(con->drizzle, "drizzle_state_command_write");
142
143
  if (con->command_data == NULL && con->command_total != 0 &&
144
      con->command != DRIZZLE_COMMAND_CHANGE_USER)
145
  {
146
    return DRIZZLE_RETURN_PAUSE;
147
  }
148
149
  if (con->buffer_size == 0)
150
  {
151
    con->buffer_ptr= con->buffer;
152
    start= con->buffer;
153
  }
154
  else
155
    start= con->buffer_ptr + con->buffer_size;
156
157
  if (con->command_offset == 0)
158
  {
159
    /* Make sure we can fit the largest non-streaming packet, currently a
160
       DRIZZLE_COMMAND_CHANGE_USER command. */
161
162
    con->packet_size= 1  /* Command */
163
                    + strlen(con->user) + 1
164
                    + 1  /* Scramble size */
165
                    + DRIZZLE_MAX_SCRAMBLE_SIZE
2461.1.1 by Brian Aker
Fix safety issues around calling API with no check for NULL
166
                    + strlen(con->schema) +1;
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
167
168
    /* Flush buffer if there is not enough room. */
169
    free_size= (size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer);
170
    if (free_size < con->packet_size)
171
    {
172
      drizzle_state_push(con, drizzle_state_write);
173
      return DRIZZLE_RETURN_OK;
174
    }
175
176
    /* Store packet size at the end since it may change. */
177
    con->packet_number= 1;
178
    ptr= start;
179
    ptr[3]= 0;
180
    if (con->options & DRIZZLE_CON_MYSQL)
2461.1.1 by Brian Aker
Fix safety issues around calling API with no check for NULL
181
    {
182
      ptr[4]= uint8_t(con->command);
183
    }
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
184
    else
2461.1.1 by Brian Aker
Fix safety issues around calling API with no check for NULL
185
    {
186
      ptr[4]= uint8_t(_command_drizzle_map[con->command]);
187
    }
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
188
    ptr+= 5;
189
190
    if (con->command == DRIZZLE_COMMAND_CHANGE_USER)
191
    {
192
      ptr= drizzle_pack_auth(con, ptr, &ret);
193
      if (ret != DRIZZLE_RETURN_OK)
194
        return ret;
195
196
      con->buffer_size+= (4 + con->packet_size);
197
    }
198
    else if (con->command_total == 0)
199
    {
200
      con->packet_size= 1;
201
      con->buffer_size+= 5;
202
    }
203
    else
204
    {
205
      con->packet_size= 1 + con->command_total;
206
      free_size-= 5;
207
208
      /* Copy as much of the data in as we can into the write buffer. */
209
      if (con->command_size <= free_size)
210
      {
211
        memcpy(ptr, con->command_data, con->command_size);
212
        con->command_offset= con->command_size;
213
        con->command_data= NULL;
214
        con->buffer_size+= 5 + con->command_size;
215
      }
216
      else
217
      {
218
        memcpy(ptr, con->command_data, free_size);
219
        con->command_offset= free_size;
220
        con->command_data+= free_size;
221
        con->command_size-= free_size;
222
        con->buffer_size+= 5 + free_size;
223
      }
224
    }
225
226
    /* Store packet size now. */
227
    drizzle_set_byte3(start, con->packet_size);
228
  }
229
  else
230
  {
231
    /* Write directly from the caller buffer for the rest. */
232
    con->buffer_ptr= con->command_data;
233
    con->buffer_size= con->command_size;
234
    con->command_offset+= con->command_size;
235
    con->command_data= NULL;
236
  }
237
238
  if (con->command_offset == con->command_total)
239
  {
240
    drizzle_state_pop(con);
241
242
    if (!(con->options & (DRIZZLE_CON_RAW_PACKET |
243
                          DRIZZLE_CON_NO_RESULT_READ)) &&
244
        con->command != DRIZZLE_COMMAND_FIELD_LIST)
245
    {
246
      drizzle_state_push(con, drizzle_state_result_read);
247
      drizzle_state_push(con, drizzle_state_packet_read);
248
    }
249
  }
250
251
  drizzle_state_push(con, drizzle_state_write);
252
253
  return DRIZZLE_RETURN_OK;
254
}