Skip to content

Commit 9efdb0d

Browse files
committed
alts: move ParseFramedMsg out of common
It's only called in one place, and is effectively a method on conn. Part of #8510.
1 parent 55e8b90 commit 9efdb0d

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

credentials/alts/internal/conn/common.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
package conn
2020

2121
import (
22-
"encoding/binary"
2322
"errors"
24-
"fmt"
2523
)
2624

2725
const (
@@ -48,33 +46,3 @@ func SliceForAppend(in []byte, n int) (head, tail []byte) {
4846
tail = head[len(in):]
4947
return head, tail
5048
}
51-
52-
// ParseFramedMsg parse the provided buffer and returns a frame of the format
53-
// msgLength+msg and any remaining bytes in that buffer.
54-
func ParseFramedMsg(b []byte, maxLen uint32) ([]byte, []byte, error) {
55-
// If the size field is not complete, return the provided buffer as
56-
// remaining buffer.
57-
length, sufficientBytes := parseMessageLength(b)
58-
if !sufficientBytes {
59-
return nil, b, nil
60-
}
61-
if length > maxLen {
62-
return nil, nil, fmt.Errorf("received the frame length %d larger than the limit %d", length, maxLen)
63-
}
64-
if len(b) < int(length)+4 { // account for the first 4 msg length bytes.
65-
// Frame is not complete yet.
66-
return nil, b, nil
67-
}
68-
return b[:MsgLenFieldSize+length], b[MsgLenFieldSize+length:], nil
69-
}
70-
71-
// parseMessageLength returns the message length based on frame header. It also
72-
// returns a boolean indicating if the buffer contains sufficient bytes to parse
73-
// the length header. If there are insufficient bytes, (0, false) is returned.
74-
func parseMessageLength(b []byte) (uint32, bool) {
75-
if len(b) < MsgLenFieldSize {
76-
return 0, false
77-
}
78-
msgLenField := b[:MsgLenFieldSize]
79-
return binary.LittleEndian.Uint32(msgLenField), true
80-
}

credentials/alts/internal/conn/record.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func NewConn(c net.Conn, side core.Side, recordProtocol string, key []byte, prot
144144
func (p *conn) Read(b []byte) (n int, err error) {
145145
if len(p.buf) == 0 {
146146
var framedMsg []byte
147-
framedMsg, p.nextFrame, err = ParseFramedMsg(p.nextFrame, altsRecordLengthLimit)
147+
framedMsg, err = p.parseFramedMsg(p.nextFrame, altsRecordLengthLimit)
148148
if err != nil {
149149
return n, err
150150
}
@@ -184,7 +184,7 @@ func (p *conn) Read(b []byte) (n int, err error) {
184184
return 0, err
185185
}
186186
p.protected = p.protected[:len(p.protected)+n]
187-
framedMsg, p.nextFrame, err = ParseFramedMsg(p.protected, altsRecordLengthLimit)
187+
framedMsg, err = p.parseFramedMsg(p.protected, altsRecordLengthLimit)
188188
if err != nil {
189189
return 0, err
190190
}
@@ -225,6 +225,38 @@ func (p *conn) Read(b []byte) (n int, err error) {
225225
return n, nil
226226
}
227227

228+
// parseFramedMsg parses the provided buffer and returns a frame of the format
229+
// msgLength+msg iff a full frame is available.
230+
func (p *conn) parseFramedMsg(b []byte, maxLen uint32) ([]byte, error) {
231+
// If the size field is not complete, return the provided buffer as
232+
// remaining buffer.
233+
p.nextFrame = b
234+
length, sufficientBytes := parseMessageLength(b)
235+
if !sufficientBytes {
236+
return nil, nil
237+
}
238+
if length > maxLen {
239+
return nil, fmt.Errorf("received the frame length %d larger than the limit %d", length, maxLen)
240+
}
241+
if len(b) < int(length)+4 { // account for the first 4 msg length bytes.
242+
// Frame is not complete yet.
243+
return nil, nil
244+
}
245+
p.nextFrame = b[MsgLenFieldSize+length:]
246+
return b[:MsgLenFieldSize+length], nil
247+
}
248+
249+
// parseMessageLength returns the message length based on frame header. It also
250+
// returns a boolean indicating if the buffer contains sufficient bytes to parse
251+
// the length header. If there are insufficient bytes, (0, false) is returned.
252+
func parseMessageLength(b []byte) (uint32, bool) {
253+
if len(b) < MsgLenFieldSize {
254+
return 0, false
255+
}
256+
msgLenField := b[:MsgLenFieldSize]
257+
return binary.LittleEndian.Uint32(msgLenField), true
258+
}
259+
228260
// Write encrypts, frames, and writes bytes from b to the underlying connection.
229261
func (p *conn) Write(b []byte) (n int, err error) {
230262
n = len(b)

0 commit comments

Comments
 (0)