23
23
#include "vio_priv.h"
24
#include <sys/socket.h>
25
#include <drizzled/util/test.h>
27
25
int vio_errno(Vio *vio __attribute__((unused)))
27
return socket_errno; /* On Win32 this mapped to WSAGetLastError() */
33
size_t vio_read(Vio * vio, unsigned char* buf, size_t size)
31
size_t vio_read(Vio * vio, uchar* buf, size_t size)
37
35
/* Ensure nobody uses vio_read_buff and vio_read simultaneously */
38
36
assert(vio->read_end == vio->read_pos);
39
r= read(vio->sd, buf, size);
37
errno=0; /* For linux */
38
r = read(vio->sd, buf, size);
47
45
reduce number of syscalls.
50
size_t vio_read_buff(Vio *vio, unsigned char* buf, size_t size)
48
size_t vio_read_buff(Vio *vio, uchar* buf, size_t size)
53
51
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
55
53
if (vio->read_pos < vio->read_end)
57
rc= cmin((size_t) (vio->read_end - vio->read_pos), size);
55
rc= min((size_t) (vio->read_end - vio->read_pos), size);
58
56
memcpy(buf, vio->read_pos, rc);
59
57
vio->read_pos+= rc;
66
64
else if (size < VIO_UNBUFFERED_READ_MIN_SIZE)
68
rc= vio_read(vio, (unsigned char*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
66
rc= vio_read(vio, (uchar*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
69
67
if (rc != 0 && rc != (size_t) -1)
97
int vio_blocking(Vio * vio, bool set_blocking_mode, bool *old_mode)
95
int vio_blocking(Vio * vio __attribute__((unused)), my_bool set_blocking_mode,
101
100
*old_mode= test(!(vio->fcntl_mode & O_NONBLOCK));
102
#if !defined(NO_FCNTL_NONBLOCK)
103
103
if (vio->sd >= 0)
105
105
int old_fcntl=vio->fcntl_mode;
133
135
int vio_fastsend(Vio * vio __attribute__((unused)))
138
error= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
139
&nodelay, sizeof(nodelay));
142
perror("setsockopt");
139
#if defined(IPTOS_THROUGHPUT)
141
int tos = IPTOS_THROUGHPUT;
142
r= setsockopt(vio->sd, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(tos));
144
#endif /* IPTOS_THROUGHPUT */
149
r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
150
IF_WIN(const char*, void*) &nodelay,
149
int32_t vio_keepalive(Vio* vio, bool set_keep_alive)
162
int vio_keepalive(Vio* vio, my_bool set_keep_alive)
157
r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
167
if (vio->type != VIO_TYPE_NAMEDPIPE)
160
perror("setsockopt");
171
r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt,
169
180
vio_should_retry(Vio * vio __attribute__((unused)))
172
return (en == EAGAIN || en == EINTR ||
182
int en = socket_errno;
183
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR ||
184
en == SOCKET_EWOULDBLOCK);
178
189
vio_was_interrupted(Vio *vio __attribute__((unused)))
181
return (en == EAGAIN || en == EINTR ||
182
en == EWOULDBLOCK || en == ETIMEDOUT);
191
int en= socket_errno;
192
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR ||
193
en == SOCKET_EWOULDBLOCK || en == SOCKET_ETIMEDOUT);
211
222
return vio->type;
225
my_socket vio_fd(Vio* vio)
219
bool vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
230
my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen)
222
char port_buf[NI_MAXSERV];
223
socklen_t addrLen = sizeof(vio->remote);
225
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
230
vio->addrLen= (int)addrLen;
232
if ((error= getnameinfo((struct sockaddr *)(&vio->remote),
235
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
240
*port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
234
strmov(buf, "127.0.0.1");
240
char port_buf[NI_MAXSERV];
241
size_socket addrLen = sizeof(vio->remote);
242
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
247
vio->addrLen= (int)addrLen;
249
if ((error= getnameinfo((struct sockaddr *)(&vio->remote),
252
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
257
*port= (uint16)strtol(port_buf, (char **)NULL, 10);
260
A lot of users do not have IPv6 loopback resolving to localhost
261
correctly setup. Should this exist? No. If we do not do it though
262
we will be getting a lot of support questions from users who
263
have bad setups. This code should be removed by say... 2012.
266
if (!memcmp(buf, "::ffff:127.0.0.1", sizeof("::ffff:127.0.0.1")))
267
strmov(buf, "127.0.0.1");
258
287
return res < 0 ? false : true; /* Don't return 1 on errors */
260
289
return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true);
264
bool vio_peek_read(Vio *vio, uint32_t *bytes)
296
my_bool vio_peek_read(Vio *vio, uint *bytes)
298
#if FIONREAD_IN_SYS_IOCTL
300
if (ioctl(vio->sd, FIONREAD, &len) < 0)
267
306
ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
271
*bytes= (uint32_t)res;
275
void vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout)
314
void vio_timeout(Vio *vio, uint which, uint timeout)
316
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
279
320
/* POSIX specifies time as struct timeval. */
280
321
struct timeval wait_timeout;
281
322
wait_timeout.tv_sec= timeout;
282
323
wait_timeout.tv_usec= 0;
284
assert(timeout >= 0 && timeout <= INT32_MAX);
285
assert(vio->sd != -1);
286
error= setsockopt(vio->sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
288
(socklen_t)sizeof(struct timeval));
291
perror("setsockopt");
325
r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO,
326
IF_WIN(const char*, const void*)&wait_timeout,
327
sizeof(wait_timeout));
332
Platforms not suporting setting of socket timeout should either use
333
thr_alarm or just run without read/write timeout(s)