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 if not defaultgw then
119 defaultgw = luci.util.exec("ip route list exact 0.0.0.0/0 table all"):match(" via (%S+)")
122 local function compare(a,b)
123 if a.proto == b.proto then
124 return a.linkCost < b.linkCost
126 return a.proto < b.proto
130 for _, dev in ipairs(devices) do
131 for _, net in ipairs(dev:get_wifinets()) do
132 assoclist[#assoclist+1] = {}
133 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
134 assoclist[#assoclist]['network'] = net.iwdata.network
135 assoclist[#assoclist]['device'] = net.iwdata.device
136 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
140 for k, v in ipairs(data) do
145 local arptable = sys.net.arptable()
152 if resolve == "1" then
153 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
155 v.hostname = hostname
158 if v.proto == '4' then
159 uci:foreach("network", "interface",function(vif)
160 if vif.ipaddr and vif.ipaddr == v.localIP then
161 interface = vif['.name'] or vif.interface
162 lmac = string.lower(vif.macaddr or "")
166 for _, arpt in ipairs(arptable) do
167 ip = arpt['IP address']
168 if ip == v.remoteIP then
169 rmac = string.lower(arpt['HW address'] or "")
172 elseif v.proto == '6' then
173 uci:foreach("network", "interface",function(vif)
174 local name = vif['.name']
175 local net = ntm:get_network(name)
176 local device = net and net:get_interface()
177 local locip = ipc.IPv6(v.localIP)
178 if device and device:ip6addrs() and locip then
179 for _, a in ipairs(device:ip6addrs()) do
180 if not a:is6linklocal() then
181 if a:host() == locip:host() then
183 --neihgt = neightbl.get(device.ifname) or {}
190 for ip,mac in pairs(neihgt) do
191 if ip == v.remoteIP then
197 for _, val in ipairs(assoclist) do
198 if val.network == interface and val.list then
199 for assocmac, assot in pairs(val.list) do
200 assocmac = string.lower(assocmac or "")
201 if rmac == assocmac then
202 signal = tonumber(assot.signal)
203 noise = tonumber(assot.noise)
204 snr = (noise*-1) - (signal*-1)
210 v.interface = interface
222 if defaultgw == v.remoteIP then
227 table.sort(data, compare)
228 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
231 function action_routes()
232 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
237 local uci = require "luci.model.uci".cursor_state()
238 local resolve = uci:get("luci_olsr", "general", "resolve")
240 for k, v in ipairs(data) do
241 if resolve == "1" then
242 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
244 v.hostname = hostname
249 local function compare(a,b)
250 if a.proto == b.proto then
251 return a.rtpMetricCost < b.rtpMetricCost
253 return a.proto < b.proto
257 table.sort(data, compare)
258 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
261 function action_topology()
262 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
267 local function compare(a,b)
268 if a.proto == b.proto then
269 return a.tcEdgeCost < b.tcEdgeCost
271 return a.proto < b.proto
275 table.sort(data, compare)
276 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
279 function action_hna()
280 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
285 local uci = require "luci.model.uci".cursor_state()
286 local resolve = uci:get("luci_olsr", "general", "resolve")
288 local function compare(a,b)
289 if a.proto == b.proto then
290 return a.genmask < b.genmask
292 return a.proto < b.proto
296 for k, v in ipairs(data) do
297 if resolve == "1" then
298 hostname = nixio.getnameinfo(v.gateway, nil, 100)
300 v.hostname = hostname
303 if v.validityTime then
304 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
308 table.sort(data, compare)
309 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
312 function action_mid()
313 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
318 local function compare(a,b)
319 if a.proto == b.proto then
320 return a.ipAddress < b.ipAddress
322 return a.proto < b.proto
326 table.sort(data, compare)
327 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
330 function action_smartgw()
331 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
336 local function compare(a,b)
337 if a.proto == b.proto then
338 return a.tcPathCost < b.tcPathCost
340 return a.proto < b.proto
344 table.sort(data, compare)
345 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
348 function action_interfaces()
349 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
354 local function compare(a,b)
355 return a.proto < b.proto
358 table.sort(data, compare)
359 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
363 function fetch_jsoninfo(otable)
364 local uci = require "luci.model.uci".cursor_state()
365 local utl = require "luci.util"
366 local json = require "luci.json"
367 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
370 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
371 jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
379 if jsonreq4 == '' and jsonreq6 == '' then
380 luci.template.render("status-olsr/error_olsr")
381 return nil, 0, 0, true
384 if jsonreq4 ~= "" then
386 jsondata4 = json.decode(jsonreq4)
387 if otable == 'status' then
388 data4 = jsondata4 or {}
390 data4 = jsondata4[otable] or {}
393 for k, v in ipairs(data4) do
394 data4[k]['proto'] = '4'
398 if jsonreq6 ~= "" then
400 jsondata6 = json.decode(jsonreq6)
401 if otable == 'status' then
402 data6 = jsondata6 or {}
404 data6 = jsondata6[otable] or {}
406 for k, v in ipairs(data6) do
407 data6[k]['proto'] = '6'
411 for k, v in ipairs(data6) do
412 table.insert(data4, v)
415 return data4, has_v4, has_v6, false