63e12e08dfce9ae0669ebe6a9ac3e3833ca1a573
[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
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 require("luci.fs")
16 require("luci.ip")
17
18 mp = Map("olsrd", "OLSR - Plugins")
19
20 p = mp:section(TypedSection, "LoadPlugin")
21 p.addremove = true
22 p.dynamic = true
23 p.anonymous = true
24
25 ign = p:option(Flag, "ignore")
26 ign.enabled = "1"
27 ign.disabled = "0"
28 ign.optional = true
29
30 lib = p:option(ListValue, "library", translate("library"))
31 lib:value("")
32 for k, v in pairs(luci.fs.dir("/usr/lib")) do
33 if v:sub(1, 6) == "olsrd_" then
34 lib:value(v)
35 end
36 end
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", "eth1" }
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
138
139 -- build plugin options with dependencies
140 for plugin, options in pairs(knownPlParams) do
141 for _, option in ipairs(options) do
142 local otype, name, default, uci2cbi, cbi2uci = unpack(option)
143 local values
144
145 if type(default) == "table" then
146 values = default
147 default = default[1]
148 end
149
150 if otype == Flag then
151 local bool = p:option( Flag, name )
152 if default == "yes" or default == "no" then
153 bool.enabled = "yes"
154 bool.disabled = "no"
155 elseif default == "on" or default == "off" then
156 bool.enabled = "on"
157 bool.disabled = "off"
158 elseif default == "1" or default == "0" then
159 bool.enabled = "1"
160 bool.disabled = "0"
161 else
162 bool.enabled = "true"
163 bool.disabled = "false"
164 end
165 bool.optional = true
166 bool.default = default
167 bool:depends({ library = plugin })
168 else
169 local field = p:option( otype, name )
170 if values then
171 for _, value in ipairs(values) do
172 field:value( value )
173 end
174 end
175 if type(uci2cbi) == "function" then
176 function field.cfgvalue(self, section)
177 return uci2cbi(otype.cfgvalue(self, section))
178 end
179 end
180 if type(cbi2uci) == "function" then
181 function field.formvalue(self, section)
182 return cbi2uci(otype.formvalue(self, section))
183 end
184 end
185 if otype == DynamicList then
186 field:value( default )
187 end
188 field.optional = true
189 field.default = default
190 field:depends({ library = plugin })
191 end
192 end
193 end
194
195 return mp