From 1dd50c6916e29dd2512f6bcd862be3353de2a646 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 26 Feb 2025 19:30:13 +0100 Subject: [PATCH] utility iterator for ipset Signed-off-by: Kristoffer Dalby --- hscontrol/util/addr.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hscontrol/util/addr.go b/hscontrol/util/addr.go index b755a8e7..c91ef0ba 100644 --- a/hscontrol/util/addr.go +++ b/hscontrol/util/addr.go @@ -2,6 +2,7 @@ package util import ( "fmt" + "iter" "net/netip" "strings" @@ -111,3 +112,16 @@ func StringToIPPrefix(prefixes []string) ([]netip.Prefix, error) { return result, nil } + +// IPSetAddrIter returns a function that iterates over all the IPs in the IPSet. +func IPSetAddrIter(ipSet *netipx.IPSet) iter.Seq[netip.Addr] { + return func(yield func(netip.Addr) bool) { + for _, rng := range ipSet.Ranges() { + for ip := rng.From(); ip.Compare(rng.To()) <= 0; ip = ip.Next() { + if !yield(ip) { + return + } + } + } + } +}