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