* luci/modules/admin-core: fix typo/spec violation in network scheme
[project/luci.git] / modules / admin-core / luasrc / tools / webadmin.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
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
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.tools.webadmin", package.seeall)
17 local uci = require("luci.model.uci")
18 require("luci.sys")
19 require("luci.ip")
20
21 function byte_format(byte)
22         local suff = {"B", "KB", "MB", "GB", "TB"}
23         for i=1, 5 do
24                 if byte > 1024 and i < 5 then
25                         byte = byte / 1024
26                 else
27                         return string.format("%.2f %s", byte, suff[i]) 
28                 end 
29         end
30 end
31
32 function date_format(secs)
33         local suff = {"min", "h", "d"}
34         local mins = 0
35         local hour = 0
36         local days = 0
37         
38         secs = math.floor(secs)
39         if secs > 60 then
40                 mins = math.floor(secs / 60)
41                 secs = secs % 60
42         end
43         
44         if mins > 60 then
45                 hour = math.floor(mins / 60)
46                 mins = mins % 60
47         end
48         
49         if hour > 24 then
50                 days = math.floor(hour / 24)
51                 hour = hour % 24
52         end
53         
54         if days > 0 then
55                 return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
56         else
57                 return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
58         end
59 end
60
61 function network_get_addresses(net)
62         local state = uci.cursor_state()
63         state:load("network")
64         local addr = {}
65         local ipv4 = state:get("network", net, "ipaddr")
66         local mav4 = state:get("network", net, "netmask")
67         local ipv6 = state:get("network", net, "ip6addr")
68         
69         if ipv4 and mav4 then
70                 ipv4 = luci.ip.IPv4(ipv4, mav4)
71                 
72                 if ipv4 then 
73                         table.insert(addr, ipv4:string())
74                 end
75         end
76
77         if ipv6 then
78                 table.insert(addr, ipv6)
79         end
80         
81         state:foreach("network", "alias",
82                 function (section)
83                         if section.interface == net then
84                                 if section.ipaddr and section.netmask then
85                                         local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
86                                         
87                                         if ipv4 then
88                                                 table.insert(addr, ipv4:string())
89                                         end
90                                 end
91                                 
92                                 if section.ip6addr then
93                                         table.insert(addr, section.ip6addr)
94                                 end
95                         end
96                 end
97         )
98         
99         return addr
100 end
101
102 function cbi_add_networks(field)
103         uci.cursor():foreach("network", "interface",
104                 function (section)
105                         if section[".name"] ~= "loopback" then
106                                 field:value(section[".name"])
107                         end
108                 end
109         )
110         field.titleref = luci.dispatcher.build_url("admin", "network", "network")
111 end
112
113 function cbi_add_knownips(field)
114         for i, dataset in ipairs(luci.sys.net.arptable()) do
115                 field:value(dataset["IP address"])
116         end
117 end
118
119 function network_get_zones(net)
120         local state = uci.cursor_state()
121         if not state:load("firewall") then
122                 return nil
123         end
124         
125         local zones = {}
126         
127         state:foreach("firewall", "zone", 
128                 function (section)
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)
132                         end
133                 end
134         )
135         
136         return zones
137 end
138
139 function firewall_find_zone(name)
140         local find
141         
142         luci.model.uci.cursor():foreach("firewall", "zone", 
143                 function (section)
144                         if section.name == name then
145                                 find = section[".name"]
146                         end
147                 end
148         )
149         
150         return find
151 end
152
153 function iface_get_network(iface)
154         local state = uci.cursor_state()
155         state:load("network")
156         local net
157         
158         state:foreach("network", "interface",
159                 function (section)
160                         local ifname = state:get(
161                                 "network", section[".name"], "ifname"
162                         )
163                         
164                         if iface == ifname then
165                                 net = section[".name"]
166                         end
167                 end
168         )
169         
170         return net
171 end