Compare commits
2 Commits
f43b8c64f4
...
71982f328f
Author | SHA1 | Date |
---|---|---|
|
71982f328f | |
|
3f57b17eb3 |
6
util.go
6
util.go
|
@ -60,7 +60,13 @@ func GetFirstAndLastIPFromCIDR(cidr string) (firstIP, lastIP net.IP, err error)
|
||||||
case net.IPv4len:
|
case net.IPv4len:
|
||||||
mask := binary.BigEndian.Uint32(subnet.Mask)
|
mask := binary.BigEndian.Uint32(subnet.Mask)
|
||||||
ip := binary.BigEndian.Uint32(subnet.IP)
|
ip := binary.BigEndian.Uint32(subnet.IP)
|
||||||
|
// To achieve the first IP address, we need to AND the IP with the mask.
|
||||||
|
// The AND operation will set all bits in the host part to 0.
|
||||||
binary.BigEndian.PutUint32(firstIP, ip&mask)
|
binary.BigEndian.PutUint32(firstIP, ip&mask)
|
||||||
|
// To achieve the last IP address, we need to OR the IP network with the inverted mask.
|
||||||
|
// The AND between the IP and the mask will set all bits in the host part to 0, keeping the network part.
|
||||||
|
// The XOR between the mask and 0xffffffff will set all bits in the host part to 1, and the network part to 0.
|
||||||
|
// The OR operation will keep the host part unchanged, and sets the host part to all 1.
|
||||||
binary.BigEndian.PutUint32(lastIP, (ip&mask)|(mask^0xffffffff))
|
binary.BigEndian.PutUint32(lastIP, (ip&mask)|(mask^0xffffffff))
|
||||||
case net.IPv6len:
|
case net.IPv6len:
|
||||||
mask1 := binary.BigEndian.Uint64(subnet.Mask[:8])
|
mask1 := binary.BigEndian.Uint64(subnet.Mask[:8])
|
||||||
|
|
Loading…
Reference in New Issue