Created
February 27, 2016 17:52
Star
You must be signed in to star a gist
AF_UNSPEC resolver for localhost
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 <stdio.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <string.h> | |
#include <netdb.h> | |
int main() | |
{ | |
struct addrinfo hints; | |
struct addrinfo *result; | |
memset(&hints, 0, sizeof(struct addrinfo)); | |
hints.ai_family = AF_UNSPEC; | |
hints.ai_socktype = SOCK_DGRAM; | |
int res = getaddrinfo("localhost", "7", &hints, &result); | |
if (res != 0) { | |
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res)); | |
return 1; | |
} | |
struct addrinfo *node; | |
char hostname[NI_MAXHOST]; | |
for (node = result; node != NULL; node = node->ai_next) { | |
if (node->ai_family == AF_INET) { | |
printf("Found AF_INET (%u):\n", AF_INET); | |
} else if (node->ai_family == AF_INET6) { | |
printf("Found AF_INET6 (%u):\n", AF_INET6); | |
} | |
res = getnameinfo(node->ai_addr, node->ai_addrlen, | |
hostname, NI_MAXHOST, | |
NULL, 0, NI_NUMERICHOST); | |
if (res != 0) { | |
fprintf(stderr, "gennameinfo failed: %s\n", | |
gai_strerror(res)); | |
} | |
printf("- %s\n", hostname); | |
} | |
freeaddrinfo(result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment