nwquic

package
v0.6.21 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 27 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Rendered for darwin/amd64

Overview

Package nwquic wraps Apple's Network.framework QUIC transport for headless pre-shared-key (PSK) authenticated multiplexed streams and datagrams.

Connections authenticate mutually using an ephemeral in-memory ECDSA P-256 TLS 1.3 identity whose certificate embeds an HMAC-SHA256 proof over the shared Config.PSK, Config.Identity, and public key, verified during the handshake without touching any macOS Keychain or triggering GUI prompts. Each Conn wraps an network.NWConnectionGroup backed by a QUIC multiplex descriptor and supports concurrent bidirectional Stream channels as well as RFC 9221 QUIC datagrams.

Basic usage:

cfg := nwquic.Config{
	ALPN:            "demo",
	PSK:             []byte("0123456789abcdef0123456789abcdef"),
	Identity:        []byte("node-a"),
	EnableDatagrams: true,
}
ln, err := nwquic.Listen("127.0.0.1:0", cfg)
if err != nil {
	log.Fatal(err)
}
defer ln.Close()
Example
package main

import (
	"context"
	"fmt"
	"io"
	"time"

	"github.com/tmc/apple/x/nwquic"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	cfg := nwquic.Config{
		ALPN:     "nwquic-example",
		PSK:      []byte("0123456789abcdef0123456789abcdef"),
		Identity: []byte("example-peer"),
	}

	ln, err := nwquic.ListenContext(ctx, "127.0.0.1:0", cfg)
	if err != nil {
		panic(err)
	}
	defer ln.Close()

	go func() {
		conn, err := ln.Accept(ctx)
		if err != nil {
			return
		}
		defer conn.Close()
		s, err := conn.AcceptStream(ctx)
		if err != nil {
			return
		}
		defer s.Close()
		msg, _ := io.ReadAll(s)
		_, _ = s.Write(msg)
		_ = s.CloseWrite()
	}()

	conn, err := nwquic.Dial(ctx, ln.Addr().String(), cfg)
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	s, err := conn.OpenStream(ctx)
	if err != nil {
		panic(err)
	}
	defer s.Close()

	_, _ = s.Write([]byte("hello quic"))
	_ = s.CloseWrite()
	reply, _ := io.ReadAll(s)
	fmt.Println(string(reply))
}
Output:
hello quic

Index

Examples

Constants

View Source
const (
	// DefaultALPN is the application-layer protocol identifier used when
	// Config.ALPN is empty.
	DefaultALPN = "nwquic"
)

Variables

View Source
var ErrClosed = errors.New("nwquic: closed")

ErrClosed is returned when operating on a closed listener, connection, or stream.

View Source
var ErrDatagramsDisabled = errors.New("nwquic: datagrams disabled")

ErrDatagramsDisabled is returned when sending or receiving datagrams on a connection whose Config did not enable datagrams.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ALPN is the TLS application-layer protocol negotiation string.
	// If empty, DefaultALPN is used.
	ALPN string

	// PSK is the pre-shared key bytes shared between client and server.
	PSK []byte

	// Identity is the pre-shared key identity bytes.
	Identity []byte

	// MaxBidiStreams sets the initial maximum number of concurrent
	// bidirectional QUIC streams. Zero defaults to 100.
	MaxBidiStreams uint64

	// EnableDatagrams enables RFC 9221 QUIC datagram frames.
	EnableDatagrams bool
}

Config configures a headless PSK-authenticated QUIC listener or client.

Both PSK and Identity must be non-empty. The zero value is not usable.

type Conn

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

Conn represents a multiplexed QUIC connection backed by an network.NWConnectionGroup.

func Dial

func Dial(ctx context.Context, addr string, cfg Config) (*Conn, error)

Dial establishes a multiplexed QUIC connection to addr using cfg.

func (*Conn) AcceptStream

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

AcceptStream waits for and returns the next peer-initiated QUIC stream.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection group and all associated streams and datagram flows.

func (*Conn) OpenStream

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

OpenStream opens a new bidirectional QUIC stream on the connection.

func (*Conn) ReceiveDatagram

func (c *Conn) ReceiveDatagram(ctx context.Context) ([]byte, error)

ReceiveDatagram waits for the next inbound QUIC DATAGRAM frame.

func (*Conn) SendDatagram

func (c *Conn) SendDatagram(ctx context.Context, data []byte) error

SendDatagram sends an unreliable QUIC DATAGRAM frame on the connection.

func (*Conn) TransferReport

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

TransferReport collects and aggregates NWDataTransferReport metrics across the connection's streams.

type Listener

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

Listener accepts incoming multiplexed QUIC connections.

func Listen

func Listen(addr string, cfg Config) (*Listener, error)

Listen starts a QUIC listener bound to addr (for example, "127.0.0.1:0").

func ListenContext

func ListenContext(ctx context.Context, addr string, cfg Config) (*Listener, error)

ListenContext starts a QUIC listener bound to addr, respecting ctx for startup.

func (*Listener) Accept

func (l *Listener) Accept(ctx context.Context) (*Conn, error)

Accept waits for and returns the next incoming QUIC connection.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the listener's local UDP address, including the assigned port when bound with port 0.

func (*Listener) Close

func (l *Listener) Close() error

Close stops the listener and rejects future Accept calls.

type Stream

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

Stream is a bidirectional QUIC stream multiplexed over a Conn.

func (*Stream) Close

func (s *Stream) Close() error

Close cancels the stream and releases its underlying Network.framework resources.

func (*Stream) CloseWrite

func (s *Stream) CloseWrite() error

CloseWrite sends a QUIC stream FIN so the peer observes io.EOF on Read while allowing this side to continue reading until the peer also finishes.

func (*Stream) Read

func (s *Stream) Read(p []byte) (int, error)

Read reads data from the QUIC stream, implementing io.Reader.

func (*Stream) StreamID

func (s *Stream) StreamID() uint64

StreamID returns the QUIC stream identifier reported by NWQuicGetStreamID.

func (*Stream) TransferReport

func (s *Stream) TransferReport(ctx context.Context) (TransferReport, error)

TransferReport collects transport statistics for this stream via network.NWDataTransferReportCollect.

func (*Stream) Write

func (s *Stream) Write(p []byte) (int, error)

Write writes data to the QUIC stream, implementing io.Writer.

type TransferReport

type TransferReport struct {
	Duration           time.Duration
	SentAppBytes       uint64
	ReceivedAppBytes   uint64
	SentWireBytes      uint64
	ReceivedWireBytes  uint64
	RetransmittedBytes uint64
	MinRTT             time.Duration
	SmoothedRTT        time.Duration
	RTTVariance        time.Duration
	PathCount          uint32
}

TransferReport holds transport statistics collected from Network.framework's NWDataTransferReport.

Jump to

Keyboard shortcuts

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