bef7e52eb170dd5cc4e542c9cd5b7a5331cf2279
[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
109         local jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
110         local jsonreq6 = utl.exec("echo /status | nc ::1 9090")
111         http.prepare_content("application/json")
112
113         if #jsonreq4 < 1 then
114                 jsonreq4 = "{}"
115         end
116
117         if #jsonreq6 < 1 then
118                 jsonreq6 = "{}"
119         end
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
139         luci.sys.net.routes(function(r) 
140                 if r.dest:prefix() == 0 then 
141                         defaultgw = r.gateway:string() 
142                 end
143         end)
144
145         local function compare(a,b)
146                 if a.proto == b.proto then
147                         return a.linkCost < b.linkCost
148                 else
149                         return a.proto < b.proto
150                 end
151         end
152
153         for _, dev in ipairs(devices) do
154                 for _, net in ipairs(dev:get_wifinets()) do
155                         assoclist[#assoclist+1] = {} 
156                         assoclist[#assoclist]['ifname'] = net.iwdata.ifname
157                         assoclist[#assoclist]['network'] = net.iwdata.network
158                         assoclist[#assoclist]['device'] = net.iwdata.device
159                         assoclist[#assoclist]['list'] = net.iwinfo.assoclist
160                 end
161         end
162
163         for k, v in ipairs(data) do
164                 local interface
165                 local snr = 1
166                 local signal = 1
167                 local noise = 1
168                 local arptable = sys.net.arptable()
169                 local mac = ""
170                 local rmac = ""
171                 local lmac = ""
172                 local ip
173                 local neihgt = {}
174                 
175                 if resolve == "1" then
176                         hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
177                         if hostname then
178                                 v.hostname = hostname
179                         end
180                 end
181                 if v.proto == '4' then
182                         uci:foreach("network", "interface",function(vif)
183                                 if vif.ipaddr and vif.ipaddr == v.localIP then
184                                         interface = vif['.name'] or vif.interface
185                                         lmac = string.lower(vif.macaddr or "") 
186                                         return
187                                 end
188                         end)
189                         for _, arpt in ipairs(arptable) do
190                                 ip = arpt['IP address']
191                                 if ip == v.remoteIP then
192                                         rmac = string.lower(arpt['HW address'] or "")
193                                 end
194                         end
195                 elseif v.proto == '6' then
196                         local uprefix = uci:get("network", "globals", "ula_prefix") or ""
197                         uprefix = string.gsub(uprefix, "::/.*", "")
198                         ip6assign_c = 0
199                         uci:foreach("network", "interface",function(vif)
200                                 local ip6assign = vif.ip6assign or 0
201                                 if ip6assign ~= 0 then
202                                         if ip6assign_c == 0 then
203                                                 ip6assign_addr = uprefix.."::1"
204                                         else
205                                                 ip6assign_addr = uprefix..":"..ip6assign_c.."::1"
206                                         end
207                                         ip6assign_c = ip6assign_c + 1
208                                 end
209                                 if ip6assign_addr == v.localIP then
210                                         interface = vif['.name'] or vif.interface
211                                         neihgt = neightbl.get(vif.ifname) or {}
212                                         return
213                                 elseif vif.ip6addr and string.gsub(vif.ip6addr, "/"..ip6assign, "") == v.localIP then
214                                         interface = vif['.name'] or vif.interface
215                                         neihgt = neightbl.get(vif.ifname) or {}
216                                         return
217                                 end
218                         end)
219                         for ip,mac in pairs(neihgt) do
220                                 if ip == v.remoteIP then
221                                         rmac = mac
222                                 end
223                         end
224                 end
225                 for _, val in ipairs(assoclist) do
226                         if val.network == interface and val.list then
227                                 for assocmac, assot in pairs(val.list) do
228                                         assocmac = string.lower(assocmac or "")
229                                         if rmac == assocmac then
230                                                 signal = tonumber(assot.signal)
231                                                 noise = tonumber(assot.noise)
232                                                 snr = signal/noise
233                                         end
234                                 end
235                         end
236                 end
237                 if interface then
238                         v.interface = interface
239                 end
240                 v.snr = snr
241                 v.signal = signal
242                 v.noise = noise
243                 if rmac then
244                         v.remoteMAC = rmac
245                 end
246                 if lmac then
247                         v.localMAC = lmac
248                 end
249
250                 if defaultgw == v.remoteIP then
251                         v.defaultgw = 1
252                 end
253         end
254
255         table.sort(data, compare)
256         luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
257 end
258
259 function action_routes()
260         local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
261         if error then
262                 return
263         end
264
265         local uci = require "luci.model.uci".cursor_state()
266         local resolve = uci:get("luci_olsr", "general", "resolve")
267
268         for k, v in ipairs(data) do
269                 if resolve == "1" then
270                         local hostname = nixio.getnameinfo(v.gateway, nil, 100)
271                         if hostname then
272                                 v.hostname = hostname
273                         end
274                 end
275         end
276
277         local function compare(a,b)
278                 if a.proto == b.proto then
279                         return a.rtpMetricCost < b.rtpMetricCost
280                 else
281                         return a.proto < b.proto
282                 end
283         end
284
285         table.sort(data, compare)
286         luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
287 end
288
289 function action_topology()
290         local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
291         if error then
292                 return
293         end
294
295         local function compare(a,b)
296                 if a.proto == b.proto then
297                         return a.tcEdgeCost < b.tcEdgeCost
298                 else
299                         return a.proto < b.proto
300                 end
301         end
302
303         table.sort(data, compare)
304         luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
305 end
306
307 function action_hna()
308         local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
309         if error then
310                 return
311         end
312
313         local uci = require "luci.model.uci".cursor_state()
314         local resolve = uci:get("luci_olsr", "general", "resolve")
315
316         local function compare(a,b)
317                 if a.proto == b.proto then
318                         return a.genmask < b.genmask
319                 else
320                         return a.proto < b.proto
321                 end
322         end
323
324         for k, v in ipairs(data) do
325                 if resolve == "1" then
326                         hostname = nixio.getnameinfo(v.gateway, nil, 100)
327                         if hostname then
328                                 v.hostname = hostname
329                         end
330                 end
331                 if v.validityTime then
332                         v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
333                 end
334         end
335
336         table.sort(data, compare)
337         luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
338 end
339
340 function action_mid()
341         local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
342         if error then
343                 return
344         end
345
346         local function compare(a,b)
347                 if a.proto == b.proto then
348                         return a.ipAddress < b.ipAddress
349                 else
350                         return a.proto < b.proto
351                 end
352         end
353
354         table.sort(data, compare)
355         luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
356 end
357
358 function action_smartgw()
359         local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
360         if error then
361                 return
362         end
363
364         local function compare(a,b)
365                 if a.proto == b.proto then
366                         return a.tcPathCost < b.tcPathCost
367                 else
368                         return a.proto < b.proto
369                 end
370         end
371
372         table.sort(data, compare)
373         luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
374 end
375
376 function action_interfaces()
377         local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
378         if error then
379                 return
380         end
381
382         local function compare(a,b)
383                 return a.proto < b.proto
384         end
385
386         table.sort(data, compare)
387         luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
388 end
389
390 -- Internal
391 function fetch_jsoninfo(otable)
392         local uci = require "luci.model.uci".cursor_state()
393         local utl = require "luci.util"
394         local json = require "luci.json"
395         local jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
396         local jsondata4 = {}
397         local jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
398         local jsondata6 = {}
399         local data4 = {}
400         local data6 = {}
401         local has_v4 = False
402         local has_v6 = False
403
404         if jsonreq4 == '' and jsonreq6 == '' then
405                 luci.template.render("status-olsr/error_olsr")
406                 return nil, 0, 0, true
407         end
408
409         if #jsonreq4 ~= 0 then
410                 has_v4 = 1
411                 jsondata4 = json.decode(jsonreq4)
412                 if otable == 'status' then
413                         data4 = jsondata4 or {}
414                 else
415                         data4 = jsondata4[otable] or {}
416                 end
417
418                 for k, v in ipairs(data4) do
419                         data4[k]['proto'] = '4'
420                 end
421
422         end
423         if #jsonreq6 ~= 0 then
424                 has_v6 = 1
425                 jsondata6 = json.decode(jsonreq6)
426                 if otable == 'status' then
427                         data6 = jsondata6 or {}
428                 else
429                         data6 = jsondata6[otable] or {}
430                 end
431                 for k, v in ipairs(data6) do
432                         data6[k]['proto'] = '6'
433                 end
434         end
435
436         for k, v in ipairs(data6) do
437                 table.insert(data4, v)
438         end
439
440         return data4, has_v4, has_v6, false
441 end
442