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