github.com/quic-go/quic-go@v0.44.0/internal/wire/datagram_frame.go (about)

     1  package wire
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/quic-go/quic-go/internal/protocol"
     7  	"github.com/quic-go/quic-go/quicvarint"
     8  )
     9  
    10  // MaxDatagramSize is the maximum size of a DATAGRAM frame (RFC 9221).
    11  // By setting it to a large value, we allow all datagrams that fit into a QUIC packet.
    12  // The value is chosen such that it can still be encoded as a 2 byte varint.
    13  // This is a var and not a const so it can be set in tests.
    14  var MaxDatagramSize protocol.ByteCount = 16383
    15  
    16  // A DatagramFrame is a DATAGRAM frame
    17  type DatagramFrame struct {
    18  	DataLenPresent bool
    19  	Data           []byte
    20  }
    21  
    22  func parseDatagramFrame(b []byte, typ uint64, _ protocol.Version) (*DatagramFrame, int, error) {
    23  	startLen := len(b)
    24  	f := &DatagramFrame{}
    25  	f.DataLenPresent = typ&0x1 > 0
    26  
    27  	var length uint64
    28  	if f.DataLenPresent {
    29  		var err error
    30  		var l int
    31  		length, l, err = quicvarint.Parse(b)
    32  		if err != nil {
    33  			return nil, 0, replaceUnexpectedEOF(err)
    34  		}
    35  		b = b[l:]
    36  		if length > uint64(len(b)) {
    37  			return nil, 0, io.EOF
    38  		}
    39  	} else {
    40  		length = uint64(len(b))
    41  	}
    42  	f.Data = make([]byte, length)
    43  	copy(f.Data, b)
    44  	return f, startLen - len(b) + int(length), nil
    45  }
    46  
    47  func (f *DatagramFrame) Append(b []byte, _ protocol.Version) ([]byte, error) {
    48  	typ := uint8(0x30)
    49  	if f.DataLenPresent {
    50  		typ ^= 0b1
    51  	}
    52  	b = append(b, typ)
    53  	if f.DataLenPresent {
    54  		b = quicvarint.Append(b, uint64(len(f.Data)))
    55  	}
    56  	b = append(b, f.Data...)
    57  	return b, nil
    58  }
    59  
    60  // MaxDataLen returns the maximum data length
    61  func (f *DatagramFrame) MaxDataLen(maxSize protocol.ByteCount, version protocol.Version) protocol.ByteCount {
    62  	headerLen := protocol.ByteCount(1)
    63  	if f.DataLenPresent {
    64  		// pretend that the data size will be 1 bytes
    65  		// if it turns out that varint encoding the length will consume 2 bytes, we need to adjust the data length afterwards
    66  		headerLen++
    67  	}
    68  	if headerLen > maxSize {
    69  		return 0
    70  	}
    71  	maxDataLen := maxSize - headerLen
    72  	if f.DataLenPresent && quicvarint.Len(uint64(maxDataLen)) != 1 {
    73  		maxDataLen--
    74  	}
    75  	return maxDataLen
    76  }
    77  
    78  // Length of a written frame
    79  func (f *DatagramFrame) Length(_ protocol.Version) protocol.ByteCount {
    80  	length := 1 + protocol.ByteCount(len(f.Data))
    81  	if f.DataLenPresent {
    82  		length += protocol.ByteCount(quicvarint.Len(uint64(len(f.Data))))
    83  	}
    84  	return length
    85  }