Still need to fix windows. :o/

This commit is contained in:
Diego Nehab 2004-07-15 06:11:53 +00:00
parent 471334c3d0
commit 9a79d500eb
11 changed files with 316 additions and 302 deletions

3
TODO
View File

@ -1,3 +1,6 @@
test associativity of socket.select
probably if is dirty, no assoc is created. wonder why...
take a look at DB's smtp patch take a look at DB's smtp patch
optmize aux_getgroupudata (Mike idea) optmize aux_getgroupudata (Mike idea)

View File

@ -77,7 +77,7 @@ int buf_meth_send(lua_State *L, p_buf buf) {
/* check if there was an error */ /* check if there was an error */
if (err != IO_DONE) { if (err != IO_DONE) {
lua_pushnil(L); lua_pushnil(L);
io_pusherror(L, buf->io, err); lua_pushstring(L, buf->io->error(buf->io->ctx, err));
lua_pushnumber(L, total); lua_pushnumber(L, total);
} else { } else {
lua_pushnumber(L, total); lua_pushnumber(L, total);
@ -98,22 +98,26 @@ int buf_meth_receive(lua_State *L, p_buf buf) {
int err = IO_DONE, top = lua_gettop(L); int err = IO_DONE, top = lua_gettop(L);
p_tm tm = tm_markstart(buf->tm); p_tm tm = tm_markstart(buf->tm);
luaL_Buffer b; luaL_Buffer b;
size_t size;
const char *part = luaL_optlstring(L, 3, "", &size);
/* initialize buffer with optional extra prefix
* (useful for concatenating previous partial results) */
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
/* receive all patterns */ luaL_addlstring(&b, part, size);
/* receive new patterns */
if (!lua_isnumber(L, 2)) { if (!lua_isnumber(L, 2)) {
static const char *patternnames[] = {"*l", "*a", NULL}; const char *p= luaL_optstring(L, 2, "*l");
const char *pattern = luaL_optstring(L, 2, "*l"); if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b);
/* get next pattern */ else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b);
int p = luaL_findstring(pattern, patternnames);
if (p == 0) err = recvline(buf, &b);
else if (p == 1) err = recvall(buf, &b);
else luaL_argcheck(L, 0, 2, "invalid receive pattern"); else luaL_argcheck(L, 0, 2, "invalid receive pattern");
/* get a fixed number of bytes */ /* get a fixed number of bytes */
} else err = recvraw(buf, (size_t) lua_tonumber(L, 2), &b); } else err = recvraw(buf, (size_t) lua_tonumber(L, 2), &b);
/* check if there was an error */ /* check if there was an error */
if (err != IO_DONE) { if (err != IO_DONE) {
/* we can't push anyting in the stack before pushing the
* contents of the buffer. this is the reason for the complication */
luaL_pushresult(&b); luaL_pushresult(&b);
io_pusherror(L, buf->io, err); lua_pushstring(L, buf->io->error(buf->io->ctx, err));
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
lua_pushnil(L); lua_pushnil(L);
lua_replace(L, -4); lua_replace(L, -4);

View File

@ -50,22 +50,28 @@ int inet_open(lua_State *L)
* Returns all information provided by the resolver given a host name * Returns all information provided by the resolver given a host name
* or ip address * or ip address
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int inet_global_toip(lua_State *L) static int inet_gethost(const char *address, struct hostent **hp) {
{
const char *address = luaL_checkstring(L, 1);
struct in_addr addr; struct in_addr addr;
struct hostent *hp;
if (inet_aton(address, &addr)) if (inet_aton(address, &addr))
hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET); return sock_gethostbyaddr((char *) &addr, sizeof(addr), hp);
else else
hp = gethostbyname(address); return sock_gethostbyname(address, hp);
if (!hp) { }
/*-------------------------------------------------------------------------*\
* Returns all information provided by the resolver given a host name
* or ip address
\*-------------------------------------------------------------------------*/
static int inet_global_tohostname(lua_State *L) {
const char *address = luaL_checkstring(L, 1);
struct hostent *hp = NULL;
int err = inet_gethost(address, &hp);
if (err != IO_DONE) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, sock_hoststrerror()); lua_pushstring(L, sock_hoststrerror(err));
return 2; return 2;
} }
addr = *((struct in_addr *) hp->h_addr); lua_pushstring(L, hp->h_name);
lua_pushstring(L, inet_ntoa(addr));
inet_pushresolved(L, hp); inet_pushresolved(L, hp);
return 2; return 2;
} }
@ -74,25 +80,22 @@ static int inet_global_toip(lua_State *L)
* Returns all information provided by the resolver given a host name * Returns all information provided by the resolver given a host name
* or ip address * or ip address
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int inet_global_tohostname(lua_State *L) static int inet_global_toip(lua_State *L)
{ {
const char *address = luaL_checkstring(L, 1); const char *address = luaL_checkstring(L, 1);
struct in_addr addr; struct hostent *hp = NULL;
struct hostent *hp; int err = inet_gethost(address, &hp);
if (inet_aton(address, &addr)) if (err != IO_DONE) {
hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
else
hp = gethostbyname(address);
if (!hp) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, sock_hoststrerror()); lua_pushstring(L, sock_hoststrerror(err));
return 2; return 2;
} }
lua_pushstring(L, hp->h_name); lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr)));
inet_pushresolved(L, hp); inet_pushresolved(L, hp);
return 2; return 2;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Gets the host name * Gets the host name
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -191,9 +194,8 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Tries to create a new inet socket * Tries to create a new inet socket
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *inet_trycreate(p_sock ps, int type) const char *inet_trycreate(p_sock ps, int type) {
{ return sock_strerror(sock_create(ps, AF_INET, type, 0));
return sock_create(ps, AF_INET, type, 0);
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -203,22 +205,23 @@ const char *inet_tryconnect(p_sock ps, const char *address,
unsigned short port, p_tm tm) unsigned short port, p_tm tm)
{ {
struct sockaddr_in remote; struct sockaddr_in remote;
const char *err; int err;
memset(&remote, 0, sizeof(remote)); memset(&remote, 0, sizeof(remote));
remote.sin_family = AF_INET; remote.sin_family = AF_INET;
remote.sin_port = htons(port); remote.sin_port = htons(port);
if (strcmp(address, "*")) { if (strcmp(address, "*")) {
if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) { if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) {
struct hostent *hp = gethostbyname(address); struct hostent *hp = NULL;
struct in_addr **addr; struct in_addr **addr;
if (!hp) return sock_hoststrerror(); err = sock_gethostbyname(address, &hp);
if (err != IO_DONE) return sock_hoststrerror(err);
addr = (struct in_addr **) hp->h_addr_list; addr = (struct in_addr **) hp->h_addr_list;
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
} }
} else remote.sin_family = AF_UNSPEC; } else remote.sin_family = AF_UNSPEC;
err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm);
if (err) sock_destroy(ps); if (err != IO_DONE) sock_destroy(ps);
return err; return sock_strerror(err);
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -227,7 +230,7 @@ const char *inet_tryconnect(p_sock ps, const char *address,
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)
{ {
struct sockaddr_in local; struct sockaddr_in local;
const char *err; int err;
memset(&local, 0, sizeof(local)); memset(&local, 0, sizeof(local));
/* address is either wildcard or a valid ip address */ /* address is either wildcard or a valid ip address */
local.sin_addr.s_addr = htonl(INADDR_ANY); local.sin_addr.s_addr = htonl(INADDR_ANY);
@ -235,15 +238,16 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port)
local.sin_family = AF_INET; local.sin_family = AF_INET;
if (strcmp(address, "*") && if (strcmp(address, "*") &&
(!strlen(address) || !inet_aton(address, &local.sin_addr))) { (!strlen(address) || !inet_aton(address, &local.sin_addr))) {
struct hostent *hp = gethostbyname(address); struct hostent *hp = NULL;
struct in_addr **addr; struct in_addr **addr;
if (!hp) return sock_hoststrerror(); err = sock_gethostbyname(address, &hp);
if (err != IO_DONE) return sock_hoststrerror(err);
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));
} }
err = sock_bind(ps, (SA *) &local, sizeof(local)); err = sock_bind(ps, (SA *) &local, sizeof(local));
if (err) sock_destroy(ps); if (err != IO_DONE) sock_destroy(ps);
return err; return sock_strerror(err);
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\

View File

@ -12,18 +12,18 @@
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Initializes C structure * Initializes C structure
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
void io_init(p_io io, p_send send, p_recv recv, p_geterr geterr, void *ctx) { void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) {
io->send = send; io->send = send;
io->recv = recv; io->recv = recv;
io->geterr = geterr; io->error = error;
io->ctx = ctx; io->ctx = ctx;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Translate error codes to Lua * I/O error strings
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *io_strerror(int code) { const char *io_strerror(int err) {
switch (code) { switch (err) {
case IO_DONE: return NULL; case IO_DONE: return NULL;
case IO_CLOSED: return "closed"; case IO_CLOSED: return "closed";
case IO_TIMEOUT: return "timeout"; case IO_TIMEOUT: return "timeout";
@ -31,15 +31,3 @@ const char *io_strerror(int code) {
default: return "unknown error"; default: return "unknown error";
} }
} }
/*-------------------------------------------------------------------------*\
* Push error message from code or from driver
\*-------------------------------------------------------------------------*/
void io_pusherror(lua_State *L, p_io io, int code)
{
const char *err = NULL;
if (code < IO_USER) err = io_strerror(code);
else err = io->geterr(io->ctx, code);
if (err) lua_pushstring(L, err);
else lua_pushnil(L);
}

View File

@ -21,13 +21,18 @@
/* IO error codes */ /* IO error codes */
enum { enum {
IO_DONE, /* operation completed successfully */ IO_DONE = 0, /* operation completed successfully */
IO_TIMEOUT, /* operation timed out */ IO_TIMEOUT = -1, /* operation timed out */
IO_CLOSED, /* the connection has been closed */ IO_CLOSED = -2, /* the connection has been closed */
IO_CLIPPED, /* maxium bytes count reached */ IO_CLIPPED = -3 /* maxium bytes count reached */
IO_USER /* last element in enum is user custom error */
}; };
/* interface to error message function */
typedef const char *(*p_error) (
void *ctx, /* context needed by send */
int err /* error code */
);
/* interface to send function */ /* interface to send function */
typedef int (*p_send) ( typedef int (*p_send) (
void *ctx, /* context needed by send */ void *ctx, /* context needed by send */
@ -37,12 +42,6 @@ typedef int (*p_send) (
p_tm tm /* timeout control */ p_tm tm /* timeout control */
); );
/* returns an error string */
typedef const char *(*p_geterr) (
void *ctx, /* context needed by geterror */
int code /* error code */
);
/* interface to recv function */ /* interface to recv function */
typedef int (*p_recv) ( typedef int (*p_recv) (
void *ctx, /* context needed by recv */ void *ctx, /* context needed by recv */
@ -57,12 +56,11 @@ typedef struct t_io_ {
void *ctx; /* context needed by send/recv */ void *ctx; /* context needed by send/recv */
p_send send; /* send function pointer */ p_send send; /* send function pointer */
p_recv recv; /* receive function pointer */ p_recv recv; /* receive function pointer */
p_geterr geterr; /* receive function pointer */ p_error error; /* strerror function */
} t_io; } t_io;
typedef t_io *p_io; typedef t_io *p_io;
const char *io_strerror(int code); void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx);
void io_pusherror(lua_State *L, p_io io, int code); const char *io_strerror(int err);
void io_init(p_io io, p_send send, p_recv recv, p_geterr geterr, void *ctx);
#endif /* IO_H */ #endif /* IO_H */

View File

@ -41,8 +41,6 @@ int sock_open(void);
int sock_close(void); int sock_close(void);
void sock_destroy(p_sock ps); void sock_destroy(p_sock ps);
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, size_t *sent, p_tm tm);
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm);
int sock_sendto(p_sock ps, const char *data, size_t count, int sock_sendto(p_sock ps, const char *data, size_t count,
size_t *sent, SA *addr, socklen_t addr_len, p_tm tm); size_t *sent, SA *addr, socklen_t addr_len, p_tm tm);
int sock_recvfrom(p_sock ps, char *data, size_t count, int sock_recvfrom(p_sock ps, char *data, size_t count,
@ -51,15 +49,22 @@ void sock_setnonblocking(p_sock ps);
void sock_setblocking(p_sock ps); void sock_setblocking(p_sock ps);
int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm); int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm);
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm); int 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); int sock_create(p_sock ps, int domain, int type, int protocol);
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len); int sock_bind(p_sock ps, SA *addr, socklen_t addr_len);
const char *sock_listen(p_sock ps, int backlog); int sock_listen(p_sock ps, int backlog);
const char *sock_accept(p_sock ps, p_sock pa, SA *addr, int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm);
socklen_t *addr_len, p_tm tm);
const char *sock_geterr(p_sock ps, int code); const char *sock_hoststrerror(int err);
const char *sock_hoststrerror(void); const char *sock_strerror(int err);
const char *sock_strerror(void);
/* these are perfect to use with the io abstraction module
and the buffered input module */
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm);
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm);
const char *sock_ioerror(p_sock ps, int err);
int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp);
int sock_gethostbyname(const char *addr, struct hostent **hp);
#endif /* SOCK_H */ #endif /* SOCK_H */

View File

@ -160,22 +160,22 @@ 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;
const char *err = sock_accept(&server->sock, &sock, NULL, NULL, tm); int err = sock_accept(&server->sock, &sock, NULL, NULL, tm);
/* if successful, push client socket */ /* if successful, push client socket */
if (!err) { if (err == IO_DONE) {
p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
aux_setclass(L, "tcp{client}", -1); aux_setclass(L, "tcp{client}", -1);
/* initialize structure fields */ /* initialize structure fields */
sock_setnonblocking(&sock); sock_setnonblocking(&sock);
clnt->sock = sock; clnt->sock = sock;
io_init(&clnt->io, (p_send) sock_send, (p_recv) sock_recv, io_init(&clnt->io, (p_send) sock_send, (p_recv) sock_recv,
(p_geterr) sock_geterr, &clnt->sock); (p_error) sock_ioerror, &clnt->sock);
tm_init(&clnt->tm, -1, -1); tm_init(&clnt->tm, -1, -1);
buf_init(&clnt->buf, &clnt->io, &clnt->tm); buf_init(&clnt->buf, &clnt->io, &clnt->tm);
return 1; return 1;
} else { } else {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, sock_strerror(err));
return 2; return 2;
} }
} }
@ -236,10 +236,10 @@ static int meth_listen(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);
int backlog = (int) luaL_optnumber(L, 2, 32); int backlog = (int) luaL_optnumber(L, 2, 32);
const char *err = sock_listen(&tcp->sock, backlog); int err = sock_listen(&tcp->sock, backlog);
if (err) { if (err != IO_DONE) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, sock_strerror(err));
return 2; return 2;
} }
/* turn master object into a server object */ /* turn master object into a server object */
@ -320,7 +320,7 @@ static int global_create(lua_State *L)
sock_setnonblocking(&sock); sock_setnonblocking(&sock);
tcp->sock = sock; tcp->sock = sock;
io_init(&tcp->io, (p_send) sock_send, (p_recv) sock_recv, io_init(&tcp->io, (p_send) sock_send, (p_recv) sock_recv,
(p_geterr) sock_geterr, &tcp->sock); (p_error) sock_ioerror, &tcp->sock);
tm_init(&tcp->tm, -1, -1); tm_init(&tcp->tm, -1, -1);
buf_init(&tcp->buf, &tcp->io, &tcp->tm); buf_init(&tcp->buf, &tcp->io, &tcp->tm);
return 1; return 1;

View File

@ -25,4 +25,6 @@ double tm_getstart(p_tm tm);
double tm_gettime(void); double tm_gettime(void);
int tm_meth_settimeout(lua_State *L, p_tm tm); int tm_meth_settimeout(lua_State *L, p_tm tm);
#define tm_iszero(tm) ((tm)->block == 0.0)
#endif /* TM_H */ #endif /* TM_H */

View File

@ -41,7 +41,6 @@ static int meth_settimeout(lua_State *L);
static int meth_getfd(lua_State *L); static int meth_getfd(lua_State *L);
static int meth_setfd(lua_State *L); static int meth_setfd(lua_State *L);
static int meth_dirty(lua_State *L); static int meth_dirty(lua_State *L);
static void pusherror(lua_State *L, int code);
/* udp object methods */ /* udp object methods */
static luaL_reg udp[] = { static luaL_reg udp[] = {
@ -100,18 +99,14 @@ int udp_open(lua_State *L)
return 0; return 0;
} }
/*=========================================================================*\ /*=========================================================================*\
* Lua methods * Lua methods
\*=========================================================================*/ \*=========================================================================*/
/*-------------------------------------------------------------------------*\ const char *udp_strerror(int err) {
* Pushes the error message /* a 'closed' error on an unconnected means the target address was not
\*-------------------------------------------------------------------------*/ * accepted by the transport layer */
void pusherror(lua_State *L, int code) { if (err == IO_CLOSED) return "refused";
const char *err = code != IO_USER? io_strerror(code): "refused"; else return sock_strerror(err);
err = err? err: sock_strerror();
if (err) lua_pushstring(L, err);
else lua_pushnil(L);
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -125,12 +120,13 @@ static int meth_send(lua_State *L) {
const char *data = luaL_checklstring(L, 2, &count); const char *data = luaL_checklstring(L, 2, &count);
tm_markstart(tm); tm_markstart(tm);
err = sock_send(&udp->sock, data, count, &sent, tm); err = sock_send(&udp->sock, data, count, &sent, tm);
if (err == IO_DONE) lua_pushnumber(L, sent); if (err != IO_DONE) {
else lua_pushnil(L); lua_pushnil(L);
/* a 'closed' error on an unconnected means the target address was not lua_pushstring(L, udp_strerror(err));
* accepted by the transport layer */ return 2;
pusherror(L, err == IO_CLOSED ? IO_USER : err); }
return 2; lua_pushnumber(L, sent);
return 1;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -153,12 +149,13 @@ static int meth_sendto(lua_State *L) {
tm_markstart(tm); tm_markstart(tm);
err = sock_sendto(&udp->sock, data, count, &sent, err = sock_sendto(&udp->sock, data, count, &sent,
(SA *) &addr, sizeof(addr), tm); (SA *) &addr, sizeof(addr), tm);
if (err == IO_DONE) lua_pushnumber(L, sent); if (err != IO_DONE) {
else lua_pushnil(L); lua_pushnil(L);
/* a 'closed' error on an unconnected means the target address was not lua_pushstring(L, udp_strerror(err));
* accepted by the transport layer */ return 2;
pusherror(L, err == IO_CLOSED ? IO_USER : err); }
return 2; lua_pushnumber(L, sent);
return 1;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -173,10 +170,13 @@ static int meth_receive(lua_State *L) {
count = MIN(count, sizeof(buffer)); count = MIN(count, sizeof(buffer));
tm_markstart(tm); tm_markstart(tm);
err = sock_recv(&udp->sock, buffer, count, &got, tm); err = sock_recv(&udp->sock, buffer, count, &got, tm);
if (err == IO_DONE) lua_pushlstring(L, buffer, got); if (err != IO_DONE) {
else lua_pushnil(L); lua_pushnil(L);
pusherror(L, err); lua_pushstring(L, udp_strerror(err));
return 2; return 2;
}
lua_pushlstring(L, buffer, got);
return 1;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -201,7 +201,7 @@ static int meth_receivefrom(lua_State *L) {
return 3; return 3;
} else { } else {
lua_pushnil(L); lua_pushnil(L);
pusherror(L, err); lua_pushstring(L, udp_strerror(err));
return 2; return 2;
} }
} }

View File

@ -13,6 +13,63 @@
#include "socket.h" #include "socket.h"
/*-------------------------------------------------------------------------*\
* Wait for readable/writable/connected socket with timeout
\*-------------------------------------------------------------------------*/
#ifdef SOCK_POLL
#include <sys/poll.h>
#define WAITFD_R POLLIN
#define WAITFD_W POLLOUT
#define WAITFD_C (POLLIN|POLLOUT)
static int sock_waitfd(int fd, int sw, p_tm tm) {
int ret;
struct pollfd pfd;
pfd.fd = fd;
pfd.events = sw;
pfd.revents = 0;
if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
do ret = poll(&pfd, 1, (int)(tm_getretry(tm)*1e3));
while (ret == -1 && errno == EINTR);
if (ret == -1) return errno;
if (ret == 0) return IO_TIMEOUT;
if (sw == WAITFD_C && (pfd.revents & (POLLIN|POLLERR))) return IO_CLOSED;
return IO_DONE;
}
#else
#define WAITFD_R 1
#define WAITFD_W 2
#define WAITFD_C (WAITFD_R|WAITFD_W)
static int sock_waitfd(int fd, int sw, p_tm tm) {
int ret;
fd_set rfds, wfds, *rp, *wp;
struct timeval tv, *tp;
double t;
if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
do {
/* must set bits within loop, because select may have modifed them */
rp = wp = NULL;
if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(fd, &rfds); rp = &rfds; }
if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(fd, &wfds); wp = &wfds; }
t = tm_getretry(tm);
tp = NULL;
if (t >= 0.0) {
tv.tv_sec = (int)t;
tv.tv_usec = (int)((t-tv.tv_sec)*1.0e6);
tp = &tv;
}
ret = select(fd+1, rp, wp, NULL, tp);
} while (ret == -1 && errno == EINTR);
if (ret == -1) return errno;
if (ret == 0) return IO_TIMEOUT;
if (sw == WAITFD_C && FD_ISSET(fd, &rfds)) return IO_CLOSED;
return IO_DONE;
}
#endif
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Initializes module * Initializes module
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -58,59 +115,19 @@ int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) {
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Creates and sets up a socket * Creates and sets up a socket
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *sock_create(p_sock ps, int domain, int type, int protocol) { int sock_create(p_sock ps, int domain, int type, int protocol) {
t_sock sock = socket(domain, type, protocol); *ps = socket(domain, type, protocol);
if (sock == SOCK_INVALID) return sock_strerror(); if (*ps != SOCK_INVALID) return IO_DONE;
*ps = sock; else return errno;
return NULL;
}
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm) {
t_sock sock = *ps;
int err;
/* don't call on closed socket */
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
/* ask system to connect */
do err = connect(sock, addr, addr_len);
while (err < 0 && errno == EINTR);
/* if no error, we're done */
if (err == 0) return NULL;
/* make sure the system is trying to connect */
if (errno != EINPROGRESS) return sock_strerror();
/* optimize for timeout = 0 */
if (tm_get(tm) == 0.0) return io_strerror(IO_TIMEOUT);
/* wait for a timeout or for the system's answer */
for ( ;; ) {
fd_set rfds, wfds;
FD_ZERO(&rfds); FD_SET(sock, &rfds);
FD_ZERO(&wfds); FD_SET(sock, &wfds);
/* we run select to avoid busy waiting */
err = sock_select(sock+1, &rfds, &wfds, NULL, tm);
/* if there was an event, check what happened */
if (err > 0) {
char dummy;
/* recv will set errno to the value a blocking connect would set */
if (err > 1 && FD_ISSET(sock, &rfds) &&
recv(sock, &dummy, 0, 0) < 0 && errno != EAGAIN)
return sock_strerror();
else
return NULL;
/* if no event happened, there was a timeout */
} else if (err == 0) return io_strerror(IO_TIMEOUT);
}
return sock_strerror();
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Binds or returns error message * Binds or returns error message
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) { int sock_bind(p_sock ps, SA *addr, socklen_t len) {
const char *err = NULL; int err = IO_DONE;
sock_setblocking(ps); sock_setblocking(ps);
if (bind(*ps, addr, addr_len) < 0) err = sock_strerror(); if (bind(*ps, addr, len) < 0) err = errno;
sock_setnonblocking(ps); sock_setnonblocking(ps);
return err; return err;
} }
@ -118,10 +135,10 @@ const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) {
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* *
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char* sock_listen(p_sock ps, int backlog) { int sock_listen(p_sock ps, int backlog) {
const char *err = NULL; int err = IO_DONE;
sock_setblocking(ps); sock_setblocking(ps);
if (listen(*ps, backlog)) err = sock_strerror(); if (listen(*ps, backlog)) err = errno;
sock_setnonblocking(ps); sock_setnonblocking(ps);
return err; return err;
} }
@ -135,38 +152,46 @@ void sock_shutdown(p_sock ps, int how) {
sock_setnonblocking(ps); sock_setnonblocking(ps);
} }
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
int err;
/* avoid calling on closed sockets */
if (*ps == SOCK_INVALID) return IO_CLOSED;
/* call connect until done or failed without being interrupted */
do if (connect(*ps, addr, len) == 0) return IO_DONE;
while ((err = errno) == EINTR);
/* if connection failed immediately, return error code */
if (err != EINPROGRESS && err != EAGAIN) return err;
/* wait until we have the result of the connection attempt or timeout */
if ((err = sock_waitfd(*ps, WAITFD_C, tm)) == IO_CLOSED) {
/* finaly find out if we succeeded connecting */
if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE;
else return errno;
/* timed out or some weirder error */
} else return err;
}
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Accept with timeout * Accept with timeout
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *sock_accept(p_sock ps, p_sock pa, SA *addr, int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) {
socklen_t *addr_len, p_tm tm) { SA daddr;
t_sock sock = *ps; socklen_t dlen = sizeof(daddr);
SA dummy_addr; int err;
socklen_t dummy_len = sizeof(dummy_addr); if (*ps == SOCK_INVALID) return IO_CLOSED;
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED); if (!addr) addr = &daddr;
if (!addr) addr = &dummy_addr; if (!len) len = &dlen;
if (!addr_len) addr_len = &dummy_len; for ( ;; ) {
for (;;) { if ((*pa = accept(*ps, addr, len)) != SOCK_INVALID) return IO_DONE;
int err; err = errno;
fd_set fds; if (err == EINTR) continue;
/* try to accept */ if (err != EAGAIN && err != ECONNABORTED) return err;
do *pa = accept(sock, addr, addr_len); if ((err = sock_waitfd(*ps, WAITFD_R, tm)) != IO_DONE) return err;
while (*pa < 0 && errno == EINTR);
/* if result is valid, we are done */
if (*pa != SOCK_INVALID) return NULL;
/* find out if we failed for a fatal reason */
/* if connection was aborted, we can try again if we have time */
if (errno != EAGAIN && errno != ECONNABORTED) return sock_strerror();
/* optimize for timeout = 0 case */
if (tm_get(tm) == 0.0) return io_strerror(IO_TIMEOUT);
/* call select to avoid busy-wait. */
FD_ZERO(&fds);
FD_SET(sock, &fds);
err = sock_select(sock+1, &fds, NULL, NULL, tm);
if (err == 0) return io_strerror(IO_TIMEOUT);
else if (err < 0) break;
} }
return sock_strerror(); /* can't reach here */
return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -174,132 +199,100 @@ const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm)
{ {
t_sock sock = *ps; int err;
/* avoid making system calls on closed sockets */ /* avoid making system calls on closed sockets */
if (sock == SOCK_INVALID) return IO_CLOSED; if (*ps == SOCK_INVALID) return IO_CLOSED;
/* loop until we send something or we give up on error */ /* loop until we send something or we give up on error */
*sent = 0;
for ( ;; ) { for ( ;; ) {
int ret; ssize_t put = send(*ps, data, count, 0);
fd_set fds; /* if we sent anything, we are done */
ssize_t put;
/* make sure we repeat in case the call was interrupted */
do put = send(sock, data, count, 0);
while (put < 0 && errno == EINTR);
/* if we sent something, get out */
if (put > 0) { if (put > 0) {
*sent = put; *sent = put;
return IO_DONE; return IO_DONE;
} }
/* deal with failure */ err = errno;
*sent = 0; /* send can't really return 0, but EPIPE means the connection was
/* here we know the connection has been closed */ closed */
if (put < 0 && errno == EPIPE) return IO_CLOSED; if (put == 0 || err == EPIPE) return IO_CLOSED;
/* send shouldn't return zero and we can only proceed if /* we call was interrupted, just try again */
* there was no serious error */ if (err == EINTR) continue;
if (put == 0 || errno != EAGAIN) return IO_USER; /* if failed fatal reason, report error */
/* optimize for the timeout = 0 case */ if (err != EAGAIN) return err;
if (tm_get(tm) == 0.0) return IO_TIMEOUT; /* wait until we can send something or we timeout */
/* run select to avoid busy wait */ if ((err = sock_waitfd(*ps, WAITFD_W, tm)) != IO_DONE) return err;
FD_ZERO(&fds);
FD_SET(sock, &fds);
ret = sock_select(sock+1, NULL, &fds, NULL, tm);
if (ret == 0) return IO_TIMEOUT;
else if (ret < 0) break;
/* otherwise, try sending again */
} }
return IO_USER; /* can't reach here */
return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Sendto with timeout * Sendto with timeout
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
SA *addr, socklen_t addr_len, p_tm tm) SA *addr, socklen_t len, p_tm tm)
{ {
t_sock sock = *ps; int err;
/* avoid making system calls on closed sockets */ if (*ps == SOCK_INVALID) return IO_CLOSED;
if (sock == SOCK_INVALID) return IO_CLOSED; *sent = 0;
/* loop until we send something or we give up on error */
for ( ;; ) { for ( ;; ) {
int ret; ssize_t put = sendto(*ps, data, count, 0, addr, len);
fd_set fds;
ssize_t put;
do put = sendto(sock, data, count, 0, addr, addr_len);
while (put < 0 && errno == EINTR);
if (put > 0) { if (put > 0) {
*sent = put; *sent = put;
return IO_DONE; return IO_DONE;
} }
*sent = 0; err = errno;
if (put < 0 && errno == EPIPE) return IO_CLOSED; if (put == 0 || err == EPIPE) return IO_CLOSED;
if (put == 0 || errno != EAGAIN) return IO_USER; if (err == EINTR) continue;
if (tm_get(tm) == 0.0) return IO_TIMEOUT; if (err != EAGAIN) return err;
FD_ZERO(&fds); if ((err = sock_waitfd(*ps, WAITFD_W, tm)) != IO_DONE) return err;
FD_SET(sock, &fds);
ret = sock_select(sock+1, NULL, &fds, NULL, tm);
if (ret == 0) return IO_TIMEOUT;
else if (ret < 0) break;
} }
return IO_USER; return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Receive with timeout * Receive with timeout
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) {
t_sock sock = *ps; int err;
if (sock == SOCK_INVALID) return IO_CLOSED; if (*ps == SOCK_INVALID) return IO_CLOSED;
for ( ;; ) { for ( ;; ) {
fd_set fds; ssize_t taken = recv(*ps, data, count, 0);
int ret;
ssize_t taken;
do taken = read(sock, data, count);
while (taken < 0 && errno == EINTR);
if (taken > 0) { if (taken > 0) {
*got = taken; *got = taken;
return IO_DONE; return IO_DONE;
} }
err = errno;
*got = 0; *got = 0;
if (taken == 0) return IO_CLOSED; if (taken == 0) return IO_CLOSED;
if (errno != EAGAIN) return IO_USER; if (err == EINTR) continue;
if (tm_get(tm) == 0.0) return IO_TIMEOUT; if (err != EAGAIN) return err;
FD_ZERO(&fds); if ((err = sock_waitfd(*ps, WAITFD_R, tm)) != IO_DONE) return err;
FD_SET(sock, &fds);
ret = sock_select(sock+1, &fds, NULL, NULL, tm);
if (ret == 0) return IO_TIMEOUT;
else if (ret < 0) break;
} }
return IO_USER; return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Recvfrom with timeout * Recvfrom with timeout
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
SA *addr, socklen_t *addr_len, p_tm tm) { SA *addr, socklen_t *len, p_tm tm) {
t_sock sock = *ps; int err;
if (sock == SOCK_INVALID) return IO_CLOSED; if (*ps == SOCK_INVALID) return IO_CLOSED;
for ( ;; ) { for ( ;; ) {
fd_set fds; ssize_t taken = recvfrom(*ps, data, count, 0, addr, len);
int ret;
ssize_t taken;
do taken = recvfrom(sock, data, count, 0, addr, addr_len);
while (taken < 0 && errno == EINTR);
if (taken > 0) { if (taken > 0) {
*got = taken; *got = taken;
return IO_DONE; return IO_DONE;
} }
err = errno;
*got = 0; *got = 0;
if (taken == 0) return IO_CLOSED; if (taken == 0) return IO_CLOSED;
if (errno != EAGAIN) return IO_USER; if (err == EINTR) continue;
if (tm_get(tm) == 0.0) return IO_TIMEOUT; if (err != EAGAIN) return err;
FD_ZERO(&fds); if ((err = sock_waitfd(*ps, WAITFD_R, tm)) != IO_DONE) return err;
FD_SET(sock, &fds);
ret = sock_select(sock+1, &fds, NULL, NULL, tm);
if (ret == 0) return IO_TIMEOUT;
else if (ret < 0) break;
} }
return IO_USER; return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -321,29 +314,46 @@ void sock_setnonblocking(p_sock ps) {
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Error translation functions * DNS helpers
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *sock_hoststrerror(void) { int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
switch (h_errno) { *hp = gethostbyaddr(addr, len, AF_INET);
case HOST_NOT_FOUND: if (*hp) return IO_DONE;
return "host not found"; else return h_errno;
default: }
return hstrerror(h_errno);
int sock_gethostbyname(const char *addr, struct hostent **hp) {
*hp = gethostbyname(addr);
if (*hp) return IO_DONE;
else return h_errno;
}
/*-------------------------------------------------------------------------*\
* Error translation functions
* Make sure important error messages are standard
\*-------------------------------------------------------------------------*/
const char *sock_hoststrerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
case HOST_NOT_FOUND: return "host_not_found";
default: return hstrerror(err);
} }
} }
/* make sure important error messages are standard */ const char *sock_strerror(int err) {
const char *sock_strerror(void) { if (err <= 0) return io_strerror(err);
switch (errno) { switch (err) {
case EADDRINUSE: case EADDRINUSE: return "eaddrinuse";
return "address already in use"; case EACCES: return "eaccess";
default: case ECONNABORTED: return "econnaborted";
return strerror(errno); case ECONNREFUSED: return "econnrefused";
case ECONNRESET: return "econnreset";
case ETIMEDOUT: return "etimedout";
default: return strerror(errno);
} }
} }
const char *sock_geterr(p_sock ps, int code) { const char *sock_ioerror(p_sock ps, int err) {
(void) ps; (void) ps;
(void) code; return sock_strerror(err);
return sock_strerror();
} }

View File

@ -19,7 +19,7 @@ http.TIMEOUT = 10
local t = socket.gettime() local t = socket.gettime()
host = host or "diego.student.dyn.cs.princeton.edu" host = host or "diego.princeton.edu"
proxy = proxy or "http://localhost:3128" proxy = proxy or "http://localhost:3128"
prefix = prefix or "/luasocket-test" prefix = prefix or "/luasocket-test"
cgiprefix = cgiprefix or "/luasocket-test-cgi" cgiprefix = cgiprefix or "/luasocket-test-cgi"