* Removed High-Level UCI-API due to Lua compiler bugs

This commit is contained in:
Steven Barth 2008-06-06 20:45:33 +00:00
parent 8644ff1eab
commit cd0fb5e44e
5 changed files with 177 additions and 212 deletions

View file

@ -22,174 +22,171 @@ end
function configure_freifunk() function configure_freifunk()
local ip = luci.http.formvalue("ip") local ip = luci.http.formvalue("ip")
local uci = luci.model.uci local uci = luci.model.uci
local cfg = uci.config local cfg = {
wireless = uci.load("wireless"),
luci_fw = uci.load("luci_fw"),
luci_splash = uci.load("luci_splash"),
olsr = uci.load("olsr")
}
-- Configure FF-Interface -- Configure FF-Interface
uci.delete("network", "ff") uci.delete("network", "ff")
uci.delete("network", "ffdhcp") uci.delete("network", "ffdhcp")
cfg.network.ff = "interface" uci.section("network", "interface", "ff", {
cfg.network.ff.type = "bridge" type = "bridge",
cfg.network.ff.proto = "static" proto = "static",
cfg.network.ff.ipaddr = ip ipaddr = ip,
cfg.network.ff.netmask = cfg.freifunk.community.mask netmask = uci.get("freifunk", "community", "mask"),
cfg.network.ff.dns = cfg.freifunk.community.dns dns = uci.get("freifunk", "community", "dns")
})
-- Reset Routing -- Reset Routing
uci.foreach("luci_fw", "routing", uci.delete_all("luci_fw", "routing",
function (section) function (section)
if section.iface == "ff" or section.oface == "ff" then return (section.iface == "ff" or section.oface == "ff")
uci.delete("luci_fw", section[".name"])
end
end) end)
if cfg.luci_fw then if cfg.luci_fw then
cfg.luci_fw[""] = "routing" uci.section("luci_fw", "routing", nil, {
cfg.luci_fw[""].iface = "ff" iface = "ff",
cfg.luci_fw[""].oface = "ff" oface = "ff",
cfg.luci_fw[""].fwd = "1" fwd = "1"
})
end end
-- Routing from Internal -- Routing from Internal
local iface = luci.http.formvalue("frominternal") local iface = luci.http.formvalue("frominternal")
if iface and iface ~= "" then if iface and iface ~= "" then
uci.foreach("luci_fw", "routing", uci.delete_all("luci_fw", "routing",
function (section) function (section)
if section.iface == iface and section.oface == "ff" then return (section.iface == iface and section.oface == "ff")
uci.delete("luci_fw", section[".name"])
end
end) end)
if cfg.luci_fw then if cfg.luci_fw then
cfg.luci_fw[""] = "routing" uci.section("luci_fw", "routing", nil, {
cfg.luci_fw[""].iface = iface iface = iface,
cfg.luci_fw[""].oface = "ff" oface = "ff",
cfg.luci_fw[""].fwd = "1" fwd = "1",
cfg.luci_fw[""].nat = "1" nat = "1"
})
end end
end end
-- Routing to External -- Routing to External
local iface = luci.http.formvalue("toexternal") local iface = luci.http.formvalue("toexternal")
if iface and iface ~= "" then if iface and iface ~= "" then
uci.foreach("luci_fw", "routing", uci.delete_all("luci_fw", "routing",
function (section) function (section)
if section.oface == iface and section.iface == "ff" then return (section.oface == iface and section.iface == "ff")
uci.delete("luci_fw", section[".name"])
end
end) end)
if cfg.luci_fw then if cfg.luci_fw then
cfg.luci_fw[""] = "routing" uci.section("luci_fw", "routing", nil, {
cfg.luci_fw[""].oface = iface oface = iface,
cfg.luci_fw[""].iface = "ff" iface = "ff",
cfg.luci_fw[""].fwd = "1" fwd = "1",
cfg.luci_fw[""].nat = "1" nat = "1"
})
end end
end end
-- Configure DHCP -- Configure DHCP
if luci.http.formvalue("dhcp") then if luci.http.formvalue("dhcp") then
local dhcpnet = cfg.freifunk.community.dhcp:match("^([0-9]+)") local dhcpnet = uci.get("freifunk", "community", "dhcp"):match("^([0-9]+)")
local dhcpip = ip:gsub("^[0-9]+", dhcpnet) local dhcpip = ip:gsub("^[0-9]+", dhcpnet)
cfg.network.ffdhcp = "interface" uci.section("network", "interface", "ffdhcp", {
cfg.network.ffdhcp.proto = "static" proto = "static",
cfg.network.ffdhcp.ifname = "br-ff:dhcp" ifname = "br-ff:dhcp",
cfg.network.ffdhcp.ipaddr = dhcpip ipaddr = dhcpip,
cfg.network.ffdhcp.netmask = cfg.freifunk.community.dhcpmask netmask = uci.get("freifunk", "community", "dhcpmask")
})
uci.foreach("dhcp", "dhcp", uci.delete_all("dhcp", "dhcp",
function (section) function (section)
if section.interface == "ffdhcp" then return (section.interface == "ffdhcp")
uci.delete("dhcp", section[".name"])
end
end) end)
local dhcpbeg = 48 + tonumber(ip:match("[0-9]+$")) * 4 local dhcpbeg = 48 + tonumber(ip:match("[0-9]+$")) * 4
uci.section("dhcp", "dhcp", nil, {
cfg.dhcp[""] = "dhcp" interface = "ffdhcp",
cfg.dhcp[""].interface = "ffdhcp" start = dhcpbeg,
cfg.dhcp[""].start = dhcpbeg limit = ((dhcpbeg < 252) and 3 or 2),
cfg.dhcp[""].limit = (dhcpbeg < 252) and 3 or 2 leasetime = "30m"
cfg.dhcp[""].leasetime = "30m" })
uci.foreach("luci_splash", "iface", uci.delete_all("luci_splash", "iface",
function (section) function (section)
if section.network == "ffdhcp" then return (section.network == "ffdhcp")
uci.delete("luci_splash", section[".name"])
end
end) end)
if cfg.luci_splash then if cfg.luci_splash then
cfg.luci_splash[""] = "iface" uci.section("luci_splash", "iface", nil, {
cfg.luci_splash[""].network = "ffdhcp" network = "ffdhcp"
})
end end
uci.foreach("luci_fw", "routing", uci.delete_all("luci_fw", "routing",
function (section) function (section)
if section.iface == "ffdhcp" or section.oface == "ffdhcp" then return (section.iface == "ffdhcp" or section.oface == "ffdhcp")
uci.delete("luci_fw", section[".name"])
end
end) end)
if cfg.luci_fw then if cfg.luci_fw then
cfg.luci_fw[""] = "routing" uci.section("luci_fw", "routing", nil, {
cfg.luci_fw[""].iface = "ffdhcp" iface = "ffdhcp",
cfg.luci_fw[""].oface = "ff" oface = "ff",
cfg.luci_fw[""].nat = "1" nat = "1"
})
local iface = luci.http.formvalue("toexternal") local iface = luci.http.formvalue("toexternal")
if iface and iface ~= "" then if iface and iface ~= "" then
cfg.luci_fw[""] = "routing" uci.section("luci_fw", "routing", nil, {
cfg.luci_fw[""].iface = "ffdhcp" iface = "ffdhcp",
cfg.luci_fw[""].oface = iface oface = iface,
cfg.luci_fw[""].nat = "1" nat = "1"
})
end end
end end
end end
-- Configure OLSR -- Configure OLSR
if luci.http.formvalue("olsr") and cfg.olsr then if luci.http.formvalue("olsr") and cfg.olsr then
uci.foreach("olsr", "Interface", uci.delete_all("olsr", "Interface")
function (section) uci.delete_all("olsr", "LoadPlugin")
uci.delete("olsr", section[".name"])
end)
uci.foreach("olsr", "LoadPlugin",
function (section)
uci.delete("olsr", section[".name"])
end)
if luci.http.formvalue("shareinet") then if luci.http.formvalue("shareinet") then
cfg.olsr.dyn_gw = "LoadPlugin" uci.section("olsr", "LoadPlugin", "dyn_gw", {
cfg.olsr.dyn_gw.Library = "olsrd_dyn_gw.so.0.4" Library = "olsrd_dyn_gw.so.0.4"
})
end end
cfg.olsr.nameservice = "LoadPlugin" uci.section("olsr", "LoadPlugin", "nameservice", {
cfg.olsr.nameservice.Library = "olsrd_nameservice.so.0.3" Library = "olsrd_nameservice.so.0.3",
cfg.olsr.nameservice.name = ip:gsub("%.", "-") name = ip:gsub("%.", "-"),
cfg.olsr.nameservice.hosts_file = "/var/etc/hosts" hosts_file = "/var/etc/hosts",
cfg.olsr.nameservice.suffix = ".olsr" suffix = ".olsr",
cfg.olsr.nameservice.latlon_infile = "/tmp/latlon.txt" latlon_infile = "/tmp/latlon.txt"
})
cfg.olsr.txtinfo = "LoadPlugin" uci.section("olsr", "LoadPlugin", "txtinfo", {
cfg.olsr.txtinfo.Library = "olsrd_txtinfo.so.0.1" Library = "olsrd_txtinfo.so.0.1",
cfg.olsr.txtinfo.Accept = "127.0.0.1" Accept = "127.0.0.1"
})
cfg.olsr[""] = "Interface" uci.section("olsr", "Interface", nil, {
cfg.olsr[""].Interface = "ff" Interface = "ff",
cfg.olsr[""].HelloInterval = "6.0" HelloInterval = "6.0",
cfg.olsr[""].HelloValidityTime = "108.0" HelloValidityTime = "108.0",
cfg.olsr[""].TcInterval = "4.0" TcInterval = "4.0",
cfg.olsr[""].TcValidityTime = "324.0" TcValidityTime = "324.0",
cfg.olsr[""].MidInterval = "18.0" MidInterval = "18.0",
cfg.olsr[""].MidValidityTime = "324.0" MidValidityTime = "324.0",
cfg.olsr[""].HnaInterval = "18.0" HnaInterval = "18.0",
cfg.olsr[""].HnaValidityTime = "108.0" HnaValidityTime = "108.0"
})
end end
-- Configure Wifi -- Configure Wifi
@ -199,32 +196,34 @@ function configure_freifunk()
local device = section[".name"] local device = section[".name"]
if luci.http.formvalue("wifi."..iface) then if luci.http.formvalue("wifi."..iface) then
uci.foreach("wireless", "wifi-iface", uci.delete_all("wireless", "wifi-iface",
function (section) function (section)
if section.device == device then return (section.device == device)
uci.delete("wireless", section[".name"])
end
end) end)
end end
cfg.wireless[device].disabled = "0" uci.tset("wireless", "device", {
cfg.wireless[device].mode = "11g" disabled = "0",
cfg.wireless[device].txantenna = "1" mode = "11g",
cfg.wireless[device].rxantenna = "1" txantenna = "1",
cfg.wireless[device].channel = cfg.freifunk.community.channel rxantenna = "1",
channel = uci.get("freifunk", "community", "channel")
})
cfg.wireless[""] = "wifi-iface" uci.section("wireless", "wifi-iface", nil, {
cfg.wireless[""].device = iface device = iface,
cfg.wireless[""].network = "ff" network = "ff",
cfg.wireless[""].mode = "adhoc" mode = "adhoc",
cfg.wireless[""].ssid = cfg.freifunk.community.essid ssid = uci.get("freifunk", "community", "essid"),
cfg.wireless[""].bssid = cfg.freifunk.community.bssid bssid = uci.get("freifunk", "community", "bssid"),
cfg.wireless[""].txpower = 13 txpower = 13
})
end) end)
end end
-- Save UCI -- Save UCI
uci.save() uci.save()
luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes")) luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
end end

View file

@ -7,7 +7,6 @@ require("luci.model.uci")
-- Init state session -- Init state session
luci.model.uci.set_savedir(luci.model.uci.savedir_state) luci.model.uci.set_savedir(luci.model.uci.savedir_state)
local uci = luci.model.uci local uci = luci.model.uci
local cfg = uci.config
function main(argv) function main(argv)
@ -61,9 +60,10 @@ end
-- Add a lease to state and invoke add_rule -- Add a lease to state and invoke add_rule
function add_lease(mac) function add_lease(mac)
cfg.luci_splash[""] = "lease" uci.section("luci_splash", "lease", nil, {
cfg.luci_splash[""].mac = mac mac = mac,
cfg.luci_splash[""].start = os.time() start = os.time()
})
add_rule(mac) add_rule(mac)
uci.save() uci.save()
@ -148,9 +148,10 @@ function sync()
local leases = uci.get_all("luci_splash") local leases = uci.get_all("luci_splash")
-- Convert leasetime to seconds -- Convert leasetime to seconds
local leasetime = tonumber(cfg.luci_splash.general.leasetime) * 3600 local leasetime = tonumber(uci.get("luci_splash", "general", "leasetime")) * 3600
-- Clean state file -- Clean state file
uci.load("luci_splash")
uci.revert("luci_splash") uci.revert("luci_splash")
@ -162,9 +163,10 @@ function sync()
remove_rule(v.mac) remove_rule(v.mac)
else else
-- Rewrite state -- Rewrite state
cfg.luci_splash[""] = "lease" uci.section("luci_splash", "lease", nil, {
cfg.luci_splash[""].mac = v.mac mac = v.mac,
cfg.luci_splash[""].start = v.start start = v.start
})
written[v.mac:lower()] = 1 written[v.mac:lower()] = 1
end end
end end

View file

@ -25,92 +25,52 @@ limitations under the License.
]]-- ]]--
local uci = require("uci") local uci = require("uci")
local util = require("luci.util") local util = require("luci.util")
local setmetatable = setmetatable local setmetatable, rawget, rawset = setmetatable, rawget, rawset
local rawget = rawget local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
local rawset = rawset
local error = error
local tostring = tostring
module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end) module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
local configs_mt = {}
local sections_mt = {}
local options_mt = {}
savedir_default = "/tmp/.uci" savedir_default = "/tmp/.uci"
confdir_default = "/etc/config" confdir_default = "/etc/config"
savedir_state = "/var/state" savedir_state = "/var/state"
config = {} function delete_all(config, type, comparator)
setmetatable(config, configs_mt) local del = {}
-- Level 1 (configs) foreach(config, type,
function configs_mt.__index(self, key) function (section)
local node = rawget(self, key) if not comparator or comparator(section) then
if not node then table.insert(del, section[".name"])
if not uci.load(key) then end
return nil end)
end
node = {} for i, j in ipairs(del) do
node[".name"] = key uci.delete("config", j)
setmetatable(node, sections_mt)
rawset(self, key, node)
end end
return node
end
function configs_mt.__newindex()
error("invalid operation")
end end
function section(config, type, name, values)
-- Level 2 (sections) local stat = true
function sections_mt.__index(self, key) if name then
local node = rawget(self, key) stat = set(config, name, type)
if not node then
node = {}
node[".conf"] = self[".name"]
node[".name"] = key
node[".type"] = uci.get(self[".name"], key)
setmetatable(node, options_mt)
rawset(self, key, node)
end
return node
end
function sections_mt.__newindex(self, key, value)
if not value then
if uci.delete(self[".name"], key) then
rawset(self, key, nil)
else
error("unable to delete section")
end
elseif key == "" then
key = uci.add(self[".name"], tostring(value))
if key then
rawset(self, "", self[key])
else
error("unable to create section")
end
else else
if not uci.set(self[".name"], key, value) then name = add(config, type)
error("unable to create section") stat = name and true
end end
end
end if stat and values then
stat = tset(config, name, values)
end
-- Level 3 (options)
function options_mt.__index(self, key) return stat and name
return uci.get(self[".conf"], self[".name"], key) end
end
function options_mt.__newindex(self, key, value) function tset(config, section, values)
if not value then local stat = true
if not uci.delete(self[".conf"], self[".name"], key) then for k, v in pairs(values) do
error("unable to delete option") if k:sub(1, 1) ~= "." then
end stat = stat and set(config, section, k, v)
else
if not uci.set(self[".conf"], self[".name"], key, tostring(value)) then
error("unable to write option")
end end
end end
end end

View file

@ -120,9 +120,11 @@ end
<% <%
if "admin" == request[1] then if "admin" == request[1] then
local ucic = 0 local ucic = 0
for n, s in pairs(require("luci.model.uci").changes()) do for i, j in pairs(require("luci.model.uci").changes()) do
for no, o in pairs(s) do for k, l in pairs(j) do
ucic = ucic + 1; for m, n in pairs(l) do
ucic = ucic + 1;
end
end end
end end
%> %>

View file

@ -120,9 +120,11 @@ end
<% <%
if "admin" == request[1] then if "admin" == request[1] then
local ucic = 0 local ucic = 0
for n, s in pairs(require("luci.model.uci").changes()) do for i, j in pairs(require("luci.model.uci").changes()) do
for no, o in pairs(s) do for k, l in pairs(j) do
ucic = ucic + 1; for m, n in pairs(l) do
ucic = ucic + 1;
end
end end
end end
%> %>