Skip to content

Commit

Permalink
refactor: func IsIPInRange updated to use net/netip pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
manoj-nutanix committed Dec 12, 2024
1 parent d5a52c1 commit 1facad3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
41 changes: 14 additions & 27 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,27 @@ package helpers

import (
"fmt"
"math/big"
"net"
"net/netip"
)

// IsIPInRange checks if the target IP falls within the start and end IP range (inclusive).
func IsIPInRange(startIP, endIP, targetIP string) (bool, error) {
// Parse the IPs
start := net.ParseIP(startIP)
end := net.ParseIP(endIP)
target := net.ParseIP(targetIP)

// Ensure all IPs are valid
if start == nil {
return false, fmt.Errorf("invalid start IP: %q", startIP)
start, err := netip.ParseAddr(startIP)
if err != nil {
return false, fmt.Errorf("invalid start IP: %w", err)
}
if end == nil {
return false, fmt.Errorf("invalid end IP: %q", endIP)
end, err := netip.ParseAddr(endIP)
if err != nil {
return false, fmt.Errorf("invalid end IP: %w", err)
}
if target == nil {
return false, fmt.Errorf("invalid target IP: %q", targetIP)
target, err := netip.ParseAddr(targetIP)
if err != nil {
return false, fmt.Errorf("invalid target IP: %w", err)
}

// Convert IPs to big integers
startInt := ipToBigInt(start)
endInt := ipToBigInt(end)
targetInt := ipToBigInt(target)

// Check if target IP is within the range
return targetInt.Cmp(startInt) >= 0 && targetInt.Cmp(endInt) <= 0, nil
}
if start.Compare(target) <= 0 && end.Compare(target) >= 0 {
return true, nil
}

// ipToBigInt converts a net.IP to a big.Int for comparison.
func ipToBigInt(ip net.IP) *big.Int {
// Normalize to 16-byte representation for both IPv4 and IPv6
ip = ip.To16()
return big.NewInt(0).SetBytes(ip)
return false, nil
}
15 changes: 12 additions & 3 deletions pkg/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,32 @@ func TestIsIPInRange(t *testing.T) {
endIP: "192.168.1.10",
targetIP: "192.168.1.5",
expectedInRange: false,
expectedErr: fmt.Errorf("invalid start IP: %q", "invalid-ip"),
expectedErr: fmt.Errorf(
"invalid start IP: ParseAddr(%q): unable to parse IP",
"invalid-ip",
),
},
{
name: "Invalid end IP",
startIP: "192.168.1.1",
endIP: "invalid-ip",
targetIP: "192.168.1.5",
expectedInRange: false,
expectedErr: fmt.Errorf("invalid end IP: %q", "invalid-ip"),
expectedErr: fmt.Errorf(
"invalid end IP: ParseAddr(%q): unable to parse IP",
"invalid-ip",
),
},
{
name: "Invalid target IP",
startIP: "192.168.1.1",
endIP: "192.168.1.10",
targetIP: "invalid-ip",
expectedInRange: false,
expectedErr: fmt.Errorf("invalid target IP: %q", "invalid-ip"),
expectedErr: fmt.Errorf(
"invalid target IP: ParseAddr(%q): unable to parse IP",
"invalid-ip",
),
},
{
name: "IPv6 range - target within range",
Expand Down

0 comments on commit 1facad3

Please sign in to comment.