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"
139 luci.sys.net.routes(function(r)
140 if r.dest:prefix() == 0 then
141 defaultgw = r.gateway:string()
145 local function compare(a,b)
146 if a.proto == b.proto then
147 return a.linkCost < b.linkCost
149 return a.proto < b.proto
153 for _, dev in ipairs(devices) do
154 for _, net in ipairs(dev:get_wifinets()) do
155 assoclist[#assoclist+1] = {}
156 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
157 assoclist[#assoclist]['network'] = net.iwdata.network
158 assoclist[#assoclist]['device'] = net.iwdata.device
159 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
163 for k, v in ipairs(data) do
168 local arptable = sys.net.arptable()
175 if resolve == "1" then
176 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
178 v.hostname = hostname
181 if v.proto == '4' then
182 uci:foreach("network", "interface",function(vif)
183 if vif.ipaddr and vif.ipaddr == v.localIP then
184 interface = vif['.name'] or vif.interface
185 lmac = string.lower(vif.macaddr or "")
189 for _, arpt in ipairs(arptable) do
190 ip = arpt['IP address']
191 if ip == v.remoteIP then
192 rmac = string.lower(arpt['HW address'] or "")
195 elseif v.proto == '6' then
196 local uprefix = uci:get("network", "globals", "ula_prefix") or ""
197 uprefix = string.gsub(uprefix, "::/.*", "")
199 uci:foreach("network", "interface",function(vif)
200 local ip6assign = vif.ip6assign or 0
201 if ip6assign ~= 0 then
202 if ip6assign_c == 0 then
203 ip6assign_addr = uprefix.."::1"
205 ip6assign_addr = uprefix..":"..ip6assign_c.."::1"
207 ip6assign_c = ip6assign_c + 1
209 if ip6assign_addr == v.localIP then
210 interface = vif['.name'] or vif.interface
211 neihgt = neightbl.get(vif.ifname) or {}
213 elseif vif.ip6addr and string.gsub(vif.ip6addr, "/"..ip6assign, "") == v.localIP then
214 interface = vif['.name'] or vif.interface
215 neihgt = neightbl.get(vif.ifname) or {}
219 for ip,mac in pairs(neihgt) do
220 if ip == v.remoteIP then
225 for _, val in ipairs(assoclist) do
226 if val.network == interface and val.list then
227 for assocmac, assot in pairs(val.list) do
228 assocmac = string.lower(assocmac or "")
229 if rmac == assocmac then
230 signal = tonumber(assot.signal)
231 noise = tonumber(assot.noise)
238 v.interface = interface
250 if defaultgw == v.remoteIP then
255 table.sort(data, compare)
256 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
259 function action_routes()
260 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
265 local uci = require "luci.model.uci".cursor_state()
266 local resolve = uci:get("luci_olsr", "general", "resolve")
268 for k, v in ipairs(data) do
269 if resolve == "1" then
270 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
272 v.hostname = hostname
277 local function compare(a,b)
278 if a.proto == b.proto then
279 return a.rtpMetricCost < b.rtpMetricCost
281 return a.proto < b.proto
285 table.sort(data, compare)
286 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
289 function action_topology()
290 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
295 local function compare(a,b)
296 if a.proto == b.proto then
297 return a.tcEdgeCost < b.tcEdgeCost
299 return a.proto < b.proto
303 table.sort(data, compare)
304 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
307 function action_hna()
308 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
313 local uci = require "luci.model.uci".cursor_state()
314 local resolve = uci:get("luci_olsr", "general", "resolve")
316 local function compare(a,b)
317 if a.proto == b.proto then
318 return a.genmask < b.genmask
320 return a.proto < b.proto
324 for k, v in ipairs(data) do
325 if resolve == "1" then
326 hostname = nixio.getnameinfo(v.gateway, nil, 100)
328 v.hostname = hostname
331 if v.validityTime then
332 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
336 table.sort(data, compare)
337 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
340 function action_mid()
341 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
346 local function compare(a,b)
347 if a.proto == b.proto then
348 return a.ipAddress < b.ipAddress
350 return a.proto < b.proto
354 table.sort(data, compare)
355 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
358 function action_smartgw()
359 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
364 local function compare(a,b)
365 if a.proto == b.proto then
366 return a.tcPathCost < b.tcPathCost
368 return a.proto < b.proto
372 table.sort(data, compare)
373 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
376 function action_interfaces()
377 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
382 local function compare(a,b)
383 return a.proto < b.proto
386 table.sort(data, compare)
387 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
391 function fetch_jsoninfo(otable)
392 local uci = require "luci.model.uci".cursor_state()
393 local utl = require "luci.util"
394 local json = require "luci.json"
395 local jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
397 local jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
404 if jsonreq4 == '' and jsonreq6 == '' then
405 luci.template.render("status-olsr/error_olsr")
406 return nil, 0, 0, true
409 if #jsonreq4 ~= 0 then
411 jsondata4 = json.decode(jsonreq4)
412 if otable == 'status' then
413 data4 = jsondata4 or {}
415 data4 = jsondata4[otable] or {}
418 for k, v in ipairs(data4) do
419 data4[k]['proto'] = '4'
423 if #jsonreq6 ~= 0 then
425 jsondata6 = json.decode(jsonreq6)
426 if otable == 'status' then
427 data6 = jsondata6 or {}
429 data6 = jsondata6[otable] or {}
431 for k, v in ipairs(data6) do
432 data6[k]['proto'] = '6'
436 for k, v in ipairs(data6) do
437 table.insert(data4, v)
440 return data4, has_v4, has_v6, false