Skip to content

Instantly share code, notes, and snippets.

@mkauf
Created November 18, 2017 10:21
Show Gist options
  • Save mkauf/ee04bffabced123c990250320a52f082 to your computer and use it in GitHub Desktop.
Save mkauf/ee04bffabced123c990250320a52f082 to your computer and use it in GitHub Desktop.
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
struct addrinfo hints, *infoptr, *p;
char host[256];
int result;
memset(&hints, '\0', sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
result = getaddrinfo("127.0.0.1 www.example.com", "443", &hints, &infoptr);
if (result) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result));
exit(1);
}
for(p = infoptr; p != NULL; p = p->ai_next) {
getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
puts(host);
}
freeaddrinfo(infoptr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment