mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-25 12:08:21 +01:00
Still need to fix windows. :o/
This commit is contained in:
parent
471334c3d0
commit
9a79d500eb
3
TODO
3
TODO
@ -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
|
||||
|
||||
optmize aux_getgroupudata (Mike idea)
|
||||
|
22
src/buffer.c
22
src/buffer.c
@ -77,7 +77,7 @@ int buf_meth_send(lua_State *L, p_buf buf) {
|
||||
/* check if there was an error */
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
io_pusherror(L, buf->io, err);
|
||||
lua_pushstring(L, buf->io->error(buf->io->ctx, err));
|
||||
lua_pushnumber(L, total);
|
||||
} else {
|
||||
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);
|
||||
p_tm tm = tm_markstart(buf->tm);
|
||||
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);
|
||||
/* receive all patterns */
|
||||
luaL_addlstring(&b, part, size);
|
||||
/* receive new patterns */
|
||||
if (!lua_isnumber(L, 2)) {
|
||||
static const char *patternnames[] = {"*l", "*a", NULL};
|
||||
const char *pattern = luaL_optstring(L, 2, "*l");
|
||||
/* get next pattern */
|
||||
int p = luaL_findstring(pattern, patternnames);
|
||||
if (p == 0) err = recvline(buf, &b);
|
||||
else if (p == 1) err = recvall(buf, &b);
|
||||
const char *p= luaL_optstring(L, 2, "*l");
|
||||
if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b);
|
||||
else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b);
|
||||
else luaL_argcheck(L, 0, 2, "invalid receive pattern");
|
||||
/* get a fixed number of bytes */
|
||||
} else err = recvraw(buf, (size_t) lua_tonumber(L, 2), &b);
|
||||
/* check if there was an error */
|
||||
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);
|
||||
io_pusherror(L, buf->io, err);
|
||||
lua_pushstring(L, buf->io->error(buf->io->ctx, err));
|
||||
lua_pushvalue(L, -2);
|
||||
lua_pushnil(L);
|
||||
lua_replace(L, -4);
|
||||
|
70
src/inet.c
70
src/inet.c
@ -50,22 +50,28 @@ int inet_open(lua_State *L)
|
||||
* Returns all information provided by the resolver given a host name
|
||||
* or ip address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_global_toip(lua_State *L)
|
||||
{
|
||||
const char *address = luaL_checkstring(L, 1);
|
||||
static int inet_gethost(const char *address, struct hostent **hp) {
|
||||
struct in_addr addr;
|
||||
struct hostent *hp;
|
||||
if (inet_aton(address, &addr))
|
||||
hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
|
||||
return sock_gethostbyaddr((char *) &addr, sizeof(addr), hp);
|
||||
else
|
||||
hp = gethostbyname(address);
|
||||
if (!hp) {
|
||||
return sock_gethostbyname(address, 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_pushstring(L, sock_hoststrerror());
|
||||
lua_pushstring(L, sock_hoststrerror(err));
|
||||
return 2;
|
||||
}
|
||||
addr = *((struct in_addr *) hp->h_addr);
|
||||
lua_pushstring(L, inet_ntoa(addr));
|
||||
lua_pushstring(L, hp->h_name);
|
||||
inet_pushresolved(L, hp);
|
||||
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
|
||||
* 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);
|
||||
struct in_addr addr;
|
||||
struct hostent *hp;
|
||||
if (inet_aton(address, &addr))
|
||||
hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
|
||||
else
|
||||
hp = gethostbyname(address);
|
||||
if (!hp) {
|
||||
struct hostent *hp = NULL;
|
||||
int err = inet_gethost(address, &hp);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, sock_hoststrerror());
|
||||
lua_pushstring(L, sock_hoststrerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushstring(L, hp->h_name);
|
||||
lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr)));
|
||||
inet_pushresolved(L, hp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* 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
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_trycreate(p_sock ps, int type)
|
||||
{
|
||||
return sock_create(ps, AF_INET, type, 0);
|
||||
const char *inet_trycreate(p_sock ps, int type) {
|
||||
return sock_strerror(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)
|
||||
{
|
||||
struct sockaddr_in remote;
|
||||
const char *err;
|
||||
int err;
|
||||
memset(&remote, 0, sizeof(remote));
|
||||
remote.sin_family = AF_INET;
|
||||
remote.sin_port = htons(port);
|
||||
if (strcmp(address, "*")) {
|
||||
if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) {
|
||||
struct hostent *hp = gethostbyname(address);
|
||||
struct hostent *hp = NULL;
|
||||
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;
|
||||
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
|
||||
}
|
||||
} else remote.sin_family = AF_UNSPEC;
|
||||
err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm);
|
||||
if (err) sock_destroy(ps);
|
||||
return err;
|
||||
if (err != IO_DONE) sock_destroy(ps);
|
||||
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)
|
||||
{
|
||||
struct sockaddr_in local;
|
||||
const char *err;
|
||||
int err;
|
||||
memset(&local, 0, sizeof(local));
|
||||
/* address is either wildcard or a valid ip address */
|
||||
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;
|
||||
if (strcmp(address, "*") &&
|
||||
(!strlen(address) || !inet_aton(address, &local.sin_addr))) {
|
||||
struct hostent *hp = gethostbyname(address);
|
||||
struct hostent *hp = NULL;
|
||||
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;
|
||||
memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
|
||||
}
|
||||
err = sock_bind(ps, (SA *) &local, sizeof(local));
|
||||
if (err) sock_destroy(ps);
|
||||
return err;
|
||||
if (err != IO_DONE) sock_destroy(ps);
|
||||
return sock_strerror(err);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
|
24
src/io.c
24
src/io.c
@ -12,34 +12,22 @@
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* 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->recv = recv;
|
||||
io->geterr = geterr;
|
||||
io->error = error;
|
||||
io->ctx = ctx;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Translate error codes to Lua
|
||||
* I/O error strings
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *io_strerror(int code) {
|
||||
switch (code) {
|
||||
const char *io_strerror(int err) {
|
||||
switch (err) {
|
||||
case IO_DONE: return NULL;
|
||||
case IO_CLOSED: return "closed";
|
||||
case IO_TIMEOUT: return "timeout";
|
||||
case IO_CLIPPED: return "clipped";
|
||||
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);
|
||||
}
|
||||
|
28
src/io.h
28
src/io.h
@ -21,13 +21,18 @@
|
||||
|
||||
/* IO error codes */
|
||||
enum {
|
||||
IO_DONE, /* operation completed successfully */
|
||||
IO_TIMEOUT, /* operation timed out */
|
||||
IO_CLOSED, /* the connection has been closed */
|
||||
IO_CLIPPED, /* maxium bytes count reached */
|
||||
IO_USER /* last element in enum is user custom error */
|
||||
IO_DONE = 0, /* operation completed successfully */
|
||||
IO_TIMEOUT = -1, /* operation timed out */
|
||||
IO_CLOSED = -2, /* the connection has been closed */
|
||||
IO_CLIPPED = -3 /* maxium bytes count reached */
|
||||
};
|
||||
|
||||
/* interface to error message function */
|
||||
typedef const char *(*p_error) (
|
||||
void *ctx, /* context needed by send */
|
||||
int err /* error code */
|
||||
);
|
||||
|
||||
/* interface to send function */
|
||||
typedef int (*p_send) (
|
||||
void *ctx, /* context needed by send */
|
||||
@ -37,12 +42,6 @@ typedef int (*p_send) (
|
||||
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 */
|
||||
typedef int (*p_recv) (
|
||||
void *ctx, /* context needed by recv */
|
||||
@ -57,12 +56,11 @@ typedef struct t_io_ {
|
||||
void *ctx; /* context needed by send/recv */
|
||||
p_send send; /* send function pointer */
|
||||
p_recv recv; /* receive function pointer */
|
||||
p_geterr geterr; /* receive function pointer */
|
||||
p_error error; /* strerror function */
|
||||
} t_io;
|
||||
typedef t_io *p_io;
|
||||
|
||||
const char *io_strerror(int code);
|
||||
void io_pusherror(lua_State *L, p_io io, int code);
|
||||
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);
|
||||
const char *io_strerror(int err);
|
||||
|
||||
#endif /* IO_H */
|
||||
|
27
src/socket.h
27
src/socket.h
@ -41,8 +41,6 @@ int sock_open(void);
|
||||
int sock_close(void);
|
||||
void sock_destroy(p_sock ps);
|
||||
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,
|
||||
size_t *sent, SA *addr, socklen_t addr_len, p_tm tm);
|
||||
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);
|
||||
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);
|
||||
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_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);
|
||||
int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm);
|
||||
int sock_create(p_sock ps, int domain, int type, int protocol);
|
||||
int sock_bind(p_sock ps, SA *addr, socklen_t addr_len);
|
||||
int sock_listen(p_sock ps, int backlog);
|
||||
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm);
|
||||
|
||||
const char *sock_geterr(p_sock ps, int code);
|
||||
const char *sock_hoststrerror(void);
|
||||
const char *sock_strerror(void);
|
||||
const char *sock_hoststrerror(int err);
|
||||
const char *sock_strerror(int err);
|
||||
|
||||
/* 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 */
|
||||
|
16
src/tcp.c
16
src/tcp.c
@ -160,22 +160,22 @@ static int meth_accept(lua_State *L)
|
||||
p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1);
|
||||
p_tm tm = tm_markstart(&server->tm);
|
||||
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 (!err) {
|
||||
if (err == IO_DONE) {
|
||||
p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
aux_setclass(L, "tcp{client}", -1);
|
||||
/* initialize structure fields */
|
||||
sock_setnonblocking(&sock);
|
||||
clnt->sock = sock;
|
||||
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);
|
||||
buf_init(&clnt->buf, &clnt->io, &clnt->tm);
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
lua_pushstring(L, sock_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@ -236,10 +236,10 @@ static int meth_listen(lua_State *L)
|
||||
{
|
||||
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1);
|
||||
int backlog = (int) luaL_optnumber(L, 2, 32);
|
||||
const char *err = sock_listen(&tcp->sock, backlog);
|
||||
if (err) {
|
||||
int err = sock_listen(&tcp->sock, backlog);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
lua_pushstring(L, sock_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
/* turn master object into a server object */
|
||||
@ -320,7 +320,7 @@ static int global_create(lua_State *L)
|
||||
sock_setnonblocking(&sock);
|
||||
tcp->sock = sock;
|
||||
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);
|
||||
buf_init(&tcp->buf, &tcp->io, &tcp->tm);
|
||||
return 1;
|
||||
|
@ -25,4 +25,6 @@ double tm_getstart(p_tm tm);
|
||||
double tm_gettime(void);
|
||||
int tm_meth_settimeout(lua_State *L, p_tm tm);
|
||||
|
||||
#define tm_iszero(tm) ((tm)->block == 0.0)
|
||||
|
||||
#endif /* TM_H */
|
||||
|
54
src/udp.c
54
src/udp.c
@ -41,7 +41,6 @@ static int meth_settimeout(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 void pusherror(lua_State *L, int code);
|
||||
|
||||
/* udp object methods */
|
||||
static luaL_reg udp[] = {
|
||||
@ -100,18 +99,14 @@ int udp_open(lua_State *L)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*=========================================================================*\
|
||||
* Lua methods
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Pushes the error message
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void pusherror(lua_State *L, int code) {
|
||||
const char *err = code != IO_USER? io_strerror(code): "refused";
|
||||
err = err? err: sock_strerror();
|
||||
if (err) lua_pushstring(L, err);
|
||||
else lua_pushnil(L);
|
||||
const char *udp_strerror(int err) {
|
||||
/* a 'closed' error on an unconnected means the target address was not
|
||||
* accepted by the transport layer */
|
||||
if (err == IO_CLOSED) return "refused";
|
||||
else return sock_strerror(err);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -125,12 +120,13 @@ static int meth_send(lua_State *L) {
|
||||
const char *data = luaL_checklstring(L, 2, &count);
|
||||
tm_markstart(tm);
|
||||
err = sock_send(&udp->sock, data, count, &sent, tm);
|
||||
if (err == IO_DONE) lua_pushnumber(L, sent);
|
||||
else lua_pushnil(L);
|
||||
/* a 'closed' error on an unconnected means the target address was not
|
||||
* accepted by the transport layer */
|
||||
pusherror(L, err == IO_CLOSED ? IO_USER : err);
|
||||
return 2;
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, sent);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -153,12 +149,13 @@ static int meth_sendto(lua_State *L) {
|
||||
tm_markstart(tm);
|
||||
err = sock_sendto(&udp->sock, data, count, &sent,
|
||||
(SA *) &addr, sizeof(addr), tm);
|
||||
if (err == IO_DONE) lua_pushnumber(L, sent);
|
||||
else lua_pushnil(L);
|
||||
/* a 'closed' error on an unconnected means the target address was not
|
||||
* accepted by the transport layer */
|
||||
pusherror(L, err == IO_CLOSED ? IO_USER : err);
|
||||
return 2;
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(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));
|
||||
tm_markstart(tm);
|
||||
err = sock_recv(&udp->sock, buffer, count, &got, tm);
|
||||
if (err == IO_DONE) lua_pushlstring(L, buffer, got);
|
||||
else lua_pushnil(L);
|
||||
pusherror(L, err);
|
||||
return 2;
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushlstring(L, buffer, got);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -201,7 +201,7 @@ static int meth_receivefrom(lua_State *L) {
|
||||
return 3;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
pusherror(L, err);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
370
src/usocket.c
370
src/usocket.c
@ -13,6 +13,63 @@
|
||||
|
||||
#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
|
||||
\*-------------------------------------------------------------------------*/
|
||||
@ -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
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *sock_create(p_sock ps, int domain, int type, int protocol) {
|
||||
t_sock sock = socket(domain, type, protocol);
|
||||
if (sock == SOCK_INVALID) return sock_strerror();
|
||||
*ps = sock;
|
||||
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();
|
||||
int sock_create(p_sock ps, int domain, int type, int protocol) {
|
||||
*ps = socket(domain, type, protocol);
|
||||
if (*ps != SOCK_INVALID) return IO_DONE;
|
||||
else return errno;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Binds or returns error message
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) {
|
||||
const char *err = NULL;
|
||||
int sock_bind(p_sock ps, SA *addr, socklen_t len) {
|
||||
int err = IO_DONE;
|
||||
sock_setblocking(ps);
|
||||
if (bind(*ps, addr, addr_len) < 0) err = sock_strerror();
|
||||
if (bind(*ps, addr, len) < 0) err = errno;
|
||||
sock_setnonblocking(ps);
|
||||
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) {
|
||||
const char *err = NULL;
|
||||
int sock_listen(p_sock ps, int backlog) {
|
||||
int err = IO_DONE;
|
||||
sock_setblocking(ps);
|
||||
if (listen(*ps, backlog)) err = sock_strerror();
|
||||
if (listen(*ps, backlog)) err = errno;
|
||||
sock_setnonblocking(ps);
|
||||
return err;
|
||||
}
|
||||
@ -135,38 +152,46 @@ void sock_shutdown(p_sock ps, int how) {
|
||||
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
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
|
||||
socklen_t *addr_len, p_tm tm) {
|
||||
t_sock sock = *ps;
|
||||
SA dummy_addr;
|
||||
socklen_t dummy_len = sizeof(dummy_addr);
|
||||
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
|
||||
if (!addr) addr = &dummy_addr;
|
||||
if (!addr_len) addr_len = &dummy_len;
|
||||
for (;;) {
|
||||
int err;
|
||||
fd_set fds;
|
||||
/* try to accept */
|
||||
do *pa = accept(sock, addr, addr_len);
|
||||
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();
|
||||
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) {
|
||||
SA daddr;
|
||||
socklen_t dlen = sizeof(daddr);
|
||||
int err;
|
||||
if (*ps == SOCK_INVALID) return IO_CLOSED;
|
||||
if (!addr) addr = &daddr;
|
||||
if (!len) len = &dlen;
|
||||
for ( ;; ) {
|
||||
if ((*pa = accept(*ps, addr, len)) != SOCK_INVALID) return IO_DONE;
|
||||
err = errno;
|
||||
if (err == EINTR) continue;
|
||||
if (err != EAGAIN && err != ECONNABORTED) return err;
|
||||
if ((err = sock_waitfd(*ps, WAITFD_R, tm)) != IO_DONE) return err;
|
||||
}
|
||||
/* 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)
|
||||
{
|
||||
t_sock sock = *ps;
|
||||
int err;
|
||||
/* 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 */
|
||||
*sent = 0;
|
||||
for ( ;; ) {
|
||||
int ret;
|
||||
fd_set fds;
|
||||
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) {
|
||||
ssize_t put = send(*ps, data, count, 0);
|
||||
/* if we sent anything, we are done */
|
||||
if (put > 0) {
|
||||
*sent = put;
|
||||
return IO_DONE;
|
||||
}
|
||||
/* deal with failure */
|
||||
*sent = 0;
|
||||
/* here we know the connection has been closed */
|
||||
if (put < 0 && errno == EPIPE) return IO_CLOSED;
|
||||
/* send shouldn't return zero and we can only proceed if
|
||||
* there was no serious error */
|
||||
if (put == 0 || errno != EAGAIN) return IO_USER;
|
||||
/* optimize for the timeout = 0 case */
|
||||
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
||||
/* run select to avoid busy wait */
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(sock, &fds);
|
||||
ret = sock_select(sock+1, NULL, &fds, NULL, tm);
|
||||
if (ret == 0) return IO_TIMEOUT;
|
||||
else if (ret < 0) break;
|
||||
/* otherwise, try sending again */
|
||||
}
|
||||
return IO_USER;
|
||||
err = errno;
|
||||
/* send can't really return 0, but EPIPE means the connection was
|
||||
closed */
|
||||
if (put == 0 || err == EPIPE) return IO_CLOSED;
|
||||
/* we call was interrupted, just try again */
|
||||
if (err == EINTR) continue;
|
||||
/* if failed fatal reason, report error */
|
||||
if (err != EAGAIN) return err;
|
||||
/* wait until we can send something or we timeout */
|
||||
if ((err = sock_waitfd(*ps, WAITFD_W, tm)) != IO_DONE) return err;
|
||||
}
|
||||
/* can't reach here */
|
||||
return err;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Sendto with timeout
|
||||
\*-------------------------------------------------------------------------*/
|
||||
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;
|
||||
/* avoid making system calls on closed sockets */
|
||||
if (sock == SOCK_INVALID) return IO_CLOSED;
|
||||
/* loop until we send something or we give up on error */
|
||||
int err;
|
||||
if (*ps == SOCK_INVALID) return IO_CLOSED;
|
||||
*sent = 0;
|
||||
for ( ;; ) {
|
||||
int ret;
|
||||
fd_set fds;
|
||||
ssize_t put;
|
||||
do put = sendto(sock, data, count, 0, addr, addr_len);
|
||||
while (put < 0 && errno == EINTR);
|
||||
if (put > 0) {
|
||||
ssize_t put = sendto(*ps, data, count, 0, addr, len);
|
||||
if (put > 0) {
|
||||
*sent = put;
|
||||
return IO_DONE;
|
||||
}
|
||||
*sent = 0;
|
||||
if (put < 0 && errno == EPIPE) return IO_CLOSED;
|
||||
if (put == 0 || errno != EAGAIN) return IO_USER;
|
||||
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
||||
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;
|
||||
}
|
||||
return IO_USER;
|
||||
err = errno;
|
||||
if (put == 0 || err == EPIPE) return IO_CLOSED;
|
||||
if (err == EINTR) continue;
|
||||
if (err != EAGAIN) return err;
|
||||
if ((err = sock_waitfd(*ps, WAITFD_W, tm)) != IO_DONE) return err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Receive with timeout
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) {
|
||||
t_sock sock = *ps;
|
||||
if (sock == SOCK_INVALID) return IO_CLOSED;
|
||||
int err;
|
||||
if (*ps == SOCK_INVALID) return IO_CLOSED;
|
||||
for ( ;; ) {
|
||||
fd_set fds;
|
||||
int ret;
|
||||
ssize_t taken;
|
||||
do taken = read(sock, data, count);
|
||||
while (taken < 0 && errno == EINTR);
|
||||
ssize_t taken = recv(*ps, data, count, 0);
|
||||
if (taken > 0) {
|
||||
*got = taken;
|
||||
return IO_DONE;
|
||||
}
|
||||
err = errno;
|
||||
*got = 0;
|
||||
if (taken == 0) return IO_CLOSED;
|
||||
if (errno != EAGAIN) return IO_USER;
|
||||
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
||||
FD_ZERO(&fds);
|
||||
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;
|
||||
if (err == EINTR) continue;
|
||||
if (err != EAGAIN) return err;
|
||||
if ((err = sock_waitfd(*ps, WAITFD_R, tm)) != IO_DONE) return err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Recvfrom with timeout
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
|
||||
SA *addr, socklen_t *addr_len, p_tm tm) {
|
||||
t_sock sock = *ps;
|
||||
if (sock == SOCK_INVALID) return IO_CLOSED;
|
||||
SA *addr, socklen_t *len, p_tm tm) {
|
||||
int err;
|
||||
if (*ps == SOCK_INVALID) return IO_CLOSED;
|
||||
for ( ;; ) {
|
||||
fd_set fds;
|
||||
int ret;
|
||||
ssize_t taken;
|
||||
do taken = recvfrom(sock, data, count, 0, addr, addr_len);
|
||||
while (taken < 0 && errno == EINTR);
|
||||
ssize_t taken = recvfrom(*ps, data, count, 0, addr, len);
|
||||
if (taken > 0) {
|
||||
*got = taken;
|
||||
return IO_DONE;
|
||||
}
|
||||
err = errno;
|
||||
*got = 0;
|
||||
if (taken == 0) return IO_CLOSED;
|
||||
if (errno != EAGAIN) return IO_USER;
|
||||
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
||||
FD_ZERO(&fds);
|
||||
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;
|
||||
if (err == EINTR) continue;
|
||||
if (err != EAGAIN) return err;
|
||||
if ((err = sock_waitfd(*ps, WAITFD_R, tm)) != IO_DONE) return err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -321,29 +314,46 @@ void sock_setnonblocking(p_sock ps) {
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Error translation functions
|
||||
* DNS helpers
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *sock_hoststrerror(void) {
|
||||
switch (h_errno) {
|
||||
case HOST_NOT_FOUND:
|
||||
return "host not found";
|
||||
default:
|
||||
return hstrerror(h_errno);
|
||||
int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
|
||||
*hp = gethostbyaddr(addr, len, AF_INET);
|
||||
if (*hp) return IO_DONE;
|
||||
else return 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(void) {
|
||||
switch (errno) {
|
||||
case EADDRINUSE:
|
||||
return "address already in use";
|
||||
default:
|
||||
return strerror(errno);
|
||||
const char *sock_strerror(int err) {
|
||||
if (err <= 0) return io_strerror(err);
|
||||
switch (err) {
|
||||
case EADDRINUSE: return "eaddrinuse";
|
||||
case EACCES: return "eaccess";
|
||||
case ECONNABORTED: return "econnaborted";
|
||||
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) code;
|
||||
return sock_strerror();
|
||||
}
|
||||
return sock_strerror(err);
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ http.TIMEOUT = 10
|
||||
|
||||
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"
|
||||
prefix = prefix or "/luasocket-test"
|
||||
cgiprefix = cgiprefix or "/luasocket-test-cgi"
|
||||
|
Loading…
Reference in New Issue
Block a user