1 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.tools.status", package.seeall)
6 local uci = require "luci.model.uci".cursor()
7 local ipc = require "luci.ip"
9 local function duid_to_mac(duid)
10 local b1, b2, b3, b4, b5, b6
12 -- DUID-LLT / Ethernet
13 if type(duid) == "string" and #duid == 28 then
14 b1, b2, b3, b4, b5, b6 = duid:match("^00010001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)%x%x%x%x%x%x%x%x$")
17 elseif type(duid) == "string" and #duid == 20 then
18 b1, b2, b3, b4, b5, b6 = duid:match("^00030001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
21 return b1 and ipc.checkmac(table.concat({ b1, b2, b3, b4, b5, b6 }, ":"))
24 local function dhcp_leases_common(family)
26 local nfs = require "nixio.fs"
27 local sys = require "luci.sys"
28 local leasefile = "/tmp/dhcp.leases"
30 uci:foreach("dhcp", "dnsmasq",
32 if s.leasefile and nfs.access(s.leasefile) then
33 leasefile = s.leasefile
38 local fd = io.open(leasefile, "r")
41 local ln = fd:read("*l")
45 local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
46 local expire = tonumber(ts) or 0
47 if ts and mac and ip and name and duid then
48 if family == 4 and not ip:match(":") then
50 expires = (expire ~= 0) and os.difftime(expire, os.time()),
51 macaddr = ipc.checkmac(mac) or "00:00:00:00:00:00",
53 hostname = (name ~= "*") and name
55 elseif family == 6 and ip:match(":") then
57 expires = (expire ~= 0) and os.difftime(expire, os.time()),
59 duid = (duid ~= "*") and duid,
60 hostname = (name ~= "*") and name
69 local lease6file = "/tmp/hosts/odhcpd"
70 uci:foreach("dhcp", "odhcpd",
72 if t.leasefile and nfs.access(t.leasefile) then
73 lease6file = t.leasefile
77 local fd = io.open(lease6file, "r")
80 local ln = fd:read("*l")
84 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (-?%d+) (%S+) (%S+) (.*)")
85 local expire = tonumber(ts) or 0
86 if ip and iaid ~= "ipv4" and family == 6 then
88 expires = (expire >= 0) and os.difftime(expire, os.time()),
91 hostname = (name ~= "-") and name
93 elseif ip and iaid == "ipv4" and family == 4 then
95 expires = (expire >= 0) and os.difftime(expire, os.time()),
96 macaddr = ipc.checkmac(duid:gsub("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$", "%1:%2:%3:%4:%5:%6")) or "00:00:00:00:00:00",
98 hostname = (name ~= "-") and name
108 local hosts = sys.net.host_hints()
109 for _, lease in ipairs(rv) do
110 local mac = duid_to_mac(lease.duid)
111 local host = mac and hosts[mac]
113 if not lease.name then
114 lease.host_hint = host.name or host.ipv4 or host.ipv6
115 elseif host.name and lease.hostname ~= host.name then
116 lease.host_hint = host.name
125 function dhcp_leases()
126 return dhcp_leases_common(4)
129 function dhcp6_leases()
130 return dhcp_leases_common(6)
133 function wifi_networks()
135 local ntm = require "luci.model.network".init()
138 for _, dev in ipairs(ntm:get_wifidevs()) do
142 name = dev:get_i18n(),
147 for _, net in ipairs(dev:get_wifinets()) do
149 for _, a in pairs(net:assoclist() or {}) do
153 rd.networks[#rd.networks+1] = {
154 name = net:shortname(),
155 link = net:adminlink(),
157 mode = net:active_mode(),
158 ssid = net:active_ssid(),
159 bssid = net:active_bssid(),
160 encryption = net:active_encryption(),
161 frequency = net:frequency(),
162 channel = net:channel(),
163 signal = net:signal(),
164 quality = net:signal_percent(),
166 bitrate = net:bitrate(),
167 ifname = net:ifname(),
168 country = net:country(),
169 txpower = net:txpower(),
170 txpoweroff = net:txpower_offset(),
172 disabled = (dev:get("disabled") == "1" or
173 net:get("disabled") == "1")
183 function wifi_network(id)
184 local ntm = require "luci.model.network".init()
185 local net = ntm:get_wifinet(id)
187 local dev = net:get_device()
191 name = net:shortname(),
192 link = net:adminlink(),
194 mode = net:active_mode(),
195 ssid = net:active_ssid(),
196 bssid = net:active_bssid(),
197 encryption = net:active_encryption(),
198 frequency = net:frequency(),
199 channel = net:channel(),
200 signal = net:signal(),
201 quality = net:signal_percent(),
203 bitrate = net:bitrate(),
204 ifname = net:ifname(),
205 country = net:country(),
206 txpower = net:txpower(),
207 txpoweroff = net:txpower_offset(),
208 disabled = (dev:get("disabled") == "1" or
209 net:get("disabled") == "1"),
213 name = dev:get_i18n()
221 function wifi_assoclist()
222 local sys = require "luci.sys"
223 local ntm = require "luci.model.network".init()
224 local hosts = sys.net.host_hints()
227 local _, dev, net, bss
229 for _, dev in ipairs(ntm:get_wifidevs()) do
230 local radioname = dev:get_i18n()
232 for _, net in ipairs(dev:get_wifinets()) do
233 local netname = net:shortname()
234 local netlink = net:adminlink()
235 local ifname = net:ifname()
237 for _, bss in pairs(net:assoclist() or {}) do
238 local host = hosts[_]
242 bss.radio = radioname
246 bss.host_name = (host) and (host.name or host.ipv4 or host.ipv6)
247 bss.host_hint = (host and host.name and (host.ipv4 or host.ipv6)) and (host.ipv4 or host.ipv6)
249 assoc[#assoc+1] = bss
254 table.sort(assoc, function(a, b)
255 if a.radio ~= b.radio then
256 return a.radio < b.radio
257 elseif a.ifname ~= b.ifname then
258 return a.ifname < b.ifname
260 return a.bssid < b.bssid
267 function switch_status(devs)
270 for dev in devs:gmatch("[^%s,]+") do
272 local swc = io.popen("swconfig dev %s show"
273 % luci.util.shellquote(dev), "r")
280 local port, up = l:match("port:(%d+) link:(%w+)")
282 local speed = l:match(" speed:(%d+)")
283 local duplex = l:match(" (%w+)-duplex")
284 local txflow = l:match(" (txflow)")
285 local rxflow = l:match(" (rxflow)")
286 local auto = l:match(" (auto)")
289 port = tonumber(port) or 0,
290 speed = tonumber(speed) or 0,
292 duplex = (duplex == "full"),
293 rxflow = (not not rxflow),
294 txflow = (not not txflow),
295 auto = (not not auto)
302 switches[dev] = ports