2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-07-10 21:26:04 +00:00
|
|
|
|
|
|
|
// Package nettype defines an interface that doesn't exist in the Go net package.
|
|
|
|
package nettype
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-15 14:43:30 +00:00
|
|
|
"io"
|
2020-07-10 21:26:04 +00:00
|
|
|
"net"
|
2022-07-25 03:08:42 +00:00
|
|
|
"net/netip"
|
2023-04-15 14:43:30 +00:00
|
|
|
"time"
|
2020-07-10 21:26:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
type PacketListenerWithNetIP interface {
|
|
|
|
ListenPacket(ctx context.Context, network, address string) (PacketConn, error)
|
|
|
|
}
|
|
|
|
|
2020-07-10 21:26:04 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
|
2023-04-15 20:08:16 +00:00
|
|
|
// PacketConn is like a net.PacketConn but uses the newer netip.AddrPort
|
|
|
|
// write/read methods.
|
2022-07-25 03:08:42 +00:00
|
|
|
type PacketConn interface {
|
|
|
|
WriteToUDPAddrPort([]byte, netip.AddrPort) (int, error)
|
2023-04-15 20:08:16 +00:00
|
|
|
ReadFromUDPAddrPort([]byte) (int, netip.AddrPort, error)
|
2023-04-15 14:43:30 +00:00
|
|
|
io.Closer
|
|
|
|
LocalAddr() net.Addr
|
|
|
|
SetDeadline(time.Time) error
|
|
|
|
SetReadDeadline(time.Time) error
|
|
|
|
SetWriteDeadline(time.Time) error
|
2022-07-25 03:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-03-05 16:50:20 +00:00
|
|
|
|
|
|
|
// ConnPacketConn is the interface that's a superset of net.Conn and net.PacketConn.
|
|
|
|
type ConnPacketConn interface {
|
|
|
|
net.Conn
|
|
|
|
net.PacketConn
|
|
|
|
}
|