modules/admin-full: add requested network id in wifi status JSON response
[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
91 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
92 page.leaf = true
93 end
94
95 page = node("admin", "network", "hosts")
96 page.target = cbi("admin_network/hosts")
97 page.title = i18n("Hostnames")
98 page.order = 40
99
100 page = node("admin", "network", "routes")
101 page.target = cbi("admin_network/routes")
102 page.title = i18n("Static Routes")
103 page.order = 50
104
105 end
106
107 function wifi_join()
108 local function param(x)
109 return luci.http.formvalue(x)
110 end
111
112 local function ptable(x)
113 x = param(x)
114 return x and (type(x) ~= "table" and { x } or x) or {}
115 end
116
117 local dev = param("device")
118 local ssid = param("join")
119
120 if dev and ssid then
121 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
122
123 if cancel then
124 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
125 else
126 local cbi = require "luci.cbi"
127 local tpl = require "luci.template"
128 local map = luci.cbi.load("admin_network/wifi_add")[1]
129
130 if map:parse() ~= cbi.FORM_DONE then
131 tpl.render("header")
132 map:render()
133 tpl.render("footer")
134 end
135 end
136 else
137 luci.template.render("admin_network/wifi_join")
138 end
139 end
140
141 function wifi_add()
142 local dev = luci.http.formvalue("device")
143 local ntm = require "luci.model.network".init()
144
145 dev = dev and ntm:get_wifidev(dev)
146
147 if dev then
148 local net = dev:add_wifinet({
149 mode = "ap",
150 ssid = "OpenWrt",
151 encryption = "none"
152 })
153
154 ntm:save("wireless")
155 luci.http.redirect(net:adminlink())
156 end
157 end
158
159 function wifi_delete(network)
160 local ntm = require "luci.model.network".init()
161
162 ntm:del_wifinet(network)
163 ntm:save("wireless")
164
165 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
166 end
167
168 function iface_status()
169 local path = luci.dispatcher.context.requestpath
170 local x = luci.model.uci.cursor_state()
171 local rv = { }
172
173 local iface
174 for iface in path[#path]:gmatch("[%w%.%-]+") do
175 local dev = x:get("network", iface, "device") or ""
176 if #dev == 0 or dev:match("^%d") or dev:match("%W") then
177 dev = x:get("network", iface, "ifname") or ""
178 dev = dev:match("%S+")
179 end
180
181 local info
182 local data = { }
183 for _, info in ipairs(nixio.getifaddrs()) do
184 local name = info.name:match("[^:]+")
185 if name == dev then
186 if info.family == "packet" then
187 data.flags = info.flags
188 data.stats = info.data
189 data.macaddr = info.addr
190 data.ifname = name
191 elseif info.family == "inet" then
192 data.ipaddrs = data.ipaddrs or { }
193 data.ipaddrs[#data.ipaddrs+1] = {
194 addr = info.addr,
195 broadaddr = info.broadaddr,
196 dstaddr = info.dstaddr,
197 netmask = info.netmask,
198 prefix = info.prefix
199 }
200 elseif info.family == "inet6" then
201 data.ip6addrs = data.ip6addrs or { }
202 data.ip6addrs[#data.ip6addrs+1] = {
203 addr = info.addr,
204 netmask = info.netmask,
205 prefix = info.prefix
206 }
207 end
208 end
209 end
210
211 if next(data) then
212 rv[#rv+1] = data
213 end
214 end
215
216 if #rv > 0 then
217 luci.http.prepare_content("application/json")
218 luci.http.write_json(rv)
219 return
220 end
221
222 luci.http.status(404, "No such device")
223 end
224
225 function wifi_status()
226 local path = luci.dispatcher.context.requestpath
227 local rv = { }
228
229 local dev
230 for dev in path[#path]:gmatch("[%w%.%-]+") do
231 local iw = luci.sys.wifi.getiwinfo(dev)
232 if iw then
233 local f
234 local j = { id = dev }
235 for _, f in ipairs({
236 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
237 "quality", "quality_max", "mode", "ssid", "bssid", "country",
238 "encryption", "ifname"
239 }) do
240 j[f] = iw[f]
241 end
242
243 rv[#rv+1] = j
244 end
245 end
246
247 if #rv > 0 then
248 luci.http.prepare_content("application/json")
249 luci.http.write_json(rv)
250 return
251 end
252
253 luci.http.status(404, "No such device")
254 end
255
256 function lease_status()
257 local rv = { }
258 local leasefile = "/var/dhcp.leases"
259
260 local uci = require "luci.model.uci".cursor()
261 local nfs = require "nixio.fs"
262
263 uci:foreach("dhcp", "dnsmasq",
264 function(s)
265 if s.leasefile and nfs.access(s.leasefile) then
266 leasefile = s.leasefile
267 return false
268 end
269 end)
270
271 local fd = io.open(leasefile, "r")
272 if fd then
273 while true do
274 local ln = fd:read("*l")
275 if not ln then
276 break
277 else
278 local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
279 if ts and mac and ip and name then
280 rv[#rv+1] = {
281 expires = os.difftime(tonumber(ts) or 0, os.time()),
282 macaddr = mac,
283 ipaddr = ip,
284 hostname = (name ~= "*") and name
285 }
286 end
287 end
288 end
289 fd:close()
290 end
291
292 luci.http.prepare_content("application/json")
293 luci.http.write_json(rv)
294 end