1 module("luci.controller.olsr", package.seeall)
4 if not nixio.fs.access("/etc/config/olsrd") then
8 require("luci.i18n").loadc("olsr")
9 local i18n = luci.i18n.translate
11 local page = node("admin", "status", "olsr")
12 page.target = call("action_index")
17 local page = node("admin", "status", "olsr", "routes")
18 page.target = call("action_routes")
19 page.title = i18n("Routen")
22 local page = node("admin", "status", "olsr", "topology")
23 page.target = call("action_topology")
24 page.title = i18n("Topologie")
27 local page = node("admin", "status", "olsr", "hna")
28 page.target = call("action_hna")
32 local page = node("admin", "status", "olsr", "mid")
33 page.target = call("action_mid")
38 {"admin", "services", "olsrd"},
39 cbi("olsr/olsrd"), "OLSR"
45 {"admin", "services", "olsrd", "iface"},
46 cbi("olsr/olsrdiface")
50 {"admin", "services", "olsrd", "hna"},
51 cbi("olsr/olsrdhna"), "HNA Announcements"
55 {"admin", "services", "olsrd", "plugins"},
56 cbi("olsr/olsrdplugins"), "Plugins"
62 local uci = require("luci.model.uci").cursor()
63 uci:foreach("olsrd", "LoadPlugin",
65 local lib = section.library
67 {"admin", "services", "olsrd", "plugins", lib },
68 cbi("olsr/olsrdplugins"),
69 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
75 function action_index()
76 local data = fetch_txtinfo("links")
78 if not data or not data.Links then
79 luci.template.render("status-olsr/error_olsr")
83 local function compare(a, b)
84 local c = tonumber(a.Cost)
85 local d = tonumber(b.Cost)
87 if not c or c == 0 then
91 if not d or d == 0 then
98 table.sort(data.Links, compare)
100 luci.template.render("status-olsr/index", {links=data.Links})
103 function action_routes()
104 local data = fetch_txtinfo("routes")
106 if not data or not data.Routes then
107 luci.template.render("status-olsr/error_olsr")
111 local function compare(a, b)
112 local c = tonumber(a.ETX)
113 local d = tonumber(b.ETX)
115 if not c or c == 0 then
119 if not d or d == 0 then
126 table.sort(data.Routes, compare)
128 luci.template.render("status-olsr/routes", {routes=data.Routes})
131 function action_topology()
132 local data = fetch_txtinfo("topology")
134 if not data or not data.Topology then
135 luci.template.render("status-olsr/error_olsr")
139 local function compare(a, b)
140 return a["Dest. IP"] < b["Dest. IP"]
143 table.sort(data.Topology, compare)
145 luci.template.render("status-olsr/topology", {routes=data.Topology})
148 function action_hna()
149 local data = fetch_txtinfo("hna")
151 if not data or not data.HNA then
152 luci.template.render("status-olsr/error_olsr")
156 local function compare(a, b)
157 return a.Destination < b.Destination
160 table.sort(data.HNA, compare)
162 luci.template.render("status-olsr/hna", {routes=data.HNA})
165 function action_mid()
166 local data = fetch_txtinfo("mid")
168 if not data or not data.MID then
169 luci.template.render("status-olsr/error_olsr")
173 local function compare(a, b)
174 return a["IP address"] < b["IP address"]
177 table.sort(data.MID, compare)
179 luci.template.render("status-olsr/mid", {mids=data.MID})
182 function action_smartgw()
183 local data = fetch_txtinfo("gateways")
185 if not data or not data.Gateways then
186 luci.template.render("status-olsr/error_olsr")
190 local function compare(a, b)
191 return a["ETX"] < b["ETX"]
194 table.sort(data.Gateways, compare)
196 luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
202 function fetch_txtinfo(otable)
204 local uci = require "luci.model.uci".cursor_state()
205 otable = otable or ""
206 local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
207 local rawdatav6 = luci.sys.httpget("http://[::1]:2006/"..otable)
212 if #rawdata ~= 0 then
213 local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
215 for i, tbl in ipairs(tables) do
216 local lines = luci.util.split(tbl, "\r?\n", nil, true)
217 name = table.remove(lines, 1):sub(8)
218 local keys = luci.util.split(table.remove(lines, 1), "\t")
219 local split = #keys - 1
220 if not data[name] then
224 for j, line in ipairs(lines) do
225 dataindex = ( dataindex + 1 )
227 local fields = luci.util.split(line, "\t", split)
229 for k, key in pairs(keys) do
230 if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
231 hostname = nixio.getnameinfo(fields[k], "inet")
233 data[name][di][key] = fields[k]
234 data[name][di]["Hostname"] = hostname
236 data[name][di][key] = fields[k]
238 elseif key == "Local IP" then
239 data[name][di][key] = fields[k]
240 data[name][di]['Local Device'] = fields[k]
241 uci:foreach("network", "interface",
243 localip = string.gsub(fields[k], ' ', '')
244 if s.ipaddr == localip then
245 data[name][di]['Local Device'] = s['.name'] or interface
248 elseif key == "Interface" then
249 data[name][di][key] = fields[k]
250 uci:foreach("network", "interface",
252 interface = string.gsub(fields[k], ' ', '')
253 if s.ifname == interface then
254 data[name][di][key] = s['.name'] or interface
258 data[name][di][key] = fields[k]
261 if data[name][di].Linkcost then
262 data[name][di].LinkQuality,
265 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
271 if #rawdatav6 ~= 0 then
272 local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
273 for i, tbl in ipairs(tables) do
274 local lines = luci.util.split(tbl, "\r?\n", nil, true)
275 name = table.remove(lines, 1):sub(8)
276 local keys = luci.util.split(table.remove(lines, 1), "\t")
277 local split = #keys - 1
278 if not data[name] then
281 for j, line in ipairs(lines) do
282 dataindex = ( dataindex + 1 )
284 local fields = luci.util.split(line, "\t", split)
286 for k, key in pairs(keys) do
287 if key == "Remote IP" then
288 hostname = nixio.getnameinfo(fields[k], "inet6")
290 data[name][di][key] = "[" .. fields[k] .. "]"
291 data[name][di]["Hostname"] = hostname
293 data[name][di][key] = "[" .. fields[k] .. "]"
295 elseif key == "Local IP" then
296 data[name][di][key] = fields[k]
297 data[name][di]['Local Device'] = fields[k]
298 uci:foreach("network", "interface",
300 local localip = string.gsub(fields[k], ' ', '')
302 local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
303 if ip6addr == localip then
304 data[name][di]['Local Device'] = s['.name'] or s.interface
308 elseif key == "Dest. IP" then
309 data[name][di][key] = "[" .. fields[k] .. "]"
310 elseif key == "Last hop IP" then
311 data[name][di][key] = "[" .. fields[k] .. "]"
312 elseif key == "IP address" then
313 data[name][di][key] = "[" .. fields[k] .. "]"
314 elseif key == "Gateway" then
315 data[name][di][key] = "[" .. fields[k] .. "]"
317 data[name][di][key] = fields[k]
321 if data[name][di].Linkcost then
322 data[name][di].LinkQuality,
325 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")