Question

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?

Was it helpful?

Solution

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]);
    }
}

OTHER TIPS

Contiki includes void uip_debug_ipaddr_print(const uip_ipaddr_t *addr); function that will do the job for you:

#include "uip.h"
#include "uip-debug.h"

...

uip_ipaddr_t addr;
uip_ip6addr_u8(&addr,
               0xaa, 0xaa, 0x00, 0x00,
               0x00, 0x00, 0x00, 0x00,
               0x02, 0x12, 0x74, 0x01,
               0x00, 0x01, 0x01, 0x01);
uip_debug_ipaddr_print(&addr);
putchar('\n');

Output:

aaaa::212:7401:1:101

In Contiki there's also uiplib_ip6addrconv function that with inverse functionality (it constructs IPv6 address object from a string).

Also, there are official guidelines how a printed IPv6 address should look like, you may want to read them: http://tools.ietf.org/html/rfc5952

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