Class PacketBuilder

java.lang.Object
io.github.mahmoudimus.quic.packet.PacketBuilder

public final class PacketBuilder extends Object
Builds a datagram of one or more coalesced packets: write a header, fill the frame space, then pad and protect. A client's Initial datagram must reach 1200 bytes (RFC 9000 section 14.1), which finish(int) pads to.

 PacketBuilder b = new PacketBuilder(ByteBuffer.allocate(1500), 1500);
 ByteBuffer frames = b.begin(new PacketHeader.Initial(V1, dcid, scid, new byte[0], 0, 1), keys.local());
 Frame.Crypto.encode(frames, 0, clientHello, frames.remaining());
 b.finish(PacketBuilder.MIN_INITIAL_DATAGRAM);
 ByteBuffer datagram = b.datagram();
 

Instances are not thread-safe.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The smallest datagram that may carry a client's Initial packet (RFC 9000 section 14.1).
  • Constructor Summary

    Constructors
    Constructor
    Description
    PacketBuilder(ByteBuffer buffer, int maxDatagramSize)
    A builder that writes from the buffer's position.
  • Method Summary

    Modifier and Type
    Method
    Description
    Starts a packet after any already finished in this datagram.
    The datagram built so far: every finished packet.
    int
    Finishes the current packet without padding beyond what header protection needs.
    int
    finish(int minDatagramSize)
    Finishes the current packet: pads it so that header protection can sample it and, if minDatagramSize asks for it, so that the datagram reaches that size; then protects it.
    int
    finish(int minDatagramSize, Consumer<ByteBuffer> rewrite)
    finish(int), letting rewrite lay the plaintext payload out again before it is protected: it gets the payload, padding included, from its position to its limit, and must keep its length.
    int
    padAfter(int minDatagramSize)
    Pads the datagram with zero bytes after its last finished packet, up to minDatagramSize: bytes a receiver discards as not a packet, as neqo pads its Initial datagrams.
    int
    The frame space left in the current packet.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MIN_INITIAL_DATAGRAM

      public static final int MIN_INITIAL_DATAGRAM
      The smallest datagram that may carry a client's Initial packet (RFC 9000 section 14.1).
      See Also:
  • Constructor Details

    • PacketBuilder

      public PacketBuilder(ByteBuffer buffer, int maxDatagramSize)
      A builder that writes from the buffer's position.
      Parameters:
      buffer - the datagram buffer; packets are written from its position on
      maxDatagramSize - the largest datagram to build, such as the path MTU's UDP payload
  • Method Details

    • begin

      public ByteBuffer begin(PacketHeader.Numbered header, DirectionalKeys keys)
      Starts a packet after any already finished in this datagram.
      Parameters:
      header - the header, with the packet number to send
      keys - the sender's keys for the header's level
      Returns:
      the frame space: a buffer whose position is where frames go and whose limit leaves room for the AEAD tag; write frames at its position
      Throws:
      IllegalStateException - if a packet is already in progress
      BufferOverflowException - if the header does not fit; the builder is unchanged
    • remaining

      public int remaining()
      The frame space left in the current packet.
      Returns:
      the bytes that frames may still use
    • finish

      public int finish(int minDatagramSize)
      Finishes the current packet: pads it so that header protection can sample it and, if minDatagramSize asks for it, so that the datagram reaches that size; then protects it.
      Parameters:
      minDatagramSize - the size the datagram must reach with this packet, or 0
      Returns:
      the index just past the protected packet
      Throws:
      IllegalStateException - if no packet is in progress or the padding does not fit
    • finish

      public int finish(int minDatagramSize, Consumer<ByteBuffer> rewrite)
      finish(int), letting rewrite lay the plaintext payload out again before it is protected: it gets the payload, padding included, from its position to its limit, and must keep its length.
      Parameters:
      minDatagramSize - the size the datagram must reach with this packet, or 0
      rewrite - the rewrite, or null for none
      Returns:
      the index just past the protected packet
    • finish

      public int finish()
      Finishes the current packet without padding beyond what header protection needs.
      Returns:
      the index just past the protected packet
    • padAfter

      public int padAfter(int minDatagramSize)
      Pads the datagram with zero bytes after its last finished packet, up to minDatagramSize: bytes a receiver discards as not a packet, as neqo pads its Initial datagrams.
      Parameters:
      minDatagramSize - the size the datagram must reach
      Returns:
      the index just past the padding
      Throws:
      IllegalStateException - if a packet is in progress or the padding does not fit
    • datagram

      public ByteBuffer datagram()
      The datagram built so far: every finished packet.
      Returns:
      a view from the first packet to the end of the last finished one