3

In one terminal, I ran

$ nc -l -6 ::1 5000

In another terminal, I ran

$ cat /proc/net/tcp6

and saw this line:

   0: 00000000000000000000000001000000:1388 00000000000000000000000000000000:0000 ...

Why does that say that I'm listening on ::1:0:0:0 and not ::1?

1 Answer 1

5

If you look at the code that generates the contents on that file, you see:

    seq_printf(seq,
           "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
           "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %lu %lu %u %u %d\n",
           i,
           src->s6_addr32[0], src->s6_addr32[1],
           src->s6_addr32[2], src->s6_addr32[3], srcp,

With:

struct in6_addr {
        union {
                __u8            u6_addr8[16];
#if __UAPI_DEF_IN6_ADDR_ALT
                __be16          u6_addr16[8];
                __be32          u6_addr32[4];
#endif
        } in6_u;
#define s6_addr                 in6_u.u6_addr8
#if __UAPI_DEF_IN6_ADDR_ALT
#define s6_addr16               in6_u.u6_addr16
#define s6_addr32               in6_u.u6_addr32
#endif
};

The 128 bit IPv6 addresses are an array of 16 bytes, with the least significant bit of the address being the least significant bit of the 16th byte. But when referenced as u6_addr32, as an array of 4 32 bit numbers on a little-endian system, the least significant bit becomes the 25th bit of the 4th and last number. The bytes of that 0x01000000 32bit integer is actually stored in memory as (hex) 0x00, 0x00 , 0x00, 0x01. So the IPv6 address is stored as 0x00 x 15, 0x01.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .