summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2008-07-07 17:14:08 +0000
committerTed Trask <ttrask01@yahoo.com>2008-07-07 17:14:08 +0000
commite87110a3692c44b508e017809bc0d9582d72295d (patch)
treec9c7fd840c7bb5fe15cfd37f1a7f7d612f8b62d0
parent980dc13e790094b67c900fab60adddf275d343b8 (diff)
downloadacf-opennhrp-e87110a3692c44b508e017809bc0d9582d72295d.tar.bz2
acf-opennhrp-e87110a3692c44b508e017809bc0d9582d72295d.tar.xz
Implemented opennhrp config and validation.
git-svn-id: svn://svn.alpinelinux.org/acf/opennhrp/trunk@1286 ab2d0c66-481e-0410-8bed-d214d4d58bed
-rw-r--r--opennhrp-config-html.lsp3
-rw-r--r--opennhrp-controller.lua4
-rw-r--r--opennhrp-model.lua147
3 files changed, 136 insertions, 18 deletions
diff --git a/opennhrp-config-html.lsp b/opennhrp-config-html.lsp
index d079d72..509236d 100644
--- a/opennhrp-config-html.lsp
+++ b/opennhrp-config-html.lsp
@@ -16,7 +16,8 @@ end ?>
<H1>Config</H1>
<?
form.action = page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action
- displayform(form)
+ local order = {"outgoinginterface", "peers", "publishedinterfaces"}
+ displayform(form, order)
?>
<? if viewlibrary and viewlibrary.dispatch_component then
diff --git a/opennhrp-controller.lua b/opennhrp-controller.lua
index 7c18cf4..aab365d 100644
--- a/opennhrp-controller.lua
+++ b/opennhrp-controller.lua
@@ -17,9 +17,9 @@ function startstop(self)
end
function config(self)
- return controllerfunctions.handle_form(self, self.model.getconfig, self.model.setconfig, self.clientdata, "Save", "Edit Config", "Configuration Set")
+ return controllerfunctions.handle_form(self, function() return self.model:getconfig() end, function(value) return self.model:setconfig(value) end, self.clientdata, "Save", "Edit Config", "Configuration Set")
end
function expert (self)
- return controllerfunctions.handle_form(self, self.model.getconfigfile, self.model.setconfigfile, self.clientdata, "Save", "Edit Config", "Configuration Set")
+ return controllerfunctions.handle_form(self, function() return self.model:getconfigfile() end, function(value) return self.model:setconfigfile(value) end, self.clientdata, "Save", "Edit Config", "Configuration Set")
end
diff --git a/opennhrp-model.lua b/opennhrp-model.lua
index 090b541..408c4a9 100644
--- a/opennhrp-model.lua
+++ b/opennhrp-model.lua
@@ -71,6 +71,112 @@ local function opennhrpctl_show()
end
--]]
+local function parseconfigfile(self, configfile)
+ if fs.is_file(configfile) then
+ configfile = fs.read_file(configfile)
+ end
+
+ -- Get the list of available interfaces
+ local interfaces = self:new("alpine-baselayout/interfaces")
+ local availableinterfaces = interfaces:read()
+ local listinterfaces = {}
+ local reverseinterfaces = {}
+ for i,int in ipairs(availableinterfaces.value) do
+ listinterfaces[#listinterfaces + 1] = int.value.name.value
+ reverseinterfaces[int.value.name.value] = #listinterfaces
+ end
+ interfaces:destroy()
+
+ local outgoinginterface = cfe({ type="select", label="Outgoing Interface", option=listinterfaces })
+ local publishedinterfaces = cfe({ type="multi", value={}, label="Published Interfaces", option=listinterfaces })
+ local peers = cfe({ type="list", value={}, label="Registered Peers", descr="protocol-address[/prefix] nbma-address" })
+ local currentinterface = ""
+ local words = {}
+ for word in configfile:gmatch("%S+") do words[#words + 1] = word end
+ for i,word in ipairs(words) do
+ if word == "interface" then
+ currentinterface = words[i+1]
+ elseif word == "map" then
+ if outgoinginterface.value ~= "" and outgoinginterface.value ~= currentinterface then
+ outgoinginterface.errtxt = "Multiple outgoing interfaces not supported"
+ else
+ outgoinginterface.value = currentinterface
+ peers.value[#peers.value + 1] = words[i+1] .. " " .. words[i+2]
+ end
+ elseif word == "shortcut-destination" then
+ publishedinterfaces.value[#publishedinterfaces.value + 1] = currentinterface
+ end
+ end
+
+ return cfe({ type="group", value={outgoinginterface=outgoinginterface, peers=peers, publishedinterfaces=publishedinterfaces}, label="OpenNHRP Config" })
+end
+
+local function generateconfigfile(config)
+ local filelines = {}
+ filelines[#filelines + 1] = "interface " .. config.value.outgoinginterface.value
+ for i,value in ipairs(config.value.peers.value) do
+ filelines[#filelines + 1] = " map " .. value .. " register"
+ end
+ filelines[#filelines + 1] = " shortcut"
+ filelines[#filelines + 1] = " redirect"
+ filelines[#filelines + 1] = " non-caching"
+ filelines[#filelines + 1] = ""
+ for i,value in ipairs(config.value.publishedinterfaces.value) do
+ filelines[#filelines + 1] = "interface " .. value
+ filelines[#filelines + 1] = " shortcut-destination"
+ filelines[#filelines + 1] = ""
+ end
+
+ return table.concat(filelines, "\n")
+end
+
+local function validateconfig(config)
+ local success = true
+ if not modelfunctions.validateselect(config.value.outgoinginterface) then
+ success = false
+ end
+ if not modelfunctions.validatemulti(config.value.publishedinterfaces) then
+ success = false
+ end
+ -- make sure don't try to publish the outgoing interface
+ for i,value in ipairs(config.value.publishedinterfaces.value) do
+ if value == config.value.outgoinginterface.value then
+ success = false
+ config.value.publishedinterfaces.errtxt = "Cannot publish outgoing interface"
+ break
+ end
+ end
+ for i,value in ipairs(config.value.peers.value) do
+ if not validator.is_ipv4(value:match("[^%s/]+")) or not validator.is_ipv4(value:match("%s(%S+)")) then
+ success = false
+ config.value.peers.errtxt = "Syntax error - invalid ip address"
+ break
+ elseif value:match("/(%d*)") and not validator.is_integer_in_range(value:match("/(%d*)"), 1, 31) then
+ success = false
+ config.value.peers.errtxt = "Syntax error - invalid prefix"
+ break
+ end
+ end
+ return success, config
+end
+
+local function validateconfigfile(self, configfile)
+ local config = parseconfigfile(self, configfile.value.filecontent.value)
+ local success, config = validateconfig(config)
+ if not success then
+ local errtxt = {}
+ for name,value in pairs(config.value) do
+ if value.errtxt then
+ errtxt[#errtxt + 1] = name .. " - " .. value.errtxt
+ end
+ end
+ if #errtxt then
+ configfile.value.filecontent.errtxt = table.concat(errtxt, "\n")
+ end
+ end
+ return success, configfile
+end
+
-- ################################################################################
-- PUBLIC FUNCTIONS
@@ -86,33 +192,44 @@ function getshowreport()
local show = cfe({ type="longtext", label="OpenNHRP show report" })
local cmd = "/usr/sbin/opennhrpctl show 2>/dev/null"
local f = io.popen( cmd )
- show.value = f:read("*a") or ""
+ show.value = f:read("*a")
+ if not show.value or show.value == "" then
+ show.value = "No report available"
+ end
return show
end
-function getconfig()
- return cfe({ type="table", value={}, label="Edit Config" })
+function getconfig(self)
+ local config = parseconfigfile(self, configfile)
+ local result, config = validateconfig(config)
+ return config
end
-function setconfig(config)
+function setconfig(self, config)
+ local success, config = validateconfig(config)
+ if success then
+ fs.write_file(configfile, generateconfigfile(config))
+ else
+ config.errtxt = "Failed to set configuration"
+ end
return config
end
-function getconfigfile()
+function getconfigfile(self)
local filedetails = modelfunctions.getfiledetails(configfile)
-
- -- Validate
-
+ local result, filedetails = validateconfigfile(self, filedetails)
return filedetails
end
-function setconfigfile(filedetails)
- -- Validate
- --local result, filedetails = validateconfigfile(filedetails)
- local result = true
-
- if result then
- fs.write_file(configfile, format.dostounix(filedetails.value.filecontent.value))
+function setconfigfile(self, filedetails)
+ filedetails.value.filename.value = configfile
+ filedetails.value.filecontent.value = string.gsub(format.dostounix(filedetails.value.filecontent.value), "\n+$", "")
+ local success, filedetails = validateconfigfile(self, filedetails)
+ if success then
+ fs.write_file(configfile, filedetails.value.filecontent.value)
+ filedetails = getconfigfile(self)
+ else
+ filedetails.errtxt = "Failed to set configuration file"
end
return filedetails