Skip to content

Instantly share code, notes, and snippets.

@roolebo
Created August 8, 2019 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roolebo/1a95e11def1f19caa5bde319c2f9c89a to your computer and use it in GitHub Desktop.
Save roolebo/1a95e11def1f19caa5bde319c2f9c89a to your computer and use it in GitHub Desktop.
getnameinfo for IPv6 address on macOS
/*
* This shows somewhat special behaviour when getnameinfo on macOS converts
* an IPv6 to a kind of IPv4-translated address.
*
* It turns IPv6 address "::ffff" into "::0.0.255.255"
*/
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
int main() {
struct sockaddr_in6 sin6 = {
.sin6_len = 28,
.sin6_family = AF_INET6,
.sin6_port = 0,
.sin6_flowinfo = 0,
.sin6_addr = {
.__u6_addr = {
.__u6_addr16 = {
0,
0,
0,
0,
0,
0,
0,
0xffff,
},
},
},
.sin6_scope_id = 0,
};
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
int error;
if ((error = getnameinfo((struct sockaddr *) &sin6, sin6.sin6_len,
hbuf, sizeof(hbuf),
sbuf, sizeof(sbuf),
NI_NUMERICHOST | NI_NUMERICSERV))) {
printf("getnameinfo: %s\n", gai_strerror(error));
return 1;
}
printf("host=%s, serv=%s\n", hbuf, sbuf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment