Skip to content

Instantly share code, notes, and snippets.

@wayt
Last active June 29, 2022 17:44
Show Gist options
  • Save wayt/a04092a16fec23e14c80375c5587b539 to your computer and use it in GitHub Desktop.
Save wayt/a04092a16fec23e14c80375c5587b539 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
int main(int argc, char const *argv[]) {
struct addrinfo hints;
struct addrinfo *res, *tmp;
char host[256];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
if (argc != 2) {
fprintf(stderr, "%s string\n", argv[0]);
exit(EXIT_FAILURE);
}
int ret = getaddrinfo(argv[1], NULL, &hints, &res);
if (ret != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
exit(EXIT_FAILURE);
}
for (tmp = res; tmp != NULL; tmp = tmp->ai_next) {
getnameinfo(tmp->ai_addr, tmp->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
printf("%d => %s\n", tmp->ai_family, host);
}
freeaddrinfo(res);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment