aboutsummaryrefslogtreecommitdiffstats
path: root/lua/inet/set.lua
blob: c96a57b590fa391feebed37e0095196033a2b41d (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
local core = require 'inet.core'
local common = require 'inet.common'

local is_inet = core.is_inet
local get_mt = common.get_mt

local insert = table.insert
local remove = table.remove
local sort = table.sort

local M = {}

local function table_compact(t, n)
	-- remove nil entries, and reorder
	local i = 0
	for j=1,n do
		if t[j] then
			if i > 0 then
				t[i] = t[j]
				i = i + 1
			end
		elseif i == 0 then
			i = j
		end
	end
	if i > 0 then
		for j=i,n do
			t[j] = nil
		end
	end
end

function M.aggregate(t)
	local flag = true
	local n = #t
	for i=1,n do
		t[i] = t[i]:network()
	end
	while flag do -- loop until no aggregatable addresses are found
		flag = false
		for i=1,n do
			local ia = t[i]
			if ia then
				local ib = ia:flip() -- counterpart
				for j=1,n do
					if j ~= i then
						if ia == t[j] then
							-- duplicate found
							t[j] = nil
							flag = true
						elseif t[j] == ib then
							-- counterpart found, aggregating
							t[i] = (ia ^ -1):network()
							t[j] = nil
							flag = true
						end
					end
				end
			end
		end
	end
	table_compact(t, n)
end

local function has(set, addr)
	assert(set)
	for i=1,#set do
		local elem = set[i]
		if elem >= addr then
			local exclude = set.exclude
			if exclude then
				return not has(exclude, addr)
			else
				return true
			end
		end
	end
	return false
end

function M.iterator(set)
	local excl = set.exclude
	local i = 1
	if #set < 1 then return nil, 'empty set' end
	local addr = set[i]
	local net = set[i]:network()
	local function iter()
		if not addr then return end
		local ret
		ret = addr/32
		addr = addr + 1
		if addr:network() ~= net then
			i = i + 1
			addr = set[i]
			if addr then
				net = set[i]:network()
			end
		end
		if has(excl, ret) then
			return iter()
		end
		return ret
	end

	return iter
end

-- iterate over the set repeatedly
function M.loopiterator(set)
	local orig_iter = M.iterator(set)
	local function iter()
		local addr = orig_iter()
		if not addr then
			orig_iter = M.iterator(set)
			addr = orig_iter()
		end
		return addr
	end
	return iter
end

local inet_set = {}
inet_set.__index = inet_set

local function check_net(self, net)
	if not is_inet(net) then return nil, 'invalid net' end
	local set_fam = self.family
	local net_fam = net:family()
	if set_fam then
		if set_fam ~= net_fam then
			return nil, 'invalid family'
		end
	else
		self.family = net_fam
	end
	return true
end

function inet_set:add(net)--, aggregate)
	if not check_net(self, net) then return check_net(self, net) end
	insert(self.nets, net)
	M.aggregate(self.nets)
	return true
end

function inet_set:remove(net)
	if not check_net(self, net) then return check_net(self, net) end
	local nets = self.nets
	local ret = false
	for i=1,#nets do
		local n = nets[i]
		if n == net then
			ret = true
			remove(nets, i)
		elseif n:contains(net) then
			ret = true
			remove(nets, i)
			for j=#n+1,#net do
				local sn = (net / j):network()
				if sn:contains(net) then
					sn = sn:flip()
				end
				self:add(sn)
			end
		end
	end
	return ret
end

function inet_set:contains(net)
	if not check_net(self, net) then return check_net(self, net) end
	local nets = self.nets
	for i=1,#nets do
		local n = nets[i]
		if n == net or n:contains(net) then
			return n
		end
	end
	return false
end

function inet_set:list()
	local nets = self.nets
	sort(nets)
	return nets
end

function inet_set:flush()
	self.nets = {}
	self.family = nil
	return true
end

local function new_set()
	return setmetatable({
		nets = {},
	}, inet_set)
end

M.new = new_set

function M.is_set(t)
	local mt = get_mt(t)
	return mt == inet_set
end

return M