~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzleclient/viosocket.c

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/*
 
17
  Note that we can't have assertion on file descriptors;  The reason for
 
18
  this is that during mysql shutdown, another thread can close a file
 
19
  we are working on.  In this case we should just return read errors from
 
20
  the file descriptior.
 
21
*/
 
22
 
 
23
#define DONT_MAP_VIO
 
24
#include <drizzled/global.h>
 
25
#include "vio.h"
 
26
#include <drizzled/util/test.h>
 
27
 
 
28
#include <sys/socket.h>
 
29
#include <string.h>
 
30
 
 
31
#include <netinet/tcp.h>
 
32
#include <sys/poll.h>
 
33
 
 
34
#include <netdb.h>
 
35
 
 
36
int drizzleclient_vio_errno(Vio *vio)
 
37
{
 
38
  (void)vio;
 
39
  return errno;
 
40
}
 
41
 
 
42
 
 
43
size_t drizzleclient_vio_read(Vio * vio, unsigned char* buf, size_t size)
 
44
{
 
45
  size_t r;
 
46
 
 
47
  /* Ensure nobody uses drizzleclient_vio_read_buff and drizzleclient_vio_read simultaneously */
 
48
  assert(vio->read_end == vio->read_pos);
 
49
  r= read(vio->sd, buf, size);
 
50
 
 
51
  return r;
 
52
}
 
53
 
 
54
 
 
55
/*
 
56
  Buffered read: if average read size is small it may
 
57
  reduce number of syscalls.
 
58
*/
 
59
 
 
60
size_t drizzleclient_vio_read_buff(Vio *vio, unsigned char* buf, size_t size)
 
61
{
 
62
  size_t rc;
 
63
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
 
64
 
 
65
  if (vio->read_pos < vio->read_end)
 
66
  {
 
67
    rc= cmin((size_t) (vio->read_end - vio->read_pos), size);
 
68
    memcpy(buf, vio->read_pos, rc);
 
69
    vio->read_pos+= rc;
 
70
    /*
 
71
      Do not try to read from the socket now even if rc < size:
 
72
      drizzleclient_vio_read can return -1 due to an error or non-blocking mode, and
 
73
      the safest way to handle it is to move to a separate branch.
 
74
    */
 
75
  }
 
76
  else if (size < VIO_UNBUFFERED_READ_MIN_SIZE)
 
77
  {
 
78
    rc= drizzleclient_vio_read(vio, (unsigned char*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
 
79
    if (rc != 0 && rc != (size_t) -1)
 
80
    {
 
81
      if (rc > size)
 
82
      {
 
83
        vio->read_pos= vio->read_buffer + size;
 
84
        vio->read_end= vio->read_buffer + rc;
 
85
        rc= size;
 
86
      }
 
87
      memcpy(buf, vio->read_buffer, rc);
 
88
    }
 
89
  }
 
90
  else
 
91
    rc= drizzleclient_vio_read(vio, buf, size);
 
92
 
 
93
  return rc;
 
94
#undef VIO_UNBUFFERED_READ_MIN_SIZE
 
95
}
 
96
 
 
97
 
 
98
size_t drizzleclient_vio_write(Vio * vio, const unsigned char* buf, size_t size)
 
99
{
 
100
  size_t r;
 
101
 
 
102
  r = write(vio->sd, buf, size);
 
103
 
 
104
  return r;
 
105
}
 
106
 
 
107
int drizzleclient_vio_blocking(Vio * vio, bool set_blocking_mode, bool *old_mode)
 
108
{
 
109
  int r=0;
 
110
 
 
111
  *old_mode= test(!(vio->fcntl_mode & O_NONBLOCK));
 
112
 
 
113
  if (vio->sd >= 0)
 
114
  {
 
115
    int old_fcntl=vio->fcntl_mode;
 
116
    if (set_blocking_mode)
 
117
      vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
 
118
    else
 
119
      vio->fcntl_mode |= O_NONBLOCK; /* set bit */
 
120
    if (old_fcntl != vio->fcntl_mode)
 
121
    {
 
122
      r= fcntl(vio->sd, F_SETFL, vio->fcntl_mode);
 
123
      if (r == -1)
 
124
      {
 
125
        vio->fcntl_mode= old_fcntl;
 
126
      }
 
127
    }
 
128
  }
 
129
 
 
130
  return r;
 
131
}
 
132
 
 
133
bool
 
134
drizzleclient_vio_is_blocking(Vio * vio)
 
135
{
 
136
  bool r;
 
137
  r = !(vio->fcntl_mode & O_NONBLOCK);
 
138
 
 
139
  return r;
 
140
}
 
141
 
 
142
 
 
143
int drizzleclient_vio_fastsend(Vio * vio)
 
144
{
 
145
  (void)vio;
 
146
  int nodelay = 1;
 
147
  int error;
 
148
 
 
149
  error= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
 
150
                    &nodelay, sizeof(nodelay));
 
151
  if (error != 0)
 
152
  {
 
153
    perror("setsockopt");
 
154
    assert(error == 0);
 
155
  }
 
156
 
 
157
  return error;
 
158
}
 
159
 
 
160
int32_t drizzleclient_vio_keepalive(Vio* vio, bool set_keep_alive)
 
161
{
 
162
  int r= 0;
 
163
  uint32_t opt= 0;
 
164
 
 
165
  if (set_keep_alive)
 
166
    opt= 1;
 
167
 
 
168
  r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
 
169
  if (r != 0)
 
170
  {
 
171
    perror("setsockopt");
 
172
    assert(r == 0);
 
173
  }
 
174
 
 
175
  return r;
 
176
}
 
177
 
 
178
 
 
179
bool
 
180
drizzleclient_vio_should_retry(Vio * vio)
 
181
{
 
182
  (void)vio;
 
183
  int en = errno;
 
184
  return (en == EAGAIN || en == EINTR ||
 
185
          en == EWOULDBLOCK);
 
186
}
 
187
 
 
188
 
 
189
bool
 
190
drizzleclient_vio_was_interrupted(Vio *vio)
 
191
{
 
192
  (void)vio;
 
193
  int en= errno;
 
194
  return (en == EAGAIN || en == EINTR ||
 
195
          en == EWOULDBLOCK || en == ETIMEDOUT);
 
196
}
 
197
 
 
198
 
 
199
int drizzleclient_vio_close(Vio * vio)
 
200
{
 
201
  int r=0;
 
202
 if (vio->type != VIO_CLOSED)
 
203
  {
 
204
    assert(vio->sd >= 0);
 
205
    if (shutdown(vio->sd, SHUT_RDWR))
 
206
      r= -1;
 
207
    if (close(vio->sd))
 
208
      r= -1;
 
209
  }
 
210
  vio->type= VIO_CLOSED;
 
211
  vio->sd=   -1;
 
212
 
 
213
  return r;
 
214
}
 
215
 
 
216
 
 
217
const char *drizzleclient_vio_description(Vio * vio)
 
218
{
 
219
  return vio->desc;
 
220
}
 
221
 
 
222
enum enum_vio_type drizzleclient_vio_type(Vio* vio)
 
223
{
 
224
  return vio->type;
 
225
}
 
226
 
 
227
int drizzleclient_vio_fd(Vio* vio)
 
228
{
 
229
  return vio->sd;
 
230
}
 
231
 
 
232
bool drizzleclient_vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
 
233
{
 
234
  int error;
 
235
  char port_buf[NI_MAXSERV];
 
236
  socklen_t addrLen = sizeof(vio->remote);
 
237
 
 
238
  if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
 
239
                  &addrLen) != 0)
 
240
  {
 
241
    return true;
 
242
  }
 
243
  vio->addrLen= (int)addrLen;
 
244
 
 
245
  if ((error= getnameinfo((struct sockaddr *)(&vio->remote),
 
246
                          addrLen,
 
247
                          buf, buflen,
 
248
                          port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
 
249
  {
 
250
    return true;
 
251
  }
 
252
 
 
253
  *port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
 
254
 
 
255
  return false;
 
256
}
 
257
 
 
258
 
 
259
/* Return 0 if there is data to be read */
 
260
 
 
261
bool drizzleclient_vio_poll_read(Vio *vio, int32_t timeout)
 
262
{
 
263
  struct pollfd fds;
 
264
  int res;
 
265
 
 
266
  fds.fd=vio->sd;
 
267
  fds.events=POLLIN;
 
268
  fds.revents=0;
 
269
  if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)
 
270
  {
 
271
    return res < 0 ? false : true;              /* Don't return 1 on errors */
 
272
  }
 
273
  return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true);
 
274
}
 
275
 
 
276
 
 
277
bool drizzleclient_vio_peek_read(Vio *vio, uint32_t *bytes)
 
278
{
 
279
  char buf[1024];
 
280
  ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
 
281
 
 
282
  if (res < 0)
 
283
    return true;
 
284
  *bytes= (uint32_t)res;
 
285
  return false;
 
286
}
 
287
 
 
288
void drizzleclient_vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout)
 
289
{
 
290
  int error;
 
291
 
 
292
  /* POSIX specifies time as struct timeval. */
 
293
  struct timeval wait_timeout;
 
294
  wait_timeout.tv_sec= timeout;
 
295
  wait_timeout.tv_usec= 0;
 
296
 
 
297
  assert(timeout >= 0 && timeout <= INT32_MAX);
 
298
  assert(vio->sd != -1);
 
299
  error= setsockopt(vio->sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
 
300
                    &wait_timeout,
 
301
                    (socklen_t)sizeof(struct timeval));
 
302
  if (error == -1 && errno != ENOPROTOOPT)
 
303
  {
 
304
    perror("setsockopt");
 
305
    assert(error == 0);
 
306
  }
 
307
}