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")
70 ipv4 = luci.ip.IPv4(ipv4, mav4)
73 table.insert(addr, ipv4:string())
78 table.insert(addr, ipv6)
81 state:foreach("network", "alias",
83 if section.interface == net then
84 if section.ipaddr and section.netmask then
85 local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
88 table.insert(addr, ipv4:string())
92 if section.ip6addr then
93 table.insert(addr, section.ip6addr)
102 function cbi_add_networks(field)
103 uci.cursor():foreach("network", "interface",
105 if section[".name"] ~= "loopback" then
106 field:value(section[".name"])
110 field.titleref = luci.dispatcher.build_url("admin", "network", "network")
113 function cbi_add_knownips(field)
114 for i, dataset in ipairs(luci.sys.net.arptable()) do
115 field:value(dataset["IP address"])
119 function network_get_zones(net)
120 local state = uci.cursor_state()
121 if not state:load("firewall") then
127 state:foreach("firewall", "zone",
129 local znet = section.network or section.name
130 if luci.util.contains(luci.util.split(znet, " "), net) then
131 table.insert(zones, section.name)
139 function firewall_find_zone(name)
142 luci.model.uci.cursor():foreach("firewall", "zone",
144 if section.name == name then
145 find = section[".name"]
153 function iface_get_network(iface)
154 local state = uci.cursor_state()
155 state:load("network")
158 state:foreach("network", "interface",
160 local ifname = state:get(
161 "network", section[".name"], "ifname"
164 if iface == ifname then
165 net = section[".name"]