luci-base: cbi: rework ListValue and MultiValue templates
[project/luci.git] / modules / luci-base / luasrc / sys.lua
index 31c3e12..fb2c4b1 100644 (file)
@@ -168,14 +168,13 @@ local function _nethints(what, callback)
                end
        end
 
-       if fs.access("/proc/net/arp") then
-               for e in io.lines("/proc/net/arp") do
-                       ip, mac = e:match("^([%d%.]+)%s+%S+%s+%S+%s+([a-fA-F0-9:]+)%s+")
-                       if ip and mac then
-                               _add(what, mac:upper(), ip, nil, nil)
-                       end
+       luci.ip.neighbors(nil, function(neigh)
+               if neigh.mac and neigh.family == 4 then
+                       _add(what, neigh.mac:upper(), neigh.dest:string(), nil, nil)
+               elseif neigh.mac and neigh.family == 6 then
+                       _add(what, neigh.mac:upper(), nil, neigh.dest:string(), nil)
                end
-       end
+       end)
 
        if fs.access("/etc/ethers") then
                for e in io.lines("/etc/ethers") do
@@ -186,14 +185,18 @@ local function _nethints(what, callback)
                end
        end
 
-       if fs.access("/var/dhcp.leases") then
-               for e in io.lines("/var/dhcp.leases") do
-                       mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
-                       if mac and ip then
-                               _add(what, mac:upper(), ip, nil, name ~= "*" and name)
+       cur:foreach("dhcp", "dnsmasq",
+               function(s)
+                       if s.leasefile and fs.access(s.leasefile) then
+                               for e in io.lines(s.leasefile) do
+                                       mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
+                                       if mac and ip then
+                                               _add(what, mac:upper(), ip, nil, name ~= "*" and name)
+                                       end
+                               end
                        end
                end
-       end
+       )
 
        cur:foreach("dhcp", "host",
                function(s)
@@ -292,6 +295,28 @@ function net.ipv6_hints(callback)
        end
 end
 
+function net.host_hints(callback)
+       if callback then
+               _nethints(1, function(mac, v4, v6, name)
+                       if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
+                               callback(mac, v4, v6, name)
+                       end
+               end)
+       else
+               local rv = { }
+               _nethints(1, function(mac, v4, v6, name)
+                       if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
+                               local e = { }
+                               if v4   then e.ipv4 = v4   end
+                               if v6   then e.ipv6 = v6   end
+                               if name then e.name = name end
+                               rv[mac] = e
+                       end
+               end)
+               return rv
+       end
+end
+
 function net.conntrack(callback)
        local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
        if not ok or not nfct then
@@ -300,12 +325,10 @@ function net.conntrack(callback)
 
        local line, connt = nil, (not callback) and { }
        for line in nfct do
-               local fam, l3, l4, timeout, state, tuples =
-                       line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +([A-Z_]+) +(.+)$")
+               local fam, l3, l4, timeout, tuples =
+                       line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
 
-               if fam and l3 and l4 and timeout and state and tuples and
-                  state ~= "TIME_WAIT"
-               then
+               if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
                        l4 = nixio.getprotobynumber(l4)
 
                        local entry = {