57941c966d0b067d53e8b4e8e67e1f9d4379c835
[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 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 network_get_addresses(net)
33 local addr = {}
34 local ipv4 = luci.model.uci.get_statevalue("network", net, "ipaddr")
35 local mav4 = luci.model.uci.get_statevalue("network", net, "netmask")
36 local ipv6 = luci.model.uci.get_statevalue("network", net, "ip6addr")
37
38 if ipv4 and mav4 then
39 ipv4 = luci.ip.IPv4(ipv4, mav4)
40
41 if ipv4 then
42 table.insert(addr, ipv4:string())
43 end
44 end
45
46 if ipv6 then
47 table.insert(addr, ipv6)
48 end
49
50 luci.model.uci.foreach("network", "alias",
51 function (section)
52 if section.interface == net then
53 if section.ipaddr and section.netmask then
54 local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
55
56 if ipv4 then
57 table.insert(addr, ipv4:string())
58 end
59 end
60
61 if section.ip6addr then
62 table.insert(addr, section.ip6addr)
63 end
64 end
65 end
66 )
67
68 return addr
69 end
70
71 function cbi_add_networks(field)
72 luci.model.uci.foreach("network", "interface",
73 function (section)
74 if section[".name"] ~= "loopback" then
75 field:value(section[".name"])
76 end
77 end
78 )
79 field.titleref = luci.dispatcher.build_url("admin", "network", "network")
80 end
81
82 function cbi_add_knownips(field)
83 for i, dataset in ipairs(luci.sys.net.arptable()) do
84 field:value(dataset["IP address"])
85 end
86 end
87
88 function network_get_zones(net)
89 if not luci.model.uci.load("firewall") then
90 return nil
91 end
92
93 local zones = {}
94
95 luci.model.uci.foreach("firewall", "zone",
96 function (section)
97 local znet = section.network or section.name
98 if luci.util.contains(luci.util.split(znet, " "), net) then
99 table.insert(zones, section.name)
100 end
101 end
102 )
103
104 return zones
105 end
106
107 function firewall_find_zone(name)
108 local find
109
110 luci.model.uci.foreach("firewall", "zone",
111 function (section)
112 if section.name == name then
113 find = section[".name"]
114 end
115 end
116 )
117
118 return find
119 end
120
121 function iface_get_network(iface)
122 local net
123
124 luci.model.uci.foreach("network", "interface",
125 function (section)
126 local ifname = luci.model.uci.get_statevalue(
127 "network", section[".name"], "ifname"
128 )
129
130 if iface == ifname then
131 net = section[".name"]
132 end
133 end
134 )
135
136 return net
137 end