1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
5 module("luci.tools.webadmin", package.seeall)
6 local uci = require("luci.model.uci")
10 function byte_format(byte)
11 local suff = {"B", "KB", "MB", "GB", "TB"}
13 if byte > 1024 and i < 5 then
16 return string.format("%.2f %s", byte, suff[i])
21 function date_format(secs)
22 local suff = {"min", "h", "d"}
27 secs = math.floor(secs)
29 mins = math.floor(secs / 60)
34 hour = math.floor(mins / 60)
39 days = math.floor(hour / 24)
44 return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
46 return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
50 function network_get_addresses(net)
51 local state = uci.cursor_state()
54 local ipv4 = state:get("network", net, "ipaddr")
55 local mav4 = state:get("network", net, "netmask")
56 local ipv6 = state:get("network", net, "ip6addr")
58 if ipv4 and #ipv4 > 0 then
59 if mav4 and #mav4 == 0 then mav4 = nil end
61 ipv4 = luci.ip.IPv4(ipv4, mav4)
64 table.insert(addr, ipv4:string())
69 table.insert(addr, ipv6)
72 state:foreach("network", "alias",
74 if section.interface == net then
75 if section.ipaddr and section.netmask then
76 local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
79 table.insert(addr, ipv4:string())
83 if section.ip6addr then
84 table.insert(addr, section.ip6addr)
93 function cbi_add_networks(field)
94 uci.cursor():foreach("network", "interface",
96 if section[".name"] ~= "loopback" then
97 field:value(section[".name"])
101 field.titleref = luci.dispatcher.build_url("admin", "network", "network")
104 function cbi_add_knownips(field)
105 for i, dataset in ipairs(luci.sys.net.arptable()) do
106 field:value(dataset["IP address"])
110 function network_get_zones(net)
111 local state = uci.cursor_state()
112 if not state:load("firewall") then
118 state:foreach("firewall", "zone",
120 local znet = section.network or section.name
121 if luci.util.contains(luci.util.split(znet, " "), net) then
122 table.insert(zones, section.name)
130 function firewall_find_zone(name)
133 luci.model.uci.cursor():foreach("firewall", "zone",
135 if section.name == name then
136 find = section[".name"]
144 function iface_get_network(iface)
145 local state = uci.cursor_state()
146 state:load("network")
149 state:foreach("network", "interface",
151 local ifname = state:get(
152 "network", section[".name"], "ifname"
155 if iface == ifname then
156 net = section[".name"]