In some C code, I have a pair of IPv6 addresses,

struct in6_addr value1;
struct in6_addr value2;

I want to know if they both refer to the same address. I can't simply use the '==' operator on these structures; nor is there a single value within a structure that I can compare with '==' (unlike with IPv4).

For now I've settled on using memcmp. First like so,

#include <string.h>
memcmp(&in6_addr_a, &in6_addr_b, sizeof(struct in6_addr)) == 0

And then later I created a macro, in6_equal, to make code calling this easier to read,

#include <string.h>
#define in6_equal(in6_addr_a, in6_addr_b) (memcmp(&in6_addr_a, &in6_addr_b, sizeof(struct in6_addr)) == 0)
fail_unless(in6_equal(value1, value2), "IPv6 values not successfully matched");

Is there an official API call that I should be using to do these comparisons, or does everyone roll their own; along the above lines?

asked 24 Jan '12, 13:34

aid's gravatar image

accept rate: 5%

I believe that to be the correct way to compare 2 in6_addr structures. This easy because it is assuming address families already match (INET6) and other things like zone ID match simply because this structure does not hold those values.


answered 15 Jul '12, 22:25

Nick%20Whaley's gravatar image

Nick Whaley
accept rate: 0%

edited 15 Jul '12, 22:29

I have not had to do IPv6 address comparisons in C/C++, but that seems like a valid way to do it (and I can't think of any drawbacks)


answered 16 Jul '12, 14:57

Ryan%20Rawdon's gravatar image

Ryan Rawdon
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 24 Jan '12, 13:34

Seen: 3,991 times

Last updated: 16 Jul '12, 14:57

powered by OSQA