Index: nse_nmaplib.cc =================================================================== --- nse_nmaplib.cc (revision 24546) +++ nse_nmaplib.cc (working copy) @@ -18,6 +18,7 @@ #include "nmap_dns.h" #include "osscan.h" #include "protocols.h" +#include "libnetutil/netutil.h" #include "nse_nmaplib.h" #include "nse_utility.h" @@ -765,17 +766,88 @@ return 1; } +/* returns a list of tables where each table contains information about each + * interface. + */ +static int l_list_interfaces (lua_State *L) +{ + int numifs = 0, numroutes = 0; + struct interface_info *iflist; + char errstr[256]; + errstr[0]='\0'; + char ipstr[INET6_ADDRSTRLEN]; + struct addr src, bcast; + + iflist = getinterfaces(&numifs, errstr, sizeof(errstr)); + + int i; + + if (iflist==NULL || numifs<=0) { + lua_pushnil(L); + lua_pushstring(L, errstr); + } else { + memset(ipstr, 0, INET6_ADDRSTRLEN); + memset(&src, 0, sizeof(src)); + memset(&bcast, 0, sizeof(bcast)); + lua_newtable(L); //base table + + for(i=0; i< numifs; i++) { + lua_newtable(L); //interface table + setsfield(L, -1, "device", iflist[i].devfullname); + setsfield(L, -1, "shortname", iflist[i].devname); + setnfield(L, -1, "netmask", iflist[i].netmask_bits); + setsfield(L, -1, "address", inet_ntop_ez(&(iflist[i].addr), + sizeof(iflist[i].addr) )); + + switch (iflist[i].device_type){ + case devt_ethernet: + setsfield(L, -1, "link", "ethernet"); + lua_pushlstring(L, (const char *) iflist[i].mac, 6); + lua_setfield(L, -2, "mac"); + + /* calculate the broadcast address */ + if (iflist[i].addr.ss_family == AF_INET) { + src.addr_type = ADDR_TYPE_IP; + src.addr_bits = iflist[i].netmask_bits; + src.addr_ip = ((struct sockaddr_in *)&(iflist[i].addr))->sin_addr.s_addr; + addr_bcast(&src, &bcast); + memset(ipstr, 0, INET6_ADDRSTRLEN); + if (addr_ntop(&bcast, ipstr, INET6_ADDRSTRLEN) != NULL) + setsfield(L, -1, "broadcast", ipstr); + } + break; + case devt_loopback: + setsfield(L, -1, "link", "loopback"); + break; + case devt_p2p: + setsfield(L, -1, "link", "p2p"); + break; + case devt_other: + default: + setsfield(L, -1, "link", "other"); + } + + setsfield(L, -1, "up", (iflist[i].device_up ? "up" : "down")); + setnfield(L, -1, "mtu", iflist[i].mtu); + + /* After setting the fields, add the interface table to the base table */ + lua_rawseti(L, -2, i); + } + } + return 1; +} + /* return the ttl (time to live) specified with the * --ttl command line option. If a wrong value is * specified it defaults to 64. */ static int l_get_ttl (lua_State *L) { - if (o.ttl < 0 || o.ttl > 255) - lua_pushnumber(L, 64); //default TTL - else - lua_pushnumber(L, o.ttl); - return 1; + if (o.ttl < 0 || o.ttl > 255) + lua_pushnumber(L, 64); //default TTL + else + lua_pushnumber(L, o.ttl); + return 1; } /* return the payload length specified by the --data-length @@ -784,11 +856,11 @@ */ static int l_get_payload_length(lua_State *L) { - if (o.extra_payload_length < 0) - lua_pushnumber(L, 0); //default payload length - else - lua_pushnumber(L, o.extra_payload_length); - return 1; + if (o.extra_payload_length < 0) + lua_pushnumber(L, 0); //default payload length + else + lua_pushnumber(L, o.extra_payload_length); + return 1; } int luaopen_nmap (lua_State *L) @@ -818,8 +890,9 @@ {"address_family", l_address_family}, {"get_interface", l_get_interface}, {"get_interface_info", l_dnet_get_interface_info}, - {"get_ttl", l_get_ttl}, - {"get_payload_length",l_get_payload_length}, + {"list_interfaces", l_list_interfaces}, + {"get_ttl", l_get_ttl}, + {"get_payload_length",l_get_payload_length}, {NULL, NULL} }; Index: nselib/nmap.luadoc =================================================================== --- nselib/nmap.luadoc (revision 24546) +++ nselib/nmap.luadoc (working copy) @@ -101,6 +101,26 @@ -- @usage local iface, err = nmap.get_interface_info("eth0") function get_interface_info(interface_name) +--- Lists network interfaces +-- +-- This script enumerates all network interfaces and returns a list of tables +-- containing information about every interface. +-- +-- Keys of each table: +-- * device The interface name, can be an interface alias. +-- * shortname A simple short name of the device. +-- * netmask The netmask bits (CIDR) of the interface. +-- * address The string representing the IP address assigned to the interface. +-- * link The string representing the hardware type of the interface. Possible values are: "ethernet", "loopback", "p2p" or "other". +-- * mac MAC address (six-byte-long binary string) of the interface if the type of the interface is "ethernet", otherwise it is nil. +-- * broadcast The string representing the broadcast address assigned to the interface if the interface type is "ethernet" and if the used address is IPv4, otherwise it is nil. +-- * up The state of the interface, possible values are "up" or "down". +-- * mtu The MTU size of the interface. +-- +-- @return Array of tables containing information about every discovered interface. +-- @usage local interfaces, err = nmap.list_interfaces() +function list_interfaces() + --- Returns the TTL (time to live) value selected by the --ttl option -- -- If there is no value specified or if the value specified with the --ttl