Skip to content

Commit

Permalink
Merge pull request #79 from bschaatsbergen/c/document-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Dec 3, 2023
2 parents 16180f9 + 5b4897a commit 575b764
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/count.go
Expand Up @@ -46,6 +46,6 @@ func init() {
}

func count(network *net.IPNet) uint64 {
count := core.AddressCount(network)
count := core.GetAddressCount(network)
return count
}
2 changes: 1 addition & 1 deletion cmd/root.go
Expand Up @@ -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) {
err := cmd.Help()
if err != nil {
Expand Down
17 changes: 12 additions & 5 deletions pkg/core/core.go
Expand Up @@ -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
Expand All @@ -18,10 +20,11 @@ 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.
func ParseCIDR(network string) (*net.IPNet, error) {
_, ip, err := net.ParseCIDR(network)
if err != nil {
Expand All @@ -30,10 +33,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)
}
4 changes: 2 additions & 2 deletions pkg/core/core_test.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
})
}
Expand Down

0 comments on commit 575b764

Please sign in to comment.