tailscale/types/nettype/nettype.go
Brad Fitzpatrick 29f7df9d8f wgengine/magicsock, etc: remove mostly unused WriteTo methods
Updates #2331
Updates #5162

Change-Id: I8291884425481eeaedde38a54adfd8ed7292a497
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-04-15 08:32:11 -07:00

66 lines
1.8 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// Package nettype defines an interface that doesn't exist in the Go net package.
package nettype
import (
"context"
"io"
"net"
"net/netip"
"time"
)
// PacketListener defines the ListenPacket method as implemented
// by net.ListenConfig, net.ListenPacket, and tstest/natlab.
type PacketListener interface {
ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error)
}
type PacketListenerWithNetIP interface {
ListenPacket(ctx context.Context, network, address string) (PacketConn, error)
}
// Std implements PacketListener using the Go net package's ListenPacket func.
type Std struct{}
func (Std) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) {
var conf net.ListenConfig
return conf.ListenPacket(ctx, network, address)
}
// PacketConn is a net.PacketConn that's about halfway (as of 2023-04-15)
// converted to use netip.AddrPort.
type PacketConn interface {
WriteToUDPAddrPort([]byte, netip.AddrPort) (int, error)
ReadFrom(p []byte) (int, net.Addr, error)
io.Closer
LocalAddr() net.Addr
SetDeadline(time.Time) error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
}
func MakePacketListenerWithNetIP(ln PacketListener) PacketListenerWithNetIP {
return packetListenerAdapter{ln}
}
type packetListenerAdapter struct {
PacketListener
}
func (a packetListenerAdapter) ListenPacket(ctx context.Context, network, address string) (PacketConn, error) {
pc, err := a.PacketListener.ListenPacket(ctx, network, address)
if err != nil {
return nil, err
}
return pc.(PacketConn), nil
}
// ConnPacketConn is the interface that's a superset of net.Conn and net.PacketConn.
type ConnPacketConn interface {
net.Conn
net.PacketConn
}