2988385b225bdd6cb41a00e081243bb26f82146e
[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" or p == "pptp" then
47                         return p
48                 elseif p == "3g" then
49                         return "comgt"
50                 elseif p == "pppoe" then
51                         return "ppp-mod-pppoe"
52                 elseif p == "pppoa" then
53                         return "ppp-mod-pppoa"
54                 end
55         end
56
57         function proto.is_installed(self)
58                 if nixio.fs.access("/lib/network/" .. p .. ".sh") then
59                         return true
60                 elseif 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 == "3g" then
65                         return nixio.fs.access("/lib/netifd/proto/3g.sh")
66                 else
67                         return nixio.fs.access("/lib/netifd/proto/ppp.sh")
68                 end
69         end
70
71         function proto.is_floating(self)
72                 return (p ~= "pppoe")
73         end
74
75         function proto.is_virtual(self)
76                 return true
77         end
78
79         function proto.get_interfaces(self)
80                 if self:is_floating() then
81                         return nil
82                 else
83                         return netmod.protocol.get_interfaces(self)
84                 end
85         end
86
87         function proto.contains_interface(self, ifc)
88                 if self:is_floating() then
89                         return (netmod:ifnameof(ifc) == self:ifname())
90                 else
91                         return netmod.protocol.contains_interface(self, ifc)
92                 end
93         end
94
95         netmod:register_pattern_virtual("^%s-%%w" % p)
96 end