Documentation ¶
Overview ¶
Package netaddr contains a IP address type that's in many ways better than the Go standard library's net.IP type. Building on that IP type, the package also contains IPPrefix, IPPort, IPRange, and IPSet types.
Notably, this package's IP type takes less memory, is immutable, comparable (supports == and being a map key), and more. See https://github.com/inetaf/netaddr for background.
IPv6 Zones ¶
IP and IPPort are the only types in this package that support IPv6 zones. Other types silently drop any passed-in zones.
Index ¶
- type IP
- func FromStdIP(std net.IP) (ip IP, ok bool)
- func FromStdIPRaw(std net.IP) (ip IP, ok bool)
- func IPFrom16(addr [16]byte) IP
- func IPFrom4(addr [4]byte) IP
- func IPv4(a, b, c, d uint8) IP
- func IPv6LinkLocalAllNodes() IP
- func IPv6Raw(addr [16]byte) IP
- func IPv6Unspecified() IP
- func MustParseIP(s string) IP
- func ParseIP(s string) (IP, error)
- func (ip IP) AppendTo(b []byte) []byte
- func (ip IP) As16() [16]byte
- func (ip IP) As4() [4]byte
- func (ip IP) BitLen() uint8
- func (ip IP) Compare(ip2 IP) int
- func (ip IP) IPAddr() *net.IPAddr
- func (ip IP) Is4() bool
- func (ip IP) Is4in6() bool
- func (ip IP) Is6() bool
- func (ip IP) IsGlobalUnicast() bool
- func (ip IP) IsInterfaceLocalMulticast() bool
- func (ip IP) IsLinkLocalMulticast() bool
- func (ip IP) IsLinkLocalUnicast() bool
- func (ip IP) IsLoopback() bool
- func (ip IP) IsMulticast() bool
- func (ip IP) IsPrivate() bool
- func (ip IP) IsUnspecified() bool
- func (ip IP) IsValid() bool
- func (ip IP) IsZero() bool
- func (ip IP) Less(ip2 IP) bool
- func (ip IP) MarshalBinary() ([]byte, error)
- func (ip IP) MarshalText() ([]byte, error)
- func (ip IP) Netmask(mask []byte) (IPPrefix, error)
- func (ip IP) Next() IP
- func (ip IP) Prefix(bits uint8) (IPPrefix, error)
- func (ip IP) Prior() IP
- func (ip IP) String() string
- func (ip IP) StringExpanded() string
- func (ip IP) Unmap() IP
- func (ip *IP) UnmarshalBinary(b []byte) error
- func (ip *IP) UnmarshalText(text []byte) error
- func (ip IP) WithZone(zone string) IP
- func (ip IP) Zone() string
- type IPPort
- func (p IPPort) AppendTo(b []byte) []byte
- func (p IPPort) IP() IP
- func (p IPPort) IsValid() bool
- func (p IPPort) IsZero() bool
- func (p IPPort) MarshalText() ([]byte, error)
- func (p IPPort) Port() uint16
- func (p IPPort) String() string
- func (p IPPort) TCPAddr() *net.TCPAddr
- func (p IPPort) UDPAddr() *net.UDPAddr
- func (p IPPort) UDPAddrAt(at *net.UDPAddr) *net.UDPAddr
- func (p *IPPort) UnmarshalText(text []byte) error
- func (p IPPort) Valid() booldeprecated
- func (p IPPort) WithIP(ip IP) IPPort
- func (p IPPort) WithPort(port uint16) IPPort
- type IPPrefix
- func (p IPPrefix) AppendTo(b []byte) []byte
- func (p IPPrefix) Bits() uint8
- func (p IPPrefix) Contains(ip IP) bool
- func (p IPPrefix) IP() IP
- func (p IPPrefix) IPNet() *net.IPNet
- func (p IPPrefix) IsSingleIP() bool
- func (p IPPrefix) IsValid() bool
- func (p IPPrefix) IsZero() bool
- func (p IPPrefix) MarshalText() ([]byte, error)
- func (p IPPrefix) Masked() IPPrefix
- func (p IPPrefix) Overlaps(o IPPrefix) bool
- func (p IPPrefix) Range() IPRange
- func (p IPPrefix) String() string
- func (p *IPPrefix) UnmarshalText(text []byte) error
- func (p IPPrefix) Valid() booldeprecated
- type IPRange
- func (r IPRange) AppendPrefixes(dst []IPPrefix) []IPPrefix
- func (r IPRange) AppendTo(b []byte) []byte
- func (r IPRange) Contains(addr IP) bool
- func (r IPRange) From() IP
- func (r IPRange) IsValid() bool
- func (r IPRange) IsZero() bool
- func (r IPRange) MarshalText() ([]byte, error)
- func (r IPRange) Overlaps(o IPRange) bool
- func (r IPRange) Prefix() (p IPPrefix, ok bool)
- func (r IPRange) Prefixes() []IPPrefix
- func (r IPRange) String() string
- func (r IPRange) To() IP
- func (r *IPRange) UnmarshalText(text []byte) error
- func (r IPRange) Valid() booldeprecated
- type IPSet
- func (s *IPSet) Contains(ip IP) bool
- func (s *IPSet) ContainsPrefix(p IPPrefix) bool
- func (s *IPSet) ContainsRange(r IPRange) bool
- func (s *IPSet) Equal(o *IPSet) bool
- func (s *IPSet) Overlaps(b *IPSet) bool
- func (s *IPSet) OverlapsPrefix(p IPPrefix) bool
- func (s *IPSet) OverlapsRange(r IPRange) bool
- func (s *IPSet) Prefixes() []IPPrefix
- func (s *IPSet) Ranges() []IPRange
- func (s *IPSet) RemoveFreePrefix(bitLen uint8) (p IPPrefix, newSet *IPSet, ok bool)
- type IPSetBuilder
- func (s *IPSetBuilder) Add(ip IP)
- func (s *IPSetBuilder) AddPrefix(p IPPrefix)
- func (s *IPSetBuilder) AddRange(r IPRange)
- func (s *IPSetBuilder) AddSet(b *IPSet)
- func (s *IPSetBuilder) Clone() *IPSetBuilder
- func (s *IPSetBuilder) Complement()
- func (s *IPSetBuilder) IPSet() (*IPSet, error)
- func (s *IPSetBuilder) Intersect(b *IPSet)
- func (s *IPSetBuilder) Remove(ip IP)
- func (s *IPSetBuilder) RemovePrefix(p IPPrefix)
- func (s *IPSetBuilder) RemoveRange(r IPRange)
- func (s *IPSetBuilder) RemoveSet(b *IPSet)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IP ¶
type IP struct {
// contains filtered or unexported fields
}
IP represents an IPv4 or IPv6 address (with or without a scoped addressing zone), similar to Go's net.IP or net.IPAddr.
Unlike net.IP or net.IPAddr, the netaddr.IP is a comparable value type (it supports == and can be a map key) and is immutable. Its memory representation is 24 bytes on 64-bit machines (the same size as a Go slice header) for both IPv4 and IPv6 address.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { ip, err := netaddr.ParseIP("192.0.2.3") if err != nil { panic(err) } // netaddr.IP supports comparison using == fmt.Println(ip == netaddr.IPv4(192, 0, 2, 3)) // netaddr.IP can be used as a map key hosts := map[netaddr.IP]string{ip: "example.net"} fmt.Println(hosts) }
Output: true map[192.0.2.3:example.net]
Example (Properties) ¶
package main import ( "fmt" "os" "text/tabwriter" "inet.af/netaddr" ) func main() { var zeroIP netaddr.IP w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) fmt.Fprintln(w, "String()\tZone()\tIsZero()\tIs4()\tIs6()\tIs4in6()") for _, ip := range []netaddr.IP{ zeroIP, netaddr.MustParseIP("192.0.2.3"), netaddr.MustParseIP("2001:db8::68"), netaddr.MustParseIP("2001:db8::68%eth0"), netaddr.MustParseIP("::ffff:192.0.2.3"), } { fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\n", ip, ip.Zone(), ip.IsZero(), ip.Is4(), ip.Is6(), ip.Is4in6()) } w.Flush() }
Output: String() Zone() IsZero() Is4() Is6() Is4in6() zero IP true false false false 192.0.2.3 false true false false 2001:db8::68 false false true false 2001:db8::68%eth0 eth0 false false true false ::ffff:c000:203 false false true true
func FromStdIP ¶
FromStdIP returns an IP from the standard library's IP type.
If std is invalid, ok is false.
FromStdIP implicitly unmaps IPv6-mapped IPv4 addresses. That is, if len(std) == 16 and contains an IPv4 address, only the IPv4 part is returned, without the IPv6 wrapper. This is the common form returned by the standard library's ParseIP: https://play.golang.org/p/qdjylUkKWxl. To convert a standard library IP without the implicit unmapping, use FromStdIPRaw.
func FromStdIPRaw ¶
FromStdIPRaw returns an IP from the standard library's IP type. If std is invalid, ok is false. Unlike FromStdIP, FromStdIPRaw does not do an implicit Unmap if len(std) == 16 and contains an IPv6-mapped IPv4 address.
func IPFrom16 ¶
IPFrom16 returns the IP address given by the bytes in addr, unmapping any v6-mapped IPv4 address.
It is equivalent to calling IPv6Raw(addr).Unmap().
func IPFrom4 ¶
IPFrom4 returns the IPv4 address given by the bytes in addr. It is equivalent to calling IPv4(addr[0], addr[1], addr[2], addr[3]).
func IPv6LinkLocalAllNodes ¶
func IPv6LinkLocalAllNodes() IP
IPv6LinkLocalAllNodes returns the IPv6 link-local all nodes multicast address ff02::1.
func IPv6Raw ¶
IPv6Raw returns the IPv6 address given by the bytes in addr, without an implicit Unmap call to unmap any v6-mapped IPv4 address.
func IPv6Unspecified ¶
func IPv6Unspecified() IP
IPv6Unspecified returns the IPv6 unspecified address ::.
func MustParseIP ¶
MustParseIP calls ParseIP(s) and panics on error. It is intended for use in tests with hard-coded strings.
func ParseIP ¶
ParseIP parses s as an IP address, returning the result. The string s can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"), or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").
func (IP) AppendTo ¶
AppendTo appends a text encoding of ip, as generated by MarshalText, to b and returns the extended buffer.
func (IP) As16 ¶
As16 returns the IP address in its 16 byte representation. IPv4 addresses are returned in their v6-mapped form. IPv6 addresses with zones are returned without their zone (use the Zone method to get it). The ip zero value returns all zeroes.
func (IP) As4 ¶
As4 returns an IPv4 or IPv4-in-IPv6 address in its 4 byte representation. If ip is the IP zero value or an IPv6 address, As4 panics. Note that 0.0.0.0 is not the zero value.
func (IP) BitLen ¶
BitLen returns the number of bits in the IP address: 32 for IPv4 or 128 for IPv6. For the zero value (see IP.IsZero), it returns 0. For IP4-mapped IPv6 addresses, it returns 128.
func (IP) Compare ¶
Compare returns an integer comparing two IPs. The result will be 0 if ip==ip2, -1 if ip < ip2, and +1 if ip > ip2. The definition of "less than" is the same as the IP.Less method.
func (IP) IPAddr ¶
IPAddr returns the net.IPAddr representation of an IP. The returned value is always non-nil, but the IPAddr.IP will be nil if ip is the zero value. If ip contains a zone identifier, IPAddr.Zone is populated.
func (IP) Is4 ¶
Is4 reports whether ip is an IPv4 address.
It returns false for IP4-mapped IPv6 addresses. See IP.Unmap.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { var zeroIP netaddr.IP ipv4 := netaddr.MustParseIP("192.0.2.3") ipv6 := netaddr.MustParseIP("2001:db8::68") ip4in6 := netaddr.MustParseIP("::ffff:192.0.2.3") fmt.Printf("IP{}.Is4() -> %v\n", zeroIP.Is4()) fmt.Printf("(%v).Is4() -> %v\n", ipv4, ipv4.Is4()) fmt.Printf("(%v).Is4() -> %v\n", ipv6, ipv6.Is4()) fmt.Printf("(%v).Is4() -> %v\n", ip4in6, ip4in6.Is4()) }
Output: IP{}.Is4() -> false (192.0.2.3).Is4() -> true (2001:db8::68).Is4() -> false (::ffff:c000:203).Is4() -> false
func (IP) Is4in6 ¶
Is4in6 reports whether ip is an IPv4-mapped IPv6 address.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { var zeroIP netaddr.IP ipv4 := netaddr.MustParseIP("192.0.2.3") ipv6 := netaddr.MustParseIP("2001:db8::68") ip4in6 := netaddr.MustParseIP("::ffff:192.0.2.3") fmt.Printf("IP{}.Is4in6() -> %v\n", zeroIP.Is4in6()) fmt.Printf("(%v).Is4in6() -> %v\n", ipv4, ipv4.Is4in6()) fmt.Printf("(%v).Is4in6() -> %v\n", ipv6, ipv6.Is4in6()) fmt.Printf("(%v).Is4in6() -> %v\n", ip4in6, ip4in6.Is4in6()) }
Output: IP{}.Is4in6() -> false (192.0.2.3).Is4in6() -> false (2001:db8::68).Is4in6() -> false (::ffff:c000:203).Is4in6() -> true
func (IP) Is6 ¶
Is6 reports whether ip is an IPv6 address, including IPv4-mapped IPv6 addresses.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { var zeroIP netaddr.IP ipv4 := netaddr.MustParseIP("192.0.2.3") ipv6 := netaddr.MustParseIP("2001:db8::68") ip4in6 := netaddr.MustParseIP("::ffff:192.0.2.3") fmt.Printf("IP{}.Is6() -> %v\n", zeroIP.Is4in6()) fmt.Printf("(%v).Is6() -> %v\n", ipv4, ipv4.Is6()) fmt.Printf("(%v).Is6() -> %v\n", ipv6, ipv6.Is6()) fmt.Printf("(%v).Is6() -> %v\n", ip4in6, ip4in6.Is6()) }
Output: IP{}.Is6() -> false (192.0.2.3).Is6() -> false (2001:db8::68).Is6() -> true (::ffff:c000:203).Is6() -> true
func (IP) IsGlobalUnicast ¶
IsGlobalUnicast reports whether ip is a global unicast address.
It returns true for IPv6 addresses which fall outside of the current IANA-allocated 2000::/3 global unicast space, with the exception of the link-local address space. It also returns true even if ip is in the IPv4 private address space or IPv6 unique local address space. If ip is the zero value, it will return false.
For reference, see RFC 1122, RFC 4291, and RFC 4632.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { var ( zeroIP netaddr.IP ipv4AllZeroes = netaddr.MustParseIP("0.0.0.0") ipv4 = netaddr.MustParseIP("192.0.2.3") ipv6AllZeroes = netaddr.MustParseIP("::") ipv6LinkLocal = netaddr.MustParseIP("fe80::1") ipv6 = netaddr.MustParseIP("2001:db8::68") ipv6Unassigned = netaddr.MustParseIP("4000::1") ip4in6 = netaddr.MustParseIP("::ffff:192.0.2.3") ) fmt.Printf("IP{}.IsGlobalUnicast() -> %v\n", zeroIP.IsGlobalUnicast()) ips := []netaddr.IP{ ipv4AllZeroes, ipv4, ipv6AllZeroes, ipv6LinkLocal, ipv6, ipv6Unassigned, ip4in6, } for _, ip := range ips { fmt.Printf("(%v).IsGlobalUnicast() -> %v\n", ip, ip.IsGlobalUnicast()) } }
Output: IP{}.IsGlobalUnicast() -> false (0.0.0.0).IsGlobalUnicast() -> false (192.0.2.3).IsGlobalUnicast() -> true (::).IsGlobalUnicast() -> false (fe80::1).IsGlobalUnicast() -> false (2001:db8::68).IsGlobalUnicast() -> true (4000::1).IsGlobalUnicast() -> true (::ffff:c000:203).IsGlobalUnicast() -> true
func (IP) IsInterfaceLocalMulticast ¶
IsInterfaceLocalMulticast reports whether ip is an IPv6 interface-local multicast address. If ip is the zero value or an IPv4 address, it will return false.
func (IP) IsLinkLocalMulticast ¶
IsLinkLocalMulticast reports whether ip is a link-local multicast address. If ip is the zero value, it will return false.
func (IP) IsLinkLocalUnicast ¶
IsLinkLocalUnicast reports whether ip is a link-local unicast address. If ip is the zero value, it will return false.
func (IP) IsLoopback ¶
IsLoopback reports whether ip is a loopback address. If ip is the zero value, it will return false.
func (IP) IsMulticast ¶
IsMulticast reports whether ip is a multicast address. If ip is the zero value, it will return false.
func (IP) IsPrivate ¶
IsPrivate reports whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC 4193 (IPv6 addresses). That is, it reports whether ip is in 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, or fc00::/7. This is the same as the standard library's net.IP.IsPrivate.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { var ( zeroIP netaddr.IP ipv4 = netaddr.MustParseIP("192.0.2.3") ipv4Private = netaddr.MustParseIP("192.168.1.1") ipv6 = netaddr.MustParseIP("2001:db8::68") ipv6Private = netaddr.MustParseIP("fd00::1") ) fmt.Printf("IP{}.IsPrivate() -> %v\n", zeroIP.IsPrivate()) ips := []netaddr.IP{ ipv4, ipv4Private, ipv6, ipv6Private, } for _, ip := range ips { fmt.Printf("(%v).IsPrivate() -> %v\n", ip, ip.IsPrivate()) } }
Output: IP{}.IsPrivate() -> false (192.0.2.3).IsPrivate() -> false (192.168.1.1).IsPrivate() -> true (2001:db8::68).IsPrivate() -> false (fd00::1).IsPrivate() -> true
func (IP) IsUnspecified ¶
IsUnspecified reports whether ip is an unspecified address, either the IPv4 address "0.0.0.0" or the IPv6 address "::".
Note that the IP zero value is not an unspecified address. Use IsZero to check for the zero value instead.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { var zeroIP netaddr.IP ipv4AllZeroes := netaddr.MustParseIP("0.0.0.0") ipv6AllZeroes := netaddr.MustParseIP("::") fmt.Printf("IP{}.IsUnspecified() -> %v\n", zeroIP.IsUnspecified()) fmt.Printf("(%v).IsUnspecified() -> %v\n", ipv4AllZeroes, ipv4AllZeroes.IsUnspecified()) fmt.Printf("(%v).IsUnspecified() -> %v\n", ipv6AllZeroes, ipv6AllZeroes.IsUnspecified()) }
Output: IP{}.IsUnspecified() -> false (0.0.0.0).IsUnspecified() -> true (::).IsUnspecified() -> true
func (IP) IsValid ¶
IsValid whether the IP is an initialized value and not the IP type's zero value.
Note that both "0.0.0.0" and "::" are valid, non-zero values.
func (IP) IsZero ¶
IsZero reports whether ip is the zero value of the IP type. The zero value is not a valid IP address of any type.
Note that "0.0.0.0" and "::" are not the zero value. Use IsUnspecified to check for these values instead.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { var zeroIP netaddr.IP ipv4AllZeroes := netaddr.MustParseIP("0.0.0.0") ipv6AllZeroes := netaddr.MustParseIP("::") fmt.Printf("IP{}.IsZero() -> %v\n", zeroIP.IsZero()) fmt.Printf("(%v).IsZero() -> %v\n", ipv4AllZeroes, ipv4AllZeroes.IsZero()) fmt.Printf("(%v).IsZero() -> %v\n", ipv6AllZeroes, ipv6AllZeroes.IsZero()) }
Output: IP{}.IsZero() -> true (0.0.0.0).IsZero() -> false (::).IsZero() -> false
func (IP) Less ¶
Less reports whether ip sorts before ip2. IP addresses sort first by length, then their address. IPv6 addresses with zones sort just after the same address without a zone.
func (IP) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (IP) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface, The encoding is the same as returned by String, with one exception: If ip is the zero value, the encoding is the empty string.
func (IP) Netmask ¶
Netmask applies a bit mask to IP, producing an IPPrefix. If IP is the zero value, a zero-value IPPrefix and a nil error are returned. If the netmask length is not 4 for IPv4 or 16 for IPv6, an error is returned. If the netmask is non-contiguous, an error is returned.
func (IP) Prefix ¶
Prefix applies a CIDR mask of leading bits to IP, producing an IPPrefix of the specified length. If IP is the zero value, a zero-value IPPrefix and a nil error are returned. If bits is larger than 32 for an IPv4 address or 128 for an IPv6 address, an error is returned.
func (IP) String ¶
String returns the string form of the IP address ip. It returns one of 4 forms:
- "invalid IP", if ip is the zero value
- IPv4 dotted decimal ("192.0.2.1")
- IPv6 ("2001:db8::1")
- IPv6 with zone ("fe80:db8::1%eth0")
Note that unlike the Go standard library's IP.String method, IP4-mapped IPv6 addresses do not format as dotted decimals.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { ipv4 := netaddr.MustParseIP("192.0.2.3") ipv6 := netaddr.MustParseIP("2001:db8::68") ip4in6 := netaddr.MustParseIP("::ffff:192.0.2.3") fmt.Printf("(%v).String() -> %v\n", ipv4, ipv4.String()) fmt.Printf("(%v).String() -> %v\n", ipv6, ipv6.String()) fmt.Printf("(%v).String() -> %v\n", ip4in6, ip4in6.String()) }
Output: (192.0.2.3).String() -> 192.0.2.3 (2001:db8::68).String() -> 2001:db8::68 (::ffff:c000:203).String() -> ::ffff:c000:203
func (IP) StringExpanded ¶
StringExpanded is like String but IPv6 addresses are expanded with leading zeroes and no "::" compression. For example, "2001:db8::1" becomes "2001:0db8:0000:0000:0000:0000:0000:0001".
func (IP) Unmap ¶
Unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
That is, if ip is an IPv6 address wrapping an IPv4 adddress, it returns the wrapped IPv4 address. Otherwise it returns ip, regardless of its type.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { ipv4 := netaddr.MustParseIP("192.0.2.3") ipv6 := netaddr.MustParseIP("2001:db8::68") ip4in6 := netaddr.MustParseIP("::ffff:192.0.2.3") fmt.Printf("(%v).Unmap() -> %v\n", ipv4, ipv4.Unmap()) fmt.Printf("(%v).Unmap() -> %v\n", ipv6, ipv6.Unmap()) fmt.Printf("(%v).Unmap() -> %v\n", ip4in6, ip4in6.Unmap()) }
Output: (192.0.2.3).Unmap() -> 192.0.2.3 (2001:db8::68).Unmap() -> 2001:db8::68 (::ffff:c000:203).Unmap() -> 192.0.2.3
func (*IP) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (*IP) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The IP address is expected in a form accepted by ParseIP. It returns an error if *ip is not the IP zero value.
func (IP) WithZone ¶
WithZone returns an IP that's the same as ip but with the provided zone. If zone is empty, the zone is removed. If ip is an IPv4 address it's returned unchanged.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { ipv4 := netaddr.MustParseIP("192.0.2.3") ipv6 := netaddr.MustParseIP("2001:db8::68") ipv6Zoned := netaddr.MustParseIP("2001:db8::68%eth0") fmt.Printf("(%v).WithZone(\"newzone\") -> %v\n", ipv4, ipv4.WithZone("newzone")) fmt.Printf("(%v).WithZone(\"newzone\") -> %v\n", ipv6, ipv6.WithZone("newzone")) fmt.Printf("(%v).WithZone(\"newzone\") -> %v\n", ipv6Zoned, ipv6Zoned.WithZone("newzone")) fmt.Printf("(%v).WithZone(\"\") -> %v\n", ipv6Zoned, ipv6Zoned.WithZone("")) }
Output: (192.0.2.3).WithZone("newzone") -> 192.0.2.3 (2001:db8::68).WithZone("newzone") -> 2001:db8::68%newzone (2001:db8::68%eth0).WithZone("newzone") -> 2001:db8::68%newzone (2001:db8::68%eth0).WithZone("") -> 2001:db8::68
type IPPort ¶
type IPPort struct {
// contains filtered or unexported fields
}
IPPort is an IP and a port number.
func FromStdAddr ¶
FromStdAddr maps the components of a standard library TCPAddr or UDPAddr into an IPPort.
func IPPortFrom ¶
IPPortFrom returns an IPPort with IP ip and port port. It does not allocate.
func MustParseIPPort ¶
MustParseIPPort calls ParseIPPort(s) and panics on error. It is intended for use in tests with hard-coded strings.
func ParseIPPort ¶
ParseIPPort parses s as an IPPort.
It doesn't do any name resolution, and ports must be numeric.
func (IPPort) AppendTo ¶
AppendTo appends a text encoding of p, as generated by MarshalText, to b and returns the extended buffer.
func (IPPort) IsValid ¶
IsValid reports whether p.IP() is valid. All ports are valid, including zero.
func (IPPort) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String, with one exception: if p.IP() is the zero value, the encoding is the empty string.
func (IPPort) TCPAddr ¶
TCPAddr returns a standard library net.TCPAddr from p. The returned value is always non-nil. If p.IP() is the zero value, then TCPAddr.IP is nil.
func (IPPort) UDPAddr ¶
UDPAddr returns a standard library net.UDPAddr from p. The returned value is always non-nil. If p.IP() is the zero value, then UDPAddr.IP is nil.
UDPAddr necessarily does two allocations. If you have an existing UDPAddr already allocated, see UDPAddrAt.
func (IPPort) UDPAddrAt ¶
UDPAddrAt is like UDPAddr, but reuses the provided UDPAddr, which must be non-nil. If at.IP has a capacity of 16, UDPAddrAt is allocation-free. It returns at to facilitate using this method as a wrapper.
func (*IPPort) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The IPPort is expected in a form accepted by ParseIPPort. It returns an error if *p is not the IPPort zero value.
type IPPrefix ¶
type IPPrefix struct {
// contains filtered or unexported fields
}
IPPrefix is an IP address prefix (CIDR) representing an IP network.
The first Bits() of IP() are specified. The remaining bits match any address. The range of Bits() is [0,32] for IPv4 or [0,128] for IPv6.
func FromStdIPNet ¶
FromStdIPNet returns an IPPrefix from the standard library's IPNet type. If std is invalid, ok is false.
func IPPrefixFrom ¶
IPPrefixFrom returns an IPPrefix with IP ip and provided bits prefix length. It does not allocate.
func MustParseIPPrefix ¶
MustParseIPPrefix calls ParseIPPrefix(s) and panics on error. It is intended for use in tests with hard-coded strings.
func ParseIPPrefix ¶
ParseIPPrefix parses s as an IP address prefix. The string can be in the form "192.168.1.0/24" or "2001::db8::/32", the CIDR notation defined in RFC 4632 and RFC 4291.
Note that masked address bits are not zeroed. Use Masked for that.
func (IPPrefix) AppendTo ¶
AppendTo appends a text encoding of p, as generated by MarshalText, to b and returns the extended buffer.
func (IPPrefix) Contains ¶
Contains reports whether the network p includes ip.
An IPv4 address will not match an IPv6 prefix. A v6-mapped IPv6 address will not match an IPv4 prefix. A zero-value IP will not match any prefix. If ip has an IPv6 zone, Contains returns false, because IPPrefixes strip zones.
func (IPPrefix) IPNet ¶
IPNet returns the net.IPNet representation of an IPPrefix. The returned value is always non-nil. Any zone identifier is dropped in the conversion.
func (IPPrefix) IsSingleIP ¶
IsSingleIP reports whether p contains exactly one IP.
func (IPPrefix) IsValid ¶
IsValid reports whether whether p.Bits() has a valid range for p.IP(). If p.IP() is zero, Valid returns false.
func (IPPrefix) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface, The encoding is the same as returned by String, with one exception: If p is the zero value, the encoding is the empty string.
func (IPPrefix) Masked ¶
Masked returns p in its canonical form, with bits of p.IP() not in p.Bits() masked off. If p is zero or otherwise invalid, Masked returns the zero value.
func (IPPrefix) Overlaps ¶
Overlaps reports whether p and o overlap at all.
If p and o are of different address families or either have a zero IP, it reports false. Like the Contains method, a prefix with a v6-mapped IPv4 IP is still treated as an IPv6 mask.
If either has a Bits of zero, it returns true.
func (IPPrefix) Range ¶
Range returns the inclusive range of IPs that p covers.
If p is zero or otherwise invalid, Range returns the zero value.
func (*IPPrefix) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The IP address is expected in a form accepted by ParseIPPrefix. It returns an error if *p is not the IPPrefix zero value.
type IPRange ¶
type IPRange struct {
// contains filtered or unexported fields
}
IPRange represents an inclusive range of IP addresses from the same address family.
The From() and To() IPs are inclusive bounds, both included in the range.
To be valid, the From() and To() values must be non-zero, have matching address families (IPv4 vs IPv6), and From() must be less than or equal to To(). IPv6 zones are stripped out and ignored. An invalid range may be ignored.
func IPRangeFrom ¶
IPRangeFrom returns an IPRange from from to to. It does not allocate.
func MustParseIPRange ¶
MustParseIPRange calls ParseIPRange(s) and panics on error. It is intended for use in tests with hard-coded strings.
func ParseIPRange ¶
ParseIPRange parses a range out of two IPs separated by a hyphen.
It returns an error if the range is not valid.
func (IPRange) AppendPrefixes ¶
AppendPrefixes is an append version of IPRange.Prefixes. It appends the IPPrefix entries that cover r to dst.
func (IPRange) AppendTo ¶
AppendTo appends a text encoding of r, as generated by MarshalText, to b and returns the extended buffer.
func (IPRange) Contains ¶
Contains reports whether the range r includes addr.
An invalid range always reports false.
If ip has an IPv6 zone, Contains returns false, because IPPrefixes strip zones.
func (IPRange) IsValid ¶
IsValid reports whether r.From() and r.To() are both non-zero and obey the documented requirements: address families match, and From is less than or equal to To.
func (IPRange) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface, The encoding is the same as returned by String, with one exception: If ip is the zero value, the encoding is the empty string.
func (IPRange) Overlaps ¶
Overlaps reports whether p and o overlap at all.
If p and o are of different address families or either are invalid, it reports false.
func (IPRange) Prefix ¶
Prefix returns r as an IPPrefix, if it can be presented exactly as such. If r is not valid or is not exactly equal to one prefix, ok is false.
func (IPRange) Prefixes ¶
Prefixes returns the set of IPPrefix entries that covers r.
If either of r's bounds are invalid, in the wrong order, or if they're of different address families, then Prefixes returns nil.
Prefixes necessarily allocates. See AppendPrefixes for a version that uses memory you provide.
func (IPRange) String ¶
String returns a string representation of the range.
For a valid range, the form is "From-To" with a single hyphen separating the IPs, the same format recognized by ParseIPRange.
func (*IPRange) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The IP range is expected in a form accepted by ParseIPRange. It returns an error if *r is not the IPRange zero value.
type IPSet ¶
type IPSet struct {
// contains filtered or unexported fields
}
IPSet represents a set of IP addresses.
IPSet is safe for concurrent use. The zero value is a valid value representing a set of no IPs. Use IPSetBuilder to construct IPSets.
Example ¶
package main import ( "fmt" "inet.af/netaddr" ) func main() { var b netaddr.IPSetBuilder b.AddPrefix(netaddr.MustParseIPPrefix("10.0.0.0/8")) b.RemovePrefix(netaddr.MustParseIPPrefix("10.0.0.0/16")) b.AddRange(netaddr.IPRangeFrom( netaddr.MustParseIP("fed0::0400"), netaddr.MustParseIP("fed0::04ff"), )) s, _ := b.IPSet() fmt.Println("Ranges:") for _, r := range s.Ranges() { fmt.Printf(" %s - %s\n", r.From(), r.To()) } fmt.Println("Prefixes:") for _, p := range s.Prefixes() { fmt.Printf(" %s\n", p) } }
Output: Ranges: 10.1.0.0 - 10.255.255.255 fed0::400 - fed0::4ff Prefixes: 10.1.0.0/16 10.2.0.0/15 10.4.0.0/14 10.8.0.0/13 10.16.0.0/12 10.32.0.0/11 10.64.0.0/10 10.128.0.0/9 fed0::400/120
func (*IPSet) Contains ¶
Contains reports whether ip is in s. If ip has an IPv6 zone, Contains returns false, because IPSets do not track zones.
func (*IPSet) ContainsPrefix ¶
ContainsPrefix reports whether all IPs in p are in s.
func (*IPSet) ContainsRange ¶
ContainsRange reports whether all IPs in r are in s.
func (*IPSet) OverlapsPrefix ¶
OverlapsPrefix reports whether any IP in p is also in s.
func (*IPSet) OverlapsRange ¶
OverlapsRange reports whether any IP in r is also in s.
func (*IPSet) RemoveFreePrefix ¶
RemoveFreePrefix splits s into a Prefix of length bitLen and a new IPSet with that prefix removed.
If no contiguous prefix of length bitLen exists in s, RemoveFreePrefix returns ok=false.
type IPSetBuilder ¶
type IPSetBuilder struct {
// contains filtered or unexported fields
}
IPSetBuilder builds an immutable IPSet.
The zero value is a valid value representing a set of no IPs.
The Add and Remove methods add or remove IPs to/from the set. Removals only affect the current membership of the set, so in general Adds should be called first. Input ranges may overlap in any way.
Most IPSetBuilder methods do not return errors. Instead, errors are accumulated and reported by IPSetBuilder.IPSet.
func (*IPSetBuilder) AddPrefix ¶
func (s *IPSetBuilder) AddPrefix(p IPPrefix)
AddPrefix adds all IPs in p to s.
func (*IPSetBuilder) AddRange ¶
func (s *IPSetBuilder) AddRange(r IPRange)
AddRange adds r to s. If r is not Valid, AddRange does nothing.
func (*IPSetBuilder) AddSet ¶
func (s *IPSetBuilder) AddSet(b *IPSet)
AddSet adds all IPs in b to s.
func (*IPSetBuilder) Clone ¶
func (s *IPSetBuilder) Clone() *IPSetBuilder
Clone returns a copy of s that shares no memory with s.
func (*IPSetBuilder) Complement ¶
func (s *IPSetBuilder) Complement()
Complement updates s to contain the complement of its current contents.
func (*IPSetBuilder) IPSet ¶
func (s *IPSetBuilder) IPSet() (*IPSet, error)
IPSet returns an immutable IPSet representing the current state of s.
Most IPSetBuilder methods do not return errors. Rather, the builder ignores any invalid inputs (such as an invalid IPPrefix), and accumulates a list of any such errors that it encountered.
IPSet also reports any such accumulated errors. Even if the returned error is non-nil, the returned IPSet is usable and contains all modifications made with valid inputs.
The builder remains usable after calling IPSet. Calling IPSet clears any accumulated errors.
func (*IPSetBuilder) Intersect ¶
func (s *IPSetBuilder) Intersect(b *IPSet)
Intersect updates s to the set intersection of s and b.
func (*IPSetBuilder) RemovePrefix ¶
func (s *IPSetBuilder) RemovePrefix(p IPPrefix)
RemovePrefix removes all IPs in p from s.
func (*IPSetBuilder) RemoveRange ¶
func (s *IPSetBuilder) RemoveRange(r IPRange)
RemoveRange removes all IPs in r from s.
func (*IPSetBuilder) RemoveSet ¶
func (s *IPSetBuilder) RemoveSet(b *IPSet)
RemoveSet removes all IPs in o from s.