1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
// Copyright 2020 The Inet.Af AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package netipx
import (
"fmt"
"net/netip"
"runtime"
"sort"
"strings"
)
// 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.
type IPSetBuilder struct {
// in are the ranges in the set.
in []IPRange
// out are the ranges to be removed from 'in'.
out []IPRange
// errs are errors accumulated during construction.
errs multiErr
}
// normalize normalizes s: s.in becomes the minimal sorted list of
// ranges required to describe s, and s.out becomes empty.
func (s *IPSetBuilder) normalize() {
const debug = false
if debug {
debugf("ranges start in=%v out=%v", s.in, s.out)
}
in, ok := mergeIPRanges(s.in)
if !ok {
return
}
out, ok := mergeIPRanges(s.out)
if !ok {
return
}
if debug {
debugf("ranges sort in=%v out=%v", in, out)
}
// in and out are sorted in ascending range order, and have no
// overlaps within each other. We can run a merge of the two lists
// in one pass.
min := make([]IPRange, 0, len(in))
for len(in) > 0 && len(out) > 0 {
rin, rout := in[0], out[0]
if debug {
debugf("step in=%v out=%v", rin, rout)
}
switch {
case !rout.IsValid() || !rin.IsValid():
// mergeIPRanges should have prevented invalid ranges from
// sneaking in.
panic("invalid IPRanges during Ranges merge")
case rout.entirelyBefore(rin):
// "out" is entirely before "in".
//
// out in
// f-------t f-------t
out = out[1:]
if debug {
debugf("out before in; drop out")
}
case rin.entirelyBefore(rout):
// "in" is entirely before "out".
//
// in out
// f------t f-------t
min = append(min, rin)
in = in[1:]
if debug {
debugf("in before out; append in")
debugf("min=%v", min)
}
case rin.coveredBy(rout):
// "out" entirely covers "in".
//
// out
// f-------------t
// f------t
// in
in = in[1:]
if debug {
debugf("in inside out; drop in")
}
case rout.inMiddleOf(rin):
// "in" entirely covers "out".
//
// in
// f-------------t
// f------t
// out
min = append(min, IPRange{from: rin.from, to: AddrPrior(rout.from)})
// Adjust in[0], not ir, because we want to consider the
// mutated range on the next iteration.
in[0].from = rout.to.Next()
out = out[1:]
if debug {
debugf("out inside in; split in, append first in, drop out, adjust second in")
debugf("min=%v", min)
}
case rout.overlapsStartOf(rin):
// "out" overlaps start of "in".
//
// out
// f------t
// f------t
// in
in[0].from = rout.to.Next()
// Can't move ir onto min yet, another later out might
// trim it further. Just discard or and continue.
out = out[1:]
if debug {
debugf("out cuts start of in; adjust in, drop out")
}
case rout.overlapsEndOf(rin):
// "out" overlaps end of "in".
//
// out
// f------t
// f------t
// in
min = append(min, IPRange{from: rin.from, to: AddrPrior(rout.from)})
in = in[1:]
if debug {
debugf("merge out cuts end of in; append shortened in")
debugf("min=%v", min)
}
default:
// The above should account for all combinations of in and
// out overlapping, but insert a panic to be sure.
panic("unexpected additional overlap scenario")
}
}
if len(in) > 0 {
// Ran out of removals before the end of in.
min = append(min, in...)
if debug {
debugf("min=%v", min)
}
}
s.in = min
s.out = nil
}
// Clone returns a copy of s that shares no memory with s.
func (s *IPSetBuilder) Clone() *IPSetBuilder {
return &IPSetBuilder{
in: append([]IPRange(nil), s.in...),
out: append([]IPRange(nil), s.out...),
}
}
func (s *IPSetBuilder) addError(msg string, args ...interface{}) {
se := new(stacktraceErr)
// Skip three frames: runtime.Callers, addError, and the IPSetBuilder
// method that called addError (such as IPSetBuilder.Add).
// The resulting stack trace ends at the line in the user's
// code where they called into netaddr.
n := runtime.Callers(3, se.pcs[:])
se.at = se.pcs[:n]
se.err = fmt.Errorf(msg, args...)
s.errs = append(s.errs, se)
}
// Add adds ip to s.
func (s *IPSetBuilder) Add(ip netip.Addr) {
if !ip.IsValid() {
s.addError("Add(IP{})")
return
}
s.AddRange(IPRangeFrom(ip, ip))
}
// AddPrefix adds all IPs in p to s.
func (s *IPSetBuilder) AddPrefix(p netip.Prefix) {
if r := RangeOfPrefix(p); r.IsValid() {
s.AddRange(r)
} else {
s.addError("AddPrefix(%v/%v)", p.Addr(), p.Bits())
}
}
// AddRange adds r to s.
// If r is not Valid, AddRange does nothing.
func (s *IPSetBuilder) AddRange(r IPRange) {
if !r.IsValid() {
s.addError("AddRange(%v-%v)", r.From(), r.To())
return
}
// If there are any removals (s.out), then we need to compact the set
// first to get the order right.
if len(s.out) > 0 {
s.normalize()
}
s.in = append(s.in, r)
}
// AddSet adds all IPs in b to s.
func (s *IPSetBuilder) AddSet(b *IPSet) {
if b == nil {
return
}
for _, r := range b.rr {
s.AddRange(r)
}
}
// Remove removes ip from s.
func (s *IPSetBuilder) Remove(ip netip.Addr) {
if !ip.IsValid() {
s.addError("Remove(IP{})")
} else {
s.RemoveRange(IPRangeFrom(ip, ip))
}
}
// RemovePrefix removes all IPs in p from s.
func (s *IPSetBuilder) RemovePrefix(p netip.Prefix) {
if r := RangeOfPrefix(p); r.IsValid() {
s.RemoveRange(r)
} else {
s.addError("RemovePrefix(%v/%v)", p.Addr(), p.Bits())
}
}
// RemoveRange removes all IPs in r from s.
func (s *IPSetBuilder) RemoveRange(r IPRange) {
if r.IsValid() {
s.out = append(s.out, r)
} else {
s.addError("RemoveRange(%v-%v)", r.From(), r.To())
}
}
// RemoveSet removes all IPs in o from s.
func (s *IPSetBuilder) RemoveSet(b *IPSet) {
if b == nil {
return
}
for _, r := range b.rr {
s.RemoveRange(r)
}
}
// removeBuilder removes all IPs in b from s.
func (s *IPSetBuilder) removeBuilder(b *IPSetBuilder) {
b.normalize()
for _, r := range b.in {
s.RemoveRange(r)
}
}
// Complement updates s to contain the complement of its current
// contents.
func (s *IPSetBuilder) Complement() {
s.normalize()
s.out = s.in
s.in = []IPRange{
RangeOfPrefix(netip.PrefixFrom(netip.AddrFrom4([4]byte{}), 0)),
RangeOfPrefix(netip.PrefixFrom(netip.IPv6Unspecified(), 0)),
}
}
// Intersect updates s to the set intersection of s and b.
func (s *IPSetBuilder) Intersect(b *IPSet) {
var o IPSetBuilder
o.Complement()
o.RemoveSet(b)
s.removeBuilder(&o)
}
func discardf(format string, args ...interface{}) {}
// debugf is reassigned by tests.
var debugf = discardf
// 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 (s *IPSetBuilder) IPSet() (*IPSet, error) {
s.normalize()
ret := &IPSet{
rr: append([]IPRange{}, s.in...),
}
if len(s.errs) == 0 {
return ret, nil
} else {
errs := s.errs
s.errs = nil
return ret, errs
}
}
// 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.
type IPSet struct {
// rr is the set of IPs that belong to this IPSet. The IPRanges
// are normalized according to IPSetBuilder.normalize, meaning
// they are a sorted, minimal representation (no overlapping
// ranges, no contiguous ranges). The implementation of various
// methods rely on this property.
rr []IPRange
}
// Ranges returns the minimum and sorted set of IP
// ranges that covers s.
func (s *IPSet) Ranges() []IPRange {
return append([]IPRange{}, s.rr...)
}
// Prefixes returns the minimum and sorted set of IP prefixes
// that covers s.
func (s *IPSet) Prefixes() []netip.Prefix {
out := make([]netip.Prefix, 0, len(s.rr))
for _, r := range s.rr {
out = append(out, r.Prefixes()...)
}
return out
}
// Equal reports whether s and o represent the same set of IP
// addresses.
func (s *IPSet) Equal(o *IPSet) bool {
if len(s.rr) != len(o.rr) {
return false
}
for i := range s.rr {
if s.rr[i] != o.rr[i] {
return false
}
}
return true
}
// Contains reports whether ip is in s.
// If ip has an IPv6 zone, Contains returns false,
// because IPSets do not track zones.
func (s *IPSet) Contains(ip netip.Addr) bool {
if ip.Zone() != "" {
return false
}
// TODO: data structure permitting more efficient lookups:
// https://github.com/inetaf/netaddr/issues/139
i := sort.Search(len(s.rr), func(i int) bool {
return ip.Less(s.rr[i].from)
})
if i == 0 {
return false
}
i--
return s.rr[i].contains(ip)
}
// ContainsRange reports whether all IPs in r are in s.
func (s *IPSet) ContainsRange(r IPRange) bool {
for _, x := range s.rr {
if r.coveredBy(x) {
return true
}
}
return false
}
// ContainsPrefix reports whether all IPs in p are in s.
func (s *IPSet) ContainsPrefix(p netip.Prefix) bool {
return s.ContainsRange(RangeOfPrefix(p))
}
// Overlaps reports whether any IP in b is also in s.
func (s *IPSet) Overlaps(b *IPSet) bool {
// TODO: sorted ranges lets us do this in O(n+m)
for _, r := range s.rr {
for _, or := range b.rr {
if r.Overlaps(or) {
return true
}
}
}
return false
}
// OverlapsRange reports whether any IP in r is also in s.
func (s *IPSet) OverlapsRange(r IPRange) bool {
// TODO: sorted ranges lets us do this more efficiently.
for _, x := range s.rr {
if x.Overlaps(r) {
return true
}
}
return false
}
// OverlapsPrefix reports whether any IP in p is also in s.
func (s *IPSet) OverlapsPrefix(p netip.Prefix) bool {
return s.OverlapsRange(RangeOfPrefix(p))
}
// 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.
func (s *IPSet) RemoveFreePrefix(bitLen uint8) (p netip.Prefix, newSet *IPSet, ok bool) {
var bestFit netip.Prefix
for _, r := range s.rr {
for _, prefix := range r.Prefixes() {
if uint8(prefix.Bits()) > bitLen {
continue
}
if !bestFit.Addr().IsValid() || prefix.Bits() > bestFit.Bits() {
bestFit = prefix
if uint8(bestFit.Bits()) == bitLen {
// exact match, done.
break
}
}
}
}
if !bestFit.Addr().IsValid() {
return netip.Prefix{}, s, false
}
prefix := netip.PrefixFrom(bestFit.Addr(), int(bitLen))
var b IPSetBuilder
b.AddSet(s)
b.RemovePrefix(prefix)
newSet, _ = b.IPSet()
return prefix, newSet, true
}
type multiErr []error
func (e multiErr) Error() string {
var ret []string
for _, err := range e {
ret = append(ret, err.Error())
}
return strings.Join(ret, "; ")
}
// A stacktraceErr combines an error with a stack trace.
type stacktraceErr struct {
pcs [16]uintptr // preallocated array of PCs
at []uintptr // stack trace whence the error
err error // underlying error
}
func (e *stacktraceErr) Error() string {
frames := runtime.CallersFrames(e.at)
buf := new(strings.Builder)
buf.WriteString(e.err.Error())
buf.WriteString(" @ ")
for {
frame, more := frames.Next()
if !more {
break
}
fmt.Fprintf(buf, "%s:%d ", frame.File, frame.Line)
}
return strings.TrimSpace(buf.String())
}
func (e *stacktraceErr) Unwrap() error {
return e.err
}
|