2dded366d69a722f316d1745db302009dbb3d83f
[project/luci.git] / protocols / ppp / luasrc / model / network / proto_ppp.lua
1 --[[
2 LuCI - Network model - 3G, PPP, PPtP, PPPoE and PPPoA protocol extension
3
4 Copyright 2011 Jo-Philipp Wich <xm@subsignal.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 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 ]]--
19
20 local netmod = luci.model.network
21
22 local _, p
23 for _, p in ipairs({"ppp", "pptp", "pppoe", "pppoa", "3g"}) do
24
25         local proto = netmod:register_protocol(p)
26
27         function proto.get_i18n(self)
28                 if p == "ppp" then
29                         return luci.i18n.translate("PPP")
30                 elseif p == "pptp" then
31                         return luci.i18n.translate("PPtP")
32                 elseif p == "3g" then
33                         return luci.i18n.translate("UMTS/GPRS/EV-DO")
34                 elseif p == "pppoe" then
35                         return luci.i18n.translate("PPPoE")
36                 elseif p == "pppoa" then
37                         return luci.i18n.translate("PPPoATM")
38                 end
39         end
40
41         function proto.ifname(self)
42                 return p .. "-" .. self.sid
43         end
44
45         function proto.opkg_package(self)
46                 if p == "ppp" then
47                         return p
48                 elseif p == "3g" then
49                         return "comgt"
50                 elseif p == "pptp" then
51                         return "ppp-mod-pptp"
52                 elseif p == "pppoe" then
53                         return "ppp-mod-pppoe"
54                 elseif p == "pppoa" then
55                         return "ppp-mod-pppoa"
56                 end
57         end
58
59         function proto.is_installed(self)
60                 if p == "pppoa" then
61                         return (nixio.fs.glob("/usr/lib/pppd/*/pppoatm.so")() ~= nil)
62                 elseif p == "pppoe" then
63                         return (nixio.fs.glob("/usr/lib/pppd/*/rp-pppoe.so")() ~= nil)
64                 elseif p == "pptp" then
65                         return (nixio.fs.glob("/usr/lib/pppd/*/pptp.so")() ~= nil)
66                 elseif p == "3g" then
67                         return nixio.fs.access("/lib/netifd/proto/3g.sh")
68                 else
69                         return nixio.fs.access("/lib/netifd/proto/ppp.sh")
70                 end
71         end
72
73         function proto.is_floating(self)
74                 return (p ~= "pppoe")
75         end
76
77         function proto.is_virtual(self)
78                 return true
79         end
80
81         function proto.get_interfaces(self)
82                 if self:is_floating() then
83                         return nil
84                 else
85                         return netmod.protocol.get_interfaces(self)
86                 end
87         end
88
89         function proto.contains_interface(self, ifc)
90                 if self:is_floating() then
91                         return (netmod:ifnameof(ifc) == self:ifname())
92                 else
93                         return netmod.protocol.contains_interface(self, ifc)
94                 end
95         end
96
97         netmod:register_pattern_virtual("^%s-%%w" % p)
98 end