1 module("luci.controller.olsr", package.seeall)
4 if not nixio.fs.access("/etc/config/olsrd") then
8 local page = node("admin", "status", "olsr")
9 page.target = template("status-olsr/overview")
10 page.title = _("OLSR")
14 local page = node("admin", "status", "olsr", "neighbors")
15 page.target = call("action_neigh")
16 page.title = _("Neighbours")
20 local page = node("admin", "status", "olsr", "routes")
21 page.target = call("action_routes")
22 page.title = _("Routes")
25 local page = node("admin", "status", "olsr", "topology")
26 page.target = call("action_topology")
27 page.title = _("Topology")
30 local page = node("admin", "status", "olsr", "hna")
31 page.target = call("action_hna")
35 local page = node("admin", "status", "olsr", "mid")
36 page.target = call("action_mid")
40 local page = node("admin", "status", "olsr", "smartgw")
41 page.target = call("action_smartgw")
42 page.title = _("SmartGW")
45 local page = node("admin", "status", "olsr", "interfaces")
46 page.target = call("action_interfaces")
47 page.title = _("Interfaces")
51 {"admin", "services", "olsrd"},
52 cbi("olsr/olsrd"), "OLSR"
58 {"admin", "services", "olsrd", "iface"},
59 cbi("olsr/olsrdiface")
63 {"admin", "services", "olsrd", "hna"},
64 cbi("olsr/olsrdhna"), _("HNA Announcements")
68 {"admin", "services", "olsrd", "plugins"},
69 cbi("olsr/olsrdplugins"), _("Plugins")
73 {"admin", "services", "olsrd", "display"},
74 cbi("olsr/olsrddisplay"), _("Display")
81 local uci = require("luci.model.uci").cursor()
82 uci:foreach("olsrd", "LoadPlugin",
84 local lib = section.library
86 {"admin", "services", "olsrd", "plugins", lib },
87 cbi("olsr/olsrdplugins"),
88 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
94 local function compare_links(a, b)
95 local c = tonumber(a.Cost)
96 local d = tonumber(b.Cost)
98 if not c or c == 0 then
102 if not d or d == 0 then
108 function action_neigh(json)
109 local data = fetch_txtinfo("links")
111 if not data or not data.Links then
112 luci.template.render("status-olsr/error_olsr")
116 table.sort(data.Links, compare_links)
118 luci.template.render("status-olsr/neighbors", {links=data.Links})
121 function action_routes()
122 local data = fetch_txtinfo("routes")
124 if not data or not data.Routes then
125 luci.template.render("status-olsr/error_olsr")
129 local function compare(a, b)
130 local c = tonumber(a.ETX)
131 local d = tonumber(b.ETX)
133 if not c or c == 0 then
137 if not d or d == 0 then
144 table.sort(data.Routes, compare)
146 luci.template.render("status-olsr/routes", {routes=data.Routes})
149 function action_topology()
150 local data = fetch_txtinfo("topology")
152 if not data or not data.Topology then
153 luci.template.render("status-olsr/error_olsr")
157 local function compare(a, b)
158 return a["Dest. IP"] < b["Dest. IP"]
161 table.sort(data.Topology, compare)
163 luci.template.render("status-olsr/topology", {routes=data.Topology})
166 function action_hna()
167 local data = fetch_txtinfo("hna")
169 if not data or not data.HNA then
170 luci.template.render("status-olsr/error_olsr")
174 local function compare(a, b)
175 return a.Destination < b.Destination
178 table.sort(data.HNA, compare)
180 luci.template.render("status-olsr/hna", {routes=data.HNA})
183 function action_mid()
184 local data = fetch_txtinfo("mid")
186 if not data or not data.MID then
187 luci.template.render("status-olsr/error_olsr")
191 local function compare(a, b)
192 return a["IP address"] < b["IP address"]
195 table.sort(data.MID, compare)
197 luci.template.render("status-olsr/mid", {mids=data.MID})
200 function action_smartgw()
201 local data = fetch_txtinfo("gateways")
203 if not data or not data.Gateways then
204 luci.template.render("status-olsr/error_olsr")
208 local function compare(a, b)
209 return a["ETX"] < b["ETX"]
212 table.sort(data.Gateways, compare)
214 luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
217 function action_interfaces()
218 local data = fetch_txtinfo("interfaces")
220 if not data or not data.Interfaces then
221 luci.template.render("status-olsr/error_olsr")
225 luci.template.render("status-olsr/interfaces", {iface=data.Interfaces})
229 function fetch_txtinfo(otable)
231 local uci = require "luci.model.uci".cursor_state()
232 local resolve = uci:get("luci_olsr", "general", "resolve")
233 otable = otable or ""
234 local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
235 local rawdatav6 = luci.sys.httpget("http://[::1]:2006/"..otable)
241 if #rawdata ~= 0 then
242 local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
244 if otable == "links" then
246 luci.sys.net.routes(function(r) if r.dest:prefix() == 0 then defaultgw = r.gateway:string() end end)
249 for i, tbl in ipairs(tables) do
250 local lines = luci.util.split(tbl, "\r?\n", nil, true)
251 name = table.remove(lines, 1):sub(8)
252 local keys = luci.util.split(table.remove(lines, 1), "\t")
253 local split = #keys - 1
254 if not data[name] then
258 for j, line in ipairs(lines) do
259 dataindex = ( dataindex + 1 )
261 local fields = luci.util.split(line, "\t", split)
263 for k, key in pairs(keys) do
264 if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
265 data[name][di][key] = fields[k]
266 if resolve == "1" then
267 hostname = nixio.getnameinfo(fields[k], "inet")
269 data[name][di]["Hostname"] = hostname
272 if key == "Remote IP" and defaultgw then
273 if defaultgw == fields[k] then
274 data[name][di]["defaultgw"] = 1
277 elseif key == "Local IP" then
278 data[name][di][key] = fields[k]
279 data[name][di]['Local Device'] = fields[k]
280 uci:foreach("network", "interface",
282 localip = string.gsub(fields[k], ' ', '')
283 if s.ipaddr == localip then
284 data[name][di]['Local Device'] = s['.name'] or interface
287 elseif key == "Interface" then
288 data[name][di][key] = fields[k]
289 uci:foreach("network", "interface",
291 interface = string.gsub(fields[k], ' ', '')
292 if s.ifname == interface then
293 data[name][di][key] = s['.name'] or interface
297 data[name][di][key] = fields[k]
300 if data[name][di].Linkcost then
301 data[name][di].LinkQuality,
304 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
310 if #rawdatav6 ~= 0 then
311 local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
312 for i, tbl in ipairs(tables) do
313 local lines = luci.util.split(tbl, "\r?\n", nil, true)
314 name = table.remove(lines, 1):sub(8)
315 local keys = luci.util.split(table.remove(lines, 1), "\t")
316 local split = #keys - 1
317 if not data[name] then
320 for j, line in ipairs(lines) do
321 dataindex = ( dataindex + 1 )
323 local fields = luci.util.split(line, "\t", split)
325 for k, key in pairs(keys) do
326 if key == "Remote IP" then
327 data[name][di][key] = "[" .. fields[k] .. "]"
328 if resolve == "1" then
329 hostname = nixio.getnameinfo(fields[k], "inet6")
331 data[name][di]["Hostname"] = hostname
334 elseif key == "Local IP" then
335 data[name][di][key] = fields[k]
336 data[name][di]['Local Device'] = fields[k]
337 uci:foreach("network", "interface",
339 local localip = string.gsub(fields[k], ' ', ''):upper()
341 s.ip6addr = luci.ip.IPv6(s.ip6addr):string()
342 local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
343 if ip6addr == localip then
344 data[name][di]['Local Device'] = s['.name'] or s.interface
348 elseif key == "Dest. IP" then
349 data[name][di][key] = "[" .. fields[k] .. "]"
350 elseif key == "Last hop IP" then
351 data[name][di][key] = "[" .. fields[k] .. "]"
352 elseif key == "IP address" then
353 data[name][di][key] = "[" .. fields[k] .. "]"
354 elseif key == "Gateway" then
355 data[name][di][key] = "[" .. fields[k] .. "]"
357 data[name][di][key] = fields[k]
361 if data[name][di].Linkcost then
362 data[name][di].LinkQuality,
365 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")