nwpacket

package
v0.6.10 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Rendered for darwin/amd64

Overview

Package nwpacket provides a small Network.framework-backed net.PacketConn.

It is intentionally narrow: callers choose the local address, interface policy, and tracing hook. The package opens clear UDP Network.framework listeners and outbound connections, then exposes them through net.PacketConn for demos that need to plug into Go or Pion surfaces. Returned connections also implement PathReporter, which reports the observed Network.framework path for an established peer. Use ListenPacketContext when listener startup must be canceled by a caller-owned context. Config.ConnectRetries can recreate an outbound peer connection after a readiness timeout on transient links.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenPacket

func ListenPacket(config Config) (net.PacketConn, error)

ListenPacket creates a Network.framework-backed net.PacketConn.

func ListenPacketContext

func ListenPacketContext(ctx context.Context, config Config) (net.PacketConn, error)

ListenPacketContext creates a Network.framework-backed net.PacketConn.

The context bounds listener startup. After startup succeeds, use deadlines and Close to control packet I/O.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"net"

	"github.com/tmc/apple/x/network/nwpacket"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	_, err := nwpacket.ListenPacketContext(ctx, nwpacket.Config{
		LocalAddr: &net.UDPAddr{IP: net.ParseIP("127.0.0.1")},
	})
	fmt.Println(errors.Is(err, context.Canceled))
}
Output:
true

Types

type Config

type Config struct {
	// InterfaceName is used for link-local zones and optional exact interface
	// selection.
	InterfaceName string

	// LocalAddr is the local UDP address to bind. Port 0 asks Network.framework
	// to choose a port.
	LocalAddr *net.UDPAddr

	RequiredInterfaceType applenetwork.NWInterfaceType
	SetRequiredInterface  bool
	IncludePeerToPeer     bool
	RequireInterface      bool
	ReuseLocalAddress     bool

	// ConnectTimeout bounds outbound connection readiness before retry. It also
	// bounds send completion when the caller has not set a write deadline. Zero
	// means five seconds.
	ConnectTimeout time.Duration

	// ConnectRetries is the number of extra outbound connection attempts after
	// a readiness timeout. Retries cancel and recreate only outbound peers.
	ConnectRetries int

	QueueLabel string
	Tracef     func(format string, args ...any)
}

Config describes a Network.framework UDP packet connection.

Example
package main

import (
	"fmt"
	"net"

	applenetwork "github.com/tmc/apple/network"
	"github.com/tmc/apple/x/network/nwpacket"
)

func main() {
	cfg := nwpacket.Config{
		InterfaceName:         "awdl0",
		LocalAddr:             &net.UDPAddr{IP: net.ParseIP("fe80::1"), Zone: "awdl0"},
		RequiredInterfaceType: applenetwork.NWInterfaceTypeWifi,
		SetRequiredInterface:  true,
		IncludePeerToPeer:     true,
		RequireInterface:      true,
		ReuseLocalAddress:     true,
	}
	fmt.Println(cfg.InterfaceName, cfg.IncludePeerToPeer)
}
Output:
awdl0 true

type Path added in v0.6.6

type Path struct {
	Status     applenetwork.NWPathStatus
	Interfaces []PathInterface
}

Path describes the Network.framework path observed for a peer connection.

func (Path) InterfaceNames added in v0.6.6

func (p Path) InterfaceNames() []string

InterfaceNames returns the path interface names in Network.framework order.

Example
package main

import (
	"fmt"

	applenetwork "github.com/tmc/apple/network"
	"github.com/tmc/apple/x/network/nwpacket"
)

func main() {
	path := nwpacket.Path{
		Status: applenetwork.NWPathStatusSatisfied,
		Interfaces: []nwpacket.PathInterface{
			{Name: "awdl0", Index: 16, Type: applenetwork.NWInterfaceTypeWifi},
			{Name: "en0", Index: 6, Type: applenetwork.NWInterfaceTypeWifi},
		},
	}
	fmt.Println(path.InterfaceNames())
}
Output:
[awdl0 en0]

func (Path) String added in v0.6.6

func (p Path) String() string
Example
package main

import (
	"fmt"

	applenetwork "github.com/tmc/apple/network"
	"github.com/tmc/apple/x/network/nwpacket"
)

func main() {
	path := nwpacket.Path{
		Status: applenetwork.NWPathStatusSatisfied,
		Interfaces: []nwpacket.PathInterface{
			{Name: "awdl0", Index: 16, Type: applenetwork.NWInterfaceTypeWifi},
		},
	}
	fmt.Println(path)
}
Output:
status=NWPathStatusSatisfied interfaces=awdl0/NWInterfaceTypeWifi(16)

func (Path) UsesInterface added in v0.6.6

func (p Path) UsesInterface(name string) bool

UsesInterface reports whether p includes name.

Example
package main

import (
	"fmt"

	applenetwork "github.com/tmc/apple/network"
	"github.com/tmc/apple/x/network/nwpacket"
)

func main() {
	path := nwpacket.Path{
		Status: applenetwork.NWPathStatusSatisfied,
		Interfaces: []nwpacket.PathInterface{
			{Name: "awdl0", Index: 16, Type: applenetwork.NWInterfaceTypeWifi},
		},
	}
	fmt.Println(path.UsesInterface("awdl0"))
}
Output:
true

type PathInterface added in v0.6.6

type PathInterface struct {
	Name  string
	Index uint32
	Type  applenetwork.NWInterfaceType
}

PathInterface describes one interface in a Network.framework path.

func (PathInterface) String added in v0.6.6

func (i PathInterface) String() string

type PathReporter added in v0.6.6

type PathReporter interface {
	PeerPath(net.Addr) (Path, error)
}

PathReporter reports the Network.framework path for a peer connection.

Jump to

Keyboard shortcuts

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