applications/luci-olsr: Fix overview page for ipv4 or ipv6 only
[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
109         local jsonreq4 = utl.exec("echo /status | nc 127.0.0.1 9090")
110         local jsonreq6 = utl.exec("echo /status | nc ::1 9090")
111         http.prepare_content("application/json")
112
113         if #jsonreq4 < 1 then
114                 jsonreq4 = "{}"
115         end
116
117         if #jsonreq6 < 1 then
118                 jsonreq6 = "{}"
119         end
120
121         http.write("{v4:" .. jsonreq4 .. ", v6:" .. jsonreq6 .. "}")
122 end
123
124 function action_neigh(json)
125         local data, has_v4, has_v6, error = fetch_jsoninfo('links')
126
127         if error then
128                 return
129         end
130
131         local uci = require "luci.model.uci".cursor_state()
132         local resolve = uci:get("luci_olsr", "general", "resolve")
133         luci.sys.net.routes(function(r) if r.dest:prefix() == 0 then defaultgw = r.gateway:string() end end)
134
135         local function compare(a,b)
136                 if a.proto == b.proto then
137                         return a.linkCost < b.linkCost
138                 else
139                         return a.proto < b.proto
140                 end
141         end
142
143         for k, v in ipairs(data) do
144                 if resolve == "1" then
145                         hostname = nixio.getnameinfo(v.remoteIP, nil, 100)
146                         if hostname then
147                                 v.hostname = hostname
148                         end
149                 end
150                 if defaultgw == v.remoteIP then
151                         v.defaultgw = 1
152                 end
153         end
154
155         table.sort(data, compare)
156         luci.template.render("status-olsr/neighbors", {links=data, has_v4=has_v4, has_v6=has_v6})
157 end
158
159 function action_routes()
160         local data, has_v4, has_v6, error = fetch_jsoninfo('routes')
161         if error then
162                 return
163         end
164
165         local uci = require "luci.model.uci".cursor_state()
166         local resolve = uci:get("luci_olsr", "general", "resolve")
167
168         for k, v in ipairs(data) do
169                 if resolve == "1" then
170                         local hostname = nixio.getnameinfo(v.gateway, nil, 100)
171                         if hostname then
172                                 v.hostname = hostname
173                         end
174                 end
175         end
176
177         local function compare(a,b)
178                 if a.proto == b.proto then
179                         return a.rtpMetricCost < b.rtpMetricCost
180                 else
181                         return a.proto < b.proto
182                 end
183         end
184
185         table.sort(data, compare)
186         luci.template.render("status-olsr/routes", {routes=data, has_v4=has_v4, has_v6=has_v6})
187 end
188
189 function action_topology()
190         local data, has_v4, has_v6, error = fetch_jsoninfo('topology')
191         if error then
192                 return
193         end
194
195         local function compare(a,b)
196                 if a.proto == b.proto then
197                         return a.tcEdgeCost < b.tcEdgeCost
198                 else
199                         return a.proto < b.proto
200                 end
201         end
202
203         table.sort(data, compare)
204         luci.template.render("status-olsr/topology", {routes=data, has_v4=has_v4, has_v6=has_v6})
205 end
206
207 function action_hna()
208         local data, has_v4, has_v6, error = fetch_jsoninfo('hna')
209         if error then
210                 return
211         end
212
213         local uci = require "luci.model.uci".cursor_state()
214         local resolve = uci:get("luci_olsr", "general", "resolve")
215
216         local function compare(a,b)
217                 if a.proto == b.proto then
218                         return a.genmask < b.genmask
219                 else
220                         return a.proto < b.proto
221                 end
222         end
223
224         for k, v in ipairs(data) do
225                 if resolve == "1" then
226                         hostname = nixio.getnameinfo(v.gateway, nil, 100)
227                         if hostname then
228                                 v.hostname = hostname
229                         end
230                 end
231                 if v.validityTime then
232                         v.validityTime = tonumber(string.format("%.0f", v.validityTime / 1000))
233                 end
234         end
235
236         table.sort(data, compare)
237         luci.template.render("status-olsr/hna", {hna=data, has_v4=has_v4, has_v6=has_v6})
238 end
239
240 function action_mid()
241         local data, has_v4, has_v6, error = fetch_jsoninfo('mid')
242         if error then
243                 return
244         end
245
246         local function compare(a,b)
247                 if a.proto == b.proto then
248                         return a.ipAddress < b.ipAddress
249                 else
250                         return a.proto < b.proto
251                 end
252         end
253
254         table.sort(data, compare)
255         luci.template.render("status-olsr/mid", {mids=data, has_v4=has_v4, has_v6=has_v6})
256 end
257
258 function action_smartgw()
259         local data, has_v4, has_v6, error = fetch_jsoninfo('gateways')
260         if error then
261                 return
262         end
263
264         local function compare(a,b)
265                 if a.proto == b.proto then
266                         return a.tcPathCost < b.tcPathCost
267                 else
268                         return a.proto < b.proto
269                 end
270         end
271
272         table.sort(data, compare)
273         luci.template.render("status-olsr/smartgw", {gws=data, has_v4=has_v4, has_v6=has_v6})
274 end
275
276 function action_interfaces()
277         local data, has_v4, has_v6, error = fetch_jsoninfo('interfaces')
278         if error then
279                 return
280         end
281
282         local function compare(a,b)
283                 return a.proto < b.proto
284         end
285
286         table.sort(data, compare)
287         luci.template.render("status-olsr/interfaces", {iface=data, has_v4=has_v4, has_v6=has_v6})
288 end
289
290 -- Internal
291 function fetch_jsoninfo(otable)
292         local utl = require "luci.util"
293         local json = require "luci.json"
294         local jsonreq4 = utl.exec("echo /" .. otable .. " | nc 127.0.0.1 9090")
295         local jsondata4 = {}
296         local jsonreq6 = utl.exec("echo /" .. otable .. " | nc ::1 9090")
297         local jsondata6 = {}
298         local data4 = {}
299         local data6 = {}
300         local has_v4 = False
301         local has_v6 = False
302
303         if jsonreq4 == '' and jsonreq6 == '' then
304                 luci.template.render("status-olsr/error_olsr")
305                 return nil, 0, 0, true
306         end
307
308         if #jsonreq4 ~= 0 then
309                 has_v4 = 1
310                 jsondata4 = json.decode(jsonreq4)
311                 if otable == 'status' then
312                         data4 = jsondata4
313                 else
314                         data4 = jsondata4[otable]
315                 end
316
317                 for k, v in ipairs(data4) do
318                     data4[k]['proto'] = '4'
319                 end
320         end
321         if #jsonreq6 ~= 0 then
322                 has_v6 = 1
323                 jsondata6 = json.decode(jsonreq6)
324                 if otable == 'status' then
325                         data6 = jsondata6
326                 else
327                         data6 = jsondata6[otable]
328                 end
329                 for k, v in ipairs(data6) do
330                     data6[k]['proto'] = '6'
331                 end
332         end
333
334         for k, v in ipairs(data6) do
335                 table.insert(data4, v)
336         end
337
338         return data4, has_v4, has_v6, false
339 end
340