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"
113 ipc.routes({ family = 4, type = 1, dest_exact = "0.0.0.0/0" },
114 function(rt) defaultgw = rt.gw end)
116 local function compare(a,b)
117 if a.proto == b.proto then
118 return a.linkCost < b.linkCost
120 return a.proto < b.proto
124 for _, dev in ipairs(devices) do
125 for _, net in ipairs(dev:get_wifinets()) do
126 assoclist[#assoclist+1] = {}
127 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
128 assoclist[#assoclist]['network'] = net.iwdata.network
129 assoclist[#assoclist]['device'] = net.iwdata.device
130 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
134 for k, v in ipairs(data) do
139 local arptable = sys.net.arptable()
146 if resolve == "1" then
147 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
149 v.hostname = hostname
152 if v.proto == '4' then
153 uci:foreach("network", "interface",function(vif)
154 if vif.ipaddr and vif.ipaddr == v.localIP then
155 interface = vif['.name'] or vif.interface
156 lmac = string.lower(vif.macaddr or "")
160 for _, arpt in ipairs(arptable) do
161 ip = arpt['IP address']
162 if ip == v.remoteIP then
163 rmac = string.lower(arpt['HW address'] or "")
166 elseif v.proto == '6' then
167 uci:foreach("network", "interface",function(vif)
168 local name = vif['.name']
169 local net = ntm:get_network(name)
170 local device = net and net:get_interface()
171 local locip = ipc.IPv6(v.localIP)
172 if device and device:ip6addrs() and locip then
173 for _, a in ipairs(device:ip6addrs()) do
174 if not a:is6linklocal() then
175 if a:host() == locip:host() then
177 --neihgt = neightbl.get(device.ifname) or {}
184 for ip,mac in pairs(neihgt) do
185 if ip == v.remoteIP then
191 for _, val in ipairs(assoclist) do
192 if val.network == interface and val.list then
193 for assocmac, assot in pairs(val.list) do
194 assocmac = string.lower(assocmac or "")
195 if rmac == assocmac then
196 signal = tonumber(assot.signal)
197 noise = tonumber(assot.noise)
198 snr = (noise*-1) - (signal*-1)
204 v.interface = interface
216 if defaultgw == v.remoteIP then
221 table.sort(data, compare)
222 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
225 function action_routes()
226 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
231 local uci = require "luci.model.uci".cursor_state()
232 local resolve = uci:get("luci_olsr", "general", "resolve")
234 for k, v in ipairs(data) do
235 if resolve == "1" then
236 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
238 v.hostname = hostname
243 local function compare(a,b)
244 if a.proto == b.proto then
245 return a.rtpMetricCost < b.rtpMetricCost
247 return a.proto < b.proto
251 table.sort(data, compare)
252 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
255 function action_topology()
256 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
261 local function compare(a,b)
262 if a.proto == b.proto then
263 return a.tcEdgeCost < b.tcEdgeCost
265 return a.proto < b.proto
269 table.sort(data, compare)
270 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
273 function action_hna()
274 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
279 local uci = require "luci.model.uci".cursor_state()
280 local resolve = uci:get("luci_olsr", "general", "resolve")
282 local function compare(a,b)
283 if a.proto == b.proto then
284 return a.genmask < b.genmask
286 return a.proto < b.proto
290 for k, v in ipairs(data) do
291 if resolve == "1" then
292 hostname = nixio.getnameinfo(v.gateway, nil, 100)
294 v.hostname = hostname
297 if v.validityTime then
298 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
302 table.sort(data, compare)
303 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
306 function action_mid()
307 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
312 local function compare(a,b)
313 if a.proto == b.proto then
314 return a.ipAddress < b.ipAddress
316 return a.proto < b.proto
320 table.sort(data, compare)
321 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
324 function action_smartgw()
325 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
330 local function compare(a,b)
331 if a.proto == b.proto then
332 return a.tcPathCost < b.tcPathCost
334 return a.proto < b.proto
338 table.sort(data, compare)
339 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
342 function action_interfaces()
343 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
348 local function compare(a,b)
349 return a.proto < b.proto
352 table.sort(data, compare)
353 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
357 function fetch_jsoninfo(otable)
358 local uci = require "luci.model.uci".cursor_state()
359 local utl = require "luci.util"
360 local json = require "luci.json"
361 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
364 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
365 jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
373 if jsonreq4 == '' and jsonreq6 == '' then
374 luci.template.render("status-olsr/error_olsr")
375 return nil, 0, 0, true
378 if jsonreq4 ~= "" then
380 jsondata4 = json.decode(jsonreq4)
381 if otable == 'status' then
382 data4 = jsondata4 or {}
384 data4 = jsondata4[otable] or {}
387 for k, v in ipairs(data4) do
388 data4[k]['proto'] = '4'
392 if jsonreq6 ~= "" then
394 jsondata6 = json.decode(jsonreq6)
395 if otable == 'status' then
396 data6 = jsondata6 or {}
398 data6 = jsondata6[otable] or {}
400 for k, v in ipairs(data6) do
401 data6[k]['proto'] = '6'
405 for k, v in ipairs(data6) do
406 table.insert(data4, v)
409 return data4, has_v4, has_v6, false