-
Notifications
You must be signed in to change notification settings - Fork 17
/
birdc.go
281 lines (219 loc) · 6.23 KB
/
birdc.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"regexp"
"strconv"
"strings"
"github.com/bamiaux/iobit"
)
type bgpCommunity struct {
AS uint16
Data uint16
}
var birdCommunityRegex = regexp.MustCompile(`\((\d+,\d+)\)`)
var monitoredPrefix = flag.String(
"peerprefix", "1.1.1.0/24", "the prefix of the other side")
var communityAS = flag.Int("communityASN", 23456,
"The shared community AS used to communicate on")
var templatePath = flag.String("templateFile", "/etc/bird/conf.orig",
"Where to find the template file")
/*
Three Communities are used:
Type 1: Game counter, Moves up
on each move, so that peers know
when a move happens.
T = Type
Z = Counter number
+-------------------------------+
|T|T|Z|Z|Z|Z|Z|Z|Z|Z|Z|Z|Z|Z|Z|Z|
+-------------------------------+
Type 2: Attack Cords
T = Type
X = X Cords
Y = Y Cords
S = Hit or Miss on last move
+-------------------------------+
|T|T|X|X|X|X|-|-|Y|Y|Y|Y|S|S|-|-|
+-------------------------------+
*/
func numberToBitReader(in uint16) iobit.Reader {
actually := uint16(in)
bits2 := make([]byte, 2)
binary.BigEndian.PutUint16(bits2, actually)
return iobit.NewReader(bits2)
}
var errNotEnoughData = fmt.Errorf("Not enough data to make a move")
var errInvalidType = fmt.Errorf("Invalid community type found")
var errDupeType = fmt.Errorf("Duplicate data read")
func readBGP() (gameIncrementor, X, Y, HitOrMissOnLast int, err error) {
communities := readCommunities(*monitoredPrefix)
readCounter, readPosition := false, false
for _, community := range communities {
if community.AS == uint16(*communityAS) {
// okay, so we are now interested!
r := numberToBitReader(community.Data)
t := r.Uint8(2)
if t == 1 {
// Counter
if readCounter {
// uh we have read it twice, oh dear?
return 0, 0, 0, 0, errDupeType
}
readCounter = true
c := r.Uint16(14)
gameIncrementor = int(c)
} else if t == 2 {
if readPosition {
// uh we have read it twice, oh dear?
return 0, 0, 0, 0, errDupeType
}
readPosition = true
xp := r.Uint16(4)
X = int(xp)
r.Skip(2)
yp := r.Uint16(4)
Y = int(yp)
hs := r.Uint16(2)
HitOrMissOnLast = int(hs)
} else {
return 0, 0, 0, 0, errInvalidType
}
}
}
if readCounter && readPosition {
return gameIncrementor, X, Y, HitOrMissOnLast, nil
}
return 0, 0, 0, 0, errNotEnoughData
}
func testBGPCode() {
for x := 0; x < 10; x++ {
for y := 0; y < 10; y++ {
_, c2 := genCommunities(1, x, y, 0)
r := numberToBitReader(c2)
t := r.Uint8(2)
if t != 2 {
log.Printf("WTF??")
}
xp := r.Uint16(4)
X := int(xp)
r.Skip(2)
yp := r.Uint16(4)
Y := int(yp)
// hs := r.Uint16(2)
if X != x {
fmt.Printf("Logic error X: Got %d != Sent %d\n", X, x)
}
if Y != y {
fmt.Printf("Logic error Y: Got %d != Sent %d\n", Y, y)
}
}
}
}
func genCommunities(gameIncrementor, X, Y, HitOrMissOnLast int) (uint16, uint16) {
counternumberbytes := make([]byte, 2)
counternumberbits := iobit.NewWriter(counternumberbytes)
counternumberbits.PutUint16(2, 1)
counternumberbits.PutUint16(14, uint16(gameIncrementor))
counternumberbits.Flush()
counterCommunity := binary.BigEndian.Uint16(counternumberbytes)
positionnumberbytes := make([]byte, 2)
positionnumberbits := iobit.NewWriter(positionnumberbytes)
positionnumberbits.PutUint16(2, 2)
positionnumberbits.PutUint16(4, uint16(X))
positionnumberbits.PutUint16(2, 0) // pad
positionnumberbits.PutUint16(4, uint16(Y))
positionnumberbits.PutUint16(2, uint16(HitOrMissOnLast))
positionnumberbits.PutUint16(2, 0) // pad
positionnumberbits.Flush()
positionCommunity := binary.BigEndian.Uint16(positionnumberbytes)
return counterCommunity, positionCommunity
}
func writeBGP(gameIncrementor, X, Y, HitOrMissOnLast int) error {
counterCommunity, positionCommunity :=
genCommunities(gameIncrementor, X, Y, HitOrMissOnLast)
// Now we have the two community strings counterCommunity and positionCommunity
templatestring := fmt.Sprintf(
"\nbgp_community.add((%d,%d));\nbgp_community.add((%d,%d));\n",
*communityAS, positionCommunity, *communityAS, counterCommunity)
templateBytes, err := ioutil.ReadFile(*templatePath)
if err != nil {
return err
}
birdConfigOutput := strings.Replace(string(templateBytes),
"###COMMUNITY###", templatestring, 1)
err = ioutil.WriteFile("/etc/bird/bird.conf", []byte(birdConfigOutput), 0640)
if err != nil {
return err
}
// now reload bird
conn, err := net.Dial("unix", "/run/bird/bird.ctl")
if err != nil {
log.Fatalf("Unable to connect to bird %s", err.Error())
}
buffer := make([]byte, 90000)
conn.Read(buffer)
defer conn.Close()
conn.Write([]byte(fmt.Sprintf("configure\n")))
buffer = make([]byte, 90000)
_, err = conn.Read(buffer)
return err
}
func resetBird() error {
templateBytes, err := ioutil.ReadFile(*templatePath)
if err != nil {
return err
}
birdConfigOutput := strings.Replace(string(templateBytes),
"###COMMUNITY###", "", 1)
err = ioutil.WriteFile("/etc/bird/bird.conf", []byte(birdConfigOutput), 0640)
if err != nil {
return err
}
// now reload bird
conn, err := net.Dial("unix", "/run/bird/bird.ctl")
if err != nil {
log.Fatalf("Unable to connect to bird %s", err.Error())
}
buffer := make([]byte, 90000)
conn.Read(buffer)
defer conn.Close()
conn.Write([]byte(fmt.Sprintf("configure\n")))
buffer = make([]byte, 90000)
_, err = conn.Read(buffer)
return err
}
func readCommunities(prefix string) (o []bgpCommunity) {
conn, err := net.Dial("unix", "/run/bird/bird.ctl")
if err != nil {
log.Fatalf("Unable to connect to bird %s", err.Error())
}
buffer := make([]byte, 90000)
conn.Read(buffer)
defer conn.Close()
conn.Write([]byte(fmt.Sprintf("show route all %s\n", prefix)))
buffer = make([]byte, 90000)
n, err := conn.Read(buffer)
if err != nil {
log.Fatalf("Unable to read from bird %s", err.Error())
}
matches :=
birdCommunityRegex.FindAllStringSubmatch(string(buffer[:n]), -1)
o = make([]bgpCommunity, 0)
for _, v := range matches {
if len(v) == 2 {
bits := strings.Split(v[1], ",")
as, _ := strconv.ParseInt(bits[0], 10, 64)
data, _ := strconv.ParseInt(bits[1], 10, 64)
o = append(o, bgpCommunity{
AS: uint16(as),
Data: uint16(data),
})
}
}
return o
}