applications/status-olsr: Disable neightbl calls in olsrd neighbors view because...
[project/luci.git] / applications / luci-olsr / luasrc / controller / olsr.lua
1 module("luci.controller.olsr", package.seeall)
2
3 function index()
4 local ipv4,ipv6
5 if nixio.fs.access("/etc/config/olsrd") then
6 ipv4 = 1
7 end
8 if nixio.fs.access("/etc/config/olsrd6") then
9 ipv6 = 1
10 end
11 if not ipv4 and not ipv6 then
12 return
13 end
14
15 require("luci.model.uci")
16 local uci = luci.model.uci.cursor_state()
17
18 uci:foreach("olsrd", "olsrd", function(s)
19 if s.SmartGateway and s.SmartGateway == "yes" then has_smartgw = true end
20 end)
21
22 local page = node("admin", "status", "olsr")
23 page.target = template("status-olsr/overview")
24 page.title = _("OLSR")
25 page.subindex = true
26
27 local page = node("admin", "status", "olsr", "json")
28 page.target = call("action_json")
29 page.title = nil
30 page.leaf = true
31
32 local page = node("admin", "status", "olsr", "neighbors")
33 page.target = call("action_neigh")
34 page.title = _("Neighbours")
35 page.subindex = true
36 page.order = 5
37
38 local page = node("admin", "status", "olsr", "routes")
39 page.target = call("action_routes")
40 page.title = _("Routes")
41 page.order = 10
42
43 local page = node("admin", "status", "olsr", "topology")
44 page.target = call("action_topology")
45 page.title = _("Topology")
46 page.order = 20
47
48 local page = node("admin", "status", "olsr", "hna")
49 page.target = call("action_hna")
50 page.title = _("HNA")
51 page.order = 30
52
53 local page = node("admin", "status", "olsr", "mid")
54 page.target = call("action_mid")
55 page.title = _("MID")
56 page.order = 50
57
58 if has_smartgw then
59 local page = node("admin", "status", "olsr", "smartgw")
60 page.target = call("action_smartgw")
61 page.title = _("SmartGW")
62 page.order = 60
63 end
64
65 local page = node("admin", "status", "olsr", "interfaces")
66 page.target = call("action_interfaces")
67 page.title = _("Interfaces")
68 page.order = 70
69
70 odsp = entry(
71 {"admin", "services", "olsrd", "display"},
72 cbi("olsr/olsrddisplay"), _("Display")
73 )
74
75 end
76
77 function action_json()
78 local http = require "luci.http"
79 local utl = require "luci.util"
80 local uci = require "luci.model.uci".cursor_state()
81 local jsonreq4
82 local jsonreq6
83
84 jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
85 jsonreq6 = utl.exec("echo /status | nc ::1 9090")
86 http.prepare_content("application/json")
87 if not jsonreq4 or jsonreq4 == "" then
88 jsonreq4 = "{}"
89 end
90 if not jsonreq6 or jsonreq6 == "" then
91 jsonreq6 = "{}"
92 end
93 http.write('{"v4":' .. jsonreq4 .. ', "v6":' .. jsonreq6 .. '}')
94 end
95
96 function action_neigh(json)
97 local data, has_v4, has_v6, error = fetch_jsoninfo('links')
98
99 if error then
100 return
101 end
102
103 local uci = require "luci.model.uci".cursor_state()
104 local resolve = uci:get("luci_olsr", "general", "resolve")
105 local ntm = require "luci.model.network".init()
106 local devices = ntm:get_wifidevs()
107 local sys = require "luci.sys"
108 local assoclist = {}
109 --local neightbl = require "neightbl"
110 local ipc = require "luci.ip"
111
112 luci.sys.net.routes(function(r)
113 if r.dest:prefix() == 0 then
114 defaultgw = r.gateway:string()
115 end
116 end)
117
118 local function compare(a,b)
119 if a.proto == b.proto then
120 return a.linkCost < b.linkCost
121 else
122 return a.proto < b.proto
123 end
124 end
125
126 for _, dev in ipairs(devices) do
127 for _, net in ipairs(dev:get_wifinets()) do
128 assoclist[#assoclist+1] = {}
129 assoclist[#assoclist]['ifname'] = net.iwdata.ifname
130 assoclist[#assoclist]['network'] = net.iwdata.network
131 assoclist[#assoclist]['device'] = net.iwdata.device
132 assoclist[#assoclist]['list'] = net.iwinfo.assoclist
133 end
134 end
135
136 for k, v in ipairs(data) do
137 local interface
138 local snr = 0
139 local signal = 0
140 local noise = 0
141 local arptable = sys.net.arptable()
142 local mac = ""
143 local rmac = ""
144 local lmac = ""
145 local ip
146 local neihgt = {}
147
148 if resolve == "1" then
149 hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
150 if hostname then
151 v.hostname = hostname
152 end
153 end
154 if v.proto == '4' then
155 uci:foreach("network", "interface",function(vif)
156 if vif.ipaddr and vif.ipaddr == v.localIP then
157 interface = vif['.name'] or vif.interface
158 lmac = string.lower(vif.macaddr or "")
159 return
160 end
161 end)
162 for _, arpt in ipairs(arptable) do
163 ip = arpt['IP address']
164 if ip == v.remoteIP then
165 rmac = string.lower(arpt['HW address'] or "")
166 end
167 end
168 elseif v.proto == '6' then
169 uci:foreach("network", "interface",function(vif)
170 local name = vif['.name']
171 local net = ntm:get_network(name)
172 local device = net and net:get_interface()
173 local locip = ipc.IPv6(v.localIP)
174 if device and device:ip6addrs() and locip then
175 for _, a in ipairs(device:ip6addrs()) do
176 if not a:is6linklocal() then
177 if a:host() == locip:host() then
178 interface = name
179 --neihgt = neightbl.get(device.ifname) or {}
180 end
181 end
182 end
183 end
184 end)
185 --[[
186 for ip,mac in pairs(neihgt) do
187 if ip == v.remoteIP then
188 rmac = mac
189 end
190 end
191 ]]--
192 end
193 for _, val in ipairs(assoclist) do
194 if val.network == interface and val.list then
195 for assocmac, assot in pairs(val.list) do
196 assocmac = string.lower(assocmac or "")
197 if rmac == assocmac then
198 signal = tonumber(assot.signal)
199 noise = tonumber(assot.noise)
200 snr = (noise*-1) - (signal*-1)
201 end
202 end
203 end
204 end
205 if interface then
206 v.interface = interface
207 end
208 v.snr = snr
209 v.signal = signal
210 v.noise = noise
211 if rmac then
212 v.remoteMAC = rmac
213 end
214 if lmac then
215 v.localMAC = lmac
216 end
217
218 if defaultgw == v.remoteIP then
219 v.defaultgw = 1
220 end
221 end
222
223 table.sort(data, compare)
224 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
225 end
226
227 function action_routes()
228 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
229 if error then
230 return
231 end
232
233 local uci = require "luci.model.uci".cursor_state()
234 local resolve = uci:get("luci_olsr", "general", "resolve")
235
236 for k, v in ipairs(data) do
237 if resolve == "1" then
238 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
239 if hostname then
240 v.hostname = hostname
241 end
242 end
243 end
244
245 local function compare(a,b)
246 if a.proto == b.proto then
247 return a.rtpMetricCost < b.rtpMetricCost
248 else
249 return a.proto < b.proto
250 end
251 end
252
253 table.sort(data, compare)
254 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
255 end
256
257 function action_topology()
258 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
259 if error then
260 return
261 end
262
263 local function compare(a,b)
264 if a.proto == b.proto then
265 return a.tcEdgeCost < b.tcEdgeCost
266 else
267 return a.proto < b.proto
268 end
269 end
270
271 table.sort(data, compare)
272 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
273 end
274
275 function action_hna()
276 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
277 if error then
278 return
279 end
280
281 local uci = require "luci.model.uci".cursor_state()
282 local resolve = uci:get("luci_olsr", "general", "resolve")
283
284 local function compare(a,b)
285 if a.proto == b.proto then
286 return a.genmask < b.genmask
287 else
288 return a.proto < b.proto
289 end
290 end
291
292 for k, v in ipairs(data) do
293 if resolve == "1" then
294 hostname = nixio.getnameinfo(v.gateway, nil, 100)
295 if hostname then
296 v.hostname = hostname
297 end
298 end
299 if v.validityTime then
300 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
301 end
302 end
303
304 table.sort(data, compare)
305 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
306 end
307
308 function action_mid()
309 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
310 if error then
311 return
312 end
313
314 local function compare(a,b)
315 if a.proto == b.proto then
316 return a.ipAddress < b.ipAddress
317 else
318 return a.proto < b.proto
319 end
320 end
321
322 table.sort(data, compare)
323 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
324 end
325
326 function action_smartgw()
327 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
328 if error then
329 return
330 end
331
332 local function compare(a,b)
333 if a.proto == b.proto then
334 return a.tcPathCost < b.tcPathCost
335 else
336 return a.proto < b.proto
337 end
338 end
339
340 table.sort(data, compare)
341 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
342 end
343
344 function action_interfaces()
345 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
346 if error then
347 return
348 end
349
350 local function compare(a,b)
351 return a.proto < b.proto
352 end
353
354 table.sort(data, compare)
355 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
356 end
357
358 -- Internal
359 function fetch_jsoninfo(otable)
360 local uci = require "luci.model.uci".cursor_state()
361 local utl = require "luci.util"
362 local json = require "luci.json"
363 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
364 local jsonreq4 = ""
365 local jsonreq6 = ""
366 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
367 jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
368 local jsondata4 = {}
369 local jsondata6 = {}
370 local data4 = {}
371 local data6 = {}
372 local has_v4 = False
373 local has_v6 = False
374
375 if jsonreq4 == '' and jsonreq6 == '' then
376 luci.template.render("status-olsr/error_olsr")
377 return nil, 0, 0, true
378 end
379
380 if jsonreq4 ~= "" then
381 has_v4 = 1
382 jsondata4 = json.decode(jsonreq4)
383 if otable == 'status' then
384 data4 = jsondata4 or {}
385 else
386 data4 = jsondata4[otable] or {}
387 end
388
389 for k, v in ipairs(data4) do
390 data4[k]['proto'] = '4'
391 end
392
393 end
394 if jsonreq6 ~= "" then
395 has_v6 = 1
396 jsondata6 = json.decode(jsonreq6)
397 if otable == 'status' then
398 data6 = jsondata6 or {}
399 else
400 data6 = jsondata6[otable] or {}
401 end
402 for k, v in ipairs(data6) do
403 data6[k]['proto'] = '6'
404 end
405 end
406
407 for k, v in ipairs(data6) do
408 table.insert(data4, v)
409 end
410
411 return data4, has_v4, has_v6, false
412 end
413