summaryrefslogtreecommitdiffstats
path: root/opennhrp-model.lua
blob: 090b5415a7d2a73682ffdc18c6431933207c6547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
module(..., package.seeall)

-- Load libraries
require("modelfunctions")
require("validator")

-- Set variables
local configfile = "/etc/opennhrp/opennhrp.conf"
local processname = "opennhrp"
local packagename = "opennhrp"

--[[
local descr = {
	Type = {
	['incomplete']="The protocol address is being resolved",
	['negative']="This protocol address is not available",
	['cached']="Protocol address was resolved successfully",
	['route']="This is a dynamic shortcut",
	['dynamic']="This entry is from a node that connected to us",
	['local']="Local interface address",
	['static']="Static mapping from configuration file (e.g. address of core)",
	},
	Flags= {
--	['up']="Connection is fully usable",
	['lower-up']="ipsec connections is up, but registration is not yet done",
	},
}

-- ################################################################################
-- LOCAL FUNCTIONS

local function opennhrpctl_show()
	local cmd_output_result={}
	local cmd_output_result_table={}
	local cmd_output_error, opennhrpstatus
	local cmd = "/usr/sbin/opennhrpctl show 2>/dev/null"
	local f = io.popen( cmd )
	for line in f:lines() do
		if string.find(line, "^Status:") then
			opennhrpstatus=line
		else
			table.insert(cmd_output_result, line)
		end
	end
	f:close()
	local cnt = 0
	for k,v in pairs(cmd_output_result) do
		if string.find(v,"^Interface") then
			cnt = cnt + 1
			cmd_output_result_table[cnt] = {}
		end
		if ( cnt > 0 ) and (v ~= "") then
			local k = string.match(v,"^(.-):%s?.*")
			cmd_output_result_table[cnt][k]=cfe({value=string.match(v,"^.-:%s?(.*)")})
			local statusdescription = string.lower(string.match(v,"^.-:%s?(.*)"))
			if (type(descr[k]) == "table") then
				cmd_output_result_table[cnt][k]['descr']=descr[k][statusdescription]
			end
		end
	end

	local peers_list = {}
	for k,v in pairs(cmd_output_result_table) do
		if (v.Interface.value) and not (peers_list[v.Interface.value]) then
			peers_list[v.Interface.value] = {}
		end
		table.insert(peers_list[v.Interface.value], v)
	end

	return peers_list,opennhrpstatus,cmd_output_error
end
--]]

-- ################################################################################
-- PUBLIC FUNCTIONS

function startstop_service(action)
	return modelfunctions.startstop_service(processname, action)
end

function getstatus()
	return modelfunctions.getstatus(processname, packagename, "Opennhrp Status")
end

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 ""
	return show
end

function getconfig()
	return cfe({ type="table", value={}, label="Edit Config" })
end

function setconfig(config)
	return config
end

function getconfigfile()
	local filedetails = modelfunctions.getfiledetails(configfile)
	
	-- Validate

	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))
	end
	
	return filedetails
end