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()
87 local v4_port = uci:get("olsrd", "olsrd_jsoninfo", "port") or 9090
88 local v6_port = uci:get("olsrd6", "olsrd_jsoninfo", "port") or 9090
90 jsonreq4 = utl.exec("(echo /status | nc 127.0.0.1 " .. v4_port .. " | sed -n '/^[}{ ]/p') 2>/dev/null" )
91 jsonreq6 = utl.exec("(echo /status | nc ::1 " .. v6_port .. " | sed -n '/^[}{ ]/p') 2>/dev/null")
92 http.prepare_content("application/json")
93 if not jsonreq4 or jsonreq4 == "" then
96 if not jsonreq6 or jsonreq6 == "" then
99 http.write('{"v4":' .. jsonreq4 .. ', "v6":' .. jsonreq6 .. '}')
103 local function local_mac_lookup(ipaddr)
106 ipaddr = tostring(ipaddr)
108 if not ifaddr_table then
109 ifaddr_table = nixio.getifaddrs()
113 for _, ifa in ipairs(ifaddr_table) do
114 if ifa.addr == ipaddr then
121 for _, ifa in ipairs(ifaddr_table) do
122 if ifa.name == dev and ifa.family == "packet" then
128 local function remote_mac_lookup(ipaddr)
131 if not neigh_table then
132 neigh_table = luci.ip.neighbors()
135 for _, n in ipairs(neigh_table) do
136 if n.mac and n.dest and n.dest:equal(ipaddr) then
142 function action_neigh(json)
143 local data, has_v4, has_v6, error = fetch_jsoninfo('links')
149 local uci = require "luci.model.uci".cursor_state()
150 local resolve = uci:get("luci_olsr", "general", "resolve")
151 local ntm = require "luci.model.network".init()
152 local devices = ntm:get_wifidevs()
153 local sys = require "luci.sys"
155 --local neightbl = require "neightbl"
156 local ntm = require "luci.model.network"
157 local ipc = require "luci.ip"
158 local nxo = require "nixio"
161 ipc.routes({ family = 4, type = 1, dest_exact = "0.0.0.0/0" },
162 function(rt) defaultgw = rt.gw end)
164 local function compare(a,b)
165 if a.proto == b.proto then
166 return a.linkCost < b.linkCost
168 return a.proto < b.proto
172 for _, dev in ipairs(devices) do
173 for _, net in ipairs(dev:get_wifinets()) do
174 local radio = net:get_device()
175 assoclist[#assoclist+1] = {}
176 assoclist[#assoclist]['ifname'] = net:ifname()
177 assoclist[#assoclist]['network'] = net:network()[1]
178 assoclist[#assoclist]['device'] = radio and radio:name() or nil
179 assoclist[#assoclist]['list'] = net:assoclist()
183 for k, v in ipairs(data) do
191 if resolve == "1" then
192 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
194 v.hostname = hostname
198 local interface = ntm:get_status_by_address(v.localIP)
199 local lmac = local_mac_lookup(v.localIP)
200 local rmac = remote_mac_lookup(v.remoteIP)
202 for _, val in ipairs(assoclist) do
203 if val.network == interface and val.list then
204 for assocmac, assot in pairs(val.list) do
205 assocmac = string.lower(assocmac or "")
206 if rmac == assocmac then
207 signal = tonumber(assot.signal)
208 noise = tonumber(assot.noise)
209 snr = (noise*-1) - (signal*-1)
215 v.interface = interface
227 if defaultgw == v.remoteIP then
232 table.sort(data, compare)
233 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
236 function action_routes()
237 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
242 local uci = require "luci.model.uci".cursor_state()
243 local resolve = uci:get("luci_olsr", "general", "resolve")
245 for k, v in ipairs(data) do
246 if resolve == "1" then
247 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
249 v.hostname = hostname
254 local function compare(a,b)
255 if a.proto == b.proto then
256 return a.rtpMetricCost < b.rtpMetricCost
258 return a.proto < b.proto
262 table.sort(data, compare)
263 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
266 function action_topology()
267 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
272 local function compare(a,b)
273 if a.proto == b.proto then
274 return a.tcEdgeCost < b.tcEdgeCost
276 return a.proto < b.proto
280 table.sort(data, compare)
281 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
284 function action_hna()
285 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
290 local uci = require "luci.model.uci".cursor_state()
291 local resolve = uci:get("luci_olsr", "general", "resolve")
293 local function compare(a,b)
294 if a.proto == b.proto then
295 return a.genmask < b.genmask
297 return a.proto < b.proto
301 for k, v in ipairs(data) do
302 if resolve == "1" then
303 hostname = nixio.getnameinfo(v.gateway, nil, 100)
305 v.hostname = hostname
308 if v.validityTime then
309 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
313 table.sort(data, compare)
314 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
317 function action_mid()
318 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
323 local function compare(a,b)
324 if a.proto == b.proto then
325 return a.ipAddress < b.ipAddress
327 return a.proto < b.proto
331 table.sort(data, compare)
332 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
335 function action_smartgw()
336 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
341 local function compare(a,b)
342 if a.proto == b.proto then
343 return a.tcPathCost < b.tcPathCost
345 return a.proto < b.proto
349 table.sort(data, compare)
350 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
353 function action_interfaces()
354 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
359 local function compare(a,b)
360 return a.proto < b.proto
363 table.sort(data, compare)
364 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
368 function fetch_jsoninfo(otable)
369 local uci = require "luci.model.uci".cursor_state()
370 local utl = require "luci.util"
371 local json = require "luci.json"
372 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
375 local v4_port = uci:get("olsrd", "olsrd_jsoninfo", "port") or 9090
376 local v6_port = uci:get("olsrd6", "olsrd_jsoninfo", "port") or 9090
378 jsonreq4 = utl.exec("(echo /" .. otable .. " | nc 127.0.0.1 " .. v4_port .. " | sed -n '/^[}{ ]/p') 2>/dev/null")
379 jsonreq6 = utl.exec("(echo /" .. otable .. " | nc ::1 " .. v6_port .. " | sed -n '/^[}{ ]/p') 2>/dev/null")
387 if jsonreq4 == '' and jsonreq6 == '' then
388 luci.template.render("status-olsr/error_olsr")
389 return nil, 0, 0, true
392 if jsonreq4 ~= "" then
394 jsondata4 = json.decode(jsonreq4)
395 if otable == 'status' then
396 data4 = jsondata4 or {}
398 data4 = jsondata4[otable] or {}
401 for k, v in ipairs(data4) do
402 data4[k]['proto'] = '4'
406 if jsonreq6 ~= "" then
408 jsondata6 = json.decode(jsonreq6)
409 if otable == 'status' then
410 data6 = jsondata6 or {}
412 data6 = jsondata6[otable] or {}
414 for k, v in ipairs(data6) do
415 data6[k]['proto'] = '6'
419 for k, v in ipairs(data6) do
420 table.insert(data4, v)
423 return data4, has_v4, has_v6, false