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"
138 luci.sys.net.routes(function(r)
139 if r.dest:prefix() == 0 then
140 defaultgw = r.gateway:string()
144 local function compare(a,b)
145 if a.proto == b.proto then
146 return a.linkCost < b.linkCost
148 return a.proto < b.proto
152 for _, dev in ipairs(devices) do
153 for _, net in ipairs(dev:get_wifinets()) do
154 assoclist[#assoclist+1] = {}
155 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
156 assoclist[#assoclist]['network'] = net.iwdata.network
157 assoclist[#assoclist]['device'] = net.iwdata.device
158 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
162 for k, v in ipairs(data) do
167 local arptable = sys.net.arptable()
173 if resolve == "1" then
174 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
176 v.hostname = hostname
179 if v.proto == '4' then
180 uci:foreach("network", "interface",function(vif)
181 if vif.ipaddr and vif.ipaddr == v.localIP then
182 interface = vif['.name'] or vif.interface
183 lmac = string.lower(vif.macaddr or "")
187 for _, arpt in ipairs(arptable) do
188 ip = arpt['IP address']
189 if ip == v.remoteIP then
190 rmac = string.lower(arpt['HW address'] or "")
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)
205 elseif v.proto == '6' then
206 uci:foreach("network", "interface",function(vif)
207 if vif.ip6addr and string.gsub(vif.ip6addr, "/64", "") == v.localIP then
208 interface = vif['.name'] or vif.interface
214 v.interface = interface
226 if defaultgw == v.remoteIP then
231 table.sort(data, compare)
232 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
235 function action_routes()
236 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
241 local uci = require "luci.model.uci".cursor_state()
242 local resolve = uci:get("luci_olsr", "general", "resolve")
244 for k, v in ipairs(data) do
245 if resolve == "1" then
246 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
248 v.hostname = hostname
253 local function compare(a,b)
254 if a.proto == b.proto then
255 return a.rtpMetricCost < b.rtpMetricCost
257 return a.proto < b.proto
261 table.sort(data, compare)
262 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
265 function action_topology()
266 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
271 local function compare(a,b)
272 if a.proto == b.proto then
273 return a.tcEdgeCost < b.tcEdgeCost
275 return a.proto < b.proto
279 table.sort(data, compare)
280 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
283 function action_hna()
284 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
289 local uci = require "luci.model.uci".cursor_state()
290 local resolve = uci:get("luci_olsr", "general", "resolve")
292 local function compare(a,b)
293 if a.proto == b.proto then
294 return a.genmask < b.genmask
296 return a.proto < b.proto
300 for k, v in ipairs(data) do
301 if resolve == "1" then
302 hostname = nixio.getnameinfo(v.gateway, nil, 100)
304 v.hostname = hostname
307 if v.validityTime then
308 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
312 table.sort(data, compare)
313 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
316 function action_mid()
317 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
322 local function compare(a,b)
323 if a.proto == b.proto then
324 return a.ipAddress < b.ipAddress
326 return a.proto < b.proto
330 table.sort(data, compare)
331 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
334 function action_smartgw()
335 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
340 local function compare(a,b)
341 if a.proto == b.proto then
342 return a.tcPathCost < b.tcPathCost
344 return a.proto < b.proto
348 table.sort(data, compare)
349 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
352 function action_interfaces()
353 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
358 local function compare(a,b)
359 return a.proto < b.proto
362 table.sort(data, compare)
363 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
367 function fetch_jsoninfo(otable)
368 local uci = require "luci.model.uci".cursor_state()
369 local utl = require "luci.util"
370 local json = require "luci.json"
371 local jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
373 local jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
380 if jsonreq4 == '' and jsonreq6 == '' then
381 luci.template.render("status-olsr/error_olsr")
382 return nil, 0, 0, true
385 if #jsonreq4 ~= 0 then
387 jsondata4 = json.decode(jsonreq4)
388 if otable == 'status' then
389 data4 = jsondata4 or {}
391 data4 = jsondata4[otable] or {}
394 for k, v in ipairs(data4) do
395 data4[k]['proto'] = '4'
399 if #jsonreq6 ~= 0 then
401 jsondata6 = json.decode(jsonreq6)
402 if otable == 'status' then
403 data6 = jsondata6 or {}
405 data6 = jsondata6[otable] or {}
407 for k, v in ipairs(data6) do
408 data6[k]['proto'] = '6'
412 for k, v in ipairs(data6) do
413 table.insert(data4, v)
416 return data4, has_v4, has_v6, false