1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Library General Public
5
License as published by the Free Software Foundation; either
6
version 2 of the License, or (at your option) any later version.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public
14
License along with this library; if not, write to the Free
15
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
#ifndef HAVE_VIO /* is Vio suppored by the Vio lib ? */
30
#include <sys/ioctl.h>
31
#define ioctlsocket(A,B,C) ioctl((A),(B),(void *)(C),sizeof(*(C)))
33
#endif /* defined(__EMX__) */
35
#if defined(MSDOS) || defined(__WIN__)
40
#define errno WSAGetLastError()
41
#define EINTR WSAEINTR
42
#define EAGAIN WSAEINPROGRESS
44
#define O_NONBLOCK 1 /* For emulation of fcntl() */
47
#define EWOULDBLOCK EAGAIN
56
my_socket sd; /* my_socket - real or imaginary */
58
my_bool localhost; /* Are we from localhost? */
59
int fcntl_mode; /* Buffered fcntl(sd,F_GETFL) */
60
my_bool fcntl_set; /* Have we done any fcntl yet?*/
61
struct sockaddr_in local; /* Local internet address */
62
struct sockaddr_in remote; /* Remote internet address */
63
enum enum_vio_type type; /* Type of connection */
64
char desc[30]; /* String description */
67
typedef void *vio_ptr;
68
typedef char *vio_cstring;
71
* Helper to fill most of the Vio* with defaults.
74
static void vio_reset(Vio* vio, enum enum_vio_type type,
75
my_socket sd, HANDLE hPipe,
78
bzero((char*) vio, sizeof(*vio));
82
vio->localhost= localhost;
85
Vio *vio_new(my_socket sd, enum enum_vio_type type, my_bool localhost)
88
DBUG_ENTER("vio_new");
89
DBUG_PRINT("enter", ("sd=%d", sd));
90
if ((vio = (Vio*) my_malloc(sizeof(*vio),MYF(MY_WME))))
92
vio_reset(vio, type, sd, 0, localhost);
93
sprintf(vio->desc, "socket (%d)", vio->sd);
101
Vio *vio_new_win32pipe(HANDLE hPipe)
104
DBUG_ENTER("vio_new_handle");
105
if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME))))
107
vio_reset(vio, VIO_TYPE_NAMEDPIPE, 0, hPipe, TRUE);
108
strmov(vio->desc, "named pipe");
115
void vio_delete(Vio * vio)
117
/* It must be safe to delete null pointers. */
118
/* This matches the semantics of C++'s delete operator. */
122
my_free((gptr) vio,MYF(0));
126
int vio_errno(Vio *vio)
128
return errno; /* On Win32 this mapped to WSAGetLastError() */
132
int vio_read(Vio * vio, gptr buf, int size)
135
DBUG_ENTER("vio_read");
136
DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size));
137
assert(vio->sd >= 0);
139
if (vio->type == VIO_TYPE_NAMEDPIPE)
142
if (!ReadFile(vio->hPipe, buf, size, &length, NULL))
146
r = recv(vio->sd, buf, size,0);
148
errno=0; /* For linux */
149
r = read(vio->sd, buf, size);
154
DBUG_PRINT("error", ("Got error %d during read",errno));
156
#endif /* DBUG_OFF */
157
DBUG_PRINT("exit", ("%d", r));
162
int vio_write(Vio * vio, const gptr buf, int size)
165
DBUG_ENTER("vio_write");
166
DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size));
167
assert(vio->sd >= 0);
169
if ( vio->type == VIO_TYPE_NAMEDPIPE)
172
if (!WriteFile(vio->hPipe, (char*) buf, size, &length, NULL))
176
r = send(vio->sd, buf, size,0);
178
r = write(vio->sd, buf, size);
183
DBUG_PRINT("error", ("Got error on write: %d",errno));
185
#endif /* DBUG_OFF */
186
DBUG_PRINT("exit", ("%d", r));
191
int vio_blocking(Vio * vio, my_bool set_blocking_mode)
194
DBUG_ENTER("vio_blocking");
195
DBUG_PRINT("enter", ("set_blocking_mode: %d", (int) set_blocking_mode));
197
#if !defined(___WIN__) && !defined(__EMX__)
198
#if !defined(NO_FCNTL_NONBLOCK)
202
int old_fcntl=vio->fcntl_mode;
205
vio->fcntl_set = TRUE;
206
old_fcntl=vio->fcntl_mode = fcntl(vio->sd, F_GETFL);
208
if (set_blocking_mode)
209
vio->fcntl_mode &= ~O_NONBLOCK; //clear bit
211
vio->fcntl_mode |= O_NONBLOCK; //set bit
212
if (old_fcntl != vio->fcntl_mode)
213
r = fcntl(vio->sd, F_SETFL, vio->fcntl_mode);
215
#endif /* !defined(NO_FCNTL_NONBLOCK) */
216
#else /* !defined(__WIN__) && !defined(__EMX__) */
218
if (vio->type != VIO_TYPE_NAMEDPIPE)
222
int old_fcntl=vio->fcntl_mode;
225
vio->fcntl_set = TRUE;
226
old_fnctl=vio->fcntl_mode=0;
228
if (set_blocking_mode)
231
vio->fcntl_mode &= ~O_NONBLOCK; //clear bit
236
vio->fcntl_mode |= O_NONBLOCK; //set bit
238
if (old_fcntl != vio->fcntl_mode)
239
r = ioctlsocket(vio->sd,FIONBIO,(void*) &arg, sizeof(arg));
241
#endif /* !defined(__WIN__) && !defined(__EMX__) */
246
vio_is_blocking(Vio * vio)
249
DBUG_ENTER("vio_is_blocking");
250
r = !(vio->fcntl_mode & O_NONBLOCK);
251
DBUG_PRINT("exit", ("%d", (int) r));
256
int vio_fastsend(Vio * vio, my_bool onoff)
259
DBUG_ENTER("vio_fastsend");
260
DBUG_PRINT("enter", ("onoff:%d", (int) onoff));
261
assert(vio->sd >= 0);
263
#ifdef IPTOS_THROUGHPUT
266
int tos = IPTOS_THROUGHPUT;
267
if (!setsockopt(vio->sd, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(tos)))
268
#endif /* !__EMX__ */
271
if (setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY, (void *) &nodelay,
273
DBUG_PRINT("warning",
274
("Couldn't set socket option for fast send"));
279
#endif /* IPTOS_THROUGHPUT */
280
DBUG_PRINT("exit", ("%d", r));
284
int vio_keepalive(Vio* vio, my_bool set_keep_alive)
288
DBUG_ENTER("vio_keepalive");
289
DBUG_PRINT("enter", ("sd=%d, set_keep_alive=%d", vio->sd, (int)
291
if (vio->type != VIO_TYPE_NAMEDPIPE)
293
assert(vio->sd >= 0);
296
r = setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt,
304
vio_should_retry(Vio * vio)
307
return en == EAGAIN || en == EINTR || en == EWOULDBLOCK;
311
int vio_close(Vio * vio)
314
DBUG_ENTER("vio_close");
315
assert(vio->sd >= 0); /* Vill also work on PIPE:s */
317
if (vio->type == VIO_TYPE_NAMEDPIPE)
319
#if defined(__NT__) && defined(MYSQL_SERVER)
320
CancelIO(vio->hPipe);
321
DisconnectNamedPipe(vio->hPipe);
323
r=CloseHandle(vio->hPipe);
329
if (shutdown(vio->sd,2))
331
if (closesocket(vio->sd))
336
DBUG_PRINT("error", ("close() failed, error: %d",errno));
337
/* FIXME: error handling (not critical for MySQL) */
339
vio_reset(vio,VIO_CLOSED,-1,0,TRUE); /* For debugging */
344
const char *vio_description(Vio * vio)
349
enum enum_vio_type vio_type(Vio* vio)
354
my_socket vio_fd(Vio* vio)
360
my_bool vio_peer_addr(Vio * vio, char *buf)
362
DBUG_ENTER("vio_peer_addr");
363
DBUG_PRINT("enter", ("sd=%d", vio->sd));
366
strmov(buf,"127.0.0.1");
370
size_socket addrLen = sizeof(struct sockaddr);
371
if (getpeername(vio->sd, (struct sockaddr *) (& (vio->remote)),
374
DBUG_PRINT("exit", ("getpeername, error: %d", errno));
377
my_inet_ntoa(vio->remote.sin_addr,buf);
379
DBUG_PRINT("exit", ("addr=%s", buf));
384
void vio_in_addr(Vio *vio, struct in_addr *in)
386
DBUG_ENTER("vio_in_addr");
388
bzero((char*) in, sizeof(*in)); /* This should never be executed */
390
*in=vio->remote.sin_addr;
394
#endif /* HAVE_VIO */