luci-app-splash: convert luci.sys.net.arptable() to luci.ip.neighbors()
authorJo-Philipp Wich <jow@openwrt.org>
Thu, 22 Jan 2015 20:52:04 +0000 (21:52 +0100)
committerJo-Philipp Wich <jow@openwrt.org>
Thu, 22 Jan 2015 20:57:00 +0000 (21:57 +0100)
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
applications/luci-app-splash/luasrc/controller/splash/splash.lua
applications/luci-app-splash/luasrc/view/admin_status/splash.htm
applications/luci-app-splash/root/usr/sbin/luci-splash

index 97d04008228ac0186525ba0c2fc841d80114f7d1..a41015ea1a653c9be124071f929fe9b282c7283d 100644 (file)
@@ -25,9 +25,18 @@ end
 
 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 = ""
        local access = false
 
+       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)
index 67bb2fc49e4e57eabc8dc8096ddac0955632fb41..831fa75f6590f6df750d08ad69891756e0d4e8d2 100644 (file)
@@ -9,6 +9,7 @@ local utl = require "luci.util"
 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 = { }
@@ -100,10 +101,12 @@ if fs.access(leasefile) then
        end
 end
 
-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
        end
 end
 
index 0f8bdc2c474a1401bbc23617e27998b14135d9df..e566e9b508ea204c7607a8bd8c5e7ebe3021980d 100755 (executable)
@@ -2,14 +2,11 @@
 
 utl = require "luci.util"
 sys = require "luci.sys"
+ipc = require "luci.ip"
 
-require("luci.model.uci")
-require("luci.sys.iptparser")
 
 -- Init state session
-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"
 
@@ -139,6 +136,30 @@ function ipvalid(ipaddr)
        return false
 end
 
+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]
@@ -157,7 +178,6 @@ function main(argv)
 
                lock()
 
-               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")
@@ -167,17 +187,12 @@ function main(argv)
                        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
+                               mac = ip_to_mac(adr)
                        end
 
                        if mac and cmd == "add-rules" then
                                if leased_macs[mac] then
-                                       add_lease(mac, arp_cache, true)
+                                       add_lease(mac, true)
                                elseif blacklist_macs[mac] then
                                        add_blacklist_rule(mac)
                                elseif whitelist_macs[mac] then
@@ -277,15 +292,6 @@ function main(argv)
        end
 end
 
--- 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 = { }
@@ -355,17 +361,11 @@ function convert_mac_to_secname(mac)
 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
-       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
@@ -598,8 +598,6 @@ function sync()
        uci:revert("luci_splash_leases")
 
         
-       local arpcache = get_arpcache()
-
        local blackwhitelist = uci:get_all("luci_splash")
        local whitelist_total = 0
        local whitelist_online = 0
@@ -618,7 +616,7 @@ function sync()
                                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
 
@@ -643,7 +641,7 @@ function sync()
                        whitelist_total = whitelist_total + 1
                        if s.mac then
                                local mac = s.mac:lower()
-                               if arpcache[mac] then
+                               if mac_to_ip(mac) then
                                        whitelist_online = whitelist_online + 1
                                end
                        end
@@ -652,7 +650,7 @@ function sync()
                        blacklist_total = blacklist_total + 1
                        if s.mac then
                                local mac = s.mac:lower()
-                               if arpcache[mac] then
+                               if mac_to_ip(mac) then
                                        blacklist_online = blacklist_online + 1
                                end
                        end
@@ -693,7 +691,6 @@ end
 
 -- Show client info
 function list()
-       local arpcache = get_arpcache()
        -- Find traffic usage
        local function traffic(lease)
                local traffic_in  = 0
@@ -722,12 +719,11 @@ function list()
                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),
-                               arp and arp[1] or "?", ti, to
+                               mac_to_dev(mac) or "?", ti, to
                        ))
                end
        end
@@ -738,11 +734,10 @@ function list()
        ) 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",
-                               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 "?", "-", "-"
                        ))
                end
        end