1 module("luci.controller.olsr", package.seeall)
3 local neigh_table = nil
4 local ifaddr_table = nil
8 if nixio.fs.access("/etc/config/olsrd") then
11 if nixio.fs.access("/etc/config/olsrd6") then
14 if not ipv4 and not ipv6 then
18 require("luci.model.uci")
19 local uci = luci.model.uci.cursor_state()
21 uci:foreach("olsrd", "olsrd", function(s)
22 if s.SmartGateway and s.SmartGateway == "yes" then has_smartgw = true end
25 local page = node("admin", "status", "olsr")
26 page.target = template("status-olsr/overview")
27 page.title = _("OLSR")
30 local page = node("admin", "status", "olsr", "json")
31 page.target = call("action_json")
35 local page = node("admin", "status", "olsr", "neighbors")
36 page.target = call("action_neigh")
37 page.title = _("Neighbours")
41 local page = node("admin", "status", "olsr", "routes")
42 page.target = call("action_routes")
43 page.title = _("Routes")
46 local page = node("admin", "status", "olsr", "topology")
47 page.target = call("action_topology")
48 page.title = _("Topology")
51 local page = node("admin", "status", "olsr", "hna")
52 page.target = call("action_hna")
56 local page = node("admin", "status", "olsr", "mid")
57 page.target = call("action_mid")
62 local page = node("admin", "status", "olsr", "smartgw")
63 page.target = call("action_smartgw")
64 page.title = _("SmartGW")
68 local page = node("admin", "status", "olsr", "interfaces")
69 page.target = call("action_interfaces")
70 page.title = _("Interfaces")
74 {"admin", "services", "olsrd", "display"},
75 cbi("olsr/olsrddisplay"), _("Display")
80 function action_json()
81 local http = require "luci.http"
82 local utl = require "luci.util"
83 local uci = require "luci.model.uci".cursor_state()
87 jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
88 jsonreq6 = utl.exec("echo /status | nc ::1 9090")
89 http.prepare_content("application/json")
90 if not jsonreq4 or jsonreq4 == "" then
93 if not jsonreq6 or jsonreq6 == "" then
96 http.write('{"v4":' .. jsonreq4 .. ', "v6":' .. jsonreq6 .. '}')
100 local function local_mac_lookup(ipaddr)
103 ipaddr = tostring(ipaddr)
105 if not ifaddr_table then
106 ifaddr_table = nixio.getifaddrs()
110 for _, ifa in ipairs(ifaddr_table) do
111 if ifa.addr == ipaddr then
118 for _, ifa in ipairs(ifaddr_table) do
119 if ifa.name == dev and ifa.family == "packet" then
125 local function remote_mac_lookup(ipaddr)
128 if not neigh_table then
129 neigh_table = luci.ip.neighbors()
132 for _, n in ipairs(neigh_table) do
133 if n.mac and n.dest and n.dest:equal(ipaddr) then
139 function action_neigh(json)
140 local data, has_v4, has_v6, error = fetch_jsoninfo('links')
146 local uci = require "luci.model.uci".cursor_state()
147 local resolve = uci:get("luci_olsr", "general", "resolve")
148 local ntm = require "luci.model.network".init()
149 local devices = ntm:get_wifidevs()
150 local sys = require "luci.sys"
152 --local neightbl = require "neightbl"
153 local ntm = require "luci.model.network"
154 local ipc = require "luci.ip"
155 local nxo = require "nixio"
158 ipc.routes({ family = 4, type = 1, dest_exact = "0.0.0.0/0" },
159 function(rt) defaultgw = rt.gw end)
161 local function compare(a,b)
162 if a.proto == b.proto then
163 return a.linkCost < b.linkCost
165 return a.proto < b.proto
169 for _, dev in ipairs(devices) do
170 for _, net in ipairs(dev:get_wifinets()) do
171 assoclist[#assoclist+1] = {}
172 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
173 assoclist[#assoclist]['network'] = net.iwdata.network
174 assoclist[#assoclist]['device'] = net.iwdata.device
175 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
179 for k, v in ipairs(data) do
187 if resolve == "1" then
188 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
190 v.hostname = hostname
194 local interface = ntm:get_status_by_address(v.localIP)
195 local lmac = local_mac_lookup(v.localIP)
196 local rmac = remote_mac_lookup(v.remoteIP)
198 for _, val in ipairs(assoclist) do
199 if val.network == interface and val.list then
200 for assocmac, assot in pairs(val.list) do
201 assocmac = string.lower(assocmac or "")
202 if rmac == assocmac then
203 signal = tonumber(assot.signal)
204 noise = tonumber(assot.noise)
205 snr = (noise*-1) - (signal*-1)
211 v.interface = interface
223 if defaultgw == v.remoteIP then
228 table.sort(data, compare)
229 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
232 function action_routes()
233 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
238 local uci = require "luci.model.uci".cursor_state()
239 local resolve = uci:get("luci_olsr", "general", "resolve")
241 for k, v in ipairs(data) do
242 if resolve == "1" then
243 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
245 v.hostname = hostname
250 local function compare(a,b)
251 if a.proto == b.proto then
252 return a.rtpMetricCost < b.rtpMetricCost
254 return a.proto < b.proto
258 table.sort(data, compare)
259 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
262 function action_topology()
263 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
268 local function compare(a,b)
269 if a.proto == b.proto then
270 return a.tcEdgeCost < b.tcEdgeCost
272 return a.proto < b.proto
276 table.sort(data, compare)
277 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
280 function action_hna()
281 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
286 local uci = require "luci.model.uci".cursor_state()
287 local resolve = uci:get("luci_olsr", "general", "resolve")
289 local function compare(a,b)
290 if a.proto == b.proto then
291 return a.genmask < b.genmask
293 return a.proto < b.proto
297 for k, v in ipairs(data) do
298 if resolve == "1" then
299 hostname = nixio.getnameinfo(v.gateway, nil, 100)
301 v.hostname = hostname
304 if v.validityTime then
305 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
309 table.sort(data, compare)
310 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
313 function action_mid()
314 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
319 local function compare(a,b)
320 if a.proto == b.proto then
321 return a.ipAddress < b.ipAddress
323 return a.proto < b.proto
327 table.sort(data, compare)
328 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
331 function action_smartgw()
332 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
337 local function compare(a,b)
338 if a.proto == b.proto then
339 return a.tcPathCost < b.tcPathCost
341 return a.proto < b.proto
345 table.sort(data, compare)
346 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
349 function action_interfaces()
350 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
355 local function compare(a,b)
356 return a.proto < b.proto
359 table.sort(data, compare)
360 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
364 function fetch_jsoninfo(otable)
365 local uci = require "luci.model.uci".cursor_state()
366 local utl = require "luci.util"
367 local json = require "luci.json"
368 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
371 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
372 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 ~= "" 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 ~= "" 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