~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/oldlibdrizzle/oldlibdrizzle.cc

  • Committer: Stewart Smith
  • Date: 2009-06-16 06:55:11 UTC
  • mto: This revision was merged to the branch mainline in revision 1094.
  • Revision ID: stewart@flamingspork.com-20090616065511-ps3ewfxj7918lwy3
rollback.test for MyISAM temp only.
- rename to myisam_rollback to reflect what it's testing
- just use create temporary table

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