Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / package / luci / protocols / luci-proto-ppp / luasrc / model / network / proto_ppp.lua
1 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local netmod = luci.model.network
5
6 local _, p
7 for _, p in ipairs({"ppp", "pptp", "pppoe", "pppoa", "l2tp"}) do
8
9         local proto = netmod:register_protocol(p)
10
11         function proto.get_i18n(self)
12                 if p == "ppp" then
13                         return luci.i18n.translate("PPP")
14                 elseif p == "pptp" then
15                         return luci.i18n.translate("PPtP")
16                 elseif p == "pppoe" then
17                         return luci.i18n.translate("PPPoE")
18                 elseif p == "pppoa" then
19                         return luci.i18n.translate("PPPoATM")
20                 elseif p == "l2tp" then
21                         return luci.i18n.translate("L2TP")
22                 end
23         end
24
25         function proto.ifname(self)
26                 return p .. "-" .. self.sid
27         end
28
29         function proto.opkg_package(self)
30                 if p == "ppp" then
31                         return p
32                 elseif p == "pptp" then
33                         return "ppp-mod-pptp"
34                 elseif p == "pppoe" then
35                         return "ppp-mod-pppoe"
36                 elseif p == "pppoa" then
37                         return "ppp-mod-pppoa"
38                 elseif p == "l2tp" then
39                         return "xl2tpd"
40                 end
41         end
42
43         function proto.is_installed(self)
44                 if p == "pppoa" then
45                         return (nixio.fs.glob("/usr/lib/pppd/*/pppoatm.so")() ~= nil)
46                 elseif p == "pppoe" then
47                         return (nixio.fs.glob("/usr/lib/pppd/*/rp-pppoe.so")() ~= nil)
48                 elseif p == "pptp" then
49                         return (nixio.fs.glob("/usr/lib/pppd/*/pptp.so")() ~= nil)
50                 elseif p == "l2tp" then
51                         return nixio.fs.access("/lib/netifd/proto/l2tp.sh")
52                 else
53                         return nixio.fs.access("/lib/netifd/proto/ppp.sh")
54                 end
55         end
56
57         function proto.is_floating(self)
58                 return (p ~= "pppoe")
59         end
60
61         function proto.is_virtual(self)
62                 return true
63         end
64
65         function proto.get_interfaces(self)
66                 if self:is_floating() then
67                         return nil
68                 else
69                         return netmod.protocol.get_interfaces(self)
70                 end
71         end
72
73         function proto.contains_interface(self, ifc)
74                 if self:is_floating() then
75                         return (netmod:ifnameof(ifc) == self:ifname())
76                 else
77                         return netmod.protocol.contains_interface(self, ifc)
78                 end
79         end
80
81         netmod:register_pattern_virtual("^%s%%-%%w" % p)
82 end