netutil

package
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2021 License: Unlicense Imports: 8 Imported by: 38

Documentation

Overview

Package netutil contains common utilities for IP, MAC, and other kinds of network addresses.

TODO(a.garipov): Add more examples.

TODO(a.garipov): Add HostPort and IPPort structs with decoding and encoding, fmt.Srtinger implementations, etc.

Index

Examples

Constants

View Source
const (
	// ErrNotAReversedIP is the underlying error returned from validation
	// functions when a domain name is not a full reversed IP address.
	ErrNotAReversedIP errors.Error = "not a full reversed ip address"
)
View Source
const MaxDomainLabelLen = 63

MaxDomainLabelLen is the maximum allowed length of a domain name label according to RFC 1035.

View Source
const MaxDomainNameLen = 253

MaxDomainNameLen is the maximum allowed length of a full domain name according to RFC 1035.

See also: https://stackoverflow.com/a/32294443/1892060.

Variables

This section is empty.

Functions

func CloneIP

func CloneIP(ip net.IP) (clone net.IP)

CloneIP returns a clone of an IP address that doesn't share the same underlying array with it.

func CloneIPs

func CloneIPs(ips []net.IP) (clone []net.IP)

CloneIPs returns a deep clone of ips.

func CloneMAC

func CloneMAC(mac net.HardwareAddr) (clone net.HardwareAddr)

CloneMAC returns a clone of a MAC address.

func CloneURL

func CloneURL(u *url.URL) (clone *url.URL)

CloneURL returns a deep clone of u. The User pointer of clone is the same, since a *url.Userinfo is effectively an immutable value.

func IPFromReversedAddr

func IPFromReversedAddr(arpa string) (ip net.IP, err error)

IPFromReversedAddr tries to convert a full reversed ARPA address to a normal IP address. arpa can be domain name or an FQDN.

Any error returned will have the underlying type of *BadDomainError.

func IPPortFromAddr

func IPPortFromAddr(addr net.Addr) (ip net.IP, port int)

IPPortFromAddr returns the IP address and the port from addr. If addr is neither a *net.TCPAddr nor a *net.UDPAddr, it returns nil and 0.

func IsValidHostInnerRune

func IsValidHostInnerRune(r rune) (ok bool)

IsValidHostInnerRune returns true if r is a valid inner—that is, neither initial nor final—rune for a hostname label.

func IsValidHostOuterRune

func IsValidHostOuterRune(r rune) (ok bool)

IsValidHostOuterRune returns true if r is a valid initial or final rune for a hostname label.

func JoinHostPort

func JoinHostPort(host string, port int) (hostport string)

JoinHostPort is a convinient wrapper for net.JoinHostPort with port of type int.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/netutil"
)

func main() {
	fmt.Println(netutil.JoinHostPort("example.com", 12345))

}
Output:


example.com:12345

func ParseIP

func ParseIP(s string) (ip net.IP, err error)

ParseIP is a wrapper around net.ParseIP that returns a useful error.

Any error returned will have the underlying type of *BadIPError.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/netutil"
)

func main() {
	ip, err := netutil.ParseIP("1.2.3.4")
	fmt.Println(ip, err)

	ip, err = netutil.ParseIP("1234::cdef")
	fmt.Println(ip, err)

	ip, err = netutil.ParseIP("!!!")
	fmt.Println(ip, err)

}
Output:


1.2.3.4 <nil>
1234::cdef <nil>
<nil> bad ip address "!!!"

func ParseIPv4

func ParseIPv4(s string) (ip net.IP, err error)

ParseIPv4 is a wrapper around net.ParseIP that makes sure that the parsed IP is an IPv4 address and returns a useful error.

Any error returned will have the underlying type of either *BadIPError or *BadIPv4Error,

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/netutil"
)

func main() {
	ip, err := netutil.ParseIPv4("1.2.3.4")
	fmt.Println(ip, err)

	ip, err = netutil.ParseIPv4("1234::cdef")
	fmt.Println(ip, err)

	ip, err = netutil.ParseIPv4("!!!")
	fmt.Println(ip, err)

}
Output:


1.2.3.4 <nil>
<nil> bad ipv4 address "1234::cdef"
<nil> bad ip address "!!!"

func SplitHost

func SplitHost(hostport string) (host string, err error)

SplitHost is a wrapper for net.SplitHostPort for cases when the hostport may or may not contain a port.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/netutil"
)

func main() {
	host, err := netutil.SplitHost("example.com:12345")
	if err != nil {
		panic(err)
	}

	fmt.Println(host)

	host, err = netutil.SplitHost("example.org")
	if err != nil {
		panic(err)
	}

	fmt.Println(host)

	_, err = netutil.SplitHost("[BAD:!")
	fmt.Println(err)

}
Output:


example.com
example.org
address [BAD:!: missing ']' in address

func SplitHostPort

func SplitHostPort(hostport string) (host string, port int, err error)

SplitHostPort is a convenient wrapper for net.SplitHostPort with port of type int.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/netutil"
)

func main() {
	host, port, err := netutil.SplitHostPort("example.com:12345")
	if err != nil {
		panic(err)
	}

	fmt.Printf("%T(%[1]v)\n", host)
	fmt.Printf("%T(%[1]v)\n", port)

}
Output:


string(example.com)
int(12345)

func ValidateDomainName

func ValidateDomainName(name string) (err error)

ValidateDomainName validates the domain name in accordance to RFC 952, RFC 1035, and with RFC-1123's inclusion of digits at the start of the host. It doesn't validate against two or more hyphens to allow punycode and internationalized domains.

Any error returned will have the underlying type of *BadDomainError.

func ValidateDomainNameLabel

func ValidateDomainNameLabel(label string) (err error)

ValidateDomainNameLabel returns an error if label is not a valid label of a domain name. An empty label is considered invalid.

Any error returned will have the underlying type of *BadLabelError.

func ValidateMAC

func ValidateMAC(mac net.HardwareAddr) (err error)

ValidateMAC returns an error if hwa is not a valid EUI-48, EUI-64, or 20-octet InfiniBand link-layer address.

Any error returned will have the underlying type of *BadMACError.

Types

type BadDomainError

type BadDomainError struct {
	// Err is the underlying error.  The type of the underlying error is
	// one of the following:
	//
	//   *BadIPError
	//   *BadIPv4Error
	//   *BadLabelError
	//   *BadLengthError
	//   *BadRuneError
	//   *EmptyError
	//   *TooLongError
	//   any error returned by the Punicode validation.
	//
	// It can also be ErrNotAReversedIP.
	Err error
	// Kind is either "arpa domain name" or "domain name".
	Kind string
	// Name is the text of the invalid domain name.
	Name string
}

BadDomainError is the underlying type of errors returned from validation functions when a domain name is invalid.

func (*BadDomainError) Error

func (err *BadDomainError) Error() (msg string)

Error implements the error interface for *BadDomainError.

func (*BadDomainError) Unwrap

func (err *BadDomainError) Unwrap() (unwrapped error)

Unwrap implements the errors.Wrapper interface for *BadDomainError. It returns err.Err.

type BadIPError

type BadIPError struct {
	// IP is the text of the invalid IP address.
	IP string
}

BadIPError is the underlying type of errors returned from validation functions when an IP address is invalid.

func (*BadIPError) Error

func (err *BadIPError) Error() (msg string)

Error implements the error interface for *BadIPError.

type BadIPv4Error

type BadIPv4Error struct {
	// IP is the text of the invalid IP address.
	IP string
}

BadIPv4Error is the underlying type of errors returned from validation functions when an IP address is not a valid IPv4 address.

func (*BadIPv4Error) Error

func (err *BadIPv4Error) Error() (msg string)

Error implements the error interface for *BadIPv4Error.

type BadLabelError

type BadLabelError struct {
	// Err is the underlying error.  The type of the underlying error is
	// either *BadRuneError, or *EmptyError, or *TooLongError.
	Err error
	// Label is the text of the label.
	Label string
}

BadLabelError is the underlying type of errors returned from validation functions when a domain name label is invalid.

func (*BadLabelError) Error

func (err *BadLabelError) Error() (msg string)

Error implements the error interface for *BadLabelError.

func (*BadLabelError) Unwrap

func (err *BadLabelError) Unwrap() (unwrapped error)

Unwrap implements the errors.Wrapper interface for *BadLabelError. It returns err.Err.

type BadLengthError

type BadLengthError struct {
	// Kind is either "arpa domain name" or "mac address".
	Kind string
	// Allowed are the allowed lengths for this kind of address.
	Allowed []int
	// Length is the length of the provided address.
	Length int
}

BadLengthError is the underlying type of errors returned from validation functions when an address or a part of an address has a bad length.

func (*BadLengthError) Error

func (err *BadLengthError) Error() (msg string)

Error implements the error interface for *BadLengthError.

type BadMACError

type BadMACError struct {
	// Err is the underlying error.  The type of the underlying error is
	// either *EmptyError, or *BadLengthError.
	Err error
	// MAC is the text of the MAC address.
	MAC net.HardwareAddr
}

BadMACError is the underlying type of errors returned from validation functions when a MAC address is invalid.

func (*BadMACError) Error

func (err *BadMACError) Error() (msg string)

Error implements the error interface for *BadMACError.

func (*BadMACError) Unwrap

func (err *BadMACError) Unwrap() (unwrapped error)

Unwrap implements the errors.Wrapper interface for *BadMACError. It returns err.Err.

type BadRuneError

type BadRuneError struct {
	// Kind is either "arpa domain name", or "domain name label", or "mac
	// address".
	Kind string
	// Rune is the invalid rune.
	Rune rune
}

BadRuneError is the underlying type of errors returned from validation functions when a rune in the address is invalid.

func (*BadRuneError) Error

func (err *BadRuneError) Error() (msg string)

Error implements the error interface for *BadRuneError.

type EmptyError

type EmptyError struct {
	// Kind is either "domain name", or "domain name label", or "mac
	// address".
	Kind string
}

EmptyError is the underlying type of errors returned from validation functions when an address or a part of an address is missing.

func (*EmptyError) Error

func (err *EmptyError) Error() (msg string)

Error implements the error interface for *EmptyError.

type IPMap

type IPMap struct {
	// contains filtered or unexported fields
}

IPMap is a map of IP addresses.

func NewIPMap

func NewIPMap(hint int) (m *IPMap)

NewIPMap returns a new empty IP map using hint as a size hint for the underlying map.

It is not safe for concurrent use, just like the usual Go maps aren't.

func (*IPMap) Del

func (m *IPMap) Del(ip net.IP)

Del deletes ip from the map. Calling Del on a nil *IPMap has no effect, just like delete on an empty map doesn't.

func (*IPMap) Get

func (m *IPMap) Get(ip net.IP) (v interface{}, ok bool)

Get returns the value from the map. Calling Get on a nil *IPMap returns nil and false, just like indexing on an empty map does.

func (*IPMap) Len

func (m *IPMap) Len() (n int)

Len returns the length of the map. A nil *IPMap has a length of zero, just like an empty map.

func (*IPMap) Range

func (m *IPMap) Range(f func(ip net.IP, v interface{}) (cont bool))

Range calls f with a copy of the key and the value for each key-value pair present in the map in an undefined order. If cont is false, range stops the iteration. Calling Range on a nil *IPMap has no effect, just like ranging over a nil map.

func (*IPMap) Set

func (m *IPMap) Set(ip net.IP, v interface{})

Set sets the value. Set panics if the m is a nil *IPMap, just like a nil map does.

func (*IPMap) ShallowClone

func (m *IPMap) ShallowClone() (sclone *IPMap)

ShallowClone returns a shallow clone of the map.

func (*IPMap) String

func (m *IPMap) String() (s string)

String implements the fmt.Stringer interface for *IPMap.

type TooLongError

type TooLongError struct {
	// Kind is either "domain name", or "domain name label", or "mac
	// address".
	Kind string
	// Max is the maximum length for this part or address kind.
	Max int
}

TooLongError is the underlying type of errors returned from validation functions when an address or a part of an address is too long.

func (*TooLongError) Error

func (err *TooLongError) Error() (msg string)

Error implements the error interface for *TooLongError.

Jump to

Keyboard shortcuts

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