luasocket/src/wsocket.c

395 lines
15 KiB
C
Raw Normal View History

/*=========================================================================*\
* Socket compatibilization module for Win32
* LuaSocket toolkit
*
2004-07-02 20:44:05 +02:00
* The penalty of calling select to avoid busy-wait is only paid when
* the I/O call fail in the first place.
2004-01-17 09:02:04 +01:00
*
* RCS ID: $Id$
\*=========================================================================*/
#include <string.h>
#include "socket.h"
2004-07-02 20:44:05 +02:00
/* WinSock doesn't have a strerror... */
static const char *wstrerror(int err);
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
2004-07-02 20:44:05 +02:00
int sock_open(void) {
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 0);
int err = WSAStartup(wVersionRequested, &wsaData );
if (err != 0) return 0;
2005-09-29 08:11:42 +02:00
if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) &&
(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) {
WSACleanup();
return 0;
}
return 1;
}
2004-05-30 23:36:22 +02:00
/*-------------------------------------------------------------------------*\
* Close module
\*-------------------------------------------------------------------------*/
2004-07-02 20:44:05 +02:00
int sock_close(void) {
2004-05-30 23:36:22 +02:00
WSACleanup();
return 1;
}
/*-------------------------------------------------------------------------*\
* Wait for readable/writable/connected socket with timeout
\*-------------------------------------------------------------------------*/
#define WAITFD_R 1
#define WAITFD_W 2
#define WAITFD_E 4
#define WAITFD_C (WAITFD_E|WAITFD_W)
int sock_waitfd(p_sock ps, int sw, p_tm tm) {
int ret;
fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
struct timeval tv, *tp = NULL;
double t;
if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; }
if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; }
if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; }
if ((t = tm_get(tm)) >= 0.0) {
tv.tv_sec = (int) t;
tv.tv_usec = (int) ((t-tv.tv_sec)*1.0e6);
tp = &tv;
}
ret = select(0, rp, wp, ep, tp);
if (ret == -1) return WSAGetLastError();
if (ret == 0) return IO_TIMEOUT;
if (sw == WAITFD_C && FD_ISSET(*ps, &efds)) return IO_CLOSED;
return IO_DONE;
}
/*-------------------------------------------------------------------------*\
* Select with int timeout in ms
\*-------------------------------------------------------------------------*/
2004-07-02 20:44:05 +02:00
int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) {
struct timeval tv;
2004-07-02 20:44:05 +02:00
double t = tm_get(tm);
tv.tv_sec = (int) t;
tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6);
2005-08-12 07:56:32 +02:00
if (n <= 0) {
Sleep((DWORD) (1000*t));
2005-08-12 07:56:32 +02:00
return 0;
} else return select(0, rfds, wfds, efds, t >= 0.0? &tv: NULL);
}
/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
2004-07-02 20:44:05 +02:00
void sock_destroy(p_sock ps) {
if (*ps != SOCK_INVALID) {
sock_setblocking(ps); /* close can take a long time on WIN32 */
closesocket(*ps);
*ps = SOCK_INVALID;
}
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
2004-07-02 20:44:05 +02:00
void sock_shutdown(p_sock ps, int how) {
sock_setblocking(ps);
shutdown(*ps, how);
sock_setnonblocking(ps);
}
/*-------------------------------------------------------------------------*\
* Creates and sets up a socket
\*-------------------------------------------------------------------------*/
int sock_create(p_sock ps, int domain, int type, int protocol) {
*ps = socket(domain, type, protocol);
if (*ps != SOCK_INVALID) return IO_DONE;
else return WSAGetLastError();
}
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
2004-07-02 20:44:05 +02:00
int err;
/* don't call on closed socket */
if (*ps == SOCK_INVALID) return IO_CLOSED;
/* ask system to connect */
if (connect(*ps, addr, len) == 0) return IO_DONE;
/* make sure the system is trying to connect */
2004-07-02 20:44:05 +02:00
err = WSAGetLastError();
if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err;
2005-02-08 11:01:01 +01:00
/* zero timeout case optimization */
if (tm_iszero(tm)) return IO_TIMEOUT;
/* we wait until something happens */
2005-09-29 08:11:42 +02:00
err = sock_waitfd(ps, WAITFD_C, tm);
if (err == IO_CLOSED) {
int len = sizeof(err);
/* give windows time to set the error (yes, disgusting) */
2005-03-11 03:21:27 +01:00
Sleep(10);
/* find out why we failed */
getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len);
/* we KNOW there was an error. if 'why' is 0, we will return
2005-02-08 11:01:01 +01:00
* "unknown error", but it's not really our fault */
return err > 0? err: IO_UNKNOWN;
} else return err;
2005-09-29 08:11:42 +02:00
}
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
int sock_bind(p_sock ps, SA *addr, socklen_t len) {
int err = IO_DONE;
sock_setblocking(ps);
if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
sock_setnonblocking(ps);
return err;
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
int sock_listen(p_sock ps, int backlog) {
int err = IO_DONE;
sock_setblocking(ps);
if (listen(*ps, backlog) < 0) err = WSAGetLastError();
sock_setnonblocking(ps);
return err;
}
/*-------------------------------------------------------------------------*\
* Accept with timeout
\*-------------------------------------------------------------------------*/
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) {
SA daddr;
socklen_t dlen = sizeof(daddr);
if (*ps == SOCK_INVALID) return IO_CLOSED;
if (!addr) addr = &daddr;
if (!len) len = &dlen;
for ( ;; ) {
int err;
/* try to get client socket */
if ((*pa = accept(*ps, addr, len)) != SOCK_INVALID) return IO_DONE;
/* find out why we failed */
err = WSAGetLastError();
2004-07-02 20:44:05 +02:00
/* if we failed because there was no connectoin, keep trying */
if (err != WSAEWOULDBLOCK && err != WSAECONNABORTED) return err;
/* call select to avoid busy wait */
if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
}
/* can't reach here */
return IO_UNKNOWN;
}
/*-------------------------------------------------------------------------*\
* Send with timeout
2004-11-27 08:58:04 +01:00
* On windows, if you try to send 10MB, the OS will buffer EVERYTHING
* this can take an awful lot of time and we will end up blocked.
* Therefore, whoever calls this function should not pass a huge buffer.
\*-------------------------------------------------------------------------*/
2004-07-02 20:44:05 +02:00
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm)
{
int err;
/* avoid making system calls on closed sockets */
if (*ps == SOCK_INVALID) return IO_CLOSED;
/* loop until we send something or we give up on error */
*sent = 0;
2004-07-02 20:44:05 +02:00
for ( ;; ) {
/* try to send something */
int put = send(*ps, data, (int) count, 0);
2004-07-02 20:44:05 +02:00
/* if we sent something, we are done */
if (put > 0) {
*sent = put;
return IO_DONE;
}
/* deal with failure */
err = WSAGetLastError();
2004-07-02 20:44:05 +02:00
/* we can only proceed if there was no serious error */
if (err != WSAEWOULDBLOCK) return err;
/* avoid busy wait */
if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
2004-07-02 20:44:05 +02:00
}
/* can't reach here */
return IO_UNKNOWN;
}
/*-------------------------------------------------------------------------*\
* Sendto with timeout
\*-------------------------------------------------------------------------*/
int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
SA *addr, socklen_t len, p_tm tm)
{
int err;
if (*ps == SOCK_INVALID) return IO_CLOSED;
*sent = 0;
2004-07-02 20:44:05 +02:00
for ( ;; ) {
2004-11-27 08:58:04 +01:00
int put = sendto(*ps, data, (int) count, 0, addr, len);
2004-07-02 20:44:05 +02:00
if (put > 0) {
*sent = put;
return IO_DONE;
}
err = WSAGetLastError();
if (err != WSAEWOULDBLOCK) return err;
if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
2004-07-02 20:44:05 +02:00
}
return IO_UNKNOWN;
}
/*-------------------------------------------------------------------------*\
* Receive with timeout
\*-------------------------------------------------------------------------*/
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) {
int err;
if (*ps == SOCK_INVALID) return IO_CLOSED;
*got = 0;
2004-07-02 20:44:05 +02:00
for ( ;; ) {
int taken = recv(*ps, data, (int) count, 0);
2004-07-02 20:44:05 +02:00
if (taken > 0) {
*got = taken;
return IO_DONE;
}
if (taken == 0) return IO_CLOSED;
err = WSAGetLastError();
if (err != WSAEWOULDBLOCK) return err;
if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
}
return IO_UNKNOWN;
}
/*-------------------------------------------------------------------------*\
* Recvfrom with timeout
\*-------------------------------------------------------------------------*/
int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
SA *addr, socklen_t *len, p_tm tm) {
int err;
if (*ps == SOCK_INVALID) return IO_CLOSED;
*got = 0;
2004-07-02 20:44:05 +02:00
for ( ;; ) {
int taken = recvfrom(*ps, data, (int) count, 0, addr, len);
2004-07-02 20:44:05 +02:00
if (taken > 0) {
*got = taken;
return IO_DONE;
}
if (taken == 0) return IO_CLOSED;
err = WSAGetLastError();
if (err != WSAEWOULDBLOCK) return err;
if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
}
return IO_UNKNOWN;
}
/*-------------------------------------------------------------------------*\
* Put socket into blocking mode
\*-------------------------------------------------------------------------*/
2004-07-02 20:44:05 +02:00
void sock_setblocking(p_sock ps) {
u_long argp = 0;
ioctlsocket(*ps, FIONBIO, &argp);
}
/*-------------------------------------------------------------------------*\
* Put socket into non-blocking mode
\*-------------------------------------------------------------------------*/
2004-07-02 20:44:05 +02:00
void sock_setnonblocking(p_sock ps) {
u_long argp = 1;
ioctlsocket(*ps, FIONBIO, &argp);
}
/*-------------------------------------------------------------------------*\
* DNS helpers
\*-------------------------------------------------------------------------*/
int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
*hp = gethostbyaddr(addr, len, AF_INET);
if (*hp) return IO_DONE;
2004-11-27 08:58:04 +01:00
else return WSAGetLastError();
}
int sock_gethostbyname(const char *addr, struct hostent **hp) {
*hp = gethostbyname(addr);
if (*hp) return IO_DONE;
2004-11-27 08:58:04 +01:00
else return WSAGetLastError();
}
/*-------------------------------------------------------------------------*\
* Error translation functions
\*-------------------------------------------------------------------------*/
const char *sock_hoststrerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
2004-07-26 06:03:55 +02:00
case WSAHOST_NOT_FOUND: return "host not found";
default: return wstrerror(err);
}
}
const char *sock_strerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
2004-07-26 06:03:55 +02:00
case WSAEADDRINUSE: return "address already in use";
case WSAECONNREFUSED: return "connection refused";
case WSAEISCONN: return "already connected";
2004-07-26 06:03:55 +02:00
case WSAEACCES: return "permission denied";
case WSAECONNABORTED: return "closed";
case WSAECONNRESET: return "closed";
case WSAETIMEDOUT: return "timeout";
default: return wstrerror(err);
}
}
const char *sock_ioerror(p_sock ps, int err) {
(void) ps;
return sock_strerror(err);
}
2004-07-02 20:44:05 +02:00
static const char *wstrerror(int err) {
switch (err) {
case WSAEINTR: return "Interrupted function call";
case WSAEACCES: return "Permission denied";
case WSAEFAULT: return "Bad address";
case WSAEINVAL: return "Invalid argument";
case WSAEMFILE: return "Too many open files";
case WSAEWOULDBLOCK: return "Resource temporarily unavailable";
case WSAEINPROGRESS: return "Operation now in progress";
case WSAEALREADY: return "Operation already in progress";
case WSAENOTSOCK: return "Socket operation on nonsocket";
case WSAEDESTADDRREQ: return "Destination address required";
case WSAEMSGSIZE: return "Message too long";
case WSAEPROTOTYPE: return "Protocol wrong type for socket";
case WSAENOPROTOOPT: return "Bad protocol option";
case WSAEPROTONOSUPPORT: return "Protocol not supported";
case WSAESOCKTNOSUPPORT: return "Socket type not supported";
case WSAEOPNOTSUPP: return "Operation not supported";
case WSAEPFNOSUPPORT: return "Protocol family not supported";
case WSAEAFNOSUPPORT:
return "Address family not supported by protocol family";
case WSAEADDRINUSE: return "Address already in use";
case WSAEADDRNOTAVAIL: return "Cannot assign requested address";
case WSAENETDOWN: return "Network is down";
case WSAENETUNREACH: return "Network is unreachable";
case WSAENETRESET: return "Network dropped connection on reset";
case WSAECONNABORTED: return "Software caused connection abort";
case WSAECONNRESET: return "Connection reset by peer";
case WSAENOBUFS: return "No buffer space available";
case WSAEISCONN: return "Socket is already connected";
case WSAENOTCONN: return "Socket is not connected";
case WSAESHUTDOWN: return "Cannot send after socket shutdown";
case WSAETIMEDOUT: return "Connection timed out";
case WSAECONNREFUSED: return "Connection refused";
case WSAEHOSTDOWN: return "Host is down";
case WSAEHOSTUNREACH: return "No route to host";
case WSAEPROCLIM: return "Too many processes";
case WSASYSNOTREADY: return "Network subsystem is unavailable";
case WSAVERNOTSUPPORTED: return "Winsock.dll version out of range";
case WSANOTINITIALISED:
return "Successful WSAStartup not yet performed";
case WSAEDISCON: return "Graceful shutdown in progress";
case WSAHOST_NOT_FOUND: return "Host not found";
case WSATRY_AGAIN: return "Nonauthoritative host not found";
case WSANO_RECOVERY: return "Nonrecoverable name lookup error";
case WSANO_DATA: return "Valid name, no data record of requested type";
default: return "Unknown error";
}
}