i wrote the following to print the ipv6 address of a mote in contiki-
static void
print_ipv6_addr(const uip_ipaddr_t *ip_addr) {
int i;
for (i = 0; i <= 7; i++) {
printf("%04x ", ip_addr->u16[i]);
}
}
My method prints-
aaaa 0000 0000 0000 1202 0174 0100 0101
whereas the IP address displayed by cooja is- aaaa::212:7401:1:101
.
I understand that 0000 0000 0000
is the same as ::
but why is the rest of it 'garbled'? What could i be doing wrong here?
It's an endianness problem. The uip_ipaddr_t
type is a union that stores IPv6 addresses using network byte order (i.e., big endianness), while your platform is apparently little endian.
To print the address correctly on every platform (including yours) you should access your ip_addr
variable using its u8
data member, as in the following:
static void
print_ipv6_addr(const uip_ipaddr_t *ip_addr) {
int i;
for (i = 0; i < 16; i++) {
printf("%02x", ip_addr->u8[i]);
}
}