Skip to content

Commit aec3406

Browse files
committed
refactor: func IsIPInRange updated to use net/netip pkg
1 parent 66c1f21 commit aec3406

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

pkg/helpers/helpers.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,27 @@ package helpers
44

55
import (
66
"fmt"
7-
"math/big"
8-
"net"
7+
"net/netip"
98
)
109

1110
// IsIPInRange checks if the target IP falls within the start and end IP range (inclusive).
1211
func IsIPInRange(startIP, endIP, targetIP string) (bool, error) {
13-
// Parse the IPs
14-
start := net.ParseIP(startIP)
15-
end := net.ParseIP(endIP)
16-
target := net.ParseIP(targetIP)
17-
18-
// Ensure all IPs are valid
19-
if start == nil {
20-
return false, fmt.Errorf("invalid start IP: %q", startIP)
12+
start, err := netip.ParseAddr(startIP)
13+
if err != nil {
14+
return false, fmt.Errorf("invalid start IP: %w", err)
2115
}
22-
if end == nil {
23-
return false, fmt.Errorf("invalid end IP: %q", endIP)
16+
end, err := netip.ParseAddr(endIP)
17+
if err != nil {
18+
return false, fmt.Errorf("invalid end IP: %w", err)
2419
}
25-
if target == nil {
26-
return false, fmt.Errorf("invalid target IP: %q", targetIP)
20+
target, err := netip.ParseAddr(targetIP)
21+
if err != nil {
22+
return false, fmt.Errorf("invalid target IP: %w", err)
2723
}
2824

29-
// Convert IPs to big integers
30-
startInt := ipToBigInt(start)
31-
endInt := ipToBigInt(end)
32-
targetInt := ipToBigInt(target)
33-
34-
// Check if target IP is within the range
35-
return targetInt.Cmp(startInt) >= 0 && targetInt.Cmp(endInt) <= 0, nil
36-
}
25+
if start.Compare(target) <= 0 && end.Compare(target) >= 0 {
26+
return true, nil
27+
}
3728

38-
// ipToBigInt converts a net.IP to a big.Int for comparison.
39-
func ipToBigInt(ip net.IP) *big.Int {
40-
// Normalize to 16-byte representation for both IPv4 and IPv6
41-
ip = ip.To16()
42-
return big.NewInt(0).SetBytes(ip)
29+
return false, nil
4330
}

pkg/helpers/helpers_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,32 @@ func TestIsIPInRange(t *testing.T) {
5757
endIP: "192.168.1.10",
5858
targetIP: "192.168.1.5",
5959
expectedInRange: false,
60-
expectedErr: fmt.Errorf("invalid start IP: %q", "invalid-ip"),
60+
expectedErr: fmt.Errorf(
61+
"invalid start IP: ParseAddr(%q): unable to parse IP",
62+
"invalid-ip",
63+
),
6164
},
6265
{
6366
name: "Invalid end IP",
6467
startIP: "192.168.1.1",
6568
endIP: "invalid-ip",
6669
targetIP: "192.168.1.5",
6770
expectedInRange: false,
68-
expectedErr: fmt.Errorf("invalid end IP: %q", "invalid-ip"),
71+
expectedErr: fmt.Errorf(
72+
"invalid end IP: ParseAddr(%q): unable to parse IP",
73+
"invalid-ip",
74+
),
6975
},
7076
{
7177
name: "Invalid target IP",
7278
startIP: "192.168.1.1",
7379
endIP: "192.168.1.10",
7480
targetIP: "invalid-ip",
7581
expectedInRange: false,
76-
expectedErr: fmt.Errorf("invalid target IP: %q", "invalid-ip"),
82+
expectedErr: fmt.Errorf(
83+
"invalid target IP: ParseAddr(%q): unable to parse IP",
84+
"invalid-ip",
85+
),
7786
},
7887
{
7988
name: "IPv6 range - target within range",

0 commit comments

Comments
 (0)