-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
265 lines (243 loc) · 5.87 KB
/
server.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
package corebgp
import (
"encoding/binary"
"errors"
"fmt"
"net"
"net/netip"
"sync"
)
// Server is a BGP server that manages peers.
type Server struct {
mu sync.Mutex
id uint32
peers map[string]*peer
// control channels & run state
serving bool
doneServingCh chan struct{}
closeCh chan struct{}
closeOnce sync.Once
}
// NewServer creates a new Server.
func NewServer(routerID netip.Addr) (*Server, error) {
if !routerID.Is4() {
return nil, errors.New("invalid router ID")
}
s := &Server{
mu: sync.Mutex{},
id: binary.BigEndian.Uint32(routerID.AsSlice()),
peers: make(map[string]*peer),
doneServingCh: make(chan struct{}),
closeCh: make(chan struct{}),
}
return s, nil
}
var (
ErrServerClosed = errors.New("server closed")
ErrPeerNotExist = errors.New("peer does not exist")
ErrPeerAlreadyExists = errors.New("peer already exists")
)
func (s *Server) handleInboundConn(conn net.Conn) {
h, _, err := net.SplitHostPort(conn.RemoteAddr().String())
if err != nil {
conn.Close()
return
}
s.mu.Lock()
defer s.mu.Unlock()
p, exists := s.peers[h]
if !exists {
conn.Close()
return
}
if p.options.localAddress.IsValid() {
h, _, err = net.SplitHostPort(conn.LocalAddr().String())
laddr, _ := netip.ParseAddr(h)
if err != nil || p.options.localAddress != laddr {
conn.Close()
return
}
}
p.incomingConnection(conn)
}
// Serve starts all peers' FSMs, starts handling incoming connections if a
// non-nil listener is provided, and then blocks. Serve returns ErrServerClosed
// upon Close() or a listener error if one occurs.
func (s *Server) Serve(listeners []net.Listener) error {
s.mu.Lock()
// check if server has been closed
select {
case <-s.doneServingCh:
s.mu.Unlock()
return ErrServerClosed
case <-s.closeCh:
s.mu.Unlock()
return ErrServerClosed
default:
}
// set serving state and enable peers
s.serving = true
for _, peer := range s.peers {
peer.start()
}
s.mu.Unlock()
defer func() {
// disable peers and set serving state before returning
s.mu.Lock()
for _, peer := range s.peers {
peer.stop()
}
s.serving = false
close(s.doneServingCh)
s.mu.Unlock()
}()
lisErrCh := make(chan error)
lisWG := &sync.WaitGroup{}
closingListeners := make(chan struct{})
for _, lis := range listeners {
lisWG.Add(1)
go func(lis net.Listener) {
defer lisWG.Done()
for {
conn, err := lis.Accept()
if err != nil {
select {
case lisErrCh <- err:
case <-closingListeners:
}
return
}
s.handleInboundConn(conn)
}
}(lis)
}
closeListeners := func() {
close(closingListeners)
for _, lis := range listeners {
lis.Close()
}
lisWG.Wait()
}
select {
case <-s.closeCh:
closeListeners()
return ErrServerClosed
case err := <-lisErrCh:
closeListeners()
return fmt.Errorf("listener error: %v", err)
}
}
// Close stops the Server. An instance of a stopped Server cannot be re-used.
func (s *Server) Close() {
s.mu.Lock()
s.closeOnce.Do(func() {
close(s.closeCh)
})
if !s.serving {
s.mu.Unlock()
return
}
s.mu.Unlock()
<-s.doneServingCh
}
// PeerConfig is the required configuration for a Peer.
type PeerConfig struct {
// RemoteAddress is the remote address of the peer.
RemoteAddress netip.Addr
// LocalAS is the local autonomous system number to populate in outbound
// OPEN messages.
LocalAS uint32
// RemoteAS is the autonomous system number to expect in OPEN messages
// from this peer.
RemoteAS uint32
}
func (p PeerConfig) validate(opts peerOptions) error {
if !opts.localAddress.IsValid() && p.RemoteAddress.IsValid() {
return nil
}
localIsIPv4 := opts.localAddress.Is4()
remoteIsIPv4 := p.RemoteAddress.Is4()
if localIsIPv4 != remoteIsIPv4 {
return errors.New("mixed address family peer address pair")
}
if !localIsIPv4 {
if !opts.localAddress.Is6() || !p.RemoteAddress.Is6() {
return errors.New("invalid peer address pair")
}
}
// https://tools.ietf.org/html/rfc7607
//
// If a BGP speaker receives zero as the peer AS in an OPEN message, it
// MUST abort the connection and send a NOTIFICATION with Error Code
// "OPEN Message Error" and subcode "Bad Peer AS" (see Section 6 of
// [RFC4271]). A router MUST NOT initiate a connection claiming to be
// AS 0.
if p.LocalAS == 0 || p.RemoteAS == 0 {
return errors.New("AS must be > 0")
}
return nil
}
// AddPeer adds a peer to the Server to be handled with the provided Plugin and
// PeerOptions.
func (s *Server) AddPeer(config PeerConfig, plugin Plugin,
opts ...PeerOption) error {
o := defaultPeerOptions()
for _, opt := range opts {
opt.apply(&o)
}
err := o.validate()
if err != nil {
return fmt.Errorf("invalid peer options: %v", err)
}
err = config.validate(o)
if err != nil {
return fmt.Errorf("peer config invalid: %v", err)
}
s.mu.Lock()
defer s.mu.Unlock()
_, exists := s.peers[config.RemoteAddress.String()]
if exists {
return ErrPeerAlreadyExists
}
p := newPeer(config, s.id, plugin, o)
if s.serving {
p.start()
}
s.peers[p.config.RemoteAddress.String()] = p
return nil
}
// DeletePeer deletes a peer from the Server.
func (s *Server) DeletePeer(ip netip.Addr) error {
s.mu.Lock()
defer s.mu.Unlock()
p, exists := s.peers[ip.String()]
if !exists {
return ErrPeerNotExist
}
if s.serving {
p.stop()
}
delete(s.peers, ip.String())
return nil
}
// GetPeer returns the configuration for the provided peer, or an error if it
// does not exist.
func (s *Server) GetPeer(ip netip.Addr) (PeerConfig, error) {
s.mu.Lock()
defer s.mu.Unlock()
p, exists := s.peers[ip.String()]
if !exists {
return PeerConfig{}, ErrPeerNotExist
}
return p.config, nil
}
// ListPeers returns the configuration for all peers.
func (s *Server) ListPeers() []PeerConfig {
s.mu.Lock()
defer s.mu.Unlock()
configs := make([]PeerConfig, 0)
for _, peer := range s.peers {
configs = append(configs, peer.config)
}
return configs
}