| 1 |
// Copyright 2020 The Inet.Af 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 netipx contains code and types that were left behind when |
| 6 |
// the old inet.af/netaddr package moved to the standard library in Go |
| 7 |
// 1.18 as net/netip. |
| 8 |
package netipx // import "go4.org/netipx" |
| 9 |
|
| 10 |
import ( |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"math" |
| 14 |
"net" |
| 15 |
"net/netip" |
| 16 |
"sort" |
| 17 |
"strings" |
| 18 |
) |
| 19 |
|
| 20 |
// FromStdIP returns an IP from the standard library's IP type. |
| 21 |
// |
| 22 |
// If std is invalid, ok is false. |
| 23 |
// |
| 24 |
// FromStdIP implicitly unmaps IPv6-mapped IPv4 addresses. That is, if |
| 25 |
// len(std) == 16 and contains an IPv4 address, only the IPv4 part is |
| 26 |
// returned, without the IPv6 wrapper. This is the common form returned by |
| 27 |
// the standard library's ParseIP: https://play.golang.org/p/qdjylUkKWxl. |
| 28 |
// To convert a standard library IP without the implicit unmapping, use |
| 29 |
// netip.AddrFromSlice. |
| 30 |
func FromStdIP(std net.IP) (ip netip.Addr, ok bool) { |
| 31 |
ret, ok := netip.AddrFromSlice(std) |
| 32 |
return ret.Unmap(), ok |
| 33 |
} |
| 34 |
|
| 35 |
// MustFromStdIP is like FromStdIP, but it panics if std is invalid. |
| 36 |
func MustFromStdIP(std net.IP) netip.Addr { |
| 37 |
ret, ok := netip.AddrFromSlice(std) |
| 38 |
if !ok { |
| 39 |
panic("not a valid IP address") |
| 40 |
} |
| 41 |
return ret.Unmap() |
| 42 |
} |
| 43 |
|
| 44 |
// FromStdIPRaw returns an IP from the standard library's IP type. |
| 45 |
// If std is invalid, ok is false. |
| 46 |
// Unlike FromStdIP, FromStdIPRaw does not do an implicit Unmap if |
| 47 |
// len(std) == 16 and contains an IPv6-mapped IPv4 address. |
| 48 |
// |
| 49 |
// Deprecated: use netip.AddrFromSlice instead. |
| 50 |
func FromStdIPRaw(std net.IP) (ip netip.Addr, ok bool) { |
| 51 |
return netip.AddrFromSlice(std) |
| 52 |
} |
| 53 |
|
| 54 |
// ParsePrefixOrAddr parses s as an IP address prefix or IP address. If s parses |
| 55 |
// as an IP address prefix, its [net/netip.Prefix.Addr] is returned. The string |
| 56 |
// s can be an IPv4 address ("192.0.2.1"), IPv6 address ("2001:db8::68"), IPv4 |
| 57 |
// prefix ("192.0.2.1/32"), or IPv6 prefix ("2001:db:68/96"). |
| 58 |
func ParsePrefixOrAddr(s string) (netip.Addr, error) { |
| 59 |
// Factored out of netip.ParsePrefix to avoid allocating an empty netip.Prefix in case it's |
| 60 |
// an address and not a prefix. |
| 61 |
i := strings.LastIndexByte(s, '/') |
| 62 |
if i < 0 { |
| 63 |
return netip.ParseAddr(s) |
| 64 |
} |
| 65 |
prefix, err := netip.ParsePrefix(s) |
| 66 |
return prefix.Addr(), err |
| 67 |
} |
| 68 |
|
| 69 |
// AddrNext returns the IP following ip. |
| 70 |
// If there is none, it returns the IP zero value. |
| 71 |
// |
| 72 |
// Deprecated: use netip.Addr.Next instead. |
| 73 |
func AddrNext(ip netip.Addr) netip.Addr { |
| 74 |
addr := u128From16(ip.As16()).addOne() |
| 75 |
if ip.Is4() { |
| 76 |
if uint32(addr.lo) == 0 { |
| 77 |
// Overflowed. |
| 78 |
return netip.Addr{} |
| 79 |
} |
| 80 |
return addr.IP4() |
| 81 |
} else { |
| 82 |
if addr.isZero() { |
| 83 |
// Overflowed |
| 84 |
return netip.Addr{} |
| 85 |
} |
| 86 |
return addr.IP6().WithZone(ip.Zone()) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// AddrPrior returns the IP before ip. |
| 91 |
// If there is none, it returns the IP zero value. |
| 92 |
// |
| 93 |
// Deprecated: use netip.Addr.Prev instead. |
| 94 |
func AddrPrior(ip netip.Addr) netip.Addr { |
| 95 |
addr := u128From16(ip.As16()) |
| 96 |
if ip.Is4() { |
| 97 |
if uint32(addr.lo) == 0 { |
| 98 |
return netip.Addr{} |
| 99 |
} |
| 100 |
return addr.subOne().IP4() |
| 101 |
} else { |
| 102 |
if addr.isZero() { |
| 103 |
return netip.Addr{} |
| 104 |
} |
| 105 |
return addr.subOne().IP6().WithZone(ip.Zone()) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// FromStdAddr maps the components of a standard library TCPAddr or |
| 110 |
// UDPAddr into an IPPort. |
| 111 |
func FromStdAddr(stdIP net.IP, port int, zone string) (_ netip.AddrPort, ok bool) { |
| 112 |
ip, ok := FromStdIP(stdIP) |
| 113 |
if !ok || port < 0 || port > math.MaxUint16 { |
| 114 |
return netip.AddrPort{}, false |
| 115 |
} |
| 116 |
ip = ip.Unmap() |
| 117 |
if zone != "" { |
| 118 |
if ip.Is4() { |
| 119 |
ok = false |
| 120 |
return |
| 121 |
} |
| 122 |
ip = ip.WithZone(zone) |
| 123 |
} |
| 124 |
return netip.AddrPortFrom(ip, uint16(port)), true |
| 125 |
} |
| 126 |
|
| 127 |
// FromStdIPNet returns an netip.Prefix from the standard library's IPNet type. |
| 128 |
// If std is invalid, ok is false. |
| 129 |
func FromStdIPNet(std *net.IPNet) (prefix netip.Prefix, ok bool) { |
| 130 |
ip, ok := FromStdIP(std.IP) |
| 131 |
if !ok { |
| 132 |
return netip.Prefix{}, false |
| 133 |
} |
| 134 |
|
| 135 |
if l := len(std.Mask); l != net.IPv4len && l != net.IPv6len { |
| 136 |
// Invalid mask. |
| 137 |
return netip.Prefix{}, false |
| 138 |
} |
| 139 |
|
| 140 |
ones, bits := std.Mask.Size() |
| 141 |
if ones == 0 && bits == 0 { |
| 142 |
// IPPrefix does not support non-contiguous masks. |
| 143 |
return netip.Prefix{}, false |
| 144 |
} |
| 145 |
|
| 146 |
return netip.PrefixFrom(ip, ones), true |
| 147 |
} |
| 148 |
|
| 149 |
// RangeOfPrefix returns the inclusive range of IPs that p covers. |
| 150 |
// |
| 151 |
// If p is zero or otherwise invalid, Range returns the zero value. |
| 152 |
func RangeOfPrefix(p netip.Prefix) IPRange { |
| 153 |
p = p.Masked() |
| 154 |
if !p.IsValid() { |
| 155 |
return IPRange{} |
| 156 |
} |
| 157 |
return IPRangeFrom(p.Addr(), PrefixLastIP(p)) |
| 158 |
} |
| 159 |
|
| 160 |
// PrefixIPNet returns the net.IPNet representation of an netip.Prefix. |
| 161 |
// The returned value is always non-nil. |
| 162 |
// Any zone identifier is dropped in the conversion. |
| 163 |
func PrefixIPNet(p netip.Prefix) *net.IPNet { |
| 164 |
if !p.IsValid() { |
| 165 |
return &net.IPNet{} |
| 166 |
} |
| 167 |
return &net.IPNet{ |
| 168 |
IP: p.Addr().AsSlice(), |
| 169 |
Mask: net.CIDRMask(p.Bits(), p.Addr().BitLen()), |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// AddrIPNet returns the net.IPNet representation of an netip.Addr |
| 174 |
// with a mask corresponding to the addresses's bit length. |
| 175 |
// The returned value is always non-nil. |
| 176 |
// Any zone identifier is dropped in the conversion. |
| 177 |
func AddrIPNet(addr netip.Addr) *net.IPNet { |
| 178 |
if !addr.IsValid() { |
| 179 |
return &net.IPNet{} |
| 180 |
} |
| 181 |
return &net.IPNet{ |
| 182 |
IP: addr.AsSlice(), |
| 183 |
Mask: net.CIDRMask(addr.BitLen(), addr.BitLen()), |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// PrefixLastIP returns the last IP in the prefix. |
| 188 |
func PrefixLastIP(p netip.Prefix) netip.Addr { |
| 189 |
if !p.IsValid() { |
| 190 |
return netip.Addr{} |
| 191 |
} |
| 192 |
a16 := p.Addr().As16() |
| 193 |
var off uint8 |
| 194 |
var bits uint8 = 128 |
| 195 |
if p.Addr().Is4() { |
| 196 |
off = 12 |
| 197 |
bits = 32 |
| 198 |
} |
| 199 |
for b := uint8(p.Bits()); b < bits; b++ { |
| 200 |
byteNum, bitInByte := b/8, 7-(b%8) |
| 201 |
a16[off+byteNum] |= 1 << uint(bitInByte) |
| 202 |
} |
| 203 |
if p.Addr().Is4() { |
| 204 |
return netip.AddrFrom16(a16).Unmap() |
| 205 |
} else { |
| 206 |
return netip.AddrFrom16(a16) // doesn't unmap |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// IPRange represents an inclusive range of IP addresses |
| 211 |
// from the same address family. |
| 212 |
// |
| 213 |
// The From and To IPs are inclusive bounds, with both included in the |
| 214 |
// range. |
| 215 |
// |
| 216 |
// To be valid, the From and To values must be non-zero, have matching |
| 217 |
// address families (IPv4 vs IPv6), and From must be less than or equal to To. |
| 218 |
// IPv6 zones are stripped out and ignored. |
| 219 |
// An invalid range may be ignored. |
| 220 |
type IPRange struct { |
| 221 |
// from is the initial IP address in the range. |
| 222 |
from netip.Addr |
| 223 |
|
| 224 |
// to is the final IP address in the range. |
| 225 |
to netip.Addr |
| 226 |
} |
| 227 |
|
| 228 |
// IPRangeFrom returns an IPRange from from to to. |
| 229 |
// It does not allocate. |
| 230 |
func IPRangeFrom(from, to netip.Addr) IPRange { |
| 231 |
return IPRange{ |
| 232 |
from: from.WithZone(""), |
| 233 |
to: to.WithZone(""), |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// From returns the lower bound of r. |
| 238 |
func (r IPRange) From() netip.Addr { return r.from } |
| 239 |
|
| 240 |
// To returns the upper bound of r. |
| 241 |
func (r IPRange) To() netip.Addr { return r.to } |
| 242 |
|
| 243 |
// ParseIPRange parses a range out of two IPs separated by a hyphen. |
| 244 |
// |
| 245 |
// It returns an error if the range is not valid. |
| 246 |
func ParseIPRange(s string) (IPRange, error) { |
| 247 |
var r IPRange |
| 248 |
h := strings.IndexByte(s, '-') |
| 249 |
if h == -1 { |
| 250 |
return r, fmt.Errorf("no hyphen in range %q", s) |
| 251 |
} |
| 252 |
from, to := s[:h], s[h+1:] |
| 253 |
var err error |
| 254 |
r.from, err = netip.ParseAddr(from) |
| 255 |
if err != nil { |
| 256 |
return r, fmt.Errorf("invalid From IP %q in range %q", from, s) |
| 257 |
} |
| 258 |
r.from = r.from.WithZone("") |
| 259 |
r.to, err = netip.ParseAddr(to) |
| 260 |
if err != nil { |
| 261 |
return r, fmt.Errorf("invalid To IP %q in range %q", to, s) |
| 262 |
} |
| 263 |
r.to = r.to.WithZone("") |
| 264 |
if !r.IsValid() { |
| 265 |
return r, fmt.Errorf("range %v to %v not valid", r.from, r.to) |
| 266 |
} |
| 267 |
return r, nil |
| 268 |
} |
| 269 |
|
| 270 |
// MustParseIPRange calls ParseIPRange(s) and panics on error. |
| 271 |
// It is intended for use in tests with hard-coded strings. |
| 272 |
func MustParseIPRange(s string) IPRange { |
| 273 |
r, err := ParseIPRange(s) |
| 274 |
if err != nil { |
| 275 |
panic(err) |
| 276 |
} |
| 277 |
return r |
| 278 |
} |
| 279 |
|
| 280 |
// String returns a string representation of the range. |
| 281 |
// |
| 282 |
// For a valid range, the form is "From-To" with a single hyphen |
| 283 |
// separating the IPs, the same format recognized by |
| 284 |
// ParseIPRange. |
| 285 |
func (r IPRange) String() string { |
| 286 |
if r.IsValid() { |
| 287 |
return fmt.Sprintf("%s-%s", r.from, r.to) |
| 288 |
} |
| 289 |
if !r.from.IsValid() || !r.to.IsValid() { |
| 290 |
return "zero IPRange" |
| 291 |
} |
| 292 |
return "invalid IPRange" |
| 293 |
} |
| 294 |
|
| 295 |
// AppendTo appends a text encoding of r, |
| 296 |
// as generated by MarshalText, |
| 297 |
// to b and returns the extended buffer. |
| 298 |
func (r IPRange) AppendTo(b []byte) []byte { |
| 299 |
if r.IsZero() { |
| 300 |
return b |
| 301 |
} |
| 302 |
b = r.from.AppendTo(b) |
| 303 |
b = append(b, '-') |
| 304 |
b = r.to.AppendTo(b) |
| 305 |
return b |
| 306 |
} |
| 307 |
|
| 308 |
// MarshalText implements the encoding.TextMarshaler interface, |
| 309 |
// The encoding is the same as returned by String, with one exception: |
| 310 |
// If ip is the zero value, the encoding is the empty string. |
| 311 |
func (r IPRange) MarshalText() ([]byte, error) { |
| 312 |
if r.IsZero() { |
| 313 |
return []byte(""), nil |
| 314 |
} |
| 315 |
var max int |
| 316 |
if r.from.Is4() { |
| 317 |
max = len("255.255.255.255-255.255.255.255") |
| 318 |
} else { |
| 319 |
max = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") |
| 320 |
} |
| 321 |
b := make([]byte, 0, max) |
| 322 |
return r.AppendTo(b), nil |
| 323 |
} |
| 324 |
|
| 325 |
// UnmarshalText implements the encoding.TextUnmarshaler interface. |
| 326 |
// The IP range is expected in a form accepted by ParseIPRange. |
| 327 |
// It returns an error if *r is not the IPRange zero value. |
| 328 |
func (r *IPRange) UnmarshalText(text []byte) error { |
| 329 |
if *r != (IPRange{}) { |
| 330 |
return errors.New("refusing to Unmarshal into non-zero IPRange") |
| 331 |
} |
| 332 |
if len(text) == 0 { |
| 333 |
return nil |
| 334 |
} |
| 335 |
var err error |
| 336 |
*r, err = ParseIPRange(string(text)) |
| 337 |
return err |
| 338 |
} |
| 339 |
|
| 340 |
// IsZero reports whether r is the zero value of the IPRange type. |
| 341 |
func (r IPRange) IsZero() bool { |
| 342 |
return r == IPRange{} |
| 343 |
} |
| 344 |
|
| 345 |
// IsValid reports whether r.From() and r.To() are both non-zero and |
| 346 |
// obey the documented requirements: address families match, and From |
| 347 |
// is less than or equal to To. |
| 348 |
func (r IPRange) IsValid() bool { |
| 349 |
return r.from.IsValid() && |
| 350 |
r.from.BitLen() == r.to.BitLen() && |
| 351 |
r.from.Zone() == r.to.Zone() && |
| 352 |
!r.to.Less(r.from) |
| 353 |
} |
| 354 |
|
| 355 |
// Valid reports whether r.From() and r.To() are both non-zero and |
| 356 |
// obey the documented requirements: address families match, and From |
| 357 |
// is less than or equal to To. |
| 358 |
// |
| 359 |
// Deprecated: use the correctly named and identical IsValid method instead. |
| 360 |
func (r IPRange) Valid() bool { return r.IsValid() } |
| 361 |
|
| 362 |
// Contains reports whether the range r includes addr. |
| 363 |
// |
| 364 |
// An invalid range always reports false. |
| 365 |
// |
| 366 |
// If ip has an IPv6 zone, Contains returns false, |
| 367 |
// because IPPrefixes strip zones. |
| 368 |
func (r IPRange) Contains(addr netip.Addr) bool { |
| 369 |
return r.IsValid() && addr.Zone() == "" && r.contains(addr) |
| 370 |
} |
| 371 |
|
| 372 |
// contains is like Contains, but without the validity check. |
| 373 |
// addr must not have a zone. |
| 374 |
func (r IPRange) contains(addr netip.Addr) bool { |
| 375 |
return r.from.Compare(addr) <= 0 && r.to.Compare(addr) >= 0 |
| 376 |
} |
| 377 |
|
| 378 |
// less reports whether r is "before" other. It is before if r.From() |
| 379 |
// is before other.From(). If they're equal, then the larger range |
| 380 |
// (higher To()) comes first. |
| 381 |
func (r IPRange) less(other IPRange) bool { |
| 382 |
if cmp := r.from.Compare(other.from); cmp != 0 { |
| 383 |
return cmp < 0 |
| 384 |
} |
| 385 |
return other.to.Less(r.to) |
| 386 |
} |
| 387 |
|
| 388 |
// entirelyBefore returns whether r lies entirely before other in IP |
| 389 |
// space. |
| 390 |
func (r IPRange) entirelyBefore(other IPRange) bool { |
| 391 |
return r.to.Less(other.from) |
| 392 |
} |
| 393 |
|
| 394 |
func lessOrEq(ip, ip2 netip.Addr) bool { return ip.Compare(ip2) <= 0 } |
| 395 |
|
| 396 |
// entirelyWithin returns whether r is entirely contained within |
| 397 |
// other. |
| 398 |
func (r IPRange) coveredBy(other IPRange) bool { |
| 399 |
return lessOrEq(other.from, r.from) && lessOrEq(r.to, other.to) |
| 400 |
} |
| 401 |
|
| 402 |
// inMiddleOf returns whether r is inside other, but not touching the |
| 403 |
// edges of other. |
| 404 |
func (r IPRange) inMiddleOf(other IPRange) bool { |
| 405 |
return other.from.Less(r.from) && r.to.Less(other.to) |
| 406 |
} |
| 407 |
|
| 408 |
// overlapsStartOf returns whether r entirely overlaps the start of |
| 409 |
// other, but not all of other. |
| 410 |
func (r IPRange) overlapsStartOf(other IPRange) bool { |
| 411 |
return lessOrEq(r.from, other.from) && r.to.Less(other.to) |
| 412 |
} |
| 413 |
|
| 414 |
// overlapsEndOf returns whether r entirely overlaps the end of |
| 415 |
// other, but not all of other. |
| 416 |
func (r IPRange) overlapsEndOf(other IPRange) bool { |
| 417 |
return other.from.Less(r.from) && lessOrEq(other.to, r.to) |
| 418 |
} |
| 419 |
|
| 420 |
// mergeIPRanges returns the minimum and sorted set of IP ranges that |
| 421 |
// cover r. |
| 422 |
func mergeIPRanges(rr []IPRange) (out []IPRange, valid bool) { |
| 423 |
// Always return a copy of r, to avoid aliasing slice memory in |
| 424 |
// the caller. |
| 425 |
switch len(rr) { |
| 426 |
case 0: |
| 427 |
return nil, true |
| 428 |
case 1: |
| 429 |
return []IPRange{rr[0]}, true |
| 430 |
} |
| 431 |
|
| 432 |
sort.Slice(rr, func(i, j int) bool { return rr[i].less(rr[j]) }) |
| 433 |
out = make([]IPRange, 1, len(rr)) |
| 434 |
out[0] = rr[0] |
| 435 |
for _, r := range rr[1:] { |
| 436 |
prev := &out[len(out)-1] |
| 437 |
switch { |
| 438 |
case !r.IsValid(): |
| 439 |
// Invalid ranges make no sense to merge, refuse to |
| 440 |
// perform. |
| 441 |
return nil, false |
| 442 |
case prev.to.Next() == r.from: |
| 443 |
// prev and r touch, merge them. |
| 444 |
// |
| 445 |
// prev r |
| 446 |
// f------tf-----t |
| 447 |
prev.to = r.to |
| 448 |
case prev.to.Less(r.from): |
| 449 |
// No overlap and not adjacent (per previous case), no |
| 450 |
// merging possible. |
| 451 |
// |
| 452 |
// prev r |
| 453 |
// f------t f-----t |
| 454 |
out = append(out, r) |
| 455 |
case prev.to.Less(r.to): |
| 456 |
// Partial overlap, update prev |
| 457 |
// |
| 458 |
// prev |
| 459 |
// f------t |
| 460 |
// f-----t |
| 461 |
// r |
| 462 |
prev.to = r.to |
| 463 |
default: |
| 464 |
// r entirely contained in prev, nothing to do. |
| 465 |
// |
| 466 |
// prev |
| 467 |
// f--------t |
| 468 |
// f-----t |
| 469 |
// r |
| 470 |
} |
| 471 |
} |
| 472 |
return out, true |
| 473 |
} |
| 474 |
|
| 475 |
// Overlaps reports whether p and o overlap at all. |
| 476 |
// |
| 477 |
// If p and o are of different address families or either are invalid, |
| 478 |
// it reports false. |
| 479 |
func (r IPRange) Overlaps(o IPRange) bool { |
| 480 |
return r.IsValid() && |
| 481 |
o.IsValid() && |
| 482 |
r.from.Compare(o.to) <= 0 && |
| 483 |
o.from.Compare(r.to) <= 0 |
| 484 |
} |
| 485 |
|
| 486 |
// prefixMaker returns a address-family-corrected IPPrefix from a and bits, |
| 487 |
// where the input bits is always in the IPv6-mapped form for IPv4 addresses. |
| 488 |
type prefixMaker func(a uint128, bits uint8) netip.Prefix |
| 489 |
|
| 490 |
// Prefixes returns the set of IPPrefix entries that covers r. |
| 491 |
// |
| 492 |
// If either of r's bounds are invalid, in the wrong order, or if |
| 493 |
// they're of different address families, then Prefixes returns nil. |
| 494 |
// |
| 495 |
// Prefixes necessarily allocates. See AppendPrefixes for a version that uses |
| 496 |
// memory you provide. |
| 497 |
func (r IPRange) Prefixes() []netip.Prefix { |
| 498 |
return r.AppendPrefixes(nil) |
| 499 |
} |
| 500 |
|
| 501 |
// AppendPrefixes is an append version of IPRange.Prefixes. It appends |
| 502 |
// the netip.Prefix entries that cover r to dst. |
| 503 |
func (r IPRange) AppendPrefixes(dst []netip.Prefix) []netip.Prefix { |
| 504 |
if !r.IsValid() { |
| 505 |
return nil |
| 506 |
} |
| 507 |
return appendRangePrefixes(dst, r.prefixFrom128AndBits, u128From16(r.from.As16()), u128From16(r.to.As16())) |
| 508 |
} |
| 509 |
|
| 510 |
func (r IPRange) prefixFrom128AndBits(a uint128, bits uint8) netip.Prefix { |
| 511 |
var ip netip.Addr |
| 512 |
if r.from.Is4() { |
| 513 |
bits -= 12 * 8 |
| 514 |
ip = a.IP4() |
| 515 |
} else { |
| 516 |
ip = a.IP6() |
| 517 |
} |
| 518 |
return netip.PrefixFrom(ip, int(bits)) |
| 519 |
} |
| 520 |
|
| 521 |
// aZeroBSet is whether, after the common bits, a is all zero bits and |
| 522 |
// b is all set (one) bits. |
| 523 |
func comparePrefixes(a, b uint128) (common uint8, aZeroBSet bool) { |
| 524 |
common = a.commonPrefixLen(b) |
| 525 |
|
| 526 |
// See whether a and b, after their common shared bits, end |
| 527 |
// in all zero bits or all one bits, respectively. |
| 528 |
if common == 128 { |
| 529 |
return common, true |
| 530 |
} |
| 531 |
|
| 532 |
m := mask6[common] |
| 533 |
return common, (a.xor(a.and(m)).isZero() && |
| 534 |
b.or(m) == uint128{^uint64(0), ^uint64(0)}) |
| 535 |
} |
| 536 |
|
| 537 |
// Prefix returns r as an IPPrefix, if it can be presented exactly as such. |
| 538 |
// If r is not valid or is not exactly equal to one prefix, ok is false. |
| 539 |
func (r IPRange) Prefix() (p netip.Prefix, ok bool) { |
| 540 |
if !r.IsValid() { |
| 541 |
return |
| 542 |
} |
| 543 |
from128 := u128From16(r.from.As16()) |
| 544 |
to128 := u128From16(r.to.As16()) |
| 545 |
if common, ok := comparePrefixes(from128, to128); ok { |
| 546 |
return r.prefixFrom128AndBits(from128, common), true |
| 547 |
} |
| 548 |
return |
| 549 |
} |
| 550 |
|
| 551 |
func appendRangePrefixes(dst []netip.Prefix, makePrefix prefixMaker, a, b uint128) []netip.Prefix { |
| 552 |
common, ok := comparePrefixes(a, b) |
| 553 |
if ok { |
| 554 |
// a to b represents a whole range, like 10.50.0.0/16. |
| 555 |
// (a being 10.50.0.0 and b being 10.50.255.255) |
| 556 |
return append(dst, makePrefix(a, common)) |
| 557 |
} |
| 558 |
// Otherwise recursively do both halves. |
| 559 |
dst = appendRangePrefixes(dst, makePrefix, a, a.bitsSetFrom(common+1)) |
| 560 |
dst = appendRangePrefixes(dst, makePrefix, b.bitsClearedFrom(common+1), b) |
| 561 |
return dst |
| 562 |
} |
| 563 |
|
| 564 |
// ComparePrefix is a compare function (returning -1, 0 or 1) |
| 565 |
// sorting prefixes first by address family (IPv4 before IPv6), |
| 566 |
// then by prefix length (smaller prefixes first), then by |
| 567 |
// address. |
| 568 |
func ComparePrefix(a, b netip.Prefix) int { |
| 569 |
aa, ba := a.Addr(), b.Addr() |
| 570 |
if al, bl := aa.BitLen(), ba.BitLen(); al != bl { |
| 571 |
if al < bl { |
| 572 |
return -1 |
| 573 |
} |
| 574 |
return 1 |
| 575 |
} |
| 576 |
ab, bb := a.Bits(), b.Bits() |
| 577 |
if ab != bb { |
| 578 |
if ab < bb { |
| 579 |
return -1 |
| 580 |
} |
| 581 |
return 1 |
| 582 |
} |
| 583 |
return aa.Compare(ba) |
| 584 |
} |