From c94d96b65c411efefb2261a57fa979a581d96d44 Mon Sep 17 00:00:00 2001 From: dmiller Date: Tue, 5 May 2026 20:58:57 +0000 Subject: [PATCH] Also handle immediate callback for pcap read, in case that ever happens. --- nse_nsock.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/nse_nsock.cc b/nse_nsock.cc index 5791fe635..f1c3f5a07 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -1097,7 +1097,7 @@ static void pcap_receive_handler (nsock_pool nsp, nsock_event nse, void *ud) nse_nsock_udata *nu = (nse_nsock_udata *) ud; lua_State *L = nu->thread; - assert(lua_status(L) == LUA_YIELD); + assert(nse_type(nse) == NSE_TYPE_PCAP_READ); if (nse_status(nse) == NSE_STATUS_SUCCESS) { const unsigned char *l2_data, *l3_data; @@ -1111,7 +1111,18 @@ static void pcap_receive_handler (nsock_pool nsp, nsock_event nse, void *ud) lua_pushlstring(L, (const char *) l2_data, l2_len); lua_pushlstring(L, (const char *) l3_data, l3_len); lua_pushnumber(L, TIMEVAL_SECS(tv)); - nse_restore(L, 5); + if(lua_status(L) == LUA_YIELD) + nse_restore(L, 5); + else + nu->action = NU_ACTION_IMMEDIATE; + return; + } + else if (lua_status(L) == LUA_OK) { + // Not aware this can happen, but better to be safe + lua_pushboolean(L, false); + lua_pushstring(L, nse_status2str(nse_status(nse))); + nu->action = NU_ACTION_IMMEDIATE; + return; } else status(L, nse_status(nse)); /* will also restore the thread */ @@ -1124,8 +1135,13 @@ static int l_pcap_receive (lua_State *L) if (nu->nsiod == NULL || nu->af != NSE_AF_PCAP) { return luaL_error(L, "not a pcap socket"); } + int oldtop = lua_gettop(L); nsock_pcap_read_packet(nsp, nu->nsiod, pcap_receive_handler, nu->timeout, nu); + if (nu->action == NU_ACTION_IMMEDIATE) { + // Immediate return + return lua_gettop(L) - oldtop; + } return yield(L, nu, "PCAP RECEIVE", FROM, 0, NULL); }