Index: scripts/ipidseq.nse =================================================================== --- scripts/ipidseq.nse (revision 16738) +++ scripts/ipidseq.nse (working copy) @@ -159,7 +159,7 @@ local ports for _, s in ipairs(states) do - ports = nmap.get_ports(host, {protocol="tcp", state=s}) + ports = nmap.get_ports(host, "tcp", s) if #ports > 0 then break end Index: nse_nmaplib.cc =================================================================== --- nse_nmaplib.cc (revision 16738) +++ nse_nmaplib.cc (working copy) @@ -447,29 +447,20 @@ /* Generates an array of port data for the given host and leaves it on * the top of the stack */ -void get_ports(lua_State *L, Target *target, int index) +static int l_get_ports (lua_State *L) { - Port *p = NULL, port; - int protocol, state = PORT_UNKNOWN; + static const char *op1[] = {"tcp", "udp", "sctp", NULL}; + static const int states1[] = {IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP}; + static const char *op2[] = {"open", "filtered", "unfiltered", "closed", + "open|filtered", "closed|filtered", NULL}; + static const int states2[] = {PORT_OPEN, PORT_FILTERED, PORT_UNFILTERED, + PORT_CLOSED, PORT_OPENFILTERED, PORT_CLOSEDFILTERED}; int i = 1; - luaL_checktype(L, index, LUA_TTABLE); - lua_getfield(L, index, "protocol"); - if (!lua_isstring(L, -1)) - luaL_error(L, "port 'protocol' field must be a string"); - protocol = strcmp(lua_tostring(L, -1), "tcp") == 0 ? IPPROTO_TCP : - strcmp(lua_tostring(L, -1), "udp") == 0 ? IPPROTO_UDP : - strcmp(lua_tostring(L, -1), "sctp") == 0 ? IPPROTO_SCTP : - luaL_error(L, "port 'protocol' field must be \"udp\", \"sctp\" or \"tcp\""); - lua_pop(L, 1); - lua_getfield(L, index, "state"); - if (lua_isstring(L, -1)) { - state = statestr2num(lua_tostring(L, -1)); - if (state == PORT_UNKNOWN) - luaL_error(L, "port 'state' field contains unknown state"); - lua_pop(L, 1); - } else if (!lua_isnil(L, -1)) { - luaL_error(L, "port 'state' field must be a string"); - } + Port *p = NULL, port; + Target *target = get_target(L, 1); + int protocol = states1[luaL_checkoption(L, 2, NULL, op1)]; + int state = states2[luaL_checkoption(L, 3, NULL, op2)]; + lua_newtable(L); while ((p = target->ports.nextPort(p, &port, protocol, state)) != NULL) { lua_newtable(L); @@ -505,16 +496,6 @@ return 1; } -/* This function can be called from Lua to obtain an array of port data from - * all ports on a host. This data is given for an individual port protocol - * and can be restricted to ports with a specific state. - */ -static int l_get_ports(lua_State *L) -{ - get_ports(L, get_target(L, 1), 2); - return 1; -} - /* unlike set_portinfo() this function sets the port state in nmap. * if for example a udp port was seen by the script as open instead of * filtered, the script is free to say so. Index: nmap.cc =================================================================== --- nmap.cc (revision 16738) +++ nmap.cc (working copy) @@ -2630,22 +2630,6 @@ return "unknown"; } -int statestr2num(const char *str) { - if (strcmp(str, "open") == 0) - return PORT_OPEN; - else if (strcmp(str, "filtered") == 0) - return PORT_FILTERED; - else if (strcmp(str, "unfiltered") == 0) - return PORT_UNFILTERED; - else if (strcmp(str, "closed") == 0) - return PORT_CLOSED; - else if (strcmp(str, "open|filtered") == 0) - return PORT_OPENFILTERED; - else if (strcmp(str, "closed|filtered") == 0) - return PORT_CLOSEDFILTERED; - return PORT_UNKNOWN; -} - int ftp_anon_connect(struct ftpinfo *ftp) { int sd; struct sockaddr_in sock; Index: nmap.h =================================================================== --- nmap.h (revision 16738) +++ nmap.h (working copy) @@ -438,7 +438,6 @@ /* general helper functions */ const char *statenum2str(int state); -int statestr2num(const char *str); const char *scantype2str(stype scantype); void reaper(int signo); char *seqreport(struct seq_info *seq);