Compare commits
2 Commits
71982f328f
...
f43b8c64f4
Author | SHA1 | Date |
---|---|---|
|
f43b8c64f4 | |
|
ae1b9c8df1 |
6
util.go
6
util.go
|
@ -60,13 +60,7 @@ func GetFirstAndLastIPFromCIDR(cidr string) (firstIP, lastIP net.IP, err error)
|
|||
case net.IPv4len:
|
||||
mask := binary.BigEndian.Uint32(subnet.Mask)
|
||||
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)
|
||||
// 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))
|
||||
case net.IPv6len:
|
||||
mask1 := binary.BigEndian.Uint64(subnet.Mask[:8])
|
||||
|
|
Loading…
Reference in New Issue