371bad870735e69b36ffe8e7add43b2b20b77f85
[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         local page  = node("admin", "status", "olsr")
9         page.target = template("status-olsr/overview")
10         page.title  = _("OLSR")
11         page.i18n   = "olsr"
12         page.subindex = true
13
14         local page  = node("admin", "status", "olsr", "neighbors")
15         page.target = call("action_neigh")
16         page.title  = _("Neighbours")
17         page.subindex = true
18         page.order  = 5
19
20         local page  = node("admin", "status", "olsr", "routes")
21         page.target = call("action_routes")
22         page.title  = _("Routes")
23         page.order  = 10
24
25         local page  = node("admin", "status", "olsr", "topology")
26         page.target = call("action_topology")
27         page.title  = _("Topology")
28         page.order  = 20
29
30         local page  = node("admin", "status", "olsr", "hna")
31         page.target = call("action_hna")
32         page.title  = _("HNA")
33         page.order  = 30
34
35         local page  = node("admin", "status", "olsr", "mid")
36         page.target = call("action_mid")
37         page.title  = _("MID")
38         page.order  = 50
39
40         local page  = node("admin", "status", "olsr", "smartgw")
41         page.target = call("action_smartgw")
42         page.title  = _("SmartGW")
43         page.order  = 60
44
45         local page  = node("admin", "status", "olsr", "interfaces")
46         page.target = call("action_interfaces")
47         page.title  = _("Interfaces")
48         page.order  = 70
49
50         local ol = entry(
51                 {"admin", "services", "olsrd"},
52                 cbi("olsr/olsrd"), "OLSR"
53         )
54         ol.i18n = "olsr"
55         ol.subindex = true
56
57         entry(
58                 {"admin", "services", "olsrd", "iface"},
59                 cbi("olsr/olsrdiface")
60         ).leaf = true
61
62         entry(
63                 {"admin", "services", "olsrd", "hna"},
64                 cbi("olsr/olsrdhna"), _("HNA Announcements")
65         )
66
67         oplg = entry(
68                 {"admin", "services", "olsrd", "plugins"},
69                 cbi("olsr/olsrdplugins"), _("Plugins")
70         )
71
72         odsp = entry(
73                 {"admin", "services", "olsrd", "display"},
74                 cbi("olsr/olsrddisplay"), _("Display")
75                 )
76
77         oplg.i18n = "olsr"
78         oplg.leaf = true
79         oplg.subindex = true
80
81         local uci = require("luci.model.uci").cursor()
82         uci:foreach("olsrd", "LoadPlugin",
83                 function (section)
84                         local lib = section.library
85                         entry(
86                                 {"admin", "services", "olsrd", "plugins", lib },
87                                 cbi("olsr/olsrdplugins"),
88                                 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
89                         )
90                 end
91         )
92 end
93
94 local function compare_links(a, b)
95         local c = tonumber(a.Cost)
96         local d = tonumber(b.Cost)
97
98         if not c or c == 0 then
99                 return false
100         end
101
102         if not d or d == 0 then
103                 return true
104         end
105         return c < d
106 end
107
108 function action_neigh(json)
109         local data = fetch_txtinfo("links")
110
111         if not data or not data.Links then
112                 luci.template.render("status-olsr/error_olsr")
113                 return nil
114         end
115
116         table.sort(data.Links, compare_links)
117
118         luci.template.render("status-olsr/neighbors", {links=data.Links})
119 end
120
121 function action_routes()
122         local data = fetch_txtinfo("routes")
123
124         if not data or not data.Routes then
125                 luci.template.render("status-olsr/error_olsr")
126                 return nil
127         end
128
129         local function compare(a, b)
130                 local c = tonumber(a.ETX)
131                 local d = tonumber(b.ETX)
132
133                 if not c or c == 0 then
134                         return false
135                 end
136
137                 if not d or d == 0 then
138                         return true
139                 end
140
141                 return c < d
142         end
143
144         table.sort(data.Routes, compare)
145
146         luci.template.render("status-olsr/routes", {routes=data.Routes})
147 end
148
149 function action_topology()
150         local data = fetch_txtinfo("topology")
151
152         if not data or not data.Topology then
153                 luci.template.render("status-olsr/error_olsr")
154                 return nil
155         end
156
157         local function compare(a, b)
158                 return a["Dest. IP"] < b["Dest. IP"]
159         end
160
161         table.sort(data.Topology, compare)
162
163         luci.template.render("status-olsr/topology", {routes=data.Topology})
164 end
165
166 function action_hna()
167         local data = fetch_txtinfo("hna")
168
169         if not data or not data.HNA then
170                 luci.template.render("status-olsr/error_olsr")
171                 return nil
172         end
173
174         local function compare(a, b)
175                 return a.Destination < b.Destination
176         end
177
178         table.sort(data.HNA, compare)
179
180         luci.template.render("status-olsr/hna", {routes=data.HNA})
181 end
182
183 function action_mid()
184         local data = fetch_txtinfo("mid")
185
186         if not data or not data.MID then
187                 luci.template.render("status-olsr/error_olsr")
188                 return nil
189         end
190
191         local function compare(a, b)
192                 return a["IP address"] < b["IP address"]
193         end
194
195         table.sort(data.MID, compare)
196
197         luci.template.render("status-olsr/mid", {mids=data.MID})
198 end
199
200 function action_smartgw()
201         local data = fetch_txtinfo("gateways")
202
203         if not data or not data.Gateways then
204                 luci.template.render("status-olsr/error_olsr")
205                 return nil
206         end
207
208         local function compare(a, b)
209                 return a["ETX"] < b["ETX"]
210         end
211
212         table.sort(data.Gateways, compare)
213
214         luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
215 end
216
217 function action_interfaces()
218         local data = fetch_txtinfo("interfaces")
219
220         if not data or not data.Interfaces then
221                 luci.template.render("status-olsr/error_olsr")
222                 return nil
223         end
224
225         luci.template.render("status-olsr/interfaces", {iface=data.Interfaces})
226 end
227
228 -- Internal
229 function fetch_txtinfo(otable)
230         require("luci.sys")
231         local uci = require "luci.model.uci".cursor_state()
232         local resolve = uci:get("luci_olsr", "general", "resolve")
233         otable = otable or ""
234         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
235         local rawdatav6 = luci.sys.httpget("http://[::1]:2006/"..otable)
236         local data = {}
237         local dataindex = 0
238         local name = ""
239         local defaultgw
240
241         if #rawdata ~= 0 then
242                 local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
243
244                 if otable == "links" then
245                         local route = {}
246                         luci.sys.net.routes(function(r) if r.dest:prefix() == 0 then defaultgw = r.gateway:string() end end)
247                 end
248
249                 for i, tbl in ipairs(tables) do
250                         local lines = luci.util.split(tbl, "\r?\n", nil, true)
251                         name  = table.remove(lines, 1):sub(8)
252                         local keys  = luci.util.split(table.remove(lines, 1), "\t")
253                         local split = #keys - 1
254                         if not data[name] then
255                                 data[name] = {}
256                         end
257
258                         for j, line in ipairs(lines) do
259                                 dataindex = ( dataindex + 1 )
260                                 di = dataindex
261                                 local fields = luci.util.split(line, "\t", split)
262                                 data[name][di] = {}
263                                 for k, key in pairs(keys) do
264                                         if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
265                                                 data[name][di][key] = fields[k]
266                                                 if resolve == "1" then
267                                                         hostname = nixio.getnameinfo(fields[k], "inet")
268                                                         if hostname then
269                                                                 data[name][di]["Hostname"] = hostname
270                                                         end
271                                                 end
272                                                 if key == "Remote IP" and defaultgw then
273                                                         if defaultgw == fields[k] then
274                                                                 data[name][di]["defaultgw"] = 1
275                                                         end
276                                                 end
277                                         elseif key == "Local IP" then
278                                                 data[name][di][key] = fields[k]
279                                                 data[name][di]['Local Device'] = fields[k]
280                                                 uci:foreach("network", "interface",
281                                                         function(s)
282                                                                 localip = string.gsub(fields[k], '      ', '')
283                                                                 if s.ipaddr == localip then
284                                                                         data[name][di]['Local Device'] = s['.name'] or interface
285                                                                 end
286                                                         end)
287                                         elseif key == "Interface" then
288                                                 data[name][di][key] = fields[k]
289                                                 uci:foreach("network", "interface",
290                                                 function(s)
291                                                         interface = string.gsub(fields[k], '    ', '')
292                                                         if s.ifname == interface then
293                                                                 data[name][di][key] = s['.name'] or interface
294                                                         end
295                                                 end)
296                                         else
297                                             data[name][di][key] = fields[k]
298                                 end
299                                 end
300                                 if data[name][di].Linkcost then
301                                         data[name][di].LinkQuality,
302                                         data[name][di].NLQ,
303                                         data[name][di].ETX =
304                                         data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
305                                 end
306                         end
307                 end
308         end
309
310         if #rawdatav6 ~= 0 then
311                 local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
312                 for i, tbl in ipairs(tables) do
313                         local lines = luci.util.split(tbl, "\r?\n", nil, true)
314                         name  = table.remove(lines, 1):sub(8)
315                         local keys  = luci.util.split(table.remove(lines, 1), "\t")
316                         local split = #keys - 1
317                         if not data[name] then
318                                 data[name] = {}
319                         end
320                         for j, line in ipairs(lines) do
321                                 dataindex = ( dataindex + 1 )
322                                 di = dataindex
323                                 local fields = luci.util.split(line, "\t", split)
324                                 data[name][di] = {}
325                                 for k, key in pairs(keys) do
326                                         if key == "Remote IP" then
327                                                 data[name][di][key] = "[" .. fields[k] .. "]"
328                                                 if resolve == "1" then
329                                                         hostname = nixio.getnameinfo(fields[k], "inet6")
330                                                         if hostname then
331                                                                 data[name][di]["Hostname"] = hostname
332                                                         end
333                                                 end
334                                         elseif key == "Local IP" then
335                                                 data[name][di][key] = fields[k]
336                                                 data[name][di]['Local Device'] = fields[k]
337                                                 uci:foreach("network", "interface",
338                                                 function(s)
339                                                         local localip = string.gsub(fields[k], '        ', ''):upper()
340                                                         if s.ip6addr then
341                                                                 s.ip6addr = luci.ip.IPv6(s.ip6addr):string()
342                                                                 local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
343                                                                 if ip6addr == localip then
344                                                                         data[name][di]['Local Device'] = s['.name'] or s.interface
345                                                                 end
346                                                         end
347                                                 end)
348                                         elseif key == "Dest. IP" then
349                                                 data[name][di][key] = "[" .. fields[k] .. "]"
350                                         elseif key == "Last hop IP" then
351                                                 data[name][di][key] = "[" .. fields[k] .. "]"
352                                         elseif key == "IP address" then
353                                                 data[name][di][key] = "[" .. fields[k] .. "]"
354                                         elseif key == "Gateway" then
355                                                 data[name][di][key] = "[" .. fields[k] .. "]"
356                                         else
357                                                 data[name][di][key] = fields[k]
358                                         end
359                                 end
360
361                                 if data[name][di].Linkcost then
362                                         data[name][di].LinkQuality,
363                                         data[name][di].NLQ,
364                                         data[name][di].ETX =
365                                         data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
366                                 end
367                         end
368                 end
369         end
370
371
372         if data then
373             return data
374         end
375 end