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
|
|
|
|
* the I/O call fail in the first place.
|
2004-01-17 09:02:04 +01:00
|
|
|
*
|
2003-06-26 20:47:49 +02:00
|
|
|
* RCS ID: $Id$
|
|
|
|
\*=========================================================================*/
|
2003-06-11 03:42:18 +02:00
|
|
|
#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);
|
|
|
|
static int wisclosed(int err);
|
2004-01-21 21:16:48 +01:00
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
int sock_open(void) {
|
2003-06-11 03:42:18 +02:00
|
|
|
WSADATA wsaData;
|
2003-06-26 20:47:49 +02:00
|
|
|
WORD wVersionRequested = MAKEWORD(2, 0);
|
|
|
|
int err = WSAStartup(wVersionRequested, &wsaData );
|
2003-06-11 03:42:18 +02:00
|
|
|
if (err != 0) return 0;
|
|
|
|
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* 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) {
|
2004-01-24 01:18:19 +01:00
|
|
|
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);
|
|
|
|
return select(n, 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
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
void sock_destroy(p_sock ps) {
|
2003-08-16 02:06:04 +02:00
|
|
|
if (*ps != SOCK_INVALID) {
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setblocking(ps); /* close can take a long time on WIN32 */
|
2003-08-16 02:06:04 +02:00
|
|
|
closesocket(*ps);
|
|
|
|
*ps = SOCK_INVALID;
|
|
|
|
}
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
*
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
void sock_shutdown(p_sock ps, int how) {
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setblocking(ps);
|
2004-01-16 08:06:31 +01:00
|
|
|
shutdown(*ps, how);
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setnonblocking(ps);
|
2004-01-16 08:06:31 +01:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Creates and sets up a socket
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
const char *sock_create(p_sock ps, int domain, int type, int protocol) {
|
2003-06-11 03:42:18 +02:00
|
|
|
t_sock sock = socket(domain, type, protocol);
|
2004-07-02 20:44:05 +02:00
|
|
|
if (sock == SOCK_INVALID) return sock_strerror();
|
2003-06-11 03:42:18 +02:00
|
|
|
*ps = sock;
|
2004-01-21 21:16:48 +01:00
|
|
|
return NULL;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Connects or returns error message
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm) {
|
2004-01-19 17:18:31 +01:00
|
|
|
t_sock sock = *ps;
|
2004-07-02 20:44:05 +02:00
|
|
|
int err;
|
2004-01-21 21:16:48 +01:00
|
|
|
fd_set efds, wfds;
|
|
|
|
/* don't call on closed socket */
|
|
|
|
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
|
|
|
|
/* ask system to connect */
|
|
|
|
err = connect(sock, addr, addr_len);
|
|
|
|
/* if no error, we're done */
|
|
|
|
if (err == 0) return NULL;
|
|
|
|
/* make sure the system is trying to connect */
|
2004-07-02 20:44:05 +02:00
|
|
|
err = WSAGetLastError();
|
|
|
|
if (err != WSAEWOULDBLOCK) return wstrerror(err);
|
|
|
|
/* optimize for timeout=0 */
|
|
|
|
if (tm_get(tm) == 0.0) return io_strerror(IO_TIMEOUT);
|
2004-01-21 21:16:48 +01:00
|
|
|
/* wait for a timeout or for the system's answer */
|
|
|
|
FD_ZERO(&wfds); FD_SET(sock, &wfds);
|
|
|
|
FD_ZERO(&efds); FD_SET(sock, &efds);
|
|
|
|
/* we run select to wait */
|
2004-07-02 20:44:05 +02:00
|
|
|
err = sock_select(0, NULL, &wfds, &efds, tm);
|
2004-01-21 21:16:48 +01:00
|
|
|
/* if select returned due to an event */
|
2004-07-02 20:44:05 +02:00
|
|
|
if (err > 0) {
|
2004-01-21 21:16:48 +01:00
|
|
|
/* if was in efds, we failed */
|
2004-01-24 01:18:19 +01:00
|
|
|
if (FD_ISSET(sock, &efds)) {
|
|
|
|
int why, len = sizeof(why);
|
2004-02-04 15:29:11 +01:00
|
|
|
/* give windows time to set the error (disgusting) */
|
|
|
|
Sleep(0);
|
2004-01-21 21:16:48 +01:00
|
|
|
/* find out why we failed */
|
|
|
|
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&why, &len);
|
2004-02-04 15:29:11 +01:00
|
|
|
/* we KNOW there was an error. if why is 0, we will return
|
|
|
|
* "unknown error", but it's not really our fault */
|
2004-07-02 20:44:05 +02:00
|
|
|
return wstrerror(why);
|
2004-01-24 01:18:19 +01:00
|
|
|
/* otherwise it must be in wfds, so we succeeded */
|
2004-01-21 21:16:48 +01:00
|
|
|
} else return NULL;
|
2004-01-24 01:18:19 +01:00
|
|
|
/* if no event happened, we timed out */
|
2004-07-02 20:44:05 +02:00
|
|
|
} else if (err == 0) return io_strerror(IO_TIMEOUT);
|
|
|
|
return sock_strerror();
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Binds or returns error message
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) {
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *err = NULL;
|
|
|
|
sock_setblocking(ps);
|
2004-07-02 20:44:05 +02:00
|
|
|
if (bind(*ps, addr, addr_len) < 0) err = sock_strerror();
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setnonblocking(ps);
|
|
|
|
return err;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
*
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
const char *sock_listen(p_sock ps, int backlog) {
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *err = NULL;
|
|
|
|
sock_setblocking(ps);
|
2004-07-02 20:44:05 +02:00
|
|
|
if (listen(*ps, backlog) < 0) err = sock_strerror();
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setnonblocking(ps);
|
|
|
|
return err;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Accept with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
|
2004-07-02 20:44:05 +02:00
|
|
|
socklen_t *addr_len, p_tm tm) {
|
2003-06-11 03:42:18 +02:00
|
|
|
t_sock sock = *ps;
|
|
|
|
SA dummy_addr;
|
2004-01-21 21:16:48 +01:00
|
|
|
socklen_t dummy_len = sizeof(dummy_addr);
|
2004-01-24 01:18:19 +01:00
|
|
|
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
|
2003-06-11 03:42:18 +02:00
|
|
|
if (!addr) addr = &dummy_addr;
|
|
|
|
if (!addr_len) addr_len = &dummy_len;
|
2004-01-21 21:16:48 +01:00
|
|
|
for (;;) {
|
2004-01-24 01:18:19 +01:00
|
|
|
fd_set rfds;
|
|
|
|
int err;
|
|
|
|
/* try to get client socket */
|
2004-01-21 21:16:48 +01:00
|
|
|
*pa = accept(sock, addr, addr_len);
|
2004-01-24 01:18:19 +01:00
|
|
|
/* if return is valid, we are done */
|
2004-06-21 08:07:58 +02:00
|
|
|
if (*pa != SOCK_INVALID) return NULL;
|
2004-01-24 01:18:19 +01:00
|
|
|
/* otherwise 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) return wstrerror(err);
|
|
|
|
/* optimize for the timeout=0 case */
|
|
|
|
if (tm_get(tm) == 0.0) return io_strerror(IO_TIMEOUT);
|
2004-01-24 01:18:19 +01:00
|
|
|
/* call select to avoid busy wait */
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(sock, &rfds);
|
2004-07-02 20:44:05 +02:00
|
|
|
err = sock_select(0, &rfds, NULL, NULL, tm);
|
|
|
|
if (err == 0) return io_strerror(IO_TIMEOUT);
|
|
|
|
else if (err < 0) break;
|
2004-01-21 21:16:48 +01:00
|
|
|
}
|
2004-07-02 20:44:05 +02:00
|
|
|
return sock_strerror();
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Send with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
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)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2004-01-17 01:17:46 +01:00
|
|
|
/* avoid making system calls on closed sockets */
|
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-07-02 20:44:05 +02:00
|
|
|
for ( ;; ) {
|
|
|
|
fd_set fds;
|
|
|
|
int ret, put;
|
|
|
|
/* try to send something */
|
|
|
|
put = send(sock, data, (int) count, 0);
|
|
|
|
/* if we sent something, we are done */
|
|
|
|
if (put > 0) {
|
|
|
|
*sent = put;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
|
|
|
/* deal with failure */
|
2003-06-11 03:42:18 +02:00
|
|
|
*sent = 0;
|
2004-07-02 20:44:05 +02:00
|
|
|
ret = WSAGetLastError();
|
|
|
|
/* check for connection closed */
|
|
|
|
if (wisclosed(ret)) return IO_CLOSED;
|
|
|
|
/* we can only proceed if there was no serious error */
|
|
|
|
if (ret != WSAEWOULDBLOCK) return IO_USER;
|
|
|
|
/* optimize for the timeout = 0 case */
|
|
|
|
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
2004-01-17 01:17:46 +01:00
|
|
|
/* run select to avoid busy wait */
|
2004-07-02 20:44:05 +02:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
|
|
|
ret = sock_select(0, NULL, &fds, NULL, tm);
|
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
|
|
|
else if (ret < 0) break;
|
|
|
|
}
|
|
|
|
return IO_USER;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Sendto with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-11 03:42:18 +02:00
|
|
|
int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
|
2004-07-02 20:44:05 +02:00
|
|
|
SA *addr, socklen_t addr_len, p_tm tm)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2004-07-02 20:44:05 +02:00
|
|
|
/* avoid making system calls on closed sockets */
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-07-02 20:44:05 +02:00
|
|
|
for ( ;; ) {
|
|
|
|
fd_set fds;
|
|
|
|
int ret, put;
|
|
|
|
/* try to send something */
|
|
|
|
put = sendto(sock, data, (int) count, 0, addr, addr_len);
|
|
|
|
/* if we sent something, we are done */
|
|
|
|
if (put > 0) {
|
|
|
|
*sent = put;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
|
|
|
/* deal with failure */
|
2003-06-11 03:42:18 +02:00
|
|
|
*sent = 0;
|
2004-07-02 20:44:05 +02:00
|
|
|
ret = WSAGetLastError();
|
|
|
|
/* check for connection closed */
|
|
|
|
if (wisclosed(ret)) return IO_CLOSED;
|
|
|
|
/* we can only proceed if there was no serious error */
|
|
|
|
if (ret != WSAEWOULDBLOCK) return IO_USER;
|
|
|
|
/* optimize for the timeout = 0 case */
|
|
|
|
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
|
|
|
/* run select to avoid busy wait */
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
|
|
|
ret = sock_select(0, NULL, &fds, NULL, tm);
|
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
|
|
|
else if (ret < 0) break;
|
|
|
|
}
|
|
|
|
return IO_USER;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Receive with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-07-02 20:44:05 +02:00
|
|
|
for ( ;; ) {
|
2004-01-17 01:17:46 +01:00
|
|
|
fd_set fds;
|
2004-07-02 20:44:05 +02:00
|
|
|
int ret, taken;
|
|
|
|
taken = recv(sock, data, (int) count, 0);
|
|
|
|
if (taken > 0) {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
2003-06-11 03:42:18 +02:00
|
|
|
*got = 0;
|
2004-07-02 20:44:05 +02:00
|
|
|
if (taken == 0 || wisclosed(ret = WSAGetLastError())) return IO_CLOSED;
|
|
|
|
if (ret != WSAEWOULDBLOCK) return IO_USER;
|
|
|
|
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
2004-01-17 01:17:46 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-07-02 20:44:05 +02:00
|
|
|
ret = sock_select(0, &fds, NULL, NULL, tm);
|
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
|
|
|
else if (ret < 0) break;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
2004-07-02 20:44:05 +02:00
|
|
|
return IO_TIMEOUT;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Recvfrom with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-11 03:42:18 +02:00
|
|
|
int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
|
2004-07-02 20:44:05 +02:00
|
|
|
SA *addr, socklen_t *addr_len, p_tm tm)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-07-02 20:44:05 +02:00
|
|
|
for ( ;; ) {
|
2004-01-17 01:17:46 +01:00
|
|
|
fd_set fds;
|
2004-07-02 20:44:05 +02:00
|
|
|
int ret, taken;
|
|
|
|
taken = recvfrom(sock, data, (int) count, 0, addr, addr_len);
|
|
|
|
if (taken > 0) {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
2003-06-11 03:42:18 +02:00
|
|
|
*got = 0;
|
2004-07-02 20:44:05 +02:00
|
|
|
if (taken == 0 || wisclosed(ret = WSAGetLastError())) return IO_CLOSED;
|
|
|
|
if (ret != WSAEWOULDBLOCK) return IO_USER;
|
|
|
|
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
2004-01-17 01:17:46 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-07-02 20:44:05 +02:00
|
|
|
ret = sock_select(0, &fds, NULL, NULL, tm);
|
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
|
|
|
else if (ret < 0) break;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
2004-07-02 20:44:05 +02:00
|
|
|
return IO_TIMEOUT;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Put socket into blocking mode
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
void sock_setblocking(p_sock ps) {
|
2003-06-26 20:47:49 +02:00
|
|
|
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) {
|
2003-06-26 20:47:49 +02:00
|
|
|
u_long argp = 1;
|
|
|
|
ioctlsocket(*ps, FIONBIO, &argp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Error translation functions
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-02 20:44:05 +02:00
|
|
|
const char *sock_hoststrerror(void) {
|
|
|
|
int err = WSAGetLastError();
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2004-07-02 20:44:05 +02:00
|
|
|
case WSAHOST_NOT_FOUND:
|
|
|
|
return "host not found";
|
|
|
|
default:
|
|
|
|
return wstrerror(err);
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-02 20:44:05 +02:00
|
|
|
const char *sock_strerror(void) {
|
|
|
|
int err = WSAGetLastError();
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2004-07-02 20:44:05 +02:00
|
|
|
case WSAEADDRINUSE:
|
|
|
|
return "address already in use";
|
|
|
|
default:
|
|
|
|
return wstrerror(err);
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-02 20:44:05 +02:00
|
|
|
const char *sock_geterr(p_sock ps, int code) {
|
|
|
|
(void) ps;
|
|
|
|
(void) code;
|
|
|
|
return sock_strerror();
|
2004-01-24 01:18:19 +01:00
|
|
|
}
|
|
|
|
|
2004-07-02 20:44:05 +02:00
|
|
|
int wisclosed(int err) {
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2004-07-02 20:44:05 +02:00
|
|
|
case WSAECONNRESET:
|
|
|
|
case WSAECONNABORTED:
|
|
|
|
case WSAESHUTDOWN:
|
|
|
|
case WSAENOTCONN:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
2004-01-24 01:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-02 20:44:05 +02:00
|
|
|
static const char *wstrerror(int err) {
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2004-07-02 20:44:05 +02:00
|
|
|
case WSAEINTR:
|
|
|
|
return "WSAEINTR: Interrupted function call";
|
|
|
|
case WSAEACCES:
|
|
|
|
return "WSAEACCES: Permission denied";
|
|
|
|
case WSAEFAULT:
|
|
|
|
return "WSAEFAULT: Bad address";
|
|
|
|
case WSAEINVAL:
|
|
|
|
return "WSAEINVAL: Invalid argument";
|
|
|
|
case WSAEMFILE:
|
|
|
|
return "WSAEMFILE: Too many open files";
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
return "WSAEWOULDBLOCK: Resource temporarily unavailable";
|
|
|
|
case WSAEINPROGRESS:
|
|
|
|
return "WSAEINPROGRESS: Operation now in progress";
|
|
|
|
case WSAEALREADY:
|
|
|
|
return "WSAEALREADY: Operation already in progress";
|
|
|
|
case WSAENOTSOCK:
|
|
|
|
return "WSAENOTSOCK: Socket operation on nonsocket";
|
|
|
|
case WSAEDESTADDRREQ:
|
|
|
|
return "WSAEDESTADDRREQ: Destination address required";
|
|
|
|
case WSAEMSGSIZE:
|
|
|
|
return "WSAEMSGSIZE: Message too long";
|
|
|
|
case WSAEPROTOTYPE:
|
|
|
|
return "WSAEPROTOTYPE: Protocol wrong type for socket";
|
|
|
|
case WSAENOPROTOOPT:
|
|
|
|
return "WSAENOPROTOOPT: Bad protocol option";
|
|
|
|
case WSAEPROTONOSUPPORT:
|
|
|
|
return "WSAEPROTONOSUPPORT: Protocol not supported";
|
|
|
|
case WSAESOCKTNOSUPPORT:
|
|
|
|
return "WSAESOCKTNOSUPPORT: Socket type not supported";
|
|
|
|
case WSAEOPNOTSUPP:
|
|
|
|
return "WSAEOPNOTSUPP: Operation not supported";
|
|
|
|
case WSAEPFNOSUPPORT:
|
|
|
|
return "WSAEPFNOSUPPORT: Protocol family not supported";
|
|
|
|
case WSAEAFNOSUPPORT:
|
|
|
|
return "WSAEAFNOSUPPORT: Address family not supported by "
|
|
|
|
"protocol family";
|
|
|
|
case WSAEADDRINUSE:
|
|
|
|
return "WSAEADDRINUSE: Address already in use";
|
|
|
|
case WSAEADDRNOTAVAIL:
|
|
|
|
return "WSAEADDRNOTAVAIL: Cannot assign requested address";
|
|
|
|
case WSAENETDOWN:
|
|
|
|
return "WSAENETDOWN: Network is down";
|
|
|
|
case WSAENETUNREACH:
|
|
|
|
return "WSAENETUNREACH: Network is unreachable";
|
|
|
|
case WSAENETRESET:
|
|
|
|
return "WSAENETRESET: Network dropped connection on reset";
|
|
|
|
case WSAECONNABORTED:
|
|
|
|
return "WSAECONNABORTED: Software caused connection abort";
|
|
|
|
case WSAECONNRESET:
|
|
|
|
return "WSAECONNRESET: Connection reset by peer";
|
|
|
|
case WSAENOBUFS:
|
|
|
|
return "WSAENOBUFS: No buffer space available";
|
|
|
|
case WSAEISCONN:
|
|
|
|
return "WSAEISCONN: Socket is already connected";
|
|
|
|
case WSAENOTCONN:
|
|
|
|
return "WSAENOTCONN: Socket is not connected";
|
|
|
|
case WSAESHUTDOWN:
|
|
|
|
return "WSAESHUTDOWN: Cannot send after socket shutdown";
|
|
|
|
case WSAETIMEDOUT:
|
|
|
|
return "WSAETIMEDOUT: Connection timed out";
|
|
|
|
case WSAECONNREFUSED:
|
|
|
|
return "WSAECONNREFUSED: Connection refused";
|
|
|
|
case WSAEHOSTDOWN:
|
|
|
|
return "WSAEHOSTDOWN: Host is down";
|
|
|
|
case WSAEHOSTUNREACH:
|
|
|
|
return "WSAEHOSTUNREACH: No route to host";
|
|
|
|
case WSAEPROCLIM:
|
|
|
|
return "WSAEPROCLIM: Too many processes";
|
|
|
|
case WSASYSNOTREADY:
|
|
|
|
return "WSASYSNOTREADY: Network subsystem is unavailable";
|
|
|
|
case WSAVERNOTSUPPORTED:
|
|
|
|
return "WSAVERNOTSUPPORTED: Winsock.dll version out of range";
|
|
|
|
case WSANOTINITIALISED:
|
|
|
|
return "WSANOTINITIALISED: Successful WSAStartup not yet performed";
|
|
|
|
case WSAEDISCON:
|
|
|
|
return "WSAEDISCON: Graceful shutdown in progress";
|
|
|
|
case WSATYPE_NOT_FOUND:
|
|
|
|
return "WSATYPE_NOT_FOUND: Class type not found";
|
|
|
|
case WSAHOST_NOT_FOUND:
|
|
|
|
return "WSAHOST_NOT_FOUND: Host not found";
|
|
|
|
case WSATRY_AGAIN:
|
|
|
|
return "WSATRY_AGAIN: Nonauthoritative host not found";
|
|
|
|
case WSANO_RECOVERY:
|
|
|
|
return "WSANO_RECOVERY: Nonrecoverable name lookup error";
|
|
|
|
case WSANO_DATA:
|
|
|
|
return "WSANO_DATA: Valid name, no data record of requested type";
|
|
|
|
case WSASYSCALLFAILURE:
|
|
|
|
return "WSASYSCALLFAILURE: System call failure";
|
|
|
|
default:
|
|
|
|
return "Unknown error";
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|