163 lines
6.7 KiB
C
163 lines
6.7 KiB
C
/*=========================================================================*\
|
|
* LuaSocket 2.0.2
|
|
* Copyright (C) 2004-2007 Diego Nehab
|
|
*
|
|
* Socket compatibilization module for Win32
|
|
*
|
|
* The penalty of calling select to avoid busy-wait is only paid when
|
|
* the I/O call fail in the first place.
|
|
*
|
|
* RCS ID: $Id: wsocket.c,v 1.36 2007/06/11 23:44:54 diego Exp $
|
|
\*=========================================================================*/
|
|
#include <string.h>
|
|
|
|
#include "socket.h"
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
* Initializes module
|
|
\*-------------------------------------------------------------------------*/
|
|
int socket_open(void) {
|
|
WSADATA wsaData;
|
|
WORD wVersionRequested = MAKEWORD(2, 0);
|
|
int err = WSAStartup(wVersionRequested, &wsaData );
|
|
if (err != 0) return 0;
|
|
if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) &&
|
|
(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) {
|
|
WSACleanup();
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
* Close module
|
|
\*-------------------------------------------------------------------------*/
|
|
int socket_close(void) {
|
|
WSACleanup();
|
|
return 1;
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
* Wait for readable/writable/connected socket with timeout
|
|
\*-------------------------------------------------------------------------*/
|
|
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
|
int ret;
|
|
fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
|
|
struct timeval tv, *tp = NULL;
|
|
double t;
|
|
if (timeout_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 = timeout_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;
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
* Close and inutilize socket
|
|
\*-------------------------------------------------------------------------*/
|
|
void socket_destroy(p_socket ps) {
|
|
if (*ps != SOCKET_INVALID) {
|
|
socket_setblocking(ps); /* close can take a long time on WIN32 */
|
|
closesocket(*ps);
|
|
*ps = SOCKET_INVALID;
|
|
}
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
* Put socket into blocking mode
|
|
\*-------------------------------------------------------------------------*/
|
|
void socket_setblocking(p_socket ps) {
|
|
u_long argp = 0;
|
|
ioctlsocket(*ps, FIONBIO, &argp);
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
* Put socket into non-blocking mode
|
|
\*-------------------------------------------------------------------------*/
|
|
void socket_setnonblocking(p_socket ps) {
|
|
u_long argp = 1;
|
|
ioctlsocket(*ps, FIONBIO, &argp);
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
* Error translation functions
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
/* WinSock doesn't have a strerror... */
|
|
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";
|
|
}
|
|
}
|
|
|
|
const char *socket_strerror(int err) {
|
|
if (err <= 0) return io_strerror(err);
|
|
switch (err) {
|
|
case WSAEADDRINUSE: return "address already in use";
|
|
case WSAECONNREFUSED: return "connection refused";
|
|
case WSAEISCONN: return "already connected";
|
|
case WSAEACCES: return "permission denied";
|
|
case WSAECONNABORTED: return "closed";
|
|
case WSAECONNRESET: return "closed";
|
|
case WSAETIMEDOUT: return "timeout";
|
|
default: return wstrerror(err);
|
|
}
|
|
}
|