netaddr

package module
v0.0.0-...-a6cfb69 Latest Latest
Warning

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

Go to latest
Published: May 10, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

netaddr

A Go library for performing calculations on IPv4 and IPv6 subnets. There is also limited support for EUI addresses.

Installation

It should be noted that while the repository name is "netaddr-go" the package name is simply "netaddr".

go get github.com/dspinhirne/netaddr-go

Usage

package main

import "fmt"
import "github.com/dspinhirne/netaddr-go"

func main() {
	net,_ := netaddr.ParseIPv4Net("192.168.1.0/24")
	fmt.Println(net)
}

Documentation

Available online here.

Documentation

Overview

netaddr is a Go library for performing calculations on IPv4 and IPv6 subnets. There is also limited support for EUI addresses.

Index

Examples

Constants

View Source
const (
	// 32 bits worth of '1'
	F32 uint32 = 0xffffffff

	// 64 bits worth of '1'
	F64 uint64 = 0xffffffffffffffff
)

Variables

This section is empty.

Functions

func IPv4PrefixLen

func IPv4PrefixLen(size uint) uint

IPv4PrefixLen returns the prefix length needed to hold the number of IP addresses specified by "size".

Example
// what size IPv4 subnet is capable of holding 200 addresses?
fmt.Println(IPv4PrefixLen(200))
Output:

24

Types

type EUI48

type EUI48 uint64

EUI48 (Extended Unique Identifier 48-bit, or EUI-48) represents a 48-bit hardware address. It is typically associated with mac-addresses.

func ParseEUI48

func ParseEUI48(eui string) (EUI48, error)

Parse an EUI-48 string into an EUI48 type. This will successfully parse most of the typically used formats such as:

  • aa-bb-cc-dd-ee-ff
  • aa:bb:cc:dd:ee:ff
  • aabb.ccdd.eeff
  • aabbccddeeff

Although, in truth, its not picky about the exact format as long as it contains exactly 12 hex characters with the optional delimiting characters '-', ':', or '.'.

func (EUI48) Bytes

func (eui EUI48) Bytes() []byte

Bytes returns a slice containing each byte of the EUI48.

func (EUI48) String

func (eui EUI48) String() string

func (EUI48) ToEUI64

func (eui EUI48) ToEUI64() EUI64

ToEUI64 converts this EUI48 into an EUI64 by inserting 0xfffe between the OUI and EUI

type EUI64

type EUI64 uint64

EUI64 (Extended Unique Identifier 64-bit, or EUI-64) represents a 64-bit hardware address.

func ParseEUI64

func ParseEUI64(eui string) (EUI64, error)

Parse an EUI-64 string into an EUI64 type. This will successfully parse most of the typically used formats such as:

  • aa-bb-cc-dd-ee-ff-00-11
  • aa:bb:cc:dd:ee:ff:00:11
  • aabb.ccdd.eeff.0011
  • aabbccddeeff0011

Although, in truth, its not picky about the exact format as long as it contains exactly 16 hex characters with the optional delimiting characters '-', ':', or '.'.

func (EUI64) Bytes

func (eui EUI64) Bytes() []byte

Bytes returns a slice containing each byte of the EUI64.

func (EUI64) String

func (eui EUI64) String() string

func (EUI64) ToIPv6

func (eui EUI64) ToIPv6(net *IPv6Net) *IPv6

ToIPv6 generates an IPv6 address from this EUI64 address and the provided IPv6Net. Nil will be returned if IPv6Net is not a /64.

Example
net, _ := ParseIPv6Net("fe80::/64")
eui48 := EUI48(0xaabbccddeeff)
ip := eui48.ToEUI64().ToIPv6(net)
fmt.Println(ip)
Output:

fe80::a8bb:ccff:fedd:eeff

type IP

type IP interface {
	String() string
	Version() uint
}

func ParseIP

func ParseIP(ip string) (IP, error)

ParseIP parses a string into an IP

Example
net, _ := ParseIP("10.0.0.0")
fmt.Println(net)
Output:

10.0.0.0

type IPNet

type IPNet interface {
	String() string
	Version() uint
}

func ParseIPNet

func ParseIPNet(net string) (IPNet, error)

ParseIPNet parses a string into an IPNet

Example
net, _ := ParseIPNet("10.0.0.0/24")
fmt.Println(net)
Output:

10.0.0.0/24

type IPv4

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

IPv4 represents an version 4 IP address.

func NewIPv4

func NewIPv4(addr uint32) *IPv4

NewIPv4 creates an IPv4 type from a uint32

Example
ip := NewIPv4(0x80000001)
fmt.Println(ip)
Output:

128.0.0.1

func ParseIPv4

func ParseIPv4(ip string) (*IPv4, error)

ParseIPv4 parses a string into an IPv4 type. IP address should be in dotted-quad format (x.x.x.x) and should not contain a netmask.

Example
ip, _ := ParseIPv4("128.0.0.1")
fmt.Println(ip)
Output:

128.0.0.1

func (*IPv4) Addr

func (ip *IPv4) Addr() uint32

Addr returns the internal uint32 address.

func (*IPv4) Cmp

func (ip *IPv4) Cmp(other *IPv4) (int, error)

Cmp compares equality with another IPv4. Return:

  • 1 if this IPv4 is numerically greater
  • 0 if the two are equal
  • -1 if this IPv4 is numerically less

func (*IPv4) MulticastMac

func (ip *IPv4) MulticastMac() EUI48

MulticastMac returns the multicast mac-address for this IP. It will return a value of 0 for addresses outside of the multicast range 224.0.0.0/4.

func (*IPv4) Next

func (ip *IPv4) Next() *IPv4

Next returns the next consecutive IPv4 or nil if the end of the address space is reached.

func (*IPv4) Prev

func (ip *IPv4) Prev() *IPv4

Prev returns the preceding IPv4 or nil if this is 0.0.0.0.

func (*IPv4) String

func (ip *IPv4) String() string

String return IPv4 address as a string.

func (*IPv4) ToNet

func (ip *IPv4) ToNet() *IPv4Net

ToNet returns the IPv4 as a IPv4Net

func (*IPv4) Version

func (ip *IPv4) Version() uint

type IPv4List

type IPv4List []*IPv4

IPv4List is a slice of IPv4 types

func NewIPv4List

func NewIPv4List(ips []string) (IPv4List, error)

NewIPv4List parses a slice of IPv4 addresses into a IPv4List.

func (IPv4List) Len

func (list IPv4List) Len() int

Len is used to implement the sort interface

func (IPv4List) Less

func (list IPv4List) Less(i, j int) bool

Less is used to implement the sort interface

func (IPv4List) Sort

func (list IPv4List) Sort() IPv4List

Sort sorts the list using sort.Sort(). Returns itself.

Example
ips := []string{"10.0.0.0", "1.0.0.0", "10.0.0.0", "192.168.1.0", "8.8.8.8", "10.0.0.0"}
list, _ := NewIPv4List(ips)
list.Sort()
fmt.Println(list)
Output:

[1.0.0.0 8.8.8.8 10.0.0.0 10.0.0.0 10.0.0.0 192.168.1.0]

func (IPv4List) Swap

func (list IPv4List) Swap(i, j int)

Swap is used to implement the sort interface

type IPv4Net

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

IPv4Net represents an IPv4 network.

func NewIPv4Net

func NewIPv4Net(ip *IPv4, m32 *Mask32) (*IPv4Net, error)

NewIPv4Net creates a IPv4Net type from a IPv4 and Mask32. If m32 is nil then default to /32.

Example
ip, _ := ParseIPv4("10.0.0.0")
m32, _ := NewMask32(24)
net, _ := NewIPv4Net(ip, m32)
fmt.Println(net)
Output:

10.0.0.0/24

func ParseIPv4Net

func ParseIPv4Net(addr string) (*IPv4Net, error)

ParseIPv4Net parses a string into an IPv4Net type. Accepts addresses in the form of:

  • single IP (eg. 192.168.1.1 -- defaults to /32)
  • CIDR format (eg. 192.168.1.1/24)
  • extended format (eg. 192.168.1.1 255.255.255.0)
Example
net, _ := ParseIPv4Net("10.0.0.0/24")
fmt.Println(net)
Output:

10.0.0.0/24

func (*IPv4Net) Cmp

func (net *IPv4Net) Cmp(other *IPv4Net) (int, error)

Cmp compares equality with another IPv4Net. Return:

  • 1 if this IPv4Net is numerically greater
  • 0 if the two are equal
  • -1 if this IPv4Net is numerically less

The comparasin is initially performed on using the Cmp() method of the network address, however, in cases where the network addresses are identical then the netmasks will be compared with the Cmp() method of the netmask.

func (*IPv4Net) Contains

func (net *IPv4Net) Contains(ip *IPv4) bool

Contains returns true if the IPv4Net contains the IPv4

func (*IPv4Net) Extended

func (net *IPv4Net) Extended() string

Extended returns the network address as a string in extended format.

Example
net, _ := ParseIPv4Net("10.0.0.0/24")
fmt.Println(net.Extended())
Output:

10.0.0.0 255.255.255.0

func (*IPv4Net) Fill

func (net *IPv4Net) Fill(list IPv4NetList) IPv4NetList

Fill returns a copy of the given IPv4NetList, stripped of any networks which are not subnets of this IPv4Net, and with any missing gaps filled in.

Example
net, _ := ParseIPv4Net("10.0.0.0/24")
subs, _ := NewIPv4NetList([]string{"10.0.0.0/26"})
subs = net.Fill(subs) // fills in the missing subnets
fmt.Println(subs)
Output:

[10.0.0.0/26 10.0.0.64/26 10.0.0.128/25]

func (*IPv4Net) Len

func (net *IPv4Net) Len() uint32

Len returns the number of IP addresses in this network. It will always return 0 for /0 networks.

func (*IPv4Net) Netmask

func (net *IPv4Net) Netmask() *Mask32

Netmask returns the Mask32 used by the IPv4Net.

func (*IPv4Net) Network

func (net *IPv4Net) Network() *IPv4

Network returns the network address of the IPv4Net.

func (*IPv4Net) Next

func (net *IPv4Net) Next() *IPv4Net

Next returns the next largest consecutive IP network or nil if the end of the address space is reached.

Example
net, _ := ParseIPv4Net("10.0.0.4/30")
next := net.Next()
fmt.Println(next)
Output:

10.0.0.8/29

func (*IPv4Net) NextSib

func (net *IPv4Net) NextSib() *IPv4Net

NextSib returns the network immediately following this one. It will return nil if the end of the address space is reached.

Example
net, _ := ParseIPv4Net("10.0.0.4/30")
next := net.NextSib()
fmt.Println(next)
Output:

10.0.0.8/30

func (*IPv4Net) Nth

func (net *IPv4Net) Nth(index uint32) *IPv4

Nth returns the IP address at the given index. The size of the network may be determined with the Len() method. If the range is exceeded then return nil.

Example
net, _ := ParseIPv4Net("10.0.0.0/24")
last := net.Len() - 1
bcast := net.Nth(last) // the broadcast address
fmt.Println(bcast)
Output:

10.0.0.255

func (*IPv4Net) NthSubnet

func (net *IPv4Net) NthSubnet(prefixLen uint, index uint32) *IPv4Net

NthSubnet returns the subnet IPv4Net at the given index. The number of subnets may be determined with the SubnetCount() method. If the range is exceeded or an invalid prefixLen is provided then return nil.

Example
net, _ := ParseIPv4Net("10.0.0.0/24")
fmt.Println(net.NthSubnet(30, 2)) // the 3rd /30 subnet
Output:

10.0.0.8/30

func (*IPv4Net) Prev

func (net *IPv4Net) Prev() *IPv4Net

Prev returns the previous largest consecutive IP network or nil if the start of the address space is reached.

Example
net, _ := ParseIPv4Net("10.0.0.8/30")
prev := net.Prev()
fmt.Println(prev)
Output:

10.0.0.0/29

func (*IPv4Net) PrevSib

func (net *IPv4Net) PrevSib() *IPv4Net

PrevSib returns the network immediately preceding this one. It will return nil if this is 0.0.0.0.

Example
net, _ := ParseIPv4Net("10.0.0.8/30")
prev := net.PrevSib()
fmt.Println(prev)
Output:

10.0.0.4/30

func (*IPv4Net) Rel

func (net *IPv4Net) Rel(other *IPv4Net) (bool, int)

Rel determines the relationship to another IPv4Net. The method returns two values: a bool and an int. If the bool is false, then the two networks are unrelated and the int will be 0. If the bool is true, then the int will be interpreted as:

  • 1 if this IPv4Net is the supernet of other
  • 0 if the two are equal
  • -1 if this IPv4Net is a subnet of other

func (*IPv4Net) Resize

func (net *IPv4Net) Resize(prefixLen uint) *IPv4Net

Resize returns a copy of the network with an adjusted netmask or nil if an invalid prefixLen is given.

Example
net, _ := ParseIPv4Net("10.0.0.8/30")
resized := net.Resize(29)
fmt.Println(resized)
Output:

10.0.0.8/29

func (*IPv4Net) String

func (net *IPv4Net) String() string

String returns the network address as a string in CIDR format.

func (*IPv4Net) SubnetCount

func (net *IPv4Net) SubnetCount(prefixLen uint) uint32

SubnetCount returns the number a subnets of a given prefix length that this IPv4Net contains. It will return 0 for invalid requests (ie. bad prefix or prefix is shorter than that of this network). It will also return 0 if the result exceeds the capacity of uint32 (ie. if you want the # of /32 a /0 will hold)

func (*IPv4Net) Summ

func (net *IPv4Net) Summ(other *IPv4Net) *IPv4Net

Summ creates a summary address from this IPv4Net and another or nil if the two networks are incapable of being summarized.

Example
net1, _ := ParseIPv4Net("10.0.0.0/30")
net2, _ := ParseIPv4Net("10.0.0.4/30")
summd := net1.Summ(net2)
fmt.Println(summd)
Output:

10.0.0.0/29

func (*IPv4Net) Version

func (ip *IPv4Net) Version() uint

type IPv4NetList

type IPv4NetList []*IPv4Net

IPv4NetList is a slice of IPv4 types

func NewIPv4NetList

func NewIPv4NetList(networks []string) (IPv4NetList, error)

NewIPv4NetList parses a slice of IP networks into a IPv4NetList.

Example
nets := []string{"10.0.0.0/24", "1.0.0.0/24"}
list, _ := NewIPv4NetList(nets)
fmt.Println(list)
Output:

[10.0.0.0/24 1.0.0.0/24]

func (IPv4NetList) Len

func (list IPv4NetList) Len() int

Len is used to implement the sort interface

func (IPv4NetList) Less

func (list IPv4NetList) Less(i, j int) bool

Less is used to implement the sort interface

func (IPv4NetList) Sort

func (list IPv4NetList) Sort() IPv4NetList

Sort sorts the list using sort.Sort(). Returns itself.

Example
nets := []string{"10.0.0.0/24", "1.0.0.0/24", "10.0.0.0/8", "192.168.1.0/26", "8.8.8.8/32", "10.0.0.0/8"}
list, _ := NewIPv4NetList(nets)
list.Sort()
fmt.Println(list)
Output:

[1.0.0.0/24 8.8.8.8/32 10.0.0.0/24 10.0.0.0/8 10.0.0.0/8 192.168.1.0/26]

func (IPv4NetList) Summ

func (list IPv4NetList) Summ() IPv4NetList

Summ returns a copy of the list with the contained IPv4Net entries sorted and summarized as much as possible.

Example
nets := []string{"10.0.0.0/24", "10.0.0.64/26", "1.1.1.0/24", "1.0.0.0/8", "3.4.5.6/32", "3.4.5.8/31", "2.2.2.224/27"}
list, _ := NewIPv4NetList(nets)
list = list.Summ()
fmt.Println(list)
Output:

[1.0.0.0/8 2.2.2.224/27 3.4.5.6/32 3.4.5.8/31 10.0.0.0/24]

func (IPv4NetList) Swap

func (list IPv4NetList) Swap(i, j int)

Swap is used to implement the sort interface

type IPv6

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

func NewIPv6

func NewIPv6(netId, hostId uint64) *IPv6

NewIPv6 creates an IPv6 type from a pair of uint64. The pair represents the upper/lower 64-bits of the address respectively

func ParseIPv6

func ParseIPv6(ip string) (*IPv6, error)

ParseIPv6 arses a string into an IPv6 type. IP address should be in one of the following formats and should not contain a netmask.

  • long format (eg. 0000:0000:0000:0000:0000:0000:0000:0001)
  • zero-compressed short format (eg. ::1)

func (*IPv6) Cmp

func (ip *IPv6) Cmp(other *IPv6) (int, error)

Cmp compares equality with another IPv6. Return:

  • 1 if this IPv6 is numerically greater
  • 0 if the two are equal
  • -1 if this IPv6 is numerically less

func (*IPv6) HostId

func (ip *IPv6) HostId() uint64

HostId returns the interal uint64 for the host id portion of the address.

func (*IPv6) IsZero

func (ip *IPv6) IsZero() bool

IsZero returns true if this address is "::"

func (*IPv6) Long

func (ip *IPv6) Long() string

Long returns the IPv6 address as a string in long (uncompressed) format.

func (*IPv6) NetId

func (ip *IPv6) NetId() uint64

NetId returns the interal uint64 for the network id portion of the address.

func (*IPv6) Next

func (ip *IPv6) Next() *IPv6

Next returns the next consecutive IPv6 or nil if the end of this /64 address space is reached.

func (*IPv6) Prev

func (ip *IPv6) Prev() *IPv6

Prev returns the preceding IPv6 or nil if this is first address of this /64 space.

func (*IPv6) String

func (ip *IPv6) String() string

String returns IPv6 as a string in zero-compressed format (per rfc5952). Use Long() to render in uncompressed format.

func (*IPv6) ToNet

func (ip *IPv6) ToNet() *IPv6Net

ToNet returns the IPv6 as a IPv6Net

func (*IPv6) Version

func (ip *IPv6) Version() uint

type IPv6List

type IPv6List []*IPv6

IPv6List is a slice of IPv6 types

func NewIPv6List

func NewIPv6List(ips []string) (IPv6List, error)

NewIPv6List parses a slice of IPv6 addresses into a IPv6List.

func (IPv6List) Len

func (list IPv6List) Len() int

Len is used to implement the sort interface

func (IPv6List) Less

func (list IPv6List) Less(i, j int) bool

Less is used to implement the sort interface

func (IPv6List) Sort

func (list IPv6List) Sort() IPv6List

Sort sorts the list using sort.Sort(). Returns itself.

Example
ips := []string{"1::", "2::", "1::1", "::", "::1", "fec0::1"}
list, _ := NewIPv6List(ips)
list.Sort()
fmt.Println(list)
Output:

[:: ::1 1:: 1::1 2:: fec0::1]

func (IPv6List) Swap

func (list IPv6List) Swap(i, j int)

Swap is used to implement the sort interface

type IPv6Net

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

IPv6Net represents an IPv6 network.

func NewIPv6Net

func NewIPv6Net(ip *IPv6, m128 *Mask128) (*IPv6Net, error)

NewIPv6Net creates a IPv6Net type from a IPv6 and Mask128. If netmask is nil then default to /64 (or /0 for address ::).

func ParseIPv6Net

func ParseIPv6Net(addr string) (*IPv6Net, error)

ParseIPv6Net parses a string into an IPv6Net type. Accepts addresses in the form of:

  • single IP (eg. FE80::1)
  • CIDR format (eg. ::1/128)

func (*IPv6Net) Cmp

func (net *IPv6Net) Cmp(other *IPv6Net) (int, error)

Cmp compares equality with another IPv6Net. Return:

  • 1 if this IPv6Net is numerically greater
  • 0 if the two are equal
  • -1 if this IPv6Net is numerically less

The comparasin is initially performed on using the Cmp() method of the network address, however, in cases where the network addresses are identical then the netmasks will be compared with the Cmp() method of the netmask.

func (*IPv6Net) Contains

func (net *IPv6Net) Contains(ip *IPv6) bool

Contains returns true if the IPv4Net contains the IPv4

func (*IPv6Net) Fill

func (net *IPv6Net) Fill(list IPv6NetList) IPv6NetList

Fill returns a copy of the given IPv6NetList, stripped of any networks which are not subnets of this IPv6Net, and with any missing gaps filled in.

func (*IPv6Net) Len

func (net *IPv6Net) Len() uint64

Len returns the number of IP addresses in this network. This is only useful if you have a subnet smaller than a /64 as it will always return 0 for prefixes <= 64.

func (*IPv6Net) Long

func (net *IPv6Net) Long() string

Long returns the network address as a string in long (uncomrpessed) format.

func (*IPv6Net) Netmask

func (net *IPv6Net) Netmask() *Mask128

Netmask returns the Mask128 used by the IPv6Net.

func (*IPv6Net) Network

func (net *IPv6Net) Network() *IPv6

Network returns the network address of the IPv6Net.

func (*IPv6Net) Next

func (net *IPv6Net) Next() *IPv6Net

Next returns the next largest consecutive IP network or nil if the end of the address space is reached.

func (*IPv6Net) NextSib

func (net *IPv6Net) NextSib() *IPv6Net

NextSib returns the network immediately following this one. It will return nil if the end of the address space is reached.

func (*IPv6Net) Nth

func (net *IPv6Net) Nth(index uint64) *IPv6

Nth returns the IP address at the given index. If the range is exceeded then return nil. This only works for /64 and greater; if the prefix length is < 64 then return nil. For /64 networks the max index is F64. If the prefix length is > 64 then use the Len() method to deterimine the size of the range.

func (*IPv6Net) NthSubnet

func (net *IPv6Net) NthSubnet(prefixLen uint, index uint64) *IPv6Net

NthSubnet returns the subnet IPv6Net at the given index. The number of subnets may be determined with the SubnetCount() method. If the range is exceeded or an invalid prefixLen is provided then return nil.

func (*IPv6Net) Prev

func (net *IPv6Net) Prev() *IPv6Net

Prev returns the previous largest consecutive IP network or nil if the start of the address space is reached.

func (*IPv6Net) PrevSib

func (net *IPv6Net) PrevSib() *IPv6Net

PrevSib returns the network immediately preceding this one. It will return nil if start of the address space is reached.

func (*IPv6Net) Rel

func (net *IPv6Net) Rel(other *IPv6Net) (bool, int)

Rel determines the relationship to another IPv6Net. The method returns two values: a bool and an int. If the bool is false, then the two networks are unrelated and the int will be 0. If the bool is true, then the int will be interpreted as:

  • 1 if this IPv6Net is the supernet of other
  • 0 if the two are equal
  • -1 if this IPv6Net is a subnet of other

func (*IPv6Net) Resize

func (net *IPv6Net) Resize(prefixLen uint) *IPv6Net

Resize returns a copy of the network with an adjusted netmask.

func (*IPv6Net) String

func (net *IPv6Net) String() string

String returns the network address as a string in zero-compressed format.

func (*IPv6Net) SubnetCount

func (net *IPv6Net) SubnetCount(prefixLen uint) uint64

SubnetCount returns the number a subnets of a given prefix length that this IPv6Net contains. It will return 0 for invalid requests (ie. bad prefix or prefix is shorter than that of this network). It will also return 0 if the result exceeds the capacity of uint64 (ie. if you want the # of /128 a /8 will hold)

func (*IPv6Net) Summ

func (net *IPv6Net) Summ(other *IPv6Net) *IPv6Net

Summ creates a summary address from this IPv6Net and another or nil if the two networks are incapable of being summarized.

func (*IPv6Net) Version

func (ip *IPv6Net) Version() uint

type IPv6NetList

type IPv6NetList []*IPv6Net

IPv6NetList is a slice of IPv6 types

func NewIPv6NetList

func NewIPv6NetList(networks []string) (IPv6NetList, error)

NewIPv6NetList parses a slice of IP networks into a IPv6NetList.

Example
nets := []string{"1::/64", "2::/64"}
list, _ := NewIPv6NetList(nets)
fmt.Println(list)
Output:

[1::/64 2::/64]

func (IPv6NetList) Len

func (list IPv6NetList) Len() int

Len is used to implement the sort interface

func (IPv6NetList) Less

func (list IPv6NetList) Less(i, j int) bool

Less is used to implement the sort interface

func (IPv6NetList) Sort

func (list IPv6NetList) Sort() IPv6NetList

Sort sorts the list using sort.Sort(). Returns itself.

Example
nets := []string{"1::/64", "2::/64", "1::/16", "::", "::1", "1::/16"}
list, _ := NewIPv6NetList(nets)
list.Sort()
fmt.Println(list)
Output:

[::/128 ::1/128 1::/64 1::/16 1::/16 2::/64]

func (IPv6NetList) Summ

func (list IPv6NetList) Summ() IPv6NetList

Summ returns a copy of the list with the contained IPv6Net entries sorted and summarized as much as possible.

Example
nets := []string{"ff00::/13", "ff08::/14", "ff0c::/14", "ff10::/12", "ff20::/11", "ff40::/10", "ff80::/9"}
list, _ := NewIPv6NetList(nets)
list = list.Summ()
fmt.Println(list)
Output:

[ff00::/8]

func (IPv6NetList) Swap

func (list IPv6NetList) Swap(i, j int)

Swap is used to implement the sort interface

type Mask128

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

Mask128 represents a 128-bit netmask used by IPv6Net.

func NewMask128

func NewMask128(prefixLen uint) (*Mask128, error)

NewMask128 converts an integer, representing the prefix length for an IPv6 network, to a Mask128 type. Integer must be from 0 to 128.

func ParseMask128

func ParseMask128(prefixLen string) (*Mask128, error)

ParseMask128 parses a prefix length string to a Mask128 type. Netmask must be in "slash" format (eg. '/64' or just '64').

func (*Mask128) Cmp

func (m128 *Mask128) Cmp(other *Mask128) int

Cmp compares equality with another Mask128. Return:

  • 1 if this Mask128 is larger in capacity
  • 0 if the two are equal
  • -1 if this Mask128 is smaller in capacity

func (*Mask128) HostIdMask

func (m128 *Mask128) HostIdMask() uint64

HostIdMask returns the internal uint64 mask for the host portion of the mask.

func (*Mask128) Len

func (m128 *Mask128) Len() uint64

Len returns the number of IP addresses in this network. This is only useful if you have a subnet smaller than a /64 as it will always return 0 for prefixes <= 64.

func (*Mask128) NetIdMask

func (m128 *Mask128) NetIdMask() uint64

NetIdMask returns the internal uint64 mask for the network portion of the mask.

func (*Mask128) PrefixLen

func (m128 *Mask128) PrefixLen() uint

PrefixLen returns the prefix length as an Uint.

func (*Mask128) String

func (m128 *Mask128) String() string

String returns the prefix length as a string.

type Mask32

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

Mask32 represents a 32-bit netmask used by IPv4Net.

func NewMask32

func NewMask32(prefixLen uint) (*Mask32, error)

NewMask32 converts an integer, representing the prefix length for an IPv4 address, to a Mask32 type. Integer must be from 0 to 32.

Example
m32, _ := NewMask32(32)
fmt.Println(m32)
Output:

/32

func ParseMask32

func ParseMask32(netmask string) (*Mask32, error)

ParseMask32 parses an IPv4 netmask or prefix length string to a Mask32 type. Netmask must be in either dotted-quad format (y.y.y.y) or "slash" format (eg. '/32' or just '32').

Example
m32, _ := ParseMask32("/32")
fmt.Println(m32)
Output:

/32

func (*Mask32) Cmp

func (m32 *Mask32) Cmp(other *Mask32) int

Cmp compares equality with another Mask32. Return:

  • 1 if this Mask32 is larger in capacity
  • 0 if the two are equal
  • -1 if this Mask32 is smaller in capacity

func (*Mask32) Extended

func (m32 *Mask32) Extended() string

Extended returns the Mask32 as a string in extended format.

Example
m32, _ := ParseMask32("/24")
fmt.Println(m32.Extended())
Output:

255.255.255.0

func (*Mask32) Len

func (m32 *Mask32) Len() uint32

Len returns the number of IP addresses in this network. It will always return 0 for /0 networks.

func (*Mask32) Mask

func (m32 *Mask32) Mask() uint32

Mask returns the internal uint32 mask.

func (*Mask32) PrefixLen

func (m32 *Mask32) PrefixLen() uint

PrefixLen returns the prefix length as an Uint.

func (*Mask32) String

func (m32 *Mask32) String() string

String returns the prefix length as a string. Use Extended() to return in extended format instead.

Jump to

Keyboard shortcuts

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