net/rioconn: add PacketListener

In this commit, add PacketListener, a nettype.PacketListener whose
ListenPacket method returns a RIO-based UDPConn.

Updates tailscale/corp#8610

Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl 2026-02-19 08:22:57 -06:00
parent e5cb1f48a6
commit e22a9f5909
No known key found for this signature in database

View File

@ -0,0 +1,38 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build windows
package rioconn
import (
"context"
"net"
"tailscale.com/types/nettype"
)
// NewPacketListener returns a packet listener that uses Registered Input/Output (RIO)
// API Extensions, if available, to provide high-performance UDP networking on Windows.
// The specified options are applied to all connections created by the listener.
// If RIO is not available, it returns an [ErrRIOUnavailable].
func NewPacketListener(options ...UDPOption) (nettype.PacketListener, error) {
if err := Initialize(); err != nil {
return nil, err
}
return &PacketListener{options: options}, nil
}
// PacketListener is a [nettype.PacketListener] that uses RIO.
type PacketListener struct {
options []UDPOption
}
// ListenPacket implements [nettype.PacketListener.ListenPacket].
func (pl *PacketListener) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) {
addr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, &net.OpError{Op: "listen", Net: network, Err: err}
}
return ListenUDP(network, addr, pl.options...)
}