Documentation ¶
Index ¶
- Constants
- Variables
- func DialContext(ctx context.Context, network, address string, opts ...func(*Options)) (net.Conn, error)
- func DialContextWithOptions(ctx context.Context, network, address string, opts *Options) (net.Conn, error)
- func DialHappyEyeballs(ctx context.Context, ips []*net.TCPAddr) (net.Conn, error)
- func DialHappyEyeballsv1(ctx context.Context, addr netapi.Address) (net.Conn, error)
- func DialHappyEyeballsv2(ctx context.Context, addr netapi.Address) (net.Conn, error)
- func DialParallel(ctx context.Context, primaries []*net.TCPAddr, fallbacks []*net.TCPAddr) (net.Conn, error)
- func DialSerial(ctx context.Context, ras []*net.TCPAddr) (net.Conn, error)
- func GetUnicastAddr(ipv6 bool, network string, name string, index int) net.Addr
- func Interleave[S ~[]T, T any](a, b S) S
- func LinuxMarkSymbol(fd int32, mark int) error
- func ListenContext(ctx context.Context, network string, address string) (net.Listener, error)
- func ListenContextWithOptions(ctx context.Context, network string, address string, opts *Options) (net.Listener, error)
- func ListenPacket(network, address string, opts ...func(*Options)) (net.PacketConn, error)
- func ListenPacketWithOptions(network, address string, opts *Options) (net.PacketConn, error)
- func LookupIP(ctx context.Context, addr netapi.Address) ([]net.IP, error)
- func MergeDnsError(err1, err2 error) error
- func PartialDeadline(now, deadline time.Time, addrsRemaining int) (time.Time, error)
- func PartialDeadlineCtx(ctx context.Context, addrsRemaining int) (context.Context, context.CancelFunc, error)
- func ResolveTCPAddr(ctx context.Context, addr netapi.Address) (*net.TCPAddr, error)
- func ResolveUDPAddr(ctx context.Context, addr netapi.Address) (*net.UDPAddr, error)
- func ResolverAddrPort(ctx context.Context, addr netapi.Address) (netip.AddrPort, error)
- func ResolverIP(ctx context.Context, addr netapi.Address) (net.IP, error)
- func WithListener() func(*Options)
- func WithTryUpgradeToBatch() func(*Options)
- type Avg
- type BatchPacketConn
- type HappyEyeballsv2Cache
- type HappyEyeballsv2Dialer
- type Options
- type SystemResolver
Constants ¶
const SocketBufferSize = 7 << 20
UDP socket read/write buffer size (7MB). The value of 7MB is chosen as it is the max supported by a default configuration of macOS. Some platforms will silently clamp the value to other maximums, such as linux clamping to net.core.{r,w}mem_max (see _linux.go for additional implementation that works around this limitation)
Variables ¶
var ( DefaultInterfaceName = "" DefaultInterfaceIndex = 0 DefaultRoutingMark = 0 // maybe need root permission DefaultMarkSymbol func(socket int32) bool )
var Bootstrap netapi.Resolver = InternetResolver
var DefaultHappyEyeballsv2Dialer = &HappyEyeballsv2Dialer[net.Conn]{ DialContext: func(ctx context.Context, ip net.IP, port uint16) (net.Conn, error) { return DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), strconv.Itoa(int(port))), func(opts *Options) { if DefaultIPv6PreferUnicastLocalAddr && opts.InterfaceName != "" || opts.InterfaceIndex != 0 { if ip.IsGlobalUnicast() && !ip.IsPrivate() && ip.To4() == nil && ip.To16() != nil { opts.LocalAddr = GetUnicastAddr(true, "tcp", opts.InterfaceName, opts.InterfaceIndex) log.Info("happy eyeballs dialer prefer ipv6", slog.Any("localaddr", opts.LocalAddr)) } } }) }, Cache: happyEyeballsCache, Avg: NewAvg(), }
var DefaultIPv6PreferUnicastLocalAddr = false
var InternetResolver netapi.Resolver = NewSystemResolver( netapi.ParseAddressPort("udp", "8.8.8.8", 53), netapi.ParseAddressPort("udp", "1.1.1.1", 53), netapi.ParseAddressPort("udp", "223.5.5.5", 53), netapi.ParseAddressPort("udp", "114.114.114.114", 53), )
var KeepAliveConfig = net.KeepAliveConfig{ Enable: true, Idle: time.Second * 300, Interval: time.Second * 20, Count: 9, }
Functions ¶
func DialContext ¶
func DialContextWithOptions ¶
func DialHappyEyeballs ¶ added in v0.3.5
DialHappyEyeballs is a function that implements Happy Eyeballs algorithm for IPv4 and IPv6 addresses. It divides given TCP addresses into primaries and fallbacks and then calls DialParallel function.
It takes a context and a slice of TCP addresses as input and returns a net.Conn and an error.
func DialHappyEyeballsv1 ¶ added in v0.3.7
func DialHappyEyeballsv2 ¶ added in v0.3.7
DialHappyEyeballsv2 impl rfc 8305
https://datatracker.ietf.org/doc/html/rfc8305 modified from https://github.com/tailscale/tailscale/blob/ee976ad704980e20ec36c6aaaad0a2ce5b30b3d5/net/dnscache/dnscache.go#L577
func DialParallel ¶ added in v0.3.5
func DialParallel(ctx context.Context, primaries []*net.TCPAddr, fallbacks []*net.TCPAddr) (net.Conn, error)
https://github.com/golang/go/blob/315b6ae682a2a4e7718924a45b8b311a0fe10043/src/net/dial.go#L534
dialParallel races two copies of dialSerial, giving the first a head start. It returns the first established connection and closes the others. Otherwise it returns an error from the first primary address.
func DialSerial ¶ added in v0.3.5
DialSerial connects to a list of addresses in sequence, returning either the first successful connection, or the first error.
func GetUnicastAddr ¶ added in v0.3.7
func Interleave ¶ added in v0.3.7
func Interleave[S ~[]T, T any](a, b S) S
Interleave combines two slices of the form [a, b, c] and [x, y, z] into a slice with elements interleaved; i.e. [a, x, b, y, c, z].
func LinuxMarkSymbol ¶
func ListenContext ¶
func ListenPacket ¶
func ListenPacket(network, address string, opts ...func(*Options)) (net.PacketConn, error)
func ListenPacketWithOptions ¶
func ListenPacketWithOptions(network, address string, opts *Options) (net.PacketConn, error)
func MergeDnsError ¶ added in v0.3.7
func PartialDeadline ¶ added in v0.3.5
PartialDeadline returns the deadline to use for a single address, when multiple addresses are pending.
func PartialDeadlineCtx ¶ added in v0.3.5
func ResolveTCPAddr ¶ added in v0.3.7
func ResolveUDPAddr ¶ added in v0.3.7
func ResolverAddrPort ¶ added in v0.3.7
func ResolverIP ¶ added in v0.3.7
func WithListener ¶ added in v0.3.6
func WithListener() func(*Options)
func WithTryUpgradeToBatch ¶ added in v0.3.7
func WithTryUpgradeToBatch() func(*Options)
Types ¶
type BatchPacketConn ¶ added in v0.3.7
func NewBatchPacketConn ¶ added in v0.3.7
func NewBatchPacketConn(pc *net.UDPConn) *BatchPacketConn
func (*BatchPacketConn) Close ¶ added in v0.3.7
func (bc *BatchPacketConn) Close() error
type HappyEyeballsv2Cache ¶ added in v0.3.7
type HappyEyeballsv2Dialer ¶ added in v0.3.7
type HappyEyeballsv2Dialer[T net.Conn] struct { DialContext func(ctx context.Context, ip net.IP, port uint16) (T, error) Cache HappyEyeballsv2Cache Avg *Avg }
func (*HappyEyeballsv2Dialer[T]) DialHappyEyeballsv2 ¶ added in v0.3.7
type Options ¶
type Options struct { // RoutingMark is the mark for each packet sent through this // socket. Changing the mark can be used for mark-based routing // without netfilter or for packet filtering. MarkSymbol func(socket int32) bool // InterfaceName is the name of interface/device to bind. // If a socket is bound to an interface, only packets received // from that particular interface are processed by the socket. InterfaceName string // InterfaceIndex is the index of interface/device to bind. // It is almost the same as InterfaceName except it uses the // index of the interface instead of the name. InterfaceIndex int LocalAddr net.Addr // contains filtered or unexported fields }
type SystemResolver ¶ added in v0.3.7
type SystemResolver struct {
// contains filtered or unexported fields
}
func NewSystemResolver ¶ added in v0.3.7
func NewSystemResolver(host ...netapi.Address) *SystemResolver
func (*SystemResolver) Close ¶ added in v0.3.7
func (d *SystemResolver) Close() error
func (*SystemResolver) LookupIP ¶ added in v0.3.7
func (d *SystemResolver) LookupIP(ctx context.Context, domain string, opts ...func(*netapi.LookupIPOption)) ([]net.IP, error)
func (*SystemResolver) Raw ¶ added in v0.3.7
func (d *SystemResolver) Raw(context.Context, dnsmessage.Question) (dnsmessage.Message, error)