~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/oldlibdrizzle/oldlibdrizzle.cc

  • Committer: Brian Aker
  • Date: 2009-11-12 16:13:04 UTC
  • mfrom: (1211.1.7 staging)
  • Revision ID: brian@gaz-20091112161304-opamiauv36fg0n6u
Rollup of Brian, Padraig, and Stewart patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2010 Brian Aker
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
18
 */
20
19
 
21
 
 
22
 
#include "config.h"
 
20
#include <drizzled/server_includes.h>
23
21
#include <drizzled/gettext.h>
24
22
#include <drizzled/error.h>
25
23
#include <drizzled/query_id.h>
 
24
#include <drizzled/sql_state.h>
26
25
#include <drizzled/session.h>
27
 
#include "drizzled/internal/my_sys.h"
28
 
#include "drizzled/internal/m_string.h"
29
26
#include <algorithm>
30
 
#include <iostream>
31
 
#include <boost/program_options.hpp>
32
 
#include <drizzled/module/option_map.h>
33
 
#include "drizzled/util/tokenize.h"
34
 
#include "drizzle_protocol.h"
35
 
 
36
 
namespace po= boost::program_options;
 
27
 
 
28
#include "pack.h"
 
29
#include "errmsg.h"
 
30
#include "oldlibdrizzle.h"
 
31
#include "options.h"
 
32
 
 
33
using namespace std;
37
34
using namespace drizzled;
38
 
using namespace std;
39
 
 
40
 
namespace drizzle_plugin
41
 
{
42
 
namespace drizzle_protocol
43
 
{
44
 
 
45
 
std::vector<std::string> ClientDrizzleProtocol::drizzle_admin_ip_addresses;
46
 
static port_constraint port;
47
 
static timeout_constraint connect_timeout;
48
 
static timeout_constraint read_timeout;
49
 
static timeout_constraint write_timeout;
50
 
static retry_constraint retry_count;
51
 
static buffer_constraint buffer_length;
52
 
 
53
 
static const uint32_t DRIZZLE_TCP_PORT= 4427;
54
 
 
55
 
ProtocolCounters *ListenDrizzleProtocol::drizzle_counters= new ProtocolCounters();
56
 
 
57
 
ListenDrizzleProtocol::~ListenDrizzleProtocol()
58
 
{
59
 
}
60
 
 
61
 
in_port_t ListenDrizzleProtocol::getPort(void) const
62
 
{
63
 
  return port;
64
 
}
65
 
 
66
 
void ClientDrizzleProtocol::drizzle_compose_ip_addresses(vector<string> options)
67
 
{
68
 
  for (vector<string>::iterator it= options.begin();
69
 
       it != options.end();
70
 
       ++it)
71
 
  {
72
 
    tokenize(*it, drizzle_admin_ip_addresses, ",", true);
73
 
  }
74
 
}
75
 
 
76
 
bool ClientDrizzleProtocol::isAdminAllowed(void)
77
 
{
78
 
  if (std::find(drizzle_admin_ip_addresses.begin(), drizzle_admin_ip_addresses.end(), session->user()->address()) != drizzle_admin_ip_addresses.end())
79
 
    return true;
80
 
 
81
 
  return false;
82
 
}
83
 
 
84
 
plugin::Client *ListenDrizzleProtocol::getClient(int fd)
 
35
 
 
36
#define PROTOCOL_VERSION 10
 
37
 
 
38
extern uint32_t drizzled_tcp_port;
 
39
 
 
40
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
 
41
static uint32_t connect_timeout;
 
42
static uint32_t read_timeout;
 
43
static uint32_t write_timeout;
 
44
static uint32_t retry_count;
 
45
static uint32_t buffer_length;
 
46
static char* bind_address;
 
47
 
 
48
const char* ListenOldLibdrizzle::getHost(void) const
 
49
{
 
50
  return bind_address;
 
51
}
 
52
 
 
53
in_port_t ListenOldLibdrizzle::getPort(void) const
 
54
{
 
55
  return (in_port_t) drizzled_tcp_port;
 
56
}
 
57
 
 
58
plugin::Client *ListenOldLibdrizzle::getClient(int fd)
85
59
{
86
60
  int new_fd;
87
61
  new_fd= acceptTcp(fd);
88
62
  if (new_fd == -1)
89
63
    return NULL;
90
64
 
91
 
  return new ClientDrizzleProtocol(new_fd, getCounters());
92
 
}
93
 
 
94
 
static int init(drizzled::module::Context &context)
95
 
{  
96
 
  const module::option_map &vm= context.getOptions();
97
 
 
98
 
  ListenDrizzleProtocol *protocol=new ListenDrizzleProtocol("drizzle_protocol", vm["bind-address"].as<std::string>(), true);
99
 
  protocol->addCountersToTable();
100
 
  context.add(protocol);
101
 
  context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
102
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("connect_timeout", connect_timeout));
103
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("read_timeout", read_timeout));
104
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("write_timeout", write_timeout));
105
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("retry_count", retry_count));
106
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("buffer_length", buffer_length));
107
 
  context.registerVariable(new sys_var_const_string_val("bind_address",
108
 
                                                        vm["bind-address"].as<std::string>()));
109
 
 
110
 
  context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenDrizzleProtocol::drizzle_counters->max_connections));
111
 
 
112
 
  return 0;
113
 
}
114
 
 
115
 
 
116
 
static void init_options(drizzled::module::option_context &context)
117
 
{
118
 
  context("port",
119
 
          po::value<port_constraint>(&port)->default_value(DRIZZLE_TCP_PORT),
120
 
          N_("Port number to use for connection or 0 for default to with Drizzle/MySQL protocol."));
121
 
  context("connect-timeout",
122
 
          po::value<timeout_constraint>(&connect_timeout)->default_value(10),
123
 
          N_("Connect Timeout."));
124
 
  context("read-timeout",
125
 
          po::value<timeout_constraint>(&read_timeout)->default_value(30),
126
 
          N_("Read Timeout."));
127
 
  context("write-timeout",
128
 
          po::value<timeout_constraint>(&write_timeout)->default_value(60),
129
 
          N_("Write Timeout."));
130
 
  context("retry-count",
131
 
          po::value<retry_constraint>(&retry_count)->default_value(10),
132
 
          N_("Retry Count."));
133
 
  context("buffer-length",
134
 
          po::value<buffer_constraint>(&buffer_length)->default_value(16384),
135
 
          N_("Buffer length."));
136
 
  context("bind-address",
137
 
          po::value<std::string>()->default_value(""),
138
 
          N_("Address to bind to."));
139
 
  context("max-connections",
140
 
          po::value<uint32_t>(&ListenDrizzleProtocol::drizzle_counters->max_connections)->default_value(1000),
141
 
          N_("Maximum simultaneous connections."));
142
 
  context("admin-ip-addresses",
143
 
          po::value<vector<string> >()->composing()->notifier(&ClientDrizzleProtocol::drizzle_compose_ip_addresses),
144
 
          N_("A restrictive IP address list for incoming admin connections."));
145
 
}
146
 
 
147
 
} /* namespace drizzle_protocol */
148
 
} /* namespace drizzle_plugin */
149
 
 
150
 
DRIZZLE_PLUGIN(drizzle_plugin::drizzle_protocol::init, NULL, drizzle_plugin::drizzle_protocol::init_options);
 
65
  return new (nothrow) ClientOldLibdrizzle(new_fd);
 
66
}
 
67
 
 
68
ClientOldLibdrizzle::ClientOldLibdrizzle(int fd)
 
69
{
 
70
  net.vio= 0;
 
71
 
 
72
  if (fd == -1)
 
73
    return;
 
74
 
 
75
  if (drizzleclient_net_init_sock(&net, fd, 0, buffer_length))
 
76
    throw bad_alloc();
 
77
 
 
78
  drizzleclient_net_set_read_timeout(&net, read_timeout);
 
79
  drizzleclient_net_set_write_timeout(&net, write_timeout);
 
80
  net.retry_count=retry_count;
 
81
}
 
82
 
 
83
ClientOldLibdrizzle::~ClientOldLibdrizzle()
 
84
{
 
85
  if (net.vio)
 
86
    drizzleclient_vio_close(net.vio);
 
87
}
 
88
 
 
89
int ClientOldLibdrizzle::getFileDescriptor(void)
 
90
{
 
91
  return drizzleclient_net_get_sd(&net);
 
92
}
 
93
 
 
94
bool ClientOldLibdrizzle::isConnected()
 
95
{
 
96
  return net.vio != 0;
 
97
}
 
98
 
 
99
bool ClientOldLibdrizzle::isReading(void)
 
100
{
 
101
  return net.reading_or_writing == 1;
 
102
}
 
103
 
 
104
bool ClientOldLibdrizzle::isWriting(void)
 
105
{
 
106
  return net.reading_or_writing == 2;
 
107
}
 
108
 
 
109
bool ClientOldLibdrizzle::flush()
 
110
{
 
111
  if (net.vio == NULL)
 
112
    return false;
 
113
  bool ret= drizzleclient_net_write(&net, (unsigned char*) packet.ptr(),
 
114
                           packet.length());
 
115
  packet.length(0);
 
116
  return ret;
 
117
}
 
118
 
 
119
void ClientOldLibdrizzle::close(void)
 
120
{
 
121
  if (net.vio)
 
122
  { 
 
123
    drizzleclient_net_close(&net);
 
124
    drizzleclient_net_end(&net);
 
125
  }
 
126
}
 
127
 
 
128
bool ClientOldLibdrizzle::authenticate()
 
129
{
 
130
  bool connection_is_valid;
 
131
 
 
132
  /* Use "connect_timeout" value during connection phase */
 
133
  drizzleclient_net_set_read_timeout(&net, connect_timeout);
 
134
  drizzleclient_net_set_write_timeout(&net, connect_timeout);
 
135
 
 
136
  connection_is_valid= checkConnection();
 
137
 
 
138
  if (connection_is_valid)
 
139
    sendOK();
 
140
  else
 
141
  {
 
142
    sendError(session->main_da.sql_errno(), session->main_da.message());
 
143
    return false;
 
144
  }
 
145
 
 
146
  /* Connect completed, set read/write timeouts back to default */
 
147
  drizzleclient_net_set_read_timeout(&net, read_timeout);
 
148
  drizzleclient_net_set_write_timeout(&net, write_timeout);
 
149
  return true;
 
150
}
 
151
 
 
152
bool ClientOldLibdrizzle::readCommand(char **l_packet, uint32_t *packet_length)
 
153
{
 
154
  /*
 
155
    This thread will do a blocking read from the client which
 
156
    will be interrupted when the next command is received from
 
157
    the client, the connection is closed or "net_wait_timeout"
 
158
    number of seconds has passed
 
159
  */
 
160
#ifdef NEVER
 
161
  /* We can do this much more efficiently with poll timeouts or watcher thread,
 
162
     disabling for now, which means net_wait_timeout == read_timeout. */
 
163
  drizzleclient_net_set_read_timeout(&net,
 
164
                                     session->variables.net_wait_timeout);
 
165
#endif
 
166
 
 
167
  net.pkt_nr=0;
 
168
 
 
169
  *packet_length= drizzleclient_net_read(&net);
 
170
  if (*packet_length == packet_error)
 
171
  {
 
172
    /* Check if we can continue without closing the connection */
 
173
 
 
174
    if(net.last_errno== CR_NET_PACKET_TOO_LARGE)
 
175
      my_error(ER_NET_PACKET_TOO_LARGE, MYF(0));
 
176
    if (session->main_da.status() == Diagnostics_area::DA_ERROR)
 
177
      sendError(session->main_da.sql_errno(), session->main_da.message());
 
178
    else
 
179
      sendOK();
 
180
 
 
181
    if (net.error != 3)
 
182
      return false;                       // We have to close it.
 
183
 
 
184
    net.error= 0;
 
185
    *packet_length= 0;
 
186
    return true;
 
187
  }
 
188
 
 
189
  *l_packet= (char*) net.read_pos;
 
190
 
 
191
  /*
 
192
    'packet_length' contains length of data, as it was stored in packet
 
193
    header. In case of malformed header, drizzleclient_net_read returns zero.
 
194
    If packet_length is not zero, drizzleclient_net_read ensures that the returned
 
195
    number of bytes was actually read from network.
 
196
    There is also an extra safety measure in drizzleclient_net_read:
 
197
    it sets packet[packet_length]= 0, but only for non-zero packets.
 
198
  */
 
199
 
 
200
  if (*packet_length == 0)                       /* safety */
 
201
  {
 
202
    /* Initialize with COM_SLEEP packet */
 
203
    (*l_packet)[0]= (unsigned char) COM_SLEEP;
 
204
    *packet_length= 1;
 
205
  }
 
206
  /* Do not rely on drizzleclient_net_read, extra safety against programming errors. */
 
207
  (*l_packet)[*packet_length]= '\0';                  /* safety */
 
208
 
 
209
#ifdef NEVER
 
210
  /* See comment above. */
 
211
  /* Restore read timeout value */
 
212
  drizzleclient_net_set_read_timeout(&net,
 
213
                                     session->variables.net_read_timeout);
 
214
#endif
 
215
 
 
216
  return true;
 
217
}
 
218
 
 
219
/**
 
220
  Return ok to the client.
 
221
 
 
222
  The ok packet has the following structure:
 
223
 
 
224
  - 0               : Marker (1 byte)
 
225
  - affected_rows    : Stored in 1-9 bytes
 
226
  - id        : Stored in 1-9 bytes
 
227
  - server_status    : Copy of session->server_status;  Can be used by client
 
228
  to check if we are inside an transaction.
 
229
  New in 4.0 client
 
230
  - warning_count    : Stored in 2 bytes; New in 4.1 client
 
231
  - message        : Stored as packed length (1-9 bytes) + message.
 
232
  Is not stored if no message.
 
233
 
 
234
  @param session           Thread handler
 
235
  @param affected_rows       Number of rows changed by statement
 
236
  @param id           Auto_increment id for first row (if used)
 
237
  @param message       Message to send to the client (Used by mysql_status)
 
238
*/
 
239
 
 
240
void ClientOldLibdrizzle::sendOK()
 
241
{
 
242
  unsigned char buff[DRIZZLE_ERRMSG_SIZE+10],*pos;
 
243
  const char *message= NULL;
 
244
  uint32_t tmp;
 
245
 
 
246
  if (!net.vio)    // hack for re-parsing queries
 
247
  {
 
248
    return;
 
249
  }
 
250
 
 
251
  buff[0]=0;                    // No fields
 
252
  if (session->main_da.status() == Diagnostics_area::DA_OK)
 
253
  {
 
254
    if (client_capabilities & CLIENT_FOUND_ROWS && session->main_da.found_rows())
 
255
      pos=drizzleclient_net_store_length(buff+1,session->main_da.found_rows());
 
256
    else
 
257
      pos=drizzleclient_net_store_length(buff+1,session->main_da.affected_rows());
 
258
    pos=drizzleclient_net_store_length(pos, session->main_da.last_insert_id());
 
259
    int2store(pos, session->main_da.server_status());
 
260
    pos+=2;
 
261
    tmp= min(session->main_da.total_warn_count(), (uint32_t)65535);
 
262
    message= session->main_da.message();
 
263
  }
 
264
  else
 
265
  {
 
266
    pos=drizzleclient_net_store_length(buff+1,0);
 
267
    pos=drizzleclient_net_store_length(pos, 0);
 
268
    int2store(pos, session->server_status);
 
269
    pos+=2;
 
270
    tmp= min(session->total_warn_count, (uint32_t)65535);
 
271
  }
 
272
 
 
273
  /* We can only return up to 65535 warnings in two bytes */
 
274
  int2store(pos, tmp);
 
275
  pos+= 2;
 
276
 
 
277
  session->main_da.can_overwrite_status= true;
 
278
 
 
279
  if (message && message[0])
 
280
  {
 
281
    size_t length= strlen(message);
 
282
    pos=drizzleclient_net_store_length(pos,length);
 
283
    memcpy(pos,(unsigned char*) message,length);
 
284
    pos+=length;
 
285
  }
 
286
  drizzleclient_net_write(&net, buff, (size_t) (pos-buff));
 
287
  drizzleclient_net_flush(&net);
 
288
 
 
289
  session->main_da.can_overwrite_status= false;
 
290
}
 
291
 
 
292
/**
 
293
  Send eof (= end of result set) to the client.
 
294
 
 
295
  The eof packet has the following structure:
 
296
 
 
297
  - 254    (DRIZZLE_PROTOCOL_NO_MORE_DATA)    : Marker (1 byte)
 
298
  - warning_count    : Stored in 2 bytes; New in 4.1 client
 
299
  - status_flag    : Stored in 2 bytes;
 
300
  For flags like SERVER_MORE_RESULTS_EXISTS.
 
301
 
 
302
  Note that the warning count will not be sent if 'no_flush' is set as
 
303
  we don't want to report the warning count until all data is sent to the
 
304
  client.
 
305
*/
 
306
 
 
307
void ClientOldLibdrizzle::sendEOF()
 
308
{
 
309
  /* Set to true if no active vio, to work well in case of --init-file */
 
310
  if (net.vio != 0)
 
311
  {
 
312
    session->main_da.can_overwrite_status= true;
 
313
    writeEOFPacket(session->main_da.server_status(),
 
314
                   session->main_da.total_warn_count());
 
315
    drizzleclient_net_flush(&net);
 
316
    session->main_da.can_overwrite_status= false;
 
317
  }
 
318
  packet.shrink(buffer_length);
 
319
}
 
320
 
 
321
 
 
322
void ClientOldLibdrizzle::sendError(uint32_t sql_errno, const char *err)
 
323
{
 
324
  uint32_t length;
 
325
  /*
 
326
    buff[]: sql_errno:2 + ('#':1 + SQLSTATE_LENGTH:5) + DRIZZLE_ERRMSG_SIZE:512
 
327
  */
 
328
  unsigned char buff[2+1+SQLSTATE_LENGTH+DRIZZLE_ERRMSG_SIZE], *pos;
 
329
 
 
330
  assert(sql_errno);
 
331
  assert(err && err[0]);
 
332
 
 
333
  /*
 
334
    It's one case when we can push an error even though there
 
335
    is an OK or EOF already.
 
336
  */
 
337
  session->main_da.can_overwrite_status= true;
 
338
 
 
339
  /* Abort multi-result sets */
 
340
  session->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
 
341
 
 
342
  /**
 
343
    Send a error string to client.
 
344
 
 
345
    For SIGNAL/RESIGNAL and GET DIAGNOSTICS functionality it's
 
346
    critical that every error that can be intercepted is issued in one
 
347
    place only, my_message_sql.
 
348
  */
 
349
 
 
350
  if (net.vio == 0)
 
351
  {
 
352
    return;
 
353
  }
 
354
 
 
355
  int2store(buff,sql_errno);
 
356
  pos= buff+2;
 
357
 
 
358
  /* The first # is to make the client backward compatible */
 
359
  buff[2]= '#';
 
360
  pos= (unsigned char*) strcpy((char*) buff+3, drizzle_errno_to_sqlstate(sql_errno));
 
361
  pos+= strlen(drizzle_errno_to_sqlstate(sql_errno));
 
362
 
 
363
  char *tmp= strncpy((char*)pos, err, DRIZZLE_ERRMSG_SIZE-1);
 
364
  tmp+= strlen((char*)pos);
 
365
  tmp[0]= '\0';
 
366
  length= (uint32_t)(tmp-(char*)buff);
 
367
  err= (char*) buff;
 
368
 
 
369
  drizzleclient_net_write_command(&net,(unsigned char) 255, (unsigned char*) "", 0, (unsigned char*) err, length);
 
370
 
 
371
  session->main_da.can_overwrite_status= false;
 
372
}
 
373
 
 
374
/**
 
375
  Send name and type of result to client.
 
376
 
 
377
  Sum fields has table name empty and field_name.
 
378
 
 
379
  @param Session        Thread data object
 
380
  @param list            List of items to send to client
 
381
  @param flag            Bit mask with the following functions:
 
382
                        - 1 send number of rows
 
383
                        - 2 send default values
 
384
                        - 4 don't write eof packet
 
385
 
 
386
  @retval
 
387
    0    ok
 
388
  @retval
 
389
    1    Error  (Note that in this case the error is not sent to the
 
390
    client)
 
391
*/
 
392
bool ClientOldLibdrizzle::sendFields(List<Item> *list)
 
393
{
 
394
  List_iterator_fast<Item> it(*list);
 
395
  Item *item;
 
396
  unsigned char buff[80];
 
397
  String tmp((char*) buff,sizeof(buff),&my_charset_bin);
 
398
 
 
399
  unsigned char *row_pos= drizzleclient_net_store_length(buff, list->elements);
 
400
  (void) drizzleclient_net_write(&net, buff, (size_t) (row_pos-buff));
 
401
 
 
402
  while ((item=it++))
 
403
  {
 
404
    char *pos;
 
405
    SendField field;
 
406
    item->make_field(&field);
 
407
 
 
408
    packet.length(0);
 
409
 
 
410
    if (store(STRING_WITH_LEN("def")) ||
 
411
        store(field.db_name) ||
 
412
        store(field.table_name) ||
 
413
        store(field.org_table_name) ||
 
414
        store(field.col_name) ||
 
415
        store(field.org_col_name) ||
 
416
        packet.realloc(packet.length()+12))
 
417
      goto err;
 
418
 
 
419
    /* Store fixed length fields */
 
420
    pos= (char*) packet.ptr()+packet.length();
 
421
    *pos++= 12;                // Length of packed fields
 
422
    /* No conversion */
 
423
    int2store(pos, field.charsetnr);
 
424
    int4store(pos+2, field.length);
 
425
    /* Add one to compensate for tinyint removal from enum. */
 
426
    pos[6]= field.type + 1;
 
427
    int2store(pos+7,field.flags);
 
428
    pos[9]= (char) field.decimals;
 
429
    pos[10]= 0;                // For the future
 
430
    pos[11]= 0;                // For the future
 
431
    pos+= 12;
 
432
 
 
433
    packet.length((uint32_t) (pos - packet.ptr()));
 
434
    if (flush())
 
435
      break;
 
436
  }
 
437
 
 
438
  /*
 
439
    Mark the end of meta-data result set, and store session->server_status,
 
440
    to show that there is no cursor.
 
441
    Send no warning information, as it will be sent at statement end.
 
442
  */
 
443
  writeEOFPacket(session->server_status, session->total_warn_count);
 
444
  return 0;
 
445
 
 
446
err:
 
447
  my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES),
 
448
             MYF(0));
 
449
  return 1;
 
450
}
 
451
 
 
452
bool ClientOldLibdrizzle::store(Field *from)
 
453
{
 
454
  if (from->is_null())
 
455
    return store();
 
456
  char buff[MAX_FIELD_WIDTH];
 
457
  String str(buff,sizeof(buff), &my_charset_bin);
 
458
 
 
459
  from->val_str(&str);
 
460
 
 
461
  return netStoreData((const unsigned char *)str.ptr(), str.length());
 
462
}
 
463
 
 
464
bool ClientOldLibdrizzle::store(void)
 
465
{
 
466
  char buff[1];
 
467
  buff[0]= (char)251;
 
468
  return packet.append(buff, sizeof(buff), PACKET_BUFFER_EXTRA_ALLOC);
 
469
}
 
470
 
 
471
bool ClientOldLibdrizzle::store(int32_t from)
 
472
{
 
473
  char buff[12];
 
474
  return netStoreData((unsigned char*) buff,
 
475
                      (size_t) (int10_to_str(from, buff, -10) - buff));
 
476
}
 
477
 
 
478
bool ClientOldLibdrizzle::store(uint32_t from)
 
479
{
 
480
  char buff[11];
 
481
  return netStoreData((unsigned char*) buff,
 
482
                      (size_t) (int10_to_str(from, buff, 10) - buff));
 
483
}
 
484
 
 
485
bool ClientOldLibdrizzle::store(int64_t from)
 
486
{
 
487
  char buff[22];
 
488
  return netStoreData((unsigned char*) buff,
 
489
                      (size_t) (int64_t10_to_str(from, buff, -10) - buff));
 
490
}
 
491
 
 
492
bool ClientOldLibdrizzle::store(uint64_t from)
 
493
{
 
494
  char buff[21];
 
495
  return netStoreData((unsigned char*) buff,
 
496
                      (size_t) (int64_t10_to_str(from, buff, 10) - buff));
 
497
}
 
498
 
 
499
bool ClientOldLibdrizzle::store(double from, uint32_t decimals, String *buffer)
 
500
{
 
501
  buffer->set_real(from, decimals, session->charset());
 
502
  return netStoreData((unsigned char*) buffer->ptr(), buffer->length());
 
503
}
 
504
 
 
505
bool ClientOldLibdrizzle::store(const char *from, size_t length)
 
506
{
 
507
  return netStoreData((const unsigned char *)from, length);
 
508
}
 
509
 
 
510
bool ClientOldLibdrizzle::wasAborted(void)
 
511
{
 
512
  return net.error && net.vio != 0;
 
513
}
 
514
 
 
515
bool ClientOldLibdrizzle::haveMoreData(void)
 
516
{
 
517
  return drizzleclient_net_more_data(&net);
 
518
}
 
519
 
 
520
bool ClientOldLibdrizzle::haveError(void)
 
521
{
 
522
  return net.error || net.vio == 0;
 
523
}
 
524
 
 
525
bool ClientOldLibdrizzle::checkConnection(void)
 
526
{
 
527
  uint32_t pkt_len= 0;
 
528
  char *end;
 
529
 
 
530
  // TCP/IP connection
 
531
  {
 
532
    char ip[NI_MAXHOST];
 
533
    uint16_t port;
 
534
 
 
535
    if (drizzleclient_net_peer_addr(&net, ip, &port, NI_MAXHOST))
 
536
    {
 
537
      my_error(ER_BAD_HOST_ERROR, MYF(0), session->security_ctx.ip.c_str());
 
538
      return false;
 
539
    }
 
540
 
 
541
    session->security_ctx.ip.assign(ip);
 
542
  }
 
543
  drizzleclient_net_keepalive(&net, true);
 
544
 
 
545
  uint32_t server_capabilites;
 
546
  {
 
547
    /* buff[] needs to big enough to hold the server_version variable */
 
548
    char buff[SERVER_VERSION_LENGTH + SCRAMBLE_LENGTH + 64];
 
549
 
 
550
    server_capabilites= CLIENT_BASIC_FLAGS;
 
551
 
 
552
#ifdef HAVE_COMPRESS
 
553
    server_capabilites|= CLIENT_COMPRESS;
 
554
#endif /* HAVE_COMPRESS */
 
555
 
 
556
    end= buff + strlen(VERSION);
 
557
    if ((end - buff) >= SERVER_VERSION_LENGTH)
 
558
      end= buff + (SERVER_VERSION_LENGTH - 1);
 
559
    memcpy(buff, VERSION, end - buff);
 
560
    *end= 0;
 
561
    end++;
 
562
 
 
563
    int4store((unsigned char*) end, global_thread_id);
 
564
    end+= 4;
 
565
 
 
566
    /* We don't use scramble anymore. */
 
567
    memset(end, 'X', SCRAMBLE_LENGTH_323);
 
568
    end+= SCRAMBLE_LENGTH_323;
 
569
    *end++= 0; /* an empty byte for some reason */
 
570
 
 
571
    int2store(end, server_capabilites);
 
572
    /* write server characteristics: up to 16 bytes allowed */
 
573
    end[2]=(char) default_charset_info->number;
 
574
    int2store(end+3, session->server_status);
 
575
    memset(end+5, 0, 13);
 
576
    end+= 18;
 
577
 
 
578
    /* Write scramble tail. */
 
579
    memset(end, 'X', SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
 
580
    end+= (SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
 
581
    *end++= 0; /* an empty byte for some reason */
 
582
 
 
583
    /* At this point we write connection message and read reply */
 
584
    if (drizzleclient_net_write_command(&net
 
585
          , (unsigned char) PROTOCOL_VERSION
 
586
          , (unsigned char*) ""
 
587
          , 0
 
588
          , (unsigned char*) buff
 
589
          , (size_t) (end-buff)) 
 
590
        ||    (pkt_len= drizzleclient_net_read(&net)) == packet_error 
 
591
        || pkt_len < MIN_HANDSHAKE_SIZE)
 
592
    {
 
593
      my_error(ER_HANDSHAKE_ERROR, MYF(0), session->security_ctx.ip.c_str());
 
594
      return false;
 
595
    }
 
596
  }
 
597
  if (packet.alloc(buffer_length))
 
598
    return false; /* The error is set by alloc(). */
 
599
 
 
600
  client_capabilities= uint2korr(net.read_pos);
 
601
 
 
602
 
 
603
  client_capabilities|= ((uint32_t) uint2korr(net.read_pos + 2)) << 16;
 
604
  session->max_client_packet_length= uint4korr(net.read_pos + 4);
 
605
  end= (char*) net.read_pos + 32;
 
606
 
 
607
  /*
 
608
    Disable those bits which are not supported by the server.
 
609
    This is a precautionary measure, if the client lies. See Bug#27944.
 
610
  */
 
611
  client_capabilities&= server_capabilites;
 
612
 
 
613
  if (end >= (char*) net.read_pos + pkt_len + 2)
 
614
  {
 
615
    my_error(ER_HANDSHAKE_ERROR, MYF(0), session->security_ctx.ip.c_str());
 
616
    return false;
 
617
  }
 
618
 
 
619
  net.return_status= &session->server_status;
 
620
 
 
621
  char *user= end;
 
622
  char *passwd= strchr(user, '\0')+1;
 
623
  uint32_t user_len= passwd - user - 1;
 
624
  char *l_db= passwd;
 
625
 
 
626
  /*
 
627
    Old clients send null-terminated string as password; new clients send
 
628
    the size (1 byte) + string (not null-terminated). Hence in case of empty
 
629
    password both send '\0'.
 
630
 
 
631
    This strlen() can't be easily deleted without changing client.
 
632
 
 
633
    Cast *passwd to an unsigned char, so that it doesn't extend the sign for
 
634
    *passwd > 127 and become 2**32-127+ after casting to uint.
 
635
  */
 
636
  uint32_t passwd_len= client_capabilities & CLIENT_SECURE_CONNECTION ?
 
637
    (unsigned char)(*passwd++) : strlen(passwd);
 
638
  l_db= client_capabilities & CLIENT_CONNECT_WITH_DB ? l_db + passwd_len + 1 : 0;
 
639
 
 
640
  /* strlen() can't be easily deleted without changing client */
 
641
  uint32_t db_len= l_db ? strlen(l_db) : 0;
 
642
 
 
643
  if (passwd + passwd_len + db_len > (char *) net.read_pos + pkt_len)
 
644
  {
 
645
    my_error(ER_HANDSHAKE_ERROR, MYF(0), session->security_ctx.ip.c_str());
 
646
    return false;
 
647
  }
 
648
 
 
649
  /* If username starts and ends in "'", chop them off */
 
650
  if (user_len > 1 && user[0] == '\'' && user[user_len - 1] == '\'')
 
651
  {
 
652
    user[user_len-1]= 0;
 
653
    user++;
 
654
    user_len-= 2;
 
655
  }
 
656
 
 
657
  session->security_ctx.user.assign(user);
 
658
 
 
659
  return session->checkUser(passwd, passwd_len, l_db);
 
660
}
 
661
 
 
662
bool ClientOldLibdrizzle::netStoreData(const unsigned char *from, size_t length)
 
663
{
 
664
  size_t packet_length= packet.length();
 
665
  /*
 
666
     The +9 comes from that strings of length longer than 16M require
 
667
     9 bytes to be stored (see drizzleclient_net_store_length).
 
668
  */
 
669
  if (packet_length+9+length > packet.alloced_length() &&
 
670
      packet.realloc(packet_length+9+length))
 
671
    return 1;
 
672
  unsigned char *to= drizzleclient_net_store_length((unsigned char*) packet.ptr()+packet_length, length);
 
673
  memcpy(to,from,length);
 
674
  packet.length((size_t) (to+length-(unsigned char*) packet.ptr()));
 
675
  return 0;
 
676
}
 
677
 
 
678
/**
 
679
  Format EOF packet according to the current client and
 
680
  write it to the network output buffer.
 
681
*/
 
682
 
 
683
void ClientOldLibdrizzle::writeEOFPacket(uint32_t server_status,
 
684
                                         uint32_t total_warn_count)
 
685
{
 
686
  unsigned char buff[5];
 
687
  /*
 
688
    Don't send warn count during SP execution, as the warn_list
 
689
    is cleared between substatements, and mysqltest gets confused
 
690
  */
 
691
  uint32_t tmp= min(total_warn_count, (uint32_t)65535);
 
692
  buff[0]= DRIZZLE_PROTOCOL_NO_MORE_DATA;
 
693
  int2store(buff+1, tmp);
 
694
  /*
 
695
    The following test should never be true, but it's better to do it
 
696
    because if 'is_fatal_error' is set the server is not going to execute
 
697
    other queries (see the if test in dispatch_command / COM_QUERY)
 
698
  */
 
699
  if (session->is_fatal_error)
 
700
    server_status&= ~SERVER_MORE_RESULTS_EXISTS;
 
701
  int2store(buff + 3, server_status);
 
702
  drizzleclient_net_write(&net, buff, 5);
 
703
}
 
704
 
 
705
static ListenOldLibdrizzle *listen_obj= NULL;
 
706
 
 
707
static int init(drizzled::plugin::Registry &registry)
 
708
{
 
709
  listen_obj= new ListenOldLibdrizzle("oldlibdrizzle");
 
710
  registry.add(listen_obj); 
 
711
  return 0;
 
712
}
 
713
 
 
714
static int deinit(drizzled::plugin::Registry &registry)
 
715
{
 
716
  registry.remove(listen_obj);
 
717
  delete listen_obj;
 
718
  return 0;
 
719
}
 
720
 
 
721
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
 
722
                           PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
 
723
                           NULL, NULL, 10, 1, 300, 0);
 
724
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
 
725
                           N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
 
726
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
 
727
                           N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
 
728
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
 
729
                           N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
 
730
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
 
731
                           N_("Buffer length."), NULL, NULL, 16384, 1024,
 
732
                           1024*1024, 0);
 
733
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
 
734
                          N_("Address to bind to."), NULL, NULL, NULL);
 
735
 
 
736
static struct st_mysql_sys_var* system_variables[]= {
 
737
  DRIZZLE_SYSVAR(connect_timeout),
 
738
  DRIZZLE_SYSVAR(read_timeout),
 
739
  DRIZZLE_SYSVAR(write_timeout),
 
740
  DRIZZLE_SYSVAR(retry_count),
 
741
  DRIZZLE_SYSVAR(buffer_length),
 
742
  DRIZZLE_SYSVAR(bind_address),
 
743
  NULL
 
744
};
 
745
 
 
746
drizzle_declare_plugin(oldlibdrizzle)
 
747
{
 
748
  "oldlibdrizzle",
 
749
  "0.1",
 
750
  "Eric Day",
 
751
  "Old libdrizzle Client",
 
752
  PLUGIN_LICENSE_GPL,
 
753
  init,             /* Plugin Init */
 
754
  deinit,           /* Plugin Deinit */
 
755
  NULL,             /* status variables */
 
756
  system_variables, /* system variables */
 
757
  NULL              /* config options */
 
758
}
 
759
drizzle_declare_plugin_end;