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"
108 local uci = require "luci.model.uci".cursor_state()
112 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
113 if IpVersion == "4" or IpVersion == "6and4" then
114 jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
116 if IpVersion == "6" or IpVersion == "6and4" then
117 jsonreq6 = utl.exec("echo /status | nc ::1 9090")
119 http.prepare_content("application/json")
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 if device and device:ip6addrs() and locip then
203 for _, a in ipairs(device:ip6addrs()) do
204 if not a:is6linklocal() then
205 if a:host() == locip:host() then
207 neihgt = neightbl.get(device.ifname) or {}
213 for ip,mac in pairs(neihgt) do
214 if ip == v.remoteIP then
219 for _, val in ipairs(assoclist) do
220 if val.network == interface and val.list then
221 for assocmac, assot in pairs(val.list) do
222 assocmac = string.lower(assocmac or "")
223 if rmac == assocmac then
224 signal = tonumber(assot.signal)
225 noise = tonumber(assot.noise)
226 snr = (noise*-1) - (signal*-1)
232 v.interface = interface
244 if defaultgw == v.remoteIP then
249 table.sort(data, compare)
250 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
253 function action_routes()
254 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
259 local uci = require "luci.model.uci".cursor_state()
260 local resolve = uci:get("luci_olsr", "general", "resolve")
262 for k, v in ipairs(data) do
263 if resolve == "1" then
264 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
266 v.hostname = hostname
271 local function compare(a,b)
272 if a.proto == b.proto then
273 return a.rtpMetricCost < b.rtpMetricCost
275 return a.proto < b.proto
279 table.sort(data, compare)
280 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
283 function action_topology()
284 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
289 local function compare(a,b)
290 if a.proto == b.proto then
291 return a.tcEdgeCost < b.tcEdgeCost
293 return a.proto < b.proto
297 table.sort(data, compare)
298 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
301 function action_hna()
302 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
307 local uci = require "luci.model.uci".cursor_state()
308 local resolve = uci:get("luci_olsr", "general", "resolve")
310 local function compare(a,b)
311 if a.proto == b.proto then
312 return a.genmask < b.genmask
314 return a.proto < b.proto
318 for k, v in ipairs(data) do
319 if resolve == "1" then
320 hostname = nixio.getnameinfo(v.gateway, nil, 100)
322 v.hostname = hostname
325 if v.validityTime then
326 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
330 table.sort(data, compare)
331 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
334 function action_mid()
335 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
340 local function compare(a,b)
341 if a.proto == b.proto then
342 return a.ipAddress < b.ipAddress
344 return a.proto < b.proto
348 table.sort(data, compare)
349 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
352 function action_smartgw()
353 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
358 local function compare(a,b)
359 if a.proto == b.proto then
360 return a.tcPathCost < b.tcPathCost
362 return a.proto < b.proto
366 table.sort(data, compare)
367 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
370 function action_interfaces()
371 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
376 local function compare(a,b)
377 return a.proto < b.proto
380 table.sort(data, compare)
381 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
385 function fetch_jsoninfo(otable)
386 local uci = require "luci.model.uci".cursor_state()
387 local utl = require "luci.util"
388 local json = require "luci.json"
389 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
392 if IpVersion == "4" or IpVersion == "6and4" then
393 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
395 if IpVersion == "6" or IpVersion == "6and4" then
396 jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
405 if jsonreq4 == '' and jsonreq6 == '' then
406 luci.template.render("status-olsr/error_olsr")
407 return nil, 0, 0, true
410 if jsonreq4 ~= "" then
412 jsondata4 = json.decode(jsonreq4)
413 if otable == 'status' then
414 data4 = jsondata4 or {}
416 data4 = jsondata4[otable] or {}
419 for k, v in ipairs(data4) do
420 data4[k]['proto'] = '4'
424 if jsonreq6 ~= "" then
426 jsondata6 = json.decode(jsonreq6)
427 if otable == 'status' then
428 data6 = jsondata6 or {}
430 data6 = jsondata6[otable] or {}
432 for k, v in ipairs(data6) do
433 data6[k]['proto'] = '6'
437 for k, v in ipairs(data6) do
438 table.insert(data4, v)
441 return data4, has_v4, has_v6, false