This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rib.go
205 lines (161 loc) · 3.57 KB
/
rib.go
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
/*
* VC5 load balancer. Copyright (C) 2021-present David Coles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package bgp
import (
"net"
)
type Update struct {
RIB []IP
Parameters Parameters
}
func (r *Update) adjRIBOutString() (out []string) {
for _, p := range r.Parameters.Filter(r.RIB) {
out = append(out, ip_string(p))
}
return
}
func (r *Update) adjRIBOut() []IP {
return r.Parameters.Filter(r.RIB)
}
func (r *Update) adjRIBOutP() ([]IP, Parameters) {
return r.Parameters.Filter(r.RIB), r.Parameters
}
func (r *Update) Filter() []IP {
return r.Parameters.Filter(r.RIB)
}
func (p *Parameters) Filter(dest []IP) []IP {
var pass []IP
filter:
for _, i := range dest {
ip := net.ParseIP(ip_string(i))
if ip == nil {
continue
}
for _, ipnet := range p.Accept {
n := net.IPNet(ipnet)
if n.Contains(ip) {
pass = append(pass, i)
continue filter
}
}
for _, ipnet := range p.Reject {
n := net.IPNet(ipnet)
if n.Contains(ip) {
continue filter
}
}
pass = append(pass, i)
}
return pass
}
func Filter(dest []IP, filter []IP) []IP {
reject := map[IP]bool{}
if len(filter) == 0 {
return dest
}
for _, i := range filter {
reject[i] = true
}
var o []IP
for _, i := range dest {
if _, rejected := reject[i]; !rejected {
o = append(o, i)
}
}
return o
}
func (r *Update) full() map[IP]bool {
n := map[IP]bool{}
for _, ip := range r.adjRIBOut() {
n[ip] = true
}
return n
}
func advertise(r []IP) map[IP]bool {
n := map[IP]bool{}
for _, ip := range r {
n[ip] = true
}
return n
}
func to_string(in []IP) (out []string) {
for _, p := range in {
out = append(out, ip_string(p))
}
return
}
func (r Update) Copy() Update {
var rib []IP
for _, x := range r.RIB {
rib = append(rib, x)
}
return Update{RIB: rib, Parameters: r.Parameters}
}
func (u *Update) Source() net.IP {
return net.ParseIP(ip_string(u.Parameters.SourceIP))
}
func (c *Update) updates(p Update) (uint64, uint64, map[IP]bool) {
nrli := map[IP]bool{}
var advertise uint64
var withdraw uint64
var vary bool = c.Parameters.Diff(p.Parameters)
curr := map[IP]bool{}
prev := map[IP]bool{}
for _, ip := range c.adjRIBOut() {
curr[ip] = true
}
for _, ip := range p.adjRIBOut() {
prev[ip] = true
}
for ip, _ := range curr {
_, ok := prev[ip] // if didn't exist in previous rib, or params have changed then need to advertise
if !ok || vary {
advertise++
nrli[ip] = true
}
}
for ip, _ := range prev {
_, ok := curr[ip] // if not in current rib then need to withdraw
if !ok {
withdraw++
nrli[ip] = false
}
}
return advertise, withdraw, nrli
}
func RIBSDiffer(a, b []IP) bool {
x := map[IP]bool{}
for _, i := range a {
x[i] = true
}
y := map[IP]bool{}
for _, i := range b {
y[i] = true
}
if len(y) != len(y) {
return true
}
for i, _ := range x {
_, ok := y[i]
if !ok {
return true
}
delete(y, i)
}
return len(y) != 0
}