modules/admin-full: fix usage of network model in network controller
[project/luci.git] / modules / admin-full / luasrc / controller / admin / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 module("luci.controller.admin.network", package.seeall)
16
17 function index()
18 require("luci.i18n")
19 local uci = require("luci.model.uci").cursor()
20 local i18n = luci.i18n.translate
21 local has_wifi = nixio.fs.stat("/etc/config/wireless")
22 local has_switch = false
23
24 uci:foreach("network", "switch",
25 function(s)
26 has_switch = true
27 return false
28 end
29 )
30
31 local page
32
33 page = node("admin", "network")
34 page.target = alias("admin", "network", "network")
35 page.title = i18n("Network")
36 page.order = 50
37 page.index = true
38
39 if has_switch then
40 page = node("admin", "network", "vlan")
41 page.target = cbi("admin_network/vlan")
42 page.title = i18n("Switch")
43 page.order = 20
44 end
45
46 if has_wifi and has_wifi.size > 0 then
47 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
48 page.leaf = true
49 page.subindex = true
50
51 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
52 page.leaf = true
53
54 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
55 page.leaf = true
56
57 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
58 page.leaf = true
59
60 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
61 page.leaf = true
62 end
63
64 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
65 page.leaf = true
66 page.subindex = true
67
68 page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
69 page.leaf = true
70
71 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
72 page.leaf = true
73
74 uci:foreach("network", "interface",
75 function (section)
76 local ifc = section[".name"]
77 if ifc ~= "loopback" then
78 entry({"admin", "network", "network", ifc},
79 true,
80 ifc:upper())
81 end
82 end
83 )
84
85 if nixio.fs.access("/etc/config/dhcp") then
86 page = node("admin", "network", "dhcpleases")
87 page.target = cbi("admin_network/dhcpleases")
88 page.title = i18n("DHCP Leases")
89 page.order = 30
90 end
91
92 page = node("admin", "network", "hosts")
93 page.target = cbi("admin_network/hosts")
94 page.title = i18n("Hostnames")
95 page.order = 40
96
97 page = node("admin", "network", "routes")
98 page.target = cbi("admin_network/routes")
99 page.title = i18n("Static Routes")
100 page.order = 50
101
102 end
103
104 function wifi_join()
105 local function param(x)
106 return luci.http.formvalue(x)
107 end
108
109 local function ptable(x)
110 x = param(x)
111 return x and (type(x) ~= "table" and { x } or x) or {}
112 end
113
114 local dev = param("device")
115 local ssid = param("join")
116
117 if dev and ssid then
118 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
119
120 if cancel then
121 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
122 else
123 local cbi = require "luci.cbi"
124 local tpl = require "luci.template"
125 local map = luci.cbi.load("admin_network/wifi_add")[1]
126
127 if map:parse() ~= cbi.FORM_DONE then
128 tpl.render("header")
129 map:render()
130 tpl.render("footer")
131 end
132 end
133 else
134 luci.template.render("admin_network/wifi_join")
135 end
136 end
137
138 function wifi_add()
139 local dev = luci.http.formvalue("device")
140 local ntm = require "luci.model.network".init()
141
142 dev = dev and ntm:get_wifidev(dev)
143
144 if dev then
145 local net = dev:add_wifinet({
146 mode = "ap",
147 ssid = "OpenWrt",
148 encryption = "none"
149 })
150
151 ntm:save("wireless")
152 luci.http.redirect(net:adminlink())
153 end
154 end
155
156 function wifi_delete(network)
157 local ntm = require "luci.model.network".init()
158
159 ntm:del_wifinet(network)
160 ntm:save("wireless")
161
162 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
163 end
164
165 function jsondump(x)
166 if x == nil then
167 luci.http.write("null")
168 elseif type(x) == "table" then
169 local k, v
170 if type(next(x)) == "number" then
171 luci.http.write("[ ")
172 for k, v in ipairs(x) do
173 jsondump(v)
174 if next(x, k) then
175 luci.http.write(", ")
176 end
177 end
178 luci.http.write(" ]")
179 else
180 luci.http.write("{ ")
181 for k, v in pairs(x) do
182 luci.http.write("%q: " % k)
183 jsondump(v)
184 if next(x, k) then
185 luci.http.write(", ")
186 end
187 end
188 luci.http.write(" }")
189 end
190 elseif type(x) == "number" or type(x) == "boolean" then
191 luci.http.write(tostring(x))
192 elseif type(x) == "string" then
193 luci.http.write("%q" % tostring(x))
194 end
195 end
196
197
198 function iface_status()
199 local path = luci.dispatcher.context.requestpath
200 local x = luci.model.uci.cursor_state()
201 local rv = { }
202
203 local iface
204 for iface in path[#path]:gmatch("[%w%.%-]+") do
205 local dev = x:get("network", iface, "device") or ""
206 if #dev == 0 or dev:match("^%d") or dev:match("%W") then
207 dev = x:get("network", iface, "ifname") or ""
208 dev = dev:match("%S+")
209 end
210
211 local info
212 local data = { }
213 for _, info in ipairs(nixio.getifaddrs()) do
214 local name = info.name:match("[^:]+")
215 if name == dev then
216 if info.family == "packet" then
217 data.flags = info.flags
218 data.stats = info.data
219 data.macaddr = info.addr
220 data.ifname = name
221 elseif info.family == "inet" then
222 data.ipaddrs = data.ipaddrs or { }
223 data.ipaddrs[#data.ipaddrs+1] = {
224 addr = info.addr,
225 broadaddr = info.broadaddr,
226 dstaddr = info.dstaddr,
227 netmask = info.netmask,
228 prefix = info.prefix
229 }
230 elseif info.family == "inet6" then
231 data.ip6addrs = data.ip6addrs or { }
232 data.ip6addrs[#data.ip6addrs+1] = {
233 addr = info.addr,
234 netmask = info.netmask,
235 prefix = info.prefix
236 }
237 end
238 end
239 end
240
241 if next(data) then
242 rv[#rv+1] = data
243 end
244 end
245
246 if #rv > 0 then
247 luci.http.prepare_content("application/json")
248 jsondump(rv)
249 return
250 end
251
252 luci.http.status(404, "No such device")
253 end
254
255 function wifi_status()
256 local path = luci.dispatcher.context.requestpath
257 local rv = { }
258
259 local dev
260 for dev in path[#path]:gmatch("[%w%.%-]+") do
261 local iw = luci.sys.wifi.getiwinfo(dev)
262 if iw then
263 local f
264 local j = { }
265 for _, f in ipairs({
266 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
267 "quality", "quality_max", "mode", "ssid", "bssid", "country",
268 "encryption", "ifname"
269 }) do
270 j[f] = iw[f]
271 end
272
273 rv[#rv+1] = j
274 end
275 end
276
277 if #rv > 0 then
278 luci.http.prepare_content("application/json")
279 jsondump(rv)
280 return
281 end
282
283 luci.http.status(404, "No such device")
284 end