Documentation
¶
Overview ¶
Package netaddr is a Go implementation of netaddr in Python to operate the IPv4/IPv6 address or network.
Index ¶
- Variables
- type IPAddress
- func (ip IPAddress) Add(num int64) IPAddress
- func (ip IPAddress) BigInt() *big.Int
- func (ip IPAddress) Binary() string
- func (ip IPAddress) Bits() string
- func (ip IPAddress) Bytes() []byte
- func (ip IPAddress) Compare(other IPAddress) int
- func (ip IPAddress) Equal(other IPAddress) bool
- func (ip IPAddress) Hex() string
- func (ip IPAddress) IP() net.IP
- func (ip IPAddress) IPv4() IPAddress
- func (ip IPAddress) IPv6() IPAddress
- func (ip IPAddress) IsGlobalUnicast() bool
- func (ip IPAddress) IsIPv4() bool
- func (ip IPAddress) IsIPv6() bool
- func (ip IPAddress) IsInterfaceLocalMulticast() bool
- func (ip IPAddress) IsLinkLocalMulticast() bool
- func (ip IPAddress) IsLinkLocalUnicast() bool
- func (ip IPAddress) IsLoopback() bool
- func (ip IPAddress) IsMulticast() bool
- func (ip IPAddress) IsPrivate() bool
- func (ip IPAddress) IsReserved() bool
- func (ip IPAddress) IsUnicast() bool
- func (ip IPAddress) IsUnspecified() bool
- func (ip IPAddress) IsValid() bool
- func (ip IPAddress) Less(other IPAddress) bool
- func (ip IPAddress) Network() (net IPNetwork)
- func (ip IPAddress) String() string
- func (ip IPAddress) Sub(num int64) IPAddress
- func (ip IPAddress) Value() string
- func (ip IPAddress) Version() int
- type IPNetwork
- func (net IPNetwork) Address() IPAddress
- func (net IPNetwork) Broadcast() IPAddress
- func (net IPNetwork) CIDR() IPNetwork
- func (net IPNetwork) Contains(other IPNetwork) bool
- func (net IPNetwork) First() IPAddress
- func (net IPNetwork) HasIP(ip IPAddress) bool
- func (net IPNetwork) HasStringIP(ip string) bool
- func (net IPNetwork) HostMask() IPAddress
- func (net IPNetwork) Last() IPAddress
- func (net IPNetwork) Mask() int
- func (net IPNetwork) Network() IPAddress
- func (net IPNetwork) NetworkMask() IPAddress
- func (net IPNetwork) Next() (IPNetwork, error)
- func (net IPNetwork) Previous() (IPNetwork, error)
- func (net IPNetwork) Size() float64
- func (net IPNetwork) String() string
- func (net IPNetwork) Version() int
- func (net IPNetwork) Walk(f func(IPAddress))
Examples ¶
- IPAddress
- IPAddress.Add
- IPAddress.Binary
- IPAddress.Bits
- IPAddress.Compare
- IPAddress.Hex
- IPAddress.IsIPv4
- IPAddress.IsIPv6
- IPAddress.Less
- IPAddress.Network
- IPAddress.String
- IPAddress.Sub
- IPAddress.Value
- IPNetwork.Broadcast
- IPNetwork.CIDR
- IPNetwork.Contains
- IPNetwork.First
- IPNetwork.HasStringIP
- IPNetwork.HostMask
- IPNetwork.Last
- IPNetwork.Network
- IPNetwork.NetworkMask
- IPNetwork.Next
- IPNetwork.Previous
- IPNetwork.Size
- IPNetwork.Walk
Constants ¶
This section is empty.
Variables ¶
var ( IPv46To4 = MustNewIPNetwork("192.88.99.0/24") // 6to4 anycast relays (RFC 3068) IPv4Loopback = MustNewIPNetwork("127.0.0.0/8") // Loopback addresses (RFC 990) IPv4LinkLocal = MustNewIPNetwork("169.254.0.0/16") // Link-Local unicast address (RFC 3927) IPv4Multicast = MustNewIPNetwork("224.0.0.0/4") // Multicast address (RFC 5771) IPv4Private = []IPNetwork{ MustNewIPNetwork("10.0.0.0/8"), MustNewIPNetwork("100.64.0.0/10"), MustNewIPNetwork("172.16.0.0/12"), MustNewIPNetwork("192.0.0.0/24"), MustNewIPNetwork("192.168.0.0/16"), MustNewIPNetwork("198.18.0.0/15"), MustNewIPNetwork("239.0.0.0/8"), } IPv4Reserved = []IPNetwork{ MustNewIPNetwork("0.0.0.0/8"), MustNewIPNetwork("192.0.2.0/24"), MustNewIPNetwork("240.0.0.0/4"), MustNewIPNetwork("198.51.100.0/24"), MustNewIPNetwork("203.0.113.0/24"), MustNewIPNetwork("233.252.0.0/24"), MustNewIPNetwork("234.0.0.0/8"), MustNewIPNetwork("235.0.0.0/8"), MustNewIPNetwork("236.0.0.0/8"), MustNewIPNetwork("237.0.0.0/8"), MustNewIPNetwork("238.0.0.0/8"), MustNewIPNetwork("225.0.0.0/8"), MustNewIPNetwork("226.0.0.0/8"), MustNewIPNetwork("227.0.0.0/8"), MustNewIPNetwork("228.0.0.0/8"), MustNewIPNetwork("229.0.0.0/8"), MustNewIPNetwork("230.0.0.0/8"), MustNewIPNetwork("231.0.0.0/8"), IPv4Loopback, IPv46To4, } )
IPv4 Network
var ( IPv6Loopback = MustNewIPNetwork("::1/128") // Loopback address IPv6LinkLocal = MustNewIPNetwork("fe80::/10") // Link-Local unicast address IPv6Multicast = MustNewIPNetwork("ff00::/8") // Multicast address IPv6Private = []IPNetwork{ MustNewIPNetwork("fc00::/7"), MustNewIPNetwork("fec0::/10"), } IPv6Reserved = []IPNetwork{ MustNewIPNetwork("::/8"), MustNewIPNetwork("0100::/8"), MustNewIPNetwork("0200::/7"), MustNewIPNetwork("0400::/6"), MustNewIPNetwork("0800::/5"), MustNewIPNetwork("1000::/4"), MustNewIPNetwork("4000::/3"), MustNewIPNetwork("6000::/3"), MustNewIPNetwork("8000::/3"), MustNewIPNetwork("a000::/3"), MustNewIPNetwork("c000::/3"), MustNewIPNetwork("e000::/4"), MustNewIPNetwork("f000::/5"), MustNewIPNetwork("f800::/6"), MustNewIPNetwork("fe00::/9"), MustNewIPNetwork("ff00::/12"), } )
IPv6 Network
Functions ¶
This section is empty.
Types ¶
type IPAddress ¶
type IPAddress struct {
// contains filtered or unexported fields
}
IPAddress represents a IPv4/IPv6 address.
Example ¶
// IPv6
ip, err := NewIPAddress("fe80::d656:43a8:fc42:948c")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ip, ip.IsValid())
s := ip.Value()
fmt.Println(ip.Equal(MustNewIPAddress(s, ip.Version())))
fmt.Println(ip.IsGlobalUnicast())
fmt.Println(ip.IsInterfaceLocalMulticast())
fmt.Println(ip.IsLinkLocalMulticast())
fmt.Println(ip.IsLinkLocalUnicast())
fmt.Println(ip.IsLoopback())
fmt.Println(ip.IsMulticast())
// Ipv4
ip, err = NewIPAddress("192.168.10.10")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ip, ip.IsValid())
fmt.Println(ip.Bytes())
s = ip.Value()
fmt.Println(ip.Equal(MustNewIPAddress(s, ip.Version())))
fmt.Println(ip.IsGlobalUnicast())
fmt.Println(ip.IsInterfaceLocalMulticast())
fmt.Println(ip.IsLinkLocalMulticast())
fmt.Println(ip.IsLinkLocalUnicast())
fmt.Println(ip.IsLoopback())
fmt.Println(ip.IsMulticast())
// Loopback
ip, _ = NewIPAddress("127.0.0.1")
fmt.Println(ip.IsLoopback())
ip, _ = NewIPAddress("::1")
fmt.Println(ip.IsLoopback())
// Integer
ip = MustNewIPAddress(2130706433) // 127.0.0.1 => 127*256^3+1 => 2130706433
fmt.Println(ip)
Output: fe80::d656:43a8:fc42:948c true true false false false true false false 192.168.10.10 true [192 168 10 10] true true false false false false false true true 127.0.0.1
func MustNewIPAddress ¶
MustNewIPAddress is the same as NewIPAddress, but panic if an error occurs.
func NewIPAddress ¶
NewIPAddress returns a new IPAddress.
The argument, ip, is a IPv4 or IPv6 address, which maybe a string, []byte, [4]byte, [16]byte, net.IP, *big.Int, int, int64, uint, uint32 or uint64.
For *big.Int, the version is necessary, which reprensents the verion of the IP address, that's, 4 or 6.
For int, int64, uint, uint32 and uint64, the version is 4 by default.
For []byte, the version is necessary if the length of bytes is not 4 or 16.
For string, it maybe either a ipv4/ipv6 address or a integer string. If it's a integer string, it represents the integer value of the ipv4/ipv6, and must appoint the version explicitly.
func (IPAddress) Add ¶ added in v0.3.0
Add returns a new IPAddress with its numerical value increased by num.
Example ¶
fmt.Println(MustNewIPAddress("192.168.10.10").Add(10).String())
fmt.Println(MustNewIPAddress("fe80::").Add(10).String())
Output: 192.168.10.20 fe80::a
func (IPAddress) Binary ¶ added in v0.2.0
Binary returns the binary format of the IP address.
For example, "10101010101010101010101010101010".
Example ¶
fmt.Println(MustNewIPAddress("192.168.10.10").Binary())
fmt.Println(MustNewIPAddress("fe80::").Binary())
Output: 11000000101010000000101000001010 11111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
func (IPAddress) Bits ¶ added in v0.2.0
Bits returns the binary format of the IP address separated by the dot.
For example, "10101010.10101010.10101010.10101010".
Example ¶
fmt.Println(MustNewIPAddress("192.168.10.10").Bits())
fmt.Println(MustNewIPAddress("fe80::").Bits())
Output: 11000000.10101000.00001010.00001010 11111110.10000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000
func (IPAddress) Compare ¶ added in v0.4.0
Compare returns an integer comparing two IPAddresses. The result will be 0 if ip==other, -1 if ip < other, and +1 if ip > other.
Example ¶
ip1 := MustNewIPAddress("192.168.10.10")
ip2 := MustNewIPAddress("192.168.10.11")
switch ip1.Compare(ip2) {
case -1:
fmt.Println("less")
case 0:
fmt.Println("equal")
case 1:
fmt.Println("greater")
}
ip1 = MustNewIPAddress("192.168.10.10")
ip2 = MustNewIPAddress("192.168.10.10")
switch ip1.Compare(ip2) {
case -1:
fmt.Println("less")
case 0:
fmt.Println("equal")
case 1:
fmt.Println("greater")
}
ip1 = MustNewIPAddress("192.168.10.11")
ip2 = MustNewIPAddress("192.168.10.10")
switch ip1.Compare(ip2) {
case -1:
fmt.Println("less")
case 0:
fmt.Println("equal")
case 1:
fmt.Println("greater")
}
Output: less equal greater
func (IPAddress) Hex ¶ added in v0.3.0
Hex returns the hexadecimal format of the IP address.
For example, "0a0b0c0d".
Example ¶
fmt.Println(MustNewIPAddress("10.11.12.13").Hex())
fmt.Println(MustNewIPAddress("fe80::").Hex())
Output: 0a0b0c0d fe800000000000000000000000000000
func (IPAddress) IsGlobalUnicast ¶
IsGlobalUnicast reports whether ip is a global unicast address.
The identification of global unicast addresses uses address type identification as defined in RFC 1122, RFC 4632 and RFC 4291 with the exception of IPv4 directed broadcast addresses. It returns true even if ip is in IPv4 private address space or local IPv6 unicast address space.
They are not
0.0.0.0 255.255.255.255 127.0.0.0/8 169.254.0.0/16 224.0.0.0/4 :: ::1 fe80::/10 ff00::/8
func (IPAddress) IsIPv4 ¶ added in v0.4.0
IsIPv4 reports whether the ip address is ipv4.
Example ¶
fmt.Println(MustNewIPAddress("192.168.10.10").IsIPv4())
fmt.Println(MustNewIPAddress("fe80::").IsIPv4())
Output: true false
func (IPAddress) IsIPv6 ¶ added in v0.4.0
IsIPv6 reports whether the ip address is ipv6.
Example ¶
fmt.Println(MustNewIPAddress("192.168.10.10").IsIPv6())
fmt.Println(MustNewIPAddress("fe80::").IsIPv6())
Output: false true
func (IPAddress) IsInterfaceLocalMulticast ¶
IsInterfaceLocalMulticast reports whether ip is an interface-local multicast address. that's, ff01::/16.
func (IPAddress) IsLinkLocalMulticast ¶
IsLinkLocalMulticast reports whether ip is a link-local multicast address, that's, 224.0.0.0/24 or ff02::/16.
func (IPAddress) IsLinkLocalUnicast ¶
IsLinkLocalUnicast reports whether ip is a link-local unicast address, that's, 169.254.0.0/16 or fe80::/10.
func (IPAddress) IsLoopback ¶
IsLoopback reports whether the ip address is the loopback, that's, 127.0.0.1/8 or ::1.
func (IPAddress) IsMulticast ¶
IsMulticast reports whether the ip address is the multicast, that's, 224.0.0.0/4 or ff00::/8.
func (IPAddress) IsReserved ¶
IsReserved reports whether the ip address is the reserved.
func (IPAddress) IsUnspecified ¶
IsUnspecified reports whether ip is an unspecified address, either the IPv4 address "0.0.0.0" or the IPv6 address "::".
func (IPAddress) Less ¶
Less reports whether ip is less than other.
Example ¶
ip1 := MustNewIPAddress("192.168.10.10")
ip2 := MustNewIPAddress("192.168.10.11")
fmt.Println(ip1.Less(ip2))
Output: true
func (IPAddress) Network ¶
Network returns the network of the ip address.
Example ¶
ip := MustNewIPAddress("192.168.10.10")
net := ip.Network()
fmt.Println(net)
ip = MustNewIPAddress("fe80::d656:43a8:fc42:948c")
net = ip.Network()
fmt.Println(net)
Output: 192.168.10.10/32 fe80::d656:43a8:fc42:948c/128
func (IPAddress) String ¶
String returns a string representation of the ip address.
It will return "" if the ip address is invalid.
Example ¶
ip := MustNewIPAddress("192.168.10.10")
fmt.Println(ip.String())
Output: 192.168.10.10
func (IPAddress) Sub ¶ added in v0.3.0
Sub returns a new IPAddress with its numerical value decreased by num.
Example ¶
fmt.Println(MustNewIPAddress("192.168.10.10").Sub(10).String())
fmt.Println(MustNewIPAddress("fe80::").Sub(10).String())
Output: 192.168.10.0 fe7f:ffff:ffff:ffff:ffff:ffff:ffff:fff6
func (IPAddress) Value ¶
Value returns the integer string representation of the ipv4/ipv6 address.
Example ¶
ip1, _ := NewIPAddress("fe80::")
ip2, _ := NewIPAddress(ip1.Value(), ip1.Version())
fmt.Println(ip1.Value())
fmt.Println(ip2.Value())
fmt.Println(ip1.Equal(ip2))
fmt.Println(MustNewIPAddress("192.168.10.10").Value())
Output: 338288524927261089654018896841347694592 338288524927261089654018896841347694592 true 3232238090
type IPNetwork ¶
type IPNetwork struct {
// contains filtered or unexported fields
}
IPNetwork represents a IPv4/IPv6 network.
func MustNewIPNetwork ¶
MustNewIPNetwork is the same as NewIPNetwork, but panic if an error occurs.
func NewIPNetwork ¶
NewIPNetwork returns a new IPNetwork.
Change 0.2.0: the subnet mask is optional. The default is 32 for IPv4, and 128 for IPv6.
func NewIPNetworkFromIP ¶
NewIPNetworkFromIP returns a new IPNetwork by net.IP.
func NewIPNetworkFromIPAddress ¶
NewIPNetworkFromIPAddress returns a new IPNetwork by IPAddress.
func (IPNetwork) Broadcast ¶
Broadcast returns the broadcast address.
Example ¶
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.Broadcast())
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.Broadcast())
Output: 192.168.10.255 fe80::d656:43a8:fc42:948f
func (IPNetwork) CIDR ¶
CIDR returns the true CIDR address for the network which omits any host bits to the right of the CIDR subnet prefix.
Example ¶
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.CIDR())
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.CIDR())
Output: 192.168.10.0/24 fe80::d656:43a8:fc42:9480/124
func (IPNetwork) Contains ¶
Contains reports whether the network contains the other network, that's, other is the subnet of the current network.
Example ¶
net1 := MustNewIPNetwork("192.168.0.0/16")
net2 := MustNewIPNetwork("192.168.10.0/24")
net3 := MustNewIPNetwork("192.168.11.0/24")
fmt.Println(net1.Contains(net2))
fmt.Println(net2.Contains(net3))
fmt.Println(net3.Contains(net1))
Output: true false false
func (IPNetwork) First ¶
First returns the first ip address of the network.
Example ¶
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.First())
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.First())
Output: 192.168.10.0 fe80::d656:43a8:fc42:9480
func (IPNetwork) HasStringIP ¶
HasStringIP reports whether the network contains this ip address.
Example ¶
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.HasStringIP("192.168.10.0"))
fmt.Println(net.HasStringIP("192.168.11.0"))
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.HasStringIP("fe80::d656:43a8:fc42:9481"))
fmt.Println(net.HasStringIP("fe80::d656:43a8:fc42:9400"))
Output: true false true false
func (IPNetwork) HostMask ¶
HostMask returns the host mask address of the network.
Example ¶
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.HostMask())
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.HostMask())
Output: 0.0.0.255 ::f
func (IPNetwork) Last ¶
Last returns the last ip address of the network.
Example ¶
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.Last())
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.Last())
Output: 192.168.10.255 fe80::d656:43a8:fc42:948f
func (IPNetwork) Network ¶
Network returns the ip address.
Example ¶
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.Network())
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.Network())
Output: 192.168.10.0 fe80::d656:43a8:fc42:9480
func (IPNetwork) NetworkMask ¶
NetworkMask returns the network mask address of the network.
Example ¶
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.NetworkMask())
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.NetworkMask())
Output: 255.255.255.0 ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0
func (IPNetwork) Next ¶
Next returns the next network of the current.
Example ¶
// IPv4
next, _ := MustNewIPNetwork("192.168.10.10/24").Next()
fmt.Println(next)
next, _ = MustNewIPNetwork("192.168.255.10/24").Next()
fmt.Println(next)
_, err := MustNewIPNetwork("255.255.255.255/24").Next()
fmt.Println(err)
// IPv6
next, _ = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124").Next()
fmt.Println(next)
_, err = MustNewIPNetwork("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/124").Next()
fmt.Println(err)
Output: 192.168.11.0/24 192.169.0.0/24 increment exceeds address boundary fe80::d656:43a8:fc42:9490/124 increment exceeds address boundary
func (IPNetwork) Previous ¶
Previous returns the previous network of the current.
Example ¶
// IPv4
previous, _ := MustNewIPNetwork("192.168.10.10/24").Previous()
fmt.Println(previous)
previous, _ = MustNewIPNetwork("192.168.0.10/24").Previous()
fmt.Println(previous)
_, err := MustNewIPNetwork("0.0.0.0/24").Previous()
fmt.Println(err)
// IPv6
previous, _ = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124").Previous()
fmt.Println(previous)
_, err = MustNewIPNetwork("::f/124").Previous()
fmt.Println(err)
Output: 192.168.9.0/24 192.167.255.0/24 decrement is less than zero fe80::d656:43a8:fc42:9470/124 decrement is less than zero
func (IPNetwork) Size ¶
Size returns the number of the hosts in the network.
Example ¶
net := MustNewIPNetwork("192.168.10.10/22")
fmt.Printf("%.0f\n", net.Size())
net = MustNewIPNetwork("fe80::/16")
fmt.Printf("%.0f\n", net.Size())
net = MustNewIPNetwork("fe80::/118")
fmt.Printf("%.0f\n", net.Size())
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Printf("%.0f\n", net.Size())
Output: 1024 5192296858534827628530496329220096 1024 16
func (IPNetwork) String ¶
String returns a string representation of the ip network.
It will return "" if the network is invalid.
func (IPNetwork) Walk ¶
Walk traverses all the hosts in the network.
Notice: For the more bigger network, please don't traverse it.
Example ¶
net := MustNewIPNetwork("192.168.10.240/28")
net.Walk(func(ip IPAddress) {
fmt.Println(ip)
})
net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
net.Walk(func(ip IPAddress) {
fmt.Println(ip)
})
Output: 192.168.10.240 192.168.10.241 192.168.10.242 192.168.10.243 192.168.10.244 192.168.10.245 192.168.10.246 192.168.10.247 192.168.10.248 192.168.10.249 192.168.10.250 192.168.10.251 192.168.10.252 192.168.10.253 192.168.10.254 192.168.10.255 fe80::d656:43a8:fc42:9480 fe80::d656:43a8:fc42:9481 fe80::d656:43a8:fc42:9482 fe80::d656:43a8:fc42:9483 fe80::d656:43a8:fc42:9484 fe80::d656:43a8:fc42:9485 fe80::d656:43a8:fc42:9486 fe80::d656:43a8:fc42:9487 fe80::d656:43a8:fc42:9488 fe80::d656:43a8:fc42:9489 fe80::d656:43a8:fc42:948a fe80::d656:43a8:fc42:948b fe80::d656:43a8:fc42:948c fe80::d656:43a8:fc42:948d fe80::d656:43a8:fc42:948e fe80::d656:43a8:fc42:948f