Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document exposed functions in pkg/core and rename to GetAddressCount #79

Merged
merged 3 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/count.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading