...

Source file src/net/netip/netip.go

Documentation: net/netip

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package netip defines an IP address type that's a small value type.
     6  // Building on that Addr type, the package also defines AddrPort (an
     7  // IP address and a port), and Prefix (an IP address and a bit length
     8  // prefix).
     9  //
    10  // Compared to the net.IP type, this package's Addr type takes less
    11  // memory, is immutable, and is comparable (supports == and being a
    12  // map key).
    13  package netip
    14  
    15  import (
    16  	"errors"
    17  	"math"
    18  	"strconv"
    19  
    20  	"internal/bytealg"
    21  	"internal/intern"
    22  	"internal/itoa"
    23  )
    24  
    25  // Sizes: (64-bit)
    26  //   net.IP:     24 byte slice header + {4, 16} = 28 to 40 bytes
    27  //   net.IPAddr: 40 byte slice header + {4, 16} = 44 to 56 bytes + zone length
    28  //   netip.Addr: 24 bytes (zone is per-name singleton, shared across all users)
    29  
    30  // Addr represents an IPv4 or IPv6 address (with or without a scoped
    31  // addressing zone), similar to net.IP or net.IPAddr.
    32  //
    33  // Unlike net.IP or net.IPAddr, Addr is a comparable value
    34  // type (it supports == and can be a map key) and is immutable.
    35  //
    36  // The zero Addr is not a valid IP address.
    37  // Addr{} is distinct from both 0.0.0.0 and ::.
    38  type Addr struct {
    39  	// addr is the hi and lo bits of an IPv6 address. If z==z4,
    40  	// hi and lo contain the IPv4-mapped IPv6 address.
    41  	//
    42  	// hi and lo are constructed by interpreting a 16-byte IPv6
    43  	// address as a big-endian 128-bit number. The most significant
    44  	// bits of that number go into hi, the rest into lo.
    45  	//
    46  	// For example, 0011:2233:4455:6677:8899:aabb:ccdd:eeff is stored as:
    47  	//  addr.hi = 0x0011223344556677
    48  	//  addr.lo = 0x8899aabbccddeeff
    49  	//
    50  	// We store IPs like this, rather than as [16]byte, because it
    51  	// turns most operations on IPs into arithmetic and bit-twiddling
    52  	// operations on 64-bit registers, which is much faster than
    53  	// bytewise processing.
    54  	addr uint128
    55  
    56  	// z is a combination of the address family and the IPv6 zone.
    57  	//
    58  	// nil means invalid IP address (for a zero Addr).
    59  	// z4 means an IPv4 address.
    60  	// z6noz means an IPv6 address without a zone.
    61  	//
    62  	// Otherwise it's the interned zone name string.
    63  	z *intern.Value
    64  }
    65  
    66  // z0, z4, and z6noz are sentinel Addr.z values.
    67  // See the Addr type's field docs.
    68  var (
    69  	z0    = (*intern.Value)(nil)
    70  	z4    = new(intern.Value)
    71  	z6noz = new(intern.Value)
    72  )
    73  
    74  // IPv6LinkLocalAllNodes returns the IPv6 link-local all nodes multicast
    75  // address ff02::1.
    76  func IPv6LinkLocalAllNodes() Addr { return AddrFrom16([16]byte{0: 0xff, 1: 0x02, 15: 0x01}) }
    77  
    78  // IPv6Unspecified returns the IPv6 unspecified address "::".
    79  func IPv6Unspecified() Addr { return Addr{z: z6noz} }
    80  
    81  // IPv4Unspecified returns the IPv4 unspecified address "0.0.0.0".
    82  func IPv4Unspecified() Addr { return AddrFrom4([4]byte{}) }
    83  
    84  // AddrFrom4 returns the address of the IPv4 address given by the bytes in addr.
    85  func AddrFrom4(addr [4]byte) Addr {
    86  	return Addr{
    87  		addr: uint128{0, 0xffff00000000 | uint64(addr[0])<<24 | uint64(addr[1])<<16 | uint64(addr[2])<<8 | uint64(addr[3])},
    88  		z:    z4,
    89  	}
    90  }
    91  
    92  // AddrFrom16 returns the IPv6 address given by the bytes in addr.
    93  // An IPv4-mapped IPv6 address is left as an IPv6 address.
    94  // (Use Unmap to convert them if needed.)
    95  func AddrFrom16(addr [16]byte) Addr {
    96  	return Addr{
    97  		addr: uint128{
    98  			beUint64(addr[:8]),
    99  			beUint64(addr[8:]),
   100  		},
   101  		z: z6noz,
   102  	}
   103  }
   104  
   105  // ipv6Slice is like IPv6Raw, but operates on a 16-byte slice. Assumes
   106  // slice is 16 bytes, caller must enforce this.
   107  func ipv6Slice(addr []byte) Addr {
   108  	return Addr{
   109  		addr: uint128{
   110  			beUint64(addr[:8]),
   111  			beUint64(addr[8:]),
   112  		},
   113  		z: z6noz,
   114  	}
   115  }
   116  
   117  // ParseAddr parses s as an IP address, returning the result. The string
   118  // s can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"),
   119  // or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").
   120  func ParseAddr(s string) (Addr, error) {
   121  	for i := 0; i < len(s); i++ {
   122  		switch s[i] {
   123  		case '.':
   124  			return parseIPv4(s)
   125  		case ':':
   126  			return parseIPv6(s)
   127  		case '%':
   128  			// Assume that this was trying to be an IPv6 address with
   129  			// a zone specifier, but the address is missing.
   130  			return Addr{}, parseAddrError{in: s, msg: "missing IPv6 address"}
   131  		}
   132  	}
   133  	return Addr{}, parseAddrError{in: s, msg: "unable to parse IP"}
   134  }
   135  
   136  // MustParseAddr calls ParseAddr(s) and panics on error.
   137  // It is intended for use in tests with hard-coded strings.
   138  func MustParseAddr(s string) Addr {
   139  	ip, err := ParseAddr(s)
   140  	if err != nil {
   141  		panic(err)
   142  	}
   143  	return ip
   144  }
   145  
   146  type parseAddrError struct {
   147  	in  string // the string given to ParseAddr
   148  	msg string // an explanation of the parse failure
   149  	at  string // optionally, the unparsed portion of in at which the error occurred.
   150  }
   151  
   152  func (err parseAddrError) Error() string {
   153  	q := strconv.Quote
   154  	if err.at != "" {
   155  		return "ParseAddr(" + q(err.in) + "): " + err.msg + " (at " + q(err.at) + ")"
   156  	}
   157  	return "ParseAddr(" + q(err.in) + "): " + err.msg
   158  }
   159  
   160  // parseIPv4 parses s as an IPv4 address (in form "192.168.0.1").
   161  func parseIPv4(s string) (ip Addr, err error) {
   162  	var fields [4]uint8
   163  	var val, pos int
   164  	var digLen int // number of digits in current octet
   165  	for i := 0; i < len(s); i++ {
   166  		if s[i] >= '0' && s[i] <= '9' {
   167  			if digLen == 1 && val == 0 {
   168  				return Addr{}, parseAddrError{in: s, msg: "IPv4 field has octet with leading zero"}
   169  			}
   170  			val = val*10 + int(s[i]) - '0'
   171  			digLen++
   172  			if val > 255 {
   173  				return Addr{}, parseAddrError{in: s, msg: "IPv4 field has value >255"}
   174  			}
   175  		} else if s[i] == '.' {
   176  			// .1.2.3
   177  			// 1.2.3.
   178  			// 1..2.3
   179  			if i == 0 || i == len(s)-1 || s[i-1] == '.' {
   180  				return Addr{}, parseAddrError{in: s, msg: "IPv4 field must have at least one digit", at: s[i:]}
   181  			}
   182  			// 1.2.3.4.5
   183  			if pos == 3 {
   184  				return Addr{}, parseAddrError{in: s, msg: "IPv4 address too long"}
   185  			}
   186  			fields[pos] = uint8(val)
   187  			pos++
   188  			val = 0
   189  			digLen = 0
   190  		} else {
   191  			return Addr{}, parseAddrError{in: s, msg: "unexpected character", at: s[i:]}
   192  		}
   193  	}
   194  	if pos < 3 {
   195  		return Addr{}, parseAddrError{in: s, msg: "IPv4 address too short"}
   196  	}
   197  	fields[3] = uint8(val)
   198  	return AddrFrom4(fields), nil
   199  }
   200  
   201  // parseIPv6 parses s as an IPv6 address (in form "2001:db8::68").
   202  func parseIPv6(in string) (Addr, error) {
   203  	s := in
   204  
   205  	// Split off the zone right from the start. Yes it's a second scan
   206  	// of the string, but trying to handle it inline makes a bunch of
   207  	// other inner loop conditionals more expensive, and it ends up
   208  	// being slower.
   209  	zone := ""
   210  	i := bytealg.IndexByteString(s, '%')
   211  	if i != -1 {
   212  		s, zone = s[:i], s[i+1:]
   213  		if zone == "" {
   214  			// Not allowed to have an empty zone if explicitly specified.
   215  			return Addr{}, parseAddrError{in: in, msg: "zone must be a non-empty string"}
   216  		}
   217  	}
   218  
   219  	var ip [16]byte
   220  	ellipsis := -1 // position of ellipsis in ip
   221  
   222  	// Might have leading ellipsis
   223  	if len(s) >= 2 && s[0] == ':' && s[1] == ':' {
   224  		ellipsis = 0
   225  		s = s[2:]
   226  		// Might be only ellipsis
   227  		if len(s) == 0 {
   228  			return IPv6Unspecified().WithZone(zone), nil
   229  		}
   230  	}
   231  
   232  	// Loop, parsing hex numbers followed by colon.
   233  	i = 0
   234  	for i < 16 {
   235  		// Hex number. Similar to parseIPv4, inlining the hex number
   236  		// parsing yields a significant performance increase.
   237  		off := 0
   238  		acc := uint32(0)
   239  		for ; off < len(s); off++ {
   240  			c := s[off]
   241  			if c >= '0' && c <= '9' {
   242  				acc = (acc << 4) + uint32(c-'0')
   243  			} else if c >= 'a' && c <= 'f' {
   244  				acc = (acc << 4) + uint32(c-'a'+10)
   245  			} else if c >= 'A' && c <= 'F' {
   246  				acc = (acc << 4) + uint32(c-'A'+10)
   247  			} else {
   248  				break
   249  			}
   250  			if acc > math.MaxUint16 {
   251  				// Overflow, fail.
   252  				return Addr{}, parseAddrError{in: in, msg: "IPv6 field has value >=2^16", at: s}
   253  			}
   254  		}
   255  		if off == 0 {
   256  			// No digits found, fail.
   257  			return Addr{}, parseAddrError{in: in, msg: "each colon-separated field must have at least one digit", at: s}
   258  		}
   259  
   260  		// If followed by dot, might be in trailing IPv4.
   261  		if off < len(s) && s[off] == '.' {
   262  			if ellipsis < 0 && i != 12 {
   263  				// Not the right place.
   264  				return Addr{}, parseAddrError{in: in, msg: "embedded IPv4 address must replace the final 2 fields of the address", at: s}
   265  			}
   266  			if i+4 > 16 {
   267  				// Not enough room.
   268  				return Addr{}, parseAddrError{in: in, msg: "too many hex fields to fit an embedded IPv4 at the end of the address", at: s}
   269  			}
   270  			// TODO: could make this a bit faster by having a helper
   271  			// that parses to a [4]byte, and have both parseIPv4 and
   272  			// parseIPv6 use it.
   273  			ip4, err := parseIPv4(s)
   274  			if err != nil {
   275  				return Addr{}, parseAddrError{in: in, msg: err.Error(), at: s}
   276  			}
   277  			ip[i] = ip4.v4(0)
   278  			ip[i+1] = ip4.v4(1)
   279  			ip[i+2] = ip4.v4(2)
   280  			ip[i+3] = ip4.v4(3)
   281  			s = ""
   282  			i += 4
   283  			break
   284  		}
   285  
   286  		// Save this 16-bit chunk.
   287  		ip[i] = byte(acc >> 8)
   288  		ip[i+1] = byte(acc)
   289  		i += 2
   290  
   291  		// Stop at end of string.
   292  		s = s[off:]
   293  		if len(s) == 0 {
   294  			break
   295  		}
   296  
   297  		// Otherwise must be followed by colon and more.
   298  		if s[0] != ':' {
   299  			return Addr{}, parseAddrError{in: in, msg: "unexpected character, want colon", at: s}
   300  		} else if len(s) == 1 {
   301  			return Addr{}, parseAddrError{in: in, msg: "colon must be followed by more characters", at: s}
   302  		}
   303  		s = s[1:]
   304  
   305  		// Look for ellipsis.
   306  		if s[0] == ':' {
   307  			if ellipsis >= 0 { // already have one
   308  				return Addr{}, parseAddrError{in: in, msg: "multiple :: in address", at: s}
   309  			}
   310  			ellipsis = i
   311  			s = s[1:]
   312  			if len(s) == 0 { // can be at end
   313  				break
   314  			}
   315  		}
   316  	}
   317  
   318  	// Must have used entire string.
   319  	if len(s) != 0 {
   320  		return Addr{}, parseAddrError{in: in, msg: "trailing garbage after address", at: s}
   321  	}
   322  
   323  	// If didn't parse enough, expand ellipsis.
   324  	if i < 16 {
   325  		if ellipsis < 0 {
   326  			return Addr{}, parseAddrError{in: in, msg: "address string too short"}
   327  		}
   328  		n := 16 - i
   329  		for j := i - 1; j >= ellipsis; j-- {
   330  			ip[j+n] = ip[j]
   331  		}
   332  		for j := ellipsis + n - 1; j >= ellipsis; j-- {
   333  			ip[j] = 0
   334  		}
   335  	} else if ellipsis >= 0 {
   336  		// Ellipsis must represent at least one 0 group.
   337  		return Addr{}, parseAddrError{in: in, msg: "the :: must expand to at least one field of zeros"}
   338  	}
   339  	return AddrFrom16(ip).WithZone(zone), nil
   340  }
   341  
   342  // AddrFromSlice parses the 4- or 16-byte byte slice as an IPv4 or IPv6 address.
   343  // Note that a net.IP can be passed directly as the []byte argument.
   344  // If slice's length is not 4 or 16, AddrFromSlice returns Addr{}, false.
   345  func AddrFromSlice(slice []byte) (ip Addr, ok bool) {
   346  	switch len(slice) {
   347  	case 4:
   348  		return AddrFrom4(*(*[4]byte)(slice)), true
   349  	case 16:
   350  		return ipv6Slice(slice), true
   351  	}
   352  	return Addr{}, false
   353  }
   354  
   355  // v4 returns the i'th byte of ip. If ip is not an IPv4, v4 returns
   356  // unspecified garbage.
   357  func (ip Addr) v4(i uint8) uint8 {
   358  	return uint8(ip.addr.lo >> ((3 - i) * 8))
   359  }
   360  
   361  // v6 returns the i'th byte of ip. If ip is an IPv4 address, this
   362  // accesses the IPv4-mapped IPv6 address form of the IP.
   363  func (ip Addr) v6(i uint8) uint8 {
   364  	return uint8(*(ip.addr.halves()[(i/8)%2]) >> ((7 - i%8) * 8))
   365  }
   366  
   367  // v6u16 returns the i'th 16-bit word of ip. If ip is an IPv4 address,
   368  // this accesses the IPv4-mapped IPv6 address form of the IP.
   369  func (ip Addr) v6u16(i uint8) uint16 {
   370  	return uint16(*(ip.addr.halves()[(i/4)%2]) >> ((3 - i%4) * 16))
   371  }
   372  
   373  // isZero reports whether ip is the zero value of the IP type.
   374  // The zero value is not a valid IP address of any type.
   375  //
   376  // Note that "0.0.0.0" and "::" are not the zero value. Use IsUnspecified to
   377  // check for these values instead.
   378  func (ip Addr) isZero() bool {
   379  	// Faster than comparing ip == Addr{}, but effectively equivalent,
   380  	// as there's no way to make an IP with a nil z from this package.
   381  	return ip.z == z0
   382  }
   383  
   384  // IsValid reports whether the Addr is an initialized address (not the zero Addr).
   385  //
   386  // Note that "0.0.0.0" and "::" are both valid values.
   387  func (ip Addr) IsValid() bool { return ip.z != z0 }
   388  
   389  // BitLen returns the number of bits in the IP address:
   390  // 128 for IPv6, 32 for IPv4, and 0 for the zero Addr.
   391  //
   392  // Note that IPv4-mapped IPv6 addresses are considered IPv6 addresses
   393  // and therefore have bit length 128.
   394  func (ip Addr) BitLen() int {
   395  	switch ip.z {
   396  	case z0:
   397  		return 0
   398  	case z4:
   399  		return 32
   400  	}
   401  	return 128
   402  }
   403  
   404  // Zone returns ip's IPv6 scoped addressing zone, if any.
   405  func (ip Addr) Zone() string {
   406  	if ip.z == nil {
   407  		return ""
   408  	}
   409  	zone, _ := ip.z.Get().(string)
   410  	return zone
   411  }
   412  
   413  // Compare returns an integer comparing two IPs.
   414  // The result will be 0 if ip == ip2, -1 if ip < ip2, and +1 if ip > ip2.
   415  // The definition of "less than" is the same as the Less method.
   416  func (ip Addr) Compare(ip2 Addr) int {
   417  	f1, f2 := ip.BitLen(), ip2.BitLen()
   418  	if f1 < f2 {
   419  		return -1
   420  	}
   421  	if f1 > f2 {
   422  		return 1
   423  	}
   424  	hi1, hi2 := ip.addr.hi, ip2.addr.hi
   425  	if hi1 < hi2 {
   426  		return -1
   427  	}
   428  	if hi1 > hi2 {
   429  		return 1
   430  	}
   431  	lo1, lo2 := ip.addr.lo, ip2.addr.lo
   432  	if lo1 < lo2 {
   433  		return -1
   434  	}
   435  	if lo1 > lo2 {
   436  		return 1
   437  	}
   438  	if ip.Is6() {
   439  		za, zb := ip.Zone(), ip2.Zone()
   440  		if za < zb {
   441  			return -1
   442  		}
   443  		if za > zb {
   444  			return 1
   445  		}
   446  	}
   447  	return 0
   448  }
   449  
   450  // Less reports whether ip sorts before ip2.
   451  // IP addresses sort first by length, then their address.
   452  // IPv6 addresses with zones sort just after the same address without a zone.
   453  func (ip Addr) Less(ip2 Addr) bool { return ip.Compare(ip2) == -1 }
   454  
   455  func (ip Addr) lessOrEq(ip2 Addr) bool { return ip.Compare(ip2) <= 0 }
   456  
   457  // Is4 reports whether ip is an IPv4 address.
   458  //
   459  // It returns false for IPv4-mapped IPv6 addresses. See Addr.Unmap.
   460  func (ip Addr) Is4() bool {
   461  	return ip.z == z4
   462  }
   463  
   464  // Is4In6 reports whether ip is an IPv4-mapped IPv6 address.
   465  func (ip Addr) Is4In6() bool {
   466  	return ip.Is6() && ip.addr.hi == 0 && ip.addr.lo>>32 == 0xffff
   467  }
   468  
   469  // Is6 reports whether ip is an IPv6 address, including IPv4-mapped
   470  // IPv6 addresses.
   471  func (ip Addr) Is6() bool {
   472  	return ip.z != z0 && ip.z != z4
   473  }
   474  
   475  // Unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
   476  //
   477  // That is, if ip is an IPv6 address wrapping an IPv4 address, it
   478  // returns the wrapped IPv4 address. Otherwise it returns ip unmodified.
   479  func (ip Addr) Unmap() Addr {
   480  	if ip.Is4In6() {
   481  		ip.z = z4
   482  	}
   483  	return ip
   484  }
   485  
   486  // WithZone returns an IP that's the same as ip but with the provided
   487  // zone. If zone is empty, the zone is removed. If ip is an IPv4
   488  // address, WithZone is a no-op and returns ip unchanged.
   489  func (ip Addr) WithZone(zone string) Addr {
   490  	if !ip.Is6() {
   491  		return ip
   492  	}
   493  	if zone == "" {
   494  		ip.z = z6noz
   495  		return ip
   496  	}
   497  	ip.z = intern.GetByString(zone)
   498  	return ip
   499  }
   500  
   501  // withoutZone unconditionally strips the zone from ip.
   502  // It's similar to WithZone, but small enough to be inlinable.
   503  func (ip Addr) withoutZone() Addr {
   504  	if !ip.Is6() {
   505  		return ip
   506  	}
   507  	ip.z = z6noz
   508  	return ip
   509  }
   510  
   511  // hasZone reports whether ip has an IPv6 zone.
   512  func (ip Addr) hasZone() bool {
   513  	return ip.z != z0 && ip.z != z4 && ip.z != z6noz
   514  }
   515  
   516  // IsLinkLocalUnicast reports whether ip is a link-local unicast address.
   517  func (ip Addr) IsLinkLocalUnicast() bool {
   518  	// Dynamic Configuration of IPv4 Link-Local Addresses
   519  	// https://datatracker.ietf.org/doc/html/rfc3927#section-2.1
   520  	if ip.Is4() {
   521  		return ip.v4(0) == 169 && ip.v4(1) == 254
   522  	}
   523  	// IP Version 6 Addressing Architecture (2.4 Address Type Identification)
   524  	// https://datatracker.ietf.org/doc/html/rfc4291#section-2.4
   525  	if ip.Is6() {
   526  		return ip.v6u16(0)&0xffc0 == 0xfe80
   527  	}
   528  	return false // zero value
   529  }
   530  
   531  // IsLoopback reports whether ip is a loopback address.
   532  func (ip Addr) IsLoopback() bool {
   533  	// Requirements for Internet Hosts -- Communication Layers (3.2.1.3 Addressing)
   534  	// https://datatracker.ietf.org/doc/html/rfc1122#section-3.2.1.3
   535  	if ip.Is4() {
   536  		return ip.v4(0) == 127
   537  	}
   538  	// IP Version 6 Addressing Architecture (2.4 Address Type Identification)
   539  	// https://datatracker.ietf.org/doc/html/rfc4291#section-2.4
   540  	if ip.Is6() {
   541  		return ip.addr.hi == 0 && ip.addr.lo == 1
   542  	}
   543  	return false // zero value
   544  }
   545  
   546  // IsMulticast reports whether ip is a multicast address.
   547  func (ip Addr) IsMulticast() bool {
   548  	// Host Extensions for IP Multicasting (4. HOST GROUP ADDRESSES)
   549  	// https://datatracker.ietf.org/doc/html/rfc1112#section-4
   550  	if ip.Is4() {
   551  		return ip.v4(0)&0xf0 == 0xe0
   552  	}
   553  	// IP Version 6 Addressing Architecture (2.4 Address Type Identification)
   554  	// https://datatracker.ietf.org/doc/html/rfc4291#section-2.4
   555  	if ip.Is6() {
   556  		return ip.addr.hi>>(64-8) == 0xff // ip.v6(0) == 0xff
   557  	}
   558  	return false // zero value
   559  }
   560  
   561  // IsInterfaceLocalMulticast reports whether ip is an IPv6 interface-local
   562  // multicast address.
   563  func (ip Addr) IsInterfaceLocalMulticast() bool {
   564  	// IPv6 Addressing Architecture (2.7.1. Pre-Defined Multicast Addresses)
   565  	// https://datatracker.ietf.org/doc/html/rfc4291#section-2.7.1
   566  	if ip.Is6() {
   567  		return ip.v6u16(0)&0xff0f == 0xff01
   568  	}
   569  	return false // zero value
   570  }
   571  
   572  // IsLinkLocalMulticast reports whether ip is a link-local multicast address.
   573  func (ip Addr) IsLinkLocalMulticast() bool {
   574  	// IPv4 Multicast Guidelines (4. Local Network Control Block (224.0.0/24))
   575  	// https://datatracker.ietf.org/doc/html/rfc5771#section-4
   576  	if ip.Is4() {
   577  		return ip.v4(0) == 224 && ip.v4(1) == 0 && ip.v4(2) == 0
   578  	}
   579  	// IPv6 Addressing Architecture (2.7.1. Pre-Defined Multicast Addresses)
   580  	// https://datatracker.ietf.org/doc/html/rfc4291#section-2.7.1
   581  	if ip.Is6() {
   582  		return ip.v6u16(0)&0xff0f == 0xff02
   583  	}
   584  	return false // zero value
   585  }
   586  
   587  // IsGlobalUnicast reports whether ip is a global unicast address.
   588  //
   589  // It returns true for IPv6 addresses which fall outside of the current
   590  // IANA-allocated 2000::/3 global unicast space, with the exception of the
   591  // link-local address space. It also returns true even if ip is in the IPv4
   592  // private address space or IPv6 unique local address space.
   593  // It returns false for the zero Addr.
   594  //
   595  // For reference, see RFC 1122, RFC 4291, and RFC 4632.
   596  func (ip Addr) IsGlobalUnicast() bool {
   597  	if ip.z == z0 {
   598  		// Invalid or zero-value.
   599  		return false
   600  	}
   601  
   602  	// Match package net's IsGlobalUnicast logic. Notably private IPv4 addresses
   603  	// and ULA IPv6 addresses are still considered "global unicast".
   604  	if ip.Is4() && (ip == IPv4Unspecified() || ip == AddrFrom4([4]byte{255, 255, 255, 255})) {
   605  		return false
   606  	}
   607  
   608  	return ip != IPv6Unspecified() &&
   609  		!ip.IsLoopback() &&
   610  		!ip.IsMulticast() &&
   611  		!ip.IsLinkLocalUnicast()
   612  }
   613  
   614  // IsPrivate reports whether ip is a private address, according to RFC 1918
   615  // (IPv4 addresses) and RFC 4193 (IPv6 addresses). That is, it reports whether
   616  // ip is in 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, or fc00::/7. This is the
   617  // same as net.IP.IsPrivate.
   618  func (ip Addr) IsPrivate() bool {
   619  	// Match the stdlib's IsPrivate logic.
   620  	if ip.Is4() {
   621  		// RFC 1918 allocates 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16 as
   622  		// private IPv4 address subnets.
   623  		return ip.v4(0) == 10 ||
   624  			(ip.v4(0) == 172 && ip.v4(1)&0xf0 == 16) ||
   625  			(ip.v4(0) == 192 && ip.v4(1) == 168)
   626  	}
   627  
   628  	if ip.Is6() {
   629  		// RFC 4193 allocates fc00::/7 as the unique local unicast IPv6 address
   630  		// subnet.
   631  		return ip.v6(0)&0xfe == 0xfc
   632  	}
   633  
   634  	return false // zero value
   635  }
   636  
   637  // IsUnspecified reports whether ip is an unspecified address, either the IPv4
   638  // address "0.0.0.0" or the IPv6 address "::".
   639  //
   640  // Note that the zero Addr is not an unspecified address.
   641  func (ip Addr) IsUnspecified() bool {
   642  	return ip == IPv4Unspecified() || ip == IPv6Unspecified()
   643  }
   644  
   645  // Prefix keeps only the top b bits of IP, producing a Prefix
   646  // of the specified length.
   647  // If ip is a zero Addr, Prefix always returns a zero Prefix and a nil error.
   648  // Otherwise, if bits is less than zero or greater than ip.BitLen(),
   649  // Prefix returns an error.
   650  func (ip Addr) Prefix(b int) (Prefix, error) {
   651  	if b < 0 {
   652  		return Prefix{}, errors.New("negative Prefix bits")
   653  	}
   654  	effectiveBits := b
   655  	switch ip.z {
   656  	case z0:
   657  		return Prefix{}, nil
   658  	case z4:
   659  		if b > 32 {
   660  			return Prefix{}, errors.New("prefix length " + itoa.Itoa(b) + " too large for IPv4")
   661  		}
   662  		effectiveBits += 96
   663  	default:
   664  		if b > 128 {
   665  			return Prefix{}, errors.New("prefix length " + itoa.Itoa(b) + " too large for IPv6")
   666  		}
   667  	}
   668  	ip.addr = ip.addr.and(mask6(effectiveBits))
   669  	return PrefixFrom(ip, b), nil
   670  }
   671  
   672  const (
   673  	netIPv4len = 4
   674  	netIPv6len = 16
   675  )
   676  
   677  // As16 returns the IP address in its 16-byte representation.
   678  // IPv4 addresses are returned as IPv4-mapped IPv6 addresses.
   679  // IPv6 addresses with zones are returned without their zone (use the
   680  // Zone method to get it).
   681  // The ip zero value returns all zeroes.
   682  func (ip Addr) As16() (a16 [16]byte) {
   683  	bePutUint64(a16[:8], ip.addr.hi)
   684  	bePutUint64(a16[8:], ip.addr.lo)
   685  	return a16
   686  }
   687  
   688  // As4 returns an IPv4 or IPv4-in-IPv6 address in its 4-byte representation.
   689  // If ip is the zero Addr or an IPv6 address, As4 panics.
   690  // Note that 0.0.0.0 is not the zero Addr.
   691  func (ip Addr) As4() (a4 [4]byte) {
   692  	if ip.z == z4 || ip.Is4In6() {
   693  		bePutUint32(a4[:], uint32(ip.addr.lo))
   694  		return a4
   695  	}
   696  	if ip.z == z0 {
   697  		panic("As4 called on IP zero value")
   698  	}
   699  	panic("As4 called on IPv6 address")
   700  }
   701  
   702  // AsSlice returns an IPv4 or IPv6 address in its respective 4-byte or 16-byte representation.
   703  func (ip Addr) AsSlice() []byte {
   704  	switch ip.z {
   705  	case z0:
   706  		return nil
   707  	case z4:
   708  		var ret [4]byte
   709  		bePutUint32(ret[:], uint32(ip.addr.lo))
   710  		return ret[:]
   711  	default:
   712  		var ret [16]byte
   713  		bePutUint64(ret[:8], ip.addr.hi)
   714  		bePutUint64(ret[8:], ip.addr.lo)
   715  		return ret[:]
   716  	}
   717  }
   718  
   719  // Next returns the address following ip.
   720  // If there is none, it returns the zero Addr.
   721  func (ip Addr) Next() Addr {
   722  	ip.addr = ip.addr.addOne()
   723  	if ip.Is4() {
   724  		if uint32(ip.addr.lo) == 0 {
   725  			// Overflowed.
   726  			return Addr{}
   727  		}
   728  	} else {
   729  		if ip.addr.isZero() {
   730  			// Overflowed
   731  			return Addr{}
   732  		}
   733  	}
   734  	return ip
   735  }
   736  
   737  // Prev returns the IP before ip.
   738  // If there is none, it returns the IP zero value.
   739  func (ip Addr) Prev() Addr {
   740  	if ip.Is4() {
   741  		if uint32(ip.addr.lo) == 0 {
   742  			return Addr{}
   743  		}
   744  	} else if ip.addr.isZero() {
   745  		return Addr{}
   746  	}
   747  	ip.addr = ip.addr.subOne()
   748  	return ip
   749  }
   750  
   751  // String returns the string form of the IP address ip.
   752  // It returns one of 5 forms:
   753  //
   754  //   - "invalid IP", if ip is the zero Addr
   755  //   - IPv4 dotted decimal ("192.0.2.1")
   756  //   - IPv6 ("2001:db8::1")
   757  //   - "::ffff:1.2.3.4" (if Is4In6)
   758  //   - IPv6 with zone ("fe80:db8::1%eth0")
   759  //
   760  // Note that unlike package net's IP.String method,
   761  // IPv4-mapped IPv6 addresses format with a "::ffff:"
   762  // prefix before the dotted quad.
   763  func (ip Addr) String() string {
   764  	switch ip.z {
   765  	case z0:
   766  		return "invalid IP"
   767  	case z4:
   768  		return ip.string4()
   769  	default:
   770  		if ip.Is4In6() {
   771  			if z := ip.Zone(); z != "" {
   772  				return "::ffff:" + ip.Unmap().string4() + "%" + z
   773  			} else {
   774  				return "::ffff:" + ip.Unmap().string4()
   775  			}
   776  		}
   777  		return ip.string6()
   778  	}
   779  }
   780  
   781  // AppendTo appends a text encoding of ip,
   782  // as generated by MarshalText,
   783  // to b and returns the extended buffer.
   784  func (ip Addr) AppendTo(b []byte) []byte {
   785  	switch ip.z {
   786  	case z0:
   787  		return b
   788  	case z4:
   789  		return ip.appendTo4(b)
   790  	default:
   791  		if ip.Is4In6() {
   792  			b = append(b, "::ffff:"...)
   793  			b = ip.Unmap().appendTo4(b)
   794  			if z := ip.Zone(); z != "" {
   795  				b = append(b, '%')
   796  				b = append(b, z...)
   797  			}
   798  			return b
   799  		}
   800  		return ip.appendTo6(b)
   801  	}
   802  }
   803  
   804  // digits is a string of the hex digits from 0 to f. It's used in
   805  // appendDecimal and appendHex to format IP addresses.
   806  const digits = "0123456789abcdef"
   807  
   808  // appendDecimal appends the decimal string representation of x to b.
   809  func appendDecimal(b []byte, x uint8) []byte {
   810  	// Using this function rather than strconv.AppendUint makes IPv4
   811  	// string building 2x faster.
   812  
   813  	if x >= 100 {
   814  		b = append(b, digits[x/100])
   815  	}
   816  	if x >= 10 {
   817  		b = append(b, digits[x/10%10])
   818  	}
   819  	return append(b, digits[x%10])
   820  }
   821  
   822  // appendHex appends the hex string representation of x to b.
   823  func appendHex(b []byte, x uint16) []byte {
   824  	// Using this function rather than strconv.AppendUint makes IPv6
   825  	// string building 2x faster.
   826  
   827  	if x >= 0x1000 {
   828  		b = append(b, digits[x>>12])
   829  	}
   830  	if x >= 0x100 {
   831  		b = append(b, digits[x>>8&0xf])
   832  	}
   833  	if x >= 0x10 {
   834  		b = append(b, digits[x>>4&0xf])
   835  	}
   836  	return append(b, digits[x&0xf])
   837  }
   838  
   839  // appendHexPad appends the fully padded hex string representation of x to b.
   840  func appendHexPad(b []byte, x uint16) []byte {
   841  	return append(b, digits[x>>12], digits[x>>8&0xf], digits[x>>4&0xf], digits[x&0xf])
   842  }
   843  
   844  func (ip Addr) string4() string {
   845  	const max = len("255.255.255.255")
   846  	ret := make([]byte, 0, max)
   847  	ret = ip.appendTo4(ret)
   848  	return string(ret)
   849  }
   850  
   851  func (ip Addr) appendTo4(ret []byte) []byte {
   852  	ret = appendDecimal(ret, ip.v4(0))
   853  	ret = append(ret, '.')
   854  	ret = appendDecimal(ret, ip.v4(1))
   855  	ret = append(ret, '.')
   856  	ret = appendDecimal(ret, ip.v4(2))
   857  	ret = append(ret, '.')
   858  	ret = appendDecimal(ret, ip.v4(3))
   859  	return ret
   860  }
   861  
   862  // string6 formats ip in IPv6 textual representation. It follows the
   863  // guidelines in section 4 of RFC 5952
   864  // (https://tools.ietf.org/html/rfc5952#section-4): no unnecessary
   865  // zeros, use :: to elide the longest run of zeros, and don't use ::
   866  // to compact a single zero field.
   867  func (ip Addr) string6() string {
   868  	// Use a zone with a "plausibly long" name, so that most zone-ful
   869  	// IP addresses won't require additional allocation.
   870  	//
   871  	// The compiler does a cool optimization here, where ret ends up
   872  	// stack-allocated and so the only allocation this function does
   873  	// is to construct the returned string. As such, it's okay to be a
   874  	// bit greedy here, size-wise.
   875  	const max = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0")
   876  	ret := make([]byte, 0, max)
   877  	ret = ip.appendTo6(ret)
   878  	return string(ret)
   879  }
   880  
   881  func (ip Addr) appendTo6(ret []byte) []byte {
   882  	zeroStart, zeroEnd := uint8(255), uint8(255)
   883  	for i := uint8(0); i < 8; i++ {
   884  		j := i
   885  		for j < 8 && ip.v6u16(j) == 0 {
   886  			j++
   887  		}
   888  		if l := j - i; l >= 2 && l > zeroEnd-zeroStart {
   889  			zeroStart, zeroEnd = i, j
   890  		}
   891  	}
   892  
   893  	for i := uint8(0); i < 8; i++ {
   894  		if i == zeroStart {
   895  			ret = append(ret, ':', ':')
   896  			i = zeroEnd
   897  			if i >= 8 {
   898  				break
   899  			}
   900  		} else if i > 0 {
   901  			ret = append(ret, ':')
   902  		}
   903  
   904  		ret = appendHex(ret, ip.v6u16(i))
   905  	}
   906  
   907  	if ip.z != z6noz {
   908  		ret = append(ret, '%')
   909  		ret = append(ret, ip.Zone()...)
   910  	}
   911  	return ret
   912  }
   913  
   914  // StringExpanded is like String but IPv6 addresses are expanded with leading
   915  // zeroes and no "::" compression. For example, "2001:db8::1" becomes
   916  // "2001:0db8:0000:0000:0000:0000:0000:0001".
   917  func (ip Addr) StringExpanded() string {
   918  	switch ip.z {
   919  	case z0, z4:
   920  		return ip.String()
   921  	}
   922  
   923  	const size = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
   924  	ret := make([]byte, 0, size)
   925  	for i := uint8(0); i < 8; i++ {
   926  		if i > 0 {
   927  			ret = append(ret, ':')
   928  		}
   929  
   930  		ret = appendHexPad(ret, ip.v6u16(i))
   931  	}
   932  
   933  	if ip.z != z6noz {
   934  		// The addition of a zone will cause a second allocation, but when there
   935  		// is no zone the ret slice will be stack allocated.
   936  		ret = append(ret, '%')
   937  		ret = append(ret, ip.Zone()...)
   938  	}
   939  	return string(ret)
   940  }
   941  
   942  // MarshalText implements the encoding.TextMarshaler interface,
   943  // The encoding is the same as returned by String, with one exception:
   944  // If ip is the zero Addr, the encoding is the empty string.
   945  func (ip Addr) MarshalText() ([]byte, error) {
   946  	switch ip.z {
   947  	case z0:
   948  		return []byte(""), nil
   949  	case z4:
   950  		max := len("255.255.255.255")
   951  		b := make([]byte, 0, max)
   952  		return ip.appendTo4(b), nil
   953  	default:
   954  		max := len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0")
   955  		b := make([]byte, 0, max)
   956  		if ip.Is4In6() {
   957  			b = append(b, "::ffff:"...)
   958  			b = ip.Unmap().appendTo4(b)
   959  			if z := ip.Zone(); z != "" {
   960  				b = append(b, '%')
   961  				b = append(b, z...)
   962  			}
   963  			return b, nil
   964  		}
   965  		return ip.appendTo6(b), nil
   966  	}
   967  
   968  }
   969  
   970  // UnmarshalText implements the encoding.TextUnmarshaler interface.
   971  // The IP address is expected in a form accepted by ParseAddr.
   972  //
   973  // If text is empty, UnmarshalText sets *ip to the zero Addr and
   974  // returns no error.
   975  func (ip *Addr) UnmarshalText(text []byte) error {
   976  	if len(text) == 0 {
   977  		*ip = Addr{}
   978  		return nil
   979  	}
   980  	var err error
   981  	*ip, err = ParseAddr(string(text))
   982  	return err
   983  }
   984  
   985  func (ip Addr) marshalBinaryWithTrailingBytes(trailingBytes int) []byte {
   986  	var b []byte
   987  	switch ip.z {
   988  	case z0:
   989  		b = make([]byte, trailingBytes)
   990  	case z4:
   991  		b = make([]byte, 4+trailingBytes)
   992  		bePutUint32(b, uint32(ip.addr.lo))
   993  	default:
   994  		z := ip.Zone()
   995  		b = make([]byte, 16+len(z)+trailingBytes)
   996  		bePutUint64(b[:8], ip.addr.hi)
   997  		bePutUint64(b[8:], ip.addr.lo)
   998  		copy(b[16:], z)
   999  	}
  1000  	return b
  1001  }
  1002  
  1003  // MarshalBinary implements the encoding.BinaryMarshaler interface.
  1004  // It returns a zero-length slice for the zero Addr,
  1005  // the 4-byte form for an IPv4 address,
  1006  // and the 16-byte form with zone appended for an IPv6 address.
  1007  func (ip Addr) MarshalBinary() ([]byte, error) {
  1008  	return ip.marshalBinaryWithTrailingBytes(0), nil
  1009  }
  1010  
  1011  // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
  1012  // It expects data in the form generated by MarshalBinary.
  1013  func (ip *Addr) UnmarshalBinary(b []byte) error {
  1014  	n := len(b)
  1015  	switch {
  1016  	case n == 0:
  1017  		*ip = Addr{}
  1018  		return nil
  1019  	case n == 4:
  1020  		*ip = AddrFrom4(*(*[4]byte)(b))
  1021  		return nil
  1022  	case n == 16:
  1023  		*ip = ipv6Slice(b)
  1024  		return nil
  1025  	case n > 16:
  1026  		*ip = ipv6Slice(b[:16]).WithZone(string(b[16:]))
  1027  		return nil
  1028  	}
  1029  	return errors.New("unexpected slice size")
  1030  }
  1031  
  1032  // AddrPort is an IP and a port number.
  1033  type AddrPort struct {
  1034  	ip   Addr
  1035  	port uint16
  1036  }
  1037  
  1038  // AddrPortFrom returns an AddrPort with the provided IP and port.
  1039  // It does not allocate.
  1040  func AddrPortFrom(ip Addr, port uint16) AddrPort { return AddrPort{ip: ip, port: port} }
  1041  
  1042  // Addr returns p's IP address.
  1043  func (p AddrPort) Addr() Addr { return p.ip }
  1044  
  1045  // Port returns p's port.
  1046  func (p AddrPort) Port() uint16 { return p.port }
  1047  
  1048  // splitAddrPort splits s into an IP address string and a port
  1049  // string. It splits strings shaped like "foo:bar" or "[foo]:bar",
  1050  // without further validating the substrings. v6 indicates whether the
  1051  // ip string should parse as an IPv6 address or an IPv4 address, in
  1052  // order for s to be a valid ip:port string.
  1053  func splitAddrPort(s string) (ip, port string, v6 bool, err error) {
  1054  	i := stringsLastIndexByte(s, ':')
  1055  	if i == -1 {
  1056  		return "", "", false, errors.New("not an ip:port")
  1057  	}
  1058  
  1059  	ip, port = s[:i], s[i+1:]
  1060  	if len(ip) == 0 {
  1061  		return "", "", false, errors.New("no IP")
  1062  	}
  1063  	if len(port) == 0 {
  1064  		return "", "", false, errors.New("no port")
  1065  	}
  1066  	if ip[0] == '[' {
  1067  		if len(ip) < 2 || ip[len(ip)-1] != ']' {
  1068  			return "", "", false, errors.New("missing ]")
  1069  		}
  1070  		ip = ip[1 : len(ip)-1]
  1071  		v6 = true
  1072  	}
  1073  
  1074  	return ip, port, v6, nil
  1075  }
  1076  
  1077  // ParseAddrPort parses s as an AddrPort.
  1078  //
  1079  // It doesn't do any name resolution: both the address and the port
  1080  // must be numeric.
  1081  func ParseAddrPort(s string) (AddrPort, error) {
  1082  	var ipp AddrPort
  1083  	ip, port, v6, err := splitAddrPort(s)
  1084  	if err != nil {
  1085  		return ipp, err
  1086  	}
  1087  	port16, err := strconv.ParseUint(port, 10, 16)
  1088  	if err != nil {
  1089  		return ipp, errors.New("invalid port " + strconv.Quote(port) + " parsing " + strconv.Quote(s))
  1090  	}
  1091  	ipp.port = uint16(port16)
  1092  	ipp.ip, err = ParseAddr(ip)
  1093  	if err != nil {
  1094  		return AddrPort{}, err
  1095  	}
  1096  	if v6 && ipp.ip.Is4() {
  1097  		return AddrPort{}, errors.New("invalid ip:port " + strconv.Quote(s) + ", square brackets can only be used with IPv6 addresses")
  1098  	} else if !v6 && ipp.ip.Is6() {
  1099  		return AddrPort{}, errors.New("invalid ip:port " + strconv.Quote(s) + ", IPv6 addresses must be surrounded by square brackets")
  1100  	}
  1101  	return ipp, nil
  1102  }
  1103  
  1104  // MustParseAddrPort calls ParseAddrPort(s) and panics on error.
  1105  // It is intended for use in tests with hard-coded strings.
  1106  func MustParseAddrPort(s string) AddrPort {
  1107  	ip, err := ParseAddrPort(s)
  1108  	if err != nil {
  1109  		panic(err)
  1110  	}
  1111  	return ip
  1112  }
  1113  
  1114  // isZero reports whether p is the zero AddrPort.
  1115  func (p AddrPort) isZero() bool { return p == AddrPort{} }
  1116  
  1117  // IsValid reports whether p.IP() is valid.
  1118  // All ports are valid, including zero.
  1119  func (p AddrPort) IsValid() bool { return p.ip.IsValid() }
  1120  
  1121  func (p AddrPort) String() string {
  1122  	switch p.ip.z {
  1123  	case z0:
  1124  		return "invalid AddrPort"
  1125  	case z4:
  1126  		a := p.ip.As4()
  1127  		buf := make([]byte, 0, 21)
  1128  		for i := range a {
  1129  			buf = strconv.AppendUint(buf, uint64(a[i]), 10)
  1130  			buf = append(buf, "...:"[i])
  1131  		}
  1132  		buf = strconv.AppendUint(buf, uint64(p.port), 10)
  1133  		return string(buf)
  1134  	default:
  1135  		// TODO: this could be more efficient allocation-wise:
  1136  		return joinHostPort(p.ip.String(), itoa.Itoa(int(p.port)))
  1137  	}
  1138  }
  1139  
  1140  func joinHostPort(host, port string) string {
  1141  	// We assume that host is a literal IPv6 address if host has
  1142  	// colons.
  1143  	if bytealg.IndexByteString(host, ':') >= 0 {
  1144  		return "[" + host + "]:" + port
  1145  	}
  1146  	return host + ":" + port
  1147  }
  1148  
  1149  // AppendTo appends a text encoding of p,
  1150  // as generated by MarshalText,
  1151  // to b and returns the extended buffer.
  1152  func (p AddrPort) AppendTo(b []byte) []byte {
  1153  	switch p.ip.z {
  1154  	case z0:
  1155  		return b
  1156  	case z4:
  1157  		b = p.ip.appendTo4(b)
  1158  	default:
  1159  		if p.ip.Is4In6() {
  1160  			b = append(b, "[::ffff:"...)
  1161  			b = p.ip.Unmap().appendTo4(b)
  1162  			if z := p.ip.Zone(); z != "" {
  1163  				b = append(b, '%')
  1164  				b = append(b, z...)
  1165  			}
  1166  		} else {
  1167  			b = append(b, '[')
  1168  			b = p.ip.appendTo6(b)
  1169  		}
  1170  		b = append(b, ']')
  1171  	}
  1172  	b = append(b, ':')
  1173  	b = strconv.AppendUint(b, uint64(p.port), 10)
  1174  	return b
  1175  }
  1176  
  1177  // MarshalText implements the encoding.TextMarshaler interface. The
  1178  // encoding is the same as returned by String, with one exception: if
  1179  // p.Addr() is the zero Addr, the encoding is the empty string.
  1180  func (p AddrPort) MarshalText() ([]byte, error) {
  1181  	var max int
  1182  	switch p.ip.z {
  1183  	case z0:
  1184  	case z4:
  1185  		max = len("255.255.255.255:65535")
  1186  	default:
  1187  		max = len("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0]:65535")
  1188  	}
  1189  	b := make([]byte, 0, max)
  1190  	b = p.AppendTo(b)
  1191  	return b, nil
  1192  }
  1193  
  1194  // UnmarshalText implements the encoding.TextUnmarshaler
  1195  // interface. The AddrPort is expected in a form
  1196  // generated by MarshalText or accepted by ParseAddrPort.
  1197  func (p *AddrPort) UnmarshalText(text []byte) error {
  1198  	if len(text) == 0 {
  1199  		*p = AddrPort{}
  1200  		return nil
  1201  	}
  1202  	var err error
  1203  	*p, err = ParseAddrPort(string(text))
  1204  	return err
  1205  }
  1206  
  1207  // MarshalBinary implements the encoding.BinaryMarshaler interface.
  1208  // It returns Addr.MarshalBinary with an additional two bytes appended
  1209  // containing the port in little-endian.
  1210  func (p AddrPort) MarshalBinary() ([]byte, error) {
  1211  	b := p.Addr().marshalBinaryWithTrailingBytes(2)
  1212  	lePutUint16(b[len(b)-2:], p.Port())
  1213  	return b, nil
  1214  }
  1215  
  1216  // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
  1217  // It expects data in the form generated by MarshalBinary.
  1218  func (p *AddrPort) UnmarshalBinary(b []byte) error {
  1219  	if len(b) < 2 {
  1220  		return errors.New("unexpected slice size")
  1221  	}
  1222  	var addr Addr
  1223  	err := addr.UnmarshalBinary(b[:len(b)-2])
  1224  	if err != nil {
  1225  		return err
  1226  	}
  1227  	*p = AddrPortFrom(addr, leUint16(b[len(b)-2:]))
  1228  	return nil
  1229  }
  1230  
  1231  // Prefix is an IP address prefix (CIDR) representing an IP network.
  1232  //
  1233  // The first Bits() of Addr() are specified. The remaining bits match any address.
  1234  // The range of Bits() is [0,32] for IPv4 or [0,128] for IPv6.
  1235  type Prefix struct {
  1236  	ip Addr
  1237  
  1238  	// bits is logically a uint8 (storing [0,128]) but also
  1239  	// encodes an "invalid" bit, currently represented by the
  1240  	// invalidPrefixBits sentinel value. It could be packed into
  1241  	// the uint8 more with more complicated expressions in the
  1242  	// accessors, but the extra byte (in padding anyway) doesn't
  1243  	// hurt and simplifies code below.
  1244  	bits int16
  1245  }
  1246  
  1247  // invalidPrefixBits is the Prefix.bits value used when PrefixFrom is
  1248  // outside the range of a uint8. It's returned as the int -1 in the
  1249  // public API.
  1250  const invalidPrefixBits = -1
  1251  
  1252  // PrefixFrom returns a Prefix with the provided IP address and bit
  1253  // prefix length.
  1254  //
  1255  // It does not allocate. Unlike Addr.Prefix, PrefixFrom does not mask
  1256  // off the host bits of ip.
  1257  //
  1258  // If bits is less than zero or greater than ip.BitLen, Prefix.Bits
  1259  // will return an invalid value -1.
  1260  func PrefixFrom(ip Addr, bits int) Prefix {
  1261  	if bits < 0 || bits > ip.BitLen() {
  1262  		bits = invalidPrefixBits
  1263  	}
  1264  	b16 := int16(bits)
  1265  	return Prefix{
  1266  		ip:   ip.withoutZone(),
  1267  		bits: b16,
  1268  	}
  1269  }
  1270  
  1271  // Addr returns p's IP address.
  1272  func (p Prefix) Addr() Addr { return p.ip }
  1273  
  1274  // Bits returns p's prefix length.
  1275  //
  1276  // It reports -1 if invalid.
  1277  func (p Prefix) Bits() int { return int(p.bits) }
  1278  
  1279  // IsValid reports whether p.Bits() has a valid range for p.IP().
  1280  // If p.Addr() is the zero Addr, IsValid returns false.
  1281  // Note that if p is the zero Prefix, then p.IsValid() == false.
  1282  func (p Prefix) IsValid() bool { return !p.ip.isZero() && p.bits >= 0 && int(p.bits) <= p.ip.BitLen() }
  1283  
  1284  func (p Prefix) isZero() bool { return p == Prefix{} }
  1285  
  1286  // IsSingleIP reports whether p contains exactly one IP.
  1287  func (p Prefix) IsSingleIP() bool { return p.bits != 0 && int(p.bits) == p.ip.BitLen() }
  1288  
  1289  // ParsePrefix parses s as an IP address prefix.
  1290  // The string can be in the form "192.168.1.0/24" or "2001:db8::/32",
  1291  // the CIDR notation defined in RFC 4632 and RFC 4291.
  1292  // IPv6 zones are not permitted in prefixes, and an error will be returned if a
  1293  // zone is present.
  1294  //
  1295  // Note that masked address bits are not zeroed. Use Masked for that.
  1296  func ParsePrefix(s string) (Prefix, error) {
  1297  	i := stringsLastIndexByte(s, '/')
  1298  	if i < 0 {
  1299  		return Prefix{}, errors.New("netip.ParsePrefix(" + strconv.Quote(s) + "): no '/'")
  1300  	}
  1301  	ip, err := ParseAddr(s[:i])
  1302  	if err != nil {
  1303  		return Prefix{}, errors.New("netip.ParsePrefix(" + strconv.Quote(s) + "): " + err.Error())
  1304  	}
  1305  	// IPv6 zones are not allowed: https://go.dev/issue/51899
  1306  	if ip.Is6() && ip.z != z6noz {
  1307  		return Prefix{}, errors.New("netip.ParsePrefix(" + strconv.Quote(s) + "): IPv6 zones cannot be present in a prefix")
  1308  	}
  1309  
  1310  	bitsStr := s[i+1:]
  1311  	bits, err := strconv.Atoi(bitsStr)
  1312  	if err != nil {
  1313  		return Prefix{}, errors.New("netip.ParsePrefix(" + strconv.Quote(s) + "): bad bits after slash: " + strconv.Quote(bitsStr))
  1314  	}
  1315  	maxBits := 32
  1316  	if ip.Is6() {
  1317  		maxBits = 128
  1318  	}
  1319  	if bits < 0 || bits > maxBits {
  1320  		return Prefix{}, errors.New("netip.ParsePrefix(" + strconv.Quote(s) + "): prefix length out of range")
  1321  	}
  1322  	return PrefixFrom(ip, bits), nil
  1323  }
  1324  
  1325  // MustParsePrefix calls ParsePrefix(s) and panics on error.
  1326  // It is intended for use in tests with hard-coded strings.
  1327  func MustParsePrefix(s string) Prefix {
  1328  	ip, err := ParsePrefix(s)
  1329  	if err != nil {
  1330  		panic(err)
  1331  	}
  1332  	return ip
  1333  }
  1334  
  1335  // Masked returns p in its canonical form, with all but the high
  1336  // p.Bits() bits of p.Addr() masked off.
  1337  //
  1338  // If p is zero or otherwise invalid, Masked returns the zero Prefix.
  1339  func (p Prefix) Masked() Prefix {
  1340  	if m, err := p.ip.Prefix(int(p.bits)); err == nil {
  1341  		return m
  1342  	}
  1343  	return Prefix{}
  1344  }
  1345  
  1346  // Contains reports whether the network p includes ip.
  1347  //
  1348  // An IPv4 address will not match an IPv6 prefix.
  1349  // An IPv4-mapped IPv6 address will not match an IPv4 prefix.
  1350  // A zero-value IP will not match any prefix.
  1351  // If ip has an IPv6 zone, Contains returns false,
  1352  // because Prefixes strip zones.
  1353  func (p Prefix) Contains(ip Addr) bool {
  1354  	if !p.IsValid() || ip.hasZone() {
  1355  		return false
  1356  	}
  1357  	if f1, f2 := p.ip.BitLen(), ip.BitLen(); f1 == 0 || f2 == 0 || f1 != f2 {
  1358  		return false
  1359  	}
  1360  	if ip.Is4() {
  1361  		// xor the IP addresses together; mismatched bits are now ones.
  1362  		// Shift away the number of bits we don't care about.
  1363  		// Shifts in Go are more efficient if the compiler can prove
  1364  		// that the shift amount is smaller than the width of the shifted type (64 here).
  1365  		// We know that p.bits is in the range 0..32 because p is Valid;
  1366  		// the compiler doesn't know that, so mask with 63 to help it.
  1367  		// Now truncate to 32 bits, because this is IPv4.
  1368  		// If all the bits we care about are equal, the result will be zero.
  1369  		return uint32((ip.addr.lo^p.ip.addr.lo)>>((32-p.bits)&63)) == 0
  1370  	} else {
  1371  		// xor the IP addresses together.
  1372  		// Mask away the bits we don't care about.
  1373  		// If all the bits we care about are equal, the result will be zero.
  1374  		return ip.addr.xor(p.ip.addr).and(mask6(int(p.bits))).isZero()
  1375  	}
  1376  }
  1377  
  1378  // Overlaps reports whether p and o contain any IP addresses in common.
  1379  //
  1380  // If p and o are of different address families or either have a zero
  1381  // IP, it reports false. Like the Contains method, a prefix with an
  1382  // IPv4-mapped IPv6 address is still treated as an IPv6 mask.
  1383  func (p Prefix) Overlaps(o Prefix) bool {
  1384  	if !p.IsValid() || !o.IsValid() {
  1385  		return false
  1386  	}
  1387  	if p == o {
  1388  		return true
  1389  	}
  1390  	if p.ip.Is4() != o.ip.Is4() {
  1391  		return false
  1392  	}
  1393  	var minBits int16
  1394  	if p.bits < o.bits {
  1395  		minBits = p.bits
  1396  	} else {
  1397  		minBits = o.bits
  1398  	}
  1399  	if minBits == 0 {
  1400  		return true
  1401  	}
  1402  	// One of these Prefix calls might look redundant, but we don't require
  1403  	// that p and o values are normalized (via Prefix.Masked) first,
  1404  	// so the Prefix call on the one that's already minBits serves to zero
  1405  	// out any remaining bits in IP.
  1406  	var err error
  1407  	if p, err = p.ip.Prefix(int(minBits)); err != nil {
  1408  		return false
  1409  	}
  1410  	if o, err = o.ip.Prefix(int(minBits)); err != nil {
  1411  		return false
  1412  	}
  1413  	return p.ip == o.ip
  1414  }
  1415  
  1416  // AppendTo appends a text encoding of p,
  1417  // as generated by MarshalText,
  1418  // to b and returns the extended buffer.
  1419  func (p Prefix) AppendTo(b []byte) []byte {
  1420  	if p.isZero() {
  1421  		return b
  1422  	}
  1423  	if !p.IsValid() {
  1424  		return append(b, "invalid Prefix"...)
  1425  	}
  1426  
  1427  	// p.ip is non-nil, because p is valid.
  1428  	if p.ip.z == z4 {
  1429  		b = p.ip.appendTo4(b)
  1430  	} else {
  1431  		if p.ip.Is4In6() {
  1432  			b = append(b, "::ffff:"...)
  1433  			b = p.ip.Unmap().appendTo4(b)
  1434  		} else {
  1435  			b = p.ip.appendTo6(b)
  1436  		}
  1437  	}
  1438  
  1439  	b = append(b, '/')
  1440  	b = appendDecimal(b, uint8(p.bits))
  1441  	return b
  1442  }
  1443  
  1444  // MarshalText implements the encoding.TextMarshaler interface,
  1445  // The encoding is the same as returned by String, with one exception:
  1446  // If p is the zero value, the encoding is the empty string.
  1447  func (p Prefix) MarshalText() ([]byte, error) {
  1448  	var max int
  1449  	switch p.ip.z {
  1450  	case z0:
  1451  	case z4:
  1452  		max = len("255.255.255.255/32")
  1453  	default:
  1454  		max = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0/128")
  1455  	}
  1456  	b := make([]byte, 0, max)
  1457  	b = p.AppendTo(b)
  1458  	return b, nil
  1459  }
  1460  
  1461  // UnmarshalText implements the encoding.TextUnmarshaler interface.
  1462  // The IP address is expected in a form accepted by ParsePrefix
  1463  // or generated by MarshalText.
  1464  func (p *Prefix) UnmarshalText(text []byte) error {
  1465  	if len(text) == 0 {
  1466  		*p = Prefix{}
  1467  		return nil
  1468  	}
  1469  	var err error
  1470  	*p, err = ParsePrefix(string(text))
  1471  	return err
  1472  }
  1473  
  1474  // MarshalBinary implements the encoding.BinaryMarshaler interface.
  1475  // It returns Addr.MarshalBinary with an additional byte appended
  1476  // containing the prefix bits.
  1477  func (p Prefix) MarshalBinary() ([]byte, error) {
  1478  	b := p.Addr().withoutZone().marshalBinaryWithTrailingBytes(1)
  1479  	b[len(b)-1] = uint8(p.Bits())
  1480  	return b, nil
  1481  }
  1482  
  1483  // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
  1484  // It expects data in the form generated by MarshalBinary.
  1485  func (p *Prefix) UnmarshalBinary(b []byte) error {
  1486  	if len(b) < 1 {
  1487  		return errors.New("unexpected slice size")
  1488  	}
  1489  	var addr Addr
  1490  	err := addr.UnmarshalBinary(b[:len(b)-1])
  1491  	if err != nil {
  1492  		return err
  1493  	}
  1494  	*p = PrefixFrom(addr, int(b[len(b)-1]))
  1495  	return nil
  1496  }
  1497  
  1498  // String returns the CIDR notation of p: "<ip>/<bits>".
  1499  func (p Prefix) String() string {
  1500  	if !p.IsValid() {
  1501  		return "invalid Prefix"
  1502  	}
  1503  	return p.ip.String() + "/" + itoa.Itoa(int(p.bits))
  1504  }
  1505  

View as plain text