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