0

I need to connect to an IPv6 address. This is not hardcoded. I will obtain IPv6 addresses in byte form (char *) and they will not be retrieved using DNS (No luck with getaddrinfo). The problem is, the sockaddr_in6 structure that I'm supposed to fill out is completely different on different platforms.

If there a good portable way to connect to an IPv6 address from the raw address bytes?

I should also mention that I am using libevent also.

Matthew Mitchell
  • 5,293
  • 14
  • 70
  • 122
  • 1
    Do you really care about the entire `struct sockaddr_in6` structure, or just a `struct in6_addr`? – ephemient Jul 16 '12 at 19:45
  • I need sockaddr_in6 to connect to a IPv6 address as far as I know. Also in6_addr (which is used by sockaddr_in6) is different on varous platforms. – Matthew Mitchell Jul 16 '12 at 19:59
  • 1
    `memset` the `sockaddr_in6` to zero then set just the fields you need (presumably `.sin6_family`, `.sin6_port`, and `.sin6_addr`). POSIX specifies that `in6_addr` must contain `.s6_addr[16]` bytes, is there any other system you care about? (I guess Windows is annoying, but that's just one case.) – ephemient Jul 16 '12 at 20:13
  • OKay thanks. I might try that. So what I'll try to do is memmove the IP bytes to sin6_addr. Like `memmove(&address.sin6_addr, IP, 16);` – Matthew Mitchell Jul 16 '12 at 20:28
  • @MatthewMitchell, `memcpy()` would be a bit more efficient since it's unlikely that your `sockaddr_in6` will overlap with the raw bytes. – mpontillo Jul 17 '12 at 06:50
  • Some say always use memmove to prevent accidental problems but you are right. – Matthew Mitchell Jul 17 '12 at 17:14
  • Heh, I hadn't heard that. I guess you could just always use Java to avoid problems with pointers and buffer overflows. ;-) – mpontillo Jul 17 '12 at 19:14

0 Answers0