1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-05 22:19:03 +00:00

Consolidate device name checking in nse_dnet.cc

This commit is contained in:
dmiller
2024-06-20 20:14:31 +00:00
parent b63eea5eea
commit a5baf9de0b

View File

@@ -42,16 +42,31 @@ static int l_dnet_new (lua_State *L)
lua_setmetatable(L, -2);
udata->eth = NULL;
udata->sock = -1;
udata->devname[0] = '\0';
return 1;
}
static struct interface_info *checkdevname (lua_State *L, int idx)
{
size_t len = 0;
const char *interface_name = luaL_checklstring(L, idx, &len);
if (len >= 32) {
luaL_argerror(L, idx, "device name too long");
return NULL;
}
struct interface_info *ii = getInterfaceByName(interface_name, o.af());
if (ii == NULL)
luaL_argerror(L, idx, "device %s not found or no address configured");
return ii;
}
static int l_dnet_get_interface_info (lua_State *L)
{
char ipstr[INET6_ADDRSTRLEN];
struct addr src, bcast;
struct interface_info *ii = getInterfaceByName(luaL_checkstring(L, 1),
o.af());
struct interface_info *ii = checkdevname(L, 1);
if (ii == NULL)
return nseU_safeerror(L, "failed to find interface");
@@ -150,11 +165,7 @@ static eth_t *open_eth_cached (lua_State *L, int dnet_index, const char *device)
static int ethernet_open (lua_State *L)
{
nse_dnet_udata *udata = (nse_dnet_udata *) nseU_checkudata(L, 1, DNET_METATABLE, "dnet");
size_t len = 0;
const char *interface_name = luaL_checklstring(L, 2, &len);
if (len >= 32)
return luaL_argerror(L, 2, "device name too long");
struct interface_info *ii = getInterfaceByName(interface_name, o.af());
struct interface_info *ii = checkdevname(L, 2);
if (ii == NULL || ii->device_type != devt_ethernet
#ifdef WIN32
@@ -163,9 +174,9 @@ static int ethernet_open (lua_State *L)
)
return luaL_argerror(L, 2, "device is not valid ethernet interface");
udata->eth = open_eth_cached(L, 1, interface_name);
strncpy(udata->devname, interface_name, len);
udata->devname[len] = '\0';
udata->eth = open_eth_cached(L, 1, ii->devfullname);
strncpy(udata->devname, ii->devfullname, 16);
udata->devname[16] = '\0';
if (o.scriptTrace())
{
log_write(LOG_STDOUT, "%s: Ethernet open %s\n",