mlocati/ip-lib

Split Subnet

iandk opened this issue · 1 comments

iandk commented

Hi,

Is there a way to split e.g. a /56 IPv6 subnet into /64 subnets?
I haven't been able to find a way to archive this in PHP.

I'd appreciate any help, thanks!

You can think of a /56 IPv6 number as a sequence of 56 fixed bits (that is, 0/1), followed by 72 variable bits:

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF????????????????????????????????????????????????????????????????????????

The same for /64 subnets: 64 fixed bits, and 64 variable bits:

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF????????????????????????????????????????????????????????????????

So, in order to represent a /56 subnet with /64 subnets, you have to declare as fixed 8 more bits (= 64-56), which gives a total of 256 (= 2⁸) subnets:

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000????????????????????????????????????????????????????????????????
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000001????????????????????????????????????????????????????????????????
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000010????????????????????????????????????????????????????????????????
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000011????????????????????????????????????????????????????????????????
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000100????????????????????????????????????????????????????????????????
[...omissis...]
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF11111011????????????????????????????????????????????????????????????????
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF11111100????????????????????????????????????????????????????????????????
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF11111101????????????????????????????????????????????????????????????????
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF11111110????????????????????????????????????????????????????????????????
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF11111111????????????????????????????????????????????????????????????????

by using the ->getStartAddress()->getBits() of a range instance, it should be rather easy to write an algorithm that does that.