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 )
91 jsonreq6 = utl.exec("echo /status | nc ::1 " .. v6_port)
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 assoclist[#assoclist+1] = {}
175 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
176 assoclist[#assoclist]['network'] = net.iwdata.network
177 assoclist[#assoclist]['device'] = net.iwdata.device
178 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
182 for k, v in ipairs(data) do
190 if resolve == "1" then
191 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
193 v.hostname = hostname
197 local interface = ntm:get_status_by_address(v.localIP)
198 local lmac = local_mac_lookup(v.localIP)
199 local rmac = remote_mac_lookup(v.remoteIP)
201 for _, val in ipairs(assoclist) do
202 if val.network == interface and val.list then
203 for assocmac, assot in pairs(val.list) do
204 assocmac = string.lower(assocmac or "")
205 if rmac == assocmac then
206 signal = tonumber(assot.signal)
207 noise = tonumber(assot.noise)
208 snr = (noise*-1) - (signal*-1)
214 v.interface = interface
226 if defaultgw == v.remoteIP then
231 table.sort(data, compare)
232 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
235 function action_routes()
236 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
241 local uci = require "luci.model.uci".cursor_state()
242 local resolve = uci:get("luci_olsr", "general", "resolve")
244 for k, v in ipairs(data) do
245 if resolve == "1" then
246 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
248 v.hostname = hostname
253 local function compare(a,b)
254 if a.proto == b.proto then
255 return a.rtpMetricCost < b.rtpMetricCost
257 return a.proto < b.proto
261 table.sort(data, compare)
262 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
265 function action_topology()
266 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
271 local function compare(a,b)
272 if a.proto == b.proto then
273 return a.tcEdgeCost < b.tcEdgeCost
275 return a.proto < b.proto
279 table.sort(data, compare)
280 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
283 function action_hna()
284 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
289 local uci = require "luci.model.uci".cursor_state()
290 local resolve = uci:get("luci_olsr", "general", "resolve")
292 local function compare(a,b)
293 if a.proto == b.proto then
294 return a.genmask < b.genmask
296 return a.proto < b.proto
300 for k, v in ipairs(data) do
301 if resolve == "1" then
302 hostname = nixio.getnameinfo(v.gateway, nil, 100)
304 v.hostname = hostname
307 if v.validityTime then
308 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
312 table.sort(data, compare)
313 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
316 function action_mid()
317 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
322 local function compare(a,b)
323 if a.proto == b.proto then
324 return a.ipAddress < b.ipAddress
326 return a.proto < b.proto
330 table.sort(data, compare)
331 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
334 function action_smartgw()
335 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
340 local function compare(a,b)
341 if a.proto == b.proto then
342 return a.tcPathCost < b.tcPathCost
344 return a.proto < b.proto
348 table.sort(data, compare)
349 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
352 function action_interfaces()
353 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
358 local function compare(a,b)
359 return a.proto < b.proto
362 table.sort(data, compare)
363 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
367 function fetch_jsoninfo(otable)
368 local uci = require "luci.model.uci".cursor_state()
369 local utl = require "luci.util"
370 local json = require "luci.json"
371 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
374 local v4_port = uci:get("olsrd", "olsrd_jsoninfo", "port") or 9090
375 local v6_port = uci:get("olsrd6", "olsrd_jsoninfo", "port") or 9090
377 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 " .. v4_port)
378 jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 " .. v6_port)
386 if jsonreq4 == '' and jsonreq6 == '' then
387 luci.template.render("status-olsr/error_olsr")
388 return nil, 0, 0, true
391 if jsonreq4 ~= "" then
393 jsondata4 = json.decode(jsonreq4)
394 if otable == 'status' then
395 data4 = jsondata4 or {}
397 data4 = jsondata4[otable] or {}
400 for k, v in ipairs(data4) do
401 data4[k]['proto'] = '4'
405 if jsonreq6 ~= "" then
407 jsondata6 = json.decode(jsonreq6)
408 if otable == 'status' then
409 data6 = jsondata6 or {}
411 data6 = jsondata6[otable] or {}
413 for k, v in ipairs(data6) do
414 data6[k]['proto'] = '6'
418 for k, v in ipairs(data6) do
419 table.insert(data4, v)
422 return data4, has_v4, has_v6, false