github.com/geofffranks/garden-linux@v0.0.0-20160715111146-26c893169cfa/network/subnets/ip_util.go (about)

     1  package subnets
     2  
     3  import "net"
     4  
     5  func equals(a *net.IPNet, b *net.IPNet) bool {
     6  	aOnes, aBits := a.Mask.Size()
     7  	bOnes, bBits := b.Mask.Size()
     8  	return a.IP.Equal(b.IP) && (aOnes == bOnes) && (aBits == bBits)
     9  }
    10  
    11  func overlaps(a *net.IPNet, b *net.IPNet) bool {
    12  	return a.Contains(b.IP) || b.Contains(a.IP)
    13  }
    14  
    15  func next(ip net.IP) net.IP {
    16  	next := clone(ip)
    17  	for i := len(next) - 1; i >= 0; i-- {
    18  		next[i]++
    19  		if next[i] != 0 {
    20  			return next
    21  		}
    22  	}
    23  
    24  	panic("overflowed maximum IP")
    25  }
    26  
    27  func clone(ip net.IP) net.IP {
    28  	clone := make([]byte, len(ip))
    29  	copy(clone, ip)
    30  	return clone
    31  }
    32  
    33  func max(ipn *net.IPNet) net.IP {
    34  	mask := ipn.Mask
    35  	min := clone(ipn.IP)
    36  
    37  	if len(mask) != len(min) {
    38  		panic("length of mask is not compatible with length of network IP")
    39  	}
    40  
    41  	max := make([]byte, len(min))
    42  	for i, b := range mask {
    43  		max[i] = min[i] | ^b
    44  	}
    45  
    46  	return net.IP(max).To16()
    47  }