I have a variable in perl cgi, that stores a hex value.
My requirement is to find if this variable contains an ipv4 address or an ipv6 address.
And as per this, I need to call
inet_ntop(AF_INET, $ip); #ipv4
Or
inet_ntop(AF_INET6, $ip); #ipv6
The solution that came up in my mind is to calculate the number of bits to find out if it is an ipv4 address or an ipv6 address.
For example,
If the hex value is 0xc0a84607 (ipv4 address), the number of bits will be 32
And if the hex value is 0xfe800000000000000cd86d653903694b (ipv6 address), the number of bits will be 128.
Please note that the number of bits should be 128 even in case of hex value corresponding to valid IPv6 addresses like ::3.
I was not able to find any library function that gives me the number of bits. Is there any way to accomplish this?
Thanks in advance.