cf3743f46a863023adf7a9b19bec195c62138d6b
[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 for _, a in ipairs(device:ip6addrs()) do
203 if not a:is6linklocal() then
204 if a:host() == locip:host() then
205 interface = name
206 neihgt = neightbl.get(device.ifname) or {}
207 end
208 end
209 end
210 end)
211 for ip,mac in pairs(neihgt) do
212 if ip == v.remoteIP then
213 rmac = mac
214 end
215 end
216 end
217 for _, val in ipairs(assoclist) do
218 if val.network == interface and val.list then
219 for assocmac, assot in pairs(val.list) do
220 assocmac = string.lower(assocmac or "")
221 if rmac == assocmac then
222 signal = tonumber(assot.signal)
223 noise = tonumber(assot.noise)
224 snr = (noise*-1) - (signal*-1)
225 end
226 end
227 end
228 end
229 if interface then
230 v.interface = interface
231 end
232 v.snr = snr
233 v.signal = signal
234 v.noise = noise
235 if rmac then
236 v.remoteMAC = rmac
237 end
238 if lmac then
239 v.localMAC = lmac
240 end
241
242 if defaultgw == v.remoteIP then
243 v.defaultgw = 1
244 end
245 end
246
247 table.sort(data, compare)
248 luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
249 end
250
251 function action_routes()
252 local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
253 if error then
254 return
255 end
256
257 local uci = require "luci.model.uci".cursor_state()
258 local resolve = uci:get("luci_olsr", "general", "resolve")
259
260 for k, v in ipairs(data) do
261 if resolve == "1" then
262 local hostname = nixio.getnameinfo(v.gateway, nil, 100)
263 if hostname then
264 v.hostname = hostname
265 end
266 end
267 end
268
269 local function compare(a,b)
270 if a.proto == b.proto then
271 return a.rtpMetricCost < b.rtpMetricCost
272 else
273 return a.proto < b.proto
274 end
275 end
276
277 table.sort(data, compare)
278 luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
279 end
280
281 function action_topology()
282 local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
283 if error then
284 return
285 end
286
287 local function compare(a,b)
288 if a.proto == b.proto then
289 return a.tcEdgeCost < b.tcEdgeCost
290 else
291 return a.proto < b.proto
292 end
293 end
294
295 table.sort(data, compare)
296 luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
297 end
298
299 function action_hna()
300 local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
301 if error then
302 return
303 end
304
305 local uci = require "luci.model.uci".cursor_state()
306 local resolve = uci:get("luci_olsr", "general", "resolve")
307
308 local function compare(a,b)
309 if a.proto == b.proto then
310 return a.genmask < b.genmask
311 else
312 return a.proto < b.proto
313 end
314 end
315
316 for k, v in ipairs(data) do
317 if resolve == "1" then
318 hostname = nixio.getnameinfo(v.gateway, nil, 100)
319 if hostname then
320 v.hostname = hostname
321 end
322 end
323 if v.validityTime then
324 v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
325 end
326 end
327
328 table.sort(data, compare)
329 luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
330 end
331
332 function action_mid()
333 local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
334 if error then
335 return
336 end
337
338 local function compare(a,b)
339 if a.proto == b.proto then
340 return a.ipAddress < b.ipAddress
341 else
342 return a.proto < b.proto
343 end
344 end
345
346 table.sort(data, compare)
347 luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
348 end
349
350 function action_smartgw()
351 local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
352 if error then
353 return
354 end
355
356 local function compare(a,b)
357 if a.proto == b.proto then
358 return a.tcPathCost < b.tcPathCost
359 else
360 return a.proto < b.proto
361 end
362 end
363
364 table.sort(data, compare)
365 luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
366 end
367
368 function action_interfaces()
369 local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
370 if error then
371 return
372 end
373
374 local function compare(a,b)
375 return a.proto < b.proto
376 end
377
378 table.sort(data, compare)
379 luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
380 end
381
382 -- Internal
383 function fetch_jsoninfo(otable)
384 local uci = require "luci.model.uci".cursor_state()
385 local utl = require "luci.util"
386 local json = require "luci.json"
387 local IpVersion = uci:get_first("olsrd", "olsrd","IpVersion")
388 local jsonreq4 = ""
389 local jsonreq6 = ""
390 if IpVersion == "4" or IpVersion == "6and4" then
391 jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
392 end
393 if IpVersion == "6" or IpVersion == "6and4" then
394 jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
395 end
396 local jsondata4 = {}
397 local jsondata6 = {}
398 local data4 = {}
399 local data6 = {}
400 local has_v4 = False
401 local has_v6 = False
402
403 if jsonreq4 == '' and jsonreq6 == '' then
404 luci.template.render("status-olsr/error_olsr")
405 return nil, 0, 0, true
406 end
407
408 if jsonreq4 ~= "" then
409 has_v4 = 1
410 jsondata4 = json.decode(jsonreq4)
411 if otable == 'status' then
412 data4 = jsondata4 or {}
413 else
414 data4 = jsondata4[otable] or {}
415 end
416
417 for k, v in ipairs(data4) do
418 data4[k]['proto'] = '4'
419 end
420
421 end
422 if jsonreq6 ~= "" then
423 has_v6 = 1
424 jsondata6 = json.decode(jsonreq6)
425 if otable == 'status' then
426 data6 = jsondata6 or {}
427 else
428 data6 = jsondata6[otable] or {}
429 end
430 for k, v in ipairs(data6) do
431 data6[k]['proto'] = '6'
432 end
433 end
434
435 for k, v in ipairs(data6) do
436 table.insert(data4, v)
437 end
438
439 return data4, has_v4, has_v6, false
440 end
441