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
15 module("luci.controller.admin.network", package.seeall)
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
24 uci:foreach("network", "switch",
33 page = node("admin", "network")
34 page.target = alias("admin", "network", "network")
35 page.title = i18n("Network")
40 page = node("admin", "network", "vlan")
41 page.target = cbi("admin_network/vlan")
42 page.title = i18n("Switch")
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)
51 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
54 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
57 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
60 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
64 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
68 page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
71 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
74 uci:foreach("network", "interface",
76 local ifc = section[".name"]
77 if ifc ~= "loopback" then
78 entry({"admin", "network", "network", ifc},
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")
92 page = node("admin", "network", "hosts")
93 page.target = cbi("admin_network/hosts")
94 page.title = i18n("Hostnames")
97 page = node("admin", "network", "routes")
98 page.target = cbi("admin_network/routes")
99 page.title = i18n("Static Routes")
105 local function param(x)
106 return luci.http.formvalue(x)
109 local function ptable(x)
111 return x and (type(x) ~= "table" and { x } or x) or {}
114 local dev = param("device")
115 local ssid = param("join")
118 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
121 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
123 local cbi = require "luci.cbi"
124 local tpl = require "luci.template"
125 local map = luci.cbi.load("admin_network/wifi_add")[1]
127 if map:parse() ~= cbi.FORM_DONE then
134 luci.template.render("admin_network/wifi_join")
139 local dev = luci.http.formvalue("device")
140 local ntm = require "luci.model.network".init()
142 dev = dev and ntm:get_wifidev(dev)
145 local net = dev:add_wifinet({
152 luci.http.redirect(net:adminlink())
156 function wifi_delete(network)
157 local ntm = require "luci.model.network".init()
159 ntm:del_network(network)
162 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
167 luci.http.write("null")
168 elseif type(x) == "table" then
170 if type(next(x)) == "number" then
171 luci.http.write("[ ")
172 for k, v in ipairs(x) do
175 luci.http.write(", ")
178 luci.http.write(" ]")
180 luci.http.write("{ ")
181 for k, v in pairs(x) do
182 luci.http.write("%q: " % k)
185 luci.http.write(", ")
188 luci.http.write(" }")
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))
198 function iface_status()
199 local path = luci.dispatcher.context.requestpath
200 local x = luci.model.uci.cursor_state()
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+")
213 for _, info in ipairs(nixio.getifaddrs()) do
214 local name = info.name:match("[^:]+")
216 if info.family == "packet" then
217 data.flags = info.flags
218 data.stats = info.data
219 data.macaddr = info.addr
221 elseif info.family == "inet" then
222 data.ipaddrs = data.ipaddrs or { }
223 data.ipaddrs[#data.ipaddrs+1] = {
225 broadaddr = info.broadaddr,
226 dstaddr = info.dstaddr,
227 netmask = info.netmask,
230 elseif info.family == "inet6" then
231 data.ip6addrs = data.ip6addrs or { }
232 data.ip6addrs[#data.ip6addrs+1] = {
234 netmask = info.netmask,
247 luci.http.prepare_content("application/json")
252 luci.http.status(404, "No such device")
255 function wifi_status()
256 local path = luci.dispatcher.context.requestpath
260 for dev in path[#path]:gmatch("[%w%.%-]+") do
261 local iw = luci.sys.wifi.getiwinfo(dev)
266 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
267 "quality", "quality_max", "mode", "ssid", "bssid", "country",
268 "encryption", "ifname"
278 luci.http.prepare_content("application/json")
283 luci.http.status(404, "No such device")