Converting values read from proc/net/tcp6
0000000000000000FFFF00000100007F
to
::ffff:10开发者_StackOverflow社区.200.0.31
I am familiar with converting ipv4 formats : which is only 0100007F used hextoString then obtain bytes and pass it to InetAddress api and i get ip address as say 10.45.34.22 .If i follow the same procedure here the result is ::ffff:0:e895:3bc7
How to go about this
Actually, ::ffff:0:e895:3bc7
is the "official" form of this address. The IPv4-like form ::ffff:10.200.0.31
(not the same address, by the way) is only an alternative way of writing it.
How can we convert? Every pair of hexadecimal digits corresponds to one byte, as well as each of the .
-separated decimal numbers in the IPv4 format. In the IPv6 format, each :
-separated block corresponds to 16 bits (2 bytes), and one "many zeroes" sequence per address can be abbreviated with ::
.
Your 0000000000000000FFFF00000100007F
thus maps to 0000:0000:0000:0000:FFFF:0000:0100:007F
as the full format IPv6-address.
If a IPv6 address is some form of a mapped IPv4 adress, then the RFC defines an alternative format in section 5. Text Representation of Special Addresses. There the last 32 bit (e.g. last two blocks, last 4 bytes) are replaced by the decimal form, dot-separated. In our case, 0100:007F
has the bytes 0x01, 0x00, 0x00 and 0x7f, which corresponds to 1.0.0.127.
Looking at this, it looks like you have some byte-order problems. The format for IPv4-Mapped IPv6 adresses is :0:0:0:0:0:ffff:‹IPv4›
, and in your case it looks like the bytes inside each 32-bit-block got swapped. So actually your adress should be 0000:0000:0000:0000:0000:FFFF:7F00:0001
, and this can be written als ::ffff:127.0.0.1
, the well-known IPv4 localhost address, mapped to IPv4.
So my first recommendation would be to swap the bytes coming from your proc/net/tcp6 to the right order, and then construct an InetAdress from it. Maybe this already gives the address in the right (mixed) format. If not, you can take the lowest 32 bits of the output separately and either format them manually, or use the InetAdress API again, to format them as IPv4.
精彩评论