Skip to content

Instantly share code, notes, and snippets.

@jaysonsantos
Created March 7, 2019 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaysonsantos/e3f7bd6390c9f0d5e378a9ad464e0120 to your computer and use it in GitHub Desktop.
Save jaysonsantos/e3f7bd6390c9f0d5e378a9ad464e0120 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
if (argc < 2) {
printf("Provide the host to be resolved\n");
return 1;
}
char *host = argv[1];
struct hostent *hp = gethostbyname(host);
if (hp == NULL)
{
fprintf(stderr, "Error resolving %s\n", host);
return 1;
}
printf("Host %s resolved to:\n", host);
for (size_t i = 0; i < hp->h_length; i++)
{
struct in_addr *addr = (struct in_addr *)hp->h_addr_list[i];
if (addr == NULL)
break;
printf("\t%s\n", inet_ntoa(*addr));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment