Skip to content

Commit

Permalink
fix: Not all IPRanges with a size of power 2 can convert to CIDR
Browse files Browse the repository at this point in the history
Signed-off-by: iiiceoo <iiiceoo@foxmail.com>
  • Loading branch information
iiiceoo committed May 10, 2024
1 parent 9cf8e48 commit f06bfbf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
unit-test:
strategy:
matrix:
go-version: [1.20.x, 1.21.x, 1.22.x]
go-version: [1.20.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
20 changes: 10 additions & 10 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ func ExampleIPRanges_Union() {
}

func ExampleIPRanges_Diff() {
ranges1, err := iprange.Parse("172.18.0.20-30", "172.18.0.1-25")
ranges1, err := iprange.Parse("172.18.0.20-30", "172.18.0.0-25")
if err != nil {
log.Fatalf("error parsing IP ranges: %v", err)
}
ranges2, err := iprange.Parse("172.18.0.5-25")
ranges2, err := iprange.Parse("172.18.0.4-26")
if err != nil {
log.Fatalf("error parsing IP ranges: %v", err)
}

fmt.Println(ranges1.Diff(ranges2))
// Output:
// [172.18.0.1/30 172.18.0.26-172.18.0.30]
// [172.18.0.0/30 172.18.0.27-172.18.0.30]
}

func ExampleIPRanges_Intersect() {
Expand All @@ -170,14 +170,14 @@ func ExampleIPRanges_Intersect() {
}

func ExampleIPRanges_Slice() {
ranges, err := iprange.Parse("172.18.0.1-5", "172.18.0.10-14")
ranges, err := iprange.Parse("172.18.0.0-3", "172.18.0.10-14")
if err != nil {
log.Fatalf("error parsing IP ranges: %v", err)
}

fmt.Println(ranges.Slice(big.NewInt(2), big.NewInt(-2)))
// Output:
// [172.18.0.3-172.18.0.5 172.18.0.10/30]
// [172.18.0.2/31 172.18.0.10-172.18.0.13]
}

func ExampleIPRanges_IsOverlap() {
Expand Down Expand Up @@ -230,7 +230,7 @@ func ExampleIPRanges_IPIterator() {
}

func ExampleIPRanges_BlockIterator() {
ranges, err := iprange.Parse("172.18.0.1-5")
ranges, err := iprange.Parse("172.18.0.0-4")
if err != nil {
log.Fatalf("error parsing IP ranges: %v", err)
}
Expand All @@ -255,10 +255,10 @@ func ExampleIPRanges_BlockIterator() {
}

// Output:
// 172.18.0.1/31
// 172.18.0.3/31
// 172.18.0.5
// 172.18.0.5
// 172.18.0.0/31
// 172.18.0.2/31
// 172.18.0.4
// 172.18.0.4
}

func ExampleIPRanges_CIDRIterator() {
Expand Down
26 changes: 15 additions & 11 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,22 @@ func (r *ipRange) String() string {
return r.start.String()
}

if inc.And(inc, dv).Sign() != 0 {
return r.start.String() + "-" + r.end.String()
}
if inc.And(inc, dv).Sign() == 0 {
bits := 32
if r.start.version() == IPv6 {
bits = 128
}

bits := 32
if r.start.version() == IPv6 {
bits = 128
}
ipNet := &net.IPNet{
IP: r.start.IP,
Mask: net.CIDRMask(bits-bl, bits),
ip := r.start.IP
mask := net.CIDRMask(bits-bl, bits)
if ip.Mask(mask).Equal(ip) {
ipNet := net.IPNet{
IP: ip,
Mask: mask,
}
return ipNet.String()
}
}

return ipNet.String()
return r.start.String() + "-" + r.end.String()
}
4 changes: 0 additions & 4 deletions ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,6 @@ func (rr *IPRanges) String() string {

// Strings returns a slice of the string representations of the IPRanges rr.
func (rr *IPRanges) Strings() []string {
if len(rr.ranges) == 0 {
return nil
}

ss := make([]string, 0, len(rr.ranges))
for _, r := range rr.ranges {
ss = append(ss, r.String())
Expand Down
2 changes: 1 addition & 1 deletion ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ var ipRangesStringsTests = []struct {
{
name: "zero",
ranges: &IPRanges{},
want: nil,
want: []string{},
},
}

Expand Down

0 comments on commit f06bfbf

Please sign in to comment.