From 090f5e030eb4737fc63adcaf23904114e62a2e36 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 11 Jul 2021 20:38:32 -0600 Subject: [PATCH 1/5] always TrimSpace of ipRange regardless of input method --- main.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 497fc44..ef7e1b3 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,6 @@ func main() { log.SetFlags(0) var ipRange string - var err error if len(os.Args) > 1 { // The range was passed in as an argument @@ -33,13 +32,11 @@ func main() { } else { // Read from standard input reader := bufio.NewReader(os.Stdin) - ipRange, err = reader.ReadString('\n') - if err != nil { - log.Fatal(err) - } - ipRange = strings.TrimSpace(ipRange) + ipRange, _ = reader.ReadString('\n') } + ipRange = strings.TrimSpace(ipRange) + spl := strings.Split(ipRange, "/") if len(spl) < 2 { log.Fatal("bad cidr") From 5f03d4c7946cc44b9eaa9d993318381eb0c6a09c Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 11 Jul 2021 20:40:28 -0600 Subject: [PATCH 2/5] trust net.ParseCIDR to validate the ipRange --- main.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/main.go b/main.go index ef7e1b3..09f4aac 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "log" "net" "os" - "strconv" "strings" "text/tabwriter" ) @@ -36,19 +35,6 @@ func main() { } ipRange = strings.TrimSpace(ipRange) - - spl := strings.Split(ipRange, "/") - if len(spl) < 2 { - log.Fatal("bad cidr") - } - r, err := strconv.Atoi(spl[1]) - if err != nil { - log.Fatal(err) - } - if r < 8 { - log.Fatal("range cannot be smaller than 8") - } - addr, network, err := net.ParseCIDR(ipRange) if err != nil { log.Fatal(err) From 054e52c4d5b96a46612e7b3f3014cdbfa534b5c7 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 11 Jul 2021 20:42:04 -0600 Subject: [PATCH 3/5] restricts to IPv4 CIDRs only --- main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.go b/main.go index 09f4aac..e8b29ac 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,11 @@ func main() { log.Fatal(err) } + ipv4 := addr.To4() + if ipv4 == nil { + log.Fatal("built for IPv4 only") + } + var ips []string for ip := addr.Mask(network.Mask); network.Contains(ip); inc(ip) { ips = append(ips, ip.String()) From e744e3ad3cdeddd3e46f5b157efec472f6db2e40 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 11 Jul 2021 20:44:45 -0600 Subject: [PATCH 4/5] more efficient direct calculation rather than looping IPs --- main.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index e8b29ac..6612c19 100644 --- a/main.go +++ b/main.go @@ -45,16 +45,23 @@ func main() { log.Fatal("built for IPv4 only") } - var ips []string - for ip := addr.Mask(network.Mask); network.Contains(ip); inc(ip) { - ips = append(ips, ip.String()) + subnetMask := network.Mask + firstIp := make([]byte, len(ipv4)) + lastIp := make([]byte, len(ipv4)) + numIPs := 0 + + for i := range ipv4 { + firstIp[i] = ipv4[i] & subnetMask[i] + lastIp[i] = ipv4[i] | (subnetMask[i] ^ 0xff) + numIPs <<= 8 + numIPs += int(subnetMask[i] ^ 0xff) } w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) fmt.Fprintf(w, "CIDR Range:\t%s\n", network) fmt.Fprintf(w, "Netmask:\t%d.%d.%d.%d\n", network.Mask[0], network.Mask[1], network.Mask[2], network.Mask[3]) - fmt.Fprintf(w, "First IP:\t%s\n", ips[0]) - fmt.Fprintf(w, "Last IP:\t%s\n", ips[len(ips)-1]) - fmt.Fprintf(w, "Addresses:\t%d\n", len(ips)) + fmt.Fprintf(w, "First IP:\t%s\n", net.IP(firstIp)) + fmt.Fprintf(w, "Last IP:\t%s\n", net.IP(lastIp)) + fmt.Fprintf(w, "Addresses:\t%d\n", numIPs+1) w.Flush() } From 30f27e7b918e604be15aac66aebded83f52c1b9f Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 11 Jul 2021 20:45:23 -0600 Subject: [PATCH 5/5] func inc is no longer required --- main.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/main.go b/main.go index 6612c19..1fd6804 100644 --- a/main.go +++ b/main.go @@ -10,15 +10,6 @@ import ( "text/tabwriter" ) -func inc(ip net.IP) { - for j := len(ip) - 1; j >= 0; j-- { - ip[j]++ - if ip[j] > 0 { - break - } - } -} - func main() { log.SetPrefix("") log.SetFlags(0)