-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
183 lines (166 loc) · 4.21 KB
/
event.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
package main
import (
"fmt"
"log"
"net"
"time"
)
type (
Event interface {
Do(*Peer) error
}
ManualStartEvent struct{}
TcpCRAckedEvent struct{}
OpenMessageEvent struct {
Message OpenMessage
}
UpdateMessageEvent struct {
Message UpdateMessage
}
NotificationMessageEvent struct {
Message NotificationMessage
}
KeepaliveMessageEvent struct{}
HoldTimerExpireEvent struct{}
KeepaliveTimerExpireEvent struct{}
LocalRIBUpdateEvent struct {
Removed []*net.IPNet
Updated []*RIBEntry
}
)
func (e ManualStartEvent) Do(p *Peer) error {
if p.State != StateIdle {
return fmt.Errorf("unexpected state: %v", p.State)
}
p.setState(StateConnect)
var err error
if p.conn, err = net.Dial("tcp", net.JoinHostPort(p.NeighborAddress, "179")); err != nil {
return err
}
p.eventChan <- TcpCRAckedEvent{}
return nil
}
func (e TcpCRAckedEvent) Do(p *Peer) error {
if p.State != StateConnect {
return fmt.Errorf("unexpected state: %v", p.State)
}
var opts []byte
for af := range p.AddressFamilies {
opts = append(opts, MultiprotocolExtensionCapability{af}.ToOptionalParameter()...)
}
if err := p.sendMessage(OpenMessage{
Version: 4,
MyAS: p.MyAS,
HoldTime: p.HoldTime,
BGPID: p.RouterID,
OptionalParameters: opts,
}); err != nil {
return fmt.Errorf("send open message: %w", err)
}
p.setState(StateOpenSent)
p.wg.Add(1)
go func() {
defer p.wg.Done()
p.receiveMessages()
}()
return nil
}
func (e OpenMessageEvent) Do(p *Peer) error {
if p.State != StateOpenSent {
return fmt.Errorf("unexpected state: %v", p.State)
}
// TODO: 中身ちゃんと見る
p.setState(StateOpenConfirm)
if err := p.sendMessage(KeepaliveMessage{}); err != nil {
return fmt.Errorf("send keepalive message: %w", err)
}
return nil
}
func (e UpdateMessageEvent) Do(p *Peer) error {
if p.State != StateEstablished {
return fmt.Errorf("unexpected state: %v", p.State)
}
ws, es, err := UpdateMessageToRIBEntries(e.Message, p)
if err != nil {
return err
}
for _, r := range ws {
rib := p.AddressFamilies[r.AF].LocalRIB
e := rib.Find(r.Prefix)
if e == nil || e.Source != p {
continue
}
if err := rib.Remove(e); err != nil {
return err
}
}
for _, e := range es {
rib := p.AddressFamilies[e.AF].LocalRIB
curr := rib.Find(e.Prefix)
if curr != nil {
if len(curr.ASPath.Segments) < len(e.ASPath.Segments) {
log.Printf("ignore update for %v (entry in RIB has priority)", e.Prefix)
continue
}
}
if err := rib.Update(e); err != nil {
return err
}
}
return nil
}
func (e NotificationMessageEvent) Do(p *Peer) error {
return fmt.Errorf("notification received: %+v", p)
}
func (e KeepaliveMessageEvent) Do(p *Peer) error {
switch p.State {
case StateOpenConfirm:
p.setState(StateEstablished)
p.startTimers()
p.registerLocalRIBHandlers()
log.Printf("sending initial update messages")
for af, f := range p.AddressFamilies {
for _, e := range f.LocalRIB.Entries() {
if e.AF != af {
continue // 違う AF が RIB に混ざってても広報しない (共有 RIB になった時対策)
}
if err := p.sendMessage(CreateUpdateMessage(e, p.MyAS, p.AddressFamilies[e.AF].SelfNextHop)); err != nil {
return fmt.Errorf("send update message: %w", err)
}
}
}
return nil
case StateEstablished:
p.holdTimer.Reset(time.Duration(p.HoldTime) * time.Second)
return nil
default:
return fmt.Errorf("unexpected state: %v", p.State)
}
}
func (e HoldTimerExpireEvent) Do(p *Peer) error {
// TODO: Send notification?
return fmt.Errorf("hold timer expired")
}
func (e KeepaliveTimerExpireEvent) Do(p *Peer) error {
if err := p.sendMessage(KeepaliveMessage{}); err != nil {
return fmt.Errorf("send keepalive message: %w", err)
}
return nil
}
func (e LocalRIBUpdateEvent) Do(p *Peer) error {
// Withdrawn
if len(e.Removed) > 0 {
for _, r := range e.Removed {
if err := p.sendMessage(CreateWithdrawnMessage(r)); err != nil {
return fmt.Errorf("send withdrawn update message: %w", err)
}
}
}
// Update
for _, e := range e.Updated {
if err := p.sendMessage(CreateUpdateMessage(e, p.MyAS, p.AddressFamilies[e.AF].SelfNextHop)); err != nil {
return fmt.Errorf("send update message: %w", err)
}
}
return nil
}