util

package
v1.15.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2023 License: GPL-3.0 Imports: 12 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllIPAddr

func AllIPAddr() string

AllIPAddr returns a catch-all IP address to bind. If the machine supports IP dual stack, "::" is returned. Otherwise "0.0.0.0" is returned.

func BidiCopy

func BidiCopy(conn1, conn2 io.ReadWriteCloser, isClient bool) error

BidiCopy does bi-directional data copy.

func IsIPDualStack

func IsIPDualStack() bool

IsIPDualStack returns true if an IPv6 socket is able to send and receive both IPv4 and IPv6 packets.

This function only supports Linux. It always returns false if running other operating systems.

func IsNilNetAddr

func IsNilNetAddr(addr net.Addr) bool

IsNilNetAddr returns true if the net.Addr is nil / empty.

func IsZeroTime

func IsZeroTime(t time.Time) bool

IsZeroTime returns true if the input time is UNIX epoch.

func LocalIPAddr

func LocalIPAddr() string

LocalIPAddr returns the localhost IP address.

func MaybeDecorateIPv6

func MaybeDecorateIPv6(addr string) string

MaybeDecorateIPv6 adds [ and ] before and after an IPv6 address. If the input string is a IPv4 address or not a valid IP address (e.g. is a domain), the same string is returned.

func NilNetAddr

func NilNetAddr() net.Addr

NilNetAddr returns an empty network address.

func ReuseAddrPort

func ReuseAddrPort(network, address string, conn syscall.RawConn) error

ReuseAddrPort sets SO_REUSEADDR and SO_REUSEPORT options to a given connection.

func SendReceive

func SendReceive(ctx context.Context, conn net.Conn, req []byte) (resp []byte, err error)

SendReceive sends a request to the connection and returns the response. The maxinum size of response is 4096 bytes.

func UnusedTCPPort

func UnusedTCPPort() (int, error)

UnusedTCPPort returns an unused TCP port.

func UnusedUDPPort

func UnusedUDPPort() (int, error)

UnusedUDPPort returns an unused UDP port.

func WaitForClose

func WaitForClose(conn net.Conn)

WaitForClose blocks the go routine. It returns when the peer closes the connection. In the meanwhile, everything send by the peer is discarded.

func ZeroTime

func ZeroTime() time.Time

ZeroTime returns the time of UNIX epoch.

Types

type ConnHandler

type ConnHandler interface {
	// Take grabs the ownership of the network connection.
	// The function returns when the handler returns the ownership.
	Take(net.Conn) (closed bool, err error)
}

ConnHandler defines a generic handler that can take the ownership of a network connection.

type DNSPolicy

type DNSPolicy uint8
const (
	DNSPolicyDefault DNSPolicy = iota
	DNSPolicyIPv4Only
	DNSPolicyIPv6Only
)

func (DNSPolicy) String

func (p DNSPolicy) String() string

type DNSResolver

type DNSResolver struct {
	DNSPolicy DNSPolicy
}

DNSResolver uses Golang's default DNS implementation to resolve host names.

func (*DNSResolver) LookupIP

func (d *DNSResolver) LookupIP(ctx context.Context, host string) (net.IP, error)

LookupIP looks up host for the given network using the DNS resolver.

type HierarchyConn

type HierarchyConn interface {
	net.Conn

	AddSubConnection(conn net.Conn)
}

HierarchyConn closes sub-connections when this connection is closed.

func WrapHierarchyConn

func WrapHierarchyConn(conn net.Conn) HierarchyConn

WrapHierarchyConn wraps an existing connection with HierarchyConn.

type IPVersion

type IPVersion uint8
const (
	IPVersionUnknown IPVersion = iota
	IPVersion4
	IPVersion6
)

func GetIPVersion

func GetIPVersion(addr string) IPVersion

GetIPVersion returns the IP version of the given network address.

func (IPVersion) String

func (v IPVersion) String() string

type NetAddr

type NetAddr struct {
	Net string
	Str string
}

NetAddr implements net.Addr interface.

func (NetAddr) Network

func (a NetAddr) Network() string

func (NetAddr) String

func (a NetAddr) String() string

type TransportProtocol

type TransportProtocol uint8
const (
	UnknownTransport TransportProtocol = iota
	UDPTransport
	TCPTransport
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL