1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/external/github.com/lucas-clemente/quic-go/internal/wire/frame_parser.go

79 lines
1.9 KiB
Go
Raw Normal View History

2018-11-20 17:51:25 -05:00
package wire
import (
"bytes"
"fmt"
2019-01-17 09:33:18 -05:00
"v2ray.com/core/external/github.com/lucas-clemente/quic-go/internal/protocol"
"v2ray.com/core/external/github.com/lucas-clemente/quic-go/internal/qerr"
2018-11-20 17:51:25 -05:00
)
// ParseNextFrame parses the next frame
// It skips PADDING frames.
2018-11-23 11:04:53 -05:00
func ParseNextFrame(r *bytes.Reader, v protocol.VersionNumber) (Frame, error) {
2018-11-20 17:51:25 -05:00
for r.Len() != 0 {
typeByte, _ := r.ReadByte()
if typeByte == 0x0 { // PADDING frame
continue
}
r.UnreadByte()
2018-11-23 11:04:53 -05:00
return parseFrame(r, typeByte, v)
2018-11-20 17:51:25 -05:00
}
return nil, nil
}
2018-11-23 11:04:53 -05:00
func parseFrame(r *bytes.Reader, typeByte byte, v protocol.VersionNumber) (Frame, error) {
2018-11-20 17:51:25 -05:00
var frame Frame
var err error
2018-11-23 11:04:53 -05:00
if typeByte&0xf8 == 0x8 {
2018-11-20 17:51:25 -05:00
frame, err = parseStreamFrame(r, v)
if err != nil {
2018-11-23 11:04:53 -05:00
return nil, qerr.Error(qerr.InvalidFrameData, err.Error())
2018-11-20 17:51:25 -05:00
}
2018-11-23 11:04:53 -05:00
return frame, nil
2018-11-20 17:51:25 -05:00
}
switch typeByte {
case 0x1:
2018-11-23 11:04:53 -05:00
frame, err = parsePingFrame(r, v)
case 0x2, 0x3:
frame, err = parseAckFrame(r, v)
2018-11-20 17:51:25 -05:00
case 0x4:
2018-11-23 11:04:53 -05:00
frame, err = parseResetStreamFrame(r, v)
2018-11-20 17:51:25 -05:00
case 0x5:
2018-11-23 11:04:53 -05:00
frame, err = parseStopSendingFrame(r, v)
2018-11-20 17:51:25 -05:00
case 0x6:
2018-11-23 11:04:53 -05:00
frame, err = parseCryptoFrame(r, v)
2018-11-20 17:51:25 -05:00
case 0x7:
2018-11-23 11:04:53 -05:00
frame, err = parseNewTokenFrame(r, v)
case 0x10:
frame, err = parseMaxDataFrame(r, v)
case 0x11:
frame, err = parseMaxStreamDataFrame(r, v)
case 0x12, 0x13:
frame, err = parseMaxStreamsFrame(r, v)
case 0x14:
frame, err = parseDataBlockedFrame(r, v)
case 0x15:
frame, err = parseStreamDataBlockedFrame(r, v)
case 0x16, 0x17:
frame, err = parseStreamsBlockedFrame(r, v)
case 0x18:
frame, err = parseNewConnectionIDFrame(r, v)
case 0x19:
frame, err = parseRetireConnectionIDFrame(r, v)
case 0x1a:
2018-11-20 17:51:25 -05:00
frame, err = parsePathChallengeFrame(r, v)
2018-11-23 11:04:53 -05:00
case 0x1b:
2018-11-20 17:51:25 -05:00
frame, err = parsePathResponseFrame(r, v)
2018-11-23 11:04:53 -05:00
case 0x1c, 0x1d:
2018-11-20 17:51:25 -05:00
frame, err = parseConnectionCloseFrame(r, v)
default:
2018-11-23 11:04:53 -05:00
err = fmt.Errorf("unknown type byte 0x%x", typeByte)
}
if err != nil {
return nil, qerr.Error(qerr.InvalidFrameData, err.Error())
2018-11-20 17:51:25 -05:00
}
2018-11-23 11:04:53 -05:00
return frame, nil
2018-11-20 17:51:25 -05:00
}