Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
function action_dispatch()
local uci = luci.model.uci.cursor_state()
function action_dispatch()
local uci = luci.model.uci.cursor_state()
- local mac = luci.sys.net.ip4mac(luci.http.getenv("REMOTE_ADDR")) or ""
+ local ipc = require "luci.ip"
+
+ local i, n
+ local mac = ""
+ for i, n in ipairs(ipc.neighbors()) do
+ if n.mac and n.dest and n.dest:equal(luci.http.getenv("REMOTE_ADDR")) then
+ mac = n.mac
+ end
+ end
+
uci:foreach("luci_splash", "lease", function(s)
if s.mac and s.mac:lower() == mac then access = true end
end)
uci:foreach("luci_splash", "lease", function(s)
if s.mac and s.mac:lower() == mac then access = true end
end)
local ipt = require "luci.sys.iptparser".IptParser()
local uci = require "luci.model.uci".cursor_state()
local wat = require "luci.tools.webadmin"
local ipt = require "luci.sys.iptparser".IptParser()
local uci = require "luci.model.uci".cursor_state()
local wat = require "luci.tools.webadmin"
+local ipc = require "luci.ip"
local fs = require "nixio.fs"
local clients = { }
local fs = require "nixio.fs"
local clients = { }
-for i, a in ipairs(luci.sys.net.arptable()) do
- local c = clients[a["HW address"]:lower()]
- if c and not c.ip then
- c.ip = a["IP address"]
+for i, n in ipairs(ipc.neighbors({ family = 4 })) do
+ if n.mac and n.dest then
+ local c = clients[n.mac]
+ if c and not c.ip then
+ c.ip = n.dest:string()
+ end
utl = require "luci.util"
sys = require "luci.sys"
utl = require "luci.util"
sys = require "luci.sys"
-require("luci.model.uci")
-require("luci.sys.iptparser")
-local uci = luci.model.uci.cursor_state()
-local ipt = luci.sys.iptparser.IptParser()
-local net = sys.net
+local uci = require "luci.model.uci".cursor_state()
local fs = require "nixio.fs"
local ip = require "luci.ip"
local fs = require "nixio.fs"
local ip = require "luci.ip"
+function mac_to_ip(mac)
+ ipc.neighbors({ family = 4 }, function(n)
+ if n.mac == mac and n.dest then
+ return n.dest:string()
+ end
+ end)
+end
+
+function mac_to_dev(mac)
+ ipc.neighbors({ family = 4 }, function(n)
+ if n.mac == mac and n.dev then
+ return n.dev
+ end
+ end)
+end
+
+function ip_to_mac(ip)
+ ipc.neighbors({ family = 4 }, function(n)
+ if n.mac and n.dest and n.dest:equal(ip) then
+ return n.mac
+ end
+ end)
+end
+
function main(argv)
local cmd = table.remove(argv, 1)
local arg = argv[1]
function main(argv)
local cmd = table.remove(argv, 1)
local arg = argv[1]
- local arp_cache = net.arptable()
local leased_macs = get_known_macs("lease")
local blacklist_macs = get_known_macs("blacklist")
local whitelist_macs = get_known_macs("whitelist")
local leased_macs = get_known_macs("lease")
local blacklist_macs = get_known_macs("blacklist")
local whitelist_macs = get_known_macs("whitelist")
if adr:find(":") then
mac = adr:lower()
else
if adr:find(":") then
mac = adr:lower()
else
- for _, e in ipairs(arp_cache) do
- if e["IP address"] == adr then
- mac = e["HW address"]:lower()
- break
- end
- end
end
if mac and cmd == "add-rules" then
if leased_macs[mac] then
end
if mac and cmd == "add-rules" then
if leased_macs[mac] then
- add_lease(mac, arp_cache, true)
elseif blacklist_macs[mac] then
add_blacklist_rule(mac)
elseif whitelist_macs[mac] then
elseif blacklist_macs[mac] then
add_blacklist_rule(mac)
elseif whitelist_macs[mac] then
--- Get current arp cache
-function get_arpcache()
- local arpcache = { }
- for _, entry in ipairs(net.arptable()) do
- arpcache[entry["HW address"]:lower()] = { entry["Device"]:lower(), entry["IP address"]:lower() }
- end
- return arpcache
-end
-
-- Get a list of known mac addresses
function get_known_macs(list)
local leased_macs = { }
-- Get a list of known mac addresses
function get_known_macs(list)
local leased_macs = { }
end
-- Add a lease to state and invoke add_rule
end
-- Add a lease to state and invoke add_rule
-function add_lease(mac, arp, no_uci)
+function add_lease(mac, no_uci)
mac = mac:lower()
-- Get current ip address
mac = mac:lower()
-- Get current ip address
- local ipaddr
- for _, entry in ipairs(arp or net.arptable()) do
- if entry["HW address"]:lower() == mac then
- ipaddr = entry["IP address"]
- break
- end
- end
+ local ipaddr = mac_to_ip(mac)
-- Add lease if there is an ip addr
if ipaddr then
-- Add lease if there is an ip addr
if ipaddr then
uci:revert("luci_splash_leases")
uci:revert("luci_splash_leases")
- local arpcache = get_arpcache()
-
local blackwhitelist = uci:get_all("luci_splash")
local whitelist_total = 0
local whitelist_online = 0
local blackwhitelist = uci:get_all("luci_splash")
local whitelist_total = 0
local whitelist_online = 0
leasecount = leasecount + 1
-- only count leases_online for connected clients
leasecount = leasecount + 1
-- only count leases_online for connected clients
- if arpcache[v.mac] then
+ if mac_to_ip(v.mac) then
leases_online = leases_online + 1
end
leases_online = leases_online + 1
end
whitelist_total = whitelist_total + 1
if s.mac then
local mac = s.mac:lower()
whitelist_total = whitelist_total + 1
if s.mac then
local mac = s.mac:lower()
whitelist_online = whitelist_online + 1
end
end
whitelist_online = whitelist_online + 1
end
end
blacklist_total = blacklist_total + 1
if s.mac then
local mac = s.mac:lower()
blacklist_total = blacklist_total + 1
if s.mac then
local mac = s.mac:lower()
blacklist_online = blacklist_online + 1
end
end
blacklist_online = blacklist_online + 1
end
end
-- Show client info
function list()
-- Show client info
function list()
- local arpcache = get_arpcache()
-- Find traffic usage
local function traffic(lease)
local traffic_in = 0
-- Find traffic usage
local function traffic(lease)
local traffic_in = 0
if s[".type"] == "lease" and s.mac then
local ti, to = traffic(s)
local mac = s.mac:lower()
if s[".type"] == "lease" and s.mac then
local ti, to = traffic(s)
local mac = s.mac:lower()
- local arp = arpcache[mac]
print(string.format(
"%-17s %-15s %-9s %3dm %-7s %7dKB %7dKB",
mac, s.ipaddr, "leased",
math.floor(( os.time() - tonumber(s.start) ) / 60),
print(string.format(
"%-17s %-15s %-9s %3dm %-7s %7dKB %7dKB",
mac, s.ipaddr, "leased",
math.floor(( os.time() - tonumber(s.start) ) / 60),
- arp and arp[1] or "?", ti, to
+ mac_to_dev(mac) or "?", ti, to
) do
if (s[".type"] == "whitelist" or s[".type"] == "blacklist") and s.mac then
local mac = s.mac:lower()
) do
if (s[".type"] == "whitelist" or s[".type"] == "blacklist") and s.mac then
local mac = s.mac:lower()
- local arp = arpcache[mac]
print(string.format(
"%-17s %-15s %-9s %4s %-7s %9s %9s",
print(string.format(
"%-17s %-15s %-9s %4s %-7s %9s %9s",
- mac, arp and arp[2] or "?", s[".type"],
- "- ", arp and arp[1] or "?", "-", "-"
+ mac, mac_to_ip(mac) or "?", s[".type"],
+ "- ", mac_to_dev(mac) or "?", "-", "-"