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