golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/crypto_stream.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.21
     6  
     7  package quic
     8  
     9  // "Implementations MUST support buffering at least 4096 bytes of data
    10  // received in out-of-order CRYPTO frames."
    11  // https://www.rfc-editor.org/rfc/rfc9000.html#section-7.5-2
    12  //
    13  // 4096 is too small for real-world cases, however, so we allow more.
    14  const cryptoBufferSize = 1 << 20
    15  
    16  // A cryptoStream is the stream of data passed in CRYPTO frames.
    17  // There is one cryptoStream per packet number space.
    18  type cryptoStream struct {
    19  	// CRYPTO data received from the peer.
    20  	in    pipe
    21  	inset rangeset[int64] // bytes received
    22  
    23  	// CRYPTO data queued for transmission to the peer.
    24  	out       pipe
    25  	outunsent rangeset[int64] // bytes in need of sending
    26  	outacked  rangeset[int64] // bytes acked by peer
    27  }
    28  
    29  // handleCrypto processes data received in a CRYPTO frame.
    30  func (s *cryptoStream) handleCrypto(off int64, b []byte, f func([]byte) error) error {
    31  	end := off + int64(len(b))
    32  	if end-s.inset.min() > cryptoBufferSize {
    33  		return localTransportError{
    34  			code:   errCryptoBufferExceeded,
    35  			reason: "crypto buffer exceeded",
    36  		}
    37  	}
    38  	s.inset.add(off, end)
    39  	if off == s.in.start {
    40  		// Fast path: This is the next chunk of data in the stream,
    41  		// so just handle it immediately.
    42  		if err := f(b); err != nil {
    43  			return err
    44  		}
    45  		s.in.discardBefore(end)
    46  	} else {
    47  		// This is either data we've already processed,
    48  		// data we can't process yet, or a mix of both.
    49  		s.in.writeAt(b, off)
    50  	}
    51  	// s.in.start is the next byte in sequence.
    52  	// If it's in s.inset, we have bytes to provide.
    53  	// If it isn't, we don't--we're either out of data,
    54  	// or only have data that comes after the next byte.
    55  	if !s.inset.contains(s.in.start) {
    56  		return nil
    57  	}
    58  	// size is the size of the first contiguous chunk of bytes
    59  	// that have not been processed yet.
    60  	size := int(s.inset[0].end - s.in.start)
    61  	if size <= 0 {
    62  		return nil
    63  	}
    64  	err := s.in.read(s.in.start, size, f)
    65  	s.in.discardBefore(s.inset[0].end)
    66  	return err
    67  }
    68  
    69  // write queues data for sending to the peer.
    70  // It does not block or limit the amount of buffered data.
    71  // QUIC connections don't communicate the amount of CRYPTO data they are willing to buffer,
    72  // so we send what we have and the peer can close the connection if it is too much.
    73  func (s *cryptoStream) write(b []byte) {
    74  	start := s.out.end
    75  	s.out.writeAt(b, start)
    76  	s.outunsent.add(start, s.out.end)
    77  }
    78  
    79  // ackOrLoss reports that an CRYPTO frame sent by us has been acknowledged by the peer, or lost.
    80  func (s *cryptoStream) ackOrLoss(start, end int64, fate packetFate) {
    81  	switch fate {
    82  	case packetAcked:
    83  		s.outacked.add(start, end)
    84  		s.outunsent.sub(start, end)
    85  		// If this ack is for data at the start of the send buffer, we can now discard it.
    86  		if s.outacked.contains(s.out.start) {
    87  			s.out.discardBefore(s.outacked[0].end)
    88  		}
    89  	case packetLost:
    90  		// Mark everything lost, but not previously acked, as needing retransmission.
    91  		// We do this by adding all the lost bytes to outunsent, and then
    92  		// removing everything already acked.
    93  		s.outunsent.add(start, end)
    94  		for _, a := range s.outacked {
    95  			s.outunsent.sub(a.start, a.end)
    96  		}
    97  	}
    98  }
    99  
   100  // dataToSend reports what data should be sent in CRYPTO frames to the peer.
   101  // It calls f with each range of data to send.
   102  // f uses sendData to get the bytes to send, and returns the number of bytes sent.
   103  // dataToSend calls f until no data is left, or f returns 0.
   104  //
   105  // This function is unusually indirect (why not just return a []byte,
   106  // or implement io.Reader?).
   107  //
   108  // Returning a []byte to the caller either requires that we store the
   109  // data to send contiguously (which we don't), allocate a temporary buffer
   110  // and copy into it (inefficient), or return less data than we have available
   111  // (requires complexity to avoid unnecessarily breaking data across frames).
   112  //
   113  // Accepting a []byte from the caller (io.Reader) makes packet construction
   114  // difficult. Since CRYPTO data is encoded with a varint length prefix, the
   115  // location of the data depends on the length of the data. (We could hardcode
   116  // a 2-byte length, of course.)
   117  //
   118  // Instead, we tell the caller how much data is, the caller figures out where
   119  // to put it (and possibly decides that it doesn't have space for this data
   120  // in the packet after all), and the caller then makes a separate call to
   121  // copy the data it wants into position.
   122  func (s *cryptoStream) dataToSend(pto bool, f func(off, size int64) (sent int64)) {
   123  	for {
   124  		off, size := dataToSend(s.out.start, s.out.end, s.outunsent, s.outacked, pto)
   125  		if size == 0 {
   126  			return
   127  		}
   128  		n := f(off, size)
   129  		if n == 0 || pto {
   130  			return
   131  		}
   132  	}
   133  }
   134  
   135  // sendData fills b with data to send to the peer, starting at off,
   136  // and marks the data as sent. The caller must have already ascertained
   137  // that there is data to send in this region using dataToSend.
   138  func (s *cryptoStream) sendData(off int64, b []byte) {
   139  	s.out.copy(off, b)
   140  	s.outunsent.sub(off, off+int64(len(b)))
   141  }