forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_parser.go
191 lines (177 loc) · 5.41 KB
/
frame_parser.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
package wire
import (
"bytes"
"errors"
"fmt"
"reflect"
"github.com/quic-go/quic-go/internal/protocol"
"github.com/quic-go/quic-go/internal/qerr"
"github.com/quic-go/quic-go/quicvarint"
)
const (
pingFrameType = 0x1
ackFrameType = 0x2
ackECNFrameType = 0x3
resetStreamFrameType = 0x4
stopSendingFrameType = 0x5
cryptoFrameType = 0x6
newTokenFrameType = 0x7
maxDataFrameType = 0x10
maxStreamDataFrameType = 0x11
bidiMaxStreamsFrameType = 0x12
uniMaxStreamsFrameType = 0x13
dataBlockedFrameType = 0x14
streamDataBlockedFrameType = 0x15
bidiStreamBlockedFrameType = 0x16
uniStreamBlockedFrameType = 0x17
newConnectionIDFrameType = 0x18
retireConnectionIDFrameType = 0x19
pathChallengeFrameType = 0x1a
pathResponseFrameType = 0x1b
connectionCloseFrameType = 0x1c
applicationCloseFrameType = 0x1d
handshakeDoneFrameType = 0x1e
)
type frameParser struct {
r bytes.Reader // cached bytes.Reader, so we don't have to repeatedly allocate them
ackDelayExponent uint8
supportsDatagrams bool
// To avoid allocating when parsing, keep a single ACK frame struct.
// It is used over and over again.
ackFrame *AckFrame
}
var _ FrameParser = &frameParser{}
// NewFrameParser creates a new frame parser.
func NewFrameParser(supportsDatagrams bool) *frameParser {
return &frameParser{
r: *bytes.NewReader(nil),
supportsDatagrams: supportsDatagrams,
ackFrame: &AckFrame{},
}
}
// ParseNext parses the next frame.
// It skips PADDING frames.
func (p *frameParser) ParseNext(data []byte, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (int, Frame, error) {
startLen := len(data)
p.r.Reset(data)
frame, err := p.parseNext(&p.r, encLevel, v)
n := startLen - p.r.Len()
p.r.Reset(nil)
return n, frame, err
}
func (p *frameParser) parseNext(r *bytes.Reader, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (Frame, error) {
for r.Len() != 0 {
typ, err := quicvarint.Read(r)
if err != nil {
return nil, &qerr.TransportError{
ErrorCode: qerr.FrameEncodingError,
ErrorMessage: err.Error(),
}
}
if typ == 0x0 { // skip PADDING frames
continue
}
f, err := p.parseFrame(r, typ, encLevel, v)
if err != nil {
return nil, &qerr.TransportError{
FrameType: typ,
ErrorCode: qerr.FrameEncodingError,
ErrorMessage: err.Error(),
}
}
return f, nil
}
return nil, nil
}
func (p *frameParser) parseFrame(r *bytes.Reader, typ uint64, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (Frame, error) {
var frame Frame
var err error
if typ&0xf8 == 0x8 {
frame, err = parseStreamFrame(r, typ, v)
} else {
switch typ {
case pingFrameType:
frame = &PingFrame{}
case ackFrameType, ackECNFrameType:
ackDelayExponent := p.ackDelayExponent
if encLevel != protocol.Encryption1RTT {
ackDelayExponent = protocol.DefaultAckDelayExponent
}
p.ackFrame.Reset()
err = parseAckFrame(p.ackFrame, r, typ, ackDelayExponent, v)
frame = p.ackFrame
case resetStreamFrameType:
frame, err = parseResetStreamFrame(r, v)
case stopSendingFrameType:
frame, err = parseStopSendingFrame(r, v)
case cryptoFrameType:
frame, err = parseCryptoFrame(r, v)
case newTokenFrameType:
frame, err = parseNewTokenFrame(r, v)
case maxDataFrameType:
frame, err = parseMaxDataFrame(r, v)
case maxStreamDataFrameType:
frame, err = parseMaxStreamDataFrame(r, v)
case bidiMaxStreamsFrameType, uniMaxStreamsFrameType:
frame, err = parseMaxStreamsFrame(r, typ, v)
case dataBlockedFrameType:
frame, err = parseDataBlockedFrame(r, v)
case streamDataBlockedFrameType:
frame, err = parseStreamDataBlockedFrame(r, v)
case bidiStreamBlockedFrameType, uniStreamBlockedFrameType:
frame, err = parseStreamsBlockedFrame(r, typ, v)
case newConnectionIDFrameType:
frame, err = parseNewConnectionIDFrame(r, v)
case retireConnectionIDFrameType:
frame, err = parseRetireConnectionIDFrame(r, v)
case pathChallengeFrameType:
frame, err = parsePathChallengeFrame(r, v)
case pathResponseFrameType:
frame, err = parsePathResponseFrame(r, v)
case connectionCloseFrameType, applicationCloseFrameType:
frame, err = parseConnectionCloseFrame(r, typ, v)
case handshakeDoneFrameType:
frame = &HandshakeDoneFrame{}
case 0x30, 0x31:
if p.supportsDatagrams {
frame, err = parseDatagramFrame(r, typ, v)
break
}
fallthrough
default:
err = errors.New("unknown frame type")
}
}
if err != nil {
return nil, err
}
if !p.isAllowedAtEncLevel(frame, encLevel) {
return nil, fmt.Errorf("%s not allowed at encryption level %s", reflect.TypeOf(frame).Elem().Name(), encLevel)
}
return frame, nil
}
func (p *frameParser) isAllowedAtEncLevel(f Frame, encLevel protocol.EncryptionLevel) bool {
switch encLevel {
case protocol.EncryptionInitial, protocol.EncryptionHandshake:
switch f.(type) {
case *CryptoFrame, *AckFrame, *ConnectionCloseFrame, *PingFrame:
return true
default:
return false
}
case protocol.Encryption0RTT:
switch f.(type) {
case *CryptoFrame, *AckFrame, *ConnectionCloseFrame, *NewTokenFrame, *PathResponseFrame, *RetireConnectionIDFrame:
return false
default:
return true
}
case protocol.Encryption1RTT:
return true
default:
panic("unknown encryption level")
}
}
func (p *frameParser) SetAckDelayExponent(exp uint8) {
p.ackDelayExponent = exp
}