1 module("luci.controller.olsr", package.seeall)
5 if nixio.fs.access("/etc/config/olsrd") then
8 if nixio.fs.access("/etc/config/olsrd6") then
11 if not ipv4 and not ipv6 then
15 require("luci.model.uci")
16 local uci = luci.model.uci.cursor_state()
18 uci:foreach("olsrd", "olsrd", function(s)
19 if s.SmartGateway and s.SmartGateway == "yes" then has_smartgw = true end
22 local page = node("admin", "status", "olsr")
23 page.target = template("status-olsr/overview")
24 page.title = _("OLSR")
27 local page = node("admin", "status", "olsr", "json")
28 page.target = call("action_json")
32 local page = node("admin", "status", "olsr", "neighbors")
33 page.target = call("action_neigh")
34 page.title = _("Neighbours")
38 local page = node("admin", "status", "olsr", "routes")
39 page.target = call("action_routes")
40 page.title = _("Routes")
43 local page = node("admin", "status", "olsr", "topology")
44 page.target = call("action_topology")
45 page.title = _("Topology")
48 local page = node("admin", "status", "olsr", "hna")
49 page.target = call("action_hna")
53 local page = node("admin", "status", "olsr", "mid")
54 page.target = call("action_mid")
59 local page = node("admin", "status", "olsr", "smartgw")
60 page.target = call("action_smartgw")
61 page.title = _("SmartGW")
65 local page = node("admin", "status", "olsr", "interfaces")
66 page.target = call("action_interfaces")
67 page.title = _("Interfaces")
71 {"admin", "services", "olsrd", "display"},
72 cbi("olsr/olsrddisplay"), _("Display")
77 function action_json()
78 local http = require "luci.http"
79 local utl = require "luci.util"
80 local uci = require "luci.model.uci".cursor_state()
84 jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
85 jsonreq6 = utl.exec("echo /status | nc ::1 9090")
86 http.prepare_content("application/json")
87 if not jsonreq4 or jsonreq4 == "" then
90 if not jsonreq6 or jsonreq6 == "" then
93 http.write('{"v4":' .. jsonreq4 .. ', "v6":' .. jsonreq6 .. '}')
96 function action_neigh(json)
97 local data, has_v4, has_v6, error = fetch_jsoninfo('links')
103 local uci = require "luci.model.uci".cursor_state()
104 local resolve = uci:get("luci_olsr", "general", "resolve")
105 local ntm = require "luci.model.network".init()
106 local devices = ntm:get_wifidevs()
107 local sys = require "luci.sys"
109 --local neightbl = require "neightbl"
110 local ipc = require "luci.ip"
112 luci.sys.net.routes(function(r)
113 if r.dest:prefix() == 0 then
114 defaultgw = r.gateway:string()
118 local function compare(a,b)
119 if a.proto == b.proto then
120 return a.linkCost < b.linkCost
122 return a.proto < b.proto
126 for _, dev in ipairs(devices) do
127 for _, net in ipairs(dev:get_wifinets()) do
128 assoclist[#assoclist+1] = {}
129 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
130 assoclist[#assoclist]['network'] = net.iwdata.network
131 assoclist[#assoclist]['device'] = net.iwdata.device
132 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
136 for k, v in ipairs(data) do
141 local arptable = sys.net.arptable()
148 if resolve == "1" then
149 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
151 v.hostname = hostname
154 if v.proto == '4' then
155 uci:foreach("network", "interface",function(vif)
156 if vif.ipaddr and vif.ipaddr == v.localIP then
157 interface = vif['.name'] or vif.interface
158 lmac = string.lower(vif.macaddr or "")
162 for _, arpt in ipairs(arptable) do
163 ip = arpt['IP address']
164 if ip == v.remoteIP then
165 rmac = string.lower(arpt['HW address'] or "")
168 elseif v.proto == '6' then
169 uci:foreach("network", "interface",function(vif)
170 local name = vif['.name']
171 local net = ntm:get_network(name)
172 local device = net and net:get_interface()
173 local locip = ipc.IPv6(v.localIP)
174 if device and device:ip6addrs() and locip then
175 for _, a in ipairs(device:ip6addrs()) do
176 if not a:is6linklocal() then
177 if a:host() == locip:host() then
179 --neihgt = neightbl.get(device.ifname) or {}
186 for ip,mac in pairs(neihgt) do
187 if ip == v.remoteIP then
193 for _, val in ipairs(assoclist) do
194 if val.network == interface and val.list then
195 for assocmac, assot in pairs(val.list) do
196 assocmac = string.lower(assocmac or "")
197 if rmac == assocmac then
198 signal = tonumber(assot.signal)
199 noise = tonumber(assot.noise)
200 snr = (noise*-1) - (signal*-1)
206 v.interface = interface
218 if defaultgw == v.remoteIP then
223 table.sort(data, compare)
224 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
227 function action_routes()
228 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
233 local uci = require "luci.model.uci".cursor_state()
234 local resolve = uci:get("luci_olsr", "general", "resolve")
236 for k, v in ipairs(data) do
237 if resolve == "1" then
238 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
240 v.hostname = hostname
245 local function compare(a,b)
246 if a.proto == b.proto then
247 return a.rtpMetricCost < b.rtpMetricCost
249 return a.proto < b.proto
253 table.sort(data, compare)
254 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
257 function action_topology()
258 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
263 local function compare(a,b)
264 if a.proto == b.proto then
265 return a.tcEdgeCost < b.tcEdgeCost
267 return a.proto < b.proto
271 table.sort(data, compare)
272 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
275 function action_hna()
276 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
281 local uci = require "luci.model.uci".cursor_state()
282 local resolve = uci:get("luci_olsr", "general", "resolve")
284 local function compare(a,b)
285 if a.proto == b.proto then
286 return a.genmask < b.genmask
288 return a.proto < b.proto
292 for k, v in ipairs(data) do
293 if resolve == "1" then
294 hostname = nixio.getnameinfo(v.gateway, nil, 100)
296 v.hostname = hostname
299 if v.validityTime then
300 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
304 table.sort(data, compare)
305 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
308 function action_mid()
309 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
314 local function compare(a,b)
315 if a.proto == b.proto then
316 return a.ipAddress < b.ipAddress
318 return a.proto < b.proto
322 table.sort(data, compare)
323 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
326 function action_smartgw()
327 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
332 local function compare(a,b)
333 if a.proto == b.proto then
334 return a.tcPathCost < b.tcPathCost
336 return a.proto < b.proto
340 table.sort(data, compare)
341 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
344 function action_interfaces()
345 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
350 local function compare(a,b)
351 return a.proto < b.proto
354 table.sort(data, compare)
355 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
359 function fetch_jsoninfo(otable)
360 local uci = require "luci.model.uci".cursor_state()
361 local utl = require "luci.util"
362 local json = require "luci.json"
363 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
366 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
367 jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
375 if jsonreq4 == '' and jsonreq6 == '' then
376 luci.template.render("status-olsr/error_olsr")
377 return nil, 0, 0, true
380 if jsonreq4 ~= "" then
382 jsondata4 = json.decode(jsonreq4)
383 if otable == 'status' then
384 data4 = jsondata4 or {}
386 data4 = jsondata4[otable] or {}
389 for k, v in ipairs(data4) do
390 data4[k]['proto'] = '4'
394 if jsonreq6 ~= "" then
396 jsondata6 = json.decode(jsonreq6)
397 if otable == 'status' then
398 data6 = jsondata6 or {}
400 data6 = jsondata6[otable] or {}
402 for k, v in ipairs(data6) do
403 data6[k]['proto'] = '6'
407 for k, v in ipairs(data6) do
408 table.insert(data4, v)
411 return data4, has_v4, has_v6, false