From 675d372a30a4087161d041db84267d1c6b2f21df Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 3 Dec 2023 02:17:55 +0100 Subject: [PATCH 1/2] chore: document core functions and rename to GetAddressCount --- cmd/count.go | 2 +- cmd/root.go | 2 +- pkg/core/core.go | 18 +++++++++++++----- pkg/core/core_test.go | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/count.go b/cmd/count.go index 44da4dd..d42d574 100644 --- a/cmd/count.go +++ b/cmd/count.go @@ -46,6 +46,6 @@ func init() { } func count(network *net.IPNet) uint64 { - count := core.AddressCount(network) + count := core.GetAddressCount(network) return count } diff --git a/cmd/root.go b/cmd/root.go index b395ba0..8975d1f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -15,7 +15,7 @@ var ( rootCmd = &cobra.Command{ Use: "cidr", Short: "cidr - CLI to perform various actions on CIDR ranges", - Version: version, // The version is set during the build by making using of `go build -ldflags` + Version: version, // The version is set during the build by making using of `go build -ldflags`. Run: func(cmd *cobra.Command, args []string) { cmd.Help() }, diff --git a/pkg/core/core.go b/pkg/core/core.go index a13c4ea..e2eec90 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -4,12 +4,14 @@ import ( "net" ) -func AddressCount(network *net.IPNet) uint64 { +// GetAddressCount returns the number of usable addresses in the given IP network. +// It considers the network type (IPv4 or IPv6) and handles edge cases for specific prefix lengths. +// The result excludes the network address and broadcast address. +func GetAddressCount(network *net.IPNet) uint64 { prefixLen, bits := network.Mask.Size() - // Check if network is IPv4 or IPv6 - if network.Mask != nil { - // Handle edge cases + // Handle edge cases for specific IPv4 prefix lengths. + if network.Mask != nil && network.IP.To4() != nil { switch prefixLen { case 32: return 1 @@ -18,10 +20,12 @@ func AddressCount(network *net.IPNet) uint64 { } } - // Remember to subtract the network address and broadcast address + // Subtract the network address and broadcast address (2) from the total number of addresses. return 1<<(uint64(bits)-uint64(prefixLen)) - 2 } +// ParseCIDR parses the given CIDR notation string and returns the corresponding IP network. +// It utilizes the net.ParseCIDR function and returns an error if parsing fails. func ParseCIDR(network string) (*net.IPNet, error) { _, ip, err := net.ParseCIDR(network) if err != nil { @@ -30,10 +34,14 @@ func ParseCIDR(network string) (*net.IPNet, error) { return ip, err } +// ContainsAddress checks if the given IP network contains the specified IP address. +// It returns true if the address is within the network, otherwise false. func ContainsAddress(network *net.IPNet, ip net.IP) bool { return network.Contains(ip) } +// Overlaps checks if there is an overlap between two IP networks. +// It returns true if there is any overlap, otherwise false. func Overlaps(network1, network2 *net.IPNet) bool { return network1.Contains(network2.IP) || network2.Contains(network1.IP) } diff --git a/pkg/core/core_test.go b/pkg/core/core_test.go index 04a7b70..54bf22c 100644 --- a/pkg/core/core_test.go +++ b/pkg/core/core_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestAddressCount(t *testing.T) { +func TestGetAddressCount(t *testing.T) { IPv4CIDR, err := ParseCIDR("10.0.0.0/16") if err != nil { t.Log(err) @@ -60,7 +60,7 @@ func TestAddressCount(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - count := AddressCount(tt.cidr) + count := GetAddressCount(tt.cidr) assert.Equal(t, int(tt.expectedCount), int(count), "Both address counts should be equal") }) } From dab15f77794ddf1010e0204ae532cce506690a05 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 3 Dec 2023 02:19:32 +0100 Subject: [PATCH 2/2] chore: remove redundant comment --- pkg/core/core.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/core/core.go b/pkg/core/core.go index e2eec90..fc3562c 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -25,7 +25,6 @@ func GetAddressCount(network *net.IPNet) uint64 { } // ParseCIDR parses the given CIDR notation string and returns the corresponding IP network. -// It utilizes the net.ParseCIDR function and returns an error if parsing fails. func ParseCIDR(network string) (*net.IPNet, error) { _, ip, err := net.ParseCIDR(network) if err != nil {