quic

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2024 License: BSD-3-Clause Imports: 29 Imported by: 0

Documentation

Overview

Package quic is an experimental, incomplete implementation of the QUIC protocol. This package is a work in progress, and is not ready for use at this time.

This package implements (or will implement) RFC 9000, RFC 9001, and RFC 9002.

Index

Constants

View Source
const (
	// QLogLevelFrame includes per-frame information.
	// When this level is enabled, packet_sent and packet_received events will
	// contain information on individual frames sent/received.
	QLogLevelFrame = slog.Level(-6)

	// QLogLevelPacket events occur at most once per packet sent or received.
	//
	// For example: packet_sent, packet_received.
	QLogLevelPacket = slog.Level(-4)

	// QLogLevelConn events occur multiple times over a connection's lifetime,
	// but less often than the frequency of individual packets.
	//
	// For example: connection_state_updated.
	QLogLevelConn = slog.Level(-2)

	// QLogLevelEndpoint events occur at most once per connection.
	//
	// For example: connection_started, connection_closed.
	QLogLevelEndpoint = slog.Level(0)
)

Log levels for qlog events.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApplicationError

type ApplicationError struct {
	Code   uint64
	Reason string
}

An ApplicationError is an application protocol error code (RFC 9000, Section 20.2). Application protocol errors may be sent when terminating a stream or connection.

func (*ApplicationError) Error

func (e *ApplicationError) Error() string

func (*ApplicationError) Is added in v0.16.0

func (e *ApplicationError) Is(err error) bool

Is reports a match if err is an *ApplicationError with a matching Code.

type Config

type Config struct {
	// TLSConfig is the endpoint's TLS configuration.
	// It must be non-nil and include at least one certificate or else set GetCertificate.
	TLSConfig *tls.Config

	// MaxBidiRemoteStreams limits the number of simultaneous bidirectional streams
	// a peer may open.
	// If zero, the default value of 100 is used.
	// If negative, the limit is zero.
	MaxBidiRemoteStreams int64

	// MaxUniRemoteStreams limits the number of simultaneous unidirectional streams
	// a peer may open.
	// If zero, the default value of 100 is used.
	// If negative, the limit is zero.
	MaxUniRemoteStreams int64

	// MaxStreamReadBufferSize is the maximum amount of data sent by the peer that a
	// stream will buffer for reading.
	// If zero, the default value of 1MiB is used.
	// If negative, the limit is zero.
	MaxStreamReadBufferSize int64

	// MaxStreamWriteBufferSize is the maximum amount of data a stream will buffer for
	// sending to the peer.
	// If zero, the default value of 1MiB is used.
	// If negative, the limit is zero.
	MaxStreamWriteBufferSize int64

	// MaxConnReadBufferSize is the maximum amount of data sent by the peer that a
	// connection will buffer for reading, across all streams.
	// If zero, the default value of 1MiB is used.
	// If negative, the limit is zero.
	MaxConnReadBufferSize int64

	// RequireAddressValidation may be set to true to enable address validation
	// of client connections prior to starting the handshake.
	//
	// Enabling this setting reduces the amount of work packets with spoofed
	// source address information can cause a server to perform,
	// at the cost of increased handshake latency.
	RequireAddressValidation bool

	// StatelessResetKey is used to provide stateless reset of connections.
	// A restart may leave an endpoint without access to the state of
	// existing connections. Stateless reset permits an endpoint to respond
	// to a packet for a connection it does not recognize.
	//
	// This field should be filled with random bytes.
	// The contents should remain stable across restarts,
	// to permit an endpoint to send a reset for
	// connections created before a restart.
	//
	// The contents of the StatelessResetKey should not be exposed.
	// An attacker can use knowledge of this field's value to
	// reset existing connections.
	//
	// If this field is left as zero, stateless reset is disabled.
	StatelessResetKey [32]byte

	// HandshakeTimeout is the maximum time in which a connection handshake must complete.
	// If zero, the default of 10 seconds is used.
	// If negative, there is no handshake timeout.
	HandshakeTimeout time.Duration

	// MaxIdleTimeout is the maximum time after which an idle connection will be closed.
	// If zero, the default of 30 seconds is used.
	// If negative, idle connections are never closed.
	//
	// The idle timeout for a connection is the minimum of the maximum idle timeouts
	// of the endpoints.
	MaxIdleTimeout time.Duration

	// KeepAlivePeriod is the time after which a packet will be sent to keep
	// an idle connection alive.
	// If zero, keep alive packets are not sent.
	// If greater than zero, the keep alive period is the smaller of KeepAlivePeriod and
	// half the connection idle timeout.
	KeepAlivePeriod time.Duration

	// QLogLogger receives qlog events.
	//
	// Events currently correspond to the definitions in draft-ietf-qlog-quic-events-03.
	// This is not the latest version of the draft, but is the latest version supported
	// by common event log viewers as of the time this paragraph was written.
	//
	// The qlog package contains a slog.Handler which serializes qlog events
	// to a standard JSON representation.
	QLogLogger *slog.Logger
}

A Config structure configures a QUIC endpoint. A Config must not be modified after it has been passed to a QUIC function. A Config may be reused; the quic package will also not modify it.

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

A Conn is a QUIC connection.

Multiple goroutines may invoke methods on a Conn simultaneously.

func (*Conn) Abort added in v0.16.0

func (c *Conn) Abort(err error)

Abort closes the connection and returns immediately.

If err is nil, Abort sends a transport error of NO_ERROR to the peer. If err is an ApplicationError, Abort sends its error code and text. Otherwise, Abort sends a transport error of APPLICATION_ERROR with the error's text.

func (*Conn) AcceptStream added in v0.14.0

func (c *Conn) AcceptStream(ctx context.Context) (*Stream, error)

AcceptStream waits for and returns the next stream created by the peer.

func (*Conn) Close added in v0.16.0

func (c *Conn) Close() error

Close closes the connection.

Close is equivalent to:

conn.Abort(nil)
err := conn.Wait(context.Background())

func (*Conn) NewSendOnlyStream added in v0.14.0

func (c *Conn) NewSendOnlyStream(ctx context.Context) (*Stream, error)

NewSendOnlyStream creates a unidirectional, send-only stream.

If the peer's maximum stream limit for the connection has been reached, NewSendOnlyStream blocks until the limit is increased or the context expires.

func (*Conn) NewStream added in v0.14.0

func (c *Conn) NewStream(ctx context.Context) (*Stream, error)

NewStream creates a stream.

If the peer's maximum stream limit for the connection has been reached, NewStream blocks until the limit is increased or the context expires.

func (*Conn) String

func (c *Conn) String() string

func (*Conn) Wait added in v0.16.0

func (c *Conn) Wait(ctx context.Context) error

Wait waits for the peer to close the connection.

If the connection is closed locally and the peer does not close its end of the connection, Wait will return with a non-nil error after the drain period expires.

If the peer closes the connection with a NO_ERROR transport error, Wait returns nil. If the peer closes the connection with an application error, Wait returns an ApplicationError containing the peer's error code and reason. If the peer closes the connection with any other status, Wait returns a non-nil error.

type Endpoint added in v0.19.0

type Endpoint struct {
	// contains filtered or unexported fields
}

An Endpoint handles QUIC traffic on a network address. It can accept inbound connections or create outbound ones.

Multiple goroutines may invoke methods on an Endpoint simultaneously.

func Listen added in v0.16.0

func Listen(network, address string, config *Config) (*Endpoint, error)

Listen listens on a local network address. The configuration config must be non-nil.

func (*Endpoint) Accept added in v0.19.0

func (e *Endpoint) Accept(ctx context.Context) (*Conn, error)

Accept waits for and returns the next connection.

func (*Endpoint) Close added in v0.19.0

func (e *Endpoint) Close(ctx context.Context) error

Close closes the Endpoint. Any blocked operations on the Endpoint or associated Conns and Stream will be unblocked and return errors.

Close aborts every open connection. Data in stream read and write buffers is discarded. It waits for the peers of any open connection to acknowledge the connection has been closed.

func (*Endpoint) Dial added in v0.19.0

func (e *Endpoint) Dial(ctx context.Context, network, address string) (*Conn, error)

Dial creates and returns a connection to a network address.

func (*Endpoint) LocalAddr added in v0.19.0

func (e *Endpoint) LocalAddr() netip.AddrPort

LocalAddr returns the local network address.

type Stream added in v0.14.0

type Stream struct {
	// contains filtered or unexported fields
}

func (*Stream) Close added in v0.15.0

func (s *Stream) Close() error

Close closes the stream. See CloseContext for more details.

func (*Stream) CloseContext added in v0.15.0

func (s *Stream) CloseContext(ctx context.Context) error

CloseContext closes the stream. Any blocked stream operations will be unblocked and return errors.

CloseContext flushes any data in the stream write buffer and waits for the peer to acknowledge receipt of the data. If the stream has been reset, it waits for the peer to acknowledge the reset. If the context expires before the peer receives the stream's data, CloseContext discards the buffer and returns the context error.

func (*Stream) CloseRead added in v0.15.0

func (s *Stream) CloseRead()

CloseRead aborts reads on the stream. Any blocked reads will be unblocked and return errors.

CloseRead notifies the peer that the stream has been closed for reading. It does not wait for the peer to acknowledge the closure. Use CloseContext to wait for the peer's acknowledgement.

func (*Stream) CloseWrite added in v0.15.0

func (s *Stream) CloseWrite()

CloseWrite aborts writes on the stream. Any blocked writes will be unblocked and return errors.

CloseWrite sends any data in the stream write buffer to the peer. It does not wait for the peer to acknowledge receipt of the data. Use CloseContext to wait for the peer's acknowledgement.

func (*Stream) Flush added in v0.19.0

func (s *Stream) Flush()

Flush flushes data written to the stream. It does not wait for the peer to acknowledge receipt of the data. Use CloseContext to wait for the peer's acknowledgement.

func (*Stream) IsReadOnly added in v0.14.0

func (s *Stream) IsReadOnly() bool

IsReadOnly reports whether the stream is read-only (a unidirectional stream created by the peer).

func (*Stream) IsWriteOnly added in v0.14.0

func (s *Stream) IsWriteOnly() bool

IsWriteOnly reports whether the stream is write-only (a unidirectional stream created locally).

func (*Stream) Read added in v0.14.0

func (s *Stream) Read(b []byte) (n int, err error)

Read reads data from the stream. See ReadContext for more details.

func (*Stream) ReadContext added in v0.14.0

func (s *Stream) ReadContext(ctx context.Context, b []byte) (n int, err error)

ReadContext reads data from the stream.

ReadContext returns as soon as at least one byte of data is available.

If the peer closes the stream cleanly, ReadContext returns io.EOF after returning all data sent by the peer. If the peer aborts reads on the stream, ReadContext returns an error wrapping StreamResetCode.

func (*Stream) Reset added in v0.15.0

func (s *Stream) Reset(code uint64)

Reset aborts writes on the stream and notifies the peer that the stream was terminated abruptly. Any blocked writes will be unblocked and return errors.

Reset sends the application protocol error code, which must be less than 2^62, to the peer. It does not wait for the peer to acknowledge receipt of the error. Use CloseContext to wait for the peer's acknowledgement.

Reset does not affect reads. Use CloseRead to abort reads on the stream.

func (*Stream) Write added in v0.14.0

func (s *Stream) Write(b []byte) (n int, err error)

Write writes data to the stream. See WriteContext for more details.

func (*Stream) WriteContext added in v0.14.0

func (s *Stream) WriteContext(ctx context.Context, b []byte) (n int, err error)

WriteContext writes data to the stream.

WriteContext writes data to the stream write buffer. Buffered data is only sent when the buffer is sufficiently full. Call the Flush method to ensure buffered data is sent.

type StreamErrorCode added in v0.15.0

type StreamErrorCode uint64

A StreamErrorCode is an application protocol error code (RFC 9000, Section 20.2) indicating whay a stream is being closed.

func (StreamErrorCode) Error added in v0.15.0

func (e StreamErrorCode) Error() string

Directories

Path Synopsis
cmd
interop
The interop command is the client and server used by QUIC interoperability tests.
The interop command is the client and server used by QUIC interoperability tests.
Package qlog serializes qlog events.
Package qlog serializes qlog events.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL