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")
13 local page = node("admin", "status", "olsr", "neighbors")
14 page.target = call("action_neigh")
15 page.title = _("Neighbours")
19 local page = node("admin", "status", "olsr", "routes")
20 page.target = call("action_routes")
21 page.title = _("Routes")
24 local page = node("admin", "status", "olsr", "topology")
25 page.target = call("action_topology")
26 page.title = _("Topology")
29 local page = node("admin", "status", "olsr", "hna")
30 page.target = call("action_hna")
34 local page = node("admin", "status", "olsr", "mid")
35 page.target = call("action_mid")
39 local page = node("admin", "status", "olsr", "smartgw")
40 page.target = call("action_smartgw")
41 page.title = _("SmartGW")
44 local page = node("admin", "status", "olsr", "interfaces")
45 page.target = call("action_interfaces")
46 page.title = _("Interfaces")
50 {"admin", "services", "olsrd"},
51 cbi("olsr/olsrd"), "OLSR"
56 {"admin", "services", "olsrd", "iface"},
57 cbi("olsr/olsrdiface")
61 {"admin", "services", "olsrd", "hna"},
62 cbi("olsr/olsrdhna"), _("HNA Announcements")
66 {"admin", "services", "olsrd", "plugins"},
67 cbi("olsr/olsrdplugins"), _("Plugins")
71 {"admin", "services", "olsrd", "display"},
72 cbi("olsr/olsrddisplay"), _("Display")
78 local uci = require("luci.model.uci").cursor()
79 uci:foreach("olsrd", "LoadPlugin",
81 local lib = section.library
83 {"admin", "services", "olsrd", "plugins", lib },
84 cbi("olsr/olsrdplugins"),
85 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
91 local function compare_links(a, b)
92 local c = tonumber(a.Cost)
93 local d = tonumber(b.Cost)
95 if not c or c == 0 then
99 if not d or d == 0 then
105 function action_neigh(json)
106 local data = fetch_txtinfo("links")
108 if not data or not data.Links then
109 luci.template.render("status-olsr/error_olsr")
113 table.sort(data.Links, compare_links)
115 luci.template.render("status-olsr/neighbors", {links=data.Links})
118 function action_routes()
119 local data = fetch_txtinfo("routes")
121 if not data or not data.Routes then
122 luci.template.render("status-olsr/error_olsr")
126 local function compare(a, b)
127 local c = tonumber(a.ETX)
128 local d = tonumber(b.ETX)
130 if not c or c == 0 then
134 if not d or d == 0 then
141 table.sort(data.Routes, compare)
143 luci.template.render("status-olsr/routes", {routes=data.Routes})
146 function action_topology()
147 local data = fetch_txtinfo("topology")
149 if not data or not data.Topology then
150 luci.template.render("status-olsr/error_olsr")
154 local function compare(a, b)
155 return a["Dest. IP"] < b["Dest. IP"]
158 table.sort(data.Topology, compare)
160 luci.template.render("status-olsr/topology", {routes=data.Topology})
163 function action_hna()
164 local data = fetch_txtinfo("hna")
166 if not data or not data.HNA then
167 luci.template.render("status-olsr/error_olsr")
171 local function compare(a, b)
172 return a.Destination < b.Destination
175 table.sort(data.HNA, compare)
177 luci.template.render("status-olsr/hna", {routes=data.HNA})
180 function action_mid()
181 local data = fetch_txtinfo("mid")
183 if not data or not data.MID then
184 luci.template.render("status-olsr/error_olsr")
188 local function compare(a, b)
189 return a["IP address"] < b["IP address"]
192 table.sort(data.MID, compare)
194 luci.template.render("status-olsr/mid", {mids=data.MID})
197 function action_smartgw()
198 local data = fetch_txtinfo("gateways")
200 if not data or not data.Gateways then
201 luci.template.render("status-olsr/error_olsr")
205 local function compare(a, b)
206 return a["ETX"] < b["ETX"]
209 table.sort(data.Gateways, compare)
211 luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
214 function action_interfaces()
215 local data = fetch_txtinfo("interfaces")
217 if not data or not data.Interfaces then
218 luci.template.render("status-olsr/error_olsr")
222 luci.template.render("status-olsr/interfaces", {iface=data.Interfaces})
226 function fetch_txtinfo(otable)
228 local uci = require "luci.model.uci".cursor_state()
229 local resolve = uci:get("luci_olsr", "general", "resolve")
230 otable = otable or ""
231 local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
232 local rawdatav6 = luci.sys.httpget("http://[::1]:2006/"..otable)
238 if #rawdata ~= 0 then
239 local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
241 if otable == "links" then
243 luci.sys.net.routes(function(r) if r.dest:prefix() == 0 then defaultgw = r.gateway:string() end end)
246 for i, tbl in ipairs(tables) do
247 local lines = luci.util.split(tbl, "\r?\n", nil, true)
248 name = table.remove(lines, 1):sub(8)
249 local keys = luci.util.split(table.remove(lines, 1), "\t")
250 local split = #keys - 1
251 if not data[name] then
255 for j, line in ipairs(lines) do
256 dataindex = ( dataindex + 1 )
258 local fields = luci.util.split(line, "\t", split)
260 for k, key in pairs(keys) do
261 if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
262 data[name][di][key] = fields[k]
263 if resolve == "1" then
264 hostname = nixio.getnameinfo(fields[k], "inet")
266 data[name][di]["Hostname"] = hostname
269 if key == "Remote IP" and defaultgw then
270 if defaultgw == fields[k] then
271 data[name][di]["defaultgw"] = 1
274 elseif key == "Local IP" then
275 data[name][di][key] = fields[k]
276 data[name][di]['Local Device'] = fields[k]
277 uci:foreach("network", "interface",
279 localip = string.gsub(fields[k], ' ', '')
280 if s.ipaddr == localip then
281 data[name][di]['Local Device'] = s['.name'] or interface
284 elseif key == "Interface" then
285 data[name][di][key] = fields[k]
286 uci:foreach("network", "interface",
288 interface = string.gsub(fields[k], ' ', '')
289 if s.ifname == interface then
290 data[name][di][key] = s['.name'] or interface
294 data[name][di][key] = fields[k]
297 if data[name][di].Linkcost then
298 data[name][di].LinkQuality,
301 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
307 if #rawdatav6 ~= 0 then
308 local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
309 for i, tbl in ipairs(tables) do
310 local lines = luci.util.split(tbl, "\r?\n", nil, true)
311 name = table.remove(lines, 1):sub(8)
312 local keys = luci.util.split(table.remove(lines, 1), "\t")
313 local split = #keys - 1
314 if not data[name] then
317 for j, line in ipairs(lines) do
318 dataindex = ( dataindex + 1 )
320 local fields = luci.util.split(line, "\t", split)
322 for k, key in pairs(keys) do
323 if key == "Remote IP" then
324 data[name][di][key] = "[" .. fields[k] .. "]"
325 if resolve == "1" then
326 hostname = nixio.getnameinfo(fields[k], "inet6")
328 data[name][di]["Hostname"] = hostname
331 elseif key == "Local IP" then
332 data[name][di][key] = fields[k]
333 data[name][di]['Local Device'] = fields[k]
334 uci:foreach("network", "interface",
336 local localip = string.gsub(fields[k], ' ', ''):upper()
338 s.ip6addr = luci.ip.IPv6(s.ip6addr):string()
339 local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
340 if ip6addr == localip then
341 data[name][di]['Local Device'] = s['.name'] or s.interface
345 elseif key == "Dest. IP" then
346 data[name][di][key] = "[" .. fields[k] .. "]"
347 elseif key == "Last hop IP" then
348 data[name][di][key] = "[" .. fields[k] .. "]"
349 elseif key == "IP address" then
350 data[name][di][key] = "[" .. fields[k] .. "]"
351 elseif key == "Gateway" then
352 data[name][di][key] = "[" .. fields[k] .. "]"
354 data[name][di][key] = fields[k]
358 if data[name][di].Linkcost then
359 data[name][di].LinkQuality,
362 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")