New accept/connect code.

Better error checking.
Better tests.
__tostring implemented.
This commit is contained in:
Diego Nehab 2004-01-24 00:18:19 +00:00
parent 42e0e74487
commit 0c9f420a35
10 changed files with 424 additions and 196 deletions

3
TODO
View File

@ -12,6 +12,9 @@ manual
local connect local connect
add thanks to 'carlos cassino' and 'david burgess' add thanks to 'carlos cassino' and 'david burgess'
add new ip- options and reuseaddr option add new ip- options and reuseaddr option
add listen to manual
bind method doesn't do listen anymore
bind doesn't turn an object into a server object: listen does.
tests tests
checar todos os metodos checar todos os metodos

View File

@ -5,6 +5,7 @@
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
#include <stdio.h>
#include "luasocket.h" #include "luasocket.h"
#include "auxiliar.h" #include "auxiliar.h"
@ -12,6 +13,27 @@
/*=========================================================================*\ /*=========================================================================*\
* Exported functions * Exported functions
\*=========================================================================*/ \*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Prints the value of a class in a nice way
\*-------------------------------------------------------------------------*/
int aux_meth_tostring(lua_State *L)
{
char buf[32];
if (!lua_getmetatable(L, 1)) goto error;
lua_pushstring(L, "__index");
lua_gettable(L, -2);
if (!lua_istable(L, -1)) goto error;
lua_pushstring(L, "class");
lua_gettable(L, -2);
if (!lua_isstring(L, -1)) goto error;
sprintf(buf, "%p", lua_touserdata(L, 1));
lua_pushfstring(L, "socket: %s: %s", lua_tostring(L, -1), buf);
return 1;
error:
lua_pushnil(L);
return 1;
}
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Initializes the module * Initializes the module
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -26,14 +48,18 @@ void aux_open(lua_State *L)
void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) void aux_newclass(lua_State *L, const char *classname, luaL_reg *func)
{ {
luaL_newmetatable(L, classname); /* mt */ luaL_newmetatable(L, classname); /* mt */
/* set __tostring metamethod */
lua_pushstring(L, "__tostring");
lua_pushcfunction(L, aux_meth_tostring);
lua_rawset(L, -3);
/* create __index table to place methods */
lua_pushstring(L, "__index"); /* mt,"__index" */ lua_pushstring(L, "__index"); /* mt,"__index" */
lua_newtable(L); /* mt,"__index",it */ lua_newtable(L); /* mt,"__index",it */
luaL_openlib(L, NULL, func, 0); luaL_openlib(L, NULL, func, 0);
#ifdef LUASOCKET_DEBUG /* put class name into class metatable */
lua_pushstring(L, "class"); /* mt,"__index",it,"class" */ lua_pushstring(L, "class"); /* mt,"__index",it,"class" */
lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */ lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */
lua_rawset(L, -3); /* mt,"__index",it */ lua_rawset(L, -3); /* mt,"__index",it */
#endif
/* get __gc method from class and use it for garbage collection */ /* get __gc method from class and use it for garbage collection */
lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc" */ lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc" */
lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc","__gc" */ lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc","__gc" */

View File

@ -213,8 +213,7 @@ const char *inet_tryconnect(p_sock ps, const char *address,
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Tries to bind socket to (address, port) * Tries to bind socket to (address, port)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *inet_trybind(p_sock ps, const char *address, unsigned short port, const char *inet_trybind(p_sock ps, const char *address, unsigned short port)
int backlog)
{ {
struct sockaddr_in local; struct sockaddr_in local;
const char *err; const char *err;
@ -231,16 +230,9 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port,
addr = (struct in_addr **) hp->h_addr_list; addr = (struct in_addr **) hp->h_addr_list;
memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
} }
sock_setblocking(ps);
err = sock_bind(ps, (SA *) &local, sizeof(local)); err = sock_bind(ps, (SA *) &local, sizeof(local));
if (err) { if (err) sock_destroy(ps);
sock_destroy(ps);
return err; return err;
} else {
sock_setnonblocking(ps);
if (backlog >= 0) sock_listen(ps, backlog);
return NULL;
}
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\

View File

@ -30,7 +30,7 @@ const char *inet_trycreate(p_sock ps, int type);
const char *inet_tryconnect(p_sock ps, const char *address, const char *inet_tryconnect(p_sock ps, const char *address,
unsigned short port, p_tm tm); unsigned short port, p_tm tm);
const char *inet_trybind(p_sock ps, const char *address, const char *inet_trybind(p_sock ps, const char *address,
unsigned short port, int backlog); unsigned short port);
int inet_meth_getpeername(lua_State *L, p_sock ps); int inet_meth_getpeername(lua_State *L, p_sock ps);
int inet_meth_getsockname(lua_State *L, p_sock ps); int inet_meth_getsockname(lua_State *L, p_sock ps);

View File

@ -39,7 +39,6 @@ typedef struct sockaddr SA;
\*=========================================================================*/ \*=========================================================================*/
int sock_open(void); int sock_open(void);
void sock_destroy(p_sock ps); void sock_destroy(p_sock ps);
void sock_listen(p_sock ps, int backlog);
void sock_shutdown(p_sock ps, int how); void sock_shutdown(p_sock ps, int how);
int sock_send(p_sock ps, const char *data, size_t count, int sock_send(p_sock ps, const char *data, size_t count,
size_t *sent, int timeout); size_t *sent, int timeout);
@ -51,10 +50,15 @@ int sock_recvfrom(p_sock ps, char *data, size_t count,
size_t *got, SA *addr, socklen_t *addr_len, int timeout); size_t *got, SA *addr, socklen_t *addr_len, int timeout);
void sock_setnonblocking(p_sock ps); void sock_setnonblocking(p_sock ps);
void sock_setblocking(p_sock ps); void sock_setblocking(p_sock ps);
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm); int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, int timeout);
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm); const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm);
const char *sock_create(p_sock ps, int domain, int type, int protocol); const char *sock_create(p_sock ps, int domain, int type, int protocol);
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len); const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len);
const char *sock_hoststrerror(void); const char *sock_listen(p_sock ps, int backlog);
const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
socklen_t *addr_len, p_tm tm);
const char *sock_hoststrerror();
#endif /* SOCK_H */ #endif /* SOCK_H */

View File

@ -22,6 +22,7 @@
\*=========================================================================*/ \*=========================================================================*/
static int global_create(lua_State *L); static int global_create(lua_State *L);
static int meth_connect(lua_State *L); static int meth_connect(lua_State *L);
static int meth_listen(lua_State *L);
static int meth_bind(lua_State *L); static int meth_bind(lua_State *L);
static int meth_send(lua_State *L); static int meth_send(lua_State *L);
static int meth_getsockname(lua_State *L); static int meth_getsockname(lua_State *L);
@ -32,7 +33,8 @@ static int meth_accept(lua_State *L);
static int meth_close(lua_State *L); static int meth_close(lua_State *L);
static int meth_setoption(lua_State *L); static int meth_setoption(lua_State *L);
static int meth_settimeout(lua_State *L); static int meth_settimeout(lua_State *L);
static int meth_fd(lua_State *L); static int meth_getfd(lua_State *L);
static int meth_setfd(lua_State *L);
static int meth_dirty(lua_State *L); static int meth_dirty(lua_State *L);
/* tcp object methods */ /* tcp object methods */
@ -41,6 +43,7 @@ static luaL_reg tcp[] = {
{"send", meth_send}, {"send", meth_send},
{"receive", meth_receive}, {"receive", meth_receive},
{"bind", meth_bind}, {"bind", meth_bind},
{"listen", meth_listen},
{"accept", meth_accept}, {"accept", meth_accept},
{"setpeername", meth_connect}, {"setpeername", meth_connect},
{"setsockname", meth_bind}, {"setsockname", meth_bind},
@ -51,7 +54,8 @@ static luaL_reg tcp[] = {
{"shutdown", meth_shutdown}, {"shutdown", meth_shutdown},
{"setoption", meth_setoption}, {"setoption", meth_setoption},
{"__gc", meth_close}, {"__gc", meth_close},
{"fd", meth_fd}, {"getfd", meth_getfd},
{"setfd", meth_setfd},
{"dirty", meth_dirty}, {"dirty", meth_dirty},
{NULL, NULL} {NULL, NULL}
}; };
@ -124,16 +128,24 @@ static int meth_setoption(lua_State *L)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Select support methods * Select support methods
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_fd(lua_State *L) static int meth_getfd(lua_State *L)
{ {
p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
lua_pushnumber(L, tcp->sock); lua_pushnumber(L, tcp->sock);
return 1; return 1;
} }
/* this is very dangerous, but can be handy for those that are brave enough */
static int meth_setfd(lua_State *L)
{
p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
tcp->sock = (t_sock) luaL_checknumber(L, 2);
return 0;
}
static int meth_dirty(lua_State *L) static int meth_dirty(lua_State *L)
{ {
p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
lua_pushboolean(L, !buf_isempty(&tcp->buf)); lua_pushboolean(L, !buf_isempty(&tcp->buf));
return 1; return 1;
} }
@ -147,9 +159,9 @@ static int meth_accept(lua_State *L)
p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1);
p_tm tm = tm_markstart(&server->tm); p_tm tm = tm_markstart(&server->tm);
t_sock sock; t_sock sock;
int err = sock_accept(&server->sock, &sock, NULL, NULL, tm); const char *err = sock_accept(&server->sock, &sock, NULL, NULL, tm);
/* if successful, push client socket */ /* if successful, push client socket */
if (err == IO_DONE) { if (!err) {
p_tcp clnt = lua_newuserdata(L, sizeof(t_tcp)); p_tcp clnt = lua_newuserdata(L, sizeof(t_tcp));
aux_setclass(L, "tcp{client}", -1); aux_setclass(L, "tcp{client}", -1);
/* initialize structure fields */ /* initialize structure fields */
@ -160,28 +172,25 @@ static int meth_accept(lua_State *L)
return 1; return 1;
} else { } else {
lua_pushnil(L); lua_pushnil(L);
io_pusherror(L, err); lua_pushstring(L, err);
return 2; return 2;
} }
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Turns a master object into a server object * Binds an object to an address
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_bind(lua_State *L) static int meth_bind(lua_State *L)
{ {
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1);
const char *address = luaL_checkstring(L, 2); const char *address = luaL_checkstring(L, 2);
unsigned short port = (unsigned short) luaL_checknumber(L, 3); unsigned short port = (unsigned short) luaL_checknumber(L, 3);
int backlog = (int) luaL_optnumber(L, 4, 1); const char *err = inet_trybind(&tcp->sock, address, port);
const char *err = inet_trybind(&tcp->sock, address, port, backlog);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);
return 2; return 2;
} }
/* turn master object into a server object if there was a listen */
if (backlog >= 0) aux_setclass(L, "tcp{server}", 1);
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
return 1; return 1;
} }
@ -217,6 +226,25 @@ static int meth_close(lua_State *L)
return 0; return 0;
} }
/*-------------------------------------------------------------------------*\
* Puts the sockt in listen mode
\*-------------------------------------------------------------------------*/
static int meth_listen(lua_State *L)
{
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1);
int backlog = (int) luaL_checknumber(L, 2);
const char *err = sock_listen(&tcp->sock, backlog);
if (err) {
lua_pushnil(L);
lua_pushstring(L, err);
return 2;
}
/* turn master object into a server object */
aux_setclass(L, "tcp{server}", 1);
lua_pushnumber(L, 1);
return 1;
}
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Shuts the connection down partially * Shuts the connection down partially
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/

View File

@ -33,7 +33,8 @@ static int meth_close(lua_State *L);
static int meth_shutdown(lua_State *L); static int meth_shutdown(lua_State *L);
static int meth_setoption(lua_State *L); static int meth_setoption(lua_State *L);
static int meth_settimeout(lua_State *L); static int meth_settimeout(lua_State *L);
static int meth_fd(lua_State *L); static int meth_getfd(lua_State *L);
static int meth_setfd(lua_State *L);
static int meth_dirty(lua_State *L); static int meth_dirty(lua_State *L);
/* udp object methods */ /* udp object methods */
@ -51,7 +52,8 @@ static luaL_reg udp[] = {
{"shutdown", meth_shutdown}, {"shutdown", meth_shutdown},
{"setoption", meth_setoption}, {"setoption", meth_setoption},
{"__gc", meth_close}, {"__gc", meth_close},
{"fd", meth_fd}, {"getfd", meth_getfd},
{"setfd", meth_setfd},
{"dirty", meth_dirty}, {"dirty", meth_dirty},
{NULL, NULL} {NULL, NULL}
}; };
@ -194,13 +196,21 @@ static int meth_receivefrom(lua_State *L)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Select support methods * Select support methods
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_fd(lua_State *L) static int meth_getfd(lua_State *L)
{ {
p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
lua_pushnumber(L, udp->sock); lua_pushnumber(L, udp->sock);
return 1; return 1;
} }
/* this is very dangerous, but can be handy for those that are brave enough */
static int meth_setfd(lua_State *L)
{
p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
udp->sock = (t_sock) luaL_checknumber(L, 2);
return 0;
}
static int meth_dirty(lua_State *L) static int meth_dirty(lua_State *L)
{ {
p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
@ -312,7 +322,7 @@ static int meth_setsockname(lua_State *L)
p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1);
const char *address = luaL_checkstring(L, 2); const char *address = luaL_checkstring(L, 2);
unsigned short port = (unsigned short) luaL_checknumber(L, 3); unsigned short port = (unsigned short) luaL_checknumber(L, 3);
const char *err = inet_trybind(&udp->sock, address, port, -1); const char *err = inet_trybind(&udp->sock, address, port);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);

View File

@ -20,9 +20,11 @@
#include "socket.h" #include "socket.h"
static const char *sock_createstrerror(void); static const char *sock_createstrerror(int err);
static const char *sock_bindstrerror(void); static const char *sock_bindstrerror(int err);
static const char *sock_connectstrerror(void); static const char *sock_connectstrerror(int err);
static const char *sock_acceptstrerror(int err);
static const char *sock_listenstrerror(int err);
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Initializes module * Initializes module
@ -46,11 +48,23 @@ int sock_open(void)
void sock_destroy(p_sock ps) void sock_destroy(p_sock ps)
{ {
if (*ps != SOCK_INVALID) { if (*ps != SOCK_INVALID) {
sock_setblocking(ps);
close(*ps); close(*ps);
*ps = SOCK_INVALID; *ps = SOCK_INVALID;
} }
} }
/*-------------------------------------------------------------------------*\
* 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);
}
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Creates and sets up a socket * Creates and sets up a socket
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -58,7 +72,7 @@ const char *sock_create(p_sock ps, int domain, int type, int protocol)
{ {
int val = 1; int val = 1;
t_sock sock = socket(domain, type, protocol); t_sock sock = socket(domain, type, protocol);
if (sock == SOCK_INVALID) return sock_createstrerror(); if (sock == SOCK_INVALID) return sock_createstrerror(errno);
*ps = sock; *ps = sock;
sock_setnonblocking(ps); sock_setnonblocking(ps);
setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val)); setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
@ -79,27 +93,22 @@ const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
/* if no error, we're done */ /* if no error, we're done */
if (err == 0) return NULL; if (err == 0) return NULL;
/* make sure the system is trying to connect */ /* make sure the system is trying to connect */
if (errno != EINPROGRESS) return io_strerror(IO_ERROR); if (errno != EINPROGRESS) return sock_connectstrerror(errno);
/* wait for a timeout or for the system's answer */ /* wait for a timeout or for the system's answer */
for ( ;; ) { for ( ;; ) {
struct timeval tv;
fd_set rfds, wfds, efds; fd_set rfds, wfds, efds;
int timeout = tm_getretry(tm);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&rfds); FD_SET(sock, &rfds); FD_ZERO(&rfds); FD_SET(sock, &rfds);
FD_ZERO(&wfds); FD_SET(sock, &wfds); FD_ZERO(&wfds); FD_SET(sock, &wfds);
FD_ZERO(&efds); FD_SET(sock, &efds); FD_ZERO(&efds); FD_SET(sock, &efds);
/* we run select to avoid busy waiting */ /* we run select to avoid busy waiting */
err = select(sock+1, &rfds, &wfds, &efds, timeout >= 0? &tv: NULL); do err = sock_select(sock+1, &rfds, &wfds, &efds, tm_getretry(tm));
/* if select was interrupted, try again */ while (err < 0 && errno == EINTR);
if (err < 0 && errno == EINTR) continue;
/* if selects readable, try reading */ /* if selects readable, try reading */
if (err > 0) { if (err > 0) {
char dummy; char dummy;
/* recv will set errno to the value a blocking connect would set */ /* recv will set errno to the value a blocking connect would set */
if (recv(sock, &dummy, 0, 0) < 0 && errno != EAGAIN) if (recv(sock, &dummy, 0, 0) < 0 && errno != EWOULDBLOCK)
return sock_connectstrerror(); return sock_connectstrerror(errno);
else else
return NULL; return NULL;
/* if no event happened, there was a timeout */ /* if no event happened, there was a timeout */
@ -113,16 +122,24 @@ const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
{ {
if (bind(*ps, addr, addr_len) < 0) return sock_bindstrerror(); const char *err = NULL;
else return NULL; sock_setblocking(ps);
if (bind(*ps, addr, addr_len) < 0) err = sock_bindstrerror(errno);
sock_setnonblocking(ps);
return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* *
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
void sock_listen(p_sock ps, int backlog) const char* sock_listen(p_sock ps, int backlog)
{ {
listen(*ps, backlog); const char *err = NULL;
sock_setblocking(ps);
if (listen(*ps, backlog))
err = sock_listenstrerror(errno);
sock_setnonblocking(ps);
return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -130,35 +147,40 @@ void sock_listen(p_sock ps, int backlog)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
void sock_shutdown(p_sock ps, int how) void sock_shutdown(p_sock ps, int how)
{ {
sock_setblocking(ps);
shutdown(*ps, how); shutdown(*ps, how);
sock_setnonblocking(ps);
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Accept with timeout * Accept with timeout
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm) const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
socklen_t *addr_len, p_tm tm)
{ {
t_sock sock = *ps; t_sock sock = *ps;
SA dummy_addr; SA dummy_addr;
socklen_t dummy_len = sizeof(dummy_addr); socklen_t dummy_len = sizeof(dummy_addr);
if (sock == SOCK_INVALID) return IO_CLOSED; if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
if (!addr) addr = &dummy_addr; if (!addr) addr = &dummy_addr;
if (!addr_len) addr_len = &dummy_len; if (!addr_len) addr_len = &dummy_len;
for (;;) { for (;;) {
int timeout = tm_getretry(tm); int err;
struct timeval tv;
fd_set fds; fd_set fds;
*pa = accept(sock, addr, addr_len); *pa = accept(sock, addr, addr_len);
if (*pa != SOCK_INVALID) return IO_DONE; /* if result is valid, we are done */
if (timeout == 0) return IO_TIMEOUT; if (*pa != SOCK_INVALID) return NULL;
tv.tv_sec = timeout / 1000; /* find out if we failed for a fatal reason */
tv.tv_usec = (timeout % 1000) * 1000; if (errno != EWOULDBLOCK && errno != ECONNABORTED)
return sock_acceptstrerror(errno);
/* call select just to avoid busy-wait. */
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
/* call select just to avoid busy-wait. */ do err = sock_select(sock+1, &fds, NULL, NULL, tm_getretry(tm));
select(sock+1, &fds, NULL, NULL, timeout >= 0? &tv: NULL); while (err < 0 && errno == EINTR);
if (err == 0) return io_strerror(IO_TIMEOUT);
} }
return IO_TIMEOUT; /* can't get here */ return io_strerror(IO_TIMEOUT); /* can't get here */
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -176,18 +198,15 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
while (put < 0 && errno == EINTR); while (put < 0 && errno == EINTR);
/* deal with failure */ /* deal with failure */
if (put <= 0) { if (put <= 0) {
struct timeval tv;
fd_set fds; fd_set fds;
/* in any case, nothing has been sent */ /* in any case, nothing has been sent */
*sent = 0; *sent = 0;
/* here we know the connection has been closed */ /* here we know the connection has been closed */
if (errno == EPIPE) return IO_CLOSED; if (errno == EPIPE) return IO_CLOSED;
/* run select to avoid busy wait */ /* run select to avoid busy wait */
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
if (select(sock+1, NULL, &fds, NULL, timeout >= 0? &tv: NULL) <= 0) { if (sock_select(sock+1, NULL, &fds, NULL, timeout) <= 0) {
/* here the call was interrupted. calling again might work */ /* here the call was interrupted. calling again might work */
if (errno == EINTR) return IO_RETRY; if (errno == EINTR) return IO_RETRY;
/* here there was no data before timeout */ /* here there was no data before timeout */
@ -213,15 +232,12 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
do put = sendto(sock, data, count, 0, addr, addr_len); do put = sendto(sock, data, count, 0, addr, addr_len);
while (put < 0 && errno == EINTR); while (put < 0 && errno == EINTR);
if (put <= 0) { if (put <= 0) {
struct timeval tv;
fd_set fds; fd_set fds;
*sent = 0; *sent = 0;
if (errno == EPIPE) return IO_CLOSED; if (errno == EPIPE) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
if (select(sock+1, NULL, &fds, NULL, timeout >= 0? &tv: NULL) <= 0) { if (sock_select(sock+1, NULL, &fds, NULL, timeout) <= 0) {
if (errno == EINTR) return IO_RETRY; if (errno == EINTR) return IO_RETRY;
else return IO_TIMEOUT; else return IO_TIMEOUT;
} else return IO_DONE; } else return IO_DONE;
@ -242,16 +258,13 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
do taken = read(sock, data, count); do taken = read(sock, data, count);
while (taken < 0 && errno == EINTR); while (taken < 0 && errno == EINTR);
if (taken <= 0) { if (taken <= 0) {
struct timeval tv;
fd_set fds; fd_set fds;
int ret; int ret;
*got = 0; *got = 0;
if (taken == 0) return IO_CLOSED; if (taken == 0) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL); ret = sock_select(sock+1, &fds, NULL, NULL, timeout);
if (ret < 0 && errno == EINTR) return IO_RETRY; if (ret < 0 && errno == EINTR) return IO_RETRY;
if (ret == 0) return IO_TIMEOUT; if (ret == 0) return IO_TIMEOUT;
else return IO_DONE; else return IO_DONE;
@ -273,16 +286,13 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
do taken = recvfrom(sock, data, count, 0, addr, addr_len); do taken = recvfrom(sock, data, count, 0, addr, addr_len);
while (taken < 0 && errno == EINTR); while (taken < 0 && errno == EINTR);
if (taken <= 0) { if (taken <= 0) {
struct timeval tv;
fd_set fds; fd_set fds;
int ret; int ret;
*got = 0; *got = 0;
if (taken == 0) return IO_CLOSED; if (taken == 0) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL); ret = sock_select(sock+1, &fds, NULL, NULL, timeout);
if (ret < 0 && errno == EINTR) return IO_RETRY; if (ret < 0 && errno == EINTR) return IO_RETRY;
if (ret == 0) return IO_TIMEOUT; if (ret == 0) return IO_TIMEOUT;
else return IO_DONE; else return IO_DONE;
@ -315,53 +325,98 @@ void sock_setnonblocking(p_sock ps)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Error translation functions * Error translation functions
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
/* return error messages for the known errors reported by gethostbyname */
const char *sock_hoststrerror(void) const char *sock_hoststrerror(void)
{ {
switch (h_errno) { switch (h_errno) {
case HOST_NOT_FOUND: return "host not found"; case HOST_NOT_FOUND: return "host not found";
case NO_ADDRESS: return "unable to resolve host name"; case NO_ADDRESS: return "valid host but no ip found";
case NO_RECOVERY: return "name server error"; case NO_RECOVERY: return "name server error";
case TRY_AGAIN: return "name server unavailable, try again later"; case TRY_AGAIN: return "name server unavailable, try again later";
default: return "unknown error"; default: return "unknown error";
} }
} }
static const char *sock_createstrerror(void) /* return error messages for the known errors reported by socket */
static const char *sock_createstrerror(int err)
{ {
switch (errno) { switch (err) {
case EPROTONOSUPPORT: return "protocol not supported";
case EACCES: return "access denied"; case EACCES: return "access denied";
case EMFILE: return "descriptor table is full"; case EMFILE: return "process file table is full";
case ENFILE: return "too many open files"; case ENFILE: return "kernel file table is full";
case EINVAL: return "unknown protocol or family";
case ENOBUFS: return "insuffucient buffer space"; case ENOBUFS: return "insuffucient buffer space";
default: return "unknown error"; default: return "unknown error";
} }
} }
static const char *sock_bindstrerror(void) /* return error messages for the known errors reported by accept */
static const char *sock_acceptstrerror(int err)
{ {
switch (errno) { switch (err) {
case EWOULDBLOCK: return io_strerror(IO_RETRY);
case EBADF: return "invalid descriptor"; case EBADF: return "invalid descriptor";
case EINVAL: return "socket already bound"; case ENOBUFS: case ENOMEM: return "insuffucient buffer space";
case EACCES: return "access denied"; case ENOTSOCK: return "descriptor not a socket";
case ENOTSOCK: return "not a socket descriptor"; case EOPNOTSUPP: return "not supported";
case EINTR: return "call interrupted";
case ECONNABORTED: return "connection aborted";
case EINVAL: return "not listening";
case EMFILE: return "process file table is full";
case ENFILE: return "kernel file table is full";
case EFAULT: return "invalid memory address";
default: return "unknown error";
}
}
/* return error messages for the known errors reported by bind */
static const char *sock_bindstrerror(int err)
{
switch (err) {
case EBADF: return "invalid descriptor";
case ENOTSOCK: return "descriptor not a socket";
case EADDRNOTAVAIL: return "address unavailable in local host";
case EADDRINUSE: return "address already in use"; case EADDRINUSE: return "address already in use";
case EADDRNOTAVAIL: return "address unavailable"; case EINVAL: return "already bound";
case EACCES: return "access denied";
case EFAULT: return "invalid memory address";
case ENOMEM: return "out of memory"; case ENOMEM: return "out of memory";
default: return "unknown error"; default: return "unknown error";
} }
} }
static const char *sock_connectstrerror(void) /* return error messages for the known errors reported by listen */
static const char *sock_listenstrerror(int err)
{ {
switch (errno) { switch (err) {
case EADDRINUSE: return "local address already in use";
case EBADF: return "invalid descriptor"; case EBADF: return "invalid descriptor";
case ENOTSOCK: return "not a socket descriptor"; case ENOTSOCK: return "descriptor not a socket";
case EADDRNOTAVAIL: return "address not availabe"; case EOPNOTSUPP: return "not supported";
case ETIMEDOUT: return "connection timed out"; default: return "unknown error";
case ECONNREFUSED: return "connection refused"; }
case EACCES: return "access denied"; }
case ENETUNREACH: return "network is unreachable";
case EADDRINUSE: return "address already in use"; /* return error messages for the known errors reported by connect */
static const char *sock_connectstrerror(int err)
{
switch (err) {
case EBADF: return "invalid descriptor";
case EFAULT: return "invalid memory address";
case ENOTSOCK: return "descriptor not a socket";
case EADDRNOTAVAIL: return "address not available in local host";
case EISCONN: return "already connected";
case ECONNREFUSED: return "connection refused";
case ETIMEDOUT: return io_strerror(IO_TIMEOUT);
case ENETUNREACH: return "network is unreachable";
case EADDRINUSE: return "local address already in use";
case EINPROGRESS: return "would block";
case EALREADY: return "connect already in progress";
case EAGAIN: return "not enough free ports";
case EAFNOSUPPORT: return "address family not supported";
case EPERM: return "broadcast not enabled or firewall block";
default: return "unknown error"; default: return "unknown error";
} }
} }

View File

@ -15,9 +15,11 @@
#include "socket.h" #include "socket.h"
static const char *sock_createstrerror(void); static const char *sock_createstrerror(int err);
static const char *sock_bindstrerror(void); static const char *sock_bindstrerror(int err);
static const char *sock_connectstrerror(void); static const char *sock_connectstrerror(int err);
static const char *sock_acceptstrerror(int err);
static const char *sock_listenstrerror(int err);
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Initializes module * Initializes module
@ -35,12 +37,24 @@ int sock_open(void)
return 1; return 1;
} }
/*-------------------------------------------------------------------------*\
* 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);
}
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Close and inutilize socket * Close and inutilize socket
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
void sock_destroy(p_sock ps) void sock_destroy(p_sock ps)
{ {
if (*ps != SOCK_INVALID) { if (*ps != SOCK_INVALID) {
sock_setblocking(ps); /* close can take a long time on WIN32 */
closesocket(*ps); closesocket(*ps);
*ps = SOCK_INVALID; *ps = SOCK_INVALID;
} }
@ -51,7 +65,9 @@ void sock_destroy(p_sock ps)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
void sock_shutdown(p_sock ps, int how) void sock_shutdown(p_sock ps, int how)
{ {
sock_setblocking(ps);
shutdown(*ps, how); shutdown(*ps, how);
sock_setnonblocking(ps);
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -61,10 +77,11 @@ const char *sock_create(p_sock ps, int domain, int type, int protocol)
{ {
int val = 1; int val = 1;
t_sock sock = socket(domain, type, protocol); t_sock sock = socket(domain, type, protocol);
if (sock == SOCK_INVALID) return sock_createstrerror(); if (sock == SOCK_INVALID)
return sock_createstrerror(WSAGetLastError());
*ps = sock; *ps = sock;
sock_setnonblocking(ps);
setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val)); setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
sock_setnonblocking(ps);
return NULL; return NULL;
} }
@ -75,7 +92,6 @@ const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
{ {
t_sock sock = *ps; t_sock sock = *ps;
int err, timeout = tm_getretry(tm); int err, timeout = tm_getretry(tm);
struct timeval tv;
fd_set efds, wfds; fd_set efds, wfds;
/* don't call on closed socket */ /* don't call on closed socket */
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED); if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
@ -84,27 +100,24 @@ const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
/* if no error, we're done */ /* if no error, we're done */
if (err == 0) return NULL; if (err == 0) return NULL;
/* make sure the system is trying to connect */ /* make sure the system is trying to connect */
if (WSAGetLastError() != WSAEWOULDBLOCK) return sock_connectstrerror(); err = WSAGetLastError();
if (err != WSAEWOULDBLOCK) return sock_connectstrerror(err);
/* wait for a timeout or for the system's answer */ /* wait for a timeout or for the system's answer */
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&wfds); FD_SET(sock, &wfds); FD_ZERO(&wfds); FD_SET(sock, &wfds);
FD_ZERO(&efds); FD_SET(sock, &efds); FD_ZERO(&efds); FD_SET(sock, &efds);
/* we run select to wait */ /* we run select to wait */
err = select(0, NULL, &wfds, &efds, timeout >= 0? &tv: NULL); err = sock_select(0, NULL, &wfds, &efds, timeout);
/* if select returned due to an event */ /* if select returned due to an event */
if (err > 0 ) { if (err > 0 ) {
/* if was in efds, we failed */ /* if was in efds, we failed */
if (FD_ISSET(sock,&efds) || !FD_ISSET(sock,&wfds)) { if (FD_ISSET(sock, &efds)) {
int why; int why, len = sizeof(why);
int len = sizeof(why);
/* find out why we failed */ /* find out why we failed */
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&why, &len); getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&why, &len);
WSASetLastError(why); return sock_connectstrerror(why);
return sock_connectstrerror(); /* otherwise it must be in wfds, so we succeeded */
/* if was in wfds, we succeeded */
} else return NULL; } else return NULL;
/* if nothing happened, we timed out */ /* if no event happened, we timed out */
} else return io_strerror(IO_TIMEOUT); } else return io_strerror(IO_TIMEOUT);
} }
@ -113,44 +126,60 @@ const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
{ {
if (bind(*ps, addr, addr_len) < 0) return sock_bindstrerror(); const char *err = NULL;
else return NULL; sock_setblocking(ps);
if (bind(*ps, addr, addr_len) < 0)
err = sock_bindstrerror(WSAGetLastError());
sock_setnonblocking(ps);
return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* *
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
void sock_listen(p_sock ps, int backlog) const char *sock_listen(p_sock ps, int backlog)
{ {
listen(*ps, backlog); const char *err = NULL;
sock_setblocking(ps);
if (listen(*ps, backlog) < 0)
err = sock_listenstrerror(WSAGetLastError());
sock_setnonblocking(ps);
return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Accept with timeout * Accept with timeout
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm) const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
socklen_t *addr_len, p_tm tm)
{ {
t_sock sock = *ps; t_sock sock = *ps;
SA dummy_addr; SA dummy_addr;
socklen_t dummy_len = sizeof(dummy_addr); socklen_t dummy_len = sizeof(dummy_addr);
if (sock == SOCK_INVALID) return IO_CLOSED; if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
if (!addr) addr = &dummy_addr; if (!addr) addr = &dummy_addr;
if (!addr_len) addr_len = &dummy_len; if (!addr_len) addr_len = &dummy_len;
for (;;) { for (;;) {
fd_set rfds;
int timeout = tm_getretry(tm); int timeout = tm_getretry(tm);
struct timeval tv; int err;
fd_set fds; /* try to get client socket */
*pa = accept(sock, addr, addr_len); *pa = accept(sock, addr, addr_len);
if (*pa != SOCK_INVALID) return IO_DONE; /* if return is valid, we are done */
if (timeout == 0) return IO_TIMEOUT; if (*pa != SOCK_INVALID) return NULL;
tv.tv_sec = timeout / 1000; /* optimization */
tv.tv_usec = (timeout % 1000) * 1000; if (timeout == 0) return io_strerror(IO_TIMEOUT);
FD_ZERO(&fds); /* otherwise find out why we failed */
FD_SET(sock, &fds); err = WSAGetLastError();
/* call select just to avoid busy-wait. */ /* if we failed because there was no connectoin, keep trying*/
select(0, &fds, NULL, NULL, timeout >= 0? &tv: NULL); 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);
} }
return IO_TIMEOUT; /* can't get here */ return io_strerror(IO_TIMEOUT); /* can't get here */
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -172,13 +201,10 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
*sent = 0; *sent = 0;
/* run select to avoid busy wait */ /* run select to avoid busy wait */
if (WSAGetLastError() == WSAEWOULDBLOCK) { if (WSAGetLastError() == WSAEWOULDBLOCK) {
struct timeval tv;
fd_set fds; fd_set fds;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = select(0, NULL, &fds, NULL, timeout >= 0 ? &tv : NULL); ret = sock_select(0, NULL, &fds, NULL, timeout);
/* tell the caller to call us again because there is more data */ /* tell the caller to call us again because there is more data */
if (ret > 0) return IO_DONE; if (ret > 0) return IO_DONE;
/* tell the caller there was no data before timeout */ /* tell the caller there was no data before timeout */
@ -211,13 +237,10 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
*sent = 0; *sent = 0;
/* run select to avoid busy wait */ /* run select to avoid busy wait */
if (WSAGetLastError() == WSAEWOULDBLOCK) { if (WSAGetLastError() == WSAEWOULDBLOCK) {
struct timeval tv;
fd_set fds; fd_set fds;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = select(0, NULL, &fds, NULL, timeout >= 0 ? &tv : NULL); ret = sock_select(0, NULL, &fds, NULL, timeout);
/* tell the caller to call us again because there is more data */ /* tell the caller to call us again because there is more data */
if (ret > 0) return IO_DONE; if (ret > 0) return IO_DONE;
/* tell the caller there was no data before timeout */ /* tell the caller there was no data before timeout */
@ -241,16 +264,13 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
if (sock == SOCK_INVALID) return IO_CLOSED; if (sock == SOCK_INVALID) return IO_CLOSED;
taken = recv(sock, data, (int) count, 0); taken = recv(sock, data, (int) count, 0);
if (taken <= 0) { if (taken <= 0) {
struct timeval tv;
fd_set fds; fd_set fds;
int ret; int ret;
*got = 0; *got = 0;
if (taken == 0) return IO_CLOSED; if (taken == 0) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = select(0, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL); ret = sock_select(0, &fds, NULL, NULL, timeout);
if (ret > 0) return IO_DONE; if (ret > 0) return IO_DONE;
else return IO_TIMEOUT; else return IO_TIMEOUT;
} else { } else {
@ -270,16 +290,13 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
if (sock == SOCK_INVALID) return IO_CLOSED; if (sock == SOCK_INVALID) return IO_CLOSED;
taken = recvfrom(sock, data, (int) count, 0, addr, addr_len); taken = recvfrom(sock, data, (int) count, 0, addr, addr_len);
if (taken <= 0) { if (taken <= 0) {
struct timeval tv;
fd_set fds; fd_set fds;
int ret; int ret;
*got = 0; *got = 0;
if (taken == 0) return IO_CLOSED; if (taken == 0) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = select(0, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL); ret = sock_select(0, &fds, NULL, NULL, timeout);
if (ret > 0) return IO_DONE; if (ret > 0) return IO_DONE;
else return IO_TIMEOUT; else return IO_TIMEOUT;
} else { } else {
@ -309,51 +326,117 @@ void sock_setnonblocking(p_sock ps)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Error translation functions * Error translation functions
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
/* return error messages for the known errors reported by gethostbyname */
const char *sock_hoststrerror(void) const char *sock_hoststrerror(void)
{ {
switch (WSAGetLastError()) { switch (WSAGetLastError()) {
case HOST_NOT_FOUND: return "host not found"; case WSANOTINITIALISED: return "not initialized";
case NO_ADDRESS: return "unable to resolve host name"; case WSAENETDOWN: return "network is down";
case NO_RECOVERY: return "name server error"; case WSAHOST_NOT_FOUND: return "host not found";
case TRY_AGAIN: return "name server unavailable, try again later."; 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";
default: return "unknown error"; default: return "unknown error";
} }
} }
static const char *sock_createstrerror(void) /* return error messages for the known errors reported by socket */
static const char *sock_createstrerror(int err)
{ {
switch (WSAGetLastError()) { switch (err) {
case WSANOTINITIALISED: return "not initialized"; case WSANOTINITIALISED: return "not initialized";
case WSAENETDOWN: return "network is down"; case WSAENETDOWN: return "network is down";
case WSAEAFNOSUPPORT: return "address family not supported";
case WSAEINPROGRESS: return "another call in progress";
case WSAEMFILE: return "descriptor table is full"; case WSAEMFILE: return "descriptor table is full";
case WSAENOBUFS: return "insufficient buffer space"; case WSAENOBUFS: return "insufficient buffer space";
case WSAEPROTONOSUPPORT: return "protocol not supported";
case WSAEPROTOTYPE: return "wrong protocol type";
case WSAESOCKTNOSUPPORT: return "socket type not supported by family";
default: return "unknown error"; default: return "unknown error";
} }
} }
static const char *sock_bindstrerror(void) /* return error messages for the known errors reported by accept */
static const char *sock_acceptstrerror(int err)
{ {
switch (WSAGetLastError()) { switch (err) {
case WSANOTINITIALISED: return "not initialized"; case WSANOTINITIALISED: return "not initialized";
case WSAENETDOWN: return "network is down"; case WSAENETDOWN: return "network is down";
case WSAEADDRINUSE: return "address already in use"; case WSAEFAULT: return "invalid memory address";
case WSAEINVAL: return "socket already bound"; case WSAEINTR: return "call interrupted";
case WSAENOBUFS: return "too many connections"; case WSAEINPROGRESS: return "another call in progress";
case WSAEFAULT: return "invalid address"; case WSAEINVAL: return "not listening";
case WSAENOTSOCK: return "not a socket descriptor"; 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";
default: return "unknown error"; default: return "unknown error";
} }
} }
static const char *sock_connectstrerror(void) /* return error messages for the known errors reported by bind */
static const char *sock_bindstrerror(int err)
{ {
switch (WSAGetLastError()) { switch (err) {
case WSANOTINITIALISED: return "not initialized"; case WSANOTINITIALISED: return "not initialized";
case WSAENETDOWN: return "network is down"; case WSAENETDOWN: return "network is down";
case WSAEACCES: return "broadcast not enabled for socket";
case WSAEADDRINUSE: return "address already in use"; case WSAEADDRINUSE: return "address already in use";
case WSAEADDRNOTAVAIL: return "address unavailable"; 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";
case WSAECONNREFUSED: return "connection refused"; case WSAECONNREFUSED: return "connection refused";
case WSAEFAULT: return "invalid memory address";
case WSAEINVAL: return "socket is listening";
case WSAEISCONN: return "socket already connected";
case WSAENETUNREACH: return "network is unreachable"; case WSAENETUNREACH: return "network is unreachable";
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";
default: return "unknown error"; default: return "unknown error";
} }
} }

View File

@ -117,7 +117,6 @@ function test_mixed(len)
local p3 = "raw " .. string.rep("z", inter) .. "bytes" local p3 = "raw " .. string.rep("z", inter) .. "bytes"
local p4 = "end" .. string.rep("w", inter) .. "bytes" local p4 = "end" .. string.rep("w", inter) .. "bytes"
local bp1, bp2, bp3, bp4 local bp1, bp2, bp3, bp4
pass(len .. " byte(s) patterns")
remote (string.format("str = data:receive(%d)", remote (string.format("str = data:receive(%d)",
string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4)))
sent, err = data:send(p1, p2, p3, p4) sent, err = data:send(p1, p2, p3, p4)
@ -137,7 +136,6 @@ function test_asciiline(len)
str = string.rep("x", math.mod(len, 10)) str = string.rep("x", math.mod(len, 10))
str10 = string.rep("aZb.c#dAe?", math.floor(len/10)) str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
str = str .. str10 str = str .. str10
pass(len .. " byte(s) line")
remote "str = data:receive()" remote "str = data:receive()"
sent, err = data:send(str, "\n") sent, err = data:send(str, "\n")
if err then fail(err) end if err then fail(err) end
@ -156,7 +154,6 @@ function test_rawline(len)
str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100),
math.floor(len/10)) math.floor(len/10))
str = str .. str10 str = str .. str10
pass(len .. " byte(s) line")
remote "str = data:receive()" remote "str = data:receive()"
sent, err = data:send(str, "\n") sent, err = data:send(str, "\n")
if err then fail(err) end if err then fail(err) end
@ -174,7 +171,6 @@ function test_raw(len)
local s1, s2, back, err local s1, s2, back, err
s1 = string.rep("x", half) s1 = string.rep("x", half)
s2 = string.rep("y", len-half) s2 = string.rep("y", len-half)
pass(len .. " byte(s) block")
remote (string.format("str = data:receive(%d)", len)) remote (string.format("str = data:receive(%d)", len))
sent, err = data:send(s1) sent, err = data:send(s1)
if err then fail(err) end if err then fail(err) end
@ -271,12 +267,10 @@ end
function empty_connect() function empty_connect()
reconnect() reconnect()
if data then data:close() data = nil end if data then data:close() data = nil end
print("before remote")
remote [[ remote [[
if data then data:close() data = nil end if data then data:close() data = nil end
data = server:accept() data = server:accept()
]] ]]
print("after remote")
data, err = socket.connect("", port) data, err = socket.connect("", port)
if not data then if not data then
pass("ok") pass("ok")
@ -286,7 +280,7 @@ end
------------------------------------------------------------------------ ------------------------------------------------------------------------
function isclosed(c) function isclosed(c)
return c:fd() == -1 or c:fd() == (2^32-1) return c:getfd() == -1 or c:getfd() == (2^32-1)
end end
function active_close() function active_close()
@ -354,13 +348,14 @@ end
------------------------------------------------------------------------ ------------------------------------------------------------------------
function accept_timeout() function accept_timeout()
io.write("accept with timeout (if it hangs, it failed): ")
local s, e = socket.bind("*", 0, 0) local s, e = socket.bind("*", 0, 0)
assert(s, e) assert(s, e)
local t = socket.time() local t = socket.time()
s:settimeout(1) s:settimeout(1)
local c, e = s:accept() local c, e = s:accept()
assert(not c, "should not accept") assert(not c, "should not accept")
assert(e == "timeout", "wrong error message") assert(e == "timeout", string.format("wrong error message (%s)", e))
t = socket.time() - t t = socket.time() - t
assert(t < 2, string.format("took to long to give up (%gs)", t)) assert(t < 2, string.format("took to long to give up (%gs)", t))
s:close() s:close()
@ -369,21 +364,50 @@ end
------------------------------------------------------------------------ ------------------------------------------------------------------------
function connect_timeout() function connect_timeout()
io.write("connect with timeout (if it hangs, it failed): ")
local t = socket.time() local t = socket.time()
local c, e = socket.tcp() local c, e = socket.tcp()
assert(c, e) assert(c, e)
c:settimeout(0.1) c:settimeout(0.1)
local r, e = c:connect("ibere.tecgraf.puc-rio.br", 80) local r, e = c:connect("ibere.tecgraf.puc-rio.br", 80)
if r or e ~= "timeout" then assert(not r, "should not connect")
print("wrong error message (this test is flaky anyways)") assert(e == "timeout", e)
end assert(socket.time() - t < 2, "took to long to give up")
if socket.time() - t > 1 then
print("took to long to give up")
end
print("whatever")
c:close() c:close()
end end
------------------------------------------------------------------------
function accept_errors()
io.write("not listenning: ")
local d, e = socket.bind("*", 0)
assert(d, e);
local c, e = socket.tcp();
assert(c, e);
d:setfd(c:getfd())
local r, e = d:accept()
assert(not r and e == "not listening", e)
print("ok")
io.write("not supported: ")
local c, e = socket.udp()
assert(c, e);
d:setfd(c:getfd())
local r, e = d:accept()
assert(not r and e == "not supported", e)
print("ok")
end
------------------------------------------------------------------------
function connect_errors()
io.write("connection refused: ")
local c, e = socket.connect("localhost", 1);
assert(not c and e == "connection refused", e)
print("ok")
io.write("host not found: ")
local c, e = socket.connect("not.exist.com", 1);
assert(not c and e == "host not found", e)
print("ok")
end
------------------------------------------------------------------------ ------------------------------------------------------------------------
function rebind_test() function rebind_test()
local c = socket.bind("localhost", 0) local c = socket.bind("localhost", 0)
@ -400,40 +424,44 @@ end
------------------------------------------------------------------------ ------------------------------------------------------------------------
test("method registration") test("method registration")
test_methods(socket.tcp(), { test_methods(socket.tcp(), {
"connect",
"send",
"receive",
"bind",
"accept", "accept",
"setpeername", "bind",
"setsockname", "close",
"connect",
"getpeername", "getpeername",
"getsockname", "getsockname",
"listen",
"receive",
"send",
"setoption", "setoption",
"setpeername",
"setsockname",
"settimeout", "settimeout",
"shutdown", "shutdown",
"close",
}) })
test_methods(socket.udp(), { test_methods(socket.udp(), {
"close",
"getpeername", "getpeername",
"getsockname", "getsockname",
"setsockname",
"setpeername",
"send",
"sendto",
"receive", "receive",
"receivefrom", "receivefrom",
"send",
"sendto",
"setoption", "setoption",
"setpeername",
"setsockname",
"settimeout", "settimeout",
"shutdown", "shutdown",
"close",
}) })
test("select function") test("select function")
test_selectbugs() test_selectbugs()
test("empty host connect: ") test("connect function")
connect_timeout()
empty_connect() empty_connect()
connect_errors()
test("rebinding: ") test("rebinding: ")
rebind_test() rebind_test()
@ -444,11 +472,10 @@ active_close()
test("closed connection detection: ") test("closed connection detection: ")
test_closed() test_closed()
test("accept with timeout (if it hangs, it failed:)") test("accept function: ")
accept_timeout() accept_timeout()
accept_errors()
test("connect with timeout (if it hangs, it failed:)")
connect_timeout()
test("mixed patterns") test("mixed patterns")
test_mixed(1) test_mixed(1)