applications/luci-wol: use net.mac_hints() instead of net.arptable() and other sources
[oweals/luci.git] / applications / luci-wol / luasrc / model / cbi / wol.lua
index 03598fa13308da9cf74a3a10f5a59f274c4f19ab..d683b6e16f539a1e57742c336a81f91042a25c01 100644 (file)
@@ -23,57 +23,9 @@ m.reset  = false
 local has_ewk = fs.access("/usr/bin/etherwake")
 local has_wol = fs.access("/usr/bin/wol")
 
-if luci.http.formvalue("cbi.submit") then
-       local host = luci.http.formvalue("cbid.wol.1.mac")
-       if host and #host > 0 then
-               local cmd
-               local util = luci.http.formvalue("cbid.wol.1.binary") or (
-                       has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
-               )
-
-               if util == "/usr/bin/etherwake" then
-                       local iface = luci.http.formvalue("cbid.wol.1.iface")
-                       cmd = "%s -D%s %q" %{
-                               util, (iface ~= "" and " -i %q" % iface or ""), host
-                       }
-               else
-                       cmd = "%s -v %q" %{ util, host }
-               end
-
-               is = m:section(SimpleSection)
-
-               function is.render()
-                       luci.http.write(
-                               "<p><br /><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
-                                       translate("Starting WoL utility:"), cmd
-                               }
-                       )
-
-                       local p = io.popen(cmd .. " 2>&1")
-                       if p then
-                               while true do
-                                       local l = p:read("*l")
-                                       if l then
-                                               if #l > 100 then l = l:sub(1, 100) .. "..." end
-                                               luci.http.write(l .. "<br />")
-                                       else
-                                               break
-                                       end
-                               end
-                               p:close()
-                       end
-
-                       luci.http.write("</code><br /></p>")
-               end
-       end
-end
-
 
 s = m:section(SimpleSection)
 
-local arp = { }
-local e, ip, mac, name
-
 if has_ewk and has_wol then
        bin = s:option(ListValue, "binary", translate("WoL program"),
                translate("Sometimes only one of both tools work. If one of fails, try the other one"))
@@ -91,34 +43,61 @@ if has_ewk then
        end
 
        iface:value("", translate("Broadcast on all interfaces"))
-       
+
        for _, e in ipairs(sys.net.devices()) do
                if e ~= "lo" then iface:value(e) end
        end
 end
 
 
-for _, e in ipairs(sys.net.arptable()) do
-       arp[e["HW address"]] = { e["IP address"] }
-end
+host = s:option(Value, "mac", translate("Host to wake up"),
+       translate("Choose the host to wake up or enter a custom MAC address to use"))
 
-for e in io.lines("/etc/ethers") do
-       mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
-       if mac and ip then arp[mac] = { ip } end
-end
+sys.net.mac_hints(function(mac, name)
+       host:value(mac, "%s (%s)" %{ mac, name })
+end)
 
-for e in io.lines("/var/dhcp.leases") do
-       mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
-       if mac and ip then arp[mac] = { ip, name ~= "*" and name } end
-end
 
-host = s:option(Value, "mac", translate("Host to wake up"),
-       translate("Choose the host to wake up or enter a custom MAC address to use"))
+function host.write(self, s, val)
+       local host = luci.http.formvalue("cbid.wol.1.mac")
+       if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
+               local cmd
+               local util = luci.http.formvalue("cbid.wol.1.binary") or (
+                       has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
+               )
+
+               if util == "/usr/bin/etherwake" then
+                       local iface = luci.http.formvalue("cbid.wol.1.iface")
+                       cmd = "%s -D%s %q" %{
+                               util, (iface ~= "" and " -i %q" % iface or ""), host
+                       }
+               else
+                       cmd = "%s -v %q" %{ util, host }
+               end
 
-for mac, ip in pairs(arp) do
-       host:value(mac, "%s (%s)" %{ mac, ip[2] or ip[1] })
+               local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
+                       translate("Starting WoL utility:"), cmd
+               }
+
+               local p = io.popen(cmd .. " 2>&1")
+               if p then
+                       while true do
+                               local l = p:read("*l")
+                               if l then
+                                       if #l > 100 then l = l:sub(1, 100) .. "..." end
+                                       msg = msg .. l .. "<br />"
+                               else
+                                       break
+                               end
+                       end
+                       p:close()
+               end
+
+               msg = msg .. "</code></p>"
+
+               m.message = msg
+       end
 end
 
 
 return m
-