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