ippool.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package appc
  4. import (
  5. "errors"
  6. "net/netip"
  7. "go4.org/netipx"
  8. )
  9. // errPoolExhausted is returned when there are no more addresses to iterate over.
  10. var errPoolExhausted = errors.New("ip pool exhausted")
  11. // ippool allows for iteration over all the addresses within a netipx.IPSet.
  12. // netipx.IPSet has a Ranges call that returns the "minimum and sorted set of IP ranges that covers [the set]".
  13. // netipx.IPRange is "an inclusive range of IP addresses from the same address family.". So we can iterate over
  14. // all the addresses in the set by keeping a track of the last address we returned, calling Next on the last address
  15. // to get the new one, and if we run off the edge of the current range, starting on the next one.
  16. type ippool struct {
  17. // ranges defines the addresses in the pool
  18. ranges []netipx.IPRange
  19. // last is internal tracking of which the last address provided was.
  20. last netip.Addr
  21. // rangeIdx is internal tracking of which netipx.IPRange from the IPSet we are currently on.
  22. rangeIdx int
  23. }
  24. func newIPPool(ipset *netipx.IPSet) *ippool {
  25. if ipset == nil {
  26. return &ippool{}
  27. }
  28. return &ippool{ranges: ipset.Ranges()}
  29. }
  30. // next returns the next address from the set, or errPoolExhausted if we have
  31. // iterated over the whole set.
  32. func (ipp *ippool) next() (netip.Addr, error) {
  33. if ipp.rangeIdx >= len(ipp.ranges) {
  34. // ipset is empty or we have iterated off the end
  35. return netip.Addr{}, errPoolExhausted
  36. }
  37. if !ipp.last.IsValid() {
  38. // not initialized yet
  39. ipp.last = ipp.ranges[0].From()
  40. return ipp.last, nil
  41. }
  42. currRange := ipp.ranges[ipp.rangeIdx]
  43. if ipp.last == currRange.To() {
  44. // then we need to move to the next range
  45. ipp.rangeIdx++
  46. if ipp.rangeIdx >= len(ipp.ranges) {
  47. return netip.Addr{}, errPoolExhausted
  48. }
  49. ipp.last = ipp.ranges[ipp.rangeIdx].From()
  50. return ipp.last, nil
  51. }
  52. ipp.last = ipp.last.Next()
  53. return ipp.last, nil
  54. }