util

package
v0.14.3 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package util provides common utility functions used by multiple portions of wirelink, primarily providing wrappers and helpers for core go libraries and constructs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneBytes added in v0.6.1

func CloneBytes(data []byte) []byte

CloneBytes returns a new copy of the input data

func CloneIPNet added in v0.10.0

func CloneIPNet(ipn net.IPNet) net.IPNet

CloneIPNet makes a deep copy of the given value

func CloneUDPAddr added in v0.10.0

func CloneUDPAddr(addr *net.UDPAddr) *net.UDPAddr

CloneUDPAddr makes a deep copy of the given address

func DecodeFrom added in v0.6.0

func DecodeFrom(value encoding.BinaryUnmarshaler, readLen int, reader io.Reader) error

DecodeFrom provides an equivalent function to Decodable.DecodeFrom, but for types that implement BinaryUnmarshaler and which have a fixed known length, e.g. to provide a default implementation for Decodable for such types

func IPToBytes added in v0.1.0

func IPToBytes(ip net.IP) (ret [net.IPv6len]byte)

IPToBytes returns the given IP normalized to a 16 byte array, suitable for use as a map key among other things

func IsGloballyRoutable added in v0.12.2

func IsGloballyRoutable(ip net.IP) bool

IsGloballyRoutable checks if an IP address looks routable across the internet or not. It will return false for any IP that is not a Global Unicast address, and also for certain special reserved subnets that are used within site-level domains but are not meant to be routed on the internet.

func IsIPv6LLMatch

func IsIPv6LLMatch(expected net.IP, actual *net.IPNet, local bool) bool

IsIPv6LLMatch checks if a given expected IPv6 address matches an actual address + mask, checking if the mask is of the expected form. The mask is expected to be /128 if local is false, or /64 if it is true

func MustByte added in v0.6.0

func MustByte(value byte, err error) byte

MustByte is a helper, esp. for ByteReader, that takes a tuple of a byte and maybe an error and panics if there is an error, or else returns the byte

func MustBytes added in v0.6.0

func MustBytes(value []byte, err error) []byte

MustBytes is a helper, esp. for BinaryMarshaller, that takes a tuple of a byte slice and maybe an error and panics if there is an error, or else returns the byte slice

func MustInt64 added in v0.6.0

func MustInt64(value int64, err error) int64

MustInt64 is a helper, esp. for encoding.binary, that takes a tuple of an int64 and maybe an error and panics if there is an error, or else returns the int64

func NormalizeIP

func NormalizeIP(ip net.IP) net.IP

NormalizeIP returns a version of the given ip normalized to its underlying family, instead of the "always in IPv6 container" format that is often used, so IPv4 values will have a length of 4 and IPv6 ones a length of 16

func SortIPNetSlice added in v0.9.0

func SortIPNetSlice(slice []net.IPNet) []net.IPNet

SortIPNetSlice sorts a slice of IPNets by their string value, returning the (modified in place) slice. OMG want generics.

func Ternary added in v0.11.1

func Ternary(value bool, trueResult, falseResult interface{}) interface{}

Ternary turns a trivial if/else into a function call

func TimeMax added in v0.1.2

func TimeMax() time.Time

TimeMax is the maximum representable time in go. see: https://stackoverflow.com/a/32620397/7649 see also `time.go` in the runtime

func UDPEqualIPPort added in v0.9.0

func UDPEqualIPPort(a, b *net.UDPAddr) bool

UDPEqualIPPort checks if to UDPAddrs are equal in terms of their IP and Port fields, but ignoring any Zone value

func WrapOrNewf added in v0.6.0

func WrapOrNewf(err error, format string, args ...interface{}) error

WrapOrNewf calls fmt.Errorf with varying format depending on whether err is nil, always returning an error value

Types

type ByteReader added in v0.12.0

type ByteReader interface {
	io.Reader
	io.ByteReader
}

ByteReader combines io.Reader and io.ByteReader

type Decodable added in v0.6.0

type Decodable interface {
	// DecodeFrom reads just enough bytes from the reader to deserialize itself
	DecodeFrom(lengthHint int, reader io.Reader) error
}

Decodable is an interface that mimics BinaryUnmarshaller, but sources from an io.Reader instead of a slice

Jump to

Keyboard shortcuts

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