Question

I want to calculate the prefix length of the given IP address. I took http://escapedrabbit.com/?p=62 as reference. My Java code was to split based on colon : and mutliply by 16. But I am not sure if that is right. Please post me the right way of calculating a IPV6 address prefix length. Code Below

  System.out.println("IPV6 Length  ==> " + ""+ipv6.split(":").length*16);
Was it helpful?

Solution

You cannot determine the prefix length from just the address. For example all these prefixes are valid:

2001:db8::/32
2001:db8::/30
2001:db8::/64
2001:db8:1:b::/64

The number of : in an address doesn't tell you anything about the prefix length. Here are the same prefixes written in a different (but also completely valid) notation:

2001:db8:0:0:0:0:0:0/32
2001:db8:0:0:0:0:0:0/30
2001:db8:0:0:0:0:0:0/64
2001:db8:1:b:0:0:0:0/64

Also, the prefix length depends on where you are looking. Let's take a look at my own IPv6 address. The ISP has 2a00:8640::/29, delegates 2a00:8640:0001::/48 to me, I put 2a00:8640:0001:0000::/64 on my home LAN, and my PC has address 2a00:8640:0001:0000:3528:2df9:b368:e9e9.

My PC has this configuration: 2a00:8640:0001:0000:3528:2df9:b368:e9e9/64, which specifies an address in subnet 2a00:8640:0001:0000::/64 (more correctly written as 2a00:8640:1::/64).

As you can see you really need an explicitly specified prefix length.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top