2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Socket compatibilization module for Win32
|
|
|
|
* LuaSocket toolkit
|
|
|
|
*
|
2004-01-17 09:02:04 +01:00
|
|
|
* We also exchanged the order of the calls to send/recv and select.
|
|
|
|
* The idea is that the outer loop (whoever is calling sock_send/recv)
|
|
|
|
* will call the function again if we didn't time out, so we can
|
|
|
|
* call write and then select only if it fails. This moves the penalty
|
|
|
|
* to when data is not available, maximizing the bandwidth if data is
|
|
|
|
* always available.
|
|
|
|
*
|
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-01-24 01:18:19 +01:00
|
|
|
static const char *sock_createstrerror(int err);
|
|
|
|
static const char *sock_bindstrerror(int err);
|
|
|
|
static const char *sock_connectstrerror(int err);
|
|
|
|
static const char *sock_acceptstrerror(int err);
|
|
|
|
static const char *sock_listenstrerror(int err);
|
2004-01-21 21:16:48 +01:00
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-11 03:42:18 +02:00
|
|
|
int sock_open(void)
|
|
|
|
{
|
|
|
|
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-01-24 01:18:19 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Select with int timeout in ms
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, int timeout)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
return select(n, rfds, wfds, efds, timeout >= 0? &tv: NULL);
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Close and inutilize socket
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-11 03:42:18 +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
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
*
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
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-01-21 21:16:48 +01: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-01-24 01:18:19 +01:00
|
|
|
if (sock == SOCK_INVALID)
|
|
|
|
return sock_createstrerror(WSAGetLastError());
|
2003-06-11 03:42:18 +02:00
|
|
|
*ps = sock;
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setnonblocking(ps);
|
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-01-21 21:16:48 +01:00
|
|
|
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
2004-01-19 17:18:31 +01:00
|
|
|
t_sock sock = *ps;
|
2004-01-21 21:16:48 +01:00
|
|
|
int err, timeout = tm_getretry(tm);
|
|
|
|
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-01-24 01:18:19 +01:00
|
|
|
err = WSAGetLastError();
|
|
|
|
if (err != WSAEWOULDBLOCK) return sock_connectstrerror(err);
|
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-01-24 01:18:19 +01:00
|
|
|
err = sock_select(0, NULL, &wfds, &efds, timeout);
|
2004-01-21 21:16:48 +01:00
|
|
|
/* if select returned due to an event */
|
|
|
|
if (err > 0 ) {
|
|
|
|
/* 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-01-24 01:18:19 +01:00
|
|
|
return sock_connectstrerror(why);
|
|
|
|
/* 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-01-21 21:16:48 +01:00
|
|
|
} else return io_strerror(IO_TIMEOUT);
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Binds or returns error message
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-21 21:16:48 +01:00
|
|
|
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *err = NULL;
|
|
|
|
sock_setblocking(ps);
|
|
|
|
if (bind(*ps, addr, addr_len) < 0)
|
|
|
|
err = sock_bindstrerror(WSAGetLastError());
|
|
|
|
sock_setnonblocking(ps);
|
|
|
|
return err;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
*
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *sock_listen(p_sock ps, int backlog)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *err = NULL;
|
|
|
|
sock_setblocking(ps);
|
|
|
|
if (listen(*ps, backlog) < 0)
|
|
|
|
err = sock_listenstrerror(WSAGetLastError());
|
|
|
|
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,
|
|
|
|
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;
|
2004-01-21 21:16:48 +01:00
|
|
|
int timeout = tm_getretry(tm);
|
2004-01-24 01:18:19 +01:00
|
|
|
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 */
|
|
|
|
if (*pa != SOCK_INVALID) return NULL;
|
|
|
|
/* optimization */
|
|
|
|
if (timeout == 0) return io_strerror(IO_TIMEOUT);
|
|
|
|
/* otherwise find out why we failed */
|
|
|
|
err = WSAGetLastError();
|
|
|
|
/* if we failed because there was no connectoin, keep trying*/
|
|
|
|
if (err != WSAEWOULDBLOCK) return sock_acceptstrerror(err);
|
|
|
|
/* call select to avoid busy wait */
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(sock, &rfds);
|
|
|
|
err = sock_select(0, &rfds, NULL, NULL, timeout);
|
|
|
|
if (err <= 0) return io_strerror(IO_TIMEOUT);
|
2004-01-21 21:16:48 +01:00
|
|
|
}
|
2004-01-24 01:18:19 +01:00
|
|
|
return io_strerror(IO_TIMEOUT); /* can't get here */
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Send with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-11 03:42:18 +02:00
|
|
|
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
|
|
|
|
int timeout)
|
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2004-02-11 04:31:53 +01:00
|
|
|
int put;
|
2003-06-11 03:42:18 +02:00
|
|
|
int ret;
|
2004-01-17 01:17:46 +01:00
|
|
|
/* avoid making system calls on closed sockets */
|
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
|
|
|
/* try to send something */
|
|
|
|
put = send(sock, data, (int) count, 0);
|
|
|
|
/* deal with failure */
|
|
|
|
if (put <= 0) {
|
|
|
|
/* in any case, nothing has been sent */
|
2003-06-11 03:42:18 +02:00
|
|
|
*sent = 0;
|
2004-01-17 01:17:46 +01:00
|
|
|
/* run select to avoid busy wait */
|
|
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) {
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-01-24 01:18:19 +01:00
|
|
|
ret = sock_select(0, NULL, &fds, NULL, timeout);
|
2004-05-28 09:24:43 +02:00
|
|
|
/* tell the caller to call us again because now we can send */
|
|
|
|
if (ret > 0) return IO_RETRY;
|
|
|
|
/* tell the caller we can't send anything before timint out */
|
2004-01-17 01:17:46 +01:00
|
|
|
else return IO_TIMEOUT;
|
|
|
|
/* here we know the connection has been closed */
|
|
|
|
} else return IO_CLOSED;
|
|
|
|
/* here we successfully sent something */
|
|
|
|
} else {
|
|
|
|
*sent = put;
|
|
|
|
return IO_DONE;
|
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,
|
|
|
|
SA *addr, socklen_t addr_len, int timeout)
|
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2004-02-11 04:31:53 +01:00
|
|
|
int put;
|
2003-06-11 03:42:18 +02:00
|
|
|
int ret;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-01-17 01:17:46 +01:00
|
|
|
put = sendto(sock, data, (int) count, 0, addr, addr_len);
|
|
|
|
if (put <= 0) {
|
2003-06-11 03:42:18 +02:00
|
|
|
*sent = 0;
|
2004-01-17 01:17:46 +01:00
|
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) {
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-01-24 01:18:19 +01:00
|
|
|
ret = sock_select(0, NULL, &fds, NULL, timeout);
|
2004-05-28 09:24:43 +02:00
|
|
|
if (ret > 0) return IO_RETRY;
|
2004-01-17 01:17:46 +01:00
|
|
|
else return IO_TIMEOUT;
|
|
|
|
} else return IO_CLOSED;
|
|
|
|
} else {
|
|
|
|
*sent = put;
|
|
|
|
return IO_DONE;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Receive with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-11 03:42:18 +02:00
|
|
|
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
|
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2004-02-11 04:31:53 +01:00
|
|
|
int taken;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-01-17 01:17:46 +01:00
|
|
|
taken = recv(sock, data, (int) count, 0);
|
|
|
|
if (taken <= 0) {
|
|
|
|
fd_set fds;
|
|
|
|
int ret;
|
2003-06-11 03:42:18 +02:00
|
|
|
*got = 0;
|
2004-03-16 07:42:53 +01:00
|
|
|
if (taken == 0 || WSAGetLastError() != WSAEWOULDBLOCK) return IO_CLOSED;
|
2004-01-17 01:17:46 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-01-24 01:18:19 +01:00
|
|
|
ret = sock_select(0, &fds, NULL, NULL, timeout);
|
2004-05-28 09:24:43 +02:00
|
|
|
if (ret > 0) return IO_RETRY;
|
2004-01-17 01:17:46 +01:00
|
|
|
else return IO_TIMEOUT;
|
|
|
|
} else {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
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,
|
|
|
|
SA *addr, socklen_t *addr_len, int timeout)
|
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2004-02-11 04:31:53 +01:00
|
|
|
int taken;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-01-17 01:17:46 +01:00
|
|
|
taken = recvfrom(sock, data, (int) count, 0, addr, addr_len);
|
|
|
|
if (taken <= 0) {
|
|
|
|
fd_set fds;
|
|
|
|
int ret;
|
2003-06-11 03:42:18 +02:00
|
|
|
*got = 0;
|
2004-03-16 07:42:53 +01:00
|
|
|
if (taken == 0 || WSAGetLastError() != WSAEWOULDBLOCK) return IO_CLOSED;
|
2004-01-17 01:17:46 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-01-24 01:18:19 +01:00
|
|
|
ret = sock_select(0, &fds, NULL, NULL, timeout);
|
2004-05-28 09:24:43 +02:00
|
|
|
if (ret > 0) return IO_RETRY;
|
2004-01-17 01:17:46 +01:00
|
|
|
else return IO_TIMEOUT;
|
|
|
|
} else {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
2003-06-11 03:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Put socket into blocking mode
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void sock_setblocking(p_sock ps)
|
|
|
|
{
|
|
|
|
u_long argp = 0;
|
|
|
|
ioctlsocket(*ps, FIONBIO, &argp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Put socket into non-blocking mode
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void sock_setnonblocking(p_sock ps)
|
|
|
|
{
|
|
|
|
u_long argp = 1;
|
|
|
|
ioctlsocket(*ps, FIONBIO, &argp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Error translation functions
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-24 01:18:19 +01:00
|
|
|
/* return error messages for the known errors reported by gethostbyname */
|
2003-06-11 03:42:18 +02:00
|
|
|
const char *sock_hoststrerror(void)
|
|
|
|
{
|
|
|
|
switch (WSAGetLastError()) {
|
2004-01-24 01:18:19 +01:00
|
|
|
case WSANOTINITIALISED: return "not initialized";
|
|
|
|
case WSAENETDOWN: return "network is down";
|
|
|
|
case WSAHOST_NOT_FOUND: return "host not found";
|
|
|
|
case WSATRY_AGAIN: return "name server unavailable, try again later";
|
|
|
|
case WSANO_RECOVERY: return "name server error";
|
|
|
|
case WSANO_DATA: return "host not found";
|
|
|
|
case WSAEINPROGRESS: return "another call in progress";
|
|
|
|
case WSAEFAULT: return "invalid memory address";
|
|
|
|
case WSAEINTR: return "call interrupted";
|
2003-06-11 03:42:18 +02:00
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/* return error messages for the known errors reported by socket */
|
|
|
|
static const char *sock_createstrerror(int err)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2003-06-11 03:42:18 +02:00
|
|
|
case WSANOTINITIALISED: return "not initialized";
|
|
|
|
case WSAENETDOWN: return "network is down";
|
2004-01-24 01:18:19 +01:00
|
|
|
case WSAEAFNOSUPPORT: return "address family not supported";
|
|
|
|
case WSAEINPROGRESS: return "another call in progress";
|
2003-06-11 03:42:18 +02:00
|
|
|
case WSAEMFILE: return "descriptor table is full";
|
|
|
|
case WSAENOBUFS: return "insufficient buffer space";
|
2004-01-24 01:18:19 +01:00
|
|
|
case WSAEPROTONOSUPPORT: return "protocol not supported";
|
|
|
|
case WSAEPROTOTYPE: return "wrong protocol type";
|
|
|
|
case WSAESOCKTNOSUPPORT: return "socket type not supported by family";
|
2003-06-11 03:42:18 +02:00
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/* return error messages for the known errors reported by accept */
|
|
|
|
static const char *sock_acceptstrerror(int err)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2003-06-11 03:42:18 +02:00
|
|
|
case WSANOTINITIALISED: return "not initialized";
|
|
|
|
case WSAENETDOWN: return "network is down";
|
2004-01-24 01:18:19 +01:00
|
|
|
case WSAEFAULT: return "invalid memory address";
|
|
|
|
case WSAEINTR: return "call interrupted";
|
|
|
|
case WSAEINPROGRESS: return "another call in progress";
|
|
|
|
case WSAEINVAL: return "not listening";
|
|
|
|
case WSAEMFILE: return "descriptor table is full";
|
|
|
|
case WSAENOBUFS: return "insufficient buffer space";
|
|
|
|
case WSAENOTSOCK: return "descriptor not a socket";
|
|
|
|
case WSAEOPNOTSUPP: return "not supported";
|
|
|
|
case WSAEWOULDBLOCK: return "call would block";
|
2003-06-11 03:42:18 +02:00
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/* return error messages for the known errors reported by bind */
|
|
|
|
static const char *sock_bindstrerror(int err)
|
2003-06-11 03:42:18 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2003-06-11 03:42:18 +02:00
|
|
|
case WSANOTINITIALISED: return "not initialized";
|
|
|
|
case WSAENETDOWN: return "network is down";
|
2004-01-24 01:18:19 +01:00
|
|
|
case WSAEACCES: return "broadcast not enabled for socket";
|
2003-06-11 03:42:18 +02:00
|
|
|
case WSAEADDRINUSE: return "address already in use";
|
2004-01-24 01:18:19 +01:00
|
|
|
case WSAEADDRNOTAVAIL: return "address not available in local host";
|
|
|
|
case WSAEFAULT: return "invalid memory address";
|
|
|
|
case WSAEINPROGRESS: return "another call in progress";
|
|
|
|
case WSAEINVAL: return "already bound";
|
|
|
|
case WSAENOBUFS: return "insuficient buffer space";
|
|
|
|
case WSAENOTSOCK: return "descriptor not a socket";
|
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return error messages for the known errors reported by listen */
|
|
|
|
static const char *sock_listenstrerror(int err)
|
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case WSANOTINITIALISED: return "not initialized";
|
|
|
|
case WSAENETDOWN: return "network is down";
|
|
|
|
case WSAEADDRINUSE: return "local address already in use";
|
|
|
|
case WSAEINPROGRESS: return "another call in progress";
|
|
|
|
case WSAEINVAL: return "not bound";
|
|
|
|
case WSAEISCONN: return "already connected";
|
|
|
|
case WSAEMFILE: return "descriptor table is full";
|
|
|
|
case WSAENOBUFS: return "insuficient buffer space";
|
|
|
|
case WSAENOTSOCK: return "descriptor not a socket";
|
|
|
|
case WSAEOPNOTSUPP: return "not supported";
|
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return error messages for the known errors reported by connect */
|
|
|
|
static const char *sock_connectstrerror(int err)
|
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case WSANOTINITIALISED: return "not initialized";
|
|
|
|
case WSAENETDOWN: return "network is down";
|
|
|
|
case WSAEADDRINUSE: return "local address already in use";
|
|
|
|
case WSAEINTR: return "call interrupted";
|
|
|
|
case WSAEINPROGRESS: return "another call in progress";
|
|
|
|
case WSAEALREADY: return "connect already in progress";
|
|
|
|
case WSAEADDRNOTAVAIL: return "invalid remote address";
|
|
|
|
case WSAEAFNOSUPPORT: return "address family not supported";
|
2003-06-11 03:42:18 +02:00
|
|
|
case WSAECONNREFUSED: return "connection refused";
|
2004-01-24 01:18:19 +01:00
|
|
|
case WSAEFAULT: return "invalid memory address";
|
|
|
|
case WSAEINVAL: return "socket is listening";
|
|
|
|
case WSAEISCONN: return "socket already connected";
|
2003-06-11 03:42:18 +02:00
|
|
|
case WSAENETUNREACH: return "network is unreachable";
|
2004-01-24 01:18:19 +01:00
|
|
|
case WSAENOTSOCK: return "descriptor not a socket";
|
|
|
|
case WSAETIMEDOUT: return io_strerror(IO_TIMEOUT);
|
|
|
|
case WSAEWOULDBLOCK: return "would block";
|
|
|
|
case WSAEACCES: return "broadcast not enabled";
|
2003-06-11 03:42:18 +02:00
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|