Skip to content

Commit

Permalink
Update get-all-ips-in-cidr.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Strong-Foundation authored Dec 27, 2024
1 parent 91e4651 commit 67c4076
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions Go/Network/get-all-ips-in-cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ import (
)

// GetIPsInCIDR returns all IP addresses in a given CIDR block.
func GetIPsInCIDR(cidr string) ([]string, error) {
func GetIPsInCIDR(cidrBlock string) ([]string, error) {
// Parse the CIDR block
_, ipNet, err := net.ParseCIDR(cidr)
_, network, err := net.ParseCIDR(cidrBlock)
if err != nil {
return nil, err
}

// Calculate the number of IPs in the CIDR block
ips := []string{}
for ip := ipNet.IP.Mask(ipNet.Mask); ipNet.Contains(ip); {
ips = append(ips, ip.String())
// Calculate the list of IPs in the CIDR block
ipAddresses := []string{}
for currentIP := network.IP.Mask(network.Mask); network.Contains(currentIP); {
ipAddresses = append(ipAddresses, currentIP.String())

// Increment the IP address by 1
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] != 0 {
for byteIndex := len(currentIP) - 1; byteIndex >= 0; byteIndex-- {
currentIP[byteIndex]++
if currentIP[byteIndex] != 0 {
break
}
}
}

return ips, nil
return ipAddresses, nil
}

func main() {
cidr := "192.168.1.0/29"
ips, err := GetIPsInCIDR(cidr)
cidrBlock := "192.168.1.0/29"
ipAddresses, err := GetIPsInCIDR(cidrBlock)
if err != nil {
fmt.Println("Error:", err)
return
}

// Print all IPs in the CIDR
for _, ip := range ips {
// Print all IP addresses in the CIDR block
for _, ip := range ipAddresses {
fmt.Println(ip)
}
}

0 comments on commit 67c4076

Please sign in to comment.