netmask

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: BSD-3-Clause Imports: 4 Imported by: 1

Documentation

Overview

Package netmask defiens a value type representing an network mask for IPv4 and IPv6.

Compared to the net.IPMask type, this package takes less memory, is immutable, and is comparable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mask

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

Mask represents an IPv4 mask or an IPv6 prefix, similar to net.IPMask or netip.Prefix.

Unlike net.IPMask, Mask is a comparable value type (it supports == and can be map key) and is immutable.

Unlike netip.Prefix, Mask is not attached to an IP address, and does not require IPv4 masks to be a prefix.

func MaskFrom

func MaskFrom(ones, bits int) Mask

MaskFrom returns a Mask consisting of 'ones' 1 bits followed by 0s up to a total length of 'bits' bits.

func MaskFrom16

func MaskFrom16(mask [16]byte) Mask

MaskFrom16 returns the IPv6 prefix given by the prefix in mask. Note that if the prefix is not one bits followed by all zero bits the invalid Mask is returned.

func MaskFrom4

func MaskFrom4(mask [4]byte) Mask

MaskFrom4 returns the IPv4 mask given by the bytes in mask.

func MaskFromSlice

func MaskFromSlice(mask []byte) (Mask, bool)

MaskFromSlice parses the 4- or 16-byte slices as an IPv4 netmask or IPv6 prefix. Note that a net.IPMask can by passed directly as the []byte argument. IIf slice's length is not 4 or 16, MaskFromSlice returns Mask{}, false.

func (Mask) AsSlice

func (mask Mask) AsSlice() []byte

AsSlice returns an IPv4 or IPv6 mask in its respective 4-byte or 16-byte representation.

func (Mask) Bits

func (mask Mask) Bits() int

Bits returns the masks's prefix length.

It reports -1 if the mask does not contain a prefix.

func (Mask) Equal

func (x Mask) Equal(y Mask) bool

func (Mask) Is4

func (mask Mask) Is4() bool

Is4 reports whether the Mask is for IPv4.

func (Mask) Is6

func (mask Mask) Is6() bool

Is6 reports whether the mask is for IPv6.

func (Mask) IsValid

func (mask Mask) IsValid() bool

IsValid reports whether the Mask is an initialized mask (not the zero Mask).

Note that a non-prefix mask is considered valid, even for IPv6.

func (Mask) MarshalBinary

func (mask Mask) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface. It returns a zero-length slice for the zero Mask, the 4-byte mask for IPv4, and a 1-byte prefix for IPv6.

func (Mask) MarshalText

func (mask Mask) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String, with one exception: If mask is the zero Mask, the encoding is the empty string.

func (Mask) String

func (mask Mask) String() string

String returns the string form of the Mask mask. It returns one of these forms:

- "invalid Mask", if mask is the zero Mask - IPv4 dotted decimal ("255.255.255.0") - IPv6 prefix ("64")

func (*Mask) UnmarshalBinary

func (mask *Mask) UnmarshalBinary(b []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It expects data in the form generated by MarshalBinary.

func (*Mask) UnmarshalText

func (mask *Mask) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The mask is expected in a form generated by MarshalText.

Jump to

Keyboard shortcuts

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