Skip to content

Instantly share code, notes, and snippets.

@drdaeman
Created March 25, 2011 15:30
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 drdaeman/887022 to your computer and use it in GitHub Desktop.
Save drdaeman/887022 to your computer and use it in GitHub Desktop.
Possibly weird getaddrinfo() behavior
#include <netdb.h>
#include <stdio.h>
#include <string.h>
int main(void) {
struct addrinfo hints, *ai;
char buf[INET6_ADDRSTRLEN];
int ret;
/*
Excerpt from GNU getaddrinfo(3) manpage, as of 2009-09-03:
If hint.ai_flags specifies the AI_V4MAPPED flag, and hints.ai_family
was specified as AF_INET6, and no matching IPv6 addresses could be
found, then return IPv4-mapped IPv6 addresses in the list pointed
to by res.
*/
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6;
hints.ai_flags = AI_V4MAPPED;
/*
Expected: getaddrinfo succeeded: ::ffff:74.125.232.51
Result: getaddrinfo failed: No address associated with hostname
*/
ret = getaddrinfo("google.com", NULL, &hints, &ai);
if (ret != 0) {
printf("getaddrinfo failed: %s\n", gai_strerror(ret));
} else {
inet_ntop(AF_INET6,
&(((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr),
&buf, sizeof(buf));
printf("getaddrinfo succeeded: %s\n", buf);
freeaddrinfo(ai);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment