From 8d4e240f6ae50d9b22ddc44f5e207018935da907 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Tue, 8 Feb 2005 10:01:01 +0000 Subject: Forward server working on Mac OS X... --- src/buffer.c | 2 +- src/inet.c | 1 - src/luasocket.c | 4 ++-- src/luasocket.h | 4 ++-- src/mime.c | 4 ++-- src/mime.h | 2 +- src/mime.lua | 3 ++- src/socket.h | 4 ++++ src/socket.lua | 3 ++- src/tcp.c | 22 ++++++++++++++++++++-- src/unix.c | 27 ++++++++++++++++++++------- src/unix.h | 2 +- src/usocket.c | 26 +++++++++++++++++++------- src/wsocket.c | 15 ++++++++++++--- 14 files changed, 88 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/buffer.c b/src/buffer.c index 0ec7b4d..45cd0f2 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -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 diff --git a/src/inet.c b/src/inet.c index e2afcdf..d713643 100644 --- a/src/inet.c +++ b/src/inet.c @@ -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); } diff --git a/src/luasocket.c b/src/luasocket.c index 4b829f8..8f13dbc 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -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); diff --git a/src/luasocket.h b/src/luasocket.h index db54a18..768e335 100644 --- a/src/luasocket.h +++ b/src/luasocket.h @@ -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 */ diff --git a/src/mime.c b/src/mime.c index dcc4af3..67f9f5b 100644 --- a/src/mime.c +++ b/src/mime.c @@ -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); diff --git a/src/mime.h b/src/mime.h index 688d043..d596861 100644 --- a/src/mime.h +++ b/src/mime.h @@ -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 */ diff --git a/src/mime.lua b/src/mime.lua index 4d5bdba..6492a96 100644 --- a/src/mime.lua +++ b/src/mime.lua @@ -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 diff --git a/src/socket.h b/src/socket.h index 368c2b6..639229d 100644 --- a/src/socket.h +++ b/src/socket.h @@ -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); diff --git a/src/socket.lua b/src/socket.lua index 1c82750..f3563e7 100644 --- a/src/socket.lua +++ b/src/socket.lua @@ -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") ----------------------------------------------------------------------------- diff --git a/src/tcp.c b/src/tcp.c index 0b3706b..3a84191 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -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 \*-------------------------------------------------------------------------*/ diff --git a/src/unix.c b/src/unix.c index 1e0e252..c169268 100644 --- a/src/unix.c +++ b/src/unix.c @@ -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': diff --git a/src/unix.h b/src/unix.h index 7b2a5c5..aaaef3d 100644 --- a/src/unix.h +++ b/src/unix.h @@ -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 */ diff --git a/src/usocket.c b/src/usocket.c index c1ab725..3428a0c 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -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; } /*-------------------------------------------------------------------------*\ diff --git a/src/wsocket.c b/src/wsocket.c index 69fac4d..c0686cd 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -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; } -- cgit v1.2.3-55-g6feb