Documentation
¶
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. Use ListenPacketContext when listener startup must be canceled by a caller-owned context.
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 ¶
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
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