Skip to content

Instantly share code, notes, and snippets.

@roman-yepishev
Created February 27, 2016 17:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save roman-yepishev/dadb0dc86ccc8cf31ce8 to your computer and use it in GitHub Desktop.
AF_UNSPEC resolver for localhost
#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