2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.tools.webadmin", package.seeall)
17 local uci = require("luci.model.uci")
21 function byte_format(byte)
22 local suff = {"B", "KB", "MB", "GB", "TB"}
24 if byte > 1024 and i < 5 then
27 return string.format("%.2f %s", byte, suff[i])
32 function date_format(secs)
33 local suff = {"min", "h", "d"}
38 secs = math.floor(secs)
40 mins = math.floor(secs / 60)
45 hour = math.floor(mins / 60)
50 days = math.floor(hour / 24)
55 return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
57 return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
61 function network_get_addresses(net)
62 local state = uci.cursor_state()
65 local ipv4 = state:get("network", net, "ipaddr")
66 local mav4 = state:get("network", net, "netmask")
67 local ipv6 = state:get("network", net, "ip6addr")
69 if ipv4 and #ipv4 > 0 then
70 if mav4 and #mav4 == 0 then mav4 = nil end
72 ipv4 = luci.ip.IPv4(ipv4, mav4)
75 table.insert(addr, ipv4:string())
80 table.insert(addr, ipv6)
83 state:foreach("network", "alias",
85 if section.interface == net then
86 if section.ipaddr and section.netmask then
87 local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
90 table.insert(addr, ipv4:string())
94 if section.ip6addr then
95 table.insert(addr, section.ip6addr)
104 function cbi_add_networks(field)
105 uci.cursor():foreach("network", "interface",
107 if section[".name"] ~= "loopback" then
108 field:value(section[".name"])
112 field.titleref = luci.dispatcher.build_url("admin", "network", "network")
115 function cbi_add_knownips(field)
116 for i, dataset in ipairs(luci.sys.net.arptable()) do
117 field:value(dataset["IP address"])
121 function network_get_zones(net)
122 local state = uci.cursor_state()
123 if not state:load("firewall") then
129 state:foreach("firewall", "zone",
131 local znet = section.network or section.name
132 if luci.util.contains(luci.util.split(znet, " "), net) then
133 table.insert(zones, section.name)
141 function firewall_find_zone(name)
144 luci.model.uci.cursor():foreach("firewall", "zone",
146 if section.name == name then
147 find = section[".name"]
155 function iface_get_network(iface)
156 local state = uci.cursor_state()
157 state:load("network")
160 state:foreach("network", "interface",
162 local ifname = state:get(
163 "network", section[".name"], "ifname"
166 if iface == ifname then
167 net = section[".name"]