summaryrefslogtreecommitdiffhomepage
path: root/protocols/luci-proto-relay/luasrc/model/network/proto_relay.lua
blob: 42bd1f846acd69150ffba0696452808e49f16fa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
--[[
LuCI - Network model - relay protocol extension

Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

]]--

local netmod = luci.model.network
local device = luci.util.class(netmod.interface)

netmod:register_pattern_virtual("^relay-%w")

local proto = netmod:register_protocol("relay")

function proto.get_i18n(self)
	return luci.i18n.translate("Relay bridge")
end

function proto.ifname(self)
	return "relay-" .. self.sid
end

function proto.opkg_package(self)
	return "relayd"
end

function proto.is_installed(self)
	return nixio.fs.access("/etc/init.d/relayd")
end

function proto.is_floating(self)
	return true
end

function proto.is_virtual(self)
	return true
end

function proto.get_interface(self)
	return device(self.sid, self)
end

function proto.get_interfaces(self)
	if not self.ifaces then
		local ifs = { }
		local _, net, dev

		for net in luci.util.imatch(self:_get("network")) do
			net = netmod:get_network(net)
			if net then
				dev = net:get_interface()
				if dev then
					ifs[dev:name()] = dev
				end
			end
		end

		for dev in luci.util.imatch(self:_get("ifname")) do
			dev = netmod:get_interface(dev)
			if dev then
				ifs[dev:name()] = dev
			end
		end

		self.ifaces = { }

		for _, dev in luci.util.kspairs(ifs) do
			self.ifaces[#self.ifaces+1] = dev
		end
	end

	return self.ifaces
end

function proto.uptime(self)
	local net
	local upt = 0
	for net in luci.util.imatch(self:_get("network")) do
		net = netmod:get_network(net)
		if net then
			upt = math.max(upt, net:uptime())
		end
	end
	return upt
end


function device.__init__(self, ifname, network)
	self.ifname  = ifname
	self.network = network
end

function device.type(self)
	return "tunnel"
end

function device.is_up(self)
	if self.network then
		local _, dev
		for _, dev in ipairs(self.network:get_interfaces()) do
			if not dev:is_up() then
				return false
			end
		end
		return true
	end
	return false
end

function device._stat(self, what)
	local v = 0
	if self.network then
		local _, dev
		for _, dev in ipairs(self.network:get_interfaces()) do
			v = v + dev[what](dev)
		end
	end
	return v
end

function device.rx_bytes(self) return self:_stat("rx_bytes") end
function device.tx_bytes(self) return self:_stat("tx_bytes") end
function device.rx_packets(self) return self:_stat("rx_packets") end
function device.tx_packets(self) return self:_stat("tx_packets") end

function device.mac(self)
	if self.network then
		local _, dev
		for _, dev in ipairs(self.network:get_interfaces()) do
			return dev:mac()
		end
	end
end

function device.ipaddrs(self)
	local addrs = { }
	if self.network then
		addrs[1] = luci.ip.IPv4(self.network:_get("ipaddr"))
	end
	return addrs
end

function device.ip6addrs(self)
	return { }
end

function device.shortname(self)
	return "%s %q" % { luci.i18n.translate("Relay"), self.ifname }
end

function device.get_type_i18n(self)
	return luci.i18n.translate("Relay Bridge")
end