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")
120 if not jsonreq4 or jsonreq4 == "" then
123 if not jsonreq6 or jsonreq6 == "" then
126 http.write('{"v4":' .. jsonreq4 .. ', "v6":' .. jsonreq6 .. '}')
129 function action_neigh(json)
130 local data, has_v4, has_v6, error = fetch_jsoninfo('links')
136 local uci = require "luci.model.uci".cursor_state()
137 local resolve = uci:get("luci_olsr", "general", "resolve")
138 local ntm = require "luci.model.network".init()
139 local devices = ntm:get_wifidevs()
140 local sys = require "luci.sys"
142 local neightbl = require "neightbl"
143 local ipc = require "luci.ip"
145 luci.sys.net.routes(function(r)
146 if r.dest:prefix() == 0 then
147 defaultgw = r.gateway:string()
151 local function compare(a,b)
152 if a.proto == b.proto then
153 return a.linkCost < b.linkCost
155 return a.proto < b.proto
159 for _, dev in ipairs(devices) do
160 for _, net in ipairs(dev:get_wifinets()) do
161 assoclist[#assoclist+1] = {}
162 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
163 assoclist[#assoclist]['network'] = net.iwdata.network
164 assoclist[#assoclist]['device'] = net.iwdata.device
165 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
169 for k, v in ipairs(data) do
174 local arptable = sys.net.arptable()
181 if resolve == "1" then
182 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
184 v.hostname = hostname
187 if v.proto == '4' then
188 uci:foreach("network", "interface",function(vif)
189 if vif.ipaddr and vif.ipaddr == v.localIP then
190 interface = vif['.name'] or vif.interface
191 lmac = string.lower(vif.macaddr or "")
195 for _, arpt in ipairs(arptable) do
196 ip = arpt['IP address']
197 if ip == v.remoteIP then
198 rmac = string.lower(arpt['HW address'] or "")
201 elseif v.proto == '6' then
202 uci:foreach("network", "interface",function(vif)
203 local name = vif['.name']
204 local net = ntm:get_network(name)
205 local device = net and net:get_interface()
206 local locip = ipc.IPv6(v.localIP)
207 if device and device:ip6addrs() and locip then
208 for _, a in ipairs(device:ip6addrs()) do
209 if not a:is6linklocal() then
210 if a:host() == locip:host() then
212 neihgt = neightbl.get(device.ifname) or {}
218 for ip,mac in pairs(neihgt) do
219 if ip == v.remoteIP then
224 for _, val in ipairs(assoclist) do
225 if val.network == interface and val.list then
226 for assocmac, assot in pairs(val.list) do
227 assocmac = string.lower(assocmac or "")
228 if rmac == assocmac then
229 signal = tonumber(assot.signal)
230 noise = tonumber(assot.noise)
231 snr = (noise*-1) - (signal*-1)
237 v.interface = interface
249 if defaultgw == v.remoteIP then
254 table.sort(data, compare)
255 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
258 function action_routes()
259 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
264 local uci = require "luci.model.uci".cursor_state()
265 local resolve = uci:get("luci_olsr", "general", "resolve")
267 for k, v in ipairs(data) do
268 if resolve == "1" then
269 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
271 v.hostname = hostname
276 local function compare(a,b)
277 if a.proto == b.proto then
278 return a.rtpMetricCost < b.rtpMetricCost
280 return a.proto < b.proto
284 table.sort(data, compare)
285 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
288 function action_topology()
289 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
294 local function compare(a,b)
295 if a.proto == b.proto then
296 return a.tcEdgeCost < b.tcEdgeCost
298 return a.proto < b.proto
302 table.sort(data, compare)
303 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
306 function action_hna()
307 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
312 local uci = require "luci.model.uci".cursor_state()
313 local resolve = uci:get("luci_olsr", "general", "resolve")
315 local function compare(a,b)
316 if a.proto == b.proto then
317 return a.genmask < b.genmask
319 return a.proto < b.proto
323 for k, v in ipairs(data) do
324 if resolve == "1" then
325 hostname = nixio.getnameinfo(v.gateway, nil, 100)
327 v.hostname = hostname
330 if v.validityTime then
331 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
335 table.sort(data, compare)
336 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
339 function action_mid()
340 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
345 local function compare(a,b)
346 if a.proto == b.proto then
347 return a.ipAddress < b.ipAddress
349 return a.proto < b.proto
353 table.sort(data, compare)
354 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
357 function action_smartgw()
358 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
363 local function compare(a,b)
364 if a.proto == b.proto then
365 return a.tcPathCost < b.tcPathCost
367 return a.proto < b.proto
371 table.sort(data, compare)
372 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
375 function action_interfaces()
376 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
381 local function compare(a,b)
382 return a.proto < b.proto
385 table.sort(data, compare)
386 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
390 function fetch_jsoninfo(otable)
391 local uci = require "luci.model.uci".cursor_state()
392 local utl = require "luci.util"
393 local json = require "luci.json"
394 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
397 if IpVersion == "4" or IpVersion == "6and4" then
398 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
400 if IpVersion == "6" or IpVersion == "6and4" then
401 jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
410 if jsonreq4 == '' and jsonreq6 == '' then
411 luci.template.render("status-olsr/error_olsr")
412 return nil, 0, 0, true
415 if jsonreq4 ~= "" then
417 jsondata4 = json.decode(jsonreq4)
418 if otable == 'status' then
419 data4 = jsondata4 or {}
421 data4 = jsondata4[otable] or {}
424 for k, v in ipairs(data4) do
425 data4[k]['proto'] = '4'
429 if jsonreq6 ~= "" then
431 jsondata6 = json.decode(jsonreq6)
432 if otable == 'status' then
433 data6 = jsondata6 or {}
435 data6 = jsondata6[otable] or {}
437 for k, v in ipairs(data6) do
438 data6[k]['proto'] = '6'
442 for k, v in ipairs(data6) do
443 table.insert(data4, v)
446 return data4, has_v4, has_v6, false