move protocol support into a new protocols/ subdir
[project/luci.git] / protocols / relay / luasrc / model / network / proto_relay.lua
1 --[[
2 LuCI - Network model - relay 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 local device = luci.util.class(netmod.interface)
22
23 netmod:register_pattern_virtual("^relay-%w")
24
25 local proto = netmod:register_protocol("relay")
26
27 function proto.get_i18n(self)
28         return luci.i18n.translate("Relay bridge")
29 end
30
31 function proto.ifname(self)
32         return "relay-" .. self.sid
33 end
34
35 function proto.opkg_package(self)
36         return "relayd"
37 end
38
39 function proto.is_installed(self)
40         return nixio.fs.access("/lib/network/relay.sh")
41 end
42
43 function proto.is_floating(self)
44         return true
45 end
46
47 function proto.is_virtual(self)
48         return true
49 end
50
51 function proto.get_interface(self)
52         return device(self.sid, self)
53 end
54
55 function proto.get_interfaces(self)
56         if not self.ifaces then
57                 local ifs = { }
58                 local _, net, dev
59
60                 for net in luci.util.imatch(self:_get("network")) do
61                         net = netmod:get_network(net)
62                         if net then
63                                 dev = net:get_interface()
64                                 if dev then
65                                         ifs[dev:name()] = dev
66                                 end
67                         end
68                 end
69
70                 for dev in luci.util.imatch(self:_get("ifname")) do
71                         dev = netmod:get_interface(dev)
72                         if dev then
73                                 ifs[dev:name()] = dev
74                         end
75                 end
76
77                 self.ifaces = { }
78
79                 for _, dev in luci.util.kspairs(ifs) do
80                         self.ifaces[#self.ifaces+1] = dev
81                 end
82         end
83
84         return self.ifaces
85 end
86
87 function proto.uptime(self)
88         local net
89         local upt = 0
90         for net in luci.util.imatch(self:_get("network")) do
91                 net = netmod:get_network(net)
92                 if net then
93                         upt = math.max(upt, net:uptime())
94                 end
95         end
96         return upt
97 end
98
99
100 function device.__init__(self, ifname, network)
101         self.ifname  = ifname
102         self.network = network
103 end
104
105 function device.type(self)
106         return "tunnel"
107 end
108
109 function device.is_up(self)
110         if self.network then
111                 local _, dev
112                 for _, dev in ipairs(self.network:get_interfaces()) do
113                         if not dev:is_up() then
114                                 return false
115                         end
116                 end
117                 return true
118         end
119         return false
120 end
121
122 function device._stat(self, what)
123         local v = 0
124         if self.network then
125                 local _, dev
126                 for _, dev in ipairs(self.network:get_interfaces()) do
127                         v = v + dev[what](dev)
128                 end
129         end
130         return v
131 end
132
133 function device.rx_bytes(self) return self:_stat("rx_bytes") end
134 function device.tx_bytes(self) return self:_stat("tx_bytes") end
135 function device.rx_packets(self) return self:_stat("rx_packets") end
136 function device.tx_packets(self) return self:_stat("tx_packets") end
137
138 function device.mac(self)
139         if self.network then
140                 local _, dev
141                 for _, dev in ipairs(self.network:get_interfaces()) do
142                         return dev:mac()
143                 end
144         end
145 end
146
147 function device.ipaddrs(self)
148         local addrs = { }
149         if self.network then
150                 addrs[1] = luci.ip.IPv4(self.network:_get("ipaddr"))
151         end
152         return addrs
153 end
154
155 function device.ip6addrs(self)
156         return { }
157 end
158
159 function device.shortname(self)
160         return "%s %q" % { luci.i18n.translate("Relay"), self.ifname }
161 end
162
163 function device.get_type_i18n(self)
164         return luci.i18n.translate("Relay Bridge")
165 end