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 = template("status-olsr/overview")
13 page.title = i18n("OLSR")
17 local page = node("admin", "status", "olsr", "neighbors")
18 page.target = call("action_neigh")
19 page.title = i18n("Neighbours")
23 local page = node("admin", "status", "olsr", "routes")
24 page.target = call("action_routes")
25 page.title = i18n("Routes")
28 local page = node("admin", "status", "olsr", "topology")
29 page.target = call("action_topology")
30 page.title = i18n("Topology")
33 local page = node("admin", "status", "olsr", "hna")
34 page.target = call("action_hna")
35 page.title = i18n("HNA")
38 local page = node("admin", "status", "olsr", "mid")
39 page.target = call("action_mid")
40 page.title = i18n("MID")
43 local page = node("admin", "status", "olsr", "smartgw")
44 page.target = call("action_smartgw")
45 page.title = i18n("SmartGW")
48 local page = node("admin", "status", "olsr", "interfaces")
49 page.target = call("action_interfaces")
50 page.title = i18n("Interfaces")
54 {"admin", "services", "olsrd"},
55 cbi("olsr/olsrd"), "OLSR"
61 {"admin", "services", "olsrd", "iface"},
62 cbi("olsr/olsrdiface")
66 {"admin", "services", "olsrd", "hna"},
67 cbi("olsr/olsrdhna"), i18n("HNA Announcements")
71 {"admin", "services", "olsrd", "plugins"},
72 cbi("olsr/olsrdplugins"), i18n("Plugins")
76 {"admin", "services", "olsrd", "display"},
77 cbi("olsr/olsrddisplay"), i18n("Display")
84 local uci = require("luci.model.uci").cursor()
85 uci:foreach("olsrd", "LoadPlugin",
87 local lib = section.library
89 {"admin", "services", "olsrd", "plugins", lib },
90 cbi("olsr/olsrdplugins"),
91 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
97 function action_neigh()
98 local data = fetch_txtinfo("links")
100 if not data or not data.Links then
101 luci.template.render("status-olsr/error_olsr")
105 local function compare(a, b)
106 local c = tonumber(a.Cost)
107 local d = tonumber(b.Cost)
109 if not c or c == 0 then
113 if not d or d == 0 then
120 table.sort(data.Links, compare)
122 luci.template.render("status-olsr/neighbors", {links=data.Links})
125 function action_routes()
126 local data = fetch_txtinfo("routes")
128 if not data or not data.Routes then
129 luci.template.render("status-olsr/error_olsr")
133 local function compare(a, b)
134 local c = tonumber(a.ETX)
135 local d = tonumber(b.ETX)
137 if not c or c == 0 then
141 if not d or d == 0 then
148 table.sort(data.Routes, compare)
150 luci.template.render("status-olsr/routes", {routes=data.Routes})
153 function action_topology()
154 local data = fetch_txtinfo("topology")
156 if not data or not data.Topology then
157 luci.template.render("status-olsr/error_olsr")
161 local function compare(a, b)
162 return a["Dest. IP"] < b["Dest. IP"]
165 table.sort(data.Topology, compare)
167 luci.template.render("status-olsr/topology", {routes=data.Topology})
170 function action_hna()
171 local data = fetch_txtinfo("hna")
173 if not data or not data.HNA then
174 luci.template.render("status-olsr/error_olsr")
178 local function compare(a, b)
179 return a.Destination < b.Destination
182 table.sort(data.HNA, compare)
184 luci.template.render("status-olsr/hna", {routes=data.HNA})
187 function action_mid()
188 local data = fetch_txtinfo("mid")
190 if not data or not data.MID then
191 luci.template.render("status-olsr/error_olsr")
195 local function compare(a, b)
196 return a["IP address"] < b["IP address"]
199 table.sort(data.MID, compare)
201 luci.template.render("status-olsr/mid", {mids=data.MID})
204 function action_smartgw()
205 local data = fetch_txtinfo("gateways")
207 if not data or not data.Gateways then
208 luci.template.render("status-olsr/error_olsr")
212 local function compare(a, b)
213 return a["ETX"] < b["ETX"]
216 table.sort(data.Gateways, compare)
218 luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
221 function action_interfaces()
222 local data = fetch_txtinfo("interfaces")
224 if not data or not data.Interfaces then
225 luci.template.render("status-olsr/error_olsr")
229 luci.template.render("status-olsr/interfaces", {iface=data.Interfaces})
233 function fetch_txtinfo(otable)
235 local uci = require "luci.model.uci".cursor_state()
236 local resolve = uci:get("luci_olsr", "general", "resolve")
237 otable = otable or ""
238 local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
239 local rawdatav6 = luci.sys.httpget("http://[::1]:2006/"..otable)
245 if #rawdata ~= 0 then
246 local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
248 if otable == "links" then
250 luci.sys.net.routes(function(r) if r.dest:prefix() == 0 then defaultgw = r.gateway:string() end end)
253 for i, tbl in ipairs(tables) do
254 local lines = luci.util.split(tbl, "\r?\n", nil, true)
255 name = table.remove(lines, 1):sub(8)
256 local keys = luci.util.split(table.remove(lines, 1), "\t")
257 local split = #keys - 1
258 if not data[name] then
262 for j, line in ipairs(lines) do
263 dataindex = ( dataindex + 1 )
265 local fields = luci.util.split(line, "\t", split)
267 for k, key in pairs(keys) do
268 if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
269 data[name][di][key] = fields[k]
270 if resolve == "1" then
271 hostname = nixio.getnameinfo(fields[k], "inet")
273 data[name][di]["Hostname"] = hostname
276 if key == "Remote IP" and defaultgw then
277 if defaultgw == fields[k] then
278 data[name][di]["defaultgw"] = 1
281 elseif key == "Local IP" then
282 data[name][di][key] = fields[k]
283 data[name][di]['Local Device'] = fields[k]
284 uci:foreach("network", "interface",
286 localip = string.gsub(fields[k], ' ', '')
287 if s.ipaddr == localip then
288 data[name][di]['Local Device'] = s['.name'] or interface
291 elseif key == "Interface" then
292 data[name][di][key] = fields[k]
293 uci:foreach("network", "interface",
295 interface = string.gsub(fields[k], ' ', '')
296 if s.ifname == interface then
297 data[name][di][key] = s['.name'] or interface
301 data[name][di][key] = fields[k]
304 if data[name][di].Linkcost then
305 data[name][di].LinkQuality,
308 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
314 if #rawdatav6 ~= 0 then
315 local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
316 for i, tbl in ipairs(tables) do
317 local lines = luci.util.split(tbl, "\r?\n", nil, true)
318 name = table.remove(lines, 1):sub(8)
319 local keys = luci.util.split(table.remove(lines, 1), "\t")
320 local split = #keys - 1
321 if not data[name] then
324 for j, line in ipairs(lines) do
325 dataindex = ( dataindex + 1 )
327 local fields = luci.util.split(line, "\t", split)
329 for k, key in pairs(keys) do
330 if key == "Remote IP" then
331 data[name][di][key] = "[" .. fields[k] .. "]"
332 if resolve == "1" then
333 hostname = nixio.getnameinfo(fields[k], "inet6")
335 data[name][di]["Hostname"] = hostname
338 elseif key == "Local IP" then
339 data[name][di][key] = fields[k]
340 data[name][di]['Local Device'] = fields[k]
341 uci:foreach("network", "interface",
343 local localip = string.gsub(fields[k], ' ', '')
345 local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
346 if ip6addr == localip then
347 data[name][di]['Local Device'] = s['.name'] or s.interface
351 elseif key == "Dest. IP" then
352 data[name][di][key] = "[" .. fields[k] .. "]"
353 elseif key == "Last hop IP" then
354 data[name][di][key] = "[" .. fields[k] .. "]"
355 elseif key == "IP address" then
356 data[name][di][key] = "[" .. fields[k] .. "]"
357 elseif key == "Gateway" then
358 data[name][di][key] = "[" .. fields[k] .. "]"
360 data[name][di][key] = fields[k]
364 if data[name][di].Linkcost then
365 data[name][di].LinkQuality,
368 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")