Created
March 25, 2011 15:30
-
-
Save drdaeman/887022 to your computer and use it in GitHub Desktop.
Possibly weird getaddrinfo() behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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