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 ¶
- type Mask
- func (mask Mask) AsSlice() []byte
- func (mask Mask) Bits() int
- func (x Mask) Equal(y Mask) bool
- func (mask Mask) Is4() bool
- func (mask Mask) Is6() bool
- func (mask Mask) IsValid() bool
- func (mask Mask) MarshalBinary() ([]byte, error)
- func (mask Mask) MarshalText() ([]byte, error)
- func (mask Mask) String() string
- func (mask *Mask) UnmarshalBinary(b []byte) error
- func (mask *Mask) UnmarshalText(text []byte) error
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 ¶
MaskFrom returns a Mask consisting of 'ones' 1 bits followed by 0s up to a total length of 'bits' bits.
func MaskFrom16 ¶
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 MaskFromSlice ¶
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 ¶
AsSlice returns an IPv4 or IPv6 mask in its respective 4-byte or 16-byte representation.
func (Mask) Bits ¶
Bits returns the masks's prefix length.
It reports -1 if the mask does not contain a prefix.
func (Mask) IsValid ¶
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 ¶
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 ¶
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 ¶
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 ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It expects data in the form generated by MarshalBinary.
func (*Mask) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The mask is expected in a form generated by MarshalText.