~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/conn.c

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 Connection Definitions
41
 
 */
42
 
 
43
 
#include "common.h"
44
 
 
45
 
/**
46
 
 * @addtogroup drizzle_con_static Static Connection Declarations
47
 
 * @ingroup drizzle_con
48
 
 * @{
49
 
 */
50
 
 
51
 
/**
52
 
 * Set socket options for a connection.
53
 
 *
54
 
 * @param[in] con Connection structure previously initialized with
55
 
 *  drizzle_con_create(), drizzle_con_clone(), or related functions.
56
 
 * @return Standard drizzle return value.
57
 
 */
58
 
static drizzle_return_t _con_setsockopt(drizzle_con_st *con);
59
 
 
60
 
/** @} */
61
 
 
62
 
/*
63
 
 * Common Definitions
64
 
 */
65
 
 
66
 
int drizzle_con_fd(const drizzle_con_st *con)
67
 
{
68
 
  return con->fd;
69
 
}
70
 
 
71
 
drizzle_return_t drizzle_con_set_fd(drizzle_con_st *con, int fd)
72
 
{
73
 
  drizzle_return_t ret;
74
 
 
75
 
  con->fd= fd;
76
 
 
77
 
  ret= _con_setsockopt(con);
78
 
  if (ret != DRIZZLE_RETURN_OK)
79
 
    con->drizzle->last_errno= errno;
80
 
 
81
 
  return ret;
82
 
}
83
 
 
84
 
void drizzle_con_close(drizzle_con_st *con)
85
 
{
86
 
  if (con->fd == -1)
87
 
    return;
88
 
 
89
 
  (void)close(con->fd);
90
 
  con->fd= -1;
91
 
 
92
 
  con->options&= (drizzle_con_options_t)~DRIZZLE_CON_READY;
93
 
  con->packet_number= 0;
94
 
  con->buffer_ptr= con->buffer;
95
 
  con->buffer_size= 0;
96
 
  con->events= 0;
97
 
  con->revents= 0;
98
 
 
99
 
  drizzle_state_reset(con);
100
 
}
101
 
 
102
 
drizzle_return_t drizzle_con_set_events(drizzle_con_st *con, short events)
103
 
{
104
 
  drizzle_return_t ret;
105
 
 
106
 
  if ((con->events | events) == con->events)
107
 
    return DRIZZLE_RETURN_OK;
108
 
 
109
 
  con->events|= events;
110
 
 
111
 
  if (con->drizzle->event_watch_fn != NULL)
112
 
  {
113
 
    ret= con->drizzle->event_watch_fn(con, con->events,
114
 
                                      con->drizzle->event_watch_context);
115
 
    if (ret != DRIZZLE_RETURN_OK)
116
 
    {
117
 
      drizzle_con_close(con);
118
 
      return ret;
119
 
    }
120
 
  }
121
 
 
122
 
  return DRIZZLE_RETURN_OK;
123
 
}
124
 
 
125
 
drizzle_return_t drizzle_con_set_revents(drizzle_con_st *con, short revents)
126
 
{
127
 
  drizzle_return_t ret;
128
 
 
129
 
  if (revents != 0)
130
 
    con->options|= DRIZZLE_CON_IO_READY;
131
 
 
132
 
  con->revents= revents;
133
 
 
134
 
  /* Remove external POLLOUT watch if we didn't ask for it. Otherwise we spin
135
 
     forever until another POLLIN state change. This is much more efficient
136
 
     than removing POLLOUT on every state change since some external polling
137
 
     mechanisms need to use a system call to change flags (like Linux epoll). */
138
 
  if (revents & POLLOUT && !(con->events & POLLOUT) &&
139
 
      con->drizzle->event_watch_fn != NULL)
140
 
  {
141
 
    ret= con->drizzle->event_watch_fn(con, con->events,
142
 
                                      con->drizzle->event_watch_context);
143
 
    if (ret != DRIZZLE_RETURN_OK)
144
 
    {
145
 
      drizzle_con_close(con);
146
 
      return ret;
147
 
    }
148
 
  }
149
 
 
150
 
  con->events&= (short)~revents;
151
 
 
152
 
  return DRIZZLE_RETURN_OK;
153
 
}
154
 
 
155
 
drizzle_st *drizzle_con_drizzle(const drizzle_con_st *con)
156
 
{
157
 
  return con->drizzle;
158
 
}
159
 
 
160
 
const char *drizzle_con_error(const drizzle_con_st *con)
161
 
{
162
 
  return drizzle_error(con->drizzle);
163
 
}
164
 
 
165
 
int drizzle_con_errno(const drizzle_con_st *con)
166
 
{
167
 
  return drizzle_errno(con->drizzle);
168
 
}
169
 
 
170
 
uint16_t drizzle_con_error_code(const drizzle_con_st *con)
171
 
{
172
 
  return drizzle_error_code(con->drizzle);
173
 
}
174
 
 
175
 
const char *drizzle_con_sqlstate(const drizzle_con_st *con)
176
 
{
177
 
  return drizzle_sqlstate(con->drizzle);
178
 
}
179
 
 
180
 
drizzle_con_options_t drizzle_con_options(const drizzle_con_st *con)
181
 
{
182
 
  return con->options;
183
 
}
184
 
 
185
 
void drizzle_con_set_options(drizzle_con_st *con,
186
 
                             drizzle_con_options_t options)
187
 
{
188
 
  con->options= options;
189
 
}
190
 
 
191
 
void drizzle_con_add_options(drizzle_con_st *con,
192
 
                             drizzle_con_options_t options)
193
 
{
194
 
  con->options|= options;
195
 
 
196
 
  /* If asking for the experimental Drizzle protocol, clean the MySQL flag. */
197
 
  if (con->options & DRIZZLE_CON_EXPERIMENTAL)
198
 
    con->options&= (drizzle_con_options_t)~DRIZZLE_CON_MYSQL;
199
 
}
200
 
 
201
 
void drizzle_con_remove_options(drizzle_con_st *con,
202
 
                                drizzle_con_options_t options)
203
 
{
204
 
  con->options&= ~options;
205
 
}
206
 
 
207
 
const char *drizzle_con_host(const drizzle_con_st *con)
208
 
{
209
 
  if (con->socket_type == DRIZZLE_CON_SOCKET_TCP)
210
 
  {
211
 
    if (con->socket.tcp.host == NULL && !(con->options & DRIZZLE_CON_LISTEN))
212
 
      return DRIZZLE_DEFAULT_TCP_HOST;
213
 
 
214
 
    return con->socket.tcp.host;
215
 
  }
216
 
 
217
 
  return NULL;
218
 
}
219
 
 
220
 
in_port_t drizzle_con_port(const drizzle_con_st *con)
221
 
{
222
 
  if (con->socket_type == DRIZZLE_CON_SOCKET_TCP)
223
 
  {
224
 
    if (con->socket.tcp.port != 0)
225
 
      return con->socket.tcp.port;
226
 
 
227
 
    if (con->options & DRIZZLE_CON_MYSQL)
228
 
      return DRIZZLE_DEFAULT_TCP_PORT_MYSQL;
229
 
 
230
 
    return DRIZZLE_DEFAULT_TCP_PORT;
231
 
  }
232
 
 
233
 
  return 0;
234
 
}
235
 
 
236
 
void drizzle_con_set_tcp(drizzle_con_st *con, const char *host, in_port_t port)
237
 
{
238
 
  drizzle_con_reset_addrinfo(con);
239
 
 
240
 
  con->socket_type= DRIZZLE_CON_SOCKET_TCP;
241
 
 
242
 
  if (host == NULL)
243
 
    con->socket.tcp.host= NULL;
244
 
  else
245
 
  {
246
 
    con->socket.tcp.host= con->socket.tcp.host_buffer;
247
 
    strncpy(con->socket.tcp.host, host, NI_MAXHOST);
248
 
    con->socket.tcp.host[NI_MAXHOST - 1]= 0;
249
 
  }
250
 
 
251
 
  con->socket.tcp.port= port;
252
 
}
253
 
 
254
 
const char *drizzle_con_user(const drizzle_con_st *con)
255
 
{
256
 
  return con->user;
257
 
}
258
 
 
259
 
const char *drizzle_con_password(const drizzle_con_st *con)
260
 
{
261
 
  return con->password;
262
 
}
263
 
 
264
 
void drizzle_con_set_auth(drizzle_con_st *con, const char *user,
265
 
                          const char *password)
266
 
{
267
 
  if (user == NULL)
268
 
    con->user[0]= 0;
269
 
  else
270
 
  {
271
 
    strncpy(con->user, user, DRIZZLE_MAX_USER_SIZE);
272
 
    con->user[DRIZZLE_MAX_USER_SIZE - 1]= 0;
273
 
  }
274
 
 
275
 
  if (password == NULL)
276
 
    con->password[0]= 0;
277
 
  else
278
 
  {
279
 
    strncpy(con->password, password, DRIZZLE_MAX_PASSWORD_SIZE);
280
 
    con->password[DRIZZLE_MAX_PASSWORD_SIZE - 1]= 0;
281
 
  }
282
 
}
283
 
 
284
 
const char *drizzle_con_db(const drizzle_con_st *con)
285
 
{
286
 
  return con->db;
287
 
}
288
 
 
289
 
void drizzle_con_set_db(drizzle_con_st *con, const char *db)
290
 
{
291
 
  if (db == NULL)
292
 
    con->db[0]= 0;
293
 
  else
294
 
  {
295
 
    strncpy(con->db, db, DRIZZLE_MAX_DB_SIZE);
296
 
    con->db[DRIZZLE_MAX_DB_SIZE - 1]= 0;
297
 
  }
298
 
}
299
 
 
300
 
void *drizzle_con_context(const drizzle_con_st *con)
301
 
{
302
 
  return con->context;
303
 
}
304
 
 
305
 
void drizzle_con_set_context(drizzle_con_st *con, void *context)
306
 
{
307
 
  con->context= context;
308
 
}
309
 
 
310
 
void drizzle_con_set_context_free_fn(drizzle_con_st *con,
311
 
                                     drizzle_con_context_free_fn *function)
312
 
{
313
 
  con->context_free_fn= function;
314
 
}
315
 
 
316
 
uint8_t drizzle_con_protocol_version(const drizzle_con_st *con)
317
 
{
318
 
  return con->protocol_version;
319
 
}
320
 
 
321
 
const char *drizzle_con_server_version(const drizzle_con_st *con)
322
 
{
323
 
  return con->server_version;
324
 
}
325
 
 
326
 
uint32_t drizzle_con_server_version_number(const drizzle_con_st *con)
327
 
{
328
 
  uint32_t major;
329
 
  uint32_t minor;
330
 
  uint32_t version;
331
 
  const char *current;
332
 
  char *end;
333
 
 
334
 
  current= con->server_version;
335
 
 
336
 
  major= (uint32_t)strtoul(current, &end, 10);
337
 
  current= end + 1;
338
 
  minor= (uint32_t)strtoul(current, &end, 10);
339
 
  current= end + 1;
340
 
  version= (uint32_t)strtoul(current, &end, 10);
341
 
 
342
 
  return (major * 10000) + (minor * 100) + version;
343
 
}
344
 
 
345
 
uint32_t drizzle_con_thread_id(const drizzle_con_st *con)
346
 
{
347
 
  return con->thread_id;
348
 
}
349
 
 
350
 
const uint8_t *drizzle_con_scramble(const drizzle_con_st *con)
351
 
{
352
 
  return con->scramble;
353
 
}
354
 
 
355
 
drizzle_capabilities_t drizzle_con_capabilities(const drizzle_con_st *con)
356
 
{
357
 
  return con->capabilities;
358
 
}
359
 
 
360
 
drizzle_charset_t drizzle_con_charset(const drizzle_con_st *con)
361
 
{
362
 
  return con->charset;
363
 
}
364
 
 
365
 
drizzle_con_status_t drizzle_con_status(const drizzle_con_st *con)
366
 
{
367
 
  return con->status;
368
 
}
369
 
 
370
 
uint32_t drizzle_con_max_packet_size(const drizzle_con_st *con)
371
 
{
372
 
  return con->max_packet_size;
373
 
}
374
 
 
375
 
/*
376
 
 * Client Definitions
377
 
 */
378
 
 
379
 
drizzle_return_t drizzle_con_connect(drizzle_con_st *con)
380
 
{
381
 
  if (con->options & DRIZZLE_CON_READY)
382
 
    return DRIZZLE_RETURN_OK;
383
 
 
384
 
  if (drizzle_state_none(con))
385
 
  {
386
 
    if (!(con->options & DRIZZLE_CON_RAW_PACKET))
387
 
    {
388
 
      drizzle_state_push(con, drizzle_state_handshake_server_read);
389
 
      drizzle_state_push(con, drizzle_state_packet_read);
390
 
    }
391
 
 
392
 
    drizzle_state_push(con, drizzle_state_connect);
393
 
    drizzle_state_push(con, drizzle_state_addrinfo);
394
 
  }
395
 
 
396
 
  return drizzle_state_loop(con);
397
 
}
398
 
 
399
 
drizzle_result_st *drizzle_con_quit(drizzle_con_st *con,
400
 
                                    drizzle_result_st *result,
401
 
                                    drizzle_return_t *ret_ptr)
402
 
{
403
 
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUIT, NULL, 0,
404
 
                                   0, ret_ptr);
405
 
}
406
 
 
407
 
drizzle_result_st *drizzle_quit(drizzle_con_st *con,
408
 
                                drizzle_result_st *result,
409
 
                                drizzle_return_t *ret_ptr)
410
 
{
411
 
  return drizzle_con_quit(con, result, ret_ptr);
412
 
}
413
 
 
414
 
drizzle_result_st *drizzle_con_select_db(drizzle_con_st *con,
415
 
                                         drizzle_result_st *result,
416
 
                                         const char *db,
417
 
                                         drizzle_return_t *ret_ptr)
418
 
{
419
 
  drizzle_con_set_db(con, db);
420
 
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_INIT_DB,
421
 
                                   db, strlen(db), strlen(db), ret_ptr);
422
 
}
423
 
 
424
 
drizzle_result_st *drizzle_select_db(drizzle_con_st *con,
425
 
                                     drizzle_result_st *result,
426
 
                                     const char *db,
427
 
                                     drizzle_return_t *ret_ptr)
428
 
{
429
 
  return drizzle_con_select_db(con, result, db, ret_ptr);
430
 
}
431
 
 
432
 
drizzle_result_st *drizzle_con_shutdown(drizzle_con_st *con,
433
 
                                        drizzle_result_st *result,
434
 
                                        drizzle_return_t *ret_ptr)
435
 
{
436
 
  if (con->options & DRIZZLE_CON_MYSQL)
437
 
  {
438
 
    return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_SHUTDOWN,
439
 
                                     "0", 1, 1, ret_ptr);
440
 
  }
441
 
 
442
 
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_SHUTDOWN, NULL,
443
 
                                   0, 0, ret_ptr);
444
 
}
445
 
 
446
 
drizzle_result_st *drizzle_shutdown(drizzle_con_st *con,
447
 
                                    drizzle_result_st *result, uint32_t level,
448
 
                                    drizzle_return_t *ret_ptr)
449
 
{
450
 
  (void) level;
451
 
  return drizzle_con_shutdown(con, result, ret_ptr);
452
 
}
453
 
 
454
 
drizzle_result_st *drizzle_con_ping(drizzle_con_st *con,
455
 
                                    drizzle_result_st *result,
456
 
                                    drizzle_return_t *ret_ptr)
457
 
{
458
 
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_PING, NULL, 0,
459
 
                                   0, ret_ptr);
460
 
}
461
 
 
462
 
drizzle_result_st *drizzle_ping(drizzle_con_st *con,
463
 
                                drizzle_result_st *result,
464
 
                                drizzle_return_t *ret_ptr)
465
 
{
466
 
  return drizzle_con_ping(con, result, ret_ptr);
467
 
}
468
 
 
469
 
drizzle_result_st *drizzle_con_command_write(drizzle_con_st *con,
470
 
                                             drizzle_result_st *result,
471
 
                                             drizzle_command_t command,
472
 
                                             const void *data, size_t size,
473
 
                                             size_t total,
474
 
                                             drizzle_return_t *ret_ptr)
475
 
{
476
 
  if (!(con->options & DRIZZLE_CON_READY))
477
 
  {
478
 
    if (con->options & DRIZZLE_CON_RAW_PACKET)
479
 
    {
480
 
      drizzle_set_error(con->drizzle, "drizzle_command_write",
481
 
                        "connection not ready");
482
 
      *ret_ptr= DRIZZLE_RETURN_NOT_READY;
483
 
      return result;
484
 
    }
485
 
 
486
 
    *ret_ptr= drizzle_con_connect(con);
487
 
    if (*ret_ptr != DRIZZLE_RETURN_OK)
488
 
      return result;
489
 
  }
490
 
 
491
 
  if (drizzle_state_none(con))
492
 
  {
493
 
    if (con->options & (DRIZZLE_CON_RAW_PACKET | DRIZZLE_CON_NO_RESULT_READ))
494
 
      con->result= NULL;
495
 
    else
496
 
    {
497
 
      con->result= drizzle_result_create(con, result);
498
 
      if (con->result == NULL)
499
 
      {
500
 
        *ret_ptr= DRIZZLE_RETURN_MEMORY;
501
 
        return NULL;
502
 
      }
503
 
    }
504
 
 
505
 
    con->command= command;
506
 
    con->command_data= (uint8_t *)data;
507
 
    con->command_size= size;
508
 
    con->command_offset= 0;
509
 
    con->command_total= total;
510
 
 
511
 
    drizzle_state_push(con, drizzle_state_command_write);
512
 
  }
513
 
  else if (con->command_data == NULL)
514
 
  {
515
 
    con->command_data= (uint8_t *)data;
516
 
    con->command_size= size;
517
 
  }
518
 
 
519
 
  *ret_ptr= drizzle_state_loop(con);
520
 
  if (*ret_ptr == DRIZZLE_RETURN_PAUSE)
521
 
    *ret_ptr= DRIZZLE_RETURN_OK;
522
 
  else if (*ret_ptr != DRIZZLE_RETURN_OK &&
523
 
           *ret_ptr != DRIZZLE_RETURN_IO_WAIT &&
524
 
           *ret_ptr != DRIZZLE_RETURN_ERROR_CODE)
525
 
  {
526
 
    drizzle_result_free(con->result);
527
 
    con->result= result;
528
 
  }
529
 
 
530
 
  return con->result;
531
 
}
532
 
 
533
 
/*
534
 
 * Server Definitions
535
 
 */
536
 
 
537
 
drizzle_return_t drizzle_con_listen(drizzle_con_st *con)
538
 
{
539
 
  if (con->options & DRIZZLE_CON_READY)
540
 
    return DRIZZLE_RETURN_OK;
541
 
 
542
 
  if (drizzle_state_none(con))
543
 
  {
544
 
    drizzle_state_push(con, drizzle_state_listen);
545
 
    drizzle_state_push(con, drizzle_state_addrinfo);
546
 
  }
547
 
 
548
 
  return drizzle_state_loop(con);
549
 
}
550
 
 
551
 
int drizzle_con_backlog(const drizzle_con_st *con)
552
 
{
553
 
  return con->backlog;
554
 
}
555
 
 
556
 
void drizzle_con_set_backlog(drizzle_con_st *con, int backlog)
557
 
{
558
 
  con->backlog= backlog;
559
 
}
560
 
 
561
 
void drizzle_con_set_protocol_version(drizzle_con_st *con,
562
 
                                      uint8_t protocol_version)
563
 
{
564
 
  con->protocol_version= protocol_version;
565
 
}
566
 
 
567
 
void drizzle_con_set_server_version(drizzle_con_st *con,
568
 
                                    const char *server_version)
569
 
{
570
 
  if (server_version == NULL)
571
 
    con->server_version[0]= 0;
572
 
  else
573
 
  {
574
 
    strncpy(con->server_version, server_version,
575
 
            DRIZZLE_MAX_SERVER_VERSION_SIZE);
576
 
    con->server_version[DRIZZLE_MAX_SERVER_VERSION_SIZE - 1]= 0;
577
 
  }
578
 
}
579
 
 
580
 
void drizzle_con_set_thread_id(drizzle_con_st *con, uint32_t thread_id)
581
 
{
582
 
  con->thread_id= thread_id;
583
 
}
584
 
 
585
 
void drizzle_con_set_scramble(drizzle_con_st *con, const uint8_t *scramble)
586
 
{
587
 
  if (scramble == NULL)
588
 
    con->scramble= NULL;
589
 
  else
590
 
  {
591
 
    con->scramble= con->scramble_buffer;
592
 
    memcpy(con->scramble, scramble, DRIZZLE_MAX_SCRAMBLE_SIZE);
593
 
  }
594
 
}
595
 
 
596
 
void drizzle_con_set_capabilities(drizzle_con_st *con,
597
 
                                  drizzle_capabilities_t capabilities)
598
 
{
599
 
  con->capabilities= capabilities;
600
 
}
601
 
 
602
 
void drizzle_con_set_charset(drizzle_con_st *con, drizzle_charset_t charset)
603
 
{
604
 
  con->charset= charset;
605
 
}
606
 
 
607
 
void drizzle_con_set_status(drizzle_con_st *con, drizzle_con_status_t status)
608
 
{
609
 
  con->status= status;
610
 
}
611
 
 
612
 
void drizzle_con_set_max_packet_size(drizzle_con_st *con,
613
 
                                     uint32_t max_packet_size)
614
 
{
615
 
  con->max_packet_size= max_packet_size;
616
 
}
617
 
 
618
 
void drizzle_con_copy_handshake(drizzle_con_st *con, drizzle_con_st *from)
619
 
{
620
 
  drizzle_con_set_auth(con, from->user, NULL);
621
 
  drizzle_con_set_scramble(con, from->scramble);
622
 
  drizzle_con_set_db(con, from->db);
623
 
  drizzle_con_set_protocol_version(con, from->protocol_version);
624
 
  drizzle_con_set_server_version(con, from->server_version);
625
 
  drizzle_con_set_thread_id(con, from->thread_id);
626
 
  drizzle_con_set_scramble(con, from->scramble);
627
 
  drizzle_con_set_capabilities(con, from->capabilities);
628
 
  drizzle_con_set_charset(con, from->charset);
629
 
  drizzle_con_set_status(con, from->status);
630
 
  drizzle_con_set_max_packet_size(con, from->max_packet_size);
631
 
}
632
 
 
633
 
void *drizzle_con_command_read(drizzle_con_st *con,
634
 
                               drizzle_command_t *command, size_t *offset,
635
 
                               size_t *size, size_t *total,
636
 
                               drizzle_return_t *ret_ptr)
637
 
{
638
 
  if (drizzle_state_none(con))
639
 
  {
640
 
    con->packet_number= 0;
641
 
    con->command_offset= 0;
642
 
    con->command_total= 0;
643
 
 
644
 
    drizzle_state_push(con, drizzle_state_command_read);
645
 
    drizzle_state_push(con, drizzle_state_packet_read);
646
 
  }
647
 
 
648
 
  *offset= con->command_offset;
649
 
 
650
 
  *ret_ptr= drizzle_state_loop(con);
651
 
  if (*ret_ptr == DRIZZLE_RETURN_PAUSE)
652
 
    *ret_ptr= DRIZZLE_RETURN_OK;
653
 
 
654
 
  *command= con->command;
655
 
  *size= con->command_size;
656
 
  *total= con->command_total;
657
 
 
658
 
  return con->command_data;
659
 
}
660
 
 
661
 
void *drizzle_con_command_buffer(drizzle_con_st *con,
662
 
                                 drizzle_command_t *command, size_t *total,
663
 
                                 drizzle_return_t *ret_ptr)
664
 
{
665
 
  uint8_t *command_data;
666
 
  size_t offset= 0;
667
 
  size_t size= 0;
668
 
 
669
 
  command_data= drizzle_con_command_read(con, command, &offset, &size, total,
670
 
                                         ret_ptr);
671
 
  if (*ret_ptr != DRIZZLE_RETURN_OK)
672
 
    return NULL;
673
 
 
674
 
  if (command_data == NULL)
675
 
  {
676
 
    *total= 0;
677
 
    return NULL;
678
 
  }
679
 
 
680
 
  if (con->command_buffer == NULL)
681
 
  {
682
 
    con->command_buffer= malloc((*total) + 1);
683
 
    if (con->command_buffer == NULL)
684
 
    {
685
 
      drizzle_set_error(con->drizzle, "drizzle_command_buffer", "malloc");
686
 
      *ret_ptr= DRIZZLE_RETURN_MEMORY;
687
 
      return NULL;
688
 
    }
689
 
  }
690
 
 
691
 
  memcpy(con->command_buffer + offset, command_data, size);
692
 
 
693
 
  while ((offset + size) != (*total))
694
 
  {
695
 
    command_data= drizzle_con_command_read(con, command, &offset, &size, total,
696
 
                                           ret_ptr);
697
 
    if (*ret_ptr != DRIZZLE_RETURN_OK)
698
 
      return NULL;
699
 
 
700
 
    memcpy(con->command_buffer + offset, command_data, size);
701
 
  }
702
 
 
703
 
  command_data= con->command_buffer;
704
 
  con->command_buffer= NULL;
705
 
  command_data[*total]= 0;
706
 
 
707
 
  return command_data;
708
 
}
709
 
 
710
 
/*
711
 
 * Local Definitions
712
 
 */
713
 
 
714
 
void drizzle_con_reset_addrinfo(drizzle_con_st *con)
715
 
{
716
 
  switch (con->socket_type)
717
 
  {
718
 
  case DRIZZLE_CON_SOCKET_TCP:
719
 
    if (con->socket.tcp.addrinfo != NULL)
720
 
    {
721
 
      freeaddrinfo(con->socket.tcp.addrinfo);
722
 
      con->socket.tcp.addrinfo= NULL;
723
 
    }
724
 
    break;
725
 
 
726
 
  case DRIZZLE_CON_SOCKET_UDS:
727
 
    con->socket.uds.addrinfo.ai_addr= NULL;
728
 
    break;
729
 
 
730
 
  default:
731
 
    break;
732
 
  }
733
 
 
734
 
  con->addrinfo_next= NULL;
735
 
}
736
 
 
737
 
/*
738
 
 * State Definitions
739
 
 */
740
 
 
741
 
drizzle_return_t drizzle_state_addrinfo(drizzle_con_st *con)
742
 
{
743
 
  drizzle_con_tcp_st *tcp;
744
 
  const char *host;
745
 
  char port[NI_MAXSERV];
746
 
  struct addrinfo ai;
747
 
  int ret;
748
 
 
749
 
  drizzle_log_debug(con->drizzle, "drizzle_state_addrinfo");
750
 
 
751
 
  switch (con->socket_type)
752
 
  {
753
 
  case DRIZZLE_CON_SOCKET_TCP:
754
 
    tcp= &(con->socket.tcp);
755
 
 
756
 
    if (tcp->addrinfo != NULL)
757
 
    {
758
 
      freeaddrinfo(tcp->addrinfo);
759
 
      tcp->addrinfo= NULL;
760
 
    }
761
 
 
762
 
    if (tcp->port != 0)
763
 
      snprintf(port, NI_MAXSERV, "%u", tcp->port);
764
 
    else if (con->options & DRIZZLE_CON_MYSQL)
765
 
      snprintf(port, NI_MAXSERV, "%u", DRIZZLE_DEFAULT_TCP_PORT_MYSQL);
766
 
    else
767
 
      snprintf(port, NI_MAXSERV, "%u", DRIZZLE_DEFAULT_TCP_PORT);
768
 
 
769
 
    memset(&ai, 0, sizeof(struct addrinfo));
770
 
    ai.ai_socktype= SOCK_STREAM;
771
 
    ai.ai_protocol= IPPROTO_TCP;
772
 
 
773
 
    if (con->options & DRIZZLE_CON_LISTEN)
774
 
    {
775
 
      ai.ai_flags = AI_PASSIVE;
776
 
      ai.ai_family = AF_UNSPEC;
777
 
      host= tcp->host;
778
 
    }
779
 
    else
780
 
    {
781
 
      if (tcp->host == NULL)
782
 
        host= DRIZZLE_DEFAULT_TCP_HOST;
783
 
      else
784
 
        host= tcp->host;
785
 
    }
786
 
 
787
 
    ret= getaddrinfo(host, port, &ai, &(tcp->addrinfo));
788
 
    if (ret != 0)
789
 
    {
790
 
      drizzle_set_error(con->drizzle, "drizzle_state_addrinfo",
791
 
                        "getaddrinfo:%s", gai_strerror(ret));
792
 
      return DRIZZLE_RETURN_GETADDRINFO;
793
 
    }
794
 
 
795
 
    con->addrinfo_next= tcp->addrinfo;
796
 
 
797
 
    break;
798
 
 
799
 
  case DRIZZLE_CON_SOCKET_UDS:
800
 
    con->addrinfo_next= &(con->socket.uds.addrinfo);
801
 
    break;
802
 
 
803
 
  default:
804
 
    break;
805
 
  }
806
 
 
807
 
  drizzle_state_pop(con);
808
 
  return DRIZZLE_RETURN_OK;
809
 
}
810
 
 
811
 
drizzle_return_t drizzle_state_connect(drizzle_con_st *con)
812
 
{
813
 
  int ret;
814
 
  drizzle_return_t dret;
815
 
 
816
 
  drizzle_log_debug(con->drizzle, "drizzle_state_connect");
817
 
 
818
 
  if (con->fd != -1)
819
 
  {
820
 
    (void)close(con->fd);
821
 
    con->fd= -1;
822
 
  }
823
 
 
824
 
  if (con->addrinfo_next == NULL)
825
 
  {
826
 
    drizzle_set_error(con->drizzle, "drizzle_state_connect",
827
 
                      "could not connect");
828
 
    drizzle_state_reset(con);
829
 
    return DRIZZLE_RETURN_COULD_NOT_CONNECT;
830
 
  }
831
 
 
832
 
  con->fd= socket(con->addrinfo_next->ai_family,
833
 
                  con->addrinfo_next->ai_socktype,
834
 
                  con->addrinfo_next->ai_protocol);
835
 
  if (con->fd == -1)
836
 
  {
837
 
    drizzle_set_error(con->drizzle, "drizzle_state_connect", "socket:%d",
838
 
                      errno);
839
 
    con->drizzle->last_errno= errno;
840
 
    return DRIZZLE_RETURN_ERRNO;
841
 
  }
842
 
 
843
 
  dret= _con_setsockopt(con);
844
 
  if (dret != DRIZZLE_RETURN_OK)
845
 
  {
846
 
    con->drizzle->last_errno= errno;
847
 
    return dret;
848
 
  }
849
 
 
850
 
  while (1)
851
 
  {
852
 
    ret= connect(con->fd, con->addrinfo_next->ai_addr,
853
 
                 con->addrinfo_next->ai_addrlen);
854
 
 
855
 
#ifdef _WIN32
856
 
    /*Mapping windows specific error codes to Posix*/
857
 
    errno = WSAGetLastError();
858
 
    switch(errno) {
859
 
    case WSAEINVAL:
860
 
    case WSAEALREADY:
861
 
    case WSAEWOULDBLOCK:
862
 
      errno = EINPROGRESS;
863
 
      break;
864
 
    default:
865
 
      break;
866
 
    }
867
 
#endif /* _WIN32 */
868
 
        
869
 
    drizzle_log_crazy(con->drizzle, "connect return=%d errno=%d", ret, errno);
870
 
 
871
 
    if (ret == 0)
872
 
    {
873
 
      con->addrinfo_next= NULL;
874
 
      break;
875
 
    }
876
 
 
877
 
    if (errno == EAGAIN || errno == EINTR)
878
 
      continue;
879
 
 
880
 
    if (errno == EINPROGRESS)
881
 
    {
882
 
      drizzle_state_pop(con);
883
 
      drizzle_state_push(con, drizzle_state_connecting);
884
 
      return DRIZZLE_RETURN_OK;
885
 
    }
886
 
 
887
 
    if (errno == ECONNREFUSED || errno == ENETUNREACH || errno == ETIMEDOUT)
888
 
    {
889
 
      con->addrinfo_next= con->addrinfo_next->ai_next;
890
 
      return DRIZZLE_RETURN_OK;
891
 
    }
892
 
 
893
 
    drizzle_set_error(con->drizzle, "drizzle_state_connect", "connect:%d",
894
 
                      errno);
895
 
    con->drizzle->last_errno= errno;
896
 
    return DRIZZLE_RETURN_ERRNO;
897
 
  }
898
 
 
899
 
  drizzle_state_pop(con);
900
 
  return DRIZZLE_RETURN_OK;
901
 
}
902
 
 
903
 
drizzle_return_t drizzle_state_connecting(drizzle_con_st *con)
904
 
{
905
 
  drizzle_return_t ret;
906
 
 
907
 
  drizzle_log_debug(con->drizzle, "drizzle_state_connecting");
908
 
 
909
 
  while (1)
910
 
  {
911
 
    if (con->revents & POLLOUT)
912
 
    {
913
 
      drizzle_state_pop(con);
914
 
      return DRIZZLE_RETURN_OK;
915
 
    }
916
 
    else if (con->revents & (POLLERR | POLLHUP | POLLNVAL))
917
 
    {
918
 
      con->revents= 0;
919
 
      drizzle_state_pop(con);
920
 
      drizzle_state_push(con, drizzle_state_connect);
921
 
      con->addrinfo_next= con->addrinfo_next->ai_next;
922
 
      return DRIZZLE_RETURN_OK;
923
 
    }
924
 
 
925
 
    ret= drizzle_con_set_events(con, POLLOUT);
926
 
    if (ret != DRIZZLE_RETURN_OK)
927
 
      return ret;
928
 
 
929
 
    if (con->drizzle->options & DRIZZLE_NON_BLOCKING)
930
 
      return DRIZZLE_RETURN_IO_WAIT;
931
 
 
932
 
    ret= drizzle_con_wait(con->drizzle);
933
 
    if (ret != DRIZZLE_RETURN_OK)
934
 
      return ret;
935
 
  }
936
 
}
937
 
 
938
 
drizzle_return_t drizzle_state_read(drizzle_con_st *con)
939
 
{
940
 
  drizzle_return_t ret;
941
 
  ssize_t read_size;
942
 
 
943
 
  drizzle_log_debug(con->drizzle, "drizzle_state_read");
944
 
 
945
 
  if (con->buffer_size == 0)
946
 
    con->buffer_ptr= con->buffer;
947
 
  else if ((con->buffer_ptr - con->buffer) > (DRIZZLE_MAX_BUFFER_SIZE / 2))
948
 
  {
949
 
    memmove(con->buffer, con->buffer_ptr, con->buffer_size);
950
 
    con->buffer_ptr= con->buffer;
951
 
  }
952
 
 
953
 
  if ((con->revents & POLLIN) == 0 &&
954
 
      (con->drizzle->options & DRIZZLE_NON_BLOCKING))
955
 
  {
956
 
    /* non-blocking mode: return IO_WAIT instead of attempting to read. This
957
 
     * avoids reading immediately after writing a command, which typically
958
 
     * returns EAGAIN. This improves performance. */
959
 
    ret= drizzle_con_set_events(con, POLLIN);
960
 
    if (ret != DRIZZLE_RETURN_OK)
961
 
      return ret;
962
 
    return DRIZZLE_RETURN_IO_WAIT;
963
 
  }
964
 
 
965
 
  while (1)
966
 
  {
967
 
    size_t available_buffer= (size_t)DRIZZLE_MAX_BUFFER_SIZE -
968
 
        ((size_t)(con->buffer_ptr - con->buffer) + con->buffer_size);
969
 
    read_size = recv(con->fd, (char *)con->buffer_ptr + con->buffer_size,
970
 
                     available_buffer, 0);
971
 
#ifdef _WIN32
972
 
    /*Get windows error codes and map it to Posix*/
973
 
    errno = WSAGetLastError();
974
 
    switch(errno) {
975
 
    case WSAEWOULDBLOCK:
976
 
    case 10057:
977
 
      errno = EAGAIN;
978
 
      break;
979
 
    default:
980
 
      break;
981
 
    }
982
 
#endif /* _WIN32 */     
983
 
    drizzle_log_crazy(con->drizzle, "read fd=%d return=%zd errno=%d", con->fd,
984
 
                      read_size, errno);
985
 
 
986
 
    if (read_size == 0)
987
 
    {
988
 
      drizzle_set_error(con->drizzle, "drizzle_state_read",
989
 
                        "lost connection to server (EOF)");
990
 
      return DRIZZLE_RETURN_LOST_CONNECTION;
991
 
    }
992
 
    else if (read_size == -1)
993
 
    {
994
 
      if (errno == EAGAIN)
995
 
      {
996
 
        /* clear the read ready flag */
997
 
        con->revents&= ~POLLIN;
998
 
        ret= drizzle_con_set_events(con, POLLIN);
999
 
        if (ret != DRIZZLE_RETURN_OK)
1000
 
          return ret;
1001
 
 
1002
 
        if (con->drizzle->options & DRIZZLE_NON_BLOCKING)
1003
 
          return DRIZZLE_RETURN_IO_WAIT;
1004
 
 
1005
 
        ret= drizzle_con_wait(con->drizzle);
1006
 
        if (ret != DRIZZLE_RETURN_OK)
1007
 
          return ret;
1008
 
 
1009
 
        continue;
1010
 
      }
1011
 
      else if (errno == ECONNREFUSED)
1012
 
      {
1013
 
        con->revents= 0;
1014
 
        drizzle_state_pop(con);
1015
 
        drizzle_state_push(con, drizzle_state_connect);
1016
 
        con->addrinfo_next= con->addrinfo_next->ai_next;
1017
 
        return DRIZZLE_RETURN_OK;
1018
 
      }
1019
 
      else if (errno == EINTR)
1020
 
        continue;
1021
 
      else if (errno == EPIPE || errno == ECONNRESET)
1022
 
      {
1023
 
        drizzle_set_error(con->drizzle, "drizzle_state_read",
1024
 
                          "lost connection to server (%d)", errno);
1025
 
        return DRIZZLE_RETURN_LOST_CONNECTION;
1026
 
      }
1027
 
 
1028
 
      drizzle_set_error(con->drizzle, "drizzle_state_read", "read:%d", errno);
1029
 
      con->drizzle->last_errno= errno;
1030
 
      return DRIZZLE_RETURN_ERRNO;
1031
 
    }
1032
 
 
1033
 
    /* clear the "read ready" flag if we read all available data. */
1034
 
    if ((size_t) read_size < available_buffer) con->revents&= ~POLLIN;
1035
 
    con->buffer_size+= (size_t)read_size;
1036
 
    break;
1037
 
  }
1038
 
 
1039
 
  drizzle_state_pop(con);
1040
 
  return DRIZZLE_RETURN_OK;
1041
 
}
1042
 
 
1043
 
drizzle_return_t drizzle_state_write(drizzle_con_st *con)
1044
 
{
1045
 
  drizzle_return_t ret;
1046
 
  ssize_t write_size;
1047
 
 
1048
 
  drizzle_log_debug(con->drizzle, "drizzle_state_write");
1049
 
 
1050
 
  while (con->buffer_size != 0)
1051
 
  {
1052
 
  
1053
 
    write_size = send(con->fd,(char *) con->buffer_ptr, con->buffer_size, 0);
1054
 
 
1055
 
    drizzle_log_crazy(con->drizzle, "write fd=%d return=%zd errno=%d", con->fd,
1056
 
                      write_size, errno);
1057
 
 
1058
 
    if (write_size == 0)
1059
 
    {
1060
 
      drizzle_set_error(con->drizzle, "drizzle_state_write",
1061
 
                        "lost connection to server (EOF)");
1062
 
      return DRIZZLE_RETURN_LOST_CONNECTION;
1063
 
    }
1064
 
    else if (write_size == -1)
1065
 
    {
1066
 
      if (errno == EAGAIN)
1067
 
      {
1068
 
        ret= drizzle_con_set_events(con, POLLOUT);
1069
 
        if (ret != DRIZZLE_RETURN_OK)
1070
 
          return ret;
1071
 
 
1072
 
        if (con->drizzle->options & DRIZZLE_NON_BLOCKING)
1073
 
          return DRIZZLE_RETURN_IO_WAIT;
1074
 
 
1075
 
        ret= drizzle_con_wait(con->drizzle);
1076
 
        if (ret != DRIZZLE_RETURN_OK)
1077
 
          return ret;
1078
 
 
1079
 
        continue;
1080
 
      }
1081
 
      else if (errno == EINTR)
1082
 
        continue;
1083
 
      else if (errno == EPIPE || errno == ECONNRESET)
1084
 
      {
1085
 
        drizzle_set_error(con->drizzle, "drizzle_state_write",
1086
 
                          "lost connection to server (%d)", errno);
1087
 
        return DRIZZLE_RETURN_LOST_CONNECTION;
1088
 
      }
1089
 
 
1090
 
      drizzle_set_error(con->drizzle, "drizzle_state_write", "write:%d", errno);
1091
 
      con->drizzle->last_errno= errno;
1092
 
      return DRIZZLE_RETURN_ERRNO;
1093
 
    }
1094
 
 
1095
 
    con->buffer_ptr+= write_size;
1096
 
    con->buffer_size-= (size_t)write_size;
1097
 
    if (con->buffer_size == 0)
1098
 
      break;
1099
 
  }
1100
 
 
1101
 
  con->buffer_ptr= con->buffer;
1102
 
 
1103
 
  drizzle_state_pop(con);
1104
 
  return DRIZZLE_RETURN_OK;
1105
 
}
1106
 
 
1107
 
drizzle_return_t drizzle_state_listen(drizzle_con_st *con)
1108
 
{
1109
 
  char host[NI_MAXHOST];
1110
 
  char port[NI_MAXSERV];
1111
 
  int ret;
1112
 
  int fd;
1113
 
  int opt;
1114
 
  drizzle_con_st *new_con;
1115
 
 
1116
 
  for (; con->addrinfo_next != NULL;
1117
 
       con->addrinfo_next= con->addrinfo_next->ai_next)
1118
 
  {
1119
 
    ret= getnameinfo(con->addrinfo_next->ai_addr,
1120
 
                     con->addrinfo_next->ai_addrlen, host, NI_MAXHOST, port,
1121
 
                     NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV);
1122
 
    if (ret != 0)
1123
 
    {
1124
 
      drizzle_set_error(con->drizzle, "drizzle_state_listen", "getnameinfo:%s",
1125
 
                        gai_strerror(ret));
1126
 
      return DRIZZLE_RETURN_GETADDRINFO;
1127
 
    }
1128
 
 
1129
 
    /* Call to socket() can fail for some getaddrinfo results, try another. */
1130
 
    fd= socket(con->addrinfo_next->ai_family, con->addrinfo_next->ai_socktype,
1131
 
               con->addrinfo_next->ai_protocol);
1132
 
    if (fd == -1)
1133
 
    {
1134
 
      drizzle_log_info(con->drizzle, "could not listen on %s:%s", host, port);
1135
 
      drizzle_set_error(con->drizzle, "drizzle_state_listen", "socket:%d",
1136
 
                        errno);
1137
 
      continue;
1138
 
    }
1139
 
        
1140
 
        opt= 1;
1141
 
#ifdef _WIN32
1142
 
        ret= setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,(const char*) &opt, sizeof(opt));
1143
 
#else
1144
 
        ret= setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
1145
 
#endif /* _WIN32 */
1146
 
    if (ret == -1)
1147
 
    {
1148
 
      close(fd);
1149
 
      drizzle_set_error(con->drizzle, "drizzle_state_listen", "setsockopt:%d",
1150
 
                        errno);
1151
 
      return DRIZZLE_RETURN_ERRNO;
1152
 
    }
1153
 
 
1154
 
    ret= bind(fd, con->addrinfo_next->ai_addr, con->addrinfo_next->ai_addrlen);
1155
 
    if (ret == -1)
1156
 
    {
1157
 
      close(fd);
1158
 
      drizzle_set_error(con->drizzle, "drizzle_state_listen", "bind:%d", errno);
1159
 
      if (errno == EADDRINUSE)
1160
 
      {
1161
 
        if (con->fd == -1)
1162
 
        {
1163
 
          drizzle_log_info(con->drizzle, "could not listen on %s:%s", host,
1164
 
                           port);
1165
 
        }
1166
 
 
1167
 
        continue;
1168
 
      }
1169
 
 
1170
 
      return DRIZZLE_RETURN_ERRNO;
1171
 
    }
1172
 
 
1173
 
    if (listen(fd, con->backlog) == -1)
1174
 
    {
1175
 
      close(fd);
1176
 
      drizzle_set_error(con->drizzle, "drizzle_state_listen", "listen:%d",
1177
 
                        errno);
1178
 
      return DRIZZLE_RETURN_ERRNO;
1179
 
    }
1180
 
 
1181
 
    if (con->fd == -1)
1182
 
    {
1183
 
      con->fd= fd;
1184
 
      new_con= con;
1185
 
    }
1186
 
    else
1187
 
    {
1188
 
      new_con= drizzle_con_clone(con->drizzle, NULL, con);
1189
 
      if (new_con == NULL)
1190
 
      {
1191
 
        close(fd);
1192
 
        return DRIZZLE_RETURN_MEMORY;
1193
 
      }
1194
 
 
1195
 
      new_con->fd= fd;
1196
 
    }
1197
 
 
1198
 
    /* Wait for read events on the listening socket. */
1199
 
    ret= drizzle_con_set_events(new_con, POLLIN);
1200
 
    if (ret != DRIZZLE_RETURN_OK)
1201
 
    {
1202
 
      drizzle_con_free(new_con);
1203
 
      return ret;
1204
 
    }
1205
 
 
1206
 
    drizzle_log_info(con->drizzle, "listening on %s:%s", host, port);
1207
 
  }
1208
 
 
1209
 
  /* Report last socket() error if we couldn't find an address to bind. */
1210
 
  if (con->fd == -1)
1211
 
    return DRIZZLE_RETURN_ERRNO;
1212
 
 
1213
 
  drizzle_state_pop(con);
1214
 
  return DRIZZLE_RETURN_OK;
1215
 
}
1216
 
 
1217
 
/*
1218
 
 * Static Definitions
1219
 
 */
1220
 
 
1221
 
static drizzle_return_t _con_setsockopt(drizzle_con_st *con)
1222
 
{
1223
 
  int ret;
1224
 
  struct linger linger;
1225
 
  struct timeval waittime;
1226
 
 
1227
 
  ret= 1;
1228
 
 
1229
 
#ifdef _WIN32
1230
 
  ret= setsockopt(con->fd, IPPROTO_TCP, TCP_NODELAY, (const char*)&ret,
1231
 
                  (socklen_t)sizeof(int));
1232
 
#else
1233
 
  ret= setsockopt(con->fd, IPPROTO_TCP, TCP_NODELAY, &ret,
1234
 
                  (socklen_t)sizeof(int));
1235
 
#endif /* _WIN32 */
1236
 
 
1237
 
  if (ret == -1 && errno != EOPNOTSUPP)
1238
 
  {
1239
 
    drizzle_set_error(con->drizzle, "_con_setsockopt",
1240
 
                      "setsockopt:TCP_NODELAY:%d", errno);
1241
 
    return DRIZZLE_RETURN_ERRNO;
1242
 
  }
1243
 
 
1244
 
  linger.l_onoff= 1;
1245
 
  linger.l_linger= DRIZZLE_DEFAULT_SOCKET_TIMEOUT;
1246
 
 
1247
 
#ifdef _WIN32
1248
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_LINGER, (const char*)&linger,
1249
 
                  (socklen_t)sizeof(struct linger));
1250
 
#else
1251
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_LINGER, &linger,
1252
 
                  (socklen_t)sizeof(struct linger));
1253
 
#endif /* _WIN32 */
1254
 
 
1255
 
  if (ret == -1)
1256
 
  {
1257
 
    drizzle_set_error(con->drizzle, "_con_setsockopt",
1258
 
                      "setsockopt:SO_LINGER:%d", errno);
1259
 
    return DRIZZLE_RETURN_ERRNO;
1260
 
  }
1261
 
 
1262
 
  waittime.tv_sec= DRIZZLE_DEFAULT_SOCKET_TIMEOUT;
1263
 
  waittime.tv_usec= 0;
1264
 
 
1265
 
#ifdef _WIN32
1266
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&waittime,
1267
 
                  (socklen_t)sizeof(struct timeval));
1268
 
#else
1269
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_SNDTIMEO, &waittime,
1270
 
                  (socklen_t)sizeof(struct timeval));
1271
 
#endif /* _WIN32 */
1272
 
 
1273
 
  if (ret == -1 && errno != ENOPROTOOPT)
1274
 
  {
1275
 
    drizzle_set_error(con->drizzle, "_con_setsockopt",
1276
 
                      "setsockopt:SO_SNDTIMEO:%d", errno);
1277
 
    return DRIZZLE_RETURN_ERRNO;
1278
 
  }
1279
 
 
1280
 
#ifdef _WIN32
1281
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&waittime,
1282
 
                  (socklen_t)sizeof(struct timeval));
1283
 
#else
1284
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_RCVTIMEO, &waittime,
1285
 
                  (socklen_t)sizeof(struct timeval));
1286
 
#endif /* _WIN32 */
1287
 
 
1288
 
  if (ret == -1 && errno != ENOPROTOOPT)
1289
 
  {
1290
 
    drizzle_set_error(con->drizzle, "_con_setsockopt",
1291
 
                      "setsockopt:SO_RCVTIMEO:%d", errno);
1292
 
    return DRIZZLE_RETURN_ERRNO;
1293
 
  }
1294
 
 
1295
 
  ret= DRIZZLE_DEFAULT_SOCKET_SEND_SIZE;
1296
 
#ifdef _WIN32
1297
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_SNDBUF, (const char*)&ret, (socklen_t)sizeof(int));
1298
 
#else
1299
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_SNDBUF, &ret, (socklen_t)sizeof(int));
1300
 
#endif /* _WIN32 */
1301
 
  if (ret == -1)
1302
 
  {
1303
 
    drizzle_set_error(con->drizzle, "_con_setsockopt",
1304
 
                      "setsockopt:SO_SNDBUF:%d", errno);
1305
 
    return DRIZZLE_RETURN_ERRNO;
1306
 
  }
1307
 
 
1308
 
  ret= DRIZZLE_DEFAULT_SOCKET_RECV_SIZE;
1309
 
#ifdef _WIN32
1310
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_RCVBUF, (const char*)&ret, (socklen_t)sizeof(int));
1311
 
#else
1312
 
  ret= setsockopt(con->fd, SOL_SOCKET, SO_RCVBUF, &ret, (socklen_t)sizeof(int));
1313
 
#endif /* _WIN32 */
1314
 
  if (ret == -1)
1315
 
  {
1316
 
    drizzle_set_error(con->drizzle, "_con_setsockopt",
1317
 
                      "setsockopt:SO_RCVBUF:%d", errno);
1318
 
    return DRIZZLE_RETURN_ERRNO;
1319
 
  }
1320
 
 
1321
 
#if defined (_WIN32)
1322
 
  {
1323
 
    unsigned long asyncmode;
1324
 
    asyncmode= 1;
1325
 
    ioctlsocket(con->fd, FIONBIO, &asyncmode);
1326
 
  }
1327
 
#else
1328
 
  ret= fcntl(con->fd, F_GETFL, 0);
1329
 
  if (ret == -1)
1330
 
  {
1331
 
    drizzle_set_error(con->drizzle, "_con_setsockopt", "fcntl:F_GETFL:%d",
1332
 
                      errno);
1333
 
    return DRIZZLE_RETURN_ERRNO;
1334
 
  }
1335
 
 
1336
 
  ret= fcntl(con->fd, F_SETFL, ret | O_NONBLOCK);
1337
 
  if (ret == -1)
1338
 
  {
1339
 
    drizzle_set_error(con->drizzle, "_con_setsockopt", "fcntl:F_SETFL:%d",
1340
 
                      errno);
1341
 
    return DRIZZLE_RETURN_ERRNO;
1342
 
  }
1343
 
#endif
1344
 
 
1345
 
  return DRIZZLE_RETURN_OK;
1346
 
}