In net.IPNet
type we have the Contains method to check if a net.IP
is inside it. But what I want is to know if a net.IPNet
is inside another net.IPNet
. I tried a solution working with big.Int
but it didn't work (also, the function interface was terrible). Any ideas?
// ipnet2 contains ipnet1 ?
func ContainsCIDR(ipnet1, ipnet2 net.IPNet) bool {
var begin big.Int
begin.SetBytes([]byte(ipnet1.IP))
var mask big.Int
mask.SetBytes([]byte(ipnet1.Mask))
var diff big.Int
diff.Xor(&begin, &mask)
var end big.Int
end.Add(&begin, &diff)
ipBegin := net.IP(begin.Bytes())
ipEnd := net.IP(end.Bytes())
return ipnet2.Contains(ipBegin) && ipnet2.Contains(ipEnd)
}