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