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