Documentation ¶
Overview ¶
netaddr is a Go library for performing calculations on IPv4 and IPv6 subnets. There is also limited support for EUI addresses.
Index ¶
- Constants
- func IPv4PrefixLen(size uint) uint
- type EUI48
- type EUI64
- type IP
- type IPNet
- type IPv4
- type IPv4List
- type IPv4Net
- func (net *IPv4Net) Cmp(other *IPv4Net) (int, error)
- func (net *IPv4Net) Contains(ip *IPv4) bool
- func (net *IPv4Net) Extended() string
- func (net *IPv4Net) Fill(list IPv4NetList) IPv4NetList
- func (net *IPv4Net) Len() uint32
- func (net *IPv4Net) Netmask() *Mask32
- func (net *IPv4Net) Network() *IPv4
- func (net *IPv4Net) Next() *IPv4Net
- func (net *IPv4Net) NextSib() *IPv4Net
- func (net *IPv4Net) Nth(index uint32) *IPv4
- func (net *IPv4Net) NthSubnet(prefixLen uint, index uint32) *IPv4Net
- func (net *IPv4Net) Prev() *IPv4Net
- func (net *IPv4Net) PrevSib() *IPv4Net
- func (net *IPv4Net) Rel(other *IPv4Net) (bool, int)
- func (net *IPv4Net) Resize(prefixLen uint) *IPv4Net
- func (net *IPv4Net) String() string
- func (net *IPv4Net) SubnetCount(prefixLen uint) uint32
- func (net *IPv4Net) Summ(other *IPv4Net) *IPv4Net
- func (ip *IPv4Net) Version() uint
- type IPv4NetList
- type IPv6
- func (ip *IPv6) Cmp(other *IPv6) (int, error)
- func (ip *IPv6) HostId() uint64
- func (ip *IPv6) IPv4(pl int) *IPv4
- func (ip *IPv6) IsZero() bool
- func (ip *IPv6) Long() string
- func (ip *IPv6) NetId() uint64
- func (ip *IPv6) Next() *IPv6
- func (ip *IPv6) Prev() *IPv6
- func (ip *IPv6) String() string
- func (ip *IPv6) ToNet() *IPv6Net
- func (ip *IPv6) Version() uint
- type IPv6List
- type IPv6Net
- func (net *IPv6Net) Cmp(other *IPv6Net) (int, error)
- func (net *IPv6Net) Contains(ip *IPv6) bool
- func (net *IPv6Net) Fill(list IPv6NetList) IPv6NetList
- func (net *IPv6Net) Len() uint64
- func (net *IPv6Net) Long() string
- func (net *IPv6Net) Netmask() *Mask128
- func (net *IPv6Net) Network() *IPv6
- func (net *IPv6Net) Next() *IPv6Net
- func (net *IPv6Net) NextSib() *IPv6Net
- func (net *IPv6Net) Nth(index uint64) *IPv6
- func (net *IPv6Net) NthSubnet(prefixLen uint, index uint64) *IPv6Net
- func (net *IPv6Net) Prev() *IPv6Net
- func (net *IPv6Net) PrevSib() *IPv6Net
- func (net *IPv6Net) Rel(other *IPv6Net) (bool, int)
- func (net *IPv6Net) Resize(prefixLen uint) *IPv6Net
- func (net *IPv6Net) String() string
- func (net *IPv6Net) SubnetCount(prefixLen uint) uint64
- func (net *IPv6Net) Summ(other *IPv6Net) *IPv6Net
- func (ip *IPv6Net) Version() uint
- type IPv6NetList
- type Mask128
- type Mask32
Examples ¶
- EUI64.ToIPv6
- IPv4List.Sort
- IPv4Net.Extended
- IPv4Net.Fill
- IPv4Net.Next
- IPv4Net.NextSib
- IPv4Net.Nth
- IPv4Net.NthSubnet
- IPv4Net.Prev
- IPv4Net.PrevSib
- IPv4Net.Resize
- IPv4Net.Summ
- IPv4NetList.Sort
- IPv4NetList.Summ
- IPv4PrefixLen
- IPv6List.Sort
- IPv6NetList.Sort
- IPv6NetList.Summ
- Mask32.Extended
- NewIPv4
- NewIPv4Net
- NewIPv4NetList
- NewIPv6NetList
- NewMask32
- ParseIP
- ParseIPNet
- ParseIPv4
- ParseIPv4Net
- ParseMask32
Constants ¶
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 ¶
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 ¶
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 '.'.
type EUI64 ¶
type EUI64 uint64
EUI64 (Extended Unique Identifier 64-bit, or EUI-64) represents a 64-bit hardware address.
func ParseEUI64 ¶
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) ToIPv6 ¶
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 IPv4 ¶
type IPv4 struct {
// contains filtered or unexported fields
}
IPv4 represents an version 4 IP address.
func NewIPv4 ¶
NewIPv4 creates an IPv4 type from a uint32
Example ¶
ip := NewIPv4(0x80000001) fmt.Println(ip)
Output: 128.0.0.1
func ParseIPv4 ¶
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) Cmp ¶
Cmp compares equality with another IPv4. Return:
- 1 if this IPv4 is numerically greater than other
- 0 if the two are equal
- -1 if this IPv4 is numerically less than other
func (*IPv4) MulticastMac ¶
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 ¶
Next returns the next consecutive IPv4 or nil if the end of the address space is reached.
type IPv4List ¶
type IPv4List []*IPv4
IPv4List is a slice of IPv4 types
func NewIPv4List ¶
NewIPv4List parses a slice of IPv4 addresses into a IPv4List.
type IPv4Net ¶
type IPv4Net struct {
// contains filtered or unexported fields
}
IPv4Net represents an IPv4 network.
func NewIPv4Net ¶
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 ¶
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 ¶
Cmp compares equality with another IPv4Net. Return:
- 1 if this IPv4Net is numerically greater than other
- 0 if the two are equal
- -1 if this IPv4Net is numerically less than other
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) Extended ¶
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 ¶
Len returns the number of IP addresses in this network. It will always return 0 for /0 networks.
func (*IPv4Net) Next ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) SubnetCount ¶
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)
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 ¶
NewIPv6 creates an IPv6 type from a pair of uint64. The pair represents the upper/lower 64-bits of the address respectively
func ParseIPv6 ¶
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 ¶
Cmp compares equality with another IPv6. Return:
- 1 if this IPv6 is numerically greater than other
- 0 if the two are equal
- -1 if this IPv6 is numerically less than other
func (*IPv6) IPv4 ¶
IPv4 generates an IPv4 from an IPv6 address. The IPv4 address is generated based on the mechanism described by RFC 6052. The argument pl (prefix length) should be one of: 32, 40, 48, 56, 64, or 96. Defaults to 96 unless one of the supported values is provided.
func (*IPv6) Next ¶
Next returns the next consecutive IPv6 or nil if the end of this /64 address space is reached.
func (*IPv6) Prev ¶
Prev returns the preceding IPv6 or nil if this is first address of this /64 space.
type IPv6List ¶
type IPv6List []*IPv6
IPv6List is a slice of IPv6 types
func NewIPv6List ¶
NewIPv6List parses a slice of IPv6 addresses into a IPv6List.
type IPv6Net ¶
type IPv6Net struct {
// contains filtered or unexported fields
}
IPv6Net represents an IPv6 network.
func NewIPv6Net ¶
NewIPv6Net creates a IPv6Net type from a IPv6 and Mask128. If netmask is nil then default to /64 (or /0 for address ::).
func ParseIPv6Net ¶
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 ¶
Cmp compares equality with another IPv6Net. Return:
- 1 if this IPv6Net is numerically greater than other
- 0 if the two are equal
- -1 if this IPv6Net is numerically less than other
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) 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 ¶
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) Next ¶
Next returns the next largest consecutive IP network or nil if the end of the address space is reached.
func (*IPv6Net) NextSib ¶
NextSib returns the network immediately following this one. It will return nil if the end of the address space is reached.
func (*IPv6Net) Nth ¶
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 ¶
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 ¶
Prev returns the previous largest consecutive IP network or nil if the start of the address space is reached.
func (*IPv6Net) PrevSib ¶
PrevSib returns the network immediately preceding this one. It will return nil if start of the address space is reached.
func (*IPv6Net) Rel ¶
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) SubnetCount ¶
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)
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 ¶
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 ¶
ParseMask128 parses a prefix length string to a Mask128 type. Netmask must be in "slash" format (eg. '/64' or just '64').
func (*Mask128) Cmp ¶
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 ¶
HostIdMask returns the internal uint64 mask for the host portion of the mask.
func (*Mask128) Len ¶
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 ¶
NetIdMask returns the internal uint64 mask for the network portion of the mask.
type Mask32 ¶
type Mask32 struct {
// contains filtered or unexported fields
}
Mask32 represents a 32-bit netmask used by IPv4Net.
func NewMask32 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Len returns the number of IP addresses in this network. It will always return 0 for /0 networks.