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