Compare commits

..

2 Commits

Author SHA1 Message Date
Francesco Cheinasso 71982f328f
Merge 3f57b17eb3 into ef45dd3322 2024-01-12 08:47:35 +00:00
Francesco Cheinasso 3f57b17eb3 fix 2024-01-12 09:47:30 +01:00
1 changed files with 6 additions and 0 deletions

View File

@ -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])