Forward server working on Mac OS X...

This commit is contained in:
Diego Nehab
2005-02-08 10:01:01 +00:00
parent 5d32848674
commit 8d4e240f6a
29 changed files with 800 additions and 101 deletions

View File

@ -123,7 +123,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) {
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);
} else err = recvraw(buf, (size_t) lua_tonumber(L, 2)-size, &b);
/* check if there was an error */
if (err != IO_DONE) {
/* we can't push anyting in the stack before pushing the

View File

@ -220,7 +220,6 @@ const char *inet_tryconnect(p_sock ps, const char *address,
}
} else remote.sin_family = AF_UNSPEC;
err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm);
if (err != IO_DONE) sock_destroy(ps);
return sock_strerror(err);
}

View File

@ -87,7 +87,7 @@ static int global_unload(lua_State *L) {
static int base_open(lua_State *L) {
if (sock_open()) {
/* export functions (and leave namespace table on top of stack) */
luaL_module(L, "socket", func, 0);
luaL_openlib(L, "socket", func, 0);
#ifdef LUASOCKET_DEBUG
lua_pushstring(L, "DEBUG");
lua_pushboolean(L, 1);
@ -108,7 +108,7 @@ static int base_open(lua_State *L) {
/*-------------------------------------------------------------------------*\
* Initializes all library modules.
\*-------------------------------------------------------------------------*/
LUASOCKET_API int luaopen_lsocket(lua_State *L) {
LUASOCKET_API int luaopen_csocket(lua_State *L) {
int i;
base_open(L);
for (i = 0; mod[i].name; i++) mod[i].func(L);

View File

@ -13,7 +13,7 @@
/*-------------------------------------------------------------------------*\
* Current luasocket version
\*-------------------------------------------------------------------------*/
#define LUASOCKET_VERSION "LuaSocket 2.0 (beta3)"
#define LUASOCKET_VERSION "LuaSocket 2.0"
#define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab"
#define LUASOCKET_AUTHORS "Diego Nehab"
@ -27,6 +27,6 @@
/*-------------------------------------------------------------------------*\
* Initializes the library.
\*-------------------------------------------------------------------------*/
LUASOCKET_API int luaopen_socket(lua_State *L);
LUASOCKET_API int luaopen_csocket(lua_State *L);
#endif /* LUASOCKET_H */

View File

@ -78,9 +78,9 @@ static UC b64unbase[256];
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
MIME_API int luaopen_lmime(lua_State *L)
MIME_API int luaopen_cmime(lua_State *L)
{
luaL_module(L, "mime", func, 0);
luaL_openlib(L, "mime", func, 0);
/* initialize lookup tables */
qpsetup(qpclass, qpunbase);
b64setup(b64unbase);

View File

@ -19,6 +19,6 @@
#define MIME_API extern
#endif
MIME_API int luaopen_mime(lua_State *L);
MIME_API int luaopen_cmime(lua_State *L);
#endif /* MIME_H */

View File

@ -8,9 +8,10 @@
-----------------------------------------------------------------------------
-- Declare module and import dependencies
-----------------------------------------------------------------------------
package.loaded.base = _G
local base = require("base")
local ltn12 = require("ltn12")
local mime = require("lmime")
local mime = require("cmime")
module("mime")
-- encode, decode and wrap algorithm tables

View File

@ -45,11 +45,15 @@ 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,
size_t *got, SA *addr, socklen_t *addr_len, p_tm tm);
void sock_setnonblocking(p_sock ps);
void sock_setblocking(p_sock ps);
int sock_waitfd(int fd, int sw, p_tm tm);
int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm);
int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm);
int sock_connected(p_sock ps, 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);

View File

@ -7,10 +7,11 @@
-----------------------------------------------------------------------------
-- Declare module and import dependencies
-----------------------------------------------------------------------------
package.loaded.base = _G
local base = require("base")
local string = require("string")
local math = require("math")
local socket = require("lsocket")
local socket = require("csocket")
module("socket")
-----------------------------------------------------------------------------

View File

@ -20,6 +20,7 @@
\*=========================================================================*/
static int global_create(lua_State *L);
static int meth_connect(lua_State *L);
static int meth_connected(lua_State *L);
static int meth_listen(lua_State *L);
static int meth_bind(lua_State *L);
static int meth_send(lua_State *L);
@ -45,6 +46,7 @@ static luaL_reg tcp[] = {
{"bind", meth_bind},
{"close", meth_close},
{"connect", meth_connect},
{"connected", meth_connected},
{"dirty", meth_dirty},
{"getfd", meth_getfd},
{"getpeername", meth_getpeername},
@ -113,12 +115,12 @@ static int meth_receive(lua_State *L) {
}
static int meth_getstats(lua_State *L) {
p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1);
return buf_meth_getstats(L, &tcp->buf);
}
static int meth_setstats(lua_State *L) {
p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1);
return buf_meth_setstats(L, &tcp->buf);
}
@ -224,6 +226,22 @@ static int meth_connect(lua_State *L)
return 1;
}
static int meth_connected(lua_State *L)
{
static t_tm tm = {-1, -1};
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1);
int err = sock_connected(&tcp->sock, &tm);
if (err != IO_DONE) {
lua_pushnil(L);
lua_pushstring(L, sock_strerror(err));
return 2;
}
/* turn master object into a client object */
aux_setclass(L, "tcp{client}", 1);
lua_pushnumber(L, 1);
return 1;
}
/*-------------------------------------------------------------------------*\
* Closes socket used by object
\*-------------------------------------------------------------------------*/

View File

@ -32,6 +32,8 @@ 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 int meth_getstats(lua_State *L);
static int meth_setstats(lua_State *L);
static const char *unix_tryconnect(p_unix un, const char *path);
static const char *unix_trybind(p_unix un, const char *path);
@ -46,6 +48,8 @@ static luaL_reg un[] = {
{"connect", meth_connect},
{"dirty", meth_dirty},
{"getfd", meth_getfd},
{"getstats", meth_getstats},
{"setstats", meth_setstats},
{"listen", meth_listen},
{"receive", meth_receive},
{"send", meth_send},
@ -75,7 +79,7 @@ static luaL_reg func[] = {
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
int unix_open(lua_State *L) {
int luaopen_socketunix(lua_State *L) {
/* create classes */
aux_newclass(L, "unix{master}", un);
aux_newclass(L, "unix{client}", un);
@ -84,11 +88,9 @@ int unix_open(lua_State *L) {
aux_add2group(L, "unix{master}", "unix{any}");
aux_add2group(L, "unix{client}", "unix{any}");
aux_add2group(L, "unix{server}", "unix{any}");
aux_add2group(L, "unix{client}", "unix{client,server}");
aux_add2group(L, "unix{server}", "unix{client,server}");
/* define library functions */
luaL_openlib(L, NULL, func, 0);
return 0;
luaL_openlib(L, "socket", func, 0);
return 1;
}
/*=========================================================================*\
@ -107,6 +109,16 @@ static int meth_receive(lua_State *L) {
return buf_meth_receive(L, &un->buf);
}
static int meth_getstats(lua_State *L) {
p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1);
return buf_meth_getstats(L, &un->buf);
}
static int meth_setstats(lua_State *L) {
p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1);
return buf_meth_setstats(L, &un->buf);
}
/*-------------------------------------------------------------------------*\
* Just call option handler
\*-------------------------------------------------------------------------*/
@ -250,7 +262,8 @@ static int meth_close(lua_State *L)
{
p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1);
sock_destroy(&un->sock);
return 0;
lua_pushnumber(L, 1);
return 1;
}
/*-------------------------------------------------------------------------*\
@ -277,7 +290,7 @@ static int meth_listen(lua_State *L)
\*-------------------------------------------------------------------------*/
static int meth_shutdown(lua_State *L)
{
p_unix un = (p_unix) aux_checkgroup(L, "unix{client}", 1);
p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1);
const char *how = luaL_optstring(L, 2, "both");
switch (how[0]) {
case 'b':

View File

@ -23,6 +23,6 @@ typedef struct t_unix_ {
} t_unix;
typedef t_unix *p_unix;
int unix_open(lua_State *L);
int luaopen_socketunix(lua_State *L);
#endif /* UNIX_H */

View File

@ -22,7 +22,7 @@
#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 sock_waitfd(int fd, int sw, p_tm tm) {
int ret;
struct pollfd pfd;
pfd.fd = fd;
@ -44,7 +44,7 @@ static int sock_waitfd(int fd, int sw, p_tm tm) {
#define WAITFD_W 2
#define WAITFD_C (WAITFD_R|WAITFD_W)
static int sock_waitfd(int fd, int sw, p_tm tm) {
int sock_waitfd(int fd, int sw, p_tm tm) {
int ret;
fd_set rfds, wfds, *rp, *wp;
struct timeval tv, *tp;
@ -166,12 +166,20 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
while ((err = errno) == EINTR);
/* if connection failed immediately, return error code */
if (err != EINPROGRESS && err != EAGAIN) return err;
/* zero timeout case optimization */
if (tm_iszero(tm)) return IO_TIMEOUT;
/* 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 */
return sock_connected(ps, tm);
}
/*-------------------------------------------------------------------------*\
* Checks if socket is connected, or return reason for failure
\*-------------------------------------------------------------------------*/
int sock_connected(p_sock ps, p_tm tm) {
int err;
if ((err = sock_waitfd(*ps, WAITFD_C, tm) == IO_CLOSED)) {
if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE;
else return errno;
/* timed out or some weirder error */
} else return err;
}
@ -321,13 +329,17 @@ void sock_setnonblocking(p_sock ps) {
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;
else if (h_errno) return h_errno;
else if (errno) return errno;
else return IO_UNKNOWN;
}
int sock_gethostbyname(const char *addr, struct hostent **hp) {
*hp = gethostbyname(addr);
if (*hp) return IO_DONE;
else return h_errno;
else if (h_errno) return h_errno;
else if (errno) return errno;
else return IO_UNKNOWN;
}
/*-------------------------------------------------------------------------*\

View File

@ -45,7 +45,7 @@ int sock_close(void) {
#define WAITFD_E 4
#define WAITFD_C (WAITFD_E|WAITFD_W)
static int sock_waitfd(t_sock fd, int sw, p_tm tm) {
int sock_waitfd(t_sock fd, int sw, p_tm tm) {
int ret;
fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
struct timeval tv, *tp = NULL;
@ -118,7 +118,17 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
/* make sure the system is trying to connect */
err = WSAGetLastError();
if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err;
/* zero timeout case optimization */
if (tm_iszero(tm)) return IO_TIMEOUT;
/* we wait until something happens */
return sock_connected(ps, tm);
}
/*-------------------------------------------------------------------------*\
* Check if socket is connected
\*-------------------------------------------------------------------------*/
int sock_connected(p_sock ps) {
int err;
if ((err = sock_waitfd(*ps, WAITFD_C, tm)) == IO_CLOSED) {
int len = sizeof(err);
/* give windows time to set the error (yes, disgusting) */
@ -126,9 +136,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
/* find out why we failed */
getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len);
/* we KNOW there was an error. if why is 0, we will return
* "unknown error", but it's not really our fault */
* "unknown error", but it's not really our fault */
return err > 0? err: IO_UNKNOWN;
/* here we deal with the case in which it worked, timedout or weird errors */
} else return err;
}