Sumit Das
Sumit Das

Reputation: 1017

Converting an Ipv6 type address to network byte order

I have an ipv6 address in the following format

uint32_t adress6[4];

So the above array stores 4 uint32_t type data which equals 16 bytes overall and hence an ipv6 type address.

How can I convert the address stored in the above format to a network byte order?

Upvotes: 3

Views: 6656

Answers (1)

Metric Crapton
Metric Crapton

Reputation: 491

You'll need the details of how the address is laid out in the array.

Typically an IPv6 address is built with the uint32 elements in network order. The uint32 elements themselves are stored in host order.

Example:

ADDRESS: dead:beef:feed:face:cafe:babe:baad:c0de

adress6[0] = 0xdeadbeef;
adress6[1] = 0xfeedface;
adress6[2] = 0xcafebabe;
adress6[3] = 0xbaadc0de;

The array is in network order but each integer element is going to be in host order.

To get to network order you could do something like the following:

void network_order_me ( uint32_t *host_ipv6, uint32_t *net_ipv6 ) {
  net_ipv6[0] = htonl(host_ipv6[0]);
  net_ipv6[1] = htonl(host_ipv6[1]);
  net_ipv6[2] = htonl(host_ipv6[2]);
  net_ipv6[3] = htonl(host_ipv6[3]);
}

Upvotes: 6

Related Questions