static int sck_close (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); LuaSocket *ls = tolsock(L, LOSKI_BASESOCKET); tosock(L, LOSKI_BASESOCKET); /* make sure argument is an open socket */ return aux_close(drv, L, ls); }
/* succ [, errmsg] = socket:listen([backlog]) */ static int lst_listen (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_LSTNSOCKET); int backlog = luaL_optint(L, 2, 32); int res = loski_listensocket(drv, socket, backlog); return pushsockres(L, 0, res); }
static int dgm_setoption (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_DGRMSOCKET); loski_SocketOption opt = checksockopt(L, 2, dgm_opts); int val = lua_toboolean(L, 3); int res = loski_setsocketoption(drv, socket, opt, val); return pushsockres(L, 0, res); }
/* succ [, errmsg] = socket:shutdown([mode]) */ static int cnt_shutdown (lua_State *L) { static const char *const waynames[] = {"send", "receive", "both", NULL}; loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_CONNSOCKET); int way = luaL_checkoption(L, 2, "both", waynames); int res = loski_shutdownsocket(drv, socket, way); return pushsockres(L, 0, res); }
/* sent [, errmsg] = socket:send(data [, i [, j]]) */ static int cnt_send (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_CONNSOCKET); size_t sz, sent; const char *data = checkstream(L, 2, 3, 4, &sz); int res = loski_sendtosocket(drv, socket, data, sz, &sent, NULL); lua_pushinteger(L, sent); return pushsockres(L, 1, res); }
/* succ [, errmsg] = socket:connect(host, port) */ static int str_connect (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_STRMSOCKET); loski_Address address; int res = checkaddress(drv, L, 2, 3, &address); if (res != 0) return pushaddrres(L, 0, res); res = loski_connectsocket(drv, socket, &address); return pushsockres(L, 0, res); }
static int cnt_setoption (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_CONNSOCKET); loski_SocketOption opt = checksockopt(L, 2, cnt_opts); int val = (opt == LOSKI_SOCKOPT_LINGER) ? luaL_checkint(L, 3) : lua_toboolean(L, 3); int res = loski_setsocketoption(drv, socket, opt, val); return pushsockres(L, 0, res); }
/* value [, errmsg] = socket:getoption(name) */ static int lst_getoption (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_LSTNSOCKET); loski_SocketOption opt = checksockopt(L, 2, lst_opts); int val; int res = loski_getsocketoption(drv, socket, opt, &val); if (res == 0) lua_pushboolean(L, val); return pushsockres(L, 1, res); }
/* host, port = socket:getaddress([site]) */ static int sck_getaddress (lua_State *L) { static const char *const sites[] = {"local", "remote", NULL}; loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_BASESOCKET); loski_Address address; int site = luaL_checkoption(L, 2, "local", sites); int res = loski_socketaddress(drv, socket, &address, site); if (res == 0) return pushaddress(drv, L, &address); return pushsockres(L, 0, res); }
static int cnt_getoption (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_CONNSOCKET); loski_SocketOption opt = checksockopt(L, 2, cnt_opts); int val; int res = loski_getsocketoption(drv, socket, opt, &val); if (res == 0) { if (opt == LOSKI_SOCKOPT_LINGER) lua_pushinteger(L, val); else lua_pushboolean(L, val); } return pushsockres(L, 1, res); }
/* data [, errmsg] = socket:receive(size) */ static int cnt_receive (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_CONNSOCKET); luaL_Buffer lbuf; size_t sz, len; char *buf = newbuffer(L, 2, &lbuf, &sz); int res = loski_recvfromsocket(drv, socket, buf, sz, &len, NULL); if (res == 0) { luaL_addsize(&lbuf, len); luaL_pushresult(&lbuf); /* close buffer */ } return pushsockres(L, 1, res); }
/* socket [, errmsg] = socket:accept([getfrom]) */ static int lst_accept (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_LSTNSOCKET); LuaSocket *ls = newsock(L, LOSKI_CONNSOCKET); int res, nr = 1; if (lua_toboolean(L, 2)) { loski_Address address; res = loski_acceptsocket(drv, socket, &ls->socket, &address); if (res == 0) nr += pushaddress(drv, L, &address); } else { res = loski_acceptsocket(drv, socket, &ls->socket, NULL); } if (res == 0) ls->closed = 0; return pushsockres(L, nr, res); }
/* sent [, errmsg] = socket:send(data [, i [, j [, host, port]]]) */ static int dgm_send (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_DGRMSOCKET); size_t sz, sent; const char *data = checkstream(L, 2, 3, 4, &sz); int res; if (!lua_isnoneornil(L, 5)) { loski_Address address; res = checkaddress(drv, L, 5, 6, &address); if (res != 0) return pushaddrres(L, 0, res); res = loski_sendtosocket(drv, socket, data, sz, &sent, &address); } else { res = loski_sendtosocket(drv, socket, data, sz, &sent, NULL); } lua_pushinteger(L, sent); return pushsockres(L, 1, res); }
void cleanup_dcc(void) { time_t now = time(NULL); sock_t *sock = main_sock; for (; sock; sock = sock->next) { #if 0 alldcc("dcc cleanup: checking %s(%d), SOCK_DCC? %s, SOCK_TELNET %s, SOCK_LISTEN %s", sock->name ? sock->name : "unknown", sock->socket, (sock->flags & SOCK_DCC ? "yes" : "no"), (sock->flags & SOCK_TELNET ? "yes" : "no"),(sock->flags & SOCK_LISTEN ? "yes" : "no")); #endif if (sock->flags & SOCK_DCC || (sock->flags & SOCK_TELNET && !(sock->flags & SOCK_LISTEN))) { if (!(sock->flags & SOCK_CONN) && sock->last + CONNTIMEOUT <= now) sock->flags |= SOCK_DEL; if (!(sock->flags & SOCK_AUTH) && sock->conn + CONNTIMEOUT <= now) { tosock(sock, "Login grace time exceeded, closing connection.\n"); sock->flags |= SOCK_DEL; } } } }
/* data [, errmsg] = socket:receive(size [, getfrom]) */ static int dgm_receive (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_DGRMSOCKET); luaL_Buffer lbuf; size_t sz, len; char *buf = newbuffer(L, 2, &lbuf, &sz); int res, nr = 1; if (lua_toboolean(L, 3)) { loski_Address address; res = loski_recvfromsocket(drv, socket, buf, sz, &len, &address); if (res == 0) { luaL_addsize(&lbuf, len); luaL_pushresult(&lbuf); /* close buffer */ nr += pushaddress(drv, L, &address); } } else { res = loski_recvfromsocket(drv, socket, buf, sz, &len, NULL); if (res == 0) { luaL_addsize(&lbuf, len); luaL_pushresult(&lbuf); /* close buffer */ } } return pushsockres(L, nr, res); }