1 module("luci.controller.olsr", package.seeall)
4 if not nixio.fs.access("/etc/config/olsrd") then
8 require("luci.model.uci")
9 local uci = luci.model.uci.cursor_state()
11 uci:foreach("olsrd", "olsrd", function(s)
12 if s.SmartGateway and s.SmartGateway == "yes" then has_smartgw = true end
15 local page = node("admin", "status", "olsr")
16 page.target = template("status-olsr/overview")
17 page.title = _("OLSR")
20 local page = node("admin", "status", "olsr", "json")
21 page.target = call("action_json")
25 local page = node("admin", "status", "olsr", "neighbors")
26 page.target = call("action_neigh")
27 page.title = _("Neighbours")
31 local page = node("admin", "status", "olsr", "routes")
32 page.target = call("action_routes")
33 page.title = _("Routes")
36 local page = node("admin", "status", "olsr", "topology")
37 page.target = call("action_topology")
38 page.title = _("Topology")
41 local page = node("admin", "status", "olsr", "hna")
42 page.target = call("action_hna")
46 local page = node("admin", "status", "olsr", "mid")
47 page.target = call("action_mid")
52 local page = node("admin", "status", "olsr", "smartgw")
53 page.target = call("action_smartgw")
54 page.title = _("SmartGW")
58 local page = node("admin", "status", "olsr", "interfaces")
59 page.target = call("action_interfaces")
60 page.title = _("Interfaces")
64 {"admin", "services", "olsrd"},
65 cbi("olsr/olsrd"), "OLSR"
70 {"admin", "services", "olsrd", "iface"},
71 cbi("olsr/olsrdiface")
75 {"admin", "services", "olsrd", "hna"},
76 cbi("olsr/olsrdhna"), _("HNA Announcements")
80 {"admin", "services", "olsrd", "plugins"},
81 cbi("olsr/olsrdplugins"), _("Plugins")
85 {"admin", "services", "olsrd", "display"},
86 cbi("olsr/olsrddisplay"), _("Display")
92 local uci = require("luci.model.uci").cursor()
93 uci:foreach("olsrd", "LoadPlugin",
95 local lib = section.library
97 {"admin", "services", "olsrd", "plugins", lib },
98 cbi("olsr/olsrdplugins"),
99 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
105 function action_json()
106 local http = require "luci.http"
107 local utl = require "luci.util"
109 local jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
110 local jsonreq6 = utl.exec("echo /status | nc ::1 9090")
111 http.prepare_content("application/json")
113 if #jsonreq4 < 1 then
117 if #jsonreq6 < 1 then
121 http.write("{v4:" .. jsonreq4 .. ", v6:" .. jsonreq6 .. "}")
124 function action_neigh(json)
125 local data, has_v4, has_v6, error = fetch_jsoninfo('links')
131 local uci = require "luci.model.uci".cursor_state()
132 local resolve = uci:get("luci_olsr", "general", "resolve")
133 local ntm = require "luci.model.network".init()
134 local devices = ntm:get_wifidevs()
135 local sys = require "luci.sys"
137 local neightbl = require "neightbl"
138 local ipc = require "luci.ip"
140 luci.sys.net.routes(function(r)
141 if r.dest:prefix() == 0 then
142 defaultgw = r.gateway:string()
146 local function compare(a,b)
147 if a.proto == b.proto then
148 return a.linkCost < b.linkCost
150 return a.proto < b.proto
154 for _, dev in ipairs(devices) do
155 for _, net in ipairs(dev:get_wifinets()) do
156 assoclist[#assoclist+1] = {}
157 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
158 assoclist[#assoclist]['network'] = net.iwdata.network
159 assoclist[#assoclist]['device'] = net.iwdata.device
160 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
164 for k, v in ipairs(data) do
169 local arptable = sys.net.arptable()
176 if resolve == "1" then
177 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
179 v.hostname = hostname
182 if v.proto == '4' then
183 uci:foreach("network", "interface",function(vif)
184 if vif.ipaddr and vif.ipaddr == v.localIP then
185 interface = vif['.name'] or vif.interface
186 lmac = string.lower(vif.macaddr or "")
190 for _, arpt in ipairs(arptable) do
191 ip = arpt['IP address']
192 if ip == v.remoteIP then
193 rmac = string.lower(arpt['HW address'] or "")
196 elseif v.proto == '6' then
197 uci:foreach("network", "interface",function(vif)
198 local name = vif['.name']
199 local net = ntm:get_network(name)
200 local device = net and net:get_interface()
201 local locip = ipc.IPv6(v.localIP)
202 for _, a in ipairs(device:ip6addrs()) do
203 if not a:is6linklocal() then
204 if a:host() == locip:host() then
206 neihgt = neightbl.get(device.ifname) or {}
211 for ip,mac in pairs(neihgt) do
212 if ip == v.remoteIP then
217 for _, val in ipairs(assoclist) do
218 if val.network == interface and val.list then
219 for assocmac, assot in pairs(val.list) do
220 assocmac = string.lower(assocmac or "")
221 if rmac == assocmac then
222 signal = tonumber(assot.signal)
223 noise = tonumber(assot.noise)
224 snr = (noise*-1) - (signal*-1)
230 v.interface = interface
242 if defaultgw == v.remoteIP then
247 table.sort(data, compare)
248 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
251 function action_routes()
252 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
257 local uci = require "luci.model.uci".cursor_state()
258 local resolve = uci:get("luci_olsr", "general", "resolve")
260 for k, v in ipairs(data) do
261 if resolve == "1" then
262 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
264 v.hostname = hostname
269 local function compare(a,b)
270 if a.proto == b.proto then
271 return a.rtpMetricCost < b.rtpMetricCost
273 return a.proto < b.proto
277 table.sort(data, compare)
278 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
281 function action_topology()
282 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
287 local function compare(a,b)
288 if a.proto == b.proto then
289 return a.tcEdgeCost < b.tcEdgeCost
291 return a.proto < b.proto
295 table.sort(data, compare)
296 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
299 function action_hna()
300 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
305 local uci = require "luci.model.uci".cursor_state()
306 local resolve = uci:get("luci_olsr", "general", "resolve")
308 local function compare(a,b)
309 if a.proto == b.proto then
310 return a.genmask < b.genmask
312 return a.proto < b.proto
316 for k, v in ipairs(data) do
317 if resolve == "1" then
318 hostname = nixio.getnameinfo(v.gateway, nil, 100)
320 v.hostname = hostname
323 if v.validityTime then
324 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
328 table.sort(data, compare)
329 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
332 function action_mid()
333 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
338 local function compare(a,b)
339 if a.proto == b.proto then
340 return a.ipAddress < b.ipAddress
342 return a.proto < b.proto
346 table.sort(data, compare)
347 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
350 function action_smartgw()
351 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
356 local function compare(a,b)
357 if a.proto == b.proto then
358 return a.tcPathCost < b.tcPathCost
360 return a.proto < b.proto
364 table.sort(data, compare)
365 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
368 function action_interfaces()
369 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
374 local function compare(a,b)
375 return a.proto < b.proto
378 table.sort(data, compare)
379 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
383 function fetch_jsoninfo(otable)
384 local uci = require "luci.model.uci".cursor_state()
385 local utl = require "luci.util"
386 local json = require "luci.json"
387 local jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
389 local jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
396 if jsonreq4 == '' and jsonreq6 == '' then
397 luci.template.render("status-olsr/error_olsr")
398 return nil, 0, 0, true
401 if #jsonreq4 ~= 0 then
403 jsondata4 = json.decode(jsonreq4)
404 if otable == 'status' then
405 data4 = jsondata4 or {}
407 data4 = jsondata4[otable] or {}
410 for k, v in ipairs(data4) do
411 data4[k]['proto'] = '4'
415 if #jsonreq6 ~= 0 then
417 jsondata6 = json.decode(jsonreq6)
418 if otable == 'status' then
419 data6 = jsondata6 or {}
421 data6 = jsondata6[otable] or {}
423 for k, v in ipairs(data6) do
424 data6[k]['proto'] = '6'
428 for k, v in ipairs(data6) do
429 table.insert(data4, v)
432 return data4, has_v4, has_v6, false