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"
|
|
|
|
"net"
|
2022-07-25 03:08:42 +00:00
|
|
|
"net/netip"
|
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
|
|
|
|
|
|
|
type PacketConn interface {
|
|
|
|
net.PacketConn
|
|
|
|
WriteToUDPAddrPort([]byte, netip.AddrPort) (int, 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
|
|
|
|
}
|
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
|
|
|
|
}
|