Created
December 11, 2023 20:12
-
-
Save rof20004/218cd84450352e32c962f152516014e3 to your computer and use it in GitHub Desktop.
CIDR conflicts checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
) | |
func CIDRConflict(cidr1, cidr2 string) (bool, error) { | |
_, network1, err := net.ParseCIDR(cidr1) | |
if err != nil { | |
return false, err | |
} | |
_, network2, err := net.ParseCIDR(cidr2) | |
if err != nil { | |
return false, err | |
} | |
return intersect(network1, network2), nil | |
} | |
func intersect(n1, n2 *net.IPNet) bool { | |
return n2.Contains(n1.IP) || n1.Contains(n2.IP) | |
} | |
func main() { | |
cidr1 := "100.64.0.0/16" | |
cidr2 := "100.128.0.0/16" | |
conflict, err := CIDRConflict(cidr1, cidr2) | |
if err != nil { | |
fmt.Printf("Error: %v\n", err) | |
return | |
} | |
if conflict { | |
fmt.Printf("CIDR blocks %s and %s conflict.\n", cidr1, cidr2) | |
} else { | |
fmt.Printf("CIDR blocks %s and %s do NOT conflict.\n", cidr1, cidr2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment