From c51d4acf1c2a8675a3bb043e799ff4390cef47d6 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Sat, 16 Aug 2003 00:06:04 +0000 Subject: Adjusted a few inconsistencies with the manual. --- src/ftp.lua | 4 +- src/http.lua | 2 +- src/inet.c | 29 ++++++++--- src/smtp.lua | 12 ++--- src/tcp.c | 164 +++++++++++++++++++++++++++++----------------------------- src/timeout.c | 2 +- src/timeout.h | 2 +- src/udp.c | 10 ++-- src/usocket.c | 6 ++- src/wsocket.c | 6 ++- 10 files changed, 128 insertions(+), 109 deletions(-) (limited to 'src') diff --git a/src/ftp.lua b/src/ftp.lua index 9d75d2a..25226a6 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -224,7 +224,7 @@ function Private.port(control) local server, ctl_ip ctl_ip, answer = control:getsockname() server, answer = socket.bind(ctl_ip, 0) - server:timeout(Public.TIMEOUT) + server:settimeout(Public.TIMEOUT) local ip, p, ph, pl ip, p = server:getsockname() pl = math.mod(p, 256) @@ -404,7 +404,7 @@ function Private.open(parsed) local control, err = socket.connect(parsed.host, parsed.port) if not control then return nil, err end -- make sure we don't block forever - control:timeout(Public.TIMEOUT) + control:settimeout(Public.TIMEOUT) -- check greeting local code, answer = Private.greet(control) if not code then return nil, answer end diff --git a/src/http.lua b/src/http.lua index 4ef2c87..212e8f6 100644 --- a/src/http.lua +++ b/src/http.lua @@ -553,7 +553,7 @@ function Public.request_cb(request, response) sock, response.error = socket.connect(parsed.host, parsed.port) if not sock then return response end -- set connection timeout so that we do not hang forever - sock:timeout(Public.TIMEOUT) + sock:settimeout(Public.TIMEOUT) -- send request message response.error = Private.send_request(sock, request.method, Private.request_uri(parsed), request.headers, request.body_cb) diff --git a/src/inet.c b/src/inet.c index 574399c..f15a5a4 100644 --- a/src/inet.c +++ b/src/inet.c @@ -20,6 +20,7 @@ static int inet_global_toip(lua_State *L); static int inet_global_tohostname(lua_State *L); static void inet_pushresolved(lua_State *L, struct hostent *hp); +/* DNS functions */ static luaL_reg func[] = { { "toip", inet_global_toip }, { "tohostname", inet_global_tohostname }, @@ -34,7 +35,19 @@ static luaL_reg func[] = { \*-------------------------------------------------------------------------*/ void inet_open(lua_State *L) { - luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); + lua_pushstring(L, LUASOCKET_LIBNAME); + lua_gettable(L, LUA_GLOBALSINDEX); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + lua_newtable(L); + lua_pushstring(L, LUASOCKET_LIBNAME); + lua_pushvalue(L, -2); + lua_settable(L, LUA_GLOBALSINDEX); + } + lua_pushstring(L, "dns"); + lua_newtable(L); + luaL_openlib(L, NULL, func, 0); + lua_settable(L, -3); lua_pop(L, 1); } @@ -100,10 +113,11 @@ int inet_meth_getpeername(lua_State *L, p_sock ps) socklen_t peer_len = sizeof(peer); if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { lua_pushnil(L); - return 1; + lua_pushstring(L, "getpeername failed"); + } else { + lua_pushstring(L, inet_ntoa(peer.sin_addr)); + lua_pushnumber(L, ntohs(peer.sin_port)); } - lua_pushstring(L, inet_ntoa(peer.sin_addr)); - lua_pushnumber(L, ntohs(peer.sin_port)); return 2; } @@ -116,10 +130,11 @@ int inet_meth_getsockname(lua_State *L, p_sock ps) socklen_t local_len = sizeof(local); if (getsockname(*ps, (SA *) &local, &local_len) < 0) { lua_pushnil(L); - return 1; + lua_pushstring(L, "getsockname failed"); + } else { + lua_pushstring(L, inet_ntoa(local.sin_addr)); + lua_pushnumber(L, ntohs(local.sin_port)); } - lua_pushstring(L, inet_ntoa(local.sin_addr)); - lua_pushnumber(L, ntohs(local.sin_port)); return 2; } diff --git a/src/smtp.lua b/src/smtp.lua index 209825b..5249160 100644 --- a/src/smtp.lua +++ b/src/smtp.lua @@ -243,7 +243,7 @@ function Private.open(server) -- connect to server and make sure we won't hang local sock, err = socket.connect(server, Public.PORT) if not sock then return nil, err end - sock:timeout(Public.TIMEOUT) + sock:settimeout(Public.TIMEOUT) -- initial server greeting code, answer = Private.check_answer(sock, 220) if not code then return nil, answer end @@ -305,10 +305,10 @@ end ----------------------------------------------------------------------------- function Public.mail(message) local sock, err = Private.open(message.server) - if not sock then return err end + if not sock then return nil, err end local code, answer = Private.send(sock, message) - if not code then return answer end - code, answer = Private.close(sock) - if code then return nil end - return answer + if not code then return nil, answer end + code, answer = Private.close(sock) + if code then return 1 + else return nil, answer end end diff --git a/src/tcp.c b/src/tcp.c index 28f38f5..098e29d 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -29,10 +29,10 @@ static int meth_receive(lua_State *L); static int meth_accept(lua_State *L); static int meth_close(lua_State *L); static int meth_setoption(lua_State *L); -static int meth_timeout(lua_State *L); +static int meth_settimeout(lua_State *L); static int meth_fd(lua_State *L); static int meth_dirty(lua_State *L); -static int opt_nodelay(lua_State *L); +static int opt_tcp_nodelay(lua_State *L); static int opt_keepalive(lua_State *L); static int opt_linger(lua_State *L); @@ -47,7 +47,7 @@ static luaL_reg tcp[] = { {"setsockname", meth_bind}, {"getpeername", meth_getpeername}, {"getsockname", meth_getsockname}, - {"timeout", meth_timeout}, + {"settimeout", meth_settimeout}, {"close", meth_close}, {"setoption", meth_setoption}, {"__gc", meth_close}, @@ -59,7 +59,7 @@ static luaL_reg tcp[] = { /* socket option handlers */ static luaL_reg opt[] = { {"keepalive", opt_keepalive}, - {"nodelay", opt_nodelay}, + {"tcp-nodelay", opt_tcp_nodelay}, {"linger", opt_linger}, {NULL, NULL} }; @@ -83,8 +83,8 @@ void tcp_open(lua_State *L) aux_add2group(L, "tcp{master}", "tcp{any}"); aux_add2group(L, "tcp{client}", "tcp{any}"); aux_add2group(L, "tcp{server}", "tcp{any}"); - aux_add2group(L, "tcp{client}", "tcp{client, server}"); - aux_add2group(L, "tcp{server}", "tcp{client, server}"); + aux_add2group(L, "tcp{client}", "tcp{client,server}"); + aux_add2group(L, "tcp{server}", "tcp{client,server}"); /* both server and client objects are selectable */ aux_add2group(L, "tcp{client}", "select{able}"); aux_add2group(L, "tcp{server}", "select{able}"); @@ -121,7 +121,7 @@ static int meth_setoption(lua_State *L) static int opt_boolean(lua_State *L, int level, int name) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); + p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); int val = aux_checkboolean(L, 2); if (setsockopt(tcp->sock, level, name, (char *) &val, sizeof(val)) < 0) { lua_pushnil(L); @@ -132,8 +132,8 @@ static int opt_boolean(lua_State *L, int level, int name) return 1; } -/* disables the Nagle algorithm */ -static int opt_nodelay(lua_State *L) +/* disables the Naggle algorithm */ +static int opt_tcp_nodelay(lua_State *L) { struct protoent *pe = getprotobyname("TCP"); if (!pe) { @@ -155,13 +155,13 @@ int opt_linger(lua_State *L) struct linger li; if (!lua_istable(L, 2)) luaL_typerror(L, 2, lua_typename(L, LUA_TTABLE)); - lua_pushstring(L, "onoff"); + lua_pushstring(L, "on"); lua_gettable(L, 2); - if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid onoff field"); - li.l_onoff = (int) lua_tonumber(L, -1); - lua_pushstring(L, "linger"); + if (!lua_isboolean(L, -1)) luaL_argerror(L, 2, "invalid 'on' field"); + li.l_onoff = lua_toboolean(L, -1); + lua_pushstring(L, "timeout"); lua_gettable(L, 2); - if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid linger field"); + if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid 'timeout' field"); li.l_linger = (int) lua_tonumber(L, -1); if (setsockopt(tcp->sock, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li) < 0)) { @@ -178,122 +178,122 @@ int opt_linger(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_fd(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); + p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); lua_pushnumber(L, tcp->sock); return 1; } static int meth_dirty(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); + p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); lua_pushboolean(L, !buf_isempty(&tcp->buf)); return 1; } /*-------------------------------------------------------------------------*\ -* Just call inet methods -\*-------------------------------------------------------------------------*/ -static int meth_getpeername(lua_State *L) -{ - p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); - return inet_meth_getpeername(L, &tcp->sock); -} - -static int meth_getsockname(lua_State *L) -{ - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); - return inet_meth_getsockname(L, &tcp->sock); -} - -/*-------------------------------------------------------------------------*\ -* Just call tm methods -\*-------------------------------------------------------------------------*/ -static int meth_timeout(lua_State *L) -{ - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); - return tm_meth_timeout(L, &tcp->tm); -} - -/*-------------------------------------------------------------------------*\ -* Closes socket used by object +* Waits for and returns a client object attempting connection to the +* server object \*-------------------------------------------------------------------------*/ -static int meth_close(lua_State *L) +static int meth_accept(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); - sock_destroy(&tcp->sock); - return 0; + struct sockaddr_in addr; + socklen_t addr_len = sizeof(addr); + p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); + p_tm tm = &server->tm; + p_tcp client = lua_newuserdata(L, sizeof(t_tcp)); + tm_markstart(tm); + aux_setclass(L, "tcp{client}", -1); + for ( ;; ) { + sock_accept(&server->sock, &client->sock, + (SA *) &addr, &addr_len, tm_get(tm)); + if (client->sock == SOCK_INVALID) { + if (tm_get(tm) == 0) { + lua_pushnil(L); + io_pusherror(L, IO_TIMEOUT); + return 2; + } + } else break; + } + /* initialize remaining structure fields */ + io_init(&client->io, (p_send) sock_send, (p_recv) sock_recv, &client->sock); + tm_init(&client->tm, -1, -1); + buf_init(&client->buf, &client->io, &client->tm); + return 1; } /*-------------------------------------------------------------------------*\ -* Turns a master tcp object into a client object. +* Turns a master object into a server object \*-------------------------------------------------------------------------*/ -static int meth_connect(lua_State *L) +static int meth_bind(lua_State *L) { p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); const char *address = luaL_checkstring(L, 2); unsigned short port = (unsigned short) luaL_checknumber(L, 3); - const char *err = inet_tryconnect(&tcp->sock, address, port); + int backlog = (int) luaL_optnumber(L, 4, 1); + const char *err = inet_trybind(&tcp->sock, address, port, backlog); if (err) { lua_pushnil(L); lua_pushstring(L, err); return 2; } - /* turn master object into a client object */ - aux_setclass(L, "tcp{client}", 1); + /* turn master object into a server object */ + aux_setclass(L, "tcp{server}", 1); lua_pushnumber(L, 1); return 1; } /*-------------------------------------------------------------------------*\ -* Turns a master object into a server object +* Turns a master tcp object into a client object. \*-------------------------------------------------------------------------*/ -static int meth_bind(lua_State *L) +static int meth_connect(lua_State *L) { p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); const char *address = luaL_checkstring(L, 2); unsigned short port = (unsigned short) luaL_checknumber(L, 3); - int backlog = (int) luaL_optnumber(L, 4, 1); - const char *err = inet_trybind(&tcp->sock, address, port, backlog); + const char *err = inet_tryconnect(&tcp->sock, address, port); if (err) { lua_pushnil(L); lua_pushstring(L, err); return 2; } - /* turn master object into a server object */ - aux_setclass(L, "tcp{server}", 1); + /* turn master object into a client object */ + aux_setclass(L, "tcp{client}", 1); lua_pushnumber(L, 1); return 1; } /*-------------------------------------------------------------------------*\ -* Waits for and returns a client object attempting connection to the -* server object +* Closes socket used by object \*-------------------------------------------------------------------------*/ -static int meth_accept(lua_State *L) +static int meth_close(lua_State *L) { - struct sockaddr_in addr; - socklen_t addr_len = sizeof(addr); - p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); - p_tm tm = &server->tm; - p_tcp client = lua_newuserdata(L, sizeof(t_tcp)); - tm_markstart(tm); - aux_setclass(L, "tcp{client}", -1); - for ( ;; ) { - sock_accept(&server->sock, &client->sock, - (SA *) &addr, &addr_len, tm_get(tm)); - if (client->sock == SOCK_INVALID) { - if (tm_get(tm) == 0) { - lua_pushnil(L); - io_pusherror(L, IO_TIMEOUT); - return 2; - } - } else break; - } - /* initialize remaining structure fields */ - io_init(&client->io, (p_send) sock_send, (p_recv) sock_recv, &client->sock); - tm_init(&client->tm, -1, -1); - buf_init(&client->buf, &client->io, &client->tm); - return 1; + p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); + sock_destroy(&tcp->sock); + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Just call inet methods +\*-------------------------------------------------------------------------*/ +static int meth_getpeername(lua_State *L) +{ + p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); + return inet_meth_getpeername(L, &tcp->sock); +} + +static int meth_getsockname(lua_State *L) +{ + p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); + return inet_meth_getsockname(L, &tcp->sock); +} + +/*-------------------------------------------------------------------------*\ +* Just call tm methods +\*-------------------------------------------------------------------------*/ +static int meth_settimeout(lua_State *L) +{ + p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); + return tm_meth_settimeout(L, &tcp->tm); } /*=========================================================================*\ diff --git a/src/timeout.c b/src/timeout.c index 6a30e3a..38d1135 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -125,7 +125,7 @@ void tm_open(lua_State *L) * time: time out value in seconds * mode: "b" for block timeout, "t" for total timeout. (default: b) \*-------------------------------------------------------------------------*/ -int tm_meth_timeout(lua_State *L, p_tm tm) +int tm_meth_settimeout(lua_State *L, p_tm tm) { int ms = lua_isnil(L, 2) ? -1 : (int) (luaL_checknumber(L, 2)*1000.0); const char *mode = luaL_optstring(L, 3, "b"); diff --git a/src/timeout.h b/src/timeout.h index 32eb836..ef2f533 100644 --- a/src/timeout.h +++ b/src/timeout.h @@ -26,6 +26,6 @@ void tm_markstart(p_tm tm); int tm_getstart(p_tm tm); int tm_get(p_tm tm); int tm_gettime(void); -int tm_meth_timeout(lua_State *L, p_tm tm); +int tm_meth_settimeout(lua_State *L, p_tm tm); #endif /* TM_H */ diff --git a/src/udp.c b/src/udp.c index b772b2e..6647711 100644 --- a/src/udp.c +++ b/src/udp.c @@ -30,7 +30,7 @@ static int meth_setsockname(lua_State *L); static int meth_setpeername(lua_State *L); static int meth_close(lua_State *L); static int meth_setoption(lua_State *L); -static int meth_timeout(lua_State *L); +static int meth_settimeout(lua_State *L); static int meth_fd(lua_State *L); static int meth_dirty(lua_State *L); static int opt_dontroute(lua_State *L); @@ -46,7 +46,7 @@ static luaL_reg udp[] = { {"sendto", meth_sendto}, {"receive", meth_receive}, {"receivefrom", meth_receivefrom}, - {"timeout", meth_timeout}, + {"settimeout", meth_settimeout}, {"close", meth_close}, {"setoption", meth_setoption}, {"__gc", meth_close}, @@ -252,10 +252,10 @@ static int opt_broadcast(lua_State *L) /*-------------------------------------------------------------------------*\ * Just call tm methods \*-------------------------------------------------------------------------*/ -static int meth_timeout(lua_State *L) +static int meth_settimeout(lua_State *L) { p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); - return tm_meth_timeout(L, &udp->tm); + return tm_meth_settimeout(L, &udp->tm); } /*-------------------------------------------------------------------------*\ @@ -297,7 +297,7 @@ static int meth_close(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_setsockname(lua_State *L) { - p_udp udp = (p_udp) aux_checkclass(L, "udp{master}", 1); + p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); const char *address = luaL_checkstring(L, 2); unsigned short port = (unsigned short) luaL_checknumber(L, 3); const char *err = inet_trybind(&udp->sock, address, port, -1); diff --git a/src/usocket.c b/src/usocket.c index cdd550c..202238b 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -26,8 +26,10 @@ int sock_open(void) \*-------------------------------------------------------------------------*/ void sock_destroy(p_sock ps) { - close(*ps); - *ps = SOCK_INVALID; + if (*ps != SOCK_INVALID) { + close(*ps); + *ps = SOCK_INVALID; + } } /*-------------------------------------------------------------------------*\ diff --git a/src/wsocket.c b/src/wsocket.c index 2ce828e..f9e1084 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -29,8 +29,10 @@ int sock_open(void) \*-------------------------------------------------------------------------*/ void sock_destroy(p_sock ps) { - closesocket(*ps); - *ps = SOCK_INVALID; + if (*ps != SOCK_INVALID) { + closesocket(*ps); + *ps = SOCK_INVALID; + } } /*-------------------------------------------------------------------------*\ -- cgit v1.2.3-55-g6feb