From e87110a3692c44b508e017809bc0d9582d72295d Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Mon, 7 Jul 2008 17:14:08 +0000 Subject: Implemented opennhrp config and validation. git-svn-id: svn://svn.alpinelinux.org/acf/opennhrp/trunk@1286 ab2d0c66-481e-0410-8bed-d214d4d58bed --- opennhrp-model.lua | 147 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 132 insertions(+), 15 deletions(-) (limited to 'opennhrp-model.lua') 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 -- cgit v1.2.3