-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbgpreader.go
232 lines (186 loc) · 4.49 KB
/
bgpreader.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"bufio"
"fmt"
"github.com/miekg/bitradix"
"io"
"log"
"net"
"os"
"strconv"
"strings"
"sync"
)
var neighbors Neighbors
var neighbors_lock sync.RWMutex
func bgpReader() {
neighbors = make(Neighbors)
r := bufio.NewReader(os.Stdin)
var err error
for line, err := r.ReadString('\n'); err == nil; line, err = r.ReadString('\n') {
line = strings.TrimSpace(line)
if line == "shutdown" {
log.Println("Shutdown command received")
return
}
if len(line) > 0 {
processLine(line)
}
}
if err != nil && err != io.EOF {
log.Println(err)
return
} else {
log.Println("EOF")
}
}
func processLine(line string) {
f := strings.SplitN(line, " ", 4)
if len(f) < 3 {
log.Printf("Did not split line into enough parts\nLINE: [%s]\nF: %#v\n", line, f)
return
}
neighbor_ip := f[1]
command := f[2]
neighbors_lock.RLock()
defer neighbors_lock.RUnlock()
if neighbors[neighbor_ip] == nil {
neighbors_lock.RUnlock()
neighbors_lock.Lock()
neighbor := new(Neighbor)
neighbor.trie = bitradix.New32()
neighbors[neighbor_ip] = neighbor
neighbors_lock.Unlock()
neighbors_lock.RLock()
defer neighbors_lock.RUnlock() // double?
}
neighbor := neighbors[neighbor_ip]
neighbor.lock.Lock()
defer neighbor.lock.Unlock()
switch command {
case "up", "connected", "down":
neighbor.State = command
log.Println("State change", line)
return
case "update":
neighbor.State = "update " + f[3]
return
case "announced":
// fmt.Printf("R: %#v\n", r)
neighbor.Updates++
route := parseRoute(f[3])
if ones, _ := route.Prefix.Mask.Size(); ones < 8 || ones > 25 {
// fmt.Println("prefix mask too big or small", route.Prefix)
} else {
if neighbor.AsnPrefix == nil {
neighbor.AsnPrefix = make(map[ASN]Prefixes)
}
if neighbor.PrefixAsn == nil {
neighbor.PrefixAsn = make(Prefixes)
}
if neighbor.AsnPrefix[route.PrimaryASN] == nil {
neighbor.AsnPrefix[route.PrimaryASN] = make(Prefixes)
}
neighbor.AsnPrefix[route.PrimaryASN][route.Prefix.String()] = 0
neighbor.PrefixAsn[route.Prefix.String()] = route.PrimaryASN
addRoute(neighbor.trie, route.Prefix, route)
}
case "withdrawn":
neighbor.Updates++
route := parseRoute(f[3])
removeRoute(neighbor.trie, route.Prefix)
if asn, exists := neighbor.PrefixAsn[route.Prefix.String()]; exists {
// fmt.Println("Removing ASN from prefix", asn, route.Prefix)
delete(neighbor.PrefixAsn, route.Prefix.String())
delete(neighbor.AsnPrefix[asn], route.Prefix.String())
} else {
log.Println("Could not find prefix in PrefixAsn")
log.Println("%#v", neighbor.PrefixAsn)
}
default:
err_text := fmt.Sprintf("Command not implemented: %s\n%s\n", command, line)
log.Println(err_text)
err := fmt.Errorf(err_text)
panic(err)
}
if neighbor.Updates%25000 == 0 {
log.Printf("Processed %v updates from %s\n", neighbor.Updates, neighbor_ip)
}
}
func parseRoute(input string) *Route {
r := strings.Split(input, " ")
route := new(Route)
route.Options = make(map[string]string)
aspath := make(ASPath, 0)
var key string
state := parseKey
for _, v := range r {
// fmt.Printf("k: %s, v: %s, state: %#v\n", key, v, state)
switch state {
case parseKey:
{
state = parseValue
key = v
continue
}
case parseValue:
if v == "[" {
state = parseList
continue
}
state = parseKey
if key == "as-path" {
addASPath(&aspath, v)
}
route.Options[key] = v
continue
case parseList:
{
if v == "]" {
state = parseKey
continue
}
if key != "as-path" {
log.Println("can only do list for as-path")
log.Printf("key: %s, v: %s\n\n", key, v)
log.Println("LINE", input)
state = parseSkip
continue
}
if v == "(" {
state = parseSkip
continue
}
addASPath(&aspath, v)
}
case parseSkip:
switch v {
case ")":
state = parseList
case "]":
state = parseKey
}
}
}
// fmt.Printf("%#v / %#v\n", route, aspath)
_, prefix, err := net.ParseCIDR(route.Options["route"])
if err != nil {
log.Printf("Could not parse prefix %s %e\n", route.Options["route"], err)
panic("bad prefix")
}
route.Prefix = prefix
// fmt.Printf("IP: %s, PREFIX: %s\n", ip, prefix)
if len(aspath) > 0 {
route.PrimaryASN = ASN(aspath[len(aspath)-1])
route.ASPath = aspath
}
return route
}
func addASPath(aspath *ASPath, v string) {
asn, err := strconv.Atoi(v)
if err != nil {
log.Println("Could not parse number", v)
panic("Bad as-path")
}
*aspath = append(*aspath, ASN(asn))
}