luci-olsr: Fix Net list netmask parsing
[project/luci.git] / applications / luci-olsr / luasrc / model / cbi / olsr / olsrdplugins.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 require("luci.fs")
17 require("luci.ip")
18
19
20 if arg[1] then
21         mp = Map("olsrd", translate("olsrd_plugins", "OLSR - Plugins"))
22
23         p = mp:section(TypedSection, "LoadPlugin")
24         p:depends("library", arg[1])
25         p.anonymous = true
26
27         ign = p:option(Flag, "ignore", "Enable")
28         ign.enabled  = "0"
29         ign.disabled = "1"
30         ign.rmempty  = false
31         function ign.cfgvalue(self, section)
32                 return Flag.cfgvalue(self, section) or "0"
33         end
34
35         lib = p:option(DummyValue, "library", translate("library"))
36         lib.default = arg[1]
37
38         local function Range(x,y)
39                 local t = {}
40                 for i = x, y do t[#t+1] = i end
41                 return t
42         end
43
44         local function Cidr2IpMask(val)
45                 if val then
46                         for i = 1, #val do
47                                 local cidr = luci.ip.IPv4(val[i]) or luci.ip.IPv6(val[i])
48                                 if cidr then
49                                         val[i] = cidr:network():string() .. " " .. cidr:mask():string()
50                                 end
51                         end
52                         return val
53                 end
54         end
55
56         local function IpMask2Cidr(val)
57                 if val then
58                         for i = 1, #val do
59                                 local ip, mask = val[i]:gmatch("([^%s]+)%s+([^%s]+)")()
60                                 local cidr
61                                 if ip and mask and ip:match(":") then
62                                         cidr = luci.ip.IPv6(ip, mask)
63                                 elseif ip and mask then
64                                         cidr = luci.ip.IPv4(ip, mask)
65                                 end
66
67                                 if cidr then
68                                         val[i] = cidr:string()
69                                 end
70                         end
71                         return val
72                 end
73         end
74
75
76         local knownPlParams = {
77                 ["olsrd_bmf.so.1.5.3"] = {
78                         { Value,                "BmfInterface",                 "bmf0" },
79                         { Value,                "BmfInterfaceIp",               "10.10.10.234/24" },
80                         { Flag,                 "DoLocalBroadcast",             "no" },
81                         { Flag,                 "CapturePacketsOnOlsrInterfaces", "yes" },
82                         { ListValue,    "BmfMechanism",                 { "UnicastPromiscuous", "Broadcast" } },
83                         { Value,                "BroadcastRetransmitCount",     "2" },
84                         { Value,                "FanOutLimit",                  "4" },
85                         { DynamicList,  "NonOlsrIf",                    "br-lan" }
86                 },
87
88                 ["olsrd_dyn_gw.so.0.4"] = {
89                         { Value,                "Interval",                             "40" },
90                         { DynamicList,  "Ping",                                 "141.1.1.1" },
91                         { DynamicList,  "HNA",                                  "192.168.80.0/24", IpMask2Cidr, Cidr2IpMask }
92                 },
93
94                 ["olsrd_httpinfo.so.0.1"] = {
95                         { Value,                "port",                                 "80" },
96                         { DynamicList,  "Host",                                 "163.24.87.3" },
97                         { DynamicList,  "Net",                                  "0.0.0.0/0", IpMask2Cidr, Cidr2IpMask }
98                 },
99
100                 ["olsrd_nameservice.so.0.3"] = {
101                         { DynamicList,  "name",                                 "my-name.mesh" },
102                         { DynamicList,  "hosts",                                "1.2.3.4 name-for-other-interface.mesh" },
103                         { Value,                "suffix",                               ".olsr" },
104                         { Value,                "hosts_file",                   "/path/to/hosts_file" },
105                         { Value,                "add_hosts",                    "/path/to/file" },
106                         { Value,                "dns_server",                   "141.1.1.1" },
107                         { Value,                "resolv_file",                  "/path/to/resolv.conf" },
108                         { Value,                "interval",                             "120" },
109                         { Value,                "timeout",                              "240" },
110                         { Value,                "lat",                                  "12.123" },
111                         { Value,                "lon",                                  "12.123" },
112                         { Value,                "latlon_file",                  "/var/run/latlon.js" },
113                         { Value,                "latlon_infile",                "/var/run/gps.txt" },
114                         { Value,                "sighup_pid_file",              "/var/run/dnsmasq.pid" },
115                         { Value,                "name_change_script",   "/usr/local/bin/announce_new_hosts.sh" },
116                         { Value,                "services_change_script",       "/usr/local/bin/announce_new_services.sh" }
117                 },
118
119                 ["olsrd_quagga.so.0.2.2"] = {
120                         { StaticList,   "redistribute",                 {
121                                 "system", "kernel", "connect", "static", "rip", "ripng", "ospf",
122                                 "ospf6", "isis", "bgp", "hsls"
123                         } },
124                         { ListValue,    "ExportRoutes",                 { "only", "both" } },
125                         { Flag,                 "LocalPref",                    "true" },
126                         { Value,                "Distance",                             Range(0,255) }
127                 },
128
129                 ["olsrd_secure.so.0.5"] = {
130                         { Value,                "Keyfile",                              "/etc/private-olsr.key" }
131                 },
132
133                 ["olsrd_txtinfo.so.0.1"] = {
134                         { Value,                "accept",                               "10.247.200.4" }
135                 },
136
137                 ["olsrd_watchdog.so.0.1"] = {
138                         { Value,                "file",                                 "/var/run/olsrd.watchdog" },
139                         { Value,                "interval",                             "30" }
140                 },
141
142                 ["olsrd_mdns.so.1.0.0"] = {
143                         { DynamicList,  "NonOlsrIf",                    "br-lan" }
144                 },
145
146                 ["olsrd_arprefresh.so.0.1"]             = {},
147                 ["olsrd_dot_draw.so.0.3"]               = {},
148                 ["olsrd_dyn_gw_plain.so.0.4"]   = {},
149                 ["olsrd_pgraph.so.1.1"]                 = {},
150                 ["olsrd_tas.so.0.1"]                    = {}
151         }
152
153
154         -- build plugin options with dependencies
155         if knownPlParams[arg[1]] then
156                 for _, option in ipairs(knownPlParams[arg[1]]) do
157                         local otype, name, default, uci2cbi, cbi2uci = unpack(option)
158                         local values
159
160                         if type(default) == "table" then
161                                 values  = default
162                                 default = default[1]
163                         end
164
165                         if otype == Flag then
166                                 local bool = p:option( Flag, name )
167                                 if default == "yes" or default == "no" then
168                                         bool.enabled  = "yes"
169                                         bool.disabled = "no"
170                                 elseif default == "on" or default == "off" then
171                                         bool.enabled  = "on"
172                                         bool.disabled = "off"
173                                 elseif default == "1" or default == "0" then
174                                         bool.enabled  = "1"
175                                         bool.disabled = "0"
176                                 else
177                                         bool.enabled  = "true"
178                                         bool.disabled = "false"
179                                 end
180                                 bool.optional = true
181                                 bool.default = default
182                                 bool:depends({ library = plugin })
183                         else
184                                 local field = p:option( otype, name )
185                                 if values then
186                                         for _, value in ipairs(values) do
187                                                 field:value( value )
188                                         end
189                                 end
190                                 if type(uci2cbi) == "function" then
191                                         function field.cfgvalue(self, section)
192                                                 return uci2cbi(otype.cfgvalue(self, section))
193                                         end
194                                 end
195                                 if type(cbi2uci) == "function" then
196                                         function field.formvalue(self, section)
197                                                 return cbi2uci(otype.formvalue(self, section))
198                                         end
199                                 end
200                                 field.optional = true
201                                 field.default = default
202                                 --field:depends({ library = arg[1] })
203                         end
204                 end
205         end
206
207         return mp
208
209 else
210
211         mpi = Map("olsrd", "OLSR - Plugins")
212
213         local plugins = {}
214         mpi.uci:foreach("olsrd", "LoadPlugin",
215                 function(section)
216                         if section.library and not plugins[section.library] then
217                                 plugins[section.library] = true
218                         end
219                 end
220         )
221
222         -- create a loadplugin section for each found plugin
223         for k, v in pairs(luci.fs.dir("/usr/lib")) do
224                 if v:sub(1, 6) == "olsrd_" then
225                         if not plugins[v] then
226                                 mpi.uci:section(
227                                         "olsrd", "LoadPlugin", nil,
228                                         { library = v, ignore = 1 }
229                                 )
230                         end
231                 end
232         end
233
234         t = mpi:section( TypedSection, "LoadPlugin", "Plugins" )
235         t.anonymous = true
236         t.template  = "cbi/tblsection"
237         t.override_scheme = true
238         function t.extedit(self, section)
239                 local lib = self.map:get(section, "library") or ""
240                 return luci.dispatcher.build_url("admin", "services", "olsrd", "plugins") .. "/" .. lib
241         end
242
243         ign = t:option( Flag, "ignore", "Enabled" )
244         ign.enabled  = "0"
245         ign.disabled = "1"
246         ign.rmempty  = false
247         function ign.cfgvalue(self, section)
248                 return Flag.cfgvalue(self, section) or "0"
249         end
250
251         t:option( DummyValue, "library", "Library" )
252
253         return mpi
254 end