diff --git a/nse_nsock.cc b/nse_nsock.cc index 254a8d2..71148f1 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -40,6 +40,8 @@ enum { #define THREAD_I 1 /* The thread that yielded */ #define BUFFER_I 2 /* Location of Userdata Buffer */ +#define PCAP_IODS "pcap_iods" /* NSOCK_SOCKET metatable key */ + extern NmapOps o; typedef struct nse_nsock_udata @@ -851,8 +853,15 @@ static int l_close (lua_State *L) static int nsock_gc (lua_State *L) { nse_nsock_udata *nu = check_nsock_udata(L, 1); - if (nu->nsiod) + if (nu->nsiod) { + lua_getmetatable(L, 1); + lua_getfield(L, -1, PCAP_IODS); + lua_pushvalue(L,1); + lua_pushnil(L); + lua_rawset(L,-3); + lua_pop(L, 2); return l_close(L); + } return 0; } @@ -913,6 +922,7 @@ static int l_pcap_open (lua_State *L) nsock_iod *nsiod = (nsock_iod *) lua_touserdata(L, -1); if (nsiod == NULL) /* does not exist */ { + lua_pop(L, 1); /* the nonexistant socket */ nsiod = (nsock_iod *) lua_newuserdata(L, sizeof(nsock_iod)); lua_pushvalue(L, PCAP_SOCKET); lua_setmetatable(L, -2); @@ -924,12 +934,17 @@ static int l_pcap_open (lua_State *L) lua_pushvalue(L, -2); /* the pcap socket nsiod */ lua_pushboolean(L, 1); /* dummy variable */ lua_rawset(L, -3); - lua_pop(L, 1); /* the socket user value */ char *e = nsock_pcap_open(nsp, *nsiod, lua_tostring(L, 6), snaplen, lua_toboolean(L, 4), bpf); if (e) luaL_error(L, "%s", e); } + lua_getmetatable(L, 1); + lua_getfield(L, -1, PCAP_IODS); + lua_pushvalue(L, 1); + lua_pushvalue(L, -4); + lua_rawset(L, -3); /* NSOCK_SOCKET[PCAP_IODS][this]=pcap_nsiod */ + lua_pop(L, 3); nu->nsiod = *nsiod; nu->is_pcap = 1; return 0; @@ -1025,7 +1040,7 @@ LUALIB_API int luaopen_nsock (lua_State *L) lua_pushcclosure(L, nsock_gc, 6); lua_setfield(L, -2, "__gc"); lua_newtable(L); - lua_setfield(L, -2, "__metatable"); /* protect metatable */ + lua_setfield(L, -2, PCAP_IODS); lua_pop(L, 1); /* NSOCK_SOCKET */ /* Create the nsock pcap metatable */