-
Notifications
You must be signed in to change notification settings - Fork 26
/
fsm.go
233 lines (215 loc) · 5.09 KB
/
fsm.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
// Copyright 2018-2024 go-m3ua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package m3ua
import (
"context"
"errors"
"sync"
"github.com/wmnsk/go-m3ua/messages"
"github.com/wmnsk/go-m3ua/messages/params"
)
// State represents ASP State.
type State uint8
// M3UA status definitions.
const (
StateAspDown State = iota
StateAspInactive
StateAspActive
StateSCTPCDI
StateSCTPRI
)
func (c *Conn) handleStateUpdate(current State) error {
c.mu.Lock()
defer c.mu.Unlock()
previous := c.state
c.state = current
switch c.mode {
case modeClient:
if err := c.handleStateUpdateAsClient(current, previous); err != nil {
return err
}
return nil
case modeServer:
if err := c.handleStateUpdateAsServer(current, previous); err != nil {
return err
}
return nil
default:
return errors.New("not implemented yet")
}
}
func (c *Conn) handleStateUpdateAsClient(current, previous State) error {
switch current {
case StateAspDown:
c.sctpInfo.Stream = 0
return c.initiateASPSM()
case StateAspInactive:
return c.initiateASPTM()
case StateAspActive:
if current != previous {
c.established <- struct{}{}
c.beatAllow.Broadcast()
}
return nil
case StateSCTPCDI, StateSCTPRI:
return ErrSCTPNotAlive
default:
return ErrInvalidState
}
}
func (c *Conn) handleStateUpdateAsServer(current, previous State) error {
switch current {
case StateAspDown:
// do nothing. just wait for the message from peer and state is updated
return nil
case StateAspInactive:
// do nothing. just wait for the message from peer and state is updated
// XXX - send DAVA to notify peer?
return nil
case StateAspActive:
if current != previous {
c.established <- struct{}{}
c.beatAllow.Broadcast()
}
return nil
case StateSCTPCDI, StateSCTPRI:
return ErrSCTPNotAlive
default:
return ErrInvalidState
}
}
func (c *Conn) handleSignals(ctx context.Context, m3 messages.M3UA) {
select {
case <-ctx.Done():
return
default:
}
// Signal validations
if m3.Version() != 1 {
c.errChan <- NewErrInvalidVersion(m3.Version())
return
}
switch msg := m3.(type) {
// Transfer message
case *messages.Data:
go c.handleData(ctx, msg)
c.stateChan <- c.state
// ASPSM
case *messages.AspUp:
if err := c.handleAspUp(msg); err != nil {
c.errChan <- err
}
c.stateChan <- StateAspInactive
case *messages.AspUpAck:
if err := c.handleAspUpAck(msg); err != nil {
c.errChan <- err
}
c.stateChan <- StateAspInactive
case *messages.AspDown:
if err := c.handleAspDown(msg); err != nil {
c.errChan <- err
}
c.stateChan <- StateAspDown
case *messages.AspDownAck:
if err := c.handleAspDownAck(msg); err != nil {
c.errChan <- err
}
c.stateChan <- StateAspDown
// ASPTM
case *messages.AspActive:
if err := c.handleAspActive(msg); err != nil {
c.errChan <- err
}
c.stateChan <- StateAspActive
case *messages.AspActiveAck:
if err := c.handleAspActiveAck(msg); err != nil {
c.errChan <- err
}
c.stateChan <- StateAspActive
case *messages.AspInactive:
if err := c.handleAspInactive(msg); err != nil {
c.errChan <- err
}
c.stateChan <- StateAspInactive
case *messages.AspInactiveAck:
if err := c.handleAspInactiveAck(msg); err != nil {
c.errChan <- err
}
c.stateChan <- StateAspInactive
case *messages.Heartbeat:
if err := c.handleHeartbeat(msg); err != nil {
c.errChan <- err
}
c.stateChan <- c.state
case *messages.HeartbeatAck:
if err := c.handleHeartbeatAck(msg); err != nil {
c.errChan <- err
}
c.beatAckChan <- struct{}{}
c.stateChan <- c.state
// Management
case *messages.Error:
if err := c.handleError(msg); err != nil {
c.errChan <- err
}
c.stateChan <- c.state
case *messages.Notify:
if err := c.handleNotify(msg); err != nil {
c.errChan <- err
}
c.stateChan <- c.state
// Others: SSNM and RKM is not implemented.
default:
c.errChan <- NewErrUnsupportedMessage(m3)
c.stateChan <- c.state
}
}
func (c *Conn) monitor(ctx context.Context) {
c.errChan = make(chan error)
c.dataChan = make(chan *params.ProtocolDataPayload, 0xffff)
c.beatAckChan = make(chan struct{})
c.beatAllow = sync.NewCond(&sync.Mutex{})
c.beatAllow.L.Lock()
go c.heartbeat(ctx)
defer c.beatAllow.Broadcast()
buf := make([]byte, 1500)
for {
select {
case <-ctx.Done():
c.Close()
return
case err := <-c.errChan:
if e := c.handleErrors(err); e != nil {
c.Close()
return
}
continue
case state := <-c.stateChan:
// Act properly based on current state.
if err := c.handleStateUpdate(state); err != nil {
if errors.Is(err, ErrSCTPNotAlive) {
c.Close()
return
}
}
// Read from conn to see something coming from the peer.
n, _, err := c.sctpConn.SCTPRead(buf)
if err != nil {
c.Close()
return
}
raw := make([]byte, n)
copy(raw, buf)
go func() {
// Parse the received packet as M3UA. Undecodable packets are ignored.
msg, err := messages.Parse(raw)
if err != nil {
logf("failed to parse M3UA message: %v, %x", err, raw)
return
}
c.handleSignals(ctx, msg)
}()
}
}
}