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