Skip to content

Commit 18d432c

Browse files
committed
fix: correctly filter IPs from CIDR during aggregation
1 parent 0770c57 commit 18d432c

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

cmd/mapcidr/main.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ func process(wg *sync.WaitGroup, chancidr, outputchan chan string) {
384384
)
385385

386386
ranger, _ = ipranger.New()
387-
388387
for cidr := range chancidr {
389388
// if it's an ip turn it into a cidr
390389
if ip := net.ParseIP(cidr); ip != nil {
@@ -453,8 +452,22 @@ func process(wg *sync.WaitGroup, chancidr, outputchan chan string) {
453452
continue
454453
}
455454

456-
// In case of coalesce/shuffle we need to know all the cidrs and aggregate them by calling the proper function
457-
if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count {
455+
if len(options.FilterIP) != 0 {
456+
for _, ip := range getIPList([]*net.IPNet{pCidr}) {
457+
if options.FilterIP != nil && sliceutil.Contains(options.FilterIP, ip.String()) {
458+
continue
459+
}
460+
singleCIDR := &net.IPNet{
461+
IP: ip,
462+
Mask: net.CIDRMask(len(ip)*8, len(ip)*8),
463+
}
464+
allCidrs = append(allCidrs, singleCIDR)
465+
}
466+
if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count {
467+
_ = ranger.Add(cidr)
468+
}
469+
} else if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count {
470+
// In case of coalesce/shuffle we need to know all the cidrs and aggregate them by calling the proper function
458471
_ = ranger.Add(cidr)
459472
allCidrs = append(allCidrs, pCidr)
460473
} else {

cmd/mapcidr/main_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ func TestProcess(t *testing.T) {
220220
SortDescending: true,
221221
},
222222
expectedOutput: []string{"192.168.1.3", "192.168.1.2", "192.168.1.1", "192.168.1.0"},
223+
}, {
224+
name: "FilterIPWithAggregation",
225+
chancidr: make(chan string),
226+
outputchan: make(chan string),
227+
options: Options{
228+
FileCidr: []string{"10.0.0.0/30"},
229+
FilterIP: []string{"10.0.0.1"},
230+
Aggregate: true,
231+
},
232+
expectedOutput: []string{"10.0.0.0/32", "10.0.0.2/31"},
223233
},
224234
}
225235
var wg sync.WaitGroup

ip.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ func ipNetToRange(ipNet net.IPNet) netWithRange {
279279
firstIP = firstIP.Mask(ipNet.Mask)
280280
lastIP = lastIP.Mask(ipNet.Mask)
281281

282-
if firstIP.To4() != nil {
283-
firstIP = append(v4Mappedv6Prefix, firstIP...)
282+
if ip4 := firstIP.To4(); ip4 != nil && !(len(firstIP) == net.IPv6len && bytes.Equal(firstIP[:12], v4Mappedv6Prefix)) {
283+
firstIP = append(v4Mappedv6Prefix, ip4...)
284284
lastIP = append(v4Mappedv6Prefix, lastIP...)
285285
}
286286

0 commit comments

Comments
 (0)