Skip to content

Commit

Permalink
Merge branch 'fix_667'
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Sep 18, 2023
2 parents b668a76 + 0cb2257 commit bb8caa9
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions pkg/nftset_utils/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ package nftset_utils
import (
"errors"
"fmt"
"github.com/google/nftables"
"go4.org/netipx"
"net/netip"
"sync"
"time"

"github.com/google/nftables"
"go4.org/netipx"
)

var (
Expand Down Expand Up @@ -113,16 +114,18 @@ func (h *NftSetHandler) AddElems(es ...netip.Prefix) error {
elems = make([]nftables.SetElement, 0, len(es))
}

for _, e := range es {
if set.Interval && !e.IsSingleIP() {
r := netipx.RangeOfPrefix(e)
start := r.From()
end := r.To()
elems = append(
elems,
nftables.SetElement{Key: start.AsSlice(), IntervalEnd: false},
nftables.SetElement{Key: end.Next().AsSlice(), IntervalEnd: true},
)
for i, e := range es {
if !e.IsValid() {
return fmt.Errorf("invalid prefix at index %d", i)
}
if set.Interval {
start := e.Masked().Addr()
elems = append(elems, nftables.SetElement{Key: start.AsSlice(), IntervalEnd: false})

end := netipx.PrefixLastIP(e).Next() // may be invalid if end is overflowed
if end.IsValid() {
elems = append(elems, nftables.SetElement{Key: end.AsSlice(), IntervalEnd: true})
}
} else {
elems = append(elems, nftables.SetElement{Key: e.Addr().AsSlice()})
}
Expand Down

0 comments on commit bb8caa9

Please sign in to comment.