2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
14 module("luci.controller.admin.network", package.seeall)
18 local uci = require("luci.model.uci").cursor()
19 local i18n = luci.i18n.translate
20 local has_wifi = nixio.fs.stat("/etc/config/wireless")
21 local has_switch = false
23 uci:foreach("network", "switch",
32 page = node("admin", "network")
33 page.target = alias("admin", "network", "network")
34 page.title = i18n("Network")
39 page = node("admin", "network", "vlan")
40 page.target = cbi("admin_network/vlan")
41 page.title = i18n("Switch")
45 if has_wifi and has_wifi.size > 0 then
46 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
50 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
53 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
56 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
59 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
63 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
67 page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
70 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
73 uci:foreach("network", "interface",
75 local ifc = section[".name"]
76 if ifc ~= "loopback" then
77 entry({"admin", "network", "network", ifc},
84 if nixio.fs.access("/etc/config/dhcp") then
85 page = node("admin", "network", "dhcpleases")
86 page.target = cbi("admin_network/dhcpleases")
87 page.title = i18n("DHCP Leases")
91 page = node("admin", "network", "hosts")
92 page.target = cbi("admin_network/hosts")
93 page.title = i18n("Hostnames")
96 page = node("admin", "network", "routes")
97 page.target = cbi("admin_network/routes")
98 page.title = i18n("Static Routes")
104 local function param(x)
105 return luci.http.formvalue(x)
108 local function ptable(x)
110 return x and (type(x) ~= "table" and { x } or x) or {}
113 local dev = param("device")
114 local ssid = param("join")
117 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
120 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
122 local cbi = require "luci.cbi"
123 local tpl = require "luci.template"
124 local map = luci.cbi.load("admin_network/wifi_add")[1]
126 if map:parse() ~= cbi.FORM_DONE then
133 luci.template.render("admin_network/wifi_join")
138 local dev = luci.http.formvalue("device")
139 local uci = require "luci.model.uci".cursor()
140 local wlm = require "luci.model.wireless"
145 local net = wlm:add_network({
153 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless", dev, net:name()))
157 function wifi_delete(network)
158 local uci = require "luci.model.uci".cursor()
159 local wlm = require "luci.model.wireless"
162 wlm:del_network(network)
165 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
170 luci.http.write("null")
171 elseif type(x) == "table" then
173 if type(next(x)) == "number" then
174 luci.http.write("[ ")
175 for k, v in ipairs(x) do
178 luci.http.write(", ")
181 luci.http.write(" ]")
183 luci.http.write("{ ")
184 for k, v in pairs(x) do
185 luci.http.write("%q: " % k)
188 luci.http.write(", ")
191 luci.http.write(" }")
193 elseif type(x) == "number" or type(x) == "boolean" then
194 luci.http.write(tostring(x))
195 elseif type(x) == "string" then
196 luci.http.write("%q" % tostring(x))
201 function iface_status()
202 local path = luci.dispatcher.context.requestpath
203 local iface = path[#path]
206 local x = luci.model.uci.cursor_state()
208 local dev = x:get("network", iface, "device") or ""
209 if #dev == 0 or dev:match("^%d") or dev:match("%W") then
210 dev = x:get("network", iface, "ifname") or ""
211 dev = dev:match("%S+")
214 for _, info in ipairs(nixio.getifaddrs()) do
215 local name = info.name:match("[^:]+")
217 if info.family == "packet" then
218 data.flags = info.flags
219 data.stats = info.data
220 data.macaddr = info.addr
222 elseif info.family == "inet" then
223 data.ipaddrs = data.ipaddrs or { }
224 data.ipaddrs[#data.ipaddrs+1] = {
226 broadaddr = info.broadaddr,
227 dstaddr = info.dstaddr,
228 netmask = info.netmask,
231 elseif info.family == "inet6" then
232 data.ip6addrs = data.ip6addrs or { }
233 data.ip6addrs[#data.ip6addrs+1] = {
235 netmask = info.netmask,
243 luci.http.prepare_content("application/json")
248 luci.http.status(404, "No such device")
251 function wifi_status()
252 local path = luci.dispatcher.context.requestpath
253 local dev = path[#path]
254 local iw = luci.sys.wifi.getiwinfo(dev)
260 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
261 "quality", "quality_max", "mode", "ssid", "bssid", "country",
262 "encryption", "ifname"
267 luci.http.prepare_content("application/json")
272 luci.http.status(404, "No such device")