2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* 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
|
2015-08-21 20:39:34 +02:00
|
|
|
* the I/O call fail in the first place.
|
2003-06-26 20:47:49 +02:00
|
|
|
\*=========================================================================*/
|
2019-02-26 00:04:29 +01:00
|
|
|
#include "luasocket.h"
|
|
|
|
|
2003-06-11 03:42:18 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "socket.h"
|
2015-03-03 21:17:06 +01:00
|
|
|
#include "pierror.h"
|
2003-06-11 03:42:18 +02:00
|
|
|
|
2004-07-02 20:44:05 +02:00
|
|
|
/* WinSock doesn't have a strerror... */
|
|
|
|
static const char *wstrerror(int err);
|
2004-01-21 21:16:48 +01:00
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-08-21 20:39:34 +02:00
|
|
|
* Initializes module
|
2003-06-26 20:47:49 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_open(void) {
|
2003-06-11 03:42:18 +02:00
|
|
|
WSADATA wsaData;
|
2015-08-21 20:39:34 +02:00
|
|
|
WORD wVersionRequested = MAKEWORD(2, 0);
|
2003-06-26 20:47:49 +02:00
|
|
|
int err = WSAStartup(wVersionRequested, &wsaData );
|
2003-06-11 03:42:18 +02:00
|
|
|
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)) {
|
2003-06-11 03:42:18 +02:00
|
|
|
WSACleanup();
|
2015-08-21 20:39:34 +02:00
|
|
|
return 0;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-30 23:36:22 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-08-21 20:39:34 +02:00
|
|
|
* Close module
|
2004-05-30 23:36:22 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_close(void) {
|
2004-05-30 23:36:22 +02:00
|
|
|
WSACleanup();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-07-16 08:48:48 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* 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)
|
|
|
|
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
2004-07-16 08:48:48 +02:00
|
|
|
int ret;
|
|
|
|
fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
|
|
|
|
struct timeval tv, *tp = NULL;
|
|
|
|
double t;
|
2005-11-20 08:20:26 +01:00
|
|
|
if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
|
2015-08-21 20:39:34 +02:00
|
|
|
if (sw & WAITFD_R) {
|
|
|
|
FD_ZERO(&rfds);
|
2011-05-25 22:57:22 +02:00
|
|
|
FD_SET(*ps, &rfds);
|
2015-08-21 20:39:34 +02:00
|
|
|
rp = &rfds;
|
2005-11-20 08:20:26 +01:00
|
|
|
}
|
2005-03-11 01:20:21 +01:00
|
|
|
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; }
|
2005-11-20 08:20:26 +01:00
|
|
|
if ((t = timeout_get(tm)) >= 0.0) {
|
2004-07-16 08:48:48 +02:00
|
|
|
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;
|
2005-03-11 01:20:21 +01:00
|
|
|
if (sw == WAITFD_C && FD_ISSET(*ps, &efds)) return IO_CLOSED;
|
2004-07-16 08:48:48 +02:00
|
|
|
return IO_DONE;
|
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Select with int timeout in ms
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
|
2005-11-20 08:20:26 +01:00
|
|
|
p_timeout tm) {
|
2015-08-21 20:39:34 +02:00
|
|
|
struct timeval tv;
|
2005-11-20 08:20:26 +01:00
|
|
|
double t = timeout_get(tm);
|
2004-07-02 20:44:05 +02:00
|
|
|
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) {
|
2005-08-23 07:53:14 +02:00
|
|
|
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);
|
2004-01-24 01:18:19 +01:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Close and inutilize socket
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
|
2005-10-07 06:40:59 +02:00
|
|
|
if (*ps != SOCKET_INVALID) {
|
|
|
|
socket_setblocking(ps); /* close can take a long time on WIN32 */
|
2003-08-16 02:06:04 +02:00
|
|
|
closesocket(*ps);
|
2005-10-07 06:40:59 +02:00
|
|
|
*ps = SOCKET_INVALID;
|
2003-08-16 02:06:04 +02:00
|
|
|
}
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-08-21 20:39:34 +02:00
|
|
|
*
|
2004-01-16 08:06:31 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
|
2005-10-07 06:40:59 +02:00
|
|
|
socket_setblocking(ps);
|
2004-01-16 08:06:31 +01:00
|
|
|
shutdown(*ps, how);
|
2005-10-07 06:40:59 +02:00
|
|
|
socket_setnonblocking(ps);
|
2004-01-16 08:06:31 +01:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Creates and sets up a socket
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) {
|
2004-07-16 08:48:48 +02:00
|
|
|
*ps = socket(domain, type, protocol);
|
2005-10-07 06:40:59 +02:00
|
|
|
if (*ps != SOCKET_INVALID) return IO_DONE;
|
2004-07-16 08:48:48 +02:00
|
|
|
else return WSAGetLastError();
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Connects or returns error message
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
|
2004-07-02 20:44:05 +02:00
|
|
|
int err;
|
2004-01-21 21:16:48 +01:00
|
|
|
/* don't call on closed socket */
|
2005-10-07 06:40:59 +02:00
|
|
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
2004-01-21 21:16:48 +01:00
|
|
|
/* ask system to connect */
|
2004-07-16 08:48:48 +02:00
|
|
|
if (connect(*ps, addr, len) == 0) return IO_DONE;
|
2004-01-21 21:16:48 +01:00
|
|
|
/* make sure the system is trying to connect */
|
2004-07-02 20:44:05 +02:00
|
|
|
err = WSAGetLastError();
|
2004-07-16 08:48:48 +02:00
|
|
|
if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err;
|
2005-02-08 11:01:01 +01:00
|
|
|
/* zero timeout case optimization */
|
2005-11-20 08:20:26 +01:00
|
|
|
if (timeout_iszero(tm)) return IO_TIMEOUT;
|
2004-07-16 08:48:48 +02:00
|
|
|
/* we wait until something happens */
|
2005-10-07 06:40:59 +02:00
|
|
|
err = socket_waitfd(ps, WAITFD_C, tm);
|
2005-09-29 08:11:42 +02:00
|
|
|
if (err == IO_CLOSED) {
|
2018-08-22 22:37:32 +02:00
|
|
|
int elen = sizeof(err);
|
2004-07-16 08:48:48 +02:00
|
|
|
/* give windows time to set the error (yes, disgusting) */
|
2005-03-11 03:21:27 +01:00
|
|
|
Sleep(10);
|
2004-07-16 08:48:48 +02:00
|
|
|
/* find out why we failed */
|
2018-08-22 22:37:32 +02:00
|
|
|
getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &elen);
|
2005-03-11 01:20:21 +01:00
|
|
|
/* 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 */
|
2015-08-21 20:39:34 +02:00
|
|
|
return err > 0? err: IO_UNKNOWN;
|
2004-07-16 08:48:48 +02:00
|
|
|
} else return err;
|
2005-09-29 08:11:42 +02:00
|
|
|
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Binds or returns error message
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
|
2004-07-16 08:48:48 +02:00
|
|
|
int err = IO_DONE;
|
2005-10-07 06:40:59 +02:00
|
|
|
socket_setblocking(ps);
|
2004-07-16 08:48:48 +02:00
|
|
|
if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
|
2005-10-07 06:40:59 +02:00
|
|
|
socket_setnonblocking(ps);
|
2004-01-24 01:18:19 +01:00
|
|
|
return err;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-08-21 20:39:34 +02:00
|
|
|
*
|
2003-06-26 20:47:49 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
|
2004-07-16 08:48:48 +02:00
|
|
|
int err = IO_DONE;
|
2005-10-07 06:40:59 +02:00
|
|
|
socket_setblocking(ps);
|
2004-07-16 08:48:48 +02:00
|
|
|
if (listen(*ps, backlog) < 0) err = WSAGetLastError();
|
2005-10-07 06:40:59 +02:00
|
|
|
socket_setnonblocking(ps);
|
2004-01-24 01:18:19 +01:00
|
|
|
return err;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Accept with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
|
2005-11-20 08:20:26 +01:00
|
|
|
p_timeout tm) {
|
2005-10-07 06:40:59 +02:00
|
|
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
2004-07-16 08:48:48 +02:00
|
|
|
for ( ;; ) {
|
2004-01-24 01:18:19 +01:00
|
|
|
int err;
|
|
|
|
/* try to get client socket */
|
2005-10-07 06:40:59 +02:00
|
|
|
if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE;
|
2004-07-16 08:48:48 +02:00
|
|
|
/* find out why we failed */
|
2015-08-21 20:39:34 +02:00
|
|
|
err = WSAGetLastError();
|
2004-07-02 20:44:05 +02:00
|
|
|
/* if we failed because there was no connectoin, keep trying */
|
2004-07-16 08:48:48 +02:00
|
|
|
if (err != WSAEWOULDBLOCK && err != WSAECONNABORTED) return err;
|
2004-01-24 01:18:19 +01:00
|
|
|
/* call select to avoid busy wait */
|
2005-10-07 06:40:59 +02:00
|
|
|
if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
|
2015-08-21 20:39:34 +02:00
|
|
|
}
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Send with timeout
|
2015-08-21 20:39:34 +02: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.
|
2004-11-27 08:58:04 +01:00
|
|
|
* Therefore, whoever calls this function should not pass a huge buffer.
|
2003-06-26 20:47:49 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
|
2005-11-20 08:20:26 +01:00
|
|
|
size_t *sent, p_timeout tm)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
2004-07-16 08:48:48 +02:00
|
|
|
int err;
|
2007-06-12 01:44:54 +02:00
|
|
|
*sent = 0;
|
2004-01-17 01:17:46 +01:00
|
|
|
/* avoid making system calls on closed sockets */
|
2005-10-07 06:40:59 +02:00
|
|
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
2004-07-16 08:48:48 +02:00
|
|
|
/* loop until we send something or we give up on error */
|
2004-07-02 20:44:05 +02:00
|
|
|
for ( ;; ) {
|
|
|
|
/* try to send something */
|
2011-05-25 22:57:22 +02:00
|
|
|
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 */
|
2015-08-21 20:39:34 +02:00
|
|
|
err = WSAGetLastError();
|
2004-07-02 20:44:05 +02:00
|
|
|
/* we can only proceed if there was no serious error */
|
2004-07-16 08:48:48 +02:00
|
|
|
if (err != WSAEWOULDBLOCK) return err;
|
|
|
|
/* avoid busy wait */
|
2005-10-07 06:40:59 +02:00
|
|
|
if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
|
2015-08-21 20:39:34 +02:00
|
|
|
}
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Sendto with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
|
2005-11-20 08:20:26 +01:00
|
|
|
SA *addr, socklen_t len, p_timeout tm)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
2004-07-16 08:48:48 +02:00
|
|
|
int err;
|
|
|
|
*sent = 0;
|
2007-06-12 01:44:54 +02:00
|
|
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
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;
|
|
|
|
}
|
2015-08-21 20:39:34 +02:00
|
|
|
err = WSAGetLastError();
|
2004-07-16 08:48:48 +02:00
|
|
|
if (err != WSAEWOULDBLOCK) return err;
|
2005-10-07 06:40:59 +02:00
|
|
|
if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
|
2015-08-21 20:39:34 +02:00
|
|
|
}
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Receive with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
|
2015-08-21 20:39:34 +02:00
|
|
|
p_timeout tm)
|
2013-05-28 11:27:06 +02:00
|
|
|
{
|
|
|
|
int err, prev = IO_DONE;
|
2004-07-16 08:48:48 +02:00
|
|
|
*got = 0;
|
2007-06-12 01:44:54 +02:00
|
|
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
2004-07-02 20:44:05 +02:00
|
|
|
for ( ;; ) {
|
2004-07-16 08:48:48 +02:00
|
|
|
int taken = recv(*ps, data, (int) count, 0);
|
2004-07-02 20:44:05 +02:00
|
|
|
if (taken > 0) {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
2004-07-16 08:48:48 +02:00
|
|
|
if (taken == 0) return IO_CLOSED;
|
|
|
|
err = WSAGetLastError();
|
2015-08-21 20:39:34 +02:00
|
|
|
/* On UDP, a connreset simply means the previous send failed.
|
|
|
|
* So we try again.
|
|
|
|
* On TCP, it means our socket is now useless, so the error passes.
|
2013-05-28 11:27:06 +02:00
|
|
|
* (We will loop again, exiting because the same error will happen) */
|
|
|
|
if (err != WSAEWOULDBLOCK) {
|
|
|
|
if (err != WSAECONNRESET || prev == WSAECONNRESET) return err;
|
|
|
|
prev = err;
|
|
|
|
}
|
2005-10-07 06:40:59 +02:00
|
|
|
if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Recvfrom with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
|
2015-08-21 20:39:34 +02:00
|
|
|
SA *addr, socklen_t *len, p_timeout tm)
|
2013-05-28 11:27:06 +02:00
|
|
|
{
|
|
|
|
int err, prev = IO_DONE;
|
2004-07-16 08:48:48 +02:00
|
|
|
*got = 0;
|
2007-06-12 01:44:54 +02:00
|
|
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
2004-07-02 20:44:05 +02:00
|
|
|
for ( ;; ) {
|
2004-07-16 08:48:48 +02:00
|
|
|
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;
|
|
|
|
}
|
2004-07-16 08:48:48 +02:00
|
|
|
if (taken == 0) return IO_CLOSED;
|
|
|
|
err = WSAGetLastError();
|
2015-08-21 20:39:34 +02:00
|
|
|
/* On UDP, a connreset simply means the previous send failed.
|
|
|
|
* So we try again.
|
2013-05-28 11:27:06 +02:00
|
|
|
* On TCP, it means our socket is now useless, so the error passes.
|
|
|
|
* (We will loop again, exiting because the same error will happen) */
|
|
|
|
if (err != WSAEWOULDBLOCK) {
|
|
|
|
if (err != WSAECONNRESET || prev == WSAECONNRESET) return err;
|
|
|
|
prev = err;
|
|
|
|
}
|
2005-10-07 06:40:59 +02:00
|
|
|
if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Put socket into blocking mode
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
|
2003-06-26 20:47:49 +02:00
|
|
|
u_long argp = 0;
|
|
|
|
ioctlsocket(*ps, FIONBIO, &argp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Put socket into non-blocking mode
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
|
2003-06-26 20:47:49 +02:00
|
|
|
u_long argp = 1;
|
|
|
|
ioctlsocket(*ps, FIONBIO, &argp);
|
|
|
|
}
|
|
|
|
|
2004-07-16 08:48:48 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-08-21 20:39:34 +02:00
|
|
|
* DNS helpers
|
2004-07-16 08:48:48 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
|
2004-07-16 08:48:48 +02:00
|
|
|
*hp = gethostbyaddr(addr, len, AF_INET);
|
|
|
|
if (*hp) return IO_DONE;
|
2004-11-27 08:58:04 +01:00
|
|
|
else return WSAGetLastError();
|
2004-07-16 08:48:48 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) {
|
2004-07-16 08:48:48 +02:00
|
|
|
*hp = gethostbyname(addr);
|
|
|
|
if (*hp) return IO_DONE;
|
2004-11-27 08:58:04 +01:00
|
|
|
else return WSAGetLastError();
|
2004-07-16 08:48:48 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Error translation functions
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
|
2004-07-16 08:48:48 +02:00
|
|
|
if (err <= 0) return io_strerror(err);
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
|
2015-08-21 20:39:34 +02:00
|
|
|
default: return wstrerror(err);
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE const char *socket_strerror(int err) {
|
2004-07-16 08:48:48 +02:00
|
|
|
if (err <= 0) return io_strerror(err);
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAEADDRINUSE: return PIE_ADDRINUSE;
|
|
|
|
case WSAECONNREFUSED : return PIE_CONNREFUSED;
|
|
|
|
case WSAEISCONN: return PIE_ISCONN;
|
|
|
|
case WSAEACCES: return PIE_ACCESS;
|
|
|
|
case WSAECONNABORTED: return PIE_CONNABORTED;
|
|
|
|
case WSAECONNRESET: return PIE_CONNRESET;
|
|
|
|
case WSAETIMEDOUT: return PIE_TIMEDOUT;
|
2004-07-16 08:48:48 +02:00
|
|
|
default: return wstrerror(err);
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) {
|
2011-05-25 22:57:22 +02:00
|
|
|
(void) ps;
|
|
|
|
return socket_strerror(err);
|
2004-01-24 01:18:19 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE static const char *wstrerror(int err) {
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2004-07-16 08:48:48 +02:00
|
|
|
case WSAEINTR: return "Interrupted function call";
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAEACCES: return PIE_ACCESS; // "Permission denied";
|
2004-07-16 08:48:48 +02:00
|
|
|
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";
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAESOCKTNOSUPPORT: return PIE_SOCKTYPE; // "Socket type not supported";
|
2004-07-16 08:48:48 +02:00
|
|
|
case WSAEOPNOTSUPP: return "Operation not supported";
|
|
|
|
case WSAEPFNOSUPPORT: return "Protocol family not supported";
|
2015-08-21 20:39:34 +02:00
|
|
|
case WSAEAFNOSUPPORT: return PIE_FAMILY; // "Address family not supported by protocol family";
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAEADDRINUSE: return PIE_ADDRINUSE; // "Address already in use";
|
2004-07-16 08:48:48 +02:00
|
|
|
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";
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAECONNRESET: return PIE_CONNRESET; // "Connection reset by peer";
|
2004-07-16 08:48:48 +02:00
|
|
|
case WSAENOBUFS: return "No buffer space available";
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAEISCONN: return PIE_ISCONN; // "Socket is already connected";
|
2004-07-16 08:48:48 +02:00
|
|
|
case WSAENOTCONN: return "Socket is not connected";
|
|
|
|
case WSAESHUTDOWN: return "Cannot send after socket shutdown";
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAETIMEDOUT: return PIE_TIMEDOUT; // "Connection timed out";
|
|
|
|
case WSAECONNREFUSED: return PIE_CONNREFUSED; // "Connection refused";
|
2004-07-16 08:48:48 +02:00
|
|
|
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";
|
2015-08-21 20:39:34 +02:00
|
|
|
case WSANOTINITIALISED:
|
2004-07-16 08:48:48 +02:00
|
|
|
return "Successful WSAStartup not yet performed";
|
|
|
|
case WSAEDISCON: return "Graceful shutdown in progress";
|
2015-03-03 21:17:06 +01:00
|
|
|
case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; // "Host not found";
|
2004-07-16 08:48:48 +02:00
|
|
|
case WSATRY_AGAIN: return "Nonauthoritative host not found";
|
2015-08-21 20:39:34 +02:00
|
|
|
case WSANO_RECOVERY: return PIE_FAIL; // "Nonrecoverable name lookup error";
|
2004-07-16 08:48:48 +02:00
|
|
|
case WSANO_DATA: return "Valid name, no data record of requested type";
|
|
|
|
default: return "Unknown error";
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-20 21:58:52 +01:00
|
|
|
|
2019-02-26 00:04:29 +01:00
|
|
|
LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
|
2015-08-21 20:39:34 +02:00
|
|
|
if (err == 0) return NULL;
|
2012-11-20 21:58:52 +01:00
|
|
|
switch (err) {
|
2015-03-03 21:17:06 +01:00
|
|
|
case EAI_AGAIN: return PIE_AGAIN;
|
|
|
|
case EAI_BADFLAGS: return PIE_BADFLAGS;
|
2012-11-20 21:58:52 +01:00
|
|
|
#ifdef EAI_BADHINTS
|
2015-03-03 21:17:06 +01:00
|
|
|
case EAI_BADHINTS: return PIE_BADHINTS;
|
2012-11-20 21:58:52 +01:00
|
|
|
#endif
|
2015-03-03 21:17:06 +01:00
|
|
|
case EAI_FAIL: return PIE_FAIL;
|
|
|
|
case EAI_FAMILY: return PIE_FAMILY;
|
|
|
|
case EAI_MEMORY: return PIE_MEMORY;
|
|
|
|
case EAI_NONAME: return PIE_NONAME;
|
2013-05-25 12:07:38 +02:00
|
|
|
#ifdef EAI_OVERFLOW
|
2015-03-03 21:17:06 +01:00
|
|
|
case EAI_OVERFLOW: return PIE_OVERFLOW;
|
2013-05-25 12:07:38 +02:00
|
|
|
#endif
|
2012-11-20 21:58:52 +01:00
|
|
|
#ifdef EAI_PROTOCOL
|
2015-03-03 21:17:06 +01:00
|
|
|
case EAI_PROTOCOL: return PIE_PROTOCOL;
|
2012-11-20 21:58:52 +01:00
|
|
|
#endif
|
2015-03-03 21:17:06 +01:00
|
|
|
case EAI_SERVICE: return PIE_SERVICE;
|
|
|
|
case EAI_SOCKTYPE: return PIE_SOCKTYPE;
|
2013-05-25 12:07:38 +02:00
|
|
|
#ifdef EAI_SYSTEM
|
2015-08-21 20:39:34 +02:00
|
|
|
case EAI_SYSTEM: return strerror(errno);
|
2013-05-25 12:07:38 +02:00
|
|
|
#endif
|
2012-11-20 21:58:52 +01:00
|
|
|
default: return gai_strerror(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|