* luci/app-olsr: first round of olsr improvements:
[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
29 lib = p:option(ListValue, "library", translate("library"))
30 lib:value("")
31 for k, v in pairs(luci.fs.dir("/usr/lib")) do
32         if v:sub(1, 6) == "olsrd_" then
33                 lib:value(v)
34         end
35 end
36
37 local function Range(x,y)
38         local t = {}
39         for i = x, y do t[#t+1] = i end
40         return t
41 end
42
43 local function Cidr2IpMask(val)
44         local cidr = luci.ip.IPv4(val) or luci.ip.IPv6(val)
45         if not cidr then return val else return cidr end
46 end
47
48 local function IpMask2Cidr(val)
49         local ip, mask = val:gmatch("([^%s+])%s+([^%s+])")
50         local cidr
51         if ip and mask and ip:match(":") then
52                 cidr = luci.ip.IPv6(ip, mask)
53         elseif ip and mask then
54                 cidr = luci.ip.IPv4(ip, mask)
55         end
56         if not cidr then return val else return cidr end
57 end
58
59
60 local knownPlParams = {
61         ["olsrd_bmf.so.1.5.3"] = {
62                 { Value,                "BmfInterface",                 "bmf0" },
63                 { Value,                "BmfInterfaceIp",               "10.10.10.234/24" },
64                 { Flag,                 "DoLocalBroadcast",             "no" },
65                 { Flag,                 "CapturePacketsOnOlsrInterfaces", "yes" },
66                 { ListValue,    "BmfMechanism",                 { "UnicastPromiscuous", "Broadcast" } },
67                 { Value,                "BroadcastRetransmitCount",     "2" },
68                 { Value,                "FanOutLimit",                  "4" },
69                 { DynamicList,  "NonOlsrIf",                    "eth1" },
70         },
71
72         ["olsrd_dyn_gw.so.0.4"] = {
73                 { Value,                "Interval",                             "40" },
74                 { DynamicList,  "Ping",                                 "141.1.1.1" },
75                 { DynamicList,  "HNA",                                  "192.168.80.0/24", Cidr2IpMask, IpMask2Cidr }
76         },
77
78         ["olsrd_httpinfo.so.0.1"] = {
79                 { Value,                "port",                                 "80" },
80                 { DynamicList,  "Host",                                 "163.24.87.3" },
81                 { DynamicList,  "Net",                                  "0.0.0.0/0", Cidr2IpMask, IpMask2Cidr },
82         },
83
84         ["olsrd_nameservice.so.0.2"] = {
85                 { DynamicList,  "name",                                 "my-name.mesh" },
86                 { DynamicList,  "hosts",                                "1.2.3.4 name-for-other-interface.mesh" },
87                 { Value,                "suffix",                               ".olsr" },
88                 { Value,                "hosts_file",                   "/path/to/hosts_file" },
89                 { Value,                "add_hosts",                    "/path/to/file" },
90                 { Value,                "dns_server",                   "141.1.1.1" },
91                 { Value,                "resolv_file",                  "/path/to/resolv.conf" },
92                 { Value,                "interval",                             "120" },
93                 { Value,                "timeout",                              "240" },
94                 { Value,                "lat",                                  "12.123" },
95                 { Value,                "lon",                                  "12.123" },
96                 { Value,                "latlon_file",                  "/var/run/latlon.js" },
97                 { Value,                "latlon_infile",                "/var/run/gps.txt" },
98                 { Value,                "sighup_pid_file",              "/var/run/dnsmasq.pid" },
99                 { Value,                "name_change_script",   "/usr/local/bin/announce_new_hosts.sh" },
100                 { Value,                "services_change_script",       "/usr/local/bin/announce_new_services.sh" }
101         },
102
103         ["olsrd_quagga.so.0.2.2"] = {
104                 { StaticList,   "redistribute",                 {
105                         "system", "kernel", "connect", "static", "rip", "ripng", "ospf",
106                         "ospf6", "isis", "bgp", "hsls"
107                 } },
108                 { ListValue,    "ExportRoutes",                 { "only", "both" } },
109                 { Flag,                 "LocalPref",                    "true" },
110                 { Value,                "Distance",                             Range(0,255) }
111         },
112
113         ["olsrd_secure.so.0.5"] = {
114                 { Value,                "Keyfile",                              "/etc/private-olsr.key" }
115         },
116
117         ["olsrd_txtinfo.so.0.1"] = {
118                 { Value,                "accept",                               "10.247.200.4" }
119         }
120 }
121
122
123 -- build plugin options with dependencies
124 for plugin, options in pairs(knownPlParams) do
125         for _, option in ipairs(options) do
126                 local otype, name, default, uci2cbi, cbi2uci = unpack(option)
127                 local values
128
129                 if type(default) == "table" then
130                         values  = default
131                         default = default[1]
132                 end
133
134                 if otype == Flag then
135                         local bool = p:option( Flag, name )
136                         if default == "yes" or default == "no" then
137                                 bool.enabled  = "yes"
138                                 bool.disabled = "no"
139                         elseif default == "on" or default == "off" then
140                                 bool.enabled  = "on"
141                                 bool.disabled = "off"
142                         elseif default == "1" or default == "0" then
143                                 bool.enabled  = "1"
144                                 bool.disabled = "0"
145                         else
146                                 bool.enabled  = "true"
147                                 bool.disabled = "false"
148                         end
149                         bool.default = default
150                         bool:depends({ library = plugin })
151                 else
152                         local field = p:option( otype, name )
153                         if values then
154                                 for _, value in ipairs(values) do
155                                         field:value( value )
156                                 end
157                         end
158                         if type(uci2cbi) == "function" then
159                                 function field.cfgvalue(self, section)
160                                         return uci2cbi(otype.cfgvalue(self, section))
161                                 end
162                         end
163                         if type(cbi2uci) == "function" then
164                                 function field.formvalue(self, section)
165                                         return cbi2uci(otype.formvalue(self, section))
166                                 end
167                         end
168                         field.default = default
169                         field:depends({ library = plugin })
170                 end
171         end
172 end
173
174 return mp